src/video/windows/SDL_windowswindow.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 10 Feb 2011 14:44:25 -0800
changeset 5246 58265e606e4e
parent 5178 4191af605cb0
child 5262 b530ef003506
permissions -rw-r--r--
Window coordinates are in the global space and windows are not tied to a particular display.
Also added Ctrl-Enter keybinding to the test code to toggle fullscreen mode for testing.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@1895
    24
#include "../SDL_sysvideo.h"
slouken@2970
    25
#include "../SDL_pixels_c.h"
slouken@1895
    26
#include "../../events/SDL_keyboard_c.h"
slouken@1895
    27
slouken@5062
    28
#include "SDL_windowsvideo.h"
slouken@5062
    29
#include "SDL_windowswindow.h"
slouken@1895
    30
slouken@5062
    31
/* This is included after SDL_windowsvideo.h, which includes windows.h */
slouken@1895
    32
#include "SDL_syswm.h"
slouken@3168
    33
slouken@5086
    34
/* Windows CE compatibility */
slouken@5086
    35
#ifndef SWP_NOCOPYBITS
slouken@5086
    36
#define SWP_NOCOPYBITS 0
slouken@5086
    37
#endif
slouken@3168
    38
slouken@2713
    39
/* Fake window to help with DirectInput events. */
slouken@2713
    40
HWND SDL_HelperWindow = NULL;
slouken@2714
    41
static WCHAR *SDL_HelperWindowClassName = TEXT("SDLHelperWindowInputCatcher");
slouken@2714
    42
static WCHAR *SDL_HelperWindowName = TEXT("SDLHelperWindowInputMsgWindow");
slouken@2713
    43
static ATOM SDL_HelperWindowClass = 0;
slouken@2713
    44
slouken@1895
    45
static int
slouken@1951
    46
SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created)
slouken@1895
    47
{
slouken@1951
    48
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@5246
    49
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@1895
    50
    SDL_WindowData *data;
slouken@1895
    51
slouken@1895
    52
    /* Allocate the window data */
slouken@1895
    53
    data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
slouken@1895
    54
    if (!data) {
slouken@1895
    55
        SDL_OutOfMemory();
slouken@1895
    56
        return -1;
slouken@1895
    57
    }
slouken@3685
    58
    data->window = window;
slouken@1895
    59
    data->hwnd = hwnd;
slouken@1913
    60
    data->hdc = GetDC(hwnd);
slouken@1895
    61
    data->created = created;
slouken@1895
    62
    data->mouse_pressed = SDL_FALSE;
slouken@1951
    63
    data->videodata = videodata;
slouken@1895
    64
slouken@1895
    65
    /* Associate the data with the window */
slouken@1895
    66
    if (!SetProp(hwnd, TEXT("SDL_WindowData"), data)) {
slouken@1913
    67
        ReleaseDC(hwnd, data->hdc);
slouken@1895
    68
        SDL_free(data);
slouken@1895
    69
        WIN_SetError("SetProp() failed");
slouken@1895
    70
        return -1;
slouken@1895
    71
    }
slouken@1895
    72
slouken@1895
    73
    /* Set up the window proc function */
slouken@5086
    74
#ifdef GWLP_WNDPROC
slouken@1895
    75
    data->wndproc = (WNDPROC) GetWindowLongPtr(hwnd, GWLP_WNDPROC);
dewyatt@4733
    76
    if (data->wndproc == WIN_WindowProc) {
slouken@3566
    77
        data->wndproc = NULL;
slouken@5086
    78
    } else {
dewyatt@4735
    79
        SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) WIN_WindowProc);
dewyatt@4735
    80
    }
slouken@5086
    81
#else
slouken@5086
    82
    data->wndproc = (WNDPROC) GetWindowLong(hwnd, GWL_WNDPROC);
slouken@5086
    83
    if (data->wndproc == WIN_WindowProc) {
slouken@5086
    84
        data->wndproc = NULL;
slouken@5086
    85
    } else {
slouken@5086
    86
        SetWindowLong(hwnd, GWL_WNDPROC, (LONG_PTR) WIN_WindowProc);
slouken@5086
    87
    }
slouken@5086
    88
#endif
slouken@1895
    89
