]> git.mxchange.org Git - flightgear.git/blob - src/GUI/CocoaMouseCursor.mm
Explicit AppKit includes for Mac.
[flightgear.git] / src / GUI / CocoaMouseCursor.mm
1 // CocoaMouseCursor.cxx - mouse cursor using Cocoa APIs
2
3 // Copyright (C) 2013 James Turner <zakalawe@mac.com>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 //
19
20 #include "CocoaMouseCursor.hxx"
21
22 #include <AppKit/NSImage.h>
23 #include <AppKit/NSCursor.h>
24
25 #include <map>
26
27 #include <Main/globals.hxx>
28 #include <GUI/CocoaHelpers_private.h>
29
30 class CocoaMouseCursor::CocoaMouseCursorPrivate
31 {
32 public:
33     Cursor activeCursorKey;
34     
35     typedef std::map<Cursor, NSCursor*> CursorMap;
36     CursorMap cursors;
37 };
38
39 NSCursor* cocoaCursorForKey(FGMouseCursor::Cursor aKey)
40 {
41     NSImage* img = nil;
42     
43     NSString* path = [NSString stringWithCString:globals->get_fg_root().c_str()
44                                             encoding:NSUTF8StringEncoding];
45     path = [path stringByAppendingPathComponent:@"gui"];
46     
47     switch (aKey) {
48     case FGMouseCursor::CURSOR_HAND: return [NSCursor pointingHandCursor];
49     case FGMouseCursor::CURSOR_CROSSHAIR: return [NSCursor crosshairCursor];
50     case FGMouseCursor::CURSOR_IBEAM: return [NSCursor IBeamCursor];
51     case FGMouseCursor::CURSOR_CLOSED_HAND: return [NSCursor closedHandCursor];
52             
53     // FIXME - use a proper left-right cursor here.
54     case FGMouseCursor::CURSOR_LEFT_RIGHT: return [NSCursor resizeLeftRightCursor];
55     case FGMouseCursor::CURSOR_UP_DOWN: return [NSCursor resizeUpDownCursor];
56             
57     case FGMouseCursor::CURSOR_SPIN_CW:
58         path = [path stringByAppendingPathComponent:@"cursor-spin-cw.png"];
59         img = [[NSImage alloc] initWithContentsOfFile:path];
60         return [[NSCursor alloc] initWithImage:img hotSpot:NSMakePoint(16,16)];
61             
62     case FGMouseCursor::CURSOR_SPIN_CCW:
63         path = [path stringByAppendingPathComponent:@"cursor-spin-cw.png"];
64         img = [[NSImage alloc] initWithContentsOfFile:path];
65         return [[NSCursor alloc] initWithImage:img hotSpot:NSMakePoint(16,16)];
66             
67     default: return [NSCursor arrowCursor];
68     }
69
70 }
71
72 CocoaMouseCursor::CocoaMouseCursor() :
73     d(new CocoaMouseCursorPrivate)
74 {
75     
76 }
77
78 CocoaMouseCursor::~CocoaMouseCursor()
79 {
80     
81     
82 }
83
84
85 void CocoaMouseCursor::setCursor(Cursor aCursor)
86 {
87     if (aCursor == d->activeCursorKey) {
88         return;
89     }
90  
91     CocoaAutoreleasePool pool;   
92     d->activeCursorKey = aCursor;
93     if (d->cursors.find(aCursor) == d->cursors.end()) {
94         d->cursors[aCursor] = cocoaCursorForKey(aCursor);
95         [d->cursors[aCursor] retain];
96     }
97     
98     [d->cursors[aCursor] set];
99 }
100
101 void CocoaMouseCursor::setCursorVisible(bool aVis)
102 {
103     CocoaAutoreleasePool pool;
104     if (aVis) {
105         [NSCursor unhide];
106     } else {
107         [NSCursor hide];
108     }
109 }
110
111 void CocoaMouseCursor::hideCursorUntilMouseMove()
112 {
113     [NSCursor setHiddenUntilMouseMoves:YES];
114 }
115
116 void CocoaMouseCursor::mouseMoved()
117 {
118     // no-op
119 }