src/video/win32/SDL_win32window.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
child 1913 83420da906a5
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@1895
     3
    Copyright (C) 1997-2006 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@1895
    25
#include "../../events/SDL_keyboard_c.h"
slouken@1895
    26
slouken@1895
    27
#include "SDL_win32video.h"
slouken@1895
    28
slouken@1895
    29
/* This is included after SDL_win32video.h, which includes windows.h */
slouken@1895
    30
#include "SDL_syswm.h"
slouken@1895
    31
slouken@1895
    32
slouken@1895
    33
static int
slouken@1895
    34
SetupWindowData(SDL_Window * window, HWND hwnd, BOOL created)
slouken@1895
    35
{
slouken@1895
    36
    SDL_WindowData *data;
slouken@1895
    37
slouken@1895
    38
    /* Allocate the window data */
slouken@1895
    39
    data = (SDL_WindowData *) SDL_malloc(sizeof(*data));
slouken@1895
    40
    if (!data) {
slouken@1895
    41
        SDL_OutOfMemory();
slouken@1895
    42
        return -1;
slouken@1895
    43
    }
slouken@1895
    44
    data->windowID = window->id;
slouken@1895
    45
    data->hwnd = hwnd;
slouken@1895
    46
    data->created = created;
slouken@1895
    47
    data->mouse_pressed = SDL_FALSE;
slouken@1895
    48
    data->videodata = (SDL_VideoData *) SDL_GetVideoDevice()->driverdata;
slouken@1895
    49
slouken@1895
    50
    /* Associate the data with the window */
slouken@1895
    51
    if (!SetProp(hwnd, TEXT("SDL_WindowData"), data)) {
slouken@1895
    52
        SDL_free(data);
slouken@1895
    53
        WIN_SetError("SetProp() failed");
slouken@1895
    54
        return -1;
slouken@1895
    55
    }
slouken@1895
    56
slouken@1895
    57
    /* Set up the window proc function */
slouken@1895
    58
    data->wndproc = (WNDPROC) GetWindowLongPtr(hwnd, GWLP_WNDPROC);
slouken@1895
    59
    if (data->wndproc == NULL) {
slouken@1895
    60
        data->wndproc = DefWindowProc;
slouken@1895
    61
    } else {
slouken@1895
    62
        SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) WIN_WindowProc);
slouken@1895
    63
    }
slouken@1895
    64
slouken@1895
    65
    /* Fill in the SDL window with the window data */
slouken@1895
    66
    {
slouken@1895
    67
        POINT point;
slouken@1895
    68
        point.x = 0;
slouken@1895
    69
        point.y = 0;
slouken@1895
    70
        if (ClientToScreen(hwnd, &point)) {
slouken@1895
    71
            window->x = point.x;
slouken@1895
    72
            window->y = point.y;
slouken@1895
    73
        }
slouken@1895
    74
    }
slouken@1895
    75
    {
slouken@1895
    76
        RECT rect;
slouken@1895
    77
        if (GetClientRect(hwnd, &rect)) {
slouken@1895
    78
            window->w = rect.right;
slouken@1895
    79
            window->h = rect.bottom;
slouken@1895
    80
        }
slouken@1895
    81
    }
slouken@1895
    82
    {
slouken@1895
    83
        DWORD style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
    84
        if (style & WS_VISIBLE) {
slouken@1895
    85
            window->flags |= SDL_WINDOW_SHOWN;
slouken@1895
    86
        } else {
slouken@1895
    87
            window->flags &= ~SDL_WINDOW_SHOWN;
slouken@1895
    88
        }
slouken@1895
    89
        if (style & (WS_BORDER | WS_THICKFRAME)) {
slouken@1895
    90
            window->flags &= ~SDL_WINDOW_BORDERLESS;
slouken@1895
    91
        } else {
slouken@1895
    92
            window->flags |= SDL_WINDOW_BORDERLESS;
slouken@1895
    93
        }
slouken@1895
    94
        if (style & WS_THICKFRAME) {
slouken@1895
    95
            window->flags |= SDL_WINDOW_RESIZABLE;
slouken@1895
    96
        } else {
slouken@1895
    97
            window->flags &= ~SDL_WINDOW_RESIZABLE;
slouken@1895
    98
        }
slouken@1895
    99
        if (style & WS_MAXIMIZE) {
slouken@1895
   100
            window->flags |= SDL_WINDOW_MAXIMIZED;
slouken@1895
   101
        } else {
slouken@1895
   102
            window->flags &= ~SDL_WINDOW_MAXIMIZED;
slouken@1895
   103
        }
slouken@1895
   104
        if (style & WS_MINIMIZE) {
slouken@1895
   105
            window->flags |= SDL_WINDOW_MINIMIZED;
slouken@1895
   106
        } else {
slouken@1895
   107
            window->flags &= ~SDL_WINDOW_MINIMIZED;
slouken@1895
   108
        }
slouken@1895
   109
    }
