src/video/windows/SDL_windowswindow.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 05 Jun 2018 12:46:09 -0700
changeset 12006 1b7ed3438a7f
parent 11939 69afb0599152
child 12067 0e37a23d93cb
permissions -rw-r--r--
Fix creating a minimized window in SDL to not cause focus to be stolen (because ShowWindow( hwnd, SW_MINIMIZE ) would be called after creation, thus changing focus to the prior window based on some per-app list in windows, rather than the window being created with WS_MINIMIZED to start with).

This means we have to consider SDL_WINDOW_MINIMIZED a window creation flag, but on non-windows platforms we just remove it and let the normal FinishWindowCreation re-apply and do the minimize as I have no idea what is right on them or if anything should change.

CR: Phil
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 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
*/
icculus@8093
    21
#include "../../SDL_internal.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@8072
    31
#include "../../events/SDL_mouse_c.h"
slouken@1895
    32
slouken@5062
    33
#include "SDL_windowsvideo.h"
slouken@5062
    34
#include "SDL_windowswindow.h"
slouken@8144
    35
#include "SDL_hints.h"
slouken@1895
    36
slouken@6523
    37
/* Dropfile support */
slouken@6523
    38
#include <shellapi.h>
slouken@6523
    39
slouken@5062
    40
/* This is included after SDL_windowsvideo.h, which includes windows.h */
slouken@1895
    41
#include "SDL_syswm.h"
slouken@1895
    42
slouken@5086
    43
/* Windows CE compatibility */
slouken@5086
    44
#ifndef SWP_NOCOPYBITS
slouken@5086
    45
#define SWP_NOCOPYBITS 0
slouken@5086
    46
#endif
slouken@3168
    47
slouken@2713
    48
/* Fake window to help with DirectInput events. */
slouken@2713
    49
HWND SDL_HelperWindow = NULL;
slouken@2714
    50
static WCHAR *SDL_HelperWindowClassName = TEXT("SDLHelperWindowInputCatcher");
slouken@2714
    51
static WCHAR *SDL_HelperWindowName = TEXT("SDLHelperWindowInputMsgWindow");
slouken@2713
    52
static ATOM SDL_HelperWindowClass = 0;
slouken@2713
    53
slouken@11824
    54
/* For borderless Windows, still want the following flags:
slouken@11824
    55
   - WS_CAPTION: this seems to enable the Windows minimize animation
slouken@11824
    56
   - WS_SYSMENU: enables system context menu on task bar
slouken@11824
    57
   - WS_MINIMIZEBOX: window will respond to Windows minimize commands sent to all windows, such as windows key + m, shaking title bar, etc.
slouken@11824
    58
   This will also cause the task bar to overlap the window and other windowed behaviors, so only use this for windows that shouldn't appear to be fullscreen
slouken@11824
    59
 */
slouken@11528
    60
slouken@5305
    61
#define STYLE_BASIC         (WS_CLIPSIBLINGS | WS_CLIPCHILDREN)
slouken@5305
    62
#define STYLE_FULLSCREEN    (WS_POPUP)
slouken@11824
    63
#define STYLE_BORDERLESS    (WS_POPUP)
slouken@11824
    64
#define STYLE_BORDERLESS_WINDOWED (WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
slouken@5305
    65
#define STYLE_NORMAL        (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
slouken@5305
    66
#define STYLE_RESIZABLE     (WS_THICKFRAME | WS_MAXIMIZEBOX)
slouken@5305
    67
#define STYLE_MASK          (STYLE_FULLSCREEN | STYLE_BORDERLESS | STYLE_NORMAL | STYLE_RESIZABLE)
slouken@5305
    68
slouken@5305
    69
static DWORD
slouken@5305
    70
GetWindowStyle(SDL_Window * window)
slouken@5305
    71
{
slouken@5305
    72
    DWORD style = 0;
slouken@5305
    73
slouken@6078
    74
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@5305
    75
        style |= STYLE_FULLSCREEN;
slouken@6078
    76
    } else {
slouken@11460
    77
        if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@11824
    78
            /* SDL 2.1:
slouken@11824
    79
               This behavior more closely matches other platform where the window is borderless
slouken@11824
    80
               but still interacts with the window manager (e.g. task bar shows above it, it can
slouken@11824
    81
               be resized to fit within usable desktop area, etc.) so this should be the behavior
slouken@11824
    82
               for a future SDL release.
slouken@11824
    83
slouken@11824
    84
               If you want a borderless window the size of the desktop that looks like a fullscreen
slouken@11824
    85
               window, then you should use the SDL_WINDOW_FULLSCREEN_DESKTOP flag.
slouken@11824
    86
             */
slouken@11824
    87
            if (SDL_GetHintBoolean("SDL_BORDERLESS_WINDOWED_STYLE", SDL_FALSE)) {
slouken@11824
    88
                style |= STYLE_BORDERLESS_WINDOWED;
slouken@11824
    89
            } else {
slouken@11824
    90
                style |= STYLE_BORDERLESS;
slouken@11824
    91
            }
slouken@11460
    92
        } else {
slouken@11460
    93
            style |= STYLE_NORMAL;
slouken@11460
    94
        }
slouken@11460
    95
slouken@11460
    96
        /* You can have a borderless resizable window */
slouken@6078
    97
        if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@5305
    98
            style |= STYLE_RESIZABLE;
slouken@6078
    99
        }
slouken@12006
   100
slouken@12006
   101
		/* Need to set initialize minimize style, or when we call ShowWindow with WS_MINIMIZE it will activate a random window */
slouken@12006
   102
		if ( window->flags & SDL_WINDOW_MINIMIZED )
slouken@12006
   103
			style |= WS_MINIMIZE;
slouken@6078
   104
    }
slouken@5305
   105
    return style;
slouken@5305
   106
}
slouken@5305
   107
slouken@7585
   108
static void
slouken@11481
   109
WIN_AdjustWindowRectWithStyle(SDL_Window *window, DWORD style, BOOL menu, int *x, int *y, int *width, int *height, SDL_bool use_current)
slouken@11461
   110
{
slouken@11461
   111
    RECT rect;
slouken@11461
   112
slouken@11461
   113
    rect.left = 0;
slouken@11461
   114
    rect.top = 0;
slouken@11481
   115
    rect.right = (use_current ? window->w : window->windowed.w);
slouken@11481
   116
    rect.bottom = (use_current ? window->h : window->windowed.h);
slouken@11529
   117
slouken@11835
   118
    /* borderless windows will have WM_NCCALCSIZE return 0 for the non-client area. When this happens, it looks like windows will send a resize message
slouken@11835
   119
       expanding the window client area to the previous window + chrome size, so shouldn't need to adjust the window size for the set styles.
slouken@11835
   120
     */
slouken@11540
   121
    if (!(window->flags & SDL_WINDOW_BORDERLESS))
slouken@11540
   122
        AdjustWindowRectEx(&rect, style, menu, 0);
slouken@11461
   123
slouken@11481
   124
    *x = (use_current ? window->x : window->windowed.x) + rect.left;
slouken@11481
   125
    *y = (use_current ? window->y : window->windowed.y) + rect.top;
slouken@11461
   126
    *width = (rect.right - rect.left);
slouken@11461
   127
    *height = (rect.bottom - rect.top);
slouken@11461
   128
}
slouken@11461
   129
