File: menu.c

package info (click to toggle)
ezquake 2.2%2Bgit20150324-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 6,996 kB
  • ctags: 16,582
  • sloc: ansic: 143,243; makefile: 339; tcl: 107; sh: 28
file content (1439 lines) | stat: -rw-r--r-- 36,396 bytes parent folder | download
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
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
/*
Copyright (C) 1996-1997 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifndef _WIN32
#include <dirent.h>
#include <sys/stat.h>
#endif
#include "quakedef.h"
#include "gl_model.h"
#include "gl_local.h"
#ifndef CLIENTONLY
#include "server.h"
#endif
#include "menu.h"
#include "EX_browser.h"
#include "Ctrl_Tab.h"
#include "menu_demo.h"
#include "menu_proxy.h"
#include "menu_options.h"
#include "menu_ingame.h"
#include "menu_multiplayer.h"
#include "EX_FileList.h"
#include "help.h"
#include "utils.h"
#include "qsound.h"
#include "keys.h"
#include "common_draw.h"
#include "menu_mp3player.h"


qbool vid_windowedmouse = true;
void (*vid_menudrawfn)(void);
void (*vid_menukeyfn)(int key);
void CL_Disconnect_f(void);

extern cvar_t con_shift, scr_menualpha;

void M_Menu_Main_f (void);
	void M_Menu_SinglePlayer_f (void);
		void M_Menu_Load_f (void);
		void M_Menu_Save_f (void);
	void M_Menu_MultiPlayer_f (void);
			void M_Menu_SEdit_f (void);
		void M_Menu_Demos_f (void);
		void M_Menu_GameOptions_f (void);
	void M_Menu_Options_f (void);
#ifdef WITH_MP3_PLAYER
	void M_Menu_MP3_Control_f (void);
#endif // WITH_MP3_PLAYER
	void M_Menu_Quit_f (void);

void M_Main_Draw (void);
	void M_SinglePlayer_Draw (void);
		void M_Load_Draw (void);
		void M_Save_Draw (void);
	void M_MultiPlayer_Draw (void);
		void M_ServerList_Draw (void);
			void M_SEdit_Draw (void);
		void M_Demo_Draw (void);
		void M_GameOptions_Draw (void);
		void M_Proxy_Draw (void);
	void M_Options_Draw (void);
	void M_Help_Draw (void);
	void M_Quit_Draw (void);

void M_Main_Key (int key);
	void M_SinglePlayer_Key (int key);
		void M_Load_Key (int key);
		void M_Save_Key (int key);
	void M_MultiPlayerSub_Key (int key);
		void M_ServerList_Key (int key);
			void M_SEdit_Key (int key);
		void M_Demo_Key (int key);
		qbool M_GameOptions_Key (int key);
		void M_Proxy_Key (int key);
	void M_Options_Key (int key, wchar unichar);
	void M_Help_Key (int key);
	void M_Quit_Key (int key);

void M_Menu_Help_f (void);

#define QUAKE_ID_PLAQUE_PATH	"gfx/qplaque.lmp"

m_state_t m_state;

qbool    m_entersound;          // play after drawing a frame, so caching
                                // won't disrupt the sound
qbool    m_recursiveDraw;
int            m_topmenu;       // set if a submenu was entered via a
                                // menu_* command
#define    SLIDER_RANGE    10

typedef struct menu_window_s {
	int x;
	int y;
	int w;
	int h;
} menu_window_t;

//=============================================================================
/* Support Routines */

cvar_t     scr_scaleMenu = {"scr_scaleMenu","1"};
int        menuwidth = 320;
int        menuheight = 240;

cvar_t     scr_centerMenu = {"scr_centerMenu","1"};
cvar_t     menu_ingame = {"menu_ingame", "1"};
int        m_yofs = 0;

void M_DrawCharacter (int cx, int line, int num) {
	Draw_Character (cx + ((menuwidth - 320)>>1), line + m_yofs, num);
}

void M_Print_GetPoint(int cx, int cy, int *rx, int *ry, const char *str, qbool red) {
	cx += ((menuwidth - 320)>>1);
	cy += m_yofs;
	*rx = cx;
	*ry = cy;
	if (red)
		Draw_Alt_String (cx, cy, str);
	else
		Draw_String(cx, cy, str);
}

void M_Print (int cx, int cy, char *str) {
	int rx, ry;
	M_Print_GetPoint(cx, cy, &rx, &ry, str, true);
}

void M_PrintWhite (int cx, int cy, char *str) {
	Draw_String (cx + ((menuwidth - 320)>>1), cy + m_yofs, str);
}

// replacement of M_DrawTransPic - sometimes we need the real coordinate of the pic
static void M_DrawTransPic_GetPoint (int x, int y, int *rx, int *ry, mpic_t *pic)
{
	*rx = x + ((menuwidth - 320)>>1);
	*ry = y + m_yofs;
	Draw_TransPic (x + ((menuwidth - 320)>>1), y + m_yofs, pic);
}

void M_DrawTransPic (int x, int y, mpic_t *pic) {
	int tx, ty;
	M_DrawTransPic_GetPoint(x, y, &tx, &ty, pic);
}

void M_DrawPic (int x, int y, mpic_t *pic) {
	Draw_Pic (x + ((menuwidth - 320)>>1), y + m_yofs, pic);
}

void M_DrawTextBox (int x, int y, int width, int lines) {
	Draw_TextBox (x + ((menuwidth - 320) >> 1), y + m_yofs, width, lines);
}

void M_DrawSlider (int x, int y, float range) {
	int    i;

	range = bound(0, range, 1);
	M_DrawCharacter (x-8, y, 128);
	for (i=0 ; i<SLIDER_RANGE ; i++)
		M_DrawCharacter (x + i*8, y, 129);
	M_DrawCharacter (x+i*8, y, 130);
	M_DrawCharacter (x + (SLIDER_RANGE-1)*8 * range, y, 131);
}

qbool Key_IsLeftRightSameBind(int b);

