src/video/wincommon/SDL_sysevents.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 19 Aug 2002 02:34:22 +0000
changeset 447 16d0449891b8
parent 338 518ffd98a8f6
child 451 24edec3cafe4
permissions -rw-r--r--
Fixed mouse grab going fullscreen to windowed in Windows
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
#include <stdlib.h>
slouken@0
    29
#include <stdio.h>
slouken@0
    30
#include <windows.h>
slouken@0
    31
slouken@0
    32
#include "SDL_getenv.h"
slouken@0
    33
#include "SDL_events.h"
slouken@0
    34
#include "SDL_video.h"
slouken@0
    35
#include "SDL_error.h"
slouken@0
    36
#include "SDL_syswm.h"
slouken@0
    37
#include "SDL_sysevents.h"
slouken@0
    38
#include "SDL_events_c.h"
slouken@0
    39
#include "SDL_sysvideo.h"
slouken@0
    40
#include "SDL_lowvideo.h"
slouken@0
    41
#include "SDL_syswm_c.h"
slouken@0
    42
#include "SDL_main.h"
slouken@0
    43
slouken@0
    44
#ifdef WMMSG_DEBUG
slouken@0
    45
#include "wmmsg.h"
slouken@0
    46
#endif
slouken@0
    47
slouken@0
    48
#ifdef _WIN32_WCE
slouken@0
    49
#define NO_GETKEYBOARDSTATE
slouken@0
    50
#endif
slouken@0
    51
slouken@0
    52
/* The window we use for everything... */
slouken@0
    53
const char *SDL_Appname = NULL;
slouken@0
    54
HINSTANCE SDL_Instance = NULL;
slouken@0
    55
HWND SDL_Window = NULL;
slouken@0
    56
RECT SDL_bounds = {0, 0, 0, 0};
slouken@0
    57
int SDL_resizing = 0;
slouken@0
    58
int mouse_relative = 0;
slouken@0
    59
int posted = 0;
slouken@304
    60
#ifndef NO_CHANGEDISPLAYSETTINGS
slouken@304
    61
DEVMODE SDL_fullscreen_mode;
slouken@304
    62
#endif
slouken@338
    63
WORD *gamma_saved = NULL;
slouken@0
    64
slouken@0
    65
slouken@0
    66
/* Functions called by the message processing function */
slouken@0
    67
LONG
slouken@0
    68
(*HandleMessage)(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)=NULL;
slouken@0
    69
void (*WIN_RealizePalette)(_THIS);
slouken@0
    70
void (*WIN_PaletteChanged)(_THIS, HWND window);
slouken@0
    71
void (*WIN_WinPAINT)(_THIS, HDC hdc);
slouken@338
    72
extern void DIB_SwapGamma(_THIS);
slouken@0
    73
slouken@304
    74
static void SDL_RestoreGameMode(void)
slouken@304
    75
{
slouken@304
    76
#ifndef NO_CHANGEDISPLAYSETTINGS
slouken@304
    77
	ShowWindow(SDL_Window, SW_RESTORE);
slouken@304
    78
	ChangeDisplaySettings(&SDL_fullscreen_mode, CDS_FULLSCREEN);
slouken@304
    79
#endif
slouken@304
    80
}
slouken@304
    81
static void SDL_RestoreDesktopMode(void)
slouken@304
    82
{
slouken@304
    83
#ifndef NO_CHANGEDISPLAYSETTINGS
slouken@304
    84
	ShowWindow(SDL_Window, SW_MINIMIZE);
slouken@304
    85
	ChangeDisplaySettings(NULL, 0);
slouken@304
    86
#endif
slouken@304
    87
}
slouken@304
    88
slouken@0
    89
#ifdef WM_MOUSELEAVE
slouken@0
    90
/* 
slouken@0
    91
   Special code to handle mouse leave events - this sucks...
slouken@0
    92
   http://support.microsoft.com/support/kb/articles/q183/1/07.asp
slouken@0
    93
slouken@0
    94
   TrackMouseEvent() is only available on Win98 and WinNT.
slouken@0
    95
   _TrackMouseEvent() is available on Win95, but isn't yet in the mingw32
slouken@0
    96
   development environment, and only works on systems that have had IE 3.0
slouken@0
    97
   or newer installed on them (which is not the case with the base Win95).
slouken@0
    98
   Therefore, we implement our own version of _TrackMouseEvent() which
slouken@0
    99
   uses our own implementation if TrackMouseEvent() is not available.
slouken@0
   100
*/
slouken@0
   101
