File: mhdlg.c

package info (click to toggle)
glhack 1.2-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 26,680 kB
  • ctags: 21,101
  • sloc: ansic: 208,574; cpp: 13,139; yacc: 2,005; makefile: 1,155; lex: 377; sh: 122; awk: 89; sed: 11
file content (787 lines) | stat: -rw-r--r-- 23,056 bytes parent folder | download | duplicates (22)
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
/* Copyright (C) 2001 by Alex Kompel <shurikk@pacbell.net> */
/* NetHack may be freely redistributed.  See license for details. */

/* various dialog boxes are defined here */

#include "winMS.h"
#include "hack.h"
#include "func_tab.h"
#include "mhdlg.h"
#include "mhmain.h"

#define CheckDlgButton(dlg, btn_id, st) SendDlgItemMessage((dlg), (btn_id), BM_SETCHECK, (WPARAM)(st), 0)


/*---------------------------------------------------------------*/
/* data for getlin dialog */
struct getlin_data {
	const char*	question;
	char*		result;
	size_t		result_size;
};

LRESULT CALLBACK	GetlinDlgProc(HWND, UINT, WPARAM, LPARAM);

int mswin_getlin_window (
	const char *question, 
	char *result, 
	size_t result_size
)
{
	int ret;
	struct getlin_data data;

	/* initilize dialog data */
	ZeroMemory(&data, sizeof(data));
	data.question = question;
	data.result = result;
	data.result_size = result_size;

	/* create modal dialog window */
	ret = DialogBoxParam(
			GetNHApp()->hApp,
			MAKEINTRESOURCE(IDD_GETLIN),
			GetNHApp()->hMainWnd,
			GetlinDlgProc,
			(LPARAM)&data
	);
	if( ret==-1 ) panic("Cannot create getlin window");
	
	return ret;
}
    
LRESULT CALLBACK GetlinDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	struct getlin_data* data;
	RECT   main_rt, text_rt, dlg_rt, edit_rt;
	SIZE   dlg_sz;
	TCHAR  wbuf[BUFSZ];
	HDC	   hdc;
	HWND   control;
	HWND   hwndMap;	

#if defined(WIN_CE_POCKETPC)
	SHInputDialog(hWnd, message, wParam);
