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