src/video/windows/SDL_windowswindow.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 24 Nov 2013 23:56:17 -0500
changeset 8093 b43765095a6f
parent 8072 9d8865351afd
child 8144 855b1d6d7d8e
permissions -rw-r--r--
Make internal SDL sources include SDL_internal.h instead of SDL_config.h

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