src/video/win32/SDL_win32window.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 09 Feb 2009 05:32:12 +0000
changeset 3057 089a77aebb7d
parent 3045 9acb9f0f7f0d
child 3097 0d12e8f1de3c
permissions -rw-r--r--
Added test program for SDL_CreateWindowFrom()
Make sure OpenGL library is loaded before working with OpenGL windows,
even those created with SDL_CreateWindowFrom()
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@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@2710
    41
extern HCTX *g_hCtx;            /* the table of tablet event contexts, each windows has to have it's own tablet context */
slouken@2767
    42
static Uint32 highestId = 0;    /* the highest id of the tablet context */
slouken@1895
    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@1895
    54
    SDL_WindowData *data;
slouken@1895
    55
slouken@1895
    56
    /* Allocate the window data */
slouken@1895
    57
    data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
slouken@1895
    58
    if (!data) {
slouken@1895
    59
        SDL_OutOfMemory();
slouken@1895
    60
        return -1;
slouken@1895
    61
    }
slouken@1895
    62
    data->windowID = window->id;
slouken@1895
    63
    data->hwnd = hwnd;
slouken@1913
    64
    data->hdc = GetDC(hwnd);
slouken@1895
    65
    data->created = created;
slouken@1895
    66
    data->mouse_pressed = SDL_FALSE;
slouken@1951
    67
    data->videodata = videodata;
slouken@1895
    68
slouken@1895
    69
    /* Associate the data with the window */
slouken@1895
    70
    if (!SetProp(hwnd, TEXT("SDL_WindowData"), data)) {
slouken@1913
    71
        ReleaseDC(hwnd, data->hdc);
slouken@1895
    72
        SDL_free(data);
slouken@1895
    73
        WIN_SetError("SetProp() failed");
slouken@1895
    74
        return -1;
slouken@1895
    75
    }
slouken@1895
    76
slouken@1895
    77
    /* Set up the window proc function */
slouken@1895
    78
    data->wndproc = (WNDPROC) GetWindowLongPtr(hwnd, GWLP_WNDPROC);
slouken@1895
    79
    if (data->wndproc == NULL) {
slouken@1895
    80
        data->wndproc = DefWindowProc;
slouken@1895
    81
    } else {
slouken@1895
    82
        SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) WIN_WindowProc);
slouken@1895
    83
    }
slouken@1895
    84
slouken@1895
    85
    /* Fill in the SDL window with the window data */
slouken@1895
    86
    {
slouken@1895
    87
        POINT point;
slouken@1895
    88
        point.x = 0;
slouken@1895
    89
        point.y = 0;
slouken@1895
    90
        if (ClientToScreen(hwnd, &point)) {
slouken@1895
    91
            window->x = point.x;
slouken@1895
    92
            window->y = point.y;
slouken@1895
    93
        }
slouken@1895
    94
    }
slouken@1895
    95
    {
slouken@1895
    96
        RECT rect;
slouken@1895
    97
        if (GetClientRect(hwnd, &rect)) {
slouken@1895
    98
            window->w = rect.right;
slouken@1895
    99
            window->h = rect.bottom;
slouken@1895
   100
        }
slouken@1895
   101
    }
slouken@1895
   102
    {
slouken@1895
   103
        DWORD style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   104
        if (style & WS_VISIBLE) {
slouken@1895
   105
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1895
   106
        } else {
slouken@1895
   107
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1895
   108
        }
slouken@1895
   109
        if (style & (WS_BORDER | WS_THICKFRAME)) {
slouken@1895
   110
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1895
   111
        } else {
slouken@1895
   112
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1895
   113
        }
slouken@1895
   114
        if (style & WS_THICKFRAME) {
slouken@1895
   115
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1895
   116
        } else {
slouken@1895
   117
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1895
   118
        }
slouken@1895
   119
        if (style & WS_MAXIMIZE) {
slouken@1895
   120
            window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1895
   121
        } else {
slouken@1895
   122
            window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1895
   123
        }
slouken@1895
   124
        if (style & WS_MINIMIZE) {
slouken@1895
   125
            window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1895
   126
        } else {
slouken@1895
   127
            window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1895
   128
        }
slouken@1895
   129
    }