slouken@1895
    90
    /* Fill in the SDL window with the window data */
slouken@1895
    91
    {
slouken@1895
    92
        POINT point;
slouken@1895
    93
        point.x = 0;
slouken@1895
    94
        point.y = 0;
slouken@1895
    95
        if (ClientToScreen(hwnd, &point)) {
slouken@5246
    96
            window->x = point.x;
slouken@5246
    97
            window->y = point.y;
slouken@1895
    98
        }
slouken@1895
    99
    }
slouken@1895
   100
    {
slouken@1895
   101
        RECT rect;
slouken@1895
   102
        if (GetClientRect(hwnd, &rect)) {
slouken@1895
   103
            window->w = rect.right;
slouken@1895
   104
            window->h = rect.bottom;
slouken@1895
   105
        }
slouken@1895
   106
    }
slouken@1895
   107
    {
slouken@1895
   108
        DWORD style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   109
        if (style & WS_VISIBLE) {
slouken@1895
   110
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1895
   111
        } else {
slouken@1895
   112
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1895
   113
        }
slouken@1895
   114
        if (style & (WS_BORDER | WS_THICKFRAME)) {
slouken@1895
   115
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1895
   116
        } else {
slouken@1895
   117
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1895
   118
        }
slouken@1895
   119
        if (style & WS_THICKFRAME) {
slouken@1895
   120
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1895
   121
        } else {
slouken@1895
   122
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1895
   123
        }
slouken@5086
   124
#ifdef WS_MAXIMIZE
slouken@1895
   125
        if (style & WS_MAXIMIZE) {
slouken@1895
   126
            window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@5086
   127
        } else
slouken@5086
   128
#endif
slouken@5086
   129
        {
slouken@1895
   130
            window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1895
   131
        }
slouken@5086
   132
#ifdef WS_MINIMIZE
slouken@1895
   133
        if (style & WS_MINIMIZE) {
slouken@1895
   134
            window->flags |= SDL_WINDOW_MINIMIZED;
slouken@5086
   135
        } else
slouken@5086
   136
#endif
slouken@5086
   137
        {
slouken@1895
   138
            window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1895
   139
        }
slouken@1895
   140
    }
slouken@1895
   141
    if (GetFocus() == hwnd) {
slouken@1895
   142
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@4465
   143
        SDL_SetKeyboardFocus(data->window);
slouken@1895
   144
slouken@1895
   145
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1895
   146
            RECT rect;
slouken@1895
   147
            GetClientRect(hwnd, &rect);
slouken@1895
   148
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   149
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   150
            ClipCursor(&rect);
slouken@1895
   151
        }
slouken@1895
   152
    }
slouken@1895
   153
slouken@4919
   154
	/* Enable multi-touch */
slouken@4948
   155
    if (videodata->RegisterTouchWindow) {
slouken@4948
   156
        videodata->RegisterTouchWindow(hwnd, (TWF_FINETOUCH|TWF_WANTPALM));
slouken@4948
   157
    }
slouken@4919
   158
slouken@1895
   159
    /* All done! */
slouken@1895
   160
    window->driverdata = data;
slouken@1895
   161
    return 0;
slouken@1895
   162
}
slouken@1895
   163
slouken@1895
   164
int
slouken@1895
   165
