src/video/windows/SDL_windowswindow.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 17 Oct 2013 23:02:29 -0700
changeset 7828 1451063c8ecd
parent 7817 357bccc72a1c
child 7920 94ed2e31a603
permissions -rw-r--r--
Fixed building using MinGW
Our SDL_windows.h needed to be included before anything else so UNICODE is defined.
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@1895
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1895
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1895
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1895
    20
*/
slouken@1895
    21
#include "SDL_config.h"
slouken@1895
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_WINDOWS
slouken@6044
    24
slouken@7828
    25
#include "../../core/windows/SDL_windows.h"
slouken@7828
    26
slouken@7523
    27
#include "SDL_assert.h"
slouken@1895
    28
#include "../SDL_sysvideo.h"
slouken@2970
    29
#include "../SDL_pixels_c.h"
slouken@1895
    30
#include "../../events/SDL_keyboard_c.h"
slouken@1895
    31
slouken@5062
    32
#include "SDL_windowsvideo.h"
slouken@5062
    33
#include "SDL_windowswindow.h"
slouken@1895
    34
slouken@6523
    35
/* Dropfile support */
slouken@6523
    36
#include <shellapi.h>
slouken@6523
    37
slouken@5062
    38
/* This is included after SDL_windowsvideo.h, which includes windows.h */
slouken@1895
    39
#include "SDL_syswm.h"
slouken@1895
    40
slouken@5086
    41
/* Windows CE compatibility */
slouken@5086
    42
#ifndef SWP_NOCOPYBITS
slouken@5086
    43
#define SWP_NOCOPYBITS 0
slouken@5086
    44
#endif
slouken@3168
    45
slouken@2713
    46
/* Fake window to help with DirectInput events. */
slouken@2713
    47
HWND SDL_HelperWindow = NULL;
slouken@2714
    48
static WCHAR *SDL_HelperWindowClassName = TEXT("SDLHelperWindowInputCatcher");
slouken@2714
    49
static WCHAR *SDL_HelperWindowName = TEXT("SDLHelperWindowInputMsgWindow");
slouken@2713
    50
static ATOM SDL_HelperWindowClass = 0;
slouken@2713
    51
slouken@5305
    52
#define STYLE_BASIC         (WS_CLIPSIBLINGS | WS_CLIPCHILDREN)
slouken@5305
    53
#define STYLE_FULLSCREEN    (WS_POPUP)
slouken@5305
    54
#define STYLE_BORDERLESS    (WS_POPUP)
slouken@5305
    55
#define STYLE_NORMAL        (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
slouken@5305
    56
#define STYLE_RESIZABLE     (WS_THICKFRAME | WS_MAXIMIZEBOX)
slouken@5305
    57
#define STYLE_MASK          (STYLE_FULLSCREEN | STYLE_BORDERLESS | STYLE_NORMAL | STYLE_RESIZABLE)
slouken@5305
    58
slouken@5305
    59
static DWORD
slouken@5305
    60
GetWindowStyle(SDL_Window * window)
slouken@5305
    61
{
slouken@5305
    62
    DWORD style = 0;
slouken@5305
    63
slouken@6078
    64
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@5305
    65
        style |= STYLE_FULLSCREEN;
slouken@6078
    66
    } else {
slouken@6078
    67
        if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@5305
    68
            style |= STYLE_BORDERLESS;
slouken@6078
    69
        } else {
slouken@5305
    70
            style |= STYLE_NORMAL;
slouken@6078
    71
        }
slouken@6078
    72
        if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@5305
    73
            style |= STYLE_RESIZABLE;
slouken@6078
    74
        }
slouken@6078
    75
    }
slouken@5305
    76
    return style;
slouken@5305
    77
}
slouken@5305
    78
slouken@7585
    79
static void
slouken@7585
    80