slouken@1895
   130
    if (GetFocus() == hwnd) {
slouken@1895
   131
        int index = data->videodata->keyboard;
slouken@1895
   132
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@1895
   133
        SDL_SetKeyboardFocus(index, data->windowID);
slouken@1895
   134
slouken@1895
   135
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1895
   136
            RECT rect;
slouken@1895
   137
            GetClientRect(hwnd, &rect);
slouken@1895
   138
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   139
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   140
            ClipCursor(&rect);
slouken@1895
   141
        }
slouken@1895
   142
    }
slouken@1895
   143
slouken@1895
   144
    /* All done! */
slouken@1895
   145
    window->driverdata = data;
slouken@1895
   146
    return 0;
slouken@1895
   147
}
slouken@1895
   148
slouken@1895
   149
int
slouken@1895
   150
WIN_CreateWindow(_THIS, SDL_Window * window)
slouken@1895
   151
{
slouken@2726
   152
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@2710
   153
    RAWINPUTDEVICE Rid;
slouken@2710
   154
    AXIS TabX, TabY;
slouken@2728
   155
    LOGCONTEXTA lc;
slouken@1895
   156
    HWND hwnd;
slouken@1895
   157
    HWND top;
slouken@1895
   158
    RECT rect;
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
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   175
        top = HWND_TOPMOST;
slouken@1895
   176
    } else {
slouken@1895
   177
        top = HWND_NOTOPMOST;
slouken@1895
   178
    }
slouken@1895
   179
    rect.left = 0;
slouken@1895
   180
    rect.top = 0;
slouken@1895
   181
    rect.right = window->w;
slouken@1895
   182
    rect.bottom = window->h;
slouken@1895
   183
    AdjustWindowRectEx(&rect, style, FALSE, 0);
slouken@1895
   184
    w = (rect.right - rect.left);
slouken@1895
   185
    h = (rect.bottom - rect.top);
slouken@1895
   186
slouken@2876
   187
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@2876
   188
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@1895
   189
        x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
slouken@1895
   190
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@1895
   191
        x = CW_USEDEFAULT;
slouken@1895
   192
    } else {
slouken@1895
   193
        x = window->x + rect.left;
slouken@1895
   194
    }
slouken@2876
   195
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@2876
   196
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@1895
   197
        y = (GetSystemMetrics(SM_CYSCREEN) - h) / 2;
slouken@1895
   198
    } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
slouken@1895
   199
        y = CW_USEDEFAULT;
slouken@1895
   200
    } else {
slouken@1895
   201
        y = window->y + rect.top;
slouken@1895
   202
    }
slouken@1895
   203
slouken@1956
   204
    hwnd =
slouken@1956
   205
        CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, NULL, NULL,
slouken@1956
   206
                     SDL_Instance, NULL);
slouken@1895
   207
    if (!hwnd) {
slouken@1895
   208
        WIN_SetError("Couldn't create window");
slouken@1895
   209
        return -1;
slouken@1895
   210
    }
slouken@1895
   211
slouken@2710
   212
    /* we're configuring the tablet data. See Wintab reference for more info */
