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