WIN_CreateWindow(_THIS, SDL_Window * window)
slouken@1895
   166
{
slouken@5246
   167
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@1895
   168
    HWND hwnd;
slouken@1895
   169
    RECT rect;
slouken@3528
   170
    SDL_Rect bounds;
slouken@1913
   171
    DWORD style = (WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
slouken@1895
   172
    int x, y;
slouken@1895
   173
    int w, h;
slouken@1895
   174
slouken@2876
   175
    if (window->flags & (SDL_WINDOW_BORDERLESS | SDL_WINDOW_FULLSCREEN)) {
slouken@1895
   176
        style |= WS_POPUP;
slouken@1895
   177
    } else {
slouken@1895
   178
        style |= (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX);
slouken@1895
   179
    }
slouken@2876
   180
    if ((window->flags & SDL_WINDOW_RESIZABLE)
slouken@2876
   181
        && !(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@1895
   182
        style |= (WS_THICKFRAME | WS_MAXIMIZEBOX);
slouken@1895
   183
    }
slouken@1895
   184
slouken@1895
   185
    /* Figure out what the window area will be */
slouken@1895
   186
    rect.left = 0;
slouken@1895
   187
    rect.top = 0;
slouken@1895
   188
    rect.right = window->w;
slouken@1895
   189
    rect.bottom = window->h;
slouken@1895
   190
    AdjustWindowRectEx(&rect, style, FALSE, 0);
slouken@1895
   191
    w = (rect.right - rect.left);
slouken@1895
   192
    h = (rect.bottom - rect.top);
slouken@1895
   193
slouken@3528
   194
    WIN_GetDisplayBounds(_this, display, &bounds);
slouken@4486
   195
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@4486
   196
        /* The bounds when this window is visible is the fullscreen mode */
slouken@4486
   197
        SDL_DisplayMode fullscreen_mode;
slouken@4486
   198
        if (SDL_GetWindowDisplayMode(window, &fullscreen_mode) == 0) {
slouken@4486
   199
            bounds.w = fullscreen_mode.w;
slouken@4486
   200
            bounds.h = fullscreen_mode.h;
slouken@4486
   201
        }
slouken@4486
   202
    }
slouken@2876
   203
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@5246
   204
        || SDL_WINDOWPOS_ISCENTERED(window->x)) {
slouken@4486
   205
        x = bounds.x + (bounds.w - w) / 2;
slouken@5246
   206
    } else if (SDL_WINDOWPOS_ISUNDEFINED(window->x)) {
slouken@3528
   207
        if (bounds.x == 0) {
slouken@3528
   208
            x = CW_USEDEFAULT;
slouken@3528
   209
        } else {
slouken@3528
   210
            x = bounds.x;
slouken@3528
   211
        }
slouken@1895
   212
    } else {
slouken@5246
   213
        x = window->x + rect.left;
slouken@1895
   214
    }
slouken@2876
   215
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@5246
   216
        || SDL_WINDOWPOS_ISCENTERED(window->y)) {
slouken@4486
   217
        y = bounds.y + (bounds.h - h) / 2;
slouken@5246
   218
    } else if (SDL_WINDOWPOS_ISUNDEFINED(window->x)) {
slouken@3528
   219
        if (bounds.x == 0) {
slouken@3528
   220
            y = CW_USEDEFAULT;
slouken@3528
   221
        } else {
slouken@3528
   222
            y = bounds.y;
slouken@3528
   223
        }
slouken@1895
   224
    } else {
slouken@5246
   225
        y = window->y + rect.top;
slouken@1895
   226
    }
slouken@1895
   227
slouken@1956
   228
    hwnd =
slouken@1956
   229
        CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, NULL, NULL,
slouken@1956
   230
                     SDL_Instance, NULL);
slouken@1895
   231
    if (!hwnd) {
slouken@1895
   232
        WIN_SetError("Couldn't create window");
slouken@1895
   233
        return -1;
slouken@1895
   234
    }
jimtla@4650
   235
	//RegisterTouchWindow(hwnd, 0);
slouken@1895
   236
slouken@2710
   237
    WIN_PumpEvents(_this);
slouken@2710
   238
slouken@1951
   239
    if (SetupWindowData(_this, window, hwnd, SDL_TRUE) < 0) {
slouken@1895
   240
        DestroyWindow(hwnd);
slouken@1895
   241
        return -1;
slouken@1895
   242
    }
slouken@5088
   243
#if SDL_VIDEO_OPENGL_WGL
slouken@1913
   244
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1913
   245
        if (WIN_GL_SetupWindow(_this, window) < 0) {
slouken@1913
   246
            WIN_DestroyWindow(_this, window);
slouken@1913
   247
            return -1;
slouken@1913
   248
        }
slouken@1913
   249
    }
slouken@1913
   250
#endif
slouken@1895
   251
    return 0;
slouken@1895
   252
}
slouken@1895
   253
slouken@1895
   254
int
slouken@1895
   255
WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1895
   256
{
slouken@1895
   257
    HWND hwnd = (HWND) data;
slouken@1895
   258
    LPTSTR title;
slouken@1895
   259
    int titleLen;
slouken@1895
   260
slouken@1895
   261
    /* Query the title from the existing window */
slouken@1895
   262
    titleLen = GetWindowTextLength(hwnd);
slouken@1895
   263
    title = SDL_stack_alloc(TCHAR, titleLen + 1);
slouken@1895
   264
    if (title) {
slouken@1895
   265
        titleLen = GetWindowText(hwnd, title, titleLen);
slouken@1895
   266
    } else {
slouken@1895
   267
        titleLen = 0;
slouken@1895
   268
    }
slouken@1895
   269
    if (titleLen > 0) {
slouken@1895
   270
        window->title = WIN_StringToUTF8(title);
slouken@1895
   271
    }
slouken@1895
   272
    if (title) {
slouken@1895
   273
        SDL_stack_free(title);
slouken@1895
   274
    }
slouken@1895
   275
slouken@1951
   276
    if (SetupWindowData(_this, window, hwnd, SDL_FALSE) < 0) {
slouken@1895
   277
        return -1;
slouken@1895
   278
    }
slouken@1895
   279
    return 0;
slouken@1895
   280
}
slouken@1895
   281
slouken@1895
   282
void
slouken@1895
   283
WIN_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1895
   284
{
slouken@1895
   285
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   286
    LPTSTR title;
slouken@1895
   287
slouken@1895
   288
    if (window->title) {
slouken@1895
   289
        title = WIN_UTF8ToString(window->title);
slouken@1895
   290
    } else {
slouken@1895
   291
        title = NULL;
slouken@1895
   292
    }
slouken@1895
   293
    SetWindowText(hwnd, title ? title : TEXT(""));
slouken@1895
   294
    if (title) {
slouken@1895
   295
        SDL_free(title);
slouken@1895
   296
    }
slouken@1895
   297
}
slouken@1895
   298
slouken@1895
   299
void
slouken@2970
   300
WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2970
   301
{
slouken@2970
   302
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@2970
   303
    HICON hicon = NULL;
slouken@2970
   304
slouken@2990
   305
    if (icon) {
slouken@2970
   306
        BYTE *icon_bmp;
slouken@2970
   307
        int icon_len;
slouken@2970
   308
        SDL_RWops *dst;
slouken@2970
   309
        SDL_PixelFormat format;
slouken@2970
   310
        SDL_Surface *surface;
slouken@2970
   311
slouken@2970
   312
        /* Create temporary bitmap buffer */
slouken@2970
   313
        icon_len = 40 + icon->h * icon->w * 4;
slouken@2970
   314
        icon_bmp = SDL_stack_alloc(BYTE, icon_len);
slouken@2970
   315
        dst = SDL_RWFromMem(icon_bmp, icon_len);
slouken@2970
   316
        if (!dst) {
slouken@2970
   317
            SDL_stack_free(icon_bmp);
slouken@2970
   318
            return;
slouken@2970
   319
        }
slouken@2970
   320
slouken@2970
   321
        /* Write the BITMAPINFO header */
slouken@2970
   322
        SDL_WriteLE32(dst, 40);
slouken@2970
   323
        SDL_WriteLE32(dst, icon->w);
slouken@2990
   324
        SDL_WriteLE32(dst, icon->h * 2);
slouken@2970
   325
        SDL_WriteLE16(dst, 1);
slouken@2970
   326
        SDL_WriteLE16(dst, 32);
slouken@2970
   327
        SDL_WriteLE32(dst, BI_RGB);
slouken@2970
   328
        SDL_WriteLE32(dst, icon->h * icon->w * 4);
slouken@2970
   329
        SDL_WriteLE32(dst, 0);
slouken@2970
   330
        SDL_WriteLE32(dst, 0);
slouken@2970
   331
        SDL_WriteLE32(dst, 0);
slouken@2970
   332
        SDL_WriteLE32(dst, 0);
slouken@2970
   333
slouken@2970
   334
        /* Convert the icon to a 32-bit surface with alpha channel */
slouken@2970
   335
        SDL_InitFormat(&format, 32,
slouken@2970
   336
                       0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
slouken@2970
   337
        surface = SDL_ConvertSurface(icon, &format, 0);
slouken@2970
   338
        if (surface) {
slouken@2970
   339
            /* Write the pixels upside down into the bitmap buffer */
slouken@2970
   340
            int y = surface->h;
slouken@2970
   341
            while (y--) {
slouken@2990
   342
                Uint8 *src = (Uint8 *) surface->pixels + y * surface->pitch;
slouken@2970
   343
                SDL_RWwrite(dst, src, surface->pitch, 1);
slouken@2970
   344
            }
slouken@2970
   345
            SDL_FreeSurface(surface);
slouken@2970
   346
slouken@3097
   347
/* TODO: create the icon in WinCE (CreateIconFromResource isn't available) */
slouken@3097
   348
#ifndef _WIN32_WCE
slouken@2990
   349
            hicon =
slouken@2990
   350
                CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
slouken@3097
   351
#endif
slouken@2970
   352
        }
slouken@2970
   353
        SDL_RWclose(dst);
slouken@2970
   354
        SDL_stack_free(icon_bmp);
slouken@2970
   355
    }
slouken@2970
   356
slouken@2971
   357
    /* Set the icon for the window */
slouken@2990
   358
    SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);
slouken@2971
   359
slouken@2971
   360
    /* Set the icon in the task manager (should we do this?) */
slouken@2990
   361
    SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) hicon);