slouken@2728
   213
    if (videodata->wintabDLL
slouken@2728
   214
        && videodata->WTInfoA(WTI_DEFSYSCTX, 0, &lc) != 0) {
slouken@2710
   215
        lc.lcPktData = PACKETDATA;
slouken@2710
   216
        lc.lcPktMode = PACKETMODE;
slouken@2710
   217
        lc.lcOptions |= CXO_MESSAGES;
slouken@2710
   218
        lc.lcOptions |= CXO_SYSTEM;
slouken@2710
   219
        lc.lcMoveMask = PACKETDATA;
slouken@2710
   220
        lc.lcBtnDnMask = lc.lcBtnUpMask = PACKETDATA;
slouken@2728
   221
        videodata->WTInfoA(WTI_DEVICES, DVC_X, &TabX);
slouken@2728
   222
        videodata->WTInfoA(WTI_DEVICES, DVC_Y, &TabY);
slouken@2710
   223
        lc.lcInOrgX = 0;
slouken@2710
   224
        lc.lcInOrgY = 0;
slouken@2710
   225
        lc.lcInExtX = TabX.axMax;
slouken@2710
   226
        lc.lcInExtY = TabY.axMax;
slouken@2710
   227
        lc.lcOutOrgX = 0;
slouken@2710
   228
        lc.lcOutOrgY = 0;
slouken@2710
   229
        lc.lcOutExtX = GetSystemMetrics(SM_CXSCREEN);
slouken@2710
   230
        lc.lcOutExtY = -GetSystemMetrics(SM_CYSCREEN);
slouken@2710
   231
        if (window->id > highestId) {
slouken@2710
   232
            HCTX *tmp_hctx;
slouken@2710
   233
            highestId = window->id;
slouken@2710
   234
            tmp_hctx =
slouken@2710
   235
                (HCTX *) SDL_realloc(g_hCtx, (highestId + 1) * sizeof(HCTX));
slouken@2710
   236
            if (!tmp_hctx) {
slouken@2710
   237
                SDL_OutOfMemory();
slouken@2710
   238
                DestroyWindow(hwnd);
slouken@2710
   239
                return -1;
slouken@2710
   240
            }
slouken@2710
   241
            g_hCtx = tmp_hctx;
slouken@2710
   242
        }
slouken@2728
   243
        g_hCtx[window->id] = videodata->WTOpenA(hwnd, &lc, TRUE);
slouken@2710
   244
    }
slouken@2710
   245
slouken@2710
   246
    /* we're telling the window, we want it to report raw input events from mice */
slouken@2710
   247
    Rid.usUsagePage = 0x01;
slouken@2710
   248
    Rid.usUsage = 0x02;
slouken@2710
   249
    Rid.dwFlags = RIDEV_INPUTSINK;
slouken@2710
   250
    Rid.hwndTarget = hwnd;
slouken@2710
   251
    RegisterRawInputDevices(&Rid, 1, sizeof(Rid));
slouken@2710
   252
slouken@2710
   253
    WIN_PumpEvents(_this);
slouken@2710
   254
slouken@1951
   255
    if (SetupWindowData(_this, window, hwnd, SDL_TRUE) < 0) {
slouken@1895
   256
        DestroyWindow(hwnd);
slouken@1895
   257
        return -1;
slouken@1895
   258
    }
slouken@1952
   259
#ifdef SDL_VIDEO_OPENGL_WGL
slouken@1913
   260
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1913
   261
        if (WIN_GL_SetupWindow(_this, window) < 0) {
slouken@1913
   262
            WIN_DestroyWindow(_this, window);
slouken@1913
   263
            return -1;
slouken@1913
   264
        }
slouken@1913
   265
    }
slouken@1913
   266
#endif
slouken@1895
   267
    return 0;
slouken@1895
   268
}
slouken@1895
   269
slouken@1895
   270
int
slouken@1895
   271
WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1895
   272
{
slouken@1895
   273
    HWND hwnd = (HWND) data;
slouken@1895
   274
    LPTSTR title;
slouken@1895
   275
    int titleLen;
slouken@1895
   276
slouken@1895
   277
    /* Query the title from the existing window */
slouken@1895
   278
    titleLen = GetWindowTextLength(hwnd);
slouken@1895
   279
    title = SDL_stack_alloc(TCHAR, titleLen + 1);
slouken@1895
   280
    if (title) {
slouken@1895
   281
        titleLen = GetWindowText(hwnd, title, titleLen);
slouken@1895
   282
    } else {
slouken@1895
   283
        titleLen = 0;
slouken@1895
   284
    }
slouken@1895
   285
    if (titleLen > 0) {
slouken@1895
   286
        window->title = WIN_StringToUTF8(title);
slouken@1895
   287
    }
slouken@1895
   288
    if (title) {
slouken@1895
   289
        SDL_stack_free(title);
slouken@1895
   290
    }
slouken@1895
   291
slouken@1951
   292
    if (SetupWindowData(_this, window, hwnd, SDL_FALSE) < 0) {
slouken@1895
   293
        return -1;
slouken@1895
   294
    }
slouken@1895
   295
    return 0;
slouken@1895
   296
}
slouken@1895
   297
