File: mi_tool.c

package info (click to toggle)
xblast-tnt 2.10.4-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,444 kB
  • sloc: ansic: 54,105; sh: 4,014; makefile: 129; sed: 16
file content (980 lines) | stat: -rw-r--r-- 21,606 bytes parent folder | download | duplicates (6)
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
/*
 * file mi_tool.c - toolkit for xblast menus
 *
 * $Id: mi_tool.c,v 1.33 2006/02/10 13:22:12 fzago Exp $
 *
 * Program XBLAST
 * (C) by Oliver Vogel (e-mail: m.vogel@ndh.net)
 *
 * 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; or (at your option)
 * any later version
 *
 * This program is distributed in the hope that it will be entertaining,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILTY 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
 */

#include "xblast.h"

/*
 * local variables
 */
static XBMenuItem *itemFirst = NULL;
static XBMenuItem *itemLast = NULL;
static XBMenuItem *itemFocus = NULL;
static XBBool execFlag = XBTrue;
static XBBool fadeFlag = XBFalse;
static XBMenuItem *defaultItem = NULL;
static XBMenuItem *abortItem = NULL;
static XBBool mapMode = 0;
static int setPressed = 0;
static XBComboEntryList yesNoTable[] = {
	{N_("no"), 0, NULL},
	{N_("yes"), 1, NULL},
	{NULL, 0, NULL},
};

/*
 * delete any menu item
 */
static void
DeleteMenuItem (XBMenuItem * item)
{
	assert (item != NULL);
	switch (item->type) {
	case MIT_Button:
		MenuDeleteButton (item);
		break;
	case MIT_Color:
		MenuDeleteColor (item);
		break;
	case MIT_Combo:
		MenuDeleteCombo (item);
		break;
	case MIT_Cyclic:
		MenuDeleteCyclic (item);
		break;
	case MIT_Keysym:
		MenuDeleteKeysym (item);
		break;
	case MIT_Label:
		MenuDeleteLabel (item);
		break;
	case MIT_Player:
		MenuDeletePlayer (item);
		break;
	case MIT_String:
		MenuDeleteString (item);
		break;
	case MIT_Toggle:
		MenuDeleteToggle (item);
		break;
	case MIT_Integer:
		MenuDeleteInteger (item);
		break;
	case MIT_Tag:
		MenuDeleteTag (item);
		break;
	case MIT_Host:
		MenuDeleteHost (item);
		break;
	case MIT_Table:
		MenuDeleteTable (item);
		break;
	case MIT_Team:
		MenuDeleteTeam (item);
		break;
	default:
		break;
	}
	free (item);
}								/* DeleteMenuItem */

/*****************
 * mouse buttons *
 *****************/

/*
 * store press status of mouse
 */
void
SetPressed (XBBool mode)
{
	setPressed = mode;
}								/* SetPressed */

/*
 * get press status of mouse
 */
XBBool
GetPressed (void)
{
	return setPressed;
}								/* GetPressed */

/***************
 * editor mode *
 ***************/

/*
 * set editor mode
 */
void
SetXBEditMapMode (XBBool mode)
{
	mapMode = mode;
}								/* SetXBEditMapMode */

/*
 * get editor mode
 */
XBBool
GetXBEditMapMode (void)
{
	return mapMode;
}								/* GetXBEditMapMode */

/***************
 * local tools *
 ***************/

/*
 * find item by id
 */
static XBMenuItem *
FindItem (MENU_ID id)
{
	XBMenuItem *item;

	for (item = itemFirst; item != NULL; item = item->next) {
		if (item->id == id) {
			return item;
		}
	}
	return NULL;
}								/* FindItem */

/**************
 * menu tools *
 **************/

/*
 * an menu item was selected (i.e Space was pressed)
 */
static void
SelectItem (XBMenuItem * item)
{
	if (item != NULL && item->select != NULL) {
		(*item->select) (item);
	}
}								/* SelectItem */

