| 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) |
| 34 | typedef struct HICON__* HICON; |
| 35 | typedef 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) |
| 44 | OBJC_CLASS NSCursor; |
| 45 | #endif |
| 46 | |
| 47 | #if PLATFORM(WIN) |
| 48 | typedef struct HICON__ *HICON; |
| 49 | typedef HICON HCURSOR; |
| 50 | #endif |
| 51 | |
| 52 | namespace WebCore { |
| 53 | |
| 54 | class Image; |
| 55 | |
| 56 | #if PLATFORM(WIN) |
| 57 | |
| 58 | class SharedCursor : public RefCounted<SharedCursor> { |
| 59 | public: |
| 60 | static Ref<SharedCursor> create(HCURSOR); |
| 61 | WEBCORE_EXPORT ~SharedCursor(); |
| 62 | HCURSOR nativeCursor() const { return m_nativeCursor; } |
| 63 | |
| 64 | private: |
| 65 | SharedCursor(HCURSOR); |
| 66 | HCURSOR m_nativeCursor; |
| 67 | }; |
| 68 | |
| 69 | #endif |
| 70 | |
| 71 | #if PLATFORM(WIN) |
| 72 | using PlatformCursor = RefPtr<SharedCursor>; |
| 73 | #elif USE(APPKIT) |
| 74 | using PlatformCursor = NSCursor *; |
| 75 | #elif PLATFORM(GTK) |
| 76 | using PlatformCursor = GRefPtr<GdkCursor>; |
| 77 | #else |
| 78 | using PlatformCursor = void*; |
| 79 | #endif |
| 80 | |
| 81 | class Cursor { |
| 82 | WTF_MAKE_FAST_ALLOCATED; |
| 83 | public: |
| 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 | , |
| 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 | |
| 157 | private: |
| 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 | |
| 178 | IntPoint determineHotSpot(Image*, const IntPoint& specifiedHotSpot); |
| 179 | |
| 180 | WEBCORE_EXPORT const Cursor& pointerCursor(); |
| 181 | const Cursor& crossCursor(); |
| 182 | WEBCORE_EXPORT const Cursor& handCursor(); |
| 183 | const Cursor& moveCursor(); |
| 184 | WEBCORE_EXPORT const Cursor& iBeamCursor(); |
| 185 | const Cursor& waitCursor(); |
| 186 | const Cursor& helpCursor(); |
| 187 | const Cursor& eastResizeCursor(); |
| 188 | const Cursor& northResizeCursor(); |
| 189 | const Cursor& northEastResizeCursor(); |
| 190 | const Cursor& northWestResizeCursor(); |
| 191 | const Cursor& southResizeCursor(); |
| 192 | const Cursor& southEastResizeCursor(); |
| 193 | const Cursor& southWestResizeCursor(); |
| 194 | const Cursor& westResizeCursor(); |
| 195 | const Cursor& northSouthResizeCursor(); |
| 196 | const Cursor& eastWestResizeCursor(); |
| 197 | const Cursor& northEastSouthWestResizeCursor(); |
| 198 | const Cursor& northWestSouthEastResizeCursor(); |
| 199 | const Cursor& columnResizeCursor(); |
| 200 | const Cursor& rowResizeCursor(); |
| 201 | const Cursor& middlePanningCursor(); |
| 202 | const Cursor& eastPanningCursor(); |
| 203 | const Cursor& northPanningCursor(); |
| 204 | const Cursor& northEastPanningCursor(); |
| 205 | const Cursor& northWestPanningCursor(); |
| 206 | const Cursor& southPanningCursor(); |
| 207 | const Cursor& southEastPanningCursor(); |
| 208 | const Cursor& southWestPanningCursor(); |
| 209 | const Cursor& westPanningCursor(); |
| 210 | const Cursor& verticalTextCursor(); |
| 211 | const Cursor& cellCursor(); |
| 212 | const Cursor& (); |
| 213 | const Cursor& noDropCursor(); |
| 214 | const Cursor& notAllowedCursor(); |
| 215 | const Cursor& progressCursor(); |
| 216 | const Cursor& aliasCursor(); |
| 217 | const Cursor& zoomInCursor(); |
| 218 | const Cursor& zoomOutCursor(); |
| 219 | const Cursor& copyCursor(); |
| 220 | const Cursor& noneCursor(); |
| 221 | const Cursor& grabCursor(); |
| 222 | const Cursor& grabbingCursor(); |
| 223 | |
| 224 | #if !PLATFORM(IOS_FAMILY) |
| 225 | |
| 226 | inline 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 | |