[go: up one dir, main page]

File: winmain.cxx

package info (click to toggle)
lincity 1.11-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,488 kB
  • ctags: 2,641
  • sloc: cpp: 24,598; makefile: 245; sh: 32; perl: 10
file content (1286 lines) | stat: -rwxr-xr-x 40,423 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
// winmain.cxx	Win32 stuff - part of LinCity
// Copyright (c) I J Peters 1995,1996.  Please read the file 'COPYRIGHT'.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <malloc.h>
#include <memory.h>

#include "lin-city.h"
#include "common.h"
#include "generic.h"
#include "mouse.h"

// Define this to use DIB's instead of DDB's
//#define WIN32_USEDIB

// Global Variables
static char szClassNameWithMenu[] = APPNAME " (with menu)";
static char szClassNameWithoutMenu[] = APPNAME " (without menu)";
static char szAppName[] = APPNAME;	// The name of this application
static char szTitle[]   = APPNAME;	// The title bar text
static HBRUSH hbrBackground = 0;

// Private Function Prototypes
static ATOM MyRegisterClass(CONST WNDCLASS*);
static BOOL InitApplication(HINSTANCE);
static BOOL InitInstance(HINSTANCE, int);
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
static BOOL CenterWindow (HWND, HWND);
static HBITMAP InitializeBackingStore (HWND);
static BOOL CopyBackingStoreToScreen (HDC, HWND, LPPAINTSTRUCT);
static void CreateDDB (HWND hWnd);
static void CreateDIB (void);
static void InitializePalette (void);
static void DoSquareMouse (HDC hdc);



//----------------------------------------------------------------------------
//  FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
//
//  PURPOSE: Entry point for the application.
//
//  COMMENTS:
//
//	This function initializes the application and processes the
//	message loop.
//----------------------------------------------------------------------------
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	if (!hPrevInstance) {
		// Perform instance initialization:
		if (!InitApplication(hInstance)) {
			return (FALSE);
		}
	}

	// Perform application initialization, including creating the
	// Lin-City main window.
	if (!InitInstance(hInstance, nCmdShow)) {
		return (FALSE);
	}

	// Load keyboard accelerators (shortkut keys)
	display.hAccelTable = LoadAccelerators (hInstance, szAppName);

	// Take care of any outstanding messages before calling main()
	ProcessPendingEvents ();

	// main() contains main message loop
	char *args[] = { "wlincity" };
	main (1, args);

	// Take care of any outstanding messages after main() finishes.
	ProcessPendingEvents ();

	return 0;
	lpCmdLine; // This will prevent 'unused formal parameter' warnings
}


//----------------------------------------------------------------------------
//  FUNCTION: MyRegisterClass(CONST WNDCLASS*)
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
// 	This function and its usage is only necessary if you want this code
// 	to be compatible with Win32 systems prior to the 'RegisterClassEx'
//	function that was added to Windows 95. It is important to call this function
// 	so that the application will get 'well formed' small icons associated
// 	with it.
//----------------------------------------------------------------------------
ATOM MyRegisterClass(CONST WNDCLASS *lpwc)
{
	HANDLE  hMod;
	FARPROC proc;
	WNDCLASSEX wcex;

	hMod = GetModuleHandle ("USER32");
	if (hMod != NULL) {
#if defined (UNICODE)
		proc = GetProcAddress ((HMODULE)hMod, "RegisterClassExW");
#else
		proc = GetProcAddress ((HMODULE)hMod, "RegisterClassExA");
#endif
		if (proc != NULL) {

			wcex.style			= lpwc->style;
			wcex.lpfnWndProc	= lpwc->lpfnWndProc;
			wcex.cbClsExtra		= lpwc->cbClsExtra;
			wcex.cbWndExtra		= lpwc->cbWndExtra;
			wcex.hInstance		= lpwc->hInstance;
			wcex.hIcon			= lpwc->hIcon;
			wcex.hCursor		= lpwc->hCursor;
			wcex.hbrBackground	= lpwc->hbrBackground;
			wcex.lpszMenuName	= lpwc->lpszMenuName;
			wcex.lpszClassName	= lpwc->lpszClassName;

			// Added elements for Windows 95:
			wcex.cbSize = sizeof(WNDCLASSEX);
			wcex.hIconSm = LoadIcon(wcex.hInstance, "SMALL");

			// GCS: This typedef is for the typecast...
			typedef int (FAR WINAPI *my_FARPROC)(WNDCLASSEX*);
			return (*((my_FARPROC)(proc)))(&wcex);
		}
	}
	return (RegisterClass(lpwc));
}