/*
 * clear all menu items
 */
void
MenuClear (void)
{
	XBMenuItem *item_next;
	while (itemFirst != NULL) {
		item_next = itemFirst->next;
		DeleteMenuItem (itemFirst);
		itemFirst = item_next;
	}
	itemLast = itemFocus = NULL;
	fadeFlag = XBTrue;
	defaultItem = NULL;
	abortItem = NULL;
	MenuResetBase ();
	MenuClearMap ();
}								/* MenuClear */

/*
 * set directional links between items for arrow keys
 */
void
MenuSetLinks (void)
{
	XBMenuItem *ptr;

	for (ptr = itemFirst; ptr != NULL; ptr = ptr->next) {
		if (NULL != ptr->mouse) {
			ptr->left = MenuFindLeftItem (ptr);
			ptr->right = MenuFindRightItem (ptr);
			ptr->up = MenuFindUpperItem (ptr);
			ptr->down = MenuFindLowerItem (ptr);
		}
	}
}								/* MenuSetLinks */

/*
 * select item
 */
void
MenuSelect (MENU_ID id)
{
	SelectItem (FindItem (id));
}								/* MenuSelect */

/*
 * set default item
 */
void
MenuSetDefault (MENU_ID id)
{
	defaultItem = FindItem (id);
	if (NULL != defaultItem && MIT_Button == defaultItem->type) {
		MenuSetButtonIcon (defaultItem, ISA_Default);
	}
}								/* MenuSetDefault */

/*
 * set abort item
 */
void
MenuSetAbort (MENU_ID id)
{
	abortItem = FindItem (id);
	if (NULL != abortItem && MIT_Button == abortItem->type) {
		MenuSetButtonIcon (abortItem, ISA_Abort);
	}
}								/* MenuSetDefault */

/*
 * async execution of function
 */
void
MenuExecFunc (MIC_button func, void *data)
{
	execFlag = XBTrue;
	MenuButtonSetNextExec (func, data);
}								/* MenuExecFunc */

/*
 * activate/deactivate item
 */
void
MenuSetActive (MENU_ID id, XBBool active)
{
	XBMenuItem *item = FindItem (id);
	if (NULL != item) {
		/* set flags */
		if (active) {
			item->flags &= ~MIF_DEACTIVATED;
		}
		else {
			item->flags |= MIF_DEACTIVATED;
		}
		switch (item->type) {
		case MIT_Button:
			MenuActivateButton (item, active);
			break;
		case MIT_Table:
			MenuActivateTable (item, active);
			break;
		default:
			break;
		}
	}
}								/* MenuSetActive */

/*
 * get item id with focus
 */
MENU_ID
MenuGetFocus (void)
{
	return (NULL != itemFocus) ? itemFocus->id : 0;
}								/* MenuGetFocus */

/*------------------------------------------------------------------------*
 *
 * add items to menu
 *
 *------------------------------------------------------------------------*/

/*
 * add a generic item to the menu
 */
static MENU_ID
MenuAdd (XBMenuItem * item)
{
	if (itemLast == NULL) {
		itemFirst = item;
	}
	if (itemLast != NULL) {
		itemLast->next = item;
		item->prev = itemLast;
	}
	itemLast = item;
	itemLast->next = NULL;
	if (itemFocus == NULL && item->focus != NULL) {
		item->flags |= MIF_FOCUS;
		itemFocus = item;
		(*item->focus) (item, XBTrue);
	}
	return item->id;
}								/* MenuAdd */

/*
 * add horizontal button to menu
 */
MENU_ID
MenuAddHButton (int x, int y, int w, const char *text, MIC_button func, void *funcData)
{
	return MenuAdd (MenuCreateHButton (x, y, w, text, func, funcData));
}								/* MenuAddHButton */

/*
 * add vertical button to menu
 */
