src/video/windows/SDL_windowswindow.c
author Sam Lantinga
Tue, 08 Feb 2011 16:50:51 -0800
changeset 5229 c015d3e63631
parent 5178 4191af605cb0
child 5246 58265e606e4e
permissions -rw-r--r--
Fixed setting the texture unit, still doesn't work.
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@3685
    49
    SDL_VideoDisplay *display = window->display;
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@3530
    96
            SDL_Rect bounds;
slouken@3530
    97
            WIN_GetDisplayBounds(_this, display, &bounds);
slouken@3530
    98
            window->x = point.x - bounds.x;
slouken@3530
    99
            window->y = point.y - bounds.y;
slouken@1895
   100
        }
slouken@1895
   101
    }
slouken@1895
   102
    {
slouken@1895
   103
        RECT rect;
slouken@1895
   104
        if (GetClientRect(hwnd, &rect)) {
slouken@1895
   105
            window->w = rect.right;
slouken@1895
   106
            window->h = rect.bottom;
slouken@1895
   107
        }
slouken@1895
   108
    }
slouken@1895
   109
    {
slouken@1895
   110
        DWORD style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   111
        if (style & WS_VISIBLE) {
slouken@1895
   112
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1895
   113
        } else {
slouken@1895
   114
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1895
   115
        }
slouken@1895
   116
        if (style & (WS_BORDER | WS_THICKFRAME)) {
slouken@1895
   117
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1895
   118
        } else {
slouken@1895
   119
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1895
   120
        }
slouken@1895
   121
        if (style & WS_THICKFRAME) {
slouken@1895
   122
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1895
   123
        } else {
slouken@1895
   124
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1895
   125
        }
slouken@5086
   126
#ifdef WS_MAXIMIZE
slouken@1895
   127
        if (style & WS_MAXIMIZE) {
slouken@1895
   128
            window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@5086
   129
        } else
slouken@5086
   130
#endif
slouken@5086
   131
        {
slouken@1895
   132
            window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1895
   133
        }
slouken@5086
   134
#ifdef WS_MINIMIZE
slouken@1895
   135
        if (style & WS_MINIMIZE) {
slouken@1895
   136
            window->flags |= SDL_WINDOW_MINIMIZED;
slouken@5086
   137
        } else
slouken@5086
   138
#endif
slouken@5086
   139
        {
slouken@1895
   140
            window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1895
   141
        }
slouken@1895
   142
    }
slouken@1895
   143
    if (GetFocus() == hwnd) {
slouken@1895
   144
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@4465
   145
        SDL_SetKeyboardFocus(data->window);
slouken@1895
   146
slouken@1895
   147
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1895
   148
            RECT rect;
slouken@1895
   149
            GetClientRect(hwnd, &rect);
slouken@1895
   150
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   151
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   152
            ClipCursor(&rect);
slouken@1895
   153
        }
slouken@1895
   154
    }
slouken@1895
   155
slouken@4919
   156
	/* Enable multi-touch */
slouken@4948
   157
    if (videodata->RegisterTouchWindow) {
slouken@4948
   158
        videodata->RegisterTouchWindow(hwnd, (TWF_FINETOUCH|TWF_WANTPALM));
slouken@4948
   159
    }
slouken@4919
   160
slouken@1895
   161
    /* All done! */
slouken@1895
   162
    window->driverdata = data;
slouken@1895
   163
    return 0;
slouken@1895
   164
}
slouken@1895
   165
slouken@1895
   166
int
slouken@1895
   167
