src/video/windows/SDL_windowswindow.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Feb 2011 23:07:14 -0800
changeset 5305 75f5ff92ad08
parent 5288 d4381f3b0d1e
child 5382 fd9a7a0afcbf
permissions -rwxr-xr-x
Fixed a host of issues with Windows fullscreen modes. Toggling fullscreen OpenGL works now in my test environment.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@1895
    24
#include "../SDL_sysvideo.h"
slouken@2970
    25
#include "../SDL_pixels_c.h"
slouken@1895
    26
#include "../../events/SDL_keyboard_c.h"
slouken@1895
    27
slouken@5062
    28
#include "SDL_windowsvideo.h"
slouken@5062
    29
#include "SDL_windowswindow.h"
slouken@1895
    30
slouken@5062
    31
/* This is included after SDL_windowsvideo.h, which includes windows.h */
slouken@1895
    32
#include "SDL_syswm.h"
slouken@3168
    33
slouken@5086
    34
/* Windows CE compatibility */
slouken@5086
    35
#ifndef SWP_NOCOPYBITS
slouken@5086
    36
#define SWP_NOCOPYBITS 0
slouken@5086
    37
#endif
slouken@3168
    38
slouken@2713
    39
/* Fake window to help with DirectInput events. */
slouken@2713
    40
HWND SDL_HelperWindow = NULL;
slouken@2714
    41
static WCHAR *SDL_HelperWindowClassName = TEXT("SDLHelperWindowInputCatcher");
slouken@2714
    42
static WCHAR *SDL_HelperWindowName = TEXT("SDLHelperWindowInputMsgWindow");
slouken@2713
    43
static ATOM SDL_HelperWindowClass = 0;
slouken@2713
    44
slouken@5305
    45
#define STYLE_BASIC         (WS_CLIPSIBLINGS | WS_CLIPCHILDREN)
slouken@5305
    46
#define STYLE_FULLSCREEN    (WS_POPUP)
slouken@5305
    47
#define STYLE_BORDERLESS    (WS_POPUP)
slouken@5305
    48
#define STYLE_NORMAL        (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
slouken@5305
    49
#define STYLE_RESIZABLE     (WS_THICKFRAME | WS_MAXIMIZEBOX)
slouken@5305
    50
#define STYLE_MASK          (STYLE_FULLSCREEN | STYLE_BORDERLESS | STYLE_NORMAL | STYLE_RESIZABLE)
slouken@5305
    51
slouken@5305
    52
static DWORD
slouken@5305
    53
GetWindowStyle(SDL_Window * window)
slouken@5305
    54
{
slouken@5305
    55
    DWORD style = 0;
slouken@5305
    56
slouken@5305
    57
	if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@5305
    58
        style |= STYLE_FULLSCREEN;
slouken@5305
    59
	} else {
slouken@5305
    60
		if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@5305
    61
            style |= STYLE_BORDERLESS;
slouken@5305
    62
		} else {
slouken@5305
    63
            style |= STYLE_NORMAL;
slouken@5305
    64
		}
slouken@5305
    65
		if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@5305
    66
            style |= STYLE_RESIZABLE;
slouken@5305
    67
		}
slouken@5305
    68
	}
slouken@5305
    69
    return style;
slouken@5305
    70
}
slouken@5305
    71
slouken@1895
    72
static int
slouken@1951
    73
SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created)
slouken@1895
    74
{
slouken@1951
    75
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@5246
    76
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@1895
    77
    SDL_WindowData *data;
slouken@1895
    78
slouken@1895
    79
    /* Allocate the window data */
slouken@1895
    80
    data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
slouken@1895
    81
    if (!data) {
slouken@1895
    82
        SDL_OutOfMemory();
slouken@1895
    83
        return -1;
slouken@1895
    84
    }
slouken@3685
    85
    data->window = window;
slouken@1895
    86
    data->hwnd = hwnd;
slouken@1913
    87
    data->hdc = GetDC(hwnd);
slouken@1895
    88
    data->created = created;
slouken@1895
    89
    data->mouse_pressed = SDL_FALSE;
slouken@1951
    90
    data->videodata = videodata;
slouken@1895
    91
slouken@1895
    92
    /* Associate the data with the window */
slouken@1895
    93
    if (!SetProp(hwnd, TEXT("SDL_WindowData"), data)) {
slouken@1913
    94
        ReleaseDC(hwnd, data->hdc);
slouken@1895
    95
        SDL_free(data);
slouken@1895
    96
        WIN_SetError("SetProp() failed");
slouken@1895
    97
        return -1;
slouken@1895
    98
    }
slouken@1895
    99
slouken@1895
   100
    /* Set up the window proc function */
slouken@5086
   101
#ifdef GWLP_WNDPROC
slouken@1895
   102
    data->wndproc = (WNDPROC) GetWindowLongPtr(hwnd, GWLP_WNDPROC);
dewyatt@4733
   103
    if (data->wndproc == WIN_WindowProc) {
slouken@3566
   104
        data->wndproc = NULL;
slouken@5086
   105
    } else {
dewyatt@4735
   106
        SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) WIN_WindowProc);
