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