slouken@11461
   130
static void
slouken@11481
   131
WIN_AdjustWindowRect(SDL_Window *window, int *x, int *y, int *width, int *height, SDL_bool use_current)
slouken@11461
   132
{
slouken@11461
   133
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@11461
   134
    HWND hwnd = data->hwnd;
slouken@11461
   135
    DWORD style;
slouken@11461
   136
    BOOL menu;
slouken@11461
   137
slouken@11481
   138
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@11481
   139
    menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@11481
   140
    WIN_AdjustWindowRectWithStyle(window, style, menu, x, y, width, height, use_current);
slouken@11461
   141
}
slouken@11461
   142
slouken@11461
   143
static void
slouken@7585
   144
WIN_SetWindowPositionInternal(_THIS, SDL_Window * window, UINT flags)
slouken@7585
   145
{
slouken@7941
   146
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@7941
   147
    HWND hwnd = data->hwnd;
slouken@7585
   148
    HWND top;
slouken@7585
   149
    int x, y;
slouken@7585
   150
    int w, h;
slouken@7585
   151
slouken@7585
   152
    /* Figure out what the window area will be */
slouken@11100
   153
    if (SDL_ShouldAllowTopmost() && ((window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS) || (window->flags & SDL_WINDOW_ALWAYS_ON_TOP))) {
slouken@7585
   154
        top = HWND_TOPMOST;
slouken@7585
   155
    } else {
slouken@7585
   156
        top = HWND_NOTOPMOST;
slouken@7585
   157
    }
slouken@11461
   158
slouken@11481
   159
    WIN_AdjustWindowRect(window, &x, &y, &w, &h, SDL_TRUE);    
slouken@7585
   160
slouken@8817
   161
    data->expected_resize = SDL_TRUE;
slouken@9886
   162
    SetWindowPos(hwnd, top, x, y, w, h, flags);
slouken@8817
   163
    data->expected_resize = SDL_FALSE;
slouken@7585
   164
}
slouken@7585
   165
slouken@1895
   166
static int
slouken@11101
   167
SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, HWND parent, SDL_bool created)
slouken@1895
   168
{
slouken@1951
   169
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1895
   170
    SDL_WindowData *data;
slouken@1895
   171
slouken@1895
   172
    /* Allocate the window data */
slouken@7775
   173
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   174
    if (!data) {
icculus@7037
   175
        return SDL_OutOfMemory();
slouken@1895
   176
    }
slouken@3685
   177
    data->window = window;
slouken@1895
   178
    data->hwnd = hwnd;
slouken@11101
   179
    data->parent = parent;
slouken@1913
   180
    data->hdc = GetDC(hwnd);
slouken@10634
   181
    data->hinstance = (HINSTANCE) GetWindowLongPtr(hwnd, GWLP_HINSTANCE);
slouken@1895
   182
    data->created = created;
slouken@6943
   183
    data->mouse_button_flags = 0;
slouken@1951
   184
    data->videodata = videodata;
slouken@9889
   185
    data->initializing = SDL_TRUE;
slouken@1895
   186
icculus@5980
   187
    window->driverdata = data;
icculus@5980
   188
slouken@1895
   189
    /* Associate the data with the window */
slouken@1895
   190
    if (!SetProp(hwnd, TEXT("SDL_WindowData"), data)) {
slouken@1913
   191
        ReleaseDC(hwnd, data->hdc);
slouken@1895
   192
        SDL_free(data);
icculus@7037
   193
        return WIN_SetError("SetProp() failed");
slouken@1895
   194
    }
slouken@1895
   195
slouken@1895
   196
    /* Set up the window proc function */
slouken@5086
   197
#ifdef GWLP_WNDPROC
slouken@1895
   198
    data->wndproc = (WNDPROC) GetWindowLongPtr(hwnd, GWLP_WNDPROC);
dewyatt@4733
   199
    if (data->wndproc == WIN_WindowProc) {
slouken@3566
   200
        data->wndproc = NULL;
slouken@5086
   201
    } else {
dewyatt@4735
   202
        SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) WIN_WindowProc);
dewyatt@4735
   203
    }
slouken@5086
   204
#else
slouken@5086
   205
    data->wndproc = (WNDPROC) GetWindowLong(hwnd, GWL_WNDPROC);
slouken@5086
   206
    if (data->wndproc == WIN_WindowProc) {
slouken@5086
   207
        data->wndproc = NULL;
slouken@5086
   208
    } else {
slouken@5086
   209
        SetWindowLong(hwnd, GWL_WNDPROC, (LONG_PTR) WIN_WindowProc);
slouken@5086
   210
    }
slouken@5086
   211
#endif
slouken@1895
   212
slouken@1895
   213
    /* Fill in the SDL window with the window data */
slouken@1895
   214
    {
slouken@7585
   215
        RECT rect;
slouken@7585
   216
        if (GetClientRect(hwnd, &rect)) {
slouken@7585
   217
            int w = rect.right;
slouken@7585
   218
            int h = rect.bottom;
slouken@11835
   219
            if ((window->windowed.w && window->windowed.w != w) || (window->windowed.h && window->windowed.h != h)) {
gabomdq@7663
   220
                /* We tried to create a window larger than the desktop and Windows didn't allow it.  Override! */
slouken@9886
   221
                int x, y;
slouken@9886
   222
                int w, h;
slouken@9886
   223
slouken@9886
   224
                /* Figure out what the window area will be */
slouken@11835
   225
                WIN_AdjustWindowRect(window, &x, &y, &w, &h, SDL_FALSE);
slouken@9886
   226
                SetWindowPos(hwnd, HWND_NOTOPMOST, x, y, w, h, SWP_NOCOPYBITS | SWP_NOZORDER | SWP_NOACTIVATE);
slouken@7585
   227
            } else {
slouken@7585
   228
                window->w = w;
slouken@7585
   229
                window->h = h;
slouken@7585
   230
            }
slouken@7585
   231
        }
slouken@7585
   232
    }