#endif

	switch (message) 
	{
	case WM_INITDIALOG:
		data = (struct getlin_data*)lParam;
		SetWindowText(hWnd, NH_A2W(data->question, wbuf, sizeof(wbuf)));
		SetWindowLong(hWnd, GWL_USERDATA, lParam);

		/* get title text width */
		SetRect(&text_rt, 0, 0, 100, 50);
		hdc = GetWindowDC(hWnd);
		DrawText(hdc, wbuf, _tcslen(wbuf), &text_rt, 
			     DT_CALCRECT | DT_SINGLELINE | DT_NOPREFIX | DT_LEFT | DT_VCENTER );
		ReleaseDC(hWnd, hdc);

		/* center dialog in the main window */
		GetWindowRect(hWnd, &dlg_rt);
		hwndMap = mswin_hwnd_from_winid(WIN_MAP);
		GetWindowRect( IsWindow(hwndMap)? hwndMap : GetNHApp()->hMainWnd, &main_rt);
		dlg_sz.cx = max(dlg_rt.right-dlg_rt.left, 
			            min( text_rt.right-text_rt.left+GetSystemMetrics(SM_CXICON),
						     main_rt.right-main_rt.left ) );
		dlg_sz.cy = min(dlg_rt.bottom - dlg_rt.top, main_rt.bottom - main_rt.top);
		dlg_rt.left = (main_rt.left+main_rt.right-dlg_sz.cx)/2;
		dlg_rt.right = dlg_rt.left + dlg_sz.cx;
		dlg_rt.top = (main_rt.top+main_rt.bottom-dlg_sz.cy)/2;
		dlg_rt.bottom = dlg_rt.top + dlg_sz.cy;
		MoveWindow( hWnd,
					(main_rt.left+main_rt.right-dlg_sz.cx)/2,
					(main_rt.top+main_rt.bottom-dlg_sz.cy)/2,
					dlg_sz.cx,
					dlg_sz.cy,
					TRUE );

		/* change layout of controls */
		GetClientRect(hWnd, &dlg_rt);

		control = GetDlgItem(hWnd, IDC_GETLIN_EDIT);
		GetWindowRect(control, &edit_rt);
		MoveWindow( control,
					0,
					0,
					dlg_rt.right - dlg_rt.left,
					edit_rt.bottom - edit_rt.top,
					TRUE );

		control = GetDlgItem(hWnd, IDOK);
		GetWindowRect(control, &text_rt);
		MoveWindow( control,
					0,
					edit_rt.bottom - edit_rt.top,
					(dlg_rt.right-dlg_rt.left)/2,
					text_rt.bottom - text_rt.top,
					TRUE );

		control = GetDlgItem(hWnd, IDCANCEL);
		GetWindowRect(control, &text_rt);
		MoveWindow( control,
					(dlg_rt.right-dlg_rt.left)/2,
					edit_rt.bottom - edit_rt.top,
					(dlg_rt.right-dlg_rt.left)/2,
					text_rt.bottom - text_rt.top,
					TRUE );

#if defined(WIN_CE_SMARTPHONE)
		NHSPhoneDialogSetup(hWnd, TRUE, FALSE);
#endif

		/* set focus to the edit control */
		SetFocus(GetDlgItem(hWnd, IDC_GETLIN_EDIT));

		/* tell windows that we've set the focus */
		return FALSE; 
	break;

	case WM_COMMAND: 
	{
		TCHAR wbuf[BUFSZ];

		switch (LOWORD(wParam)) 
        { 
			/* OK button was pressed */
			case IDOK:
		      data = (struct getlin_data*)GetWindowLong(hWnd, GWL_USERDATA);
			  SendDlgItemMessage(hWnd, IDC_GETLIN_EDIT, WM_GETTEXT, (WPARAM)sizeof(wbuf), (LPARAM)wbuf );
			  NH_W2A(wbuf, data->result, data->result_size);

			  /* Fall through. */

			/* cancel button was pressed */
			case IDCANCEL: 
				EndDialog(hWnd, wParam); 
			return TRUE;
		}
	} break;

#if defined(WIN_CE_SMARTPHONE)
	case WM_HOTKEY:
		if(VK_TBACK == HIWORD(lParam)) {
			SHSendBackToFocusWindow(message, wParam, lParam);
		}
	break;
#endif

	} /* end switch (message) */
	return FALSE;
}


/*---------------------------------------------------------------*/
/* dialog data for the list of extended commands */
struct extcmd_data {
	int*		selection;
};

LRESULT CALLBACK	ExtCmdDlgProc(HWND, UINT, WPARAM, LPARAM);

int mswin_ext_cmd_window (int* selection)
{
	int ret;
	struct extcmd_data data;
	
	/* init dialog data */
	ZeroMemory(&data, sizeof(data));
	*selection = -1;
	data.selection = selection;

	/* create modal dialog window */
	ret = DialogBoxParam(
			GetNHApp()->hApp,
			MAKEINTRESOURCE(IDD_EXTCMD),
			GetNHApp()->hMainWnd,
			ExtCmdDlgProc,
			(LPARAM)&data
	);
	if( ret==-1 ) panic("Cannot create extcmd window");
	return ret;
}
    