MENU_ID
MenuAddVButton (int x, int y, int h, const char *text, MIC_button func, void *funcData)
{
	return MenuAdd (MenuCreateVButton (x, y, h, text, func, funcData));
}								/* MenuAddVButton */

/*
 * add a checkbox item to menu
 */
MENU_ID
MenuAddToggle (int x, int y, int w, const char *text, XBBool * pState)
{
	return MenuAdd (MenuCreateToggle (x, y, w, text, pState));
}								/* MenuAddToggle */

/*
 * add a label/title to menu
 */
MENU_ID
MenuAddLabel (int x, int y, int w, const char *text)
{
	return MenuAdd (MenuCreateLabel (x, y, w, text));
}								/* MenuAddLabel */

/*
 * add label variant 1 to menu
 */
MENU_ID
MenuAddLabel1 (int x, int y, int w, const char *text)
{
	return MenuAdd (MenuCreateLabel1 (x, y, w, text));
}								/* MenuAddLabel1 */

/*
 * add label variant 2 to menu
 */
MENU_ID
MenuAddLabel2 (int x, int y, int w, const char *text)
{
	return MenuAdd (MenuCreateLabel2 (x, y, w, text));
}								/* MenuAddLabel2 */

/*
 * add a combobox to menu, check only integer keys
 */
MENU_ID
MenuAddComboInt (int x, int y, int w_text, const char *text, int w, int *value,
				 XBComboEntryList * table)
{
	return MenuAdd (MenuCreateCombo (x, y, w_text, text, w, value, NULL, NULL, table));
}								/* MenuAddComboInt */

/*
 * a simple yes/no combobox to menu
 */
MENU_ID
MenuAddComboBool (int x, int y, int w_text, const char *text, int w, XBBool * value)
{
	return MenuAdd (MenuCreateCombo (x, y, w_text, text, w, (int *)value, NULL, NULL, yesNoTable));
}								/* MenuAddComboInt */

/*
 * add a combobox to menu, check only data entry
 */
MENU_ID
MenuAddComboData (int x, int y, int w_text, const char *text, int w, void **data,
				  XBComboEntryList * table)
{
	return MenuAdd (MenuCreateCombo (x, y, w_text, text, w, NULL, data, NULL, table));
}								/* MenuAddComboData */

/*
 * add a combobox to menu, check only atom entry
 */
MENU_ID
MenuAddComboAtom (int x, int y, int w_text, const char *text, int w, XBAtom * atom,
				  XBComboEntryList * table)
{
	return MenuAdd (MenuCreateCombo (x, y, w_text, text, w, NULL, NULL, atom, table));
}								/* MenuAddComboAtom */

/*
 * add a combobox to menu, check all entries
 */
MENU_ID
MenuAddCombo (int x, int y, int w_text, const char *text, int w, int *value, void **data,
			  XBAtom * atom, XBComboEntryList * table)
{
	return MenuAdd (MenuCreateCombo (x, y, w_text, text, w, value, data, atom, table));
}								/* MenuAddCombo */

/*
 * add a player graphics to menu
 */
MENU_ID
MenuAddPlayer (int x, int y, int w, int sprite, const CFGPlayerGraphics ** cfg, int n_anime,
			   BMSpriteAnimation * anime)
{
	return MenuAdd (MenuCreatePlayer (x, y, w, sprite, cfg, n_anime, anime));
}								/* MenuAddPlayer */

/*
 * add a player graphics to menu
 */
MENU_ID
MenuAddConfigPlayer (int x, int y, int w, int sprite, const CFGPlayerGraphics ** cfg, int n_anime,
					 BMSpriteAnimation * anime, XBRgbValue * pRgb)
{
	return MenuAdd (MenuCreateConfigPlayer (x, y, w, sprite, cfg, n_anime, anime, pRgb));
}								/* MenuAddPlayer */

/*
 * add an item for string input to menu
 */