slouken@7585
   233
    {
slouken@1895
   234
        POINT point;
slouken@1895
   235
        point.x = 0;
slouken@1895
   236
        point.y = 0;
slouken@1895
   237
        if (ClientToScreen(hwnd, &point)) {
slouken@5246
   238
            window->x = point.x;
slouken@5246
   239
            window->y = point.y;
slouken@1895
   240
        }
slouken@1895
   241
    }
slouken@1895
   242
    {
slouken@1895
   243
        DWORD style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   244
        if (style & WS_VISIBLE) {
slouken@1895
   245
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1895
   246
        } else {
slouken@1895
   247
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1895
   248
        }
slouken@11528
   249
        if (style & WS_POPUP) {
slouken@11528
   250
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@11528
   251
        } else {
slouken@1895
   252
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1895
   253
        }
slouken@1895
   254
        if (style & WS_THICKFRAME) {
slouken@1895
   255
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1895
   256
        } else {
slouken@1895
   257
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1895
   258
        }
slouken@5086
   259
#ifdef WS_MAXIMIZE
slouken@1895
   260
        if (style & WS_MAXIMIZE) {
slouken@1895
   261
            window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@5086
   262
        } else
slouken@5086
   263
#endif
slouken@5086
   264
        {
slouken@1895
   265
            window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1895
   266
        }
slouken@5086
   267
#ifdef WS_MINIMIZE
slouken@1895
   268
        if (style & WS_MINIMIZE) {
slouken@1895
   269
            window->flags |= SDL_WINDOW_MINIMIZED;
slouken@5086
   270
        } else
slouken@5086
   271
#endif
slouken@5086
   272
        {
slouken@1895
   273
            window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1895
   274
        }
slouken@1895
   275
    }
slouken@1895
   276
    if (GetFocus() == hwnd) {
slouken@1895
   277
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@4465
   278
        SDL_SetKeyboardFocus(data->window);
slouken@1895
   279
slouken@1895
   280
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1895
   281
            RECT rect;
slouken@1895
   282
            GetClientRect(hwnd, &rect);
slouken@1895
   283
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   284
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   285
            ClipCursor(&rect);
slouken@1895
   286
        }
slouken@1895
   287
    }
slouken@1895
   288
slouken@6078
   289
    /* Enable multi-touch */
slouken@4948
   290
    if (videodata->RegisterTouchWindow) {
slouken@4948
   291
        videodata->RegisterTouchWindow(hwnd, (TWF_FINETOUCH|TWF_WANTPALM));
slouken@4948
   292
    }
slouken@4919
   293
slouken@6523
   294
    /* Enable dropping files */
slouken@6523
   295
    DragAcceptFiles(hwnd, TRUE);
slouken@6523
   296
slouken@9889
   297
    data->initializing = SDL_FALSE;
slouken@9888
   298
slouken@1895
   299
    /* All done! */
slouken@1895
   300
    return 0;
slouken@1895
   301
}
slouken@1895
   302
slouken@10806
   303
slouken@10806
   304
slouken@1895
   305
int
slouken@1895
   306
WIN_CreateWindow(_THIS, SDL_Window * window)
slouken@1895
   307
{
slouken@11100
   308
    HWND hwnd, parent = NULL;
slouken@5305
   309
    DWORD style = STYLE_BASIC;
slouken@1895
   310
    int x, y;
slouken@1895
   311
    int w, h;
slouken@7191
   312
slouken@11100
   313
    if (window->flags & SDL_WINDOW_SKIP_TASKBAR) {
slouken@11100
   314
        parent = CreateWindow(SDL_Appname, TEXT(""), STYLE_BASIC, 0, 0, 32, 32, NULL, NULL, SDL_Instance, NULL);
slouken@11100
   315
    }
slouken@11100
   316
slouken@5305
   317
    style |= GetWindowStyle(window);
slouken@1895
   318
slouken@1895
   319
    /* Figure out what the window area will be */
slouken@11835
   320
    WIN_AdjustWindowRectWithStyle(window, style, FALSE, &x, &y, &w, &h, SDL_FALSE);
slouken@1895
   321
slouken@1956
   322
    hwnd =
slouken@11100
   323
        CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, parent, NULL,
slouken@1956
   324
                     SDL_Instance, NULL);
slouken@1895
   325
    if (!hwnd) {
icculus@7037
   326
        return WIN_SetError("Couldn't create window");
slouken@1895
   327
    }
slouken@1895
   328
slouken@2710
   329
    WIN_PumpEvents(_this);
slouken@2710
   330
slouken@11101
   331
    if (SetupWindowData(_this, window, hwnd, parent, SDL_TRUE) < 0) {
slouken@1895
   332
        DestroyWindow(hwnd);
slouken@11101
   333
        if (parent) {
slouken@11101
   334
            DestroyWindow(parent);
slouken@11101
   335
        }
slouken@1895
   336
        return -1;
slouken@1895
   337
    }
gabomdq@8021
   338
slouken@11835
   339
    /* Inform Windows of the frame change so we can respond to WM_NCCALCSIZE */
slouken@11540
   340
    SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
slouken@11528
   341
slouken@12006
   342
	if ( window->flags & SDL_WINDOW_MINIMIZED )
slouken@12006
   343
		ShowWindow( hwnd, SW_SHOWMINNOACTIVE );
slouken@12006
   344
slouken@10806
   345
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
slouken@10806
   346
        return 0;
slouken@10806
   347
    }
slouken@10806
   348
slouken@10806
   349
    /* The rest of this macro mess is for OpenGL or OpenGL ES windows */
slouken@10806
   350
#if SDL_VIDEO_OPENGL_ES2
slouken@10806
   351
    if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES
gabomdq@8021
   352
#if SDL_VIDEO_OPENGL_WGL
slouken@10806
   353
        && (!_this->gl_data || WIN_GL_UseEGL(_this))
slouken@10806
   354
#endif /* SDL_VIDEO_OPENGL_WGL */
slouken@10806
   355
    ) {
slouken@10806
   356
#if SDL_VIDEO_OPENGL_EGL
gabomdq@8021
   357
        if (WIN_GLES_SetupWindow(_this, window) < 0) {
gabomdq@8021
   358
            WIN_DestroyWindow(_this, window);
gabomdq@8021
   359
            return -1;
gabomdq@8021
   360
        }
slouken@10806
   361
        return 0;
gabomdq@8021
   362
#else
slouken@10806
   363
        return SDL_SetError("Could not create GLES window surface (EGL support not configured)");
slouken@10806
   364
#endif /* SDL_VIDEO_OPENGL_EGL */ 
slouken@10806
   365
    }
gabomdq@8021
   366
#endif /* SDL_VIDEO_OPENGL_ES2 */
gabomdq@8021
   367
