src/video/windows/SDL_windowsevents.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 13 Aug 2017 14:15:52 -0700
changeset 11261 2d458a198021
parent 11242 70ae86f64702
child 11300 68a80d7afec3
permissions -rw-r--r--
Fixed bug 2841 - Hint to set resource id for window icon

Alexey

Seems to be a missing functionality. I want to set an icon from RC file. I cant pass MAKEINTRESOURCE(X) string to SDL_RegisterApp() cause string returned by MAKEINTRESOURCE string is not actually a string and SDL_strlen will crash. Moreover LoadImage seems to be loading wrong icon size. LoadIcon seems to be fine.
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@1895
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1895
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1895
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1895
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@2710
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_WINDOWS
slouken@1895
    24
slouken@5062
    25
#include "SDL_windowsvideo.h"
slouken@5062
    26
#include "SDL_windowsshape.h"
slouken@9597
    27
#include "SDL_system.h"
slouken@1895
    28
#include "SDL_syswm.h"
slouken@8218
    29
#include "SDL_timer.h"
slouken@1895
    30
#include "SDL_vkeys.h"
slouken@11261
    31
#include "SDL_hints.h"
slouken@1895
    32
#include "../../events/SDL_events_c.h"
slouken@4919
    33
#include "../../events/SDL_touch_c.h"
slouken@6938
    34
#include "../../events/scancodes_windows.h"
icculus@8942
    35
#include "SDL_assert.h"
andrewb@9829
    36
#include "SDL_hints.h"
slouken@1895
    37
slouken@6523
    38
/* Dropfile support */
slouken@6523
    39
#include <shellapi.h>
slouken@6523
    40
jorgen@7276
    41
/* For GET_X_LPARAM, GET_Y_LPARAM. */
jorgen@7276
    42
#include <windowsx.h>
slouken@6523
    43
gabomdq@7678
    44
/* #define WMMSG_DEBUG */
slouken@1895
    45
#ifdef WMMSG_DEBUG
slouken@7191
    46
#include <stdio.h>
slouken@1895
    47
#include "wmmsg.h"
slouken@1895
    48
#endif
slouken@1895
    49
dludwig@9668
    50
/* For processing mouse WM_*BUTTON* and WM_MOUSEMOVE message-data from GetMessageExtraInfo() */
dludwig@9668
    51
#define MOUSEEVENTF_FROMTOUCH 0xFF515700
dludwig@9668
    52
slouken@1895
    53
/* Masks for processing the windows KEYDOWN and KEYUP messages */
slouken@2317
    54
#define REPEATED_KEYMASK    (1<<30)
slouken@2317
    55
#define EXTENDED_KEYMASK    (1<<24)
slouken@1895
    56
bob@2324
    57
#define VK_ENTER    10          /* Keypad Enter ... no VKEY defined? */
slouken@6931
    58
#ifndef VK_OEM_NEC_EQUAL
slouken@7191
    59
#define VK_OEM_NEC_EQUAL 0x92
slouken@6931
    60
#endif
slouken@2313
    61
icculus@2127
    62
/* Make sure XBUTTON stuff is defined that isn't in older Platform SDKs... */
icculus@2127
    63
#ifndef WM_XBUTTONDOWN
icculus@2127
    64
#define WM_XBUTTONDOWN 0x020B
icculus@2127
    65
#endif
icculus@2127
    66
#ifndef WM_XBUTTONUP
icculus@2127
    67
#define WM_XBUTTONUP 0x020C
icculus@2127
    68
#endif
icculus@2127
    69
#ifndef GET_XBUTTON_WPARAM
icculus@2127
    70
#define GET_XBUTTON_WPARAM(w) (HIWORD(w))
icculus@2127
    71
#endif
bobbens@2733
    72
#ifndef WM_INPUT
bobbens@2733
    73
#define WM_INPUT 0x00ff
bobbens@2733
    74
#endif
slouken@4932
    75
#ifndef WM_TOUCH
slouken@4868
    76
#define WM_TOUCH 0x0240
slouken@4932
    77
#endif
slouken@7911
    78
#ifndef WM_MOUSEHWHEEL
slouken@7911
    79
#define WM_MOUSEHWHEEL 0x020E
slouken@7911
    80
#endif
slouken@8624
    81
#ifndef WM_UNICHAR
slouken@8624
    82
#define WM_UNICHAR 0x0109
slouken@8624
    83
#endif
slouken@4919
    84
slouken@7191
    85
static SDL_Scancode
michael@11241
    86
VKeytoScancode(WPARAM vkey)
michael@11241
    87
{
michael@11241
    88
    switch (vkey) {
michael@11241
    89
    case VK_CLEAR: return SDL_SCANCODE_CLEAR;
michael@11241
    90
    case VK_MODECHANGE: return SDL_SCANCODE_MODE;
michael@11241
    91
    case VK_SELECT: return SDL_SCANCODE_SELECT;
michael@11241
    92
    case VK_EXECUTE: return SDL_SCANCODE_EXECUTE;
michael@11241
    93
    case VK_HELP: return SDL_SCANCODE_HELP;
michael@11241
    94
    case VK_PAUSE: return SDL_SCANCODE_PAUSE;
michael@11241
    95
    case VK_NUMLOCK: return SDL_SCANCODE_NUMLOCKCLEAR;
michael@11241
    96
michael@11241
    97
    case VK_F13: return SDL_SCANCODE_F13;
michael@11241
    98
    case VK_F14: return SDL_SCANCODE_F14;
michael@11241
    99
    case VK_F15: return SDL_SCANCODE_F15;
michael@11241
   100
    case VK_F16: return SDL_SCANCODE_F16;
michael@11241
   101
    case VK_F17: return SDL_SCANCODE_F17;
michael@11241
   102
    case VK_F18: return SDL_SCANCODE_F18;
michael@11241
   103
    case VK_F19: return SDL_SCANCODE_F19;
michael@11241
   104
    case VK_F20: return SDL_SCANCODE_F20;
michael@11241
   105
    case VK_F21: return SDL_SCANCODE_F21;
michael@11241
   106
    case VK_F22: return SDL_SCANCODE_F22;
michael@11241
   107
    case VK_F23: return SDL_SCANCODE_F23;
michael@11241
   108
    case VK_F24: return SDL_SCANCODE_F24;
michael@11241
   109
michael@11241
   110
    case VK_OEM_NEC_EQUAL: return SDL_SCANCODE_KP_EQUALS;
michael@11241
   111
    case VK_BROWSER_BACK: return SDL_SCANCODE_AC_BACK;
michael@11241
   112
    case VK_BROWSER_FORWARD: return SDL_SCANCODE_AC_FORWARD;
michael@11241
   113
    case VK_BROWSER_REFRESH: return SDL_SCANCODE_AC_REFRESH;
michael@11241
   114
    case VK_BROWSER_STOP: return SDL_SCANCODE_AC_STOP;
michael@11241
   115
    case VK_BROWSER_SEARCH: return SDL_SCANCODE_AC_SEARCH;
michael@11241
   116
    case VK_BROWSER_FAVORITES: return SDL_SCANCODE_AC_BOOKMARKS;
michael@11241
   117
    case VK_BROWSER_HOME: return SDL_SCANCODE_AC_HOME;
michael@11241
   118
    case VK_VOLUME_MUTE: return SDL_SCANCODE_AUDIOMUTE;
michael@11241
   119
    case VK_VOLUME_DOWN: return SDL_SCANCODE_VOLUMEDOWN;
michael@11241
   120
    case VK_VOLUME_UP: return SDL_SCANCODE_VOLUMEUP;
michael@11241
   121
michael@11241
   122
    case VK_MEDIA_NEXT_TRACK: return SDL_SCANCODE_AUDIONEXT;
michael@11241
   123
    case VK_MEDIA_PREV_TRACK: return SDL_SCANCODE_AUDIOPREV;
michael@11241
   124
    case VK_MEDIA_STOP: return SDL_SCANCODE_AUDIOSTOP;
michael@11241
   125
    case VK_MEDIA_PLAY_PAUSE: return SDL_SCANCODE_AUDIOPLAY;
michael@11241
   126
    case VK_LAUNCH_MAIL: return SDL_SCANCODE_MAIL;
michael@11241
   127
    case VK_LAUNCH_MEDIA_SELECT: return SDL_SCANCODE_MEDIASELECT;
michael@11241
   128
michael@11241
   129
    case VK_OEM_102: return SDL_SCANCODE_NONUSBACKSLASH;
michael@11241
   130
michael@11241
   131
    case VK_ATTN: return SDL_SCANCODE_SYSREQ;
michael@11241
   132
    case VK_CRSEL: return SDL_SCANCODE_CRSEL;
michael@11241
   133
    case VK_EXSEL: return SDL_SCANCODE_EXSEL;
michael@11241
   134
    case VK_OEM_CLEAR: return SDL_SCANCODE_CLEAR;
michael@11241
   135
michael@11241
   136
    case VK_LAUNCH_APP1: return SDL_SCANCODE_APP1;
michael@11241
   137
    case VK_LAUNCH_APP2: return SDL_SCANCODE_APP2;
michael@11241
   138
michael@11241
   139
    default: return SDL_SCANCODE_UNKNOWN;
michael@11241
   140
    }
michael@11241
   141
}
michael@11241
   142
michael@11241
   143
static SDL_Scancode
slouken@8813
   144