MENU_ID
MenuAddString (int x, int y, int w_text, const char *text, int w, char *buffer, size_t len)
{
	return MenuAdd (MenuCreateString (x, y, w_text, text, w, buffer, len));
}								/* MenuAddString */

/*
 * add an item for changing a color to menu
 */
MENU_ID
MenuAddColor (int x, int y, int w, const char *text, XBColor * color, XBRgbValue * pRgb)
{
	return MenuAdd (MenuCreateColor (x, y, w, text, color, pRgb));
}								/* MenuAddColor */

/*
 * add an item for assigning a key to menu
 */
MENU_ID
MenuAddKeysym (int x, int y, int w, const char *text, XBAtom * pKey)
{
	return MenuAdd (MenuCreateKeysym (x, y, w, text, pKey));
}								/* MenuAddKeysym */

/*
 * add an invisble item for a poll function
 */
MENU_ID
MenuAddCyclic (MIC_cyclic func, void *par)
{
	return MenuAdd (MenuCreateCyclic (func, par));
}								/* MenuAddCyclic */

/*
 * add an item for integer input to menu
 */
MENU_ID
MenuAddInteger (int x, int y, int w_text, const char *text, int w, int *pValue, int min, int max)
{
	return MenuAdd (MenuCreateInteger (x, y, w_text, text, w, pValue, min, max));
}								/* MenuAddInteger */

/*
 * add a name tag for players
 */
MENU_ID
MenuAddTag (int x, int y, int w, const char **pText)
{
	return MenuAdd (MenuCreateTag (x, y, w, pText));
}								/* MenuAddTag */

/*
 * add integer tag (counter)
 */
MENU_ID
MenuAddIntTag (int x, int y, int w, int *pNr)
{
	return MenuAdd (MenuCreateIntTag (x, y, w, pNr));
}								/* MenuAddIntTag */

/*******************************************
 * host items for server/client wait menus *
 *******************************************/

/*
 * add a generic host button
 */
MENU_ID
MenuAddHost (int x, int y, int w, unsigned client, const char **pText, XBHSFocusFunc focusFunc,
			 XBHSChangeFunc chgFunc, XBHSUpdateFunc upFunc)
{
	return MenuAdd (MenuCreateHost (x, y, w, client, pText, focusFunc, chgFunc, upFunc));
}								/* MenuAddHost */

/*
 * host button (server)
 */
MENU_ID
MenuAddServer (int x, int y, int w, const char **pText)
{
	return MenuAdd (MenuCreateServer (x, y, w, pText));
}								/* MenuAddServer */

/*
 * host button (client)
 */
MENU_ID
MenuAddClient (int x, int y, int w, const char **pText, XBHostState * pState, const int *pPing)
{
	return MenuAdd (MenuCreateClient (x, y, w, pText, pState, pPing));
}								/* MenuAddClient */

/*
 * host button (peer)
 */
MENU_ID
MenuAddPeer (int x, int y, int w, const char **pText, XBHostState * pState, const int *pPing)
{
	return MenuAdd (MenuCreatePeer (x, y, w, pText, pState, pPing));
}								/* MenuAddPeer */

/*******************************************
 * team items for server/client wait menus *
 *******************************************/

/*
 * team button (generic)
 */
MENU_ID
MenuAddTeam (int x, int y, int w, unsigned id, unsigned player, XBTSFocusFunc focusFunc,
			 XBTSChangeFunc chgFunc, XBTSUpdateFunc upFunc)
{
	return MenuAdd (MenuCreateTeam (x, y, w, id, player, focusFunc, chgFunc, upFunc));
}								/* MenuAddTeam */

/*
 * team button (server)
 */
MENU_ID
MenuAddServerTeam (int x, int y, int w, XBTeamState * pTeam)
{
	return MenuAdd (MenuCreateServerTeam (x, y, w, pTeam));
}								/* MenuAddServerTeam */

/*
 * team button (peeer)
 */