slouken@5088
   368
#if SDL_VIDEO_OPENGL_WGL
slouken@10806
   369
    if (WIN_GL_SetupWindow(_this, window) < 0) {
slouken@10806
   370
        WIN_DestroyWindow(_this, window);
slouken@10806
   371
        return -1;
slouken@1913
   372
    }
slouken@10806
   373
#else
slouken@10806
   374
    return SDL_SetError("Could not create GL window (WGL support not configured)");
slouken@1913
   375
#endif
gabomdq@8021
   376
slouken@1895
   377
    return 0;
slouken@1895
   378
}
slouken@1895
   379
slouken@1895
   380
int
slouken@1895
   381
WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1895
   382
{
slouken@1895
   383
    HWND hwnd = (HWND) data;
slouken@1895
   384
    LPTSTR title;
slouken@1895
   385
    int titleLen;
slouken@1895
   386
slouken@1895
   387
    /* Query the title from the existing window */
slouken@1895
   388
    titleLen = GetWindowTextLength(hwnd);
slouken@1895
   389
    title = SDL_stack_alloc(TCHAR, titleLen + 1);
slouken@1895
   390
    if (title) {
slouken@1895
   391
        titleLen = GetWindowText(hwnd, title, titleLen);
slouken@1895
   392
    } else {
slouken@1895
   393
        titleLen = 0;
slouken@1895
   394
    }
slouken@1895
   395
    if (titleLen > 0) {
slouken@1895
   396
        window->title = WIN_StringToUTF8(title);
slouken@1895
   397
    }
slouken@1895
   398
    if (title) {
slouken@1895
   399
        SDL_stack_free(title);
slouken@1895
   400
    }
slouken@1895
   401
slouken@11102
   402
    if (SetupWindowData(_this, window, hwnd, GetParent(hwnd), SDL_FALSE) < 0) {
slouken@1895
   403
        return -1;
slouken@1895
   404
    }
slouken@8144
   405
slouken@8144
   406
#if SDL_VIDEO_OPENGL_WGL
slouken@8144
   407
    {
slouken@8144
   408
        const char *hint = SDL_GetHint(SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT);
slouken@8144
   409
        if (hint) {
philipp@8778
   410
            /* This hint is a pointer (in string form) of the address of
philipp@8778
   411
               the window to share a pixel format with
philipp@8778
   412
            */
slouken@8144
   413
            SDL_Window *otherWindow = NULL;
slouken@8144
   414
            SDL_sscanf(hint, "%p", (void**)&otherWindow);
slouken@8144
   415
philipp@8778
   416
            /* Do some error checking on the pointer */
slouken@8144
   417
            if (otherWindow != NULL && otherWindow->magic == &_this->window_magic)
slouken@8144
   418
            {
philipp@8778
   419
                /* If the otherWindow has SDL_WINDOW_OPENGL set, set it for the new window as well */
slouken@8144
   420
                if (otherWindow->flags & SDL_WINDOW_OPENGL)
slouken@8144
   421
                {
slouken@8144
   422
                    window->flags |= SDL_WINDOW_OPENGL;
slouken@8144
   423
                    if(!WIN_GL_SetPixelFormatFrom(_this, otherWindow, window)) {
slouken@8144
   424
                        return -1;
slouken@8144
   425
                    }
slouken@8144
   426
                }
slouken@8144
   427
            }
slouken@8144
   428
        }
slouken@8144
   429
    }
slouken@8144
   430
#endif
slouken@1895
   431
    return 0;
slouken@1895
   432
}
slouken@1895
   433
slouken@1895
   434
void
slouken@1895
   435
WIN_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1895
   436
{
slouken@1895
   437
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
icculus@9474
   438
    LPTSTR title = WIN_UTF8ToString(window->title);
icculus@9474
   439
    SetWindowText(hwnd, title);
slouken@7719
   440
    SDL_free(title);
slouken@1895
   441
}
slouken@1895
   442
slouken@1895
   443
void
slouken@2970
   444
WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2970
   445
{
slouken@2970
   446
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@2970
   447
    HICON hicon = NULL;
slouken@5418
   448
    BYTE *icon_bmp;
slouken@11825
   449
    int icon_len, mask_len, y;
slouken@5418
   450
    SDL_RWops *dst;
slouken@2970
   451
slouken@11825
   452
    /* Create temporary buffer for ICONIMAGE structure */
slouken@11825
   453
    mask_len = (icon->h * (icon->w + 7)/8);
slouken@11825
   454
    icon_len = 40 + icon->h * icon->w * sizeof(Uint32) + mask_len;
slouken@5418
   455
    icon_bmp = SDL_stack_alloc(BYTE, icon_len);
slouken@5418
   456
    dst = SDL_RWFromMem(icon_bmp, icon_len);
slouken@5418
   457
    if (!dst) {
slouken@5418
   458
        SDL_stack_free(icon_bmp);
slouken@5418
   459
        return;
slouken@5418
   460
    }
slouken@2970
   461
slouken@5418
   462
    /* Write the BITMAPINFO header */
slouken@5418
   463
    SDL_WriteLE32(dst, 40);
slouken@5418
   464
    SDL_WriteLE32(dst, icon->w);
slouken@5418
   465
    SDL_WriteLE32(dst, icon->h * 2);
slouken@5418
   466
    SDL_WriteLE16(dst, 1);
slouken@5418
   467
    SDL_WriteLE16(dst, 32);
slouken@5418
   468
    SDL_WriteLE32(dst, BI_RGB);
slouken@10816
   469
    SDL_WriteLE32(dst, icon->h * icon->w * sizeof(Uint32));
slouken@5418
   470
    SDL_WriteLE32(dst, 0);
slouken@5418
   471
    SDL_WriteLE32(dst, 0);
slouken@5418
   472
    SDL_WriteLE32(dst, 0);
slouken@5418
   473
    SDL_WriteLE32(dst, 0);
slouken@5418
   474
slouken@7523
   475
    /* Write the pixels upside down into the bitmap buffer */
slouken@7523
   476
    SDL_assert(icon->format->format == SDL_PIXELFORMAT_ARGB8888);
slouken@7527
   477
    y = icon->h;
slouken@7523
   478
    while (y--) {
slouken@7523
   479
        Uint8 *src = (Uint8 *) icon->pixels + y * icon->pitch;
slouken@10816
   480
        SDL_RWwrite(dst, src, icon->w * sizeof(Uint32), 1);
slouken@7523
   481
    }
slouken@2970
   482
slouken@11825
   483
    /* Write the mask */
slouken@11825
   484
    SDL_memset(icon_bmp + icon_len - mask_len, 0xFF, mask_len);
slouken@11825
   485
slouken@7523
   486
    hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
slouken@7523
   487
slouken@5418
   488
    SDL_RWclose(dst);
slouken@5418
   489
    SDL_stack_free(icon_bmp);
slouken@2970
   490
slouken@2971
   491
    /* Set the icon for the window */
slouken@2990
   492
    SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);