dewyatt@4735
   107
    }
slouken@5086
   108
#else
slouken@5086
   109
    data->wndproc = (WNDPROC) GetWindowLong(hwnd, GWL_WNDPROC);
slouken@5086
   110
    if (data->wndproc == WIN_WindowProc) {
slouken@5086
   111
        data->wndproc = NULL;
slouken@5086
   112
    } else {
slouken@5086
   113
        SetWindowLong(hwnd, GWL_WNDPROC, (LONG_PTR) WIN_WindowProc);
slouken@5086
   114
    }
slouken@5086
   115
#endif
slouken@1895
   116
slouken@1895
   117
    /* Fill in the SDL window with the window data */
slouken@1895
   118
    {
slouken@1895
   119
        POINT point;
slouken@1895
   120
        point.x = 0;
slouken@1895
   121
        point.y = 0;
slouken@1895
   122
        if (ClientToScreen(hwnd, &point)) {
slouken@5246
   123
            window->x = point.x;
slouken@5246
   124
            window->y = point.y;
slouken@1895
   125
        }
slouken@1895
   126
    }
slouken@1895
   127
    {
slouken@1895
   128
        RECT rect;
slouken@1895
   129
        if (GetClientRect(hwnd, &rect)) {
slouken@1895
   130
            window->w = rect.right;
slouken@1895
   131
            window->h = rect.bottom;
slouken@1895
   132
        }
slouken@1895
   133
    }
slouken@1895
   134
    {
slouken@1895
   135
        DWORD style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   136
        if (style & WS_VISIBLE) {
slouken@1895
   137
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1895
   138
        } else {
slouken@1895
   139
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1895
   140
        }
slouken@1895
   141
        if (style & (WS_BORDER | WS_THICKFRAME)) {
slouken@1895
   142
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1895
   143
        } else {
slouken@1895
   144
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1895
   145
        }
slouken@1895
   146
        if (style & WS_THICKFRAME) {
slouken@1895
   147
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1895
   148
        } else {
slouken@1895
   149
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1895
   150
        }
slouken@5086
   151
#ifdef WS_MAXIMIZE
slouken@1895
   152
        if (style & WS_MAXIMIZE) {
slouken@1895
   153
            window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@5086
   154
        } else
slouken@5086
   155
#endif
slouken@5086
   156
        {
slouken@1895
   157
            window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1895
   158
        }
slouken@5086
   159
#ifdef WS_MINIMIZE
slouken@1895
   160
        if (style & WS_MINIMIZE) {
slouken@1895
   161
            window->flags |= SDL_WINDOW_MINIMIZED;
slouken@5086
   162
        } else
slouken@5086
   163
#endif
slouken@5086
   164
        {
slouken@1895
   165
            window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1895
   166
        }
slouken@1895
   167
    }
slouken@1895
   168
    if (GetFocus() == hwnd) {
slouken@1895
   169
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@4465
   170
        SDL_SetKeyboardFocus(data->window);
slouken@1895
   171
slouken@1895
   172
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1895
   173
            RECT rect;
slouken@1895
   174
            GetClientRect(hwnd, &rect);
slouken@1895
   175
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   176
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   177
            ClipCursor(&rect);
slouken@1895
   178
        }
slouken@1895
   179
    }
slouken@1895
   180
slouken@4919
   181
	/* Enable multi-touch */
slouken@4948
   182
    if (videodata->RegisterTouchWindow) {
slouken@4948
   183
        videodata->RegisterTouchWindow(hwnd, (TWF_FINETOUCH|TWF_WANTPALM));
slouken@4948
   184
    }
slouken@4919
   185
slouken@1895
   186
    /* All done! */
slouken@1895
   187
    window->driverdata = data;
slouken@1895
   188
    return 0;
slouken@1895
   189
}
slouken@1895
   190
slouken@1895
   191
int
slouken@1895
   192