WIN_CreateWindow(_THIS, SDL_Window * window)
slouken@1895
   168
{
slouken@3685
   169
    SDL_VideoDisplay *display = window->display;
slouken@1895
   170
    HWND hwnd;
slouken@1895
   171
    RECT rect;
slouken@3528
   172
    SDL_Rect bounds;
slouken@1913
   173
    DWORD style = (WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
slouken@1895
   174
    int x, y;
slouken@1895
   175
    int w, h;
slouken@1895
   176
slouken@2876
   177
    if (window->flags & (SDL_WINDOW_BORDERLESS | SDL_WINDOW_FULLSCREEN)) {
slouken@1895
   178
        style |= WS_POPUP;
slouken@1895
   179
    } else {
slouken@1895
   180
        style |= (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX);
slouken@1895
   181
    }
slouken@2876
   182
    if ((window->flags & SDL_WINDOW_RESIZABLE)
slouken@2876
   183
        && !(window->flags & SDL_WINDOW_FULLSCREEN)) {
slouken@1895
   184
        style |= (WS_THICKFRAME | WS_MAXIMIZEBOX);
slouken@1895
   185
    }
slouken@1895
   186
slouken@1895
   187
    /* Figure out what the window area will be */
slouken@1895
   188
    rect.left = 0;
slouken@1895
   189
    rect.top = 0;
slouken@1895
   190
    rect.right = window->w;
slouken@1895
   191
    rect.bottom = window->h;
slouken@1895
   192
    AdjustWindowRectEx(&rect, style, FALSE, 0);
slouken@1895
   193
    w = (rect.right - rect.left);
slouken@1895
   194
    h = (rect.bottom - rect.top);
slouken@1895
   195
slouken@3528
   196
    WIN_GetDisplayBounds(_this, display, &bounds);
slouken@4486
   197
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@4486
   198
        /* The bounds when this window is visible is the fullscreen mode */
slouken@4486
   199
        SDL_DisplayMode fullscreen_mode;
slouken@4486
   200
        if (SDL_GetWindowDisplayMode(window, &fullscreen_mode) == 0) {
slouken@4486
   201
            bounds.w = fullscreen_mode.w;
slouken@4486
   202
            bounds.h = fullscreen_mode.h;
slouken@4486
   203
        }
slouken@4486
   204
    }
slouken@2876
   205
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@2876
   206
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@4486
   207
        x = bounds.x + (bounds.w - w) / 2;
slouken@1895
   208
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@3528
   209
        if (bounds.x == 0) {
slouken@3528
   210
            x = CW_USEDEFAULT;
slouken@3528
   211
        } else {
slouken@3528
   212
            x = bounds.x;
slouken@3528
   213
        }
slouken@1895
   214
    } else {
slouken@3530
   215
        x = bounds.x + window->x + rect.left;
slouken@1895
   216
    }
slouken@2876
   217
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@2876
   218
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@4486
   219
        y = bounds.y + (bounds.h - h) / 2;
slouken@3528
   220
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@3528
   221
        if (bounds.x == 0) {
slouken@3528
   222
            y = CW_USEDEFAULT;
slouken@3528
   223
        } else {
slouken@3528
   224
            y = bounds.y;
slouken@3528
   225
        }
slouken@1895
   226
    } else {
slouken@3530
   227
        y = bounds.y + window->y + rect.top;
slouken@1895
   228
    }
slouken@1895
   229
slouken@1956
   230
    hwnd =
slouken@1956
   231
        CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, NULL, NULL,
slouken@1956
   232
                     SDL_Instance, NULL);
slouken@1895
   233
    if (!hwnd) {
slouken@1895
   234
        WIN_SetError("Couldn't create window");
slouken@1895
   235
        return -1;
slouken@1895
   236
    }
jimtla@4650
   237
	//RegisterTouchWindow(hwnd, 0);
slouken@1895
   238
slouken@2710
   239
    WIN_PumpEvents(_this);
slouken@2710
   240
slouken@1951
   241
    if (SetupWindowData(_this, window, hwnd, SDL_TRUE) < 0) {
slouken@1895
   242
        DestroyWindow(hwnd);
slouken@1895
   243
        return -1;
slouken@1895
   244
    }
slouken@5088
   245
#if SDL_VIDEO_OPENGL_WGL
slouken@1913
   246
    if (window->flags & SDL_WINDOW_OPENGL) {
slouken@1913
   247
        if (WIN_GL_SetupWindow(_this, window) < 0) {
slouken@1913
   248
            WIN_DestroyWindow(_this, window);
slouken@1913
   249
            return -1;
slouken@1913
   250
        }
slouken@1913
   251
    }
slouken@1913
   252
#endif
slouken@1895
   253
    return 0;
slouken@1895
   254
}
slouken@1895
   255
slouken@1895
   256
int
slouken@1895
   257
WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1895
   258
{
slouken@1895
   259
    HWND hwnd = (HWND) data;
slouken@1895
   260
    LPTSTR title;
slouken@1895
   261
    int titleLen;
slouken@1895
   262
slouken@1895
   263
    /* Query the title from the existing window */
slouken@1895
   264
    titleLen = GetWindowTextLength(hwnd);
slouken@1895
   265
    title = SDL_stack_alloc(TCHAR, titleLen + 1);
slouken@1895
   266
    if (title) {
slouken@1895
   267
        titleLen = GetWindowText(hwnd, title, titleLen);
slouken@1895
   268
    } else {
slouken@1895
   269
        titleLen = 0;
slouken@1895
   270
    }
slouken@1895
   271
    if (titleLen > 0) {
slouken@1895
   272
        window->title = WIN_StringToUTF8(title);
slouken@1895
   273
    }
slouken@1895
   274
    if (title) {
slouken@1895
   275
        SDL_stack_free(title);
slouken@1895
   276
    }
slouken@1895
   277
slouken@1951
   278
    if (SetupWindowData(_this, window, hwnd, SDL_FALSE) < 0) {
slouken@1895
   279
        return -1;
slouken@1895
   280
    }
slouken@1895
   281
    return 0;
slouken@1895
   282
}
slouken@1895
   283
slouken@1895
   284
void
slouken@1895
   285
WIN_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1895
   286
{
slouken@1895
   287
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   288
    LPTSTR title;
slouken@1895
   289
slouken@1895
   290
    if (window->title) {
slouken@1895
   291
        title = WIN_UTF8ToString(window->title);
slouken@1895
   292
    } else {
slouken@1895
   293
        title = NULL;
slouken@1895
   294
    }
slouken@1895
   295
    SetWindowText(hwnd, title ? title : TEXT(""));
slouken@1895
   296
    if (title) {
slouken@1895
   297
        SDL_free(title);
slouken@1895
   298
    }
slouken@1895
   299
}
slouken@1895
   300
slouken@1895
   301
void
slouken@2970
   302
WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
slouken@2970
   303
{
slouken@2970
   304
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@2970
   305
    HICON hicon = NULL;
slouken@2970
   306
slouken@2990
   307
    if (icon) {
slouken@2970
   308
        BYTE *icon_bmp;
slouken@2970
   309
        int icon_len;
slouken@2970
   310
        SDL_RWops *dst;
slouken@2970
   311
        SDL_PixelFormat format;
slouken@2970
   312
        SDL_Surface *surface;
slouken@2970
   313
slouken@2970
   314
        /* Create temporary bitmap buffer */
slouken@2970
   315
        icon_len = 40 + icon->h * icon->w * 4;
slouken@2970
   316
        icon_bmp = SDL_stack_alloc(BYTE, icon_len);
slouken@2970
   317
        dst = SDL_RWFromMem(icon_bmp, icon_len);
slouken@2970
   318
        if (!dst) {
slouken@2970
   319
            SDL_stack_free(icon_bmp);
slouken@2970
   320
            return;
slouken@2970
   321
        }
slouken@2970
   322
slouken@2970
   323
        /* Write the BITMAPINFO header */
slouken@2970
   324
        SDL_WriteLE32(dst, 40);
slouken@2970
   325
        SDL_WriteLE32(dst, icon->w);
slouken@2990
   326
        SDL_WriteLE32(dst, icon->h * 2);
slouken@2970
   327
        SDL_WriteLE16(dst, 1);
slouken@2970
   328
        SDL_WriteLE16(dst, 32);
slouken@2970
   329
        SDL_WriteLE32(dst, BI_RGB);
slouken@2970
   330
        SDL_WriteLE32(dst, icon->h * icon->w * 4);
slouken@2970
   331
        SDL_WriteLE32(dst, 0);
slouken@2970
   332
        SDL_WriteLE32(dst, 0);
slouken@2970
   333
        SDL_WriteLE32(dst, 0);
slouken@2970
   334
        SDL_WriteLE32(dst, 0);
slouken@2970
   335
slouken@2970
   336
        /* Convert the icon to a 32-bit surface with alpha channel */
slouken@2970
   337
        SDL_InitFormat(&format, 32,
slouken@2970
   338
                       0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
slouken@2970
   339
        surface = SDL_ConvertSurface(icon, &format, 0);
slouken@2970
   340
        if (surface) {
slouken@2970
   341
            /* Write the pixels upside down into the bitmap buffer */
slouken@2970
   342
            int y = surface->h;
slouken@2970
   343
            while (y--) {
slouken@2990
   344
                Uint8 *src = (Uint8 *) surface->pixels + y * surface->pitch;
slouken@2970
   345
                SDL_RWwrite(dst, src, surface->pitch, 1);
slouken@2970
   346
            }
slouken@2970
   347
            SDL_FreeSurface(surface);
slouken@2970
   348
slouken@3097
   349
/* TODO: create the icon in WinCE (CreateIconFromResource isn't available) */
slouken@3097
   350
#ifndef _WIN32_WCE
slouken@2990
   351
            hicon =
slouken@2990
   352
                CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
slouken@3097
   353
#endif
slouken@2970
   354
        }
slouken@2970
   355
        SDL_RWclose(dst);
slouken@2970
   356
        SDL_stack_free(icon_bmp);
slouken@2970
   357
    }
slouken@2970
   358
slouken@2971
   359
    /* Set the icon for the window */
slouken@2990
   360
    SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);
slouken@2971
   361
slouken@2971
   362
    /* Set the icon in the task manager (should we do this?) */
slouken@2990
   363
    SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) hicon);
