1 // CocoaMouseCursor.cxx - mouse cursor using Cocoa APIs
3 // Copyright (C) 2013 James Turner <zakalawe@mac.com>
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.
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.
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.
20 #include "CocoaMouseCursor.hxx"
22 #include <Foundation/NSPathUtilities.h>
23 #include <AppKit/NSImage.h>
24 #include <AppKit/NSCursor.h>
28 #include <Main/globals.hxx>
29 #include <GUI/CocoaHelpers_private.h>
31 class CocoaMouseCursor::CocoaMouseCursorPrivate
34 Cursor activeCursorKey;
36 typedef std::map<Cursor, NSCursor*> CursorMap;
40 NSCursor* cocoaCursorForKey(FGMouseCursor::Cursor aKey)
44 NSString* path = [NSString stringWithCString:globals->get_fg_root().c_str()
45 encoding:NSUTF8StringEncoding];
46 path = [path stringByAppendingPathComponent:@"gui"];
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];
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];
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)];
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)];
68 default: return [NSCursor arrowCursor];
73 CocoaMouseCursor::CocoaMouseCursor() :
74 d(new CocoaMouseCursorPrivate)
79 CocoaMouseCursor::~CocoaMouseCursor()
86 void CocoaMouseCursor::setCursor(Cursor aCursor)
88 if (aCursor == d->activeCursorKey) {
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];
99 [d->cursors[aCursor] set];
102 void CocoaMouseCursor::setCursorVisible(bool aVis)
104 CocoaAutoreleasePool pool;
112 void CocoaMouseCursor::hideCursorUntilMouseMove()
114 [NSCursor setHiddenUntilMouseMoves:YES];
117 void CocoaMouseCursor::mouseMoved()