src/video/cocoa/SDL_cocoawindow.m
author Sam Lantinga <slouken@libsdl.org>
Sat, 29 Jul 2006 23:00:15 +0000
changeset 1958 5fc6fb0fb605
parent 1957 c7803e4c21d3
child 1959 25d6537feea4
permissions -rw-r--r--
Added right/other drag code for Cocoa windows
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 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_syswm.h"
    25 #include "../SDL_sysvideo.h"
    26 #include "../../events/SDL_keyboard_c.h"
    27 #include "../../events/SDL_mouse_c.h"
    28 #include "../../events/SDL_windowevents_c.h"
    29 
    30 #include "SDL_cocoavideo.h"
    31 
    32 static __inline__ void ConvertNSRect(NSRect *r)
    33 {
    34     /* FIXME: Cache the display used for this window */
    35     r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
    36 }
    37 
    38 @implementation Cocoa_WindowListener
    39 
    40 - (void)listen:(SDL_WindowData *)data
    41 {
    42     NSNotificationCenter *center;
    43 
    44     _data = data;
    45 
    46     center = [NSNotificationCenter defaultCenter];
    47 
    48     [_data->window setNextResponder:self];
    49     if ([_data->window delegate] != nil) {
    50         [center addObserver:self selector:@selector(windowDisExpose:) name:NSWindowDidExposeNotification object:_data->window];
    51         [center addObserver:self selector:@selector(windowDidMove:) name:NSWindowDidMoveNotification object:_data->window];
    52         [center addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:_data->window];
    53         [center addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:_data->window];
    54         [center addObserver:self selector:@selector(windowDidDeminiaturize:) name:NSWindowDidDeminiaturizeNotification object:_data->window];
    55         [center addObserver:self selector:@selector(windowDidBecomeKey:) name:NSWindowDidBecomeKeyNotification object:_data->window];
    56         [center addObserver:self selector:@selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:_data->window];
    57     } else {
    58         [_data->window setDelegate:self];
    59     }
    60     [center addObserver:self selector:@selector(windowDidHide:) name:NSApplicationDidHideNotification object:NSApp];
    61     [center addObserver:self selector:@selector(windowDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp];
    62 
    63     [_data->window setAcceptsMouseMovedEvents:YES];
    64 }
    65 
    66 - (void)close
    67 {
    68     NSNotificationCenter *center;
    69 
    70     center = [NSNotificationCenter defaultCenter];
    71 
    72     [_data->window setNextResponder:nil];
    73     if ([_data->window delegate] != self) {
    74         [center removeObserver:self name:NSWindowDidExposeNotification object:_data->window];
    75         [center removeObserver:self name:NSWindowDidMoveNotification object:_data->window];
    76         [center removeObserver:self name:NSWindowDidResizeNotification object:_data->window];
    77         [center removeObserver:self name:NSWindowDidMiniaturizeNotification object:_data->window];
    78         [center removeObserver:self name:NSWindowDidDeminiaturizeNotification object:_data->window];
    79         [center removeObserver:self name:NSWindowDidBecomeKeyNotification object:_data->window];
    80         [center removeObserver:self name:NSWindowDidResignKeyNotification object:_data->window];
    81     } else {
    82         [_data->window setDelegate:nil];
    83     }
    84     [center removeObserver:self name:NSApplicationDidHideNotification object:NSApp];
    85     [center removeObserver:self name:NSApplicationDidUnhideNotification object:NSApp];
    86 }
    87 
    88 - (BOOL)windowShouldClose:(id)sender
    89 {
    90     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_CLOSE, 0, 0);
    91     return NO;
    92 }
    93 
    94 - (void)windowDidExpose:(NSNotification *)aNotification
    95 {
    96     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_EXPOSED, 0, 0);
    97 }
    98 
    99 - (void)windowDidMove:(NSNotification *)aNotification
   100 {
   101     int x, y;
   102     NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
   103     ConvertNSRect(&rect);
   104     x = (int)rect.origin.x;
   105     y = (int)rect.origin.y;
   106     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_MOVED, x, y);
   107 }
   108 
   109 - (void)windowDidResize:(NSNotification *)aNotification
   110 {
   111     int w, h;
   112     NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
   113     w = (int)rect.size.width;
   114     h = (int)rect.size.height;
   115     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_RESIZED, w, h);
   116 }
   117 
   118 - (void)windowDidMiniaturize:(NSNotification *)aNotification
   119 {
   120     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   121 }
   122 
   123 - (void)windowDidDeminiaturize:(NSNotification *)aNotification
   124 {
   125     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_RESTORED, 0, 0);
   126 }
   127 
   128 - (void)windowDidBecomeKey:(NSNotification *)aNotification
   129 {
   130     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_FOCUS_GAINED, 0, 0);
   131 }
   132 
   133 - (void)windowDidResignKey:(NSNotification *)aNotification
   134 {
   135     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
   136 }
   137 
   138 - (void)windowDidHide:(NSNotification *)aNotification
   139 {
   140     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_HIDDEN, 0, 0);
   141 }
   142 
   143 - (void)windowDidUnhide:(NSNotification *)aNotification
   144 {
   145     SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_SHOWN, 0, 0);
   146 }
   147 
   148 - (void)mouseDown:(NSEvent *)theEvent
   149 {
   150     int index;
   151 
   152     index = _data->videodata->mouse;
   153     SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_LEFT);
   154 }
   155 
   156 - (void)rightMouseDown:(NSEvent *)theEvent
   157 {
   158     int index;
   159 
   160     index = _data->videodata->mouse;
   161     SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_RIGHT);
   162 }
   163 
   164 - (void)otherMouseDown:(NSEvent *)theEvent
   165 {
   166     int index;
   167 
   168     index = _data->videodata->mouse;
   169     SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_MIDDLE);
   170 }
   171 
   172 - (void)mouseUp:(NSEvent *)theEvent
   173 {
   174     int index;
   175 
   176     index = _data->videodata->mouse;
   177     SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_LEFT);
   178 }
   179 
   180 - (void)rightMouseUp:(NSEvent *)theEvent
   181 {
   182     int index;
   183 
   184     index = _data->videodata->mouse;
   185     SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_RIGHT);
   186 }
   187 
   188 - (void)otherMouseUp:(NSEvent *)theEvent
   189 {
   190     int index;
   191 
   192     index = _data->videodata->mouse;
   193     SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_MIDDLE);
   194 }
   195 
   196 - (void)mouseMoved:(NSEvent *)theEvent
   197 {
   198     SDL_Window *window = SDL_GetWindowFromID(_data->windowID);
   199     int index;
   200     SDL_Mouse *mouse;
   201     NSPoint point;
   202     NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
   203 
   204     index = _data->videodata->mouse;
   205     mouse = SDL_GetMouse(index);
   206     if (mouse->focus != _data->windowID) {
   207         SDL_SetMouseFocus(index, _data->windowID);
   208     }
   209 
   210     point = [NSEvent mouseLocation];
   211     point.x = point.x - rect.origin.x;
   212     point.y = rect.size.height - (point.y - rect.origin.y);
   213     SDL_SendMouseMotion(index, 0, (int)point.x, (int)point.y);
   214 }
   215 
   216 - (void)mouseDragged:(NSEvent *)theEvent
   217 {
   218     [self mouseMoved:theEvent];
   219 }
   220 
   221 - (void)rightMouseDragged:(NSEvent *)theEvent
   222 {
   223     [self mouseMoved:theEvent];
   224 }
   225 
   226 - (void)otherMouseDragged:(NSEvent *)theEvent
   227 {
   228     [self mouseMoved:theEvent];
   229 }
   230 
   231 - (void)scrollWheel:(NSEvent *)theEvent
   232 {
   233     int index;
   234 
   235     index = _data->videodata->mouse;
   236     SDL_SendMouseWheel(index, (int)([theEvent deltaY]+0.9f));
   237 }
   238 
   239 - (void)keyDown:(NSEvent *)theEvent
   240 {
   241 fprintf(stderr, "keyDown\n");
   242 }
   243 
   244 - (void)keyUp:(NSEvent *)theEvent
   245 {
   246 fprintf(stderr, "keyUp\n");
   247 }
   248 
   249 @end
   250 
   251 static int
   252 SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
   253 {
   254     NSAutoreleasePool *pool;
   255     SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
   256     SDL_WindowData *data;
   257 
   258     /* Allocate the window data */
   259     data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
   260     if (!data) {
   261         SDL_OutOfMemory();
   262         return -1;
   263     }
   264     data->windowID = window->id;
   265     data->window = nswindow;
   266     data->created = created;
   267     data->videodata = videodata;
   268 
   269     pool = [[NSAutoreleasePool alloc] init];
   270 
   271     /* Create an event listener for the window */
   272     data->listener = [[Cocoa_WindowListener alloc] init];
   273     [data->listener listen:data];
   274 
   275     /* Fill in the SDL window with the window data */
   276     {
   277         NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
   278         ConvertNSRect(&rect);
   279         window->x = (int)rect.origin.x;
   280         window->y = (int)rect.origin.y;
   281         window->w = (int)rect.size.width;
   282         window->h = (int)rect.size.height;
   283     }
   284     if ([nswindow isVisible]) {
   285         window->flags |= SDL_WINDOW_SHOWN;
   286     } else {
   287         window->flags &= ~SDL_WINDOW_SHOWN;
   288     }
   289     {
   290         unsigned int style = [nswindow styleMask];
   291 
   292         if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
   293             window->flags |= SDL_WINDOW_BORDERLESS;
   294         } else {
   295             window->flags &= ~SDL_WINDOW_BORDERLESS;
   296         }
   297         if (style & NSResizableWindowMask) {
   298             window->flags |= SDL_WINDOW_RESIZABLE;
   299         } else {
   300             window->flags &= ~SDL_WINDOW_RESIZABLE;
   301         }
   302     }
   303     if ([nswindow isZoomed]) {
   304         window->flags |= SDL_WINDOW_MAXIMIZED;
   305     } else {
   306         window->flags &= ~SDL_WINDOW_MAXIMIZED;
   307     }
   308     if ([nswindow isMiniaturized]) {
   309         window->flags |= SDL_WINDOW_MINIMIZED;
   310     } else {
   311         window->flags &= ~SDL_WINDOW_MINIMIZED;
   312     }
   313     if ([nswindow isKeyWindow]) {
   314         int index = data->videodata->keyboard;
   315         window->flags |= SDL_WINDOW_INPUT_FOCUS;
   316         SDL_SetKeyboardFocus(index, data->windowID);
   317 
   318         if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
   319             /* FIXME */
   320         }
   321     }
   322 
   323     /* All done! */
   324     [pool release];
   325     window->driverdata = data;
   326     return 0;
   327 }
   328 
   329 int
   330 Cocoa_CreateWindow(_THIS, SDL_Window * window)
   331 {
   332     NSAutoreleasePool *pool;
   333     NSWindow *nswindow;
   334     NSRect rect;
   335     unsigned int style;
   336     NSString *title;
   337     int status;
   338 
   339     pool = [[NSAutoreleasePool alloc] init];
   340 
   341     if (window->x == SDL_WINDOWPOS_CENTERED) {
   342         rect.origin.x = (CGDisplayPixelsWide(kCGDirectMainDisplay) - window->w) / 2;
   343     } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
   344         rect.origin.x = 0;
   345     } else {
   346         rect.origin.x = window->x;
   347     }
   348     if (window->y == SDL_WINDOWPOS_CENTERED) {
   349         rect.origin.y = (CGDisplayPixelsHigh(kCGDirectMainDisplay) - window->h) / 2;
   350     } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
   351         rect.origin.y = 0;
   352     } else {
   353         rect.origin.y = window->y;
   354     }
   355     rect.size.width = window->w;
   356     rect.size.height = window->h;
   357     ConvertNSRect(&rect);
   358 
   359     if (window->flags & SDL_WINDOW_BORDERLESS) {
   360         style = NSBorderlessWindowMask;
   361     } else {
   362         style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
   363     }
   364     if (window->flags & SDL_WINDOW_RESIZABLE) {
   365         style |= NSResizableWindowMask;
   366     }
   367 
   368     nswindow = [[NSWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE];
   369 
   370     [pool release];
   371 
   372     if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
   373         [nswindow release];
   374         return -1;
   375     }
   376 #ifdef SDL_VIDEO_OPENGL_CGL
   377     if (window->flags & SDL_WINDOW_OPENGL) {
   378         if (Cocoa_GL_SetupWindow(_this, window) < 0) {
   379             Cocoa_DestroyWindow(_this, window);
   380             return -1;
   381         }
   382     }
   383 #endif
   384     return 0;
   385 }
   386 
   387 int
   388 Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
   389 {
   390     NSAutoreleasePool *pool;
   391     NSWindow *nswindow = (NSWindow *) data;
   392     NSString *title;
   393     int status;
   394 
   395     pool = [[NSAutoreleasePool alloc] init];
   396 
   397     /* Query the title from the existing window */
   398     title = [nswindow title];
   399     if (title) {
   400         window->title = SDL_strdup([title UTF8String]);
   401     }
   402 
   403     [pool release];
   404 
   405     return SetupWindowData(_this, window, nswindow, SDL_FALSE);
   406 }
   407 
   408 void
   409 Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
   410 {
   411     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   412     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   413     NSString *string;
   414 
   415     if(window->title) {
   416         string = [[NSString alloc] initWithUTF8String:window->title];
   417     } else {
   418         string = [[NSString alloc] init];
   419     }
   420     [nswindow setTitle:string];
   421     [string release];
   422 
   423     [pool release];
   424 }
   425 
   426 void
   427 Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
   428 {
   429     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   430     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   431     NSRect rect;
   432 
   433     rect.origin.x = window->x;
   434     rect.origin.y = window->y;
   435     rect.size.width = window->w;
   436     rect.size.height = window->h;
   437     ConvertNSRect(&rect);
   438     rect = [nswindow frameRectForContentRect:rect];
   439     [nswindow setFrameOrigin:rect.origin];
   440     [pool release];
   441 }
   442 
   443 void
   444 Cocoa_SetWindowSize(_THIS, SDL_Window * window)
   445 {
   446     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   447     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   448     NSSize size;
   449 
   450     size.width = window->w;
   451     size.height = window->h;
   452     [nswindow setContentSize:size];
   453     [pool release];
   454 }
   455 
   456 void
   457 Cocoa_ShowWindow(_THIS, SDL_Window * window)
   458 {
   459     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   460     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   461 
   462     if (![nswindow isMiniaturized]) {
   463         [nswindow makeKeyAndOrderFront:nil];
   464     }
   465     [pool release];
   466 }
   467 
   468 void
   469 Cocoa_HideWindow(_THIS, SDL_Window * window)
   470 {
   471     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   472     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   473 
   474     [nswindow orderOut:nil];
   475     [pool release];
   476 }
   477 
   478 void
   479 Cocoa_RaiseWindow(_THIS, SDL_Window * window)
   480 {
   481     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   482     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   483 
   484     [nswindow makeKeyAndOrderFront:nil];
   485     [pool release];
   486 }
   487 
   488 void
   489 Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
   490 {
   491     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   492     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   493 
   494     [nswindow zoom:nil];
   495     [pool release];
   496 }
   497 
   498 void
   499 Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
   500 {
   501     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   502     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   503 
   504     [nswindow miniaturize:nil];
   505     [pool release];
   506 }
   507 
   508 void
   509 Cocoa_RestoreWindow(_THIS, SDL_Window * window)
   510 {
   511     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   512     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   513 
   514     if ([nswindow isMiniaturized]) {
   515         [nswindow deminiaturize:nil];
   516     } else if ([nswindow isZoomed]) {
   517         [nswindow zoom:nil];
   518     }
   519     [pool release];
   520 }
   521 
   522 void
   523 Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
   524 {
   525     if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
   526         (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
   527         /* FIXME: Grab mouse */
   528     } else {
   529         /* FIXME: Release mouse */
   530     }
   531 }
   532 
   533 void
   534 Cocoa_DestroyWindow(_THIS, SDL_Window * window)
   535 {
   536     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   537     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   538 
   539     if (data) {
   540 #ifdef SDL_VIDEO_OPENGL_CGL
   541         if (window->flags & SDL_WINDOW_OPENGL) {
   542             Cocoa_GL_CleanupWindow(_this, window);
   543         }
   544 #endif
   545         [data->listener close];
   546         [data->listener release];
   547         if (data->created) {
   548             [data->window close];
   549         }
   550         SDL_free(data);
   551     }
   552     [pool release];
   553 }
   554 
   555 SDL_bool
   556 Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
   557 {
   558     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   559 
   560     if (info->version.major <= SDL_MAJOR_VERSION) {
   561         //info->window = nswindow;
   562         return SDL_TRUE;
   563     } else {
   564         SDL_SetError("Application not compiled with SDL %d.%d\n",
   565                      SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
   566         return SDL_FALSE;
   567     }
   568 }
   569 
   570 /* vi: set ts=4 sw=4 expandtab: */