rnd-20031202-1-src
[rocksndiamonds.git] / src / screens.c
1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back!               *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * screens.c                                                *
12 ***********************************************************/
13
14 #include "libgame/libgame.h"
15
16 #include "screens.h"
17 #include "events.h"
18 #include "game.h"
19 #include "tools.h"
20 #include "editor.h"
21 #include "files.h"
22 #include "tape.h"
23 #include "cartoons.h"
24 #include "network.h"
25 #include "init.h"
26
27 /* screens in the setup menu */
28 #define SETUP_MODE_MAIN                 0
29 #define SETUP_MODE_GAME                 1
30 #define SETUP_MODE_EDITOR               2
31 #define SETUP_MODE_INPUT                3
32 #define SETUP_MODE_SHORTCUT             4
33 #define SETUP_MODE_GRAPHICS             5
34 #define SETUP_MODE_SOUND                6
35 #define SETUP_MODE_ARTWORK              7
36 #define SETUP_MODE_CHOOSE_GRAPHICS      8
37 #define SETUP_MODE_CHOOSE_SOUNDS        9
38 #define SETUP_MODE_CHOOSE_MUSIC         10
39
40 #define MAX_SETUP_MODES                 11
41
42 /* for input setup functions */
43 #define SETUPINPUT_SCREEN_POS_START     0
44 #define SETUPINPUT_SCREEN_POS_END       (SCR_FIELDY - 4)
45 #define SETUPINPUT_SCREEN_POS_EMPTY1    (SETUPINPUT_SCREEN_POS_START + 3)
46 #define SETUPINPUT_SCREEN_POS_EMPTY2    (SETUPINPUT_SCREEN_POS_END - 1)
47
48 /* screens on the info screen */
49 #define INFO_MODE_MAIN                  0
50 #define INFO_MODE_ELEMENTS              1
51 #define INFO_MODE_MUSIC                 2
52 #define INFO_MODE_CREDITS               3
53 #define INFO_MODE_PROGRAM               4
54 #define INFO_MODE_LEVELSET              5
55
56 #define MAX_INFO_MODES                  6
57
58 /* for various menu stuff  */
59 #define MAX_INFO_ELEMENTS_ON_SCREEN     10
60 #define MAX_MENU_ENTRIES_ON_SCREEN      (SCR_FIELDY - 2)
61 #define MENU_SCREEN_START_YPOS          2
62 #define MENU_SCREEN_VALUE_XPOS          14
63
64 /* buttons and scrollbars identifiers */
65 #define SCREEN_CTRL_ID_SCROLL_UP        0
66 #define SCREEN_CTRL_ID_SCROLL_DOWN      1
67 #define SCREEN_CTRL_ID_SCROLL_VERTICAL  2
68
69 #define NUM_SCREEN_SCROLLBUTTONS        2
70 #define NUM_SCREEN_SCROLLBARS           1
71 #define NUM_SCREEN_GADGETS              3
72
73 /* forward declarations of internal functions */
74 static void HandleScreenGadgets(struct GadgetInfo *);
75 static void HandleSetupScreen_Generic(int, int, int, int, int);
76 static void HandleSetupScreen_Input(int, int, int, int, int);
77 static void CustomizeKeyboard(int);
78 static void CalibrateJoystick(int);
79 static void execSetupArtwork(void);
80 static void HandleChooseTree(int, int, int, int, int, TreeInfo **);
81
82 static void DrawChooseLevel(void);
83 static void DrawInfoScreen(void);
84 static void DrawSetupScreen(void);
85
86 static void DrawInfoScreen_HelpAnim(int, int, boolean);
87 static void DrawInfoScreen_HelpText(int, int, int, int);
88 static void HandleInfoScreen_Main(int, int, int, int, int);
89 static void HandleInfoScreen_Elements(int);
90 static void HandleInfoScreen_Music(int);
91 static void HandleInfoScreen_Credits(int);
92 static void HandleInfoScreen_Program(int);
93
94 static void MapChooseTreeGadgets(TreeInfo *);
95
96 static struct GadgetInfo *screen_gadget[NUM_SCREEN_GADGETS];
97 static int setup_mode = SETUP_MODE_MAIN;
98 static int info_mode = INFO_MODE_MAIN;
99
100 #define mSX (SX + (game_status >= GAME_MODE_MAIN &&     \
101                    game_status <= GAME_MODE_SETUP ?     \
102                    menu.draw_xoffset[game_status] : menu.draw_xoffset_default))
103 #define mSY (SY + (game_status >= GAME_MODE_MAIN &&     \
104                    game_status <= GAME_MODE_SETUP ?     \
105                    menu.draw_yoffset[game_status] : menu.draw_yoffset_default))
106
107 #define NUM_MENU_ENTRIES_ON_SCREEN (menu.list_size[game_status] > 2 ?   \
108                                     menu.list_size[game_status] :       \
109                                     MAX_MENU_ENTRIES_ON_SCREEN)
110
111 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
112 #define NUM_SCROLLBAR_BITMAPS           2
113 static Bitmap *scrollbar_bitmap[NUM_SCROLLBAR_BITMAPS];
114 #endif
115
116
117 static void drawCursorExt(int xpos, int ypos, int color, int graphic)
118 {
119   static int cursor_array[SCR_FIELDY];
120
121   if (xpos == 0)
122   {
123     if (graphic != 0)
124       cursor_array[ypos] = graphic;
125     else
126       graphic = cursor_array[ypos];
127   }
128
129   if (color == FC_RED)
130     graphic = (graphic == IMG_MENU_BUTTON_LEFT  ? IMG_MENU_BUTTON_LEFT_ACTIVE :
131                graphic == IMG_MENU_BUTTON_RIGHT ? IMG_MENU_BUTTON_RIGHT_ACTIVE:
132                IMG_MENU_BUTTON_ACTIVE);
133
134   ypos += MENU_SCREEN_START_YPOS;
135
136   DrawBackground(mSX + xpos * TILEX, mSY + ypos * TILEY, TILEX, TILEY);
137   DrawGraphicThruMaskExt(drawto, mSX + xpos * TILEX, mSY + ypos * TILEY,
138                          graphic, 0);
139 }
140
141 static void initCursor(int ypos, int graphic)
142 {
143   drawCursorExt(0, ypos, FC_BLUE, graphic);
144 }
145
146 static void drawCursor(int ypos, int color)
147 {
148   drawCursorExt(0, ypos, color, 0);
149 }
150
151 static void drawCursorXY(int xpos, int ypos, int graphic)
152 {
153   drawCursorExt(xpos, ypos, -1, graphic);
154 }
155
156 static void drawChooseTreeCursor(int ypos, int color)
157 {
158   int last_game_status = game_status;   /* save current game status */
159
160   /* force LEVELS draw offset on artwork setup screen */
161   game_status = GAME_MODE_LEVELS;
162
163   drawCursorExt(0, ypos, color, 0);
164
165   game_status = last_game_status;       /* restore current game status */
166 }
167
168 static void PlayMenuSound()
169 {
170   int sound = menu.sound[game_status];
171
172   if (sound == SND_UNDEFINED)
173     return;
174
175   if (sound_info[sound].loop)
176     PlaySoundLoop(sound);
177   else
178     PlaySound(sound);
179 }
180
181 static void PlayMenuSoundIfLoop()
182 {
183   int sound = menu.sound[game_status];
184
185   if (sound == SND_UNDEFINED)
186     return;
187
188   if (sound_info[sound].loop)
189     PlaySoundLoop(sound);
190 }
191
192 static void PlayMenuMusic()
193 {
194   int music = menu.music[game_status];
195
196   if (music == MUS_UNDEFINED)
197     return;
198
199   PlayMusic(music);
200 }
201
202 void DrawHeadline()
203 {
204   int text1_width = getTextWidth(PROGRAM_TITLE_STRING,     FONT_TITLE_1);
205   int text2_width = getTextWidth(PROGRAM_COPYRIGHT_STRING, FONT_TITLE_2);
206   int x1 = SX + (SXSIZE - text1_width) / 2;
207   int x2 = SX + (SXSIZE - text2_width) / 2;
208
209   DrawText(x1, SY + 8,  PROGRAM_TITLE_STRING,     FONT_TITLE_1);
210   DrawText(x2, SY + 46, PROGRAM_COPYRIGHT_STRING, FONT_TITLE_2);
211 }
212
213 static void ToggleFullscreenIfNeeded()
214 {
215   if (setup.fullscreen != video.fullscreen_enabled)
216   {
217     /* save old door content */
218     BlitBitmap(backbuffer, bitmap_db_door,
219                DX, DY, DXSIZE, DYSIZE, DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
220
221     /* toggle fullscreen */
222     ChangeVideoModeIfNeeded(setup.fullscreen);
223     setup.fullscreen = video.fullscreen_enabled;
224
225     /* redraw background to newly created backbuffer */
226     BlitBitmap(graphic_info[IMG_GLOBAL_BORDER].bitmap, backbuffer,
227                0,0, WIN_XSIZE,WIN_YSIZE, 0,0);
228
229     /* restore old door content */
230     BlitBitmap(bitmap_db_door, backbuffer,
231                DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1, DXSIZE, DYSIZE, DX, DY);
232
233     redraw_mask = REDRAW_ALL;
234   }
235 }
236
237 void DrawMainMenu()
238 {
239   static LevelDirTree *leveldir_last_valid = NULL;
240   char *name_text = (!options.network && setup.team_mode ? "Team:" : "Name:");
241   int name_width  = getTextWidth("Name:",  FONT_MENU_1);
242   int level_width = getTextWidth("Level:", FONT_MENU_1);
243   int i;
244
245   UnmapAllGadgets();
246   FadeSoundsAndMusic();
247
248   KeyboardAutoRepeatOn();
249   ActivateJoystick();
250
251   SetDrawDeactivationMask(REDRAW_NONE);
252   SetDrawBackgroundMask(REDRAW_FIELD);
253
254   audio.sound_deactivated = FALSE;
255
256   /* needed if last screen was the playing screen, invoked from level editor */
257   if (level_editor_test_game)
258   {
259     game_status = GAME_MODE_EDITOR;
260     DrawLevelEd();
261
262     return;
263   }
264
265   /* needed if last screen was the editor screen */
266   UndrawSpecialEditorDoor();
267
268   /* needed if last screen was the setup screen and fullscreen state changed */
269   ToggleFullscreenIfNeeded();
270
271   /* leveldir_current may be invalid (level group, parent link) */
272   if (!validLevelSeries(leveldir_current))
273     leveldir_current = getFirstValidTreeInfoEntry(leveldir_last_valid);
274
275   /* store valid level series information */
276   leveldir_last_valid = leveldir_current;
277
278   /* needed if last screen (level choice) changed graphics, sounds or music */
279   ReloadCustomArtwork();
280
281 #ifdef TARGET_SDL
282   SetDrawtoField(DRAW_BACKBUFFER);
283 #endif
284
285   /* map gadgets for main menu screen */
286   MapTapeButtons();
287
288   /* level_nr may have been set to value over handicap with level editor */
289   if (setup.handicap && level_nr > leveldir_current->handicap_level)
290     level_nr = leveldir_current->handicap_level;
291
292   GetPlayerConfig();
293   LoadLevel(level_nr);
294
295   SetMainBackgroundImage(IMG_BACKGROUND_MAIN);
296   ClearWindow();
297
298   DrawHeadline();
299
300   DrawText(mSX + 32, mSY + 2*32, name_text, FONT_MENU_1);
301   DrawText(mSX + 32, mSY + 3*32, "Level:", FONT_MENU_1);
302   DrawText(mSX + 32, mSY + 4*32, "Hall Of Fame", FONT_MENU_1);
303   DrawText(mSX + 32, mSY + 5*32, "Level Creator", FONT_MENU_1);
304   DrawText(mSX + 32, mSY + 6*32, "Info Screen", FONT_MENU_1);
305   DrawText(mSX + 32, mSY + 7*32, "Start Game", FONT_MENU_1);
306   DrawText(mSX + 32, mSY + 8*32, "Setup", FONT_MENU_1);
307   DrawText(mSX + 32, mSY + 9*32, "Quit", FONT_MENU_1);
308
309   DrawText(mSX + 32 + name_width, mSY + 2*32, setup.player_name, FONT_INPUT_1);
310   DrawText(mSX + level_width + 5 * 32, mSY + 3*32, int2str(level_nr,3),
311            FONT_VALUE_1);
312
313   DrawMicroLevel(MICROLEV_XPOS, MICROLEV_YPOS, TRUE);
314
315   DrawTextF(mSX + 32 + level_width - 2, mSY + 3*32 + 1, FONT_TEXT_3, "%d-%d",
316             leveldir_current->first_level, leveldir_current->last_level);
317
318   if (leveldir_current->readonly)
319   {
320     DrawTextS(mSX + level_width + 9 * 32 - 2,
321               mSY + 3 * 32 + 1 - 7, FONT_TEXT_3, "READ");
322     DrawTextS(mSX + level_width + 9 * 32 - 2,
323               mSY + 3 * 32 + 1 + 7, FONT_TEXT_3, "ONLY");
324   }
325
326   for (i = 0; i < 8; i++)
327     initCursor(i, (i == 1 || i == 4 || i == 6 ? IMG_MENU_BUTTON_RIGHT :
328                    IMG_MENU_BUTTON));
329
330   drawCursorXY(level_width/32 + 4, 1, IMG_MENU_BUTTON_LEFT);
331   drawCursorXY(level_width/32 + 8, 1, IMG_MENU_BUTTON_RIGHT);
332
333   DrawText(SX + 56, SY + 326, "A Game by Artsoft Entertainment", FONT_TITLE_2);
334
335   FadeToFront();
336   InitAnimation();
337
338   HandleMainMenu(0, 0, 0, 0, MB_MENU_INITIALIZE);
339
340   TapeStop();
341   if (TAPE_IS_EMPTY(tape))
342     LoadTape(level_nr);
343   DrawCompleteVideoDisplay();
344
345   PlayMenuSound();
346   PlayMenuMusic();
347
348   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
349 }
350
351 static void gotoTopLevelDir()
352 {
353   /* move upwards to top level directory */
354   while (leveldir_current->node_parent)
355   {
356     /* write a "path" into level tree for easy navigation to last level */
357     if (leveldir_current->node_parent->node_group->cl_first == -1)
358     {
359       int num_leveldirs = numTreeInfoInGroup(leveldir_current);
360       int leveldir_pos = posTreeInfo(leveldir_current);
361       int num_page_entries;
362       int cl_first, cl_cursor;
363
364       if (num_leveldirs <= NUM_MENU_ENTRIES_ON_SCREEN)
365         num_page_entries = num_leveldirs;
366       else
367         num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
368
369       cl_first = MAX(0, leveldir_pos - num_page_entries + 1);
370       cl_cursor = leveldir_pos - cl_first;
371
372       leveldir_current->node_parent->node_group->cl_first = cl_first;
373       leveldir_current->node_parent->node_group->cl_cursor = cl_cursor;
374     }
375
376     leveldir_current = leveldir_current->node_parent;
377   }
378 }
379
380 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
381 {
382   static int choice = 5;
383   int x = 0;
384   int y = choice;
385
386   if (button == MB_MENU_INITIALIZE)
387   {
388     drawCursor(choice, FC_RED);
389     return;
390   }
391
392   if (mx || my)         /* mouse input */
393   {
394     x = (mx - mSX) / 32;
395     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
396   }
397   else if (dx || dy)    /* keyboard input */
398   {
399     if (dx && choice == 1)
400       x = (dx < 0 ? 10 : 14);
401     else if (dy)
402       y = choice + dy;
403   }
404
405   if (y == 1 && ((x == 10 && level_nr > leveldir_current->first_level) ||
406                  (x == 14 && level_nr < leveldir_current->last_level)) &&
407       button)
408   {
409     static unsigned long level_delay = 0;
410     int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
411     int old_level_nr = level_nr;
412     int new_level_nr;
413
414     new_level_nr = level_nr + (x == 10 ? -step : +step);
415     if (new_level_nr < leveldir_current->first_level)
416       new_level_nr = leveldir_current->first_level;
417     if (new_level_nr > leveldir_current->last_level)
418       new_level_nr = leveldir_current->last_level;
419
420     if (setup.handicap && new_level_nr > leveldir_current->handicap_level)
421       new_level_nr = leveldir_current->handicap_level;
422
423     if (new_level_nr != old_level_nr &&
424         DelayReached(&level_delay, GADGET_FRAME_DELAY))
425     {
426       level_nr = new_level_nr;
427
428       DrawText(mSX + 11 * 32, mSY + 3 * 32, int2str(level_nr, 3),
429                FONT_VALUE_1);
430
431       LoadLevel(level_nr);
432       DrawMicroLevel(MICROLEV_XPOS, MICROLEV_YPOS, TRUE);
433
434       TapeErase();
435       LoadTape(level_nr);
436       DrawCompleteVideoDisplay();
437
438       /* needed because DrawMicroLevel() takes some time */
439       BackToFront();
440       SyncDisplay();
441       DelayReached(&level_delay, 0);    /* reset delay counter */
442     }
443   }
444   else if (IN_VIS_FIELD(x, y) &&
445            y >= 0 && y <= 7 && (y != 1 || x < 10))
446   {
447     if (button)
448     {
449       if (y != choice)
450       {
451         drawCursor(y, FC_RED);
452         drawCursor(choice, FC_BLUE);
453         choice = y;
454       }
455     }
456     else
457     {
458       if (y == 0)
459       {
460         game_status = GAME_MODE_PSEUDO_TYPENAME;
461         HandleTypeName(strlen(setup.player_name), 0);
462       }
463       else if (y == 1)
464       {
465         if (leveldir_first)
466         {
467           game_status = GAME_MODE_LEVELS;
468           SaveLevelSetup_LastSeries();
469           SaveLevelSetup_SeriesInfo();
470
471           gotoTopLevelDir();
472
473           DrawChooseLevel();
474         }
475       }
476       else if (y == 2)
477       {
478         game_status = GAME_MODE_SCORES;
479         DrawHallOfFame(-1);
480       }
481       else if (y == 3)
482       {
483         if (leveldir_current->readonly &&
484             strcmp(setup.player_name, "Artsoft") != 0)
485           Request("This level is read only !", REQ_CONFIRM);
486         game_status = GAME_MODE_EDITOR;
487         DrawLevelEd();
488       }
489       else if (y == 4)
490       {
491         game_status = GAME_MODE_INFO;
492         info_mode = INFO_MODE_MAIN;
493         DrawInfoScreen();
494       }
495       else if (y == 5)
496       {
497         if (setup.autorecord)
498           TapeStartRecording();
499
500 #if defined(PLATFORM_UNIX)
501         if (options.network)
502           SendToServer_StartPlaying();
503         else
504 #endif
505         {
506           game_status = GAME_MODE_PLAYING;
507           StopAnimation();
508           InitGame();
509         }
510       }
511       else if (y == 6)
512       {
513         game_status = GAME_MODE_SETUP;
514         setup_mode = SETUP_MODE_MAIN;
515         DrawSetupScreen();
516       }
517       else if (y == 7)
518       {
519         SaveLevelSetup_LastSeries();
520         SaveLevelSetup_SeriesInfo();
521         if (Request("Do you really want to quit ?", REQ_ASK | REQ_STAY_CLOSED))
522           game_status = GAME_MODE_QUIT;
523       }
524     }
525   }
526
527   if (game_status == GAME_MODE_MAIN)
528   {
529     DrawMicroLevel(MICROLEV_XPOS, MICROLEV_YPOS, FALSE);
530     DoAnimation();
531   }
532 }
533
534
535 /* ========================================================================= */
536 /* info screen functions                                                     */
537 /* ========================================================================= */
538
539 static struct TokenInfo *info_info;
540 static int num_info_info;
541
542 static void execInfoElements()
543 {
544   info_mode = INFO_MODE_ELEMENTS;
545   DrawInfoScreen();
546 }
547
548 static void execInfoMusic()
549 {
550   info_mode = INFO_MODE_MUSIC;
551   DrawInfoScreen();
552 }
553
554 static void execInfoCredits()
555 {
556   info_mode = INFO_MODE_CREDITS;
557   DrawInfoScreen();
558 }
559
560 static void execInfoProgram()
561 {
562   info_mode = INFO_MODE_PROGRAM;
563   DrawInfoScreen();
564 }
565
566 static void execInfoLevelSet()
567 {
568   info_mode = INFO_MODE_LEVELSET;
569   DrawInfoScreen();
570 }
571
572 static void execExitInfo()
573 {
574   game_status = GAME_MODE_MAIN;
575   DrawMainMenu();
576 }
577
578 static struct TokenInfo info_info_main[] =
579 {
580   { TYPE_ENTER_SCREEN,  execInfoElements,       "Elements Info"         },
581   { TYPE_ENTER_SCREEN,  execInfoMusic,          "Music Info"            },
582   { TYPE_ENTER_SCREEN,  execInfoCredits,        "Credits"               },
583   { TYPE_ENTER_SCREEN,  execInfoProgram,        "Program Info"          },
584   { TYPE_ENTER_SCREEN,  execInfoLevelSet,       "Level Set Info"        },
585   { TYPE_EMPTY,         NULL,                   ""                      },
586   { TYPE_LEAVE_MENU,    execExitInfo,           "Exit"                  },
587
588   { 0,                  NULL,                   NULL                    }
589 };
590
591 static void DrawInfoScreen_Main()
592 {
593   int i;
594
595   UnmapAllGadgets();
596   CloseDoor(DOOR_CLOSE_2);
597
598   ClearWindow();
599
600   DrawText(mSX + 16, mSY + 16, "Info Screen", FONT_TITLE_1);
601
602   info_info = info_info_main;
603   num_info_info = 0;
604
605   for (i = 0; info_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
606   {
607     int ypos = MENU_SCREEN_START_YPOS + i;
608     int font_nr = FONT_MENU_1;
609
610     DrawText(mSX + 32, mSY + ypos * 32, info_info[i].text, font_nr);
611
612     if (info_info[i].type & TYPE_ENTER_MENU)
613       initCursor(i, IMG_MENU_BUTTON_RIGHT);
614     else if (info_info[i].type & TYPE_LEAVE_MENU)
615       initCursor(i, IMG_MENU_BUTTON_LEFT);
616     else if (info_info[i].type & ~TYPE_SKIP_ENTRY)
617       initCursor(i, IMG_MENU_BUTTON);
618
619     num_info_info++;
620   }
621
622   FadeToFront();
623   InitAnimation();
624
625   PlayMenuSound();
626   PlayMenuMusic();
627
628   HandleInfoScreen_Main(0, 0, 0, 0, MB_MENU_INITIALIZE);
629 }
630
631 void HandleInfoScreen_Main(int mx, int my, int dx, int dy, int button)
632 {
633   static int choice_store[MAX_INFO_MODES];
634   int choice = choice_store[info_mode];         /* always starts with 0 */
635   int x = 0;
636   int y = choice;
637
638   if (button == MB_MENU_INITIALIZE)
639   {
640     /* advance to first valid menu entry */
641     while (choice < num_info_info &&
642            info_info[choice].type & TYPE_SKIP_ENTRY)
643       choice++;
644     choice_store[info_mode] = choice;
645
646     drawCursor(choice, FC_RED);
647     return;
648   }
649   else if (button == MB_MENU_LEAVE)
650   {
651     for (y = 0; y < num_info_info; y++)
652     {
653       if (info_info[y].type & TYPE_LEAVE_MENU)
654       {
655         void (*menu_callback_function)(void) = info_info[y].value;
656
657         menu_callback_function();
658         break;  /* absolutely needed because function changes 'info_info'! */
659       }
660     }
661
662     return;
663   }
664
665   if (mx || my)         /* mouse input */
666   {
667     x = (mx - mSX) / 32;
668     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
669   }
670   else if (dx || dy)    /* keyboard input */
671   {
672     if (dx)
673     {
674       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE_MENU : TYPE_ENTER_MENU);
675
676       if (info_info[choice].type & menu_navigation_type ||
677           info_info[choice].type & TYPE_ENTER_SCREEN ||
678           info_info[choice].type & TYPE_BOOLEAN_STYLE)
679         button = MB_MENU_CHOICE;
680     }
681     else if (dy)
682       y = choice + dy;
683
684     /* jump to next non-empty menu entry (up or down) */
685     while (y > 0 && y < num_info_info - 1 &&
686            info_info[y].type & TYPE_SKIP_ENTRY)
687       y += dy;
688   }
689
690   if (IN_VIS_FIELD(x, y) &&
691       y >= 0 && y < num_info_info && info_info[y].type & ~TYPE_SKIP_ENTRY)
692   {
693     if (button)
694     {
695       if (y != choice)
696       {
697         drawCursor(y, FC_RED);
698         drawCursor(choice, FC_BLUE);
699         choice = choice_store[info_mode] = y;
700       }
701     }
702     else if (!(info_info[y].type & TYPE_GHOSTED))
703     {
704       if (info_info[y].type & TYPE_ENTER_OR_LEAVE_MENU)
705       {
706         void (*menu_callback_function)(void) = info_info[choice].value;
707
708         menu_callback_function();
709       }
710     }
711   }
712 }
713
714 void DrawInfoScreen_HelpAnim(int start, int max_anims, boolean init)
715 {
716   static int infoscreen_step[MAX_INFO_ELEMENTS_ON_SCREEN];
717   static int infoscreen_frame[MAX_INFO_ELEMENTS_ON_SCREEN];
718   int xstart = mSX + 16;
719   int ystart = mSY + 64 + 2 * 32;
720   int ystep = TILEY + 4;
721   int element, action, direction;
722   int graphic;
723   int delay;
724   int sync_frame;
725   int i, j;
726
727   if (init)
728   {
729     for (i = 0; i < MAX_INFO_ELEMENTS_ON_SCREEN; i++)
730       infoscreen_step[i] = infoscreen_frame[i] = 0;
731
732     SetMainBackgroundImage(IMG_BACKGROUND_INFO);
733     ClearWindow();
734     DrawHeadline();
735
736     DrawTextSCentered(100, FONT_TEXT_1, "The Game Elements:");
737
738     DrawTextSCentered(SYSIZE - 20, FONT_TEXT_4,
739                       "Press any key or button for next page");
740
741     FrameCounter = 0;
742   }
743
744   i = j = 0;
745   while (helpanim_info[j].element != HELPANIM_LIST_END)
746   {
747     if (i >= start + MAX_INFO_ELEMENTS_ON_SCREEN ||
748         i >= max_anims)
749       break;
750     else if (i < start)
751     {
752       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
753         j++;
754
755       j++;
756       i++;
757
758       continue;
759     }
760
761     j += infoscreen_step[i - start];
762
763     element = helpanim_info[j].element;
764     action = helpanim_info[j].action;
765     direction = helpanim_info[j].direction;
766
767     if (action != -1 && direction != -1)
768       graphic = el_act_dir2img(element, action, direction);
769     else if (action != -1)
770       graphic = el_act2img(element, action);
771     else if (direction != -1)
772       graphic = el_act2img(element, direction);
773     else
774       graphic = el2img(element);
775
776     delay = helpanim_info[j++].delay;
777
778     if (delay == -1)
779       delay = 1000000;
780
781     if (infoscreen_frame[i - start] == 0)
782     {
783       sync_frame = 0;
784       infoscreen_frame[i - start] = delay - 1;
785     }
786     else
787     {
788       sync_frame = delay - infoscreen_frame[i - start];
789       infoscreen_frame[i - start]--;
790     }
791
792     if (helpanim_info[j].element == -1)
793     {
794       if (!infoscreen_frame[i - start])
795         infoscreen_step[i - start] = 0;
796     }
797     else
798     {
799       if (!infoscreen_frame[i - start])
800         infoscreen_step[i - start]++;
801       while (helpanim_info[j].element != -1)
802         j++;
803     }
804
805     j++;
806
807     ClearRectangleOnBackground(drawto, xstart, ystart + (i - start) * ystep,
808                                TILEX, TILEY);
809     DrawGraphicAnimationExt(drawto, xstart, ystart + (i - start) * ystep,
810                             graphic, sync_frame, USE_MASKING);
811
812     if (init)
813       DrawInfoScreen_HelpText(element, action, direction, i - start);
814
815     i++;
816   }
817
818   redraw_mask |= REDRAW_FIELD;
819
820   FrameCounter++;
821 }
822
823 static char *getHelpText(int element, int action, int direction)
824 {
825   char token[MAX_LINE_LEN];
826
827   strcpy(token, element_info[element].token_name);
828
829   if (action != -1)
830     strcat(token, element_action_info[action].suffix);
831
832   if (direction != -1)
833     strcat(token, element_direction_info[MV_DIR_BIT(direction)].suffix);
834
835   return getHashEntry(helptext_info, token);
836 }
837
838 void DrawInfoScreen_HelpText(int element, int action, int direction, int ypos)
839 {
840 #if 0
841   int font_nr = FONT_TEXT_2;
842 #else
843   int font_nr = FONT_LEVEL_NUMBER;
844 #endif
845   int font_width = getFontWidth(font_nr);
846   int sx = mSX + MINI_TILEX + TILEX + MINI_TILEX;
847   int sy = mSY + 65 + 2 * 32 + 1;
848   int ystep = TILEY + 4;
849   int pad_x = sx - SX;
850   int max_chars_per_line = (SXSIZE - pad_x - MINI_TILEX) / font_width;
851   int max_lines_per_text = 2;    
852   char *text = NULL;
853
854   if (action != -1 && direction != -1)          /* element.action.direction */
855     text = getHelpText(element, action, direction);
856
857   if (text == NULL && action != -1)             /* element.action */
858     text = getHelpText(element, action, -1);
859
860   if (text == NULL && direction != -1)          /* element.direction */
861     text = getHelpText(element, -1, direction);
862
863   if (text == NULL)                             /* base element */
864     text = getHelpText(element, -1, -1);
865
866   if (text == NULL)                             /* not found */
867     text = "No description available";
868
869   if (strlen(text) <= max_chars_per_line)       /* only one line of text */
870     sy += getFontHeight(font_nr) / 2;
871
872   DrawTextWrapped(sx, sy + ypos * ystep, text, font_nr,
873                   max_chars_per_line, max_lines_per_text);
874 }
875
876 void DrawInfoScreen_Elements()
877 {
878   LoadHelpAnimInfo();
879   LoadHelpTextInfo();
880
881   HandleInfoScreen_Elements(MB_MENU_INITIALIZE);
882
883   FadeToFront();
884   InitAnimation();
885 }
886
887 void HandleInfoScreen_Elements(int button)
888 {
889   static unsigned long info_delay = 0;
890   static int num_anims;
891   static int num_pages;
892   static int page;
893   int anims_per_page = MAX_INFO_ELEMENTS_ON_SCREEN;
894   int button_released = !button;
895   int i;
896
897   if (button == MB_MENU_INITIALIZE)
898   {
899     boolean new_element = TRUE;
900
901     num_anims = 0;
902     for (i = 0; helpanim_info[i].element != HELPANIM_LIST_END; i++)
903     {
904       if (helpanim_info[i].element == HELPANIM_LIST_NEXT)
905         new_element = TRUE;
906       else if (new_element)
907       {
908         num_anims++;
909         new_element = FALSE;
910       }
911     }
912
913     num_pages = (num_anims + anims_per_page - 1) / anims_per_page;
914     page = 0;
915   }
916   else if (button == MB_MENU_LEAVE)
917   {
918     info_mode = INFO_MODE_MAIN;
919     DrawInfoScreen();
920
921     return;
922   }
923
924   if (button_released || button == MB_MENU_INITIALIZE)
925   {
926     if (button != MB_MENU_INITIALIZE)
927       page++;
928
929     if (page >= num_pages)
930     {
931       FadeSoundsAndMusic();
932
933       info_mode = INFO_MODE_MAIN;
934       DrawInfoScreen();
935
936       return;
937     }
938
939     DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, TRUE);
940   }
941   else
942   {
943     if (DelayReached(&info_delay, GAME_FRAME_DELAY))
944       if (page < num_pages)
945         DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, FALSE);
946
947     PlayMenuSoundIfLoop();
948   }
949 }
950
951 void DrawInfoScreen_Music()
952 {
953   ClearWindow();
954   DrawHeadline();
955
956   LoadMusicInfo();
957
958   HandleInfoScreen_Music(MB_MENU_INITIALIZE);
959 }
960
961 void HandleInfoScreen_Music(int button)
962 {
963   static struct MusicFileInfo *list = NULL;
964   int ystart = 150, ystep = 30;
965   int ybottom = SYSIZE - 20;
966   int button_released = !button;
967
968   if (button == MB_MENU_INITIALIZE)
969   {
970     list = music_file_info;
971
972     if (list == NULL)
973     {
974       FadeSoundsAndMusic();
975
976       ClearWindow();
977       DrawHeadline();
978
979       DrawTextSCentered(100, FONT_TEXT_1, "No music info for this level set.");
980
981       DrawTextSCentered(ybottom, FONT_TEXT_4,
982                         "Press any key or button for info menu");
983
984       return;
985     }
986   }
987   else if (button == MB_MENU_LEAVE)
988   {
989     info_mode = INFO_MODE_MAIN;
990     DrawInfoScreen();
991
992     return;
993   }
994
995   if (button_released || button == MB_MENU_INITIALIZE)
996   {
997     if (list == NULL)
998     {
999       info_mode = INFO_MODE_MAIN;
1000       DrawInfoScreen();
1001
1002       return;
1003     }
1004
1005     FadeSoundsAndMusic();
1006
1007     ClearWindow();
1008     DrawHeadline();
1009
1010     DrawTextSCentered(100, FONT_TEXT_1, "The Game Background Music:");
1011
1012     DrawTextSCentered(ystart + 0 * ystep, FONT_TEXT_2, "Excerpt from");
1013     DrawTextFCentered(ystart + 1 * ystep, FONT_TEXT_3, "\"%s\"", list->title);
1014     DrawTextSCentered(ystart + 2 * ystep, FONT_TEXT_2, "by");
1015     DrawTextFCentered(ystart + 3 * ystep, FONT_TEXT_3, "%s", list->artist);
1016     DrawTextSCentered(ystart + 4 * ystep, FONT_TEXT_2, "from the album");
1017     DrawTextFCentered(ystart + 5 * ystep, FONT_TEXT_3, "\"%s\"", list->album);
1018
1019     DrawTextSCentered(ybottom, FONT_TEXT_4,
1020                       "Press any key or button for next page");
1021
1022     PlayMusic(list->music);
1023
1024     list = list->next;
1025   }
1026 }
1027
1028 void DrawInfoScreen_Credits()
1029 {
1030   int ystart = 150, ystep = 30;
1031   int ybottom = SYSIZE - 20;
1032
1033   FadeSoundsAndMusic();
1034
1035   ClearWindow();
1036   DrawHeadline();
1037
1038   DrawTextSCentered(100, FONT_TEXT_1, "Credits:");
1039   DrawTextSCentered(ystart + 0 * ystep, FONT_TEXT_2, "DOS port of the game:");
1040   DrawTextSCentered(ystart + 1 * ystep, FONT_TEXT_3, "Guido Schulz");
1041   DrawTextSCentered(ystart + 2 * ystep, FONT_TEXT_2, "Additional toons:");
1042   DrawTextSCentered(ystart + 3 * ystep, FONT_TEXT_3, "Karl Hörnell");
1043   DrawTextSCentered(ystart + 5 * ystep, FONT_TEXT_2,
1044                     "...and many thanks to all contributors");
1045   DrawTextSCentered(ystart + 6 * ystep, FONT_TEXT_2, "of new levels!");
1046
1047   DrawTextSCentered(ybottom, FONT_TEXT_4,
1048                     "Press any key or button for info menu");
1049 }
1050
1051 void HandleInfoScreen_Credits(int button)
1052 {
1053   int button_released = !button;
1054
1055   if (button == MB_MENU_LEAVE)
1056   {
1057     info_mode = INFO_MODE_MAIN;
1058     DrawInfoScreen();
1059
1060     return;
1061   }
1062
1063   if (button_released)
1064   {
1065     FadeSoundsAndMusic();
1066
1067     info_mode = INFO_MODE_MAIN;
1068     DrawInfoScreen();
1069   }
1070   else
1071   {
1072     PlayMenuSoundIfLoop();
1073   }
1074 }
1075
1076 void DrawInfoScreen_Program()
1077 {
1078   int ystart = 150, ystep = 30;
1079   int ybottom = SYSIZE - 20;
1080
1081   ClearWindow();
1082   DrawHeadline();
1083
1084   DrawTextSCentered(100, FONT_TEXT_1, "Program Information:");
1085
1086   DrawTextSCentered(ystart + 0 * ystep, FONT_TEXT_2,
1087                     "This game is Freeware!");
1088   DrawTextSCentered(ystart + 1 * ystep, FONT_TEXT_2,
1089                     "If you like it, send e-mail to:");
1090   DrawTextSCentered(ystart + 2 * ystep, FONT_TEXT_3,
1091                     "info@artsoft.org");
1092   DrawTextSCentered(ystart + 3 * ystep, FONT_TEXT_2,
1093                     "or SnailMail to:");
1094   DrawTextSCentered(ystart + 4 * ystep + 0, FONT_TEXT_3,
1095                     "Holger Schemel");
1096   DrawTextSCentered(ystart + 4 * ystep + 20, FONT_TEXT_3,
1097                     "Detmolder Strasse 189");
1098   DrawTextSCentered(ystart + 4 * ystep + 40, FONT_TEXT_3,
1099                     "33604 Bielefeld");
1100   DrawTextSCentered(ystart + 4 * ystep + 60, FONT_TEXT_3,
1101                     "Germany");
1102
1103   DrawTextSCentered(ystart + 7 * ystep, FONT_TEXT_2,
1104                     "If you have created new levels,");
1105   DrawTextSCentered(ystart + 8 * ystep, FONT_TEXT_2,
1106                     "send them to me to include them!");
1107   DrawTextSCentered(ystart + 9 * ystep, FONT_TEXT_2,
1108                     ":-)");
1109
1110   DrawTextSCentered(ybottom, FONT_TEXT_4,
1111                     "Press any key or button for info menu");
1112 }
1113
1114 void HandleInfoScreen_Program(int button)
1115 {
1116   int button_released = !button;
1117
1118   if (button == MB_MENU_LEAVE)
1119   {
1120     info_mode = INFO_MODE_MAIN;
1121     DrawInfoScreen();
1122
1123     return;
1124   }
1125
1126   if (button_released)
1127   {
1128     FadeSoundsAndMusic();
1129
1130     info_mode = INFO_MODE_MAIN;
1131     DrawInfoScreen();
1132   }
1133   else
1134   {
1135     PlayMenuSoundIfLoop();
1136   }
1137 }
1138
1139 void DrawInfoScreen_LevelSet()
1140 {
1141   int ystart = 150;
1142   int ybottom = SYSIZE - 20;
1143   char *filename = getLevelSetInfoFilename();
1144 #if 0
1145   int font_nr = FONT_TEXT_2;
1146 #else
1147   int font_nr = FONT_LEVEL_NUMBER;
1148 #endif
1149   int font_width = getFontWidth(font_nr);
1150   int font_height = getFontHeight(font_nr);
1151   int pad_x = 32;
1152   int pad_y = ystart;
1153   int sx = SX + pad_x;
1154   int sy = SY + pad_y;
1155   int max_chars_per_line = (SXSIZE - 2 * pad_x) / font_width;
1156   int max_lines_per_screen = (SYSIZE - pad_y) / font_height - 1;
1157
1158   ClearWindow();
1159   DrawHeadline();
1160
1161   DrawTextSCentered(100, FONT_TEXT_1, "Level Set Information:");
1162
1163   DrawTextSCentered(ybottom, FONT_TEXT_4,
1164                     "Press any key or button for info menu");
1165
1166   if (filename != NULL)
1167     DrawTextFromFile(sx, sy, filename, font_nr, max_chars_per_line,
1168                      max_lines_per_screen);
1169   else
1170     DrawTextSCentered(ystart, FONT_TEXT_2,
1171                       "No information for this level set.");
1172 }
1173
1174 void HandleInfoScreen_LevelSet(int button)
1175 {
1176   int button_released = !button;
1177
1178   if (button == MB_MENU_LEAVE)
1179   {
1180     info_mode = INFO_MODE_MAIN;
1181     DrawInfoScreen();
1182
1183     return;
1184   }
1185
1186   if (button_released)
1187   {
1188     FadeSoundsAndMusic();
1189
1190     info_mode = INFO_MODE_MAIN;
1191     DrawInfoScreen();
1192   }
1193   else
1194   {
1195     PlayMenuSoundIfLoop();
1196   }
1197 }
1198
1199 void DrawInfoScreen()
1200 {
1201   SetMainBackgroundImage(IMG_BACKGROUND_INFO);
1202
1203   if (info_mode == INFO_MODE_ELEMENTS)
1204     DrawInfoScreen_Elements();
1205   else if (info_mode == INFO_MODE_MUSIC)
1206     DrawInfoScreen_Music();
1207   else if (info_mode == INFO_MODE_CREDITS)
1208     DrawInfoScreen_Credits();
1209   else if (info_mode == INFO_MODE_PROGRAM)
1210     DrawInfoScreen_Program();
1211   else if (info_mode == INFO_MODE_LEVELSET)
1212     DrawInfoScreen_LevelSet();
1213   else
1214     DrawInfoScreen_Main();
1215
1216   if (info_mode != INFO_MODE_MUSIC)
1217   {
1218     PlayMenuSound();
1219     PlayMenuMusic();
1220   }
1221 }
1222
1223 void HandleInfoScreen(int mx, int my, int dx, int dy, int button)
1224 {
1225   if (info_mode == INFO_MODE_ELEMENTS)
1226     HandleInfoScreen_Elements(button);
1227   else if (info_mode == INFO_MODE_MUSIC)
1228     HandleInfoScreen_Music(button);
1229   else if (info_mode == INFO_MODE_CREDITS)
1230     HandleInfoScreen_Credits(button);
1231   else if (info_mode == INFO_MODE_PROGRAM)
1232     HandleInfoScreen_Program(button);
1233   else if (info_mode == INFO_MODE_LEVELSET)
1234     HandleInfoScreen_LevelSet(button);
1235   else
1236     HandleInfoScreen_Main(mx, my, dx, dy, button);
1237
1238   DoAnimation();
1239 }
1240
1241
1242 /* ========================================================================= */
1243 /* type name functions                                                       */
1244 /* ========================================================================= */
1245
1246 void HandleTypeName(int newxpos, Key key)
1247 {
1248   static int xpos = 0, ypos = 2;
1249   int font_width = getFontWidth(FONT_INPUT_1_ACTIVE);
1250   int name_width = getFontWidth(FONT_MENU_1) * strlen("Name:");
1251   int startx = mSX + 32 + name_width;
1252   int starty = mSY + ypos * 32;
1253
1254   if (newxpos)
1255   {
1256     xpos = newxpos;
1257
1258     DrawText(startx, starty, setup.player_name, FONT_INPUT_1_ACTIVE);
1259     DrawText(startx + xpos * font_width, starty, "_", FONT_INPUT_1_ACTIVE);
1260
1261     return;
1262   }
1263
1264   if (((key >= KSYM_A && key <= KSYM_Z) ||
1265        (key >= KSYM_a && key <= KSYM_z)) && 
1266       xpos < MAX_PLAYER_NAME_LEN)
1267   {
1268     char ascii;
1269
1270     if (key >= KSYM_A && key <= KSYM_Z)
1271       ascii = 'A' + (char)(key - KSYM_A);
1272     else
1273       ascii = 'a' + (char)(key - KSYM_a);
1274
1275     setup.player_name[xpos] = ascii;
1276     setup.player_name[xpos + 1] = 0;
1277     xpos++;
1278
1279     DrawText(startx, starty, setup.player_name, FONT_INPUT_1_ACTIVE);
1280     DrawText(startx + xpos * font_width, starty, "_", FONT_INPUT_1_ACTIVE);
1281   }
1282   else if ((key == KSYM_Delete || key == KSYM_BackSpace) && xpos > 0)
1283   {
1284     xpos--;
1285     setup.player_name[xpos] = 0;
1286
1287     DrawText(startx + xpos * font_width, starty, "_ ", FONT_INPUT_1_ACTIVE);
1288   }
1289   else if (key == KSYM_Return && xpos > 0)
1290   {
1291     DrawText(startx, starty, setup.player_name, FONT_INPUT_1);
1292     DrawText(startx + xpos * font_width, starty, " ", FONT_INPUT_1_ACTIVE);
1293
1294     SaveSetup();
1295     game_status = GAME_MODE_MAIN;
1296   }
1297 }
1298
1299
1300 /* ========================================================================= */
1301 /* tree menu functions                                                       */
1302 /* ========================================================================= */
1303
1304 static void DrawChooseTree(TreeInfo **ti_ptr)
1305 {
1306   UnmapAllGadgets();
1307
1308   FreeScreenGadgets();
1309   CreateScreenGadgets();
1310
1311   CloseDoor(DOOR_CLOSE_2);
1312
1313   ClearWindow();
1314
1315   HandleChooseTree(0, 0, 0, 0, MB_MENU_INITIALIZE, ti_ptr);
1316   MapChooseTreeGadgets(*ti_ptr);
1317
1318   FadeToFront();
1319   InitAnimation();
1320 }
1321
1322 static void AdjustChooseTreeScrollbar(int id, int first_entry, TreeInfo *ti)
1323 {
1324   struct GadgetInfo *gi = screen_gadget[id];
1325   int items_max, items_visible, item_position;
1326
1327   items_max = numTreeInfoInGroup(ti);
1328   items_visible = NUM_MENU_ENTRIES_ON_SCREEN;
1329   item_position = first_entry;
1330
1331   if (item_position > items_max - items_visible)
1332     item_position = items_max - items_visible;
1333
1334   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
1335                GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
1336                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
1337 }
1338
1339 static void drawChooseTreeList(int first_entry, int num_page_entries,
1340                                TreeInfo *ti)
1341 {
1342   int i;
1343   char buffer[SCR_FIELDX * 2];
1344   int max_buffer_len = (SCR_FIELDX - 2) * 2;
1345   char *title_string = NULL;
1346   int xoffset_setup = 16;
1347   int yoffset_setup = 16;
1348   int xoffset = (ti->type == TREE_TYPE_LEVEL_DIR ? 0 : xoffset_setup);
1349   int yoffset = (ti->type == TREE_TYPE_LEVEL_DIR ? 0 : yoffset_setup);
1350   int last_game_status = game_status;   /* save current game status */
1351
1352   title_string =
1353     (ti->type == TREE_TYPE_LEVEL_DIR ? "Level Directories" :
1354      ti->type == TREE_TYPE_GRAPHICS_DIR ? "Custom Graphics" :
1355      ti->type == TREE_TYPE_SOUNDS_DIR ? "Custom Sounds" :
1356      ti->type == TREE_TYPE_MUSIC_DIR ? "Custom Music" : "");
1357
1358   DrawText(SX + xoffset, SY + yoffset, title_string, FONT_TITLE_1);
1359
1360   /* force LEVELS font on artwork setup screen */
1361   game_status = GAME_MODE_LEVELS;
1362
1363   /* clear tree list area, but not title or scrollbar */
1364   DrawBackground(mSX, mSY + MENU_SCREEN_START_YPOS * 32,
1365                  SXSIZE - 32 + menu.scrollbar_xoffset,
1366                  MAX_MENU_ENTRIES_ON_SCREEN * 32);
1367
1368   for (i = 0; i < num_page_entries; i++)
1369   {
1370     TreeInfo *node, *node_first;
1371     int entry_pos = first_entry + i;
1372     int ypos = MENU_SCREEN_START_YPOS + i;
1373
1374     node_first = getTreeInfoFirstGroupEntry(ti);
1375     node = getTreeInfoFromPos(node_first, entry_pos);
1376
1377     strncpy(buffer, node->name , max_buffer_len);
1378     buffer[max_buffer_len] = '\0';
1379
1380     DrawText(mSX + 32, mSY + ypos * 32, buffer, FONT_TEXT_1 + node->color);
1381
1382     if (node->parent_link)
1383       initCursor(i, IMG_MENU_BUTTON_LEFT);
1384     else if (node->level_group)
1385       initCursor(i, IMG_MENU_BUTTON_RIGHT);
1386     else
1387       initCursor(i, IMG_MENU_BUTTON);
1388   }
1389
1390   game_status = last_game_status;       /* restore current game status */
1391
1392   redraw_mask |= REDRAW_FIELD;
1393 }
1394
1395 static void drawChooseTreeInfo(int entry_pos, TreeInfo *ti)
1396 {
1397   TreeInfo *node, *node_first;
1398   int x, last_redraw_mask = redraw_mask;
1399
1400   if (ti->type != TREE_TYPE_LEVEL_DIR)
1401     return;
1402
1403   node_first = getTreeInfoFirstGroupEntry(ti);
1404   node = getTreeInfoFromPos(node_first, entry_pos);
1405
1406   DrawBackground(SX + 32, SY + 32, SXSIZE - 64, 32);
1407
1408   if (node->parent_link)
1409     DrawTextFCentered(40, FONT_TITLE_2, "leave group \"%s\"",
1410                       node->class_desc);
1411   else if (node->level_group)
1412     DrawTextFCentered(40, FONT_TITLE_2, "enter group \"%s\"",
1413                       node->class_desc);
1414   else if (ti->type == TREE_TYPE_LEVEL_DIR)
1415     DrawTextFCentered(40, FONT_TITLE_2, "%3d levels (%s)",
1416                       node->levels, node->class_desc);
1417
1418   /* let BackToFront() redraw only what is needed */
1419   redraw_mask = last_redraw_mask | REDRAW_TILES;
1420   for (x = 0; x < SCR_FIELDX; x++)
1421     MarkTileDirty(x, 1);
1422 }
1423
1424 static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
1425                              TreeInfo **ti_ptr)
1426 {
1427   TreeInfo *ti = *ti_ptr;
1428   int x = 0;
1429   int y = ti->cl_cursor;
1430   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
1431   int num_entries = numTreeInfoInGroup(ti);
1432   int num_page_entries;
1433   int last_game_status = game_status;   /* save current game status */
1434
1435   /* force LEVELS draw offset on choose level and artwork setup screen */
1436   game_status = GAME_MODE_LEVELS;
1437
1438   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
1439     num_page_entries = num_entries;
1440   else
1441     num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
1442
1443   game_status = last_game_status;       /* restore current game status */
1444
1445   if (button == MB_MENU_INITIALIZE)
1446   {
1447     int num_entries = numTreeInfoInGroup(ti);
1448     int entry_pos = posTreeInfo(ti);
1449
1450     if (ti->cl_first == -1)
1451     {
1452       /* only on initialization */
1453       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
1454       ti->cl_cursor = entry_pos - ti->cl_first;
1455     }
1456     else if (ti->cl_cursor >= num_page_entries ||
1457              (num_entries > num_page_entries &&
1458               num_entries - ti->cl_first < num_page_entries))
1459     {
1460       /* only after change of list size (by custom graphic configuration) */
1461       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
1462       ti->cl_cursor = entry_pos - ti->cl_first;
1463     }
1464
1465     if (dx == 999)      /* first entry is set by scrollbar position */
1466       ti->cl_first = dy;
1467     else
1468       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1469                                 ti->cl_first, ti);
1470
1471     drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1472     drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1473     drawChooseTreeCursor(ti->cl_cursor, FC_RED);
1474
1475     return;
1476   }
1477   else if (button == MB_MENU_LEAVE)
1478   {
1479     if (ti->node_parent)
1480     {
1481       *ti_ptr = ti->node_parent;
1482       DrawChooseTree(ti_ptr);
1483     }
1484     else if (game_status == GAME_MODE_SETUP)
1485     {
1486       execSetupArtwork();
1487     }
1488     else
1489     {
1490       game_status = GAME_MODE_MAIN;
1491       DrawMainMenu();
1492     }
1493
1494     return;
1495   }
1496
1497   if (mx || my)         /* mouse input */
1498   {
1499     int last_game_status = game_status; /* save current game status */
1500
1501     /* force LEVELS draw offset on artwork setup screen */
1502     game_status = GAME_MODE_LEVELS;
1503
1504     x = (mx - mSX) / 32;
1505     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
1506
1507     game_status = last_game_status;     /* restore current game status */
1508   }
1509   else if (dx || dy)    /* keyboard or scrollbar/scrollbutton input */
1510   {
1511     /* move cursor instead of scrolling when already at start/end of list */
1512     if (dy == -1 * SCROLL_LINE && ti->cl_first == 0)
1513       dy = -1;
1514     else if (dy == +1 * SCROLL_LINE &&
1515              ti->cl_first + num_page_entries == num_entries)
1516       dy = 1;
1517
1518     /* handle scrolling screen one line or page */
1519     if (ti->cl_cursor + dy < 0 ||
1520         ti->cl_cursor + dy > num_page_entries - 1)
1521     {
1522       if (ABS(dy) == SCROLL_PAGE)
1523         step = num_page_entries - 1;
1524
1525       if (dy < 0 && ti->cl_first > 0)
1526       {
1527         /* scroll page/line up */
1528
1529         ti->cl_first -= step;
1530         if (ti->cl_first < 0)
1531           ti->cl_first = 0;
1532
1533         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1534         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1535         drawChooseTreeCursor(ti->cl_cursor, FC_RED);
1536         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1537                                   ti->cl_first, ti);
1538       }
1539       else if (dy > 0 && ti->cl_first + num_page_entries < num_entries)
1540       {
1541         /* scroll page/line down */
1542
1543         ti->cl_first += step;
1544         if (ti->cl_first + num_page_entries > num_entries)
1545           ti->cl_first = MAX(0, num_entries - num_page_entries);
1546
1547         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1548         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1549         drawChooseTreeCursor(ti->cl_cursor, FC_RED);
1550         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1551                                   ti->cl_first, ti);
1552       }
1553
1554       return;
1555     }
1556
1557     /* handle moving cursor one line */
1558     y = ti->cl_cursor + dy;
1559   }
1560
1561   if (dx == 1)
1562   {
1563     TreeInfo *node_first, *node_cursor;
1564     int entry_pos = ti->cl_first + y;
1565
1566     node_first = getTreeInfoFirstGroupEntry(ti);
1567     node_cursor = getTreeInfoFromPos(node_first, entry_pos);
1568
1569     if (node_cursor->node_group)
1570     {
1571       node_cursor->cl_first = ti->cl_first;
1572       node_cursor->cl_cursor = ti->cl_cursor;
1573       *ti_ptr = node_cursor->node_group;
1574       DrawChooseTree(ti_ptr);
1575
1576       return;
1577     }
1578   }
1579   else if (dx == -1 && ti->node_parent)
1580   {
1581     *ti_ptr = ti->node_parent;
1582     DrawChooseTree(ti_ptr);
1583
1584     return;
1585   }
1586
1587   if (IN_VIS_FIELD(x, y) &&
1588       mx < screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x &&
1589       y >= 0 && y < num_page_entries)
1590   {
1591     if (button)
1592     {
1593       if (y != ti->cl_cursor)
1594       {
1595         drawChooseTreeCursor(y, FC_RED);
1596         drawChooseTreeCursor(ti->cl_cursor, FC_BLUE);
1597         drawChooseTreeInfo(ti->cl_first + y, ti);
1598         ti->cl_cursor = y;
1599       }
1600     }
1601     else
1602     {
1603       TreeInfo *node_first, *node_cursor;
1604       int entry_pos = ti->cl_first + y;
1605
1606       node_first = getTreeInfoFirstGroupEntry(ti);
1607       node_cursor = getTreeInfoFromPos(node_first, entry_pos);
1608
1609       if (node_cursor->node_group)
1610       {
1611         node_cursor->cl_first = ti->cl_first;
1612         node_cursor->cl_cursor = ti->cl_cursor;
1613         *ti_ptr = node_cursor->node_group;
1614         DrawChooseTree(ti_ptr);
1615       }
1616       else if (node_cursor->parent_link)
1617       {
1618         *ti_ptr = node_cursor->node_parent;
1619         DrawChooseTree(ti_ptr);
1620       }
1621       else
1622       {
1623         node_cursor->cl_first = ti->cl_first;
1624         node_cursor->cl_cursor = ti->cl_cursor;
1625         *ti_ptr = node_cursor;
1626
1627         if (ti->type == TREE_TYPE_LEVEL_DIR)
1628         {
1629           LoadLevelSetup_SeriesInfo();
1630
1631           SaveLevelSetup_LastSeries();
1632           SaveLevelSetup_SeriesInfo();
1633           TapeErase();
1634         }
1635
1636         if (game_status == GAME_MODE_SETUP)
1637         {
1638           execSetupArtwork();
1639         }
1640         else
1641         {
1642           game_status = GAME_MODE_MAIN;
1643           DrawMainMenu();
1644         }
1645       }
1646     }
1647   }
1648 }
1649
1650 void DrawChooseLevel()
1651 {
1652   SetMainBackgroundImage(IMG_BACKGROUND_LEVELS);
1653
1654   DrawChooseTree(&leveldir_current);
1655
1656   PlayMenuSound();
1657   PlayMenuMusic();
1658 }
1659
1660 void HandleChooseLevel(int mx, int my, int dx, int dy, int button)
1661 {
1662   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
1663
1664   DoAnimation();
1665 }
1666
1667 void DrawHallOfFame(int highlight_position)
1668 {
1669   UnmapAllGadgets();
1670   FadeSoundsAndMusic();
1671   CloseDoor(DOOR_CLOSE_2);
1672
1673   if (highlight_position < 0) 
1674     LoadScore(level_nr);
1675
1676   FadeToFront();
1677   InitAnimation();
1678
1679   PlayMenuSound();
1680   PlayMenuMusic();
1681
1682   HandleHallOfFame(highlight_position, 0, 0, 0, MB_MENU_INITIALIZE);
1683 }
1684
1685 static void drawHallOfFameList(int first_entry, int highlight_position)
1686 {
1687   int i;
1688
1689   SetMainBackgroundImage(IMG_BACKGROUND_SCORES);
1690   ClearWindow();
1691
1692   DrawText(mSX + 80, mSY + 8, "Hall Of Fame", FONT_TITLE_1);
1693   DrawTextFCentered(46, FONT_TITLE_2, "HighScores of Level %d", level_nr);
1694
1695   for (i = 0; i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
1696   {
1697     int entry = first_entry + i;
1698     boolean active = (entry == highlight_position);
1699     int font_nr1 = (active ? FONT_TEXT_1_ACTIVE : FONT_TEXT_1);
1700     int font_nr2 = (active ? FONT_TEXT_2_ACTIVE : FONT_TEXT_2);
1701     int font_nr3 = (active ? FONT_TEXT_3_ACTIVE : FONT_TEXT_3);
1702     int font_nr4 = (active ? FONT_TEXT_4_ACTIVE : FONT_TEXT_4);
1703     int dx1 = 3 * getFontWidth(font_nr1);
1704     int dx2 = dx1 + getFontWidth(font_nr1);
1705     int dx3 = dx2 + 25 * getFontWidth(font_nr3);
1706     int sy = mSY + 64 + i * 32;
1707
1708     DrawText(mSX, sy, int2str(entry + 1, 3), font_nr1);
1709     DrawText(mSX + dx1, sy, ".", font_nr1);
1710     DrawText(mSX + dx2, sy, ".........................", font_nr3);
1711     if (strcmp(highscore[entry].Name, EMPTY_PLAYER_NAME) != 0)
1712       DrawText(mSX + dx2, sy, highscore[entry].Name, font_nr2);
1713     DrawText(mSX + dx3, sy, int2str(highscore[entry].Score, 5), font_nr4);
1714   }
1715
1716   redraw_mask |= REDRAW_FIELD;
1717 }
1718
1719 void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
1720 {
1721   static int first_entry = 0;
1722   static int highlight_position = 0;
1723   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
1724   int button_released = !button;
1725
1726   if (button == MB_MENU_INITIALIZE)
1727   {
1728     first_entry = 0;
1729     highlight_position = mx;
1730     drawHallOfFameList(first_entry, highlight_position);
1731
1732     return;
1733   }
1734
1735   if (ABS(dy) == SCROLL_PAGE)           /* handle scrolling one page */
1736     step = NUM_MENU_ENTRIES_ON_SCREEN - 1;
1737
1738   if (dy < 0)
1739   {
1740     if (first_entry > 0)
1741     {
1742       first_entry -= step;
1743       if (first_entry < 0)
1744         first_entry = 0;
1745
1746       drawHallOfFameList(first_entry, highlight_position);
1747     }
1748   }
1749   else if (dy > 0)
1750   {
1751     if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
1752     {
1753       first_entry += step;
1754       if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
1755         first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
1756
1757       drawHallOfFameList(first_entry, highlight_position);
1758     }
1759   }
1760   else if (button_released)
1761   {
1762     FadeSound(SND_BACKGROUND_SCORES);
1763     game_status = GAME_MODE_MAIN;
1764     DrawMainMenu();
1765   }
1766
1767   if (game_status == GAME_MODE_SCORES)
1768     PlayMenuSoundIfLoop();
1769
1770   DoAnimation();
1771 }
1772
1773
1774 /* ========================================================================= */
1775 /* setup screen functions                                                    */
1776 /* ========================================================================= */
1777
1778 static struct TokenInfo *setup_info;
1779 static int num_setup_info;
1780
1781 static char *graphics_set_name;
1782 static char *sounds_set_name;
1783 static char *music_set_name;
1784
1785 static void execSetupMain()
1786 {
1787   setup_mode = SETUP_MODE_MAIN;
1788   DrawSetupScreen();
1789 }
1790
1791 static void execSetupGame()
1792 {
1793   setup_mode = SETUP_MODE_GAME;
1794   DrawSetupScreen();
1795 }
1796
1797 static void execSetupEditor()
1798 {
1799   setup_mode = SETUP_MODE_EDITOR;
1800   DrawSetupScreen();
1801 }
1802
1803 static void execSetupGraphics()
1804 {
1805   setup_mode = SETUP_MODE_GRAPHICS;
1806   DrawSetupScreen();
1807 }
1808
1809 static void execSetupSound()
1810 {
1811   setup_mode = SETUP_MODE_SOUND;
1812   DrawSetupScreen();
1813 }
1814
1815 static void execSetupArtwork()
1816 {
1817   setup.graphics_set = artwork.gfx_current->identifier;
1818   setup.sounds_set = artwork.snd_current->identifier;
1819   setup.music_set = artwork.mus_current->identifier;
1820
1821   /* needed if last screen (setup choice) changed graphics, sounds or music */
1822   ReloadCustomArtwork();
1823
1824   /* needed for displaying artwork name instead of artwork identifier */
1825   graphics_set_name = artwork.gfx_current->name;
1826   sounds_set_name = artwork.snd_current->name;
1827   music_set_name = artwork.mus_current->name;
1828
1829   setup_mode = SETUP_MODE_ARTWORK;
1830   DrawSetupScreen();
1831 }
1832
1833 static void execSetupChooseGraphics()
1834 {
1835   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
1836   DrawSetupScreen();
1837 }
1838
1839 static void execSetupChooseSounds()
1840 {
1841   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
1842   DrawSetupScreen();
1843 }
1844
1845 static void execSetupChooseMusic()
1846 {
1847   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
1848   DrawSetupScreen();
1849 }
1850
1851 static void execSetupInput()
1852 {
1853   setup_mode = SETUP_MODE_INPUT;
1854   DrawSetupScreen();
1855 }
1856
1857 static void execSetupShortcut()
1858 {
1859   setup_mode = SETUP_MODE_SHORTCUT;
1860   DrawSetupScreen();
1861 }
1862
1863 static void execExitSetup()
1864 {
1865   game_status = GAME_MODE_MAIN;
1866   DrawMainMenu();
1867 }
1868
1869 static void execSaveAndExitSetup()
1870 {
1871   SaveSetup();
1872   execExitSetup();
1873 }
1874
1875 static struct TokenInfo setup_info_main[] =
1876 {
1877   { TYPE_ENTER_MENU,    execSetupGame,          "Game Settings"         },
1878   { TYPE_ENTER_MENU,    execSetupEditor,        "Editor Settings"       },
1879   { TYPE_ENTER_MENU,    execSetupGraphics,      "Graphics"              },
1880   { TYPE_ENTER_MENU,    execSetupSound,         "Sound & Music"         },
1881   { TYPE_ENTER_MENU,    execSetupArtwork,       "Custom Artwork"        },
1882   { TYPE_ENTER_MENU,    execSetupInput,         "Input Devices"         },
1883   { TYPE_ENTER_MENU,    execSetupShortcut,      "Key Shortcuts"         },
1884   { TYPE_EMPTY,         NULL,                   ""                      },
1885   { TYPE_LEAVE_MENU,    execExitSetup,          "Exit"                  },
1886   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   "Save and Exit"         },
1887
1888   { 0,                  NULL,                   NULL                    }
1889 };
1890
1891 static struct TokenInfo setup_info_game[] =
1892 {
1893   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode:"            },
1894   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
1895   { TYPE_SWITCH,        &setup.time_limit,      "Timelimit:"            },
1896   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record:"          },
1897   { TYPE_EMPTY,         NULL,                   ""                      },
1898   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1899
1900   { 0,                  NULL,                   NULL                    }
1901 };
1902
1903 static struct TokenInfo setup_info_editor[] =
1904 {
1905   { TYPE_STRING,        NULL,                   "Offer Special Elements:"},
1906   { TYPE_SWITCH,        &setup.editor.el_boulderdash,   "BoulderDash:"  },
1907   { TYPE_SWITCH,        &setup.editor.el_emerald_mine,  "Emerald Mine:" },
1908   { TYPE_SWITCH,        &setup.editor.el_more,          "More:"         },
1909   { TYPE_SWITCH,        &setup.editor.el_sokoban,       "Sokoban:"      },
1910   { TYPE_SWITCH,        &setup.editor.el_supaplex,      "Supaplex:"     },
1911   { TYPE_SWITCH,        &setup.editor.el_diamond_caves, "Diamd. Caves:" },
1912   { TYPE_SWITCH,        &setup.editor.el_dx_boulderdash,"DX Boulderd.:" },
1913   { TYPE_SWITCH,        &setup.editor.el_chars,         "Characters:"   },
1914   { TYPE_SWITCH,        &setup.editor.el_custom,        "Custom:"       },
1915   { TYPE_SWITCH,        &setup.editor.el_custom_more,   "More Custom:"  },
1916   { TYPE_SWITCH,        &setup.editor.el_headlines,     "Headlines:"    },
1917   { TYPE_SWITCH,        &setup.editor.el_user_defined,  "User defined:" },
1918   { TYPE_EMPTY,         NULL,                   ""                      },
1919   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1920
1921   { 0,                  NULL,                   NULL                    }
1922 };
1923
1924 static struct TokenInfo setup_info_graphics[] =
1925 {
1926   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen:"           },
1927   { TYPE_SWITCH,        &setup.scroll_delay,    "Scroll Delay:"         },
1928   { TYPE_SWITCH,        &setup.soft_scrolling,  "Soft Scroll.:"         },
1929 #if 0
1930   { TYPE_SWITCH,        &setup.double_buffering,"Buffered gfx:"         },
1931   { TYPE_SWITCH,        &setup.fading,          "Fading:"               },
1932 #endif
1933   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Doors:"          },
1934   { TYPE_SWITCH,        &setup.toons,           "Toons:"                },
1935   { TYPE_EMPTY,         NULL,                   ""                      },
1936   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1937
1938   { 0,                  NULL,                   NULL                    }
1939 };
1940
1941 static struct TokenInfo setup_info_sound[] =
1942 {
1943   { TYPE_SWITCH,        &setup.sound_simple,    "Simple Sound:"         },
1944   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Loops:"          },
1945   { TYPE_SWITCH,        &setup.sound_music,     "Game Music:"           },
1946   { TYPE_EMPTY,         NULL,                   ""                      },
1947   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1948
1949   { 0,                  NULL,                   NULL                    }
1950 };
1951
1952 static struct TokenInfo setup_info_artwork[] =
1953 {
1954   { TYPE_ENTER_MENU,    execSetupChooseGraphics,"Custom Graphics"       },
1955   { TYPE_STRING,        &graphics_set_name,     ""                      },
1956   { TYPE_ENTER_MENU,    execSetupChooseSounds,  "Custom Sounds"         },
1957   { TYPE_STRING,        &sounds_set_name,       ""                      },
1958   { TYPE_ENTER_MENU,    execSetupChooseMusic,   "Custom Music"          },
1959   { TYPE_STRING,        &music_set_name,        ""                      },
1960   { TYPE_EMPTY,         NULL,                   ""                      },
1961   { TYPE_STRING,        NULL,                   "Override Level Artwork:"},
1962   { TYPE_YES_NO,        &setup.override_level_graphics, "Graphics:"     },
1963   { TYPE_YES_NO,        &setup.override_level_sounds,   "Sounds:"       },
1964   { TYPE_YES_NO,        &setup.override_level_music,    "Music:"        },
1965   { TYPE_EMPTY,         NULL,                   ""                      },
1966   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1967
1968   { 0,                  NULL,                   NULL                    }
1969 };
1970
1971 static struct TokenInfo setup_info_shortcut[] =
1972 {
1973   { TYPE_KEYTEXT,       NULL,                   "Quick Save Game:",     },
1974   { TYPE_KEY,           &setup.shortcut.save_game,      ""              },
1975   { TYPE_KEYTEXT,       NULL,                   "Quick Load Game:",     },
1976   { TYPE_KEY,           &setup.shortcut.load_game,      ""              },
1977   { TYPE_KEYTEXT,       NULL,                   "Toggle Pause:",        },
1978   { TYPE_KEY,           &setup.shortcut.toggle_pause,   ""              },
1979   { TYPE_EMPTY,         NULL,                   ""                      },
1980   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on Esc:"           },
1981   { TYPE_EMPTY,         NULL,                   ""                      },
1982   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1983
1984   { 0,                  NULL,                   NULL                    }
1985 };
1986
1987 static Key getSetupKey()
1988 {
1989   Key key = KSYM_UNDEFINED;
1990   boolean got_key_event = FALSE;
1991
1992   while (!got_key_event)
1993   {
1994     if (PendingEvent())         /* got event */
1995     {
1996       Event event;
1997
1998       NextEvent(&event);
1999
2000       switch(event.type)
2001       {
2002         case EVENT_KEYPRESS:
2003           {
2004             key = GetEventKey((KeyEvent *)&event, TRUE);
2005
2006             /* press 'Escape' or 'Enter' to keep the existing key binding */
2007             if (key == KSYM_Escape || key == KSYM_Return)
2008               key = KSYM_UNDEFINED;     /* keep old value */
2009
2010             got_key_event = TRUE;
2011           }
2012           break;
2013
2014         case EVENT_KEYRELEASE:
2015           key_joystick_mapping = 0;
2016           break;
2017
2018         default:
2019           HandleOtherEvents(&event);
2020           break;
2021       }
2022     }
2023
2024     DoAnimation();
2025     BackToFront();
2026
2027     /* don't eat all CPU time */
2028     Delay(10);
2029   }
2030
2031   return key;
2032 }
2033
2034 static void drawSetupValue(int pos)
2035 {
2036   int xpos = MENU_SCREEN_VALUE_XPOS;
2037   int ypos = MENU_SCREEN_START_YPOS + pos;
2038   int font_nr = FONT_VALUE_1;
2039   int type = setup_info[pos].type;
2040   void *value = setup_info[pos].value;
2041   char *value_string = (!(type & TYPE_GHOSTED) ? getSetupValue(type, value) :
2042                         "n/a");
2043
2044   if (value_string == NULL)
2045     return;
2046
2047   if (type & TYPE_KEY)
2048   {
2049     xpos = 3;
2050
2051     if (type & TYPE_QUERY)
2052     {
2053       value_string = "<press key>";
2054       font_nr = FONT_INPUT_1_ACTIVE;
2055     }
2056   }
2057   else if (type & TYPE_STRING)
2058   {
2059     int max_value_len = (SCR_FIELDX - 2) * 2;
2060
2061     xpos = 1;
2062     font_nr = FONT_VALUE_2;
2063
2064     if (strlen(value_string) > max_value_len)
2065       value_string[max_value_len] = '\0';
2066   }
2067   else if (type & TYPE_BOOLEAN_STYLE)
2068   {
2069     font_nr = (*(boolean *)value ? FONT_OPTION_ON : FONT_OPTION_OFF);
2070   }
2071
2072   DrawText(mSX + xpos * 32, mSY + ypos * 32,
2073            (xpos == 3 ? "              " : "   "), font_nr);
2074   DrawText(mSX + xpos * 32, mSY + ypos * 32, value_string, font_nr);
2075 }
2076
2077 static void changeSetupValue(int pos)
2078 {
2079   if (setup_info[pos].type & TYPE_BOOLEAN_STYLE)
2080   {
2081     *(boolean *)setup_info[pos].value ^= TRUE;
2082   }
2083   else if (setup_info[pos].type & TYPE_KEY)
2084   {
2085     Key key;
2086
2087     setup_info[pos].type |= TYPE_QUERY;
2088     drawSetupValue(pos);
2089     setup_info[pos].type &= ~TYPE_QUERY;
2090
2091     key = getSetupKey();
2092     if (key != KSYM_UNDEFINED)
2093       *(Key *)setup_info[pos].value = key;
2094   }
2095
2096   drawSetupValue(pos);
2097 }
2098
2099 static void DrawSetupScreen_Generic()
2100 {
2101   char *title_string = NULL;
2102   int i;
2103
2104   UnmapAllGadgets();
2105   CloseDoor(DOOR_CLOSE_2);
2106
2107   ClearWindow();
2108
2109   if (setup_mode == SETUP_MODE_MAIN)
2110   {
2111     setup_info = setup_info_main;
2112     title_string = "Setup";
2113   }
2114   else if (setup_mode == SETUP_MODE_GAME)
2115   {
2116     setup_info = setup_info_game;
2117     title_string = "Setup Game";
2118   }
2119   else if (setup_mode == SETUP_MODE_EDITOR)
2120   {
2121     setup_info = setup_info_editor;
2122     title_string = "Setup Editor";
2123   }
2124   else if (setup_mode == SETUP_MODE_GRAPHICS)
2125   {
2126     setup_info = setup_info_graphics;
2127     title_string = "Setup Graphics";
2128   }
2129   else if (setup_mode == SETUP_MODE_SOUND)
2130   {
2131     setup_info = setup_info_sound;
2132     title_string = "Setup Sound";
2133   }
2134   else if (setup_mode == SETUP_MODE_ARTWORK)
2135   {
2136     setup_info = setup_info_artwork;
2137     title_string = "Custom Artwork";
2138   }
2139   else if (setup_mode == SETUP_MODE_SHORTCUT)
2140   {
2141     setup_info = setup_info_shortcut;
2142     title_string = "Setup Shortcuts";
2143   }
2144
2145   DrawText(mSX + 16, mSY + 16, title_string, FONT_TITLE_1);
2146
2147   num_setup_info = 0;
2148   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
2149   {
2150     void *value_ptr = setup_info[i].value;
2151     int ypos = MENU_SCREEN_START_YPOS + i;
2152     int font_nr = FONT_MENU_1;
2153
2154     /* set some entries to "unchangeable" according to other variables */
2155     if ((value_ptr == &setup.sound_simple && !audio.sound_available) ||
2156         (value_ptr == &setup.sound_loops  && !audio.loops_available) ||
2157         (value_ptr == &setup.sound_music  && !audio.music_available) ||
2158         (value_ptr == &setup.fullscreen   && !video.fullscreen_available))
2159       setup_info[i].type |= TYPE_GHOSTED;
2160
2161     if (setup_info[i].type & TYPE_STRING)
2162       font_nr = FONT_MENU_2;
2163
2164     DrawText(mSX + 32, mSY + ypos * 32, setup_info[i].text, font_nr);
2165
2166     if (setup_info[i].type & TYPE_ENTER_MENU)
2167       initCursor(i, IMG_MENU_BUTTON_RIGHT);
2168     else if (setup_info[i].type & TYPE_LEAVE_MENU)
2169       initCursor(i, IMG_MENU_BUTTON_LEFT);
2170     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
2171       initCursor(i, IMG_MENU_BUTTON);
2172
2173     if (setup_info[i].type & TYPE_VALUE)
2174       drawSetupValue(i);
2175
2176     num_setup_info++;
2177   }
2178
2179   FadeToFront();
2180   InitAnimation();
2181   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
2182 }
2183
2184 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
2185 {
2186   static int choice_store[MAX_SETUP_MODES];
2187   int choice = choice_store[setup_mode];        /* always starts with 0 */
2188   int x = 0;
2189   int y = choice;
2190
2191   if (button == MB_MENU_INITIALIZE)
2192   {
2193     /* advance to first valid menu entry */
2194     while (choice < num_setup_info &&
2195            setup_info[choice].type & TYPE_SKIP_ENTRY)
2196       choice++;
2197     choice_store[setup_mode] = choice;
2198
2199     drawCursor(choice, FC_RED);
2200     return;
2201   }
2202   else if (button == MB_MENU_LEAVE)
2203   {
2204     for (y = 0; y < num_setup_info; y++)
2205     {
2206       if (setup_info[y].type & TYPE_LEAVE_MENU)
2207       {
2208         void (*menu_callback_function)(void) = setup_info[y].value;
2209
2210         menu_callback_function();
2211         break;  /* absolutely needed because function changes 'setup_info'! */
2212       }
2213     }
2214
2215     return;
2216   }
2217
2218   if (mx || my)         /* mouse input */
2219   {
2220     x = (mx - mSX) / 32;
2221     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2222   }
2223   else if (dx || dy)    /* keyboard input */
2224   {
2225     if (dx)
2226     {
2227       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE_MENU : TYPE_ENTER_MENU);
2228
2229       if (setup_info[choice].type & menu_navigation_type ||
2230           setup_info[choice].type & TYPE_BOOLEAN_STYLE)
2231         button = MB_MENU_CHOICE;
2232     }
2233     else if (dy)
2234       y = choice + dy;
2235
2236     /* jump to next non-empty menu entry (up or down) */
2237     while (y > 0 && y < num_setup_info - 1 &&
2238            setup_info[y].type & TYPE_SKIP_ENTRY)
2239       y += dy;
2240   }
2241
2242   if (IN_VIS_FIELD(x, y) &&
2243       y >= 0 && y < num_setup_info && setup_info[y].type & ~TYPE_SKIP_ENTRY)
2244   {
2245     if (button)
2246     {
2247       if (y != choice)
2248       {
2249         drawCursor(y, FC_RED);
2250         drawCursor(choice, FC_BLUE);
2251         choice = choice_store[setup_mode] = y;
2252       }
2253     }
2254     else if (!(setup_info[y].type & TYPE_GHOSTED))
2255     {
2256       if (setup_info[y].type & TYPE_ENTER_OR_LEAVE_MENU)
2257       {
2258         void (*menu_callback_function)(void) = setup_info[choice].value;
2259
2260         menu_callback_function();
2261       }
2262       else
2263       {
2264         if (setup_info[y].type & TYPE_KEYTEXT &&
2265             setup_info[y + 1].type & TYPE_KEY)
2266           y++;
2267
2268         if (setup_info[y].type & TYPE_VALUE)
2269           changeSetupValue(y);
2270       }
2271     }
2272   }
2273 }
2274
2275 void DrawSetupScreen_Input()
2276 {
2277   ClearWindow();
2278
2279   DrawText(mSX+16, mSY+16, "Setup Input", FONT_TITLE_1);
2280
2281   initCursor(0, IMG_MENU_BUTTON);
2282   initCursor(1, IMG_MENU_BUTTON);
2283   initCursor(2, IMG_MENU_BUTTON_RIGHT);
2284   initCursor(13, IMG_MENU_BUTTON_LEFT);
2285
2286   drawCursorXY(10, 0, IMG_MENU_BUTTON_LEFT);
2287   drawCursorXY(12, 0, IMG_MENU_BUTTON_RIGHT);
2288
2289   DrawText(mSX+32, mSY+2*32, "Player:", FONT_MENU_1);
2290   DrawText(mSX+32, mSY+3*32, "Device:", FONT_MENU_1);
2291   DrawText(mSX+32, mSY+15*32, "Back",   FONT_MENU_1);
2292
2293 #if 0
2294   DeactivateJoystickForCalibration();
2295   DrawTextSCentered(SYSIZE - 20, FONT_TEXT_4,
2296                     "Joysticks deactivated on this screen");
2297 #endif
2298
2299   HandleSetupScreen_Input(0, 0, 0, 0, MB_MENU_INITIALIZE);
2300   FadeToFront();
2301   InitAnimation();
2302 }
2303
2304 static void setJoystickDeviceToNr(char *device_name, int device_nr)
2305 {
2306   if (device_name == NULL)
2307     return;
2308
2309   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
2310     device_nr = 0;
2311
2312   if (strlen(device_name) > 1)
2313   {
2314     char c1 = device_name[strlen(device_name) - 1];
2315     char c2 = device_name[strlen(device_name) - 2];
2316
2317     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
2318       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
2319   }
2320   else
2321     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
2322             strlen(device_name));
2323 }
2324
2325 static void drawPlayerSetupInputInfo(int player_nr)
2326 {
2327   int i;
2328   static struct SetupKeyboardInfo custom_key;
2329   static struct
2330   {
2331     Key *key;
2332     char *text;
2333   } custom[] =
2334   {
2335     { &custom_key.left,  "Joystick Left"  },
2336     { &custom_key.right, "Joystick Right" },
2337     { &custom_key.up,    "Joystick Up"    },
2338     { &custom_key.down,  "Joystick Down"  },
2339     { &custom_key.snap,  "Button 1"       },
2340     { &custom_key.bomb,  "Button 2"       }
2341   };
2342   static char *joystick_name[MAX_PLAYERS] =
2343   {
2344     "Joystick1",
2345     "Joystick2",
2346     "Joystick3",
2347     "Joystick4"
2348   };
2349
2350   custom_key = setup.input[player_nr].key;
2351
2352   DrawText(mSX+11*32, mSY+2*32, int2str(player_nr +1, 1), FONT_INPUT_1_ACTIVE);
2353 #if 1
2354   DrawGraphicThruMaskExt(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
2355                          PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
2356 #else
2357   DrawGraphicThruMask(8, 2, PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
2358 #endif
2359
2360   if (setup.input[player_nr].use_joystick)
2361   {
2362     char *device_name = setup.input[player_nr].joy.device_name;
2363
2364     DrawText(mSX+8*32, mSY+3*32,
2365              joystick_name[getJoystickNrFromDeviceName(device_name)],
2366              FONT_VALUE_1);
2367     DrawText(mSX+32, mSY+4*32, "Calibrate", FONT_MENU_1);
2368   }
2369   else
2370   {
2371     DrawText(mSX+8*32, mSY+3*32, "Keyboard ", FONT_VALUE_1);
2372     DrawText(mSX+32,   mSY+4*32, "Customize", FONT_MENU_1);
2373   }
2374
2375   DrawText(mSX+32, mSY+5*32, "Actual Settings:", FONT_MENU_1);
2376   drawCursorXY(1, 4, IMG_MENU_BUTTON_LEFT);
2377   drawCursorXY(1, 5, IMG_MENU_BUTTON_RIGHT);
2378   drawCursorXY(1, 6, IMG_MENU_BUTTON_UP);
2379   drawCursorXY(1, 7, IMG_MENU_BUTTON_DOWN);
2380   DrawText(mSX+2*32, mSY+6*32, ":", FONT_VALUE_OLD);
2381   DrawText(mSX+2*32, mSY+7*32, ":", FONT_VALUE_OLD);
2382   DrawText(mSX+2*32, mSY+8*32, ":", FONT_VALUE_OLD);
2383   DrawText(mSX+2*32, mSY+9*32, ":", FONT_VALUE_OLD);
2384   DrawText(mSX+32, mSY+10*32, "Snap Field:", FONT_VALUE_OLD);
2385   DrawText(mSX+32, mSY+12*32, "Place Bomb:", FONT_VALUE_OLD);
2386
2387   for (i = 0; i < 6; i++)
2388   {
2389     int ypos = 6 + i + (i > 3 ? i-3 : 0);
2390
2391     DrawText(mSX + 3*32, mSY + ypos*32,
2392              "              ", FONT_VALUE_1);
2393     DrawText(mSX + 3*32, mSY + ypos*32,
2394              (setup.input[player_nr].use_joystick ?
2395               custom[i].text :
2396               getKeyNameFromKey(*custom[i].key)), FONT_VALUE_1);
2397   }
2398 }
2399
2400 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
2401 {
2402   static int choice = 0;
2403   static int player_nr = 0;
2404   int x = 0;
2405   int y = choice;
2406   int pos_start  = SETUPINPUT_SCREEN_POS_START;
2407   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
2408   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
2409   int pos_end    = SETUPINPUT_SCREEN_POS_END;
2410
2411   if (button == MB_MENU_INITIALIZE)
2412   {
2413     drawPlayerSetupInputInfo(player_nr);
2414     drawCursor(choice, FC_RED);
2415
2416     return;
2417   }
2418   else if (button == MB_MENU_LEAVE)
2419   {
2420     setup_mode = SETUP_MODE_MAIN;
2421     DrawSetupScreen();
2422     InitJoysticks();
2423
2424     return;
2425   }
2426
2427   if (mx || my)         /* mouse input */
2428   {
2429     x = (mx - mSX) / 32;
2430     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2431   }
2432   else if (dx || dy)    /* keyboard input */
2433   {
2434     if (dx && choice == 0)
2435       x = (dx < 0 ? 10 : 12);
2436     else if ((dx && choice == 1) ||
2437              (dx == +1 && choice == 2) ||
2438              (dx == -1 && choice == pos_end))
2439       button = MB_MENU_CHOICE;
2440     else if (dy)
2441       y = choice + dy;
2442
2443     if (y >= pos_empty1 && y <= pos_empty2)
2444       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
2445   }
2446
2447   if (IN_VIS_FIELD(x, y) &&
2448       y == 0 && ((x < 10 && !button) || ((x == 10 || x == 12) && button)))
2449   {
2450     static unsigned long delay = 0;
2451
2452     if (!DelayReached(&delay, GADGET_FRAME_DELAY))
2453       return;
2454
2455     player_nr = (player_nr + (x == 10 ? -1 : +1) + MAX_PLAYERS) % MAX_PLAYERS;
2456
2457     drawPlayerSetupInputInfo(player_nr);
2458   }
2459   else if (IN_VIS_FIELD(x, y) &&
2460            y >= pos_start && y <= pos_end &&
2461            !(y >= pos_empty1 && y <= pos_empty2))
2462   {
2463     if (button)
2464     {
2465       if (y != choice)
2466       {
2467         drawCursor(y, FC_RED);
2468         drawCursor(choice, FC_BLUE);
2469         choice = y;
2470       }
2471     }
2472     else
2473     {
2474       if (y == 1)
2475       {
2476         char *device_name = setup.input[player_nr].joy.device_name;
2477
2478         if (!setup.input[player_nr].use_joystick)
2479         {
2480           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
2481
2482           setJoystickDeviceToNr(device_name, new_device_nr);
2483           setup.input[player_nr].use_joystick = TRUE;
2484         }
2485         else
2486         {
2487           int device_nr = getJoystickNrFromDeviceName(device_name);
2488           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
2489
2490           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
2491             setup.input[player_nr].use_joystick = FALSE;
2492           else
2493             setJoystickDeviceToNr(device_name, new_device_nr);
2494         }
2495
2496         drawPlayerSetupInputInfo(player_nr);
2497       }
2498       else if (y == 2)
2499       {
2500         if (setup.input[player_nr].use_joystick)
2501         {
2502           InitJoysticks();
2503           CalibrateJoystick(player_nr);
2504         }
2505         else
2506           CustomizeKeyboard(player_nr);
2507       }
2508       else if (y == pos_end)
2509       {
2510         InitJoysticks();
2511
2512         setup_mode = SETUP_MODE_MAIN;
2513         DrawSetupScreen();
2514       }
2515     }
2516   }
2517 }
2518
2519 void CustomizeKeyboard(int player_nr)
2520 {
2521   int i;
2522   int step_nr;
2523   boolean finished = FALSE;
2524   static struct SetupKeyboardInfo custom_key;
2525   static struct
2526   {
2527     Key *key;
2528     char *text;
2529   } customize_step[] =
2530   {
2531     { &custom_key.left,  "Move Left"  },
2532     { &custom_key.right, "Move Right" },
2533     { &custom_key.up,    "Move Up"    },
2534     { &custom_key.down,  "Move Down"  },
2535     { &custom_key.snap,  "Snap Field" },
2536     { &custom_key.bomb,  "Place Bomb" }
2537   };
2538
2539   /* read existing key bindings from player setup */
2540   custom_key = setup.input[player_nr].key;
2541
2542   ClearWindow();
2543   DrawText(mSX + 16, mSY + 16, "Keyboard Input", FONT_TITLE_1);
2544
2545   BackToFront();
2546   InitAnimation();
2547
2548   step_nr = 0;
2549   DrawText(mSX, mSY + (2+2*step_nr)*32,
2550            customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
2551   DrawText(mSX, mSY + (2+2*step_nr+1)*32,
2552            "Key:", FONT_INPUT_1_ACTIVE);
2553   DrawText(mSX + 4*32, mSY + (2+2*step_nr+1)*32,
2554            getKeyNameFromKey(*customize_step[step_nr].key), FONT_VALUE_OLD);
2555
2556   while (!finished)
2557   {
2558     if (PendingEvent())         /* got event */
2559     {
2560       Event event;
2561
2562       NextEvent(&event);
2563
2564       switch(event.type)
2565       {
2566         case EVENT_KEYPRESS:
2567           {
2568             Key key = GetEventKey((KeyEvent *)&event, FALSE);
2569
2570             if (key == KSYM_Escape || (key == KSYM_Return && step_nr == 6))
2571             {
2572               finished = TRUE;
2573               break;
2574             }
2575
2576             /* all keys configured -- wait for "Escape" or "Return" key */
2577             if (step_nr == 6)
2578               break;
2579
2580             /* press 'Enter' to keep the existing key binding */
2581             if (key == KSYM_Return)
2582               key = *customize_step[step_nr].key;
2583
2584             /* check if key already used */
2585             for (i = 0; i < step_nr; i++)
2586               if (*customize_step[i].key == key)
2587                 break;
2588             if (i < step_nr)
2589               break;
2590
2591             /* got new key binding */
2592             *customize_step[step_nr].key = key;
2593             DrawText(mSX + 4*32, mSY + (2+2*step_nr+1)*32,
2594                      "             ", FONT_VALUE_1);
2595             DrawText(mSX + 4*32, mSY + (2+2*step_nr+1)*32,
2596                      getKeyNameFromKey(key), FONT_VALUE_1);
2597             step_nr++;
2598
2599             /* un-highlight last query */
2600             DrawText(mSX, mSY+(2+2*(step_nr-1))*32,
2601                      customize_step[step_nr-1].text, FONT_MENU_1);
2602             DrawText(mSX, mSY+(2+2*(step_nr-1)+1)*32,
2603                      "Key:", FONT_MENU_1);
2604
2605             /* press 'Enter' to leave */
2606             if (step_nr == 6)
2607             {
2608               DrawText(mSX + 16, mSY + 15*32+16,
2609                        "Press Enter", FONT_TITLE_1);
2610               break;
2611             }
2612
2613             /* query next key binding */
2614             DrawText(mSX, mSY+(2+2*step_nr)*32,
2615                      customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
2616             DrawText(mSX, mSY+(2+2*step_nr+1)*32,
2617                      "Key:", FONT_INPUT_1_ACTIVE);
2618             DrawText(mSX + 4*32, mSY+(2+2*step_nr+1)*32,
2619                      getKeyNameFromKey(*customize_step[step_nr].key),
2620                      FONT_VALUE_OLD);
2621           }
2622           break;
2623
2624         case EVENT_KEYRELEASE:
2625           key_joystick_mapping = 0;
2626           break;
2627
2628         default:
2629           HandleOtherEvents(&event);
2630           break;
2631       }
2632     }
2633
2634     DoAnimation();
2635     BackToFront();
2636
2637     /* don't eat all CPU time */
2638     Delay(10);
2639   }
2640
2641   /* write new key bindings back to player setup */
2642   setup.input[player_nr].key = custom_key;
2643
2644   StopAnimation();
2645   DrawSetupScreen_Input();
2646 }
2647
2648 static boolean CalibrateJoystickMain(int player_nr)
2649 {
2650   int new_joystick_xleft = JOYSTICK_XMIDDLE;
2651   int new_joystick_xright = JOYSTICK_XMIDDLE;
2652   int new_joystick_yupper = JOYSTICK_YMIDDLE;
2653   int new_joystick_ylower = JOYSTICK_YMIDDLE;
2654   int new_joystick_xmiddle, new_joystick_ymiddle;
2655
2656   int joystick_fd = joystick.fd[player_nr];
2657   int x, y, last_x, last_y, xpos = 8, ypos = 3;
2658   boolean check[3][3];
2659   int check_remaining = 3 * 3;
2660   int joy_x, joy_y;
2661   int joy_value;
2662   int result = -1;
2663
2664   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
2665     return FALSE;
2666
2667   if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
2668     return FALSE;
2669
2670   ClearWindow();
2671
2672   for (y = 0; y < 3; y++)
2673   {
2674     for (x = 0; x < 3; x++)
2675     {
2676       DrawGraphic(xpos + x - 1, ypos + y - 1, IMG_MENU_CALIBRATE_BLUE, 0);
2677       check[x][y] = FALSE;
2678     }
2679   }
2680
2681   DrawText(mSX,      mSY +  6 * 32, " ROTATE JOYSTICK ", FONT_TITLE_1);
2682   DrawText(mSX,      mSY +  7 * 32, "IN ALL DIRECTIONS", FONT_TITLE_1);
2683   DrawText(mSX + 16, mSY +  9 * 32, "  IF ALL BALLS  ",  FONT_TITLE_1);
2684   DrawText(mSX,      mSY + 10 * 32, "   ARE YELLOW,   ", FONT_TITLE_1);
2685   DrawText(mSX,      mSY + 11 * 32, " CENTER JOYSTICK ", FONT_TITLE_1);
2686   DrawText(mSX,      mSY + 12 * 32, "       AND       ", FONT_TITLE_1);
2687   DrawText(mSX,      mSY + 13 * 32, "PRESS ANY BUTTON!", FONT_TITLE_1);
2688
2689   joy_value = Joystick(player_nr);
2690   last_x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
2691   last_y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
2692
2693   /* eventually uncalibrated center position (joystick could be uncentered) */
2694   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
2695     return FALSE;
2696
2697   new_joystick_xmiddle = joy_x;
2698   new_joystick_ymiddle = joy_y;
2699
2700   DrawGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_RED, 0);
2701   BackToFront();
2702
2703   while (Joystick(player_nr) & JOY_BUTTON);     /* wait for released button */
2704   InitAnimation();
2705
2706   while (result < 0)
2707   {
2708     if (PendingEvent())         /* got event */
2709     {
2710       Event event;
2711
2712       NextEvent(&event);
2713
2714       switch(event.type)
2715       {
2716         case EVENT_KEYPRESS:
2717           switch(GetEventKey((KeyEvent *)&event, TRUE))
2718           {
2719             case KSYM_Return:
2720               if (check_remaining == 0)
2721                 result = 1;
2722               break;
2723
2724             case KSYM_Escape:
2725               result = 0;
2726               break;
2727
2728             default:
2729               break;
2730           }
2731           break;
2732
2733         case EVENT_KEYRELEASE:
2734           key_joystick_mapping = 0;
2735           break;
2736
2737         default:
2738           HandleOtherEvents(&event);
2739           break;
2740       }
2741     }
2742
2743     if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
2744       return FALSE;
2745
2746     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_x);
2747     new_joystick_xright = MAX(new_joystick_xright, joy_x);
2748     new_joystick_yupper = MIN(new_joystick_yupper, joy_y);
2749     new_joystick_ylower = MAX(new_joystick_ylower, joy_y);
2750
2751     setup.input[player_nr].joy.xleft = new_joystick_xleft;
2752     setup.input[player_nr].joy.yupper = new_joystick_yupper;
2753     setup.input[player_nr].joy.xright = new_joystick_xright;
2754     setup.input[player_nr].joy.ylower = new_joystick_ylower;
2755     setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
2756     setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
2757
2758     CheckJoystickData();
2759
2760     joy_value = Joystick(player_nr);
2761
2762     if (joy_value & JOY_BUTTON && check_remaining == 0)
2763       result = 1;
2764
2765     x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
2766     y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
2767
2768     if (x != last_x || y != last_y)
2769     {
2770       DrawGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_YELLOW, 0);
2771       DrawGraphic(xpos + x,      ypos + y,      IMG_MENU_CALIBRATE_RED,    0);
2772
2773       last_x = x;
2774       last_y = y;
2775
2776       if (check_remaining > 0 && !check[x+1][y+1])
2777       {
2778         check[x+1][y+1] = TRUE;
2779         check_remaining--;
2780       }
2781
2782 #if 0
2783 #ifdef DEBUG
2784       printf("LEFT / MIDDLE / RIGHT == %d / %d / %d\n",
2785              setup.input[player_nr].joy.xleft,
2786              setup.input[player_nr].joy.xmiddle,
2787              setup.input[player_nr].joy.xright);
2788       printf("UP / MIDDLE / DOWN == %d / %d / %d\n",
2789              setup.input[player_nr].joy.yupper,
2790              setup.input[player_nr].joy.ymiddle,
2791              setup.input[player_nr].joy.ylower);
2792 #endif
2793 #endif
2794
2795     }
2796
2797     DoAnimation();
2798     BackToFront();
2799
2800     /* don't eat all CPU time */
2801     Delay(10);
2802   }
2803
2804   /* calibrated center position (joystick should now be centered) */
2805   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
2806     return FALSE;
2807
2808   new_joystick_xmiddle = joy_x;
2809   new_joystick_ymiddle = joy_y;
2810
2811   StopAnimation();
2812
2813   DrawSetupScreen_Input();
2814
2815   /* wait until the last pressed button was released */
2816   while (Joystick(player_nr) & JOY_BUTTON)
2817   {
2818     if (PendingEvent())         /* got event */
2819     {
2820       Event event;
2821
2822       NextEvent(&event);
2823       HandleOtherEvents(&event);
2824
2825       Delay(10);
2826     }
2827   }
2828
2829   return TRUE;
2830 }
2831
2832 void CalibrateJoystick(int player_nr)
2833 {
2834   if (!CalibrateJoystickMain(player_nr))
2835   {
2836     ClearWindow();
2837
2838     DrawText(mSX + 16, mSY + 6 * 32, "  JOYSTICK NOT  ",  FONT_TITLE_1);
2839     DrawText(mSX,      mSY + 7 * 32, "    AVAILABLE    ", FONT_TITLE_1);
2840     BackToFront();
2841     Delay(2000);        /* show error message for two seconds */
2842   }
2843 }
2844
2845 void DrawSetupScreen()
2846 {
2847   DeactivateJoystick();
2848
2849   SetMainBackgroundImage(IMG_BACKGROUND_SETUP);
2850
2851   if (setup_mode == SETUP_MODE_INPUT)
2852     DrawSetupScreen_Input();
2853   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
2854     DrawChooseTree(&artwork.gfx_current);
2855   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
2856     DrawChooseTree(&artwork.snd_current);
2857   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
2858     DrawChooseTree(&artwork.mus_current);
2859   else
2860     DrawSetupScreen_Generic();
2861
2862   PlayMenuSound();
2863   PlayMenuMusic();
2864 }
2865
2866 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
2867 {
2868   if (setup_mode == SETUP_MODE_INPUT)
2869     HandleSetupScreen_Input(mx, my, dx, dy, button);
2870   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
2871     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
2872   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
2873     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
2874   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
2875     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
2876   else
2877     HandleSetupScreen_Generic(mx, my, dx, dy, button);
2878
2879   DoAnimation();
2880 }
2881
2882 void HandleGameActions()
2883 {
2884   if (game_status != GAME_MODE_PLAYING)
2885     return;
2886
2887   if (local_player->LevelSolved)
2888     GameWon();
2889
2890   if (AllPlayersGone && !TAPE_IS_STOPPED(tape))
2891     TapeStop();
2892
2893   GameActions();
2894   BackToFront();
2895
2896   if (tape.auto_play && !tape.playing)
2897     AutoPlayTape();     /* continue automatically playing next tape */
2898 }
2899
2900 /* ---------- new screen button stuff -------------------------------------- */
2901
2902 /* graphic position and size values for buttons and scrollbars */
2903 #define SC_SCROLLBUTTON_XSIZE           TILEX
2904 #define SC_SCROLLBUTTON_YSIZE           TILEY
2905
2906 #define SC_SCROLL_VERTICAL_XSIZE        SC_SCROLLBUTTON_XSIZE
2907 #define SC_SCROLL_VERTICAL_YSIZE        ((MAX_MENU_ENTRIES_ON_SCREEN - 2) * \
2908                                          SC_SCROLLBUTTON_YSIZE)
2909 #define SC_SCROLL_UP_XPOS               (SXSIZE - SC_SCROLLBUTTON_XSIZE)
2910 #define SC_SCROLL_UP_YPOS               (2 * SC_SCROLLBUTTON_YSIZE)
2911 #define SC_SCROLL_VERTICAL_XPOS         SC_SCROLL_UP_XPOS
2912 #define SC_SCROLL_VERTICAL_YPOS         (SC_SCROLL_UP_YPOS + \
2913                                          SC_SCROLLBUTTON_YSIZE)
2914 #define SC_SCROLL_DOWN_XPOS             SC_SCROLL_UP_XPOS
2915 #define SC_SCROLL_DOWN_YPOS             (SC_SCROLL_VERTICAL_YPOS + \
2916                                          SC_SCROLL_VERTICAL_YSIZE)
2917
2918 #define SC_BORDER_SIZE                  14
2919
2920 static struct
2921 {
2922   int gfx_unpressed, gfx_pressed;
2923   int x, y;
2924   int gadget_id;
2925   char *infotext;
2926 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
2927 {
2928   {
2929     IMG_MENU_BUTTON_UP, IMG_MENU_BUTTON_UP_ACTIVE,
2930     SC_SCROLL_UP_XPOS, SC_SCROLL_UP_YPOS,
2931     SCREEN_CTRL_ID_SCROLL_UP,
2932     "scroll up"
2933   },
2934   {
2935     IMG_MENU_BUTTON_DOWN, IMG_MENU_BUTTON_DOWN_ACTIVE,
2936     SC_SCROLL_DOWN_XPOS, SC_SCROLL_DOWN_YPOS,
2937     SCREEN_CTRL_ID_SCROLL_DOWN,
2938     "scroll down"
2939   }
2940 };
2941
2942 static struct
2943 {
2944 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2945   Bitmap **gfx_unpressed, **gfx_pressed;
2946 #else
2947   int gfx_unpressed, gfx_pressed;
2948 #endif
2949   int x, y;
2950   int width, height;
2951   int type;
2952   int gadget_id;
2953   char *infotext;
2954 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
2955 {
2956   {
2957 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2958     &scrollbar_bitmap[0], &scrollbar_bitmap[1],
2959 #else
2960     IMG_MENU_SCROLLBAR, IMG_MENU_SCROLLBAR_ACTIVE,
2961 #endif
2962     SC_SCROLL_VERTICAL_XPOS, SC_SCROLL_VERTICAL_YPOS,
2963     SC_SCROLL_VERTICAL_XSIZE, SC_SCROLL_VERTICAL_YSIZE,
2964     GD_TYPE_SCROLLBAR_VERTICAL,
2965     SCREEN_CTRL_ID_SCROLL_VERTICAL,
2966     "scroll level series vertically"
2967   }
2968 };
2969
2970 static void CreateScreenScrollbuttons()
2971 {
2972   struct GadgetInfo *gi;
2973   unsigned long event_mask;
2974   int i;
2975
2976   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
2977   {
2978     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
2979     int gfx_unpressed, gfx_pressed;
2980     int x, y, width, height;
2981     int gd_x1, gd_x2, gd_y1, gd_y2;
2982     int id = scrollbutton_info[i].gadget_id;
2983
2984     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
2985
2986     x = mSX + scrollbutton_info[i].x + menu.scrollbar_xoffset;
2987     y = mSY + scrollbutton_info[i].y;
2988     width = SC_SCROLLBUTTON_XSIZE;
2989     height = SC_SCROLLBUTTON_YSIZE;
2990
2991     if (id == SCREEN_CTRL_ID_SCROLL_DOWN)
2992       y = mSY + (SC_SCROLL_VERTICAL_YPOS +
2993                  (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE);
2994
2995     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
2996     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
2997     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
2998     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
2999     gd_x1 = graphic_info[gfx_unpressed].src_x;
3000     gd_y1 = graphic_info[gfx_unpressed].src_y;
3001     gd_x2 = graphic_info[gfx_pressed].src_x;
3002     gd_y2 = graphic_info[gfx_pressed].src_y;
3003
3004     gi = CreateGadget(GDI_CUSTOM_ID, id,
3005                       GDI_CUSTOM_TYPE_ID, i,
3006                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
3007                       GDI_X, x,
3008                       GDI_Y, y,
3009                       GDI_WIDTH, width,
3010                       GDI_HEIGHT, height,
3011                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
3012                       GDI_STATE, GD_BUTTON_UNPRESSED,
3013                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
3014                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
3015                       GDI_DIRECT_DRAW, FALSE,
3016                       GDI_EVENT_MASK, event_mask,
3017                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
3018                       GDI_END);
3019
3020     if (gi == NULL)
3021       Error(ERR_EXIT, "cannot create gadget");
3022
3023     screen_gadget[id] = gi;
3024   }
3025 }
3026
3027 static void CreateScreenScrollbars()
3028 {
3029   int i;
3030
3031   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
3032   {
3033     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
3034 #if !defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
3035     int gfx_unpressed, gfx_pressed;
3036 #endif
3037     int x, y, width, height;
3038     int gd_x1, gd_x2, gd_y1, gd_y2;
3039     struct GadgetInfo *gi;
3040     int items_max, items_visible, item_position;
3041     unsigned long event_mask;
3042     int num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
3043     int id = scrollbar_info[i].gadget_id;
3044
3045     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
3046
3047     x = mSX + scrollbar_info[i].x + menu.scrollbar_xoffset;
3048     y = mSY + scrollbar_info[i].y;
3049     width  = scrollbar_info[i].width;
3050     height = scrollbar_info[i].height;
3051
3052     if (id == SCREEN_CTRL_ID_SCROLL_VERTICAL)
3053       height = (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE;
3054
3055     items_max = num_page_entries;
3056     items_visible = num_page_entries;
3057     item_position = 0;
3058
3059 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
3060     gd_bitmap_unpressed = *scrollbar_info[i].gfx_unpressed;
3061     gd_bitmap_pressed   = *scrollbar_info[i].gfx_pressed;
3062     gd_x1 = 0;
3063     gd_y1 = 0;
3064     gd_x2 = 0;
3065     gd_y2 = 0;
3066 #else
3067     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
3068     gfx_pressed   = scrollbar_info[i].gfx_pressed;
3069     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
3070     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
3071     gd_x1 = graphic_info[gfx_unpressed].src_x;
3072     gd_y1 = graphic_info[gfx_unpressed].src_y;
3073     gd_x2 = graphic_info[gfx_pressed].src_x;
3074     gd_y2 = graphic_info[gfx_pressed].src_y;
3075 #endif
3076
3077     gi = CreateGadget(GDI_CUSTOM_ID, id,
3078                       GDI_CUSTOM_TYPE_ID, i,
3079                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
3080                       GDI_X, x,
3081                       GDI_Y, y,
3082                       GDI_WIDTH, width,
3083                       GDI_HEIGHT, height,
3084                       GDI_TYPE, scrollbar_info[i].type,
3085                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
3086                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
3087                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
3088                       GDI_STATE, GD_BUTTON_UNPRESSED,
3089                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
3090                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
3091                       GDI_BORDER_SIZE, SC_BORDER_SIZE, SC_BORDER_SIZE,
3092                       GDI_DIRECT_DRAW, FALSE,
3093                       GDI_EVENT_MASK, event_mask,
3094                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
3095                       GDI_END);
3096
3097     if (gi == NULL)
3098       Error(ERR_EXIT, "cannot create gadget");
3099
3100     screen_gadget[id] = gi;
3101   }
3102 }
3103
3104 void CreateScreenGadgets()
3105 {
3106   int last_game_status = game_status;   /* save current game status */
3107
3108 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
3109   int i;
3110
3111   for (i = 0; i < NUM_SCROLLBAR_BITMAPS; i++)
3112   {
3113     scrollbar_bitmap[i] = CreateBitmap(TILEX, TILEY, DEFAULT_DEPTH);
3114
3115     /* copy pointers to clip mask and GC */
3116     scrollbar_bitmap[i]->clip_mask =
3117       graphic_info[IMG_MENU_SCROLLBAR + i].clip_mask;
3118     scrollbar_bitmap[i]->stored_clip_gc =
3119       graphic_info[IMG_MENU_SCROLLBAR + i].clip_gc;
3120
3121     BlitBitmap(graphic_info[IMG_MENU_SCROLLBAR + i].bitmap,
3122                scrollbar_bitmap[i],
3123                graphic_info[IMG_MENU_SCROLLBAR + i].src_x,
3124                graphic_info[IMG_MENU_SCROLLBAR + i].src_y,
3125                TILEX, TILEY, 0, 0);
3126   }
3127 #endif
3128
3129   /* force LEVELS draw offset for scrollbar / scrollbutton gadgets */
3130   game_status = GAME_MODE_LEVELS;
3131
3132   CreateScreenScrollbuttons();
3133   CreateScreenScrollbars();
3134
3135   game_status = last_game_status;       /* restore current game status */
3136 }
3137
3138 void FreeScreenGadgets()
3139 {
3140   int i;
3141
3142 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
3143   for (i = 0; i < NUM_SCROLLBAR_BITMAPS; i++)
3144   {
3145     /* prevent freeing clip mask and GC twice */
3146     scrollbar_bitmap[i]->clip_mask = None;
3147     scrollbar_bitmap[i]->stored_clip_gc = None;
3148
3149     FreeBitmap(scrollbar_bitmap[i]);
3150   }
3151 #endif
3152
3153   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
3154     FreeGadget(screen_gadget[i]);
3155 }
3156
3157 void MapChooseTreeGadgets(TreeInfo *ti)
3158 {
3159   int num_entries = numTreeInfoInGroup(ti);
3160   int i;
3161
3162   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
3163     return;
3164
3165   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
3166     MapGadget(screen_gadget[i]);
3167 }
3168
3169 #if 0
3170 void UnmapChooseTreeGadgets()
3171 {
3172   int i;
3173
3174   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
3175     UnmapGadget(screen_gadget[i]);
3176 }
3177 #endif
3178
3179 static void HandleScreenGadgets(struct GadgetInfo *gi)
3180 {
3181   int id = gi->custom_id;
3182
3183   if (game_status != GAME_MODE_LEVELS && game_status != GAME_MODE_SETUP)
3184     return;
3185
3186   switch (id)
3187   {
3188     case SCREEN_CTRL_ID_SCROLL_UP:
3189       if (game_status == GAME_MODE_LEVELS)
3190         HandleChooseLevel(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
3191       else if (game_status == GAME_MODE_SETUP)
3192         HandleSetupScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
3193       break;
3194
3195     case SCREEN_CTRL_ID_SCROLL_DOWN:
3196       if (game_status == GAME_MODE_LEVELS)
3197         HandleChooseLevel(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
3198       else if (game_status == GAME_MODE_SETUP)
3199         HandleSetupScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
3200       break;
3201
3202     case SCREEN_CTRL_ID_SCROLL_VERTICAL:
3203       if (game_status == GAME_MODE_LEVELS)
3204         HandleChooseLevel(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
3205       else if (game_status == GAME_MODE_SETUP)
3206         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
3207       break;
3208
3209     default:
3210       break;
3211   }
3212 }