src/video/win32/SDL_win32window.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 06 Jul 2010 08:22:36 -0700
changeset 4486 237b1eb53e4d
parent 4465 3e69e077cb95
child 4500 eff4e88cc1e8
permissions -rw-r--r--
Fixed fullscreen window position
Fixed position calculation for centered windows
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 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@2710
    22
slouken@2710
    23
/* we need to define it, so that raw input is included */
slouken@2710
    24
slouken@2710
    25
#if (_WIN32_WINNT < 0x0501)
slouken@2710
    26
#undef _WIN32_WINNT
slouken@2710
    27
#define _WIN32_WINNT 0x0501
slouken@2710
    28
#endif
slouken@2710
    29
slouken@1895
    30
#include "SDL_config.h"
slouken@1895
    31
slouken@1895
    32
#include "../SDL_sysvideo.h"
slouken@2970
    33
#include "../SDL_pixels_c.h"
slouken@1895
    34
#include "../../events/SDL_keyboard_c.h"
slouken@1895
    35
slouken@1895
    36
#include "SDL_win32video.h"
slouken@1895
    37
slouken@1895
    38
/* This is included after SDL_win32video.h, which includes windows.h */
slouken@1895
    39
#include "SDL_syswm.h"
slouken@1895
    40
slouken@3168
    41
slouken@3168
    42
#define SHFS_SHOWTASKBAR            0x0001
slouken@3168
    43
#define SHFS_HIDETASKBAR            0x0002
slouken@3168
    44
#define SHFS_SHOWSIPBUTTON          0x0004
slouken@3168
    45
#define SHFS_HIDESIPBUTTON          0x0008
slouken@3168
    46
#define SHFS_SHOWSTARTICON          0x0010
slouken@3168
    47
#define SHFS_HIDESTARTICON          0x0020
slouken@3168
    48
slouken@3168
    49
#ifdef _WIN32_WCE
slouken@3168
    50
// dynamically load aygshell dll because we want SDL to work on HPC and be300
slouken@3168
    51
int aygshell_loaded = 0;
slouken@3168
    52
BOOL(WINAPI * SHFullScreen) (HWND hwndRequester, DWORD dwState) = 0;
slouken@3168
    53
slouken@3168
    54
slouken@3168
    55
static BOOL
slouken@3168
    56
CE_SHFullScreen(HWND hwndRequester, DWORD dwState)
slouken@3168
    57
{
slouken@3168
    58
    if (SHFullScreen == 0 && aygshell_loaded == 0) {
slouken@3168
    59
        aygshell_loaded = 0;
slouken@3168
    60
        void *lib = SDL_LoadObject("aygshell.dll");
slouken@3168
    61
        if (lib) {
slouken@3168
    62
            SHFullScreen =
slouken@3168
    63
                (BOOL(WINAPI *) (HWND, DWORD)) SDL_LoadFunction(lib,
slouken@3168
    64
                                                                "SHFullScreen");
slouken@3168
    65
        }
slouken@3168
    66
    }
slouken@3168
    67
slouken@3168
    68
    if (SHFullScreen) {
slouken@3168
    69
        SHFullScreen(hwndRequester, dwState);
slouken@3168
    70
        //printf("SHFullscreen(%i)\n",dwState);
slouken@3168
    71
    }
slouken@3168
    72
}
slouken@3168
    73
slouken@3168
    74
#endif
slouken@3168
    75
slouken@2713
    76
/* Fake window to help with DirectInput events. */
slouken@2713
    77
HWND SDL_HelperWindow = NULL;
slouken@2714
    78
static WCHAR *SDL_HelperWindowClassName = TEXT("SDLHelperWindowInputCatcher");
slouken@2714
    79
static WCHAR *SDL_HelperWindowName = TEXT("SDLHelperWindowInputMsgWindow");
slouken@2713
    80
static ATOM SDL_HelperWindowClass = 0;
slouken@2713
    81
slouken@1895
    82
static int
slouken@1951
    83
SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created)
slouken@1895
    84
{
slouken@1951
    85
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@3685
    86
    SDL_VideoDisplay *display = window->display;
slouken@1895
    87
    SDL_WindowData *data;
slouken@1895
    88
slouken@1895
    89
    /* Allocate the window data */
slouken@1895
    90
    data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
slouken@1895
    91
    if (!data) {
slouken@1895
    92
        SDL_OutOfMemory();
slouken@1895
    93
        return -1;
slouken@1895
    94
    }
slouken@3685
    95
    data->window = window;
slouken@1895
    96
    data->hwnd = hwnd;
slouken@1913
    97
    data->hdc = GetDC(hwnd);
slouken@1895
    98
    data->created = created;
slouken@1895
    99
    data->mouse_pressed = SDL_FALSE;
slouken@1951
   100
    data->videodata = videodata;
slouken@1895
   101
slouken@1895
   102
    /* Associate the data with the window */
slouken@1895
   103
    if (!SetProp(hwnd, TEXT("SDL_WindowData"), data)) {
slouken@1913
   104
        ReleaseDC(hwnd, data->hdc);
slouken@1895
   105
        SDL_free(data);
slouken@1895
   106
        WIN_SetError("SetProp() failed");
slouken@1895
   107
        return -1;
slouken@1895
   108
    }
slouken@1895
   109
slouken@1895
   110
    /* Set up the window proc function */
slouken@1895
   111
    data->wndproc = (WNDPROC) GetWindowLongPtr(hwnd, GWLP_WNDPROC);
slouken@3566
   112
    if (data->wndproc == DefWindowProc) {
slouken@3566
   113
        data->wndproc = NULL;
slouken@1895
   114
    }
slouken@3566
   115
    SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) WIN_WindowProc);
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@3530
   123
            SDL_Rect bounds;
slouken@3530
   124
            WIN_GetDisplayBounds(_this, display, &bounds);
slouken@3530
   125
            window->x = point.x - bounds.x;
slouken@3530
   126
            window->y = point.y - bounds.y;
slouken@1895
   127
        }
slouken@1895
   128
    }
slouken@1895
   129
    {
slouken@1895
   130
        RECT rect;
slouken@1895
   131
        if (GetClientRect(hwnd, &rect)) {
slouken@1895
   132
            window->w = rect.right;
slouken@1895
   133
            window->h = rect.bottom;
slouken@1895
   134
        }
slouken@1895
   135
    }
slouken@1895
   136
    {
slouken@1895
   137
        DWORD style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   138
        if (style & WS_VISIBLE) {
slouken@1895
   139
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1895
   140
        } else {
slouken@1895
   141
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1895
   142
        }
slouken@1895
   143
        if (style & (WS_BORDER | WS_THICKFRAME)) {
slouken@1895
   144
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1895
   145
        } else {
slouken@1895
   146
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1895
   147
        }
slouken@1895
   148
        if (style & WS_THICKFRAME) {
slouken@1895
   149
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1895
   150
        } else {
slouken@1895
   151
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1895
   152
        }
slouken@1895
   153
        if (style & WS_MAXIMIZE) {
slouken@1895
   154
            window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1895
   155
        } else {
slouken@1895
   156
            window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1895
   157
        }
slouken@1895
   158
        if (style & WS_MINIMIZE) {
slouken@1895
   159
            window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1895
   160
        } else {
slouken@1895
   161
            window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1895
   162
        }
slouken@1895
   163
    }
slouken@1895
   164
    if (GetFocus() == hwnd) {
slouken@1895
   165
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@4465
   166
        SDL_SetKeyboardFocus(data->window);
slouken@1895
   167
slouken@1895
   168
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1895
   169
            RECT rect;
slouken@1895
   170
            GetClientRect(hwnd, &rect);
slouken@1895
   171
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   172
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   173
            ClipCursor(&rect);
slouken@1895
   174
        }
slouken@1895
   175
    }
slouken@1895
   176
slouken@1895
   177
    /* All done! */
slouken@1895
   178
    window->driverdata = data;
slouken@1895
   179
    return 0;
slouken@1895
   180
}
slouken@1895
   181
slouken@1895
   182
int
slouken@1895
   183