LRESULT CALLBACK ExtCmdDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	struct extcmd_data* data;
	RECT   main_rt, dlg_rt;
	SIZE   dlg_sz;
	int    i;
	const char *ptr;
	TCHAR wbuf[255];

	switch (message) 
	{
	case WM_INITDIALOG:
		data = (struct extcmd_data*)lParam;
		SetWindowLong(hWnd, GWL_USERDATA, lParam);

		/* center dialog in the main window */
		GetWindowRect(GetNHApp()->hMainWnd, &main_rt);
		GetWindowRect(hWnd, &dlg_rt);
		dlg_sz.cx = dlg_rt.right - dlg_rt.left;
		dlg_sz.cy = dlg_rt.bottom - dlg_rt.top;

		dlg_rt.left = (main_rt.left+main_rt.right-dlg_sz.cx)/2;
		dlg_rt.right = dlg_rt.left + dlg_sz.cx;
		dlg_rt.top = (main_rt.top+main_rt.bottom-dlg_sz.cy)/2;
		dlg_rt.bottom = dlg_rt.top + dlg_sz.cy;
		MoveWindow( hWnd,
					(main_rt.left+main_rt.right-dlg_sz.cx)/2,
					(main_rt.top+main_rt.bottom-dlg_sz.cy)/2,
					dlg_sz.cx,
					dlg_sz.cy,
					TRUE );

		/* fill combobox with extended commands */
		for(i=0; (ptr=extcmdlist[i].ef_txt); i++) {
			SendDlgItemMessage(hWnd, IDC_EXTCMD_LIST, LB_ADDSTRING, (WPARAM)0, (LPARAM)NH_A2W(ptr, wbuf, sizeof(wbuf)) );
		}

#if defined(WIN_CE_SMARTPHONE)
		NHSPhoneDialogSetup(hWnd, FALSE, FALSE);

		GetClientRect(hWnd, &dlg_rt);
		MoveWindow(GetDlgItem(hWnd, IDC_EXTCMD_LIST),
			       dlg_rt.left, dlg_rt.top, 
				   dlg_rt.right-dlg_rt.left, dlg_rt.bottom-dlg_rt.top,
				   TRUE);
#endif

		/* set focus to the list control */
		SetFocus(GetDlgItem(hWnd, IDC_EXTCMD_LIST));

		/* tell windows we set the focus */
		return FALSE;
	break;

	case WM_COMMAND:
        data = (struct extcmd_data*)GetWindowLong(hWnd, GWL_USERDATA);
		switch (LOWORD(wParam)) 
        { 
		  /* OK button ws clicked */
          case IDOK:
			  *data->selection = SendDlgItemMessage(hWnd, IDC_EXTCMD_LIST, LB_GETCURSEL, (WPARAM)0, (LPARAM)0 );
			  if( *data->selection==LB_ERR )
				  *data->selection = -1;
			  /* Fall through. */

		  /* CANCEL button ws clicked */
		  case IDCANCEL:
				EndDialog(hWnd, wParam); 
		  return TRUE;

		  /* list control events */
		  case IDC_EXTCMD_LIST:
				switch(HIWORD(wParam)) {

				case LBN_DBLCLK: 
				  /* double click within the list 
					 wParam 
					   The low-order word is the list box identifier. 
					   The high-order word is the notification message. 
					 lParam 
					   Handle to the list box
					*/
				   *data->selection = SendMessage((HWND)lParam, LB_GETCURSEL, (WPARAM)0, (LPARAM)0);
				   if( *data->selection==LB_ERR )
					   *data->selection = -1;
				   EndDialog(hWnd, IDOK); 
		   	       return TRUE;
				}
		  break;
		}
	}
	return FALSE;
}

/*---------------------------------------------------------------*/
/* player selector dialog data */
struct plsel_data {
	int*	selection;
};

BOOL CALLBACK	PlayerSelectorDlgProc(HWND, UINT, WPARAM, LPARAM);
static void 		plselInitDialog(HWND hWnd);
static void			plselAdjustLists(HWND hWnd, int changed_opt);
static int			plselFinalSelection(HWND hWnd, int* selection);

int mswin_player_selection_window ( int* selection )
{
	int ret;
	struct plsel_data data;

	/* init dialog data */
	ZeroMemory(&data, sizeof(data));
	data.selection = selection;

	/* create modal dialog */
	ret = DialogBoxParam(
			GetNHApp()->hApp,
			MAKEINTRESOURCE(IDD_PLAYER_SELECTOR),
			GetNHApp()->hMainWnd,
			PlayerSelectorDlgProc,
			(LPARAM)&data
	);
	if( ret==-1 ) panic("Cannot create getlin window");
	
	return ret;
}

