Added support for placing windows on different displays
authorSam Lantinga <slouken@libsdl.org>
Tue, 01 Dec 2009 11:50:00 +0000
changeset 3506e829b6098435
parent 3505 a1bf34bc2a58
child 3507 3712547eac4f
Added support for placing windows on different displays
src/video/cocoa/SDL_cocoawindow.h
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/src/video/cocoa/SDL_cocoawindow.h	Tue Dec 01 10:41:58 2009 +0000
     1.2 +++ b/src/video/cocoa/SDL_cocoawindow.h	Tue Dec 01 11:50:00 2009 +0000
     1.3 @@ -72,6 +72,7 @@
     1.4      SDL_WindowID windowID;
     1.5      NSWindow *window;
     1.6      SDL_bool created;
     1.7 +    CGDirectDisplayID display;
     1.8      Cocoa_WindowListener *listener;
     1.9      struct SDL_VideoData *videodata;
    1.10  };
     2.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Tue Dec 01 10:41:58 2009 +0000
     2.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Tue Dec 01 11:50:00 2009 +0000
     2.3 @@ -31,7 +31,6 @@
     2.4  
     2.5  static __inline__ void ConvertNSRect(NSRect *r)
     2.6  {
     2.7 -    /* FIXME: Cache the display used for this window */
     2.8      r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
     2.9  }
    2.10  
    2.11 @@ -99,10 +98,11 @@
    2.12  - (void)windowDidMove:(NSNotification *)aNotification
    2.13  {
    2.14      int x, y;
    2.15 +    NSRect disp = CGDisplayBounds(_data->display);
    2.16      NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
    2.17      ConvertNSRect(&rect);
    2.18 -    x = (int)rect.origin.x;
    2.19 -    y = (int)rect.origin.y;
    2.20 +    x = (int)rect.origin.x - disp.origin.x;
    2.21 +    y = (int)rect.origin.y - disp.origin.y;
    2.22      SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_MOVED, x, y);
    2.23  }
    2.24  
    2.25 @@ -309,6 +309,7 @@
    2.26  {
    2.27      NSAutoreleasePool *pool;
    2.28      SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
    2.29 +    SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
    2.30      SDL_WindowData *data;
    2.31  
    2.32      /* Allocate the window data */
    2.33 @@ -320,6 +321,7 @@
    2.34      data->windowID = window->id;
    2.35      data->window = nswindow;
    2.36      data->created = created;
    2.37 +    data->display = displaydata->display;
    2.38      data->videodata = videodata;
    2.39  
    2.40      pool = [[NSAutoreleasePool alloc] init];
    2.41 @@ -330,10 +332,11 @@
    2.42  
    2.43      /* Fill in the SDL window with the window data */
    2.44      {
    2.45 +        NSRect disp = CGDisplayBounds(data->display);
    2.46          NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
    2.47          ConvertNSRect(&rect);
    2.48 -        window->x = (int)rect.origin.x;
    2.49 -        window->y = (int)rect.origin.y;
    2.50 +        window->x = (int)rect.origin.x - disp.origin.x;
    2.51 +        window->y = (int)rect.origin.y - disp.origin.y;
    2.52          window->w = (int)rect.size.width;
    2.53          window->h = (int)rect.size.height;
    2.54      }
    2.55 @@ -385,30 +388,26 @@
    2.56  int
    2.57  Cocoa_CreateWindow(_THIS, SDL_Window * window)
    2.58  {
    2.59 -    NSAutoreleasePool *pool;
    2.60 +    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    2.61      NSWindow *nswindow;
    2.62 +    SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
    2.63      NSRect rect;
    2.64      unsigned int style;
    2.65      NSString *title;
    2.66      int status;
    2.67  
    2.68 -    pool = [[NSAutoreleasePool alloc] init];
    2.69 -
    2.70 +    rect = CGDisplayBounds(displaydata->display);
    2.71      if ((window->flags & SDL_WINDOW_FULLSCREEN)
    2.72          || window->x == SDL_WINDOWPOS_CENTERED) {
    2.73 -        rect.origin.x = (CGDisplayPixelsWide(kCGDirectMainDisplay) - window->w) / 2;
    2.74 -    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
    2.75 -        rect.origin.x = 0;
    2.76 -    } else {
    2.77 -        rect.origin.x = window->x;
    2.78 +        rect.origin.x += (rect.size.width - window->w) / 2;
    2.79 +    } else if (window->x != SDL_WINDOWPOS_UNDEFINED) {
    2.80 +        rect.origin.x += window->x;
    2.81      }
    2.82      if ((window->flags & SDL_WINDOW_FULLSCREEN)
    2.83          || window->y == SDL_WINDOWPOS_CENTERED) {
    2.84 -        rect.origin.y = (CGDisplayPixelsHigh(kCGDirectMainDisplay) - window->h) / 2;
    2.85 -    } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
    2.86 -        rect.origin.y = 0;
    2.87 -    } else {
    2.88 -        rect.origin.y = window->y;
    2.89 +        rect.origin.y += (rect.size.height - window->h) / 2;
    2.90 +    } else if (window->x != SDL_WINDOWPOS_UNDEFINED) {
    2.91 +        rect.origin.y += window->y;
    2.92      }
    2.93      rect.size.width = window->w;
    2.94      rect.size.height = window->h;
    2.95 @@ -423,7 +422,22 @@
    2.96          style |= NSResizableWindowMask;
    2.97      }
    2.98  
    2.99 -    nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE];
   2.100 +    /* Figure out which screen to place this window */
   2.101 +    NSArray *screens = [NSScreen screens];
   2.102 +    NSScreen *screen = nil;
   2.103 +    NSScreen *candidate;
   2.104 +    for (candidate in screens) {
   2.105 +        NSRect screenRect = [candidate frame];
   2.106 +        if (rect.origin.x >= screenRect.origin.x &&
   2.107 +            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
   2.108 +            rect.origin.y >= screenRect.origin.y &&
   2.109 +            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
   2.110 +            screen = candidate;
   2.111 +            rect.origin.x -= screenRect.origin.x;
   2.112 +            rect.origin.y -= screenRect.origin.y;
   2.113 +        }
   2.114 +    }
   2.115 +    nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE screen:screen];
   2.116  
   2.117      [pool release];
   2.118  
   2.119 @@ -478,19 +492,21 @@
   2.120  {
   2.121      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   2.122      NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   2.123 +    SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
   2.124      NSRect rect;
   2.125  
   2.126 +    rect = CGDisplayBounds(displaydata->display);
   2.127      if ((window->flags & SDL_WINDOW_FULLSCREEN)
   2.128          || window->x == SDL_WINDOWPOS_CENTERED) {
   2.129 -        rect.origin.x = (CGDisplayPixelsWide(kCGDirectMainDisplay) - window->w) / 2;
   2.130 +        rect.origin.x += (rect.size.width - window->w) / 2;
   2.131      } else {
   2.132 -        rect.origin.x = window->x;
   2.133 +        rect.origin.x += window->x;
   2.134      }
   2.135      if ((window->flags & SDL_WINDOW_FULLSCREEN)
   2.136          || window->y == SDL_WINDOWPOS_CENTERED) {
   2.137 -        rect.origin.y = (CGDisplayPixelsHigh(kCGDirectMainDisplay) - window->h) / 2;
   2.138 +        rect.origin.y += (rect.size.height - window->h) / 2;
   2.139      } else {
   2.140 -        rect.origin.y = window->y;
   2.141 +        rect.origin.y += window->y;
   2.142      }
   2.143      rect.size.width = window->w;
   2.144      rect.size.height = window->h;