void M_FindKeysForCommand (const char *command, int *twokeys) {
	int count, j, l;
	char *b;

	twokeys[0] = twokeys[1] = -1;
	l = strlen(command) + 1;
	count = 0;

	for (j = 0 ; j < (sizeof(keybindings) / sizeof(*keybindings)); j++) {
		b = keybindings[j];
		if (!b)
			continue;
		if (!strncmp (b, command, l)) {
			if (count) {
				if (j == twokeys[0] + 1 && (twokeys[0] == K_LCTRL || twokeys[0] == K_LSHIFT || twokeys[0] == K_LALT)) {

					twokeys[0]--;
					continue;
				}
			}
			twokeys[count] = j;
			count++;
			if (count == 2) {

				if (Key_IsLeftRightSameBind(twokeys[1]))
					twokeys[1]++;
				break;
			}
		}
	}
}

void M_Unscale_Menu(void)
{
	// do not scale this menu
	if (scr_scaleMenu.value) 
	{
		menuwidth = vid.width;
		menuheight = vid.height;
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity ();
		glOrtho  (0, menuwidth, menuheight, 0, -99999, 99999);
	}
}

// will apply menu scaling effect for given window region
// scr_scaleMenu 1 uses glOrtho function and we use the same algorithm in here
static void M_Window_Adjust(const menu_window_t *original, menu_window_t *scaled)
{
	double sc_x, sc_y; // scale factors

	if (scr_scaleMenu.value)
	{
		sc_x = (double) vid.width / (double) menuwidth;
		sc_y = (double) vid.height / (double) menuheight;
		scaled->x = original->x * sc_x;
		scaled->y = original->y * sc_y;
		scaled->w = original->w * sc_x;
		scaled->h = original->h * sc_y;
	}
	else
	{
		memcpy(scaled, original, sizeof(menu_window_t));
	}
}

// this function will look at window borders and current mouse cursor position
// and will change which item in the window should be selected
// we presume that all entries have same height and occupy the whole window
// 1st par: input, window position & size
// 2nd par: input, mouse position
// 3rd par: input, how many entries does the window have
// 4th par: output, newly selected entry, first entry is 0, second 1, ...
// return value: does the cursor belong to this window? yes/no
static qbool M_Mouse_Select(const menu_window_t *uw, const mouse_state_t *m, int entries, int *newentry)
{
	double entryheight;
	double nentry;
	menu_window_t rw;
	menu_window_t *w = &rw; // just a language "shortcut"
	
	M_Window_Adjust(uw, w);

	// window is invisible
	if (!(w->h > 0) || !(w->w > 0)) return false;

	// check if the pointer is inside of the window
	if (m->x < w->x || m->y < w->y || m->x > w->x + w->w || m->y > w->y + w->h)
		return false; // no, it's not

	entryheight = w->h / entries;
	nentry = (int) (m->y - w->y) / (int) entryheight;
	
	*newentry = bound(0, nentry, entries-1);

	return true;
}

//=============================================================================

void M_EnterMenu (int state) {
	if (key_dest != key_menu) {
		m_topmenu = state;
		if (state != m_proxy) {
			Con_ClearNotify ();
			// hide the console
			scr_conlines = 0;
			scr_con_current = 0;
		}
	} else {
		m_topmenu = m_none;
	}

	key_dest = key_menu;
	m_state = state;
	m_entersound = true;
}

static void M_ToggleHeadMenus(int type)
{
	m_entersound = true;

	// do not ever use ingame menu, if user doesn't wish so
	if (!menu_ingame.integer) {
		type = m_main;
	}

	if (key_dest == key_menu) {
		if (m_state != type) {
			M_EnterMenu(type);
			return;
		}
		key_dest = key_game;
		m_state = m_none;
	} else {
		M_EnterMenu(type);
	}
}

void M_ToggleMenu_f (void) {
	if (cls.state == ca_active) {
		M_ToggleHeadMenus(m_ingame);
	}
	else M_ToggleHeadMenus(m_main);
}

void M_EnterProxyMenu () {
	M_EnterMenu(m_proxy);
}

void M_LeaveMenu (int parent) {
	if (m_topmenu == m_state) {
		m_state = m_none;
		key_dest = key_game;
	} else {
		m_state = parent;
		m_entersound = true;
	}
}

// dunno how to call this function
// must leave all menus instead of calling few functions in row
void M_LeaveMenus (void) {
//	m_entersound = true; // fixme: which value we must set ???

	m_topmenu = m_none;
	m_state   = m_none;
	key_dest  = key_game;
}

void M_ToggleProxyMenu_f (void) {
	// this is what user has bound to a key - that means 
	// when in proxy menu -> turn it off; and vice versa
	m_entersound = true;

	if ((key_dest == key_menu) && (m_state == m_proxy)) {
		M_LeaveMenus();
		Menu_Proxy_Toggle();
	} else {
		M_EnterMenu (m_proxy);
		Menu_Proxy_Toggle();
	}
}

//=============================================================================
/* MAIN MENU */

int    m_main_cursor;
static qbool	newmainmenu = false;
menu_window_t m_main_window;

void M_Menu_Main_f (void) {
	M_EnterMenu (m_main);
}

#define BIGLETTER_WIDTH	64
#define BIGLETTER_HEIGHT	64
#define BIGMENU_LEFT				72
#define BIGMENU_TOP					32
#define BIGMENU_ITEMS_SCALE			0.3
#define	BIGMENU_LETTER_SPACING		-2
#define BIGMENU_VERTICAL_PADDING	2

typedef struct bigmenu_items_s {
	const char *label;
	void (* enter_handler) (void);
} bigmenu_items_t;

#define BIGMENU_ITEMS_COUNT(x) (sizeof(x) / sizeof(bigmenu_items_t))

bigmenu_items_t mainmenu_items[] = {
	{"Single Player", M_Menu_SinglePlayer_f},
	{"Multiplayer", M_Menu_MultiPlayer_f},
	{"Options", M_Menu_Options_f},
	{"Demos", M_Menu_Demos_f},
#ifdef WITH_MP3_PLAYER
	{"MP3 Player", M_Menu_MP3_Control_f},
#endif
	{"Help", M_Menu_Help_f},
	{"Quit", M_Menu_Quit_f}
};

