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