src/video/windows/SDL_windowswindow.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Feb 2014 10:02:18 -0800
changeset 8205 1a97f96f55cd
parent 8149 681eb46b8ac4
child 8238 9e09e1561e00
permissions -rw-r--r--
Fixed bug where a window created fullscreen and hidden would get activated and "shown" but never actually be visible.
This is the case with the Steam In-Home Streaming client.
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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@5305
    54
#define STYLE_BASIC         (WS_CLIPSIBLINGS | WS_CLIPCHILDREN)
slouken@5305
    55
#define STYLE_FULLSCREEN    (WS_POPUP)
slouken@5305
    56
#define STYLE_BORDERLESS    (WS_POPUP)
slouken@5305
    57
#define STYLE_NORMAL        (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
slouken@5305
    58
#define STYLE_RESIZABLE     (WS_THICKFRAME | WS_MAXIMIZEBOX)
slouken@5305
    59
#define STYLE_MASK          (STYLE_FULLSCREEN | STYLE_BORDERLESS | STYLE_NORMAL | STYLE_RESIZABLE)
slouken@5305
    60
slouken@5305
    61
static DWORD
slouken@5305
    62
GetWindowStyle(SDL_Window * window)
slouken@5305
    63
{
slouken@5305
    64
    DWORD style = 0;
slouken@5305
    65
slouken@6078
    66
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@5305
    67
        style |= STYLE_FULLSCREEN;
slouken@6078
    68
    } else {
slouken@6078
    69
        if (window->flags & SDL_WINDOW_BORDERLESS) {
slouken@5305
    70
            style |= STYLE_BORDERLESS;
slouken@6078
    71
        } else {
slouken@5305
    72
            style |= STYLE_NORMAL;
slouken@6078
    73
        }
slouken@6078
    74
        if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@5305
    75
            style |= STYLE_RESIZABLE;
slouken@6078
    76
        }
slouken@6078
    77
    }
slouken@5305
    78
    return style;
slouken@5305
    79
}
slouken@5305
    80
slouken@7585
    81
static void
slouken@7585
    82
WIN_SetWindowPositionInternal(_THIS, SDL_Window * window, UINT flags)
slouken@7585
    83
{
slouken@7941
    84
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@7941
    85
    HWND hwnd = data->hwnd;
slouken@7585
    86
    RECT rect;
slouken@7585
    87
    DWORD style;
slouken@7585
    88
    HWND top;
slouken@7585
    89
    BOOL menu;
slouken@7585
    90
    int x, y;
slouken@7585
    91
    int w, h;
slouken@7585
    92
slouken@7585
    93
    /* Figure out what the window area will be */
slouken@7585
    94
    if (SDL_ShouldAllowTopmost() && (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) {
slouken@7585
    95
        top = HWND_TOPMOST;
slouken@7585
    96
    } else {
slouken@7585
    97
        top = HWND_NOTOPMOST;
slouken@7585
    98
    }
slouken@7585
    99
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@7585
   100
    rect.left = 0;
slouken@7585
   101
    rect.top = 0;
slouken@7585
   102
    rect.right = window->w;
slouken@7585
   103
    rect.bottom = window->h;
slouken@7585
   104
    menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@7585
   105
    AdjustWindowRectEx(&rect, style, menu, 0);
slouken@7585
   106
    w = (rect.right - rect.left);
slouken@7585
   107
    h = (rect.bottom - rect.top);
slouken@7585
   108
    x = window->x + rect.left;
slouken@7585
   109
    y = window->y + rect.top;
slouken@7585
   110
slouken@7941
   111
    data->expected_resize = TRUE;
slouken@7585
   112
    SetWindowPos(hwnd, top, x, y, w, h, flags);
slouken@7941
   113
    data->expected_resize = FALSE;
slouken@7585
   114
}
slouken@7585
   115
slouken@1895
   116
static int
slouken@1951
   117
SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created)
slouken@1895
   118
{
slouken@1951
   119
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1895
   120
    SDL_WindowData *data;
slouken@1895
   121
slouken@1895
   122
    /* Allocate the window data */
slouken@7775
   123
    data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   124
    if (!data) {
icculus@7037
   125
        return SDL_OutOfMemory();
slouken@1895
   126
    }
slouken@3685
   127
    data->window = window;
slouken@1895
   128
    data->hwnd = hwnd;
slouken@1913
   129
    data->hdc = GetDC(hwnd);
slouken@1895
   130
    data->created = created;
slouken@6943
   131
    data->mouse_button_flags = 0;
slouken@1951
   132
    data->videodata = videodata;
slouken@1895
   133
icculus@5980
   134
    window->driverdata = data;
icculus@5980
   135
slouken@1895
   136
    /* Associate the data with the window */
slouken@1895
   137
    if (!SetProp(hwnd, TEXT("SDL_WindowData"), data)) {
slouken@1913
   138
        ReleaseDC(hwnd, data->hdc);
slouken@1895
   139
        SDL_free(data);
icculus@7037
   140
        return WIN_SetError("SetProp() failed");
slouken@1895
   141
    }
slouken@1895
   142
slouken@1895
   143
    /* Set up the window proc function */
slouken@5086
   144
#ifdef GWLP_WNDPROC
slouken@1895
   145
    data->wndproc = (WNDPROC) GetWindowLongPtr(hwnd, GWLP_WNDPROC);
dewyatt@4733
   146
    if (data->wndproc == WIN_WindowProc) {
slouken@3566
   147
        data->wndproc = NULL;
slouken@5086
   148
    } else {
dewyatt@4735
   149
        SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) WIN_WindowProc);