slouken@1895
   110
    if (GetFocus() == hwnd) {
slouken@1895
   111
        int index = data->videodata->keyboard;
slouken@1895
   112
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
slouken@1895
   113
        SDL_SetKeyboardFocus(index, data->windowID);
slouken@1895
   114
slouken@1895
   115
        if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
slouken@1895
   116
            RECT rect;
slouken@1895
   117
            GetClientRect(hwnd, &rect);
slouken@1895
   118
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   119
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   120
            ClipCursor(&rect);
slouken@1895
   121
        }
slouken@1895
   122
    }
slouken@1895
   123
slouken@1895
   124
    /* All done! */
slouken@1895
   125
    window->driverdata = data;
slouken@1895
   126
    return 0;
slouken@1895
   127
}
slouken@1895
   128
slouken@1895
   129
int
slouken@1895
   130
WIN_CreateWindow(_THIS, SDL_Window * window)
slouken@1895
   131
{
slouken@1895
   132
    HWND hwnd;
slouken@1895
   133
    LPTSTR title = NULL;
slouken@1895
   134
    HWND top;
slouken@1895
   135
    RECT rect;
slouken@1895
   136
    DWORD style = 0;
slouken@1895
   137
    int x, y;
slouken@1895
   138
    int w, h;
slouken@1895
   139
slouken@1895
   140
    if (window->title) {
slouken@1895
   141
        title = WIN_UTF8ToString(window->title);
slouken@1895
   142
    } else {
slouken@1895
   143
        title = NULL;
slouken@1895
   144
    }
slouken@1895
   145
slouken@1895
   146
    if (window->flags & SDL_WINDOW_SHOWN) {
slouken@1895
   147
        style |= WS_VISIBLE;
slouken@1895
   148
    }
slouken@1895
   149
    if ((window->flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS))) {
slouken@1895
   150
        style |= WS_POPUP;
slouken@1895
   151
    } else {
slouken@1895
   152
        style |= (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX);
slouken@1895
   153
    }
slouken@1895
   154
    if (window->flags & SDL_WINDOW_RESIZABLE) {
slouken@1895
   155
        style |= (WS_THICKFRAME | WS_MAXIMIZEBOX);
slouken@1895
   156
    }
slouken@1895
   157
    if (window->flags & SDL_WINDOW_MAXIMIZED) {
slouken@1895
   158
        style |= WS_MAXIMIZE;
slouken@1895
   159
    }
slouken@1895
   160
    if (window->flags & SDL_WINDOW_MINIMIZED) {
slouken@1895
   161
        style |= WS_MINIMIZE;
slouken@1895
   162
    }
slouken@1895
   163
slouken@1895
   164
    /* Figure out what the window area will be */
slouken@1895
   165
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   166
        top = HWND_TOPMOST;
slouken@1895
   167
    } else {
slouken@1895
   168
        top = HWND_NOTOPMOST;
slouken@1895
   169
    }
slouken@1895
   170
    rect.left = 0;
slouken@1895
   171
    rect.top = 0;
slouken@1895
   172
    rect.right = window->w;
slouken@1895
   173
    rect.bottom = window->h;
slouken@1895
   174
    AdjustWindowRectEx(&rect, style, FALSE, 0);
slouken@1895
   175
    w = (rect.right - rect.left);
slouken@1895
   176
    h = (rect.bottom - rect.top);