WIN_CreateWindow(_THIS, SDL_Window * window)
slouken@1895
   193
{
slouken@5246
   194
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@1895
   195
    HWND hwnd;
slouken@1895
   196
    RECT rect;
slouken@3528
   197
    SDL_Rect bounds;
slouken@5305
   198
    DWORD style = STYLE_BASIC;
slouken@1895
   199
    int x, y;
slouken@1895
   200
    int w, h;
slouken@5305
   201
    
slouken@5305
   202
    style |= GetWindowStyle(window);
slouken@1895
   203
slouken@1895
   204
    /* Figure out what the window area will be */
slouken@1895
   205
    rect.left = 0;
slouken@1895
   206
    rect.top = 0;
slouken@1895
   207
    rect.right = window->w;
slouken@1895
   208
    rect.bottom = window->h;
slouken@1895
   209
    AdjustWindowRectEx(&rect, style, FALSE, 0);
slouken@1895
   210
    w = (rect.right - rect.left);
slouken@1895
   211
    h = (rect.bottom - rect.top);
slouken@1895
   212
slouken@3528
   213
    WIN_GetDisplayBounds(_this, display, &bounds);
slouken@5305
   214
    if (SDL_WINDOWPOS_ISCENTERED(window->x)) {
slouken@4486
   215
        x = bounds.x + (bounds.w - w) / 2;
slouken@5246
   216
    } else if (SDL_WINDOWPOS_ISUNDEFINED(window->x)) {
slouken@3528
   217
        if (bounds.x == 0) {
slouken@3528
   218
            x = CW_USEDEFAULT;
slouken@3528
   219
        } else {
slouken@3528
   220
            x = bounds.x;
slouken@3528
   221
        }
slouken@1895
   222
    } else {
slouken@5246
   223
        x = window->x + rect.left;
slouken@1895
   224
    }
slouken@5305
   225
    if (SDL_WINDOWPOS_ISCENTERED(window->y)) {
slouken@4486
   226
        y = bounds.y + (bounds.h - h) / 2;
slouken@5246
   227
    } else if (SDL_WINDOWPOS_ISUNDEFINED(window->x)) {
slouken@3528
   228
        if (bounds.x == 0) {
slouken@3528
   229
            y = CW_USEDEFAULT;
slouken@3528
   230
        } else {
slouken@3528
   231
            y = bounds.y;
slouken@3528
   232
        }
slouken@1895
   233
    } else {
slouken@5246
   234
        y = window->y + rect.top;
slouken@1895
   235
    }
slouken@1895
   236
slouken@1956
   237
    hwnd =
slouken@1956
   238
        CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, NULL, NULL,
slouken@1956
   239
                     SDL_Instance, NULL);
slouken@1895
   240
    if (!hwnd) {
slouken@1895
   241
        WIN_SetError("Couldn't create window");
slouken@1895
   242
        return -1;
slouken@1895
   243
    }
slouken@1895
   244
slouken@2710
   245
    WIN_PumpEvents(_this);
slouken@2710
   246
slouken@1951
   247
    if (SetupWindowData(_this, window, hwnd, SDL_TRUE) < 0) {
slouken@1895
   248
        DestroyWindow(hwnd);
slouken@1895
   249
        return -1;
slouken@1895
   250
    }
slouken@5088
   251
#if SDL_VIDEO_OPENGL_WGL
slouken@1913
   252
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1913
   253
        if (WIN_GL_SetupWindow(_this, window) < 0) {
slouken@1913
   254
            WIN_DestroyWindow(_this, window);
slouken@1913
   255
            return -1;
slouken@1913
   256
        }
slouken@1913
   257
    }
slouken@1913
   258
#endif
slouken@1895
   259
    return 0;
slouken@1895
   260
}
slouken@1895
   261
slouken@1895
   262
int
slouken@1895
   263
WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1895
   264
{
slouken@1895
   265
    HWND hwnd = (HWND) data;
slouken@1895
   266
    LPTSTR title;
slouken@1895
   267
    int titleLen;
slouken@1895
   268
slouken@1895
   269
    /* Query the title from the existing window */
slouken@1895
   270
    titleLen = GetWindowTextLength(hwnd);
slouken@1895
   271
    title = SDL_stack_alloc(TCHAR, titleLen + 1);
slouken@1895
   272
    if (title) {
slouken@1895
   273
        titleLen = GetWindowText(hwnd, title, titleLen);
slouken@1895
   274
    } else {
slouken@1895
   275
        titleLen = 0;
slouken@1895
   276
    }
slouken@1895
   277
    if (titleLen > 0) {
slouken@1895
   278
        window->title = WIN_StringToUTF8(title);
slouken@1895
   279
    }
slouken@1895
   280
    if (title) {
slouken@1895
   281
        SDL_stack_free(title);
slouken@1895
   282
    }
slouken@1895
   283
slouken@1951
   284
    if (SetupWindowData(_this, window, hwnd, SDL_FALSE) < 0) {
slouken@1895
   285
        return -1;
slouken@1895
   286
    }
slouken@1895
   287
    return 0;
slouken@1895
   288
}
slouken@1895
   289