dewyatt@4735
   150
    }
slouken@5086
   151
#else
slouken@5086
   152
    data->wndproc = (WNDPROC) GetWindowLong(hwnd, GWL_WNDPROC);
slouken@5086
   153
    if (data->wndproc == WIN_WindowProc) {
slouken@5086
   154
        data->wndproc = NULL;
slouken@5086
   155
    } else {
slouken@5086
   156
        SetWindowLong(hwnd, GWL_WNDPROC, (LONG_PTR) WIN_WindowProc);
slouken@5086
   157
    }
slouken@5086
   158
#endif
slouken@1895
   159
slouken@1895
   160
    /* Fill in the SDL window with the window data */
slouken@1895
   161
    {
slouken@7585
   162
        RECT rect;
slouken@7585
   163
        if (GetClientRect(hwnd, &rect)) {
slouken@7585
   164
            int w = rect.right;
slouken@7585
   165
            int h = rect.bottom;
slouken@7585
   166
            if ((window->w && window->w != w) || (window->h && window->h != h)) {
gabomdq@7663
   167
                /* We tried to create a window larger than the desktop and Windows didn't allow it.  Override! */
slouken@7585
   168
                WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOZORDER | SWP_NOACTIVATE);
slouken@7585
   169
            } else {
slouken@7585
   170
                window->w = w;
slouken@7585
   171
                window->h = h;
slouken@7585
   172
            }
slouken@7585
   173
        }
slouken@7585
   174
    }
slouken@7585
   175
    {
slouken@1895
   176
        POINT point;
slouken@1895
   177
        point.x = 0;
slouken@1895
   178
        point.y = 0;
slouken@1895
   179
        if (ClientToScreen(hwnd, &point)) {
slouken@5246
   180
            window->x = point.x;
slouken@5246
   181
            window->y = point.y;
slouken@1895
   182
        }
slouken@1895
   183
    }
slouken@1895
   184
    {
slouken@1895
   185
        DWORD style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   186
        if (style & WS_VISIBLE) {
slouken@1895
   187
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1895
   188
        } else {
slouken@1895
   189
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1895
   190
        }
slouken@1895
   191
        if (style & (WS_BORDER | WS_THICKFRAME)) {
slouken@1895
   192
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1895
   193
        } else {
slouken@1895
   194
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1895
   195
        }
slouken@1895
   196
        if (style & WS_THICKFRAME) {
slouken@1895
   197
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1895
   198
        } else {
slouken@1895
   199
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1895
   200
        }
slouken@5086
   201
#ifdef WS_MAXIMIZE
slouken@1895
   202
        if (style & WS_MAXIMIZE) {
slouken@1895
   203
            window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@5086
   204
        } else
slouken@5086
   205
#endif
slouken@5086
   206
        {
slouken@1895
   207
            window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1895
   208
        }
slouken@5086
   209
#ifdef WS_MINIMIZE
slouken@1895
   210
        if (style & WS_MINIMIZE) {
slouken@1895
   211
            window->flags |= SDL_WINDOW_MINIMIZED;
slouken@5086
   212
        } else
slouken@5086
   213
#endif
slouken@5086
   214
        {
slouken@1895
   215
            window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1895
   216
        }
slouken@1895
   217
    }
slouken@1895
   218
    if (GetFocus() == hwnd) {
slouken@1895
   219
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@4465
   220
        SDL_SetKeyboardFocus(data->window);
slouken@1895
   221
slouken@1895
   222
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1895
   223
            RECT rect;
slouken@1895
   224
            GetClientRect(hwnd, &rect);
slouken@1895
   225
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   226
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   227
            ClipCursor(&rect);
slouken@1895
   228
        }
slouken@1895
   229
    }
slouken@1895
   230
slouken@6078
   231
    /* Enable multi-touch */
slouken@4948
   232
    if (videodata->RegisterTouchWindow) {
slouken@4948
   233
        videodata->RegisterTouchWindow(hwnd, (TWF_FINETOUCH|TWF_WANTPALM));
slouken@4948
   234
    }