WIN_SetWindowPositionInternal(_THIS, SDL_Window * window, UINT flags)
slouken@7585
    81
{
slouken@7585
    82
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@7585
    83
    RECT rect;
slouken@7585
    84
    DWORD style;
slouken@7585
    85
    HWND top;
slouken@7585
    86
    BOOL menu;
slouken@7585
    87
    int x, y;
slouken@7585
    88
    int w, h;
slouken@7585
    89
slouken@7585
    90
    /* Figure out what the window area will be */
slouken@7585
    91
    if (SDL_ShouldAllowTopmost() && (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) {
slouken@7585
    92
        top = HWND_TOPMOST;
slouken@7585
    93
    } else {
slouken@7585
    94
        top = HWND_NOTOPMOST;
slouken@7585
    95
    }
slouken@7585
    96
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@7585
    97
    rect.left = 0;
slouken@7585
    98
    rect.top = 0;
slouken@7585
    99
    rect.right = window->w;
slouken@7585
   100
    rect.bottom = window->h;
slouken@7585
   101
    menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@7585
   102
    AdjustWindowRectEx(&rect, style, menu, 0);
slouken@7585
   103
    w = (rect.right - rect.left);
slouken@7585
   104
    h = (rect.bottom - rect.top);
slouken@7585
   105
    x = window->x + rect.left;
slouken@7585
   106
    y = window->y + rect.top;
slouken@7585
   107
slouken@7585
   108
    SetWindowPos(hwnd, top, x, y, w, h, flags);
slouken@7585
   109
}
slouken@7585
   110
slouken@1895
   111
static int
slouken@1951
   112
SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created)
slouken@1895
   113
{
slouken@1951
   114
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1895
   115
    SDL_WindowData *data;
slouken@1895
   116
slouken@1895
   117
    /* Allocate the window data */
slouken@7775
   118
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   119
    if (!data) {
icculus@7037
   120
        return SDL_OutOfMemory();
slouken@1895
   121
    }
slouken@3685
   122
    data->window = window;
slouken@1895
   123
    data->hwnd = hwnd;
slouken@1913
   124
    data->hdc = GetDC(hwnd);
slouken@1895
   125
    data->created = created;
slouken@6943
   126
    data->mouse_button_flags = 0;
slouken@1951
   127
    data->videodata = videodata;
slouken@1895
   128
icculus@5980
   129
    window->driverdata = data;
icculus@5980
   130
slouken@1895
   131
    /* Associate the data with the window */
slouken@1895
   132
    if (!SetProp(hwnd, TEXT("SDL_WindowData"), data)) {
slouken@1913
   133
        ReleaseDC(hwnd, data->hdc);
slouken@1895
   134
        SDL_free(data);
icculus@7037
   135
        return WIN_SetError("SetProp() failed");
slouken@1895
   136
    }
slouken@1895
   137
slouken@1895
   138
    /* Set up the window proc function */
slouken@5086
   139
#ifdef GWLP_WNDPROC
slouken@1895
   140
    data->wndproc = (WNDPROC) GetWindowLongPtr(hwnd, GWLP_WNDPROC);
dewyatt@4733
   141
    if (data->wndproc == WIN_WindowProc) {
slouken@3566
   142
        data->wndproc = NULL;
slouken@5086
   143
    } else {
dewyatt@4735
   144
        SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) WIN_WindowProc);
dewyatt@4735
   145
    }
slouken@5086
   146
#else
slouken@5086
   147
    data->wndproc = (WNDPROC) GetWindowLong(hwnd, GWL_WNDPROC);
slouken@5086
   148
    if (data->wndproc == WIN_WindowProc) {
slouken@5086
   149
        data->wndproc = NULL;
slouken@5086
   150
    } else {
slouken@5086
   151
        SetWindowLong(hwnd, GWL_WNDPROC, (LONG_PTR) WIN_WindowProc);
slouken@5086
   152
    }
slouken@5086
   153
#endif
slouken@1895
   154
slouken@1895
   155
    /* Fill in the SDL window with the window data */
slouken@1895
   156
    {
slouken@7585
   157
        RECT rect;
slouken@7585
   158
        if (GetClientRect(hwnd, &rect)) {
slouken@7585
   159
            int w = rect.right;
slouken@7585
   160
            int h = rect.bottom;
slouken@7585
   161
            if ((window->w && window->w != w) || (window->h && window->h != h)) {
gabomdq@7663
   162
                /* We tried to create a window larger than the desktop and Windows didn't allow it.  Override! */
slouken@7585
   163
                WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOZORDER | SWP_NOACTIVATE);
slouken@7585
   164
            } else {
slouken@7585
   165
                window->w = w;
slouken@7585
   166
                window->h = h;
slouken@7585
   167
            }
slouken@7585
   168
        }
