src/video/cocoa/SDL_cocoawindow.m
changeset 3506 e829b6098435
parent 3414 1e45c3012a4f
child 3507 3712547eac4f
     1.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Tue Dec 01 10:41:58 2009 +0000
     1.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Tue Dec 01 11:50:00 2009 +0000
     1.3 @@ -31,7 +31,6 @@
     1.4  
     1.5  static __inline__ void ConvertNSRect(NSRect *r)
     1.6  {
     1.7 -    /* FIXME: Cache the display used for this window */
     1.8      r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
     1.9  }
    1.10  
    1.11 @@ -99,10 +98,11 @@
    1.12  - (void)windowDidMove:(NSNotification *)aNotification
    1.13  {
    1.14      int x, y;
    1.15 +    NSRect disp = CGDisplayBounds(_data->display);
    1.16      NSRect rect = [_data->window contentRectForFrameRect:[_data->window frame]];
    1.17      ConvertNSRect(&rect);
    1.18 -    x = (int)rect.origin.x;
    1.19 -    y = (int)rect.origin.y;
    1.20 +    x = (int)rect.origin.x - disp.origin.x;
    1.21 +    y = (int)rect.origin.y - disp.origin.y;
    1.22      SDL_SendWindowEvent(_data->windowID, SDL_WINDOWEVENT_MOVED, x, y);
    1.23  }
    1.24  
    1.25 @@ -309,6 +309,7 @@
    1.26  {
    1.27      NSAutoreleasePool *pool;
    1.28      SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
    1.29 +    SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
    1.30      SDL_WindowData *data;
    1.31  
    1.32      /* Allocate the window data */
    1.33 @@ -320,6 +321,7 @@
    1.34      data->windowID = window->id;
    1.35      data->window = nswindow;
    1.36      data->created = created;
    1.37 +    data->display = displaydata->display;
    1.38      data->videodata = videodata;
    1.39  
    1.40      pool = [[NSAutoreleasePool alloc] init];
    1.41 @@ -330,10 +332,11 @@
    1.42  
    1.43      /* Fill in the SDL window with the window data */
    1.44      {
    1.45 +        NSRect disp = CGDisplayBounds(data->display);
    1.46          NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
    1.47          ConvertNSRect(&rect);
    1.48 -        window->x = (int)rect.origin.x;
    1.49 -        window->y = (int)rect.origin.y;
    1.50 +        window->x = (int)rect.origin.x - disp.origin.x;
    1.51 +        window->y = (int)rect.origin.y - disp.origin.y;
    1.52          window->w = (int)rect.size.width;
    1.53          window->h = (int)rect.size.height;
    1.54      }
    1.55 @@ -385,30 +388,26 @@
    1.56  int
    1.57  Cocoa_CreateWindow(_THIS, SDL_Window * window)
    1.58  {
    1.59 -    NSAutoreleasePool *pool;
    1.60 +    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    1.61      NSWindow *nswindow;
    1.62 +    SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
    1.63      NSRect rect;
    1.64      unsigned int style;
    1.65      NSString *title;
    1.66      int status;
    1.67  
    1.68 -    pool = [[NSAutoreleasePool alloc] init];
    1.69 -
    1.70 +    rect = CGDisplayBounds(displaydata->display);
    1.71      if ((window->flags & SDL_WINDOW_FULLSCREEN)
    1.72          || window->x == SDL_WINDOWPOS_CENTERED) {
    1.73 -        rect.origin.x = (CGDisplayPixelsWide(kCGDirectMainDisplay) - window->w) / 2;
    1.74 -    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
    1.75 -        rect.origin.x = 0;
    1.76 -    } else {
    1.77 -        rect.origin.x = window->x;
    1.78 +        rect.origin.x += (rect.size.width - window->w) / 2;
    1.79 +    } else if (window->x != SDL_WINDOWPOS_UNDEFINED) {
    1.80 +        rect.origin.x += window->x;
    1.81      }
    1.82      if ((window->flags & SDL_WINDOW_FULLSCREEN)
    1.83          || window->y == SDL_WINDOWPOS_CENTERED) {
    1.84 -        rect.origin.y = (CGDisplayPixelsHigh(kCGDirectMainDisplay) - window->h) / 2;
    1.85 -    } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
    1.86 -        rect.origin.y = 0;
    1.87 -    } else {
    1.88 -        rect.origin.y = window->y;
    1.89 +        rect.origin.y += (rect.size.height - window->h) / 2;
    1.90 +    } else if (window->x != SDL_WINDOWPOS_UNDEFINED) {
    1.91 +        rect.origin.y += window->y;
    1.92      }
    1.93      rect.size.width = window->w;
    1.94      rect.size.height = window->h;
    1.95 @@ -423,7 +422,22 @@
    1.96          style |= NSResizableWindowMask;
    1.97      }
    1.98  
    1.99 -    nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE];
   1.100 +    /* Figure out which screen to place this window */
   1.101 +    NSArray *screens = [NSScreen screens];
   1.102 +    NSScreen *screen = nil;
   1.103 +    NSScreen *candidate;
   1.104 +    for (candidate in screens) {
   1.105 +        NSRect screenRect = [candidate frame];
   1.106 +        if (rect.origin.x >= screenRect.origin.x &&
   1.107 +            rect.origin.x < screenRect.origin.x + screenRect.size.width &&
   1.108 +            rect.origin.y >= screenRect.origin.y &&
   1.109 +            rect.origin.y < screenRect.origin.y + screenRect.size.height) {
   1.110 +            screen = candidate;
   1.111 +            rect.origin.x -= screenRect.origin.x;
   1.112 +            rect.origin.y -= screenRect.origin.y;
   1.113 +        }
   1.114 +    }
   1.115 +    nswindow = [[SDLWindow alloc] initWithContentRect:rect styleMask:style backing:NSBackingStoreBuffered defer:FALSE screen:screen];
   1.116  
   1.117      [pool release];
   1.118  
   1.119 @@ -478,19 +492,21 @@
   1.120  {
   1.121      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   1.122      NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->window;
   1.123 +    SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayFromWindow(window)->driverdata;
   1.124      NSRect rect;
   1.125  
   1.126 +    rect = CGDisplayBounds(displaydata->display);
   1.127      if ((window->flags & SDL_WINDOW_FULLSCREEN)
   1.128          || window->x == SDL_WINDOWPOS_CENTERED) {
   1.129 -        rect.origin.x = (CGDisplayPixelsWide(kCGDirectMainDisplay) - window->w) / 2;
   1.130 +        rect.origin.x += (rect.size.width - window->w) / 2;
   1.131      } else {
   1.132 -        rect.origin.x = window->x;
   1.133 +        rect.origin.x += window->x;
   1.134      }
   1.135      if ((window->flags & SDL_WINDOW_FULLSCREEN)
   1.136          || window->y == SDL_WINDOWPOS_CENTERED) {
   1.137 -        rect.origin.y = (CGDisplayPixelsHigh(kCGDirectMainDisplay) - window->h) / 2;
   1.138 +        rect.origin.y += (rect.size.height - window->h) / 2;
   1.139      } else {
   1.140 -        rect.origin.y = window->y;
   1.141 +        rect.origin.y += window->y;
   1.142      }
   1.143      rect.size.width = window->w;
   1.144      rect.size.height = window->h;