WindowsScanCodeToSDLScanCode(LPARAM lParam, WPARAM wParam)
jorgen@6922
   145
{
slouken@7191
   146
    SDL_Scancode code;
slouken@8813
   147
    int nScanCode = (lParam >> 16) & 0xFF;
slouken@11242
   148
    SDL_bool bIsExtended = (lParam & (1 << 24)) != 0;
jorgen@6922
   149
slouken@11242
   150
    code = VKeytoScancode(wParam);
jorgen@6922
   151
slouken@11242
   152
    if (code == SDL_SCANCODE_UNKNOWN && nScanCode <= 127) {
slouken@11242
   153
        code = windows_scancode_table[nScanCode];
slouken@7191
   154
slouken@11242
   155
        if (bIsExtended) {
slouken@11242
   156
            switch (code) {
slouken@11242
   157
            case SDL_SCANCODE_RETURN:
slouken@11242
   158
                code = SDL_SCANCODE_KP_ENTER;
slouken@11242
   159
                break;
slouken@11242
   160
            case SDL_SCANCODE_LALT:
slouken@11242
   161
                code = SDL_SCANCODE_RALT;
slouken@11242
   162
                break;
slouken@11242
   163
            case SDL_SCANCODE_LCTRL:
slouken@11242
   164
                code = SDL_SCANCODE_RCTRL;
slouken@11242
   165
                break;
slouken@11242
   166
            case SDL_SCANCODE_SLASH:
slouken@11242
   167
                code = SDL_SCANCODE_KP_DIVIDE;
slouken@11242
   168
                break;
slouken@11242
   169
            case SDL_SCANCODE_CAPSLOCK:
slouken@11242
   170
                code = SDL_SCANCODE_KP_PLUS;
slouken@11242
   171
                break;
slouken@11242
   172
            default:
slouken@11242
   173
                break;
slouken@11242
   174
            }
michael@11241
   175
        } else {
michael@11241
   176
            switch (code) {
slouken@11242
   177
            case SDL_SCANCODE_HOME:
slouken@11242
   178
                code = SDL_SCANCODE_KP_7;
slouken@11242
   179
                break;
slouken@11242
   180
            case SDL_SCANCODE_UP:
slouken@11242
   181
                code = SDL_SCANCODE_KP_8;
slouken@11242
   182
                break;
slouken@11242
   183
            case SDL_SCANCODE_PAGEUP:
slouken@11242
   184
                code = SDL_SCANCODE_KP_9;
slouken@11242
   185
                break;
slouken@11242
   186
            case SDL_SCANCODE_LEFT:
slouken@11242
   187
                code = SDL_SCANCODE_KP_4;
slouken@11242
   188
                break;
slouken@11242
   189
            case SDL_SCANCODE_RIGHT:
slouken@11242
   190
                code = SDL_SCANCODE_KP_6;
slouken@11242
   191
                break;
slouken@11242
   192
            case SDL_SCANCODE_END:
slouken@11242
   193
                code = SDL_SCANCODE_KP_1;
slouken@11242
   194
                break;
slouken@11242
   195
            case SDL_SCANCODE_DOWN:
slouken@11242
   196
                code = SDL_SCANCODE_KP_2;
slouken@11242
   197
                break;
slouken@11242
   198
            case SDL_SCANCODE_PAGEDOWN:
slouken@11242
   199
                code = SDL_SCANCODE_KP_3;
slouken@11242
   200
                break;
slouken@11242
   201
            case SDL_SCANCODE_INSERT:
slouken@11242
   202
                code = SDL_SCANCODE_KP_0;
slouken@11242
   203
                break;
slouken@11242
   204
            case SDL_SCANCODE_DELETE:
slouken@11242
   205
                code = SDL_SCANCODE_KP_PERIOD;
slouken@11242
   206
                break;
slouken@11242
   207
            case SDL_SCANCODE_PRINTSCREEN:
slouken@11242
   208
                code = SDL_SCANCODE_KP_MULTIPLY;
slouken@11242
   209
                break;
michael@11241
   210
            default:
michael@11241
   211
                break;
michael@11241
   212
            }
slouken@7191
   213
        }
slouken@7191
   214
    }
slouken@7191
   215
    return code;
jorgen@6922
   216
}
jorgen@6922
   217
slouken@10379
   218
static SDL_bool
slouken@10379
   219
WIN_ShouldIgnoreFocusClick()
slouken@10379
   220
{
slouken@10499
   221
    return !SDL_GetHintBoolean(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, SDL_FALSE);
slouken@10379
   222
}
jorgen@6922
   223
philipp@10982
   224
static void
dludwig@9668
   225
WIN_CheckWParamMouseButton(SDL_bool bwParamMousePressed, SDL_bool bSDLMousePressed, SDL_WindowData *data, Uint8 button, SDL_MouseID mouseID)
slouken@6943
   226
{
slouken@10377
   227
    if (data->focus_click_pending & SDL_BUTTON(button)) {
slouken@10377
   228
        /* Ignore the button click for activation */
slouken@10377
   229
        if (!bwParamMousePressed) {
slouken@10377
   230
            data->focus_click_pending &= ~SDL_BUTTON(button);
slouken@10377
   231
            if (!data->focus_click_pending) {
slouken@10377
   232
                WIN_UpdateClipCursor(data->window);
slouken@10377
   233
            }
slouken@10377
   234
        }
slouken@10379
   235
        if (WIN_ShouldIgnoreFocusClick()) {
slouken@10379
   236
            return;
slouken@10379
   237
        }
slouken@8916
   238
    }
slouken@8916
   239
slouken@8813
   240
    if (bwParamMousePressed && !bSDLMousePressed) {
dludwig@9668
   241
        SDL_SendMouseButton(data->window, mouseID, SDL_PRESSED, button);
slouken@8813
   242
    } else if (!bwParamMousePressed && bSDLMousePressed) {
dludwig@9668
   243
        SDL_SendMouseButton(data->window, mouseID, SDL_RELEASED, button);
slouken@7191
   244
    }
slouken@6943
   245
}
slouken@6943
   246
slouken@6943
   247
/*
slouken@6943
   248
* Some windows systems fail to send a WM_LBUTTONDOWN sometimes, but each mouse move contains the current button state also
slouken@6943
   249
*  so this funciton reconciles our view of the world with the current buttons reported by windows
slouken@6943
   250
*/
philipp@10982
   251
static void
dludwig@9668
   252
WIN_CheckWParamMouseButtons(WPARAM wParam, SDL_WindowData *data, SDL_MouseID mouseID)
slouken@6943
   253
{
slouken@8813
   254
    if (wParam != data->mouse_button_flags) {
slouken@8813
   255
        Uint32 mouseFlags = SDL_GetMouseState(NULL, NULL);
dludwig@9668
   256
        WIN_CheckWParamMouseButton((wParam & MK_LBUTTON), (mouseFlags & SDL_BUTTON_LMASK), data, SDL_BUTTON_LEFT, mouseID);
dludwig@9668
   257
        WIN_CheckWParamMouseButton((wParam & MK_MBUTTON), (mouseFlags & SDL_BUTTON_MMASK), data, SDL_BUTTON_MIDDLE, mouseID);
dludwig@9668
   258
        WIN_CheckWParamMouseButton((wParam & MK_RBUTTON), (mouseFlags & SDL_BUTTON_RMASK), data, SDL_BUTTON_RIGHT, mouseID);
dludwig@9668
   259
        WIN_CheckWParamMouseButton((wParam & MK_XBUTTON1), (mouseFlags & SDL_BUTTON_X1MASK), data, SDL_BUTTON_X1, mouseID);
dludwig@9668
   260
        WIN_CheckWParamMouseButton((wParam & MK_XBUTTON2), (mouseFlags & SDL_BUTTON_X2MASK), data, SDL_BUTTON_X2, mouseID);
slouken@7191
   261
        data->mouse_button_flags = wParam;
slouken@7191
   262
    }
slouken@6943
   263
}
slouken@6943
   264
slouken@6943
   265
philipp@10982
   266
static void
slouken@8813
   267