slouken@2970
   364
}
slouken@2970
   365
slouken@2970
   366
void
slouken@1895
   367
WIN_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1895
   368
{
slouken@3685
   369
    SDL_VideoDisplay *display = window->display;
slouken@1895
   370
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   371
    RECT rect;
slouken@3528
   372
    SDL_Rect bounds;
slouken@1895
   373
    DWORD style;
slouken@1895
   374
    HWND top;
slouken@3168
   375
    BOOL menu;
slouken@1895
   376
    int x, y;
slouken@4486
   377
    int w, h;
slouken@1895
   378
slouken@1895
   379
    /* Figure out what the window area will be */
slouken@1895
   380
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   381
        top = HWND_TOPMOST;
slouken@1895
   382
    } else {
slouken@1895
   383
        top = HWND_NOTOPMOST;
slouken@1895
   384
    }
slouken@1895
   385
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   386
    rect.left = 0;
slouken@1895
   387
    rect.top = 0;
slouken@1895
   388
    rect.right = window->w;
slouken@1895
   389
    rect.bottom = window->h;
slouken@3168
   390
#ifdef _WIN32_WCE
slouken@3168
   391
    menu = FALSE;
slouken@3168
   392
#else
slouken@3168
   393
    menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   394
#endif
slouken@3168
   395
    AdjustWindowRectEx(&rect, style, menu, 0);
slouken@4486
   396
    w = (rect.right - rect.left);
slouken@4486
   397
    h = (rect.bottom - rect.top);
slouken@2875
   398
slouken@3528
   399
    WIN_GetDisplayBounds(_this, display, &bounds);
slouken@4486
   400
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@4486
   401
        /* The bounds when this window is visible is the fullscreen mode */
slouken@4486
   402
        SDL_DisplayMode fullscreen_mode;
slouken@4486
   403
        if (SDL_GetWindowDisplayMode(window, &fullscreen_mode) == 0) {
slouken@4486
   404
            bounds.w = fullscreen_mode.w;
slouken@4486
   405
            bounds.h = fullscreen_mode.h;
slouken@4486
   406
        }
slouken@4486
   407
    }
slouken@2876
   408
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@2876
   409
        || window->x == SDL_WINDOWPOS_CENTERED) {
slouken@4486
   410
        x = bounds.x + (bounds.w - w) / 2;
slouken@2875
   411
    } else {
slouken@3530
   412
        x = bounds.x + window->x + rect.left;
slouken@2875
   413
    }
slouken@2876
   414
    if ((window->flags & SDL_WINDOW_FULLSCREEN)
slouken@2876
   415
        || window->y == SDL_WINDOWPOS_CENTERED) {
slouken@4486
   416
        y = bounds.y + (bounds.h - h) / 2;
slouken@2875
   417
    } else {
slouken@3530
   418
        y = bounds.y + window->y + rect.top;
slouken@2875
   419
    }
slouken@1895
   420
slouken@1956
   421
    SetWindowPos(hwnd, top, x, y, 0, 0, (SWP_NOCOPYBITS | SWP_NOSIZE));
slouken@1895
   422
}
slouken@1895
   423
slouken@1895
   424
void
slouken@1895
   425
