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
, bool hotSpotSpecified
, const IntPoint
& specifiedHotSpot
)
36 IntRect imageRect
= image
->rect();
38 // Hot spot must be inside cursor rectangle.
39 if (hotSpotSpecified
) {
40 if (imageRect
.contains(specifiedHotSpot
)) {
41 return specifiedHotSpot
;
45 clampTo
<int>(specifiedHotSpot
.x(), imageRect
.x(), imageRect
.maxX() - 1),
46 clampTo
<int>(specifiedHotSpot
.y(), imageRect
.y(), imageRect
.maxY() - 1));
49 // If hot spot is not specified externally, it can be extracted from some image formats (e.g. .cur).
50 IntPoint intrinsicHotSpot
;
51 bool imageHasIntrinsicHotSpot
= image
->getHotSpot(intrinsicHotSpot
);
52 if (imageHasIntrinsicHotSpot
&& imageRect
.contains(intrinsicHotSpot
))
53 return intrinsicHotSpot
;
55 // If neither is provided, use a default value of (0, 0).
59 const Cursor
& Cursor::fromType(Cursor::Type type
)
63 return pointerCursor();
74 case Cursor::EastResize
:
75 return eastResizeCursor();
76 case Cursor::NorthResize
:
77 return northResizeCursor();
78 case Cursor::NorthEastResize
:
79 return northEastResizeCursor();
80 case Cursor::NorthWestResize
:
81 return northWestResizeCursor();
82 case Cursor::SouthResize
:
83 return southResizeCursor();
84 case Cursor::SouthEastResize
:
85 return southEastResizeCursor();
86 case Cursor::SouthWestResize
:
87 return southWestResizeCursor();
88 case Cursor::WestResize
:
89 return westResizeCursor();
90 case Cursor::NorthSouthResize
:
91 return northSouthResizeCursor();
92 case Cursor::EastWestResize
:
93 return eastWestResizeCursor();
94 case Cursor::NorthEastSouthWestResize
:
95 return northEastSouthWestResizeCursor();
96 case Cursor::NorthWestSouthEastResize
:
97 return northWestSouthEastResizeCursor();
98 case Cursor::ColumnResize
:
99 return columnResizeCursor();
100 case Cursor::RowResize
:
101 return rowResizeCursor();
102 case Cursor::MiddlePanning
:
103 return middlePanningCursor();
104 case Cursor::EastPanning
:
105 return eastPanningCursor();
106 case Cursor::NorthPanning
:
107 return northPanningCursor();
108 case Cursor::NorthEastPanning
:
109 return northEastPanningCursor();
110 case Cursor::NorthWestPanning
:
111 return northWestPanningCursor();
112 case Cursor::SouthPanning
:
113 return southPanningCursor();
114 case Cursor::SouthEastPanning
:
115 return southEastPanningCursor();
116 case Cursor::SouthWestPanning
:
117 return southWestPanningCursor();
118 case Cursor::WestPanning
:
119 return westPanningCursor();
122 case Cursor::VerticalText
:
123 return verticalTextCursor();
126 case Cursor::ContextMenu
:
127 return contextMenuCursor();
129 return aliasCursor();
130 case Cursor::Progress
:
131 return progressCursor();
133 return noDropCursor();
138 case Cursor::NotAllowed
:
139 return notAllowedCursor();
141 return zoomInCursor();
142 case Cursor::ZoomOut
:
143 return zoomOutCursor();
146 case Cursor::Grabbing
:
147 return grabbingCursor();
149 ASSERT_NOT_REACHED();
151 return pointerCursor();
154 Cursor::Cursor(Image
* image
, bool hotSpotSpecified
, const IntPoint
& hotSpot
)
157 , m_hotSpot(determineHotSpot(image
, hotSpotSpecified
, hotSpot
))
158 , m_imageScaleFactor(1)
162 Cursor::Cursor(Image
* image
, bool hotSpotSpecified
, const IntPoint
& hotSpot
, float scale
)
165 , m_hotSpot(determineHotSpot(image
, hotSpotSpecified
, hotSpot
))
166 , m_imageScaleFactor(scale
)
170 Cursor::Cursor(Type type
)
172 , m_imageScaleFactor(1)
176 Cursor::Cursor(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
)
184 Cursor
& Cursor::operator=(const Cursor
& other
)
186 m_type
= other
.m_type
;
187 m_image
= other
.m_image
;
188 m_hotSpot
= other
.m_hotSpot
;
189 m_imageScaleFactor
= other
.m_imageScaleFactor
;
197 const Cursor
& pointerCursor()
199 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::Pointer
));
203 const Cursor
& crossCursor()
205 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::Cross
));
209 const Cursor
& handCursor()
211 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::Hand
));
215 const Cursor
& moveCursor()
217 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::Move
));
221 const Cursor
& verticalTextCursor()
223 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::VerticalText
));
227 const Cursor
& cellCursor()
229 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::Cell
));
233 const Cursor
& contextMenuCursor()
235 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::ContextMenu
));
239 const Cursor
& aliasCursor()
241 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::Alias
));
245 const Cursor
& zoomInCursor()
247 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::ZoomIn
));
251 const Cursor
& zoomOutCursor()
253 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::ZoomOut
));
257 const Cursor
& copyCursor()
259 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::Copy
));
263 const Cursor
& noneCursor()
265 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::None
));
269 const Cursor
& progressCursor()
271 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::Progress
));
275 const Cursor
& noDropCursor()
277 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::NoDrop
));
281 const Cursor
& notAllowedCursor()
283 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::NotAllowed
));
287 const Cursor
& iBeamCursor()
289 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::IBeam
));
293 const Cursor
& waitCursor()
295 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::Wait
));
299 const Cursor
& helpCursor()
301 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::Help
));
305 const Cursor
& eastResizeCursor()
307 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::EastResize
));
311 const Cursor
& northResizeCursor()
313 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::NorthResize
));
317 const Cursor
& northEastResizeCursor()
319 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::NorthEastResize
));
323 const Cursor
& northWestResizeCursor()
325 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::NorthWestResize
));
329 const Cursor
& southResizeCursor()
331 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::SouthResize
));
335 const Cursor
& southEastResizeCursor()
337 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::SouthEastResize
));
341 const Cursor
& southWestResizeCursor()
343 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::SouthWestResize
));
347 const Cursor
& westResizeCursor()
349 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::WestResize
));
353 const Cursor
& northSouthResizeCursor()
355 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::NorthSouthResize
));
359 const Cursor
& eastWestResizeCursor()
361 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::EastWestResize
));
365 const Cursor
& northEastSouthWestResizeCursor()
367 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::NorthEastSouthWestResize
));
371 const Cursor
& northWestSouthEastResizeCursor()
373 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::NorthWestSouthEastResize
));
377 const Cursor
& columnResizeCursor()
379 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::ColumnResize
));
383 const Cursor
& rowResizeCursor()
385 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::RowResize
));
389 const Cursor
& middlePanningCursor()
391 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::MiddlePanning
));
395 const Cursor
& eastPanningCursor()
397 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::EastPanning
));
401 const Cursor
& northPanningCursor()
403 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::NorthPanning
));
407 const Cursor
& northEastPanningCursor()
409 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::NorthEastPanning
));
413 const Cursor
& northWestPanningCursor()
415 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::NorthWestPanning
));
419 const Cursor
& southPanningCursor()
421 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::SouthPanning
));
425 const Cursor
& southEastPanningCursor()
427 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::SouthEastPanning
));
431 const Cursor
& southWestPanningCursor()
433 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::SouthWestPanning
));
437 const Cursor
& westPanningCursor()
439 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::WestPanning
));
443 const Cursor
& grabCursor()
445 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::Grab
));
449 const Cursor
& grabbingCursor()
451 DEFINE_STATIC_LOCAL(Cursor
, c
, (Cursor::Grabbing
));