WIN_CheckRawMouseButtons(ULONG rawButtons, SDL_WindowData *data)
slouken@6943
   268
{
slouken@8813
   269
    if (rawButtons != data->mouse_button_flags) {
slouken@8813
   270
        Uint32 mouseFlags = SDL_GetMouseState(NULL, NULL);
slouken@8813
   271
        if ((rawButtons & RI_MOUSE_BUTTON_1_DOWN))
dludwig@9668
   272
            WIN_CheckWParamMouseButton((rawButtons & RI_MOUSE_BUTTON_1_DOWN), (mouseFlags & SDL_BUTTON_LMASK), data, SDL_BUTTON_LEFT, 0);
slouken@8813
   273
        if ((rawButtons & RI_MOUSE_BUTTON_1_UP))
dludwig@9668
   274
            WIN_CheckWParamMouseButton(!(rawButtons & RI_MOUSE_BUTTON_1_UP), (mouseFlags & SDL_BUTTON_LMASK), data, SDL_BUTTON_LEFT, 0);
slouken@8813
   275
        if ((rawButtons & RI_MOUSE_BUTTON_2_DOWN))
dludwig@9668
   276
            WIN_CheckWParamMouseButton((rawButtons & RI_MOUSE_BUTTON_2_DOWN), (mouseFlags & SDL_BUTTON_RMASK), data, SDL_BUTTON_RIGHT, 0);
slouken@8813
   277
        if ((rawButtons & RI_MOUSE_BUTTON_2_UP))
dludwig@9668
   278
            WIN_CheckWParamMouseButton(!(rawButtons & RI_MOUSE_BUTTON_2_UP), (mouseFlags & SDL_BUTTON_RMASK), data, SDL_BUTTON_RIGHT, 0);
slouken@8813
   279
        if ((rawButtons & RI_MOUSE_BUTTON_3_DOWN))
dludwig@9668
   280
            WIN_CheckWParamMouseButton((rawButtons & RI_MOUSE_BUTTON_3_DOWN), (mouseFlags & SDL_BUTTON_MMASK), data, SDL_BUTTON_MIDDLE, 0);
slouken@8813
   281
        if ((rawButtons & RI_MOUSE_BUTTON_3_UP))
dludwig@9668
   282
            WIN_CheckWParamMouseButton(!(rawButtons & RI_MOUSE_BUTTON_3_UP), (mouseFlags & SDL_BUTTON_MMASK), data, SDL_BUTTON_MIDDLE, 0);
slouken@8813
   283
        if ((rawButtons & RI_MOUSE_BUTTON_4_DOWN))
dludwig@9668
   284
            WIN_CheckWParamMouseButton((rawButtons & RI_MOUSE_BUTTON_4_DOWN), (mouseFlags & SDL_BUTTON_X1MASK), data, SDL_BUTTON_X1, 0);
slouken@8813
   285
        if ((rawButtons & RI_MOUSE_BUTTON_4_UP))
dludwig@9668
   286
            WIN_CheckWParamMouseButton(!(rawButtons & RI_MOUSE_BUTTON_4_UP), (mouseFlags & SDL_BUTTON_X1MASK), data, SDL_BUTTON_X1, 0);
slouken@8813
   287
        if ((rawButtons & RI_MOUSE_BUTTON_5_DOWN))
dludwig@9668
   288
            WIN_CheckWParamMouseButton((rawButtons & RI_MOUSE_BUTTON_5_DOWN), (mouseFlags & SDL_BUTTON_X2MASK), data, SDL_BUTTON_X2, 0);
slouken@8813
   289
        if ((rawButtons & RI_MOUSE_BUTTON_5_UP))
dludwig@9668
   290
            WIN_CheckWParamMouseButton(!(rawButtons & RI_MOUSE_BUTTON_5_UP), (mouseFlags & SDL_BUTTON_X2MASK), data, SDL_BUTTON_X2, 0);
slouken@7191
   291
        data->mouse_button_flags = rawButtons;
slouken@7191
   292
    }
slouken@6943
   293
}
slouken@6943
   294
philipp@10982
   295
static void
slouken@8813
   296
WIN_CheckAsyncMouseRelease(SDL_WindowData *data)
slouken@8212
   297
{
slouken@8212
   298
    Uint32 mouseFlags;
slouken@8212
   299
    SHORT keyState;
slouken@8212
   300
slouken@8212
   301
    /* mouse buttons may have changed state here, we need to resync them,
slouken@8212
   302
       but we will get a WM_MOUSEMOVE right away which will fix things up if in non raw mode also
slouken@8212
   303
    */
slouken@8813
   304
    mouseFlags = SDL_GetMouseState(NULL, NULL);
slouken@8212
   305
slouken@8813
   306
    keyState = GetAsyncKeyState(VK_LBUTTON);
slouken@8796
   307
    if (!(keyState & 0x8000)) {
dludwig@9668
   308
        WIN_CheckWParamMouseButton(SDL_FALSE, (mouseFlags & SDL_BUTTON_LMASK), data, SDL_BUTTON_LEFT, 0);
slouken@8796
   309
    }
slouken@8796
   310
    keyState = GetAsyncKeyState(VK_RBUTTON);
slouken@8796
   311
    if (!(keyState & 0x8000)) {
dludwig@9668
   312
        WIN_CheckWParamMouseButton(SDL_FALSE, (mouseFlags & SDL_BUTTON_RMASK), data, SDL_BUTTON_RIGHT, 0);
slouken@8796
   313
    }
slouken@8796
   314
    keyState = GetAsyncKeyState(VK_MBUTTON);
slouken@8796
   315
    if (!(keyState & 0x8000)) {
dludwig@9668
   316
        WIN_CheckWParamMouseButton(SDL_FALSE, (mouseFlags & SDL_BUTTON_MMASK), data, SDL_BUTTON_MIDDLE, 0);
slouken@8796
   317
    }
slouken@8796
   318
    keyState = GetAsyncKeyState(VK_XBUTTON1);
slouken@8796
   319
    if (!(keyState & 0x8000)) {
dludwig@9668
   320
        WIN_CheckWParamMouseButton(SDL_FALSE, (mouseFlags & SDL_BUTTON_X1MASK), data, SDL_BUTTON_X1, 0);
slouken@8796
   321
    }
slouken@8796
   322
    keyState = GetAsyncKeyState(VK_XBUTTON2);
slouken@8796
   323
    if (!(keyState & 0x8000)) {
dludwig@9668
   324
        WIN_CheckWParamMouseButton(SDL_FALSE, (mouseFlags & SDL_BUTTON_X2MASK), data, SDL_BUTTON_X2, 0);
slouken@8796
   325
    }
slouken@8212
   326
    data->mouse_button_flags = 0;
slouken@8212
   327
}
slouken@8212
   328
philipp@10982
   329
static BOOL
slouken@7645
   330
WIN_ConvertUTF32toUTF8(UINT32 codepoint, char * text)
slouken@7645
   331
{
slouken@7645
   332
    if (codepoint <= 0x7F) {
slouken@7645
   333
        text[0] = (char) codepoint;
slouken@7645
   334
        text[1] = '\0';
slouken@7645
   335
    } else if (codepoint <= 0x7FF) {
slouken@7645
   336
        text[0] = 0xC0 | (char) ((codepoint >> 6) & 0x1F);
slouken@7645
   337
        text[1] = 0x80 | (char) (codepoint & 0x3F);
slouken@7645
   338
        text[2] = '\0';
slouken@7645
   339
    } else if (codepoint <= 0xFFFF) {
slouken@7645
   340
        text[0] = 0xE0 | (char) ((codepoint >> 12) & 0x0F);
slouken@7645
   341
        text[1] = 0x80 | (char) ((codepoint >> 6) & 0x3F);
slouken@7645
   342
        text[2] = 0x80 | (char) (codepoint & 0x3F);
slouken@7645
   343
        text[3] = '\0';
slouken@7645
   344
    } else if (codepoint <= 0x10FFFF) {
slouken@7645
   345
        text[0] = 0xF0 | (char) ((codepoint >> 18) & 0x0F);
slouken@7645
   346
        text[1] = 0x80 | (char) ((codepoint >> 12) & 0x3F);
slouken@7645
   347
        text[2] = 0x80 | (char) ((codepoint >> 6) & 0x3F);
slouken@7645
   348
        text[3] = 0x80 | (char) (codepoint & 0x3F);
slouken@7645
   349
        text[4] = '\0';
slouken@7645
   350
    } else {
slouken@7645
   351
        return SDL_FALSE;
slouken@7645
   352
    }
slouken@7645
   353
    return SDL_TRUE;
slouken@7645
   354
}
slouken@7645
   355
andrewb@9829
   356
static SDL_bool
andrewb@9829
   357
ShouldGenerateWindowCloseOnAltF4(void)
andrewb@9829
   358
{
slouken@10499
   359
    return !SDL_GetHintBoolean(SDL_HINT_WINDOWS_NO_CLOSE_ON_ALT_F4, SDL_FALSE);
andrewb@9829
   360
}
andrewb@9829
   361
slouken@1895
   362
LRESULT CALLBACK
slouken@1895
   363
WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
slouken@1895
   364
{
slouken@1895
   365
    SDL_WindowData *data;
slouken@3566
   366
    LRESULT returnCode = -1;
slouken@1895
   367
slouken@1951
   368
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@4429
   369
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1951
   370
        SDL_SysWMmsg wmmsg;
slouken@1951
   371
slouken@1951
   372
        SDL_VERSION(&wmmsg.version);
slouken@4900
   373
        wmmsg.subsystem = SDL_SYSWM_WINDOWS;
slouken@5056
   374
        wmmsg.msg.win.hwnd = hwnd;
slouken@5056
   375
        wmmsg.msg.win.msg = msg;
slouken@5056
   376
        wmmsg.msg.win.wParam = wParam;
slouken@5056
   377
        wmmsg.msg.win.lParam = lParam;
slouken@1951
   378
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
   379
    }
slouken@1951
   380
slouken@1895
   381
    /* Get the window data for the window */
slouken@1895
   382
    data = (SDL_WindowData *) GetProp(hwnd, TEXT("SDL_WindowData"));
slouken@1895
   383
    if (!data) {
slouken@1895
   384
        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
slouken@1895
   385
    }
jimtla@4650
   386
slouken@1895
   387