slouken@2970
   362
}
slouken@2970
   363
slouken@2970
   364
void
slouken@1895
   365
WIN_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1895
   366
{
slouken@5246
   367
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@1895
   368
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   369
    RECT rect;
slouken@3528
   370
    SDL_Rect bounds;
slouken@1895
   371
    DWORD style;
slouken@1895
   372
    HWND top;
slouken@3168
   373
    BOOL menu;
slouken@1895
   374
    int x, y;
slouken@4486
   375
    int w, h;
slouken@1895
   376
slouken@1895
   377
    /* Figure out what the window area will be */
slouken@1895
   378
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   379
        top = HWND_TOPMOST;
slouken@1895
   380
    } else {
slouken@1895
   381
        top = HWND_NOTOPMOST;
slouken@1895
   382
    }
slouken@1895
   383
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   384
    rect.left = 0;
slouken@1895
   385
    rect.top = 0;
slouken@1895
   386
    rect.right = window->w;
slouken@1895
   387
    rect.bottom = window->h;
slouken@3168
   388
#ifdef _WIN32_WCE
slouken@3168
   389
    menu = FALSE;
slouken@3168
   390
#else
slouken@3168
   391
    menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   392
#endif
slouken@3168
   393
    AdjustWindowRectEx(&rect, style, menu, 0);
slouken@4486
   394
    w = (rect.right - rect.left);
slouken@4486
   395
    h = (rect.bottom - rect.top);
slouken@2875
   396
slouken@3528
   397
    WIN_GetDisplayBounds(_this, display, &bounds);
slouken@4486
   398
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@4486
   399
        /* The bounds when this window is visible is the fullscreen mode */
slouken@4486
   400
        SDL_DisplayMode fullscreen_mode;
slouken@4486
   401
        if (SDL_GetWindowDisplayMode(window, &fullscreen_mode) == 0) {
slouken@4486
   402
            bounds.w = fullscreen_mode.w;
slouken@4486
   403
            bounds.h = fullscreen_mode.h;
slouken@4486
   404
        }
slouken@4486
   405
    }
slouken@2876
   406
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@5246
   407
        || SDL_WINDOWPOS_ISCENTERED(window->x)) {
slouken@4486
   408
        x = bounds.x + (bounds.w - w) / 2;
slouken@2875
   409
    } else {
slouken@5246
   410
        x = window->x + rect.left;
slouken@2875
   411
    }
slouken@2876
   412
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@5246
   413
        || SDL_WINDOWPOS_ISCENTERED(window->y)) {
slouken@4486
   414
        y = bounds.y + (bounds.h - h) / 2;
slouken@2875
   415
    } else {
slouken@5246
   416
        y = window->y + rect.top;
slouken@2875
   417
    }
slouken@1895
   418
slouken@1956
   419
    SetWindowPos(hwnd, top, x, y, 0, 0, (SWP_NOCOPYBITS | SWP_NOSIZE));
slouken@1895
   420
}
slouken@1895
   421
slouken@1895
   422
void
slouken@1895
   423