WIN_SetWindowSize(_THIS, SDL_Window * window)
slouken@1895
   426
{
slouken@1895
   427
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   428
    RECT rect;
slouken@1895
   429
    DWORD style;
slouken@1895
   430
    HWND top;
slouken@3168
   431
    BOOL menu;
slouken@1895
   432
    int w, h;
slouken@1895
   433
slouken@1895
   434
    /* Figure out what the window area will be */
slouken@1895
   435
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   436
        top = HWND_TOPMOST;
slouken@1895
   437
    } else {
slouken@1895
   438
        top = HWND_NOTOPMOST;
slouken@1895
   439
    }
slouken@1895
   440
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   441
    rect.left = 0;
slouken@1895
   442
    rect.top = 0;
slouken@1895
   443
    rect.right = window->w;
slouken@1895
   444
    rect.bottom = window->h;
slouken@3168
   445
#ifdef _WIN32_WCE
slouken@3168
   446
    menu = FALSE;
slouken@3168
   447
#else
slouken@3168
   448
    menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   449
#endif
slouken@3168
   450
    AdjustWindowRectEx(&rect, style, menu, 0);
slouken@1895
   451
    w = (rect.right - rect.left);
slouken@1895
   452
    h = (rect.bottom - rect.top);
slouken@1895
   453
slouken@2968
   454
    SetWindowPos(hwnd, top, 0, 0, w, h, (SWP_NOCOPYBITS | SWP_NOMOVE));
slouken@5178
   455
}
slouken@5178
   456
slouken@5178
   457
#ifdef _WIN32_WCE
slouken@5178
   458
void WINCE_ShowWindow(_THIS, SDL_Window* window, int visible)
slouken@5178
   459
{
slouken@5178
   460
    SDL_WindowData* windowdata = (SDL_WindowData*) window->driverdata;
slouken@5178
   461
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@5178
   462
slouken@5178
   463
    if(visible) {
slouken@5178
   464
        if(window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@5178
   465
            if(videodata->SHFullScreen)
slouken@5178
   466
                videodata->SHFullScreen(windowdata->hwnd, SHFS_HIDETASKBAR | SHFS_HIDESTARTICON | SHFS_HIDESIPBUTTON);
slouken@5178
   467
slouken@5178
   468
            ShowWindow(FindWindow(TEXT("HHTaskBar"), NULL), SW_HIDE);
slouken@5178
   469
        }
slouken@5178
   470
slouken@5178
   471
        ShowWindow(windowdata->hwnd, SW_SHOW);
slouken@5178
   472
        SetForegroundWindow(windowdata->hwnd);
slouken@5178
   473
    } else {
slouken@5178
   474
        ShowWindow(windowdata->hwnd, SW_HIDE);
slouken@5178
   475
slouken@5178
   476
        if(window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@5178
   477
            if(videodata->SHFullScreen)
slouken@5178
   478
                videodata->SHFullScreen(windowdata->hwnd, SHFS_SHOWTASKBAR | SHFS_SHOWSTARTICON | SHFS_SHOWSIPBUTTON);
slouken@5178
   479
slouken@5178
   480
            ShowWindow(FindWindow(TEXT("HHTaskBar"), NULL), SW_SHOW);
slouken@5178
   481
slouken@5178
   482
        }
slouken@5178
   483
    }
slouken@1895
   484
}
slouken@5178
   485
#endif /* _WIN32_WCE */
slouken@1895
   486
slouken@1895
   487
void
slouken@1895
   488
WIN_ShowWindow(_THIS, SDL_Window * window)
slouken@1895
   489
{
slouken@4569
   490
#ifdef _WIN32_WCE
slouken@4569
   491
    WINCE_ShowWindow(_this, window, 1);
slouken@4569
   492
#else
slouken@1895
   493
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   494
    ShowWindow(hwnd, SW_SHOW);
slouken@3168
   495
#endif
slouken@1895
   496
}
slouken@1895
   497
slouken@1895
   498
void
slouken@1895
   499
WIN_HideWindow(_THIS, SDL_Window * window)
slouken@1895
   500
{
slouken@4569
   501
#ifdef _WIN32_WCE
slouken@4569
   502
    WINCE_ShowWindow(_this, window, 0);
slouken@4569
   503
#else
slouken@1895
   504
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   505
    ShowWindow(hwnd, SW_HIDE);
slouken@3168
   506
#endif
slouken@1895
   507
}
slouken@1895
   508
slouken@1895
   509
