src/video/win32/SDL_win32window.c
author Edgar Simo <bobbens@gmail.com>
Fri, 12 Dec 2008 15:51:57 +0000
changeset 2863 b632927c28c8
parent 2859 99210400e8b9
child 2865 09adf4854163
permissions -rw-r--r--
Some error checking when destroying the HelperWindow.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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@1895
    33
#include "../../events/SDL_keyboard_c.h"
slouken@1895
    34
slouken@1895
    35
#include "SDL_win32video.h"
slouken@1895
    36
slouken@1895
    37
/* This is included after SDL_win32video.h, which includes windows.h */
slouken@1895
    38
#include "SDL_syswm.h"
slouken@1895
    39
slouken@2710
    40
extern HCTX *g_hCtx;            /* the table of tablet event contexts, each windows has to have it's own tablet context */
slouken@2767
    41
static Uint32 highestId = 0;    /* the highest id of the tablet context */
slouken@1895
    42
slouken@2713
    43
/* Fake window to help with DirectInput events. */
slouken@2713
    44
HWND SDL_HelperWindow = NULL;
slouken@2714
    45
static WCHAR *SDL_HelperWindowClassName = TEXT("SDLHelperWindowInputCatcher");
slouken@2714
    46
static WCHAR *SDL_HelperWindowName = TEXT("SDLHelperWindowInputMsgWindow");
slouken@2713
    47
static ATOM SDL_HelperWindowClass = 0;
slouken@2713
    48
slouken@1895
    49
static int
slouken@1951
    50
SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created)
slouken@1895
    51
{
slouken@1951
    52
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1895
    53
    SDL_WindowData *data;
slouken@1895
    54
slouken@1895
    55
    /* Allocate the window data */
slouken@1895
    56
    data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
slouken@1895
    57
    if (!data) {
slouken@1895
    58
        SDL_OutOfMemory();
slouken@1895
    59
        return -1;
slouken@1895
    60
    }
slouken@1895
    61
    data->windowID = window->id;
slouken@1895
    62
    data->hwnd = hwnd;
slouken@1913
    63
    data->hdc = GetDC(hwnd);
slouken@1895
    64
    data->created = created;
slouken@1895
    65
    data->mouse_pressed = SDL_FALSE;
slouken@1951
    66
    data->videodata = videodata;
slouken@1895
    67
slouken@1895
    68
    /* Associate the data with the window */
slouken@1895
    69
    if (!SetProp(hwnd, TEXT("SDL_WindowData"), data)) {
slouken@1913
    70
        ReleaseDC(hwnd, data->hdc);
slouken@1895
    71
        SDL_free(data);
slouken@1895
    72
        WIN_SetError("SetProp() failed");
slouken@1895
    73
        return -1;
slouken@1895
    74
    }
slouken@1895
    75
slouken@1895
    76
    /* Set up the window proc function */
slouken@1895
    77
    data->wndproc = (WNDPROC) GetWindowLongPtr(hwnd, GWLP_WNDPROC);
slouken@1895
    78
    if (data->wndproc == NULL) {
slouken@1895
    79
        data->wndproc = DefWindowProc;
slouken@1895
    80
    } else {
slouken@1895
    81
        SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) WIN_WindowProc);
slouken@1895
    82
    }
slouken@1895
    83
slouken@1895
    84
    /* Fill in the SDL window with the window data */
slouken@1895
    85
    {
slouken@1895
    86
        POINT point;
slouken@1895
    87
        point.x = 0;
slouken@1895
    88
        point.y = 0;
slouken@1895
    89
        if (ClientToScreen(hwnd, &point)) {
slouken@1895
    90
            window->x = point.x;
slouken@1895
    91
            window->y = point.y;
slouken@1895
    92
        }
slouken@1895
    93
    }
slouken@1895
    94
    {
slouken@1895
    95
        RECT rect;
slouken@1895
    96
        if (GetClientRect(hwnd, &rect)) {
slouken@1895
    97
            window->w = rect.right;
slouken@1895
    98
            window->h = rect.bottom;
slouken@1895
    99
        }
slouken@1895
   100
    }
