Simplified and unified the window creation process a little.
authorSam Lantinga <slouken@libsdl.org>
Mon, 21 Feb 2011 22:03:39 -0800
changeset 5380 2de85077eb0b
parent 5379 78c071fe4c37
child 5381 79e60256dbf7
Simplified and unified the window creation process a little.
include/SDL_video.h
src/video/SDL_video.c
src/video/cocoa/SDL_cocoawindow.m
--- a/include/SDL_video.h	Mon Feb 21 21:32:11 2011 -0800
+++ b/include/SDL_video.h	Mon Feb 21 22:03:39 2011 -0800
@@ -97,13 +97,14 @@
  */
 typedef enum
 {
-    SDL_WINDOW_FULLSCREEN = 0x00000001,         /**< fullscreen window, implies borderless */
+    SDL_WINDOW_FULLSCREEN = 0x00000001,         /**< fullscreen window */
     SDL_WINDOW_OPENGL = 0x00000002,             /**< window usable with OpenGL context */
     SDL_WINDOW_SHOWN = 0x00000004,              /**< window is visible */
-    SDL_WINDOW_BORDERLESS = 0x00000008,         /**< no window decoration */
-    SDL_WINDOW_RESIZABLE = 0x00000010,          /**< window can be resized */
-    SDL_WINDOW_MINIMIZED = 0x00000020,          /**< window is minimized */
-    SDL_WINDOW_MAXIMIZED = 0x00000040,          /**< window is maximized */
+    SDL_WINDOW_HIDDEN = 0x00000008,             /**< window is not visible */
+    SDL_WINDOW_BORDERLESS = 0x00000010,         /**< no window decoration */
+    SDL_WINDOW_RESIZABLE = 0x00000020,          /**< window can be resized */
+    SDL_WINDOW_MINIMIZED = 0x00000040,          /**< window is minimized */
+    SDL_WINDOW_MAXIMIZED = 0x00000080,          /**< window is maximized */
     SDL_WINDOW_INPUT_GRABBED = 0x00000100,      /**< window has grabbed input focus */
     SDL_WINDOW_INPUT_FOCUS = 0x00000200,        /**< window has input focus */
     SDL_WINDOW_MOUSE_FOCUS = 0x00000400,        /**< window has mouse focus */
--- a/src/video/SDL_video.c	Mon Feb 21 21:32:11 2011 -0800
+++ b/src/video/SDL_video.c	Mon Feb 21 22:03:39 2011 -0800
@@ -581,6 +581,21 @@
 }
 
 int
+SDL_GetIndexOfDisplay(SDL_VideoDisplay *display)
+{
+    int displayIndex;
+
+    for (displayIndex = 0; displayIndex < _this->num_displays; ++displayIndex) {
+        if (display == &_this->displays[displayIndex]) {
+            return displayIndex;
+        }
+    }
+
+    /* Couldn't find the display, just use index 0 */
+    return 0;
+}
+
+int
 SDL_GetDisplayBounds(int displayIndex, SDL_Rect * rect)
 {
     CHECK_DISPLAY_INDEX(displayIndex, -1);
@@ -1066,14 +1081,32 @@
     SDL_OnWindowResized(window);
 }
 
+#define CREATE_FLAGS \
+    (SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE)
+
+static void
+SDL_FinishWindowCreation(SDL_Window *window, Uint32 flags)
+{
+    if (flags & SDL_WINDOW_MAXIMIZED) {
+        SDL_MaximizeWindow(window);
+    }
+    if (flags & SDL_WINDOW_MINIMIZED) {
+        SDL_MinimizeWindow(window);
+    }
+    if (flags & SDL_WINDOW_FULLSCREEN) {
+        SDL_SetWindowFullscreen(window, SDL_TRUE);
+    }
+    if (flags & SDL_WINDOW_INPUT_GRABBED) {
+        SDL_SetWindowGrab(window, SDL_TRUE);
+    }
+    if (!(flags & SDL_WINDOW_HIDDEN)) {
+        SDL_ShowWindow(window);
+    }
+}
+
 SDL_Window *
 SDL_CreateWindow(const char *title, int x, int y, int w, int h, Uint32 flags)
 {
-    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
-                                  SDL_WINDOW_OPENGL |
-                                  SDL_WINDOW_BORDERLESS |
-                                  SDL_WINDOW_RESIZABLE |
-                                  SDL_WINDOW_INPUT_GRABBED);
     SDL_Window *window;
 
     if (!_this) {
@@ -1101,7 +1134,22 @@
     window->y = y;
     window->w = w;
     window->h = h;
-    window->flags = (flags & allowed_flags);
+    if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISUNDEFINED(y) ||
+        SDL_WINDOWPOS_ISCENTERED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
+        SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
+        int displayIndex;
+        SDL_Rect bounds;
+
+        displayIndex = SDL_GetIndexOfDisplay(display);
+        SDL_GetDisplayBounds(displayIndex, &bounds);
+        if (SDL_WINDOWPOS_ISUNDEFINED(x) || SDL_WINDOWPOS_ISCENTERED(y)) {
+            window->x = bounds.x + (bounds.w - w) / 2;
+        }
+        if (SDL_WINDOWPOS_ISUNDEFINED(y) || SDL_WINDOWPOS_ISCENTERED(y)) {
+            window->y = bounds.y + (bounds.h - h) / 2;
+        }
+    }
+    window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
     window->next = _this->windows;
     if (_this->windows) {
         _this->windows->prev = window;
@@ -1116,16 +1164,7 @@
     if (title) {
         SDL_SetWindowTitle(window, title);
     }
-    if (flags & SDL_WINDOW_MAXIMIZED) {
-        SDL_MaximizeWindow(window);
-    }
-    if (flags & SDL_WINDOW_MINIMIZED) {
-        SDL_MinimizeWindow(window);
-    }
-    if (flags & SDL_WINDOW_SHOWN) {
-        SDL_ShowWindow(window);
-    }
-    SDL_UpdateWindowGrab(window);
+    SDL_FinishWindowCreation(window, flags);
 
     return window;
 }
