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