slouken@1895
   298
void
slouken@1895
   299
WIN_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1895
   300
{
slouken@1895
   301
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   302
    LPTSTR title;
slouken@1895
   303
slouken@1895
   304
    if (window->title) {
slouken@1895
   305
        title = WIN_UTF8ToString(window->title);
slouken@1895
   306
    } else {
slouken@1895
   307
        title = NULL;
slouken@1895
   308
    }
slouken@1895
   309
    SetWindowText(hwnd, title ? title : TEXT(""));
slouken@1895
   310
    if (title) {
slouken@1895
   311
        SDL_free(title);
slouken@1895
   312
    }
slouken@1895
   313
}
slouken@1895
   314
slouken@1895
   315
void
slouken@2970
   316
WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2970
   317
{
slouken@2970
   318
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@2970
   319
    HICON hicon = NULL;
slouken@2970
   320
slouken@2990
   321
    if (icon) {
slouken@2970
   322
        BYTE *icon_bmp;
slouken@2970
   323
        int icon_len;
slouken@2970
   324
        SDL_RWops *dst;
slouken@2970
   325
        SDL_PixelFormat format;
slouken@2970
   326
        SDL_Surface *surface;
slouken@2970
   327
slouken@2970
   328
        /* Create temporary bitmap buffer */
slouken@2970
   329
        icon_len = 40 + icon->h * icon->w * 4;
slouken@2970
   330
        icon_bmp = SDL_stack_alloc(BYTE, icon_len);
slouken@2970
   331
        dst = SDL_RWFromMem(icon_bmp, icon_len);
slouken@2970
   332
        if (!dst) {
slouken@2970
   333
            SDL_stack_free(icon_bmp);
slouken@2970
   334
            return;
slouken@2970
   335
        }
slouken@2970
   336
slouken@2970
   337
        /* Write the BITMAPINFO header */
slouken@2970
   338
        SDL_WriteLE32(dst, 40);
slouken@2970
   339
        SDL_WriteLE32(dst, icon->w);
slouken@2990
   340
        SDL_WriteLE32(dst, icon->h * 2);
slouken@2970
   341
        SDL_WriteLE16(dst, 1);
slouken@2970
   342
        SDL_WriteLE16(dst, 32);
slouken@2970
   343
        SDL_WriteLE32(dst, BI_RGB);
slouken@2970
   344
        SDL_WriteLE32(dst, icon->h * icon->w * 4);
slouken@2970
   345
        SDL_WriteLE32(dst, 0);
slouken@2970
   346
        SDL_WriteLE32(dst, 0);
slouken@2970
   347
        SDL_WriteLE32(dst, 0);
slouken@2970
   348
        SDL_WriteLE32(dst, 0);
slouken@2970
   349
slouken@2970
   350
        /* Convert the icon to a 32-bit surface with alpha channel */
slouken@2970
   351
        SDL_InitFormat(&format, 32,
slouken@2970
   352
                       0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
slouken@2970
   353
        surface = SDL_ConvertSurface(icon, &format, 0);
slouken@2970
   354
        if (surface) {
slouken@2970
   355
            /* Write the pixels upside down into the bitmap buffer */
slouken@2970
   356
            int y = surface->h;
slouken@2970
   357
            while (y--) {
slouken@2990
   358
                Uint8 *src = (Uint8 *) surface->pixels + y * surface->pitch;
slouken@2970
   359
                SDL_RWwrite(dst, src, surface->pitch, 1);
slouken@2970
   360
            }
slouken@2970
   361
            SDL_FreeSurface(surface);
slouken@2970
   362
slouken@2990
   363
            hicon =
slouken@2990
   364
                CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
slouken@2970
   365
        }
slouken@2970
   366
        SDL_RWclose(dst);
slouken@2970
   367
        SDL_stack_free(icon_bmp);
slouken@2970
   368
    }
slouken@2970
   369
slouken@2971
   370
    /* Set the icon for the window */
slouken@2990
   371
    SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);
slouken@2971
   372
slouken@2971
   373
    /* Set the icon in the task manager (should we do this?) */
slouken@2990
   374
    SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) hicon);