#define    MAIN_ITEMS    (newmainmenu ? BIGMENU_ITEMS_COUNT(mainmenu_items) : 5)

// mcharset must be supported in this point
static void M_BigMenu_DrawItems(bigmenu_items_t *menuitems, const unsigned int items, int left_corner, int top_corner, int *width, int *height)
{
	int i;
	int mheight = 0;
	int mwidth = 0;
	int x = left_corner;
	int y = top_corner;

	for (i = 0; i < items; i++) {
		int thiswidth = strlen(menuitems[i].label)*BIGMENU_ITEMS_SCALE*BIGLETTER_WIDTH;
		mheight += BIGMENU_ITEMS_SCALE*BIGLETTER_HEIGHT + BIGMENU_VERTICAL_PADDING;
		mwidth = max(mwidth, thiswidth);
		Draw_BigString(x, y, menuitems[i].label, NULL, 0, 
			BIGMENU_ITEMS_SCALE, 1, BIGMENU_LETTER_SPACING);
		y += BIGMENU_ITEMS_SCALE*BIGLETTER_HEIGHT + BIGMENU_VERTICAL_PADDING;
	}

	*width = mwidth;
	*height = mheight;
}

void M_Main_Draw (void) {
	int f = (int) (curtime * 10) % 6;
	mpic_t *p;
	int itemheight;

	M_DrawTransPic (16, BIGMENU_TOP, Draw_CachePic (QUAKE_ID_PLAQUE_PATH) );

	// the Main Manu heading
	p = Draw_CachePic ("gfx/ttl_main.lmp");
	M_DrawPic ( (320-p->width)/2, 4, p);

	// Main Menu items
	if (Draw_BigFontAvailable()) {
		newmainmenu = true;
		m_main_window.x = BIGMENU_LEFT + (menuwidth - 320)/2;
		m_main_window.y = BIGMENU_TOP + m_yofs;
		M_BigMenu_DrawItems(mainmenu_items, BIGMENU_ITEMS_COUNT(mainmenu_items), m_main_window.x, m_main_window.y,
						 &m_main_window.w, &m_main_window.h);
		itemheight = m_main_window.h / BIGMENU_ITEMS_COUNT(mainmenu_items);
	}
	else {
		newmainmenu = false;
		p = Draw_CachePic ("gfx/mainmenu.lmp");
		m_main_window.w = p->width;
		m_main_window.h = p->height;
		M_DrawTransPic_GetPoint (72, 32, &m_main_window.x, &m_main_window.y, p);
		
		// main menu specific correction, mainmenu.lmp|png have some useless extra space at the bottom
		// that makes the mouse pointer position calculation imperfect
		m_main_window.h *= 0.9;

		itemheight = 20;
	}	

	M_DrawTransPic (54, BIGMENU_TOP + m_main_cursor * itemheight,
		Draw_CachePic(va("gfx/menudot%i.lmp", f+1)) );
}

static void M_Main_Enter(const unsigned int entry)
{
	if (newmainmenu) {
		mainmenu_items[entry].enter_handler();
	}
	else {
		switch (entry) {
		case 0: M_Menu_SinglePlayer_f (); break;
		case 1:	M_Menu_MultiPlayer_f (); break;
		case 2: M_Menu_Options_f (); break;
#ifdef WITH_MP3_PLAYER
		case 3: M_Menu_MP3_Control_f(); break;
#endif // WITH_MP3_PLAYER
		case 4: M_Menu_Quit_f (); break;
		}
	}
}

void M_Main_Key (int key) {
	switch (key) {
	case K_ESCAPE:
	case K_MOUSE2:
		key_dest = key_game;
		m_state = m_none;
		break;

	case '`':
	case '~':
		key_dest = key_console;
		m_state = m_none;
		break;

	case K_UPARROW:
	case K_MWHEELUP:
		S_LocalSound ("misc/menu1.wav");
		if (--m_main_cursor < 0)
			m_main_cursor = MAIN_ITEMS - 1;
		break;

	case K_DOWNARROW:
	case K_MWHEELDOWN:
		S_LocalSound ("misc/menu1.wav");
		if (++m_main_cursor >= MAIN_ITEMS)
			m_main_cursor = 0;
		break;

	case K_HOME:
	case K_PGUP:
		S_LocalSound ("misc/menu1.wav");
		m_main_cursor = 0;
		break;

	case K_END:
	case K_PGDN:
		S_LocalSound ("misc/menu1.wav");
		m_main_cursor = MAIN_ITEMS - 1;
		break;

	case K_ENTER:
	case K_MOUSE1:
		m_entersound = true;
		M_Main_Enter((unsigned) m_main_cursor);
	}
}

static qbool M_Main_Mouse_Event(const mouse_state_t* ms)
{
	M_Mouse_Select(&m_main_window, ms, MAIN_ITEMS, &m_main_cursor);
    
    if (ms->button_up == 1) M_Main_Key(K_MOUSE1);
    if (ms->button_up == 2) M_Main_Key(K_MOUSE2);

    return true;
}

//=============================================================================
/* OPTIONS MENU */

void M_Menu_Options_f (void) {
	M_EnterMenu (m_options);
}

void M_Options_Key (int key, wchar unichar) {
	Menu_Options_Key(key, unichar); // menu_options module
}

void M_Options_Draw (void) {
	Menu_Options_Draw();	// menu_options module
}

//=============================================================================
/* PROXY MENU */

// key press in demos menu
void M_Proxy_Key (int key) {
	int togglekeys[2];

	// the menu_proxy module doesn't know which key user has bound to toggleproxymenu action
	M_FindKeysForCommand("toggleproxymenu", togglekeys);
	if ((key == togglekeys[0]) || (key == togglekeys[1])) {
		Menu_Proxy_Toggle(); 
		M_LeaveMenus();
		return;
	}

	// ppl are used to access console even when in qizmo menu
	M_FindKeysForCommand("toggleconsole", togglekeys);
	if ((key == togglekeys[0]) || (key == togglekeys[1])) {
		Con_ToggleConsole_f();
		return;
	}

	Menu_Proxy_Key(key); // menu_proxy module
}


