src/video/cocoa/SDL_cocoawindow.m
changeset 5249 7a963be087ef
parent 5246 58265e606e4e
child 5253 9e70b360f423
     1.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Thu Feb 10 22:49:14 2011 -0800
     1.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Fri Feb 11 00:25:44 2011 -0800
     1.3 @@ -398,6 +398,22 @@
     1.4  
     1.5  @end
     1.6  
     1.7 +static unsigned int
     1.8 +GetStyleMask(SDL_Window * window)
     1.9 +{
    1.10 +    unsigned int style;
    1.11 +
    1.12 +    if (window->flags & SDL_WINDOW_BORDERLESS) {
    1.13 +        style = NSBorderlessWindowMask;
    1.14 +    } else {
    1.15 +        style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
    1.16 +    }
    1.17 +    if (window->flags & SDL_WINDOW_RESIZABLE) {
    1.18 +        style |= NSResizableWindowMask;
    1.19 +    }
    1.20 +    return style;
    1.21 +}
    1.22 +
    1.23  static int
    1.24  SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, SDL_bool created)
    1.25  {
    1.26 @@ -406,7 +422,7 @@
    1.27      SDL_WindowData *data;
    1.28  
    1.29      /* Allocate the window data */
    1.30 -    data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
    1.31 +    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
    1.32      if (!data) {
    1.33          SDL_OutOfMemory();
    1.34          return -1;
    1.35 @@ -424,7 +440,6 @@
    1.36  
    1.37      /* Fill in the SDL window with the window data */
    1.38      {
    1.39 -        SDL_Rect bounds;
    1.40          NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
    1.41          NSView *contentView = [[SDLView alloc] initWithFrame: rect
    1.42                                                      listener: data->listener];
    1.43 @@ -495,16 +510,14 @@
    1.44      unsigned int style;
    1.45  
    1.46      Cocoa_GetDisplayBounds(_this, display, &bounds);
    1.47 -    if ((window->flags & SDL_WINDOW_FULLSCREEN)
    1.48 -        || SDL_WINDOWPOS_ISCENTERED(window->x)) {
    1.49 +    if (SDL_WINDOWPOS_ISCENTERED(window->x)) {
    1.50          rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
    1.51      } else if (SDL_WINDOWPOS_ISUNDEFINED(window->x)) {
    1.52          rect.origin.x = bounds.x;
    1.53      } else {
    1.54          rect.origin.x = window->x;
    1.55      }
    1.56 -    if ((window->flags & SDL_WINDOW_FULLSCREEN)
    1.57 -        || SDL_WINDOWPOS_ISCENTERED(window->y)) {
    1.58 +    if (SDL_WINDOWPOS_ISCENTERED(window->y)) {
    1.59          rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
    1.60      } else if (SDL_WINDOWPOS_ISUNDEFINED(window->y)) {
    1.61          rect.origin.y = bounds.y;
    1.62 @@ -515,14 +528,7 @@
    1.63      rect.size.height = window->h;
    1.64      ConvertNSRect(&rect);
    1.65  
    1.66 -    if (window->flags & SDL_WINDOW_BORDERLESS) {
    1.67 -        style = NSBorderlessWindowMask;
    1.68 -    } else {
    1.69 -        style = (NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask);
    1.70 -    }
    1.71 -    if (window->flags & SDL_WINDOW_RESIZABLE) {
    1.72 -        style |= NSResizableWindowMask;
    1.73 -    }
    1.74 +    style = GetStyleMask(window);
    1.75  
    1.76      /* Figure out which screen to place this window */
    1.77      NSArray *screens = [NSScreen screens];
    1.78 @@ -600,14 +606,12 @@
    1.79      SDL_Rect bounds;
    1.80  
    1.81      Cocoa_GetDisplayBounds(_this, display, &bounds);
    1.82 -    if ((window->flags & SDL_WINDOW_FULLSCREEN)
    1.83 -        || SDL_WINDOWPOS_ISCENTERED(window->x)) {
    1.84 +    if (SDL_WINDOWPOS_ISCENTERED(window->x)) {
    1.85          rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
    1.86      } else {
    1.87          rect.origin.x = window->x;
    1.88      }
    1.89 -    if ((window->flags & SDL_WINDOW_FULLSCREEN)
    1.90 -        || SDL_WINDOWPOS_ISCENTERED(window->y)) {
    1.91 +    if (SDL_WINDOWPOS_ISCENTERED(window->y)) {
    1.92          rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
    1.93      } else {
    1.94          rect.origin.y = window->y;
    1.95 @@ -700,6 +704,56 @@
    1.96  }
    1.97  
    1.98  void
    1.99 +Cocoa_SetWindowFullscreen(_THIS, SDL_Window * window)
   1.100 +{
   1.101 +    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   1.102 +    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   1.103 +    NSWindow *nswindow = data->nswindow;
   1.104 +    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
   1.105 +    NSRect rect;
   1.106 +    unsigned int style;
   1.107 +
   1.108 +    if (FULLSCREEN_VISIBLE(window)) {
   1.109 +        SDL_Rect bounds;
   1.110 +
   1.111 +        Cocoa_GetDisplayBounds(_this, display, &bounds);
   1.112 +        rect.origin.x = bounds.x;
   1.113 +        rect.origin.y = bounds.y;
   1.114 +        rect.size.width = bounds.w;
   1.115 +        rect.size.height = bounds.h;
   1.116 +        ConvertNSRect(&rect);
   1.117 +
   1.118 +        style = NSBorderlessWindowMask;
   1.119 +    } else {
   1.120 +        rect.origin.x = window->windowed.x;
   1.121 +        rect.origin.y = window->windowed.y;
   1.122 +        rect.size.width = window->windowed.w;
   1.123 +        rect.size.height = window->windowed.h;
   1.124 +        /* FIXME: This calculation is wrong, we're changing the origin */
   1.125 +        ConvertNSRect(&rect);
   1.126 +
   1.127 +        style = GetStyleMask(window);
   1.128 +    }
   1.129 +
   1.130 +    [nswindow setStyleMask:style];
   1.131 +    [nswindow setContentSize:rect.size];
   1.132 +    rect = [nswindow frameRectForContentRect:rect];
   1.133 +    [nswindow setFrameOrigin:rect.origin];
   1.134 +
   1.135 +#ifdef FULLSCREEN_TOGGLEABLE
   1.136 +    if (FULLSCREEN_VISIBLE(window)) {
   1.137 +        /* OpenGL is rendering to the window, so make it visible! */
   1.138 +        [nswindow setLevel:CGShieldingWindowLevel()];
   1.139 +    } else {
   1.140 +        [nswindow setLevel:kCGNormalWindowLevel];
   1.141 +    }
   1.142 +#endif
   1.143 +    [nswindow makeKeyAndOrderFront:nil];
   1.144 +
   1.145 +    [pool release];
   1.146 +}
   1.147 +
   1.148 +void
   1.149  Cocoa_SetWindowGrab(_THIS, SDL_Window * window)
   1.150  {
   1.151      if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&