slouken@4919
   235
slouken@6523
   236
    /* Enable dropping files */
slouken@6523
   237
    DragAcceptFiles(hwnd, TRUE);
slouken@6523
   238
slouken@1895
   239
    /* All done! */
slouken@1895
   240
    return 0;
slouken@1895
   241
}
slouken@1895
   242
slouken@1895
   243
int
slouken@1895
   244
WIN_CreateWindow(_THIS, SDL_Window * window)
slouken@1895
   245
{
slouken@1895
   246
    HWND hwnd;
slouken@1895
   247
    RECT rect;
slouken@5305
   248
    DWORD style = STYLE_BASIC;
slouken@1895
   249
    int x, y;
slouken@1895
   250
    int w, h;
slouken@7191
   251
slouken@5305
   252
    style |= GetWindowStyle(window);
slouken@1895
   253
slouken@1895
   254
    /* Figure out what the window area will be */
slouken@5382
   255
    rect.left = window->x;
slouken@5382
   256
    rect.top = window->y;
slouken@5382
   257
    rect.right = window->x + window->w;
slouken@5382
   258
    rect.bottom = window->y + window->h;
slouken@1895
   259
    AdjustWindowRectEx(&rect, style, FALSE, 0);
slouken@5382
   260
    x = rect.left;
slouken@5382
   261
    y = rect.top;
slouken@1895
   262
    w = (rect.right - rect.left);
slouken@1895
   263
    h = (rect.bottom - rect.top);
slouken@1895
   264
slouken@1956
   265
    hwnd =
slouken@1956
   266
        CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, NULL, NULL,
slouken@1956
   267
                     SDL_Instance, NULL);
slouken@1895
   268
    if (!hwnd) {
icculus@7037
   269
        return WIN_SetError("Couldn't create window");
slouken@1895
   270
    }
slouken@1895
   271
slouken@2710
   272
    WIN_PumpEvents(_this);
slouken@2710
   273
slouken@1951
   274
    if (SetupWindowData(_this, window, hwnd, SDL_TRUE) < 0) {
slouken@1895
   275
        DestroyWindow(hwnd);
slouken@1895
   276
        return -1;
slouken@1895
   277
    }
gabomdq@8021
   278
gabomdq@8021
   279
#if SDL_VIDEO_OPENGL_WGL
gabomdq@8021
   280
    /* We need to initialize the extensions before deciding how to create ES profiles */
gabomdq@8021
   281
    if (window->flags & SDL_WINDOW_OPENGL) {
gabomdq@8021
   282
        WIN_GL_InitExtensions(_this);
gabomdq@8021
   283
    }
gabomdq@8021
   284
#endif
gabomdq@8021
   285
gabomdq@8021
   286
#if SDL_VIDEO_OPENGL_ES2
gabomdq@8021
   287
    if ((window->flags & SDL_WINDOW_OPENGL) &&
gabomdq@8021
   288
        _this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES
gabomdq@8021
   289
#if SDL_VIDEO_OPENGL_WGL           
gabomdq@8021
   290
        && (!_this->gl_data || !_this->gl_data->HAS_WGL_EXT_create_context_es2_profile)
gabomdq@8021
   291
#endif  
gabomdq@8021
   292
        ) {
gabomdq@8021
   293
#if SDL_VIDEO_OPENGL_EGL  
gabomdq@8021
   294
        if (WIN_GLES_SetupWindow(_this, window) < 0) {
gabomdq@8021
   295
            WIN_DestroyWindow(_this, window);
gabomdq@8021
   296
            return -1;
gabomdq@8021
   297
        }
gabomdq@8021
   298
#else
gabomdq@8021
   299
        return SDL_SetError("Could not create GLES window surface (no EGL support available)");
gabomdq@8021
   300
#endif /* SDL_VIDEO_OPENGL_EGL */
gabomdq@8021
   301
    } else 
gabomdq@8021
   302
#endif /* SDL_VIDEO_OPENGL_ES2 */
gabomdq@8021
   303
slouken@5088
   304
#if SDL_VIDEO_OPENGL_WGL
slouken@1913
   305
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1913
   306
        if (WIN_GL_SetupWindow(_this, window) < 0) {
slouken@1913
   307
            WIN_DestroyWindow(_this, window);
slouken@1913
   308
            return -1;
slouken@1913
   309
        }
slouken@1913
   310
    }
slouken@1913
   311
#endif
gabomdq@8021
   312
slouken@1895
   313
    return 0;
slouken@1895
   314
}
slouken@1895
   315
slouken@1895
   316
int
slouken@1895
   317
WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1895
   318
{
slouken@1895
   319
    HWND hwnd = (HWND) data;
slouken@1895
   320
    LPTSTR title;
slouken@1895
   321
    int titleLen;
slouken@1895
   322
slouken@1895
   323
    /* Query the title from the existing window */
slouken@1895
   324
    titleLen = GetWindowTextLength(hwnd);
slouken@1895
   325
    title = SDL_stack_alloc(TCHAR, titleLen + 1);
slouken@1895
   326
    if (title) {
slouken@1895
   327
        titleLen = GetWindowText(hwnd, title, titleLen);
slouken@1895
   328
    } else {
slouken@1895
   329
        titleLen = 0;
slouken@1895
   330
    }
slouken@1895
   331
    if (titleLen > 0) {
slouken@1895
   332
        window->title = WIN_StringToUTF8(title);
slouken@1895
   333
    }
slouken@1895
   334
    if (title) {
slouken@1895
   335
        SDL_stack_free(title);
slouken@1895
   336
    }
slouken@1895
   337
slouken@1951
   338
    if (SetupWindowData(_this, window, hwnd, SDL_FALSE) < 0) {
slouken@1895
   339
        return -1;
slouken@1895
   340
    }
slouken@8144
   341
slouken@8144
   342
#if SDL_VIDEO_OPENGL_WGL
slouken@8144
   343
    {
slouken@8144
   344
        const char *hint = SDL_GetHint(SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT);
slouken@8144
   345
        if (hint) {
slouken@8144
   346
            // This hint is a pointer (in string form) of the address of
slouken@8144
   347
            // the window to share a pixel format with
slouken@8144
   348
            SDL_Window *otherWindow = NULL;
slouken@8144
   349
            SDL_sscanf(hint, "%p", (void**)&otherWindow);
slouken@8144
   350
slouken@8144
   351
            // Do some error checking on the pointer
slouken@8144
   352
            if (otherWindow != NULL && otherWindow->magic == &_this->window_magic)
slouken@8144
   353
            {
slouken@8144
   354
                // If the otherWindow has SDL_WINDOW_OPENGL set, set it for the new window as well
slouken@8144
   355
                if (otherWindow->flags & SDL_WINDOW_OPENGL)
slouken@8144
   356
                {
slouken@8144
   357
                    window->flags |= SDL_WINDOW_OPENGL;
slouken@8144
   358
                    if(!WIN_GL_SetPixelFormatFrom(_this, otherWindow, window)) {
slouken@8144
   359
                        return -1;
slouken@8144
   360
                    }
slouken@8144
   361
                }
slouken@8144
   362
            }
slouken@8144
   363
        }
slouken@8144
   364
    }
slouken@8144
   365
#endif
slouken@1895
   366
    return 0;
slouken@1895
   367
}
slouken@1895
   368
slouken@1895
   369
void
slouken@1895
   370
WIN_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1895
   371
{
slouken@1895
   372
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   373
    LPTSTR title;
slouken@1895
   374
slouken@1895
   375
    if (window->title) {
slouken@1895
   376
        title = WIN_UTF8ToString(window->title);
slouken@1895
   377
    } else {
slouken@1895
   378
        title = NULL;
slouken@1895
   379
    }
slouken@1895
   380
    SetWindowText(hwnd, title ? title : TEXT(""));
slouken@7719
   381
    SDL_free(title);
slouken@1895
   382
}
slouken@1895
   383
slouken@1895
   384
void
slouken@2970
   385
WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2970
   386
{
slouken@2970
   387
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@2970
   388
    HICON hicon = NULL;
slouken@5418
   389
    BYTE *icon_bmp;
slouken@7527
   390
    int icon_len, y;
slouken@5418
   391
    SDL_RWops *dst;
slouken@2970
   392
slouken@5418
   393
    /* Create temporary bitmap buffer */
slouken@5418
   394
    icon_len = 40 + icon->h * icon->w * 4;
slouken@5418
   395
    icon_bmp = SDL_stack_alloc(BYTE, icon_len);
slouken@5418
   396
    dst = SDL_RWFromMem(icon_bmp, icon_len);
slouken@5418
   397
    if (!dst) {
slouken@5418
   398
        SDL_stack_free(icon_bmp);
slouken@5418
   399
        return;
slouken@5418
   400
    }
slouken@2970
   401
slouken@5418
   402
    /* Write the BITMAPINFO header */
slouken@5418
   403
    SDL_WriteLE32(dst, 40);
slouken@5418
   404
    SDL_WriteLE32(dst, icon->w);
slouken@5418
   405
    SDL_WriteLE32(dst, icon->h * 2);
slouken@5418
   406
    SDL_WriteLE16(dst, 1);
slouken@5418
   407
    SDL_WriteLE16(dst, 32);
slouken@5418
   408
    SDL_WriteLE32(dst, BI_RGB);
slouken@5418
   409
    SDL_WriteLE32(dst, icon->h * icon->w * 4);
slouken@5418
   410
    SDL_WriteLE32(dst, 0);
slouken@5418
   411
    SDL_WriteLE32(dst, 0);
slouken@5418
   412
    SDL_WriteLE32(dst, 0);
slouken@5418
   413
    SDL_WriteLE32(dst, 0);
slouken@5418
   414
slouken@7523
   415
    /* Write the pixels upside down into the bitmap buffer */
slouken@7523
   416
    SDL_assert(icon->format->format == SDL_PIXELFORMAT_ARGB8888);
slouken@7527
   417
    y = icon->h;
slouken@7523
   418
    while (y--) {
slouken@7523
   419
        Uint8 *src = (Uint8 *) icon->pixels + y * icon->pitch;
slouken@7523
   420
        SDL_RWwrite(dst, src, icon->pitch, 1);
slouken@7523
   421
    }
slouken@2970
   422
slouken@7523
   423
    hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
slouken@7523
   424
slouken@5418
   425
    SDL_RWclose(dst);
slouken@5418
   426
    SDL_stack_free(icon_bmp);
slouken@2970
   427
slouken@2971
   428
    /* Set the icon for the window */
slouken@2990
   429
    SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);