slouken@1895
   177
slouken@1895
   178
    if ((window->flags & SDL_WINDOW_FULLSCREEN) ||
slouken@1895
   179
        window->x == SDL_WINDOWPOS_CENTERED) {
slouken@1895
   180
        x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
slouken@1895
   181
    } else if (window->x == SDL_WINDOWPOS_UNDEFINED) {
slouken@1895
   182
        x = CW_USEDEFAULT;
slouken@1895
   183
    } else {
slouken@1895
   184
        x = window->x + rect.left;
slouken@1895
   185
    }
slouken@1895
   186
    if ((window->flags & SDL_WINDOW_FULLSCREEN) ||
slouken@1895
   187
        window->y == SDL_WINDOWPOS_CENTERED) {
slouken@1895
   188
        y = (GetSystemMetrics(SM_CYSCREEN) - h) / 2;
slouken@1895
   189
    } else if (window->y == SDL_WINDOWPOS_UNDEFINED) {
slouken@1895
   190
        y = CW_USEDEFAULT;
slouken@1895
   191
    } else {
slouken@1895
   192
        y = window->y + rect.top;
slouken@1895
   193
    }
slouken@1895
   194
slouken@1895
   195
    hwnd = CreateWindow(SDL_Appname,
slouken@1895
   196
                        title ? title : TEXT(""),
slouken@1895
   197
                        style, x, y, w, h, NULL, NULL, SDL_Instance, NULL);
slouken@1895
   198
    WIN_PumpEvents(_this);
slouken@1895
   199
slouken@1895
   200
    if (title) {
slouken@1895
   201
        SDL_free(title);
slouken@1895
   202
    }
slouken@1895
   203
slouken@1895
   204
    if (!hwnd) {
slouken@1895
   205
        WIN_SetError("Couldn't create window");
slouken@1895
   206
        return -1;
slouken@1895
   207
    }
slouken@1895
   208
slouken@1895
   209
    if (SetupWindowData(window, hwnd, TRUE) < 0) {
slouken@1895
   210
        DestroyWindow(hwnd);
slouken@1895
   211
        return -1;
slouken@1895
   212
    }
slouken@1895
   213
    return 0;
slouken@1895
   214
}
slouken@1895
   215
slouken@1895
   216
int
slouken@1895
   217
WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
slouken@1895
   218
{
slouken@1895
   219
    HWND hwnd = (HWND) data;
slouken@1895
   220
    LPTSTR title;
slouken@1895
   221
    int titleLen;
slouken@1895
   222
slouken@1895
   223
    /* Query the title from the existing window */
slouken@1895
   224
    titleLen = GetWindowTextLength(hwnd);
slouken@1895
   225
    title = SDL_stack_alloc(TCHAR, titleLen + 1);
slouken@1895
   226
    if (title) {
slouken@1895
   227
        titleLen = GetWindowText(hwnd, title, titleLen);
slouken@1895
   228
    } else {
slouken@1895
   229
        titleLen = 0;
slouken@1895
   230
    }
slouken@1895
   231
    if (titleLen > 0) {
slouken@1895
   232
        window->title = WIN_StringToUTF8(title);
slouken@1895
   233
    }
slouken@1895
   234
    if (title) {
slouken@1895
   235
        SDL_stack_free(title);
slouken@1895
   236
    }
slouken@1895
   237
slouken@1895
   238
    if (SetupWindowData(window, hwnd, FALSE) < 0) {
slouken@1895
   239
        return -1;
slouken@1895
   240
    }
slouken@1895
   241
    return 0;
slouken@1895
   242
}
slouken@1895
   243
slouken@1895
   244
void
slouken@1895
   245
WIN_SetWindowTitle(_THIS, SDL_Window * window)
slouken@1895
   246
{
slouken@1895
   247
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   248
    LPTSTR title;
slouken@1895
   249
slouken@1895
   250
    if (window->title) {
slouken@1895
   251
        title = WIN_UTF8ToString(window->title);
slouken@1895
   252
    } else {
slouken@1895
   253
        title = NULL;
slouken@1895
   254
    }
slouken@1895
   255
    SetWindowText(hwnd, title ? title : TEXT(""));
slouken@1895
   256
    if (title) {
slouken@1895
   257
        SDL_free(title);
slouken@1895
   258
    }
slouken@1895
   259
}
slouken@1895
   260