//=============================================================================
/* HELP MENU */

int        help_page;
#define    NUM_HELP_PAGES    6

void M_Menu_Help_f (void) {
	M_EnterMenu (m_help);
}

void M_Help_Draw (void) {
	int x, y, w, h;

	M_Unscale_Menu();

    // this will add top, left and bottom padding
    // right padding is not added because it causes annoying scrollbar behaviour
    // when mouse gets off the scrollbar to the right side of it
	w = vid.width - OPTPADDING; // here used to be a limit to 512x... size
	h = vid.height - OPTPADDING*2;
	x = OPTPADDING;
	y = OPTPADDING;

	Menu_Help_Draw (x, y, w, h);
}

//=============================================================================
/* QUIT MENU */

int        msgNumber;
int        m_quit_prevstate;
qbool    wasInMenus;

void M_Menu_Quit_f (void) {
	extern cvar_t cl_confirmquit;

	if (!cl_confirmquit.integer) Host_Quit();

	if (m_state == m_quit)
		return;
	wasInMenus = (key_dest == key_menu);
	m_quit_prevstate = m_state;
	msgNumber = rand()&7;
	M_EnterMenu (m_quit);
}

void M_Quit_Key (int key) {
	switch (key) {
		case K_MOUSE2:
		case K_ESCAPE:
		case 'n':
		case 'N':
			if (wasInMenus) {
				m_state = m_quit_prevstate;
				m_entersound = true;
			} else {
				key_dest = key_game;
				m_state = m_none;
			}
			break;

		case '`':
		case '~':
			key_dest = key_console;
			m_state = m_none;
			break;

		case K_MOUSE1:
		case K_ENTER:
		case 'Y':
		case 'y':
			key_dest = key_console;
			Host_Quit ();
			break;

		default:
			break;
	}
}

void M_Quit_Draw (void) { // Quit screen text.
	M_DrawTextBox (0, 0, 38, 26);
	M_PrintWhite (16, 12,  "  Quake version 1.09 by id Software\n\n");
	M_PrintWhite (16, 28,  "Programming        Art \n");
	M_Print (16, 36,  " John Carmack       Adrian Carmack\n");
	M_Print (16, 44,  " Michael Abrash     Kevin Cloud\n");
	M_Print (16, 52,  " John Cash          Paul Steed\n");
	M_Print (16, 60,  " Dave 'Zoid' Kirsch\n");
	M_PrintWhite (16, 68,  "Design             Biz\n");
	M_Print (16, 76,  " John Romero        Jay Wilbur\n");
	M_Print (16, 84,  " Sandy Petersen     Mike Wilson\n");
	M_Print (16, 92,  " American McGee     Donna Jackson\n");
	M_Print (16, 100,  " Tim Willits        Todd Hollenshead\n");
	M_PrintWhite (16, 108, "Support            Projects\n");
	M_Print (16, 116, " Barrett Alexander  Shawn Green\n");
	M_PrintWhite (16, 124, "Sound Effects\n");
	M_Print (16, 132, " Trent Reznor and Nine Inch Nails\n\n");
	M_PrintWhite (16, 148, "Quake is a trademark of Id Software,\n");
	M_PrintWhite (16, 156, "inc., (c)1996 Id Software, inc. All\n");
	M_PrintWhite (16, 164, "rights reserved. NIN logo is a\n");
	M_PrintWhite (16, 172, "registered trademark licensed to\n");
	M_PrintWhite (16, 180, "Nothing Interactive, Inc. All rights\n");
	M_PrintWhite (16, 188, "reserved.\n\n");
	M_Print (16, 204, "          Press y to exit\n");
}

//=============================================================================
/* SINGLE PLAYER MENU */

#ifndef CLIENTONLY

#define    SINGLEPLAYER_ITEMS    3
int    m_singleplayer_cursor;
qbool m_singleplayer_confirm;
qbool m_singleplayer_notavail;
menu_window_t m_singleplayer_window;
static qbool m_singleplayer_big = false;
static bigmenu_items_t m_singleplayer_items[] = {
	{"New Game", NULL},
	{"Load", NULL}, 
	{"Save", NULL}
};

extern    cvar_t    maxclients;

void M_Menu_SinglePlayer_f (void) {
	M_EnterMenu (m_singleplayer);
	m_singleplayer_confirm = false;
	m_singleplayer_notavail = false;
}

void M_SinglePlayer_Draw (void) {
	int f = (int)(curtime * 10)%6;
	mpic_t *p;
	int itemheight;

#ifndef WITH_NQPROGS
	if (m_singleplayer_notavail) {
		p = Draw_CachePic ("gfx/ttl_sgl.lmp");
		M_DrawPic ( (320-p->width)/2, 4, p);
		M_DrawTextBox (60, 10*8, 24, 4);
		M_PrintWhite (80, 12*8, " Cannot start a game");
		M_PrintWhite (80, 13*8, "spprogs.dat not found");
		return;
	}
#endif

	if (m_singleplayer_confirm) {
		M_PrintWhite (64, 11*8, "Are you sure you want to");
		M_PrintWhite (64, 12*8, "    start a new game?");
		return;
	}

	M_DrawTransPic (16, BIGMENU_TOP, Draw_CachePic (QUAKE_ID_PLAQUE_PATH) );
	p = Draw_CachePic ("gfx/ttl_sgl.lmp");
	M_DrawPic ( (320-p->width)/2, 4, p);

	if (Draw_BigFontAvailable()) {
		m_singleplayer_big = true;
		m_singleplayer_window.x = BIGMENU_LEFT + ((menuwidth - 320)>>1);
		m_singleplayer_window.y = BIGMENU_TOP + m_yofs;
		M_BigMenu_DrawItems(m_singleplayer_items, BIGMENU_ITEMS_COUNT(m_singleplayer_items),
			m_singleplayer_window.x, m_singleplayer_window.y, &m_singleplayer_window.w,
			&m_singleplayer_window.h);
		itemheight = m_singleplayer_window.h / BIGMENU_ITEMS_COUNT(m_singleplayer_items);
	}
	else {
		m_singleplayer_big = false;
		p = Draw_CachePic ("gfx/sp_menu.lmp");
		m_singleplayer_window.w = p->width;
		m_singleplayer_window.h = p->height;
		M_DrawTransPic_GetPoint(72, 32, &m_singleplayer_window.x, &m_singleplayer_window.y, p);
		itemheight = 20;
	}

	M_DrawTransPic (54, BIGMENU_TOP + m_singleplayer_cursor * itemheight,
		Draw_CachePic( va("gfx/menudot%i.lmp", f+1 ) ) );

}

