src/video/cocoa/SDL_cocoawindow.m
changeset 6848 478ecc8a58b3
parent 6832 156e608ec4ef
child 6885 700f1b25f77f
equal deleted inserted replaced
6847:c0fa29d3fade 6848:478ecc8a58b3
   505 }
   505 }
   506 
   506 
   507 static int
   507 static int
   508 SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
   508 SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
   509 {
   509 {
       
   510     NSAutoreleasePool *pool;
   510     SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
   511     SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
   511     SDL_WindowData *data;
   512     SDL_WindowData *data;
   512 
   513 
   513     /* Allocate the window data */
   514     /* Allocate the window data */
   514     data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
   515     data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
   519     data->window = window;
   520     data->window = window;
   520     data->nswindow = nswindow;
   521     data->nswindow = nswindow;
   521     data->created = created;
   522     data->created = created;
   522     data->videodata = videodata;
   523     data->videodata = videodata;
   523 
   524 
   524     @autoreleasepool {
   525     pool = [[NSAutoreleasePool alloc] init];
   525         /* Create an event listener for the window */
   526 
   526         data->listener = [[Cocoa_WindowListener alloc] init];
   527     /* Create an event listener for the window */
   527 
   528     data->listener = [[Cocoa_WindowListener alloc] init];
   528         /* Fill in the SDL window with the window data */
   529 
   529         {
   530     /* Fill in the SDL window with the window data */
   530             NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
   531     {
   531             ConvertNSRect(&rect);
   532         NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
   532             window->x = (int)rect.origin.x;
   533         ConvertNSRect(&rect);
   533             window->y = (int)rect.origin.y;
   534         window->x = (int)rect.origin.x;
   534             window->w = (int)rect.size.width;
   535         window->y = (int)rect.origin.y;
   535             window->h = (int)rect.size.height;
   536         window->w = (int)rect.size.width;
   536         }
   537         window->h = (int)rect.size.height;
   537 
   538     }
   538         /* Set up the listener after we create the view */
   539 
   539         [data->listener listen:data];
   540     /* Set up the listener after we create the view */
   540 
   541     [data->listener listen:data];
   541         if ([nswindow isVisible]) {
   542 
   542             window->flags |= SDL_WINDOW_SHOWN;
   543     if ([nswindow isVisible]) {
       
   544         window->flags |= SDL_WINDOW_SHOWN;
       
   545     } else {
       
   546         window->flags &= ~SDL_WINDOW_SHOWN;
       
   547     }
       
   548     {
       
   549         unsigned int style = [nswindow styleMask];
       
   550 
       
   551         if (style == NSBorderlessWindowMask) {
       
   552             window->flags |= SDL_WINDOW_BORDERLESS;
   543         } else {
   553         } else {
   544             window->flags &= ~SDL_WINDOW_SHOWN;
   554             window->flags &= ~SDL_WINDOW_BORDERLESS;
   545         }
   555         }
   546         {
   556         if (style & NSResizableWindowMask) {
   547             unsigned int style = [nswindow styleMask];
   557             window->flags |= SDL_WINDOW_RESIZABLE;
   548 
       
   549             if (style == NSBorderlessWindowMask) {
       
   550                 window->flags |= SDL_WINDOW_BORDERLESS;
       
   551             } else {
       
   552                 window->flags &= ~SDL_WINDOW_BORDERLESS;
       
   553             }
       
   554             if (style & NSResizableWindowMask) {
       
   555                 window->flags |= SDL_WINDOW_RESIZABLE;
       
   556             } else {
       
   557                 window->flags &= ~SDL_WINDOW_RESIZABLE;
       
   558             }
       
   559         }
       
   560         /* isZoomed always returns true if the window is not resizable */
       
   561         if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
       
   562             window->flags |= SDL_WINDOW_MAXIMIZED;
       
   563         } else {
   558         } else {
   564             window->flags &= ~SDL_WINDOW_MAXIMIZED;
   559             window->flags &= ~SDL_WINDOW_RESIZABLE;
   565         }
   560         }
   566         if ([nswindow isMiniaturized]) {
   561     }
   567             window->flags |= SDL_WINDOW_MINIMIZED;
   562     /* isZoomed always returns true if the window is not resizable */
   568         } else {
   563     if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
   569             window->flags &= ~SDL_WINDOW_MINIMIZED;
   564         window->flags |= SDL_WINDOW_MAXIMIZED;
   570         }
   565     } else {
   571         if ([nswindow isKeyWindow]) {
   566         window->flags &= ~SDL_WINDOW_MAXIMIZED;
   572             window->flags |= SDL_WINDOW_INPUT_FOCUS;
   567     }
   573             SDL_SetKeyboardFocus(data->window);
   568     if ([nswindow isMiniaturized]) {
   574         }
   569         window->flags |= SDL_WINDOW_MINIMIZED;
   575 
   570     } else {
   576         /* All done! */
   571         window->flags &= ~SDL_WINDOW_MINIMIZED;
   577         window->driverdata = data;
   572     }
   578         return 0;
   573     if ([nswindow isKeyWindow]) {
   579     }
   574         window->flags |= SDL_WINDOW_INPUT_FOCUS;
       
   575         SDL_SetKeyboardFocus(data->window);
       
   576     }
       
   577 
       
   578     /* All done! */
       
   579     [pool release];
       
   580     window->driverdata = data;
       
   581     return 0;
   580 }
   582 }
   581 
   583 
   582 int
   584 int
   583 Cocoa_CreateWindow(_THIS, SDL_Window * window)
   585 Cocoa_CreateWindow(_THIS, SDL_Window * window)
   584 {
   586 {
   585     @autoreleasepool {
   587     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   586         NSWindow *nswindow;
   588     NSWindow *nswindow;
   587         SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
   589     SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
   588         NSRect rect;
   590     NSRect rect;
   589         SDL_Rect bounds;
   591     SDL_Rect bounds;
   590         unsigned int style;
   592     unsigned int style;
   591 
   593 
   592         Cocoa_GetDisplayBounds(_this, display, &bounds);
   594     Cocoa_GetDisplayBounds(_this, display, &bounds);
   593         rect.origin.x = window->x;
   595     rect.origin.x = window->x;
   594         rect.origin.y = window->y;
   596     rect.origin.y = window->y;
   595         rect.size.width = window->w;
   597     rect.size.width = window->w;
   596         rect.size.height = window->h;
   598     rect.size.height = window->h;
   597         ConvertNSRect(&rect);
   599     ConvertNSRect(&rect);
   598 
   600 
   599         style = GetWindowStyle(window);
   601     style = GetWindowStyle(window);
   600 
   602 
   601         /* Figure out which screen to place this window */
   603     /* Figure out which screen to place this window */
   602         NSArray *screens = [NSScreen screens];
   604     NSArray *screens = [NSScreen screens];
   603         NSScreen *screen = nil;
   605     NSScreen *screen = nil;
   604         NSScreen *candidate;
   606     NSScreen *candidate;
   605         int i, count = [screens count];
   607     int i, count = [screens count];
   606         for (i = 0; i < count; ++i) {
   608     for (i = 0; i < count; ++i) {
   607             candidate = [screens objectAtIndex:i];
   609         candidate = [screens objectAtIndex:i];
   608             NSRect screenRect = [candidate frame];
   610         NSRect screenRect = [candidate frame];
   609             if (rect.origin.x >= screenRect.origin.x &&
   611         if (rect.origin.x >= screenRect.origin.x &&
   610                 rect.origin.x < screenRect.origin.x + screenRect.size.width &&
   612             rect.origin.x < screenRect.origin.x + screenRect.size.width &&
   611                 rect.origin.y >= screenRect.origin.y &&
   613             rect.origin.y >= screenRect.origin.y &&
   612                 rect.origin.y < screenRect.origin.y + screenRect.size.height) {
   614             rect.origin.y < screenRect.origin.y + screenRect.size.height) {
   613                 screen = candidate;
   615             screen = candidate;
   614                 rect.origin.x -= screenRect.origin.x;
   616             rect.origin.x -= screenRect.origin.x;
   615                 rect.origin.y -= screenRect.origin.y;
   617             rect.origin.y -= screenRect.origin.y;
   616             }
   618         }
   617         }
   619     }
   618         nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:YES screen:screen];
   620     nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:YES screen:screen];
   619 
   621 
   620         // Create a default view for this window
   622     // Create a default view for this window
   621         rect = [nswindow contentRectForFrameRect:[nswindow frame]];
   623     rect = [nswindow contentRectForFrameRect:[nswindow frame]];
   622         NSView *contentView = [[SDLView alloc] initWithFrame:rect];
   624     NSView *contentView = [[SDLView alloc] initWithFrame:rect];
   623         [nswindow setContentView: contentView];
   625     [nswindow setContentView: contentView];
   624         [contentView release];
   626     [contentView release];
   625 
   627 
   626         if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
   628     [pool release];
   627             [nswindow release];
   629 
   628             return -1;
   630     if (SetupWindowData(_this, window, nswindow, SDL_TRUE) < 0) {
   629         }
   631         [nswindow release];
   630         return 0;
   632         return -1;
   631     }
   633     }
       
   634     return 0;
   632 }
   635 }
   633 
   636 
   634 int
   637 int
   635 Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
   638 Cocoa_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
   636 {
   639 {
       
   640     NSAutoreleasePool *pool;
   637     NSWindow *nswindow = (NSWindow *) data;
   641     NSWindow *nswindow = (NSWindow *) data;
   638     NSString *title;
   642     NSString *title;
   639 
   643 
   640     @autoreleasepool {
   644     pool = [[NSAutoreleasePool alloc] init];
   641         /* Query the title from the existing window */
   645 
   642         title = [nswindow title];
   646     /* Query the title from the existing window */
   643         if (title) {
   647     title = [nswindow title];
   644             window->title = SDL_strdup([title UTF8String]);
   648     if (title) {
   645         }
   649         window->title = SDL_strdup([title UTF8String]);
   646     }
   650     }
       
   651 
       
   652     [pool release];
   647 
   653 
   648     return SetupWindowData(_this, window, nswindow, SDL_FALSE);
   654     return SetupWindowData(_this, window, nswindow, SDL_FALSE);
   649 }
   655 }
   650 
   656 
   651 void
   657 void
   652 Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
   658 Cocoa_SetWindowTitle(_THIS, SDL_Window * window)
   653 {
   659 {
   654     @autoreleasepool {
   660     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   655         NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   661     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   656         NSString *string;
   662     NSString *string;
   657 
   663 
   658         if(window->title) {
   664     if(window->title) {
   659             string = [[NSString alloc] initWithUTF8String:window->title];
   665         string = [[NSString alloc] initWithUTF8String:window->title];
   660         } else {
   666     } else {
   661             string = [[NSString alloc] init];
   667         string = [[NSString alloc] init];
   662         }
   668     }
   663         [nswindow setTitle:string];
   669     [nswindow setTitle:string];
   664         [string release];
   670     [string release];
   665     }
   671 
       
   672     [pool release];
   666 }
   673 }
   667 
   674 
   668 void
   675 void
   669 Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
   676 Cocoa_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
   670 {
   677 {
   671     @autoreleasepool {
   678     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   672         NSImage *nsimage = Cocoa_CreateImage(icon);
   679     NSImage *nsimage = Cocoa_CreateImage(icon);
   673 
   680 
   674         if (nsimage) {
   681     if (nsimage) {
   675             [NSApp setApplicationIconImage:nsimage];
   682         [NSApp setApplicationIconImage:nsimage];
   676         }
   683     }
   677     }
   684 
       
   685     [pool release];
   678 }
   686 }
   679 
   687 
   680 void
   688 void
   681 Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
   689 Cocoa_SetWindowPosition(_THIS, SDL_Window * window)
   682 {
   690 {
   683     @autoreleasepool {
   691     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   684         NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   692     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   685         NSRect rect;
   693     NSRect rect;
   686         Uint32 moveHack;
   694     Uint32 moveHack;
   687 
   695 
   688         rect.origin.x = window->x;
   696     rect.origin.x = window->x;
   689         rect.origin.y = window->y;
   697     rect.origin.y = window->y;
   690         rect.size.width = window->w;
   698     rect.size.width = window->w;
   691         rect.size.height = window->h;
   699     rect.size.height = window->h;
   692         ConvertNSRect(&rect);
   700     ConvertNSRect(&rect);
   693 
   701 
   694         moveHack = s_moveHack;
   702     moveHack = s_moveHack;
   695         s_moveHack = 0;
   703     s_moveHack = 0;
   696         [nswindow setFrameOrigin:rect.origin];
   704     [nswindow setFrameOrigin:rect.origin];
   697         s_moveHack = moveHack;
   705     s_moveHack = moveHack;
   698 
   706 
   699         if (window == _this->current_glwin) {
   707     if (window == _this->current_glwin) {
   700             [((NSOpenGLContext *) _this->current_glctx) update];
   708         [((NSOpenGLContext *) _this->current_glctx) update];
   701         }
   709     }
   702     }
   710 
       
   711     [pool release];
   703 }
   712 }
   704 
   713 
   705 void
   714 void
   706 Cocoa_SetWindowSize(_THIS, SDL_Window * window)
   715 Cocoa_SetWindowSize(_THIS, SDL_Window * window)
   707 {
   716 {
   708     @autoreleasepool {
   717     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   709         SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
   718     SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
   710         NSWindow *nswindow = windata->nswindow;
   719     NSWindow *nswindow = windata->nswindow;
   711         NSSize size;
   720     NSSize size;
   712 
   721 
   713         size.width = window->w;
   722     size.width = window->w;
   714         size.height = window->h;
   723     size.height = window->h;
   715         [nswindow setContentSize:size];
   724     [nswindow setContentSize:size];
   716 
   725 
   717         if (window == _this->current_glwin) {
   726     if (window == _this->current_glwin) {
   718             [((NSOpenGLContext *) _this->current_glctx) update];
   727         [((NSOpenGLContext *) _this->current_glctx) update];
   719         }
   728     }
   720     }
   729 
       
   730     [pool release];
   721 }
   731 }
   722 
   732 
   723 void
   733 void
   724 Cocoa_SetWindowMinimumSize(_THIS, SDL_Window * window)
   734 Cocoa_SetWindowMinimumSize(_THIS, SDL_Window * window)
   725 {
   735 {
   726     @autoreleasepool {
   736     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   727         SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
   737     SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
   728             
   738         
   729         NSSize minSize;
   739     NSSize minSize;
   730         minSize.width = window->min_w;
   740     minSize.width = window->min_w;
   731         minSize.height = window->min_h;
   741     minSize.height = window->min_h;
   732             
   742         
   733         [windata->nswindow setContentMinSize:minSize];
   743     [windata->nswindow setContentMinSize:minSize];
   734     }
   744     
       
   745     [pool release];
   735 }
   746 }
   736 
   747 
   737 void
   748 void
   738 Cocoa_SetWindowMaximumSize(_THIS, SDL_Window * window)
   749 Cocoa_SetWindowMaximumSize(_THIS, SDL_Window * window)
   739 {
   750 {
   740     @autoreleasepool {
   751     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   741         SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
   752     SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
   742             
   753         
   743         NSSize maxSize;
   754     NSSize maxSize;
   744         maxSize.width = window->max_w;
   755     maxSize.width = window->max_w;
   745         maxSize.height = window->max_h;
   756     maxSize.height = window->max_h;
   746             
   757         
   747         [windata->nswindow setContentMaxSize:maxSize];
   758     [windata->nswindow setContentMaxSize:maxSize];
   748     }
   759     
       
   760     [pool release];
   749 }
   761 }
   750 
   762 
   751 void
   763 void
   752 Cocoa_ShowWindow(_THIS, SDL_Window * window)
   764 Cocoa_ShowWindow(_THIS, SDL_Window * window)
   753 {
   765 {
   754     @autoreleasepool {
   766     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   755         NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   767     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   756 
   768 
   757         if (![nswindow isMiniaturized]) {
   769     if (![nswindow isMiniaturized]) {
   758             [nswindow makeKeyAndOrderFront:nil];
   770         [nswindow makeKeyAndOrderFront:nil];
   759         }
   771     }
   760     }
   772     [pool release];
   761 }
   773 }
   762 
   774 
   763 void
   775 void
   764 Cocoa_HideWindow(_THIS, SDL_Window * window)
   776 Cocoa_HideWindow(_THIS, SDL_Window * window)
   765 {
   777 {
   766     @autoreleasepool {
   778     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   767         NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   779     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   768         [nswindow orderOut:nil];
   780 
   769     }
   781     [nswindow orderOut:nil];
       
   782     [pool release];
   770 }
   783 }
   771 
   784 
   772 void
   785 void
   773 Cocoa_RaiseWindow(_THIS, SDL_Window * window)
   786 Cocoa_RaiseWindow(_THIS, SDL_Window * window)
   774 {
   787 {
   775     @autoreleasepool {
   788     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   776         NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   789     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   777         [nswindow makeKeyAndOrderFront:nil];
   790 
   778     }
   791     [nswindow makeKeyAndOrderFront:nil];
       
   792     [pool release];
   779 }
   793 }
   780 
   794 
   781 void
   795 void
   782 Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
   796 Cocoa_MaximizeWindow(_THIS, SDL_Window * window)
   783 {
   797 {
   784     @autoreleasepool {
   798     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   785         NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   799     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
       
   800 
       
   801     [nswindow zoom:nil];
       
   802 
       
   803     if (window == _this->current_glwin) {
       
   804         [((NSOpenGLContext *) _this->current_glctx) update];
       
   805     }
       
   806 
       
   807     [pool release];
       
   808 }
       
   809 
       
   810 void
       
   811 Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
       
   812 {
       
   813     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
   814     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
       
   815 
       
   816     [nswindow miniaturize:nil];
       
   817     [pool release];
       
   818 }
       
   819 
       
   820 void
       
   821 Cocoa_RestoreWindow(_THIS, SDL_Window * window)
       
   822 {
       
   823     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
       
   824     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
       
   825 
       
   826     if ([nswindow isMiniaturized]) {
       
   827         [nswindow deminiaturize:nil];
       
   828     } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
   786         [nswindow zoom:nil];
   829         [nswindow zoom:nil];
   787 
   830     }
   788         if (window == _this->current_glwin) {
   831     [pool release];
   789             [((NSOpenGLContext *) _this->current_glctx) update];
       
   790         }
       
   791     }
       
   792 }
       
   793 
       
   794 void
       
   795 Cocoa_MinimizeWindow(_THIS, SDL_Window * window)
       
   796 {
       
   797     @autoreleasepool {
       
   798         NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
       
   799         [nswindow miniaturize:nil];
       
   800     }
       
   801 }
       
   802 
       
   803 void
       
   804 Cocoa_RestoreWindow(_THIS, SDL_Window * window)
       
   805 {
       
   806     @autoreleasepool {
       
   807         NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
       
   808 
       
   809         if ([nswindow isMiniaturized]) {
       
   810             [nswindow deminiaturize:nil];
       
   811         } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
       
   812             [nswindow zoom:nil];
       
   813         }
       
   814     }
       
   815 }
   832 }
   816 
   833 
   817 static NSWindow *
   834 static NSWindow *
   818 Cocoa_RebuildWindow(SDL_WindowData * data, NSWindow * nswindow, unsigned style)
   835 Cocoa_RebuildWindow(SDL_WindowData * data, NSWindow * nswindow, unsigned style)
   819 {
   836 {
   835 void
   852 void
   836 Cocoa_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
   853 Cocoa_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
   837 {
   854 {
   838     /* this message arrived in 10.6. You're out of luck on older OSes. */
   855     /* this message arrived in 10.6. You're out of luck on older OSes. */
   839 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
   856 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
   840     @autoreleasepool {
   857     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   841         NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   858     NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
   842         if ([nswindow respondsToSelector:@selector(setStyleMask:)]) {
   859     if ([nswindow respondsToSelector:@selector(setStyleMask:)]) {
   843             [nswindow setStyleMask:GetWindowStyle(window)];
   860         [nswindow setStyleMask:GetWindowStyle(window)];
   844             if (bordered) {
   861         if (bordered) {
   845                 Cocoa_SetWindowTitle(_this, window);  // this got blanked out.
   862             Cocoa_SetWindowTitle(_this, window);  // this got blanked out.
   846             }
   863         }
   847         }
   864     }
   848     }
   865     [pool release];
   849 #endif
   866 #endif
   850 }
   867 }
   851 
   868 
   852 void
   869 void
   853 Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
   870 Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
   854 {
   871 {
   855     @autoreleasepool {
   872     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   856         SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   873     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   857         NSWindow *nswindow = data->nswindow;
   874     NSWindow *nswindow = data->nswindow;
   858         NSRect rect;
   875     NSRect rect;
   859 
   876 
   860         /* The view responder chain gets messed with during setStyleMask */
   877     /* The view responder chain gets messed with during setStyleMask */
   861         if ([[nswindow contentView] nextResponder] == data->listener) {
   878     if ([[nswindow contentView] nextResponder] == data->listener) {
   862             [[nswindow contentView] setNextResponder:nil];
   879         [[nswindow contentView] setNextResponder:nil];
   863         }
   880     }
   864 
   881 
   865         if (fullscreen) {
   882     if (fullscreen) {
   866             SDL_Rect bounds;
   883         SDL_Rect bounds;
   867 
   884 
   868             Cocoa_GetDisplayBounds(_this, display, &bounds);
   885         Cocoa_GetDisplayBounds(_this, display, &bounds);
   869             rect.origin.x = bounds.x;
   886         rect.origin.x = bounds.x;
   870             rect.origin.y = bounds.y;
   887         rect.origin.y = bounds.y;
   871             rect.size.width = bounds.w;
   888         rect.size.width = bounds.w;
   872             rect.size.height = bounds.h;
   889         rect.size.height = bounds.h;
   873             ConvertNSRect(&rect);
   890         ConvertNSRect(&rect);
   874 
   891 
   875             /* Hack to fix origin on Mac OS X 10.4 */
   892         /* Hack to fix origin on Mac OS X 10.4 */
   876             NSRect screenRect = [[nswindow screen] frame];
   893         NSRect screenRect = [[nswindow screen] frame];
   877             if (screenRect.size.height >= 1.0f) {
   894         if (screenRect.size.height >= 1.0f) {
   878                 rect.origin.y += (screenRect.size.height - rect.size.height);
   895             rect.origin.y += (screenRect.size.height - rect.size.height);
   879             }
   896         }
   880 
   897 
   881             if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
   898         if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
   882                 [nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
   899             [nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
   883             } else {
       
   884                 nswindow = Cocoa_RebuildWindow(data, nswindow, NSBorderlessWindowMask);
       
   885             }
       
   886         } else {
   900         } else {
   887             rect.origin.x = window->windowed.x;
   901             nswindow = Cocoa_RebuildWindow(data, nswindow, NSBorderlessWindowMask);
   888             rect.origin.y = window->windowed.y;
   902         }
   889             rect.size.width = window->windowed.w;
   903     } else {
   890             rect.size.height = window->windowed.h;
   904         rect.origin.x = window->windowed.x;
   891             ConvertNSRect(&rect);
   905         rect.origin.y = window->windowed.y;
   892 
   906         rect.size.width = window->windowed.w;
   893             if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
   907         rect.size.height = window->windowed.h;
   894                 [nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)GetWindowStyle(window)];
   908         ConvertNSRect(&rect);
   895             } else {
   909 
   896                 nswindow = Cocoa_RebuildWindow(data, nswindow, GetWindowStyle(window));
   910         if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
   897             }
   911             [nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)GetWindowStyle(window)];
   898         }
       
   899 
       
   900         /* The view responder chain gets messed with during setStyleMask */
       
   901         if ([[nswindow contentView] nextResponder] != data->listener) {
       
   902             [[nswindow contentView] setNextResponder:data->listener];
       
   903         }
       
   904 
       
   905         s_moveHack = 0;
       
   906         [nswindow setFrameOrigin:rect.origin];
       
   907         [nswindow setContentSize:rect.size];
       
   908         s_moveHack = SDL_GetTicks();
       
   909 
       
   910         /* When the window style changes the title is cleared */
       
   911         if (!fullscreen) {
       
   912             Cocoa_SetWindowTitle(_this, window);
       
   913         }
       
   914 
       
   915     #ifdef FULLSCREEN_TOGGLEABLE
       
   916         if (SDL_ShouldAllowTopmost() && fullscreen) {
       
   917             /* OpenGL is rendering to the window, so make it visible! */
       
   918             [nswindow setLevel:CGShieldingWindowLevel()];
       
   919         } else {
   912         } else {
   920             [nswindow setLevel:kCGNormalWindowLevel];
   913             nswindow = Cocoa_RebuildWindow(data, nswindow, GetWindowStyle(window));
   921         }
   914         }
   922     #endif
   915     }
   923         [nswindow makeKeyAndOrderFront:nil];
   916 
   924 
   917     /* The view responder chain gets messed with during setStyleMask */
   925         if (window == _this->current_glwin) {
   918     if ([[nswindow contentView] nextResponder] != data->listener) {
   926             [((NSOpenGLContext *) _this->current_glctx) update];
   919         [[nswindow contentView] setNextResponder:data->listener];
   927         }
   920     }
   928     }
   921 
       
   922     s_moveHack = 0;
       
   923     [nswindow setFrameOrigin:rect.origin];
       
   924     [nswindow setContentSize:rect.size];
       
   925     s_moveHack = SDL_GetTicks();
       
   926 
       
   927     /* When the window style changes the title is cleared */
       
   928     if (!fullscreen) {
       
   929         Cocoa_SetWindowTitle(_this, window);
       
   930     }
       
   931 
       
   932 #ifdef FULLSCREEN_TOGGLEABLE
       
   933     if (SDL_ShouldAllowTopmost() && fullscreen) {
       
   934         /* OpenGL is rendering to the window, so make it visible! */
       
   935         [nswindow setLevel:CGShieldingWindowLevel()];
       
   936     } else {
       
   937         [nswindow setLevel:kCGNormalWindowLevel];
       
   938     }
       
   939 #endif
       
   940     [nswindow makeKeyAndOrderFront:nil];
       
   941 
       
   942     if (window == _this->current_glwin) {
       
   943         [((NSOpenGLContext *) _this->current_glctx) update];
       
   944     }
       
   945 
       
   946     [pool release];
   929 }
   947 }
   930 
   948 
   931 int
   949 int
   932 Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
   950 Cocoa_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
   933 {
   951 {
  1007 }
  1025 }
  1008 
  1026 
  1009 void
  1027 void
  1010 Cocoa_DestroyWindow(_THIS, SDL_Window * window)
  1028 Cocoa_DestroyWindow(_THIS, SDL_Window * window)
  1011 {
  1029 {
  1012     @autoreleasepool {
  1030     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  1013         SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  1031     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  1014 
  1032 
  1015         if (data) {
  1033     if (data) {
  1016             [data->listener close];
  1034         [data->listener close];
  1017             [data->listener release];
  1035         [data->listener release];
  1018             if (data->created) {
  1036         if (data->created) {
  1019                 [data->nswindow close];
  1037             [data->nswindow close];
  1020             }
  1038         }
  1021             SDL_free(data);
  1039         SDL_free(data);
  1022         }
  1040     }
  1023     }
  1041     [pool release];
  1024 }
  1042 }
  1025 
  1043 
  1026 SDL_bool
  1044 SDL_bool
  1027 Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
  1045 Cocoa_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
  1028 {
  1046 {