BOOL CALLBACK PlayerSelectorDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	struct plsel_data* data;
	RECT   main_rt, dlg_rt;
	SIZE   dlg_sz;

	switch (message) 
	{
	case WM_INITDIALOG:
		data = (struct plsel_data*)lParam;
		SetWindowLong(hWnd, GWL_USERDATA, lParam);

		/* center dialog in the main window */
		GetWindowRect(GetNHApp()->hMainWnd, &main_rt);
		GetWindowRect(hWnd, &dlg_rt);
		dlg_sz.cx = dlg_rt.right - dlg_rt.left;
		dlg_sz.cy = dlg_rt.bottom - dlg_rt.top;

		dlg_rt.left = (main_rt.left+main_rt.right-dlg_sz.cx)/2;
		dlg_rt.right = dlg_rt.left + dlg_sz.cx;
		dlg_rt.top = (main_rt.top+main_rt.bottom-dlg_sz.cy)/2;
		dlg_rt.bottom = dlg_rt.top + dlg_sz.cy;
		MoveWindow( hWnd,
					(main_rt.left+main_rt.right-dlg_sz.cx)/2,
					(main_rt.top+main_rt.bottom-dlg_sz.cy)/2,
					dlg_sz.cx,
					dlg_sz.cy,
					TRUE );

		/* init dialog */
		plselInitDialog(hWnd);

#if defined(WIN_CE_SMARTPHONE)
		NHSPhoneDialogSetup(hWnd, FALSE, FALSE);
#endif
		/* set focus on the role checkbox (random) field */
		SetFocus(GetDlgItem(hWnd, IDC_PLSEL_ROLE_RANDOM));

		/* tell windows we set the focus */
		return FALSE;
	break;

	case WM_COMMAND:
        data = (struct plsel_data*)GetWindowLong(hWnd, GWL_USERDATA);
		switch (LOWORD(wParam)) { 

		/* OK button was clicked */
		case IDOK:
			if( plselFinalSelection(hWnd, data->selection) ) {
				EndDialog(hWnd, wParam); 
			} else {
				MessageBox(hWnd, TEXT("Cannot match this role. Try something else."), TEXT("STOP"), MB_OK );
			}
		return TRUE;

		/* CANCEL button was clicked */
		case IDCANCEL:
			*data->selection = -1;
			EndDialog(hWnd, wParam); 
		return TRUE;

		/* following are events from dialog controls: 
		   "random" checkboxes send BN_CLICKED messages;
		   role/race/... combo-boxes send CBN_SELENDOK 
		   if something was selected;
		*/
		case IDC_PLSEL_ROLE_RANDOM:
			if( HIWORD(wParam)==BN_CLICKED ) {
				/* enable corresponding list window if "random"
				   checkbox was "unchecked" */
				EnableWindow(
					GetDlgItem(hWnd, IDC_PLSEL_ROLE_LIST), 
					SendMessage((HWND)lParam, BM_GETCHECK, 0, 0)==BST_UNCHECKED
				  );
			}
		break;

		case IDC_PLSEL_RACE_RANDOM:
			if( HIWORD(wParam)==BN_CLICKED ) {
				EnableWindow(
					GetDlgItem(hWnd, IDC_PLSEL_RACE_LIST), 
					SendMessage((HWND)lParam, BM_GETCHECK, 0, 0)==BST_UNCHECKED
				  );
			}
		break;

		case IDC_PLSEL_GENDER_RANDOM:
			if( HIWORD(wParam)==BN_CLICKED ) {
				EnableWindow(
					GetDlgItem(hWnd, IDC_PLSEL_GENDER_LIST), 
					SendMessage((HWND)lParam, BM_GETCHECK, 0, 0)==BST_UNCHECKED
				  );
			}
		break;

		case IDC_PLSEL_ALIGN_RANDOM:
			if( HIWORD(wParam)==BN_CLICKED ) {
				EnableWindow(
					GetDlgItem(hWnd, IDC_PLSEL_ALIGN_LIST), 
					SendMessage((HWND)lParam, BM_GETCHECK, 0, 0)==BST_UNCHECKED
				  );
			}
		break;

		case IDC_PLSEL_ROLE_LIST:
			if( HIWORD(wParam)==CBN_SELENDOK ) {
				/* filter out invalid options if 
				   the selection was made */
				plselAdjustLists( hWnd, LOWORD(wParam) );
			}
		break;

		case IDC_PLSEL_RACE_LIST:
			if( HIWORD(wParam)==CBN_SELENDOK ) {
				plselAdjustLists( hWnd, LOWORD(wParam) );
			}
		break;

		case IDC_PLSEL_GENDER_LIST:
			if( HIWORD(wParam)==CBN_SELENDOK ) {
				plselAdjustLists( hWnd, LOWORD(wParam) );
			}
		break;

		case IDC_PLSEL_ALIGN_LIST:
			if( HIWORD(wParam)==CBN_SELENDOK ) {
				plselAdjustLists( hWnd, LOWORD(wParam) );
			}
		break;
		}
	break;
	}
	return FALSE;
}