WIN_CreateWindow(_THIS, SDL_Window * window)
slouken@1895
   184
{
slouken@3685
   185
    SDL_VideoDisplay *display = window->display;
slouken@1895
   186
    HWND hwnd;
slouken@1895
   187
    RECT rect;
slouken@3528
   188
    SDL_Rect bounds;
slouken@1913
   189
    DWORD style = (WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
slouken@1895
   190
    int x, y;
slouken@1895
   191
    int w, h;
slouken@1895
   192
slouken@2876
   193
    if (window->flags & (SDL_WINDOW_BORDERLESS | SDL_WINDOW_FULLSCREEN)) {
slouken@1895
   194
        style |= WS_POPUP;
slouken@1895
   195
    } else {
slouken@1895
   196
        style |= (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX);
slouken@1895
   197
    }
slouken@2876
   198
    if ((window->flags & SDL_WINDOW_RESIZABLE)
slouken@2876
   199
        && !(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@1895
   200
        style |= (WS_THICKFRAME | WS_MAXIMIZEBOX);
slouken@1895
   201
    }
slouken@1895
   202
slouken@1895
   203
    /* Figure out what the window area will be */
slouken@1895
   204
    rect.left = 0;
slouken@1895
   205
    rect.top = 0;
slouken@1895
   206
    rect.right = window->w;
slouken@1895
   207
    rect.bottom = window->h;
slouken@1895
   208
    AdjustWindowRectEx(&rect, style, FALSE, 0);
slouken@1895
   209
    w = (rect.right - rect.left);
slouken@1895
   210
    h = (rect.bottom - rect.top);
slouken@1895
   211
slouken@3528
   212
    WIN_GetDisplayBounds(_this, display, &bounds);
slouken@4486
   213
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@4486
   214
        /* The bounds when this window is visible is the fullscreen mode */
slouken@4486
   215
        SDL_DisplayMode fullscreen_mode;
slouken@4486
   216
        if (SDL_GetWindowDisplayMode(window, &fullscreen_mode) == 0) {
slouken@4486
   217
            bounds.w = fullscreen_mode.w;
slouken@4486
   218
            bounds.h = fullscreen_mode.h;
slouken@4486
   219
        }
slouken@4486
   220
    }
slouken@2876
   221
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@2876
   222
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@4486
   223
        x = bounds.x + (bounds.w - w) / 2;
slouken@1895
   224
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@3528
   225
        if (bounds.x == 0) {
slouken@3528
   226
            x = CW_USEDEFAULT;
slouken@3528
   227
        } else {
slouken@3528
   228
            x = bounds.x;
slouken@3528
   229
        }
slouken@1895
   230
    } else {
slouken@3530
   231
        x = bounds.x + window->x + rect.left;
slouken@1895
   232
    }
slouken@2876
   233
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@2876
   234
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@4486
   235
        y = bounds.y + (bounds.h - h) / 2;
slouken@3528
   236
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@3528
   237
        if (bounds.x == 0) {
slouken@3528
   238
            y = CW_USEDEFAULT;
slouken@3528
   239
        } else {
slouken@3528
   240
            y = bounds.y;
slouken@3528
   241
        }
slouken@1895
   242
    } else {
slouken@3530
   243
        y = bounds.y + window->y + rect.top;
slouken@1895
   244
    }
slouken@1895
   245
slouken@1956
   246
    hwnd =
slouken@1956
   247
        CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, NULL, NULL,
slouken@1956
   248
                     SDL_Instance, NULL);
slouken@1895
   249
    if (!hwnd) {
slouken@1895
   250
        WIN_SetError("Couldn't create window");
slouken@1895
   251
        return -1;
slouken@1895
   252
    }
slouken@1895
   253
slouken@2710
   254
    WIN_PumpEvents(_this);
slouken@2710
   255
slouken@1951
   256
    if (SetupWindowData(_this, window, hwnd, SDL_TRUE) < 0) {
slouken@1895
   257
        DestroyWindow(hwnd);
slouken@1895
   258
        return -1;
slouken@1895
   259
    }
slouken@1952
   260
