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