void setComboBoxValue(HWND hWnd, int combo_box, int value)
{
	int index_max = SendDlgItemMessage(hWnd, combo_box, CB_GETCOUNT, 0, 0);
	int index;
	int value_to_set = LB_ERR;
	for (index = 0; index < index_max; index++) {
	    if (SendDlgItemMessage(hWnd, combo_box, CB_GETITEMDATA, (WPARAM)index, 0) == value) {
		value_to_set = index;
		break;
	    }
	}
	SendDlgItemMessage(hWnd, combo_box, CB_SETCURSEL, (WPARAM)value_to_set, 0);
}

/* initialize player selector dialog */
void plselInitDialog(HWND hWnd)
{
	TCHAR wbuf[BUFSZ];

	/* set player name */
	SetDlgItemText(hWnd, IDC_PLSEL_NAME, NH_A2W(plname, wbuf, sizeof(wbuf)));

	/* check flags for consistency */
	if( flags.initrole>=0 ) {
		if (flags.initrace>=0 && !validrace(flags.initrole, flags.initrace)) {
			flags.initrace = ROLE_NONE;
		}

		if (flags.initgend>=0 && !validgend(flags.initrole, flags.initrace, flags.initgend)) {
			flags.initgend = ROLE_NONE;
		}

		if (flags.initalign>=0 && !validalign(flags.initrole, flags.initrace, flags.initalign)) {
			flags.initalign = ROLE_NONE;
		}
	}

	/* populate select boxes */
	plselAdjustLists(hWnd, -1);

	/* intialize roles list */
	if( flags.initrole<0 || !ok_role(flags.initrole, ROLE_NONE, ROLE_NONE, ROLE_NONE)) {
		CheckDlgButton(hWnd, IDC_PLSEL_ROLE_RANDOM, BST_CHECKED);
		EnableWindow(GetDlgItem(hWnd, IDC_PLSEL_ROLE_LIST), FALSE);
	} else {
		CheckDlgButton(hWnd, IDC_PLSEL_ROLE_RANDOM, BST_UNCHECKED);
		EnableWindow(GetDlgItem(hWnd, IDC_PLSEL_ROLE_LIST), TRUE);
		setComboBoxValue(hWnd, IDC_PLSEL_ROLE_LIST, flags.initrole);
	}

	/* intialize races list */
	if( flags.initrace<0 || !ok_race(flags.initrole, flags.initrace, ROLE_NONE, ROLE_NONE) ) {
		CheckDlgButton(hWnd, IDC_PLSEL_RACE_RANDOM, BST_CHECKED);
		EnableWindow(GetDlgItem(hWnd, IDC_PLSEL_RACE_LIST), FALSE);
	} else {
		CheckDlgButton(hWnd, IDC_PLSEL_RACE_RANDOM, BST_UNCHECKED);
		EnableWindow(GetDlgItem(hWnd, IDC_PLSEL_RACE_LIST), TRUE);
		setComboBoxValue(hWnd, IDC_PLSEL_RACE_LIST, flags.initrace);
	}

	/* intialize genders list */
	if( flags.initgend<0 || !ok_gend(flags.initrole, flags.initrace, flags.initgend, ROLE_NONE)) {
		CheckDlgButton(hWnd, IDC_PLSEL_GENDER_RANDOM, BST_CHECKED);
		EnableWindow(GetDlgItem(hWnd, IDC_PLSEL_GENDER_LIST), FALSE);
	} else {
		CheckDlgButton(hWnd, IDC_PLSEL_GENDER_RANDOM, BST_UNCHECKED);
		EnableWindow(GetDlgItem(hWnd, IDC_PLSEL_GENDER_LIST), TRUE);
		setComboBoxValue(hWnd, IDC_PLSEL_GENDER_LIST, flags.initgend);
	}

	/* intialize alignments list */
	if( flags.initalign<0 || !ok_align(flags.initrole, flags.initrace, flags.initgend, flags.initalign) ) {
		CheckDlgButton(hWnd, IDC_PLSEL_ALIGN_RANDOM, BST_CHECKED);
		EnableWindow(GetDlgItem(hWnd, IDC_PLSEL_ALIGN_LIST), FALSE);
	} else {
		CheckDlgButton(hWnd, IDC_PLSEL_ALIGN_RANDOM, BST_UNCHECKED);
		EnableWindow(GetDlgItem(hWnd, IDC_PLSEL_ALIGN_LIST), TRUE);
		setComboBoxValue(hWnd, IDC_PLSEL_ALIGN_LIST, flags.initalign);
	}
}