#ifdef SDL_VIDEO_OPENGL_WGL
slouken@1913
   261
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1913
   262
        if (WIN_GL_SetupWindow(_this, window) < 0) {
slouken@1913
   263
            WIN_DestroyWindow(_this, window);
slouken@1913
   264
            return -1;
slouken@1913
   265
        }
slouken@1913
   266
    }
slouken@1913
   267
#endif
slouken@1895
   268
    return 0;
slouken@1895
   269
}
slouken@1895
   270
slouken@1895
   271
int
slouken@1895
   272
WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1895
   273
{
slouken@1895
   274
    HWND hwnd = (HWND) data;
slouken@1895
   275
    LPTSTR title;
slouken@1895
   276
    int titleLen;
slouken@1895
   277
slouken@1895
   278
    /* Query the title from the existing window */
slouken@1895
   279
    titleLen = GetWindowTextLength(hwnd);
slouken@1895
   280
    title = SDL_stack_alloc(TCHAR, titleLen + 1);
slouken@1895
   281
    if (title) {
slouken@1895
   282
        titleLen = GetWindowText(hwnd, title, titleLen);
slouken@1895
   283
    } else {
slouken@1895
   284
        titleLen = 0;
slouken@1895
   285
    }
slouken@1895
   286
    if (titleLen > 0) {
slouken@1895
   287
        window->title = WIN_StringToUTF8(title);
slouken@1895
   288
    }
slouken@1895
   289
    if (title) {
slouken@1895
   290
        SDL_stack_free(title);
slouken@1895
   291
    }
slouken@1895
   292
slouken@1951
   293
    if (SetupWindowData(_this, window, hwnd, SDL_FALSE) < 0) {
slouken@1895
   294
        return -1;
slouken@1895
   295
    }
slouken@1895
   296
    return 0;
slouken@1895
   297
}
slouken@1895
   298
slouken@1895
   299
void
slouken@1895
   300
WIN_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1895
   301
{
slouken@1895
   302
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   303
    LPTSTR title;
slouken@1895
   304
slouken@1895
   305
    if (window->title) {
slouken@1895
   306
        title = WIN_UTF8ToString(window->title);
slouken@1895
   307
    } else {
slouken@1895
   308
        title = NULL;
slouken@1895
   309
    }
slouken@1895
   310
    SetWindowText(hwnd, title ? title : TEXT(""));
slouken@1895
   311
    if (title) {
slouken@1895
   312
        SDL_free(title);
slouken@1895
   313
    }
slouken@1895
   314
}
slouken@1895
   315
slouken@1895
   316
void
slouken@2970
   317
WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2970
   318
{
slouken@2970
   319
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@2970
   320
    HICON hicon = NULL;
slouken@2970
   321
slouken@2990
   322
    if (icon) {
slouken@2970
   323
        BYTE *icon_bmp;
slouken@2970
   324
        int icon_len;
slouken@2970
   325
        SDL_RWops *dst;
slouken@2970
   326
        SDL_PixelFormat format;
slouken@2970
   327
        SDL_Surface *surface;
slouken@2970
   328
slouken@2970
   329
        /* Create temporary bitmap buffer */
slouken@2970
   330
        icon_len = 40 + icon->h * icon->w * 4;
slouken@2970
   331
        icon_bmp = SDL_stack_alloc(BYTE, icon_len);
slouken@2970
   332
        dst = SDL_RWFromMem(icon_bmp, icon_len);
slouken@2970
   333
        if (!dst) {
slouken@2970
   334
            SDL_stack_free(icon_bmp);
slouken@2970
   335
            return;
slouken@2970
   336
        }
slouken@2970
   337
slouken@2970
   338
        /* Write the BITMAPINFO header */
slouken@2970
   339
        SDL_WriteLE32(dst, 40);
slouken@2970
   340
        SDL_WriteLE32(dst, icon->w);
slouken@2990
   341
        SDL_WriteLE32(dst, icon->h * 2);
slouken@2970
   342
        SDL_WriteLE16(dst, 1);
slouken@2970
   343
        SDL_WriteLE16(dst, 32);
slouken@2970
   344
        SDL_WriteLE32(dst, BI_RGB);
slouken@2970
   345
        SDL_WriteLE32(dst, icon->h * icon->w * 4);
slouken@2970
   346
        SDL_WriteLE32(dst, 0);
slouken@2970
   347
        SDL_WriteLE32(dst, 0);
slouken@2970
   348
        SDL_WriteLE32(dst, 0);
slouken@2970
   349
        SDL_WriteLE32(dst, 0);
slouken@2970
   350
slouken@2970
   351
        /* Convert the icon to a 32-bit surface with alpha channel */
slouken@2970
   352
        SDL_InitFormat(&format, 32,
slouken@2970
   353
                       0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
slouken@2970
   354
        surface = SDL_ConvertSurface(icon, &format, 0);
slouken@2970
   355
        if (surface) {
slouken@2970
   356
            /* Write the pixels upside down into the bitmap buffer */
slouken@2970
   357
            int y = surface->h;
slouken@2970
   358
            while (y--) {
slouken@2990
   359
                Uint8 *src = (Uint8 *) surface->pixels + y * surface->pitch;
slouken@2970
   360
                SDL_RWwrite(dst, src, surface->pitch, 1);
slouken@2970
   361
            }
slouken@2970
   362
            SDL_FreeSurface(surface);
slouken@2970
   363
slouken@3097
   364
/* TODO: create the icon in WinCE (CreateIconFromResource isn't available) */
slouken@3097
   365
#ifndef _WIN32_WCE
slouken@2990
   366
            hicon =
slouken@2990
   367
                CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
slouken@3097
   368
#endif
slouken@2970
   369
        }
slouken@2970
   370
        SDL_RWclose(dst);
slouken@2970
   371
        SDL_stack_free(icon_bmp);
slouken@2970
   372
    }
slouken@2970
   373
slouken@2971
   374
    /* Set the icon for the window */
slouken@2990
   375
    SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);
slouken@2971
   376
slouken@2971
   377
    /* Set the icon in the task manager (should we do this?) */
slouken@2990
   378
    SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) hicon);
slouken@2970
   379
}
slouken@2970
   380
slouken@2970
   381
void
slouken@1895
   382
WIN_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1895
   383
{
slouken@3685
   384
    SDL_VideoDisplay *display = window->display;
slouken@1895
   385
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   386
    RECT rect;
slouken@3528
   387
    SDL_Rect bounds;
slouken@1895
   388
    DWORD style;
slouken@1895
   389
    HWND top;
slouken@3168
   390
    BOOL menu;
slouken@1895
   391
    int x, y;
slouken@4486
   392
    int w, h;
slouken@1895
   393
slouken@1895
   394
    /* Figure out what the window area will be */
slouken@1895
   395
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   396
        top = HWND_TOPMOST;
slouken@1895
   397
    } else {
slouken@1895
   398
        top = HWND_NOTOPMOST;
slouken@1895
   399
    }
slouken@1895
   400
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   401
    rect.left = 0;
slouken@1895
   402
    rect.top = 0;
slouken@1895
   403
    rect.right = window->w;
slouken@1895
   404
    rect.bottom = window->h;
slouken@3168
   405
#ifdef _WIN32_WCE
slouken@3168
   406
    menu = FALSE;
slouken@3168
   407
#else
slouken@3168
   408
    menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   409
#endif
slouken@3168
   410
    AdjustWindowRectEx(&rect, style, menu, 0);
slouken@4486
   411
    w = (rect.right - rect.left);
slouken@4486
   412
    h = (rect.bottom - rect.top);
slouken@2875
   413
slouken@3528
   414
    WIN_GetDisplayBounds(_this, display, &bounds);
slouken@4486
   415
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@4486
   416
        /* The bounds when this window is visible is the fullscreen mode */
slouken@4486
   417
        SDL_DisplayMode fullscreen_mode;
slouken@4486
   418
        if (SDL_GetWindowDisplayMode(window, &fullscreen_mode) == 0) {
slouken@4486
   419
            bounds.w = fullscreen_mode.w;
slouken@4486
   420
            bounds.h = fullscreen_mode.h;
slouken@4486
   421
        }
