API 0.9.5
AppKit/CPCursor.j
Go to the documentation of this file.
00001 /*
00002 Cursor support by browser:
00003     OS X 10.6/Chrome 8       : All
00004     OS X 10.6/Safari 5       : All
00005     OS X 10.6/Firefox 3      : All except disappearingItemCursor (no url() support)
00006     OS X 10.6/Firefox 3.5    : All except disappearingItemCursor (no url() support)
00007     OS X 10.6/Firefox 3.6    : All except disappearingItemCursor, contextualMenuCursor, dragLinkCursor, dragCopyCursor, operationNotAllowedCursor (no url() support)
00008     OS X 10.6/Firefox 4.0b10 : All
00009     OS X/Opera 9             : All except disappearingItemCursor, closedHandCursor, openHandCursor, contextualMenuCursor, dragLinkCursor, dragCopyCursor, operationNotAllowedCursor, resizeUpDownCursor, resizeLeftRightCursor  (no url() support)
00010     OS X/Opera 10            : All except disappearingItemCursor, closedHandCursor, contextualMenuCursor, dragLinkCursor, dragCopyCursor, operationNotAllowedCursor (no url() support)
00011     OS X/Opera 11            : All except disappearingItemCursor, closedHandCursor, contextualMenuCursor, dragLinkCursor, dragCopyCursor, operationNotAllowedCursor (no url() support)
00012     Win XP/Chrome 8          : All
00013     Win XP/Safari 5          : All
00014     Win XP/Firefox 3         : All
00015     Win XP/Firefox 3.5       : All
00016     Win XP/Firefox 3.6       : All
00017     Win XP/Firefox 4.0b10    : All
00018     Win XP/Opera 10          : All except disappearingItemCursor, closedHandCursor, openHandCursor, contextualMenuCursor, dragLinkCursor, dragCopyCursor, operationNotAllowedCursor, resizeUpDownCursor, resizeLeftRightCursor (no url() support)
00019     Win XP/Opera 11          : All except disappearingItemCursor, closedHandCursor, openHandCursor, contextualMenuCursor, dragLinkCursor, dragCopyCursor, operationNotAllowedCursor, resizeUpDownCursor, resizeLeftRightCursor (no url() support)
00020     Win XP/IE 7              : All
00021     Win XP/IE 8              : All
00022 */
00023 
00024 
00025 var currentCursor = nil,
00026     cursorStack = [],
00027     cursors = {};
00028 
00029 @implementation CPCursor : CPObject
00030 {
00031     CPString _cssString;
00032     CPString _hotSpot;
00033     CPImage  _image;
00034     BOOL     _isSetOnMouseEntered;
00035     BOOL     _isSetOnMouseExited;
00036 }
00037 
00038 - (id)initWithCSSString:(CPString)aString
00039 {
00040     if (self = [super init])
00041         _cssString = aString;
00042 
00043     return self;
00044 }
00045 
00046 // hotspot is supported in CSS3 (but not IE).
00047 - (id)initWithImage:(CPImage)image hotSpot:(CPPoint)hotSpot
00048 {
00049     _hotSpot = hotSpot;
00050     _image = image;
00051     return [self initWithCSSString:"url(" + [_image filename] + ")" + hotSpot.x + " " + hotSpot.y + ", auto"];
00052 }
00053 
00054 // foregroundColor and backgroundColor are ignored in Cocoa as well.  See http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSCursor_Class/Reference/Reference.html
00055 - (id)initWithImage:(CPImage)image foregroundColorHint:(CPColor)foregroundColor backgroundColorHint:(CPColor)backgroundColor hotSpot:(CPPoint)aHotSpot
00056 {
00057     return [self initWithImage:image hotSpot:hotSpot];
00058 }
00059 
00060 + (void)hide
00061 {
00062     [self _setCursorCSS:"none"]; // Not supported in IE
00063 }
00064 
00065 + (void)unhide
00066 {
00067     [self _setCursorCSS:[currentCursor _cssString]];
00068 }
00069 
00070 + (void)setHiddenUntilMouseMoves:(BOOL)flag
00071 {
00072     if (flag)
00073         [CPCursor hide];
00074     else
00075         [CPCursor unhide];
00076 }
00077 
00078 - (void)pop
00079 {
00080     [CPCursor pop];
00081 }
00082 
00083 + (void)pop
00084 {
00085     if (cursorStack.length > 1)
00086     {
00087         cursorStack.pop();
00088         currentCursor = cursorStack[cursorStack.length - 1];
00089     }
00090 }
00091 
00092 - (void)push
00093 {
00094     currentCursor = cursorStack.push(self);
00095 }
00096 
00097 - (void)set
00098 {
00099     currentCursor = self;
00100 
00101 #if PLATFORM(DOM)
00102     [[self class] _setCursorCSS:_cssString];
00103 #endif
00104 }
00105 
00106 - (void)mouseEntered:(CPEvent)event
00107 {
00108 }
00109 
00110 - (void)mouseExited:(CPEvent)event
00111 {
00112 }
00113 
00114 + (CPCursor)currentCursor
00115 {
00116     return currentCursor;
00117 }
00118 
00119 + (void)_setCursorCSS:(CPString)aString
00120 {
00121 #if PLATFORM(DOM)
00122     var platformWindows = [[CPPlatformWindow visiblePlatformWindows] allObjects];
00123     for (var i = 0, count = [platformWindows count]; i < count; i++)
00124         platformWindows[i]._DOMBodyElement.style.cursor = aString;
00125 #endif
00126 }
00127 
00128 // Internal method that is used to return the system cursors.  Caches the system cursors for performance.
00129 + (CPCursor)_systemCursorWithName:(CPString)cursorName cssString:(CPString)aString hasImage:(BOOL)doesHaveImage
00130 {
00131     var cursor = cursors[cursorName];
00132     if (typeof cursor === 'undefined')
00133     {
00134         var cssString;
00135         if (doesHaveImage)
00136             cssString = @"url(" + [[CPBundle bundleForClass:self] resourcePath] + @"/CPCursor/" + cursorName + ".cur), " + aString;
00137         else
00138             cssString = aString
00139         cursor = [[CPCursor alloc] initWithCSSString:cssString];
00140         cursors[cursorName] = cursor;
00141     }
00142     return cursor;
00143 }
00144 
00145 + (CPCursor)arrowCursor
00146 {
00147     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:"default" hasImage:NO];
00148 }
00149 
00150 + (CPCursor)crosshairCursor
00151 {
00152     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"crosshair" hasImage:NO];
00153 }
00154 
00155 + (CPCursor)IBeamCursor
00156 {
00157     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"text" hasImage:NO];
00158 }
00159 
00160 + (CPCursor)pointingHandCursor
00161 {
00162     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"pointer" hasImage:NO];
00163 }
00164 
00165 + (CPCursor)resizeDownCursor
00166 {
00167     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"s-resize" hasImage:NO];
00168 }
00169 
00170 + (CPCursor)resizeUpCursor
00171 {
00172     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"n-resize" hasImage:NO];
00173 }
00174 
00175 + (CPCursor)resizeLeftCursor
00176 {
00177     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"w-resize" hasImage:NO];
00178 }
00179 
00180 + (CPCursor)resizeRightCursor
00181 {
00182     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"e-resize" hasImage:NO];
00183 }
00184 
00185 + (CPCursor)resizeLeftRightCursor
00186 {
00187     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"col-resize" hasImage:NO];
00188 }
00189 
00190 + (CPCursor)resizeUpDownCursor
00191 {
00192     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"row-resize" hasImage:NO];
00193 }
00194 
00195 + (CPCursor)operationNotAllowedCursor
00196 {
00197     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"not-allowed" hasImage:NO];
00198 }
00199 
00200 + (CPCursor)dragCopyCursor
00201 {
00202     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"copy" hasImage:YES];
00203 }
00204 
00205 + (CPCursor)dragLinkCursor
00206 {
00207     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"alias" hasImage:YES];
00208 }
00209 
00210 + (CPCursor)contextualMenuCursor
00211 {
00212     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"context-menu" hasImage:YES];
00213 }
00214 
00215 + (CPCursor)openHandCursor
00216 {
00217     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"move" hasImage:YES];
00218 }
00219 
00220 + (CPCursor)closedHandCursor
00221 {
00222     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"-moz-grabbing" hasImage:YES];
00223 }
00224 
00225 + (CPCursor)disappearingItemCursor
00226 {
00227     return [CPCursor _systemCursorWithName:CPStringFromSelector(_cmd) cssString:@"auto" hasImage:YES];
00228 }
00229 
00230 @end
00231 
00232 @implementation CPCursor(CPCoding)
00233 
00234 - (id)initWithCoder:(CPCoder)coder
00235 {
00236     if (self = [super init])
00237         _cssString = [coder decodeObjectForKey:@"CPCursorNameKey"];
00238 
00239     return self;
00240 }
00241 
00242 - (void)encodeWithCoder:(CPCoder)coder
00243 {
00244     [coder encodeObject:_cssString forKey:@"CPCursorNameKey"];
00245 }
00246 
00247 @end
00248 
00249 @implementation CPCursor (CPSynthesizedAccessors)
00250 
00254 - (CPString)_cssString
00255 {
00256     return _cssString;
00257 }
00258 
00262 - (void)set_cssString:(CPString)aValue
00263 {
00264     _cssString = aValue;
00265 }
00266 
00270 - (CPString)hotSpot
00271 {
00272     return _hotSpot;
00273 }
00274 
00278 - (CPImage)image
00279 {
00280     return _image;
00281 }
00282 
00286 - (BOOL)isSetOnMouseEntered
00287 {
00288     return _isSetOnMouseEntered;
00289 }
00290 
00294 - (void)setOnMouseEntered:(BOOL)aValue
00295 {
00296     _isSetOnMouseEntered = aValue;
00297 }
00298 
00302 - (BOOL)isSetOnMouseExited
00303 {
00304     return _isSetOnMouseExited;
00305 }
00306 
00310 - (void)setOnMouseExited:(BOOL)aValue
00311 {
00312     _isSetOnMouseExited = aValue;
00313 }
00314 
00315 @end
 All Classes Files Functions Variables Defines