slouken@2970
   375
}
slouken@2970
   376
slouken@2970
   377
void
slouken@1895
   378
WIN_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1895
   379
{
slouken@1895
   380
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   381
    RECT rect;
slouken@1895
   382
    DWORD style;
slouken@1895
   383
    HWND top;
slouken@1895
   384
    int x, y;
slouken@1895
   385
slouken@1895
   386
    /* Figure out what the window area will be */
slouken@1895
   387
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   388
        top = HWND_TOPMOST;
slouken@1895
   389
    } else {
slouken@1895
   390
        top = HWND_NOTOPMOST;
slouken@1895
   391
    }
slouken@1895
   392
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   393
    rect.left = 0;
slouken@1895
   394
    rect.top = 0;
slouken@1895
   395
    rect.right = window->w;
slouken@1895
   396
    rect.bottom = window->h;
slouken@1895
   397
    AdjustWindowRectEx(&rect, style,
slouken@1895
   398
                       (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) !=
slouken@1895
   399
                                                           NULL), 0);
slouken@2875
   400
slouken@2876
   401
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@2876
   402
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@2875
   403
        x = (GetSystemMetrics(SM_CXSCREEN) - window->w) / 2;
slouken@2875
   404
    } else {
slouken@2875
   405
        x = window->x + rect.left;
slouken@2875
   406
    }
slouken@2876
   407
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@2876
   408
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@2875
   409
        y = (GetSystemMetrics(SM_CYSCREEN) - window->h) / 2;
slouken@2875
   410
    } else {
slouken@2875
   411
        y = window->y + rect.top;
slouken@2875
   412
    }
slouken@1895
   413
slouken@1956
   414
    SetWindowPos(hwnd, top, x, y, 0, 0, (SWP_NOCOPYBITS | SWP_NOSIZE));
slouken@1895
   415
}
slouken@1895
   416
slouken@1895
   417
void
slouken@1895
   418
WIN_SetWindowSize(_THIS, SDL_Window * window)
slouken@1895
   419
{
slouken@1895
   420
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   421
    RECT rect;
slouken@1895
   422
    DWORD style;
slouken@1895
   423
    HWND top;
slouken@1895
   424
    int w, h;
slouken@1895
   425
slouken@1895
   426
    /* Figure out what the window area will be */
slouken@1895
   427
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   428
        top = HWND_TOPMOST;
slouken@1895
   429
    } else {
slouken@1895
   430
        top = HWND_NOTOPMOST;
slouken@1895
   431
    }
slouken@1895
   432
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   433
    rect.left = 0;
slouken@1895
   434
    rect.top = 0;
slouken@1895
   435
    rect.right = window->w;
slouken@1895
   436
    rect.bottom = window->h;
slouken@1895
   437
    AdjustWindowRectEx(&rect, style,
slouken@1895
   438
                       (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) !=
slouken@1895
   439
                                                           NULL), 0);
slouken@1895
   440
    w = (rect.right - rect.left);
slouken@1895
   441
    h = (rect.bottom - rect.top);
slouken@1895
   442
slouken@2968
   443
    SetWindowPos(hwnd, top, 0, 0, w, h, (SWP_NOCOPYBITS | SWP_NOMOVE));
slouken@1895
   444
}
slouken@1895
   445
slouken@1895
   446
void
slouken@1895
   447
WIN_ShowWindow(_THIS, SDL_Window * window)
slouken@1895
   448
{
slouken@1895
   449
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   450
slouken@1895
   451
    ShowWindow(hwnd, SW_SHOW);
slouken@1895
   452
}
slouken@1895
   453
