API  0.9.10
CPCursor.j
Go to the documentation of this file.
1 /*
2 Cursor support by browser:
3  OS X 10.6/Chrome 8 : All
4  OS X 10.6/Safari 5 : All
5  OS X 10.6/Firefox 3 : All except disappearingItemCursor (no url() support)
6  OS X 10.6/Firefox 3.5 : All except disappearingItemCursor (no url() support)
7  OS X 10.6/Firefox 3.6 : All except disappearingItemCursor, contextualMenuCursor, dragLinkCursor, dragCopyCursor, operationNotAllowedCursor (no url() support)
8  OS X 10.6/Firefox 4.0b10 : All
9  OS X/Opera 9 : All except disappearingItemCursor, closedHandCursor, openHandCursor, contextualMenuCursor, dragLinkCursor, dragCopyCursor, operationNotAllowedCursor, resizeUpDownCursor, resizeLeftRightCursor (no url() support)
10  OS X/Opera 10 : All except disappearingItemCursor, closedHandCursor, contextualMenuCursor, dragLinkCursor, dragCopyCursor, operationNotAllowedCursor (no url() support)
11  OS X/Opera 11 : All except disappearingItemCursor, closedHandCursor, contextualMenuCursor, dragLinkCursor, dragCopyCursor, operationNotAllowedCursor (no url() support)
12  Win XP/Chrome 8 : All
13  Win XP/Safari 5 : All
14  Win XP/Firefox 3 : All
15  Win XP/Firefox 3.5 : All
16  Win XP/Firefox 3.6 : All
17  Win XP/Firefox 4.0b10 : All
18  Win XP/Opera 10 : All except disappearingItemCursor, closedHandCursor, openHandCursor, contextualMenuCursor, dragLinkCursor, dragCopyCursor, operationNotAllowedCursor, resizeUpDownCursor, resizeLeftRightCursor (no url() support)
19  Win XP/Opera 11 : All except disappearingItemCursor, closedHandCursor, openHandCursor, contextualMenuCursor, dragLinkCursor, dragCopyCursor, operationNotAllowedCursor, resizeUpDownCursor, resizeLeftRightCursor (no url() support)
20  Win XP/IE 7 : All
21  Win XP/IE 8 : All
22 */
23 
24 
25 @global CPApp
26 
27 var currentCursor = nil,
29  cursors = {},
31 
32 @implementation CPCursor : CPObject
33 {
34  CPString _cssString;
35  CPString _hotSpot;
36  CPImage _image;
37  BOOL _isSetOnMouseEntered;
38  BOOL _isSetOnMouseExited;
39 }
40 
41 + (void)initialize
42 {
43  if (self !== CPCursor)
44  return;
45 
46  // IE < 9 does not support some CSS cursors, we map them to supported ones
47  ieCursorMap = {
48  "ew-resize": "e-resize",
49  "ns-resize": "n-resize",
50  "nesw-resize": "ne-resize",
51  "nwse-resize": "nw-resize"
52  };
53 }
54 
55 - (id)initWithCSSString:(CPString)aString
56 {
57  if (self = [super init])
58  _cssString = aString;
59 
60  return self;
61 }
62 
67 - (id)initWithImage:(CPImage)image hotSpot:(CGPoint)hotSpot
68 {
69  _hotSpot = hotSpot;
70  _image = image;
71  return [self initWithCSSString:"url(" + [_image filename] + ")" + hotSpot.x + " " + hotSpot.y + ", auto"];
72 }
73 
79 - (id)initWithImage:(CPImage)image foregroundColorHint:(CPColor)foregroundColor backgroundColorHint:(CPColor)backgroundColor hotSpot:(CGPoint)aHotSpot
80 {
81  return [self initWithImage:image hotSpot:aHotSpot];
82 }
83 
84 + (void)hide
85 {
86  [self _setCursorCSS:@"none"]; // Not supported in IE < 9
87 }
88 
89 + (void)unhide
90 {
91  [self _setCursorCSS:[currentCursor _cssString]];
92 }
93 
94 + (void)setHiddenUntilMouseMoves:(BOOL)flag
95 {
96  if (flag)
97  [CPCursor hide];
98  else
99  [CPCursor unhide];
100 }
101 
102 - (void)pop
103 {
104  [CPCursor pop];
105 }
106 
107 + (void)pop
108 {
109  if (cursorStack.length > 1)
110  {
111  cursorStack.pop();
112  currentCursor = cursorStack[cursorStack.length - 1];
113  }
114 }
115 
116 - (void)push
117 {
118  cursorStack.push(self);
119  currentCursor = self;
120 }
121 
122 - (void)set
123 {
124  if (currentCursor === self)
125  return;
126 
127  currentCursor = self;
128 
129 #if PLATFORM(DOM)
130  [[self class] _setCursorCSS:_cssString];
131 #endif
132 }
133 
134 - (void)mouseEntered:(CPEvent)event
135 {
136 }
137 
138 - (void)mouseExited:(CPEvent)event
139 {
140 }
141 
143 {
144  return currentCursor;
145 }
146 
147 + (void)_setCursorCSS:(CPString)aString
148 {
149 #if PLATFORM(DOM)
150  var platformWindows = [[CPPlatformWindow visiblePlatformWindows] allObjects];
151 
152  for (var i = 0, count = [platformWindows count]; i < count; i++)
153  platformWindows[i]._DOMBodyElement.style.cursor = aString;
154 #endif
155 }
156 
157 // Internal method that is used to return the system cursors. Caches the system cursors for performance.
158 + (CPCursor)_systemCursorWithName:(CPString)cursorName cssString:(CPString)aString hasImage:(BOOL)doesHaveImage
159 {
160  var cursor = cursors[cursorName];
161 
162  if (typeof cursor === "undefined")
163  {
164  var cssString;
165 
166  if (doesHaveImage)
167  {
168  var themeResourcePath = [[[CPApp themeBlend] bundle] resourcePath],
169  extension = CPBrowserIsEngine(CPInternetExplorerBrowserEngine) ? @"cur" : @"png";
170 
171  cssString = [CPString stringWithFormat:@"url(%@cursors/%@.%@), %@", themeResourcePath, cursorName, extension, aString];
172  }
173 
174  else
175  {
176  // IE <= 8 does not support some cursors, map them to supported cursors
178 
179  if (ieLessThan9)
180  cssString = ieCursorMap[aString] || aString;
181  else
182  cssString = aString;
183  }
184 
185  cursor = [[CPCursor alloc] initWithCSSString:cssString];
186  cursors[cursorName] = cursor;
187  }
188 
189  return cursor;
190 }
191 
193 {
194  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"default" hasImage:NO];
195 }
196 
198 {
199  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"crosshair" hasImage:NO];
200 }
201 
203 {
204  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"text" hasImage:NO];
205 }
206 
208 {
209  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"pointer" hasImage:NO];
210 }
211 
213 {
214  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"nw-resize" hasImage:NO];
215 }
216 
218 {
219  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"nwse-resize" hasImage:NO];
220 }
221 
223 {
224  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"ne-resize" hasImage:NO];
225 }
226 
228 {
229  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"nesw-resize" hasImage:NO];
230 }
231 
233 {
234  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"sw-resize" hasImage:NO];
235 }
236 
238 {
239  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"se-resize" hasImage:NO];
240 }
241 
243 {
244  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"s-resize" hasImage:NO];
245 }
246 
248 {
249  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"n-resize" hasImage:NO];
250 }
251 
253 {
254  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"w-resize" hasImage:NO];
255 }
256 
258 {
259  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"e-resize" hasImage:NO];
260 }
261 
263 {
264  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"col-resize" hasImage:NO];
265 }
266 
268 {
269  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"ew-resize" hasImage:NO];
270 }
271 
273 {
274  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"row-resize" hasImage:NO];
275 }
276 
278 {
279  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"ns-resize" hasImage:NO];
280 }
281 
283 {
284  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"not-allowed" hasImage:NO];
285 }
286 
288 {
289  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"copy" hasImage:YES];
290 }
291 
293 {
294  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"alias" hasImage:YES];
295 }
296 
298 {
299  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"context-menu" hasImage:YES];
300 }
301 
303 {
304  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"move" hasImage:YES];
305 }
306 
308 {
309  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"-moz-grabbing" hasImage:YES];
310 }
311 
313 {
314  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"auto" hasImage:YES];
315 }
316 
317 @end
318 
319 @implementation CPCursor(CPCoding)
320 
321 - (id)initWithCoder:(CPCoder)coder
322 {
323  if (self = [super init])
324  _cssString = [coder decodeObjectForKey:@"CPCursorNameKey"];
325 
326  return self;
327 }
328 
329 - (void)encodeWithCoder:(CPCoder)coder
330 {
331  [coder encodeObject:_cssString forKey:@"CPCursorNameKey"];
332 }
333 
334 @end
335 
337 
341 - (CPString)_cssString
342 {
343  return _cssString;
344 }
345 
349 - (void)set_cssString:(CPString)aValue
350 {
351  _cssString = aValue;
352 }
353 
358 {
359  return _hotSpot;
360 }
361 
365 - (CPImage)image
366 {
367  return _image;
368 }
369 
374 {
375  return _isSetOnMouseEntered;
376 }
377 
381 - (void)setOnMouseEntered:(BOOL)aValue
382 {
383  _isSetOnMouseEntered = aValue;
384 }
385 
390 {
391  return _isSetOnMouseExited;
392 }
393 
397 - (void)setOnMouseExited:(BOOL)aValue
398 {
399  _isSetOnMouseExited = aValue;
400 }
401 
402 @end
CPCursor openHandCursor()
Definition: CPCursor.j:302
CPCursor dragLinkCursor()
Definition: CPCursor.j:292
CPCursor crosshairCursor()
Definition: CPCursor.j:197
CPCursor resizeRightCursor()
Definition: CPCursor.j:257
CPCursor arrowCursor()
Definition: CPCursor.j:192
CPCursor operationNotAllowedCursor()
Definition: CPCursor.j:282
CPCursor pointingHandCursor()
Definition: CPCursor.j:207
CPCursor resizeEastWestCursor()
Definition: CPCursor.j:267
CPCursor disappearingItemCursor()
Definition: CPCursor.j:312
void push()
Definition: CPCursor.j:116
void hide()
Definition: CPCursor.j:84
CPCursor resizeSouthwestCursor()
Definition: CPCursor.j:232
CPCursor dragCopyCursor()
Definition: CPCursor.j:287
void unhide()
Definition: CPCursor.j:89
CPCursor closedHandCursor()
Definition: CPCursor.j:307
void pop()
Definition: CPCursor.j:102
CPCursor contextualMenuCursor()
Definition: CPCursor.j:297
CPSet visiblePlatformWindows()
An immutable string (collection of characters).
Definition: CPString.h:2
CPInternetExplorerBrowserEngine
Definition: CPImage.h:2
function CPFeatureIsCompatible(aFeature)
CPCursor resizeLeftRightCursor()
Definition: CPCursor.j:262
CPCursor resizeUpCursor()
Definition: CPCursor.j:247
CPHTMLCanvasFeature
CPCursor resizeNorthwestSoutheastCursor()
Definition: CPCursor.j:217
CPCursor resizeNortheastSouthwestCursor()
Definition: CPCursor.j:227
CPCursor resizeNortheastCursor()
Definition: CPCursor.j:222
Defines methods for use when archiving & restoring (enc/decoding).
Definition: CPCoder.h:2
CPCursor resizeDownCursor()
Definition: CPCursor.j:242
global CPApp var cursorStack
Definition: CPCursor.j:28
id initWithCSSString:(CPString aString)
Definition: CPCursor.j:55
id init()
Definition: CPObject.j:145
CPCursor resizeNorthSouthCursor()
Definition: CPCursor.j:277
global CPApp var ieCursorMap
Definition: CPCursor.j:30
CPCursor currentCursor()
Definition: CPCursor.j:142
void set()
Definition: CPCursor.j:122
CPCursor resizeNorthwestCursor()
Definition: CPCursor.j:212
Definition: CPEvent.h:2
Class class()
Definition: CPObject.j:179
void initialize()
Definition: CPCursor.j:41
function CPBrowserIsEngine(anEngine)
CPCursor resizeLeftCursor()
Definition: CPCursor.j:252
CPCursor resizeSoutheastCursor()
Definition: CPCursor.j:237
CPCursor IBeamCursor()
Definition: CPCursor.j:202
global CPApp var currentCursor
Definition: CPCursor.j:27
CPCursor resizeUpDownCursor()
Definition: CPCursor.j:272
id alloc()
Definition: CPObject.j:130
global CPApp var cursors
Definition: CPCursor.j:29
id stringWithFormat:(CPString format, [,] ...)
Definition: CPString.j:166
id initWithImage:hotSpot:(CPImage image, [hotSpot] CGPoint hotSpot)
Definition: CPCursor.j:67