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