static BOOL (WINAPI *_TrackMouseEvent)(TRACKMOUSEEVENT *ptme) = NULL;
slouken@0
   102
slouken@0
   103
static VOID CALLBACK
slouken@0
   104
TrackMouseTimerProc(HWND hWnd, UINT uMsg, UINT idEvent, DWORD dwTime)
slouken@0
   105
{
slouken@0
   106
	RECT rect;
slouken@0
   107
	POINT pt;
slouken@0
   108
slouken@0
   109
	GetClientRect(hWnd, &rect);
slouken@0
   110
	MapWindowPoints(hWnd, NULL, (LPPOINT)&rect, 2);
slouken@0
   111
	GetCursorPos(&pt);
slouken@0
   112
	if ( !PtInRect(&rect, pt) || (WindowFromPoint(pt) != hWnd) ) {
slouken@0
   113
		if ( !KillTimer(hWnd, idEvent) ) {
slouken@0
   114
			/* Error killing the timer! */
slouken@0
   115
		}
slouken@0
   116
		PostMessage(hWnd, WM_MOUSELEAVE, 0, 0);
slouken@0
   117
	}
slouken@0
   118
}
slouken@0
   119
static BOOL WINAPI WIN_TrackMouseEvent(TRACKMOUSEEVENT *ptme)
slouken@0
   120
{
slouken@0
   121
	if ( ptme->dwFlags == TME_LEAVE ) {
slouken@0
   122
		return SetTimer(ptme->hwndTrack, ptme->dwFlags, 100,
slouken@0
   123
		                (TIMERPROC)TrackMouseTimerProc);
slouken@0
   124
	}
slouken@0
   125
	return FALSE;
slouken@0
   126
}
slouken@0
   127
#endif /* WM_MOUSELEAVE */
slouken@0
   128
slouken@0
   129
/* Function to retrieve the current keyboard modifiers */
slouken@0
   130
static void WIN_GetKeyboardState(void)
slouken@0
   131
{
slouken@0
   132
#ifndef NO_GETKEYBOARDSTATE
slouken@0
   133
	SDLMod state;
slouken@0
   134
	BYTE keyboard[256];
slouken@327
   135
	Uint8 *kstate = SDL_GetKeyState(NULL);
slouken@0
   136
slouken@0
   137
	state = KMOD_NONE;
slouken@0
   138
	if ( GetKeyboardState(keyboard) ) {
slouken@0
   139
		if ( keyboard[VK_LSHIFT] & 0x80) {
slouken@0
   140
			state |= KMOD_LSHIFT;
slouken@0
   141
		}
slouken@0
   142
		if ( keyboard[VK_RSHIFT] & 0x80) {
slouken@0
   143
			state |= KMOD_RSHIFT;
slouken@0
   144
		}
slouken@0
   145
		if ( keyboard[VK_LCONTROL] & 0x80) {
slouken@0
   146
			state |= KMOD_LCTRL;
slouken@0
   147
		}
slouken@0
   148
		if ( keyboard[VK_RCONTROL] & 0x80) {
slouken@0
   149
			state |= KMOD_RCTRL;
slouken@0
   150
		}
slouken@0
   151
		if ( keyboard[VK_LMENU] & 0x80) {
slouken@0
   152
			state |= KMOD_LALT;
slouken@0
   153
		}
slouken@0
   154
		if ( keyboard[VK_RMENU] & 0x80) {
slouken@0
   155
			state |= KMOD_RALT;
slouken@0
   156
		}
slouken@1
   157
		if ( keyboard[VK_NUMLOCK] & 0x01) {
slouken@0
   158
			state |= KMOD_NUM;
slouken@327
   159
			kstate[SDLK_NUMLOCK] = SDL_PRESSED;
slouken@0
   160
		}
slouken@1
   161
		if ( keyboard[VK_CAPITAL] & 0x01) {
slouken@0
   162
			state |= KMOD_CAPS;
slouken@327
   163
			kstate[SDLK_CAPSLOCK] = SDL_PRESSED;
slouken@0
   164
		}
slouken@0
   165
	}
slouken@0
   166
	SDL_SetModState(state);
slouken@0
   167
#endif /* !NO_GETKEYBOARDSTATE */
slouken@0
   168
}
slouken@0
   169