#ifdef WMMSG_DEBUG
slouken@7941
   388
    {
slouken@7941
   389
        char message[1024];
slouken@7941
   390
        if (msg > MAX_WMMSG) {
slouken@7941
   391
            SDL_snprintf(message, sizeof(message), "Received windows message: %p UNKNOWN (%d) -- 0x%X, 0x%X\n", hwnd, msg, wParam, lParam);
slouken@7941
   392
        } else {
slouken@7941
   393
            SDL_snprintf(message, sizeof(message), "Received windows message: %p %s -- 0x%X, 0x%X\n", hwnd, wmtab[msg], wParam, lParam);
slouken@7941
   394
        }
slouken@7941
   395
        OutputDebugStringA(message);
slouken@7941
   396
    }
slouken@7816
   397
#endif /* WMMSG_DEBUG */
slouken@2710
   398
dewyatt@4752
   399
    if (IME_HandleMessage(hwnd, msg, wParam, &lParam, data->videodata))
dewyatt@4752
   400
        return 0;
slouken@1895
   401
slouken@1895
   402
    switch (msg) {
slouken@1895
   403
slouken@1895
   404
    case WM_SHOWWINDOW:
slouken@1895
   405
        {
slouken@1895
   406
            if (wParam) {
slouken@3685
   407
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1895
   408
            } else {
slouken@3685
   409
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1895
   410
            }
slouken@1895
   411
        }
slouken@1895
   412
        break;
slouken@1895
   413
slouken@1895
   414
    case WM_ACTIVATE:
slouken@1895
   415
        {
slouken@8796
   416
            POINT cursorPos;
slouken@1895
   417
            BOOL minimized;
slouken@1895
   418
slouken@1895
   419
            minimized = HIWORD(wParam);
slouken@1895
   420
            if (!minimized && (LOWORD(wParam) != WA_INACTIVE)) {
slouken@10539
   421
                if (LOWORD(wParam) == WA_CLICKACTIVE) {
slouken@10539
   422
                    if (GetAsyncKeyState(VK_LBUTTON)) {
slouken@10539
   423
                        data->focus_click_pending |= SDL_BUTTON_LMASK;
slouken@10539
   424
                    }
slouken@10539
   425
                    if (GetAsyncKeyState(VK_RBUTTON)) {
slouken@10539
   426
                        data->focus_click_pending |= SDL_BUTTON_RMASK;
slouken@10539
   427
                    }
slouken@10539
   428
                    if (GetAsyncKeyState(VK_MBUTTON)) {
slouken@10539
   429
                        data->focus_click_pending |= SDL_BUTTON_MMASK;
slouken@10539
   430
                    }
slouken@10539
   431
                    if (GetAsyncKeyState(VK_XBUTTON1)) {
slouken@10539
   432
                        data->focus_click_pending |= SDL_BUTTON_X1MASK;
slouken@10539
   433
                    }
slouken@10539
   434
                    if (GetAsyncKeyState(VK_XBUTTON2)) {
slouken@10539
   435
                        data->focus_click_pending |= SDL_BUTTON_X2MASK;
slouken@10539
   436
                    }
slouken@10377
   437
                }
slouken@10377
   438
                
slouken@3685
   439
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@4465
   440
                if (SDL_GetKeyboardFocus() != data->window) {
slouken@4465
   441
                    SDL_SetKeyboardFocus(data->window);
slouken@1895
   442
                }
slouken@8796
   443
                
slouken@8796
   444
                GetCursorPos(&cursorPos);
slouken@8796
   445
                ScreenToClient(hwnd, &cursorPos);
slouken@8796
   446
                SDL_SendMouseMotion(data->window, 0, 0, cursorPos.x, cursorPos.y);
slouken@8796
   447
                
slouken@8212
   448
                WIN_CheckAsyncMouseRelease(data);
slouken@6350
   449
slouken@4504
   450
                /*
slouken@4504
   451
                 * FIXME: Update keyboard state
slouken@4504
   452
                 */
slouken@4504
   453
                WIN_CheckClipboardUpdate(data->videodata);
icculus@9974
   454
icculus@9974
   455
                SDL_ToggleModState(KMOD_CAPS, (GetKeyState(VK_CAPITAL) & 0x0001) != 0);
icculus@9974
   456
                SDL_ToggleModState(KMOD_NUM, (GetKeyState(VK_NUMLOCK) & 0x0001) != 0);
slouken@1895
   457
            } else {
dludwig@9990
   458
                data->in_window_deactivation = SDL_TRUE;
dludwig@9990
   459
slouken@4465
   460
                if (SDL_GetKeyboardFocus() == data->window) {
slouken@4465
   461
                    SDL_SetKeyboardFocus(NULL);
slouken@10411
   462
                    WIN_ResetDeadKeys();
slouken@1895
   463
                }
slouken@8050
   464
slouken@8050
   465
                ClipCursor(NULL);
dludwig@9990
   466
dludwig@9990
   467
                data->in_window_deactivation = SDL_FALSE;
slouken@1895
   468
            }
slouken@1895
   469
        }
slouken@3566
   470
        returnCode = 0;
slouken@3566
   471
        break;
slouken@1895
   472
slouken@7191
   473
    case WM_MOUSEMOVE:
slouken@8071
   474
        {
slouken@8071
   475
            SDL_Mouse *mouse = SDL_GetMouse();
slouken@8071
   476
            if (!mouse->relative_mode || mouse->relative_mode_warp) {
dludwig@9668
   477
                SDL_MouseID mouseID = (((GetMessageExtraInfo() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH) ? SDL_TOUCH_MOUSEID : 0);
dludwig@9668
   478
                SDL_SendMouseMotion(data->window, mouseID, 0, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
slouken@8071
   479
            }
slouken@8071
   480
        }
slouken@7191
   481
        /* don't break here, fall through to check the wParam like the button presses */
slouken@7191
   482
    case WM_LBUTTONUP:
slouken@7191
   483
    case WM_RBUTTONUP:
slouken@7191
   484
    case WM_MBUTTONUP:
slouken@7191
   485
    case WM_XBUTTONUP:
slouken@7191
   486
    case WM_LBUTTONDOWN:
slouken@8713
   487
    case WM_LBUTTONDBLCLK:
slouken@7191
   488
    case WM_RBUTTONDOWN:
slouken@8713
   489
    case WM_RBUTTONDBLCLK:
slouken@7191
   490
    case WM_MBUTTONDOWN:
slouken@8713
   491
    case WM_MBUTTONDBLCLK:
slouken@7191
   492
    case WM_XBUTTONDOWN:
slouken@8713
   493
    case WM_XBUTTONDBLCLK:
slouken@8071
   494
        {
slouken@8071
   495
            SDL_Mouse *mouse = SDL_GetMouse();
slouken@8071
   496
            if (!mouse->relative_mode || mouse->relative_mode_warp) {
dludwig@9668
   497
                SDL_MouseID mouseID = (((GetMessageExtraInfo() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH) ? SDL_TOUCH_MOUSEID : 0);
dludwig@9668
   498
                WIN_CheckWParamMouseButtons(wParam, data, mouseID);
slouken@8071
   499
            }
slouken@8071
   500
        }
slouken@7191
   501
        break;
slouken@3139
   502
slouken@7191
   503
    case WM_INPUT:
slouken@8071
   504
        {
slouken@8071
   505
            SDL_Mouse *mouse = SDL_GetMouse();
slouken@8071
   506
            HRAWINPUT hRawInput = (HRAWINPUT)lParam;
slouken@8071
   507
            RAWINPUT inp;
slouken@8071
   508
            UINT size = sizeof(inp);
icculus@8942
   509
            const SDL_bool isRelative = mouse->relative_mode || mouse->relative_mode_warp;
icculus@8942
   510
            const SDL_bool isCapture = ((data->window->flags & SDL_WINDOW_MOUSE_CAPTURE) != 0);
slouken@6782
   511
icculus@8942
   512
            if (!isRelative || mouse->focus != data->window) {
icculus@8942
   513
                if (!isCapture) {
icculus@8942
   514
                    break;
icculus@8942
   515
                }
slouken@8071
   516
            }
slouken@6782
   517
slouken@8071
   518
            GetRawInputData(hRawInput, RID_INPUT, &inp, &size, sizeof(RAWINPUTHEADER));
slouken@6350
   519
slouken@8071
   520
            /* Mouse data */
slouken@8071
   521
            if (inp.header.dwType == RIM_TYPEMOUSE) {
icculus@8942
   522
                if (isRelative) {
slouken@8976
   523
                    RAWMOUSE* rawmouse = &inp.data.mouse;
slouken@6350
   524
slouken@8976
   525
                    if ((rawmouse->usFlags & 0x01) == MOUSE_MOVE_RELATIVE) {
slouken@8976
   526
                        SDL_SendMouseMotion(data->window, 0, 1, (int)rawmouse->lLastX, (int)rawmouse->lLastY);
icculus@8942
   527
                    } else {
icculus@8942
   528
                        /* synthesize relative moves from the abs position */
icculus@8942
   529
                        static SDL_Point initialMousePoint;
icculus@8942
   530
                        if (initialMousePoint.x == 0 && initialMousePoint.y == 0) {
slouken@8976
   531
                            initialMousePoint.x = rawmouse->lLastX;
slouken@8976
   532
                            initialMousePoint.y = rawmouse->lLastY;
icculus@8942
   533
                        }
icculus@8942
   534
slouken@10864
   535
                        SDL_SendMouseMotion(data->window, 0, 1, (int)(rawmouse->lLastX-initialMousePoint.x), (int)(rawmouse->lLastY-initialMousePoint.y));
icculus@8942
   536
slouken@8976
   537
                        initialMousePoint.x = rawmouse->lLastX;
slouken@8976
   538
                        initialMousePoint.y = rawmouse->lLastY;
slouken@8071
   539
                    }
slouken@8976
   540
                    WIN_CheckRawMouseButtons(rawmouse->usButtonFlags, data);
icculus@8942
   541
                } else if (isCapture) {
icculus@8942
   542
                    /* we check for where Windows thinks the system cursor lives in this case, so we don't really lose mouse accel, etc. */
icculus@8942
   543
                    POINT pt;
slouken@10863
   544
                    RECT hwndRect;
slouken@10864
   545
                    HWND currentHnd;
slouken@10864
   546
icculus@8942
   547
                    GetCursorPos(&pt);
slouken@10864
   548
                    currentHnd = WindowFromPoint(pt);
slouken@10864
   549
                    ScreenToClient(hwnd, &pt);
slouken@10864
   550
                    GetClientRect(hwnd, &hwndRect);
slouken@10863
   551
slouken@10863
   552
                    /* if in the window, WM_MOUSEMOVE, etc, will cover it. */
slouken@10864
   553
                    if(currentHnd != hwnd || pt.x < 0 || pt.y < 0 || pt.x > hwndRect.right || pt.y > hwndRect.right) {
slouken@10864
   554
                        SDL_SendMouseMotion(data->window, 0, 0, (int)pt.x, (int)pt.y);
icculus@8949
   555
                        SDL_SendMouseButton(data->window, 0, GetAsyncKeyState(VK_LBUTTON) & 0x8000 ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_LEFT);
icculus@8949
   556
                        SDL_SendMouseButton(data->window, 0, GetAsyncKeyState(VK_RBUTTON) & 0x8000 ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_RIGHT);
icculus@8949
   557
                        SDL_SendMouseButton(data->window, 0, GetAsyncKeyState(VK_MBUTTON) & 0x8000 ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_MIDDLE);
icculus@8949
   558
                        SDL_SendMouseButton(data->window, 0, GetAsyncKeyState(VK_XBUTTON1) & 0x8000 ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_X1);
icculus@8949
   559
                        SDL_SendMouseButton(data->window, 0, GetAsyncKeyState(VK_XBUTTON2) & 0x8000 ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_X2);
icculus@8942
   560
                    }
icculus@8942
   561
                } else {
icculus@8942
   562
                    SDL_assert(0 && "Shouldn't happen");
slouken@7191
   563
                }
slouken@7191
   564
            }
slouken@7191
   565
        }
slouken@7191
   566
        break;
slouken@6350
   567
slouken@5049
   568
    case WM_MOUSEWHEEL:
slouken@5049
   569
        {
slouken@7813
   570
            static short s_AccumulatedMotion;
slouken@5049
   571
slouken@7813
   572
            s_AccumulatedMotion += GET_WHEEL_DELTA_WPARAM(wParam);
slouken@7813
   573
            if (s_AccumulatedMotion > 0) {
slouken@7813
   574
                while (s_AccumulatedMotion >= WHEEL_DELTA) {
urkle@9257
   575
                    SDL_SendMouseWheel(data->window, 0, 0, 1, SDL_MOUSEWHEEL_NORMAL);
slouken@7813
   576
                    s_AccumulatedMotion -= WHEEL_DELTA;
slouken@7813
   577
                }
slouken@7813
   578
            } else {
slouken@7813
   579
                while (s_AccumulatedMotion <= -WHEEL_DELTA) {
urkle@9257
   580
                    SDL_SendMouseWheel(data->window, 0, 0, -1, SDL_MOUSEWHEEL_NORMAL);
slouken@7813
   581
                    s_AccumulatedMotion += WHEEL_DELTA;
slouken@7813
   582
                }
slouken@7813
   583
            }
slouken@5049
   584
        }
slouken@8071
   585
        break;
slouken@5049
   586
slouken@7911
   587
    case WM_MOUSEHWHEEL:
slouken@7911
   588
        {
slouken@7911
   589
            static short s_AccumulatedMotion;
slouken@7911
   590
slouken@7911
   591
            s_AccumulatedMotion += GET_WHEEL_DELTA_WPARAM(wParam);
slouken@7911
   592
            if (s_AccumulatedMotion > 0) {
slouken@7911
   593
                while (s_AccumulatedMotion >= WHEEL_DELTA) {
urkle@9257
   594
                    SDL_SendMouseWheel(data->window, 0, 1, 0, SDL_MOUSEWHEEL_NORMAL);
slouken@7911
   595
                    s_AccumulatedMotion -= WHEEL_DELTA;
slouken@7911
   596
                }
slouken@7911
   597
            } else {
slouken@7911
   598
                while (s_AccumulatedMotion <= -WHEEL_DELTA) {
urkle@9257
   599
                    SDL_SendMouseWheel(data->window, 0, -1, 0, SDL_MOUSEWHEEL_NORMAL);
slouken@7911
   600
                    s_AccumulatedMotion += WHEEL_DELTA;
slouken@7911
   601
                }
slouken@7911
   602
            }
slouken@7911
   603
        }
slouken@8071
   604
        break;
slouken@7911
   605
slouken@5086
   606
#ifdef WM_MOUSELEAVE
slouken@1895
   607
    case WM_MOUSELEAVE:
icculus@8942
   608
        if (SDL_GetMouseFocus() == data->window && !SDL_GetMouse()->relative_mode && !(data->window->flags & SDL_WINDOW_MOUSE_CAPTURE)) {
slouken@8035
   609
            if (!IsIconic(hwnd)) {
slouken@8035
   610
                POINT cursorPos;
slouken@8035
   611
                GetCursorPos(&cursorPos);
slouken@8035
   612
                ScreenToClient(hwnd, &cursorPos);
slouken@8035
   613
                SDL_SendMouseMotion(data->window, 0, 0, cursorPos.x, cursorPos.y);
slouken@8035
   614
            }
slouken@7191
   615
            SDL_SetMouseFocus(NULL);
slouken@1895
   616
        }
slouken@3566
   617
        returnCode = 0;
slouken@3566
   618
        break;
slouken@5086
   619
#endif /* WM_MOUSELEAVE */
slouken@1895
   620
slouken@7645
   621
    case WM_KEYDOWN:
slouken@7646
   622
    case WM_SYSKEYDOWN:
slouken@7645
   623
        {
slouken@8813
   624
            SDL_Scancode code = WindowsScanCodeToSDLScanCode(lParam, wParam);
icculus@9641
   625
            const Uint8 *keyboardState = SDL_GetKeyboardState(NULL);
icculus@9641
   626
icculus@9641
   627
            /* Detect relevant keyboard shortcuts */
icculus@9641
   628
            if (keyboardState[SDL_SCANCODE_LALT] == SDL_PRESSED || keyboardState[SDL_SCANCODE_RALT] == SDL_PRESSED) {
icculus@9641
   629
                /* ALT+F4: Close window */
andrewb@9829
   630
                if (code == SDL_SCANCODE_F4 && ShouldGenerateWindowCloseOnAltF4()) {
icculus@9641
   631
                    SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
icculus@9641
   632
                }
icculus@9641
   633
            }
icculus@9641
   634
slouken@8813
   635
            if (code != SDL_SCANCODE_UNKNOWN) {
slouken@8813
   636
                SDL_SendKeyboardKey(SDL_PRESSED, code);
slouken@7646
   637
            }
slouken@7646
   638
        }
slouken@9781
   639
 
slouken@3566
   640
        returnCode = 0;
slouken@3566
   641
        break;
slouken@1895
   642
slouken@1895
   643
    case WM_SYSKEYUP:
slouken@1895
   644
    case WM_KEYUP:
slouken@1895
   645
        {
slouken@8813
   646
            SDL_Scancode code = WindowsScanCodeToSDLScanCode(lParam, wParam);
gabomdq@7975
   647
            const Uint8 *keyboardState = SDL_GetKeyboardState(NULL);
gabomdq@7975
   648
slouken@8813
   649
            if (code != SDL_SCANCODE_UNKNOWN) {
slouken@6938
   650
                if (code == SDL_SCANCODE_PRINTSCREEN &&
gabomdq@7975
   651
                    keyboardState[code] == SDL_RELEASED) {
slouken@6938
   652
                    SDL_SendKeyboardKey(SDL_PRESSED, code);
slouken@6938
   653
                }
slouken@6938
   654
                SDL_SendKeyboardKey(SDL_RELEASED, code);
slouken@2308
   655
            }
slouken@1895
   656
        }
slouken@3566
   657
        returnCode = 0;
slouken@3566
   658
        break;
slouken@1895
   659
slouken@8143
   660
    case WM_UNICHAR:
slouken@9889
   661
        if ( wParam == UNICODE_NOCHAR ) {
slouken@9889
   662
            returnCode = 1;
slouken@9889
   663
            break;
slouken@9889
   664
        }
slouken@9889
   665
        /* otherwise fall through to below */
slouken@9889
   666
    case WM_CHAR:
slouken@9889
   667
        {
slouken@9889
   668
            char text[5];
slouken@9889
   669
            if ( WIN_ConvertUTF32toUTF8( (UINT32)wParam, text ) ) {
slouken@9889
   670
                SDL_SendKeyboardText( text );
slouken@9889
   671
            }
slouken@9889
   672
        }
slouken@9889
   673
        returnCode = 0;
slouken@8143
   674
        break;
slouken@8143
   675
slouken@5086
   676
#ifdef WM_INPUTLANGCHANGE
slouken@2311
   677
    case WM_INPUTLANGCHANGE:
slouken@2311
   678
        {
slouken@4465
   679
            WIN_UpdateKeymap();
slouken@9898
   680
            SDL_SendKeymapChangedEvent();
slouken@2311
   681
        }
slouken@3566
   682
        returnCode = 1;
slouken@3566
   683
        break;
slouken@5086
   684
#endif /* WM_INPUTLANGCHANGE */
slouken@2311
   685
slouken@8253
   686
    case WM_NCLBUTTONDOWN:
slouken@8253
   687
        {
slouken@8253
   688
            data->in_title_click = SDL_TRUE;
slouken@8253
   689
        }
slouken@8253
   690
        break;
slouken@8253
   691
slouken@8916
   692
    case WM_CAPTURECHANGED:
slouken@8253
   693
        {
slouken@8253
   694
            data->in_title_click = SDL_FALSE;
slouken@8253
   695
slouken@8916
   696
            /* The mouse may have been released during a modal loop */
slouken@8212
   697
            WIN_CheckAsyncMouseRelease(data);
slouken@8036
   698
        }
slouken@8036
   699
        break;
slouken@8036
   700
slouken@5086
   701
#ifdef WM_GETMINMAXINFO
slouken@1895
   702
    case WM_GETMINMAXINFO:
slouken@1895
   703
        {
slouken@1895
   704
            MINMAXINFO *info;
slouken@1895
   705
            RECT size;
slouken@1895
   706
            int x, y;
slouken@1895
   707
            int w, h;
stopiccot@6682
   708
            int min_w, min_h;
slouken@6788
   709
            int max_w, max_h;
slouken@1895
   710
            int style;
slouken@3168
   711
            BOOL menu;
slouken@7191
   712
            BOOL constrain_max_size;
slouken@1895
   713
stopiccot@6682
   714
            if (SDL_IsShapedWindow(data->window))
eligottlieb@4815
   715
                Win32_ResizeWindowShape(data->window);
slouken@1895
   716
slouken@7941
   717
            /* If this is an expected size change, allow it */
slouken@7941
   718
            if (data->expected_resize) {
slouken@7941
   719
                break;
slouken@7941
   720
            }
slouken@7941
   721
slouken@1895
   722
            /* Get the current position of our window */
slouken@1895
   723
            GetWindowRect(hwnd, &size);
slouken@1895
   724
            x = size.left;
slouken@1895
   725
            y = size.top;
slouken@1895
   726
slouken@1895
   727
            /* Calculate current size of our window */
slouken@3685
   728
            SDL_GetWindowSize(data->window, &w, &h);
stopiccot@6682
   729
            SDL_GetWindowMinimumSize(data->window, &min_w, &min_h);
slouken@6788
   730
            SDL_GetWindowMaximumSize(data->window, &max_w, &max_h);
stopiccot@6682
   731
slouken@7191
   732
            /* Store in min_w and min_h difference between current size and minimal
stopiccot@6682
   733
               size so we don't need to call AdjustWindowRectEx twice */
stopiccot@6682
   734
            min_w -= w;
stopiccot@6682
   735
            min_h -= h;
slouken@6837
   736
            if (max_w && max_h) {
slouken@6837
   737
                max_w -= w;
slouken@6837
   738
                max_h -= h;
slouken@6862
   739
                constrain_max_size = TRUE;
slouken@6862
   740
            } else {
slouken@6862
   741
                constrain_max_size = FALSE;
slouken@6837
   742
            }
stopiccot@6682
   743
slouken@1895
   744
            size.top = 0;
slouken@1895
   745
            size.left = 0;
slouken@1895
   746
            size.bottom = h;
slouken@1895
   747
            size.right = w;
slouken@1895
   748
slouken@3168
   749
            style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   750
            /* DJM - according to the docs for GetMenu(), the
slouken@1895
   751
               return value is undefined if hwnd is a child window.
slouken@8817
   752
               Apparently it's too difficult for MS to check
slouken@1895
   753
               inside their function, so I have to do it here.
slouken@1895
   754
             */
slouken@3168
   755
            menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   756
            AdjustWindowRectEx(&size, style, menu, 0);
slouken@1895
   757
            w = size.right - size.left;
slouken@1895
   758
            h = size.bottom - size.top;
slouken@1895
   759
slouken@1895
   760
            /* Fix our size to the current size */
slouken@1895
   761
            info = (MINMAXINFO *) lParam;
stopiccot@6682
   762
            if (SDL_GetWindowFlags(data->window) & SDL_WINDOW_RESIZABLE) {
stopiccot@6682
   763
                info->ptMinTrackSize.x = w + min_w;
stopiccot@6682
   764
                info->ptMinTrackSize.y = h + min_h;
slouken@6862
   765
                if (constrain_max_size) {
slouken@6862
   766
                    info->ptMaxTrackSize.x = w + max_w;
slouken@6862
   767
                    info->ptMaxTrackSize.y = h + max_h;
slouken@6862
   768
                }
stopiccot@6682
   769
            } else {
stopiccot@6682
   770
                info->ptMaxSize.x = w;
stopiccot@6682
   771
                info->ptMaxSize.y = h;
stopiccot@6682
   772
                info->ptMaxPosition.x = x;
stopiccot@6682
   773
                info->ptMaxPosition.y = y;
stopiccot@6682
   774
                info->ptMinTrackSize.x = w;
stopiccot@6682
   775
                info->ptMinTrackSize.y = h;
stopiccot@6682
   776
                info->ptMaxTrackSize.x = w;
stopiccot@6682
   777
                info->ptMaxTrackSize.y = h;
stopiccot@6682
   778
            }
slouken@1895
   779
        }
slouken@3566
   780
        returnCode = 0;
slouken@3566
   781
        break;
slouken@5086
   782
#endif /* WM_GETMINMAXINFO */
slouken@1895
   783
icculus@10387
   784
    case WM_WINDOWPOSCHANGING:
icculus@10387
   785
icculus@10387
   786
        if (data->expected_resize) {
icculus@10387
   787
            returnCode = 0;
icculus@10387
   788
        }
icculus@10387
   789
        break;
icculus@10387
   790
slouken@1895
   791
    case WM_WINDOWPOSCHANGED:
slouken@1895
   792
        {
slouken@1895
   793
            RECT rect;
slouken@1895
   794
            int x, y;
slouken@1895
   795
            int w, h;
slouken@8817
   796
            
slouken@9888
   797
            if (data->initializing || data->in_border_change) {
slouken@8817
   798
                break;
slouken@8817
   799
            }
slouken@1895
   800
slouken@8036
   801
            if (!GetClientRect(hwnd, &rect) || IsRectEmpty(&rect)) {
slouken@3256
   802
                break;
slouken@3256
   803
            }
slouken@1895
   804
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   805
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   806
slouken@8036
   807
            WIN_UpdateClipCursor(data->window);
slouken@1895
   808
slouken@1895
   809
            x = rect.left;
slouken@1895
   810
            y = rect.top;
slouken@3685
   811
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1895
   812
slouken@1895
   813
            w = rect.right - rect.left;
slouken@1895
   814
            h = rect.bottom - rect.top;
slouken@3685
   815
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED, w,
slouken@1895
   816
                                h);
flibitijibibo@10417
   817
flibitijibibo@10417
   818
            /* Forces a WM_PAINT event */
flibitijibibo@10417
   819
            InvalidateRect(hwnd, NULL, FALSE);
slouken@1895
   820
        }
slouken@1895
   821
        break;
slouken@1895
   822
slouken@7941
   823
    case WM_SIZE:
slouken@7941
   824
        {
slouken@8813
   825
            switch (wParam) {
slouken@7941
   826
            case SIZE_MAXIMIZED:
slouken@7941
   827
                SDL_SendWindowEvent(data->window,
slouken@11007
   828
                    SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@11007
   829
                SDL_SendWindowEvent(data->window,
slouken@7941
   830
                    SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
slouken@7941
   831
                break;
slouken@7941
   832
            case SIZE_MINIMIZED:
slouken@7941
   833
                SDL_SendWindowEvent(data->window,
slouken@7941
   834
                    SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@7941
   835
                break;
slouken@7941
   836
            default:
slouken@7941
   837
                SDL_SendWindowEvent(data->window,
slouken@7941
   838
                    SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@7941
   839
                break;
slouken@7941
   840
            }
slouken@7941
   841
        }
slouken@7941
   842
        break;
slouken@7941
   843
slouken@1895
   844
    case WM_SETCURSOR:
slouken@1895
   845
        {
slouken@3076
   846
            Uint16 hittest;
slouken@1895
   847
slouken@3076
   848
            hittest = LOWORD(lParam);
slouken@3076
   849
            if (hittest == HTCLIENT) {
slouken@5421
   850
                SetCursor(SDL_cursor);
slouken@3566
   851
                returnCode = TRUE;
slouken@8813
   852
            } else if (!g_WindowFrameUsableWhileCursorHidden && !SDL_cursor) {
slouken@8813
   853
                SetCursor(NULL);
slouken@8813
   854
                returnCode = TRUE;
slouken@3076
   855
            }
slouken@1895
   856
        }
slouken@1895
   857
        break;
slouken@1895
   858
slouken@1895
   859
        /* We were occluded, refresh our display */
slouken@1895
   860
    case WM_PAINT:
slouken@1895
   861
        {
slouken@1895
   862
            RECT rect;
slouken@1895
   863
            if (GetUpdateRect(hwnd, &rect, FALSE)) {
slouken@7295
   864
                ValidateRect(hwnd, NULL);
slouken@3685
   865
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED,
slouken@1895
   866
                                    0, 0);
slouken@1895
   867
            }
slouken@1895
   868
        }
slouken@3566
   869
        returnCode = 0;
slouken@3566
   870
        break;
slouken@3095
   871
slouken@1895
   872
        /* We'll do our own drawing, prevent flicker */
slouken@1895
   873
    case WM_ERASEBKGND:
slouken@1895
   874
        {
slouken@1895
   875
        }
slouken@1895
   876
        return (1);
slouken@1895
   877
slouken@1895
   878
    case WM_SYSCOMMAND:
slouken@1895
   879
        {
slouken@9889
   880
            if ((wParam & 0xFFF0) == SC_KEYMENU) {
slouken@9889
   881
                return (0);
slouken@9889
   882
            }
icculus@9624
   883
icculus@9624
   884
#if defined(SC_SCREENSAVE) || defined(SC_MONITORPOWER)
slouken@1895
   885
            /* Don't start the screensaver or blank the monitor in fullscreen apps */
slouken@1895
   886
            if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
slouken@1895
   887
                (wParam & 0xFFF0) == SC_MONITORPOWER) {
slouken@3032
   888
                if (SDL_GetVideoDevice()->suspend_screensaver) {
slouken@1895
   889
                    return (0);
slouken@1895
   890
                }
slouken@1895
   891
            }
icculus@9624
   892
#endif /* System has screensaver support */
slouken@1895
   893
        }
slouken@1895
   894
        break;
slouken@1895
   895
slouken@1895
   896
    case WM_CLOSE:
slouken@1895
   897
        {
slouken@3685
   898
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1895
   899
        }
slouken@3566
   900
        returnCode = 0;
slouken@3566
   901
        break;
slouken@4919
   902
slouken@7191
   903
    case WM_TOUCH:
slouken@11215
   904
        if (data->videodata->GetTouchInputInfo && data->videodata->CloseTouchInputHandle) {
slouken@7191
   905
            UINT i, num_inputs = LOWORD(wParam);
slouken@7191
   906
            PTOUCHINPUT inputs = SDL_stack_alloc(TOUCHINPUT, num_inputs);
slouken@7191
   907
            if (data->videodata->GetTouchInputInfo((HTOUCHINPUT)lParam, num_inputs, inputs, sizeof(TOUCHINPUT))) {
slouken@7191
   908
                RECT rect;
slouken@7191
   909
                float x, y;
slouken@4919
   910
slouken@7191
   911
                if (!GetClientRect(hwnd, &rect) ||
slouken@7191
   912
                    (rect.right == rect.left && rect.bottom == rect.top)) {
philipp@7631
   913
                    if (inputs) {
philipp@7631
   914
                        SDL_stack_free(inputs);
philipp@7631
   915
                    }
slouken@7191
   916
                    break;
slouken@7191
   917
                }
slouken@7191
   918
                ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@7191
   919
                ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@7191
   920
                rect.top *= 100;
slouken@7191
   921
                rect.left *= 100;
slouken@7191
   922
                rect.bottom *= 100;
slouken@7191
   923
                rect.right *= 100;
slouken@4932
   924
slouken@7191
   925
                for (i = 0; i < num_inputs; ++i) {
slouken@7191
   926
                    PTOUCHINPUT input = &inputs[i];
slouken@4919
   927
icculus@7543
   928
                    const SDL_TouchID touchId = (SDL_TouchID)((size_t)input->hSource);
slouken@9679
   929
                    if (SDL_AddTouch(touchId, "") < 0) {
slouken@9679
   930
                        continue;
slouken@7191
   931
                    }
slouken@4932
   932
slouken@7191
   933
                    /* Get the normalized coordinates for the window */
slouken@7191
   934
                    x = (float)(input->x - rect.left)/(rect.right - rect.left);
slouken@7191
   935
                    y = (float)(input->y - rect.top)/(rect.bottom - rect.top);
slouken@4932
   936
slouken@7191
   937
                    if (input->dwFlags & TOUCHEVENTF_DOWN) {
slouken@7191
   938
                        SDL_SendTouch(touchId, input->dwID, SDL_TRUE, x, y, 1.0f);
slouken@7191
   939
                    }
slouken@7191
   940
                    if (input->dwFlags & TOUCHEVENTF_MOVE) {
slouken@7191
   941
                        SDL_SendTouchMotion(touchId, input->dwID, x, y, 1.0f);
slouken@7191
   942
                    }
slouken@7191
   943
                    if (input->dwFlags & TOUCHEVENTF_UP) {
slouken@7191
   944
                        SDL_SendTouch(touchId, input->dwID, SDL_FALSE, x, y, 1.0f);
slouken@7191
   945
                    }
slouken@7191
   946
                }
slouken@7191
   947
            }
slouken@7191
   948
            SDL_stack_free(inputs);
slouken@4932
   949
slouken@7191
   950
            data->videodata->CloseTouchInputHandle((HTOUCHINPUT)lParam);
slouken@7191
   951
            return 0;
slouken@7191
   952
        }
slouken@7191
   953
        break;
slouken@6523
   954
slouken@6523
   955
    case WM_DROPFILES:
slouken@6523
   956
        {
slouken@6523
   957
            UINT i;
slouken@6523
   958
            HDROP drop = (HDROP) wParam;
slouken@6523
   959
            UINT count = DragQueryFile(drop, 0xFFFFFFFF, NULL, 0);
slouken@6523
   960
            for (i = 0; i < count; ++i) {
slouken@6523
   961
                UINT size = DragQueryFile(drop, i, NULL, 0) + 1;
slouken@6523
   962
                LPTSTR buffer = SDL_stack_alloc(TCHAR, size);
slouken@6523
   963
                if (buffer) {
slouken@6523
   964
                    if (DragQueryFile(drop, i, buffer, size)) {
slouken@6523
   965
                        char *file = WIN_StringToUTF8(buffer);
icculus@10022
   966
                        SDL_SendDropFile(data->window, file);
slouken@6523
   967
                        SDL_free(file);
slouken@6523
   968
                    }
slouken@6523
   969
                    SDL_stack_free(buffer);
slouken@6523
   970
                }
slouken@6523
   971
            }
icculus@10022
   972
            SDL_SendDropComplete(data->window);
slouken@6523
   973
            DragFinish(drop);
slouken@6523
   974
            return 0;
slouken@6523
   975
        }
slouken@6523
   976
        break;
icculus@8938
   977
icculus@8938
   978
    case WM_NCHITTEST:
icculus@8938
   979
        {
icculus@8938
   980
            SDL_Window *window = data->window;
icculus@8938
   981
            if (window->hit_test) {
icculus@8941
   982
                POINT winpoint = { (int) LOWORD(lParam), (int) HIWORD(lParam) };
slouken@8976
   983
                if (ScreenToClient(hwnd, &winpoint)) {
icculus@8941
   984
                    const SDL_Point point = { (int) winpoint.x, (int) winpoint.y };
icculus@8941
   985
                    const SDL_HitTestResult rc = window->hit_test(window, &point, window->hit_test_data);
icculus@8948
   986
                    switch (rc) {
icculus@10028
   987
                        #define POST_HIT_TEST(ret) { SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIT_TEST, 0, 0); return ret; }
icculus@10028
   988
                        case SDL_HITTEST_DRAGGABLE: POST_HIT_TEST(HTCAPTION);
icculus@10028
   989
                        case SDL_HITTEST_RESIZE_TOPLEFT: POST_HIT_TEST(HTTOPLEFT);
icculus@10028
   990
                        case SDL_HITTEST_RESIZE_TOP: POST_HIT_TEST(HTTOP);
icculus@10028
   991
                        case SDL_HITTEST_RESIZE_TOPRIGHT: POST_HIT_TEST(HTTOPRIGHT);
icculus@10028
   992
                        case SDL_HITTEST_RESIZE_RIGHT: POST_HIT_TEST(HTRIGHT);
icculus@10028
   993
                        case SDL_HITTEST_RESIZE_BOTTOMRIGHT: POST_HIT_TEST(HTBOTTOMRIGHT);
icculus@10028
   994
                        case SDL_HITTEST_RESIZE_BOTTOM: POST_HIT_TEST(HTBOTTOM);
icculus@10028
   995
                        case SDL_HITTEST_RESIZE_BOTTOMLEFT: POST_HIT_TEST(HTBOTTOMLEFT);
icculus@10028
   996
                        case SDL_HITTEST_RESIZE_LEFT: POST_HIT_TEST(HTLEFT);
icculus@10028
   997
                        #undef POST_HIT_TEST
slouken@8976
   998
                        case SDL_HITTEST_NORMAL: return HTCLIENT;
icculus@8941
   999
                    }
icculus@8938
  1000
                }
icculus@8948
  1001
                /* If we didn't return, this will call DefWindowProc below. */
icculus@8938
  1002
            }
icculus@8938
  1003
        }
icculus@8938
  1004
        break;
icculus@8938
  1005
slouken@6523
  1006
    }
slouken@3566
  1007
slouken@3566
  1008
    /* If there's a window proc, assume it's going to handle messages */
slouken@3566
  1009
    if (data->wndproc) {
slouken@3566
  1010
        return CallWindowProc(data->wndproc, hwnd, msg, wParam, lParam);
slouken@3566
  1011
    } else if (returnCode >= 0) {
slouken@3566
  1012
        return returnCode;
slouken@3566
  1013
    } else {
slouken@3566
  1014
        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
slouken@3566
  1015
    }
slouken@1895
  1016
}
slouken@1895
  1017
slouken@9597
  1018
/* A message hook called before TranslateMessage() */
slouken@9597
  1019
static SDL_WindowsMessageHook g_WindowsMessageHook = NULL;
slouken@9599
  1020
static void *g_WindowsMessageHookData = NULL;
slouken@9597
  1021
slouken@9599
  1022
void SDL_SetWindowsMessageHook(SDL_WindowsMessageHook callback, void *userdata)
slouken@9597
  1023
{
slouken@9597
  1024
    g_WindowsMessageHook = callback;
slouken@9599
  1025
    g_WindowsMessageHookData = userdata;
slouken@9597
  1026
}
slouken@9597
  1027
slouken@1895
  1028
void
slouken@1895
  1029
WIN_PumpEvents(_THIS)
slouken@1895
  1030
{
icculus@7561
  1031
    const Uint8 *keystate;
slouken@1895
  1032
    MSG msg;
slouken@8218
  1033
    DWORD start_ticks = GetTickCount();
slouken@8218
  1034
slouken@8814
  1035
    if (g_WindowsEnableMessageLoop) {
slouken@8814
  1036
        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
slouken@9597
  1037
            if (g_WindowsMessageHook) {
slouken@9599
  1038
                g_WindowsMessageHook(g_WindowsMessageHookData, msg.hwnd, msg.message, msg.wParam, msg.lParam);
slouken@9597
  1039
            }
slouken@9597
  1040
slouken@8814
  1041
            /* Always translate the message in case it's a non-SDL window (e.g. with Qt integration) */
slouken@8814
  1042
            TranslateMessage(&msg);
slouken@8814
  1043
            DispatchMessage(&msg);
slouken@8218
  1044
slouken@8814
  1045
            /* Make sure we don't busy loop here forever if there are lots of events coming in */
slouken@8814
  1046
            if (SDL_TICKS_PASSED(msg.time, start_ticks)) {
slouken@8814
  1047
                break;
slouken@8814
  1048
            }
slouken@8218
  1049
        }
slouken@1895
  1050
    }
icculus@7561
  1051
icculus@7561
  1052
    /* Windows loses a shift KEYUP event when you have both pressed at once and let go of one.
icculus@7561
  1053
       You won't get a KEYUP until both are released, and that keyup will only be for the second
icculus@7561
  1054
       key you released. Take heroic measures and check the keystate as of the last handled event,
icculus@7561
  1055
       and if we think a key is pressed when Windows doesn't, unstick it in SDL's state. */
icculus@7561
  1056
    keystate = SDL_GetKeyboardState(NULL);
icculus@7561
  1057
    if ((keystate[SDL_SCANCODE_LSHIFT] == SDL_PRESSED) && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
icculus@7561
  1058
        SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
icculus@7561
  1059
    }
icculus@7561
  1060
    if ((keystate[SDL_SCANCODE_RSHIFT] == SDL_PRESSED) && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
icculus@7561
  1061
        SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RSHIFT);
icculus@7561
  1062
    }
slouken@1895
  1063
}
slouken@1895
  1064
slouken@1895
  1065
static int app_registered = 0;
slouken@1895
  1066
LPTSTR SDL_Appname = NULL;
slouken@1895
  1067
Uint32 SDL_Appstyle = 0;
slouken@1895
  1068
HINSTANCE SDL_Instance = NULL;
slouken@1895
  1069
slouken@1895
  1070
/* Register the class for this application */
slouken@1895
  1071
int
slouken@1895
  1072
SDL_RegisterApp(char *name, Uint32 style, void *hInst)
slouken@1895
  1073
{
slouken@11261
  1074
    const char *hint;
slouken@10426
  1075
    WNDCLASSEX wcex;
slouken@10426
  1076
    TCHAR path[MAX_PATH];
slouken@1895
  1077
slouken@1895
  1078
    /* Only do this once... */
slouken@1895
  1079
    if (app_registered) {
slouken@1895
  1080
        ++app_registered;
slouken@1895
  1081
        return (0);
slouken@1895
  1082
    }
slouken@1895
  1083
    if (!name && !SDL_Appname) {
slouken@1895
  1084
        name = "SDL_app";
slouken@5086
  1085
#if defined(CS_BYTEALIGNCLIENT) || defined(CS_OWNDC)
slouken@1895
  1086
        SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC);
slouken@5086
  1087
#endif
slouken@1895
  1088
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
  1089
    }
slouken@1895
  1090
slouken@1895
  1091
    if (name) {
slouken@1895
  1092
        SDL_Appname = WIN_UTF8ToString(name);
slouken@1895
  1093
        SDL_Appstyle = style;
slouken@1895
  1094
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
  1095
    }
slouken@1895
  1096
slouken@1895
  1097
    /* Register the application class */
slouken@10427
  1098
    wcex.cbSize         = sizeof(WNDCLASSEX);
slouken@10427
  1099
    wcex.hCursor        = NULL;
slouken@10427
  1100
    wcex.hIcon          = NULL;
slouken@10427
  1101
    wcex.hIconSm        = NULL;
slouken@10427
  1102
    wcex.lpszMenuName   = NULL;
slouken@10427
  1103
    wcex.lpszClassName  = SDL_Appname;
slouken@10427
  1104
    wcex.style          = SDL_Appstyle;
slouken@10427
  1105
    wcex.hbrBackground  = NULL;
slouken@10427
  1106
    wcex.lpfnWndProc    = WIN_WindowProc;
slouken@10427
  1107
    wcex.hInstance      = SDL_Instance;
slouken@10427
  1108
    wcex.cbClsExtra     = 0;
slouken@10427
  1109
    wcex.cbWndExtra     = 0;
slouken@10427
  1110
slouken@11261
  1111
    hint = SDL_GetHint(SDL_HINT_WINDOWS_INTRESOURCE_ICON);
slouken@11261
  1112
    if (hint && *hint) {
slouken@11261
  1113
        wcex.hIcon = LoadIcon(SDL_Instance, MAKEINTRESOURCE(SDL_atoi(hint)));
slouken@11261
  1114
slouken@11261
  1115
        hint = SDL_GetHint(SDL_HINT_WINDOWS_INTRESOURCE_ICON_SMALL);
slouken@11261
  1116
        if (hint && *hint) {
slouken@11261
  1117
            wcex.hIconSm = LoadIcon(SDL_Instance, MAKEINTRESOURCE(SDL_atoi(hint)));
slouken@11261
  1118
        }
slouken@11261
  1119
    } else {
slouken@11261
  1120
        /* Use the first icon as a default icon, like in the Explorer */
slouken@11261
  1121
        GetModuleFileName(SDL_Instance, path, MAX_PATH);
slouken@11261
  1122
        ExtractIconEx(path, 0, &wcex.hIcon, &wcex.hIconSm, 1);
slouken@11261
  1123
    }
slouken@10427
  1124
slouken@10426
  1125
    if (!RegisterClassEx(&wcex)) {
icculus@7037
  1126
        return SDL_SetError("Couldn't register application class");
slouken@1895
  1127
    }
slouken@1895
  1128
slouken@1895
  1129
    app_registered = 1;
icculus@7037
  1130
    return 0;
slouken@1895
  1131
}
slouken@1895
  1132