MENU_ID
MenuAddPeerTeam (int x, int y, int w, XBTeamState * pTeam)
{
	return MenuAdd (MenuCreatePeerTeam (x, y, w, pTeam));
}								/* MenuAddPeerTeam */

/*
 * add statitics header to menu
 */
MENU_ID
MenuAddStatHeader (int x, int y, int w, const char *title)
{
	return MenuAdd (MenuCreateStatHeader (x, y, w, title));
}								/* MenuAddHButton */

/*
 * add table entry to menu
 */
MENU_ID
MenuAddStatEntry (int x, int y, int w, const XBStatData * stat, MIC_button func, void *funcData)
{
	return MenuAdd (MenuCreateStatEntry (x, y, w, stat, func, funcData));
}								/* MenuAddHButton */

/*
 * add table entry to menu
 */
MENU_ID
MenuAddDemoEntry (int x, int y, int w, const CFGDemoEntry * demo, MIC_button func, void *funcData)
{
	return MenuAdd (MenuCreateDemoEntry (x, y, w, demo, func, funcData));
}								/* MenuAddHButton */

/*
 * add statitics header to menu
 */
MENU_ID
MenuAddDemoHeader (int x, int y, int w)
{
	return MenuAdd (MenuCreateDemoHeader (x, y, w));
}								/* MenuAddHButton */

/*
 * add statitics header to menu
 */
MENU_ID
MenuAddGameEntry (int x, int y, int w, const XBNetworkGame ** game, MIC_button func)
{
	return MenuAdd (MenuCreateGameEntry (x, y, w, game, func));
}								/* MenuAddHButton */

/*
 * add statitics header to menu
 */
MENU_ID
MenuAddGameHeader (int x, int y, int w)
{
	return MenuAdd (MenuCreateGameHeader (x, y, w));
}								/* MenuAddHButton */

/*
 * XBCC add statitics header to menu
 */
MENU_ID
MenuAddCentralHeader (int x, int y, int w, const char *title)
{
	return MenuAdd (MenuCreateCentralHeader (x, y, w, title));
}								/* MenuAddHButton */

/*
 * XBCC add table entry to menu
 */
MENU_ID
MenuAddCentralEntry (int x, int y, int w, const XBCentralData * stat, MIC_button func,
					 void *funcData)
{
	return MenuAdd (MenuCreateCentralEntry (x, y, w, stat, func, funcData));
}								/* MenuAddHButton */

/*
 * XBCC add info header to menu
 */
MENU_ID
MenuAddInfoHeader (int x, int y, int w, const char *title)
{
	return MenuAdd (MenuCreateInfoHeader (x, y, w, title));
}								/* MenuAddHButton */

/*
 * XBCC add table entry to menu
 */
MENU_ID
MenuAddInfoEntry (int x, int y, int w, const XBCentralInfo * stat, MIC_button func, void *funcData)
{
	return MenuAdd (MenuCreateInfoEntry (x, y, w, stat, func, funcData));
}								/* MenuAddHButton */

/*------------------------------------------------------------------------
 *
 * Event handling
 *
 *------------------------------------------------------------------------*/

/*
 * redraw routine for menu (used after timer event)
 */
void
MenuUpdateWindow (void)
{
	XBMenuItem *item;
	XBEventData eData;

	/* call poll routines for all objects */
	for (item = itemFirst; item != NULL; item = item->next) {
		if (NULL != item->poll) {
			(*item->poll) (item);
		}
	}
	/* shuffle sprites and mark them */
	ShuffleAllSprites ();
	/* set rectangles to be redrawn */
	SetRedrawRectangles ();
	/* shuffle sprites and mark them */
	MarkAllSprites ();
	/* update maze pixmap */
	UpdateMaze ();
	/* draw sprites into pixmap */
	DrawAllSprites ();
	/* fade in if neccessary */
	if (fadeFlag) {
		fadeFlag = XBFalse;
		/* inits */
		GUI_InitFade (XBFM_IN, PIXH + SCOREH);
		/* do it */
		while (GUI_DoFade ()) {
			while (XBE_TIMER != GUI_WaitEvent (&eData))
				continue;
		}
	}
	/* update window from pixmap */
	GUI_FlushPixmap (XBTrue);
	/* clear the redraw map */
	ClearRedrawMap ();
}								/* MenuUpdateWindow */