slouken@145
   170
/* The main Win32 event handler
slouken@145
   171
DJM: This is no longer static as (DX5/DIB)_CreateWindow needs it
slouken@145
   172
*/
slouken@145
   173
LONG CALLBACK WinMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
slouken@0
   174
{
slouken@0
   175
	SDL_VideoDevice *this = current_video;
slouken@0
   176
	static int mouse_pressed = 0;
slouken@0
   177
	static int in_window = 0;
slouken@0
   178
#ifdef WMMSG_DEBUG
slouken@0
   179
	fprintf(stderr, "Received windows message:  ");
slouken@0
   180
	if ( msg > MAX_WMMSG ) {
slouken@0
   181
		fprintf(stderr, "%d", msg);
slouken@0
   182
	} else {
slouken@0
   183
		fprintf(stderr, "%s", wmtab[msg]);
slouken@0
   184
	}
slouken@0
   185
	fprintf(stderr, " -- 0x%X, 0x%X\n", wParam, lParam);
slouken@0
   186
#endif
slouken@0
   187
	switch (msg) {
slouken@0
   188
slouken@0
   189
		case WM_ACTIVATE: {
slouken@0
   190
			SDL_VideoDevice *this = current_video;
slouken@0
   191
			BOOL minimized;
slouken@0
   192
			Uint8 appstate;
slouken@0
   193
slouken@0
   194
			minimized = HIWORD(wParam);
slouken@0
   195
			if ( !minimized && (LOWORD(wParam) != WA_INACTIVE) ) {
slouken@0
   196
				/* Gain the following states */
slouken@0
   197
				appstate = SDL_APPACTIVE|SDL_APPINPUTFOCUS;
slouken@0
   198
				if ( this->input_grab != SDL_GRAB_OFF ) {
slouken@0
   199
					WIN_GrabInput(this, SDL_GRAB_ON);
slouken@0
   200
				}
slouken@0
   201
				if ( !(SDL_GetAppState()&SDL_APPINPUTFOCUS) ) {
slouken@338
   202
					if ( ! DDRAW_FULLSCREEN() ) {
slouken@338
   203
						DIB_SwapGamma(this);
slouken@338
   204
					}
slouken@304
   205
					if ( WINDIB_FULLSCREEN() ) {
slouken@304
   206
						SDL_RestoreGameMode();
slouken@304
   207
					}
slouken@0
   208
				}
slouken@0
   209
				posted = SDL_PrivateAppActive(1, appstate);
slouken@0
   210
				WIN_GetKeyboardState();
slouken@0
   211
			} else {
slouken@0
   212
				/* Lose the following states */
slouken@0
   213
				appstate = SDL_APPINPUTFOCUS;
slouken@0
   214
				if ( minimized ) {
slouken@0
   215
					appstate |= SDL_APPACTIVE;
slouken@0
   216
				}
slouken@0
   217
				if ( this->input_grab != SDL_GRAB_OFF ) {
slouken@0
   218
					WIN_GrabInput(this, SDL_GRAB_OFF);
slouken@0
   219
				}
slouken@0
   220
				if ( SDL_GetAppState() & SDL_APPINPUTFOCUS ) {
slouken@338
   221
					if ( ! DDRAW_FULLSCREEN() ) {
slouken@338
   222
						DIB_SwapGamma(this);
slouken@338
   223
					}
slouken@304
   224
					if ( WINDIB_FULLSCREEN() ) {
slouken@304
   225
						SDL_RestoreDesktopMode();
slouken@304
   226
					}
slouken@0
   227
				}
slouken@0
   228
				posted = SDL_PrivateAppActive(0, appstate);
slouken@0
   229
			}
slouken@0
   230
			return(0);
slouken@0
   231
		}
slouken@0
   232
		break;
slouken@0
   233
slouken@0
   234
		case WM_MOUSEMOVE: {
slouken@0
   235
			
slouken@0
   236
			/* Mouse is handled by DirectInput when fullscreen */
slouken@13
   237
			if ( SDL_VideoSurface && ! DINPUT_FULLSCREEN() ) {
slouken@0
   238
				Sint16 x, y;
slouken@0
   239
slouken@0
   240
				/* mouse has entered the window */
slouken@0
   241
				if ( ! in_window ) {
slouken@0
   242
#ifdef WM_MOUSELEAVE
slouken@0
   243
					TRACKMOUSEEVENT tme;
slouken@0
   244
slouken@0
   245
					tme.cbSize = sizeof(tme);
slouken@0
   246
					tme.dwFlags = TME_LEAVE;
slouken@0
   247
					tme.hwndTrack = SDL_Window;
slouken@0
   248
					_TrackMouseEvent(&tme);
slouken@0
   249
#endif /* WM_MOUSELEAVE */
slouken@0
   250
					in_window = TRUE;
slouken@0
   251
slouken@0
   252
					posted = SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
slouken@0
   253
				}
slouken@0
   254
slouken@0
   255
				/* mouse has moved within the window */
slouken@0
   256
				x = LOWORD(lParam);
slouken@0
   257
				y = HIWORD(lParam);
slouken@0
   258
				if ( mouse_relative ) {
slouken@0
   259
					POINT center;
slouken@0
   260
					center.x = (SDL_VideoSurface->w/2);
slouken@0
   261
					center.y = (SDL_VideoSurface->h/2);
slouken@0
   262
					x -= (Sint16)center.x;
slouken@0
   263
					y -= (Sint16)center.y;
slouken@0
   264
					if ( x || y ) {
slouken@0
   265
						ClientToScreen(SDL_Window, &center);
slouken@0
   266
						SetCursorPos(center.x, center.y);
slouken@0
   267
						posted = SDL_PrivateMouseMotion(0, 1, x, y);
slouken@0
   268
					}
slouken@0
   269
				} else {
slouken@0
   270
					posted = SDL_PrivateMouseMotion(0, 0, x, y);
slouken@0
   271
				}
slouken@0
   272
			}
slouken@0
   273
		}
slouken@0
   274
		return(0);
slouken@0
   275
slouken@0
   276
#ifdef WM_MOUSELEAVE
slouken@0
   277
		case WM_MOUSELEAVE: {
slouken@0
   278
slouken@0
   279
			/* Mouse is handled by DirectInput when fullscreen */
slouken@13
   280
			if ( SDL_VideoSurface && ! DINPUT_FULLSCREEN() ) {
slouken@0
   281
				/* mouse has left the window */
slouken@0
   282
				/* or */
slouken@0
   283
				/* Elvis has left the building! */
slouken@0
   284
				in_window = FALSE;
slouken@0
   285
				posted = SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
slouken@0
   286
			}
slouken@0
   287
		}
slouken@0
   288
		return(0);
slouken@0
   289
#endif /* WM_MOUSELEAVE */
slouken@0
   290
slouken@0
   291
		case WM_LBUTTONDOWN:
slouken@0
   292
		case WM_LBUTTONUP:
slouken@0
   293
		case WM_MBUTTONDOWN:
slouken@0
   294
		case WM_MBUTTONUP:
slouken@0
   295
		case WM_RBUTTONDOWN:
slouken@0
   296
		case WM_RBUTTONUP: {
slouken@0
   297
			/* Mouse is handled by DirectInput when fullscreen */
slouken@13
   298
			if ( SDL_VideoSurface && ! DINPUT_FULLSCREEN() ) {
slouken@0
   299
				Sint16 x, y;
slouken@0
   300
				Uint8 button, state;
slouken@0
   301
slouken@145
   302
				/* DJM:
slouken@145
   303
				   We want the SDL window to take focus so that
slouken@145
   304
				   it acts like a normal windows "component"
slouken@145
   305
				   (e.g. gains keyboard focus on a mouse click).
slouken@145
   306
				 */
slouken@145
   307
				SetFocus(SDL_Window);
slouken@145
   308
slouken@0
   309
				/* Figure out which button to use */
slouken@0
   310
				switch (msg) {
slouken@0
   311
					case WM_LBUTTONDOWN:
slouken@0
   312
						button = 1;
slouken@0
   313
						state = SDL_PRESSED;
slouken@0
   314
						break;
slouken@0
   315
					case WM_LBUTTONUP:
slouken@0
   316
						button = 1;
slouken@0
   317
						state = SDL_RELEASED;
slouken@0
   318
						break;
slouken@0
   319
					case WM_MBUTTONDOWN:
slouken@0
   320
						button = 2;
slouken@0
   321
						state = SDL_PRESSED;
slouken@0
   322
						break;
slouken@0
   323
					case WM_MBUTTONUP:
slouken@0
   324
						button = 2;
slouken@0
   325
						state = SDL_RELEASED;
slouken@0
   326
						break;
slouken@0
   327
					case WM_RBUTTONDOWN:
slouken@0
   328
						button = 3;
slouken@0
   329
						state = SDL_PRESSED;
slouken@0
   330
						break;
slouken@0
   331
					case WM_RBUTTONUP:
slouken@0
   332
						button = 3;
slouken@0
   333
						state = SDL_RELEASED;
slouken@0
   334
						break;
slouken@0
   335
					default:
slouken@0
   336
						/* Eh? Unknown button? */
slouken@0
   337
						return(0);
slouken@0
   338
				}
slouken@0
   339
				if ( state == SDL_PRESSED ) {
slouken@0
   340
					/* Grab mouse so we get up events */
slouken@0
   341
					if ( ++mouse_pressed > 0 ) {
slouken@0
   342
						SetCapture(hwnd);
slouken@0
   343
					}
slouken@0
   344
				} else {
slouken@0
   345
					/* Release mouse after all up events */
slouken@0
   346
					if ( --mouse_pressed <= 0 ) {
slouken@0
   347
						ReleaseCapture();
slouken@0
   348
						mouse_pressed = 0;
slouken@0
   349
					}
slouken@0
   350
				}
slouken@0
   351
				if ( mouse_relative ) {
slouken@0
   352
				/*	RJR: March 28, 2000
slouken@0
   353
					report internal mouse position if in relative mode */
slouken@0
   354
					x = 0; y = 0;
slouken@0
   355
				} else {
slouken@0
   356
					x = (Sint16)LOWORD(lParam);
slouken@0
   357
					y = (Sint16)HIWORD(lParam);
slouken@0
   358
				}
slouken@0
   359
				posted = SDL_PrivateMouseButton(
slouken@0
   360
							state, button, x, y);
slouken@0
   361
			}
slouken@0
   362
		}
slouken@0
   363
		return(0);
slouken@0
   364
slouken@61
   365
slouken@61
   366
#if (_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)
slouken@61
   367
		case WM_MOUSEWHEEL: 
slouken@61
   368
			if ( SDL_VideoSurface && ! DINPUT_FULLSCREEN() ) {
slouken@61
   369
				int move = (short)HIWORD(wParam);
slouken@162
   370
				if ( move ) {
slouken@162
   371
					Uint8 button;
slouken@162
   372
					if ( move > 0 )
slouken@162
   373
						button = 4;
slouken@162
   374
					else
slouken@162
   375
						button = 5;
slouken@61
   376
					posted = SDL_PrivateMouseButton(
slouken@162
   377
						SDL_PRESSED, button, 0, 0);
slouken@332
   378
					posted |= SDL_PrivateMouseButton(
slouken@332
   379
						SDL_RELEASED, button, 0, 0);
slouken@61
   380
				}
slouken@61
   381
			}
slouken@61
   382
			return(0);
slouken@61
   383
#endif
slouken@61
   384
slouken@0
   385
#ifdef WM_GETMINMAXINFO
slouken@0
   386
		/* This message is sent as a way for us to "check" the values
slouken@0
   387
		 * of a position change.  If we don't like it, we can adjust
slouken@0
   388
		 * the values before they are changed.
slouken@0
   389
		 */
slouken@0
   390
		case WM_GETMINMAXINFO: {
slouken@0
   391
			MINMAXINFO *info;
slouken@0
   392
			RECT        size;
slouken@0
   393
			int x, y;
slouken@0
   394
			int width;
slouken@0
   395
			int height;
slouken@0
   396
slouken@0
   397
			/* We don't want to clobber an internal resize */
slouken@0
   398
			if ( SDL_resizing )
slouken@0
   399
				return(0);
slouken@0
   400
slouken@0
   401
			/* We allow resizing with the SDL_RESIZABLE flag */
slouken@0
   402
			if ( SDL_PublicSurface &&
slouken@0
   403
				(SDL_PublicSurface->flags & SDL_RESIZABLE) ) {
slouken@0
   404
				return(0);
slouken@0
   405
			}
slouken@0
   406
slouken@0
   407
			/* Get the current position of our window */
slouken@0
   408
			GetWindowRect(SDL_Window, &size);
slouken@0
   409
			x = size.left;
slouken@0
   410
			y = size.top;
slouken@0
   411
slouken@0
   412
			/* Calculate current width and height of our window */
slouken@0
   413
			size.top = 0;
slouken@0
   414
			size.left = 0;
slouken@0
   415
			if ( SDL_PublicSurface != NULL ) {
slouken@0
   416
				size.bottom = SDL_PublicSurface->h;
slouken@0
   417
				size.right = SDL_PublicSurface->w;
slouken@0
   418
			} else {
slouken@0
   419
				size.bottom = 0;
slouken@0
   420
				size.right = 0;
slouken@0
   421
			}
slouken@0
   422
			AdjustWindowRect(&size, GetWindowLong(hwnd, GWL_STYLE),
slouken@0
   423
									FALSE);
slouken@0
   424
			width = size.right - size.left;
slouken@0
   425
			height = size.bottom - size.top;
slouken@0
   426
slouken@0
   427
			/* Fix our size to the current size */
slouken@0
   428
			info = (MINMAXINFO *)lParam;
slouken@0
   429
			info->ptMaxSize.x = width;
slouken@0
   430
			info->ptMaxSize.y = height;
slouken@0
   431
			info->ptMaxPosition.x = x;
slouken@0
   432
			info->ptMaxPosition.y = y;
slouken@0
   433
			info->ptMinTrackSize.x = width;
slouken@0
   434
			info->ptMinTrackSize.y = height;
slouken@0
   435
			info->ptMaxTrackSize.x = width;
slouken@0
   436
			info->ptMaxTrackSize.y = height;
slouken@0
   437
		}
slouken@0
   438
		return(0);
slouken@0
   439
#endif /* WM_GETMINMAXINFO */
slouken@0
   440
slouken@447
   441
		case WM_WINDOWPOSCHANGED: {
slouken@0
   442
			SDL_VideoDevice *this = current_video;
slouken@447
   443
			int w, h;
slouken@0
   444
slouken@0
   445
			GetClientRect(SDL_Window, &SDL_bounds);
slouken@0
   446
			ClientToScreen(SDL_Window, (LPPOINT)&SDL_bounds);
slouken@0
   447
			ClientToScreen(SDL_Window, (LPPOINT)&SDL_bounds+1);
slouken@447
   448
			w = SDL_bounds.right-SDL_bounds.left;
slouken@447
   449
			h = SDL_bounds.bottom-SDL_bounds.top;
slouken@0
   450
			if ( this->input_grab != SDL_GRAB_OFF ) {
slouken@0
   451
				ClipCursor(&SDL_bounds);
slouken@0
   452
			}
slouken@447
   453
			if ( SDL_PublicSurface && 
slouken@0
   454
				(SDL_PublicSurface->flags & SDL_RESIZABLE) ) {
slouken@447
   455
				SDL_PrivateResize(w, h);
slouken@0
   456
			}
slouken@0
   457
			return(0);
slouken@0
   458
		}
slouken@0
   459
		break;
slouken@0
   460
slouken@0
   461
		/* We need to set the cursor */
slouken@0
   462
		case WM_SETCURSOR: {
slouken@0
   463
			Uint16 hittest;
slouken@0
   464
slouken@0
   465
			hittest = LOWORD(lParam);
slouken@0
   466
			if ( hittest == HTCLIENT ) {
slouken@0
   467
				SetCursor(SDL_hcursor);
slouken@0
   468
				return(TRUE);
slouken@0
   469
			}
slouken@0
   470
		}
slouken@0
   471
		break;
slouken@0
   472
slouken@0
   473
		/* We are about to get palette focus! */
slouken@0
   474
		case WM_QUERYNEWPALETTE: {
slouken@0
   475
			WIN_RealizePalette(current_video);
slouken@0
   476
			return(TRUE);
slouken@0
   477
		}
slouken@0
   478
		break;
slouken@0
   479
slouken@0
   480
		/* Another application changed the palette */
slouken@0
   481
		case WM_PALETTECHANGED: {
slouken@0
   482
			WIN_PaletteChanged(current_video, (HWND)wParam);
slouken@0
   483
		}
slouken@0
   484
		break;
slouken@0
   485
slouken@0
   486
		/* We were occluded, refresh our display */
slouken@0
   487
		case WM_PAINT: {
slouken@0
   488
			HDC hdc;
slouken@0
   489
			PAINTSTRUCT ps;
slouken@0
   490
slouken@0
   491
			hdc = BeginPaint(SDL_Window, &ps);
slouken@0
   492
			if ( current_video->screen &&
slouken@0
   493
			     !(current_video->screen->flags & SDL_OPENGL) ) {
slouken@0
   494
				WIN_WinPAINT(current_video, hdc);
slouken@0
   495
			}
slouken@0
   496
			EndPaint(SDL_Window, &ps);
slouken@0
   497
		}
slouken@0
   498
		return(0);
slouken@0
   499
slouken@145
   500
		/* DJM: Send an expose event in this case */
slouken@0
   501
		case WM_ERASEBKGND: {
slouken@145
   502
			posted = SDL_PrivateExpose();
slouken@0
   503
		}
slouken@145
   504
		return(0);
slouken@0
   505
slouken@0
   506
		case WM_CLOSE: {
slouken@0
   507
			if ( (posted = SDL_PrivateQuit()) )
slouken@0
   508
				PostQuitMessage(0);
slouken@0
   509
		}
slouken@0
   510
		return(0);
slouken@0
   511
slouken@0
   512
		case WM_DESTROY: {
slouken@0
   513
			PostQuitMessage(0);
slouken@0
   514
		}
slouken@0
   515
		return(0);
slouken@0
   516
slouken@0
   517
		default: {
slouken@0
   518
			/* Special handling by the video driver */
slouken@0
   519
			if (HandleMessage) {
slouken@0
   520
				return(HandleMessage(current_video,
slouken@0
   521
			                     hwnd, msg, wParam, lParam));
slouken@0
   522
			}
slouken@0
   523
		}
slouken@0
   524
		break;
slouken@0
   525
	}
slouken@0
   526
	return(DefWindowProc(hwnd, msg, wParam, lParam));
slouken@0
   527
}
slouken@0
   528