void
slouken@1895
   510
WIN_RaiseWindow(_THIS, SDL_Window * window)
slouken@1895
   511
{
slouken@1895
   512
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   513
    HWND top;
slouken@1895
   514
slouken@1895
   515
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   516
        top = HWND_TOPMOST;
slouken@1895
   517
    } else {
slouken@1895
   518
        top = HWND_NOTOPMOST;
slouken@1895
   519
    }
slouken@1895
   520
    SetWindowPos(hwnd, top, 0, 0, 0, 0, (SWP_NOMOVE | SWP_NOSIZE));
slouken@1895
   521
}
slouken@1895
   522
slouken@1895
   523
void
slouken@1895
   524
WIN_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1895
   525
{
slouken@1895
   526
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@4569
   527
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@3168
   528
slouken@3168
   529
#ifdef _WIN32_WCE
slouken@4569
   530
    if((window->flags & SDL_WINDOW_FULLSCREEN) && videodata->SHFullScreen)
slouken@4569
   531
        videodata->SHFullScreen(hwnd, SHFS_HIDETASKBAR | SHFS_HIDESTARTICON | SHFS_HIDESIPBUTTON);
slouken@3168
   532
#endif
slouken@4569
   533
slouken@4569
   534
    ShowWindow(hwnd, SW_MAXIMIZE);
slouken@1895
   535
}
slouken@1895
   536
slouken@1895
   537
void
slouken@1895
   538
WIN_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1895
   539
{
slouken@1895
   540
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@4569
   541
    SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
slouken@1895
   542
slouken@1895
   543
    ShowWindow(hwnd, SW_MINIMIZE);
slouken@3168
   544
slouken@3168
   545
#ifdef _WIN32_WCE
slouken@4569
   546
    if((window->flags & SDL_WINDOW_FULLSCREEN) && videodata->SHFullScreen)
slouken@4569
   547
	videodata->SHFullScreen(hwnd, SHFS_SHOWTASKBAR | SHFS_SHOWSTARTICON | SHFS_SHOWSIPBUTTON);
slouken@3168
   548
#endif
slouken@1895
   549
}
slouken@1895
   550
slouken@1895
   551
void
slouken@1895
   552
WIN_RestoreWindow(_THIS, SDL_Window * window)
slouken@1895
   553
{
slouken@1895
   554
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   555
slouken@1895
   556
    ShowWindow(hwnd, SW_RESTORE);
slouken@1895
   557
}
slouken@1895
   558
slouken@1895
   559
void
slouken@1895
   560
WIN_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1895
   561
{
slouken@1895
   562
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   563
slouken@2876
   564
    if ((window->flags & (SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_FULLSCREEN))
slouken@2876
   565
        && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   566
        RECT rect;
slouken@1895
   567
        GetClientRect(hwnd, &rect);
slouken@1895
   568
        ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   569
        ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   570
        ClipCursor(&rect);
slouken@1895
   571
    } else {
slouken@1895
   572
        ClipCursor(NULL);
slouken@1895
   573
    }
slouken@1895
   574
}
slouken@1895
   575
slouken@1895
   576
void
slouken@1895
   577
WIN_DestroyWindow(_THIS, SDL_Window * window)
slouken@1895
   578
{
slouken@1895
   579
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1895
   580
slouken@1895
   581
    if (data) {
slouken@4569
   582
#ifdef _WIN32_WCE
slouken@4569
   583
	WINCE_ShowWindow(_this, window, 0);
slouken@4569
   584
#endif
slouken@1913
   585
        ReleaseDC(data->hwnd, data->hdc);
slouken@1895
   586
        if (data->created) {
slouken@1895
   587
            DestroyWindow(data->hwnd);
slouken@1895
   588
        }
slouken@1895
   589
        SDL_free(data);
slouken@1895
   590
    }
slouken@1895
   591
}
slouken@1895
   592
slouken@1895
   593
SDL_bool
slouken@1895
   594
WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1895
   595
{
slouken@1895
   596
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   597
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@4900
   598
        info->subsystem = SDL_SYSWM_WINDOWS;
slouken@5056
   599
        info->info.win.window = hwnd;
slouken@1895
   600
        return SDL_TRUE;
slouken@1895
   601
    } else {
slouken@1895
   602
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1895
   603
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1895
   604
        return SDL_FALSE;
slouken@1895
   605
    }
