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