slouken@7585
   169
    }
slouken@7585
   170
    {
slouken@1895
   171
        POINT point;
slouken@1895
   172
        point.x = 0;
slouken@1895
   173
        point.y = 0;
slouken@1895
   174
        if (ClientToScreen(hwnd, &point)) {
slouken@5246
   175
            window->x = point.x;
slouken@5246
   176
            window->y = point.y;
slouken@1895
   177
        }
slouken@1895
   178
    }
slouken@1895
   179
    {
slouken@1895
   180
        DWORD style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   181
        if (style & WS_VISIBLE) {
slouken@1895
   182
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1895
   183
        } else {
slouken@1895
   184
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1895
   185
        }
slouken@1895
   186
        if (style & (WS_BORDER | WS_THICKFRAME)) {
slouken@1895
   187
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1895
   188
        } else {
slouken@1895
   189
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1895
   190
        }
slouken@1895
   191
        if (style & WS_THICKFRAME) {
slouken@1895
   192
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1895
   193
        } else {
slouken@1895
   194
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1895
   195
        }
slouken@5086
   196
#ifdef WS_MAXIMIZE
slouken@1895
   197
        if (style & WS_MAXIMIZE) {
slouken@1895
   198
            window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@5086
   199
        } else
slouken@5086
   200
#endif
slouken@5086
   201
        {
slouken@1895
   202
            window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1895
   203
        }
slouken@5086
   204
#ifdef WS_MINIMIZE
slouken@1895
   205
        if (style & WS_MINIMIZE) {
slouken@1895
   206
            window->flags |= SDL_WINDOW_MINIMIZED;
slouken@5086
   207
        } else
slouken@5086
   208
#endif
slouken@5086
   209
        {
slouken@1895
   210
            window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1895
   211
        }
slouken@1895
   212
    }
slouken@1895
   213
    if (GetFocus() == hwnd) {
slouken@1895
   214
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@4465
   215
        SDL_SetKeyboardFocus(data->window);
slouken@1895
   216
slouken@1895
   217
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1895
   218
            RECT rect;
slouken@1895
   219
            GetClientRect(hwnd, &rect);
slouken@1895
   220
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   221
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   222
            ClipCursor(&rect);
slouken@1895
   223
        }
slouken@1895
   224
    }
slouken@1895
   225
slouken@6078
   226
    /* Enable multi-touch */
slouken@4948
   227
    if (videodata->RegisterTouchWindow) {
slouken@4948
   228
        videodata->RegisterTouchWindow(hwnd, (TWF_FINETOUCH|TWF_WANTPALM));
slouken@4948
   229
    }
slouken@4919
   230
slouken@6523
   231
    /* Enable dropping files */
slouken@6523
   232
    DragAcceptFiles(hwnd, TRUE);
slouken@6523
   233
slouken@1895
   234
    /* All done! */
slouken@1895
   235
    return 0;
slouken@1895
   236
}
slouken@1895
   237
slouken@1895
   238
int
slouken@1895
   239
WIN_CreateWindow(_THIS, SDL_Window * window)
slouken@1895
   240
{
slouken@1895
   241
    HWND hwnd;
slouken@1895
   242
    RECT rect;
slouken@5305
   243
    DWORD style = STYLE_BASIC;
slouken@1895
   244
    int x, y;
slouken@1895
   245
    int w, h;
slouken@7191
   246
slouken@5305
   247
    style |= GetWindowStyle(window);
slouken@1895
   248
slouken@1895
   249
    /* Figure out what the window area will be */
slouken@5382
   250
    rect.left = window->x;
slouken@5382
   251
    rect.top = window->y;
slouken@5382
   252
    rect.right = window->x + window->w;
slouken@5382
   253
    rect.bottom = window->y + window->h;
slouken@1895
   254
    AdjustWindowRectEx(&rect, style, FALSE, 0);
slouken@5382
   255
    x = rect.left;
slouken@5382
   256
    y = rect.top;
slouken@1895
   257
    w = (rect.right - rect.left);
slouken@1895
   258
    h = (rect.bottom - rect.top);
slouken@1895
   259
slouken@1956
   260
    hwnd =
slouken@1956
   261
        CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, NULL, NULL,
slouken@1956
   262
                     SDL_Instance, NULL);