slouken@4486
   422
    }
slouken@2876
   423
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@2876
   424
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@4486
   425
        x = bounds.x + (bounds.w - w) / 2;
slouken@2875
   426
    } else {
slouken@3530
   427
        x = bounds.x + window->x + rect.left;
slouken@2875
   428
    }
slouken@2876
   429
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@2876
   430
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@4486
   431
        y = bounds.y + (bounds.h - h) / 2;
slouken@2875
   432
    } else {
slouken@3530
   433
        y = bounds.y + window->y + rect.top;
slouken@2875
   434
    }
slouken@1895
   435
slouken@1956
   436
    SetWindowPos(hwnd, top, x, y, 0, 0, (SWP_NOCOPYBITS | SWP_NOSIZE));
slouken@1895
   437
}
slouken@1895
   438
slouken@1895
   439
void
slouken@1895
   440
WIN_SetWindowSize(_THIS, SDL_Window * window)
slouken@1895
   441
{
slouken@1895
   442
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   443
    RECT rect;
slouken@1895
   444
    DWORD style;
slouken@1895
   445
    HWND top;
slouken@3168
   446
    BOOL menu;
slouken@1895
   447
    int w, h;
slouken@1895
   448
slouken@1895
   449
    /* Figure out what the window area will be */
slouken@1895
   450
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   451
        top = HWND_TOPMOST;
slouken@1895
   452
    } else {
slouken@1895
   453
        top = HWND_NOTOPMOST;
slouken@1895
   454
    }
slouken@1895
   455
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   456
    rect.left = 0;
slouken@1895
   457
    rect.top = 0;
slouken@1895
   458
    rect.right = window->w;
slouken@1895
   459
    rect.bottom = window->h;
slouken@3168
   460
#ifdef _WIN32_WCE
slouken@3168
   461
    menu = FALSE;
slouken@3168
   462
#else
slouken@3168
   463
    menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   464
#endif
slouken@3168
   465
    AdjustWindowRectEx(&rect, style, menu, 0);
slouken@1895
   466
    w = (rect.right - rect.left);
slouken@1895
   467
    h = (rect.bottom - rect.top);
slouken@1895
   468
slouken@2968
   469
    SetWindowPos(hwnd, top, 0, 0, w, h, (SWP_NOCOPYBITS | SWP_NOMOVE));
slouken@1895
   470
}
slouken@1895
   471
slouken@1895
   472
void
slouken@1895
   473
WIN_ShowWindow(_THIS, SDL_Window * window)
slouken@1895
   474
{
slouken@1895
   475
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   476
slouken@1895
   477
    ShowWindow(hwnd, SW_SHOW);
slouken@3168
   478
slouken@3168
   479
#ifdef _WIN32_WCE
slouken@3168
   480
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@3168
   481
        CE_SHFullScreen(hwnd,
slouken@3168
   482
                        SHFS_HIDESTARTICON | SHFS_HIDETASKBAR |
slouken@3168
   483
                        SHFS_HIDESIPBUTTON);
slouken@3168
   484
    }
slouken@3168
   485
#endif
slouken@1895
   486
}
slouken@1895
   487
slouken@1895
   488
void
slouken@1895
   489
WIN_HideWindow(_THIS, SDL_Window * window)
slouken@1895
   490
{
slouken@1895
   491
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   492
slouken@1895
   493
    ShowWindow(hwnd, SW_HIDE);
slouken@3168
   494
slouken@3168
   495
#ifdef _WIN32_WCE
slouken@3168
   496
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@3168
   497
        CE_SHFullScreen(hwnd,
slouken@3168
   498
                        SHFS_SHOWSTARTICON | SHFS_SHOWTASKBAR |
slouken@3168
   499
                        SHFS_SHOWSIPBUTTON);
slouken@3168
   500
    }
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@3168
   516
slouken@3168
   517