slouken@2971
   493
slouken@2971
   494
    /* Set the icon in the task manager (should we do this?) */
slouken@2990
   495
    SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) hicon);
slouken@2970
   496
}
slouken@2970
   497
icculus@6427
   498
void
icculus@6427
   499
WIN_SetWindowPosition(_THIS, SDL_Window * window)
icculus@6427
   500
{
slouken@6535
   501
    WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOSIZE | SWP_NOACTIVATE);
slouken@1895
   502
}
slouken@1895
   503
slouken@1895
   504
void
slouken@1895
   505
WIN_SetWindowSize(_THIS, SDL_Window * window)
slouken@1895
   506
{
slouken@6535
   507
    WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOACTIVATE);
slouken@5246
   508
}
slouken@5178
   509
slouken@11815
   510
int
slouken@11815
   511
WIN_GetWindowBordersSize(_THIS, SDL_Window * window, int *top, int *left, int *bottom, int *right)
slouken@11815
   512
{
slouken@11815
   513
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@11815
   514
    RECT rcClient, rcWindow;
slouken@11815
   515
    POINT ptDiff;
slouken@11815
   516
slouken@11815
   517
    /* rcClient stores the size of the inner window, while rcWindow stores the outer size relative to the top-left
slouken@11815
   518
     * screen position; so the top/left values of rcClient are always {0,0} and bottom/right are {height,width} */
slouken@11815
   519
    GetClientRect(hwnd, &rcClient);
slouken@11815
   520
    GetWindowRect(hwnd, &rcWindow);
slouken@11815
   521
slouken@11815
   522
    /* convert the top/left values to make them relative to
slouken@11815
   523
     * the window; they will end up being slightly negative */
slouken@11815
   524
    ptDiff.y = rcWindow.top;
slouken@11815
   525
    ptDiff.x = rcWindow.left;
slouken@11815
   526
slouken@11815
   527
    ScreenToClient(hwnd, &ptDiff);
slouken@11815
   528
slouken@11815
   529
    rcWindow.top  = ptDiff.y;
slouken@11815
   530
    rcWindow.left = ptDiff.x;
slouken@11815
   531
slouken@11815
   532
    /* convert the bottom/right values to make them relative to the window,
slouken@11815
   533
     * these will be slightly bigger than the inner width/height */
slouken@11815
   534
    ptDiff.y = rcWindow.bottom;
slouken@11815
   535
    ptDiff.x = rcWindow.right;
slouken@11815
   536
slouken@11815
   537
    ScreenToClient(hwnd, &ptDiff);
slouken@11815
   538
slouken@11815
   539
    rcWindow.bottom = ptDiff.y;
slouken@11815
   540
    rcWindow.right  = ptDiff.x;
slouken@11815
   541
slouken@11815
   542
    /* Now that both the inner and outer rects use the same coordinate system we can substract them to get the border size.
slouken@11815
   543
     * Keep in mind that the top/left coordinates of rcWindow are negative because the border lies slightly before {0,0},
slouken@11815
   544
     * so switch them around because SDL2 wants them in positive. */
slouken@11815
   545
    *top    = rcClient.top    - rcWindow.top;
slouken@11815
   546
    *left   = rcClient.left   - rcWindow.left;
slouken@11815
   547
    *bottom = rcWindow.bottom - rcClient.bottom;
slouken@11815
   548
    *right  = rcWindow.right  - rcClient.right;
slouken@11815
   549
slouken@11815
   550
    return 0;
slouken@11815
   551
}
slouken@11815
   552
slouken@1895
   553
void
slouken@1895
   554
WIN_ShowWindow(_THIS, SDL_Window * window)
slouken@1895
   555
{
slouken@11939
   556
	DWORD style;
slouken@11939
   557
	HWND hwnd;
slouken@11939
   558
	int nCmdShow;
slouken@11939
   559
	
slouken@11939
   560
	hwnd = ( (SDL_WindowData *)window->driverdata )->hwnd;
slouken@11939
   561
	nCmdShow = SW_SHOW;
slouken@11939
   562
	style = GetWindowLong(hwnd, GWL_EXSTYLE);
slouken@11939
   563
	if ( style & WS_EX_NOACTIVATE )
slouken@11939
   564
		nCmdShow = SW_SHOWNOACTIVATE;
slouken@11939
   565
	
slouken@11939
   566
    ShowWindow(hwnd, nCmdShow );
slouken@1895
   567
}
slouken@1895
   568
slouken@1895
   569
void
slouken@1895
   570
WIN_HideWindow(_THIS, SDL_Window * window)
slouken@1895
   571
{
slouken@1895
   572
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   573
    ShowWindow(hwnd, SW_HIDE);
slouken@1895
   574
}
slouken@1895
   575
slouken@1895
   576
void
slouken@1895
   577
WIN_RaiseWindow(_THIS, SDL_Window * window)
slouken@1895
   578
{
slime73@10450
   579
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slime73@10450
   580
    SetForegroundWindow(hwnd);
slouken@1895
   581
}
slouken@1895
   582
slouken@1895
   583
void
slouken@1895
   584
WIN_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1895
   585
{
slouken@7941
   586
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@7941
   587
    HWND hwnd = data->hwnd;
slouken@8817
   588
    data->expected_resize = SDL_TRUE;
slouken@1895
   589
    ShowWindow(hwnd, SW_MAXIMIZE);
slouken@8817
   590
    data->expected_resize = SDL_FALSE;
slouken@1895
   591
}
slouken@1895
   592
slouken@1895
   593
void
slouken@1895
   594
WIN_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1895
   595
{
slouken@1895
   596
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   597
    ShowWindow(hwnd, SW_MINIMIZE);
slouken@1895
   598
}
slouken@1895
   599
slouken@1895
   600
void
icculus@6422
   601
WIN_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
icculus@6422
   602
{
slouken@8817
   603
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@8817
   604
    HWND hwnd = data->hwnd;
slouken@11251
   605
    DWORD style;
icculus@6428
   606
slouken@11251
   607
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@11251
   608
    style &= ~STYLE_MASK;
slouken@11251
   609
    style |= GetWindowStyle(window);
icculus@6428
   610
slouken@8817
   611
    data->in_border_change = SDL_TRUE;
slouken@9886
   612
    SetWindowLong(hwnd, GWL_STYLE, style);
slouken@8817
   613
    WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE);
