1/*
2 * Copyright (C) 2004, 2006, 2008 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "Image.h"
29#include "IntPoint.h"
30#include <wtf/Assertions.h>
31#include <wtf/RefPtr.h>
32
33#if PLATFORM(WIN)
34typedef struct HICON__* HICON;
35typedef HICON HCURSOR;
36#include <wtf/RefCounted.h>
37#elif PLATFORM(COCOA)
38#include <wtf/RetainPtr.h>
39#elif PLATFORM(GTK)
40#include "GRefPtrGtk.h"
41#endif
42
43#if USE(APPKIT)
44OBJC_CLASS NSCursor;
45#endif
46
47#if PLATFORM(WIN)
48typedef struct HICON__ *HICON;
49typedef HICON HCURSOR;
50#endif
51
52namespace WebCore {
53
54class Image;
55
56#if PLATFORM(WIN)
57
58class SharedCursor : public RefCounted<SharedCursor> {
59public:
60 static Ref<SharedCursor> create(HCURSOR);
61 WEBCORE_EXPORT ~SharedCursor();
62 HCURSOR nativeCursor() const { return m_nativeCursor; }
63
64private:
65 SharedCursor(HCURSOR);
66 HCURSOR m_nativeCursor;
67};
68
69#endif
70
71#if PLATFORM(WIN)
72using PlatformCursor = RefPtr<SharedCursor>;
73#elif USE(APPKIT)
74using PlatformCursor = NSCursor *;
75#elif PLATFORM(GTK)
76using PlatformCursor = GRefPtr<GdkCursor>;
77#else
78using PlatformCursor = void*;
79#endif
80
81class Cursor {
82 WTF_MAKE_FAST_ALLOCATED;
83public:
84 enum Type {
85 Pointer = 0,
86 Cross,
87 Hand,
88 IBeam,
89 Wait,
90 Help,
91 EastResize,
92 NorthResize,
93 NorthEastResize,
94 NorthWestResize,
95 SouthResize,
96 SouthEastResize,
97 SouthWestResize,
98 WestResize,
99 NorthSouthResize,
100 EastWestResize,
101 NorthEastSouthWestResize,
102 NorthWestSouthEastResize,
103 ColumnResize,
104 RowResize,
105 MiddlePanning,
106 EastPanning,
107 NorthPanning,
108 NorthEastPanning,
109 NorthWestPanning,
110 SouthPanning,
111 SouthEastPanning,
112 SouthWestPanning,
113 WestPanning,
114 Move,
115 VerticalText,
116 Cell,
117 ContextMenu,
118 Alias,
119 Progress,
120 NoDrop,
121 Copy,
122 None,
123 NotAllowed,
124 ZoomIn,
125 ZoomOut,
126 Grab,
127 Grabbing,
128 Custom
129 };
130
131 Cursor() = default;
132
133#if !PLATFORM(IOS_FAMILY)
134
135 WEBCORE_EXPORT static const Cursor& fromType(Cursor::Type);
136
137 WEBCORE_EXPORT Cursor(Image*, const IntPoint& hotSpot);
138
139#if ENABLE(MOUSE_CURSOR_SCALE)
140 // Hot spot is in image pixels.
141 WEBCORE_EXPORT Cursor(Image*, const IntPoint& hotSpot, float imageScaleFactor);
142#endif
143
144 explicit Cursor(Type);
145
146 Type type() const;
147 Image* image() const { return m_image.get(); }
148 const IntPoint& hotSpot() const { return m_hotSpot; }
149
150#if ENABLE(MOUSE_CURSOR_SCALE)
151 // Image scale in image pixels per logical (UI) pixel.
152 float imageScaleFactor() const { return m_imageScaleFactor; }
153#endif
154
155 WEBCORE_EXPORT PlatformCursor platformCursor() const;
156
157private:
158 void ensurePlatformCursor() const;
159
160 // The type of -1 indicates an invalid Cursor that should never actually get used.
161 Type m_type { static_cast<Type>(-1) };
162 RefPtr<Image> m_image;
163 IntPoint m_hotSpot;
164
165#if ENABLE(MOUSE_CURSOR_SCALE)
166 float m_imageScaleFactor { 1 };
167#endif
168
169#if !USE(APPKIT)
170 mutable PlatformCursor m_platformCursor { nullptr };
171#else
172 mutable RetainPtr<NSCursor> m_platformCursor;
173#endif
174
175#endif // !PLATFORM(IOS_FAMILY)
176};
177
178IntPoint determineHotSpot(Image*, const IntPoint& specifiedHotSpot);
179
180WEBCORE_EXPORT const Cursor& pointerCursor();
181const Cursor& crossCursor();
182WEBCORE_EXPORT const Cursor& handCursor();
183const Cursor& moveCursor();
184WEBCORE_EXPORT const Cursor& iBeamCursor();
185const Cursor& waitCursor();
186const Cursor& helpCursor();
187const Cursor& eastResizeCursor();
188const Cursor& northResizeCursor();
189const Cursor& northEastResizeCursor();
190const Cursor& northWestResizeCursor();
191const Cursor& southResizeCursor();
192const Cursor& southEastResizeCursor();
193const Cursor& southWestResizeCursor();
194const Cursor& westResizeCursor();
195const Cursor& northSouthResizeCursor();
196const Cursor& eastWestResizeCursor();
197const Cursor& northEastSouthWestResizeCursor();
198const Cursor& northWestSouthEastResizeCursor();
199const Cursor& columnResizeCursor();
200const Cursor& rowResizeCursor();
201const Cursor& middlePanningCursor();
202const Cursor& eastPanningCursor();
203const Cursor& northPanningCursor();
204const Cursor& northEastPanningCursor();
205const Cursor& northWestPanningCursor();
206const Cursor& southPanningCursor();
207const Cursor& southEastPanningCursor();
208const Cursor& southWestPanningCursor();
209const Cursor& westPanningCursor();
210const Cursor& verticalTextCursor();
211const Cursor& cellCursor();
212const Cursor& contextMenuCursor();
213const Cursor& noDropCursor();
214const Cursor& notAllowedCursor();
215const Cursor& progressCursor();
216const Cursor& aliasCursor();
217const Cursor& zoomInCursor();
218const Cursor& zoomOutCursor();
219const Cursor& copyCursor();
220const Cursor& noneCursor();
221const Cursor& grabCursor();
222const Cursor& grabbingCursor();
223
224#if !PLATFORM(IOS_FAMILY)
225
226inline Cursor::Type Cursor::type() const
227{
228 ASSERT(m_type >= 0);
229 ASSERT(m_type <= Custom);
230 return m_type;
231}
232
233#endif
234
235} // namespace WebCore
236