slouken@1895
   290
void
slouken@1895
   291
WIN_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1895
   292
{
slouken@1895
   293
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   294
    LPTSTR title;
slouken@1895
   295
slouken@1895
   296
    if (window->title) {
slouken@1895
   297
        title = WIN_UTF8ToString(window->title);
slouken@1895
   298
    } else {
slouken@1895
   299
        title = NULL;
slouken@1895
   300
    }
slouken@1895
   301
    SetWindowText(hwnd, title ? title : TEXT(""));
slouken@1895
   302
    if (title) {
slouken@1895
   303
        SDL_free(title);
slouken@1895
   304
    }
slouken@1895
   305
}
slouken@1895
   306
slouken@1895
   307
void
slouken@2970
   308
WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2970
   309
{
slouken@2970
   310
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@2970
   311
    HICON hicon = NULL;
slouken@2970
   312
slouken@2990
   313
    if (icon) {
slouken@2970
   314
        BYTE *icon_bmp;
slouken@2970
   315
        int icon_len;
slouken@2970
   316
        SDL_RWops *dst;
slouken@2970
   317
        SDL_PixelFormat format;
slouken@2970
   318
        SDL_Surface *surface;
slouken@2970
   319
slouken@2970
   320
        /* Create temporary bitmap buffer */
slouken@2970
   321
        icon_len = 40 + icon->h * icon->w * 4;
slouken@2970
   322
        icon_bmp = SDL_stack_alloc(BYTE, icon_len);
slouken@2970
   323
        dst = SDL_RWFromMem(icon_bmp, icon_len);
slouken@2970
   324
        if (!dst) {
slouken@2970
   325
            SDL_stack_free(icon_bmp);
slouken@2970
   326
            return;
slouken@2970
   327
        }
slouken@2970
   328
slouken@2970
   329
        /* Write the BITMAPINFO header */
slouken@2970
   330
        SDL_WriteLE32(dst, 40);
slouken@2970
   331
        SDL_WriteLE32(dst, icon->w);
slouken@2990
   332
        SDL_WriteLE32(dst, icon->h * 2);
slouken@2970
   333
        SDL_WriteLE16(dst, 1);
slouken@2970
   334
        SDL_WriteLE16(dst, 32);
slouken@2970
   335
        SDL_WriteLE32(dst, BI_RGB);
slouken@2970
   336
        SDL_WriteLE32(dst, icon->h * icon->w * 4);
slouken@2970
   337
        SDL_WriteLE32(dst, 0);
slouken@2970
   338
        SDL_WriteLE32(dst, 0);
slouken@2970
   339
        SDL_WriteLE32(dst, 0);
slouken@2970
   340
        SDL_WriteLE32(dst, 0);
slouken@2970
   341
slouken@2970
   342
        /* Convert the icon to a 32-bit surface with alpha channel */
slouken@5288
   343
        SDL_InitFormat(&format, SDL_PIXELFORMAT_ARGB8888);
slouken@2970
   344
        surface = SDL_ConvertSurface(icon, &format, 0);
slouken@2970
   345
        if (surface) {
slouken@2970
   346
            /* Write the pixels upside down into the bitmap buffer */
slouken@2970
   347
            int y = surface->h;
slouken@2970
   348
            while (y--) {
slouken@2990
   349
                Uint8 *src = (Uint8 *) surface->pixels + y * surface->pitch;
slouken@2970
   350
                SDL_RWwrite(dst, src, surface->pitch, 1);
slouken@2970
   351
            }
slouken@2970
   352
            SDL_FreeSurface(surface);
slouken@2970
   353
slouken@3097
   354
/* TODO: create the icon in WinCE (CreateIconFromResource isn't available) */
slouken@3097
   355
#ifndef _WIN32_WCE
slouken@2990
   356
            hicon =
slouken@2990
   357
                CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
slouken@3097
   358
#endif
slouken@2970
   359
        }
slouken@2970
   360
        SDL_RWclose(dst);
slouken@2970
   361
        SDL_stack_free(icon_bmp);
slouken@2970
   362
    }
slouken@2970
   363
slouken@2971
   364
    /* Set the icon for the window */
slouken@2990
   365
    SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);
slouken@2971
   366
