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