Back to index

lightning-sunbird  0.9+nobinonly
nsCursorManager.mm
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is nsCursors.
00015  *
00016  * The Initial Developer of the Original Code is 
00017  * Andrew Thompson.
00018  * Portions created by the Andrew Thompson are Copyright (C) 2004
00019  * Andrew Thompson. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #import "nsCursorManager.h"
00038 #import "math.h"
00039 
00040 static nsCursorManager *gInstance;
00041 
00045 @interface nsCursorManager(PrivateMethods)
00054 - (nsMacCursor *) getCursor: (nsCursor) aCursor;
00055 
00063 + (nsMacCursor *) createCursor: (enum nsCursor) aCursor;
00064 
00073 + (nsMacCursor *) createNSCursor: (SEL) aPantherCursor orThemeCursor: (ThemeCursor) aJaguarCursor;
00074 
00084 + (nsMacCursor *) createNSCursor: (SEL) aPantherCursor orImageCursor: (NSString *) aImageName withHotspot: (NSPoint) aPoint;
00085 @end
00086 
00091 static BOOL isPantherOrLater();
00092 
00093 static BOOL isPantherOrLater() 
00094 {
00095   static PRBool gInitVer = PR_FALSE;
00096   static PRBool gOnPantherOrLater = PR_FALSE;
00097   if(!gInitVer)
00098   {
00099     long version;
00100     OSErr err = ::Gestalt(gestaltSystemVersion, &version);
00101     gOnPantherOrLater = (err == noErr && version >= 0x00001030);
00102     gInitVer = PR_TRUE;
00103   }
00104   return gOnPantherOrLater;
00105 }
00106 
00107 @implementation nsCursorManager
00108 
00109 + (nsCursorManager *) sharedInstance
00110 {
00111   if (gInstance == nil)
00112   {
00113     gInstance = [[nsCursorManager alloc] init];
00114   }
00115   return gInstance;
00116 }
00117 
00118 + (void) dispose
00119 {
00120   [gInstance release];
00121   gInstance = nil;
00122 }
00123 
00124 + (nsMacCursor *) createCursor: (enum nsCursor) aCursor 
00125 {
00126   switch(aCursor)
00127   {
00128     case eCursor_standard:
00129       return [nsMacCursor cursorWithCursor: [NSCursor arrowCursor]];
00130     case eCursor_wait:
00131       return [nsMacCursor cursorWithThemeCursor: kThemeWatchCursor];
00132     case eCursor_select:              
00133       return [nsMacCursor cursorWithCursor: [NSCursor IBeamCursor]];
00134     case eCursor_hyperlink:
00135       return [nsCursorManager createNSCursor: @selector(pointingHandCursor) orThemeCursor: kThemePointingHandCursor];                  
00136     case eCursor_crosshair:
00137       return [nsCursorManager createNSCursor: @selector(crosshairCursor) orThemeCursor: kThemeCrossCursor];                                        
00138     case eCursor_move:
00139       return [nsCursorManager createNSCursor: @selector(openHandCursor) orThemeCursor: kThemeOpenHandCursor];                   
00140     case eCursor_help:
00141       return [nsMacCursor cursorWithImageNamed: @"help" hotSpot: NSMakePoint(1,1)];        
00142     case eCursor_copy:
00143       return [nsMacCursor cursorWithThemeCursor: kThemeCopyArrowCursor];
00144     case eCursor_alias:
00145       return [nsMacCursor cursorWithThemeCursor: kThemeAliasArrowCursor];
00146     case eCursor_context_menu:
00147       return [nsMacCursor cursorWithThemeCursor: kThemeContextualMenuArrowCursor];
00148 
00149     case eCursor_cell:
00150       return [nsMacCursor cursorWithThemeCursor: kThemePlusCursor];
00151     case eCursor_grab:
00152       return [nsCursorManager createNSCursor: @selector(openHandCursor) orThemeCursor: kThemeOpenHandCursor];
00153     case eCursor_grabbing:
00154       return [nsCursorManager createNSCursor: @selector(closedHandCursor) orThemeCursor: kThemeClosedHandCursor];
00155     case eCursor_spinning:
00156       return [nsMacCursor cursorWithResources: 200 lastFrame: 203]; // better than kThemeSpinningCursor        
00157     case eCursor_zoom_in:
00158       return [nsMacCursor cursorWithImageNamed: @"zoomIn" hotSpot: NSMakePoint(6,6)];
00159     case eCursor_zoom_out:
00160       return [nsMacCursor cursorWithImageNamed: @"zoomOut" hotSpot: NSMakePoint(6,6)];
00161     case eCursor_vertical_text:
00162       return [nsMacCursor cursorWithImageNamed: @"vtIBeam" hotSpot: NSMakePoint(7,8)];
00163     case eCursor_all_scroll:
00164       return [nsCursorManager createNSCursor: @selector(openHandCursor) orThemeCursor: kThemeOpenHandCursor];                   
00165     case eCursor_not_allowed:
00166     case eCursor_no_drop:
00167       return [nsMacCursor cursorWithThemeCursor: kThemeNotAllowedCursor];
00168 
00169     // Resize Cursors:
00170     //North
00171     case eCursor_n_resize:
00172         return [nsCursorManager createNSCursor: @selector(resizeUpCursor) orImageCursor: @"arrowN" withHotspot: NSMakePoint(7,7)];
00173     //North East
00174     case eCursor_ne_resize:
00175         return [nsMacCursor cursorWithImageNamed: @"sizeNE" hotSpot: NSMakePoint(8,7)];
00176     //East
00177     case eCursor_e_resize:        
00178         return [nsCursorManager createNSCursor: @selector(resizeRightCursor) orThemeCursor: kThemeResizeRightCursor];
00179     //South East
00180     case eCursor_se_resize:
00181         return [nsMacCursor cursorWithImageNamed: @"sizeSE" hotSpot: NSMakePoint(8,8)];
00182     //South
00183     case eCursor_s_resize:
00184         return [nsCursorManager createNSCursor: @selector(resizeDownCursor) orImageCursor: @"arrowS" withHotspot: NSMakePoint(7,7)];
00185     //South West
00186     case eCursor_sw_resize:
00187         return [nsMacCursor cursorWithImageNamed: @"sizeSW" hotSpot: NSMakePoint(6,8)];
00188     //West
00189     case eCursor_w_resize:
00190         return [nsCursorManager createNSCursor: @selector(resizeLeftCursor) orThemeCursor: kThemeResizeLeftCursor];
00191     //North West
00192     case eCursor_nw_resize:
00193         return [nsMacCursor cursorWithImageNamed: @"sizeNW" hotSpot: NSMakePoint(7,7)];
00194     //North & South
00195     case eCursor_ns_resize:
00196         return [nsCursorManager createNSCursor: @selector(resizeUpDownCursor) orImageCursor: @"sizeNS" withHotspot: NSMakePoint(7,7)];                         
00197     //East & West
00198     case eCursor_ew_resize:
00199         return [nsCursorManager createNSCursor: @selector(resizeLeftRightCursor) orThemeCursor: kThemeResizeLeftRightCursor];                  
00200     //North East & South West
00201     case eCursor_nesw_resize:
00202         return [nsMacCursor cursorWithImageNamed: @"sizeNESW" hotSpot: NSMakePoint(8,8)];
00203     //North West & South East        
00204     case eCursor_nwse_resize:
00205         return [nsMacCursor cursorWithImageNamed: @"sizeNWSE" hotSpot: NSMakePoint(8,8)];
00206     //Column Resize
00207     case eCursor_col_resize:
00208         return [nsMacCursor cursorWithImageNamed: @"colResize" hotSpot: NSMakePoint(8,8)];
00209     //Row Resize        
00210     case eCursor_row_resize:
00211         return [nsMacCursor cursorWithImageNamed: @"rowResize" hotSpot: NSMakePoint(8,8)];
00212     default:
00213       return [nsMacCursor cursorWithCursor: [NSCursor arrowCursor]];
00214   }
00215 }
00216 
00217 + (nsMacCursor *) createNSCursor: (SEL) aPantherCursor orThemeCursor: (ThemeCursor) aJaguarCursor
00218 {
00219   return isPantherOrLater() ?   [nsMacCursor cursorWithCursor: [NSCursor performSelector: aPantherCursor]] :
00220                                 [nsMacCursor cursorWithThemeCursor: aJaguarCursor];
00221 }
00222 
00223 + (nsMacCursor *) createNSCursor: (SEL) aPantherCursor orImageCursor: (NSString *) aImageName withHotspot: (NSPoint) aPoint
00224 {
00225   return isPantherOrLater() ?      [nsMacCursor cursorWithCursor: [NSCursor performSelector: aPantherCursor]] :
00226                                 [nsMacCursor cursorWithImageNamed: aImageName hotSpot: aPoint];
00227 }
00228 
00229 - (id) init
00230 {
00231   if ( (self = [super init]) ) 
00232   {
00233     mCursors = [[NSMutableDictionary alloc] initWithCapacity: 25];
00234   }
00235   return self;
00236 }
00237 
00238 - (void) setCursor: (enum nsCursor) aCursor
00239 {
00240   if (aCursor != mCurrentCursor)
00241   {
00242     [[self getCursor: mCurrentCursor] unset];
00243     [[self getCursor: aCursor] set];
00244     mCurrentCursor = aCursor;
00245   }
00246 }
00247 
00248 - (nsMacCursor *) getCursor: (enum nsCursor) aCursor
00249 {
00250   nsMacCursor * result = [mCursors objectForKey: [NSNumber numberWithInt: aCursor]];
00251   if ( result == nil ) 
00252   {
00253     result = [nsCursorManager createCursor: aCursor];
00254     [mCursors setObject: result forKey: [NSNumber numberWithInt: aCursor]];
00255   }
00256   return result;
00257 }
00258 
00259 - (void) dealloc
00260 {
00261   [[self getCursor: mCurrentCursor] unset];
00262   [mCursors release];    
00263   [super dealloc];    
00264 }
00265 @end