slouken@2971
   367
    /* Set the icon in the task manager (should we do this?) */
slouken@2990
   368
    SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) hicon);
slouken@2970
   369
}
slouken@2970
   370
slouken@2970
   371
void
slouken@1895
   372
WIN_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1895
   373
{
slouken@5246
   374
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@1895
   375
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   376
    RECT rect;
slouken@3528
   377
    SDL_Rect bounds;
slouken@1895
   378
    DWORD style;
slouken@1895
   379
    HWND top;
slouken@3168
   380
    BOOL menu;
slouken@1895
   381
    int x, y;
slouken@4486
   382
    int w, h;
slouken@1895
   383
slouken@1895
   384
    /* Figure out what the window area will be */
slouken@1895
   385
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   386
        top = HWND_TOPMOST;
slouken@1895
   387
    } else {
slouken@1895
   388
        top = HWND_NOTOPMOST;
slouken@1895
   389
    }
slouken@1895
   390
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   391
    rect.left = 0;
slouken@1895
   392
    rect.top = 0;
slouken@1895
   393
    rect.right = window->w;
slouken@1895
   394
    rect.bottom = window->h;
slouken@3168
   395
#ifdef _WIN32_WCE
slouken@3168
   396
    menu = FALSE;
slouken@3168
   397
#else
slouken@3168
   398
    menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   399
#endif
slouken@3168
   400
    AdjustWindowRectEx(&rect, style, menu, 0);
slouken@4486
   401
    w = (rect.right - rect.left);
slouken@4486
   402
    h = (rect.bottom - rect.top);
slouken@2875
   403
slouken@3528
   404
    WIN_GetDisplayBounds(_this, display, &bounds);
slouken@5305
   405
    if (SDL_WINDOWPOS_ISCENTERED(window->x)) {
slouken@4486
   406
        x = bounds.x + (bounds.w - w) / 2;
slouken@2875
   407
    } else {
slouken@5246
   408
        x = window->x + rect.left;
slouken@2875
   409
    }
slouken@5305
   410
    if (SDL_WINDOWPOS_ISCENTERED(window->y)) {
slouken@4486
   411
        y = bounds.y + (bounds.h - h) / 2;
slouken@2875
   412
    } else {
slouken@5246
   413
        y = window->y + rect.top;
slouken@2875
   414
    }
slouken@1895
   415
slouken@1956
   416
    SetWindowPos(hwnd, top, x, y, 0, 0, (SWP_NOCOPYBITS | SWP_NOSIZE));
slouken@1895
   417
}
slouken@1895
   418
slouken@1895
   419
void
slouken@1895
   420
WIN_SetWindowSize(_THIS, SDL_Window * window)
slouken@1895
   421
{
slouken@1895
   422
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   423
    RECT rect;
slouken@1895
   424
    DWORD style;
slouken@1895
   425
    HWND top;
slouken@3168
   426
    BOOL menu;
slouken@1895
   427
    int w, h;
slouken@1895
   428
slouken@1895
   429
    /* Figure out what the window area will be */
slouken@1895
   430
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   431
        top = HWND_TOPMOST;
slouken@1895
   432
    } else {
slouken@1895
   433
        top = HWND_NOTOPMOST;
slouken@1895
   434
    }
slouken@1895
   435
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   436
    rect.left = 0;
slouken@1895
   437
    rect.top = 0;
slouken@1895
   438
    rect.right = window->w;
slouken@1895
   439
    rect.bottom = window->h;
slouken@3168
   440
#ifdef _WIN32_WCE
slouken@3168
   441
    menu = FALSE;
slouken@3168
   442
#else
slouken@3168
   443
    menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   444
#endif
slouken@3168
   445
    AdjustWindowRectEx(&rect, style, menu, 0);
slouken@1895
   446
    w = (rect.right - rect.left);
slouken@1895
   447
    h = (rect.bottom - rect.top);
slouken@1895
   448
slouken@2968
   449
    SetWindowPos(hwnd, top, 0, 0, w, h, (SWP_NOCOPYBITS | SWP_NOMOVE));
slouken@5246
   450
}
slouken@5246
   451
slouken@5246
   452
#ifdef _WIN32_WCE
slouken@5246
   453
