src/video/windows/SDL_windowswindow.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 20 Jan 2011 18:04:05 -0800
changeset 5062 e8916fe9cfc8
parent 5056 src/video/win32/SDL_win32window.c@8b7988f42fcb
child 5086 c2539ff054c8
permissions -rw-r--r--
Fixed bug #925

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