| 1 |
// CocoaMouseCursor.cxx - mouse cursor using Cocoa APIs |
| 2 |
|
| 3 |
// Copyright (C) 2013 James Turner <[email protected]> |
| 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 |
#include "MouseCursor.hxx" |
| 22 |
|
| 23 |
#include <Foundation/NSPathUtilities.h> |
| 24 |
#include <AppKit/NSImage.h> |
| 25 |
#include <AppKit/NSCursor.h> |
| 26 |
|
| 27 |
#include <map> |
| 28 |
|
| 29 |
#include <Main/globals.hxx> |
| 30 |
#include <GUI/CocoaHelpers_private.h> |
| 31 |
|
| 32 |
class CocoaMouseCursor::CocoaMouseCursorPrivate |
| 33 |
{ |
| 34 |
public: |
| 35 |
Cursor activeCursorKey; |
| 36 |
|
| 37 |
typedef std::map<Cursor, NSCursor*> CursorMap; |
| 38 |
CursorMap cursors; |
| 39 |
}; |
| 40 |
|
| 41 |
NSCursor* cocoaCursorForKey(FGMouseCursor::Cursor aKey) |
| 42 |
{ |
| 43 |
NSImage* img = nil; |
| 44 |
|
| 45 |
std::string p = globals->get_fg_root().utf8Str(); |
| 46 |
NSString* path = [NSString stringWithCString:p.c_str() encoding:NSUTF8StringEncoding]; |
| 47 |
path = [path stringByAppendingPathComponent:@"gui"]; |
| 48 |
|
| 49 |
switch (aKey) { |
| 50 |
case FGMouseCursor::CURSOR_HAND: return [NSCursor pointingHandCursor]; |
| 51 |
case FGMouseCursor::CURSOR_CROSSHAIR: return [NSCursor crosshairCursor]; |
| 52 |
case FGMouseCursor::CURSOR_IBEAM: return [NSCursor IBeamCursor]; |
| 53 |
case FGMouseCursor::CURSOR_CLOSED_HAND: return [NSCursor closedHandCursor]; |
| 54 |
|
| 55 |
// FIXME - use a proper left-right cursor here. |
| 56 |
case FGMouseCursor::CURSOR_LEFT_RIGHT: return [NSCursor resizeLeftRightCursor]; |
| 57 |
case FGMouseCursor::CURSOR_UP_DOWN: return [NSCursor resizeUpDownCursor]; |
| 58 |
|
| 59 |
case FGMouseCursor::CURSOR_LEFT_SIDE: |
| 60 |
return [NSCursor resizeLeftCursor]; |
| 61 |
case FGMouseCursor::CURSOR_RIGHT_SIDE: |
| 62 |
return [NSCursor resizeRightCursor]; |
| 63 |
case FGMouseCursor::CURSOR_TOP_SIDE: |
| 64 |
return [NSCursor resizeUpCursor]; |
| 65 |
case FGMouseCursor::CURSOR_BOTTOM_SIDE: |
| 66 |
return [NSCursor resizeDownCursor]; |
| 67 |
|
| 68 |
case FGMouseCursor::CURSOR_TOP_LEFT: |
| 69 |
return [NSCursor _topLeftResizeCursor]; |
| 70 |
case FGMouseCursor::CURSOR_TOP_RIGHT: |
| 71 |
return [NSCursor _topRightResizeCursor]; |
| 72 |
case FGMouseCursor::CURSOR_BOTTOM_LEFT: |
| 73 |
return [NSCursor _bottomLeftResizeCursor]; |
| 74 |
case FGMouseCursor::CURSOR_BOTTOM_RIGHT: |
| 75 |
return [NSCursor _bottomRightResizeCursor]; |
| 76 |
|
| 77 |
case FGMouseCursor::CURSOR_WAIT: |
| 78 |
return [NSCursor _waitCursor]; |
| 79 |
|
| 80 |
case FGMouseCursor::CURSOR_SPIN_CW: |
| 81 |
path = [path stringByAppendingPathComponent:@"cursor-spin-cw.png"]; |
| 82 |
img = [[NSImage alloc] initWithContentsOfFile:path]; |
| 83 |
return [[NSCursor alloc] initWithImage:img hotSpot:NSMakePoint(16,16)]; |
| 84 |
|
| 85 |
case FGMouseCursor::CURSOR_SPIN_CCW: |
| 86 |
path = [path stringByAppendingPathComponent:@"cursor-spin-cw.png"]; |
| 87 |
img = [[NSImage alloc] initWithContentsOfFile:path]; |
| 88 |
return [[NSCursor alloc] initWithImage:img hotSpot:NSMakePoint(16,16)]; |
| 89 |
|
| 90 |
default: return [NSCursor arrowCursor]; |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
CocoaMouseCursor::CocoaMouseCursor() : |
| 96 |
d(new CocoaMouseCursorPrivate) |
| 97 |
{ |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
CocoaMouseCursor::~CocoaMouseCursor() |
| 102 |
{ |
| 103 |
|
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
void CocoaMouseCursor::setCursor(Cursor aCursor) |
| 109 |
{ |
| 110 |
if (aCursor == d->activeCursorKey) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
m_currentCursor = aCursor; |
| 115 |
|
| 116 |
CocoaAutoreleasePool pool; |
| 117 |
d->activeCursorKey = aCursor; |
| 118 |
if (d->cursors.find(aCursor) == d->cursors.end()) { |
| 119 |
d->cursors[aCursor] = cocoaCursorForKey(aCursor); |
| 120 |
[d->cursors[aCursor] retain]; |
| 121 |
} |
| 122 |
|
| 123 |
[d->cursors[aCursor] set]; |
| 124 |
} |
| 125 |
|
| 126 |
void CocoaMouseCursor::setCursorVisible(bool aVis) |
| 127 |
{ |
| 128 |
CocoaAutoreleasePool pool; |
| 129 |
if (aVis) { |
| 130 |
[NSCursor unhide]; |
| 131 |
} else { |
| 132 |
[NSCursor hide]; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
void CocoaMouseCursor::hideCursorUntilMouseMove() |
| 137 |
{ |
| 138 |
[NSCursor setHiddenUntilMouseMoves:YES]; |
| 139 |
} |
| 140 |
|
| 141 |
void CocoaMouseCursor::mouseMoved() |
| 142 |
{ |
| 143 |
// no-op |
| 144 |
} |