//----------------------------------------------------------------------------
//  FUNCTION: InitApplication(HANDLE)
//
//  PURPOSE: Initializes window data and registers window classes
//
//  COMMENTS:
//	   We need two classes -- one which has a menu, one without a menu
//         (for full-screen).  This is registered by calling RegisterClass 
//         for Win 3.1/NT, or MyRegisterClass for Win 95.
//----------------------------------------------------------------------------
BOOL InitApplication(HINSTANCE hInstance)
{
	// If Lin City is already running, focus existing process.
	// Win32 always sets hPrevInstance to NULL, so check w/ FindWindow()
	HWND hwnd = FindWindow (szClassNameWithMenu, NULL);
	if (!hwnd) hwnd = FindWindow (szClassNameWithoutMenu, NULL);
	if (hwnd) {
		// We found another version of ourself. Lets defer to it:
		if (IsIconic(hwnd)) {
			ShowWindow(hwnd, SW_RESTORE);
		}
		SetForegroundWindow (hwnd);
		return FALSE;
	}

	// Create brush for background: Nasty Brown (Lin City color # 105)
	hbrBackground = CreateSolidBrush (RGB (0x4C,0x4C,0));

	// Fill in WNDCLASS for class WITH MENU.
	WNDCLASS  wcWithMenu;
	// wcWithMenu.style		= CS_HREDRAW | CS_VREDRAW;
	wcWithMenu.style		= 0;
	wcWithMenu.lpfnWndProc		= (WNDPROC)WndProc;
	wcWithMenu.cbClsExtra		= 0;
	wcWithMenu.cbWndExtra		= 0;
	wcWithMenu.hInstance		= hInstance;
	wcWithMenu.hIcon		= LoadIcon (hInstance, szAppName);
	wcWithMenu.hCursor		= LoadCursor(NULL, IDC_ARROW);
	// wcWithMenu.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcWithMenu.hbrBackground	= hbrBackground;
	if (IS_WIN95) {  // Windows95 has different recommended help menu format.
		wcWithMenu.lpszMenuName	= "WIN95";
	} else {
		wcWithMenu.lpszMenuName	= szAppName;
	}
	wcWithMenu.lpszClassName	= szClassNameWithMenu;

	// Fill in WNDCLASS for class WITH MENU.
	WNDCLASS  wcWithoutMenu;
	wcWithoutMenu.style		= 0;
	wcWithoutMenu.lpfnWndProc	= (WNDPROC)WndProc;
	wcWithoutMenu.cbClsExtra	= 0;
	wcWithoutMenu.cbWndExtra	= 0;
	wcWithoutMenu.hInstance		= hInstance;
	wcWithoutMenu.hIcon		= LoadIcon (hInstance, szAppName);
	wcWithoutMenu.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcWithoutMenu.hbrBackground	= hbrBackground;
	wcWithoutMenu.lpszMenuName	= "";
	wcWithoutMenu.lpszClassName	= szClassNameWithoutMenu;

	// Register the window classes and return success/failure code.
	if (IS_WIN95) {
		BOOL rv;
		rv = MyRegisterClass(&wcWithMenu);
		if (!rv) return rv;
		rv = MyRegisterClass(&wcWithoutMenu);
		return rv;
	} else {
		BOOL rv;
		rv = RegisterClass(&wcWithMenu);
		if (!rv) return rv;
		rv = RegisterClass(&wcWithoutMenu);
		return rv;
	}
}


//----------------------------------------------------------------------------
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE:  Creates main window 
//----------------------------------------------------------------------------
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	// Determine graphics capabilities
	HDC hDCGlobal = GetDC (NULL);
	INT iRasterCaps;
	iRasterCaps = GetDeviceCaps(hDCGlobal, RASTERCAPS);
	if (iRasterCaps & RC_PALETTE) {
		display.hasPalette = TRUE;
		display.paletteSize = GetDeviceCaps (hDCGlobal, SIZEPALETTE);
		display.defaultPaletteSize = GetDeviceCaps (hDCGlobal, NUMCOLORS);
	} else {
		display.hasPalette = FALSE;
	}
	display.colorDepth = GetDeviceCaps (hDCGlobal, BITSPIXEL);
	ReleaseDC (NULL, hDCGlobal);

	// Decide whether to use DIB's or DDB's
#if defined (WIN32_USEDIB)
	if (display.colorDepth == 8)	// Only use DIB for 256 colors
		display.useDIB = TRUE;
	else
		display.useDIB = FALSE;
#else
	display.useDIB = FALSE;
#endif

	// Do some global initializations
	display.hInst = hInstance;
	display.fullscreen = FALSE;
	//display.fullscreen = TRUE;
	display.screenW = GetSystemMetrics (SM_CXSCREEN);
	display.screenH = GetSystemMetrics (SM_CYSCREEN);
	display.winFullscreenClientW = (INT) GetSystemMetrics (SM_CXFULLSCREEN);
	display.winFullscreenClientH = (INT) GetSystemMetrics (SM_CYFULLSCREEN);
	InitializePalette ();

	// Choose one of Lin City window types
	//    a)  no pix doubling, no border
	//    b)  no pix doubling, 30 pixel border
	//    c)  pix doubling, no border
	if ((display.screenW >= WINWIDTH*2)
		&& display.screenH >= WINHEIGHT*2) {
		pix_double = 1;
		borderx = 0;
		bordery = 0;
	} else if ((display.screenW >= WINWIDTH+(2*BORDERX))
		&& (display.screenH >= WINHEIGHT+(2*BORDERY))) {
		pix_double = 0;
		borderx = BORDERX;
		bordery = BORDERY;
	} else {
		pix_double = 0;
		borderx = 0;
		bordery = 0;
	}
	display.clientW = ((pix_double+1)*WINWIDTH) + (2*borderx);
	display.clientH = ((pix_double+1)*WINHEIGHT) + (2*bordery);

	// Choose one of three client window types
	//    a)  full screen
	//    b)  maximized window
	//    c)  regular window
	// Check regular window first.
	char *szClassName = 0;
	DWORD dwStyle;
	RECT client_size;
	dwStyle = WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
	client_size.left = 0;
	client_size.top = 0;
	client_size.right = display.clientW;
	client_size.bottom = display.clientH;
	AdjustWindowRect (&client_size, dwStyle, TRUE);
	if (((client_size.right - client_size.left) <= display.screenW)
	    && ((client_size.bottom - client_size.top) <= display.screenH)) {
		// Use regular (overlapped) window
		szClassName = szClassNameWithMenu;
		if (client_size.left < 0) {
			client_size.right -= client_size.left;
			client_size.left = CW_USEDEFAULT;
		}
		if (client_size.top < 0) {
			client_size.bottom -= client_size.top;
			client_size.top = CW_USEDEFAULT;
		}
	} else {
		// Check maximized by subtracting out the DLGFRAME size.
		// Note that the "obvious" method of calling AdjustWindowRect()
		// with style WS_MAXIMIZE doesn't work!
		int border_x = GetSystemMetrics (SM_CXDLGFRAME);
		int border_y = GetSystemMetrics (SM_CYDLGFRAME);
		if (((client_size.right - client_size.left - 2*border_x) <= display.screenW)
		    && ((client_size.bottom - client_size.top - 2*border_y) <= display.screenH)) {
			// Use maximized window
			szClassName = szClassNameWithMenu;
			// dwStyle |= WS_MAXIMIZE;
			if (client_size.left < 0) {
				client_size.right -= client_size.left;
				client_size.left = CW_USEDEFAULT;
			}
			if (client_size.top < 0) {
				client_size.bottom -= client_size.top;
				client_size.top = CW_USEDEFAULT;
			}
			nCmdShow = SW_SHOWMAXIMIZED;
		} else {
			// Need fullsize window
			szClassName = szClassNameWithoutMenu;
			dwStyle = WS_POPUP;
			client_size.left = 0;
			client_size.top = 0;
			client_size.right = display.screenW;
			client_size.bottom = display.screenH;
		}
	}

	// Create the window
	display.hWnd = CreateWindow (
		szClassName,				// Class name
		szTitle,				// Caption
		dwStyle,				// Style
		client_size.left, client_size.top,	// Position
		client_size.right, client_size.bottom,	// Size
		(HWND)NULL,				// Parent window (no parent)
		(HMENU)NULL,				// use class menu
		(HINSTANCE)hInstance,			// handle to window instance
		(LPVOID)NULL				// no params to pass on
	);
	if (!display.hWnd) {
		return (FALSE);
	}

	// Display the window
	ShowWindow (display.hWnd, nCmdShow);
	UpdateWindow (display.hWnd);

	return (TRUE);
}


