summaryrefslogtreecommitdiff
path: root/src/SFML/Window/OSX/CursorImpl.mm
blob: ed905ef5f51bbe4468f0f484a32d8bae2ae7fd8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2018 Marco Antognini (antognini.marco@gmail.com),
//                         Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//    you must not claim that you wrote the original software.
//    If you use this software in a product, an acknowledgment
//    in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//    and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window/OSX/CursorImpl.hpp>

#import <SFML/Window/OSX/AutoreleasePoolWrapper.h>
#import <SFML/Window/OSX/NSImage+raw.h>
#import <AppKit/AppKit.h>

namespace
{

////////////////////////////////////////////////////////////
NSCursor* loadFromSelector(SEL selector)
{
    // The caller is responsible for retaining the cursor.
    if ([NSCursor respondsToSelector:selector])
        return [NSCursor performSelector:selector];
    else
        return nil;
}

}

namespace sf
{
namespace priv
{

////////////////////////////////////////////////////////////
CursorImpl::CursorImpl() :
m_cursor(nil)
{
    // Just ask for a pool
    ensureThreadHasPool();
}


////////////////////////////////////////////////////////////
CursorImpl::~CursorImpl()
{
    [m_cursor release];
}


////////////////////////////////////////////////////////////
bool CursorImpl::loadFromPixels(const Uint8* pixels, Vector2u size, Vector2u hotspot)
{
    [m_cursor release];

    NSSize   nssize    = NSMakeSize(size.x, size.y);
    NSImage* image     = [NSImage imageWithRawData:pixels andSize:nssize];
    NSPoint  nshotspot = NSMakePoint(hotspot.x, hotspot.y);

    m_cursor = [[NSCursor alloc] initWithImage:image hotSpot:nshotspot];

    return m_cursor != nil;
}

////////////////////////////////////////////////////////////
bool CursorImpl::loadFromSystem(Cursor::Type type)
{
    [m_cursor release];

    switch (type)
    {
        default: return false;

        case Cursor::Arrow:           m_cursor = [NSCursor arrowCursor];               break;
        case Cursor::Text:            m_cursor = [NSCursor IBeamCursor];               break;
        case Cursor::Hand:            m_cursor = [NSCursor pointingHandCursor];        break;
        case Cursor::SizeHorizontal:  m_cursor = [NSCursor resizeLeftRightCursor];     break;
        case Cursor::SizeVertical:    m_cursor = [NSCursor resizeUpDownCursor];        break;
        case Cursor::Cross:           m_cursor = [NSCursor crosshairCursor];           break;
        case Cursor::NotAllowed:      m_cursor = [NSCursor operationNotAllowedCursor]; break;
            
        // These cursor types are undocumented, may not be available on some platforms
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
        case Cursor::SizeBottomLeftTopRight:
            m_cursor = loadFromSelector(@selector(_windowResizeNorthEastSouthWestCursor));
            break;

        case Cursor::SizeTopLeftBottomRight:
            m_cursor = loadFromSelector(@selector(_windowResizeNorthWestSouthEastCursor));
            break;

        case Cursor::Help:
            m_cursor = loadFromSelector(@selector(_helpCursor));
            break;
#pragma clang diagnostic pop
    }

    if (m_cursor)
        [m_cursor retain];

    return m_cursor != nil;
}


} // namespace priv

} // namespace sf