src/video/cocoa/SDL_cocoawindow.m
changeset 5249 7a963be087ef
parent 5246 58265e606e4e
child 5253 9e70b360f423
equal deleted inserted replaced
5248:ff2564c24045 5249:7a963be087ef
   396     [listener mouseDown:theEvent];
   396     [listener mouseDown:theEvent];
   397 }
   397 }
   398 
   398 
   399 @end
   399 @end
   400 
   400 
       
   401 static unsigned int
       
   402 GetStyleMask(SDL_Window * window)
       
   403 {
       
   404     unsigned int style;
       
   405 
       
   406     if (window->flags & SDL_WINDOW_BORDERLESS) {
       
   407         style = NSBorderlessWindowMask;
       
   408     } else {
       
   409         style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
       
   410     }
       
   411     if (window->flags & SDL_WINDOW_RESIZABLE) {
       
   412         style |= NSResizableWindowMask;
       
   413     }
       
   414     return style;
       
   415 }
       
   416 
   401 static int
   417 static int
   402 SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
   418 SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
   403 {
   419 {
   404     NSAutoreleasePool *pool;
   420     NSAutoreleasePool *pool;
   405     SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
   421     SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
   406     SDL_WindowData *data;
   422     SDL_WindowData *data;
   407 
   423 
   408     /* Allocate the window data */
   424     /* Allocate the window data */
   409     data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
   425     data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
   410     if (!data) {
   426     if (!data) {
   411         SDL_OutOfMemory();
   427         SDL_OutOfMemory();
   412         return -1;
   428         return -1;
   413     }
   429     }
   414     data->window = window;
   430     data->window = window;
   422     data->listener = [[Cocoa_WindowListener alloc] init];
   438     data->listener = [[Cocoa_WindowListener alloc] init];
   423     [data->listener listen:data];
   439     [data->listener listen:data];
   424 
   440 
   425     /* Fill in the SDL window with the window data */
   441     /* Fill in the SDL window with the window data */
   426     {
   442     {
   427         SDL_Rect bounds;
       
   428         NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
   443         NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
   429         NSView *contentView = [[SDLView alloc] initWithFrame: rect
   444         NSView *contentView = [[SDLView alloc] initWithFrame: rect
   430                                                     listener: data->listener];
   445                                                     listener: data->listener];
   431 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
   446 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
   432         [contentView setAcceptsTouchEvents:YES];
   447         [contentView setAcceptsTouchEvents:YES];
   493     NSRect rect;
   508     NSRect rect;
   494     SDL_Rect bounds;
   509     SDL_Rect bounds;
   495     unsigned int style;
   510     unsigned int style;
   496 
   511 
   497     Cocoa_GetDisplayBounds(_this, display, &bounds);
   512     Cocoa_GetDisplayBounds(_this, display, &bounds);
   498     if ((window->flags & SDL_WINDOW_FULLSCREEN)
   513     if (SDL_WINDOWPOS_ISCENTERED(window->x)) {
   499         || SDL_WINDOWPOS_ISCENTERED(window->x)) {
       
   500         rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
   514         rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
   501     } else if (SDL_WINDOWPOS_ISUNDEFINED(window->x)) {
   515     } else if (SDL_WINDOWPOS_ISUNDEFINED(window->x)) {
   502         rect.origin.x = bounds.x;
   516         rect.origin.x = bounds.x;
   503     } else {
   517     } else {
   504         rect.origin.x = window->x;
   518         rect.origin.x = window->x;
   505     }
   519     }
   506     if ((window->flags & SDL_WINDOW_FULLSCREEN)
   520     if (SDL_WINDOWPOS_ISCENTERED(window->y)) {
   507         || SDL_WINDOWPOS_ISCENTERED(window->y)) {
       
   508         rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
   521         rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
   509     } else if (SDL_WINDOWPOS_ISUNDEFINED(window->y)) {
   522     } else if (SDL_WINDOWPOS_ISUNDEFINED(window->y)) {
   510         rect.origin.y = bounds.y;
   523         rect.origin.y = bounds.y;
   511     } else {
   524     } else {
   512         rect.origin.y = window->y;
   525         rect.origin.y = window->y;
   513     }
   526     }
   514     rect.size.width = window->w;
   527     rect.size.width = window->w;
   515     rect.size.height = window->h;
   528     rect.size.height = window->h;
   516     ConvertNSRect(&rect);
   529     ConvertNSRect(&rect);
   517 
   530 
   518     if (window->flags & SDL_WINDOW_BORDERLESS) {
   531     style = GetStyleMask(window);
   519         style = NSBorderlessWindowMask;
       
   520     } else {
       
   521         style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
       
   522     }
       
   523     if (window->flags & SDL_WINDOW_RESIZABLE) {
       
   524         style |= NSResizableWindowMask;
       
   525     }
       
   526 
   532 
   527     /* Figure out which screen to place this window */
   533     /* Figure out which screen to place this window */
   528     NSArray *screens = [NSScreen screens];
   534     NSArray *screens = [NSScreen screens];
   529     NSScreen *screen = nil;
   535     NSScreen *screen = nil;
   530     NSScreen *candidate;
   536     NSScreen *candidate;
   598     SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
   604     SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
   599     NSRect rect;
   605     NSRect rect;
   600     SDL_Rect bounds;
   606     SDL_Rect bounds;
   601 
   607 
   602     Cocoa_GetDisplayBounds(_this, display, &bounds);
   608     Cocoa_GetDisplayBounds(_this, display, &bounds);
   603     if ((window->flags & SDL_WINDOW_FULLSCREEN)
   609     if (SDL_WINDOWPOS_ISCENTERED(window->x)) {
   604         || SDL_WINDOWPOS_ISCENTERED(window->x)) {
       
   605         rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
   610         rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
   606     } else {
   611     } else {
   607         rect.origin.x = window->x;
   612         rect.origin.x = window->x;
   608     }
   613     }
   609     if ((window->flags & SDL_WINDOW_FULLSCREEN)
   614     if (SDL_WINDOWPOS_ISCENTERED(window->y)) {
   610         || SDL_WINDOWPOS_ISCENTERED(window->y)) {
       
   611         rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
   615         rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
   612     } else {
   616     } else {
   613         rect.origin.y = window->y;
   617         rect.origin.y = window->y;
   614     }
   618     }
   615     rect.size.width = window->w;
   619     rect.size.width = window->w;
   694     if ([nswindow isMiniaturized]) {
   698     if ([nswindow isMiniaturized]) {
   695         [nswindow deminiaturize:nil];
   699         [nswindow deminiaturize:nil];
   696     } else if ([nswindow isZoomed]) {
   700     } else if ([nswindow isZoomed]) {
   697         [nswindow zoom:nil];
   701         [nswindow zoom:nil];
   698     }
   702     }
       
   703     [pool release];
       
   704 }
       
   705 
       
   706 void
       
   707 Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window)
       
   708 {
       
   709     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
   710     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
       
   711     NSWindow *nswindow = data->nswindow;
       
   712     SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
       
   713     NSRect rect;
       
   714     unsigned int style;
       
   715 
       
   716     if (FULLSCREEN_VISIBLE(window)) {
       
   717         SDL_Rect bounds;
       
   718 
       
   719         Cocoa_GetDisplayBounds(_this, display, &bounds);
       
   720         rect.origin.x = bounds.x;
       
   721         rect.origin.y = bounds.y;
       
   722         rect.size.width = bounds.w;
       
   723         rect.size.height = bounds.h;
       
   724         ConvertNSRect(&rect);
       
   725 
       
   726         style = NSBorderlessWindowMask;
       
   727     } else {
       
   728         rect.origin.x = window->windowed.x;
       
   729         rect.origin.y = window->windowed.y;
       
   730         rect.size.width = window->windowed.w;
       
   731         rect.size.height = window->windowed.h;
       
   732         /* FIXME: This calculation is wrong, we're changing the origin */
       
   733         ConvertNSRect(&rect);
       
   734 
       
   735         style = GetStyleMask(window);
       
   736     }
       
   737 
       
   738     [nswindow setStyleMask:style];
       
   739     [nswindow setContentSize:rect.size];
       
   740     rect = [nswindow frameRectForContentRect:rect];
       
   741     [nswindow setFrameOrigin:rect.origin];
       
   742 
       
   743 #ifdef FULLSCREEN_TOGGLEABLE
       
   744     if (FULLSCREEN_VISIBLE(window)) {
       
   745         /* OpenGL is rendering to the window, so make it visible! */
       
   746         [nswindow setLevel:CGShieldingWindowLevel()];
       
   747     } else {
       
   748         [nswindow setLevel:kCGNormalWindowLevel];
       
   749     }
       
   750 #endif
       
   751     [nswindow makeKeyAndOrderFront:nil];
       
   752 
   699     [pool release];
   753     [pool release];
   700 }
   754 }
   701 
   755 
   702 void
   756 void
   703 Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
   757 Cocoa_SetWindowGrab(_THIS, SDL_Window * window)