@@ -1160,12 +1199,6 @@
 int
 SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
 {
-    const Uint32 allowed_flags = (SDL_WINDOW_FULLSCREEN |
-                                  SDL_WINDOW_OPENGL |
-                                  SDL_WINDOW_BORDERLESS |
-                                  SDL_WINDOW_RESIZABLE |
-                                  SDL_WINDOW_INPUT_GRABBED |
-                                  SDL_WINDOW_FOREIGN);
     char *title = window->title;
 
     if ((flags & SDL_WINDOW_OPENGL) && !_this->GL_CreateContext) {
@@ -1204,7 +1237,7 @@
     }
 
     window->title = NULL;
-    window->flags = (flags & allowed_flags);
+    window->flags = ((flags & CREATE_FLAGS) | SDL_WINDOW_HIDDEN);
 
     if (_this->CreateWindow && !(flags & SDL_WINDOW_FOREIGN)) {
         if (_this->CreateWindow(_this, window) < 0) {
@@ -1219,16 +1252,7 @@
         SDL_SetWindowTitle(window, title);
         SDL_free(title);
     }
-    if (flags & SDL_WINDOW_MAXIMIZED) {
-        SDL_MaximizeWindow(window);
-    }
-    if (flags & SDL_WINDOW_MINIMIZED) {
-        SDL_MinimizeWindow(window);
-    }
-    if (flags & SDL_WINDOW_SHOWN) {
-        SDL_ShowWindow(window);
-    }
-    SDL_UpdateWindowGrab(window);
+    SDL_FinishWindowCreation(window, flags);
 
     return 0;
 }
--- a/src/video/cocoa/SDL_cocoawindow.m	Mon Feb 21 21:32:11 2011 -0800
+++ b/src/video/cocoa/SDL_cocoawindow.m	Mon Feb 21 22:03:39 2011 -0800
@@ -516,7 +516,7 @@
     {
         unsigned int style = [nswindow styleMask];
 
-        if ((style & ~NSResizableWindowMask) == NSBorderlessWindowMask) {
+        if (style == NSBorderlessWindowMask) {
             window->flags |= SDL_WINDOW_BORDERLESS;
         } else {
             window->flags &= ~SDL_WINDOW_BORDERLESS;
@@ -527,7 +527,8 @@
             window->flags &= ~SDL_WINDOW_RESIZABLE;
         }
     }
-    if ([nswindow isZoomed]) {
+    /* isZoomed always returns true if the window is not resizable */
+    if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
         window->flags |= SDL_WINDOW_MAXIMIZED;
     } else {
         window->flags &= ~SDL_WINDOW_MAXIMIZED;
@@ -540,10 +541,6 @@
     if ([nswindow isKeyWindow]) {
         window->flags |= SDL_WINDOW_INPUT_FOCUS;
         SDL_SetKeyboardFocus(data->window);
-
-        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
-            /* FIXME */
-        }
     }
 
     /* All done! */
@@ -563,20 +560,8 @@
     unsigned int style;
 
     Cocoa_GetDisplayBounds(_this, display, &bounds);
-    if (SDL_WINDOWPOS_ISCENTERED(window->x)) {
-        rect.origin.x = bounds.x + (bounds.w - window->w) / 2;
-    } else if (SDL_WINDOWPOS_ISUNDEFINED(window->x)) {
-        rect.origin.x = bounds.x;
-    } else {
-        rect.origin.x = window->x;
-    }
-    if (SDL_WINDOWPOS_ISCENTERED(window->y)) {
-        rect.origin.y = bounds.y + (bounds.h - window->h) / 2;
-    } else if (SDL_WINDOWPOS_ISUNDEFINED(window->y)) {
-        rect.origin.y = bounds.y;
-    } else {
-        rect.origin.y = window->y;
-    }
+    rect.origin.x = window->x;
+    rect.origin.y = window->y;
     rect.size.width = window->w;
     rect.size.height = window->h;
     ConvertNSRect(&rect);
@@ -763,7 +748,7 @@
 
     if ([nswindow isMiniaturized]) {
         [nswindow deminiaturize:nil];
-    } else if ([nswindow isZoomed]) {
+    } else if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
         [nswindow zoom:nil];
     }
     [pool release];