slouken@145
   529
/* Allow the application handle to be stored and retrieved later */
slouken@149
   530
static void *SDL_handle = NULL;
slouken@145
   531
slouken@149
   532
void SDL_SetModuleHandle(void *handle)
slouken@145
   533
{
slouken@145
   534
	SDL_handle = handle;
slouken@145
   535
}
slouken@149
   536
void *SDL_GetModuleHandle(void)
slouken@145
   537
{
slouken@145
   538
	void *handle;
slouken@145
   539
slouken@145
   540
	if ( SDL_handle ) {
slouken@145
   541
		handle = SDL_handle;
slouken@145
   542
	} else {
slouken@145
   543
		/* Warning:
slouken@145
   544
		   If SDL is built as a DLL, this will return a handle to
slouken@145
   545
		   the DLL, not the application, and DirectInput may fail
slouken@145
   546
		   to initialize.
slouken@145
   547
		 */
slouken@145
   548
		handle = GetModuleHandle(NULL);
slouken@145
   549
	}
slouken@145
   550
	return(handle);
slouken@145
   551
}
slouken@145
   552
slouken@0
   553
/* This allows the SDL_WINDOWID hack */
slouken@0
   554
const char *SDL_windowid = NULL;
slouken@0
   555
slouken@0
   556
/* Register the class for this application -- exported for winmain.c */
slouken@149
   557
