Unify the SetWindowPos() calls so that they all set the window state based on SDL state.
authorSam Lantinga <slouken@libsdl.org>
Wed, 07 Aug 2013 00:46:42 -0700
changeset 75857fd59fc01ee7
parent 7584 864eca4d27a7
child 7586 6f1539dc6b57
Unify the SetWindowPos() calls so that they all set the window state based on SDL state.
This prevents a rogue call to SetWindowPos() from changing the state unexpectedly.
Also moved the size correction code above the window position query, because the initial window size can affect the positioning.
src/video/windows/SDL_windowswindow.c
     1.1 --- a/src/video/windows/SDL_windowswindow.c	Wed Aug 07 00:10:31 2013 -0700
     1.2 +++ b/src/video/windows/SDL_windowswindow.c	Wed Aug 07 00:46:42 2013 -0700
     1.3 @@ -74,6 +74,38 @@
     1.4      return style;
     1.5  }
     1.6  
     1.7 +static void
     1.8 +WIN_SetWindowPositionInternal(_THIS, SDL_Window * window, UINT flags)
     1.9 +{
    1.10 +    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
    1.11 +    RECT rect;
    1.12 +    DWORD style;
    1.13 +    HWND top;
    1.14 +    BOOL menu;
    1.15 +    int x, y;
    1.16 +    int w, h;
    1.17 +
    1.18 +    /* Figure out what the window area will be */
    1.19 +    if (SDL_ShouldAllowTopmost() && (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) {
    1.20 +        top = HWND_TOPMOST;
    1.21 +    } else {
    1.22 +        top = HWND_NOTOPMOST;
    1.23 +    }
    1.24 +    style = GetWindowLong(hwnd, GWL_STYLE);
    1.25 +    rect.left = 0;
    1.26 +    rect.top = 0;
    1.27 +    rect.right = window->w;
    1.28 +    rect.bottom = window->h;
    1.29 +    menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
    1.30 +    AdjustWindowRectEx(&rect, style, menu, 0);
    1.31 +    w = (rect.right - rect.left);
    1.32 +    h = (rect.bottom - rect.top);
    1.33 +    x = window->x + rect.left;
    1.34 +    y = window->y + rect.top;
    1.35 +
    1.36 +    SetWindowPos(hwnd, top, x, y, w, h, flags);
    1.37 +}
    1.38 +
    1.39  static int
    1.40  SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created)
    1.41  {
    1.42 @@ -120,6 +152,20 @@
    1.43  
    1.44      /* Fill in the SDL window with the window data */
    1.45      {
    1.46 +        RECT rect;
    1.47 +        if (GetClientRect(hwnd, &rect)) {
    1.48 +            int w = rect.right;
    1.49 +            int h = rect.bottom;
    1.50 +            if ((window->w && window->w != w) || (window->h && window->h != h)) {
    1.51 +                // We tried to create a window larger than the desktop and Windows didn't allow it.  Override!
    1.52 +                WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOZORDER | SWP_NOACTIVATE);
    1.53 +            } else {
    1.54 +                window->w = w;
    1.55 +                window->h = h;
    1.56 +            }
    1.57 +        }
    1.58 +    }
    1.59 +    {
    1.60          POINT point;
    1.61          point.x = 0;
    1.62          point.y = 0;
    1.63 @@ -129,20 +175,6 @@
    1.64          }
    1.65      }
    1.66      {
    1.67 -        RECT rect;
    1.68 -        if (GetClientRect(hwnd, &rect)) {
    1.69 -            int w = rect.right;
    1.70 -            int h = rect.bottom;
    1.71 -            if ((window->w && window->w != w) || (window->h && window->h != h)) {
    1.72 -                // We tried to create a window larger than the desktop and Windows didn't allow it.  Override!
    1.73 -                SetWindowPos(hwnd, NULL, 0, 0, window->w, window->h, SWP_NOCOPYBITS | SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
    1.74 -            } else {
    1.75 -                window->w = w;
    1.76 -                window->h = h;
    1.77 -            }
    1.78 -        }
    1.79 -    }
    1.80 -    {
    1.81          DWORD style = GetWindowLong(hwnd, GWL_STYLE);
    1.82          if (style & WS_VISIBLE) {
    1.83              window->flags |= SDL_WINDOW_SHOWN;
    1.84 @@ -343,38 +375,6 @@
    1.85      SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) hicon);
    1.86  }
    1.87  
    1.88 -static void
    1.89 -WIN_SetWindowPositionInternal(_THIS, SDL_Window * window, UINT flags)
    1.90 -{
    1.91 -    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
    1.92 -    RECT rect;
    1.93 -    DWORD style;
    1.94 -    HWND top;
    1.95 -    BOOL menu;
    1.96 -    int x, y;
    1.97 -    int w, h;
    1.98 -
    1.99 -    /* Figure out what the window area will be */
   1.100 -    if ( SDL_ShouldAllowTopmost() && (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS )) {
   1.101 -        top = HWND_TOPMOST;
   1.102 -    } else {
   1.103 -        top = HWND_NOTOPMOST;
   1.104 -    }
   1.105 -    style = GetWindowLong(hwnd, GWL_STYLE);
   1.106 -    rect.left = 0;
   1.107 -    rect.top = 0;
   1.108 -    rect.right = window->w;
   1.109 -    rect.bottom = window->h;
   1.110 -    menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
   1.111 -    AdjustWindowRectEx(&rect, style, menu, 0);
   1.112 -    w = (rect.right - rect.left);
   1.113 -    h = (rect.bottom - rect.top);
   1.114 -    x = window->x + rect.left;
   1.115 -    y = window->y + rect.top;
   1.116 -
   1.117 -    SetWindowPos(hwnd, top, x, y, w, h, flags);
   1.118 -}
   1.119 -
   1.120  void
   1.121  WIN_SetWindowPosition(_THIS, SDL_Window * window)
   1.122  {
   1.123 @@ -404,15 +404,7 @@
   1.124  void
   1.125  WIN_RaiseWindow(_THIS, SDL_Window * window)
   1.126  {
   1.127 -    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
   1.128 -    HWND top;
   1.129 -
   1.130 -    if ( SDL_ShouldAllowTopmost() && (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS )) {
   1.131 -        top = HWND_TOPMOST;
   1.132 -    } else {
   1.133 -        top = HWND_NOTOPMOST;
   1.134 -    }
   1.135 -    SetWindowPos(hwnd, top, 0, 0, 0, 0, (SWP_NOMOVE | SWP_NOSIZE));
   1.136 +    WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE);
   1.137  }
   1.138  
   1.139  void
   1.140 @@ -444,7 +436,7 @@
   1.141      }
   1.142  
   1.143      SetWindowLong(hwnd, GWL_STYLE, style);
   1.144 -    SetWindowPos(hwnd, hwnd, window->x, window->y, window->w, window->h, SWP_FRAMECHANGED | SWP_NOREPOSITION | SWP_NOZORDER |SWP_NOACTIVATE | SWP_NOSENDCHANGING);
   1.145 +    WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_FRAMECHANGED | SWP_NOREPOSITION | SWP_NOZORDER |SWP_NOACTIVATE | SWP_NOSENDCHANGING);
   1.146  }
   1.147  
   1.148  void
   1.149 @@ -468,7 +460,7 @@
   1.150      int x, y;
   1.151      int w, h;
   1.152  
   1.153 -    if ( SDL_ShouldAllowTopmost() && (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS )) {
   1.154 +    if (SDL_ShouldAllowTopmost() && (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) {
   1.155          top = HWND_TOPMOST;
   1.156      } else {
   1.157          top = HWND_NOTOPMOST;
   1.158 @@ -554,25 +546,13 @@
   1.159          ClipCursor(NULL);
   1.160      }
   1.161  
   1.162 -    if ( window->flags & SDL_WINDOW_FULLSCREEN )
   1.163 -    {
   1.164 -        HWND top;
   1.165 -        SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   1.166 -        HWND hwnd = data->hwnd;
   1.167 +    if (window->flags & SDL_WINDOW_FULLSCREEN) {
   1.168          UINT flags = SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE;
   1.169  
   1.170 -        if ( SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_INPUT_FOCUS ) ) {
   1.171 -            top = HWND_TOPMOST;
   1.172 -        } else {
   1.173 -            top = HWND_NOTOPMOST;
   1.174 -            flags |= SWP_NOZORDER;
   1.175 -        }
   1.176 -
   1.177          if (!(window->flags & SDL_WINDOW_SHOWN)) {
   1.178              flags |= SWP_NOACTIVATE;
   1.179          }
   1.180 -
   1.181 -        SetWindowPos(hwnd, top, 0, 0, 0, 0, flags);
   1.182 +        WIN_SetWindowPositionInternal(_this, window, flags);
   1.183      }
   1.184  }
   1.185