#ifndef WITH_NQPROGS
static void CheckSPGame (void) {
	vfsfile_t *v;
	if ((v = FS_OpenVFS("spprogs.dat", "rb", FS_ANY))) {
		VFS_CLOSE(v);
		m_singleplayer_notavail = false;
	} else {
		m_singleplayer_notavail = true;
	}
}
#endif	// !WITH_NQPROGS

static void StartNewGame (void) {
	extern cvar_t sv_progtype;

	key_dest = key_game;
	m_state = m_none;
	Cvar_Set (&maxclients, "1");
	Cvar_Set (&teamplay, "0");
	Cvar_Set (&deathmatch, "0");
	Cvar_Set (&coop, "0");

	Cvar_Set (&sv_progsname, "spprogs"); // force progsname
#ifdef USE_PR2
	Cvar_Set (&sv_progtype, "0"); // force .dat
#endif

	if (com_serveractive)
		Cbuf_AddText ("disconnect\n");

	progs = (dprograms_t *) FS_LoadHunkFile ("spprogs.dat", NULL);
	//if (progs && !file_from_gamedir)
	//	Cbuf_AddText ("gamedir qw\n");
	Cbuf_AddText ("map start\n");
}

void M_SinglePlayer_Key (int key) {
#ifndef WITH_NQPROGS
	if (m_singleplayer_notavail) {
		switch (key) {
			case K_BACKSPACE:
			case K_ESCAPE:
			case K_ENTER:
				m_singleplayer_notavail = false;
				break;
		}
		return;
	}
#endif

	if (m_singleplayer_confirm) {
		if (key == K_ESCAPE || key == 'n' || key == K_MOUSE2) {
			m_singleplayer_confirm = false;
			m_entersound = true;
		} else if (key == 'y' || key == K_ENTER || key == K_MOUSE1) {
			StartNewGame ();
		}
		return;
	}

	switch (key) {
		case K_BACKSPACE:
			m_topmenu = m_none;    // intentional fallthrough
		case K_MOUSE2:
		case K_ESCAPE:
			M_LeaveMenu (m_main);
			break;

		case '`':
		case '~':
			key_dest = key_console;
			m_state = m_none;
			break;

		case K_DOWNARROW:
		case K_MWHEELDOWN:
			S_LocalSound ("misc/menu1.wav");
			if (++m_singleplayer_cursor >= SINGLEPLAYER_ITEMS)
				m_singleplayer_cursor = 0;
			break;

		case K_UPARROW:
		case K_MWHEELUP:
			S_LocalSound ("misc/menu1.wav");
			if (--m_singleplayer_cursor < 0)
				m_singleplayer_cursor = SINGLEPLAYER_ITEMS - 1;
			break;

		case K_HOME:
		case K_PGUP:
			S_LocalSound ("misc/menu1.wav");
			m_singleplayer_cursor = 0;
			break;

		case K_END:
		case K_PGDN:
			S_LocalSound ("misc/menu1.wav");
			m_singleplayer_cursor = SINGLEPLAYER_ITEMS - 1;
			break;

		case K_ENTER:
		case K_MOUSE1:
			switch (m_singleplayer_cursor) {
				case 0:
#ifndef WITH_NQPROGS
					CheckSPGame ();
					if (m_singleplayer_notavail) {
						m_entersound = true;
						return;
					}
#endif
					if (com_serveractive) {
						// bring up confirmation dialog
						m_singleplayer_confirm = true;
						m_entersound = true;
					} else {
						StartNewGame ();
					}
					break;

				case 1:
					M_Menu_Load_f ();
					break;

				case 2:
					M_Menu_Save_f ();
					break;
			}
	}
}

qbool M_SinglePlayer_Mouse_Event(const mouse_state_t* ms)
{
	M_Mouse_Select(&m_singleplayer_window, ms, SINGLEPLAYER_ITEMS, &m_singleplayer_cursor);

    if (ms->button_up == 1) M_SinglePlayer_Key(K_MOUSE1);
    if (ms->button_up == 2) M_SinglePlayer_Key(K_MOUSE2);

    return true;
}

#else    // !CLIENTONLY

void M_Menu_SinglePlayer_f (void) {
	M_EnterMenu (m_singleplayer);
}

void M_SinglePlayer_Draw (void) {
	mpic_t *p;

	M_DrawTransPic (16, 4, Draw_CachePic (QUAKE_ID_PLAQUE_PATH) );
	p = Draw_CachePic ("gfx/ttl_sgl.lmp");
	M_DrawPic ( (320-p->width)/2, 4, p);
	//    M_DrawTransPic (72, 32, Draw_CachePic ("gfx/sp_menu.lmp") );

	M_DrawTextBox (60, 10*8, 23, 4);
	M_PrintWhite (88, 12*8, "This client is for");
	M_PrintWhite (88, 13*8, "Internet play only");
}

void M_SinglePlayer_Key (key) {
	switch (key) {
		case K_BACKSPACE:
			m_topmenu = m_none;    // intentional fallthrough
		case K_ESCAPE:
		case K_ENTER:
			M_LeaveMenu (m_main);
			break;

		case '`':
		case '~':
			key_dest = key_console;
			m_state = m_none;
			break;
	}
}
#endif    // CLIENTONLY