slouken@1895
   454
void
slouken@1895
   455
WIN_HideWindow(_THIS, SDL_Window * window)
slouken@1895
   456
{
slouken@1895
   457
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   458
slouken@1895
   459
    ShowWindow(hwnd, SW_HIDE);
slouken@1895
   460
}
slouken@1895
   461
slouken@1895
   462
void
slouken@1895
   463
WIN_RaiseWindow(_THIS, SDL_Window * window)
slouken@1895
   464
{
slouken@1895
   465
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   466
    HWND top;
slouken@1895
   467
slouken@1895
   468
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   469
        top = HWND_TOPMOST;
slouken@1895
   470
    } else {
slouken@1895
   471
        top = HWND_NOTOPMOST;
slouken@1895
   472
    }
slouken@1895
   473
    SetWindowPos(hwnd, top, 0, 0, 0, 0, (SWP_NOMOVE | SWP_NOSIZE));
slouken@1895
   474
}
slouken@1895
   475
slouken@1895
   476
void
slouken@1895
   477
WIN_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1895
   478
{
slouken@1895
   479
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   480
slouken@1895
   481
    ShowWindow(hwnd, SW_MAXIMIZE);
slouken@1895
   482
}
slouken@1895
   483
slouken@1895
   484
void
slouken@1895
   485
WIN_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1895
   486
{
slouken@1895
   487
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   488
slouken@1895
   489
    ShowWindow(hwnd, SW_MINIMIZE);
slouken@1895
   490
}
slouken@1895
   491
slouken@1895
   492
void
slouken@1895
   493
WIN_RestoreWindow(_THIS, SDL_Window * window)
slouken@1895
   494
{
slouken@1895
   495
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   496
slouken@1895
   497
    ShowWindow(hwnd, SW_RESTORE);
slouken@1895
   498
}
slouken@1895
   499
slouken@1895
   500
void
slouken@1895
   501
WIN_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1895
   502
{
slouken@1895
   503
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   504
slouken@2876
   505
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
slouken@2876
   506
        && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   507
        RECT rect;
slouken@1895
   508
        GetClientRect(hwnd, &rect);
slouken@1895
   509
        ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   510
        ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   511
        ClipCursor(&rect);
slouken@1895
   512
    } else {
slouken@1895
   513
        ClipCursor(NULL);
slouken@1895
   514
    }
slouken@1895
   515
}
slouken@1895
   516
slouken@1895
   517
void
slouken@1895
   518
WIN_DestroyWindow(_THIS, SDL_Window * window)
slouken@1895
   519
{
slouken@2726
   520
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1895
   521
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1895
   522
slouken@1895
   523
    if (data) {
slouken@1913
   524
        ReleaseDC(data->hwnd, data->hdc);
slouken@1895
   525
        if (data->created) {
slouken@2726
   526
            if (videodata->wintabDLL) {
slouken@2726
   527
                videodata->WTClose(g_hCtx[window->id]);
slouken@2726
   528
            }
slouken@1895
   529
            DestroyWindow(data->hwnd);
slouken@1895
   530
        }
slouken@1895
   531
        SDL_free(data);
slouken@1895
   532
    }
slouken@1895
   533
}
slouken@1895
   534
slouken@1895
   535
SDL_bool
slouken@1895
   536
WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1895
   537
{
slouken@1895
   538
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   539
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@1895
   540
        info->window = hwnd;
slouken@1895
   541
        return SDL_TRUE;
slouken@1895
   542
    } else {
slouken@1895
   543
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1895
   544
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1895
   545
        return SDL_FALSE;
slouken@1895
   546
    }
slouken@1895
   547
}
slouken@1895
   548
slouken@2713
   549
slouken@2713
   550
/*
slouken@2713
   551
 * Creates a HelperWindow used for DirectInput events.
slouken@2713
   552
 */
slouken@2713
   553
int
slouken@2713
   554
