src/video/cocoa/SDL_cocoamouse.m
author Sam Lantinga
Mon, 21 Feb 2011 23:10:00 -0800
changeset 5387 03c79d46bf5c
parent 5376 183ec2d4485c
child 5391 06b239f48b91
permissions -rw-r--r--
Actually set the cursor! :)
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2011 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_events.h"
    25 #include "SDL_cocoavideo.h"
    26 
    27 #include "../../events/SDL_mouse_c.h"
    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 *nscursor = (NSCursor *)cursor->driverdata;
    96 
    97             [nscursor set];
    98             [NSCursor unhide];
    99         } else {
   100             [NSCursor hide];
   101         }
   102     }
   103 
   104     [pool release];
   105 
   106     return 0;
   107 }
   108 
   109 static void
   110 Cocoa_WarpMouse(SDL_Window * window, int x, int y)
   111 {
   112     CGPoint point;
   113 
   114     point.x = (CGFloat)window->x + x;
   115     point.y = (CGFloat)window->y + y;
   116     CGWarpMouseCursorPosition(point);
   117 }
   118 
   119 void
   120 Cocoa_InitMouse(_THIS)
   121 {
   122     SDL_Mouse *mouse = SDL_GetMouse();
   123     SDL_Cursor *cursor;
   124 
   125     mouse->CreateCursor = Cocoa_CreateCursor;
   126     mouse->ShowCursor = Cocoa_ShowCursor;
   127     mouse->WarpMouse = Cocoa_WarpMouse;
   128     mouse->FreeCursor = Cocoa_FreeCursor;
   129 
   130     cursor = Cocoa_CreateDefaultCursor();
   131     mouse->cursors = mouse->cur_cursor = cursor;
   132 }
   133 
   134 static int
   135 ConvertMouseButtonToSDL(int button)
   136 {
   137     switch (button)
   138     {
   139         case 0:
   140             return(SDL_BUTTON_LEFT);   /* 1 */
   141         case 1:
   142             return(SDL_BUTTON_RIGHT);  /* 3 */
   143         case 2:
   144             return(SDL_BUTTON_MIDDLE); /* 2 */
   145     }
   146     return button+1;
   147 }
   148 
   149 void
   150 Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
   151 {
   152     /* We're correctly using views even in fullscreen mode now */
   153 }
   154 
   155 void
   156 Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
   157 {
   158     float x = [event deltaX];
   159     float y = [event deltaY];
   160 
   161     if (x > 0) {
   162         x += 0.9f;
   163     } else if (x < 0) {
   164         x -= 0.9f;
   165     }
   166     if (y > 0) {
   167         y += 0.9f;
   168     } else if (y < 0) {
   169         y -= 0.9f;
   170     }
   171     SDL_SendMouseWheel(window, (int)x, (int)y);
   172 }
   173 
   174 void
   175 Cocoa_QuitMouse(_THIS)
   176 {
   177 }
   178 
   179 /* vi: set ts=4 sw=4 expandtab: */