//=============================================================================
/* LOAD/SAVE MENU */

#ifndef CLIENTONLY

#define    MAX_SAVEGAMES        12

int        load_cursor;        // 0 < load_cursor < MAX_SAVEGAMES
char    m_filenames[MAX_SAVEGAMES][SAVEGAME_COMMENT_LENGTH + 1];
int        loadable[MAX_SAVEGAMES];
menu_window_t load_window, save_window;

void M_ScanSaves (char *sp_gamedir) {
	int i, j;
	char name[MAX_OSPATH];
	vfsfile_t *f;

	for (i = 0; i < MAX_SAVEGAMES; i++) {
		strlcpy (m_filenames[i], "--- UNUSED SLOT ---", SAVEGAME_COMMENT_LENGTH + 1);
		loadable[i] = false;

		snprintf (name, sizeof(name), "save/s%i.sav", i);
		if (!(f = FS_OpenVFS(name, "rb", FS_GAME_OS)))
			continue;
		VFS_GETS(f, name, sizeof(name));
		VFS_GETS(f, name, sizeof(name));
		strlcpy (m_filenames[i], name, sizeof(m_filenames[i]));

		// change _ back to space
		for (j = 0; j < SAVEGAME_COMMENT_LENGTH; j++)
			if (m_filenames[i][j] == '_')
				m_filenames[i][j] = ' ';
		loadable[i] = true;
		VFS_CLOSE(f);
	}
}

void M_Menu_Load_f (void) {
	vfsfile_t *f;

	if (!(f = FS_OpenVFS("spprogs.dat", "rb", FS_ANY)))
		return;

	M_EnterMenu (m_load);
	// VFS-FIXME: file_from_gamedir is not set in FS_OpenVFS
	M_ScanSaves (!file_from_gamedir ? "qw" : com_gamedir);
}

void M_Menu_Save_f (void) {
	if (sv.state != ss_active)
		return;
	if (cl.intermission)
		return;

	M_EnterMenu (m_save);
	M_ScanSaves (com_gamedir);
}

void M_Load_Draw (void) {
	int i;
	mpic_t *p;
	int lx = 0, ly = 0;	// lower bounds of the window

	p = Draw_CachePic ("gfx/p_load.lmp");
	M_DrawPic ( (320 - p->width) >> 1, 4, p);

	for (i = 0; i < MAX_SAVEGAMES; i++)
	{
		if (i == 0)
			M_Print_GetPoint (16, 32 + 8*i, &load_window.x, &load_window.y, m_filenames[i], load_cursor == 0);
		else 
			M_Print_GetPoint (16, 32 + 8*i, &lx, &ly, m_filenames[i], load_cursor == i);
	}

	load_window.w = SAVEGAME_COMMENT_LENGTH*8; // presume 8 pixels for each letter
	load_window.h = ly - load_window.y + 8;

	// line cursor
	M_DrawCharacter (8, 32 + load_cursor * 8, FLASHINGARROW());
}

void M_Save_Draw (void) {
	int i;
	mpic_t *p;
	int lx = 0, ly = 0;	// lower bounds of the window

	p = Draw_CachePic ("gfx/p_save.lmp");
	M_DrawPic ( (320 - p->width) >> 1, 4, p);

	for (i = 0; i < MAX_SAVEGAMES; i++)
	{
		if (i == 0)
			M_Print_GetPoint (16, 32 + 8 * i, &save_window.x, &save_window.y, m_filenames[i], load_cursor == 0);
		else
			M_Print_GetPoint (16, 32 + 8 * i, &lx, &ly, m_filenames[i], load_cursor == i);
	}

	save_window.w = SAVEGAME_COMMENT_LENGTH*8; // presume 8 pixels for each letter
	save_window.h = ly - save_window.y + 8;

	// line cursor
	M_DrawCharacter (8, 32 + load_cursor * 8, FLASHINGARROW());
}

void M_Load_Key (int key) {
	switch (key) {
		case K_BACKSPACE:
			m_topmenu = m_none;    // intentional fallthrough
		case K_MOUSE2:
		case K_ESCAPE:
			M_LeaveMenu (m_singleplayer);
			break;

		case '`':
		case '~':
			key_dest = key_console;
			m_state = m_none;
			break;

		case K_ENTER:
		case K_MOUSE1:
			S_LocalSound ("misc/menu2.wav");
			if (!loadable[load_cursor])
				return;
			m_state = m_none;
			key_dest = key_game;

			// issue the load command
			if (FS_LoadHunkFile ("spprogs.dat", NULL) && !file_from_gamedir)
				; //Cbuf_AddText("disconnect; gamedir qw\n");
			Cbuf_AddText (va ("load s%i\n", load_cursor) );
			return;

		case K_UPARROW:
		case K_MWHEELUP:
		case K_LEFTARROW:
			S_LocalSound ("misc/menu1.wav");
			load_cursor--;
			if (load_cursor < 0)
				load_cursor = MAX_SAVEGAMES - 1;
			break;

		case K_DOWNARROW:
		case K_MWHEELDOWN:
		case K_RIGHTARROW:
			S_LocalSound ("misc/menu1.wav");
			load_cursor++;
			if (load_cursor >= MAX_SAVEGAMES)
				load_cursor = 0;
			break;
	}
}

void M_Save_Key (int key) {
	switch (key) {
		case K_BACKSPACE:
			m_topmenu = m_none;    // intentional fallthrough
		case K_MOUSE2:
		case K_ESCAPE:
			M_LeaveMenu (m_singleplayer);
			break;

		case '`':
		case '~':
			key_dest = key_console;
			m_state = m_none;
			break;

		case K_ENTER:
		case K_MOUSE1:
			m_state = m_none;
			key_dest = key_game;
			Cbuf_AddText (va("save s%i\n", load_cursor));
			return;

		case K_UPARROW:
		case K_MWHEELUP:
		case K_LEFTARROW:
			S_LocalSound ("misc/menu1.wav");
			load_cursor--;
			if (load_cursor < 0)
				load_cursor = MAX_SAVEGAMES-1;
			break;

		case K_DOWNARROW:
		case K_MWHEELDOWN:
		case K_RIGHTARROW:
			S_LocalSound ("misc/menu1.wav");
			load_cursor++;
			if (load_cursor >= MAX_SAVEGAMES)
				load_cursor = 0;
			break;
	}
}

