API  0.9.8
 All Classes Files Functions Variables Typedefs Macros Groups Pages
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  currentCursor = self;
125 
126 #if PLATFORM(DOM)
127  [[self class] _setCursorCSS:_cssString];
128 #endif
129 }
130 
131 - (void)mouseEntered:(CPEvent)event
132 {
133 }
134 
135 - (void)mouseExited:(CPEvent)event
136 {
137 }
138 
140 {
141  return currentCursor;
142 }
143 
144 + (void)_setCursorCSS:(CPString)aString
145 {
146 #if PLATFORM(DOM)
147  var platformWindows = [[CPPlatformWindow visiblePlatformWindows] allObjects];
148 
149  for (var i = 0, count = [platformWindows count]; i < count; i++)
150  platformWindows[i]._DOMBodyElement.style.cursor = aString;
151 #endif
152 }
153 
154 // Internal method that is used to return the system cursors. Caches the system cursors for performance.
155 + (CPCursor)_systemCursorWithName:(CPString)cursorName cssString:(CPString)aString hasImage:(BOOL)doesHaveImage
156 {
157  var cursor = cursors[cursorName];
158 
159  if (typeof cursor === "undefined")
160  {
161  var cssString;
162 
163  if (doesHaveImage)
164  {
165  var themeResourcePath = [[[CPApp themeBlend] bundle] resourcePath],
166  extension = CPBrowserIsEngine(CPInternetExplorerBrowserEngine) ? @"cur" : @"png";
167 
168  cssString = [CPString stringWithFormat:@"url(%@cursors/%@.%@), %@", themeResourcePath, cursorName, extension, aString];
169  }
170 
171  else
172  {
173  // IE <= 8 does not support some cursors, map them to supported cursors
175 
176  if (ieLessThan9)
177  cssString = ieCursorMap[aString] || aString;
178  else
179  cssString = aString;
180  }
181 
182  cursor = [[CPCursor alloc] initWithCSSString:cssString];
183  cursors[cursorName] = cursor;
184  }
185 
186  return cursor;
187 }
188 
189 + (CPCursor)arrowCursor
190 {
191  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"default" hasImage:NO];
192 }
193 
194 + (CPCursor)crosshairCursor
195 {
196  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"crosshair" hasImage:NO];
197 }
198 
199 + (CPCursor)IBeamCursor
200 {
201  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"text" hasImage:NO];
202 }
203 
204 + (CPCursor)pointingHandCursor
205 {
206  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"pointer" hasImage:NO];
207 }
208 
209 + (CPCursor)resizeNorthwestCursor
210 {
211  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"nw-resize" hasImage:NO];
212 }
213 
214 + (CPCursor)resizeNorthwestSoutheastCursor
215 {
216  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"nwse-resize" hasImage:NO];
217 }
218 
219 + (CPCursor)resizeNortheastCursor
220 {
221  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"ne-resize" hasImage:NO];
222 }
223 
224 + (CPCursor)resizeNortheastSouthwestCursor
225 {
226  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"nesw-resize" hasImage:NO];
227 }
228 
229 + (CPCursor)resizeSouthwestCursor
230 {
231  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"sw-resize" hasImage:NO];
232 }
233 
234 + (CPCursor)resizeSoutheastCursor
235 {
236  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"se-resize" hasImage:NO];
237 }
238 
239 + (CPCursor)resizeDownCursor
240 {
241  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"s-resize" hasImage:NO];
242 }
243 
244 + (CPCursor)resizeUpCursor
245 {
246  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"n-resize" hasImage:NO];
247 }
248 
249 + (CPCursor)resizeLeftCursor
250 {
251  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"w-resize" hasImage:NO];
252 }
253 
254 + (CPCursor)resizeRightCursor
255 {
256  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"e-resize" hasImage:NO];
257 }
258 
259 + (CPCursor)resizeLeftRightCursor
260 {
261  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"col-resize" hasImage:NO];
262 }
263 
264 + (CPCursor)resizeEastWestCursor
265 {
266  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"ew-resize" hasImage:NO];
267 }
268 
269 + (CPCursor)resizeUpDownCursor
270 {
271  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"row-resize" hasImage:NO];
272 }
273 
274 + (CPCursor)resizeNorthSouthCursor
275 {
276  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"ns-resize" hasImage:NO];
277 }
278 
279 + (CPCursor)operationNotAllowedCursor
280 {
281  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"not-allowed" hasImage:NO];
282 }
283 
284 + (CPCursor)dragCopyCursor
285 {
286  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"copy" hasImage:YES];
287 }
288 
289 + (CPCursor)dragLinkCursor
290 {
291  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"alias" hasImage:YES];
292 }
293 
294 + (CPCursor)contextualMenuCursor
295 {
296  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"context-menu" hasImage:YES];
297 }
298 
299 + (CPCursor)openHandCursor
300 {
301  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"move" hasImage:YES];
302 }
303 
304 + (CPCursor)closedHandCursor
305 {
306  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"-moz-grabbing" hasImage:YES];
307 }
308 
309 + (CPCursor)disappearingItemCursor
310 {
311  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"auto" hasImage:YES];
312 }
313 
314 @end
315 
316 @implementation CPCursor(CPCoding)
317 
318 - (id)initWithCoder:(CPCoder)coder
319 {
320  if (self = [super init])
321  _cssString = [coder decodeObjectForKey:@"CPCursorNameKey"];
322 
323  return self;
324 }
325 
326 - (void)encodeWithCoder:(CPCoder)coder
327 {
328  [coder encodeObject:_cssString forKey:@"CPCursorNameKey"];
329 }
330 
331 @end
332 
334 
338 - (CPString)_cssString
339 {
340  return _cssString;
341 }
342 
346 - (void)set_cssString:(CPString)aValue
347 {
348  _cssString = aValue;
349 }
350 
354 - (CPString)hotSpot
355 {
356  return _hotSpot;
357 }
358 
362 - (CPImage)image
363 {
364  return _image;
365 }
366 
370 - (BOOL)isSetOnMouseEntered
371 {
372  return _isSetOnMouseEntered;
373 }
374 
378 - (void)setOnMouseEntered:(BOOL)aValue
379 {
380  _isSetOnMouseEntered = aValue;
381 }
382 
386 - (BOOL)isSetOnMouseExited
387 {
388  return _isSetOnMouseExited;
389 }
390 
394 - (void)setOnMouseExited:(BOOL)aValue
395 {
396  _isSetOnMouseExited = aValue;
397 }
398 
399 @end