slouken@2971
   430
slouken@2971
   431
    /* Set the icon in the task manager (should we do this?) */
slouken@2990
   432
    SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) hicon);
slouken@2970
   433
}
slouken@2970
   434
icculus@6427
   435
void
icculus@6427
   436
WIN_SetWindowPosition(_THIS, SDL_Window * window)
icculus@6427
   437
{
slouken@6535
   438
    WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOSIZE | SWP_NOACTIVATE);
slouken@1895
   439
}
slouken@1895
   440
slouken@1895
   441
void
slouken@1895
   442
WIN_SetWindowSize(_THIS, SDL_Window * window)
slouken@1895
   443
{
slouken@6535
   444
    WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOACTIVATE);
slouken@5246
   445
}
slouken@5178
   446
slouken@1895
   447
void
slouken@1895
   448
WIN_ShowWindow(_THIS, SDL_Window * window)
slouken@1895
   449
{
slouken@1895
   450
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   451
    ShowWindow(hwnd, SW_SHOW);
slouken@1895
   452
}
slouken@1895
   453
slouken@1895
   454
void
slouken@1895
   455
WIN_HideWindow(_THIS, SDL_Window * window)
slouken@1895
   456
{
slouken@1895
   457
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   458
    ShowWindow(hwnd, SW_HIDE);
slouken@1895
   459
}
slouken@1895
   460
slouken@1895
   461
void
slouken@1895
   462
WIN_RaiseWindow(_THIS, SDL_Window * window)
slouken@1895
   463
{
slouken@7585
   464
    WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE);
slouken@1895
   465
}
slouken@1895
   466
slouken@1895
   467
void
slouken@1895
   468
WIN_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1895
   469
{
slouken@7941
   470
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@7941
   471
    HWND hwnd = data->hwnd;
slouken@7941
   472
    data->expected_resize = TRUE;
slouken@1895
   473
    ShowWindow(hwnd, SW_MAXIMIZE);
slouken@7941
   474
    data->expected_resize = FALSE;
slouken@1895
   475
}
slouken@1895
   476
slouken@1895
   477
void
slouken@1895
   478
WIN_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1895
   479
{
slouken@1895
   480
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   481
    ShowWindow(hwnd, SW_MINIMIZE);
slouken@1895
   482
}
slouken@1895
   483
slouken@1895
   484
void
icculus@6422
   485
WIN_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
icculus@6422
   486
{
icculus@6422
   487
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
icculus@6428
   488
    DWORD style = GetWindowLong(hwnd, GWL_STYLE);
icculus@6428
   489
icculus@6428
   490
    if (bordered) {
icculus@6428
   491
        style &= ~STYLE_BORDERLESS;
icculus@6428
   492
        style |= STYLE_NORMAL;
icculus@6428
   493
    } else {
icculus@6428
   494
        style &= ~STYLE_NORMAL;
icculus@6428
   495
        style |= STYLE_BORDERLESS;
icculus@6428
   496
    }
icculus@6428
   497
icculus@6428
   498
    SetWindowLong(hwnd, GWL_STYLE, style);
slouken@7585
   499
    WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_FRAMECHANGED | SWP_NOREPOSITION | SWP_NOZORDER |SWP_NOACTIVATE | SWP_NOSENDCHANGING);
icculus@6422
   500
}
icculus@6422
   501
icculus@6422
   502
void
slouken@1895
   503
WIN_RestoreWindow(_THIS, SDL_Window * window)
slouken@1895
   504
{
slouken@7941
   505
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@7941
   506
    HWND hwnd = data->hwnd;
slouken@7941
   507
    data->expected_resize = TRUE;
slouken@1895
   508
    ShowWindow(hwnd, SW_RESTORE);
slouken@7941
   509
    data->expected_resize = FALSE;
slouken@1895
   510
}
slouken@1895
   511
