Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
slouken committed Feb 26, 2011
1 parent f1250f8 commit 90db455
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 17 deletions.
8 changes: 8 additions & 0 deletions src/events/SDL_windowevents.c
Expand Up @@ -89,13 +89,21 @@ SDL_SendWindowEvent(SDL_Window * window, Uint8 windowevent, int data1,
SDL_WINDOWPOS_ISUNDEFINED(data2)) {
return 0;
}
if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
window->windowed.x = data1;
window->windowed.y = data2;
}
if (data1 == window->x && data2 == window->y) {
return 0;
}
window->x = data1;
window->y = data2;
break;
case SDL_WINDOWEVENT_RESIZED:
if (!(window->flags & SDL_WINDOW_FULLSCREEN)) {
window->windowed.w = data1;
window->windowed.h = data2;
}
if (data1 == window->w && data2 == window->h) {
return 0;
}
Expand Down
3 changes: 3 additions & 0 deletions src/video/SDL_sysvideo.h
Expand Up @@ -76,6 +76,9 @@ struct SDL_Window
int w, h;
Uint32 flags;

/* Stored position and size for windowed mode */
SDL_Rect windowed;

SDL_DisplayMode fullscreen_mode;

SDL_Surface *surface;
Expand Down
5 changes: 5 additions & 0 deletions src/video/SDL_video.c
Expand Up @@ -1087,6 +1087,11 @@ SDL_UpdateFullscreenMode(SDL_Window * window)
static void
SDL_FinishWindowCreation(SDL_Window *window, Uint32 flags)
{
window->windowed.x = window->x;
window->windowed.y = window->y;
window->windowed.w = window->w;
window->windowed.h = window->h;

if (flags & SDL_WINDOW_MAXIMIZED) {
SDL_MaximizeWindow(window);
}
Expand Down
42 changes: 35 additions & 7 deletions src/video/cocoa/SDL_cocoawindow.m
Expand Up @@ -22,6 +22,7 @@
#include "SDL_config.h"

#include "SDL_syswm.h"
#include "SDL_timer.h" /* For SDL_GetTicks() */
#include "../SDL_sysvideo.h"
#include "../../events/SDL_keyboard_c.h"
#include "../../events/SDL_mouse_c.h"
Expand All @@ -31,6 +32,9 @@
#include "SDL_cocoashape.h"
#include "SDL_cocoamouse.h"


static Uint32 s_moveHack;

static __inline__ void ConvertNSRect(NSRect *r)
{
r->origin.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - r->origin.y - r->size.height;
Expand Down Expand Up @@ -115,11 +119,29 @@ - (void)windowDidExpose:(NSNotification *)aNotification
- (void)windowDidMove:(NSNotification *)aNotification
{
int x, y;
NSRect rect = [_data->nswindow contentRectForFrameRect:[_data->nswindow frame]];
SDL_Window *window = _data->window;
NSWindow *nswindow = _data->nswindow;
NSRect rect = [nswindow contentRectForFrameRect:[nswindow frame]];
ConvertNSRect(&rect);

if (s_moveHack) {
SDL_bool blockMove = ((SDL_GetTicks() - s_moveHack) < 500);

s_moveHack = 0;

if (blockMove) {
/* Cocoa is adjusting the window in response to a mode change */
rect.origin.x = window->x;
rect.origin.y = window->y;
ConvertNSRect(&rect);
[nswindow setFrameOrigin:rect.origin];
return;
}
}

x = (int)rect.origin.x;
y = (int)rect.origin.y;
SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MOVED, x, y);
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_MOVED, x, y);
}

- (void)windowDidResize:(NSNotification *)aNotification
Expand Down Expand Up @@ -786,17 +808,23 @@ - (void)rightMouseDown:(NSEvent *)theEvent
if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
[nswindow performSelector: @selector(setStyleMask:) withObject: (id)NSBorderlessWindowMask];
}
[nswindow setFrameOrigin:rect.origin];
[nswindow setContentSize:rect.size];
} else {
rect.origin.x = window->windowed.x;
rect.origin.y = window->windowed.y;
rect.size.width = window->windowed.w;
rect.size.height = window->windowed.h;
ConvertNSRect(&rect);

if ([nswindow respondsToSelector: @selector(setStyleMask:)]) {
[nswindow performSelector: @selector(setStyleMask:) withObject: (id)(uintptr_t)GetWindowStyle(window)];
}

// This doesn't seem to do anything...
//[nswindow setFrameOrigin:origin];
}

s_moveHack = 0;
[nswindow setFrameOrigin:rect.origin];
[nswindow setContentSize:rect.size];
s_moveHack = SDL_GetTicks();

#ifdef FULLSCREEN_TOGGLEABLE
if (fullscreen) {
/* OpenGL is rendering to the window, so make it visible! */
Expand Down
12 changes: 4 additions & 8 deletions src/video/windows/SDL_windowswindow.c
Expand Up @@ -553,19 +553,15 @@ WIN_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display,
WIN_GetDisplayBounds(_this, display, &bounds);

if (fullscreen) {
/* Save the windowed position */
data->windowed_x = window->x;
data->windowed_y = window->y;

x = bounds.x;
y = bounds.y;
w = bounds.w;
h = bounds.h;
} else {
rect.left = 0;
rect.top = 0;
rect.right = window->w;
rect.bottom = window->h;
rect.right = window->windowed.w;
rect.bottom = window->windowed.h;
#ifdef _WIN32_WCE
menu = FALSE;
#else
Expand All @@ -574,8 +570,8 @@ WIN_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display,
AdjustWindowRectEx(&rect, style, menu, 0);
w = (rect.right - rect.left);
h = (rect.bottom - rect.top);
x = data->windowed_x + rect.left;
y = data->windowed_y + rect.top;
x = window->windowed.x + rect.left;
y = window->windowed.y + rect.top;
}
SetWindowLong(hwnd, GWL_STYLE, style);
SetWindowPos(hwnd, top, x, y, w, h, SWP_NOCOPYBITS);
Expand Down
2 changes: 0 additions & 2 deletions src/video/windows/SDL_windowswindow.h
Expand Up @@ -43,8 +43,6 @@ typedef struct
WNDPROC wndproc;
SDL_bool created;
int mouse_pressed;
int windowed_x;
int windowed_y;
struct SDL_VideoData *videodata;
} SDL_WindowData;

Expand Down

0 comments on commit 90db455

Please sign in to comment.