#ifdef _WIN32_WCE
slouken@3168
   518
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@3168
   519
        CE_SHFullScreen(hwnd,
slouken@3168
   520
                        SHFS_HIDESTARTICON | SHFS_HIDETASKBAR |
slouken@3168
   521
                        SHFS_HIDESIPBUTTON);
slouken@3168
   522
    }
slouken@3168
   523
#endif
slouken@1895
   524
}
slouken@1895
   525
slouken@1895
   526
void
slouken@1895
   527
WIN_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1895
   528
{
slouken@1895
   529
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   530
slouken@1895
   531
    ShowWindow(hwnd, SW_MAXIMIZE);
slouken@3168
   532
slouken@3168
   533
#ifdef _WIN32_WCE
slouken@3168
   534
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@3168
   535
        CE_SHFullScreen(hwnd,
slouken@3168
   536
                        SHFS_HIDESTARTICON | SHFS_HIDETASKBAR |
slouken@3168
   537
                        SHFS_HIDESIPBUTTON);
slouken@3168
   538
    }
slouken@3168
   539
#endif
slouken@1895
   540
}
slouken@1895
   541
slouken@1895
   542
void
slouken@1895
   543
WIN_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1895
   544
{
slouken@1895
   545
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   546
slouken@1895
   547
    ShowWindow(hwnd, SW_MINIMIZE);
slouken@3168
   548
slouken@3168
   549
#ifdef _WIN32_WCE
slouken@3168
   550
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@3168
   551
        CE_SHFullScreen(hwnd,
slouken@3168
   552
                        SHFS_SHOWSTARTICON | SHFS_SHOWTASKBAR |
slouken@3168
   553
                        SHFS_SHOWSIPBUTTON);
slouken@3168
   554
    }
slouken@3168
   555
#endif
slouken@1895
   556
}
slouken@1895
   557
slouken@1895
   558
void
slouken@1895
   559
WIN_RestoreWindow(_THIS, SDL_Window * window)
slouken@1895
   560
{
slouken@1895
   561
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   562
slouken@1895
   563
    ShowWindow(hwnd, SW_RESTORE);
slouken@1895
   564
}
slouken@1895
   565
slouken@1895
   566
void
slouken@1895
   567
WIN_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1895
   568
{
slouken@1895
   569
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   570
slouken@2876
   571
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
slouken@2876
   572
        && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   573
        RECT rect;
slouken@1895
   574
        GetClientRect(hwnd, &rect);
slouken@1895
   575
        ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   576
        ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   577
        ClipCursor(&rect);
slouken@1895
   578
    } else {
slouken@1895
   579
        ClipCursor(NULL);
slouken@1895
   580
    }
slouken@1895
   581
}
slouken@1895
   582
slouken@1895
   583
void
slouken@1895
   584
WIN_DestroyWindow(_THIS, SDL_Window * window)
slouken@1895
   585
{
slouken@1895
   586
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1895
   587
slouken@1895
   588
    if (data) {
slouken@1913
   589
        ReleaseDC(data->hwnd, data->hdc);
slouken@1895
   590
        if (data->created) {
slouken@1895
   591
            DestroyWindow(data->hwnd);
slouken@1895
   592
        }
slouken@1895
   593
        SDL_free(data);
slouken@1895
   594
    }
slouken@1895
   595
}
slouken@1895
   596
slouken@1895
   597
SDL_bool
slouken@1895
   598
WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1895
   599
{
slouken@1895
   600
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   601
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@1895
   602
        info->window = hwnd;
slouken@1895
   603
        return SDL_TRUE;
slouken@1895
   604
    } else {
slouken@1895
   605
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1895
   606
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1895
   607
        return SDL_FALSE;
slouken@1895
   608
    }
slouken@1895
   609
}
slouken@1895
   610
slouken@2713
   611
slouken@2713
   612
/*
slouken@2713
   613
 * Creates a HelperWindow used for DirectInput events.
slouken@2713
   614
 */
slouken@2713
   615
int
slouken@2713
   616