/*
 * move focus to next item in list
 */
static void
MoveFocus (XBMenuKey dir)
{
	if (itemFocus != NULL) {
		XBMenuItem *newFocus;
		switch (dir) {
			/* arrow keys */
		case XBMK_LEFT:
			newFocus = itemFocus->left;
			while (newFocus && newFocus->flags & MIF_DEACTIVATED) {
				newFocus = newFocus->left;
			}
			break;
		case XBMK_RIGHT:
			newFocus = itemFocus->right;
			while (newFocus && newFocus->flags & MIF_DEACTIVATED) {
				newFocus = newFocus->right;
			}
			break;
		case XBMK_UP:
			newFocus = itemFocus->up;
			while (newFocus && newFocus->flags & MIF_DEACTIVATED) {
				newFocus = newFocus->up;
			}
			break;
		case XBMK_DOWN:
			newFocus = itemFocus->down;
			while (newFocus && newFocus->flags & MIF_DEACTIVATED) {
				newFocus = newFocus->down;
			}
			break;
			/* next in list */
		case XBMK_NEXT:
			newFocus = itemFocus->next;
			while (newFocus != NULL) {
				if (newFocus->focus != NULL && !(newFocus->flags & MIF_DEACTIVATED)) {
					break;
				}
				newFocus = newFocus->next;
			}
			break;
			/* previous in list */
		case XBMK_PREV:
			newFocus = itemFocus->prev;
			while (newFocus != NULL) {
				if (newFocus->focus != NULL && !(newFocus->flags & MIF_DEACTIVATED)) {
					break;
				}
				newFocus = newFocus->prev;
			}
			break;
		default:
			return;
		}
		if (NULL != newFocus) {
			itemFocus->flags &= ~MIF_FOCUS;
			newFocus->flags |= MIF_FOCUS;
			(*itemFocus->focus) (itemFocus, XBFalse);
			(*newFocus->focus) (newFocus, XBTrue);
			itemFocus = newFocus;
		}
	}
}								/* MoveFocus */

/*
 * set mouse to position
 */
static void
SetMousePosition (int x, int y)
{
	XBMenuItem *newItem = MenuGetMouseItem (x, y);
	/* set new focus if needed */
	if (NULL != newItem && !(newItem->flags & MIF_DEACTIVATED) && newItem != itemFocus) {
		/* take back old focus */
		if (NULL != itemFocus) {
			itemFocus->flags &= ~MIF_FOCUS;
			assert (NULL != itemFocus->focus);
			(*itemFocus->focus) (itemFocus, XBFalse);
		}
		/* set new focus */
		itemFocus = newItem;
		itemFocus->flags |= MIF_FOCUS;
		assert (NULL != itemFocus->focus);
		(*itemFocus->focus) (itemFocus, XBTrue);
	}
}								/* SetMousePosition */

/*
 * determine item under mouse
 */
static void
MouseItem (XBEventCode button, int x, int y)
{
	if (itemFocus != NULL && itemFocus == MenuGetMouseItem (x, y) && itemFocus->mouse != NULL) {
		(*itemFocus->mouse) (itemFocus, button);
	}
}								/* MouseItem */

/*
 * edit events
 */
