API  0.9.6
 All Classes Files Functions Variables 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 var currentCursor = nil,
27  cursors = {},
29 
30 @implementation CPCursor : CPObject
31 {
32  CPString _cssString;
33  CPString _hotSpot;
34  CPImage _image;
35  BOOL _isSetOnMouseEntered;
36  BOOL _isSetOnMouseExited;
37 }
38 
39 + (void)initialize
40 {
41  if (self !== CPCursor)
42  return;
43 
44  // IE < 9 does not support some CSS cursors, we map them to supported ones
45  ieCursorMap = {
46  "ew-resize": "e-resize",
47  "ns-resize": "n-resize",
48  "nesw-resize": "ne-resize",
49  "nwse-resize": "nw-resize"
50  };
51 }
52 
53 - (id)initWithCSSString:(CPString)aString
54 {
55  if (self = [super init])
56  _cssString = aString;
57 
58  return self;
59 }
60 
65 - (id)initWithImage:(CPImage)image hotSpot:(CGPoint)hotSpot
66 {
67  _hotSpot = hotSpot;
68  _image = image;
69  return [self initWithCSSString:"url(" + [_image filename] + ")" + hotSpot.x + " " + hotSpot.y + ", auto"];
70 }
71 
77 - (id)initWithImage:(CPImage)image foregroundColorHint:(CPColor)foregroundColor backgroundColorHint:(CPColor)backgroundColor hotSpot:(CGPoint)aHotSpot
78 {
79  return [self initWithImage:image hotSpot:hotSpot];
80 }
81 
82 + (void)hide
83 {
84  [self _setCursorCSS:@"none"]; // Not supported in IE < 9
85 }
86 
87 + (void)unhide
88 {
89  [self _setCursorCSS:[currentCursor _cssString]];
90 }
91 
92 + (void)setHiddenUntilMouseMoves:(BOOL)flag
93 {
94  if (flag)
95  [CPCursor hide];
96  else
97  [CPCursor unhide];
98 }
99 
100 - (void)pop
101 {
102  [CPCursor pop];
103 }
104 
105 + (void)pop
106 {
107  if (cursorStack.length > 1)
108  {
109  cursorStack.pop();
110  currentCursor = cursorStack[cursorStack.length - 1];
111  }
112 }
113 
114 - (void)push
115 {
116  currentCursor = cursorStack.push(self);
117 }
118 
119 - (void)set
120 {
121  currentCursor = self;
122 
123 #if PLATFORM(DOM)
124  [[self class] _setCursorCSS:_cssString];
125 #endif
126 }
127 
128 - (void)mouseEntered:(CPEvent)event
129 {
130 }
131 
132 - (void)mouseExited:(CPEvent)event
133 {
134 }
135 
137 {
138  return currentCursor;
139 }
140 
141 + (void)_setCursorCSS:(CPString)aString
142 {
143 #if PLATFORM(DOM)
144  var platformWindows = [[CPPlatformWindow visiblePlatformWindows] allObjects];
145 
146  for (var i = 0, count = [platformWindows count]; i < count; i++)
147  platformWindows[i]._DOMBodyElement.style.cursor = aString;
148 #endif
149 }
150 
151 // Internal method that is used to return the system cursors. Caches the system cursors for performance.
152 + (CPCursor)_systemCursorWithName:(CPString)cursorName cssString:(CPString)aString hasImage:(BOOL)doesHaveImage
153 {
154  var cursor = cursors[cursorName];
155 
156  if (typeof cursor === "undefined")
157  {
158  var cssString;
159 
160  if (doesHaveImage)
161  cssString = [CPString stringWithFormat:@"url(%@/CPCursor/%@.cur), %@", [[CPBundle bundleForClass:self] resourcePath], cursorName, aString];
162  else
163  {
164  // IE <= 8 does not support some cursors, map them to supported cursors
166 
167  if (ieLessThan9)
168  cssString = ieCursorMap[aString] || aString;
169  else
170  cssString = aString;
171  }
172 
173  cursor = [[CPCursor alloc] initWithCSSString:cssString];
174  cursors[cursorName] = cursor;
175  }
176 
177  return cursor;
178 }
179 
180 + (CPCursor)arrowCursor
181 {
182  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"default" hasImage:NO];
183 }
184 
185 + (CPCursor)crosshairCursor
186 {
187  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"crosshair" hasImage:NO];
188 }
189 
190 + (CPCursor)IBeamCursor
191 {
192  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"text" hasImage:NO];
193 }
194 
195 + (CPCursor)pointingHandCursor
196 {
197  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"pointer" hasImage:NO];
198 }
199 
200 + (CPCursor)resizeNorthwestCursor
201 {
202  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"nw-resize" hasImage:NO];
203 }
204 
205 + (CPCursor)resizeNorthwestSoutheastCursor
206 {
207  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"nwse-resize" hasImage:NO];
208 }
209 
210 + (CPCursor)resizeNortheastCursor
211 {
212  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"ne-resize" hasImage:NO];
213 }
214 
215 + (CPCursor)resizeNortheastSouthwestCursor
216 {
217  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"nesw-resize" hasImage:NO];
218 }
219 
220 + (CPCursor)resizeSouthwestCursor
221 {
222  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"sw-resize" hasImage:NO];
223 }
224 
225 + (CPCursor)resizeSoutheastCursor
226 {
227  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"se-resize" hasImage:NO];
228 }
229 
230 + (CPCursor)resizeDownCursor
231 {
232  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"s-resize" hasImage:NO];
233 }
234 
235 + (CPCursor)resizeUpCursor
236 {
237  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"n-resize" hasImage:NO];
238 }
239 
240 + (CPCursor)resizeLeftCursor
241 {
242  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"w-resize" hasImage:NO];
243 }
244 
245 + (CPCursor)resizeRightCursor
246 {
247  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"e-resize" hasImage:NO];
248 }
249 
250 + (CPCursor)resizeLeftRightCursor
251 {
252  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"col-resize" hasImage:NO];
253 }
254 
255 + (CPCursor)resizeEastWestCursor
256 {
257  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"ew-resize" hasImage:NO];
258 }
259 
260 + (CPCursor)resizeUpDownCursor
261 {
262  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"row-resize" hasImage:NO];
263 }
264 
265 + (CPCursor)resizeNorthSouthCursor
266 {
267  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"ns-resize" hasImage:NO];
268 }
269 
270 + (CPCursor)operationNotAllowedCursor
271 {
272  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"not-allowed" hasImage:NO];
273 }
274 
275 + (CPCursor)dragCopyCursor
276 {
277  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"copy" hasImage:YES];
278 }
279 
280 + (CPCursor)dragLinkCursor
281 {
282  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"alias" hasImage:YES];
283 }
284 
285 + (CPCursor)contextualMenuCursor
286 {
287  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"context-menu" hasImage:YES];
288 }
289 
290 + (CPCursor)openHandCursor
291 {
292  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"move" hasImage:YES];
293 }
294 
295 + (CPCursor)closedHandCursor
296 {
297  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"-moz-grabbing" hasImage:YES];
298 }
299 
300 + (CPCursor)disappearingItemCursor
301 {
302  return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"auto" hasImage:YES];
303 }
304 
305 @end
306 
307 @implementation CPCursor(CPCoding)
308 
309 - (id)initWithCoder:(CPCoder)coder
310 {
311  if (self = [super init])
312  _cssString = [coder decodeObjectForKey:@"CPCursorNameKey"];
313 
314  return self;
315 }
316 
317 - (void)encodeWithCoder:(CPCoder)coder
318 {
319  [coder encodeObject:_cssString forKey:@"CPCursorNameKey"];
320 }
321 
322 @end
323 
325 
329 - (CPString)_cssString
330 {
331  return _cssString;
332 }
333 
337 - (void)set_cssString:(CPString)aValue
338 {
339  _cssString = aValue;
340 }
341 
345 - (CPString)hotSpot
346 {
347  return _hotSpot;
348 }
349 
353 - (CPImage)image
354 {
355  return _image;
356 }
357 
361 - (BOOL)isSetOnMouseEntered
362 {
363  return _isSetOnMouseEntered;
364 }
365 
369 - (void)setOnMouseEntered:(BOOL)aValue
370 {
371  _isSetOnMouseEntered = aValue;
372 }
373 
377 - (BOOL)isSetOnMouseExited
378 {
379  return _isSetOnMouseExited;
380 }
381 
385 - (void)setOnMouseExited:(BOOL)aValue
386 {
387  _isSetOnMouseExited = aValue;
388 }
389 
390 @end