/* adjust role/race/alignment/gender list - filter out
   invalid combinations 
   changed_sel points to the list where selection occured
   (-1 if unknown)
*/
void  plselAdjustLists(HWND hWnd, int changed_sel)
{
	HWND control_role, control_race, control_gender, control_align;
	int  initrole, initrace, initgend, initalign;
	int i;
	int ind;
	int valid_opt;
	TCHAR wbuf[255];

	/* get control handles */
	control_role = GetDlgItem(hWnd, IDC_PLSEL_ROLE_LIST);
	control_race = GetDlgItem(hWnd, IDC_PLSEL_RACE_LIST);
	control_gender = GetDlgItem(hWnd, IDC_PLSEL_GENDER_LIST);
	control_align = GetDlgItem(hWnd, IDC_PLSEL_ALIGN_LIST);

	/* get current selections */	
	ind = SendMessage(control_role, CB_GETCURSEL, 0, 0);
	initrole = (ind==LB_ERR)? flags.initrole : SendMessage(control_role, CB_GETITEMDATA, ind, 0);

	ind = SendMessage(control_race, CB_GETCURSEL, 0, 0);
	initrace = (ind==LB_ERR)? flags.initrace : SendMessage(control_race, CB_GETITEMDATA, ind, 0);

	ind = SendMessage(control_gender, CB_GETCURSEL, 0, 0);
	initgend = (ind==LB_ERR)? flags.initgend : SendMessage(control_gender, CB_GETITEMDATA, ind, 0);

	ind = SendMessage(control_align, CB_GETCURSEL, 0, 0);
	initalign = (ind==LB_ERR)? flags.initalign : SendMessage(control_align, CB_GETITEMDATA, ind, 0);

	/* intialize roles list */
	if( changed_sel==-1 ) {
		valid_opt = 0;

		/* reset content and populate the list */
		SendMessage(control_role, CB_RESETCONTENT, 0, 0); 
		for (i = 0; roles[i].name.m; i++) {
			if (ok_role(i, initrace, initgend, initalign)) {
			    if (initgend>=0 && flags.female && roles[i].name.f)
					ind = SendMessage(control_role, CB_ADDSTRING, (WPARAM)0, (LPARAM)NH_A2W(roles[i].name.f, wbuf, sizeof(wbuf)) );
				else 
					ind = SendMessage(control_role, CB_ADDSTRING, (WPARAM)0, (LPARAM)NH_A2W(roles[i].name.m, wbuf, sizeof(wbuf)) );

				SendMessage(control_role, CB_SETITEMDATA, (WPARAM)ind, (LPARAM)i );
				if( i==initrole ) { 
					SendMessage(control_role, CB_SETCURSEL, (WPARAM)ind, (LPARAM)0 );
					valid_opt = 1;
				}
			}
		}
		
		/* set selection to the previously selected role
		   if it is still valid */
		if( !valid_opt ) {
			initrole = ROLE_NONE;
			initrace = ROLE_NONE;
			initgend = ROLE_NONE;
			initalign = ROLE_NONE;
			SendMessage(control_role, CB_SETCURSEL, (WPARAM)-1, (LPARAM)0 );
		}

		/* trigger change of the races list */
		changed_sel=IDC_PLSEL_ROLE_LIST;
	}

	/* intialize races list */
	if( changed_sel==IDC_PLSEL_ROLE_LIST ) {
		valid_opt = 0;

		/* reset content and populate the list */
		SendMessage(control_race, CB_RESETCONTENT, 0, 0); 
		for (i = 0; races[i].noun; i++)
			if (ok_race(initrole, i, ROLE_NONE, ROLE_NONE)) {
				ind = SendMessage(control_race, CB_ADDSTRING, (WPARAM)0, (LPARAM)NH_A2W(races[i].noun, wbuf, sizeof(wbuf)) ); 
				SendMessage(control_race, CB_SETITEMDATA, (WPARAM)ind, (LPARAM)i ); 
				if( i==initrace ) { 
					SendMessage(control_race, CB_SETCURSEL, (WPARAM)ind, (LPARAM)0 );
					valid_opt = 1;
				}
			}

		/* set selection to the previously selected race
		   if it is still valid */
		if( !valid_opt ) {
			initrace = ROLE_NONE;
			initgend = ROLE_NONE;
			initalign = ROLE_NONE;
			SendMessage(control_race, CB_SETCURSEL, (WPARAM)-1, (LPARAM)0 );
		}

		/* trigger change of the genders list */
		changed_sel=IDC_PLSEL_RACE_LIST;
	}

	/* intialize genders list */
	if( changed_sel==IDC_PLSEL_RACE_LIST ) {
		valid_opt = 0;

		/* reset content and populate the list */
		SendMessage(control_gender, CB_RESETCONTENT, 0, 0); 
		for (i = 0; i < ROLE_GENDERS; i++)
			if (ok_gend(initrole, initrace, i, ROLE_NONE)) {
				ind = SendMessage(control_gender, CB_ADDSTRING, (WPARAM)0, (LPARAM)NH_A2W(genders[i].adj, wbuf, sizeof(wbuf)) ); 
				SendMessage(control_gender, CB_SETITEMDATA, (WPARAM)ind, (LPARAM)i ); 
				if( i==initgend ) { 
					SendMessage(control_gender, CB_SETCURSEL, (WPARAM)ind, (LPARAM)0 );
					valid_opt = 1;
				}
			}

		/* set selection to the previously selected gender
		   if it is still valid */
		if( !valid_opt ) {
			initgend = ROLE_NONE;
			initalign = ROLE_NONE;
			SendMessage(control_gender, CB_SETCURSEL, (WPARAM)-1, (LPARAM)0 );
		}

		/* trigger change of the alignments list */
		changed_sel=IDC_PLSEL_GENDER_LIST;
	}

	/* intialize alignments list */
	if( changed_sel==IDC_PLSEL_GENDER_LIST ) {
		valid_opt = 0;

		/* reset content and populate the list */
		SendMessage(control_align, CB_RESETCONTENT, 0, 0); 
		for (i = 0; i < ROLE_ALIGNS; i++)
			if (ok_align(initrole, initrace, initgend, i)) {
				ind = SendMessage(control_align, CB_ADDSTRING, (WPARAM)0, (LPARAM)NH_A2W(aligns[i].adj, wbuf, sizeof(wbuf)) ); 
				SendMessage(control_align, CB_SETITEMDATA, (WPARAM)ind, (LPARAM)i ); 
				if( i==initalign ) { 
					SendMessage(control_align, CB_SETCURSEL, (WPARAM)ind, (LPARAM)0 );
					valid_opt = 1;
				}
			}

		/* set selection to the previously selected alignment
		   if it is still valid */
		if( !valid_opt ) {
			initalign = ROLE_NONE;
			SendMessage(control_align, CB_SETCURSEL, (WPARAM)-1, (LPARAM)0 );
		}
	}
}

