]> git.mxchange.org Git - flightgear.git/blob - src/GUI/CocoaMouseCursor.mm
Refactor aircraft helper classes
[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 <Cocoa/Cocoa.h>
23 #include <map>
24
25 #include <Main/globals.hxx>
26 #include <GUI/CocoaHelpers_private.h>
27
28 class CocoaMouseCursor::CocoaMouseCursorPrivate
29 {
30 public:
31     Cursor activeCursorKey;
32     
33     typedef std::map<Cursor, NSCursor*> CursorMap;
34     CursorMap cursors;
35 };
36
37 NSCursor* cocoaCursorForKey(FGMouseCursor::Cursor aKey)
38 {
39     NSImage* img = nil;
40     
41     NSString* path = [NSString stringWithCString:globals->get_fg_root().c_str()
42                                             encoding:NSUTF8StringEncoding];
43     path = [path stringByAppendingPathComponent:@"gui"];
44     
45     switch (aKey) {
46     case FGMouseCursor::CURSOR_HAND: return [NSCursor pointingHandCursor];
47     case FGMouseCursor::CURSOR_CROSSHAIR: return [NSCursor crosshairCursor];
48     case FGMouseCursor::CURSOR_IBEAM: return [NSCursor IBeamCursor];
49     case FGMouseCursor::CURSOR_CLOSED_HAND: return [NSCursor closedHandCursor];
50             
51     // FIXME - use a proper left-right cursor here.
52     case FGMouseCursor::CURSOR_LEFT_RIGHT: return [NSCursor resizeLeftRightCursor];
53     case FGMouseCursor::CURSOR_UP_DOWN: return [NSCursor resizeUpDownCursor];
54             
55     case FGMouseCursor::CURSOR_SPIN_CW:
56         path = [path stringByAppendingPathComponent:@"cursor-spin-cw.png"];
57         img = [[NSImage alloc] initWithContentsOfFile:path];
58         return [[NSCursor alloc] initWithImage:img hotSpot:NSMakePoint(16,16)];
59             
60     case FGMouseCursor::CURSOR_SPIN_CCW:
61         path = [path stringByAppendingPathComponent:@"cursor-spin-cw.png"];
62         img = [[NSImage alloc] initWithContentsOfFile:path];
63         return [[NSCursor alloc] initWithImage:img hotSpot:NSMakePoint(16,16)];
64             
65     default: return [NSCursor arrowCursor];
66     }
67
68 }
69
70 CocoaMouseCursor::CocoaMouseCursor() :
71     d(new CocoaMouseCursorPrivate)
72 {
73     
74 }
75
76 CocoaMouseCursor::~CocoaMouseCursor()
77 {
78     
79     
80 }
81
82
83 void CocoaMouseCursor::setCursor(Cursor aCursor)
84 {
85     if (aCursor == d->activeCursorKey) {
86         return;
87     }
88  
89     CocoaAutoreleasePool pool;   
90     d->activeCursorKey = aCursor;
91     if (d->cursors.find(aCursor) == d->cursors.end()) {
92         d->cursors[aCursor] = cocoaCursorForKey(aCursor);
93         [d->cursors[aCursor] retain];
94     }
95     
96     [d->cursors[aCursor] set];
97 }
98
99 void CocoaMouseCursor::setCursorVisible(bool aVis)
100 {
101     CocoaAutoreleasePool pool;
102     if (aVis) {
103         [NSCursor unhide];
104     } else {
105         [NSCursor hide];
106     }
107 }
108
109 void CocoaMouseCursor::hideCursorUntilMouseMove()
110 {
111     [NSCursor setHiddenUntilMouseMoves:YES];
112 }
113
114 void CocoaMouseCursor::mouseMoved()
115 {
116     // no-op
117 }