slouken@1895
   261
void
slouken@1895
   262
WIN_SetWindowPosition(_THIS, SDL_Window * window)
slouken@1895
   263
{
slouken@1895
   264
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   265
    RECT rect;
slouken@1895
   266
    DWORD style;
slouken@1895
   267
    HWND top;
slouken@1895
   268
    int x, y;
slouken@1895
   269
    int w, h;
slouken@1895
   270
slouken@1895
   271
    /* Figure out what the window area will be */
slouken@1895
   272
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   273
        top = HWND_TOPMOST;
slouken@1895
   274
    } else {
slouken@1895
   275
        top = HWND_NOTOPMOST;
slouken@1895
   276
    }
slouken@1895
   277
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   278
    rect.left = 0;
slouken@1895
   279
    rect.top = 0;
slouken@1895
   280
    rect.right = window->w;
slouken@1895
   281
    rect.bottom = window->h;
slouken@1895
   282
    AdjustWindowRectEx(&rect, style,
slouken@1895
   283
                       (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) !=
slouken@1895
   284
                                                           NULL), 0);
slouken@1895
   285
    w = (rect.right - rect.left);
slouken@1895
   286
    h = (rect.bottom - rect.top);
slouken@1895
   287
slouken@1895
   288
    if ((window->flags & SDL_WINDOW_FULLSCREEN) ||
slouken@1895
   289
        window->x == SDL_WINDOWPOS_CENTERED) {
slouken@1895
   290
        x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
slouken@1895
   291
        window->x = x - rect.left;
slouken@1895
   292
    } else {
slouken@1895
   293
        x = window->x + rect.left;
slouken@1895
   294
    }
slouken@1895
   295
    if ((window->flags & SDL_WINDOW_FULLSCREEN) ||
slouken@1895
   296
        window->y == SDL_WINDOWPOS_CENTERED) {
slouken@1895
   297
        y = (GetSystemMetrics(SM_CYSCREEN) - h) / 2;
slouken@1895
   298
        window->y = y - rect.top;
slouken@1895
   299
    } else {
slouken@1895
   300
        y = window->y + rect.top;
slouken@1895
   301
    }
slouken@1895
   302
    SetWindowPos(hwnd, top, x, y, h, w, (SWP_NOCOPYBITS | SWP_NOSIZE));
slouken@1895
   303
}
slouken@1895
   304
slouken@1895
   305
void
slouken@1895
   306
WIN_SetWindowSize(_THIS, SDL_Window * window)
slouken@1895
   307
{
slouken@1895
   308
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   309
    RECT rect;
slouken@1895
   310
    DWORD style;
slouken@1895
   311
    HWND top;
slouken@1895
   312
    int w, h;
slouken@1895
   313
slouken@1895
   314
    /* Figure out what the window area will be */
slouken@1895
   315
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   316
        top = HWND_TOPMOST;
slouken@1895
   317
    } else {
slouken@1895
   318
        top = HWND_NOTOPMOST;
slouken@1895
   319
    }
slouken@1895
   320
    style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   321
    rect.left = 0;
slouken@1895
   322
    rect.top = 0;
slouken@1895
   323
    rect.right = window->w;
slouken@1895
   324
    rect.bottom = window->h;
slouken@1895
   325
    AdjustWindowRectEx(&rect, style,
slouken@1895
   326
                       (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) !=
slouken@1895
   327
                                                           NULL), 0);
slouken@1895
   328
    w = (rect.right - rect.left);
slouken@1895
   329
    h = (rect.bottom - rect.top);
slouken@1895
   330
slouken@1895
   331
    SetWindowPos(hwnd, top, 0, 0, h, w, (SWP_NOCOPYBITS | SWP_NOMOVE));
slouken@1895
   332
}
slouken@1895
   333
slouken@1895
   334
void
slouken@1895
   335