slouken@1895
   263
    if (!hwnd) {
icculus@7037
   264
        return WIN_SetError("Couldn't create window");
slouken@1895
   265
    }
slouken@1895
   266
slouken@2710
   267
    WIN_PumpEvents(_this);
slouken@2710
   268
slouken@1951
   269
    if (SetupWindowData(_this, window, hwnd, SDL_TRUE) < 0) {
slouken@1895
   270
        DestroyWindow(hwnd);
slouken@1895
   271
        return -1;
slouken@1895
   272
    }
slouken@5088
   273
#if SDL_VIDEO_OPENGL_WGL
slouken@1913
   274
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1913
   275
        if (WIN_GL_SetupWindow(_this, window) < 0) {
slouken@1913
   276
            WIN_DestroyWindow(_this, window);
slouken@1913
   277
            return -1;
slouken@1913
   278
        }
slouken@1913
   279
    }
slouken@1913
   280
#endif
slouken@1895
   281
    return 0;
slouken@1895
   282
}
slouken@1895
   283
slouken@1895
   284
int
slouken@1895
   285
WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1895
   286
{
slouken@1895
   287
    HWND hwnd = (HWND) data;
slouken@1895
   288
    LPTSTR title;
slouken@1895
   289
    int titleLen;
slouken@1895
   290
slouken@1895
   291
    /* Query the title from the existing window */
slouken@1895
   292
    titleLen = GetWindowTextLength(hwnd);
slouken@1895
   293
    title = SDL_stack_alloc(TCHAR, titleLen + 1);
slouken@1895
   294
    if (title) {
slouken@1895
   295
        titleLen = GetWindowText(hwnd, title, titleLen);
slouken@1895
   296
    } else {
slouken@1895
   297
        titleLen = 0;
slouken@1895
   298
    }
slouken@1895
   299
    if (titleLen > 0) {
slouken@1895
   300
        window->title = WIN_StringToUTF8(title);
slouken@1895
   301
    }
slouken@1895
   302
    if (title) {
slouken@1895
   303
        SDL_stack_free(title);
slouken@1895
   304
    }
slouken@1895
   305
slouken@1951
   306
    if (SetupWindowData(_this, window, hwnd, SDL_FALSE) < 0) {
slouken@1895
   307
        return -1;
slouken@1895
   308
    }
slouken@1895
   309
    return 0;
slouken@1895
   310
}
slouken@1895
   311
slouken@1895
   312
void
slouken@1895
   313
WIN_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1895
   314
{
slouken@1895
   315
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   316
    LPTSTR title;
slouken@1895
   317
slouken@1895
   318
    if (window->title) {
slouken@1895
   319
        title = WIN_UTF8ToString(window->title);
slouken@1895
   320
    } else {
slouken@1895
   321
        title = NULL;
slouken@1895
   322
    }
slouken@1895
   323
    SetWindowText(hwnd, title ? title : TEXT(""));
slouken@7719
   324
    SDL_free(title);
slouken@1895
   325
}
slouken@1895
   326
slouken@1895
   327
void
slouken@2970
   328
WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2970
   329
{
slouken@2970
   330
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@2970
   331
    HICON hicon = NULL;
slouken@5418
   332
    BYTE *icon_bmp;
slouken@7527
   333
    int icon_len, y;
slouken@5418
   334
    SDL_RWops *dst;
slouken@2970
   335
slouken@5418
   336
    /* Create temporary bitmap buffer */
slouken@5418
   337
    icon_len = 40 + icon->h * icon->w * 4;
slouken@5418
   338
    icon_bmp = SDL_stack_alloc(BYTE, icon_len);
slouken@5418
   339
    dst = SDL_RWFromMem(icon_bmp, icon_len);
slouken@5418
   340
    if (!dst) {
slouken@5418
   341
        SDL_stack_free(icon_bmp);
slouken@5418
   342
        return;
slouken@5418
   343
    }
slouken@2970
   344
slouken@5418
   345
    /* Write the BITMAPINFO header */
slouken@5418
   346
    SDL_WriteLE32(dst, 40);
slouken@5418
   347
    SDL_WriteLE32(dst, icon->w);
slouken@5418
   348
    SDL_WriteLE32(dst, icon->h * 2);
slouken@5418
   349
    SDL_WriteLE16(dst, 1);
slouken@5418
   350
    SDL_WriteLE16(dst, 32);
slouken@5418
   351
    SDL_WriteLE32(dst, BI_RGB);
slouken@5418
   352
    SDL_WriteLE32(dst, icon->h * icon->w * 4);
slouken@5418
   353
    SDL_WriteLE32(dst, 0);
slouken@5418
   354
    SDL_WriteLE32(dst, 0);
slouken@5418
   355
    SDL_WriteLE32(dst, 0);
slouken@5418
   356
    SDL_WriteLE32(dst, 0);
slouken@5418
   357
slouken@7523
   358
    /* Write the pixels upside down into the bitmap buffer */
slouken@7523
   359
    SDL_assert(icon->format->format == SDL_PIXELFORMAT_ARGB8888);
slouken@7527
   360
    y = icon->h;
slouken@7523
   361
    while (y--) {
slouken@7523
   362
        Uint8 *src = (Uint8 *) icon->pixels + y * icon->pitch;
slouken@7523
   363
        SDL_RWwrite(dst, src, icon->pitch, 1);
slouken@7523
   364
    }
slouken@2970
   365
slouken@7523
   366
    hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
slouken@7523
   367
slouken@5418
   368
    SDL_RWclose(dst);
slouken@5418
   369
    SDL_stack_free(icon_bmp);
slouken@2970
   370
slouken@2971
   371
    /* Set the icon for the window */
slouken@2990
   372
    SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);
slouken@2971
   373
slouken@2971
   374
    /* Set the icon in the task manager (should we do this?) */
slouken@2990
   375
    SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) hicon);
slouken@2970
   376
}
slouken@2970
   377
icculus@6427
   378
void
icculus@6427
   379
WIN_SetWindowPosition(_THIS, SDL_Window * window)
icculus@6427
   380
{
slouken@6535
   381
    WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOSIZE | SWP_NOACTIVATE);
slouken@1895
   382
}
slouken@1895
   383
slouken@1895
   384
void
slouken@1895
   385
WIN_SetWindowSize(_THIS, SDL_Window * window)
slouken@1895
   386
{
slouken@6535
   387
    WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOACTIVATE);
slouken@5246
   388
}
slouken@5178
   389
slouken@1895
   390
void
slouken@1895
   391
WIN_ShowWindow(_THIS, SDL_Window * window)
slouken@1895
   392
{
slouken@1895
   393
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   394
    ShowWindow(hwnd, SW_SHOW);
slouken@1895
   395
}
slouken@1895
   396
slouken@1895
   397
void
slouken@1895
   398
WIN_HideWindow(_THIS, SDL_Window * window)
slouken@1895
   399
{
slouken@1895
   400
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   401
    ShowWindow(hwnd, SW_HIDE);
slouken@1895
   402
}
slouken@1895
   403
slouken@1895
   404
void
slouken@1895
   405
WIN_RaiseWindow(_THIS, SDL_Window * window)
slouken@1895
   406
{
slouken@7585
   407
    WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE);
slouken@7817
   408
slouken@7817
   409
    /* Raising the window while alt-tabbed can cause it to be minimized for some reason? */
slouken@7817
   410
    WIN_RestoreWindow(_this, window);
slouken@1895
   411
}
slouken@1895
   412
slouken@1895
   413
void
slouken@1895
   414
WIN_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1895
   415
{
slouken@1895
   416
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   417
    ShowWindow(hwnd, SW_MAXIMIZE);
slouken@1895
   418
}
slouken@1895
   419
slouken@1895
   420
void
slouken@1895
   421
WIN_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1895
   422
{
slouken@1895
   423
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   424
    ShowWindow(hwnd, SW_MINIMIZE);
slouken@1895
   425
}
slouken@1895
   426
slouken@1895
   427
void
icculus@6422
   428
WIN_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
icculus@6422
   429
{
icculus@6422
   430
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
icculus@6428
   431
    DWORD style = GetWindowLong(hwnd, GWL_STYLE);
icculus@6428
   432
icculus@6428
   433
    if (bordered) {
icculus@6428
   434
        style &= ~STYLE_BORDERLESS;
icculus@6428
   435
        style |= STYLE_NORMAL;
icculus@6428
   436
    } else {
icculus@6428
   437
        style &= ~STYLE_NORMAL;
icculus@6428
   438
        style |= STYLE_BORDERLESS;
icculus@6428
   439
    }
icculus@6428
   440
icculus@6428
   441
    SetWindowLong(hwnd, GWL_STYLE, style);
slouken@7585
   442
    WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_FRAMECHANGED | SWP_NOREPOSITION | SWP_NOZORDER |SWP_NOACTIVATE | SWP_NOSENDCHANGING);