slouken@1895
   512
void
slouken@5305
   513
WIN_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
slouken@5305
   514
{
slouken@5305
   515
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5305
   516
    HWND hwnd = data->hwnd;
slouken@5305
   517
    RECT rect;
slouken@5305
   518
    SDL_Rect bounds;
slouken@5305
   519
    DWORD style;
slouken@5305
   520
    HWND top;
slouken@5305
   521
    BOOL menu;
slouken@5305
   522
    int x, y;
slouken@5305
   523
    int w, h;
slouken@5305
   524
slouken@7585
   525
    if (SDL_ShouldAllowTopmost() && (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) {
slouken@7191
   526
        top = HWND_TOPMOST;
slouken@7191
   527
    } else {
slouken@7191
   528
        top = HWND_NOTOPMOST;
slouken@7191
   529
    }
slouken@6782
   530
slouken@5305
   531
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@5305
   532
    style &= ~STYLE_MASK;
slouken@5305
   533
    style |= GetWindowStyle(window);
slouken@5305
   534
slouken@5305
   535
    WIN_GetDisplayBounds(_this, display, &bounds);
slouken@5305
   536
slouken@5305
   537
    if (fullscreen) {
slouken@5305
   538
        x = bounds.x;
slouken@5305
   539
        y = bounds.y;
slouken@5305
   540
        w = bounds.w;
slouken@5305
   541
        h = bounds.h;
slouken@5305
   542
    } else {
slouken@5305
   543
        rect.left = 0;
slouken@5305
   544
        rect.top = 0;
slouken@5398
   545
        rect.right = window->windowed.w;
slouken@5398
   546
        rect.bottom = window->windowed.h;
slouken@5305
   547
        menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@5305
   548
        AdjustWindowRectEx(&rect, style, menu, 0);
slouken@5305
   549
        w = (rect.right - rect.left);
slouken@5305
   550
        h = (rect.bottom - rect.top);
slouken@5398
   551
        x = window->windowed.x + rect.left;
slouken@5398
   552
        y = window->windowed.y + rect.top;
slouken@5305
   553
    }
slouken@5305
   554
    SetWindowLong(hwnd, GWL_STYLE, style);
slouken@7941
   555
    data->expected_resize = TRUE;
slouken@8205
   556
    SetWindowPos(hwnd, top, x, y, w, h, SWP_NOCOPYBITS | SWP_NOACTIVATE);
slouken@7941
   557
    data->expected_resize = FALSE;
slouken@5305
   558
}
slouken@5305
   559
slouken@5466
   560
int
slouken@5466
   561
WIN_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
slouken@5466
   562
{
slouken@5466
   563
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
   564
    SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
slouken@5466
   565
    HDC hdc;
slouken@5466
   566
    BOOL succeeded = FALSE;
slouken@5466
   567
slouken@5466
   568
    hdc = CreateDC(data->DeviceName, NULL, NULL, NULL);
slouken@5466
   569
    if (hdc) {
slouken@5466
   570
        succeeded = SetDeviceGammaRamp(hdc, (LPVOID)ramp);
slouken@5466
   571
        if (!succeeded) {
slouken@5466
   572
            WIN_SetError("SetDeviceGammaRamp()");
slouken@5466
   573
        }
slouken@5466
   574
        DeleteDC(hdc);
slouken@5466
   575
    }
slouken@5466
   576
    return succeeded ? 0 : -1;
slouken@5466
   577
}
slouken@5466
   578
slouken@5466
   579
int
slouken@5466
   580
WIN_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
slouken@5466
   581
{
slouken@5466
   582
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@5466
   583
    SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
slouken@5466
   584
    HDC hdc;
slouken@5466
   585
    BOOL succeeded = FALSE;
slouken@5466
   586
slouken@5466
   587
    hdc = CreateDC(data->DeviceName, NULL, NULL, NULL);
slouken@5466
   588
    if (hdc) {
slouken@5466
   589
        succeeded = GetDeviceGammaRamp(hdc, (LPVOID)ramp);
slouken@5466
   590
        if (!succeeded) {
slouken@5466
   591
            WIN_SetError("GetDeviceGammaRamp()");
slouken@5466
   592
        }
slouken@5466
   593
        DeleteDC(hdc);
slouken@5466
   594
    }
slouken@5466
   595
    return succeeded ? 0 : -1;
slouken@5466
   596
}
slouken@5466
   597
slouken@5305
   598
void
slouken@6662
   599
WIN_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
slouken@1895
   600
{
slouken@8072
   601
    WIN_UpdateClipCursor(window);
slouken@6782
   602
slouken@7585
   603
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@7584
   604
        UINT flags = SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE;
slouken@6782
   605
slouken@7584
   606
        if (!(window->flags & SDL_WINDOW_SHOWN)) {
slouken@7584
   607
            flags |= SWP_NOACTIVATE;
slouken@7584
   608
        }
slouken@7585
   609
        WIN_SetWindowPositionInternal(_this, window, flags);
slouken@7191
   610
    }
slouken@1895
   611
}
slouken@1895
   612
