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