void WINCE_ShowWindow(_THIS, SDL_Window* window, int visible)
slouken@5246
   454
{
slouken@5246
   455
    SDL_WindowData* windowdata = (SDL_WindowData*) window->driverdata;
slouken@5246
   456
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@5246
   457
slouken@5246
   458
    if(visible) {
slouken@5246
   459
        if(window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@5246
   460
            if(videodata->SHFullScreen)
slouken@5246
   461
                videodata->SHFullScreen(windowdata->hwnd, SHFS_HIDETASKBAR | SHFS_HIDESTARTICON | SHFS_HIDESIPBUTTON);
slouken@5178
   462
slouken@5246
   463
            ShowWindow(FindWindow(TEXT("HHTaskBar"), NULL), SW_HIDE);
slouken@5246
   464
        }
slouken@5246
   465
slouken@5246
   466
        ShowWindow(windowdata->hwnd, SW_SHOW);
slouken@5246
   467
        SetForegroundWindow(windowdata->hwnd);
slouken@5246
   468
    } else {
slouken@5246
   469
        ShowWindow(windowdata->hwnd, SW_HIDE);
slouken@5246
   470
slouken@5246
   471
        if(window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@5246
   472
            if(videodata->SHFullScreen)
slouken@5246
   473
                videodata->SHFullScreen(windowdata->hwnd, SHFS_SHOWTASKBAR | SHFS_SHOWSTARTICON | SHFS_SHOWSIPBUTTON);
slouken@5246
   474
slouken@5246
   475
            ShowWindow(FindWindow(TEXT("HHTaskBar"), NULL), SW_SHOW);
slouken@5246
   476
slouken@5246
   477
        }
slouken@5246
   478
    }
slouken@1895
   479
}
slouken@5178
   480
#endif /* _WIN32_WCE */
slouken@1895
   481
slouken@1895
   482
void
slouken@1895
   483
WIN_ShowWindow(_THIS, SDL_Window * window)
slouken@1895
   484
{
slouken@4569
   485
#ifdef _WIN32_WCE
slouken@4569
   486
    WINCE_ShowWindow(_this, window, 1);
slouken@4569
   487
#else
slouken@1895
   488
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   489
    ShowWindow(hwnd, SW_SHOW);
slouken@3168
   490
#endif
slouken@1895
   491
}
slouken@1895
   492
slouken@1895
   493
void
slouken@1895
   494
WIN_HideWindow(_THIS, SDL_Window * window)
slouken@1895
   495
{
slouken@4569
   496
#ifdef _WIN32_WCE
slouken@4569
   497
    WINCE_ShowWindow(_this, window, 0);
slouken@4569
   498
#else
slouken@1895
   499
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   500
    ShowWindow(hwnd, SW_HIDE);
slouken@3168
   501
#endif
slouken@1895
   502
}
slouken@1895
   503
slouken@1895
   504
void
slouken@1895
   505
WIN_RaiseWindow(_THIS, SDL_Window * window)
slouken@1895
   506
{
slouken@1895
   507
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   508
    HWND top;
slouken@1895
   509
slouken@1895
   510
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   511
        top = HWND_TOPMOST;
slouken@1895
   512
    } else {
slouken@1895
   513
        top = HWND_NOTOPMOST;
slouken@1895
   514
    }
slouken@1895
   515
    SetWindowPos(hwnd, top, 0, 0, 0, 0, (SWP_NOMOVE | SWP_NOSIZE));
slouken@1895
   516
}
slouken@1895
   517
slouken@1895
   518
void
slouken@1895
   519
WIN_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1895
   520
{
slouken@1895
   521
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@4569
   522
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@3168
   523
slouken@3168
   524
#ifdef _WIN32_WCE
slouken@4569
   525
    if((window->flags & SDL_WINDOW_FULLSCREEN) && videodata->SHFullScreen)
slouken@4569
   526
        videodata->SHFullScreen(hwnd, SHFS_HIDETASKBAR | SHFS_HIDESTARTICON | SHFS_HIDESIPBUTTON);
slouken@3168
   527
#endif
slouken@4569
   528
slouken@4569
   529
    ShowWindow(hwnd, SW_MAXIMIZE);
slouken@1895
   530
}
slouken@1895
   531
slouken@1895
   532
void
slouken@1895
   533
WIN_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1895
   534
{
slouken@1895
   535
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@4569
   536
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1895
   537
slouken@1895
   538
    ShowWindow(hwnd, SW_MINIMIZE);
slouken@3168
   539
slouken@3168
   540
#ifdef _WIN32_WCE
slouken@4569
   541
    if((window->flags & SDL_WINDOW_FULLSCREEN) && videodata->SHFullScreen)
slouken@5305
   542
        videodata->SHFullScreen(hwnd, SHFS_SHOWTASKBAR | SHFS_SHOWSTARTICON | SHFS_SHOWSIPBUTTON);
slouken@3168
   543
#endif
slouken@1895
   544
}
slouken@1895
   545