qbool M_Save_Mouse_Event(const mouse_state_t *ms)
{
	M_Mouse_Select(&save_window, ms, MAX_SAVEGAMES, &load_cursor);

    if (ms->button_up == 1) M_Save_Key(K_MOUSE1);
    if (ms->button_up == 2) M_Save_Key(K_MOUSE2);

	return true;
}

qbool M_Load_Mouse_Event(const mouse_state_t *ms)
{
	M_Mouse_Select(&load_window, ms, MAX_SAVEGAMES, &load_cursor);

    if (ms->button_up == 1) M_Load_Key(K_MOUSE1);
    if (ms->button_up == 2) M_Load_Key(K_MOUSE2);

    return true;
}

#endif


// ================================
// Multiplayer submenu
// used only if mcharset is not available making demo player menu inaccesible from mainmenu

int    m_multiplayer_cursor;
#define    MULTIPLAYER_ITEMS    2
menu_window_t m_multiplayer_window;

void M_MultiPlayerSub_Draw (void) {
	mpic_t    *p;
	int lx, ly;

	M_DrawTransPic (16, 4, Draw_CachePic ("gfx/qplaque.lmp") );
	p = Draw_CachePic ("gfx/p_multi.lmp");
	M_DrawPic ( (320-p->width)/2, 4, p);
	M_Print_GetPoint (80, 40, &m_multiplayer_window.x, &m_multiplayer_window.y, "Join Game", m_multiplayer_cursor == 0);
	m_multiplayer_window.h = 8;
	M_Print_GetPoint (80, 48, &lx, &ly, "Demos", m_multiplayer_cursor == MULTIPLAYER_ITEMS - 1);
	m_multiplayer_window.h += 8;
	m_multiplayer_window.w = 20 * 8; // presume 20 letters long word and 8 pixels for a letter
	
	// cursor
	M_DrawCharacter (64, 40 + m_multiplayer_cursor * 8, FLASHINGARROW());
}

void M_MultiPlayerSub_Key (int key) {
	switch (key) {
		case K_BACKSPACE:
			m_topmenu = m_none;    // intentional fallthrough
		case K_MOUSE2:
		case K_ESCAPE:
			M_LeaveMenu (m_main);
			break;

		case '`':
		case '~':
			key_dest = key_console;
			m_state = m_none;
			break;

		case K_DOWNARROW:
		case K_MWHEELDOWN:
			S_LocalSound ("misc/menu1.wav");
			if (++m_multiplayer_cursor >= MULTIPLAYER_ITEMS)
				m_multiplayer_cursor = 0;
			break;

		case K_UPARROW:
		case K_MWHEELUP:
			S_LocalSound ("misc/menu1.wav");
			if (--m_multiplayer_cursor < 0)
				m_multiplayer_cursor = MULTIPLAYER_ITEMS - 1;
			break;

		case K_HOME:
		case K_PGUP:
			S_LocalSound ("misc/menu1.wav");
			m_multiplayer_cursor = 0;
			break;

		case K_END:
		case K_PGDN:
			S_LocalSound ("misc/menu1.wav");
			m_multiplayer_cursor = MULTIPLAYER_ITEMS - 1;
			break;

		case K_ENTER:
		case K_MOUSE1:
			m_entersound = true;
			switch (m_multiplayer_cursor) {
				case 0:
					M_EnterMenu(m_multiplayer);
					break;
				case 1:
					M_Menu_Demos_f ();
					break;
			}
	}
}

qbool M_MultiPlayerSub_Mouse_Event(const mouse_state_t *ms)
{
	M_Mouse_Select(&m_multiplayer_window, ms, MULTIPLAYER_ITEMS, &m_multiplayer_cursor);

    if (ms->button_up == 1) M_MultiPlayerSub_Key(K_MOUSE1);
    if (ms->button_up == 2) M_MultiPlayerSub_Key(K_MOUSE2);
    
    return true;
}

void M_Menu_MultiPlayer_f (void)
{
	if (Draw_BigFontAvailable()) {
		M_EnterMenu(m_multiplayer);
	}
	else {
		// demos entry is not accessible with old main menu
		// so we need to create a submenu in the multiplayer menu
		M_EnterMenu(m_multiplayer_submenu);
	}
}

void M_Menu_Demos_f (void)
{
	M_EnterMenu(m_demos); // switches client state
}

//=============================================================================
/* Menu Subsystem */

void M_Init (void) {
	extern cvar_t menu_marked_bgcolor;
	extern cvar_t menu_marked_fade;

	Cvar_SetCurrentGroup(CVAR_GROUP_MENU);
	Cvar_Register (&scr_centerMenu);
	Cvar_Register (&menu_ingame);
	Cvar_Register (&scr_scaleMenu);
	Cvar_Register (&menu_marked_fade);

	Cvar_Register (&menu_marked_bgcolor);
	Browser_Init();
	Cvar_ResetCurrentGroup();
	Menu_Help_Init();	// help_files module
	Menu_Demo_Init();	// menu_demo module
	Menu_Options_Init(); // menu_options module
	Menu_Ingame_Init();
	Menu_MultiPlayer_Init(); // menu_multiplayer.h

	Cmd_AddCommand ("togglemenu", M_ToggleMenu_f);
	Cmd_AddCommand ("toggleproxymenu", M_ToggleProxyMenu_f);

	Cmd_AddCommand ("menu_main", M_Menu_Main_f);
#ifndef CLIENTONLY
	Cmd_AddCommand ("menu_singleplayer", M_Menu_SinglePlayer_f);
	Cmd_AddCommand ("menu_load", M_Menu_Load_f);
	Cmd_AddCommand ("menu_save", M_Menu_Save_f);
#endif
	Cmd_AddCommand ("menu_multiplayer", M_Menu_MultiPlayer_f);
	Cmd_AddCommand ("menu_slist", M_Menu_MultiPlayer_f);
#ifdef WITH_MP3_PLAYER
	Cmd_AddCommand ("menu_mp3_control", M_Menu_MP3_Control_f);
	Cmd_AddCommand ("menu_mp3_playlist", M_Menu_MP3_Playlist_f);
#endif
	Cmd_AddCommand ("menu_demos", M_Menu_Demos_f);
	Cmd_AddCommand ("menu_options", M_Menu_Options_f);
	Cmd_AddCommand ("help", M_Menu_Help_f);
	Cmd_AddCommand ("menu_help", M_Menu_Help_f);
	Cmd_AddCommand ("menu_quit", M_Menu_Quit_f);
}

