src/video/cocoa/SDL_cocoamouse.m
changeset 7158 ff52fba70795
parent 7114 02b2fe147478
child 7191 75360622e65f
equal deleted inserted replaced
7157:a082c2eeb247 7158:ff52fba70795
    26 #include "SDL_events.h"
    26 #include "SDL_events.h"
    27 #include "SDL_cocoamouse.h"
    27 #include "SDL_cocoamouse.h"
    28 
    28 
    29 #include "../../events/SDL_mouse_c.h"
    29 #include "../../events/SDL_mouse_c.h"
    30 
    30 
       
    31 @implementation NSCursor (InvisibleCursor)
       
    32 + (NSCursor *)invisibleCursor
       
    33 {
       
    34     static NSCursor *invisibleCursor = NULL;
       
    35     if (!invisibleCursor) {
       
    36         /* RAW 16x16 transparent GIF */
       
    37         static unsigned char cursorBytes[] = {
       
    38             0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80,
       
    39             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xF9, 0x04,
       
    40             0x01, 0x00, 0x00, 0x01, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x10,
       
    41             0x00, 0x10, 0x00, 0x00, 0x02, 0x0E, 0x8C, 0x8F, 0xA9, 0xCB, 0xED,
       
    42             0x0F, 0xA3, 0x9C, 0xB4, 0xDA, 0x8B, 0xB3, 0x3E, 0x05, 0x00, 0x3B
       
    43         };
       
    44 
       
    45         NSData *cursorData = [NSData dataWithBytesNoCopy:&cursorBytes[0]
       
    46                                                   length:sizeof(cursorBytes)
       
    47                                             freeWhenDone:NO];
       
    48         NSImage *cursorImage = [[[NSImage alloc] initWithData:cursorData] autorelease];
       
    49         invisibleCursor = [[NSCursor alloc] initWithImage:cursorImage
       
    50                                                   hotSpot:NSZeroPoint];
       
    51     }
       
    52 
       
    53     return invisibleCursor;
       
    54 }
       
    55 @end
       
    56 
    31 
    57 
    32 static SDL_Cursor *
    58 static SDL_Cursor *
    33 Cocoa_CreateDefaultCursor()
    59 Cocoa_CreateDefaultCursor()
    34 {
    60 {
    35     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    61     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   151 }
   177 }
   152 
   178 
   153 static int
   179 static int
   154 Cocoa_ShowCursor(SDL_Cursor * cursor)
   180 Cocoa_ShowCursor(SDL_Cursor * cursor)
   155 {
   181 {
   156 	/* We need to track the previous state because hide and unhide calls need to
   182     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   157 	 * be matched, but ShowCursor calls don't.
   183 
   158 	 */
   184     SDL_VideoDevice *device = SDL_GetVideoDevice();
   159 	static SDL_bool isShown = SDL_TRUE;
   185     SDL_Window *window = (device ? device->windows : NULL);
   160     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   186     for (; window != NULL; window = window->next) {
   161 
   187         SDL_WindowData *driverdata = (SDL_WindowData *)window->driverdata;
   162     if (cursor) {
   188         if (driverdata) {
   163         NSCursor *nscursor = (NSCursor *)cursor->driverdata;
   189             [driverdata->nswindow performSelectorOnMainThread:@selector(invalidateCursorRectsForView:)
   164 
   190                                                    withObject:[driverdata->nswindow contentView]
   165         /* We're possibly executing from an event handler where this operation
   191                                                 waitUntilDone:NO];
   166          * is unsupported. This will execute it in the main Cocoa event loop
   192         }
   167          * after this returns.
       
   168          */
       
   169         [nscursor performSelectorOnMainThread:@selector(set)
       
   170                                    withObject:nil
       
   171                                 waitUntilDone:NO];
       
   172 
       
   173 		if (!isShown) {
       
   174 			[NSCursor unhide];
       
   175 			isShown = SDL_TRUE;
       
   176 		}
       
   177 	} else if (isShown) {
       
   178 		[NSCursor hide];
       
   179 		isShown = SDL_FALSE;
       
   180     }
   193     }
   181 
   194 
   182     [pool release];
   195     [pool release];
   183 
   196 
   184     return 0;
   197     return 0;