slouken@1895
   613
void
slouken@1895
   614
WIN_DestroyWindow(_THIS, SDL_Window * window)
slouken@1895
   615
{
slouken@1895
   616
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1895
   617
slouken@1895
   618
    if (data) {
slouken@1913
   619
        ReleaseDC(data->hwnd, data->hdc);
slouken@1895
   620
        if (data->created) {
slouken@1895
   621
            DestroyWindow(data->hwnd);
icculus@5580
   622
        } else {
icculus@5580
   623
            /* Restore any original event handler... */
icculus@5580
   624
            if (data->wndproc != NULL) {
icculus@5580
   625
#ifdef GWLP_WNDPROC
icculus@5581
   626
                SetWindowLongPtr(data->hwnd, GWLP_WNDPROC,
icculus@5581
   627
                                 (LONG_PTR) data->wndproc);
icculus@5580
   628
#else
icculus@5581
   629
                SetWindowLong(data->hwnd, GWL_WNDPROC,
icculus@5581
   630
                              (LONG_PTR) data->wndproc);
icculus@5580
   631
#endif
icculus@5580
   632
            }
slouken@1895
   633
        }
slouken@1895
   634
        SDL_free(data);
slouken@1895
   635
    }
slouken@1895
   636
}
slouken@1895
   637
slouken@1895
   638
SDL_bool
slouken@1895
   639
WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1895
   640
{
slouken@1895
   641
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   642
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
   643
        info->subsystem = SDL_SYSWM_WINDOWS;
slouken@5056
   644
        info->info.win.window = hwnd;
slouken@1895
   645
        return SDL_TRUE;
slouken@1895
   646
    } else {
slouken@1895
   647
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1895
   648
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1895
   649
        return SDL_FALSE;
slouken@1895
   650
    }
slouken@1895
   651
}
slouken@1895
   652
slouken@2713
   653
slouken@2713
   654
/*
slouken@2713
   655
 * Creates a HelperWindow used for DirectInput events.
slouken@2713
   656
 */
slouken@2713
   657
int
slouken@2713
   658
SDL_HelperWindowCreate(void)
slouken@2713
   659
{
slouken@3097
   660
    HINSTANCE hInstance = GetModuleHandle(NULL);
slouken@3097
   661
    WNDCLASS wce;
slouken@2713
   662
bobbens@3045
   663
    /* Make sure window isn't created twice. */
bobbens@3045
   664
    if (SDL_HelperWindow != NULL) {
bobbens@3045
   665
        return 0;
bobbens@3045
   666
    }
bobbens@3045
   667
slouken@2713
   668
    /* Create the class. */
slouken@2714
   669
    SDL_zero(wce);
slouken@3097
   670
    wce.lpfnWndProc = DefWindowProc;
slouken@2713
   671
    wce.lpszClassName = (LPCWSTR) SDL_HelperWindowClassName;
slouken@2713
   672
    wce.hInstance = hInstance;
slouken@2713
   673
slouken@2713
   674
    /* Register the class. */
slouken@3097
   675
    SDL_HelperWindowClass = RegisterClass(&wce);
slouken@2713
   676
    if (SDL_HelperWindowClass == 0) {
icculus@7037
   677
        return WIN_SetError("Unable to create Helper Window Class");
slouken@2713
   678
    }
slouken@2713
   679
slouken@2713
   680
    /* Create the window. */
slouken@2714
   681
    SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,
slouken@2714
   682
                                      SDL_HelperWindowName,
slouken@3168
   683
                                      WS_OVERLAPPED, CW_USEDEFAULT,
slouken@2714
   684
                                      CW_USEDEFAULT, CW_USEDEFAULT,
icculus@6430
   685
                                      CW_USEDEFAULT, HWND_MESSAGE, NULL,
slouken@2714
   686
                                      hInstance, NULL);
slouken@2713
   687
    if (SDL_HelperWindow == NULL) {
bobbens@3045
   688
        UnregisterClass(SDL_HelperWindowClassName, hInstance);
icculus@7037
   689
        return WIN_SetError("Unable to create Helper Window");
slouken@2713
   690
    }
slouken@2713
   691
slouken@2713
   692
    return 0;
slouken@2713
   693
}
slouken@2713
   694
slouken@2713
   695
slouken@2713
   696
/*
slouken@2713
   697
 * Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
slouken@2713
   698
 */
