src/video/windows/SDL_windowswindow.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 01 Aug 2013 00:27:22 -0400
changeset 7559 0dd3b05797f9
parent 7556 574f465da18a
child 7583 7e053ad01aa3
permissions -rw-r--r--
Move the fix for Bugzilla #1395 into WIN_GL_SetupWindow() directly.

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