slouken@1895
   101
    {
slouken@1895
   102
        DWORD style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   103
        if (style & WS_VISIBLE) {
slouken@1895
   104
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1895
   105
        } else {
slouken@1895
   106
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1895
   107
        }
slouken@1895
   108
        if (style & (WS_BORDER | WS_THICKFRAME)) {
slouken@1895
   109
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1895
   110
        } else {
slouken@1895
   111
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1895
   112
        }
slouken@1895
   113
        if (style & WS_THICKFRAME) {
slouken@1895
   114
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1895
   115
        } else {
slouken@1895
   116
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1895
   117
        }
slouken@1895
   118
        if (style & WS_MAXIMIZE) {
slouken@1895
   119
            window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1895
   120
        } else {
slouken@1895
   121
            window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1895
   122
        }
slouken@1895
   123
        if (style & WS_MINIMIZE) {
slouken@1895
   124
            window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1895
   125
        } else {
slouken@1895
   126
            window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1895
   127
        }
slouken@1895
   128
    }
slouken@1895
   129
    if (GetFocus() == hwnd) {
slouken@1895
   130
        int index = data->videodata->keyboard;
slouken@1895
   131
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@1895
   132
        SDL_SetKeyboardFocus(index, data->windowID);
slouken@1895
   133
slouken@1895
   134
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1895
   135
            RECT rect;
slouken@1895
   136
            GetClientRect(hwnd, &rect);
slouken@1895
   137
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   138
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   139
            ClipCursor(&rect);
slouken@1895
   140
        }
slouken@1895
   141
    }
slouken@1895
   142
slouken@1895
   143
    /* All done! */
slouken@1895
   144
    window->driverdata = data;
slouken@1895
   145
    return 0;
slouken@1895
   146
}
slouken@1895
   147
slouken@1895
   148
int
slouken@1895
   149
WIN_CreateWindow(_THIS, SDL_Window * window)
slouken@1895
   150
{
slouken@2726
   151
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@2710
   152
    RAWINPUTDEVICE Rid;
slouken@2710
   153
    AXIS TabX, TabY;
slouken@2728
   154
    LOGCONTEXTA lc;
slouken@1895
   155
    HWND hwnd;
slouken@1895
   156
    HWND top;
slouken@1895
   157
    RECT rect;
slouken@1913
   158
    DWORD style = (WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
slouken@1895
   159
    int x, y;
slouken@1895
   160
    int w, h;
slouken@1895
   161
slouken@1956
   162
    if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@1895
   163
        style |= WS_POPUP;
slouken@1895
   164
    } else {
slouken@1895
   165
        style |= (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX);
slouken@1895
   166
    }
slouken@1895
   167
    if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@1895
   168
        style |= (WS_THICKFRAME | WS_MAXIMIZEBOX);
slouken@1895
   169
    }
slouken@1895
   170
slouken@1895
   171
    /* Figure out what the window area will be */
slouken@1895
   172
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   173
        top = HWND_TOPMOST;
slouken@1895
   174
    } else {
slouken@1895
   175
        top = HWND_NOTOPMOST;
slouken@1895
   176
    }
slouken@1895
   177
    rect.left = 0;
slouken@1895
   178
    rect.top = 0;
slouken@1895
   179
    rect.right = window->w;
slouken@1895
   180
    rect.bottom = window->h;
slouken@1895
   181
    AdjustWindowRectEx(&rect, style, FALSE, 0);
slouken@1895
   182
    w = (rect.right - rect.left);
slouken@1895
   183
    h = (rect.bottom - rect.top);
slouken@1895
   184
slouken@1956
   185
    if (window->x == SDL_WINDOWPOS_CENTERED) {
slouken@1895
   186
        x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
slouken@1895
   187
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@1895
   188
        x = CW_USEDEFAULT;
slouken@1895
   189
    } else {
slouken@1895
   190
        x = window->x + rect.left;
slouken@1895
   191
    }
slouken@1956
   192
    if (window->y == SDL_WINDOWPOS_CENTERED) {
slouken@1895
   193
        y = (GetSystemMetrics(SM_CYSCREEN) - h) / 2;
slouken@1895
   194
    } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
slouken@1895
   195
        y = CW_USEDEFAULT;
slouken@1895
   196
    } else {
slouken@1895
   197
        y = window->y + rect.top;
slouken@1895
   198
    }
slouken@1895
   199
slouken@1956
   200
    hwnd =
slouken@1956
   201
        CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, NULL, NULL,