slouken@2713
   699
void
slouken@2713
   700
SDL_HelperWindowDestroy(void)
slouken@2713
   701
{
slouken@3097
   702
    HINSTANCE hInstance = GetModuleHandle(NULL);
bobbens@2863
   703
slouken@2713
   704
    /* Destroy the window. */
bobbens@2863
   705
    if (SDL_HelperWindow != NULL) {
bobbens@2863
   706
        if (DestroyWindow(SDL_HelperWindow) == 0) {
slouken@4500
   707
            WIN_SetError("Unable to destroy Helper Window");
slouken@2865
   708
            return;
bobbens@2863
   709
        }
slouken@2713
   710
        SDL_HelperWindow = NULL;
slouken@2713
   711
    }
slouken@2713
   712
slouken@2713
   713
    /* Unregister the class. */
bobbens@2863
   714
    if (SDL_HelperWindowClass != 0) {
bobbens@2863
   715
        if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
slouken@4500
   716
            WIN_SetError("Unable to destroy Helper Window Class");
slouken@2865
   717
            return;
bobbens@2863
   718
        }
slouken@2713
   719
        SDL_HelperWindowClass = 0;
slouken@2713
   720
    }
slouken@2713
   721
}
slouken@2713
   722
jorgen@7089
   723
void WIN_OnWindowEnter(_THIS, SDL_Window * window)
jorgen@7089
   724
{
jorgen@7089
   725
#ifdef WM_MOUSELEAVE
jorgen@7089
   726
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
jorgen@7089
   727
    TRACKMOUSEEVENT trackMouseEvent;
jorgen@7089
   728
jorgen@7089
   729
    if (!data || !data->hwnd) {
jorgen@7089
   730
        /* The window wasn't fully initialized */
jorgen@7089
   731
        return;
jorgen@7089
   732
    }
jorgen@7089
   733
jorgen@7089
   734
    trackMouseEvent.cbSize = sizeof(TRACKMOUSEEVENT);
jorgen@7089
   735
    trackMouseEvent.dwFlags = TME_LEAVE;
jorgen@7089
   736
    trackMouseEvent.hwndTrack = data->hwnd;
jorgen@7089
   737
jorgen@7089
   738
    TrackMouseEvent(&trackMouseEvent);
jorgen@7089
   739
#endif /* WM_MOUSELEAVE */
jorgen@7089
   740
}
jorgen@7089
   741
slouken@8072
   742
void
slouken@8072
   743
WIN_UpdateClipCursor(SDL_Window *window)
slouken@8072
   744
{
slouken@8072
   745
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@8072
   746
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@8072
   747
slouken@8072
   748
    /* Don't clip the cursor while we're in the modal resize or move loop */
slouken@8072
   749
    if (data->in_modal_loop) {
slouken@8072
   750
        ClipCursor(NULL);
slouken@8072
   751
        return;
slouken@8072
   752
    }
slouken@8072
   753
slouken@8072
   754
    if ((mouse->relative_mode || (window->flags & SDL_WINDOW_INPUT_GRABBED)) &&
slouken@8072
   755
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@8072
   756
        if (mouse->relative_mode && !mouse->relative_mode_warp) {
slouken@8072
   757
            LONG cx, cy;
slouken@8072
   758
            RECT rect;
slouken@8072
   759
            GetWindowRect(data->hwnd, &rect);
slouken@8072
   760
slouken@8072
   761
            cx = (rect.left + rect.right) / 2;
slouken@8072
   762
            cy = (rect.top + rect.bottom) / 2;
slouken@8072
   763
slouken@8072
   764
            /* Make an absurdly small clip rect */
slouken@8072
   765
            rect.left = cx - 1;
slouken@8072
   766
            rect.right = cx + 1;
slouken@8072
   767
            rect.top = cy - 1;
slouken@8072
   768
            rect.bottom = cy + 1;
slouken@8072
   769
slouken@8072
   770
            ClipCursor(&rect);
slouken@8072
   771
        } else {
slouken@8072
   772
            RECT rect;
slouken@8072
   773
            if (GetClientRect(data->hwnd, &rect) && !IsRectEmpty(&rect)) {
slouken@8072
   774
                ClientToScreen(data->hwnd, (LPPOINT) & rect);
slouken@8072
   775
                ClientToScreen(data->hwnd, (LPPOINT) & rect + 1);
slouken@8072
   776
                ClipCursor(&rect);
slouken@8072
   777
            }
slouken@8072
   778
        }
slouken@8072
   779
    } else {
slouken@8072
   780
        ClipCursor(NULL);
slouken@8072
   781
    }
slouken@8072
   782
}
slouken@8072
   783
slouken@6044
   784
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
slouken@6044
   785
slouken@1895
   786
/* vi: set ts=4 sw=4 expandtab: */