WIN_SetWindowSize(_THIS, SDL_Window * window)
slouken@1895
   424
{
slouken@1895
   425
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   426
    RECT rect;
slouken@1895
   427
    DWORD style;
slouken@1895
   428
    HWND top;
slouken@3168
   429
    BOOL menu;
slouken@1895
   430
    int w, h;
slouken@1895
   431
slouken@1895
   432
    /* Figure out what the window area will be */
slouken@1895
   433
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   434
        top = HWND_TOPMOST;
slouken@1895
   435
    } else {
slouken@1895
   436
        top = HWND_NOTOPMOST;
slouken@1895
   437
    }
slouken@1895
   438
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   439
    rect.left = 0;
slouken@1895
   440
    rect.top = 0;
slouken@1895
   441
    rect.right = window->w;
slouken@1895
   442
    rect.bottom = window->h;
slouken@3168
   443
#ifdef _WIN32_WCE
slouken@3168
   444
    menu = FALSE;
slouken@3168
   445
#else
slouken@3168
   446
    menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   447
#endif
slouken@3168
   448
    AdjustWindowRectEx(&rect, style, menu, 0);
slouken@1895
   449
    w = (rect.right - rect.left);
slouken@1895
   450
    h = (rect.bottom - rect.top);
slouken@1895
   451
slouken@2968
   452
    SetWindowPos(hwnd, top, 0, 0, w, h, (SWP_NOCOPYBITS | SWP_NOMOVE));
slouken@5246
   453
}
slouken@5246
   454
slouken@5246
   455
#ifdef _WIN32_WCE
slouken@5246
   456
void WINCE_ShowWindow(_THIS, SDL_Window* window, int visible)
slouken@5246
   457
{
slouken@5246
   458
    SDL_WindowData* windowdata = (SDL_WindowData*) window->driverdata;
slouken@5246
   459
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@5246
   460
slouken@5246
   461
    if(visible) {
slouken@5246
   462
        if(window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@5246
   463
            if(videodata->SHFullScreen)
slouken@5246
   464
                videodata->SHFullScreen(windowdata->hwnd, SHFS_HIDETASKBAR | SHFS_HIDESTARTICON | SHFS_HIDESIPBUTTON);
slouken@5178
   465
slouken@5246
   466
            ShowWindow(FindWindow(TEXT("HHTaskBar"), NULL), SW_HIDE);
slouken@5246
   467
        }
slouken@5246
   468
slouken@5246
   469
        ShowWindow(windowdata->hwnd, SW_SHOW);
slouken@5246
   470
        SetForegroundWindow(windowdata->hwnd);
slouken@5246
   471
    } else {
slouken@5246
   472
        ShowWindow(windowdata->hwnd, SW_HIDE);
slouken@5246
   473
slouken@5246
   474
        if(window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@5246
   475
            if(videodata->SHFullScreen)
slouken@5246
   476
                videodata->SHFullScreen(windowdata->hwnd, SHFS_SHOWTASKBAR | SHFS_SHOWSTARTICON | SHFS_SHOWSIPBUTTON);
slouken@5246
   477
slouken@5246
   478
            ShowWindow(FindWindow(TEXT("HHTaskBar"), NULL), SW_SHOW);
slouken@5246
   479
slouken@5246
   480
        }
slouken@5246
   481
    }
slouken@1895
   482
}
slouken@5178
   483
#endif /* _WIN32_WCE */
slouken@1895
   484
slouken@1895
   485
void
slouken@1895
   486
WIN_ShowWindow(_THIS, SDL_Window * window)
slouken@1895
   487
{
slouken@4569
   488
#ifdef _WIN32_WCE
slouken@4569
   489
    WINCE_ShowWindow(_this, window, 1);
slouken@4569
   490
#else
slouken@1895
   491
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   492
    ShowWindow(hwnd, SW_SHOW);
slouken@3168
   493
#endif
slouken@1895
   494
}
slouken@1895
   495
slouken@1895
   496
void
slouken@1895
   497
WIN_HideWindow(_THIS, SDL_Window * window)
slouken@1895
   498
{
slouken@4569
   499
#ifdef _WIN32_WCE
slouken@4569
   500
    WINCE_ShowWindow(_this, window, 0);
slouken@4569
   501
#else
slouken@1895
   502
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   503
    ShowWindow(hwnd, SW_HIDE);
slouken@3168
   504
#endif
slouken@1895
   505
}
slouken@1895
   506