slouken@1956
   202
                     SDL_Instance, NULL);
slouken@1895
   203
    if (!hwnd) {
slouken@1895
   204
        WIN_SetError("Couldn't create window");
slouken@1895
   205
        return -1;
slouken@1895
   206
    }
slouken@1895
   207
slouken@2710
   208
    /* we're configuring the tablet data. See Wintab reference for more info */
slouken@2728
   209
    if (videodata->wintabDLL
slouken@2728
   210
        && videodata->WTInfoA(WTI_DEFSYSCTX, 0, &lc) != 0) {
slouken@2710
   211
        lc.lcPktData = PACKETDATA;
slouken@2710
   212
        lc.lcPktMode = PACKETMODE;
slouken@2710
   213
        lc.lcOptions |= CXO_MESSAGES;
slouken@2710
   214
        lc.lcOptions |= CXO_SYSTEM;
slouken@2710
   215
        lc.lcMoveMask = PACKETDATA;
slouken@2710
   216
        lc.lcBtnDnMask = lc.lcBtnUpMask = PACKETDATA;
slouken@2728
   217
        videodata->WTInfoA(WTI_DEVICES, DVC_X, &TabX);
slouken@2728
   218
        videodata->WTInfoA(WTI_DEVICES, DVC_Y, &TabY);
slouken@2710
   219
        lc.lcInOrgX = 0;
slouken@2710
   220
        lc.lcInOrgY = 0;
slouken@2710
   221
        lc.lcInExtX = TabX.axMax;
slouken@2710
   222
        lc.lcInExtY = TabY.axMax;
slouken@2710
   223
        lc.lcOutOrgX = 0;
slouken@2710
   224
        lc.lcOutOrgY = 0;
slouken@2710
   225
        lc.lcOutExtX = GetSystemMetrics(SM_CXSCREEN);
slouken@2710
   226
        lc.lcOutExtY = -GetSystemMetrics(SM_CYSCREEN);
slouken@2710
   227
        if (window->id > highestId) {
slouken@2710
   228
            HCTX *tmp_hctx;
slouken@2710
   229
            highestId = window->id;
slouken@2710
   230
            tmp_hctx =
slouken@2710
   231
                (HCTX *) SDL_realloc(g_hCtx, (highestId + 1) * sizeof(HCTX));
slouken@2710
   232
            if (!tmp_hctx) {
slouken@2710
   233
                SDL_OutOfMemory();
slouken@2710
   234
                DestroyWindow(hwnd);
slouken@2710
   235
                return -1;
slouken@2710
   236
            }
slouken@2710
   237
            g_hCtx = tmp_hctx;
slouken@2710
   238
        }
slouken@2728
   239
        g_hCtx[window->id] = videodata->WTOpenA(hwnd, &lc, TRUE);
slouken@2710
   240
    }
slouken@2710
   241
slouken@2710
   242
    /* we're telling the window, we want it to report raw input events from mice */
slouken@2710
   243
    Rid.usUsagePage = 0x01;
slouken@2710
   244
    Rid.usUsage = 0x02;
slouken@2710
   245
    Rid.dwFlags = RIDEV_INPUTSINK;
slouken@2710
   246
    Rid.hwndTarget = hwnd;
slouken@2710
   247
    RegisterRawInputDevices(&Rid, 1, sizeof(Rid));
slouken@2710
   248
slouken@2710
   249
    WIN_PumpEvents(_this);
slouken@2710
   250
slouken@1951
   251
    if (SetupWindowData(_this, window, hwnd, SDL_TRUE) < 0) {
slouken@1895
   252
        DestroyWindow(hwnd);
slouken@1895
   253
        return -1;
slouken@1895
   254
    }
slouken@1952
   255
#ifdef SDL_VIDEO_OPENGL_WGL
slouken@1913
   256
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1913
   257
        if (WIN_GL_SetupWindow(_this, window) < 0) {
slouken@1913
   258
            WIN_DestroyWindow(_this, window);
slouken@1913
   259
            return -1;
slouken@1913
   260
        }
slouken@1913
   261
    }
slouken@1913
   262
#endif
slouken@1895
   263
    return 0;
slouken@1895
   264
}
slouken@1895
   265
slouken@1895
   266
int
slouken@1895
   267
WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1895
   268
{
slouken@1895
   269
    HWND hwnd = (HWND) data;
slouken@1895
   270
    LPTSTR title;
slouken@1895
   271
    int titleLen;
slouken@1895
   272
slouken@1895
   273
    /* Query the title from the existing window */
slouken@1895
   274
    titleLen = GetWindowTextLength(hwnd);
slouken@1895
   275
    title = SDL_stack_alloc(TCHAR, titleLen + 1);
slouken@1895
   276
    if (title) {
slouken@1895
   277
        titleLen = GetWindowText(hwnd, title, titleLen);
slouken@1895
   278
    } else {
slouken@1895
   279
        titleLen = 0;
slouken@1895
   280
    }
slouken@1895
   281
    if (titleLen > 0) {
slouken@1895
   282
        window->title = WIN_StringToUTF8(title);
slouken@1895
   283
    }
slouken@1895
   284
    if (title) {
slouken@1895
   285
        SDL_stack_free(title);
slouken@1895
   286
    }
slouken@1895
   287
slouken@1951
   288
    if (SetupWindowData(_this, window, hwnd, SDL_FALSE) < 0) {
slouken@1895
   289
        return -1;
slouken@1895
   290
    }
slouken@1895
   291
    return 0;
slouken@1895
   292
}
slouken@1895
   293
slouken@1895
   294
void
slouken@1895
   295
WIN_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1895
   296
{
slouken@1895
   297
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   298
    LPTSTR title;
slouken@1895
   299
slouken@1895
   300
    if (window->title) {
slouken@1895
   301
        title = WIN_UTF8ToString(window->title);
slouken@1895
   302
    } else {
slouken@1895
   303
        title = NULL;
slouken@1895
   304
    }
slouken@1895
   305
    SetWindowText(hwnd, title ? title : TEXT(""));
slouken@1895
   306
    if (title) {
slouken@1895
   307
        SDL_free(title);
slouken@1895
   308
    }
slouken@1895
   309
}
slouken@1895
   310
slouken@1895
   311
void
slouken@1895
   312
WIN_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1895
   313
{
slouken@1895
   314
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   315
    RECT rect;
slouken@1895
   316
    DWORD style;
slouken@1895
   317
    HWND top;
slouken@1895
   318
    int x, y;
slouken@1895
   319
slouken@1895
   320
    /* Figure out what the window area will be */
slouken@1895
   321
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   322
        top = HWND_TOPMOST;
slouken@1895
   323
    } else {
slouken@1895
   324
        top = HWND_NOTOPMOST;
slouken@1895
   325
    }
slouken@1895
   326
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   327
    rect.left = 0;
slouken@1895
   328
    rect.top = 0;
slouken@1895
   329
    rect.right = window->w;
slouken@1895
   330
    rect.bottom = window->h;
slouken@1895
   331
    AdjustWindowRectEx(&rect, style,
slouken@1895
   332
                       (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) !=
slouken@1895
   333
                                                           NULL), 0);
slouken@1956
   334
    x = window->x + rect.left;
slouken@1956
   335
    y = window->y + rect.top;
slouken@1895
   336
slouken@1956
   337
    SetWindowPos(hwnd, top, x, y, 0, 0, (SWP_NOCOPYBITS | SWP_NOSIZE));
slouken@1895
   338
}
slouken@1895
   339
slouken@1895
   340
void
slouken@1895
   341
WIN_SetWindowSize(_THIS, SDL_Window * window)
slouken@1895
   342
{
slouken@1895
   343
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   344
    RECT rect;
slouken@1895
   345
    DWORD style;
slouken@1895
   346
    HWND top;
slouken@1895
   347
    int w, h;
slouken@1895
   348
slouken@1895
   349
    /* Figure out what the window area will be */
slouken@1895
   350
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   351
        top = HWND_TOPMOST;
slouken@1895
   352
    } else {
slouken@1895
   353
        top = HWND_NOTOPMOST;
slouken@1895
   354
    }
slouken@1895
   355
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   356
    rect.left = 0;
slouken@1895
   357
    rect.top = 0;
slouken@1895
   358
    rect.right = window->w;
slouken@1895
   359
    rect.bottom = window->h;
slouken@1895
   360
    AdjustWindowRectEx(&rect, style,
slouken@1895
   361
                       (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) !=
slouken@1895
   362
                                                           NULL), 0);