/* player made up his mind - get final selection here */ 
int	plselFinalSelection(HWND hWnd, int* selection)
{
	int ind;

	/* get current selections */
	if( SendDlgItemMessage(hWnd, IDC_PLSEL_ROLE_RANDOM, BM_GETCHECK, 0, 0)==BST_CHECKED ) {
		flags.initrole = ROLE_RANDOM;
	} else {
		ind = SendDlgItemMessage(hWnd, IDC_PLSEL_ROLE_LIST, CB_GETCURSEL, 0, 0);
		flags.initrole = (ind==LB_ERR)? ROLE_RANDOM : SendDlgItemMessage(hWnd, IDC_PLSEL_ROLE_LIST, CB_GETITEMDATA, ind, 0);
	}

	if( SendDlgItemMessage(hWnd, IDC_PLSEL_RACE_RANDOM, BM_GETCHECK, 0, 0)==BST_CHECKED ) {
		flags.initrace = ROLE_RANDOM;
	} else {
		ind = SendDlgItemMessage(hWnd, IDC_PLSEL_RACE_LIST, CB_GETCURSEL, 0, 0);
		flags.initrace = (ind==LB_ERR)? ROLE_RANDOM : SendDlgItemMessage(hWnd, IDC_PLSEL_RACE_LIST, CB_GETITEMDATA, ind, 0);
	}

	if( SendDlgItemMessage(hWnd, IDC_PLSEL_GENDER_RANDOM, BM_GETCHECK, 0, 0)==BST_CHECKED ) {
		flags.initgend = ROLE_RANDOM;
	} else {
		ind = SendDlgItemMessage(hWnd, IDC_PLSEL_GENDER_LIST, CB_GETCURSEL, 0, 0);
		flags.initgend = (ind==LB_ERR)? ROLE_RANDOM : SendDlgItemMessage(hWnd, IDC_PLSEL_GENDER_LIST, CB_GETITEMDATA, ind, 0);
	}

	if( SendDlgItemMessage(hWnd, IDC_PLSEL_ALIGN_RANDOM, BM_GETCHECK, 0, 0)==BST_CHECKED ) {
		flags.initalign = ROLE_RANDOM;
	} else {
		ind = SendDlgItemMessage(hWnd, IDC_PLSEL_ALIGN_LIST, CB_GETCURSEL, 0, 0);
		flags.initalign = (ind==LB_ERR)? ROLE_RANDOM : SendDlgItemMessage(hWnd, IDC_PLSEL_ALIGN_LIST, CB_GETITEMDATA, ind, 0);
	}
	

	/* check the role */
	if( flags.initrole==ROLE_RANDOM ) {
		flags.initrole = pick_role(flags.initrace, flags.initgend, flags.initalign, PICK_RANDOM);
		if (flags.initrole < 0) {
			MessageBox(hWnd, TEXT("Incompatible role!"), TEXT("STOP"), MB_OK);
			return FALSE;
		}
	}

	/* Select a race, if necessary */
	/* force compatibility with role */
	if (flags.initrace==ROLE_RANDOM || !validrace(flags.initrole, flags.initrace)) {
		/* pre-selected race not valid */
		if (flags.initrace == ROLE_RANDOM) {
			flags.initrace = pick_race(flags.initrole, flags.initgend, flags.initalign, PICK_RANDOM);
		}
		
		if (flags.initrace < 0) {
			MessageBox(hWnd, TEXT("Incompatible race!"), TEXT("STOP"), MB_OK);
			return FALSE;
		}
	}

	/* Select a gender, if necessary */
	/* force compatibility with role/race, try for compatibility with
	 * pre-selected alignment */
	if (flags.initgend < 0 || 
		!validgend(flags.initrole, flags.initrace, flags.initgend)) {
	    /* pre-selected gender not valid */
	    if (flags.initgend == ROLE_RANDOM) {
			flags.initgend = pick_gend(flags.initrole, flags.initrace, flags.initalign, PICK_RANDOM);
		}
		
		if (flags.initgend < 0) {
			MessageBox(hWnd, TEXT("Incompatible gender!"), TEXT("STOP"), MB_OK);
			return FALSE;
		}
	}

	/* Select an alignment, if necessary */
	/* force compatibility with role/race/gender */
	if (flags.initalign < 0 || 
		!validalign(flags.initrole, flags.initrace,	flags.initalign)) {
		/* pre-selected alignment not valid */
		if (flags.initalign == ROLE_RANDOM) {
			flags.initalign = pick_align(flags.initrole, flags.initrace, flags.initgend, PICK_RANDOM);
		} else {
			MessageBox(hWnd, TEXT("Incompatible alignment!"), TEXT("STOP"), MB_OK);
			return FALSE;
		}
	}

	return TRUE;
}