slouken@1895
  1133
/* Unregisters the windowclass registered in SDL_RegisterApp above. */
slouken@1895
  1134
void
slouken@1895
  1135
SDL_UnregisterApp()
slouken@1895
  1136
{
slouken@10426
  1137
    WNDCLASSEX wcex;
slouken@1895
  1138
slouken@1895
  1139
    /* SDL_RegisterApp might not have been called before */
slouken@1895
  1140
    if (!app_registered) {
slouken@1895
  1141
        return;
slouken@1895
  1142
    }
slouken@1895
  1143
    --app_registered;
slouken@1895
  1144
    if (app_registered == 0) {
slouken@1895
  1145
        /* Check for any registered window classes. */
slouken@10426
  1146
        if (GetClassInfoEx(SDL_Instance, SDL_Appname, &wcex)) {
slouken@1895
  1147
            UnregisterClass(SDL_Appname, SDL_Instance);
slouken@10426
  1148
            if (wcex.hIcon) DestroyIcon(wcex.hIcon);
slouken@10426
  1149
            if (wcex.hIconSm) DestroyIcon(wcex.hIconSm);
slouken@1895
  1150
        }
slouken@1895
  1151
        SDL_free(SDL_Appname);
slouken@1895
  1152
        SDL_Appname = NULL;
slouken@1895
  1153
    }
slouken@1895
  1154
}
slouken@1895
  1155
slouken@6044
  1156
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
slouken@6044
  1157
slouken@1895
  1158
/* vi: set ts=4 sw=4 expandtab: */