2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "platform/Cursor.h"
31 IntPoint determineHotSpot(Image* image, const IntPoint& specifiedHotSpot)
36 // Hot spot must be inside cursor rectangle.
37 IntRect imageRect = image->rect();
38 if (imageRect.contains(specifiedHotSpot))
39 return specifiedHotSpot;
41 // If hot spot is not specified externally, it can be extracted from some image formats (e.g. .cur).
42 IntPoint intrinsicHotSpot;
43 bool imageHasIntrinsicHotSpot = image->getHotSpot(intrinsicHotSpot);
44 if (imageHasIntrinsicHotSpot && imageRect.contains(intrinsicHotSpot))
45 return intrinsicHotSpot;
50 const Cursor& Cursor::fromType(Cursor::Type type)
54 return pointerCursor();
65 case Cursor::EastResize:
66 return eastResizeCursor();
67 case Cursor::NorthResize:
68 return northResizeCursor();
69 case Cursor::NorthEastResize:
70 return northEastResizeCursor();
71 case Cursor::NorthWestResize:
72 return northWestResizeCursor();
73 case Cursor::SouthResize:
74 return southResizeCursor();
75 case Cursor::SouthEastResize:
76 return southEastResizeCursor();
77 case Cursor::SouthWestResize:
78 return southWestResizeCursor();
79 case Cursor::WestResize:
80 return westResizeCursor();
81 case Cursor::NorthSouthResize:
82 return northSouthResizeCursor();
83 case Cursor::EastWestResize:
84 return eastWestResizeCursor();
85 case Cursor::NorthEastSouthWestResize:
86 return northEastSouthWestResizeCursor();
87 case Cursor::NorthWestSouthEastResize:
88 return northWestSouthEastResizeCursor();
89 case Cursor::ColumnResize:
90 return columnResizeCursor();
91 case Cursor::RowResize:
92 return rowResizeCursor();
93 case Cursor::MiddlePanning:
94 return middlePanningCursor();
95 case Cursor::EastPanning:
96 return eastPanningCursor();
97 case Cursor::NorthPanning:
98 return northPanningCursor();
99 case Cursor::NorthEastPanning:
100 return northEastPanningCursor();
101 case Cursor::NorthWestPanning:
102 return northWestPanningCursor();
103 case Cursor::SouthPanning:
104 return southPanningCursor();
105 case Cursor::SouthEastPanning:
106 return southEastPanningCursor();
107 case Cursor::SouthWestPanning:
108 return southWestPanningCursor();
109 case Cursor::WestPanning:
110 return westPanningCursor();
113 case Cursor::VerticalText:
114 return verticalTextCursor();
117 case Cursor::ContextMenu:
118 return contextMenuCursor();
120 return aliasCursor();
121 case Cursor::Progress:
122 return progressCursor();
124 return noDropCursor();
129 case Cursor::NotAllowed:
130 return notAllowedCursor();
132 return zoomInCursor();
133 case Cursor::ZoomOut:
134 return zoomOutCursor();
137 case Cursor::Grabbing:
138 return grabbingCursor();
140 ASSERT_NOT_REACHED();
142 return pointerCursor();
145 Cursor::Cursor(Image* image, const IntPoint& hotSpot)
148 , m_hotSpot(determineHotSpot(image, hotSpot))
149 , m_imageScaleFactor(1)
153 Cursor::Cursor(Image* image, const IntPoint& hotSpot, float scale)
156 , m_hotSpot(determineHotSpot(image, hotSpot))
157 , m_imageScaleFactor(scale)
161 Cursor::Cursor(Type type)
163 , m_imageScaleFactor(1)
167 Cursor::Cursor(const Cursor& other)
168 : m_type(other.m_type)
169 , m_image(other.m_image)
170 , m_hotSpot(other.m_hotSpot)
171 , m_imageScaleFactor(other.m_imageScaleFactor)
175 Cursor& Cursor::operator=(const Cursor& other)
177 m_type = other.m_type;
178 m_image = other.m_image;
179 m_hotSpot = other.m_hotSpot;
180 m_imageScaleFactor = other.m_imageScaleFactor;
188 const Cursor& pointerCursor()
190 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Pointer));
194 const Cursor& crossCursor()
196 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Cross));
200 const Cursor& handCursor()
202 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Hand));
206 const Cursor& moveCursor()
208 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Move));
212 const Cursor& verticalTextCursor()
214 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::VerticalText));
218 const Cursor& cellCursor()
220 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Cell));
224 const Cursor& contextMenuCursor()
226 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::ContextMenu));
230 const Cursor& aliasCursor()
232 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Alias));
236 const Cursor& zoomInCursor()
238 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::ZoomIn));
242 const Cursor& zoomOutCursor()
244 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::ZoomOut));
248 const Cursor& copyCursor()
250 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Copy));
254 const Cursor& noneCursor()
256 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::None));
260 const Cursor& progressCursor()
262 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Progress));
266 const Cursor& noDropCursor()
268 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NoDrop));
272 const Cursor& notAllowedCursor()
274 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NotAllowed));
278 const Cursor& iBeamCursor()
280 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::IBeam));
284 const Cursor& waitCursor()
286 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Wait));
290 const Cursor& helpCursor()
292 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Help));
296 const Cursor& eastResizeCursor()
298 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::EastResize));
302 const Cursor& northResizeCursor()
304 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthResize));
308 const Cursor& northEastResizeCursor()
310 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthEastResize));
314 const Cursor& northWestResizeCursor()
316 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthWestResize));
320 const Cursor& southResizeCursor()
322 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::SouthResize));
326 const Cursor& southEastResizeCursor()
328 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::SouthEastResize));
332 const Cursor& southWestResizeCursor()
334 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::SouthWestResize));
338 const Cursor& westResizeCursor()
340 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::WestResize));
344 const Cursor& northSouthResizeCursor()
346 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthSouthResize));
350 const Cursor& eastWestResizeCursor()
352 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::EastWestResize));
356 const Cursor& northEastSouthWestResizeCursor()
358 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthEastSouthWestResize));
362 const Cursor& northWestSouthEastResizeCursor()
364 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthWestSouthEastResize));
368 const Cursor& columnResizeCursor()
370 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::ColumnResize));
374 const Cursor& rowResizeCursor()
376 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::RowResize));
380 const Cursor& middlePanningCursor()
382 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::MiddlePanning));
386 const Cursor& eastPanningCursor()
388 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::EastPanning));
392 const Cursor& northPanningCursor()
394 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthPanning));
398 const Cursor& northEastPanningCursor()
400 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthEastPanning));
404 const Cursor& northWestPanningCursor()
406 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthWestPanning));
410 const Cursor& southPanningCursor()
412 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::SouthPanning));
416 const Cursor& southEastPanningCursor()
418 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::SouthEastPanning));
422 const Cursor& southWestPanningCursor()
424 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::SouthWestPanning));
428 const Cursor& westPanningCursor()
430 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::WestPanning));
434 const Cursor& grabCursor()
436 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Grab));
440 const Cursor& grabbingCursor()
442 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Grabbing));