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