SDL_HelperWindowCreate(void)
slouken@2713
   555
{
slouken@2713
   556
    HINSTANCE hInstance = GetModuleHandleA(NULL);
slouken@2713
   557
    WNDCLASSEX wce;
slouken@2713
   558
bobbens@3045
   559
    /* Make sure window isn't created twice. */
bobbens@3045
   560
    if (SDL_HelperWindow != NULL) {
bobbens@3045
   561
        return 0;
bobbens@3045
   562
    }
bobbens@3045
   563
slouken@2713
   564
    /* Create the class. */
slouken@2714
   565
    SDL_zero(wce);
slouken@2713
   566
    wce.cbSize = sizeof(WNDCLASSEX);
slouken@2713
   567
    wce.lpfnWndProc = DefWindowProcA;
slouken@2713
   568
    wce.lpszClassName = (LPCWSTR) SDL_HelperWindowClassName;
slouken@2713
   569
    wce.hInstance = hInstance;
slouken@2713
   570
slouken@2713
   571
    /* Register the class. */
slouken@2714
   572
    SDL_HelperWindowClass = RegisterClassEx(&wce);
slouken@2713
   573
    if (SDL_HelperWindowClass == 0) {
slouken@2713
   574
        SDL_SetError("Unable to create Helper Window Class: error %d.",
slouken@2713
   575
                     GetLastError());
slouken@2713
   576
        return -1;
slouken@2713
   577
    }
slouken@2713
   578
slouken@2713
   579
    /* Create the window. */
slouken@2714
   580
    SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,
slouken@2714
   581
                                      SDL_HelperWindowName,
slouken@2714
   582
                                      WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
slouken@2714
   583
                                      CW_USEDEFAULT, CW_USEDEFAULT,
slouken@2714
   584
                                      CW_USEDEFAULT, HWND_MESSAGE, NULL,
slouken@2714
   585
                                      hInstance, NULL);
slouken@2713
   586
    if (SDL_HelperWindow == NULL) {
bobbens@3045
   587
        UnregisterClass(SDL_HelperWindowClassName, hInstance);
slouken@2713
   588
        SDL_SetError("Unable to create Helper Window: error %d.",
slouken@2713
   589
                     GetLastError());
slouken@2713
   590
        return -1;
slouken@2713
   591
    }
slouken@2713
   592
slouken@2713
   593
    return 0;
slouken@2713
   594
}
slouken@2713
   595
slouken@2713
   596
slouken@2713
   597
/*
slouken@2713
   598
 * Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
slouken@2713
   599
 */
slouken@2713
   600
void
slouken@2713
   601
SDL_HelperWindowDestroy(void)
slouken@2713
   602
{
bobbens@2863
   603
    HINSTANCE hInstance = GetModuleHandleA(NULL);
bobbens@2863
   604
slouken@2713
   605
    /* Destroy the window. */
bobbens@2863
   606
    if (SDL_HelperWindow != NULL) {
bobbens@2863
   607
        if (DestroyWindow(SDL_HelperWindow) == 0) {
slouken@2865
   608
            SDL_SetError("Unable to destroy Helper Window: error %d.",
slouken@2865
   609
                         GetLastError());
slouken@2865
   610
            return;
bobbens@2863
   611
        }
slouken@2713
   612
        SDL_HelperWindow = NULL;
slouken@2713
   613
    }
slouken@2713
   614
slouken@2713
   615
    /* Unregister the class. */
bobbens@2863
   616
    if (SDL_HelperWindowClass != 0) {
bobbens@2863
   617
        if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
slouken@2865
   618
            SDL_SetError("Unable to destroy Helper Window Class: error %d.",
slouken@2865
   619
                         GetLastError());
slouken@2865
   620
            return;
bobbens@2863
   621
        }
slouken@2713
   622
        SDL_HelperWindowClass = 0;
slouken@2713
   623
    }
slouken@2713
   624
}
slouken@2713
   625
slouken@2713
   626
slouken@1895
   627
/* vi: set ts=4 sw=4 expandtab: */