icculus@6422
   443
}
icculus@6422
   444
icculus@6422
   445
void
slouken@1895
   446
WIN_RestoreWindow(_THIS, SDL_Window * window)
slouken@1895
   447
{
slouken@1895
   448
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   449
slouken@1895
   450
    ShowWindow(hwnd, SW_RESTORE);
slouken@1895
   451
}
slouken@1895
   452
slouken@1895
   453
void
slouken@5305
   454
WIN_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
slouken@5305
   455
{
slouken@5305
   456
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5305
   457
    HWND hwnd = data->hwnd;
slouken@5305
   458
    RECT rect;
slouken@5305
   459
    SDL_Rect bounds;
slouken@5305
   460
    DWORD style;
slouken@5305
   461
    HWND top;
slouken@5305
   462
    BOOL menu;
slouken@5305
   463
    int x, y;
slouken@5305
   464
    int w, h;
slouken@5305
   465
slouken@7585
   466
    if (SDL_ShouldAllowTopmost() && (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) {
slouken@7191
   467
        top = HWND_TOPMOST;
slouken@7191
   468
    } else {
slouken@7191
   469
        top = HWND_NOTOPMOST;
slouken@7191
   470
    }
slouken@6782
   471
slouken@5305
   472
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@5305
   473
    style &= ~STYLE_MASK;
slouken@5305
   474
    style |= GetWindowStyle(window);
slouken@5305
   475
slouken@5305
   476
    WIN_GetDisplayBounds(_this, display, &bounds);
slouken@5305
   477
slouken@5305
   478
    if (fullscreen) {
slouken@5305
   479
        x = bounds.x;
slouken@5305
   480
        y = bounds.y;
slouken@5305
   481
        w = bounds.w;
slouken@5305
   482
        h = bounds.h;
slouken@5305
   483
    } else {
slouken@5305
   484
        rect.left = 0;
slouken@5305
   485
        rect.top = 0;
slouken@5398
   486
        rect.right = window->windowed.w;
slouken@5398
   487
        rect.bottom = window->windowed.h;
slouken@5305
   488
        menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@5305
   489
        AdjustWindowRectEx(&rect, style, menu, 0);
slouken@5305
   490
        w = (rect.right - rect.left);
slouken@5305
   491
        h = (rect.bottom - rect.top);
slouken@5398
   492
        x = window->windowed.x + rect.left;
slouken@5398
   493
        y = window->windowed.y + rect.top;
slouken@5305
   494
    }
slouken@5305
   495
    SetWindowLong(hwnd, GWL_STYLE, style);
slouken@5305
   496
    SetWindowPos(hwnd, top, x, y, w, h, SWP_NOCOPYBITS);
slouken@5305
   497
}
slouken@5305
   498
slouken@5466
   499
int
slouken@5466
   500
WIN_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
   501
{
slouken@5466
   502
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
   503
    SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
slouken@5466
   504
    HDC hdc;
slouken@5466
   505
    BOOL succeeded = FALSE;
slouken@5466
   506
slouken@5466
   507
    hdc = CreateDC(data->DeviceName, NULL, NULL, NULL);
slouken@5466
   508
    if (hdc) {
slouken@5466
   509
        succeeded = SetDeviceGammaRamp(hdc, (LPVOID)ramp);
slouken@5466
   510
        if (!succeeded) {
slouken@5466
   511
            WIN_SetError("SetDeviceGammaRamp()");
slouken@5466
   512
        }
slouken@5466
   513
        DeleteDC(hdc);
slouken@5466
   514
    }
slouken@5466
   515
    return succeeded ? 0 : -1;
slouken@5466
   516
}
slouken@5466
   517
slouken@5466
   518
int
slouken@5466
   519
WIN_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
slouken@5466
   520
{
slouken@5466
   521
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
   522
    SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
slouken@5466
   523
    HDC hdc;
slouken@5466
   524
    BOOL succeeded = FALSE;
slouken@5466
   525
slouken@5466
   526
    hdc = CreateDC(data->DeviceName, NULL, NULL, NULL);
slouken@5466
   527
    if (hdc) {
slouken@5466
   528
        succeeded = GetDeviceGammaRamp(hdc, (LPVOID)ramp);
slouken@5466
   529
        if (!succeeded) {
slouken@5466
   530
            WIN_SetError("GetDeviceGammaRamp()");
slouken@5466
   531
        }
slouken@5466
   532
        DeleteDC(hdc);
slouken@5466
   533
    }
slouken@5466
   534
    return succeeded ? 0 : -1;
slouken@5466
   535
}
slouken@5466
   536
slouken@5305
   537
void
slouken@6662
   538
WIN_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1895
   539
{
slouken@1895
   540
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   541
slouken@6662
   542
    if (grabbed) {
slouken@1895
   543
        RECT rect;
slouken@1895
   544
        GetClientRect(hwnd, &rect);
slouken@1895
   545
        ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   546
        ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   547
        ClipCursor(&rect);
slouken@1895
   548
    } else {
slouken@1895
   549
        ClipCursor(NULL);
slouken@1895
   550
    }
slouken@6782
   551
slouken@7585
   552
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@7584
   553
        UINT flags = SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE;
slouken@6782
   554
slouken@7584
   555
        if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@7584
   556
            flags |= SWP_NOACTIVATE;
slouken@7584
   557
        }
slouken@7585
   558
        WIN_SetWindowPositionInternal(_this, window, flags);
slouken@7191
   559
    }
