src/video/cocoa/SDL_cocoamouse.m
changeset 5376 183ec2d4485c
parent 5371 fc3d3d580777
child 5387 03c79d46bf5c
equal deleted inserted replaced
5375:16877f74123c 5376:183ec2d4485c
    24 #include "SDL_events.h"
    24 #include "SDL_events.h"
    25 #include "SDL_cocoavideo.h"
    25 #include "SDL_cocoavideo.h"
    26 
    26 
    27 #include "../../events/SDL_mouse_c.h"
    27 #include "../../events/SDL_mouse_c.h"
    28 
    28 
       
    29 
       
    30 static SDL_Cursor *
       
    31 Cocoa_CreateDefaultCursor()
       
    32 {
       
    33     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
    34     NSCursor *nscursor;
       
    35     SDL_Cursor *cursor = NULL;
       
    36 
       
    37     nscursor = [NSCursor arrowCursor];
       
    38 
       
    39     if (nscursor) {
       
    40         cursor = SDL_calloc(1, sizeof(*cursor));
       
    41         if (cursor) {
       
    42             cursor->driverdata = nscursor;
       
    43             [nscursor retain];
       
    44         }
       
    45     }
       
    46 
       
    47     [pool release];
       
    48 
       
    49     return cursor;
       
    50 }
       
    51 
       
    52 static SDL_Cursor *
       
    53 Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
       
    54 {
       
    55     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
    56     NSImage *nsimage;
       
    57     NSCursor *nscursor = NULL;
       
    58     SDL_Cursor *cursor = NULL;
       
    59 
       
    60     nsimage = Cocoa_CreateImage(surface);
       
    61     if (nsimage) {
       
    62         nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
       
    63     }
       
    64 
       
    65     if (nscursor) {
       
    66         cursor = SDL_calloc(1, sizeof(*cursor));
       
    67         if (cursor) {
       
    68             cursor->driverdata = nscursor;
       
    69         }
       
    70     }
       
    71 
       
    72     [pool release];
       
    73 
       
    74     return cursor;
       
    75 }
       
    76 
       
    77 static void
       
    78 Cocoa_FreeCursor(SDL_Cursor * cursor)
       
    79 {
       
    80     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
    81     NSCursor *nscursor = (NSCursor *)cursor->driverdata;
       
    82 
       
    83     [nscursor release];
       
    84 
       
    85     [pool release];
       
    86 }
       
    87 
       
    88 static int
       
    89 Cocoa_ShowCursor(SDL_Cursor * cursor)
       
    90 {
       
    91     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
    92 
       
    93     if (SDL_GetMouseFocus()) {
       
    94         if (cursor) {
       
    95             [NSCursor unhide];
       
    96         } else {
       
    97             [NSCursor hide];
       
    98         }
       
    99     }
       
   100 
       
   101     [pool release];
       
   102 
       
   103     return 0;
       
   104 }
       
   105 
       
   106 static void
       
   107 Cocoa_WarpMouse(SDL_Window * window, int x, int y)
       
   108 {
       
   109     CGPoint point;
       
   110 
       
   111     point.x = (CGFloat)window->x + x;
       
   112     point.y = (CGFloat)window->y + y;
       
   113     CGWarpMouseCursorPosition(point);
       
   114 }
       
   115 
    29 void
   116 void
    30 Cocoa_InitMouse(_THIS)
   117 Cocoa_InitMouse(_THIS)
    31 {
   118 {
       
   119     SDL_Mouse *mouse = SDL_GetMouse();
       
   120     SDL_Cursor *cursor;
       
   121 
       
   122     mouse->CreateCursor = Cocoa_CreateCursor;
       
   123     mouse->ShowCursor = Cocoa_ShowCursor;
       
   124     mouse->WarpMouse = Cocoa_WarpMouse;
       
   125     mouse->FreeCursor = Cocoa_FreeCursor;
       
   126 
       
   127     cursor = Cocoa_CreateDefaultCursor();
       
   128     mouse->cursors = mouse->cur_cursor = cursor;
    32 }
   129 }
    33 
   130 
    34 static int
   131 static int
    35 ConvertMouseButtonToSDL(int button)
   132 ConvertMouseButtonToSDL(int button)
    36 {
   133 {