WIN_ShowWindow(_THIS, SDL_Window * window)
slouken@1895
   336
{
slouken@1895
   337
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   338
slouken@1895
   339
    ShowWindow(hwnd, SW_SHOW);
slouken@1895
   340
}
slouken@1895
   341
slouken@1895
   342
void
slouken@1895
   343
WIN_HideWindow(_THIS, SDL_Window * window)
slouken@1895
   344
{
slouken@1895
   345
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   346
slouken@1895
   347
    ShowWindow(hwnd, SW_HIDE);
slouken@1895
   348
}
slouken@1895
   349
slouken@1895
   350
void
slouken@1895
   351
WIN_RaiseWindow(_THIS, SDL_Window * window)
slouken@1895
   352
{
slouken@1895
   353
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   354
    HWND top;
slouken@1895
   355
slouken@1895
   356
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1895
   357
        top = HWND_TOPMOST;
slouken@1895
   358
    } else {
slouken@1895
   359
        top = HWND_NOTOPMOST;
slouken@1895
   360
    }
slouken@1895
   361
    SetWindowPos(hwnd, top, 0, 0, 0, 0, (SWP_NOMOVE | SWP_NOSIZE));
slouken@1895
   362
}
slouken@1895
   363
slouken@1895
   364
void
slouken@1895
   365
WIN_MaximizeWindow(_THIS, SDL_Window * window)
slouken@1895
   366
{
slouken@1895
   367
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   368
slouken@1895
   369
    ShowWindow(hwnd, SW_MAXIMIZE);
slouken@1895
   370
}
slouken@1895
   371
slouken@1895
   372
void
slouken@1895
   373
WIN_MinimizeWindow(_THIS, SDL_Window * window)
slouken@1895
   374
{
slouken@1895
   375
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   376
slouken@1895
   377
    ShowWindow(hwnd, SW_MINIMIZE);
slouken@1895
   378
}
slouken@1895
   379
slouken@1895
   380
void
slouken@1895
   381
WIN_RestoreWindow(_THIS, SDL_Window * window)
slouken@1895
   382
{
slouken@1895
   383
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   384
slouken@1895
   385
    ShowWindow(hwnd, SW_RESTORE);
slouken@1895
   386
}
slouken@1895
   387
slouken@1895
   388
void
slouken@1895
   389
WIN_SetWindowGrab(_THIS, SDL_Window * window)
slouken@1895
   390
{
slouken@1895
   391
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   392
slouken@1895
   393
    if ((window->flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1895
   394
        (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   395
        RECT rect;
slouken@1895
   396
        GetClientRect(hwnd, &rect);
slouken@1895
   397
        ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   398
        ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   399
        ClipCursor(&rect);
slouken@1895
   400
    } else {
slouken@1895
   401
        ClipCursor(NULL);
slouken@1895
   402
    }
slouken@1895
   403
}
slouken@1895
   404
slouken@1895
   405
void
slouken@1895
   406
WIN_DestroyWindow(_THIS, SDL_Window * window)
slouken@1895
   407
{
slouken@1895
   408
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@1895
   409
slouken@1895
   410
    if (data) {
slouken@1895
   411
        if (data->created) {
slouken@1895
   412
            DestroyWindow(data->hwnd);
slouken@1895
   413
        }
slouken@1895
   414
        SDL_free(data);
slouken@1895
   415
    }
slouken@1895
   416
}
slouken@1895
   417
slouken@1895
   418
SDL_bool
slouken@1895
   419
WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
slouken@1895
   420
{
slouken@1895
   421
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@1895
   422
    if (info->version.major <= SDL_MAJOR_VERSION) {
slouken@1895
   423
        info->window = hwnd;
slouken@1895
   424
        /* FIXME! */
slouken@1895
   425
        info->hglrc = NULL;
slouken@1895
   426
        return SDL_TRUE;
slouken@1895
   427
    } else {
slouken@1895
   428
        SDL_SetError("Application not compiled with SDL %d.%d\n",
slouken@1895
   429
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
slouken@1895
   430
        return SDL_FALSE;
slouken@1895
   431
    }
slouken@1895
   432
}
slouken@1895
   433
slouken@1895
   434
/* vi: set ts=4 sw=4 expandtab: */