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