//----------------------------------------------------------------------------
//  FUNCTION: MainLoop ()
//----------------------------------------------------------------------------
void MainLoop (void)
{
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0)) {
		if (!TranslateAccelerator (msg.hwnd, (HACCEL)display.hAccelTable, &msg)) {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
}


//----------------------------------------------------------------------------
//  FUNCTION: ProcessNextEvent ()
//            Wait for a message if necessary,
//            then process all messages.
//----------------------------------------------------------------------------
void ProcessNextEvent (void)
{
	MSG msg;
	if (GetMessage (&msg, NULL, 0, 0)) {
		if (!TranslateAccelerator (msg.hwnd, (HACCEL)display.hAccelTable, &msg)) {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	ProcessPendingEvents ();
}


//----------------------------------------------------------------------------
//  FUNCTION: ProcessPendingEvents ()
//            Process all outstanding messages.
//----------------------------------------------------------------------------
void ProcessPendingEvents (void)
{
	MSG msg;
	while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
		if (!TranslateAccelerator (msg.hwnd, (HACCEL)display.hAccelTable, &msg)) {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
}


//----------------------------------------------------------------------------
//  FUNCTION: EnableWindowsMenuItems ()
//  FUNCTION: DisableWindowsMenuItems ()
//            Enable and Disable "Load" and "Save" items
//----------------------------------------------------------------------------
void EnableWindowsMenuItems (void)
{
	HMENU hMenu = GetMenu (display.hWnd);
	EnableMenuItem (hMenu, IDM_OPEN, MF_BYCOMMAND | MF_ENABLED);
	EnableMenuItem (hMenu, IDM_SAVE, MF_BYCOMMAND | MF_ENABLED);
	EnableMenuItem (hMenu, IDM_HELPCONTENTS, MF_BYCOMMAND | MF_ENABLED);
}

void DisableWindowsMenuItems (void)
{
	HMENU hMenu = GetMenu (display.hWnd);
	EnableMenuItem (hMenu, IDM_OPEN, MF_BYCOMMAND | MF_GRAYED);
	EnableMenuItem (hMenu, IDM_SAVE, MF_BYCOMMAND | MF_GRAYED);
	EnableMenuItem (hMenu, IDM_HELPCONTENTS, MF_BYCOMMAND | MF_GRAYED);
}


//----------------------------------------------------------------------------
//  FUNCTION: HandleMouse ()
//----------------------------------------------------------------------------
void HandleMouse ()
{
	MSG msg;
	while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
		if (!TranslateAccelerator (msg.hwnd, (HACCEL)display.hAccelTable, &msg)) {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
			// We can ignore mouse moves, while we are processing events,
			// But we can't ignore clicks
			if (cs_current_mouse_button != cs_mouse_button)
				cs_mouse_handler (cs_current_mouse_button,
						cs_current_mouse_x - cs_mouse_x,
						cs_current_mouse_y - cs_mouse_y);
		}
	}
	// Now take care of the last mouse move/click
	if (cs_current_mouse_x != cs_mouse_x 
			|| cs_current_mouse_y != cs_mouse_y
			|| cs_current_mouse_button != cs_mouse_button)
		cs_mouse_handler (cs_current_mouse_button,
				cs_current_mouse_x - cs_mouse_x,
				cs_current_mouse_y - cs_mouse_y);
}


//----------------------------------------------------------------------------
//  FUNCTION: GetKeystroke ()
//----------------------------------------------------------------------------
char GetKeystroke()
{
	char key;
	ProcessPendingEvents ();
	key = x_key_value;
	x_key_value = 0;
	return key;
}


//----------------------------------------------------------------------------
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//----------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;

	switch (message) { 

		case WM_COMMAND:
		{
			wmId	= LOWORD(wParam); // Remember, these are...
			wmEvent = HIWORD(wParam); // ...different for Win32!

			//Parse the menu selections:
			switch (wmId) {

				case IDM_OPEN:
					load_flag = 1;
					DisableWindowsMenuItems ();
					break;

				case IDM_SAVE:
					save_flag = 1;
					DisableWindowsMenuItems ();
					break;

				case IDM_EXIT:
					// GCS FIX: I'm still not quite satisfied with this...
					// x_key_value = 'q';
					DestroyWindow (hWnd);
					break;

				case IDM_ABOUT:
					DialogBox(display.hInst, "AboutBox", hWnd, (DLGPROC)About);
					break;

				case IDM_HELPCONTENTS:
					help_flag = 1;
					DisableWindowsMenuItems ();
					activate_help("index.hlp");
					break;

				default:
					return (DefWindowProc(hWnd, message, wParam, lParam));
			}
		}
		break;

		case WM_KEYDOWN:
		{
			int nVirtKey = (int) wParam;
			// LPARAM lKeyData = lParam;
			x_key_shifted = (GetKeyState (VK_SHIFT) & 0x80000000) ? TRUE : FALSE;
			switch (nVirtKey)
			{
				case VK_LEFT:
					x_key_value = 1;
				break;
 
				case VK_DOWN:
					x_key_value = 2;
				break;

				case VK_UP:
					x_key_value = 3;
				break;

				case VK_RIGHT:
					x_key_value = 4;
				break;
			}
		}
		break;

		case WM_CHAR:
		{
			TCHAR chCharCode = (TCHAR) wParam;	// character code
			LPARAM lKeyData = lParam;			// key data
			if (chCharCode == 8) {				// Fix backspace
				chCharCode = 127;
			}
			x_key_value = chCharCode;
		}
		break;

		case WM_NCRBUTTONUP: // RightClick on windows non-client area
		{
			if (IS_WIN95 && SendMessage(hWnd, WM_NCHITTEST, 0, lParam) == HTSYSMENU)
			{
				// The user has clicked the right button on the applications
				// 'System Menu'. Here is where you would alter the default
				// system menu to reflect your application. Notice how the
				// explorer deals with this. For this app, we aren't doing
				// anything
				return (DefWindowProc(hWnd, message, wParam, lParam));
			} else {
				// Nothing we are interested in, allow default handling...
				return (DefWindowProc(hWnd, message, wParam, lParam));
			}
		}
		break;

		case WM_RBUTTONDOWN: // RightClick in windows client area
		{
			cs_current_mouse_x = UnAdjustX (LOWORD (lParam));
			cs_current_mouse_y = UnAdjustY (HIWORD (lParam));
			cs_current_mouse_button = MOUSE_RIGHTBUTTON;
			cs_mouse_shifted = (wParam & MK_SHIFT) ? 1 : 0;
		}
		break;

		case WM_LBUTTONDOWN: // Left Click in windows client area
		{
			cs_current_mouse_x = UnAdjustX (LOWORD (lParam));
			cs_current_mouse_y = UnAdjustY (HIWORD (lParam));
			cs_current_mouse_button = MOUSE_LEFTBUTTON;
			cs_mouse_shifted = (wParam & MK_SHIFT) ? 1 : 0;
		}
		break;

		case WM_RBUTTONUP:
		case WM_LBUTTONUP:
		{
			cs_current_mouse_x = UnAdjustX (LOWORD (lParam));
			cs_current_mouse_y = UnAdjustY (HIWORD (lParam));
			cs_current_mouse_button = 0;
			cs_mouse_shifted = (wParam & MK_SHIFT) ? 1 : 0;
		}
		break;

		case WM_MOUSEMOVE:
		{
			cs_current_mouse_x = UnAdjustX (LOWORD(lParam));
			cs_current_mouse_y = UnAdjustY (HIWORD(lParam));
			cs_mouse_shifted = (wParam & MK_SHIFT) ? 1 : 0;
		}
		break;
 
		case WM_DISPLAYCHANGE: // Only comes through on plug'n'play systems
		{
			SIZE szScreen;
			BOOL fChanged = (BOOL)wParam;

			szScreen.cx = LOWORD(lParam);
			szScreen.cy = HIWORD(lParam);
			
			if (fChanged) {
				// The display 'has' changed. szScreen reflects the
				// new size.
				MessageBox (GetFocus(), "Display Changed", szAppName, 0);
			} else {
				// The display 'is' changing. szScreen reflects the
				// original size.
				MessageBeep(0);
			}
		}
		break;

		case WM_PAINT:
		{
			PAINTSTRUCT	ps;
			HDC			hdc;

			hdc = BeginPaint (hWnd, &ps);
			CopyBackingStoreToScreen (hdc, hWnd, &ps);
			EndPaint (hWnd, &ps);
		}
		break;

		case WM_CREATE:		// Create client area
		{
			InitializeBackingStore (hWnd);
		}
		break;

		case WM_DESTROY:
		{
			// PostQuitMessage(0);
			exit (0);	// OK??
		}
		break;

		case WM_SIZE:		// Resize window
		default:
			return (DefWindowProc(hWnd, message, wParam, lParam));
	}
	return (0);
}


//----------------------------------------------------------------------------
//  FUNCTION: InitializeBackingStore ()
//----------------------------------------------------------------------------
HBITMAP InitializeBackingStore (HWND hWnd)
{
	RECT rc;

	// Set up the remaining global variables
	GetClientRect(hWnd, &rc);
	display.winW = rc.right-rc.left;
	display.winH = rc.bottom-rc.top;

	InitializePalette ();
	CreateDDB (hWnd);
	if (display.useDIB) {
		CreateDIB ();
	}
	display.hSaveUnderHdc = 0;
	display.hSaveUnderBitmap = 0;

	return 0;
}


//----------------------------------------------------------------------------
//  FUNCTION: InitializePalette ()
//----------------------------------------------------------------------------
void InitializePalette (void) {
	INT iLoop;

	display.hPal = 0;
	display.pLogPal = 0;

	// Clear out palette arrays
	for (iLoop = 0; iLoop < 256; iLoop++) {
		display.colorrefPal [iLoop] = 0;
		display.brushPal [iLoop] = 0;
	}

	// If the device uses a palette (e.g. 8 bit display),
	// we need to create an HPALETTE for the HDC.
	if (display.hasPalette) {

		// Allocate memory for LOGPALETTE
		display.pLogPal = (NPLOGPALETTE) LocalAlloc (LMEM_FIXED,
				  (sizeof (LOGPALETTE) +
				  (sizeof (PALETTEENTRY) * (display.paletteSize))));
		if (!display.pLogPal) {
			MessageBox(display.hWnd, "Not enough memory for logical palette.", NULL, MB_OK | MB_ICONHAND);
			PostQuitMessage (0) ;
			exit (-1);
		}

		display.pLogPal->palVersion    = 0x300;
		display.pLogPal->palNumEntries = display.paletteSize;

		// Fill palette with system colors by default
		for (iLoop = 0; iLoop < display.paletteSize; iLoop++) {
				*((WORD *) (&display.pLogPal->palPalEntry[iLoop].peRed)) = (WORD)iLoop;
				display.pLogPal->palPalEntry[iLoop].peBlue  = 0;
				display.pLogPal->palPalEntry[iLoop].peFlags = PC_EXPLICIT;
		}

		// Convert LOGPALETTE into HPALETTE
		display.hPal = CreatePalette ((LPLOGPALETTE) display.pLogPal);
	}
}
			
//----------------------------------------------------------------------------
//  FUNCTION: CreateDIB ()
//----------------------------------------------------------------------------
void CreateDIB (void)
{
	// Let's create a 256 color DIB, just for testing!
	ULONG		sizBMI;
	INT			iNumClr = 256;
	INT			iLoop;

	// Get memory for 256 color BITMAPINFO
	sizBMI = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * iNumClr;
	if ((display.pbminfo = (PBITMAPINFO) GlobalAlloc (GMEM_FIXED | GMEM_ZEROINIT, sizBMI)) == NULL) {
		MessageBox(display.hWnd, "Failed in Memory Allocation for bminfo!", "Error", MB_OK);
		exit (-1);
	}

	// Fill in bitmap info
	display.pbminfo->bmiHeader.biSize = 0x28;						// GDI need this to work
//	display.pbminfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);	// GDI need this to work
	display.pbminfo->bmiHeader.biWidth = display.winW;
	display.pbminfo->bmiHeader.biHeight = display.winH;
	display.pbminfo->bmiHeader.biPlanes = 1;
	display.pbminfo->bmiHeader.biBitCount = 8;
	display.pbminfo->bmiHeader.biCompression = BI_RGB;
	display.pbminfo->bmiHeader.biSizeImage = display.winW * display.winH;	// One byte per pixel
	display.pbminfo->bmiHeader.biXPelsPerMeter = 0;
	display.pbminfo->bmiHeader.biYPelsPerMeter = 0;
	display.pbminfo->bmiHeader.biClrUsed = 0;
	display.pbminfo->bmiHeader.biClrImportant = 0;

#if defined (commentout)
	// Fill in colormap w/ default colors
	// FillBMIColors (&display.pbminfo->bmiColors[0]);
#endif

	// Fill in colormap w/ black
	for (iLoop = 0; iNumClr; iLoop++) {
		display.pbminfo->bmiColors[iLoop].rgbRed = 0;
		display.pbminfo->bmiColors[iLoop].rgbGreen = 0;
		display.pbminfo->bmiColors[iLoop].rgbBlue = 0;
		display.pbminfo->bmiColors[iLoop].rgbReserved = 0;
	}

	// CreateDIBSection() will allocate the pBits.
	display.hDIB = CreateDIBSection (display.hdcMem, display.pbminfo,
			DIB_RGB_COLORS, (void**)(&display.pBits), NULL, 0);
}


//----------------------------------------------------------------------------
//  FUNCTION: CreateDDB ()
//----------------------------------------------------------------------------
void CreateDDB (HWND hWnd)
{
	HDC hdc;
	HBITMAP	hBitmapOri;
	HPALETTE hPalOld;

	hdc = GetDC(hWnd);
	if (display.hasPalette) {
		hPalOld = SelectPalette(hdc, (HPALETTE)display.hPal, FALSE);
		if (RealizePalette(hdc)) {
			UpdateColors (hdc);
		}
	}

	// Before an application can use a memory device 
	// context for drawing operations, it must select 
	// a bitmap of the correct width and height into 
	// the device context. Once a bitmap has been selected, 
	// the device context can be used to prepare images 
	// that will be copied to the screen or printed. 
	display.hdcMem = CreateCompatibleDC(hdc);
	if (display.hasPalette) {
		display.hPaletteMemOri = SelectPalette(display.hdcMem, (HPALETTE)display.hPal, FALSE);
		RealizePalette (display.hdcMem);
	}
	display.hBitmap = CreateCompatibleBitmap(hdc, display.winW, display.winH);
	hBitmapOri = (HBITMAP)SelectObject(display.hdcMem, display.hBitmap);

	// Write that nasty brown color into the backing store
	RECT rect = { 0, 0, display.winW, display.winH };
	HBRUSH hbr, hbrOld;
	hbr = hbrBackground;
	hbrOld = (HBRUSH)SelectObject (hdc, hbr);				// Select brush
	FillRect (display.hdcMem, &rect, (HBRUSH)hbr);			// Draw rectangle
	hbr = (HBRUSH)SelectObject (display.hdcMem, hbrOld);	// Unselect brush

#if defined (commentout)
	// Debugging code, writes pattern to backing store
	for (i = 0; i < display.winW; i++) {
		for (int j = 0; j < display.winH; j++) {
		//	SetPixel (display.hdcMem, i, j, RGB(((i+j)%256), 0, 0));
			SetPixel (display.hdcMem, i, j, PALETTEINDEX((i+j) % 256));
		}
	}
#endif

#if defined (commentout)
	// GCS: For now, just keep the old bitmap around.
	//      I'll use it to swap out the full-sized color bitmap
	//      when I do a SetDIBits/GetDIBits.
	display.hBitmapOri = hBitmapOri;
#endif

#if defined (commentout)
	// GCS: Instead of reselecting the default bitmap back into the hdc,
	//      I'm gonna delete it. Any problems with this??
	DeleteObject (hBitmapOri);
#endif

	ReleaseDC(hWnd, hdc);
}


//----------------------------------------------------------------------------
//  FUNCTION: CopyBackingStoreToScreen ()
//----------------------------------------------------------------------------
BOOL CopyBackingStoreToScreen(HDC hdc, HWND hWnd, LPPAINTSTRUCT ps)
{
	HPALETTE hPalOld, hPalOldMem;

	// Is this necessary???
	if (display.hasPalette) {
		hPalOldMem = SelectPalette(display.hdcMem, (HPALETTE)display.hPal, FALSE);
		RealizePalette(display.hdcMem);
		hPalOld = SelectPalette(hdc, (HPALETTE)display.hPal, FALSE);
		RealizePalette(hdc);
	}

	// Copy DIB into DDB
	if (display.useDIB) {
		if (SetDIBits (display.hdcMem, display.hBitmap, 0, 
				display.pbminfo->bmiHeader.biHeight, (LPSTR)display.pBits, 
				display.pbminfo, DIB_RGB_COLORS) == 0) {
			MessageBox (display.hWnd, "Failed in SetDIBits!", "Error", MB_OK);
			exit (-1);
		}
	}

	// Blast DDB onto screen
	if (!BitBlt(hdc, ps->rcPaint.left, ps->rcPaint.top,
			(int)ps->rcPaint.right - ps->rcPaint.left,
			(int)ps->rcPaint.bottom - ps->rcPaint.top,
			display.hdcMem, ps->rcPaint.left, 
			ps->rcPaint.top, SRCCOPY)) {
		MessageBox (NULL, "BitBlt failed!", "Failure!", MB_OK);
	}

	// Draw square mouse
	if (cs_square_mouse_visible) {
		DoSquareMouse (hdc);
	}

	if (display.hasPalette) {
		display.hPal = SelectPalette(display.hdcMem, hPalOldMem, FALSE);
		display.hPal = SelectPalette(hdc, hPalOld, FALSE);
	}

	return TRUE;
}

//----------------------------------------------------------------------------
//  FUNCTION: DoSquareMouse ()
//----------------------------------------------------------------------------
void DoSquareMouse (HDC hdc)
{
  RECT rect;
  int size = (main_types[selected_type].size)*16;

  // Select Brush
  HBRUSH hbr, hbrOld;
  hbr = GetPaletteBrush (white(31));
  hbrOld = (HBRUSH)SelectObject (hdc, hbr);

  // Top rectangle
  rect.left = AdjustX (omx-2);
  rect.top = AdjustY (omy-2);
  rect.right = AdjustX (omx+size+1);
  rect.bottom = AdjustY (omy-1);
  FillRect (hdc, &rect, hbr);

  // Left rectangle
  rect.left = AdjustX (omx-2);
  rect.top = AdjustY (omy);
  rect.right = AdjustX (omx-1);
  rect.bottom = AdjustY (omy+size-1);
  FillRect (hdc, &rect, hbr);

  // Right rectangle
  rect.left = AdjustX (omx+size);
  rect.top = AdjustY (omy);
  rect.right = AdjustX (omx+size+1);
  rect.bottom = AdjustY (omy+size-1);
  FillRect (hdc, &rect, hbr);

  // Bottom rectangle
  rect.left = AdjustX (omx-2);
  rect.top = AdjustY (omy+size);
  rect.right = AdjustX (omx+size+1);
  rect.bottom = AdjustY (omy+size+1);
  FillRect (hdc, &rect, hbr);

  // Unselect brush
  hbr = (HBRUSH)SelectObject (display.hdcMem, hbrOld);
}

//----------------------------------------------------------------------------
//  FUNCTION: CopyPixmapToScreen ()
//----------------------------------------------------------------------------
#if defined (USE_PIXMAPS)
void CopyPixmapToScreen (int t2, int src_x, int src_y, int width, int height,
						 int dst_x, int dst_y)
{
	HDC hdcPixmap;
	RECT rect;

	// Calculate update rectangle
	rect.left = AdjustX (dst_x);
	rect.top = AdjustY (dst_y);
	rect.right = AdjustX (dst_x + width + 1);
	rect.bottom = AdjustY (dst_y + height + 1);

	// Copy bitmap
	if (display.useDIB) {
		HBITMAP hOldBitmap, hOldBitmapMem;
		hdcPixmap = CreateCompatibleDC(display.hdcMem);
		hOldBitmap = (HBITMAP)SelectObject(hdcPixmap, icon_pixmap[t2]);
		hOldBitmapMem = (HBITMAP)SelectObject(display.hdcMem, display.hDIB);
		BitBlt (display.hdcMem, dst_x, dst_y, width, height, hdcPixmap, 
				src_x, src_y, SRCCOPY);
		icon_pixmap[t2] = (HBITMAP)SelectObject(hdcPixmap, hOldBitmap);
		display.hDIB = (HBITMAP)SelectObject(display.hdcMem, hOldBitmapMem);
		DeleteDC (hdcPixmap);
	} else {
		HBITMAP hOldBitmap;
		hdcPixmap = CreateCompatibleDC(display.hdcMem);
		hOldBitmap = (HBITMAP)SelectObject(hdcPixmap, icon_pixmap[t2]);
		BitBlt (display.hdcMem, AdjustX (dst_x), AdjustY (dst_y), 
				width << pix_double, height << pix_double, hdcPixmap,
				src_x << pix_double, src_y << pix_double, SRCCOPY);
		icon_pixmap[t2] = (HBITMAP)SelectObject(hdcPixmap, hOldBitmap);
		DeleteDC (hdcPixmap);
	}

	// Queue up refresh
	InvalidateRect (display.hWnd, &rect, FALSE);
#if defined (commentout)
	// Refresh now!!
	UpdateWindow (display.hWnd);
#endif
}
#endif


//----------------------------------------------------------------------------
//  FUNCTION: About(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for "About" dialog box
// 		This version allows greater flexibility over the contents of the 'About' box,
// 		by pulling out values from the 'Version' resource.
//
//  MESSAGES:
//
//	WM_INITDIALOG - initialize dialog box
//	WM_COMMAND	- Input received
//
//----------------------------------------------------------------------------
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	static  HFONT hfontDlg;		// Font for dialog text
	static	HFONT hFinePrint;	// Font for 'fine print' in dialog
	DWORD   dwVerInfoSize;		// Size of version information block
	LPSTR   lpVersion;			// String pointer to 'version' text
	DWORD   dwVerHnd=0;			// An 'ignored' parameter, always '0'
	UINT	uVersionLen;
	WORD	wRootLen;
	BOOL	bRetCode;
	int	 i;
	char	szFullPath[256];
	char	szResult[256];
	char	szGetName[256];
	DWORD	dwVersion;
	char	szVersion[40];
	DWORD	dwResult;

	switch (message) {
		case WM_INITDIALOG:
			ShowWindow (hDlg, SW_HIDE);
			hfontDlg = CreateFont(16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				VARIABLE_PITCH | FF_SWISS, "");
			hFinePrint = CreateFont(16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				VARIABLE_PITCH | FF_SWISS, "");
			CenterWindow (hDlg, GetWindow (hDlg, GW_OWNER));
			GetModuleFileName (display.hInst, szFullPath, sizeof(szFullPath));

			// Now lets dive in and pull out the version information:
			dwVerInfoSize = GetFileVersionInfoSize(szFullPath, &dwVerHnd);
			if (dwVerInfoSize) {
				LPSTR   lpstrVffInfo;
				HANDLE  hMem;
				hMem = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
				lpstrVffInfo  = (char*) GlobalLock(hMem);
				GetFileVersionInfo(szFullPath, dwVerHnd, dwVerInfoSize, lpstrVffInfo);
				// The below 'hex' value looks a little confusing, but
				// essentially what it is, is the hexidecimal representation
				// of a couple different values that represent the language
				// and character set that we are wanting string values for.
				// 040904E4 is a very common one, because it means:
				//   US English, Windows MultiLingual characterset
				// Or to pull it all apart:
				// 04------		= SUBLANG_ENGLISH_USA
				// --09----		= LANG_ENGLISH
				// ----04E4 = 1252 = Codepage for Windows:Multilingual
				lstrcpy(szGetName, "\\StringFileInfo\\040904E4\\");	 
				wRootLen = lstrlen(szGetName); // Save this position
			
				// Set the title of the dialog:
				lstrcat (szGetName, "ProductName");
				bRetCode = VerQueryValue((LPVOID)lpstrVffInfo,
					(LPSTR)szGetName,
//					(LPVOID)&lpVersion,
					(LPVOID*)&lpVersion,
					(UINT *)&uVersionLen);
				lstrcpy(szResult, "About ");
				lstrcat(szResult, lpVersion);
				SetWindowText (hDlg, szResult);

				// Walk through the dialog items that we want to replace:
				for (i = DLG_VERFIRST; i <= DLG_VERLAST; i++) {
					GetDlgItemText(hDlg, i, szResult, sizeof(szResult));
					szGetName[wRootLen] = (char)0;
					lstrcat (szGetName, szResult);
					uVersionLen   = 0;
					lpVersion	 = NULL;
					bRetCode	  =  VerQueryValue((LPVOID)lpstrVffInfo,
						(LPSTR)szGetName,
//						(LPVOID)&lpVersion,
						(LPVOID*)&lpVersion,
						(UINT *)&uVersionLen);

					if ( bRetCode && uVersionLen && lpVersion) {
					// Replace dialog item text with version info
						lstrcpy(szResult, lpVersion);
						SetDlgItemText(hDlg, i, szResult);
					} else {
						dwResult = GetLastError();
						wsprintf (szResult, "Error %lu", dwResult);
						SetDlgItemText (hDlg, i, szResult);
					}
					SendMessage (GetDlgItem (hDlg, i), WM_SETFONT, 
						(UINT)((i==DLG_VERLAST)?hFinePrint:hfontDlg),
						TRUE);
				} // for (i = DLG_VERFIRST; i <= DLG_VERLAST; i++)


				GlobalUnlock(hMem);
				GlobalFree(hMem);

			} else {
				// No version information available.
			} // if (dwVerInfoSize)

			SendMessage (GetDlgItem (hDlg, IDC_LABEL), WM_SETFONT,
				(WPARAM)hfontDlg,(LPARAM)TRUE);

			// We are  using GetVersion rather then GetVersionEx
			// because earlier versions of Windows NT and Win32s
			// didn't include GetVersionEx:
			dwVersion = GetVersion();

			if (dwVersion < 0x80000000) {
				// Windows NT
				wsprintf (szVersion, "Microsoft Windows NT %u.%u (Build: %u)",
					(DWORD)(LOBYTE(LOWORD(dwVersion))),
					(DWORD)(HIBYTE(LOWORD(dwVersion))),
					(DWORD)(HIWORD(dwVersion)) );
			} else if (LOBYTE(LOWORD(dwVersion))<4) {
				// Win32s
				wsprintf (szVersion, "Microsoft Win32s %u.%u (Build: %u)",
					(DWORD)(LOBYTE(LOWORD(dwVersion))),
					(DWORD)(HIBYTE(LOWORD(dwVersion))),
					(DWORD)(HIWORD(dwVersion) & ~0x8000) );
			} else {
				// Windows 95
				wsprintf (szVersion, "Microsoft Windows 95 %u.%u",
					(DWORD)(LOBYTE(LOWORD(dwVersion))),
					(DWORD)(HIBYTE(LOWORD(dwVersion))) );
			}

			SetWindowText (GetDlgItem(hDlg, IDC_OSVERSION), szVersion);
			ShowWindow (hDlg, SW_SHOW);
			return (TRUE);

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
				EndDialog(hDlg, TRUE);
				DeleteObject (hfontDlg);
				DeleteObject (hFinePrint);
				return (TRUE);
			}
			break;
	}

	return FALSE;
}