slouken@1895
   560
}
slouken@1895
   561
slouken@1895
   562
void
slouken@1895
   563
WIN_DestroyWindow(_THIS, SDL_Window * window)
slouken@1895
   564
{
slouken@1895
   565
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1895
   566
slouken@1895
   567
    if (data) {
slouken@1913
   568
        ReleaseDC(data->hwnd, data->hdc);
slouken@1895
   569
        if (data->created) {
slouken@1895
   570
            DestroyWindow(data->hwnd);
icculus@5580
   571
        } else {
icculus@5580
   572
            /* Restore any original event handler... */
icculus@5580
   573
            if (data->wndproc != NULL) {
icculus@5580
   574
#ifdef GWLP_WNDPROC
icculus@5581
   575
                SetWindowLongPtr(data->hwnd, GWLP_WNDPROC,
icculus@5581
   576
                                 (LONG_PTR) data->wndproc);
icculus@5580
   577
#else
icculus@5581
   578
                SetWindowLong(data->hwnd, GWL_WNDPROC,
icculus@5581
   579
                              (LONG_PTR) data->wndproc);
icculus@5580
   580
#endif
icculus@5580
   581
            }
slouken@1895
   582
        }
slouken@1895
   583
        SDL_free(data);
slouken@1895
   584
    }
slouken@1895
   585
}
slouken@1895
   586
slouken@1895
   587
SDL_bool
slouken@1895
   588
WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1895
   589
{
slouken@1895
   590
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   591
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
   592
        info->subsystem = SDL_SYSWM_WINDOWS;
slouken@5056
   593
        info->info.win.window = hwnd;
slouken@1895
   594
        return SDL_TRUE;
slouken@1895
   595
    } else {
slouken@1895
   596
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1895
   597
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1895
   598
        return SDL_FALSE;
slouken@1895
   599
    }
slouken@1895
   600
}
slouken@1895
   601
slouken@2713
   602
slouken@2713
   603
/*
slouken@2713
   604
 * Creates a HelperWindow used for DirectInput events.
slouken@2713
   605
 */
slouken@2713
   606
int
slouken@2713
   607