slouken@1895
   546
void
slouken@1895
   547
WIN_RestoreWindow(_THIS, SDL_Window * window)
slouken@1895
   548
{
slouken@1895
   549
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   550
slouken@1895
   551
    ShowWindow(hwnd, SW_RESTORE);
slouken@1895
   552
}
slouken@1895
   553
slouken@1895
   554
void
slouken@5305
   555
WIN_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
slouken@5305
   556
{
slouken@5305
   557
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5305
   558
    HWND hwnd = data->hwnd;
slouken@5305
   559
    RECT rect;
slouken@5305
   560
    SDL_Rect bounds;
slouken@5305
   561
    DWORD style;
slouken@5305
   562
    HWND top;
slouken@5305
   563
    BOOL menu;
slouken@5305
   564
    int x, y;
slouken@5305
   565
    int w, h;
slouken@5305
   566
slouken@5305
   567
    if (fullscreen) {
slouken@5305
   568
        top = HWND_TOPMOST;
slouken@5305
   569
    } else {
slouken@5305
   570
        top = HWND_NOTOPMOST;
slouken@5305
   571
    }
slouken@5305
   572
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@5305
   573
    style &= ~STYLE_MASK;
slouken@5305
   574
    style |= GetWindowStyle(window);
slouken@5305
   575
slouken@5305
   576
    WIN_GetDisplayBounds(_this, display, &bounds);
slouken@5305
   577
slouken@5305
   578
    if (fullscreen) {
slouken@5305
   579
        /* Save the windowed position */
slouken@5305
   580
        data->windowed_x = window->x;
slouken@5305
   581
        data->windowed_y = window->y;
slouken@5305
   582
slouken@5305
   583
        x = bounds.x;
slouken@5305
   584
        y = bounds.y;
slouken@5305
   585
        w = bounds.w;
slouken@5305
   586
        h = bounds.h;
slouken@5305
   587
    } else {
slouken@5305
   588
        rect.left = 0;
slouken@5305
   589
        rect.top = 0;
slouken@5305
   590
        rect.right = window->w;
slouken@5305
   591
        rect.bottom = window->h;
slouken@5305
   592
#ifdef _WIN32_WCE
slouken@5305
   593
        menu = FALSE;
slouken@5305
   594
#else
slouken@5305
   595
        menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@5305
   596
#endif
slouken@5305
   597
        AdjustWindowRectEx(&rect, style, menu, 0);
slouken@5305
   598
        w = (rect.right - rect.left);
slouken@5305
   599
        h = (rect.bottom - rect.top);
slouken@5305
   600
        x = data->windowed_x + rect.left;
slouken@5305
   601
        y = data->windowed_y + rect.top;
slouken@5305
   602
    }
slouken@5305
   603
    SetWindowLong(hwnd, GWL_STYLE, style);
slouken@5305
   604
    SetWindowPos(hwnd, top, x, y, w, h, SWP_NOCOPYBITS);
slouken@5305
   605
}
slouken@5305
   606
slouken@5305
   607
void
slouken@1895
   608
WIN_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1895
   609
{
slouken@1895
   610
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   611
slouken@2876
   612
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
slouken@2876
   613
        && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   614
        RECT rect;
slouken@1895
   615
        GetClientRect(hwnd, &rect);
slouken@1895
   616
        ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   617
        ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   618
        ClipCursor(&rect);
slouken@1895
   619
    } else {
slouken@1895
   620
        ClipCursor(NULL);
slouken@1895
   621
    }
slouken@1895
   622
}
slouken@1895
   623
slouken@1895
   624
void
slouken@1895
   625
WIN_DestroyWindow(_THIS, SDL_Window * window)
slouken@1895
   626
{
slouken@1895
   627
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1895
   628
slouken@1895
   629
    if (data) {
slouken@4569
   630
#ifdef _WIN32_WCE
slouken@4569
   631
	WINCE_ShowWindow(_this, window, 0);
slouken@4569
   632
#endif
slouken@1913
   633
        ReleaseDC(data->hwnd, data->hdc);
slouken@1895
   634
        if (data->created) {
slouken@1895
   635
            DestroyWindow(data->hwnd);
slouken@1895
   636
        }
slouken@1895
   637
        SDL_free(data);
slouken@1895
   638
    }
slouken@1895
   639
}
slouken@1895
   640
slouken@1895
   641
SDL_bool
slouken@1895
   642
WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1895
   643
{
slouken@1895
   644
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   645
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
   646
        info->subsystem = SDL_SYSWM_WINDOWS;
slouken@5056
   647
        info->info.win.window = hwnd;
slouken@1895
   648
        return SDL_TRUE;
slouken@1895
   649
    } else {
slouken@1895
   650
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1895
   651
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1895
   652
        return SDL_FALSE;
slouken@1895
   653
    }
slouken@1895
   654
}
slouken@1895
   655
slouken@2713
   656
slouken@2713
   657
/*
slouken@2713
   658
 * Creates a HelperWindow used for DirectInput events.
slouken@2713
   659
 */
slouken@2713
   660
int
slouken@2713
   661
SDL_HelperWindowCreate(void)
slouken@2713
   662
{
slouken@3097
   663
    HINSTANCE hInstance = GetModuleHandle(NULL);
slouken@3097
   664
    WNDCLASS wce;
slouken@3196
   665
    HWND hWndParent = NULL;
slouken@2713
   666
bobbens@3045
   667
    /* Make sure window isn't created twice. */
bobbens@3045
   668
    if (SDL_HelperWindow != NULL) {
bobbens@3045
   669
        return 0;
bobbens@3045
   670
    }
bobbens@3045
   671
slouken@2713
   672
    /* Create the class. */
slouken@2714
   673
    SDL_zero(wce);
slouken@3097
   674
    wce.lpfnWndProc = DefWindowProc;
slouken@2713
   675
    wce.lpszClassName = (LPCWSTR) SDL_HelperWindowClassName;
slouken@2713
   676
    wce.hInstance = hInstance;
slouken@2713
   677
slouken@2713
   678
    /* Register the class. */
slouken@3097
   679
    SDL_HelperWindowClass = RegisterClass(&wce);
slouken@2713
   680
    if (SDL_HelperWindowClass == 0) {
slouken@4500
   681
        WIN_SetError("Unable to create Helper Window Class");
slouken@2713
   682
        return -1;
slouken@2713
   683
    }
slouken@2713
   684
slouken@3168
   685
#ifndef _WIN32_WCE
slouken@3168
   686
    /* WinCE doesn't have HWND_MESSAGE */
slouken@3168
   687
    hWndParent = HWND_MESSAGE;
slouken@3168
   688
#endif
slouken@3168
   689
slouken@2713
   690
    /* Create the window. */
slouken@2714
   691
    SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,
slouken@2714
   692
                                      SDL_HelperWindowName,
slouken@3168
   693
                                      WS_OVERLAPPED, CW_USEDEFAULT,
slouken@2714
   694
                                      CW_USEDEFAULT, CW_USEDEFAULT,
slouken@3168
   695
                                      CW_USEDEFAULT, hWndParent, NULL,
slouken@2714
   696
                                      hInstance, NULL);
slouken@2713
   697
    if (SDL_HelperWindow == NULL) {
bobbens@3045
   698
        UnregisterClass(SDL_HelperWindowClassName, hInstance);
slouken@4500
   699
        WIN_SetError("Unable to create Helper Window");
slouken@2713
   700
        return -1;
slouken@2713
   701
    }
slouken@2713
   702
slouken@2713
   703
    return 0;
slouken@2713
   704
}
slouken@2713
   705
slouken@2713
   706
slouken@2713
   707
/*
slouken@2713
   708
 * Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
slouken@2713
   709
 */
slouken@2713
   710
void
slouken@2713
   711
SDL_HelperWindowDestroy(void)
slouken@2713
   712
{
slouken@3097
   713
    HINSTANCE hInstance = GetModuleHandle(NULL);
bobbens@2863
   714
slouken@2713
   715
    /* Destroy the window. */
bobbens@2863
   716
    if (SDL_HelperWindow != NULL) {
bobbens@2863
   717
        if (DestroyWindow(SDL_HelperWindow) == 0) {
slouken@4500
   718
            WIN_SetError("Unable to destroy Helper Window");
slouken@2865
   719
            return;
bobbens@2863
   720
        }
slouken@2713
   721
        SDL_HelperWindow = NULL;
slouken@2713
   722
    }
slouken@2713
   723
slouken@2713
   724
    /* Unregister the class. */
bobbens@2863
   725
    if (SDL_HelperWindowClass != 0) {
bobbens@2863
   726
        if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
slouken@4500
   727
            WIN_SetError("Unable to destroy Helper Window Class");
slouken@2865
   728
            return;
bobbens@2863
   729
        }
slouken@2713
   730
        SDL_HelperWindowClass = 0;
slouken@2713
   731
    }
slouken@2713
   732
}
slouken@2713
   733
slouken@1895
   734
/* vi: set ts=4 sw=4 expandtab: */