//----------------------------------------------------------------------------
//   FUNCTION: CenterWindow(HWND, HWND)
//
//   PURPOSE: Centers one window over another. 
//
//   COMMENTS:
//
//		In this function, we save the instance handle in a global variable and
//		create and display the main program window.
//
// 		This functionwill center one window over another ensuring that 
//		the placement of the window is within the 'working area', meaning 
//		that it is both within the display limits of the screen, and not 
//		obscured by the tray or other framing elements of the desktop.
//----------------------------------------------------------------------------
BOOL CenterWindow (HWND hwndChild, HWND hwndParent)
{
	RECT	rChild, rParent, rWorkArea;
	int	 wChild, hChild, wParent, hParent;
	int	 xNew, yNew;
	BOOL 	bResult;

	// Get the Height and Width of the child window
	GetWindowRect (hwndChild, &rChild);
	wChild = rChild.right - rChild.left;
	hChild = rChild.bottom - rChild.top;

	// Get the Height and Width of the parent window
	GetWindowRect (hwndParent, &rParent);
	wParent = rParent.right - rParent.left;
	hParent = rParent.bottom - rParent.top;

	// Get the limits of the 'workarea'
	bResult = SystemParametersInfo(
		SPI_GETWORKAREA,	// system parameter to query or set
		sizeof(RECT),
		&rWorkArea,
		0);
	if (!bResult) {
		rWorkArea.left = rWorkArea.top = 0;
		rWorkArea.right = GetSystemMetrics(SM_CXSCREEN);
		rWorkArea.bottom = GetSystemMetrics(SM_CYSCREEN);
	}

	// Calculate new X position, then adjust for workarea
	xNew = rParent.left + ((wParent - wChild) /2);
	if (xNew < rWorkArea.left) {
		xNew = rWorkArea.left;
	} else if ((xNew+wChild) > rWorkArea.right) {
		xNew = rWorkArea.right - wChild;
	}

	// Calculate new Y position, then adjust for workarea
	yNew = rParent.top  + ((hParent - hChild) /2);
	if (yNew < rWorkArea.top) {
		yNew = rWorkArea.top;
	} else if ((yNew+hChild) > rWorkArea.bottom) {
		yNew = rWorkArea.bottom - hChild;
	}

	// Set it, and return
	return SetWindowPos (hwndChild, NULL, xNew, yNew, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}


//----------------------------------------------------------------------------
//   FUNCTION: AddPaletteEntry ()
//----------------------------------------------------------------------------
void AddPaletteEntry (int col, int red, int grn, int blu)
{
	red = (red * 255) / 60;
	if (red > 255) red = 255;
	grn = (grn * 255) / 60;
	if (grn > 255) grn = 255;
	blu = (blu * 255) / 60;
	if (blu > 255) blu = 255;

	display.colorrefPal[col] = RGB(red, grn, blu);

	if (display.brushPal[col]) {
		DeleteObject (display.brushPal[col]);
		display.brushPal[col] = 0;
	}

	if (display.hasPalette) {
		display.pLogPal->palPalEntry[col].peRed = red;
		display.pLogPal->palPalEntry[col].peGreen = grn;
		display.pLogPal->palPalEntry[col].peBlue = blu;
		display.pLogPal->palPalEntry[col].peFlags = PC_NOCOLLAPSE;
	}

	if (display.useDIB) {
		display.pbminfo->bmiColors[col].rgbRed = red;
		display.pbminfo->bmiColors[col].rgbGreen = grn;
		display.pbminfo->bmiColors[col].rgbBlue = blu;
		display.pbminfo->bmiColors[col].rgbReserved = 0;
	}
}


//----------------------------------------------------------------------------
//  FUNCTION: UpdatePalette ()
//----------------------------------------------------------------------------
void UpdatePalette (void)
{
	if (display.hasPalette) {
		// Make a new HPALETTE and select to hdcGlobal and hdcMem
		HPALETTE hPalNew;
		hPalNew = CreatePalette ((LPLOGPALETTE) display.pLogPal);
		SelectPalette (display.hdcMem, hPalNew, FALSE);
		RealizePalette(display.hdcMem);
		DeleteObject (display.hPal);
		display.hPal = hPalNew;
	}
}

#if defined (commentout)
static void GetFullscreenOption (void)
{
	char foo[100];

	DWORD bytesRead = GetPrivateProfileString (
		"Display",			// points to section name 
		"Full Screen",		// points to key name 
		"",					// points to default string 
		foo,				// points to destination buffer 
		99,					// size of destination buffer 
		"LINCITY.INI"	 	// points to initialization filename 
	);

	if (bytesRead > 0) {
		if (((_stricmp (foo, "TRUE")) == 0)
				|| ((_stricmp (foo, "T")) == 0)
				|| ((_stricmp (foo, "YES")) == 0)
				|| ((_stricmp (foo, "Y")) == 0)
				|| ((_stricmp (foo, "1")) == 0)) {
			int x = 0;
		} else if (((_stricmp (foo, "FALSE")) == 0)
				|| ((_stricmp (foo, "F")) == 0)
				|| ((_stricmp (foo, "NO")) == 0)
				|| ((_stricmp (foo, "N")) == 0)
				|| ((_stricmp (foo, "0")) == 0)) {
			int y = 9;
		}
	}
}
#endif