SDL_HelperWindowCreate(void)
slouken@2713
   608
{
slouken@3097
   609
    HINSTANCE hInstance = GetModuleHandle(NULL);
slouken@3097
   610
    WNDCLASS wce;
slouken@2713
   611
bobbens@3045
   612
    /* Make sure window isn't created twice. */
bobbens@3045
   613
    if (SDL_HelperWindow != NULL) {
bobbens@3045
   614
        return 0;
bobbens@3045
   615
    }
bobbens@3045
   616
slouken@2713
   617
    /* Create the class. */
slouken@2714
   618
    SDL_zero(wce);
slouken@3097
   619
    wce.lpfnWndProc = DefWindowProc;
slouken@2713
   620
    wce.lpszClassName = (LPCWSTR) SDL_HelperWindowClassName;
slouken@2713
   621
    wce.hInstance = hInstance;
slouken@2713
   622
slouken@2713
   623
    /* Register the class. */
slouken@3097
   624
    SDL_HelperWindowClass = RegisterClass(&wce);
slouken@2713
   625
    if (SDL_HelperWindowClass == 0) {
icculus@7037
   626
        return WIN_SetError("Unable to create Helper Window Class");
slouken@2713
   627
    }
slouken@2713
   628
slouken@2713
   629
    /* Create the window. */
slouken@2714
   630
    SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,
slouken@2714
   631
                                      SDL_HelperWindowName,
slouken@3168
   632
                                      WS_OVERLAPPED, CW_USEDEFAULT,
slouken@2714
   633
                                      CW_USEDEFAULT, CW_USEDEFAULT,
icculus@6430
   634
                                      CW_USEDEFAULT, HWND_MESSAGE, NULL,
slouken@2714
   635
                                      hInstance, NULL);
slouken@2713
   636
    if (SDL_HelperWindow == NULL) {
bobbens@3045
   637
        UnregisterClass(SDL_HelperWindowClassName, hInstance);
icculus@7037
   638
        return WIN_SetError("Unable to create Helper Window");
slouken@2713
   639
    }
slouken@2713
   640
slouken@2713
   641
    return 0;
slouken@2713
   642
}
slouken@2713
   643
slouken@2713
   644
slouken@2713
   645
/*
slouken@2713
   646
 * Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
slouken@2713
   647
 */
slouken@2713
   648
void
slouken@2713
   649
SDL_HelperWindowDestroy(void)
slouken@2713
   650
{
slouken@3097
   651
    HINSTANCE hInstance = GetModuleHandle(NULL);
bobbens@2863
   652
slouken@2713
   653
    /* Destroy the window. */
bobbens@2863
   654
    if (SDL_HelperWindow != NULL) {
bobbens@2863
   655
        if (DestroyWindow(SDL_HelperWindow) == 0) {
slouken@4500
   656
            WIN_SetError("Unable to destroy Helper Window");
slouken@2865
   657
            return;
bobbens@2863
   658
        }
slouken@2713
   659
        SDL_HelperWindow = NULL;
slouken@2713
   660
    }
slouken@2713
   661
slouken@2713
   662
    /* Unregister the class. */
bobbens@2863
   663
    if (SDL_HelperWindowClass != 0) {
bobbens@2863
   664
        if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
slouken@4500
   665
            WIN_SetError("Unable to destroy Helper Window Class");
slouken@2865
   666
            return;
bobbens@2863
   667
        }
slouken@2713
   668
        SDL_HelperWindowClass = 0;
slouken@2713
   669
    }
slouken@2713
   670
}
slouken@2713
   671
jorgen@7089
   672
void WIN_OnWindowEnter(_THIS, SDL_Window * window)
jorgen@7089
   673
{
jorgen@7089
   674
#ifdef WM_MOUSELEAVE
jorgen@7089
   675
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
jorgen@7089
   676
    TRACKMOUSEEVENT trackMouseEvent;
jorgen@7089
   677
jorgen@7089
   678
    if (!data || !data->hwnd) {
jorgen@7089
   679
        /* The window wasn't fully initialized */
jorgen@7089
   680
        return;
jorgen@7089
   681
    }
jorgen@7089
   682
jorgen@7089
   683
    trackMouseEvent.cbSize = sizeof(TRACKMOUSEEVENT);
jorgen@7089
   684
    trackMouseEvent.dwFlags = TME_LEAVE;
jorgen@7089
   685
    trackMouseEvent.hwndTrack = data->hwnd;
jorgen@7089
   686
jorgen@7089
   687
    TrackMouseEvent(&trackMouseEvent);
jorgen@7089
   688
#endif /* WM_MOUSELEAVE */
jorgen@7089
   689
}
jorgen@7089
   690
slouken@6044
   691
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
slouken@6044
   692
slouken@1895
   693
/* vi: set ts=4 sw=4 expandtab: */