slouken@1895
   507
void
slouken@1895
   508
WIN_RaiseWindow(_THIS, SDL_Window * window)
slouken@1895
   509
{
slouken@1895
   510
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   511
    HWND top;
slouken@1895
   512
slouken@1895
   513
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   514
        top = HWND_TOPMOST;
slouken@1895
   515
    } else {
slouken@1895
   516
        top = HWND_NOTOPMOST;
slouken@1895
   517
    }
slouken@1895
   518
    SetWindowPos(hwnd, top, 0, 0, 0, 0, (SWP_NOMOVE | SWP_NOSIZE));
slouken@1895
   519
}
slouken@1895
   520
slouken@1895
   521
void
slouken@1895
   522
WIN_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1895
   523
{
slouken@1895
   524
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@4569
   525
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@3168
   526
slouken@3168
   527
#ifdef _WIN32_WCE
slouken@4569
   528
    if((window->flags & SDL_WINDOW_FULLSCREEN) && videodata->SHFullScreen)
slouken@4569
   529
        videodata->SHFullScreen(hwnd, SHFS_HIDETASKBAR | SHFS_HIDESTARTICON | SHFS_HIDESIPBUTTON);
slouken@3168
   530
#endif
slouken@4569
   531
slouken@4569
   532
    ShowWindow(hwnd, SW_MAXIMIZE);
slouken@1895
   533
}
slouken@1895
   534
slouken@1895
   535
void
slouken@1895
   536
WIN_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1895
   537
{
slouken@1895
   538
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@4569
   539
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1895
   540
slouken@1895
   541
    ShowWindow(hwnd, SW_MINIMIZE);
slouken@3168
   542
slouken@3168
   543
#ifdef _WIN32_WCE
slouken@4569
   544
    if((window->flags & SDL_WINDOW_FULLSCREEN) && videodata->SHFullScreen)
slouken@4569
   545
	videodata->SHFullScreen(hwnd, SHFS_SHOWTASKBAR | SHFS_SHOWSTARTICON | SHFS_SHOWSIPBUTTON);
slouken@3168
   546
#endif
slouken@1895
   547
}
slouken@1895
   548
slouken@1895
   549
void
slouken@1895
   550
WIN_RestoreWindow(_THIS, SDL_Window * window)
slouken@1895
   551
{
slouken@1895
   552
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   553
slouken@1895
   554
    ShowWindow(hwnd, SW_RESTORE);
slouken@1895
   555
}
slouken@1895
   556
slouken@1895
   557
void
slouken@1895
   558
WIN_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1895
   559
{
slouken@1895
   560
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   561
slouken@2876
   562
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
slouken@2876
   563
        && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
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@1895
   572
}
slouken@1895
   573
slouken@1895
   574
void
slouken@1895
   575
WIN_DestroyWindow(_THIS, SDL_Window * window)
slouken@1895
   576
{
slouken@1895
   577
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1895
   578
slouken@1895
   579
    if (data) {
slouken@4569
   580
#ifdef _WIN32_WCE
slouken@4569
   581
	WINCE_ShowWindow(_this, window, 0);
slouken@4569
   582
#endif
slouken@1913
   583
        ReleaseDC(data->hwnd, data->hdc);
slouken@1895
   584
        if (data->created) {
slouken@1895
   585
            DestroyWindow(data->hwnd);
slouken@1895
   586
        }
slouken@1895
   587
        SDL_free(data);
slouken@1895
   588
    }
slouken@1895
   589
}
slouken@1895
   590
slouken@1895
   591
SDL_bool
slouken@1895
   592
WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1895
   593
{
slouken@1895
   594
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   595
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
   596
        info->subsystem = SDL_SYSWM_WINDOWS;
slouken@5056
   597
        info->info.win.window = hwnd;
slouken@1895
   598
        return SDL_TRUE;
slouken@1895
   599
    } else {
slouken@1895
   600
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1895
   601
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1895
   602
        return SDL_FALSE;
slouken@1895
   603
    }
slouken@1895
   604
}
slouken@1895
   605
slouken@2713
   606