slouken@8817
   614
    data->in_border_change = SDL_FALSE;
icculus@6422
   615
}
icculus@6422
   616
icculus@6422
   617
void
icculus@10385
   618
WIN_SetWindowResizable(_THIS, SDL_Window * window, SDL_bool resizable)
icculus@10385
   619
{
icculus@10385
   620
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
icculus@10385
   621
    HWND hwnd = data->hwnd;
slouken@11251
   622
    DWORD style;
icculus@10385
   623
slouken@11251
   624
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@11251
   625
    style &= ~STYLE_MASK;
slouken@11251
   626
    style |= GetWindowStyle(window);
icculus@10385
   627
icculus@10385
   628
    SetWindowLong(hwnd, GWL_STYLE, style);
icculus@10385
   629
}
icculus@10385
   630
icculus@10385
   631
void
slouken@1895
   632
WIN_RestoreWindow(_THIS, SDL_Window * window)
slouken@1895
   633
{
slouken@7941
   634
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@7941
   635
    HWND hwnd = data->hwnd;
slouken@8817
   636
    data->expected_resize = SDL_TRUE;
slouken@1895
   637
    ShowWindow(hwnd, SW_RESTORE);
slouken@8817
   638
    data->expected_resize = SDL_FALSE;
slouken@1895
   639
}
slouken@1895
   640
slouken@1895
   641
void
slouken@5305
   642
WIN_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
slouken@5305
   643
{
slouken@5305
   644
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5305
   645
    HWND hwnd = data->hwnd;
slouken@5305
   646
    SDL_Rect bounds;
slouken@5305
   647
    DWORD style;
slouken@5305
   648
    HWND top;
slouken@5305
   649
    int x, y;
slouken@5305
   650
    int w, h;
slouken@5305
   651
jgranick@10978
   652
    if (SDL_ShouldAllowTopmost() && ((window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS) || window->flags & SDL_WINDOW_ALWAYS_ON_TOP)) {
slouken@7191
   653
        top = HWND_TOPMOST;
slouken@7191
   654
    } else {
slouken@7191
   655
        top = HWND_NOTOPMOST;
slouken@7191
   656
    }
slouken@6782
   657
slouken@5305
   658
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@5305
   659
    style &= ~STYLE_MASK;
slouken@5305
   660
    style |= GetWindowStyle(window);
slouken@5305
   661
slouken@5305
   662
    WIN_GetDisplayBounds(_this, display, &bounds);
slouken@5305
   663
slouken@5305
   664
    if (fullscreen) {
slouken@5305
   665
        x = bounds.x;
slouken@5305
   666
        y = bounds.y;
slouken@5305
   667
        w = bounds.w;
slouken@5305
   668
        h = bounds.h;
dludwig@9990
   669
dludwig@9990
   670
        /* Unset the maximized flag.  This fixes
dludwig@9990
   671
           https://bugzilla.libsdl.org/show_bug.cgi?id=3215
dludwig@9990
   672
        */
dludwig@9990
   673
        if (style & WS_MAXIMIZE) {
dludwig@9990
   674
            data->windowed_mode_was_maximized = SDL_TRUE;
dludwig@9990
   675
            style &= ~WS_MAXIMIZE;
dludwig@9990
   676
        }
slouken@5305
   677
    } else {
slouken@11575
   678
        BOOL menu;
slouken@11575
   679
dludwig@9990
   680
        /* Restore window-maximization state, as applicable.
dludwig@9990
   681
           Special care is taken to *not* do this if and when we're
dludwig@9990
   682
           alt-tab'ing away (to some other window; as indicated by
dludwig@9990
   683
           in_window_deactivation), otherwise
dludwig@9990
   684
           https://bugzilla.libsdl.org/show_bug.cgi?id=3215 can reproduce!
dludwig@9990
   685
        */
dludwig@9990
   686
        if (data->windowed_mode_was_maximized && !data->in_window_deactivation) {
dludwig@9990
   687
            style |= WS_MAXIMIZE;
dludwig@9990
   688
            data->windowed_mode_was_maximized = SDL_FALSE;
dludwig@9990
   689
        }
slouken@11461
   690
slouken@11575
   691
        menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@11575
   692
        WIN_AdjustWindowRectWithStyle(window, style, menu, &x, &y, &w, &h, SDL_FALSE);
slouken@5305
   693
    }
slouken@5305
   694
    SetWindowLong(hwnd, GWL_STYLE, style);
slouken@8817
   695
    data->expected_resize = SDL_TRUE;
slouken@8205
   696
    SetWindowPos(hwnd, top, x, y, w, h, SWP_NOCOPYBITS | SWP_NOACTIVATE);
slouken@8817
   697
    data->expected_resize = SDL_FALSE;
slouken@5305
   698
}
slouken@5305
   699
slouken@5466
   700
int
slouken@5466
   701
WIN_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
   702
{
slouken@5466
   703
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
   704
    SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
slouken@5466
   705
    HDC hdc;
slouken@5466
   706
    BOOL succeeded = FALSE;
slouken@5466
   707
slouken@5466
   708
    hdc = CreateDC(data->DeviceName, NULL, NULL, NULL);
slouken@5466
   709
    if (hdc) {
slouken@5466
   710
        succeeded = SetDeviceGammaRamp(hdc, (LPVOID)ramp);
slouken@5466
   711
        if (!succeeded) {
slouken@5466
   712
            WIN_SetError("SetDeviceGammaRamp()");
slouken@5466
   713
        }
slouken@5466
   714
        DeleteDC(hdc);
slouken@5466
   715
    }
slouken@5466
   716
    return succeeded ? 0 : -1;
slouken@5466
   717
}
slouken@5466
   718
slouken@5466
   719
int
slouken@5466
   720
WIN_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
slouken@5466
   721
{
slouken@5466
   722
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
   723
    SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
slouken@5466
   724
    HDC hdc;
slouken@5466
   725
    BOOL succeeded = FALSE;
slouken@5466
   726
slouken@5466
   727
    hdc = CreateDC(data->DeviceName, NULL, NULL, NULL);
slouken@5466
   728
    if (hdc) {
slouken@5466
   729
        succeeded = GetDeviceGammaRamp(hdc, (LPVOID)ramp);
slouken@5466
   730
        if (!succeeded) {
slouken@5466
   731
            WIN_SetError("GetDeviceGammaRamp()");
slouken@5466
   732
        }
slouken@5466
   733
        DeleteDC(hdc);
slouken@5466
   734
    }
slouken@5466
   735
    return succeeded ? 0 : -1;
slouken@5466
   736
}
slouken@5466
   737
slouken@5305
   738
void
slouken@6662
   739
