Restore the windowed position and size when coming back from fullscreen.
authorSam Lantinga <slouken@libsdl.org>
Sat, 26 Feb 2011 10:11:09 -0800
changeset 539883816c8a2b58
parent 5397 88ff1f463134
child 5399 1e2a82a66df8
Restore the windowed position and size when coming back from fullscreen.
Also fixed problem where Cocoa would move the windows in response to the fullscreen mode change.
src/events/SDL_windowevents.c
src/video/SDL_sysvideo.h
src/video/SDL_video.c
src/video/cocoa/SDL_cocoawindow.m
src/video/windows/SDL_windowswindow.c
src/video/windows/SDL_windowswindow.h
     1.1 --- a/src/events/SDL_windowevents.c	Thu Feb 24 23:42:58 2011 -0800
     1.2 +++ b/src/events/SDL_windowevents.c	Sat Feb 26 10:11:09 2011 -0800
     1.3 @@ -89,6 +89,10 @@
     1.4              SDL_WINDOWPOS_ISUNDEFINED(data2)) {
     1.5              return 0;
     1.6          }
     1.7 +        if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
     1.8 +            window->windowed.x = data1;
     1.9 +            window->windowed.y = data2;
    1.10 +        }
    1.11          if (data1 == window->x && data2 == window->y) {
    1.12              return 0;
    1.13          }
    1.14 @@ -96,6 +100,10 @@
    1.15          window->y = data2;
    1.16          break;
    1.17      case SDL_WINDOWEVENT_RESIZED:
    1.18 +        if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
    1.19 +            window->windowed.w = data1;
    1.20 +            window->windowed.h = data2;
    1.21 +        }
    1.22          if (data1 == window->w && data2 == window->h) {
    1.23              return 0;
    1.24          }
     2.1 --- a/src/video/SDL_sysvideo.h	Thu Feb 24 23:42:58 2011 -0800
     2.2 +++ b/src/video/SDL_sysvideo.h	Sat Feb 26 10:11:09 2011 -0800
     2.3 @@ -76,6 +76,9 @@
     2.4      int w, h;
     2.5      Uint32 flags;
     2.6  
     2.7 +    /* Stored position and size for windowed mode */
     2.8 +    SDL_Rect windowed;
     2.9 +
    2.10      SDL_DisplayMode fullscreen_mode;
    2.11      
    2.12      SDL_Surface *surface;
     3.1 --- a/src/video/SDL_video.c	Thu Feb 24 23:42:58 2011 -0800
     3.2 +++ b/src/video/SDL_video.c	Sat Feb 26 10:11:09 2011 -0800
     3.3 @@ -1087,6 +1087,11 @@
     3.4  static void
     3.5  SDL_FinishWindowCreation(SDL_Window *window, Uint32 flags)
     3.6  {
     3.7 +    window->windowed.x = window->x;
     3.8 +    window->windowed.y = window->y;
     3.9 +    window->windowed.w = window->w;
    3.10 +    window->windowed.h = window->h;
    3.11 +
    3.12      if (flags & SDL_WINDOW_MAXIMIZED) {
    3.13          SDL_MaximizeWindow(window);
    3.14      }
     4.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Thu Feb 24 23:42:58 2011 -0800
     4.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Sat Feb 26 10:11:09 2011 -0800
     4.3 @@ -22,6 +22,7 @@
     4.4  #include "SDL_config.h"
     4.5  
     4.6  #include "SDL_syswm.h"
     4.7 +#include "SDL_timer.h"  /* For SDL_GetTicks() */
     4.8  #include "../SDL_sysvideo.h"
     4.9  #include "../../events/SDL_keyboard_c.h"
    4.10  #include "../../events/SDL_mouse_c.h"
    4.11 @@ -31,6 +32,9 @@
    4.12  #include "SDL_cocoashape.h"
    4.13  #include "SDL_cocoamouse.h"
    4.14  
    4.15 +
    4.16 +static Uint32 s_moveHack;
    4.17 +
    4.18  static __inline__ void ConvertNSRect(NSRect *r)
    4.19  {
    4.20      r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
    4.21 @@ -115,11 +119,29 @@
    4.22  - (void)windowDidMove:(NSNotification *)aNotification
    4.23  {
    4.24      int x, y;
    4.25 -    NSRect rect = [_data->nswindow contentRectForFrameRect:[_data->nswindow frame]];
    4.26 +    SDL_Window *window = _data->window;
    4.27 +    NSWindow *nswindow = _data->nswindow;
    4.28 +    NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
    4.29      ConvertNSRect(&rect);
    4.30 +
    4.31 +    if (s_moveHack) {
    4.32 +        SDL_bool blockMove = ((SDL_GetTicks() - s_moveHack) < 500);
    4.33 +
    4.34 +        s_moveHack = 0;
    4.35 +
    4.36 +        if (blockMove) {
    4.37 +            /* Cocoa is adjusting the window in response to a mode change */
    4.38 +            rect.origin.x = window->x;
    4.39 +            rect.origin.y = window->y;
    4.40 +            ConvertNSRect(&rect);
    4.41 +            [nswindow setFrameOrigin:rect.origin];
    4.42 +            return;
    4.43 +        }
    4.44 +    }
    4.45 +
    4.46      x = (int)rect.origin.x;
    4.47      y = (int)rect.origin.y;
    4.48 -    SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MOVED, x, y);
    4.49 +    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
    4.50  }
    4.51  
    4.52  - (void)windowDidResize:(NSNotification *)aNotification
    4.53 @@ -786,16 +808,22 @@
    4.54          if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
    4.55              [nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
    4.56          }
    4.57 -        [nswindow setFrameOrigin:rect.origin];
    4.58 -        [nswindow setContentSize:rect.size];
    4.59      } else {
    4.60 +        rect.origin.x = window->windowed.x;
    4.61 +        rect.origin.y = window->windowed.y;
    4.62 +        rect.size.width = window->windowed.w;
    4.63 +        rect.size.height = window->windowed.h;
    4.64 +        ConvertNSRect(&rect);
    4.65 +
    4.66          if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
    4.67              [nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)GetWindowStyle(window)];
    4.68          }
    4.69 +    }
    4.70  
    4.71 -        // This doesn't seem to do anything...
    4.72 -        //[nswindow setFrameOrigin:origin];
    4.73 -    }
    4.74 +    s_moveHack = 0;
    4.75 +    [nswindow setFrameOrigin:rect.origin];
    4.76 +    [nswindow setContentSize:rect.size];
    4.77 +    s_moveHack = SDL_GetTicks();
    4.78  
    4.79  #ifdef FULLSCREEN_TOGGLEABLE
    4.80      if (fullscreen) {
     5.1 --- a/src/video/windows/SDL_windowswindow.c	Thu Feb 24 23:42:58 2011 -0800
     5.2 +++ b/src/video/windows/SDL_windowswindow.c	Sat Feb 26 10:11:09 2011 -0800
     5.3 @@ -553,10 +553,6 @@
     5.4      WIN_GetDisplayBounds(_this, display, &bounds);
     5.5  
     5.6      if (fullscreen) {
     5.7 -        /* Save the windowed position */
     5.8 -        data->windowed_x = window->x;
     5.9 -        data->windowed_y = window->y;
    5.10 -
    5.11          x = bounds.x;
    5.12          y = bounds.y;
    5.13          w = bounds.w;
    5.14 @@ -564,8 +560,8 @@
    5.15      } else {
    5.16          rect.left = 0;
    5.17          rect.top = 0;
    5.18 -        rect.right = window->w;
    5.19 -        rect.bottom = window->h;
    5.20 +        rect.right = window->windowed.w;
    5.21 +        rect.bottom = window->windowed.h;
    5.22  #ifdef _WIN32_WCE
    5.23          menu = FALSE;
    5.24  #else
    5.25 @@ -574,8 +570,8 @@
    5.26          AdjustWindowRectEx(&rect, style, menu, 0);
    5.27          w = (rect.right - rect.left);
    5.28          h = (rect.bottom - rect.top);
    5.29 -        x = data->windowed_x + rect.left;
    5.30 -        y = data->windowed_y + rect.top;
    5.31 +        x = window->windowed.x + rect.left;
    5.32 +        y = window->windowed.y + rect.top;
    5.33      }
    5.34      SetWindowLong(hwnd, GWL_STYLE, style);
    5.35      SetWindowPos(hwnd, top, x, y, w, h, SWP_NOCOPYBITS);
     6.1 --- a/src/video/windows/SDL_windowswindow.h	Thu Feb 24 23:42:58 2011 -0800
     6.2 +++ b/src/video/windows/SDL_windowswindow.h	Sat Feb 26 10:11:09 2011 -0800
     6.3 @@ -43,8 +43,6 @@
     6.4      WNDPROC wndproc;
     6.5      SDL_bool created;
     6.6      int mouse_pressed;
     6.7 -    int windowed_x;
     6.8 -    int windowed_y;
     6.9      struct SDL_VideoData *videodata;
    6.10  } SDL_WindowData;
    6.11