SDL_HelperWindowCreate(void)
slouken@2713
   617
{
slouken@3097
   618
    HINSTANCE hInstance = GetModuleHandle(NULL);
slouken@3097
   619
    WNDCLASS wce;
slouken@3196
   620
    HWND hWndParent = NULL;
slouken@2713
   621
bobbens@3045
   622
    /* Make sure window isn't created twice. */
bobbens@3045
   623
    if (SDL_HelperWindow != NULL) {
bobbens@3045
   624
        return 0;
bobbens@3045
   625
    }
bobbens@3045
   626
slouken@2713
   627
    /* Create the class. */
slouken@2714
   628
    SDL_zero(wce);
slouken@3097
   629
    wce.lpfnWndProc = DefWindowProc;
slouken@2713
   630
    wce.lpszClassName = (LPCWSTR) SDL_HelperWindowClassName;
slouken@2713
   631
    wce.hInstance = hInstance;
slouken@2713
   632
slouken@2713
   633
    /* Register the class. */
slouken@3097
   634
    SDL_HelperWindowClass = RegisterClass(&wce);
slouken@2713
   635
    if (SDL_HelperWindowClass == 0) {
slouken@2713
   636
        SDL_SetError("Unable to create Helper Window Class: error %d.",
slouken@2713
   637
                     GetLastError());
slouken@2713
   638
        return -1;
slouken@2713
   639
    }
slouken@2713
   640
slouken@3168
   641
#ifndef _WIN32_WCE
slouken@3168
   642
    /* WinCE doesn't have HWND_MESSAGE */
slouken@3168
   643
    hWndParent = HWND_MESSAGE;
slouken@3168
   644
#endif
slouken@3168
   645
slouken@2713
   646
    /* Create the window. */
slouken@2714
   647
    SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,
slouken@2714
   648
                                      SDL_HelperWindowName,
slouken@3168
   649
                                      WS_OVERLAPPED, CW_USEDEFAULT,
slouken@2714
   650
                                      CW_USEDEFAULT, CW_USEDEFAULT,
slouken@3168
   651
                                      CW_USEDEFAULT, hWndParent, NULL,
slouken@2714
   652
                                      hInstance, NULL);
slouken@2713
   653
    if (SDL_HelperWindow == NULL) {
bobbens@3045
   654
        UnregisterClass(SDL_HelperWindowClassName, hInstance);
slouken@2713
   655
        SDL_SetError("Unable to create Helper Window: error %d.",
slouken@2713
   656
                     GetLastError());
slouken@2713
   657
        return -1;
slouken@2713
   658
    }
slouken@2713
   659
slouken@2713
   660
    return 0;
slouken@2713
   661
}
slouken@2713
   662
slouken@2713
   663
slouken@2713
   664
/*
slouken@2713
   665
 * Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
slouken@2713
   666
 */
slouken@2713
   667
void
slouken@2713
   668
SDL_HelperWindowDestroy(void)
slouken@2713
   669
{
slouken@3097
   670
    HINSTANCE hInstance = GetModuleHandle(NULL);
bobbens@2863
   671
slouken@2713
   672
    /* Destroy the window. */
bobbens@2863
   673
    if (SDL_HelperWindow != NULL) {
bobbens@2863
   674
        if (DestroyWindow(SDL_HelperWindow) == 0) {
slouken@2865
   675
            SDL_SetError("Unable to destroy Helper Window: error %d.",
slouken@2865
   676
                         GetLastError());
slouken@2865
   677
            return;
bobbens@2863
   678
        }
slouken@2713
   679
        SDL_HelperWindow = NULL;
slouken@2713
   680
    }
slouken@2713
   681
slouken@2713
   682
    /* Unregister the class. */
bobbens@2863
   683
    if (SDL_HelperWindowClass != 0) {
bobbens@2863
   684
        if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
slouken@2865
   685
            SDL_SetError("Unable to destroy Helper Window Class: error %d.",
slouken@2865
   686
                         GetLastError());
slouken@2865
   687
            return;
bobbens@2863
   688
        }
slouken@2713
   689
        SDL_HelperWindowClass = 0;
slouken@2713
   690
    }
slouken@2713
   691
}
slouken@2713
   692
slouken@2713
   693
slouken@1895
   694
/* vi: set ts=4 sw=4 expandtab: */