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