WIN_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1895
   740
{
slouken@8072
   741
    WIN_UpdateClipCursor(window);
slouken@6782
   742
slouken@7585
   743
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@7584
   744
        UINT flags = SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE;
slouken@6782
   745
slouken@7584
   746
        if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@7584
   747
            flags |= SWP_NOACTIVATE;
slouken@7584
   748
        }
slouken@7585
   749
        WIN_SetWindowPositionInternal(_this, window, flags);
slouken@7191
   750
    }
slouken@1895
   751
}
slouken@1895
   752
slouken@1895
   753
void
slouken@1895
   754
WIN_DestroyWindow(_THIS, SDL_Window * window)
slouken@1895
   755
{
slouken@1895
   756
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1895
   757
slouken@1895
   758
    if (data) {
slouken@1913
   759
        ReleaseDC(data->hwnd, data->hdc);
slouken@9665
   760
        RemoveProp(data->hwnd, TEXT("SDL_WindowData"));
slouken@1895
   761
        if (data->created) {
slouken@1895
   762
            DestroyWindow(data->hwnd);
slouken@11101
   763
            if (data->parent) {
slouken@11101
   764
                DestroyWindow(data->parent);
slouken@11101
   765
            }
icculus@5580
   766
        } else {
icculus@5580
   767
            /* Restore any original event handler... */
icculus@5580
   768
            if (data->wndproc != NULL) {
icculus@5580
   769
#ifdef GWLP_WNDPROC
icculus@5581
   770
                SetWindowLongPtr(data->hwnd, GWLP_WNDPROC,
icculus@5581
   771
                                 (LONG_PTR) data->wndproc);
icculus@5580
   772
#else
icculus@5581
   773
                SetWindowLong(data->hwnd, GWL_WNDPROC,
icculus@5581
   774
                              (LONG_PTR) data->wndproc);
icculus@5580
   775
#endif
icculus@5580
   776
            }
slouken@1895
   777
        }
slouken@1895
   778
        SDL_free(data);
slouken@1895
   779
    }
slouken@8978
   780
    window->driverdata = NULL;
slouken@1895
   781
}
slouken@1895
   782
slouken@1895
   783
SDL_bool
slouken@1895
   784
WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1895
   785
{
icculus@9422
   786
    const SDL_WindowData *data = (const SDL_WindowData *) window->driverdata;
slouken@1895
   787
    if (info->version.major <= SDL_MAJOR_VERSION) {
philipp@10631
   788
        int versionnum = SDL_VERSIONNUM(info->version.major, info->version.minor, info->version.patch);
philipp@10631
   789
slouken@4900
   790
        info->subsystem = SDL_SYSWM_WINDOWS;
icculus@9422
   791
        info->info.win.window = data->hwnd;
philipp@10631
   792
philipp@10631
   793
        if (versionnum >= SDL_VERSIONNUM(2, 0, 4)) {
philipp@10631
   794
            info->info.win.hdc = data->hdc;
philipp@10631
   795
        }
philipp@10631
   796
slouken@10634
   797
        if (versionnum >= SDL_VERSIONNUM(2, 0, 5)) {
slouken@10634
   798
            info->info.win.hinstance = data->hinstance;
slouken@10634
   799
        }
slouken@10634
   800
slouken@1895
   801
        return SDL_TRUE;
slouken@1895
   802
    } else {
philipp@10945
   803
        SDL_SetError("Application not compiled with SDL %d.%d",
slouken@1895
   804
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1895
   805
        return SDL_FALSE;
slouken@1895
   806
    }
slouken@1895
   807
}
slouken@1895
   808
slouken@2713
   809
slouken@2713
   810
/*
slouken@2713
   811
 * Creates a HelperWindow used for DirectInput events.
slouken@2713
   812
 */
slouken@2713
   813
int
slouken@2713
   814
SDL_HelperWindowCreate(void)
slouken@2713
   815
{
slouken@3097
   816
    HINSTANCE hInstance = GetModuleHandle(NULL);
slouken@3097
   817
    WNDCLASS wce;
slouken@2713
   818
bobbens@3045
   819
    /* Make sure window isn't created twice. */
bobbens@3045
   820
    if (SDL_HelperWindow != NULL) {
bobbens@3045
   821
        return 0;
bobbens@3045
   822
    }
bobbens@3045
   823
slouken@2713
   824
    /* Create the class. */
slouken@2714
   825
    SDL_zero(wce);
slouken@3097
   826
    wce.lpfnWndProc = DefWindowProc;
slouken@2713
   827
    wce.lpszClassName = (LPCWSTR) SDL_HelperWindowClassName;
slouken@2713
   828
    wce.hInstance = hInstance;
slouken@2713
   829
slouken@2713
   830
    /* Register the class. */
slouken@3097
   831
    SDL_HelperWindowClass = RegisterClass(&wce);
slouken@8238
   832
    if (SDL_HelperWindowClass == 0 && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) {
icculus@7037
   833
        return WIN_SetError("Unable to create Helper Window Class");
slouken@2713
   834
    }
slouken@2713
   835
slouken@2713
   836
    /* Create the window. */
slouken@2714
   837
    SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,
slouken@2714
   838
                                      SDL_HelperWindowName,
slouken@3168
   839
                                      WS_OVERLAPPED, CW_USEDEFAULT,
slouken@2714
   840
                                      CW_USEDEFAULT, CW_USEDEFAULT,
icculus@6430
   841
                                      CW_USEDEFAULT, HWND_MESSAGE, NULL,
slouken@2714
   842
                                      hInstance, NULL);
slouken@2713
   843
    if (SDL_HelperWindow == NULL) {
bobbens@3045
   844
        UnregisterClass(SDL_HelperWindowClassName, hInstance);
icculus@7037
   845
        return WIN_SetError("Unable to create Helper Window");
slouken@2713
   846
    }
slouken@2713
   847
slouken@2713
   848
    return 0;
slouken@2713
   849
}
slouken@2713
   850
slouken@2713
   851
slouken@2713
   852
/*
slouken@2713
   853
 * Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
slouken@2713
   854
 */
slouken@2713
   855
void
slouken@2713
   856
SDL_HelperWindowDestroy(void)
slouken@2713
   857
{
slouken@3097
   858
    HINSTANCE hInstance = GetModuleHandle(NULL);
bobbens@2863
   859
slouken@2713
   860
    /* Destroy the window. */
bobbens@2863
   861
    if (SDL_HelperWindow != NULL) {
bobbens@2863
   862
        if (DestroyWindow(SDL_HelperWindow) == 0) {
slouken@4500
   863
            WIN_SetError("Unable to destroy Helper Window");
slouken@2865
   864
            return;
bobbens@2863
   865
        }
slouken@2713
   866
        SDL_HelperWindow = NULL;
slouken@2713
   867
    }
slouken@2713
   868
slouken@2713
   869
    /* Unregister the class. */
bobbens@2863
   870
    if (SDL_HelperWindowClass != 0) {
bobbens@2863
   871
        if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
slouken@4500
   872
            WIN_SetError("Unable to destroy Helper Window Class");
slouken@2865
   873
            return;
bobbens@2863
   874
        }
slouken@2713
   875
        SDL_HelperWindowClass = 0;
slouken@2713
   876
    }
