Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / Cursor.cpp
blob350f4e928a0166438a5410200338ac68159adb85
1 /*
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
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.
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.
26 #include "config.h"
27 #include "platform/Cursor.h"
29 namespace blink {
31 IntPoint determineHotSpot(Image* image, bool hotSpotSpecified, const IntPoint& specifiedHotSpot)
33 if (image->isNull())
34 return IntPoint();
36 IntRect imageRect = image->rect();
38 // Hot spot must be inside cursor rectangle.
39 if (hotSpotSpecified) {
40 if (imageRect.contains(specifiedHotSpot)) {
41 return specifiedHotSpot;
44 return IntPoint(
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).
56 return IntPoint();
59 const Cursor& Cursor::fromType(Cursor::Type type)
61 switch (type) {
62 case Cursor::Pointer:
63 return pointerCursor();
64 case Cursor::Cross:
65 return crossCursor();
66 case Cursor::Hand:
67 return handCursor();
68 case Cursor::IBeam:
69 return iBeamCursor();
70 case Cursor::Wait:
71 return waitCursor();
72 case Cursor::Help:
73 return helpCursor();
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();
120 case Cursor::Move:
121 return moveCursor();
122 case Cursor::VerticalText:
123 return verticalTextCursor();
124 case Cursor::Cell:
125 return cellCursor();
126 case Cursor::ContextMenu:
127 return contextMenuCursor();
128 case Cursor::Alias:
129 return aliasCursor();
130 case Cursor::Progress:
131 return progressCursor();
132 case Cursor::NoDrop:
133 return noDropCursor();
134 case Cursor::Copy:
135 return copyCursor();
136 case Cursor::None:
137 return noneCursor();
138 case Cursor::NotAllowed:
139 return notAllowedCursor();
140 case Cursor::ZoomIn:
141 return zoomInCursor();
142 case Cursor::ZoomOut:
143 return zoomOutCursor();
144 case Cursor::Grab:
145 return grabCursor();
146 case Cursor::Grabbing:
147 return grabbingCursor();
148 case Cursor::Custom:
149 ASSERT_NOT_REACHED();
151 return pointerCursor();
154 Cursor::Cursor(Image* image, bool hotSpotSpecified, const IntPoint& hotSpot)
155 : m_type(Custom)
156 , m_image(image)
157 , m_hotSpot(determineHotSpot(image, hotSpotSpecified, hotSpot))
158 , m_imageScaleFactor(1)
162 Cursor::Cursor(Image* image, bool hotSpotSpecified, const IntPoint& hotSpot, float scale)
163 : m_type(Custom)
164 , m_image(image)
165 , m_hotSpot(determineHotSpot(image, hotSpotSpecified, hotSpot))
166 , m_imageScaleFactor(scale)
170 Cursor::Cursor(Type type)
171 : m_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;
190 return *this;
193 Cursor::~Cursor()
197 const Cursor& pointerCursor()
199 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Pointer));
200 return c;
203 const Cursor& crossCursor()
205 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Cross));
206 return c;
209 const Cursor& handCursor()
211 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Hand));
212 return c;
215 const Cursor& moveCursor()
217 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Move));
218 return c;
221 const Cursor& verticalTextCursor()
223 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::VerticalText));
224 return c;
227 const Cursor& cellCursor()
229 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Cell));
230 return c;
233 const Cursor& contextMenuCursor()
235 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::ContextMenu));
236 return c;
239 const Cursor& aliasCursor()
241 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Alias));
242 return c;
245 const Cursor& zoomInCursor()
247 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::ZoomIn));
248 return c;
251 const Cursor& zoomOutCursor()
253 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::ZoomOut));
254 return c;
257 const Cursor& copyCursor()
259 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Copy));
260 return c;
263 const Cursor& noneCursor()
265 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::None));
266 return c;
269 const Cursor& progressCursor()
271 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Progress));
272 return c;
275 const Cursor& noDropCursor()
277 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NoDrop));
278 return c;
281 const Cursor& notAllowedCursor()
283 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NotAllowed));
284 return c;
287 const Cursor& iBeamCursor()
289 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::IBeam));
290 return c;
293 const Cursor& waitCursor()
295 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Wait));
296 return c;
299 const Cursor& helpCursor()
301 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Help));
302 return c;
305 const Cursor& eastResizeCursor()
307 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::EastResize));
308 return c;
311 const Cursor& northResizeCursor()
313 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthResize));
314 return c;
317 const Cursor& northEastResizeCursor()
319 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthEastResize));
320 return c;
323 const Cursor& northWestResizeCursor()
325 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthWestResize));
326 return c;
329 const Cursor& southResizeCursor()
331 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::SouthResize));
332 return c;
335 const Cursor& southEastResizeCursor()
337 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::SouthEastResize));
338 return c;
341 const Cursor& southWestResizeCursor()
343 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::SouthWestResize));
344 return c;
347 const Cursor& westResizeCursor()
349 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::WestResize));
350 return c;
353 const Cursor& northSouthResizeCursor()
355 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthSouthResize));
356 return c;
359 const Cursor& eastWestResizeCursor()
361 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::EastWestResize));
362 return c;
365 const Cursor& northEastSouthWestResizeCursor()
367 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthEastSouthWestResize));
368 return c;
371 const Cursor& northWestSouthEastResizeCursor()
373 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthWestSouthEastResize));
374 return c;
377 const Cursor& columnResizeCursor()
379 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::ColumnResize));
380 return c;
383 const Cursor& rowResizeCursor()
385 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::RowResize));
386 return c;
389 const Cursor& middlePanningCursor()
391 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::MiddlePanning));
392 return c;
395 const Cursor& eastPanningCursor()
397 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::EastPanning));
398 return c;
401 const Cursor& northPanningCursor()
403 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthPanning));
404 return c;
407 const Cursor& northEastPanningCursor()
409 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthEastPanning));
410 return c;
413 const Cursor& northWestPanningCursor()
415 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::NorthWestPanning));
416 return c;
419 const Cursor& southPanningCursor()
421 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::SouthPanning));
422 return c;
425 const Cursor& southEastPanningCursor()
427 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::SouthEastPanning));
428 return c;
431 const Cursor& southWestPanningCursor()
433 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::SouthWestPanning));
434 return c;
437 const Cursor& westPanningCursor()
439 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::WestPanning));
440 return c;
443 const Cursor& grabCursor()
445 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Grab));
446 return c;
449 const Cursor& grabbingCursor()
451 DEFINE_STATIC_LOCAL(Cursor, c, (Cursor::Grabbing));
452 return c;
455 } // namespace blink