void M_Draw (void) {
	if (m_state == m_none || key_dest != key_menu || m_state == m_proxy)
		return;

	if (!m_recursiveDraw) {
		scr_copyeverything = 1;

		if (SCR_NEED_CONSOLE_BACKGROUND) {
			Draw_ConsoleBackground (scr_con_current);
			CL_S_ExtraUpdate ();
		} else {
			// if you don't like fade in ingame menu, uncomment this
			// if (m_state != m_ingame && m_state != m_democtrl)
			Draw_FadeScreen (scr_menualpha.value);
		}

		scr_fullupdate = 0;
	} else {
		m_recursiveDraw = false;
	}

	if (scr_scaleMenu.value) {
		menuwidth = 320;
		menuheight = min (vid.height, 240);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity ();
		glOrtho  (0, menuwidth, menuheight, 0, -99999, 99999);
	} else {
		menuwidth = vid.width;
		menuheight = vid.height;
	}

	if (scr_centerMenu.value)
		m_yofs = (menuheight - 200) / 2;
	else
		m_yofs = 0;

	switch (m_state) {
		case m_none: break;
		case m_main:			M_Main_Draw (); break;
		case m_singleplayer:	M_SinglePlayer_Draw (); break;
#ifndef CLIENTONLY
		case m_load:			M_Load_Draw (); break;
		case m_save:			M_Save_Draw (); break;
#endif
		case m_multiplayer:		Menu_MultiPlayer_Draw (); break;
		case m_multiplayer_submenu: M_MultiPlayerSub_Draw(); break;
		case m_options:			M_Options_Draw(); break;
		case m_proxy:			Menu_Proxy_Draw(); break;
		case m_ingame:			M_Ingame_Draw(); break;
		case m_help:			M_Help_Draw(); break;
		case m_quit:			M_Quit_Draw(); break;
		case m_demos:			Menu_Demo_Draw(); break;
#ifdef WITH_MP3_PLAYER
		case m_mp3_control:		M_Menu_MP3_Control_Draw(); break;
		case m_mp3_playlist:	M_Menu_MP3_Playlist_Draw(); break;
#endif
	}

	if (scr_scaleMenu.value) {
		glMatrixMode (GL_PROJECTION);
		glLoadIdentity ();
		glOrtho  (0, vid.width, vid.height, 0, -99999, 99999);
	}

	if (m_entersound) {
		S_LocalSound ("misc/menu2.wav");
		m_entersound = false;
	}
	CL_S_ExtraUpdate ();
}

void M_Keydown (int key, wchar unichar) {
	switch (m_state) {
		case m_none: return;
		case m_main:			M_Main_Key(key); return;
		case m_singleplayer:	M_SinglePlayer_Key(key); return;
#ifndef CLIENTONLY
		case m_load:			M_Load_Key(key); return;
		case m_save:			M_Save_Key(key); return;
#endif
		case m_multiplayer:		Menu_MultiPlayer_Key(key, unichar); return;
		case m_multiplayer_submenu: M_MultiPlayerSub_Key(key); return;
		case m_options: 		M_Options_Key(key, unichar); return;
		case m_proxy:			M_Proxy_Key(key); return;
		case m_ingame:			M_Ingame_Key(key); return;
		case m_help:			Menu_Help_Key(key, unichar); return;
		case m_quit:			M_Quit_Key(key); return;
		case m_demos:			Menu_Demo_Key(key, unichar); break;
#ifdef WITH_MP3_PLAYER
		case m_mp3_control: 	M_Menu_MP3_Control_Key(key); break;
		case m_mp3_playlist: 	M_Menu_MP3_Playlist_Key(key); break;
#endif
	}
}

qbool Menu_Mouse_Event(const mouse_state_t* ms)
{
	if (ms->button_down == K_MWHEELDOWN || ms->button_up == K_MWHEELDOWN ||
		ms->button_down == K_MWHEELUP   || ms->button_up == K_MWHEELUP)
	{
		// menus do not handle this type of mouse wheel event, they accept it as a key event	
		return false;
	}

	// send the mouse state to appropriate modules here
    // functions should report if they handled the event or not
    switch (m_state) {
	case m_main:			return M_Main_Mouse_Event(ms);
	case m_singleplayer:	return M_SinglePlayer_Mouse_Event(ms);
	case m_multiplayer:		return Menu_MultiPlayer_Mouse_Event(ms);
	case m_multiplayer_submenu: return M_MultiPlayerSub_Mouse_Event(ms);
#ifndef CLIENTONLY
	case m_load:			return M_Load_Mouse_Event(ms);
	case m_save:			return M_Save_Mouse_Event(ms);
#endif
	case m_options:			return Menu_Options_Mouse_Event(ms);
	case m_demos:			return Menu_Demo_Mouse_Event(ms);
	case m_ingame:			return Menu_Ingame_Mouse_Event(ms);
#ifdef WITH_MP3_PLAYER
	case m_mp3_control:		return M_Menu_MP3_Control_Mouse_Event(ms);
	case m_mp3_playlist:	return M_Menu_MP3_Playlist_Mouse_Event(ms);
#endif
	case m_help:			return Menu_Help_Mouse_Event(ms);
	case m_none: default:	return false;
	}
}