int SDL_RegisterApp(char *name, Uint32 style, void *hInst)
slouken@0
   558
{
slouken@0
   559
	static int initialized = 0;
slouken@0
   560
	WNDCLASS class;
slouken@0
   561
#ifdef WM_MOUSELEAVE
slouken@0
   562
	HMODULE handle;
slouken@0
   563
#endif
slouken@0
   564
slouken@0
   565
	/* Only do this once... */
slouken@0
   566
	if ( initialized ) {
slouken@0
   567
		return(0);
slouken@0
   568
	}
slouken@0
   569
slouken@0
   570
	/* This function needs to be passed the correct process handle
slouken@145
   571
	   by the application.
slouken@0
   572
	 */
slouken@0
   573
	if ( ! hInst ) {
slouken@145
   574
		hInst = SDL_GetModuleHandle();
slouken@0
   575
	}
slouken@0
   576
slouken@0
   577
	/* Register the application class */
slouken@0
   578
	class.hCursor		= NULL;
slouken@0
   579
#ifdef _WIN32_WCE
slouken@0
   580
    {
slouken@0
   581
	/* WinCE uses the UNICODE version */
slouken@36
   582
	int nLen = strlen(name)+1;
slouken@36
   583
	LPWSTR lpszW = alloca(nLen*2);
slouken@0
   584
	MultiByteToWideChar(CP_ACP, 0, name, -1, lpszW, nLen);
slouken@0
   585
	class.hIcon		= LoadImage(hInst, lpszW, IMAGE_ICON,
slouken@0
   586
	                                    0, 0, LR_DEFAULTCOLOR);
slouken@36
   587
	class.lpszMenuName	= NULL;
slouken@0
   588
	class.lpszClassName	= lpszW;
slouken@0
   589
    }
slouken@0
   590
#else
slouken@0
   591
	class.hIcon		= LoadImage(hInst, name, IMAGE_ICON,
slouken@0
   592
	                                    0, 0, LR_DEFAULTCOLOR);
slouken@0
   593
	class.lpszMenuName	= "(none)";
slouken@0
   594
	class.lpszClassName	= name;
slouken@0
   595
#endif /* _WIN32_WCE */
slouken@0
   596
	class.hbrBackground	= NULL;
slouken@149
   597
	class.hInstance		= hInst;
slouken@0
   598
	class.style		= style;
slouken@0
   599
#ifdef HAVE_OPENGL
slouken@0
   600
	class.style		|= CS_OWNDC;
slouken@0
   601
#endif
slouken@0
   602
	class.lpfnWndProc	= WinMessage;
slouken@0
   603
	class.cbWndExtra	= 0;
slouken@0
   604
	class.cbClsExtra	= 0;
slouken@0
   605
	if ( ! RegisterClass(&class) ) {
slouken@0
   606
		SDL_SetError("Couldn't register application class");
slouken@0
   607
		return(-1);
slouken@0
   608
	}
slouken@0
   609
	SDL_Appname = name;
slouken@0
   610
	SDL_Instance = hInst;
slouken@0
   611
slouken@0
   612
#ifdef WM_MOUSELEAVE
slouken@0
   613
	/* Get the version of TrackMouseEvent() we use */
slouken@0
   614
	_TrackMouseEvent = NULL;
slouken@0
   615
	handle = GetModuleHandle("USER32.DLL");
slouken@0
   616
	if ( handle ) {
slouken@0
   617
		_TrackMouseEvent = (BOOL (WINAPI *)(TRACKMOUSEEVENT *))GetProcAddress(handle, "TrackMouseEvent");
slouken@0
   618
	}
slouken@0
   619
	if ( _TrackMouseEvent == NULL ) {
slouken@0
   620
		_TrackMouseEvent = WIN_TrackMouseEvent;
slouken@0
   621
	}
slouken@0
   622
#endif /* WM_MOUSELEAVE */
slouken@0
   623
slouken@0
   624
	/* Check for SDL_WINDOWID hack */
slouken@0
   625
	SDL_windowid = getenv("SDL_WINDOWID");
slouken@0
   626
slouken@0
   627
	initialized = 1;
slouken@0
   628
	return(0);
slouken@0
   629
}
slouken@0
   630