static XBBool
EditEvent (XBEventCode event, XBEventData data)
{
	if (!mapMode) {
		return XBFalse;
	}
	switch (event) {
	case XBE_RMOUSE_2:
		SetOldBlock ();
	case XBE_RMOUSE_1:
	case XBE_RMOUSE_3:
		setPressed = 0;
		break;
	case XBE_MOUSE_2:
		SetToBlockFree ();
	case XBE_MOUSE_1:
	case XBE_MOUSE_3:
		setPressed = 1;
		fprintf (stderr, "mouse 1/3 pressed on  %i %i \n", data.pos.x, data.pos.y);
		SetEditMapBlock (data.pos.x, data.pos.y);
		return XBFalse;			/* pass mouse buttons on to default */
		/* mouse motion event */
	case XBE_MOUSE_MOVE:
		if (setPressed) {
			SetEditMapBlock (data.pos.x, data.pos.y);
		}
		UpdateMaze ();
		return XBFalse;			/* pass mouse move to default */
	default:
		return XBFalse;
	}
	return XBTrue;
}								/* EditEvent */

/*
 * default events
 */
static XBBool
DefaultEvent (XBEventCode event, XBEventData data)
{
	switch (event) {
		/* menu keys */
	case XBE_MENU:
		switch (data.value) {
		case XBMK_PREV:
		case XBMK_NEXT:
		case XBMK_LEFT:
		case XBMK_RIGHT:
		case XBMK_UP:
		case XBMK_DOWN:
			MoveFocus (data.value);
			break;
		case XBMK_SELECT:
			SelectItem (itemFocus);
			break;
		case XBMK_ABORT:
			SelectItem (abortItem);
			break;
		case XBMK_DEFAULT:
			SelectItem (defaultItem);
			break;
		default:
			break;
		}
		break;
		/* mouse events */
	case XBE_MOUSE_1:
	case XBE_MOUSE_2:
	case XBE_MOUSE_3:
		SetMousePosition (data.pos.x, data.pos.y);
		MouseItem (event, data.pos.x, data.pos.y);
		break;
		/* mouse motion event */
	case XBE_MOUSE_MOVE:
		SetMousePosition (data.pos.x, data.pos.y);
		break;
	default:
		return XBFalse;
	}
	return XBTrue;
}								/* DefaultEvent */

/*
 * event handling for menus
 */
XBBool
MenuEventLoop (void)
{
	int result;
	XBEventCode event;
	XBEventData data;

	/* load background graphics */
	MenuLoadTiles ();
	/* wait for kb event */
	GUI_SetTimer (FRAME_TIME, XBTrue);
	GUI_SetKeyboardMode (KB_MENU);
	GUI_SetMouseMode (XBTrue);
	/* event loop */
	while (1) {
		/* update window contents */
		MenuUpdateWindow ();
		/* exec any delayed functions */
		if (execFlag) {
			execFlag = XBFalse;
			result = MenuExecButton ();
			/* no menu left start (or quit) game */
			if (NULL == itemFirst) {
				/* unload background graphics */
				MenuUnloadTiles ();
				return result;
			}
		}
		/* get event from gui */
		while (XBE_TIMER != (event = GUI_WaitEvent (&data))) {
			if (!Chat_Event (event, data) &&
				!EditEvent (event, data) && !DefaultEvent (event, data)) {
				continue;
			}
			result = MenuExecButton ();
			/* no menu left start (or quit) game */
			if (NULL == itemFirst) {
				/* unload background graphics */
				MenuUnloadTiles ();
				return result;
			}
		}
	}
	/* unload background graphics */
	MenuUnloadTiles ();
	return XBTrue;
}								/* MenuEventLoop */

/*
 * set default item
 */
void
MenuDeleteItemById (MENU_ID id)
{
	XBMenuItem *itemTemp;
	XBMenuItem *item_next;
	itemTemp = FindItem (id);
	if (itemTemp == NULL) {
		return;
	}
	item_next = itemTemp->next;
	DeleteMenuItem (itemTemp);
	itemTemp = item_next;
}								/* MenuDeleteItemById */

/*
 * end of file mi_tool.c
 */