slouken@2713
   877
}
slouken@2713
   878
jorgen@7089
   879
void WIN_OnWindowEnter(_THIS, SDL_Window * window)
jorgen@7089
   880
{
jorgen@7089
   881
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
jorgen@7089
   882
jorgen@7089
   883
    if (!data || !data->hwnd) {
jorgen@7089
   884
        /* The window wasn't fully initialized */
jorgen@7089
   885
        return;
jorgen@7089
   886
    }
jorgen@7089
   887
jgranick@10978
   888
    if (window->flags & SDL_WINDOW_ALWAYS_ON_TOP) {
jgranick@10978
   889
        WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOSIZE | SWP_NOACTIVATE);
jgranick@10978
   890
    }
jorgen@7089
   891
jgranick@10978
   892
#ifdef WM_MOUSELEAVE
jgranick@10978
   893
    {
jgranick@10978
   894
        TRACKMOUSEEVENT trackMouseEvent;
jgranick@10978
   895
jgranick@10978
   896
        trackMouseEvent.cbSize = sizeof(TRACKMOUSEEVENT);
jgranick@10978
   897
        trackMouseEvent.dwFlags = TME_LEAVE;
jgranick@10978
   898
        trackMouseEvent.hwndTrack = data->hwnd;
jgranick@10978
   899
jgranick@10978
   900
        TrackMouseEvent(&trackMouseEvent);
jgranick@10978
   901
    }
jorgen@7089
   902
#endif /* WM_MOUSELEAVE */
jorgen@7089
   903
}
jorgen@7089
   904
slouken@8072
   905
void
slouken@8072
   906
WIN_UpdateClipCursor(SDL_Window *window)
slouken@8072
   907
{
slouken@8072
   908
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@8072
   909
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@8072
   910
slouken@8916
   911
    if (data->focus_click_pending) {
slouken@8072
   912
        return;
slouken@8072
   913
    }
slouken@8072
   914
slouken@8072
   915
    if ((mouse->relative_mode || (window->flags & SDL_WINDOW_INPUT_GRABBED)) &&
slouken@8072
   916
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@8072
   917
        if (mouse->relative_mode && !mouse->relative_mode_warp) {
slouken@8072
   918
            LONG cx, cy;
slouken@8072
   919
            RECT rect;
slouken@8072
   920
            GetWindowRect(data->hwnd, &rect);
slouken@8072
   921
slouken@8072
   922
            cx = (rect.left + rect.right) / 2;
slouken@8072
   923
            cy = (rect.top + rect.bottom) / 2;
slouken@8072
   924
slouken@8072
   925
            /* Make an absurdly small clip rect */
slouken@8072
   926
            rect.left = cx - 1;
slouken@8072
   927
            rect.right = cx + 1;
slouken@8072
   928
            rect.top = cy - 1;
slouken@8072
   929
            rect.bottom = cy + 1;
slouken@8072
   930
slouken@8072
   931
            ClipCursor(&rect);
slouken@8072
   932
        } else {
slouken@8072
   933
            RECT rect;
slouken@8072
   934
            if (GetClientRect(data->hwnd, &rect) && !IsRectEmpty(&rect)) {
slouken@8072
   935
                ClientToScreen(data->hwnd, (LPPOINT) & rect);
slouken@8072
   936
                ClientToScreen(data->hwnd, (LPPOINT) & rect + 1);
slouken@8072
   937
                ClipCursor(&rect);
slouken@8072
   938
            }
slouken@8072
   939
        }
slouken@8072
   940
    } else {
slouken@8072
   941
        ClipCursor(NULL);
slouken@8072
   942
    }
slouken@8072
   943
}
slouken@8072
   944
icculus@8938
   945
int
icculus@8938
   946
WIN_SetWindowHitTest(SDL_Window *window, SDL_bool enabled)
icculus@8938
   947
{
icculus@8938
   948
    return 0;  /* just succeed, the real work is done elsewhere. */
icculus@8938
   949
}
icculus@8938
   950
icculus@10025
   951
int
icculus@10025
   952
WIN_SetWindowOpacity(_THIS, SDL_Window * window, float opacity)
icculus@10025
   953
{
icculus@10025
   954
    const SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
icculus@10025
   955
    const HWND hwnd = data->hwnd;
icculus@10025
   956
    const LONG style = GetWindowLong(hwnd, GWL_EXSTYLE);
icculus@10025
   957
icculus@10025
   958
    SDL_assert(style != 0);
icculus@10025
   959
icculus@10025
   960
    if (opacity == 1.0f) {
icculus@10025
   961
        /* want it fully opaque, just mark it unlayered if necessary. */
icculus@10025
   962
        if (style & WS_EX_LAYERED) {
icculus@10025
   963
            if (SetWindowLong(hwnd, GWL_EXSTYLE, style & ~WS_EX_LAYERED) == 0) {
icculus@10025
   964
                return WIN_SetError("SetWindowLong()");
icculus@10025
   965
            }
icculus@10025
   966
        }
icculus@10025
   967
    } else {
icculus@10025
   968
        const BYTE alpha = (BYTE) ((int) (opacity * 255.0f));
icculus@10025
   969
        /* want it transparent, mark it layered if necessary. */
icculus@10025
   970
        if ((style & WS_EX_LAYERED) == 0) {
icculus@10025
   971
            if (SetWindowLong(hwnd, GWL_EXSTYLE, style | WS_EX_LAYERED) == 0) {
icculus@10025
   972
                return WIN_SetError("SetWindowLong()");
icculus@10025
   973
            }
icculus@10025
   974
        }
icculus@10025
   975
icculus@10025
   976
        if (SetLayeredWindowAttributes(hwnd, 0, alpha, LWA_ALPHA) == 0) {
icculus@10025
   977
            return WIN_SetError("SetLayeredWindowAttributes()");
icculus@10025
   978
        }
icculus@10025
   979
    }
icculus@10025
   980
icculus@10025
   981
    return 0;
icculus@10025
   982
}
icculus@10025
   983
slouken@6044
   984
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
slouken@6044
   985
slouken@1895
   986
/* vi: set ts=4 sw=4 expandtab: */