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