slouken@1895
   363
    w = (rect.right - rect.left);
slouken@1895
   364
    h = (rect.bottom - rect.top);
slouken@1895
   365
slouken@1895
   366
    SetWindowPos(hwnd, top, 0, 0, h, w, (SWP_NOCOPYBITS | SWP_NOMOVE));
slouken@1895
   367
}
slouken@1895
   368
slouken@1895
   369
void
slouken@1895
   370
WIN_ShowWindow(_THIS, SDL_Window * window)
slouken@1895
   371
{
slouken@1895
   372
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   373
slouken@1895
   374
    ShowWindow(hwnd, SW_SHOW);
slouken@1895
   375
}
slouken@1895
   376
slouken@1895
   377
void
slouken@1895
   378
WIN_HideWindow(_THIS, SDL_Window * window)
slouken@1895
   379
{
slouken@1895
   380
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   381
slouken@1895
   382
    ShowWindow(hwnd, SW_HIDE);
slouken@1895
   383
}
slouken@1895
   384
slouken@1895
   385
void
slouken@1895
   386
WIN_RaiseWindow(_THIS, SDL_Window * window)
slouken@1895
   387
{
slouken@1895
   388
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   389
    HWND top;
slouken@1895
   390
slouken@1895
   391
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   392
        top = HWND_TOPMOST;
slouken@1895
   393
    } else {
slouken@1895
   394
        top = HWND_NOTOPMOST;
slouken@1895
   395
    }
slouken@1895
   396
    SetWindowPos(hwnd, top, 0, 0, 0, 0, (SWP_NOMOVE | SWP_NOSIZE));
slouken@1895
   397
}
slouken@1895
   398
slouken@1895
   399
void
slouken@1895
   400
WIN_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1895
   401
{
slouken@1895
   402
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   403
slouken@1895
   404
    ShowWindow(hwnd, SW_MAXIMIZE);
slouken@1895
   405
}
slouken@1895
   406
slouken@1895
   407
void
slouken@1895
   408
WIN_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1895
   409
{
slouken@1895
   410
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   411
slouken@1895
   412
    ShowWindow(hwnd, SW_MINIMIZE);
slouken@1895
   413
}
slouken@1895
   414
slouken@1895
   415
void
slouken@1895
   416
WIN_RestoreWindow(_THIS, SDL_Window * window)
slouken@1895
   417
{
slouken@1895
   418
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   419
slouken@1895
   420
    ShowWindow(hwnd, SW_RESTORE);
slouken@1895
   421
}
slouken@1895
   422
slouken@1895
   423
void
slouken@1895
   424
WIN_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1895
   425
{
slouken@1895
   426
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   427
slouken@1895
   428
    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1895
   429
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   430
        RECT rect;
slouken@1895
   431
        GetClientRect(hwnd, &rect);
slouken@1895
   432
        ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   433
        ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   434
        ClipCursor(&rect);
slouken@1895
   435
    } else {
slouken@1895
   436
        ClipCursor(NULL);
slouken@1895
   437
    }
slouken@1895
   438
}
slouken@1895
   439
slouken@1895
   440
void
slouken@1895
   441
WIN_DestroyWindow(_THIS, SDL_Window * window)
slouken@1895
   442
{
slouken@2726
   443
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1895
   444
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1895
   445
slouken@1895
   446
    if (data) {
slouken@1952
   447
#ifdef SDL_VIDEO_OPENGL_WGL
slouken@1913
   448
        if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1913
   449
            WIN_GL_CleanupWindow(_this, window);
slouken@1913
   450
        }
slouken@1913
   451
#endif
slouken@1913
   452
        ReleaseDC(data->hwnd, data->hdc);
slouken@1895
   453
        if (data->created) {
slouken@2726
   454
            if (videodata->wintabDLL) {
slouken@2726
   455
                videodata->WTClose(g_hCtx[window->id]);
slouken@2726
   456
            }
slouken@1895
   457
            DestroyWindow(data->hwnd);
slouken@1895
   458
        }
slouken@1895
   459
        SDL_free(data);
slouken@1895
   460
    }
slouken@1895
   461
}
slouken@1895
   462
slouken@1895
   463
SDL_bool
slouken@1895
   464
WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1895
   465
{
slouken@1895
   466
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   467
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@1895
   468
        info->window = hwnd;
slouken@1895
   469
        return SDL_TRUE;
slouken@1895
   470
    } else {
slouken@1895
   471
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1895
   472
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1895
   473
        return SDL_FALSE;
slouken@1895
   474
    }
slouken@1895
   475
}
slouken@1895
   476
slouken@2713
   477
slouken@2713
   478
/*
slouken@2713
   479
 * Creates a HelperWindow used for DirectInput events.
slouken@2713
   480
 */
slouken@2713
   481
int
slouken@2713
   482
SDL_HelperWindowCreate(void)
slouken@2713
   483
{
slouken@2713
   484
    HINSTANCE hInstance = GetModuleHandleA(NULL);
slouken@2713
   485
    WNDCLASSEX wce;
slouken@2713
   486
slouken@2713
   487
    /* Create the class. */
slouken@2714
   488
    SDL_zero(wce);
slouken@2713
   489
    wce.cbSize = sizeof(WNDCLASSEX);
slouken@2713
   490
    wce.lpfnWndProc = DefWindowProcA;
slouken@2713
   491
    wce.lpszClassName = (LPCWSTR) SDL_HelperWindowClassName;
slouken@2713
   492
    wce.hInstance = hInstance;
slouken@2713
   493
slouken@2713
   494
    /* Register the class. */
slouken@2714
   495
    SDL_HelperWindowClass = RegisterClassEx(&wce);
slouken@2713
   496
    if (SDL_HelperWindowClass == 0) {
slouken@2713
   497
        SDL_SetError("Unable to create Helper Window Class: error %d.",
slouken@2713
   498
                     GetLastError());
slouken@2713
   499
        return -1;
slouken@2713
   500
    }
slouken@2713
   501
slouken@2713
   502
    /* Create the window. */
slouken@2714
   503
    SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,
slouken@2714
   504
                                      SDL_HelperWindowName,
slouken@2714
   505
                                      WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
slouken@2714
   506
                                      CW_USEDEFAULT, CW_USEDEFAULT,
slouken@2714
   507
                                      CW_USEDEFAULT, HWND_MESSAGE, NULL,
slouken@2714
   508
                                      hInstance, NULL);
slouken@2713
   509
    if (SDL_HelperWindow == NULL) {
slouken@2713
   510
        SDL_SetError("Unable to create Helper Window: error %d.",
slouken@2713
   511
                     GetLastError());
slouken@2713
   512
        return -1;
slouken@2713
   513
    }
slouken@2713
   514
slouken@2713
   515
    return 0;
slouken@2713
   516
}
slouken@2713
   517
slouken@2713
   518
slouken@2713
   519
/*
slouken@2713
   520
 * Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
slouken@2713
   521
 */
slouken@2713
   522
void
slouken@2713
   523
SDL_HelperWindowDestroy(void)
slouken@2713
   524
{
bobbens@2863
   525
    HINSTANCE hInstance = GetModuleHandleA(NULL);
bobbens@2863
   526
slouken@2713
   527
    /* Destroy the window. */
bobbens@2863
   528
    if (SDL_HelperWindow != NULL) {
bobbens@2863
   529
        if (DestroyWindow(SDL_HelperWindow) == 0) {
bobbens@2863
   530
           SDL_SetError("Unable to destroy Helper Window: error %d.",
bobbens@2863
   531
                        GetLastError());
bobbens@2863
   532
           return;
bobbens@2863
   533
        }
slouken@2713
   534
        SDL_HelperWindow = NULL;
slouken@2713
   535
    }
slouken@2713
   536
slouken@2713
   537
    /* Unregister the class. */
bobbens@2863
   538
    if (SDL_HelperWindowClass != 0) {
bobbens@2863
   539
        if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
bobbens@2863
   540
           SDL_SetError("Unable to destroy Helper Window Class: error %d.",
bobbens@2863
   541
                        GetLastError());
bobbens@2863
   542
           return;
bobbens@2863
   543
        }
slouken@2713
   544
        SDL_HelperWindowClass = 0;
slouken@2713
   545
    }
slouken@2713
   546
}
slouken@2713
   547
slouken@2713
   548
slouken@1895
   549
/* vi: set ts=4 sw=4 expandtab: */