slouken@1895
   606
}
slouken@1895
   607
slouken@2713
   608
slouken@2713
   609
/*
slouken@2713
   610
 * Creates a HelperWindow used for DirectInput events.
slouken@2713
   611
 */
slouken@2713
   612
int
slouken@2713
   613
SDL_HelperWindowCreate(void)
slouken@2713
   614
{
slouken@3097
   615
    HINSTANCE hInstance = GetModuleHandle(NULL);
slouken@3097
   616
    WNDCLASS wce;
slouken@3196
   617
    HWND hWndParent = NULL;
slouken@2713
   618
bobbens@3045
   619
    /* Make sure window isn't created twice. */
bobbens@3045
   620
    if (SDL_HelperWindow != NULL) {
bobbens@3045
   621
        return 0;
bobbens@3045
   622
    }
bobbens@3045
   623
slouken@2713
   624
    /* Create the class. */
slouken@2714
   625
    SDL_zero(wce);
slouken@3097
   626
    wce.lpfnWndProc = DefWindowProc;
slouken@2713
   627
    wce.lpszClassName = (LPCWSTR) SDL_HelperWindowClassName;
slouken@2713
   628
    wce.hInstance = hInstance;
slouken@2713
   629
slouken@2713
   630
    /* Register the class. */
slouken@3097
   631
    SDL_HelperWindowClass = RegisterClass(&wce);
slouken@2713
   632
    if (SDL_HelperWindowClass == 0) {
slouken@4500
   633
        WIN_SetError("Unable to create Helper Window Class");
slouken@2713
   634
        return -1;
slouken@2713
   635
    }
slouken@2713
   636
slouken@3168
   637
#ifndef _WIN32_WCE
slouken@3168
   638
    /* WinCE doesn't have HWND_MESSAGE */
slouken@3168
   639
    hWndParent = HWND_MESSAGE;
slouken@3168
   640
#endif
slouken@3168
   641
slouken@2713
   642
    /* Create the window. */
slouken@2714
   643
    SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,
slouken@2714
   644
                                      SDL_HelperWindowName,
slouken@3168
   645
                                      WS_OVERLAPPED, CW_USEDEFAULT,
slouken@2714
   646
                                      CW_USEDEFAULT, CW_USEDEFAULT,
slouken@3168
   647
                                      CW_USEDEFAULT, hWndParent, NULL,
slouken@2714
   648
                                      hInstance, NULL);
slouken@2713
   649
    if (SDL_HelperWindow == NULL) {
bobbens@3045
   650
        UnregisterClass(SDL_HelperWindowClassName, hInstance);
slouken@4500
   651
        WIN_SetError("Unable to create Helper Window");
slouken@2713
   652
        return -1;
slouken@2713
   653
    }
slouken@2713
   654
slouken@2713
   655
    return 0;
slouken@2713
   656
}
slouken@2713
   657
slouken@2713
   658
slouken@2713
   659
/*
slouken@2713
   660
 * Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
slouken@2713
   661
 */
slouken@2713
   662
void
slouken@2713
   663
SDL_HelperWindowDestroy(void)
slouken@2713
   664
{
slouken@3097
   665
    HINSTANCE hInstance = GetModuleHandle(NULL);
bobbens@2863
   666
slouken@2713
   667
    /* Destroy the window. */
bobbens@2863
   668
    if (SDL_HelperWindow != NULL) {
bobbens@2863
   669
        if (DestroyWindow(SDL_HelperWindow) == 0) {
slouken@4500
   670
            WIN_SetError("Unable to destroy Helper Window");
slouken@2865
   671
            return;
bobbens@2863
   672
        }
slouken@2713
   673
        SDL_HelperWindow = NULL;
slouken@2713
   674
    }
slouken@2713
   675
slouken@2713
   676
    /* Unregister the class. */
bobbens@2863
   677
    if (SDL_HelperWindowClass != 0) {
bobbens@2863
   678
        if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
slouken@4500
   679
            WIN_SetError("Unable to destroy Helper Window Class");
slouken@2865
   680
            return;
bobbens@2863
   681
        }
slouken@2713
   682
        SDL_HelperWindowClass = 0;
slouken@2713
   683
    }
slouken@2713
   684
}
slouken@2713
   685
slouken@1895
   686
/* vi: set ts=4 sw=4 expandtab: */