slouken@2713
   607
/*
slouken@2713
   608
 * Creates a HelperWindow used for DirectInput events.
slouken@2713
   609
 */
slouken@2713
   610
int
slouken@2713
   611
SDL_HelperWindowCreate(void)
slouken@2713
   612
{
slouken@3097
   613
    HINSTANCE hInstance = GetModuleHandle(NULL);
slouken@3097
   614
    WNDCLASS wce;
slouken@3196
   615
    HWND hWndParent = NULL;
slouken@2713
   616
bobbens@3045
   617
    /* Make sure window isn't created twice. */
bobbens@3045
   618
    if (SDL_HelperWindow != NULL) {
bobbens@3045
   619
        return 0;
bobbens@3045
   620
    }
bobbens@3045
   621
slouken@2713
   622
    /* Create the class. */
slouken@2714
   623
    SDL_zero(wce);
slouken@3097
   624
    wce.lpfnWndProc = DefWindowProc;
slouken@2713
   625
    wce.lpszClassName = (LPCWSTR) SDL_HelperWindowClassName;
slouken@2713
   626
    wce.hInstance = hInstance;
slouken@2713
   627
slouken@2713
   628
    /* Register the class. */
slouken@3097
   629
    SDL_HelperWindowClass = RegisterClass(&wce);
slouken@2713
   630
    if (SDL_HelperWindowClass == 0) {
slouken@4500
   631
        WIN_SetError("Unable to create Helper Window Class");
slouken@2713
   632
        return -1;
slouken@2713
   633
    }
slouken@2713
   634
slouken@3168
   635
#ifndef _WIN32_WCE
slouken@3168
   636
    /* WinCE doesn't have HWND_MESSAGE */
slouken@3168
   637
    hWndParent = HWND_MESSAGE;
slouken@3168
   638
#endif
slouken@3168
   639
slouken@2713
   640
    /* Create the window. */
slouken@2714
   641
    SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,
slouken@2714
   642
                                      SDL_HelperWindowName,
slouken@3168
   643
                                      WS_OVERLAPPED, CW_USEDEFAULT,
slouken@2714
   644
                                      CW_USEDEFAULT, CW_USEDEFAULT,
slouken@3168
   645
                                      CW_USEDEFAULT, hWndParent, NULL,
slouken@2714
   646
                                      hInstance, NULL);
slouken@2713
   647
    if (SDL_HelperWindow == NULL) {
bobbens@3045
   648
        UnregisterClass(SDL_HelperWindowClassName, hInstance);
slouken@4500
   649
        WIN_SetError("Unable to create Helper Window");
slouken@2713
   650
        return -1;
slouken@2713
   651
    }
slouken@2713
   652
slouken@2713
   653
    return 0;
slouken@2713
   654
}
slouken@2713
   655
slouken@2713
   656
slouken@2713
   657
/*
slouken@2713
   658
 * Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
slouken@2713
   659
 */
slouken@2713
   660
void
slouken@2713
   661
SDL_HelperWindowDestroy(void)
slouken@2713
   662
{
slouken@3097
   663
    HINSTANCE hInstance = GetModuleHandle(NULL);
bobbens@2863
   664
slouken@2713
   665
    /* Destroy the window. */
bobbens@2863
   666
    if (SDL_HelperWindow != NULL) {
bobbens@2863
   667
        if (DestroyWindow(SDL_HelperWindow) == 0) {
slouken@4500
   668
            WIN_SetError("Unable to destroy Helper Window");
slouken@2865
   669
            return;
bobbens@2863
   670
        }
slouken@2713
   671
        SDL_HelperWindow = NULL;
slouken@2713
   672
    }
slouken@2713
   673
slouken@2713
   674
    /* Unregister the class. */
bobbens@2863
   675
    if (SDL_HelperWindowClass != 0) {
bobbens@2863
   676
        if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
slouken@4500
   677
            WIN_SetError("Unable to destroy Helper Window Class");
slouken@2865
   678
            return;
bobbens@2863
   679
        }
slouken@2713
   680
        SDL_HelperWindowClass = 0;
slouken@2713
   681
    }
slouken@2713
   682
}
slouken@2713
   683
slouken@1895
   684
/* vi: set ts=4 sw=4 expandtab: */