rnd-20100712-1-src
[rocksndiamonds.git] / src / screens.c
1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back!               *
3 *----------------------------------------------------------*
4 * (c) 1995-2006 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 #include "config.h"
27
28 /* screens on the info screen */
29 #define INFO_MODE_MAIN                  0
30 #define INFO_MODE_TITLE                 1
31 #define INFO_MODE_ELEMENTS              2
32 #define INFO_MODE_MUSIC                 3
33 #define INFO_MODE_CREDITS               4
34 #define INFO_MODE_PROGRAM               5
35 #define INFO_MODE_VERSION               6
36 #define INFO_MODE_LEVELSET              7
37
38 #define MAX_INFO_MODES                  8
39
40 /* screens on the setup screen */
41 #define SETUP_MODE_MAIN                 0
42 #define SETUP_MODE_GAME                 1
43 #define SETUP_MODE_EDITOR               2
44 #define SETUP_MODE_GRAPHICS             3
45 #define SETUP_MODE_SOUND                4
46 #define SETUP_MODE_ARTWORK              5
47 #define SETUP_MODE_INPUT                6
48 #define SETUP_MODE_SHORTCUTS            7
49 #define SETUP_MODE_SHORTCUTS_1          8
50 #define SETUP_MODE_SHORTCUTS_2          9
51 #define SETUP_MODE_SHORTCUTS_3          10
52 #define SETUP_MODE_SHORTCUTS_4          11
53 #define SETUP_MODE_SHORTCUTS_5          12
54
55 /* sub-screens on the setup screen (generic) */
56 #define SETUP_MODE_CHOOSE_ARTWORK       13
57 #define SETUP_MODE_CHOOSE_OTHER         14
58
59 /* sub-screens on the setup screen (specific) */
60 #define SETUP_MODE_CHOOSE_GAME_SPEED    15
61 #define SETUP_MODE_CHOOSE_SCREEN_MODE   16
62 #define SETUP_MODE_CHOOSE_SCROLL_DELAY  17
63 #define SETUP_MODE_CHOOSE_GRAPHICS      18
64 #define SETUP_MODE_CHOOSE_SOUNDS        19
65 #define SETUP_MODE_CHOOSE_MUSIC         20
66
67 #define MAX_SETUP_MODES                 21
68
69 /* for input setup functions */
70 #define SETUPINPUT_SCREEN_POS_START     0
71 #define SETUPINPUT_SCREEN_POS_END       (SCR_FIELDY - 4)
72 #define SETUPINPUT_SCREEN_POS_EMPTY1    (SETUPINPUT_SCREEN_POS_START + 3)
73 #define SETUPINPUT_SCREEN_POS_EMPTY2    (SETUPINPUT_SCREEN_POS_END - 1)
74
75 /* for various menu stuff  */
76 #define MENU_SCREEN_START_XPOS          1
77 #define MENU_SCREEN_START_YPOS          2
78 #define MENU_SCREEN_VALUE_XPOS          14
79 #define MENU_SCREEN_MAX_XPOS            (SCR_FIELDX - 1)
80 #define MENU_TITLE1_YPOS                8
81 #define MENU_TITLE2_YPOS                46
82 #define MAX_INFO_ELEMENTS_ON_SCREEN     10
83 #define MAX_MENU_ENTRIES_ON_SCREEN      (SCR_FIELDY - MENU_SCREEN_START_YPOS)
84 #define MAX_MENU_TEXT_LENGTH_BIG        (MENU_SCREEN_VALUE_XPOS -       \
85                                          MENU_SCREEN_START_XPOS)
86 #define MAX_MENU_TEXT_LENGTH_MEDIUM     (MAX_MENU_TEXT_LENGTH_BIG * 2)
87
88 /* buttons and scrollbars identifiers */
89 #define SCREEN_CTRL_ID_PREV_LEVEL       0
90 #define SCREEN_CTRL_ID_NEXT_LEVEL       1
91 #define SCREEN_CTRL_ID_PREV_PLAYER      2
92 #define SCREEN_CTRL_ID_NEXT_PLAYER      3
93 #define SCREEN_CTRL_ID_SCROLL_UP        4
94 #define SCREEN_CTRL_ID_SCROLL_DOWN      5
95 #define SCREEN_CTRL_ID_SCROLL_VERTICAL  6
96
97 #define NUM_SCREEN_GADGETS              7
98
99 #define NUM_SCREEN_MENUBUTTONS          4
100 #define NUM_SCREEN_SCROLLBUTTONS        2
101 #define NUM_SCREEN_SCROLLBARS           1
102
103 #define SCREEN_MASK_MAIN                (1 << 0)
104 #define SCREEN_MASK_INPUT               (1 << 1)
105
106 /* graphic position and size values for buttons and scrollbars */
107 #define SC_MENUBUTTON_XSIZE             TILEX
108 #define SC_MENUBUTTON_YSIZE             TILEY
109
110 #define SC_SCROLLBUTTON_XSIZE           TILEX
111 #define SC_SCROLLBUTTON_YSIZE           TILEY
112
113 #define SC_SCROLLBAR_XPOS               (SXSIZE - SC_SCROLLBUTTON_XSIZE)
114
115 #define SC_SCROLL_VERTICAL_XSIZE        SC_SCROLLBUTTON_XSIZE
116 #define SC_SCROLL_VERTICAL_YSIZE        ((MAX_MENU_ENTRIES_ON_SCREEN - 2) * \
117                                          SC_SCROLLBUTTON_YSIZE)
118
119 #define SC_SCROLL_UP_XPOS               SC_SCROLLBAR_XPOS
120 #define SC_SCROLL_UP_YPOS               (2 * SC_SCROLLBUTTON_YSIZE)
121
122 #define SC_SCROLL_VERTICAL_XPOS         SC_SCROLLBAR_XPOS
123 #define SC_SCROLL_VERTICAL_YPOS         (SC_SCROLL_UP_YPOS + \
124                                          SC_SCROLLBUTTON_YSIZE)
125
126 #define SC_SCROLL_DOWN_XPOS             SC_SCROLLBAR_XPOS
127 #define SC_SCROLL_DOWN_YPOS             (SC_SCROLL_VERTICAL_YPOS + \
128                                          SC_SCROLL_VERTICAL_YSIZE)
129
130 #define SC_BORDER_SIZE                  14
131
132
133 /* forward declarations of internal functions */
134 static void HandleScreenGadgets(struct GadgetInfo *);
135 static void HandleSetupScreen_Generic(int, int, int, int, int);
136 static void HandleSetupScreen_Input(int, int, int, int, int);
137 static void CustomizeKeyboard(int);
138 static void CalibrateJoystick(int);
139 static void execSetupGame(void);
140 static void execSetupGraphics(void);
141 static void execSetupArtwork(void);
142 static void HandleChooseTree(int, int, int, int, int, TreeInfo **);
143
144 static void DrawChooseLevelSet(void);
145 static void DrawChooseLevelNr(void);
146 static void DrawInfoScreen(void);
147 static void DrawAndFadeInInfoScreen(int);
148 static void DrawSetupScreen(void);
149
150 static void DrawInfoScreenExt(int, int);
151 static void DrawInfoScreen_NotAvailable(char *, char *);
152 static void DrawInfoScreen_HelpAnim(int, int, boolean);
153 static void DrawInfoScreen_HelpText(int, int, int, int);
154 static void HandleInfoScreen_Main(int, int, int, int, int);
155 static void HandleInfoScreen_TitleScreen(int);
156 static void HandleInfoScreen_Elements(int);
157 static void HandleInfoScreen_Music(int);
158 static void HandleInfoScreen_Credits(int);
159 static void HandleInfoScreen_Program(int);
160 static void HandleInfoScreen_Version(int);
161
162 static void MapScreenMenuGadgets(int);
163 static void MapScreenTreeGadgets(TreeInfo *);
164
165 static struct GadgetInfo *screen_gadget[NUM_SCREEN_GADGETS];
166
167 static int info_mode = INFO_MODE_MAIN;
168 static int setup_mode = SETUP_MODE_MAIN;
169
170 static TreeInfo *screen_modes = NULL;
171 static TreeInfo *screen_mode_current = NULL;
172
173 static TreeInfo *scroll_delays = NULL;
174 static TreeInfo *scroll_delay_current = NULL;
175
176 static TreeInfo *game_speeds = NULL;
177 static TreeInfo *game_speed_current = NULL;
178
179 static TreeInfo *level_number = NULL;
180 static TreeInfo *level_number_current = NULL;
181
182 static struct
183 {
184   int value;
185   char *text;
186 } game_speeds_list[] =
187 {
188 #if 1
189   {     30,     "Very Slow"                     },
190   {     25,     "Slow"                          },
191   {     20,     "Normal"                        },
192   {     15,     "Fast"                          },
193   {     10,     "Very Fast"                     },
194 #else
195   {     1000,   "1/1s (Extremely Slow)"         },
196   {     500,    "1/2s"                          },
197   {     200,    "1/5s"                          },
198   {     100,    "1/10s"                         },
199   {     50,     "1/20s"                         },
200   {     29,     "1/35s (Original Supaplex)"     },
201   {     25,     "1/40s"                         },
202   {     20,     "1/50s (Normal Speed)"          },
203   {     14,     "1/70s (Maximum Supaplex)"      },
204   {     10,     "1/100s"                        },
205   {     5,      "1/200s"                        },
206   {     2,      "1/500s"                        },
207   {     1,      "1/1000s (Extremely Fast)"      },
208 #endif
209
210   {     -1,     NULL                            },
211 };
212
213 static struct
214 {
215   int value;
216   char *text;
217 } scroll_delays_list[] =
218 {
219   {     0,      "0 Tiles (No Scroll Delay)"     },
220   {     1,      "1 Tile"                        },
221   {     2,      "2 Tiles"                       },
222   {     3,      "3 Tiles (Default)"             },
223   {     4,      "4 Tiles"                       },
224   {     5,      "5 Tiles"                       },
225   {     6,      "6 Tiles"                       },
226   {     7,      "7 Tiles"                       },
227   {     8,      "8 Tiles (Maximum Scroll Delay)"},
228
229   {     -1,     NULL                            },
230 };
231
232 #define DRAW_MODE(s)            ((s) >= GAME_MODE_MAIN &&               \
233                                  (s) <= GAME_MODE_SETUP ? (s) :         \
234                                  (s) == GAME_MODE_PSEUDO_TYPENAME ?     \
235                                  GAME_MODE_MAIN : GAME_MODE_DEFAULT)
236
237 /* (there are no draw offset definitions needed for INFO_MODE_TITLE) */
238 #define DRAW_MODE_INFO(i)       ((i) >= INFO_MODE_ELEMENTS &&           \
239                                  (i) <= INFO_MODE_LEVELSET ? (i) :      \
240                                  INFO_MODE_MAIN)
241
242 #define DRAW_MODE_SETUP(i)      ((i) >= SETUP_MODE_MAIN &&              \
243                                  (i) <= SETUP_MODE_SHORTCUTS_5 ? (i) :  \
244                                  (i) >= SETUP_MODE_CHOOSE_GRAPHICS &&   \
245                                  (i) <= SETUP_MODE_CHOOSE_MUSIC ?       \
246                                  SETUP_MODE_CHOOSE_ARTWORK :            \
247                                  SETUP_MODE_CHOOSE_OTHER)
248
249 #define DRAW_XOFFSET_INFO(i)    (DRAW_MODE_INFO(i) == INFO_MODE_MAIN ?  \
250                                  menu.draw_xoffset[GAME_MODE_INFO] :    \
251                                  menu.draw_xoffset_info[DRAW_MODE_INFO(i)])
252 #define DRAW_YOFFSET_INFO(i)    (DRAW_MODE_INFO(i) == INFO_MODE_MAIN ?  \
253                                  menu.draw_yoffset[GAME_MODE_INFO] :    \
254                                  menu.draw_yoffset_info[DRAW_MODE_INFO(i)])
255
256 #define DRAW_XOFFSET_SETUP(i)   (DRAW_MODE_SETUP(i) == SETUP_MODE_MAIN ? \
257                                  menu.draw_xoffset[GAME_MODE_SETUP] :   \
258                                  menu.draw_xoffset_setup[DRAW_MODE_SETUP(i)])
259 #define DRAW_YOFFSET_SETUP(i)   (DRAW_MODE_SETUP(i) == SETUP_MODE_MAIN ? \
260                                  menu.draw_yoffset[GAME_MODE_SETUP] :   \
261                                  menu.draw_yoffset_setup[DRAW_MODE_SETUP(i)])
262
263 #define DRAW_XOFFSET(s)         ((s) == GAME_MODE_INFO ?                \
264                                  DRAW_XOFFSET_INFO(info_mode) :         \
265                                  (s) == GAME_MODE_SETUP ?               \
266                                  DRAW_XOFFSET_SETUP(setup_mode) :       \
267                                  menu.draw_xoffset[DRAW_MODE(s)])
268 #define DRAW_YOFFSET(s)         ((s) == GAME_MODE_INFO ?                \
269                                  DRAW_YOFFSET_INFO(info_mode) :         \
270                                  (s) == GAME_MODE_SETUP ?               \
271                                  DRAW_YOFFSET_SETUP(setup_mode) :       \
272                                  menu.draw_yoffset[DRAW_MODE(s)])
273
274 #define mSX                     (SX + DRAW_XOFFSET(game_status))
275 #define mSY                     (SY + DRAW_YOFFSET(game_status))
276
277 #define NUM_MENU_ENTRIES_ON_SCREEN (menu.list_size[game_status] > 2 ?   \
278                                     menu.list_size[game_status] :       \
279                                     MAX_MENU_ENTRIES_ON_SCREEN)
280
281 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
282 #define NUM_SCROLLBAR_BITMAPS           2
283 static Bitmap *scrollbar_bitmap[NUM_SCROLLBAR_BITMAPS];
284 #endif
285
286
287 /* title display and control definitions */
288
289 #define MAX_NUM_TITLE_SCREENS   (2 * MAX_NUM_TITLE_IMAGES +             \
290                                  2 * MAX_NUM_TITLE_MESSAGES)
291
292 static int num_title_screens = 0;
293
294 struct TitleControlInfo
295 {
296   boolean is_image;
297   boolean initial;
298   int local_nr;
299   int sort_priority;
300 };
301
302 struct TitleControlInfo title_controls[MAX_NUM_TITLE_SCREENS];
303
304 /* main menu display and control definitions */
305
306 #define MAIN_CONTROL_NAME                       0
307 #define MAIN_CONTROL_LEVELS                     1
308 #define MAIN_CONTROL_SCORES                     2
309 #define MAIN_CONTROL_EDITOR                     3
310 #define MAIN_CONTROL_INFO                       4
311 #define MAIN_CONTROL_GAME                       5
312 #define MAIN_CONTROL_SETUP                      6
313 #define MAIN_CONTROL_QUIT                       7
314 #define MAIN_CONTROL_PREV_LEVEL                 8
315 #define MAIN_CONTROL_NEXT_LEVEL                 9
316 #define MAIN_CONTROL_FIRST_LEVEL                10
317 #define MAIN_CONTROL_LAST_LEVEL                 11
318 #define MAIN_CONTROL_LEVEL_NUMBER               12
319 #define MAIN_CONTROL_LEVEL_INFO_1               13
320 #define MAIN_CONTROL_LEVEL_INFO_2               14
321 #define MAIN_CONTROL_LEVEL_NAME                 15
322 #define MAIN_CONTROL_LEVEL_AUTHOR               16
323 #define MAIN_CONTROL_LEVEL_YEAR                 17
324 #define MAIN_CONTROL_LEVEL_IMPORTED_FROM        18
325 #define MAIN_CONTROL_LEVEL_IMPORTED_BY          19
326 #define MAIN_CONTROL_LEVEL_TESTED_BY            20
327 #define MAIN_CONTROL_TITLE_1                    21
328 #define MAIN_CONTROL_TITLE_2                    22
329 #define MAIN_CONTROL_TITLE_3                    23
330
331 static char str_main_text_name[10];
332 static char str_main_text_first_level[10];
333 static char str_main_text_last_level[10];
334 static char str_main_text_level_number[10];
335
336 static char *main_text_name                     = str_main_text_name;
337 static char *main_text_first_level              = str_main_text_first_level;
338 static char *main_text_last_level               = str_main_text_last_level;
339 static char *main_text_level_number             = str_main_text_level_number;
340 static char *main_text_levels                   = "Levelset";
341 static char *main_text_scores                   = "Hall Of Fame";
342 static char *main_text_editor                   = "Level Creator";
343 static char *main_text_info                     = "Info Screen";
344 static char *main_text_game                     = "Start Game";
345 static char *main_text_setup                    = "Setup";
346 static char *main_text_quit                     = "Quit";
347 static char *main_text_level_name               = level.name;
348 static char *main_text_level_author             = level.author;
349 static char *main_text_level_year               = NULL;
350 static char *main_text_level_imported_from      = NULL;
351 static char *main_text_level_imported_by        = NULL;
352 static char *main_text_level_tested_by          = NULL;
353 static char *main_text_title_1                  = PROGRAM_TITLE_STRING;
354 static char *main_text_title_2                  = PROGRAM_COPYRIGHT_STRING;
355 static char *main_text_title_3                  = PROGRAM_GAME_BY_STRING;
356
357 struct MainControlInfo
358 {
359   int nr;
360
361   struct MenuPosInfo *pos_button;
362   int button_graphic;
363
364   struct TextPosInfo *pos_text;
365   char **text;
366
367   struct TextPosInfo *pos_input;
368   char **input;
369 };
370
371 static struct MainControlInfo main_controls[] =
372 {
373   {
374     MAIN_CONTROL_NAME,
375     &menu.main.button.name,             IMG_MENU_BUTTON_NAME,
376     &menu.main.text.name,               &main_text_name,
377     &menu.main.input.name,              &setup.player_name,
378   },
379   {
380     MAIN_CONTROL_LEVELS,
381     &menu.main.button.levels,           IMG_MENU_BUTTON_LEVELS,
382     &menu.main.text.levels,             &main_text_levels,
383     NULL,                               NULL,
384   },
385   {
386     MAIN_CONTROL_SCORES,
387     &menu.main.button.scores,           IMG_MENU_BUTTON_SCORES,
388     &menu.main.text.scores,             &main_text_scores,
389     NULL,                               NULL,
390   },
391   {
392     MAIN_CONTROL_EDITOR,
393     &menu.main.button.editor,           IMG_MENU_BUTTON_EDITOR,
394     &menu.main.text.editor,             &main_text_editor,
395     NULL,                               NULL,
396   },
397   {
398     MAIN_CONTROL_INFO,
399     &menu.main.button.info,             IMG_MENU_BUTTON_INFO,
400     &menu.main.text.info,               &main_text_info,
401     NULL,                               NULL,
402   },
403   {
404     MAIN_CONTROL_GAME,
405     &menu.main.button.game,             IMG_MENU_BUTTON_GAME,
406     &menu.main.text.game,               &main_text_game,
407     NULL,                               NULL,
408   },
409   {
410     MAIN_CONTROL_SETUP,
411     &menu.main.button.setup,            IMG_MENU_BUTTON_SETUP,
412     &menu.main.text.setup,              &main_text_setup,
413     NULL,                               NULL,
414   },
415   {
416     MAIN_CONTROL_QUIT,
417     &menu.main.button.quit,             IMG_MENU_BUTTON_QUIT,
418     &menu.main.text.quit,               &main_text_quit,
419     NULL,                               NULL,
420   },
421 #if 0
422   /* (these two buttons are real gadgets) */
423   {
424     MAIN_CONTROL_PREV_LEVEL,
425     &menu.main.button.prev_level,       IMG_MENU_BUTTON_PREV_LEVEL,
426     NULL,                               NULL,
427     NULL,                               NULL,
428   },
429   {
430     MAIN_CONTROL_NEXT_LEVEL,
431     &menu.main.button.next_level,       IMG_MENU_BUTTON_NEXT_LEVEL,
432     NULL,                               NULL,
433     NULL,                               NULL,
434   },
435 #endif
436   {
437     MAIN_CONTROL_FIRST_LEVEL,
438     NULL,                               -1,
439     &menu.main.text.first_level,        &main_text_first_level,
440     NULL,                               NULL,
441   },
442   {
443     MAIN_CONTROL_LAST_LEVEL,
444     NULL,                               -1,
445     &menu.main.text.last_level,         &main_text_last_level,
446     NULL,                               NULL,
447   },
448   {
449     MAIN_CONTROL_LEVEL_NUMBER,
450     NULL,                               -1,
451     &menu.main.text.level_number,       &main_text_level_number,
452     NULL,                               NULL,
453   },
454   {
455     MAIN_CONTROL_LEVEL_INFO_1,
456     NULL,                               -1,
457     &menu.main.text.level_info_1,       NULL,
458     NULL,                               NULL,
459   },
460   {
461     MAIN_CONTROL_LEVEL_INFO_2,
462     NULL,                               -1,
463     &menu.main.text.level_info_2,       NULL,
464     NULL,                               NULL,
465   },
466   {
467     MAIN_CONTROL_LEVEL_NAME,
468     NULL,                               -1,
469     &menu.main.text.level_name,         &main_text_level_name,
470     NULL,                               NULL,
471   },
472   {
473     MAIN_CONTROL_LEVEL_AUTHOR,
474     NULL,                               -1,
475     &menu.main.text.level_author,       &main_text_level_author,
476     NULL,                               NULL,
477   },
478   {
479     MAIN_CONTROL_LEVEL_YEAR,
480     NULL,                               -1,
481     &menu.main.text.level_year,         &main_text_level_year,
482     NULL,                               NULL,
483   },
484   {
485     MAIN_CONTROL_LEVEL_IMPORTED_FROM,
486     NULL,                               -1,
487     &menu.main.text.level_imported_from, &main_text_level_imported_from,
488     NULL,                               NULL,
489   },
490   {
491     MAIN_CONTROL_LEVEL_IMPORTED_BY,
492     NULL,                               -1,
493     &menu.main.text.level_imported_by,  &main_text_level_imported_by,
494     NULL,                               NULL,
495   },
496   {
497     MAIN_CONTROL_LEVEL_TESTED_BY,
498     NULL,                               -1,
499     &menu.main.text.level_tested_by,    &main_text_level_tested_by,
500     NULL,                               NULL,
501   },
502   {
503     MAIN_CONTROL_TITLE_1,
504     NULL,                               -1,
505     &menu.main.text.title_1,            &main_text_title_1,
506     NULL,                               NULL,
507   },
508   {
509     MAIN_CONTROL_TITLE_2,
510     NULL,                               -1,
511     &menu.main.text.title_2,            &main_text_title_2,
512     NULL,                               NULL,
513   },
514   {
515     MAIN_CONTROL_TITLE_3,
516     NULL,                               -1,
517     &menu.main.text.title_3,            &main_text_title_3,
518     NULL,                               NULL,
519   },
520
521   {
522     -1,
523     NULL,                               -1,
524     NULL,                               NULL,
525     NULL,                               NULL,
526   }
527 };
528
529
530 static int getTitleScreenGraphic(int nr, boolean initial)
531 {
532   return (initial ? IMG_TITLESCREEN_INITIAL_1 : IMG_TITLESCREEN_1) + nr;
533 }
534
535 static struct TitleMessageInfo *getTitleMessageInfo(int nr, boolean initial)
536 {
537   return (initial ? &titlemessage_initial[nr] : &titlemessage[nr]);
538 }
539
540 #if 0
541 static int getTitleScreenGameMode(boolean initial)
542 {
543   return (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
544 }
545 #endif
546
547 static int getTitleMessageGameMode(boolean initial)
548 {
549   return (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
550 }
551
552 #if 0
553 static int getTitleScreenBackground(boolean initial)
554 {
555   return (initial ? IMG_BACKGROUND_TITLE_INITIAL : IMG_BACKGROUND_TITLE);
556 }
557 #endif
558
559 #if 0
560 static int getTitleMessageBackground(int nr, boolean initial)
561 {
562   return (initial ? IMG_BACKGROUND_TITLE_INITIAL : IMG_BACKGROUND_TITLE);
563 }
564 #endif
565
566 static int getTitleBackground(int nr, boolean initial, boolean is_image)
567 {
568   int base = (is_image ?
569               (initial ? IMG_BACKGROUND_TITLESCREEN_INITIAL_1 :
570                          IMG_BACKGROUND_TITLESCREEN_1) :
571               (initial ? IMG_BACKGROUND_TITLEMESSAGE_INITIAL_1 :
572                          IMG_BACKGROUND_TITLEMESSAGE_1));
573   int graphic_global = (initial ? IMG_BACKGROUND_TITLE_INITIAL :
574                                   IMG_BACKGROUND_TITLE);
575   int graphic_local = base + nr;
576
577   if (graphic_info[graphic_local].bitmap != NULL)
578     return graphic_local;
579
580   if (graphic_info[graphic_global].bitmap != NULL)
581     return graphic_global;
582
583   return IMG_UNDEFINED;
584 }
585
586 static int getTitleSound(struct TitleControlInfo *tci)
587 {
588   boolean is_image = tci->is_image;
589   int initial = tci->initial;
590   int nr = tci->local_nr;
591   int mode = (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
592   int base = (is_image ?
593               (initial ? SND_BACKGROUND_TITLESCREEN_INITIAL_1 :
594                          SND_BACKGROUND_TITLESCREEN_1) :
595               (initial ? SND_BACKGROUND_TITLEMESSAGE_INITIAL_1 :
596                          SND_BACKGROUND_TITLEMESSAGE_1));
597   int sound_global = menu.sound[mode];
598   int sound_local = base + nr;
599
600 #if 0
601   printf("::: %d, %d, %d: %d ['%s'], %d ['%s']\n",
602          nr, initial, is_image,
603          sound_global, getSoundListEntry(sound_global)->filename,
604          sound_local, getSoundListEntry(sound_local)->filename);
605 #endif
606
607   if (!strEqual(getSoundListEntry(sound_local)->filename, UNDEFINED_FILENAME))
608     return sound_local;
609
610   if (!strEqual(getSoundListEntry(sound_global)->filename, UNDEFINED_FILENAME))
611     return sound_global;
612
613   return SND_UNDEFINED;
614 }
615
616 static int getTitleMusic(struct TitleControlInfo *tci)
617 {
618   boolean is_image = tci->is_image;
619   int initial = tci->initial;
620   int nr = tci->local_nr;
621   int mode = (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
622   int base = (is_image ?
623               (initial ? MUS_BACKGROUND_TITLESCREEN_INITIAL_1 :
624                          MUS_BACKGROUND_TITLESCREEN_1) :
625               (initial ? MUS_BACKGROUND_TITLEMESSAGE_INITIAL_1 :
626                          MUS_BACKGROUND_TITLEMESSAGE_1));
627   int music_global = menu.music[mode];
628   int music_local = base + nr;
629
630 #if 0
631   printf("::: %d, %d, %d: %d ['%s'], %d ['%s']\n",
632          nr, initial, is_image,
633          music_global, getMusicListEntry(music_global)->filename,
634          music_local, getMusicListEntry(music_local)->filename);
635 #endif
636
637   if (!strEqual(getMusicListEntry(music_local)->filename, UNDEFINED_FILENAME))
638     return music_local;
639
640   if (!strEqual(getMusicListEntry(music_global)->filename, UNDEFINED_FILENAME))
641     return music_global;
642
643   return MUS_UNDEFINED;
644 }
645
646 static struct TitleFadingInfo getTitleFading(struct TitleControlInfo *tci)
647 {
648   boolean is_image = tci->is_image;
649   int initial = tci->initial;
650   int nr = tci->local_nr;
651   struct TitleFadingInfo ti;
652
653   if (is_image)
654   {
655     int graphic = getTitleScreenGraphic(nr, initial);
656
657     /* initialize fading control values to default title config settings */
658     ti = (initial ? title_initial_default : title_default);
659
660     /* override default settings with image config settings, if defined */
661     if (graphic_info[graphic].fade_mode != FADE_MODE_DEFAULT)
662       ti.fade_mode = graphic_info[graphic].fade_mode;
663     if (graphic_info[graphic].fade_delay > -1)
664       ti.fade_delay = graphic_info[graphic].fade_delay;
665     if (graphic_info[graphic].post_delay > -1)
666       ti.post_delay = graphic_info[graphic].post_delay;
667     if (graphic_info[graphic].auto_delay > -1)
668       ti.auto_delay = graphic_info[graphic].auto_delay;
669   }
670   else
671   {
672     if (initial)
673     {
674       ti.fade_mode  = titlemessage_initial[nr].fade_mode;
675       ti.fade_delay = titlemessage_initial[nr].fade_delay;
676       ti.post_delay = titlemessage_initial[nr].post_delay;
677       ti.auto_delay = titlemessage_initial[nr].auto_delay;
678     }
679     else
680     {
681       ti.fade_mode  = titlemessage[nr].fade_mode;
682       ti.fade_delay = titlemessage[nr].fade_delay;
683       ti.post_delay = titlemessage[nr].post_delay;
684       ti.auto_delay = titlemessage[nr].auto_delay;
685     }
686   }
687
688 #if 0
689   if (ti.anim_mode == ANIM_NONE)
690     ti.fade_delay = ti.post_delay = 0;
691 #endif
692
693   return ti;
694 }
695
696 static int compareTitleControlInfo(const void *object1, const void *object2)
697 {
698   const struct TitleControlInfo *tci1 = (struct TitleControlInfo *)object1;
699   const struct TitleControlInfo *tci2 = (struct TitleControlInfo *)object2;
700   int compare_result;
701
702   if (tci1->initial != tci2->initial)
703     compare_result = (tci1->initial ? -1 : +1);
704   else if (tci1->sort_priority != tci2->sort_priority)
705     compare_result = tci1->sort_priority - tci2->sort_priority;
706   else if (tci1->is_image != tci2->is_image)
707     compare_result = (tci1->is_image ? -1 : +1);
708   else
709     compare_result = tci1->local_nr - tci2->local_nr;
710
711   return compare_result;
712 }
713
714 static void InitializeTitleControlsExt_AddTitleInfo(boolean is_image,
715                                                     boolean initial,
716                                                     int nr, int sort_priority)
717 {
718   title_controls[num_title_screens].is_image = is_image;
719   title_controls[num_title_screens].initial = initial;
720   title_controls[num_title_screens].local_nr = nr;
721   title_controls[num_title_screens].sort_priority = sort_priority;
722
723   num_title_screens++;
724 }
725
726 static void InitializeTitleControls_CheckTitleInfo(boolean initial)
727 {
728   int i;
729
730   for (i = 0; i < MAX_NUM_TITLE_IMAGES; i++)
731   {
732     int graphic = getTitleScreenGraphic(i, initial);
733     Bitmap *bitmap = graphic_info[graphic].bitmap;
734     int sort_priority = graphic_info[graphic].sort_priority;
735
736 #if 0
737     /* skip images and messages (fonts!) when using forced custom graphics */
738     if (setup.override_level_graphics && !initial)
739       continue;
740 #endif
741
742     if (bitmap != NULL)
743       InitializeTitleControlsExt_AddTitleInfo(TRUE, initial, i, sort_priority);
744   }
745
746   for (i = 0; i < MAX_NUM_TITLE_MESSAGES; i++)
747   {
748     struct TitleMessageInfo *tmi = getTitleMessageInfo(i, initial);
749     char *filename = getLevelSetTitleMessageFilename(i, initial);
750     int sort_priority = tmi->sort_priority;
751
752 #if 0
753     /* skip images and messages (fonts!) when using forced custom graphics */
754     if (setup.override_level_graphics)
755       continue;
756 #endif
757
758     if (filename != NULL)
759       InitializeTitleControlsExt_AddTitleInfo(FALSE, initial, i, sort_priority);
760   }
761 }
762
763 static void InitializeTitleControls(boolean show_title_initial)
764 {
765   num_title_screens = 0;
766
767 #if 1
768   /* 1st step: initialize title screens for game start (only when starting) */
769   if (show_title_initial)
770     InitializeTitleControls_CheckTitleInfo(TRUE);
771 #endif
772
773   /* 2nd step: initialize title screens for current level set */
774   InitializeTitleControls_CheckTitleInfo(FALSE);
775
776   /* sort title screens according to sort_priority and title number */
777   qsort(title_controls, num_title_screens, sizeof(struct TitleControlInfo),
778         compareTitleControlInfo);
779 }
780
781 static boolean visibleMenuPos(struct MenuPosInfo *pos)
782 {
783   return (pos != NULL && pos->x != -1 && pos->y != -1);
784 }
785
786 static boolean visibleTextPos(struct TextPosInfo *pos)
787 {
788   return (pos != NULL && pos->x != -1 && pos->y != -1);
789 }
790
791 static void InitializeMainControls()
792 {
793   boolean local_team_mode = (!options.network && setup.team_mode);
794   int i;
795
796   /* set main control text values to dynamically determined values */
797   sprintf(main_text_name,         "%s",   local_team_mode ? "Team:" : "Name:");
798
799   strcpy(main_text_first_level,  int2str(leveldir_current->first_level,
800                                          menu.main.text.first_level.size));
801   strcpy(main_text_last_level,   int2str(leveldir_current->last_level,
802                                          menu.main.text.last_level.size));
803   strcpy(main_text_level_number, int2str(level_nr,
804                                          menu.main.text.level_number.size));
805
806   main_text_level_year          = leveldir_current->year;
807   main_text_level_imported_from = leveldir_current->imported_from;
808   main_text_level_imported_by   = leveldir_current->imported_by;
809   main_text_level_tested_by     = leveldir_current->tested_by;
810
811   /* set main control screen positions to dynamically determined values */
812   for (i = 0; main_controls[i].nr != -1; i++)
813   {
814     struct MainControlInfo *mci = &main_controls[i];
815     int nr                         = mci->nr;
816     struct MenuPosInfo *pos_button = mci->pos_button;
817     struct TextPosInfo *pos_text   = mci->pos_text;
818     struct TextPosInfo *pos_input  = mci->pos_input;
819     char *text                     = (mci->text  ? *mci->text  : NULL);
820     char *input                    = (mci->input ? *mci->input : NULL);
821     int button_graphic             = mci->button_graphic;
822 #if 1
823     int font_text                  = (pos_text  ? pos_text->font  : -1);
824     int font_input                 = (pos_input ? pos_input->font : -1);
825 #else
826     int font_text                  = mci->font_text;
827     int font_input                 = mci->font_input;
828 #endif
829
830     int font_text_width   = (font_text  != -1 ? getFontWidth(font_text)   : 0);
831     int font_text_height  = (font_text  != -1 ? getFontHeight(font_text)  : 0);
832     int font_input_width  = (font_input != -1 ? getFontWidth(font_input)  : 0);
833     int font_input_height = (font_input != -1 ? getFontHeight(font_input) : 0);
834     int text_chars  = (text  != NULL ? strlen(text)  : 0);
835     int input_chars = (input != NULL ? strlen(input) : 0);
836
837     int button_width =
838       (button_graphic != -1 ? graphic_info[button_graphic].width  : 0);
839     int button_height =
840       (button_graphic != -1 ? graphic_info[button_graphic].height : 0);
841     int text_width   = font_text_width * text_chars;
842     int text_height  = font_text_height;
843     int input_width  = font_input_width * input_chars;
844     int input_height = font_input_height;
845
846     if (nr == MAIN_CONTROL_NAME)
847     {
848 #if 0
849       if (menu.main.input.name.x == -1)
850         menu.main.input.name.x = menu.main.text.name.x + text_width;
851       if (menu.main.input.name.y == -1)
852         menu.main.input.name.y = menu.main.text.name.y;
853 #endif
854
855 #if 1
856       menu.main.input.name.width  = input_width;
857       menu.main.input.name.height = input_height;
858 #else
859       menu.main.input.name.width  = font_input_width * MAX_PLAYER_NAME_LEN;
860       menu.main.input.name.height = font_input_height;
861 #endif
862     }
863
864     if (pos_button != NULL)             /* (x/y may be -1/-1 here) */
865     {
866       if (pos_button->width == 0)
867         pos_button->width = button_width;
868       if (pos_button->height == 0)
869         pos_button->height = button_height;
870     }
871
872     if (pos_text != NULL)               /* (x/y may be -1/-1 here) */
873     {
874 #if 1
875       /* calculate size for non-clickable text -- needed for text alignment */
876       boolean calculate_text_size = (pos_button == NULL && text != NULL);
877 #else
878       /* calculate width for non-clickable text -- needed for text alignment */
879       boolean calculate_text_width = (pos_button == NULL && text != NULL);
880 #endif
881
882       if (visibleMenuPos(pos_button))
883       {
884         if (pos_text->x == -1)
885           pos_text->x = pos_button->x + pos_button->width;
886         if (pos_text->y == -1)
887           pos_text->y = pos_button->y;
888       }
889
890 #if 1
891       if (pos_text->width == -1 || calculate_text_size)
892         pos_text->width = text_width;
893       if (pos_text->height == -1 || calculate_text_size)
894         pos_text->height = text_height;
895 #else
896       if (pos_text->width == -1 || calculate_text_width)
897         pos_text->width = text_width;
898       if (pos_text->height == -1)
899         pos_text->height = text_height;
900 #endif
901     }
902
903     if (pos_input != NULL)              /* (x/y may be -1/-1 here) */
904     {
905       if (visibleTextPos(pos_text))
906       {
907         if (pos_input->x == -1)
908           pos_input->x = pos_text->x + pos_text->width;
909         if (pos_input->y == -1)
910           pos_input->y = pos_text->y;
911       }
912
913       if (pos_input->width == -1)
914         pos_input->width = input_width;
915       if (pos_input->height == -1)
916         pos_input->height = input_height;
917     }
918   }
919 }
920
921 static void DrawCursorAndText_Main_Ext(int nr, boolean active_text,
922                                        boolean active_input)
923 {
924   int i;
925
926   for (i = 0; main_controls[i].nr != -1; i++)
927   {
928     struct MainControlInfo *mci = &main_controls[i];
929
930     if (mci->nr == nr || nr == -1)
931     {
932       struct MenuPosInfo *pos_button = mci->pos_button;
933       struct TextPosInfo *pos_text   = mci->pos_text;
934       struct TextPosInfo *pos_input  = mci->pos_input;
935       char *text                     = (mci->text  ? *mci->text  : NULL);
936       char *input                    = (mci->input ? *mci->input : NULL);
937       int button_graphic             = mci->button_graphic;
938 #if 1
939       int font_text                  = (pos_text  ? pos_text->font  : -1);
940       int font_input                 = (pos_input ? pos_input->font : -1);
941 #else
942       int font_text                  = mci->font_text;
943       int font_input                 = mci->font_input;
944 #endif
945
946       if (active_text)
947       {
948         button_graphic = BUTTON_ACTIVE(button_graphic);
949         font_text = FONT_ACTIVE(font_text);
950       }
951
952       if (active_input)
953       {
954         font_input = FONT_ACTIVE(font_input);
955       }
956
957       if (visibleMenuPos(pos_button))
958       {
959         struct MenuPosInfo *pos = pos_button;
960         int x = mSX + pos->x;
961         int y = mSY + pos->y;
962
963         DrawBackgroundForGraphic(x, y, pos->width, pos->height, button_graphic);
964         DrawFixedGraphicThruMaskExt(drawto, x, y, button_graphic, 0);
965       }
966
967       if (visibleTextPos(pos_text) && text != NULL)
968       {
969         struct TextPosInfo *pos = pos_text;
970         int x = mSX + ALIGNED_TEXT_XPOS(pos);
971         int y = mSY + ALIGNED_TEXT_YPOS(pos);
972
973 #if 1
974         /* (check why/if this is needed) */
975         DrawBackgroundForFont(x, y, pos->width, pos->height, font_text);
976 #endif
977         DrawText(x, y, text, font_text);
978       }
979
980       if (visibleTextPos(pos_input) && input != NULL)
981       {
982         struct TextPosInfo *pos = pos_input;
983         int x = mSX + ALIGNED_TEXT_XPOS(pos);
984         int y = mSY + ALIGNED_TEXT_YPOS(pos);
985
986 #if 1
987         /* (check why/if this is needed) */
988         DrawBackgroundForFont(x, y, pos->width, pos->height, font_input);
989 #endif
990         DrawText(x, y, input, font_input);
991       }
992     }
993   }
994 }
995
996 static void DrawCursorAndText_Main(int nr, boolean active_text)
997 {
998   DrawCursorAndText_Main_Ext(nr, active_text, FALSE);
999 }
1000
1001 #if 0
1002 static void DrawCursorAndText_Main_Input(int nr, boolean active_text)
1003 {
1004   DrawCursorAndText_Main_Ext(nr, active_text, TRUE);
1005 }
1006 #endif
1007
1008 static struct MainControlInfo *getMainControlInfo(int nr)
1009 {
1010   int i;
1011
1012   for (i = 0; main_controls[i].nr != -1; i++)
1013     if (main_controls[i].nr == nr)
1014       return &main_controls[i];
1015
1016   return NULL;
1017 }
1018
1019 static boolean insideMenuPosRect(struct MenuPosInfo *rect, int x, int y)
1020 {
1021   if (rect == NULL)
1022     return FALSE;
1023
1024   int rect_x = ALIGNED_TEXT_XPOS(rect);
1025   int rect_y = ALIGNED_TEXT_YPOS(rect);
1026
1027   return (x >= rect_x && x < rect_x + rect->width &&
1028           y >= rect_y && y < rect_y + rect->height);
1029 }
1030
1031 static boolean insideTextPosRect(struct TextPosInfo *rect, int x, int y)
1032 {
1033   if (rect == NULL)
1034     return FALSE;
1035
1036   int rect_x = ALIGNED_TEXT_XPOS(rect);
1037   int rect_y = ALIGNED_TEXT_YPOS(rect);
1038
1039 #if 0
1040   printf("::: insideTextPosRect: (%d, %d), (%d, %d) [%d, %d] (%d, %d) => %d\n",
1041          x, y, rect_x, rect_y, rect->x, rect->y, rect->width, rect->height,
1042          (x >= rect_x && x < rect_x + rect->width &&
1043           y >= rect_y && y < rect_y + rect->height));
1044 #endif
1045
1046   return (x >= rect_x && x < rect_x + rect->width &&
1047           y >= rect_y && y < rect_y + rect->height);
1048 }
1049
1050 static void drawCursorExt(int xpos, int ypos, boolean active, int graphic)
1051 {
1052 #if 1
1053   static int cursor_array[MAX_LEV_FIELDY];
1054 #else
1055   static int cursor_array[SCR_FIELDY];
1056 #endif
1057   int x = mSX + TILEX * xpos;
1058   int y = mSY + TILEY * (MENU_SCREEN_START_YPOS + ypos);
1059
1060   if (xpos == 0)
1061   {
1062     if (graphic != -1)
1063       cursor_array[ypos] = graphic;
1064     else
1065       graphic = cursor_array[ypos];
1066   }
1067
1068   if (active)
1069     graphic = BUTTON_ACTIVE(graphic);
1070
1071   DrawBackgroundForGraphic(x, y, TILEX, TILEY, graphic);
1072   DrawFixedGraphicThruMaskExt(drawto, x, y, graphic, 0);
1073 }
1074
1075 static void initCursor(int ypos, int graphic)
1076 {
1077   drawCursorExt(0, ypos, FALSE, graphic);
1078 }
1079
1080 static void drawCursor(int ypos, boolean active)
1081 {
1082   drawCursorExt(0, ypos, active, -1);
1083 }
1084
1085 static void drawCursorXY(int xpos, int ypos, int graphic)
1086 {
1087   drawCursorExt(xpos, ypos, FALSE, graphic);
1088 }
1089
1090 static void drawChooseTreeCursor(int ypos, boolean active)
1091 {
1092   int last_game_status = game_status;   /* save current game status */
1093
1094 #if 0
1095   /* force LEVELS draw offset on artwork setup screen */
1096   game_status = GAME_MODE_LEVELS;
1097 #endif
1098
1099   drawCursorExt(0, ypos, active, -1);
1100
1101   game_status = last_game_status;       /* restore current game status */
1102 }
1103
1104 void DrawHeadline()
1105 {
1106   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, PROGRAM_TITLE_STRING);
1107   DrawTextSCentered(MENU_TITLE2_YPOS, FONT_TITLE_2, PROGRAM_COPYRIGHT_STRING);
1108 }
1109
1110 #if 0
1111 static int getPrevlevelButtonPos()
1112 {
1113   return 10;
1114 }
1115
1116 static int getCurrentLevelTextPos()
1117 {
1118   return (getPrevlevelButtonPos() + 1);
1119 }
1120
1121 static int getNextLevelButtonPos()
1122 {
1123   return getPrevlevelButtonPos() + 3 + 1;
1124 }
1125
1126 static int getLevelRangeTextPos()
1127 {
1128   return getNextLevelButtonPos() + 1;
1129 }
1130 #endif
1131
1132 int effectiveGameStatus()
1133 {
1134   if (game_status == GAME_MODE_INFO && info_mode == INFO_MODE_TITLE)
1135     return GAME_MODE_TITLE;
1136
1137   return game_status;
1138 }
1139
1140 void DrawTitleScreenImage(int nr, boolean initial)
1141 {
1142   int graphic = getTitleScreenGraphic(nr, initial);
1143   Bitmap *bitmap = graphic_info[graphic].bitmap;
1144   int width  = graphic_info[graphic].width;
1145   int height = graphic_info[graphic].height;
1146   int src_x = graphic_info[graphic].src_x;
1147   int src_y = graphic_info[graphic].src_y;
1148   int dst_x, dst_y;
1149
1150   if (bitmap == NULL)
1151     return;
1152
1153   if (width > WIN_XSIZE)
1154   {
1155     /* image width too large for window => center image horizontally */
1156     src_x = (width - WIN_XSIZE) / 2;
1157     width = WIN_XSIZE;
1158   }
1159
1160   if (height > WIN_YSIZE)
1161   {
1162     /* image height too large for window => center image vertically */
1163     src_y = (height - WIN_YSIZE) / 2;
1164     height = WIN_YSIZE;
1165   }
1166
1167   /* always display title screens centered */
1168   dst_x = (WIN_XSIZE - width) / 2;
1169   dst_y = (WIN_YSIZE - height) / 2;
1170
1171   SetDrawBackgroundMask(REDRAW_ALL);
1172   SetWindowBackgroundImage(getTitleBackground(nr, initial, TRUE));
1173
1174   ClearRectangleOnBackground(drawto, 0, 0, WIN_XSIZE, WIN_YSIZE);
1175
1176   if (DrawingOnBackground(dst_x, dst_y))
1177   {
1178     SetClipOrigin(bitmap, bitmap->stored_clip_gc, dst_x - src_x, dst_y - src_y);
1179     BlitBitmapMasked(bitmap, drawto, src_x, src_y, width, height, dst_x, dst_y);
1180   }
1181   else
1182     BlitBitmap(bitmap, drawto, src_x, src_y, width, height, dst_x, dst_y);
1183
1184   redraw_mask = REDRAW_ALL;
1185 }
1186
1187 void DrawTitleScreenMessage(int nr, boolean initial)
1188 {
1189   char *filename = getLevelSetTitleMessageFilename(nr, initial);
1190   struct TitleMessageInfo *tmi = getTitleMessageInfo(nr, initial);
1191   int last_game_status = game_status;   /* save current game status */
1192
1193   if (filename == NULL)
1194     return;
1195
1196   /* force TITLE font on title message screen */
1197   game_status = getTitleMessageGameMode(initial);
1198
1199   /* if chars set to "-1", automatically determine by text and font width */
1200   if (tmi->chars == -1)
1201     tmi->chars = tmi->width / getFontWidth(tmi->font);
1202   else
1203     tmi->width = tmi->chars * getFontWidth(tmi->font);
1204
1205   /* if lines set to "-1", automatically determine by text and font height */
1206   if (tmi->lines == -1)
1207     tmi->lines = tmi->height / getFontHeight(tmi->font);
1208   else
1209     tmi->height = tmi->lines * getFontHeight(tmi->font);
1210
1211   SetDrawBackgroundMask(REDRAW_ALL);
1212   SetWindowBackgroundImage(getTitleBackground(nr, initial, FALSE));
1213
1214   ClearRectangleOnBackground(drawto, 0, 0, WIN_XSIZE, WIN_YSIZE);
1215
1216   DrawTextFile(ALIGNED_TEXT_XPOS(tmi), ALIGNED_TEXT_YPOS(tmi),
1217                filename, tmi->font, tmi->chars, -1, tmi->lines, 0, -1,
1218                tmi->autowrap, tmi->centered, tmi->parse_comments);
1219
1220   game_status = last_game_status;       /* restore current game status */
1221 }
1222
1223 void DrawTitleScreen()
1224 {
1225   KeyboardAutoRepeatOff();
1226
1227 #if 0
1228   SetMainBackgroundImage(IMG_BACKGROUND_TITLE);
1229 #endif
1230
1231   HandleTitleScreen(0, 0, 0, 0, MB_MENU_INITIALIZE);
1232
1233   StopAnimation();
1234 }
1235
1236 boolean CheckTitleScreen(boolean levelset_has_changed)
1237 {
1238   static boolean show_title_initial = TRUE;
1239   boolean show_titlescreen = FALSE;
1240
1241   /* needed to be able to skip title screen, if no image or message defined */
1242   InitializeTitleControls(show_title_initial);
1243
1244   if (setup.show_titlescreen && (show_title_initial || levelset_has_changed))
1245     show_titlescreen = TRUE;
1246
1247   /* show initial title images and messages only once at program start */
1248   show_title_initial = FALSE;
1249
1250   return (show_titlescreen && num_title_screens > 0);
1251 }
1252
1253 void DrawMainMenuExt(int fade_mask, boolean do_fading)
1254 {
1255   static LevelDirTree *leveldir_last_valid = NULL;
1256   boolean levelset_has_changed = FALSE;
1257
1258   FadeSetLeaveScreen();
1259
1260   /* do not fade out here -- function may continue and fade on editor screen */
1261
1262   UnmapAllGadgets();
1263   FadeSoundsAndMusic();
1264
1265   KeyboardAutoRepeatOn();
1266   ActivateJoystick();
1267
1268   SetDrawDeactivationMask(REDRAW_NONE);
1269   SetDrawBackgroundMask(REDRAW_FIELD);
1270
1271   audio.sound_deactivated = FALSE;
1272
1273   GetPlayerConfig();
1274
1275   /* needed if last screen was the playing screen, invoked from level editor */
1276   if (level_editor_test_game)
1277   {
1278     game_status = GAME_MODE_EDITOR;
1279     DrawLevelEd();
1280
1281     return;
1282   }
1283
1284   /* needed if last screen was the setup screen and fullscreen state changed */
1285   ToggleFullscreenIfNeeded();
1286
1287   /* leveldir_current may be invalid (level group, parent link) */
1288   if (!validLevelSeries(leveldir_current))
1289     leveldir_current = getFirstValidTreeInfoEntry(leveldir_last_valid);
1290
1291   if (leveldir_current != leveldir_last_valid)
1292     levelset_has_changed = TRUE;
1293
1294   /* store valid level series information */
1295   leveldir_last_valid = leveldir_current;
1296
1297   init_last = init;                     /* switch to new busy animation */
1298
1299   /* needed if last screen (level choice) changed graphics, sounds or music */
1300   ReloadCustomArtwork(0);
1301
1302   if (redraw_mask & REDRAW_ALL)
1303     fade_mask = REDRAW_ALL;
1304
1305 #if 1
1306   FadeOut(fade_mask);
1307
1308   /* needed if last screen was the editor screen */
1309   UndrawSpecialEditorDoor();
1310 #if 0
1311   if (fade_mask == REDRAW_FIELD)
1312     BackToFront();
1313 #endif
1314 #endif
1315
1316 #if 1
1317   /* needed if different viewport properties defined for menues */
1318   ChangeViewportPropertiesIfNeeded();
1319 #endif
1320
1321 #if defined(TARGET_SDL)
1322   SetDrawtoField(DRAW_BACKBUFFER);
1323 #endif
1324
1325   if (CheckTitleScreen(levelset_has_changed))
1326   {
1327     game_status = GAME_MODE_TITLE;
1328
1329     DrawTitleScreen();
1330
1331     return;
1332   }
1333
1334   /* level_nr may have been set to value over handicap with level editor */
1335   if (setup.handicap && level_nr > leveldir_current->handicap_level)
1336     level_nr = leveldir_current->handicap_level;
1337
1338   LoadLevel(level_nr);
1339   LoadScore(level_nr);
1340
1341   SetMainBackgroundImage(IMG_BACKGROUND_MAIN);
1342
1343 #if 1
1344   if (fade_mask == REDRAW_ALL)
1345   {
1346     // int door_state = GetDoorState();
1347
1348     RedrawBackground();
1349
1350     // OpenDoor(door_state | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
1351   }
1352 #endif
1353
1354   ClearField();
1355
1356   InitializeMainControls();
1357
1358   DrawCursorAndText_Main(-1, FALSE);
1359   DrawPreviewLevel(TRUE);
1360
1361   HandleMainMenu(0, 0, 0, 0, MB_MENU_INITIALIZE);
1362
1363   TapeStop();
1364   if (TAPE_IS_EMPTY(tape))
1365     LoadTape(level_nr);
1366   DrawCompleteVideoDisplay();
1367
1368   PlayMenuSound();
1369   PlayMenuMusic();
1370
1371   /* create gadgets for main menu screen */
1372   FreeScreenGadgets();
1373   CreateScreenGadgets();
1374
1375   /* map gadgets for main menu screen */
1376   MapTapeButtons();
1377   MapScreenMenuGadgets(SCREEN_MASK_MAIN);
1378
1379 #if 1
1380   if (fade_mask == REDRAW_ALL)
1381   {
1382     int door_state = GetDoorState();
1383
1384     // RedrawBackground();
1385
1386     OpenDoor(door_state | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
1387   }
1388 #endif
1389
1390   DrawMaskedBorder(REDRAW_ALL);
1391
1392   FadeIn(fade_mask);
1393   FadeSetEnterMenu();
1394
1395 #if 1
1396   /* update screen area with special editor door */
1397   redraw_mask |= REDRAW_ALL;
1398   BackToFront();
1399 #endif
1400
1401   SetMouseCursor(CURSOR_DEFAULT);
1402
1403   InitAnimation();
1404
1405   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
1406 }
1407
1408 void DrawAndFadeInMainMenu(int fade_mask)
1409 {
1410   DrawMainMenuExt(fade_mask, TRUE);
1411 }
1412
1413 void DrawMainMenu()
1414 {
1415   DrawMainMenuExt(REDRAW_ALL, FALSE);
1416 }
1417
1418 #if defined(CREATE_SPECIAL_EDITION_RND_JUE)
1419 static void gotoTopLevelDir()
1420 {
1421   /* move upwards to top level directory */
1422   while (leveldir_current->node_parent)
1423   {
1424     /* write a "path" into level tree for easy navigation to last level */
1425     if (leveldir_current->node_parent->node_group->cl_first == -1)
1426     {
1427       int num_leveldirs = numTreeInfoInGroup(leveldir_current);
1428       int leveldir_pos = posTreeInfo(leveldir_current);
1429       int num_page_entries;
1430       int cl_first, cl_cursor;
1431
1432       if (num_leveldirs <= NUM_MENU_ENTRIES_ON_SCREEN)
1433         num_page_entries = num_leveldirs;
1434       else
1435         num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
1436
1437       cl_first = MAX(0, leveldir_pos - num_page_entries + 1);
1438       cl_cursor = leveldir_pos - cl_first;
1439
1440       leveldir_current->node_parent->node_group->cl_first = cl_first;
1441       leveldir_current->node_parent->node_group->cl_cursor = cl_cursor;
1442     }
1443
1444     leveldir_current = leveldir_current->node_parent;
1445   }
1446 }
1447 #endif
1448
1449 void HandleTitleScreen(int mx, int my, int dx, int dy, int button)
1450 {
1451   static unsigned long title_delay = 0;
1452   static int title_screen_nr = 0;
1453   static int last_sound = -1, last_music = -1;
1454   boolean return_to_main_menu = FALSE;
1455   boolean use_fading_main_menu = TRUE;
1456   struct TitleControlInfo *tci;
1457   struct TitleFadingInfo fading_default;
1458   struct TitleFadingInfo fading_last = fading;
1459   struct TitleFadingInfo fading_next;
1460   int sound, music;
1461
1462   if (button == MB_MENU_INITIALIZE)
1463   {
1464     title_delay = 0;
1465     title_screen_nr = 0;
1466     tci = &title_controls[title_screen_nr];
1467
1468     last_sound = SND_UNDEFINED;
1469     last_music = MUS_UNDEFINED;
1470
1471     if (game_status == GAME_MODE_INFO)
1472     {
1473       if (num_title_screens == 0)
1474       {
1475         DrawInfoScreen_NotAvailable("Title screen information:",
1476                                     "No title screen for this level set.");
1477
1478         return;
1479       }
1480
1481       FadeSoundsAndMusic();
1482
1483 #if 1
1484       FadeOut(REDRAW_ALL);
1485 #endif
1486     }
1487
1488     if (tci->is_image)
1489       DrawTitleScreenImage(tci->local_nr, tci->initial);
1490     else
1491       DrawTitleScreenMessage(tci->local_nr, tci->initial);
1492
1493     fading_default = (tci->initial ? title_initial_default : title_default);
1494
1495     fading = fading_next = getTitleFading(tci);
1496
1497 #if 1
1498 #if 1
1499     if (!(fading_last.fade_mode & FADE_TYPE_TRANSFORM) &&
1500         fading_next.fade_mode & FADE_TYPE_TRANSFORM)
1501     {
1502       fading.fade_mode = FADE_MODE_FADE;
1503       fading.fade_delay = fading_default.fade_delay;
1504     }
1505 #else
1506     if (fading_last.fade_mode != FADE_MODE_CROSSFADE &&
1507         fading_next.fade_mode == FADE_MODE_CROSSFADE)
1508       fading.fade_mode = FADE_MODE_FADE;
1509 #endif
1510 #endif
1511
1512 #if 1
1513     sound = getTitleSound(tci);
1514     music = getTitleMusic(tci);
1515
1516     if (sound != last_sound)
1517       PlayMenuSoundExt(sound);
1518     if (music != last_music)
1519       PlayMenuMusicExt(music);
1520
1521     last_sound = sound;
1522     last_music = music;
1523 #endif
1524
1525     SetMouseCursor(CURSOR_NONE);
1526
1527 #if 1
1528     FadeIn(REDRAW_ALL);
1529 #endif
1530
1531     fading = fading_next;
1532
1533     DelayReached(&title_delay, 0);      /* reset delay counter */
1534
1535     return;
1536   }
1537
1538 #if 1
1539   if (fading.auto_delay > 0 && DelayReached(&title_delay, fading.auto_delay))
1540     button = MB_MENU_CHOICE;
1541 #else
1542   if (fading.auto_delay > -1 && DelayReached(&title_delay, fading.auto_delay))
1543     button = MB_MENU_CHOICE;
1544 #endif
1545
1546   if (button == MB_MENU_LEAVE)
1547   {
1548     return_to_main_menu = TRUE;
1549     use_fading_main_menu = FALSE;
1550   }
1551   else if (button == MB_MENU_CHOICE)
1552   {
1553     if (game_status == GAME_MODE_INFO && num_title_screens == 0)
1554     {
1555 #if 0
1556       FadeOut(REDRAW_FIELD);
1557 #endif
1558
1559       FadeSetEnterScreen();
1560
1561       info_mode = INFO_MODE_MAIN;
1562       DrawAndFadeInInfoScreen(REDRAW_FIELD);
1563
1564       return;
1565     }
1566
1567     title_screen_nr++;
1568     tci = &title_controls[title_screen_nr];
1569
1570     if (title_screen_nr < num_title_screens)
1571     {
1572       sound = getTitleSound(tci);
1573       music = getTitleMusic(tci);
1574
1575       if (sound == SND_UNDEFINED || sound != last_sound)
1576         FadeSounds();
1577       if (music == MUS_UNDEFINED || music != last_music)
1578         FadeMusic();
1579
1580 #if 1
1581       FadeOut(REDRAW_ALL);
1582 #endif
1583
1584       if (tci->is_image)
1585         DrawTitleScreenImage(tci->local_nr, tci->initial);
1586       else
1587         DrawTitleScreenMessage(tci->local_nr, tci->initial);
1588
1589       fading_next = getTitleFading(tci);
1590
1591 #if 1
1592       sound = getTitleSound(tci);
1593       music = getTitleMusic(tci);
1594
1595       if (sound != last_sound)
1596         PlayMenuSoundExt(sound);
1597       if (music != last_music)
1598         PlayMenuMusicExt(music);
1599
1600       last_sound = sound;
1601       last_music = music;
1602 #endif
1603
1604 #if 1
1605       /* last screen already faded out, next screen has no animation */
1606       if (!(fading.fade_mode & FADE_TYPE_TRANSFORM) &&
1607           fading_next.fade_mode == FADE_MODE_NONE)
1608         fading = fading_next;
1609 #else
1610       /* last screen already faded out, next screen has no animation */
1611       if (fading.fade_mode      != FADE_MODE_CROSSFADE &&
1612           fading_next.fade_mode == FADE_MODE_NONE)
1613         fading = fading_next;
1614 #endif
1615
1616 #if 1
1617       FadeIn(REDRAW_ALL);
1618 #endif
1619
1620       fading = fading_next;
1621
1622       DelayReached(&title_delay, 0);    /* reset delay counter */
1623     }
1624     else
1625     {
1626       FadeSoundsAndMusic();
1627
1628       return_to_main_menu = TRUE;
1629     }
1630   }
1631
1632   if (return_to_main_menu)
1633   {
1634 #if 0
1635     RedrawBackground();
1636 #endif
1637
1638     SetMouseCursor(CURSOR_DEFAULT);
1639
1640     if (game_status == GAME_MODE_INFO)
1641     {
1642 #if 0
1643       OpenDoor(DOOR_CLOSE_1 | DOOR_CLOSE_2 | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
1644 #endif
1645
1646       info_mode = INFO_MODE_MAIN;
1647       DrawInfoScreenExt(REDRAW_ALL, use_fading_main_menu);
1648     }
1649     else        /* default: return to main menu */
1650     {
1651 #if 0
1652       OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2 | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
1653 #endif
1654
1655       game_status = GAME_MODE_MAIN;
1656       DrawMainMenuExt(REDRAW_ALL, use_fading_main_menu);
1657     }
1658   }
1659 }
1660
1661 void HandleMainMenu_SelectLevel(int step, int direction)
1662 {
1663   int old_level_nr = level_nr;
1664   int new_level_nr;
1665
1666   new_level_nr = old_level_nr + step * direction;
1667   if (new_level_nr < leveldir_current->first_level)
1668     new_level_nr = leveldir_current->first_level;
1669   if (new_level_nr > leveldir_current->last_level)
1670     new_level_nr = leveldir_current->last_level;
1671
1672   if (setup.handicap && new_level_nr > leveldir_current->handicap_level)
1673   {
1674     /* skipping levels is only allowed when trying to skip single level */
1675     if (setup.skip_levels && step == 1 &&
1676         Request("Level still unsolved ! Skip despite handicap ?", REQ_ASK))
1677     {
1678       leveldir_current->handicap_level++;
1679       SaveLevelSetup_SeriesInfo();
1680     }
1681
1682     new_level_nr = leveldir_current->handicap_level;
1683   }
1684
1685   if (new_level_nr != old_level_nr)
1686   {
1687     struct MainControlInfo *mci= getMainControlInfo(MAIN_CONTROL_LEVEL_NUMBER);
1688
1689     PlaySound(SND_MENU_ITEM_SELECTING);
1690
1691     level_nr = new_level_nr;
1692
1693     DrawText(mSX + mci->pos_text->x, mSY + mci->pos_text->y,
1694              int2str(level_nr, menu.main.text.level_number.size),
1695              mci->pos_text->font);
1696
1697     LoadLevel(level_nr);
1698     DrawPreviewLevel(TRUE);
1699
1700     TapeErase();
1701     LoadTape(level_nr);
1702     DrawCompleteVideoDisplay();
1703
1704     /* needed because DrawPreviewLevel() takes some time */
1705     BackToFront();
1706     SyncDisplay();
1707   }
1708 }
1709
1710 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
1711 {
1712   static int choice = MAIN_CONTROL_GAME;
1713   int pos = choice;
1714   int i;
1715
1716   if (button == MB_MENU_INITIALIZE)
1717   {
1718     DrawCursorAndText_Main(choice, TRUE);
1719
1720     return;
1721   }
1722
1723   if (mx || my)         /* mouse input */
1724   {
1725     pos = -1;
1726
1727     for (i = 0; main_controls[i].nr != -1; i++)
1728     {
1729 #if 0
1730       printf("::: check click (%d, %d) for %d [%d] ...\n",
1731              mx - mSX, my - mSY, i, main_controls[i].nr);
1732 #endif
1733
1734       if (insideMenuPosRect(main_controls[i].pos_button, mx - mSX, my - mSY) ||
1735           insideTextPosRect(main_controls[i].pos_text,   mx - mSX, my - mSY) ||
1736           insideTextPosRect(main_controls[i].pos_input,  mx - mSX, my - mSY))
1737       {
1738 #if 0
1739         printf("::: inside %d\n", i);
1740 #endif
1741
1742         pos = main_controls[i].nr;
1743
1744         break;
1745       }
1746     }
1747   }
1748   else if (dx || dy)    /* keyboard input */
1749   {
1750     if (dx > 0 && (choice == MAIN_CONTROL_INFO ||
1751                    choice == MAIN_CONTROL_SETUP))
1752       button = MB_MENU_CHOICE;
1753     else if (dy)
1754       pos = choice + dy;
1755   }
1756
1757   if (pos == MAIN_CONTROL_LEVELS && dx != 0 && button)
1758   {
1759     HandleMainMenu_SelectLevel(1, dx < 0 ? -1 : +1);
1760   }
1761   else if (pos == MAIN_CONTROL_FIRST_LEVEL && !button)
1762   {
1763     HandleMainMenu_SelectLevel(MAX_LEVELS, -1);
1764   }
1765   else if (pos == MAIN_CONTROL_LAST_LEVEL && !button)
1766   {
1767     HandleMainMenu_SelectLevel(MAX_LEVELS, +1);
1768   }
1769   else if (pos == MAIN_CONTROL_LEVEL_NUMBER && !button)
1770   {
1771     game_status = GAME_MODE_LEVELNR;
1772
1773     DrawChooseLevelNr();
1774   }
1775   else if (pos >= MAIN_CONTROL_NAME && pos <= MAIN_CONTROL_QUIT)
1776   {
1777     if (button)
1778     {
1779       if (pos != choice)
1780       {
1781         PlaySound(SND_MENU_ITEM_ACTIVATING);
1782
1783         DrawCursorAndText_Main(choice, FALSE);
1784         DrawCursorAndText_Main(pos, TRUE);
1785
1786         choice = pos;
1787       }
1788     }
1789     else
1790     {
1791       PlaySound(SND_MENU_ITEM_SELECTING);
1792
1793       if (pos == MAIN_CONTROL_NAME)
1794       {
1795         game_status = GAME_MODE_PSEUDO_TYPENAME;
1796
1797         HandleTypeName(strlen(setup.player_name), 0);
1798       }
1799       else if (pos == MAIN_CONTROL_LEVELS)
1800       {
1801         if (leveldir_first)
1802         {
1803           game_status = GAME_MODE_LEVELS;
1804
1805           SaveLevelSetup_LastSeries();
1806           SaveLevelSetup_SeriesInfo();
1807
1808 #if defined(CREATE_SPECIAL_EDITION_RND_JUE)
1809           gotoTopLevelDir();
1810 #endif
1811
1812           DrawChooseLevelSet();
1813         }
1814       }
1815       else if (pos == MAIN_CONTROL_SCORES)
1816       {
1817         game_status = GAME_MODE_SCORES;
1818
1819         DrawHallOfFame(-1);
1820       }
1821       else if (pos == MAIN_CONTROL_EDITOR)
1822       {
1823         if (leveldir_current->readonly &&
1824             !strEqual(setup.player_name, "Artsoft"))
1825           Request("This level is read only !", REQ_CONFIRM);
1826
1827         game_status = GAME_MODE_EDITOR;
1828
1829         FadeSetEnterScreen();
1830
1831 #if 0
1832         /* needed if different viewport properties defined for editor */
1833         ChangeViewportPropertiesIfNeeded();
1834 #endif
1835
1836         DrawLevelEd();
1837       }
1838       else if (pos == MAIN_CONTROL_INFO)
1839       {
1840         game_status = GAME_MODE_INFO;
1841         info_mode = INFO_MODE_MAIN;
1842
1843         DrawInfoScreen();
1844       }
1845       else if (pos == MAIN_CONTROL_GAME)
1846       {
1847         StartGameActions(options.network, setup.autorecord, level.random_seed);
1848       }
1849       else if (pos == MAIN_CONTROL_SETUP)
1850       {
1851         game_status = GAME_MODE_SETUP;
1852         setup_mode = SETUP_MODE_MAIN;
1853
1854         DrawSetupScreen();
1855       }
1856       else if (pos == MAIN_CONTROL_QUIT)
1857       {
1858         SaveLevelSetup_LastSeries();
1859         SaveLevelSetup_SeriesInfo();
1860
1861         if (Request("Do you really want to quit ?", REQ_ASK | REQ_STAY_CLOSED))
1862           game_status = GAME_MODE_QUIT;
1863       }
1864     }
1865   }
1866
1867   if (game_status == GAME_MODE_MAIN)
1868   {
1869     DrawPreviewLevel(FALSE);
1870     DoAnimation();
1871   }
1872 }
1873
1874
1875 /* ========================================================================= */
1876 /* info screen functions                                                     */
1877 /* ========================================================================= */
1878
1879 static struct TokenInfo *info_info;
1880 static int num_info_info;
1881
1882 static void execInfoTitleScreen()
1883 {
1884   info_mode = INFO_MODE_TITLE;
1885
1886   DrawInfoScreen();
1887 }
1888
1889 static void execInfoElements()
1890 {
1891   info_mode = INFO_MODE_ELEMENTS;
1892
1893   DrawInfoScreen();
1894 }
1895
1896 static void execInfoMusic()
1897 {
1898   info_mode = INFO_MODE_MUSIC;
1899
1900   DrawInfoScreen();
1901 }
1902
1903 static void execInfoCredits()
1904 {
1905   info_mode = INFO_MODE_CREDITS;
1906
1907   DrawInfoScreen();
1908 }
1909
1910 static void execInfoProgram()
1911 {
1912   info_mode = INFO_MODE_PROGRAM;
1913
1914   DrawInfoScreen();
1915 }
1916
1917 static void execInfoVersion()
1918 {
1919   info_mode = INFO_MODE_VERSION;
1920
1921   DrawInfoScreen();
1922 }
1923
1924 static void execInfoLevelSet()
1925 {
1926   info_mode = INFO_MODE_LEVELSET;
1927
1928   DrawInfoScreen();
1929 }
1930
1931 static void execExitInfo()
1932 {
1933   game_status = GAME_MODE_MAIN;
1934
1935   DrawMainMenuExt(REDRAW_FIELD, FALSE);
1936 }
1937
1938 static struct TokenInfo info_info_main[] =
1939 {
1940   { TYPE_ENTER_SCREEN,  execInfoTitleScreen,    "Title Screen"          },
1941   { TYPE_ENTER_SCREEN,  execInfoElements,       "Elements Info"         },
1942   { TYPE_ENTER_SCREEN,  execInfoMusic,          "Music Info"            },
1943   { TYPE_ENTER_SCREEN,  execInfoCredits,        "Credits"               },
1944   { TYPE_ENTER_SCREEN,  execInfoProgram,        "Program Info"          },
1945   { TYPE_ENTER_SCREEN,  execInfoVersion,        "Version Info"          },
1946   { TYPE_ENTER_SCREEN,  execInfoLevelSet,       "Level Set Info"        },
1947   { TYPE_EMPTY,         NULL,                   ""                      },
1948   { TYPE_LEAVE_MENU,    execExitInfo,           "Exit"                  },
1949
1950   { 0,                  NULL,                   NULL                    }
1951 };
1952
1953 static void DrawCursorAndText_Info(int pos, boolean active)
1954 {
1955   int xpos = MENU_SCREEN_START_XPOS;
1956   int ypos = MENU_SCREEN_START_YPOS + pos;
1957   int font_nr = FONT_MENU_1;
1958
1959   if (active)
1960     font_nr = FONT_ACTIVE(font_nr);
1961
1962   DrawText(mSX + xpos * 32, mSY + ypos * 32, info_info[pos].text, font_nr);
1963
1964   if (info_info[pos].type & ~TYPE_SKIP_ENTRY)
1965     drawCursor(pos, active);
1966 }
1967
1968 static void DrawInfoScreen_Main(int fade_mask, boolean do_fading)
1969 {
1970   int i;
1971
1972   UnmapAllGadgets();
1973   CloseDoor(DOOR_CLOSE_2);
1974
1975   /* (needed after displaying title screens which disable auto repeat) */
1976   KeyboardAutoRepeatOn();
1977
1978   FadeSetLeaveScreen();
1979
1980 #if 1
1981   FadeOut(fade_mask);
1982 #endif
1983
1984 #if 1
1985   if (fade_mask == REDRAW_ALL)
1986   {
1987     RedrawBackground();
1988
1989     OpenDoor(DOOR_CLOSE_1 | DOOR_CLOSE_2 | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
1990   }
1991 #endif
1992
1993   ClearField();
1994
1995   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Info Screen");
1996
1997   info_info = info_info_main;
1998   num_info_info = 0;
1999
2000 #if 1
2001   for (i = 0; info_info[i].type != 0 && i < MAX_MENU_ENTRIES_ON_SCREEN; i++)
2002 #else
2003   for (i = 0; info_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
2004 #endif
2005   {
2006     if (info_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
2007       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
2008     else if (info_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
2009       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
2010     else if (info_info[i].type & ~TYPE_SKIP_ENTRY)
2011       initCursor(i, IMG_MENU_BUTTON);
2012
2013     DrawCursorAndText_Info(i, FALSE);
2014
2015     num_info_info++;
2016   }
2017
2018   HandleInfoScreen_Main(0, 0, 0, 0, MB_MENU_INITIALIZE);
2019
2020   PlayMenuSound();
2021   PlayMenuMusic();
2022
2023   DrawMaskedBorder(fade_mask);
2024
2025   FadeIn(fade_mask);
2026
2027   InitAnimation();
2028 }
2029
2030 void HandleInfoScreen_Main(int mx, int my, int dx, int dy, int button)
2031 {
2032   static int choice_store[MAX_INFO_MODES];
2033   int choice = choice_store[info_mode];         /* always starts with 0 */
2034   int x = 0;
2035   int y = choice;
2036
2037   if (button == MB_MENU_INITIALIZE)
2038   {
2039     /* advance to first valid menu entry */
2040     while (choice < num_info_info &&
2041            info_info[choice].type & TYPE_SKIP_ENTRY)
2042       choice++;
2043     choice_store[info_mode] = choice;
2044
2045     DrawCursorAndText_Info(choice, TRUE);
2046
2047     return;
2048   }
2049   else if (button == MB_MENU_LEAVE)
2050   {
2051     for (y = 0; y < num_info_info; y++)
2052     {
2053       if (info_info[y].type & TYPE_LEAVE_MENU)
2054       {
2055         void (*menu_callback_function)(void) = info_info[y].value;
2056
2057         FadeSetLeaveMenu();
2058
2059         menu_callback_function();
2060
2061         break;  /* absolutely needed because function changes 'info_info'! */
2062       }
2063     }
2064
2065     return;
2066   }
2067
2068   if (mx || my)         /* mouse input */
2069   {
2070     x = (mx - mSX) / 32;
2071     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2072   }
2073   else if (dx || dy)    /* keyboard input */
2074   {
2075     if (dx)
2076     {
2077       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE : TYPE_ENTER);
2078
2079       if (info_info[choice].type & menu_navigation_type ||
2080           info_info[choice].type & TYPE_ENTER_SCREEN ||
2081           info_info[choice].type & TYPE_BOOLEAN_STYLE ||
2082           info_info[choice].type & TYPE_YES_NO_AUTO)
2083         button = MB_MENU_CHOICE;
2084     }
2085     else if (dy)
2086       y = choice + dy;
2087
2088     /* jump to next non-empty menu entry (up or down) */
2089     while (y > 0 && y < num_info_info - 1 &&
2090            info_info[y].type & TYPE_SKIP_ENTRY)
2091       y += dy;
2092   }
2093
2094   if (IN_VIS_FIELD(x, y) &&
2095       y >= 0 && y < num_info_info && info_info[y].type & ~TYPE_SKIP_ENTRY)
2096   {
2097     if (button)
2098     {
2099       if (y != choice)
2100       {
2101         PlaySound(SND_MENU_ITEM_ACTIVATING);
2102
2103         DrawCursorAndText_Info(choice, FALSE);
2104         DrawCursorAndText_Info(y, TRUE);
2105
2106         choice = choice_store[info_mode] = y;
2107       }
2108     }
2109     else if (!(info_info[y].type & TYPE_GHOSTED))
2110     {
2111       PlaySound(SND_MENU_ITEM_SELECTING);
2112
2113       if (info_info[y].type & TYPE_ENTER_OR_LEAVE)
2114       {
2115         void (*menu_callback_function)(void) = info_info[choice].value;
2116
2117         FadeSetFromType(info_info[y].type);
2118
2119         menu_callback_function();
2120       }
2121     }
2122   }
2123 }
2124
2125 void DrawInfoScreen_NotAvailable(char *text_title, char *text_error)
2126 {
2127   int ystart1 = mSY - SY + 100;
2128   int ystart2 = mSY - SY + 150;
2129   int ybottom = mSY - SY + SYSIZE - 20;
2130
2131   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_LEVELSET);
2132
2133   FadeOut(REDRAW_FIELD);
2134
2135   ClearField();
2136   DrawHeadline();
2137
2138   DrawTextSCentered(ystart1, FONT_TEXT_1, text_title);
2139   DrawTextSCentered(ystart2, FONT_TEXT_2, text_error);
2140
2141   DrawTextSCentered(ybottom, FONT_TEXT_4,
2142                     "Press any key or button for info menu");
2143
2144   FadeIn(REDRAW_FIELD);
2145 }
2146
2147 void DrawInfoScreen_HelpAnim(int start, int max_anims, boolean init)
2148 {
2149   static int infoscreen_step[MAX_INFO_ELEMENTS_ON_SCREEN];
2150   static int infoscreen_frame[MAX_INFO_ELEMENTS_ON_SCREEN];
2151   int xstart = mSX + 16;
2152   int ystart1 = mSY - SY + 100;
2153   int ystart2 = mSY + 64 + 2 * 32;
2154   int ybottom = mSY - SY + SYSIZE - 20;
2155   int ystep = TILEY + 4;
2156   int element, action, direction;
2157   int graphic;
2158   int delay;
2159   int sync_frame;
2160   int i, j;
2161
2162   if (init)
2163   {
2164     for (i = 0; i < MAX_INFO_ELEMENTS_ON_SCREEN; i++)
2165       infoscreen_step[i] = infoscreen_frame[i] = 0;
2166
2167     ClearField();
2168     DrawHeadline();
2169
2170     DrawTextSCentered(ystart1, FONT_TEXT_1, "The Game Elements:");
2171
2172     DrawTextSCentered(ybottom, FONT_TEXT_4,
2173                       "Press any key or button for next page");
2174
2175     FrameCounter = 0;
2176   }
2177
2178   i = j = 0;
2179   while (helpanim_info[j].element != HELPANIM_LIST_END)
2180   {
2181     if (i >= start + MAX_INFO_ELEMENTS_ON_SCREEN ||
2182         i >= max_anims)
2183       break;
2184     else if (i < start)
2185     {
2186       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
2187         j++;
2188
2189       j++;
2190       i++;
2191
2192       continue;
2193     }
2194
2195     j += infoscreen_step[i - start];
2196
2197     element = helpanim_info[j].element;
2198     action = helpanim_info[j].action;
2199     direction = helpanim_info[j].direction;
2200
2201     if (element < 0)
2202       element = EL_UNKNOWN;
2203
2204     if (action != -1 && direction != -1)
2205       graphic = el_act_dir2img(element, action, direction);
2206     else if (action != -1)
2207       graphic = el_act2img(element, action);
2208     else if (direction != -1)
2209       graphic = el_dir2img(element, direction);
2210     else
2211       graphic = el2img(element);
2212
2213     delay = helpanim_info[j++].delay;
2214
2215     if (delay == -1)
2216       delay = 1000000;
2217
2218     if (infoscreen_frame[i - start] == 0)
2219     {
2220       sync_frame = 0;
2221       infoscreen_frame[i - start] = delay - 1;
2222     }
2223     else
2224     {
2225       sync_frame = delay - infoscreen_frame[i - start];
2226       infoscreen_frame[i - start]--;
2227     }
2228
2229     if (helpanim_info[j].element == HELPANIM_LIST_NEXT)
2230     {
2231       if (!infoscreen_frame[i - start])
2232         infoscreen_step[i - start] = 0;
2233     }
2234     else
2235     {
2236       if (!infoscreen_frame[i - start])
2237         infoscreen_step[i - start]++;
2238       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
2239         j++;
2240     }
2241
2242     j++;
2243
2244     ClearRectangleOnBackground(drawto, xstart, ystart2 + (i - start) * ystep,
2245                                TILEX, TILEY);
2246     DrawFixedGraphicAnimationExt(drawto, xstart, ystart2 + (i - start) * ystep,
2247                                  graphic, sync_frame, USE_MASKING);
2248
2249     if (init)
2250       DrawInfoScreen_HelpText(element, action, direction, i - start);
2251
2252     i++;
2253   }
2254
2255   redraw_mask |= REDRAW_FIELD;
2256
2257   FrameCounter++;
2258 }
2259
2260 static char *getHelpText(int element, int action, int direction)
2261 {
2262   char token[MAX_LINE_LEN];
2263
2264   strcpy(token, element_info[element].token_name);
2265
2266   if (action != -1)
2267     strcat(token, element_action_info[action].suffix);
2268
2269   if (direction != -1)
2270     strcat(token, element_direction_info[MV_DIR_TO_BIT(direction)].suffix);
2271
2272   return getHashEntry(helptext_info, token);
2273 }
2274
2275 void DrawInfoScreen_HelpText(int element, int action, int direction, int ypos)
2276 {
2277   int font_nr = FONT_INFO_ELEMENTS;
2278   int font_width = getFontWidth(font_nr);
2279   int sx = mSX + MINI_TILEX + TILEX + MINI_TILEX;
2280   int sy = mSY + 65 + 2 * 32 + 1;
2281   int ystep = TILEY + 4;
2282   int pad_x = sx - SX;
2283   int max_chars_per_line = (SXSIZE - pad_x - MINI_TILEX) / font_width;
2284   int max_lines_per_text = 2;    
2285   char *text = NULL;
2286
2287   if (action != -1 && direction != -1)          /* element.action.direction */
2288     text = getHelpText(element, action, direction);
2289
2290   if (text == NULL && action != -1)             /* element.action */
2291     text = getHelpText(element, action, -1);
2292
2293   if (text == NULL && direction != -1)          /* element.direction */
2294     text = getHelpText(element, -1, direction);
2295
2296   if (text == NULL)                             /* base element */
2297     text = getHelpText(element, -1, -1);
2298
2299   if (text == NULL)                             /* not found */
2300     text = "No description available";
2301
2302   if (strlen(text) <= max_chars_per_line)       /* only one line of text */
2303     sy += getFontHeight(font_nr) / 2;
2304
2305   DrawTextBuffer(sx, sy + ypos * ystep, text, font_nr,
2306                  max_chars_per_line, -1, max_lines_per_text, 0, -1,
2307                  TRUE, FALSE, FALSE);
2308 }
2309
2310 void DrawInfoScreen_TitleScreen()
2311 {
2312   DrawTitleScreen();
2313 }
2314
2315 void HandleInfoScreen_TitleScreen(int button)
2316 {
2317   HandleTitleScreen(0, 0, 0, 0, button);
2318 }
2319
2320 void DrawInfoScreen_Elements()
2321 {
2322   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_ELEMENTS);
2323
2324   FadeOut(REDRAW_FIELD);
2325
2326   LoadHelpAnimInfo();
2327   LoadHelpTextInfo();
2328
2329   HandleInfoScreen_Elements(MB_MENU_INITIALIZE);
2330
2331   FadeIn(REDRAW_FIELD);
2332
2333   InitAnimation();
2334 }
2335
2336 void HandleInfoScreen_Elements(int button)
2337 {
2338   static unsigned long info_delay = 0;
2339   static int num_anims;
2340   static int num_pages;
2341   static int page;
2342   int anims_per_page = MAX_INFO_ELEMENTS_ON_SCREEN;
2343   int i;
2344
2345   if (button == MB_MENU_INITIALIZE)
2346   {
2347     boolean new_element = TRUE;
2348
2349     num_anims = 0;
2350
2351     for (i = 0; helpanim_info[i].element != HELPANIM_LIST_END; i++)
2352     {
2353       if (helpanim_info[i].element == HELPANIM_LIST_NEXT)
2354         new_element = TRUE;
2355       else if (new_element)
2356       {
2357         num_anims++;
2358         new_element = FALSE;
2359       }
2360     }
2361
2362     num_pages = (num_anims + anims_per_page - 1) / anims_per_page;
2363     page = 0;
2364   }
2365
2366   if (button == MB_MENU_LEAVE)
2367   {
2368     PlaySound(SND_MENU_ITEM_SELECTING);
2369
2370     info_mode = INFO_MODE_MAIN;
2371     DrawInfoScreen();
2372
2373     return;
2374   }
2375   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
2376   {
2377     if (button != MB_MENU_INITIALIZE)
2378     {
2379       PlaySound(SND_MENU_ITEM_SELECTING);
2380
2381       page++;
2382     }
2383
2384     if (page >= num_pages)
2385     {
2386       FadeSoundsAndMusic();
2387
2388       info_mode = INFO_MODE_MAIN;
2389       DrawAndFadeInInfoScreen(REDRAW_FIELD);
2390
2391       return;
2392     }
2393
2394 #if 1
2395     if (page > 0)
2396       FadeSetNextScreen();
2397 #endif
2398
2399     if (button != MB_MENU_INITIALIZE)
2400       FadeOut(REDRAW_FIELD);
2401
2402     DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, TRUE);
2403
2404     if (button != MB_MENU_INITIALIZE)
2405       FadeIn(REDRAW_FIELD);
2406   }
2407   else
2408   {
2409     if (DelayReached(&info_delay, GameFrameDelay))
2410       if (page < num_pages)
2411         DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, FALSE);
2412
2413     PlayMenuSoundIfLoop();
2414   }
2415 }
2416
2417 void DrawInfoScreen_Music()
2418 {
2419   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_MUSIC);
2420
2421 #if 1
2422   FadeOut(REDRAW_FIELD);
2423 #endif
2424
2425   ClearField();
2426   DrawHeadline();
2427
2428   LoadMusicInfo();
2429
2430   HandleInfoScreen_Music(MB_MENU_INITIALIZE);
2431
2432 #if 1
2433   FadeIn(REDRAW_FIELD);
2434 #endif
2435 }
2436
2437 void HandleInfoScreen_Music(int button)
2438 {
2439   static struct MusicFileInfo *list = NULL;
2440   int ystart1 = mSY - SY + 100;
2441   int ystart2 = mSY - SY + 150;
2442   int ybottom = mSY - SY + SYSIZE - 20;
2443   int dy = 30;
2444
2445   if (button == MB_MENU_INITIALIZE)
2446   {
2447     list = music_file_info;
2448
2449     if (list == NULL)
2450     {
2451       FadeSoundsAndMusic();
2452
2453       ClearField();
2454       DrawHeadline();
2455
2456       DrawTextSCentered(ystart1, FONT_TEXT_1,
2457                         "No music info for this level set.");
2458
2459       DrawTextSCentered(ybottom, FONT_TEXT_4,
2460                         "Press any key or button for info menu");
2461
2462       return;
2463     }
2464   }
2465
2466   if (button == MB_MENU_LEAVE)
2467   {
2468     PlaySound(SND_MENU_ITEM_SELECTING);
2469
2470     FadeSoundsAndMusic();
2471
2472     info_mode = INFO_MODE_MAIN;
2473     DrawInfoScreen();
2474
2475     return;
2476   }
2477   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
2478   {
2479     int y = 0;
2480
2481     if (button != MB_MENU_INITIALIZE)
2482     {
2483       PlaySound(SND_MENU_ITEM_SELECTING);
2484
2485       if (list != NULL)
2486         list = list->next;
2487     }
2488
2489     if (list == NULL)
2490     {
2491       FadeSoundsAndMusic();
2492
2493       info_mode = INFO_MODE_MAIN;
2494       DrawAndFadeInInfoScreen(REDRAW_FIELD);
2495
2496       return;
2497     }
2498
2499     FadeSoundsAndMusic();
2500
2501 #if 1
2502     if (list != music_file_info)
2503       FadeSetNextScreen();
2504 #endif
2505
2506     if (button != MB_MENU_INITIALIZE)
2507       FadeOut(REDRAW_FIELD);
2508
2509     ClearField();
2510     DrawHeadline();
2511
2512     if (list->is_sound)
2513     {
2514       int sound = list->music;
2515
2516       if (sound_info[sound].loop)
2517         PlaySoundLoop(sound);
2518       else
2519         PlaySound(sound);
2520
2521       DrawTextSCentered(ystart1, FONT_TEXT_1, "The Game Background Sounds:");
2522     }
2523     else
2524     {
2525       PlayMusic(list->music);
2526
2527       DrawTextSCentered(ystart1, FONT_TEXT_1, "The Game Background Music:");
2528     }
2529
2530     if (!strEqual(list->title, UNKNOWN_NAME))
2531     {
2532       if (!strEqual(list->title_header, UNKNOWN_NAME))
2533         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, list->title_header);
2534
2535       DrawTextFCentered(ystart2 + y++ * dy, FONT_TEXT_3, "\"%s\"", list->title);
2536     }
2537
2538     if (!strEqual(list->artist, UNKNOWN_NAME))
2539     {
2540       if (!strEqual(list->artist_header, UNKNOWN_NAME))
2541         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, list->artist_header);
2542       else
2543         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, "by");
2544
2545       DrawTextFCentered(ystart2 + y++ * dy, FONT_TEXT_3, "%s", list->artist);
2546     }
2547
2548     if (!strEqual(list->album, UNKNOWN_NAME))
2549     {
2550       if (!strEqual(list->album_header, UNKNOWN_NAME))
2551         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, list->album_header);
2552       else
2553         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, "from the album");
2554
2555       DrawTextFCentered(ystart2 + y++ * dy, FONT_TEXT_3, "\"%s\"", list->album);
2556     }
2557
2558     if (!strEqual(list->year, UNKNOWN_NAME))
2559     {
2560       if (!strEqual(list->year_header, UNKNOWN_NAME))
2561         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, list->year_header);
2562       else
2563         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, "from the year");
2564
2565       DrawTextFCentered(ystart2 + y++ * dy, FONT_TEXT_3, "%s", list->year);
2566     }
2567
2568     DrawTextSCentered(ybottom, FONT_TEXT_4,
2569                       "Press any key or button for next page");
2570
2571     if (button != MB_MENU_INITIALIZE)
2572       FadeIn(REDRAW_FIELD);
2573   }
2574
2575   if (list != NULL && list->is_sound && sound_info[list->music].loop)
2576     PlaySoundLoop(list->music);
2577 }
2578
2579 static void DrawInfoScreen_CreditsScreen(int screen_nr)
2580 {
2581   int ystart1 = mSY - SY + 100;
2582   int ystart2 = mSY - SY + 150;
2583   int ybottom = mSY - SY + SYSIZE - 20;
2584   int ystep = 30;
2585
2586   ClearField();
2587   DrawHeadline();
2588
2589   DrawTextSCentered(ystart1, FONT_TEXT_1, "Credits:");
2590
2591   if (screen_nr == 0)
2592   {
2593     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2594                       "Special thanks to");
2595     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2596                       "Peter Liepa");
2597     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2598                       "for creating");
2599     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2600                       "\"Boulder Dash\"");
2601     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2602                       "in the year");
2603     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2604                       "1984");
2605     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
2606                       "published by");
2607     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_3,
2608                       "First Star Software");
2609   }
2610   else if (screen_nr == 1)
2611   {
2612     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2613                       "Special thanks to");
2614     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2615                       "Klaus Heinz & Volker Wertich");
2616     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2617                       "for creating");
2618     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2619                       "\"Emerald Mine\"");
2620     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2621                       "in the year");
2622     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2623                       "1987");
2624     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
2625                       "published by");
2626     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_3,
2627                       "Kingsoft");
2628   }
2629   else if (screen_nr == 2)
2630   {
2631     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2632                       "Special thanks to");
2633     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2634                       "Michael Stopp & Philip Jespersen");
2635     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2636                       "for creating");
2637     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2638                       "\"Supaplex\"");
2639     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2640                       "in the year");
2641     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2642                       "1991");
2643     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
2644                       "published by");
2645     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_3,
2646                       "Digital Integration");
2647   }
2648   else if (screen_nr == 3)
2649   {
2650     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2651                       "Special thanks to");
2652     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2653                       "Hiroyuki Imabayashi");
2654     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2655                       "for creating");
2656     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2657                       "\"Sokoban\"");
2658     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2659                       "in the year");
2660     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2661                       "1982");
2662     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
2663                       "published by");
2664     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_3,
2665                       "Thinking Rabbit");
2666   }
2667   else if (screen_nr == 4)
2668   {
2669     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2670                       "Special thanks to");
2671     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2672                       "Alan Bond");
2673     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2674                       "and");
2675     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2676                       "Jürgen Bonhagen");
2677     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2678                       "for the continuous creation");
2679     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_2,
2680                       "of outstanding level sets");
2681   }
2682   else if (screen_nr == 5)
2683   {
2684     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2685                       "Thanks to");
2686     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2687                       "Peter Elzner");
2688     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2689                       "for ideas and inspiration by");
2690     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2691                       "Diamond Caves");
2692
2693     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_2,
2694                       "Thanks to");
2695     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_3,
2696                       "Steffest");
2697     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_2,
2698                       "for ideas and inspiration by");
2699     DrawTextSCentered(ystart2 + 8 * ystep, FONT_TEXT_3,
2700                       "DX-Boulderdash");
2701   }
2702   else if (screen_nr == 6)
2703   {
2704     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2705                       "Thanks to");
2706     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2707                       "David Tritscher");
2708 #if 1
2709     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2710                       "for the code base used for the");
2711     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_2,
2712                       "native Emerald Mine engine");
2713 #else
2714     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2715                       "for the new Emerald Mine engine");
2716 #endif
2717   }
2718   else if (screen_nr == 7)
2719   {
2720     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2721                       "Thanks to");
2722     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2723                       "Guido Schulz");
2724     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2725                       "for the initial DOS port");
2726
2727     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2728                       "Thanks to");
2729     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2730                       "Karl Hörnell");
2731     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
2732                       "for some additional toons");
2733   }
2734   else if (screen_nr == 8)
2735   {
2736     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2737                       "And not to forget:");
2738     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_2,
2739                       "Many thanks to");
2740     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_3,
2741                       "All those who contributed");
2742     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2743                       "levels to this game");
2744     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_3,
2745                       "since 1995");
2746   }
2747
2748   DrawTextSCentered(ybottom, FONT_TEXT_4,
2749                     "Press any key or button for next page");
2750 }
2751
2752 void DrawInfoScreen_Credits()
2753 {
2754   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_CREDITS);
2755
2756   FadeSoundsAndMusic();
2757
2758 #if 1
2759   FadeOut(REDRAW_FIELD);
2760 #endif
2761
2762   HandleInfoScreen_Credits(MB_MENU_INITIALIZE);
2763
2764 #if 1
2765   FadeIn(REDRAW_FIELD);
2766 #endif
2767 }
2768
2769 void HandleInfoScreen_Credits(int button)
2770 {
2771   static int screen_nr = 0;
2772   int num_screens = 9;
2773
2774   if (button == MB_MENU_INITIALIZE)
2775   {
2776     screen_nr = 0;
2777
2778     // DrawInfoScreen_CreditsScreen(screen_nr);
2779   }
2780
2781   if (button == MB_MENU_LEAVE)
2782   {
2783     PlaySound(SND_MENU_ITEM_SELECTING);
2784
2785     info_mode = INFO_MODE_MAIN;
2786     DrawInfoScreen();
2787
2788     return;
2789   }
2790   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
2791   {
2792     if (button != MB_MENU_INITIALIZE)
2793     {
2794       PlaySound(SND_MENU_ITEM_SELECTING);
2795
2796       screen_nr++;
2797     }
2798
2799     if (screen_nr >= num_screens)
2800     {
2801       FadeSoundsAndMusic();
2802
2803       info_mode = INFO_MODE_MAIN;
2804       DrawAndFadeInInfoScreen(REDRAW_FIELD);
2805
2806       return;
2807     }
2808
2809 #if 1
2810     if (screen_nr > 0)
2811       FadeSetNextScreen();
2812 #endif
2813
2814     if (button != MB_MENU_INITIALIZE)
2815       FadeOut(REDRAW_FIELD);
2816
2817     DrawInfoScreen_CreditsScreen(screen_nr);
2818
2819     if (button != MB_MENU_INITIALIZE)
2820       FadeIn(REDRAW_FIELD);
2821   }
2822   else
2823   {
2824     PlayMenuSoundIfLoop();
2825   }
2826 }
2827
2828 void DrawInfoScreen_Program()
2829 {
2830   int ystart1 = mSY - SY + 100;
2831   int ystart2 = mSY - SY + 150;
2832   int ybottom = mSY - SY + SYSIZE - 20;
2833   int ystep = 30;
2834
2835   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_PROGRAM);
2836
2837 #if 1
2838   FadeOut(REDRAW_FIELD);
2839 #endif
2840
2841   ClearField();
2842   DrawHeadline();
2843
2844   DrawTextSCentered(ystart1, FONT_TEXT_1, "Program Information:");
2845
2846   DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2847                     "This game is Freeware!");
2848   DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_2,
2849                     "If you like it, send e-mail to:");
2850   DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_3,
2851                     PROGRAM_EMAIL_STRING);
2852   DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_2,
2853                     "or SnailMail to:");
2854   DrawTextSCentered(ystart2 + 4 * ystep + 0, FONT_TEXT_3,
2855                     "Holger Schemel");
2856   DrawTextSCentered(ystart2 + 4 * ystep + 20, FONT_TEXT_3,
2857                     "Detmolder Strasse 189");
2858   DrawTextSCentered(ystart2 + 4 * ystep + 40, FONT_TEXT_3,
2859                     "33604 Bielefeld");
2860   DrawTextSCentered(ystart2 + 4 * ystep + 60, FONT_TEXT_3,
2861                     "Germany");
2862   DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_2,
2863                     "More information and levels:");
2864   DrawTextSCentered(ystart2 + 8 * ystep, FONT_TEXT_3,
2865                     PROGRAM_WEBSITE_STRING);
2866   DrawTextSCentered(ystart2 + 9 * ystep, FONT_TEXT_2,
2867                     "If you have created new levels,");
2868   DrawTextSCentered(ystart2 + 10 * ystep, FONT_TEXT_2,
2869                     "send them to me to include them!");
2870   DrawTextSCentered(ystart2 + 11 * ystep, FONT_TEXT_2,
2871                     ":-)");
2872
2873   DrawTextSCentered(ybottom, FONT_TEXT_4,
2874                     "Press any key or button for info menu");
2875
2876 #if 1
2877   FadeIn(REDRAW_FIELD);
2878 #endif
2879 }
2880
2881 void HandleInfoScreen_Program(int button)
2882 {
2883   if (button == MB_MENU_LEAVE)
2884   {
2885     PlaySound(SND_MENU_ITEM_SELECTING);
2886
2887     info_mode = INFO_MODE_MAIN;
2888     DrawInfoScreen();
2889
2890     return;
2891   }
2892   else if (button == MB_MENU_CHOICE)
2893   {
2894     PlaySound(SND_MENU_ITEM_SELECTING);
2895
2896     FadeSoundsAndMusic();
2897
2898 #if 0
2899     FadeOut(REDRAW_FIELD);
2900 #endif
2901
2902     info_mode = INFO_MODE_MAIN;
2903     DrawAndFadeInInfoScreen(REDRAW_FIELD);
2904   }
2905   else
2906   {
2907     PlayMenuSoundIfLoop();
2908   }
2909 }
2910
2911 void DrawInfoScreen_Version()
2912 {
2913   int font_header = FONT_TEXT_3;
2914   int font_text = FONT_TEXT_2;
2915   int xstep = getFontWidth(font_text);
2916   int ystep = getFontHeight(font_text);
2917   int ystart1 = mSY - SY + 100;
2918   int ystart2 = mSY - SY + 150;
2919   int ybottom = mSY - SY + SYSIZE - 20;
2920   int xstart1 = mSX + 2 * xstep;
2921   int xstart2 = mSX + 19 * xstep;
2922 #if defined(TARGET_SDL)
2923   int xstart3 = mSX + 29 * xstep;
2924   SDL_version sdl_version_compiled;
2925   const SDL_version *sdl_version_linked;
2926   int driver_name_len = 8;
2927   char driver_name[driver_name_len];
2928 #endif
2929
2930   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_VERSION);
2931
2932 #if 1
2933   FadeOut(REDRAW_FIELD);
2934 #endif
2935
2936   ClearField();
2937   DrawHeadline();
2938
2939   DrawTextSCentered(ystart1, FONT_TEXT_1, "Version Information:");
2940
2941   DrawTextF(xstart1, ystart2, font_header, "Name");
2942   DrawTextF(xstart2, ystart2, font_text, PROGRAM_TITLE_STRING);
2943
2944   ystart2 += ystep;
2945   DrawTextF(xstart1, ystart2, font_header, "Version");
2946   DrawTextF(xstart2, ystart2, font_text, getProgramFullVersionString());
2947
2948   ystart2 += ystep;
2949   DrawTextF(xstart1, ystart2, font_header, "Platform");
2950   DrawTextF(xstart2, ystart2, font_text, PLATFORM_STRING);
2951
2952   ystart2 += ystep;
2953   DrawTextF(xstart1, ystart2, font_header, "Target");
2954   DrawTextF(xstart2, ystart2, font_text, TARGET_STRING);
2955
2956   ystart2 += ystep;
2957   DrawTextF(xstart1, ystart2, font_header, "Compile time");
2958   DrawTextF(xstart2, ystart2, font_text, getCompileDateString());
2959
2960 #if defined(TARGET_SDL)
2961   ystart2 += 3 * ystep;
2962   DrawTextF(xstart1, ystart2, font_header, "Library");
2963   DrawTextF(xstart2, ystart2, font_header, "compiled");
2964   DrawTextF(xstart3, ystart2, font_header, "linked");
2965
2966   SDL_VERSION(&sdl_version_compiled);
2967   sdl_version_linked = SDL_Linked_Version();
2968
2969   ystart2 += 2 * ystep;
2970   DrawTextF(xstart1, ystart2, font_text, "SDL");
2971   DrawTextF(xstart2, ystart2, font_text, "%d.%d.%d",
2972             sdl_version_compiled.major,
2973             sdl_version_compiled.minor,
2974             sdl_version_compiled.patch);
2975   DrawTextF(xstart3, ystart2, font_text, "%d.%d.%d",
2976             sdl_version_linked->major,
2977             sdl_version_linked->minor,
2978             sdl_version_linked->patch);
2979
2980   SDL_IMAGE_VERSION(&sdl_version_compiled);
2981   sdl_version_linked = IMG_Linked_Version();
2982
2983   ystart2 += ystep;
2984   DrawTextF(xstart1, ystart2, font_text, "SDL_image");
2985   DrawTextF(xstart2, ystart2, font_text, "%d.%d.%d",
2986             sdl_version_compiled.major,
2987             sdl_version_compiled.minor,
2988             sdl_version_compiled.patch);
2989   DrawTextF(xstart3, ystart2, font_text, "%d.%d.%d",
2990             sdl_version_linked->major,
2991             sdl_version_linked->minor,
2992             sdl_version_linked->patch);
2993
2994   SDL_MIXER_VERSION(&sdl_version_compiled);
2995   sdl_version_linked = Mix_Linked_Version();
2996
2997   ystart2 += ystep;
2998   DrawTextF(xstart1, ystart2, font_text, "SDL_mixer");
2999   DrawTextF(xstart2, ystart2, font_text, "%d.%d.%d",
3000             sdl_version_compiled.major,
3001             sdl_version_compiled.minor,
3002             sdl_version_compiled.patch);
3003   DrawTextF(xstart3, ystart2, font_text, "%d.%d.%d",
3004             sdl_version_linked->major,
3005             sdl_version_linked->minor,
3006             sdl_version_linked->patch);
3007
3008   SDL_NET_VERSION(&sdl_version_compiled);
3009   sdl_version_linked = SDLNet_Linked_Version();
3010
3011   ystart2 += ystep;
3012   DrawTextF(xstart1, ystart2, font_text, "SDL_net");
3013   DrawTextF(xstart2, ystart2, font_text, "%d.%d.%d",
3014             sdl_version_compiled.major,
3015             sdl_version_compiled.minor,
3016             sdl_version_compiled.patch);
3017   DrawTextF(xstart3, ystart2, font_text, "%d.%d.%d",
3018             sdl_version_linked->major,
3019             sdl_version_linked->minor,
3020             sdl_version_linked->patch);
3021
3022   ystart2 += 3 * ystep;
3023   DrawTextF(xstart1, ystart2, font_header, "Driver");
3024   DrawTextF(xstart2, ystart2, font_header, "Requested");
3025   DrawTextF(xstart3, ystart2, font_header, "Used");
3026
3027   SDL_VideoDriverName(driver_name, driver_name_len);
3028
3029   ystart2 += 2 * ystep;
3030   DrawTextF(xstart1, ystart2, font_text, "SDL_VideoDriver");
3031   DrawTextF(xstart2, ystart2, font_text, "%s", setup.system.sdl_videodriver);
3032   DrawTextF(xstart3, ystart2, font_text, "%s", driver_name);
3033
3034   SDL_AudioDriverName(driver_name, driver_name_len);
3035
3036   ystart2 += ystep;
3037   DrawTextF(xstart1, ystart2, font_text, "SDL_AudioDriver");
3038   DrawTextF(xstart2, ystart2, font_text, "%s", setup.system.sdl_audiodriver);
3039   DrawTextF(xstart3, ystart2, font_text, "%s", driver_name);
3040 #endif
3041
3042   DrawTextSCentered(ybottom, FONT_TEXT_4,
3043                     "Press any key or button for info menu");
3044
3045 #if 1
3046   FadeIn(REDRAW_FIELD);
3047 #endif
3048 }
3049
3050 void HandleInfoScreen_Version(int button)
3051 {
3052   if (button == MB_MENU_LEAVE)
3053   {
3054     PlaySound(SND_MENU_ITEM_SELECTING);
3055
3056     info_mode = INFO_MODE_MAIN;
3057     DrawInfoScreen();
3058
3059     return;
3060   }
3061   else if (button == MB_MENU_CHOICE)
3062   {
3063     PlaySound(SND_MENU_ITEM_SELECTING);
3064
3065     FadeSoundsAndMusic();
3066
3067 #if 0
3068     FadeOut(REDRAW_FIELD);
3069 #endif
3070
3071     info_mode = INFO_MODE_MAIN;
3072     DrawAndFadeInInfoScreen(REDRAW_FIELD);
3073   }
3074   else
3075   {
3076     PlayMenuSoundIfLoop();
3077   }
3078 }
3079
3080 void DrawInfoScreen_LevelSet()
3081 {
3082   struct TitleMessageInfo *tmi = &readme;
3083   char *filename = getLevelSetInfoFilename();
3084
3085   /* if chars set to "-1", automatically determine by text and font width */
3086   if (tmi->chars == -1)
3087     tmi->chars = tmi->width / getFontWidth(tmi->font);
3088   else
3089     tmi->width = tmi->chars * getFontWidth(tmi->font);
3090
3091   /* if lines set to "-1", automatically determine by text and font height */
3092   if (tmi->lines == -1)
3093     tmi->lines = tmi->height / getFontHeight(tmi->font);
3094   else
3095     tmi->height = tmi->lines * getFontHeight(tmi->font);
3096
3097   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_LEVELSET);
3098
3099 #if 1
3100   FadeOut(REDRAW_FIELD);
3101 #endif
3102
3103   ClearField();
3104   DrawHeadline();
3105
3106   DrawTextCentered(mSY + 100, FONT_TEXT_1, "Level Set Information:");
3107
3108   if (filename != NULL)
3109     DrawTextFile(mSX + ALIGNED_TEXT_XPOS(tmi), mSY + ALIGNED_TEXT_YPOS(tmi),
3110                  filename, tmi->font, tmi->chars, -1, tmi->lines, 0, -1,
3111                  tmi->autowrap, tmi->centered, tmi->parse_comments);
3112   else
3113     DrawTextCentered(mSY + ALIGNED_TEXT_YPOS(tmi), FONT_TEXT_2,
3114                      "No information for this level set.");
3115
3116   DrawTextCentered(mSY + SYSIZE - 20, FONT_TEXT_4,
3117                    "Press any key or button for info menu");
3118
3119 #if 1
3120   FadeIn(REDRAW_FIELD);
3121 #endif
3122 }
3123
3124 void HandleInfoScreen_LevelSet(int button)
3125 {
3126   if (button == MB_MENU_LEAVE)
3127   {
3128     PlaySound(SND_MENU_ITEM_SELECTING);
3129
3130     info_mode = INFO_MODE_MAIN;
3131     DrawInfoScreen();
3132
3133     return;
3134   }
3135   else if (button == MB_MENU_CHOICE)
3136   {
3137     PlaySound(SND_MENU_ITEM_SELECTING);
3138
3139     FadeSoundsAndMusic();
3140
3141 #if 0
3142     FadeOut(REDRAW_FIELD);
3143 #endif
3144
3145     info_mode = INFO_MODE_MAIN;
3146     DrawAndFadeInInfoScreen(REDRAW_FIELD);
3147   }
3148   else
3149   {
3150     PlayMenuSoundIfLoop();
3151   }
3152 }
3153
3154 static void DrawInfoScreenExt(int fade_mask, boolean do_fading)
3155 {
3156   SetMainBackgroundImage(IMG_BACKGROUND_INFO);
3157
3158   if (info_mode == INFO_MODE_TITLE)
3159     DrawInfoScreen_TitleScreen();
3160   else if (info_mode == INFO_MODE_ELEMENTS)
3161     DrawInfoScreen_Elements();
3162   else if (info_mode == INFO_MODE_MUSIC)
3163     DrawInfoScreen_Music();
3164   else if (info_mode == INFO_MODE_CREDITS)
3165     DrawInfoScreen_Credits();
3166   else if (info_mode == INFO_MODE_PROGRAM)
3167     DrawInfoScreen_Program();
3168   else if (info_mode == INFO_MODE_VERSION)
3169     DrawInfoScreen_Version();
3170   else if (info_mode == INFO_MODE_LEVELSET)
3171     DrawInfoScreen_LevelSet();
3172   else
3173     DrawInfoScreen_Main(fade_mask, do_fading);
3174
3175   if (info_mode != INFO_MODE_MAIN &&
3176       info_mode != INFO_MODE_TITLE &&
3177       info_mode != INFO_MODE_MUSIC)
3178   {
3179     PlayMenuSound();
3180     PlayMenuMusic();
3181   }
3182 }
3183
3184 void DrawAndFadeInInfoScreen(int fade_mask)
3185 {
3186   DrawInfoScreenExt(fade_mask, TRUE);
3187 }
3188
3189 void DrawInfoScreen()
3190 {
3191   DrawInfoScreenExt(REDRAW_FIELD, FALSE);
3192 }
3193
3194 void HandleInfoScreen(int mx, int my, int dx, int dy, int button)
3195 {
3196   if (info_mode == INFO_MODE_TITLE)
3197     HandleInfoScreen_TitleScreen(button);
3198   else if (info_mode == INFO_MODE_ELEMENTS)
3199     HandleInfoScreen_Elements(button);
3200   else if (info_mode == INFO_MODE_MUSIC)
3201     HandleInfoScreen_Music(button);
3202   else if (info_mode == INFO_MODE_CREDITS)
3203     HandleInfoScreen_Credits(button);
3204   else if (info_mode == INFO_MODE_PROGRAM)
3205     HandleInfoScreen_Program(button);
3206   else if (info_mode == INFO_MODE_VERSION)
3207     HandleInfoScreen_Version(button);
3208   else if (info_mode == INFO_MODE_LEVELSET)
3209     HandleInfoScreen_LevelSet(button);
3210   else
3211     HandleInfoScreen_Main(mx, my, dx, dy, button);
3212
3213   DoAnimation();
3214 }
3215
3216
3217 /* ========================================================================= */
3218 /* type name functions                                                       */
3219 /* ========================================================================= */
3220
3221 void HandleTypeName(int newxpos, Key key)
3222 {
3223   static char last_player_name[MAX_PLAYER_NAME_LEN + 1];
3224   struct MainControlInfo *mci = getMainControlInfo(MAIN_CONTROL_NAME);
3225   struct TextPosInfo *pos = mci->pos_input;
3226   int startx = mSX + ALIGNED_TEXT_XPOS(pos);
3227   int starty = mSY + ALIGNED_TEXT_YPOS(pos);
3228   static int xpos = 0;
3229   int font_nr = pos->font;
3230   int font_active_nr = FONT_ACTIVE(font_nr);
3231   int font_width = getFontWidth(font_active_nr);
3232   char key_char = getValidConfigValueChar(getCharFromKey(key));
3233   boolean is_valid_key_char = (key_char != 0 && (key_char != ' ' || xpos > 0));
3234   boolean is_active = TRUE;
3235
3236   DrawBackgroundForFont(startx,starty, pos->width, pos->height, font_active_nr);
3237
3238   if (newxpos)
3239   {
3240     strcpy(last_player_name, setup.player_name);
3241
3242     xpos = newxpos;
3243   }
3244   else if (is_valid_key_char && xpos < MAX_PLAYER_NAME_LEN)
3245   {
3246     setup.player_name[xpos] = key_char;
3247     setup.player_name[xpos + 1] = 0;
3248
3249     xpos++;
3250   }
3251   else if ((key == KSYM_Delete || key == KSYM_BackSpace) && xpos > 0)
3252   {
3253     xpos--;
3254
3255     setup.player_name[xpos] = 0;
3256   }
3257   else if (key == KSYM_Return && xpos > 0)
3258   {
3259     SaveSetup();
3260
3261     is_active = FALSE;
3262
3263     game_status = GAME_MODE_MAIN;
3264   }
3265   else if (key == KSYM_Escape)
3266   {
3267     strcpy(setup.player_name, last_player_name);
3268
3269     is_active = FALSE;
3270
3271     game_status = GAME_MODE_MAIN;
3272   }
3273
3274   if (is_active)
3275   {
3276     pos->width = (strlen(setup.player_name) + 1) * font_width;
3277     startx = mSX + ALIGNED_TEXT_XPOS(pos);
3278
3279     DrawText(startx, starty, setup.player_name, font_active_nr);
3280     DrawText(startx + xpos * font_width, starty, "_", font_active_nr);
3281   }
3282   else
3283   {
3284     pos->width = strlen(setup.player_name) * font_width;
3285     startx = mSX + ALIGNED_TEXT_XPOS(pos);
3286
3287     DrawText(startx, starty, setup.player_name, font_nr);
3288   }
3289 }
3290
3291
3292 /* ========================================================================= */
3293 /* tree menu functions                                                       */
3294 /* ========================================================================= */
3295
3296 static void DrawChooseTree(TreeInfo **ti_ptr)
3297 {
3298   UnmapAllGadgets();
3299
3300   FreeScreenGadgets();
3301   CreateScreenGadgets();
3302
3303   CloseDoor(DOOR_CLOSE_2);
3304
3305 #if 1
3306   FadeOut(REDRAW_FIELD);
3307 #endif
3308
3309   ClearField();
3310
3311   HandleChooseTree(0, 0, 0, 0, MB_MENU_INITIALIZE, ti_ptr);
3312   MapScreenTreeGadgets(*ti_ptr);
3313
3314 #if 1
3315   FadeIn(REDRAW_FIELD);
3316 #endif
3317
3318   InitAnimation();
3319 }
3320
3321 static void AdjustChooseTreeScrollbar(int id, int first_entry, TreeInfo *ti)
3322 {
3323   struct GadgetInfo *gi = screen_gadget[id];
3324   int items_max, items_visible, item_position;
3325
3326   items_max = numTreeInfoInGroup(ti);
3327   items_visible = NUM_MENU_ENTRIES_ON_SCREEN;
3328   item_position = first_entry;
3329
3330   if (item_position > items_max - items_visible)
3331     item_position = items_max - items_visible;
3332
3333   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
3334                GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
3335                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
3336 }
3337
3338 static void drawChooseTreeList(int first_entry, int num_page_entries,
3339                                TreeInfo *ti)
3340 {
3341   int i;
3342   char *title_string = NULL;
3343   int yoffset_sets = MENU_TITLE1_YPOS;
3344   int yoffset_setup = 16;
3345   int yoffset = (ti->type == TREE_TYPE_LEVEL_DIR ||
3346                  ti->type == TREE_TYPE_LEVEL_NR ? yoffset_sets : yoffset_setup);
3347   int last_game_status = game_status;   /* save current game status */
3348
3349   title_string = ti->infotext;
3350
3351   DrawTextSCentered(mSY - SY + yoffset, FONT_TITLE_1, title_string);
3352
3353 #if 0
3354   /* force LEVELS font on artwork setup screen */
3355   game_status = GAME_MODE_LEVELS;
3356 #endif
3357
3358 #if 1
3359   /* clear tree list area, but not title or scrollbar */
3360   DrawBackground(mSX, mSY + MENU_SCREEN_START_YPOS * 32,
3361                  SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset,
3362                  NUM_MENU_ENTRIES_ON_SCREEN * 32);
3363 #else
3364   /* clear tree list area, but not title or scrollbar */
3365   DrawBackground(mSX, mSY + MENU_SCREEN_START_YPOS * 32,
3366                  SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset,
3367                  MAX_MENU_ENTRIES_ON_SCREEN * 32);
3368 #endif
3369
3370   for (i = 0; i < num_page_entries; i++)
3371   {
3372     TreeInfo *node, *node_first;
3373     int entry_pos = first_entry + i;
3374     int xpos = MENU_SCREEN_START_XPOS;
3375     int ypos = MENU_SCREEN_START_YPOS + i;
3376     int startx = mSX + xpos * 32;
3377     int starty = mSY + ypos * 32;
3378     int font_nr = FONT_TEXT_1;
3379     int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
3380     int startx_text = startx + font_xoffset;
3381     int startx_scrollbar = mSX + SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
3382     int text_size = startx_scrollbar - startx_text;
3383     int max_buffer_len = text_size / getFontWidth(font_nr);
3384     char buffer[max_buffer_len + 1];
3385
3386     node_first = getTreeInfoFirstGroupEntry(ti);
3387     node = getTreeInfoFromPos(node_first, entry_pos);
3388
3389     strncpy(buffer, node->name, max_buffer_len);
3390     buffer[max_buffer_len] = '\0';
3391
3392     DrawText(startx, starty, buffer, font_nr + node->color);
3393
3394     if (node->parent_link)
3395       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
3396     else if (node->level_group)
3397       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
3398     else
3399       initCursor(i, IMG_MENU_BUTTON);
3400   }
3401
3402   game_status = last_game_status;       /* restore current game status */
3403
3404   redraw_mask |= REDRAW_FIELD;
3405 }
3406
3407 static void drawChooseTreeInfo(int entry_pos, TreeInfo *ti)
3408 {
3409   TreeInfo *node, *node_first;
3410   int x, last_redraw_mask = redraw_mask;
3411   int ypos = MENU_TITLE2_YPOS;
3412   int font_nr = FONT_TITLE_2;
3413
3414   if (ti->type == TREE_TYPE_LEVEL_NR)
3415     DrawTextFCentered(ypos, font_nr, leveldir_current->name);
3416
3417   if (ti->type != TREE_TYPE_LEVEL_DIR)
3418     return;
3419
3420   node_first = getTreeInfoFirstGroupEntry(ti);
3421   node = getTreeInfoFromPos(node_first, entry_pos);
3422
3423   DrawBackgroundForFont(SX, SY + ypos, SXSIZE, getFontHeight(font_nr), font_nr);
3424
3425 #if 1
3426   if (node->parent_link)
3427     DrawTextFCentered(ypos, font_nr, "leave \"%s\"",
3428                       node->node_parent->name);
3429   else if (node->level_group)
3430     DrawTextFCentered(ypos, font_nr, "enter \"%s\"",
3431                       node->name);
3432   else if (ti->type == TREE_TYPE_LEVEL_DIR)
3433     DrawTextFCentered(ypos, font_nr, "%3d %s (%s)",
3434                       node->levels, (node->levels > 1 ? "levels" : "level"),
3435                       node->class_desc);
3436 #else
3437   if (node->parent_link)
3438     DrawTextFCentered(ypos, font_nr, "leave group \"%s\"",
3439                       node->class_desc);
3440   else if (node->level_group)
3441     DrawTextFCentered(ypos, font_nr, "enter group \"%s\"",
3442                       node->class_desc);
3443   else if (ti->type == TREE_TYPE_LEVEL_DIR)
3444     DrawTextFCentered(ypos, font_nr, "%3d levels (%s)",
3445                       node->levels, node->class_desc);
3446 #endif
3447
3448   /* let BackToFront() redraw only what is needed */
3449   redraw_mask = last_redraw_mask | REDRAW_TILES;
3450   for (x = 0; x < SCR_FIELDX; x++)
3451     MarkTileDirty(x, 1);
3452 }
3453
3454 static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
3455                              TreeInfo **ti_ptr)
3456 {
3457   TreeInfo *ti = *ti_ptr;
3458   int x = 0;
3459   int y = ti->cl_cursor;
3460   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
3461   int num_entries = numTreeInfoInGroup(ti);
3462   int num_page_entries;
3463   int last_game_status = game_status;   /* save current game status */
3464   boolean position_set_by_scrollbar = (dx == 999);
3465
3466 #if 0
3467   /* force LEVELS draw offset on choose level and artwork setup screen */
3468   game_status = GAME_MODE_LEVELS;
3469 #endif
3470
3471   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
3472     num_page_entries = num_entries;
3473   else
3474     num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
3475
3476   game_status = last_game_status;       /* restore current game status */
3477
3478   if (button == MB_MENU_INITIALIZE)
3479   {
3480     int num_entries = numTreeInfoInGroup(ti);
3481     int entry_pos = posTreeInfo(ti);
3482
3483     if (ti->cl_first == -1)
3484     {
3485       /* only on initialization */
3486       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
3487       ti->cl_cursor = entry_pos - ti->cl_first;
3488     }
3489     else if (ti->cl_cursor >= num_page_entries ||
3490              (num_entries > num_page_entries &&
3491               num_entries - ti->cl_first < num_page_entries))
3492     {
3493       /* only after change of list size (by custom graphic configuration) */
3494       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
3495       ti->cl_cursor = entry_pos - ti->cl_first;
3496     }
3497
3498     if (position_set_by_scrollbar)
3499       ti->cl_first = dy;
3500     else
3501       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
3502                                 ti->cl_first, ti);
3503
3504     drawChooseTreeList(ti->cl_first, num_page_entries, ti);
3505     drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
3506     drawChooseTreeCursor(ti->cl_cursor, TRUE);
3507
3508     return;
3509   }
3510   else if (button == MB_MENU_LEAVE)
3511   {
3512     FadeSetLeaveMenu();
3513
3514     PlaySound(SND_MENU_ITEM_SELECTING);
3515
3516     if (ti->node_parent)
3517     {
3518       *ti_ptr = ti->node_parent;
3519       DrawChooseTree(ti_ptr);
3520     }
3521     else if (game_status == GAME_MODE_SETUP)
3522     {
3523       if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
3524         execSetupGame();
3525       else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE ||
3526                setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
3527         execSetupGraphics();
3528       else
3529         execSetupArtwork();
3530     }
3531     else
3532     {
3533       if (game_status == GAME_MODE_LEVELNR)
3534         level_nr = atoi(level_number_current->identifier);
3535
3536       game_status = GAME_MODE_MAIN;
3537
3538       DrawMainMenuExt(REDRAW_FIELD, FALSE);
3539     }
3540
3541     return;
3542   }
3543
3544   if (mx || my)         /* mouse input */
3545   {
3546     int last_game_status = game_status; /* save current game status */
3547
3548 #if 0
3549     /* force LEVELS draw offset on artwork setup screen */
3550     game_status = GAME_MODE_LEVELS;
3551 #endif
3552
3553     x = (mx - mSX) / 32;
3554     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
3555
3556     game_status = last_game_status;     /* restore current game status */
3557   }
3558   else if (dx || dy)    /* keyboard or scrollbar/scrollbutton input */
3559   {
3560     /* move cursor instead of scrolling when already at start/end of list */
3561     if (dy == -1 * SCROLL_LINE && ti->cl_first == 0)
3562       dy = -1;
3563     else if (dy == +1 * SCROLL_LINE &&
3564              ti->cl_first + num_page_entries == num_entries)
3565       dy = 1;
3566
3567     /* handle scrolling screen one line or page */
3568     if (ti->cl_cursor + dy < 0 ||
3569         ti->cl_cursor + dy > num_page_entries - 1)
3570     {
3571       if (ABS(dy) == SCROLL_PAGE)
3572         step = num_page_entries - 1;
3573
3574       if (dy < 0 && ti->cl_first > 0)
3575       {
3576         /* scroll page/line up */
3577
3578         ti->cl_first -= step;
3579         if (ti->cl_first < 0)
3580           ti->cl_first = 0;
3581
3582         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
3583         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
3584         drawChooseTreeCursor(ti->cl_cursor, TRUE);
3585
3586         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
3587                                   ti->cl_first, ti);
3588       }
3589       else if (dy > 0 && ti->cl_first + num_page_entries < num_entries)
3590       {
3591         /* scroll page/line down */
3592
3593         ti->cl_first += step;
3594         if (ti->cl_first + num_page_entries > num_entries)
3595           ti->cl_first = MAX(0, num_entries - num_page_entries);
3596
3597         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
3598         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
3599         drawChooseTreeCursor(ti->cl_cursor, TRUE);
3600
3601         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
3602                                   ti->cl_first, ti);
3603       }
3604
3605       return;
3606     }
3607
3608     /* handle moving cursor one line */
3609     y = ti->cl_cursor + dy;
3610   }
3611
3612   if (dx == 1)
3613   {
3614     TreeInfo *node_first, *node_cursor;
3615     int entry_pos = ti->cl_first + y;
3616
3617     node_first = getTreeInfoFirstGroupEntry(ti);
3618     node_cursor = getTreeInfoFromPos(node_first, entry_pos);
3619
3620     if (node_cursor->node_group)
3621     {
3622       FadeSetEnterMenu();
3623
3624       PlaySound(SND_MENU_ITEM_SELECTING);
3625
3626       node_cursor->cl_first = ti->cl_first;
3627       node_cursor->cl_cursor = ti->cl_cursor;
3628       *ti_ptr = node_cursor->node_group;
3629       DrawChooseTree(ti_ptr);
3630
3631       return;
3632     }
3633   }
3634   else if (dx == -1 && ti->node_parent)
3635   {
3636     FadeSetLeaveMenu();
3637
3638     PlaySound(SND_MENU_ITEM_SELECTING);
3639
3640     *ti_ptr = ti->node_parent;
3641     DrawChooseTree(ti_ptr);
3642
3643     return;
3644   }
3645
3646   if (!anyScrollbarGadgetActive() &&
3647       IN_VIS_FIELD(x, y) &&
3648       mx < screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x &&
3649       y >= 0 && y < num_page_entries)
3650   {
3651     if (button)
3652     {
3653       if (y != ti->cl_cursor)
3654       {
3655         PlaySound(SND_MENU_ITEM_ACTIVATING);
3656
3657         drawChooseTreeCursor(ti->cl_cursor, FALSE);
3658         drawChooseTreeCursor(y, TRUE);
3659         drawChooseTreeInfo(ti->cl_first + y, ti);
3660
3661         ti->cl_cursor = y;
3662       }
3663     }
3664     else
3665     {
3666       TreeInfo *node_first, *node_cursor;
3667       int entry_pos = ti->cl_first + y;
3668
3669       PlaySound(SND_MENU_ITEM_SELECTING);
3670
3671       node_first = getTreeInfoFirstGroupEntry(ti);
3672       node_cursor = getTreeInfoFromPos(node_first, entry_pos);
3673
3674       if (node_cursor->node_group)
3675       {
3676         FadeSetEnterMenu();
3677
3678         node_cursor->cl_first = ti->cl_first;
3679         node_cursor->cl_cursor = ti->cl_cursor;
3680         *ti_ptr = node_cursor->node_group;
3681         DrawChooseTree(ti_ptr);
3682       }
3683       else if (node_cursor->parent_link)
3684       {
3685         FadeSetLeaveMenu();
3686
3687         *ti_ptr = node_cursor->node_parent;
3688         DrawChooseTree(ti_ptr);
3689       }
3690       else
3691       {
3692         FadeSetEnterScreen();
3693
3694         node_cursor->cl_first = ti->cl_first;
3695         node_cursor->cl_cursor = ti->cl_cursor;
3696         *ti_ptr = node_cursor;
3697
3698         if (ti->type == TREE_TYPE_LEVEL_DIR)
3699         {
3700           LoadLevelSetup_SeriesInfo();
3701
3702           SaveLevelSetup_LastSeries();
3703           SaveLevelSetup_SeriesInfo();
3704           TapeErase();
3705         }
3706
3707         if (game_status == GAME_MODE_SETUP)
3708         {
3709           if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
3710             execSetupGame();
3711           else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE ||
3712                    setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
3713             execSetupGraphics();
3714           else
3715             execSetupArtwork();
3716         }
3717         else
3718         {
3719           if (game_status == GAME_MODE_LEVELNR)
3720             level_nr = atoi(level_number_current->identifier);
3721
3722           game_status = GAME_MODE_MAIN;
3723
3724           DrawMainMenu();
3725         }
3726       }
3727     }
3728   }
3729 }
3730
3731 void DrawChooseLevelSet()
3732 {
3733   SetMainBackgroundImage(IMG_BACKGROUND_LEVELS);
3734
3735   DrawChooseTree(&leveldir_current);
3736
3737   PlayMenuSound();
3738   PlayMenuMusic();
3739 }
3740
3741 void HandleChooseLevelSet(int mx, int my, int dx, int dy, int button)
3742 {
3743   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
3744
3745   DoAnimation();
3746 }
3747
3748 void DrawChooseLevelNr()
3749 {
3750   int i;
3751
3752   if (level_number != NULL)
3753   {
3754     freeTreeInfo(level_number);
3755
3756     level_number = NULL;
3757   }
3758
3759   for (i = leveldir_current->first_level; i <= leveldir_current->last_level;i++)
3760   {
3761     TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_LEVEL_NR);
3762     char identifier[32], name[32];
3763     int value = i;
3764
3765     /* temporarily load level info to get level name */
3766     LoadLevelInfoOnly(i);
3767
3768     ti->node_top = &level_number;
3769     ti->sort_priority = 10000 + value;
3770
3771     sprintf(identifier, "%d", value);
3772     sprintf(name, "%03d: %s", value, level.name);
3773
3774     setString(&ti->identifier, identifier);
3775     setString(&ti->name, name);
3776     setString(&ti->name_sorting, name);
3777
3778     pushTreeInfo(&level_number, ti);
3779   }
3780
3781   /* sort level number values to start with lowest level number */
3782   sortTreeInfo(&level_number);
3783
3784   /* set current level number to current level number */
3785   level_number_current =
3786     getTreeInfoFromIdentifier(level_number, i_to_a(level_nr));
3787
3788   /* if that also fails, set current level number to first available level */
3789   if (level_number_current == NULL)
3790     level_number_current = level_number;
3791
3792   SetMainBackgroundImage(IMG_BACKGROUND_LEVELNR);
3793
3794 #if 1
3795   DrawChooseTree(&level_number_current);
3796 #else
3797   DrawChooseTree(&leveldir_current);
3798 #endif
3799
3800   PlayMenuSound();
3801   PlayMenuMusic();
3802 }
3803
3804 void HandleChooseLevelNr(int mx, int my, int dx, int dy, int button)
3805 {
3806 #if 1
3807   HandleChooseTree(mx, my, dx, dy, button, &level_number_current);
3808 #else
3809   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
3810 #endif
3811
3812   DoAnimation();
3813 }
3814
3815 void DrawHallOfFame(int highlight_position)
3816 {
3817   UnmapAllGadgets();
3818   FadeSoundsAndMusic();
3819
3820   /* (this is needed when called from GameEnd() after winning a game) */
3821   KeyboardAutoRepeatOn();
3822   ActivateJoystick();
3823
3824   /* (this is needed when called from GameEnd() after winning a game) */
3825   SetDrawDeactivationMask(REDRAW_NONE);
3826   SetDrawBackgroundMask(REDRAW_FIELD);
3827
3828   CloseDoor(DOOR_CLOSE_2);
3829
3830   if (highlight_position < 0) 
3831     LoadScore(level_nr);
3832
3833   FadeSetEnterScreen();
3834
3835   // printf("::: %d: %d\n", game_status, menu.enter_screen[game_status]);
3836
3837 #if 1
3838   FadeOut(REDRAW_FIELD);
3839 #endif
3840
3841   InitAnimation();
3842
3843   PlayMenuSound();
3844   PlayMenuMusic();
3845
3846   HandleHallOfFame(highlight_position, 0, 0, 0, MB_MENU_INITIALIZE);
3847
3848 #if 1
3849   FadeIn(REDRAW_FIELD);
3850 #endif
3851 }
3852
3853 static void drawHallOfFameList(int first_entry, int highlight_position)
3854 {
3855   int i;
3856
3857   SetMainBackgroundImage(IMG_BACKGROUND_SCORES);
3858   ClearField();
3859
3860   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, "Hall Of Fame");
3861   DrawTextFCentered(MENU_TITLE2_YPOS, FONT_TITLE_2,
3862                     "HighScores of Level %d", level_nr);
3863
3864   for (i = 0; i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
3865   {
3866     int entry = first_entry + i;
3867     boolean active = (entry == highlight_position);
3868     int font_nr1 = (active ? FONT_TEXT_1_ACTIVE : FONT_TEXT_1);
3869     int font_nr2 = (active ? FONT_TEXT_2_ACTIVE : FONT_TEXT_2);
3870     int font_nr3 = (active ? FONT_TEXT_3_ACTIVE : FONT_TEXT_3);
3871     int font_nr4 = (active ? FONT_TEXT_4_ACTIVE : FONT_TEXT_4);
3872     int dx1 = 3 * getFontWidth(font_nr1);
3873     int dx2 = dx1 + getFontWidth(font_nr1);
3874     int dx3 = dx2 + 25 * getFontWidth(font_nr3);
3875     int sy = mSY + 64 + i * 32;
3876
3877     DrawText(mSX, sy, int2str(entry + 1, 3), font_nr1);
3878     DrawText(mSX + dx1, sy, ".", font_nr1);
3879     DrawText(mSX + dx2, sy, ".........................", font_nr3);
3880
3881     if (!strEqual(highscore[entry].Name, EMPTY_PLAYER_NAME))
3882       DrawText(mSX + dx2, sy, highscore[entry].Name, font_nr2);
3883
3884     DrawText(mSX + dx3, sy, int2str(highscore[entry].Score, 5), font_nr4);
3885   }
3886
3887   redraw_mask |= REDRAW_FIELD;
3888 }
3889
3890 void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
3891 {
3892   static int first_entry = 0;
3893   static int highlight_position = 0;
3894   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
3895
3896   if (button == MB_MENU_INITIALIZE)
3897   {
3898     first_entry = 0;
3899     highlight_position = mx;
3900     drawHallOfFameList(first_entry, highlight_position);
3901
3902     return;
3903   }
3904
3905   if (ABS(dy) == SCROLL_PAGE)           /* handle scrolling one page */
3906     step = NUM_MENU_ENTRIES_ON_SCREEN - 1;
3907
3908   if (dy < 0)
3909   {
3910     if (first_entry > 0)
3911     {
3912       first_entry -= step;
3913       if (first_entry < 0)
3914         first_entry = 0;
3915
3916       drawHallOfFameList(first_entry, highlight_position);
3917     }
3918   }
3919   else if (dy > 0)
3920   {
3921     if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
3922     {
3923       first_entry += step;
3924       if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
3925         first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
3926
3927       drawHallOfFameList(first_entry, highlight_position);
3928     }
3929   }
3930   else if (button == MB_MENU_LEAVE)
3931   {
3932     PlaySound(SND_MENU_ITEM_SELECTING);
3933
3934     FadeSound(SND_BACKGROUND_SCORES);
3935
3936     game_status = GAME_MODE_MAIN;
3937
3938     DrawMainMenu();
3939   }
3940   else if (button == MB_MENU_CHOICE)
3941   {
3942     PlaySound(SND_MENU_ITEM_SELECTING);
3943
3944     FadeSound(SND_BACKGROUND_SCORES);
3945
3946 #if 0
3947     FadeOut(REDRAW_FIELD);
3948 #endif
3949
3950     game_status = GAME_MODE_MAIN;
3951
3952     DrawAndFadeInMainMenu(REDRAW_FIELD);
3953   }
3954
3955   if (game_status == GAME_MODE_SCORES)
3956     PlayMenuSoundIfLoop();
3957
3958   DoAnimation();
3959 }
3960
3961
3962 /* ========================================================================= */
3963 /* setup screen functions                                                    */
3964 /* ========================================================================= */
3965
3966 static struct TokenInfo *setup_info;
3967 static int num_setup_info;
3968
3969 static char *screen_mode_text;
3970 static char *scroll_delay_text;
3971 static char *game_speed_text;
3972 static char *graphics_set_name;
3973 static char *sounds_set_name;
3974 static char *music_set_name;
3975
3976 static void execSetupMain()
3977 {
3978   setup_mode = SETUP_MODE_MAIN;
3979
3980   DrawSetupScreen();
3981 }
3982
3983 static void execSetupGame()
3984 {
3985   if (game_speeds == NULL)
3986   {
3987     int i;
3988
3989     for (i = 0; game_speeds_list[i].value != -1; i++)
3990     {
3991       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
3992       char identifier[32], name[32];
3993       int value = game_speeds_list[i].value;
3994       char *text = game_speeds_list[i].text;
3995
3996       ti->node_top = &game_speeds;
3997       ti->sort_priority = 10000 - value;
3998
3999       sprintf(identifier, "%d", value);
4000       sprintf(name, "%s", text);
4001
4002       setString(&ti->identifier, identifier);
4003       setString(&ti->name, name);
4004       setString(&ti->name_sorting, name);
4005       setString(&ti->infotext, "Game Speed");
4006
4007       pushTreeInfo(&game_speeds, ti);
4008     }
4009
4010     /* sort game speed values to start with slowest game speed */
4011     sortTreeInfo(&game_speeds);
4012
4013     /* set current game speed to configured game speed value */
4014     game_speed_current =
4015       getTreeInfoFromIdentifier(game_speeds, i_to_a(setup.game_frame_delay));
4016
4017     /* if that fails, set current game speed to reliable default value */
4018     if (game_speed_current == NULL)
4019       game_speed_current =
4020         getTreeInfoFromIdentifier(game_speeds, i_to_a(GAME_FRAME_DELAY));
4021
4022     /* if that also fails, set current game speed to first available speed */
4023     if (game_speed_current == NULL)
4024       game_speed_current = game_speeds;
4025   }
4026
4027   setup.game_frame_delay = atoi(game_speed_current->identifier);
4028
4029   /* needed for displaying game speed text instead of identifier */
4030   game_speed_text = game_speed_current->name;
4031
4032   setup_mode = SETUP_MODE_GAME;
4033
4034   DrawSetupScreen();
4035 }
4036
4037 static void execSetupChooseGameSpeed()
4038 {
4039   setup_mode = SETUP_MODE_CHOOSE_GAME_SPEED;
4040
4041   DrawSetupScreen();
4042 }
4043
4044 static void execSetupEditor()
4045 {
4046   setup_mode = SETUP_MODE_EDITOR;
4047
4048   DrawSetupScreen();
4049 }
4050
4051 static void execSetupGraphics()
4052 {
4053   if (video.fullscreen_available && screen_modes == NULL)
4054   {
4055     int i;
4056
4057     for (i = 0; video.fullscreen_modes[i].width != -1; i++)
4058     {
4059       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4060       char identifier[32], name[32];
4061       int x = video.fullscreen_modes[i].width;
4062       int y = video.fullscreen_modes[i].height;
4063       int xx, yy;
4064
4065       get_aspect_ratio_from_screen_mode(&video.fullscreen_modes[i], &xx, &yy);
4066
4067       ti->node_top = &screen_modes;
4068       ti->sort_priority = x * 10000 + y;
4069
4070       sprintf(identifier, "%dx%d", x, y);
4071       sprintf(name, "%d x %d [%d:%d]", x, y, xx, yy);
4072
4073       setString(&ti->identifier, identifier);
4074       setString(&ti->name, name);
4075       setString(&ti->name_sorting, name);
4076       setString(&ti->infotext, "Fullscreen Mode");
4077
4078       pushTreeInfo(&screen_modes, ti);
4079     }
4080
4081     /* sort fullscreen modes to start with lowest available screen resolution */
4082     sortTreeInfo(&screen_modes);
4083
4084     /* set current screen mode for fullscreen mode to configured setup value */
4085     screen_mode_current = getTreeInfoFromIdentifier(screen_modes,
4086                                                     setup.fullscreen_mode);
4087
4088     /* if that fails, set current screen mode to reliable default value */
4089     if (screen_mode_current == NULL)
4090       screen_mode_current = getTreeInfoFromIdentifier(screen_modes,
4091                                                       DEFAULT_FULLSCREEN_MODE);
4092
4093     /* if that also fails, set current screen mode to first available mode */
4094     if (screen_mode_current == NULL)
4095       screen_mode_current = screen_modes;
4096
4097     if (screen_mode_current == NULL)
4098       video.fullscreen_available = FALSE;
4099   }
4100
4101   if (video.fullscreen_available)
4102   {
4103     setup.fullscreen_mode = screen_mode_current->identifier;
4104
4105     /* needed for displaying screen mode name instead of identifier */
4106     screen_mode_text = screen_mode_current->name;
4107   }
4108
4109 #if 1
4110   if (scroll_delays == NULL)
4111   {
4112     int i;
4113
4114     for (i = 0; scroll_delays_list[i].value != -1; i++)
4115     {
4116       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4117       char identifier[32], name[32];
4118       int value = scroll_delays_list[i].value;
4119       char *text = scroll_delays_list[i].text;
4120
4121       ti->node_top = &scroll_delays;
4122       ti->sort_priority = value;
4123
4124       sprintf(identifier, "%d", value);
4125       sprintf(name, "%s", text);
4126
4127       setString(&ti->identifier, identifier);
4128       setString(&ti->name, name);
4129       setString(&ti->name_sorting, name);
4130       setString(&ti->infotext, "Scroll Delay");
4131
4132       pushTreeInfo(&scroll_delays, ti);
4133     }
4134
4135     /* sort scroll delay values to start with lowest scroll delay value */
4136     sortTreeInfo(&scroll_delays);
4137
4138     /* set current scroll delay value to configured scroll delay value */
4139     scroll_delay_current =
4140       getTreeInfoFromIdentifier(scroll_delays,i_to_a(setup.scroll_delay_value));
4141
4142     /* if that fails, set current scroll delay to reliable default value */
4143     if (scroll_delay_current == NULL)
4144       scroll_delay_current =
4145         getTreeInfoFromIdentifier(scroll_delays, i_to_a(STD_SCROLL_DELAY));
4146
4147     /* if that also fails, set current scroll delay to first available value */
4148     if (scroll_delay_current == NULL)
4149       scroll_delay_current = scroll_delays;
4150   }
4151
4152   setup.scroll_delay_value = atoi(scroll_delay_current->identifier);
4153
4154   /* needed for displaying scroll delay text instead of identifier */
4155   scroll_delay_text = scroll_delay_current->name;
4156 #endif
4157
4158   setup_mode = SETUP_MODE_GRAPHICS;
4159   DrawSetupScreen();
4160 }
4161
4162 static void execSetupChooseScreenMode()
4163 {
4164   if (!video.fullscreen_available)
4165     return;
4166
4167   setup_mode = SETUP_MODE_CHOOSE_SCREEN_MODE;
4168
4169   DrawSetupScreen();
4170 }
4171
4172 static void execSetupChooseScrollDelay()
4173 {
4174   setup_mode = SETUP_MODE_CHOOSE_SCROLL_DELAY;
4175
4176   DrawSetupScreen();
4177 }
4178
4179 static void execSetupSound()
4180 {
4181   setup_mode = SETUP_MODE_SOUND;
4182
4183   DrawSetupScreen();
4184 }
4185
4186 static void execSetupArtwork()
4187 {
4188 #if 0
4189   printf("::: '%s', '%s', '%s'\n",
4190          artwork.gfx_current->subdir,
4191          artwork.gfx_current->fullpath,
4192          artwork.gfx_current->basepath);
4193 #endif
4194
4195   setup.graphics_set = artwork.gfx_current->identifier;
4196   setup.sounds_set = artwork.snd_current->identifier;
4197   setup.music_set = artwork.mus_current->identifier;
4198
4199   /* needed if last screen (setup choice) changed graphics, sounds or music */
4200   ReloadCustomArtwork(0);
4201
4202   /* needed for displaying artwork name instead of artwork identifier */
4203   graphics_set_name = artwork.gfx_current->name;
4204   sounds_set_name = artwork.snd_current->name;
4205   music_set_name = artwork.mus_current->name;
4206
4207   setup_mode = SETUP_MODE_ARTWORK;
4208
4209   DrawSetupScreen();
4210 }
4211
4212 static void execSetupChooseGraphics()
4213 {
4214   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
4215
4216   DrawSetupScreen();
4217 }
4218
4219 static void execSetupChooseSounds()
4220 {
4221   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
4222
4223   DrawSetupScreen();
4224 }
4225
4226 static void execSetupChooseMusic()
4227 {
4228   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
4229
4230   DrawSetupScreen();
4231 }
4232
4233 static void execSetupInput()
4234 {
4235   setup_mode = SETUP_MODE_INPUT;
4236
4237   DrawSetupScreen();
4238 }
4239
4240 static void execSetupShortcuts()
4241 {
4242   setup_mode = SETUP_MODE_SHORTCUTS;
4243
4244   DrawSetupScreen();
4245 }
4246
4247 static void execSetupShortcuts1()
4248 {
4249   setup_mode = SETUP_MODE_SHORTCUTS_1;
4250
4251   DrawSetupScreen();
4252 }
4253
4254 static void execSetupShortcuts2()
4255 {
4256   setup_mode = SETUP_MODE_SHORTCUTS_2;
4257
4258   DrawSetupScreen();
4259 }
4260
4261 static void execSetupShortcuts3()
4262 {
4263   setup_mode = SETUP_MODE_SHORTCUTS_3;
4264
4265   DrawSetupScreen();
4266 }
4267
4268 static void execSetupShortcuts4()
4269 {
4270   setup_mode = SETUP_MODE_SHORTCUTS_4;
4271
4272   DrawSetupScreen();
4273 }
4274
4275 static void execSetupShortcuts5()
4276 {
4277   setup_mode = SETUP_MODE_SHORTCUTS_5;
4278
4279   DrawSetupScreen();
4280 }
4281
4282 static void execExitSetup()
4283 {
4284   game_status = GAME_MODE_MAIN;
4285
4286   DrawMainMenuExt(REDRAW_FIELD, FALSE);
4287 }
4288
4289 static void execSaveAndExitSetup()
4290 {
4291   SaveSetup();
4292   execExitSetup();
4293 }
4294
4295 static struct TokenInfo setup_info_main[] =
4296 {
4297   { TYPE_ENTER_MENU,    execSetupGame,          "Game & Menu"           },
4298   { TYPE_ENTER_MENU,    execSetupEditor,        "Editor"                },
4299   { TYPE_ENTER_MENU,    execSetupGraphics,      "Graphics"              },
4300   { TYPE_ENTER_MENU,    execSetupSound,         "Sound & Music"         },
4301   { TYPE_ENTER_MENU,    execSetupArtwork,       "Custom Artwork"        },
4302   { TYPE_ENTER_MENU,    execSetupInput,         "Input Devices"         },
4303   { TYPE_ENTER_MENU,    execSetupShortcuts,     "Key Shortcuts"         },
4304   { TYPE_EMPTY,         NULL,                   ""                      },
4305   { TYPE_LEAVE_MENU,    execExitSetup,          "Exit"                  },
4306   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   "Save and Exit"         },
4307
4308   { 0,                  NULL,                   NULL                    }
4309 };
4310
4311 static struct TokenInfo setup_info_game[] =
4312 {
4313   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode (Multi-Player):" },
4314   { TYPE_YES_NO,        &setup.input_on_focus,  "Only Move Focussed Player:" },
4315   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
4316   { TYPE_SWITCH,        &setup.skip_levels,     "Skip Unsolved Levels:" },
4317   { TYPE_SWITCH,        &setup.time_limit,      "Time Limit:"           },
4318   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record Tapes:"    },
4319   { TYPE_ENTER_LIST,    execSetupChooseGameSpeed, "Game Speed:"         },
4320   { TYPE_STRING,        &game_speed_text,       ""                      },
4321   { TYPE_EMPTY,         NULL,                   ""                      },
4322   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4323
4324   { 0,                  NULL,                   NULL                    }
4325 };
4326
4327 static struct TokenInfo setup_info_editor[] =
4328 {
4329 #if 0
4330   { TYPE_SWITCH,        &setup.editor.el_boulderdash,   "Boulder Dash:" },
4331   { TYPE_SWITCH,        &setup.editor.el_emerald_mine,  "Emerald Mine:" },
4332   { TYPE_SWITCH, &setup.editor.el_emerald_mine_club,    "Emerald Mine Club:" },
4333   { TYPE_SWITCH,        &setup.editor.el_more,          "Rocks'n'Diamonds:" },
4334   { TYPE_SWITCH,        &setup.editor.el_sokoban,       "Sokoban:"      },
4335   { TYPE_SWITCH,        &setup.editor.el_supaplex,      "Supaplex:"     },
4336   { TYPE_SWITCH,        &setup.editor.el_diamond_caves, "Diamond Caves II:" },
4337   { TYPE_SWITCH,        &setup.editor.el_dx_boulderdash,"DX-Boulderdash:" },
4338 #endif
4339   { TYPE_SWITCH,        &setup.editor.el_chars,         "Text Characters:" },
4340   { TYPE_SWITCH, &setup.editor.el_steel_chars, "Text Characters (Steel):" },
4341   { TYPE_SWITCH,        &setup.editor.el_custom,  "Custom & Group Elements:" },
4342 #if 0
4343   { TYPE_SWITCH,        &setup.editor.el_headlines,     "Headlines:"    },
4344 #endif
4345   { TYPE_SWITCH, &setup.editor.el_user_defined, "User defined element list:" },
4346   { TYPE_SWITCH,        &setup.editor.el_dynamic,  "Dynamic level elements:" },
4347   { TYPE_EMPTY,         NULL,                   ""                      },
4348 #if 0
4349   { TYPE_SWITCH,        &setup.editor.el_by_game,   "Show elements by game:" },
4350   { TYPE_SWITCH,        &setup.editor.el_by_type,   "Show elements by type:" },
4351   { TYPE_EMPTY,         NULL,                   ""                      },
4352 #endif
4353   { TYPE_SWITCH, &setup.editor.show_element_token,      "Show element token:" },
4354   { TYPE_EMPTY,         NULL,                   ""                      },
4355   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4356
4357   { 0,                  NULL,                   NULL                    }
4358 };
4359
4360 static struct TokenInfo setup_info_graphics[] =
4361 {
4362   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen:"           },
4363   { TYPE_ENTER_LIST,    execSetupChooseScreenMode, "Fullscreen Mode:"   },
4364   { TYPE_STRING,        &screen_mode_text,      ""                      },
4365 #if 0
4366   { TYPE_SWITCH,        &setup.scroll_delay,    "Scroll Delay:"         },
4367 #endif
4368   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay Value:" },
4369   { TYPE_STRING,        &scroll_delay_text,     ""                      },
4370 #if 0
4371   { TYPE_SWITCH,        &setup.soft_scrolling,  "Soft Scrolling:"       },
4372 #endif
4373   { TYPE_SWITCH,        &setup.fade_screens,    "Fade Screens:"         },
4374   { TYPE_SWITCH,        &setup.quick_switch,    "Quick Player Focus Switch:" },
4375   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Menu Doors:"     },
4376   { TYPE_SWITCH,        &setup.show_titlescreen,"Show Title Screens:"   },
4377   { TYPE_SWITCH,        &setup.toons,           "Show Toons:"           },
4378   { TYPE_ECS_AGA,       &setup.prefer_aga_graphics,"EMC graphics preference:" },
4379   { TYPE_SWITCH, &setup.sp_show_border_elements,"Supaplex Border Elements:" },
4380   { TYPE_SWITCH,        &setup.small_game_graphics, "Small Game Graphics:" },
4381   { TYPE_EMPTY,         NULL,                   ""                      },
4382   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4383
4384   { 0,                  NULL,                   NULL                    }
4385 };
4386
4387 static struct TokenInfo setup_info_sound[] =
4388 {
4389   { TYPE_SWITCH,        &setup.sound_simple,    "Sound Effects (Normal):"  },
4390   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Effects (Looping):" },
4391   { TYPE_SWITCH,        &setup.sound_music,     "Music:"                },
4392   { TYPE_EMPTY,         NULL,                   ""                      },
4393   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4394
4395   { 0,                  NULL,                   NULL                    }
4396 };
4397
4398 static struct TokenInfo setup_info_artwork[] =
4399 {
4400   { TYPE_ENTER_LIST,    execSetupChooseGraphics,"Custom Graphics:"      },
4401   { TYPE_STRING,        &graphics_set_name,     ""                      },
4402   { TYPE_ENTER_LIST,    execSetupChooseSounds,  "Custom Sounds:"        },
4403   { TYPE_STRING,        &sounds_set_name,       ""                      },
4404   { TYPE_ENTER_LIST,    execSetupChooseMusic,   "Custom Music:"         },
4405   { TYPE_STRING,        &music_set_name,        ""                      },
4406   { TYPE_EMPTY,         NULL,                   ""                      },
4407 #if 1
4408 #if 1
4409   { TYPE_YES_NO_AUTO,&setup.override_level_graphics,"Override Level Graphics:"},
4410   { TYPE_YES_NO_AUTO,&setup.override_level_sounds,  "Override Level Sounds:"  },
4411   { TYPE_YES_NO_AUTO,&setup.override_level_music,   "Override Level Music:"   },
4412 #else
4413   { TYPE_YES_NO, &setup.override_level_graphics,"Override Level Graphics:" },
4414   { TYPE_YES_NO, &setup.override_level_sounds,  "Override Level Sounds:"   },
4415   { TYPE_YES_NO, &setup.override_level_music,   "Override Level Music:"    },
4416   { TYPE_YES_NO, &setup.auto_override_artwork,  "Auto-Override Non-CE Sets:" },
4417 #endif
4418 #else
4419   { TYPE_STRING,        NULL,                   "Override Level Artwork:"},
4420   { TYPE_YES_NO,        &setup.override_level_graphics, "Graphics:"     },
4421   { TYPE_YES_NO,        &setup.override_level_sounds,   "Sounds:"       },
4422   { TYPE_YES_NO,        &setup.override_level_music,    "Music:"        },
4423 #endif
4424   { TYPE_EMPTY,         NULL,                   ""                      },
4425   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4426
4427   { 0,                  NULL,                   NULL                    }
4428 };
4429
4430 static struct TokenInfo setup_info_input[] =
4431 {
4432   { TYPE_SWITCH,        NULL,                   "Player:"               },
4433   { TYPE_SWITCH,        NULL,                   "Device:"               },
4434   { TYPE_ENTER_MENU,    NULL,                   ""                      },
4435   { TYPE_EMPTY,         NULL,                   ""                      },
4436   { TYPE_EMPTY,         NULL,                   ""                      },
4437   { TYPE_EMPTY,         NULL,                   ""                      },
4438   { TYPE_EMPTY,         NULL,                   ""                      },
4439   { TYPE_EMPTY,         NULL,                   ""                      },
4440   { TYPE_EMPTY,         NULL,                   ""                      },
4441   { TYPE_EMPTY,         NULL,                   ""                      },
4442   { TYPE_EMPTY,         NULL,                   ""                      },
4443   { TYPE_EMPTY,         NULL,                   ""                      },
4444   { TYPE_EMPTY,         NULL,                   ""                      },
4445   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4446
4447   { 0,                  NULL,                   NULL                    }
4448 };
4449
4450 static struct TokenInfo setup_info_shortcuts[] =
4451 {
4452   { TYPE_ENTER_MENU,    execSetupShortcuts1,    "Various Keys"          },
4453   { TYPE_ENTER_MENU,    execSetupShortcuts2,    "Player Focus"          },
4454   { TYPE_ENTER_MENU,    execSetupShortcuts3,    "Tape Buttons"          },
4455   { TYPE_ENTER_MENU,    execSetupShortcuts4,    "Sound & Music"         },
4456   { TYPE_ENTER_MENU,    execSetupShortcuts5,    "TAS Snap Keys"         },
4457   { TYPE_EMPTY,         NULL,                   ""                      },
4458   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4459
4460   { 0,                  NULL,                   NULL                    }
4461 };
4462
4463 static struct TokenInfo setup_info_shortcuts_1[] =
4464 {
4465   { TYPE_KEYTEXT,       NULL,           "Quick Save Game to Tape:",     },
4466   { TYPE_KEY,           &setup.shortcut.save_game, ""                   },
4467   { TYPE_KEYTEXT,       NULL,           "Quick Load Game from Tape:",   },
4468   { TYPE_KEY,           &setup.shortcut.load_game, ""                   },
4469   { TYPE_KEYTEXT,       NULL,           "Start Game & Toggle Pause:",   },
4470   { TYPE_KEY,           &setup.shortcut.toggle_pause, ""                },
4471   { TYPE_EMPTY,         NULL,                   ""                      },
4472   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on 'Esc' Key:"     },
4473   { TYPE_YES_NO, &setup.ask_on_escape_editor,   "Ask on 'Esc' Key (Editor):" },
4474   { TYPE_EMPTY,         NULL,                   ""                      },
4475   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4476
4477   { 0,                  NULL,                   NULL                    }
4478 };
4479
4480 static struct TokenInfo setup_info_shortcuts_2[] =
4481 {
4482   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 1:",       },
4483   { TYPE_KEY,           &setup.shortcut.focus_player[0], ""             },
4484   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 2:",       },
4485   { TYPE_KEY,           &setup.shortcut.focus_player[1], ""             },
4486   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 3:",       },
4487   { TYPE_KEY,           &setup.shortcut.focus_player[2], ""             },
4488   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 4:",       },
4489   { TYPE_KEY,           &setup.shortcut.focus_player[3], ""             },
4490   { TYPE_KEYTEXT,       NULL,           "Set Focus to All Players:",    },
4491   { TYPE_KEY,           &setup.shortcut.focus_player_all, ""            },
4492   { TYPE_EMPTY,         NULL,                   ""                      },
4493   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4494
4495   { 0,                  NULL,                   NULL                    }
4496 };
4497
4498 static struct TokenInfo setup_info_shortcuts_3[] =
4499 {
4500   { TYPE_KEYTEXT,       NULL,                   "Eject Tape:",          },
4501   { TYPE_KEY,           &setup.shortcut.tape_eject, ""                  },
4502   { TYPE_KEYTEXT,       NULL,                   "Warp / Single Step:",  },
4503   { TYPE_KEY,           &setup.shortcut.tape_extra, ""                  },
4504   { TYPE_KEYTEXT,       NULL,                   "Stop Tape:",           },
4505   { TYPE_KEY,           &setup.shortcut.tape_stop, ""                   },
4506   { TYPE_KEYTEXT,       NULL,                   "Pause / Unpause Tape:",},
4507   { TYPE_KEY,           &setup.shortcut.tape_pause, ""                  },
4508   { TYPE_KEYTEXT,       NULL,                   "Record Tape:",         },
4509   { TYPE_KEY,           &setup.shortcut.tape_record, ""                 },
4510   { TYPE_KEYTEXT,       NULL,                   "Play Tape:",           },
4511   { TYPE_KEY,           &setup.shortcut.tape_play, ""                   },
4512   { TYPE_EMPTY,         NULL,                   ""                      },
4513   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4514
4515   { 0,                  NULL,                   NULL                    }
4516 };
4517
4518 static struct TokenInfo setup_info_shortcuts_4[] =
4519 {
4520   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Normal):", },
4521   { TYPE_KEY,           &setup.shortcut.sound_simple, ""                },
4522   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Looping):", },
4523   { TYPE_KEY,           &setup.shortcut.sound_loops, ""                 },
4524   { TYPE_KEYTEXT,       NULL,           "Toggle Music:",                },
4525   { TYPE_KEY,           &setup.shortcut.sound_music, ""                 },
4526   { TYPE_EMPTY,         NULL,                   ""                      },
4527   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4528
4529   { 0,                  NULL,                   NULL                    }
4530 };
4531
4532 static struct TokenInfo setup_info_shortcuts_5[] =
4533 {
4534   { TYPE_KEYTEXT,       NULL,                   "Snap Left:",           },
4535   { TYPE_KEY,           &setup.shortcut.snap_left, ""                   },
4536   { TYPE_KEYTEXT,       NULL,                   "Snap Right:",          },
4537   { TYPE_KEY,           &setup.shortcut.snap_right, ""                  },
4538   { TYPE_KEYTEXT,       NULL,                   "Snap Up:",             },
4539   { TYPE_KEY,           &setup.shortcut.snap_up, ""                     },
4540   { TYPE_KEYTEXT,       NULL,                   "Snap Down:",           },
4541   { TYPE_KEY,           &setup.shortcut.snap_down, ""                   },
4542   { TYPE_EMPTY,         NULL,                   ""                      },
4543   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4544
4545   { 0,                  NULL,                   NULL                    }
4546 };
4547
4548 static Key getSetupKey()
4549 {
4550   Key key = KSYM_UNDEFINED;
4551   boolean got_key_event = FALSE;
4552
4553   while (!got_key_event)
4554   {
4555     if (PendingEvent())         /* got event */
4556     {
4557       Event event;
4558
4559       NextEvent(&event);
4560
4561       switch (event.type)
4562       {
4563         case EVENT_KEYPRESS:
4564           {
4565             key = GetEventKey((KeyEvent *)&event, TRUE);
4566
4567             /* press 'Escape' or 'Enter' to keep the existing key binding */
4568             if (key == KSYM_Escape || key == KSYM_Return)
4569               key = KSYM_UNDEFINED;     /* keep old value */
4570
4571             got_key_event = TRUE;
4572           }
4573           break;
4574
4575         case EVENT_KEYRELEASE:
4576           key_joystick_mapping = 0;
4577           break;
4578
4579         default:
4580           HandleOtherEvents(&event);
4581           break;
4582       }
4583     }
4584
4585     DoAnimation();
4586     BackToFront();
4587
4588     /* don't eat all CPU time */
4589     Delay(10);
4590   }
4591
4592   return key;
4593 }
4594
4595 static int getSetupTextFont(int type)
4596 {
4597   if (type & (TYPE_SWITCH       |
4598               TYPE_YES_NO       |
4599               TYPE_YES_NO_AUTO  |
4600               TYPE_STRING       |
4601               TYPE_ECS_AGA      |
4602               TYPE_KEYTEXT      |
4603               TYPE_ENTER_LIST))
4604     return FONT_MENU_2;
4605   else
4606     return FONT_MENU_1;
4607 }
4608
4609 static int getSetupValueFont(int type, void *value)
4610 {
4611   if (type & TYPE_KEY)
4612     return (type & TYPE_QUERY ? FONT_INPUT_1_ACTIVE : FONT_VALUE_1);
4613   else if (type & TYPE_STRING)
4614     return FONT_VALUE_2;
4615   else if (type & TYPE_ECS_AGA)
4616     return FONT_VALUE_1;
4617   else if (type & TYPE_BOOLEAN_STYLE)
4618     return (*(boolean *)value ? FONT_OPTION_ON : FONT_OPTION_OFF);
4619   else if (type & TYPE_YES_NO_AUTO)
4620     return (*(int *)value == AUTO  ? FONT_OPTION_ON :
4621             *(int *)value == FALSE ? FONT_OPTION_OFF : FONT_OPTION_ON);
4622   else
4623     return FONT_VALUE_1;
4624 }
4625
4626 static void drawSetupValue(int pos)
4627 {
4628   boolean font_draw_xoffset_modified = FALSE;
4629   int font_draw_xoffset_old = -1;
4630   int xpos = MENU_SCREEN_VALUE_XPOS;
4631   int ypos = MENU_SCREEN_START_YPOS + pos;
4632   int startx = mSX + xpos * 32;
4633   int starty = mSY + ypos * 32;
4634   int font_nr, font_width, font_height;
4635   int type = setup_info[pos].type;
4636   void *value = setup_info[pos].value;
4637   char *value_string = getSetupValue(type, value);
4638 #if 1
4639   int i;
4640 #endif
4641
4642   if (value_string == NULL)
4643     return;
4644
4645   if (type & TYPE_KEY)
4646   {
4647     xpos = MENU_SCREEN_START_XPOS;
4648
4649     if (type & TYPE_QUERY)
4650       value_string = "<press key>";
4651   }
4652   else if (type & TYPE_STRING)
4653   {
4654     int max_value_len = (SCR_FIELDX - 2) * 2;
4655
4656     xpos = MENU_SCREEN_START_XPOS;
4657
4658     if (strlen(value_string) > max_value_len)
4659       value_string[max_value_len] = '\0';
4660   }
4661   else if (type & TYPE_YES_NO_AUTO)
4662   {
4663     xpos = MENU_SCREEN_VALUE_XPOS - 1;
4664   }
4665
4666   startx = mSX + xpos * 32;
4667   starty = mSY + ypos * 32;
4668   font_nr = getSetupValueFont(type, value);
4669   font_width = getFontWidth(font_nr);
4670   font_height = getFontHeight(font_nr);
4671
4672   /* downward compatibility correction for Juergen Bonhagen's menu settings */
4673   if (setup_mode != SETUP_MODE_INPUT)
4674   {
4675     int check_font_nr = FONT_OPTION_ON; /* known font that needs correction */
4676     int font1_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
4677     int font2_xoffset = getFontBitmapInfo(check_font_nr)->draw_xoffset;
4678     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
4679     int text_font_nr = getSetupTextFont(FONT_MENU_2);
4680     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
4681     int text_width = MAX_MENU_TEXT_LENGTH_MEDIUM * getFontWidth(text_font_nr);
4682     boolean correct_font_draw_xoffset = FALSE;
4683
4684     if (xpos == MENU_SCREEN_START_XPOS &&
4685         startx + font1_xoffset < text_startx + text_font_xoffset)
4686       correct_font_draw_xoffset = TRUE;
4687
4688     if (xpos == MENU_SCREEN_VALUE_XPOS &&
4689         startx + font2_xoffset < text_startx + text_width + text_font_xoffset)
4690       correct_font_draw_xoffset = TRUE;
4691
4692     /* check if setup value would overlap with setup text when printed */
4693     /* (this can happen for extreme/wrong values for font draw offset) */
4694     if (correct_font_draw_xoffset)
4695     {
4696       font_draw_xoffset_old = getFontBitmapInfo(font_nr)->draw_xoffset;
4697       font_draw_xoffset_modified = TRUE;
4698
4699       if (type & TYPE_KEY)
4700         getFontBitmapInfo(font_nr)->draw_xoffset += 2 * getFontWidth(font_nr);
4701       else if (!(type & TYPE_STRING))
4702         getFontBitmapInfo(font_nr)->draw_xoffset = text_font_xoffset + 20 -
4703           MAX_MENU_TEXT_LENGTH_MEDIUM * (16 - getFontWidth(text_font_nr));
4704     }
4705   }
4706
4707 #if 0
4708   DrawBackground(startx, starty, SX + SXSIZE - startx, font_height);
4709 #else
4710   for (i = 0; i <= MENU_SCREEN_MAX_XPOS - xpos; i++)
4711     DrawText(startx + i * font_width, starty, " ", font_nr);
4712 #endif
4713
4714   DrawText(startx, starty, value_string, font_nr);
4715
4716   if (font_draw_xoffset_modified)
4717     getFontBitmapInfo(font_nr)->draw_xoffset = font_draw_xoffset_old;
4718 }
4719
4720 static void changeSetupValue(int pos, int dx)
4721 {
4722   if (setup_info[pos].type & TYPE_BOOLEAN_STYLE)
4723   {
4724     *(boolean *)setup_info[pos].value ^= TRUE;
4725   }
4726   else if (setup_info[pos].type & TYPE_YES_NO_AUTO)
4727   {
4728     *(int *)setup_info[pos].value =
4729       (dx == -1 ?
4730        (*(int *)setup_info[pos].value == AUTO ? TRUE :
4731         *(int *)setup_info[pos].value == TRUE ? FALSE : AUTO) :
4732        (*(int *)setup_info[pos].value == TRUE ? AUTO :
4733         *(int *)setup_info[pos].value == AUTO ? FALSE : TRUE));
4734   }
4735   else if (setup_info[pos].type & TYPE_KEY)
4736   {
4737     Key key;
4738
4739     setup_info[pos].type |= TYPE_QUERY;
4740     drawSetupValue(pos);
4741     setup_info[pos].type &= ~TYPE_QUERY;
4742
4743     key = getSetupKey();
4744     if (key != KSYM_UNDEFINED)
4745       *(Key *)setup_info[pos].value = key;
4746   }
4747
4748   drawSetupValue(pos);
4749 }
4750
4751 static void DrawCursorAndText_Setup(int pos, boolean active)
4752 {
4753   int xpos = MENU_SCREEN_START_XPOS;
4754   int ypos = MENU_SCREEN_START_YPOS + pos;
4755   int font_nr = getSetupTextFont(setup_info[pos].type);
4756
4757   if (setup_info == setup_info_input)
4758     font_nr = FONT_MENU_1;
4759
4760   if (active)
4761     font_nr = FONT_ACTIVE(font_nr);
4762
4763   DrawText(mSX + xpos * 32, mSY + ypos * 32, setup_info[pos].text, font_nr);
4764
4765   if (setup_info[pos].type & ~TYPE_SKIP_ENTRY)
4766     drawCursor(pos, active);
4767 }
4768
4769 static void DrawSetupScreen_Generic()
4770 {
4771   boolean redraw_all = FALSE;
4772   char *title_string = NULL;
4773   int i;
4774
4775   UnmapAllGadgets();
4776   CloseDoor(DOOR_CLOSE_2);
4777
4778   if (redraw_mask & REDRAW_ALL)
4779     redraw_all = TRUE;
4780
4781 #if 0
4782   printf("::: %s\n", (redraw_mask & REDRAW_FIELD ? "REDRAW_FIELD" :
4783                       redraw_mask & REDRAW_ALL ? "REDRAW_ALL" :
4784                       int2str(0, redraw_mask)));
4785 #endif
4786
4787 #if 0
4788   /* !!! usually REDRAW_NONE => DOES NOT WORK (with fade) => CHECK THIS !!! */
4789   FadeOut(redraw_mask);
4790 #else
4791   FadeOut(REDRAW_FIELD);
4792 #endif
4793
4794   ClearField();
4795
4796   if (setup_mode == SETUP_MODE_MAIN)
4797   {
4798     setup_info = setup_info_main;
4799     title_string = "Setup";
4800   }
4801   else if (setup_mode == SETUP_MODE_GAME)
4802   {
4803     setup_info = setup_info_game;
4804     title_string = "Setup Game";
4805   }
4806   else if (setup_mode == SETUP_MODE_EDITOR)
4807   {
4808     setup_info = setup_info_editor;
4809     title_string = "Setup Editor";
4810   }
4811   else if (setup_mode == SETUP_MODE_GRAPHICS)
4812   {
4813     setup_info = setup_info_graphics;
4814     title_string = "Setup Graphics";
4815   }
4816   else if (setup_mode == SETUP_MODE_SOUND)
4817   {
4818     setup_info = setup_info_sound;
4819     title_string = "Setup Sound";
4820   }
4821   else if (setup_mode == SETUP_MODE_ARTWORK)
4822   {
4823     setup_info = setup_info_artwork;
4824     title_string = "Custom Artwork";
4825   }
4826   else if (setup_mode == SETUP_MODE_SHORTCUTS)
4827   {
4828     setup_info = setup_info_shortcuts;
4829     title_string = "Setup Shortcuts";
4830   }
4831   else if (setup_mode == SETUP_MODE_SHORTCUTS_1)
4832   {
4833     setup_info = setup_info_shortcuts_1;
4834     title_string = "Setup Shortcuts";
4835   }
4836   else if (setup_mode == SETUP_MODE_SHORTCUTS_2)
4837   {
4838     setup_info = setup_info_shortcuts_2;
4839     title_string = "Setup Shortcuts";
4840   }
4841   else if (setup_mode == SETUP_MODE_SHORTCUTS_3)
4842   {
4843     setup_info = setup_info_shortcuts_3;
4844     title_string = "Setup Shortcuts";
4845   }
4846   else if (setup_mode == SETUP_MODE_SHORTCUTS_4)
4847   {
4848     setup_info = setup_info_shortcuts_4;
4849     title_string = "Setup Shortcuts";
4850   }
4851   else if (setup_mode == SETUP_MODE_SHORTCUTS_5)
4852   {
4853     setup_info = setup_info_shortcuts_5;
4854     title_string = "Setup Shortcuts";
4855   }
4856
4857   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, title_string);
4858
4859   num_setup_info = 0;
4860 #if 1
4861   for (i = 0; setup_info[i].type != 0 && i < MAX_MENU_ENTRIES_ON_SCREEN; i++)
4862 #else
4863   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
4864 #endif
4865   {
4866     void *value_ptr = setup_info[i].value;
4867
4868     /* set some entries to "unchangeable" according to other variables */
4869     if ((value_ptr == &setup.sound_simple && !audio.sound_available) ||
4870         (value_ptr == &setup.sound_loops  && !audio.loops_available) ||
4871         (value_ptr == &setup.sound_music  && !audio.music_available) ||
4872         (value_ptr == &setup.fullscreen   && !video.fullscreen_available) ||
4873         (value_ptr == &screen_mode_text   && !video.fullscreen_available))
4874       setup_info[i].type |= TYPE_GHOSTED;
4875
4876     if (setup_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
4877       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
4878     else if (setup_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
4879       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
4880     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
4881       initCursor(i, IMG_MENU_BUTTON);
4882
4883     DrawCursorAndText_Setup(i, FALSE);
4884
4885     if (setup_info[i].type & TYPE_VALUE)
4886       drawSetupValue(i);
4887
4888     num_setup_info++;
4889   }
4890
4891 #if 0
4892   DrawTextSCentered(SYSIZE - 20, FONT_TEXT_4,
4893                     "Joysticks deactivated in setup menu");
4894 #endif
4895
4896 #if 1
4897   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
4898 #endif
4899
4900   if (redraw_all)
4901     redraw_mask = REDRAW_ALL;
4902
4903 #if 1
4904   FadeIn(redraw_mask);
4905 #else
4906   FadeIn(REDRAW_FIELD);
4907 #endif
4908
4909   InitAnimation();
4910 #if 0
4911   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
4912 #endif
4913 }
4914
4915 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
4916 {
4917   static int choice_store[MAX_SETUP_MODES];
4918   int choice = choice_store[setup_mode];        /* always starts with 0 */
4919   int x = 0;
4920   int y = choice;
4921
4922   if (button == MB_MENU_INITIALIZE)
4923   {
4924     /* advance to first valid menu entry */
4925     while (choice < num_setup_info &&
4926            setup_info[choice].type & TYPE_SKIP_ENTRY)
4927       choice++;
4928     choice_store[setup_mode] = choice;
4929
4930     DrawCursorAndText_Setup(choice, TRUE);
4931
4932     return;
4933   }
4934   else if (button == MB_MENU_LEAVE)
4935   {
4936     PlaySound(SND_MENU_ITEM_SELECTING);
4937
4938     for (y = 0; y < num_setup_info; y++)
4939     {
4940       if (setup_info[y].type & TYPE_LEAVE_MENU)
4941       {
4942         void (*menu_callback_function)(void) = setup_info[y].value;
4943
4944         FadeSetLeaveMenu();
4945
4946         menu_callback_function();
4947
4948         break;  /* absolutely needed because function changes 'setup_info'! */
4949       }
4950     }
4951
4952     return;
4953   }
4954
4955   if (mx || my)         /* mouse input */
4956   {
4957     x = (mx - mSX) / 32;
4958     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
4959   }
4960   else if (dx || dy)    /* keyboard input */
4961   {
4962     if (dx)
4963     {
4964       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE : TYPE_ENTER);
4965
4966       if (setup_info[choice].type & menu_navigation_type ||
4967           setup_info[choice].type & TYPE_BOOLEAN_STYLE ||
4968           setup_info[choice].type & TYPE_YES_NO_AUTO)
4969         button = MB_MENU_CHOICE;
4970     }
4971     else if (dy)
4972       y = choice + dy;
4973
4974     /* jump to next non-empty menu entry (up or down) */
4975     while (y > 0 && y < num_setup_info - 1 &&
4976            setup_info[y].type & TYPE_SKIP_ENTRY)
4977       y += dy;
4978   }
4979
4980   if (IN_VIS_FIELD(x, y) && y >= 0 && y < num_setup_info)
4981   {
4982     if (button)
4983     {
4984       if (y != choice && setup_info[y].type & ~TYPE_SKIP_ENTRY)
4985       {
4986         PlaySound(SND_MENU_ITEM_ACTIVATING);
4987
4988         DrawCursorAndText_Setup(choice, FALSE);
4989         DrawCursorAndText_Setup(y, TRUE);
4990
4991         choice = choice_store[setup_mode] = y;
4992       }
4993     }
4994     else if (!(setup_info[y].type & TYPE_GHOSTED))
4995     {
4996       PlaySound(SND_MENU_ITEM_SELECTING);
4997
4998       /* when selecting key headline, execute function for key value change */
4999       if (setup_info[y].type & TYPE_KEYTEXT &&
5000           setup_info[y + 1].type & TYPE_KEY)
5001         y++;
5002
5003       /* when selecting string value, execute function for list selection */
5004       if (setup_info[y].type & TYPE_STRING && y > 0 &&
5005           setup_info[y - 1].type & TYPE_ENTER_LIST)
5006         y--;
5007
5008       if (setup_info[y].type & TYPE_ENTER_OR_LEAVE)
5009       {
5010         void (*menu_callback_function)(void) = setup_info[y].value;
5011
5012         FadeSetFromType(setup_info[y].type);
5013
5014         menu_callback_function();
5015       }
5016       else
5017       {
5018         if (setup_info[y].type & TYPE_VALUE)
5019           changeSetupValue(y, dx);
5020       }
5021     }
5022   }
5023 }
5024
5025 void DrawSetupScreen_Input()
5026 {
5027   int i;
5028
5029 #if 1
5030   FadeOut(REDRAW_FIELD);
5031 #endif
5032
5033   ClearField();
5034
5035 #if 1
5036   setup_info = setup_info_input;
5037 #endif
5038
5039   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Setup Input");
5040
5041 #if 1
5042 #if 1
5043   DrawTextSCentered(SYSIZE - 20, FONT_TITLE_2,
5044                     "Joysticks deactivated on this screen");
5045 #else
5046   DrawTextSCentered(SYSIZE - 20, FONT_TEXT_4,
5047                     "Joysticks deactivated on this screen");
5048 #endif
5049 #endif
5050
5051 #if 1
5052   for (i = 0; setup_info[i].type != 0 && i < MAX_MENU_ENTRIES_ON_SCREEN; i++)
5053 #else
5054   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
5055 #endif
5056   {
5057     if (setup_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
5058       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
5059     else if (setup_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
5060       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
5061     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
5062       initCursor(i, IMG_MENU_BUTTON);
5063
5064     DrawCursorAndText_Setup(i, FALSE);
5065   }
5066
5067 #if 0
5068   DeactivateJoystickForCalibration();
5069 #endif
5070
5071 #if 0
5072 #if 1
5073   DrawTextSCentered(SYSIZE - 20, FONT_TITLE_2,
5074                     "Joysticks deactivated on this screen");
5075 #else
5076   DrawTextSCentered(SYSIZE - 20, FONT_TEXT_4,
5077                     "Joysticks deactivated on this screen");
5078 #endif
5079 #endif
5080
5081   /* create gadgets for setup input menu screen */
5082   FreeScreenGadgets();
5083   CreateScreenGadgets();
5084
5085   /* map gadgets for setup input menu screen */
5086   MapScreenMenuGadgets(SCREEN_MASK_INPUT);
5087
5088   HandleSetupScreen_Input(0, 0, 0, 0, MB_MENU_INITIALIZE);
5089
5090 #if 1
5091   FadeIn(REDRAW_FIELD);
5092 #endif
5093
5094   InitAnimation();
5095 }
5096
5097 static void setJoystickDeviceToNr(char *device_name, int device_nr)
5098 {
5099   if (device_name == NULL)
5100     return;
5101
5102   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
5103     device_nr = 0;
5104
5105   if (strlen(device_name) > 1)
5106   {
5107     char c1 = device_name[strlen(device_name) - 1];
5108     char c2 = device_name[strlen(device_name) - 2];
5109
5110     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
5111       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
5112   }
5113   else
5114     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
5115             strlen(device_name));
5116 }
5117
5118 static void drawPlayerSetupInputInfo(int player_nr, boolean active)
5119 {
5120   int i;
5121   static struct SetupKeyboardInfo custom_key;
5122   static struct
5123   {
5124     Key *key;
5125     char *text;
5126   } custom[] =
5127   {
5128     { &custom_key.left,  "Joystick Left"  },
5129     { &custom_key.right, "Joystick Right" },
5130     { &custom_key.up,    "Joystick Up"    },
5131     { &custom_key.down,  "Joystick Down"  },
5132     { &custom_key.snap,  "Button 1"       },
5133     { &custom_key.drop,  "Button 2"       }
5134   };
5135   static char *joystick_name[MAX_PLAYERS] =
5136   {
5137     "Joystick1",
5138     "Joystick2",
5139     "Joystick3",
5140     "Joystick4"
5141   };
5142   int text_font_nr = (active ? FONT_MENU_1_ACTIVE : FONT_MENU_1);
5143
5144   InitJoysticks();
5145
5146   custom_key = setup.input[player_nr].key;
5147
5148   DrawText(mSX + 11 * 32, mSY + 2 * 32, int2str(player_nr + 1, 1),
5149            FONT_INPUT_1_ACTIVE);
5150
5151   ClearRectangleOnBackground(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
5152                              TILEX, TILEY);
5153   DrawFixedGraphicThruMaskExt(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
5154                               PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
5155
5156   if (setup.input[player_nr].use_joystick)
5157   {
5158     char *device_name = setup.input[player_nr].joy.device_name;
5159     char *text = joystick_name[getJoystickNrFromDeviceName(device_name)];
5160     int font_nr = (joystick.fd[player_nr] < 0 ? FONT_VALUE_OLD : FONT_VALUE_1);
5161
5162     DrawText(mSX + 8 * 32, mSY + 3 * 32, text, font_nr);
5163     DrawText(mSX + 32, mSY + 4 * 32, "Calibrate", text_font_nr);
5164   }
5165   else
5166   {
5167     DrawText(mSX + 8 * 32, mSY + 3 * 32, "Keyboard ", FONT_VALUE_1);
5168     DrawText(mSX + 1 * 32, mSY + 4 * 32, "Customize", text_font_nr);
5169   }
5170
5171   DrawText(mSX + 32, mSY + 5 * 32, "Actual Settings:", FONT_MENU_1);
5172
5173   drawCursorXY(1, 4, IMG_MENU_BUTTON_LEFT);
5174   drawCursorXY(1, 5, IMG_MENU_BUTTON_RIGHT);
5175   drawCursorXY(1, 6, IMG_MENU_BUTTON_UP);
5176   drawCursorXY(1, 7, IMG_MENU_BUTTON_DOWN);
5177
5178   DrawText(mSX + 2 * 32, mSY +  6 * 32, ":", FONT_VALUE_OLD);
5179   DrawText(mSX + 2 * 32, mSY +  7 * 32, ":", FONT_VALUE_OLD);
5180   DrawText(mSX + 2 * 32, mSY +  8 * 32, ":", FONT_VALUE_OLD);
5181   DrawText(mSX + 2 * 32, mSY +  9 * 32, ":", FONT_VALUE_OLD);
5182   DrawText(mSX + 1 * 32, mSY + 10 * 32, "Snap Field:", FONT_VALUE_OLD);
5183   DrawText(mSX + 1 * 32, mSY + 12 * 32, "Drop Element:", FONT_VALUE_OLD);
5184
5185   for (i = 0; i < 6; i++)
5186   {
5187     int ypos = 6 + i + (i > 3 ? i-3 : 0);
5188
5189     DrawText(mSX + 3 * 32, mSY + ypos * 32,
5190              "              ", FONT_VALUE_1);
5191     DrawText(mSX + 3 * 32, mSY + ypos * 32,
5192              (setup.input[player_nr].use_joystick ?
5193               custom[i].text :
5194               getKeyNameFromKey(*custom[i].key)), FONT_VALUE_1);
5195   }
5196 }
5197
5198 static int input_player_nr = 0;
5199
5200 void HandleSetupScreen_Input_Player(int step, int direction)
5201 {
5202   int old_player_nr = input_player_nr;
5203   int new_player_nr;
5204
5205   new_player_nr = old_player_nr + step * direction;
5206   if (new_player_nr < 0)
5207     new_player_nr = 0;
5208   if (new_player_nr > MAX_PLAYERS - 1)
5209     new_player_nr = MAX_PLAYERS - 1;
5210
5211   if (new_player_nr != old_player_nr)
5212   {
5213     input_player_nr = new_player_nr;
5214
5215     drawPlayerSetupInputInfo(input_player_nr, FALSE);
5216   }
5217 }
5218
5219 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
5220 {
5221   static int choice = 0;
5222   int x = 0;
5223   int y = choice;
5224   int pos_start  = SETUPINPUT_SCREEN_POS_START;
5225   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
5226   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
5227   int pos_end    = SETUPINPUT_SCREEN_POS_END;
5228
5229   if (button == MB_MENU_INITIALIZE)
5230   {
5231     drawPlayerSetupInputInfo(input_player_nr, (choice == 2));
5232
5233     DrawCursorAndText_Setup(choice, TRUE);
5234
5235     return;
5236   }
5237   else if (button == MB_MENU_LEAVE)
5238   {
5239     setup_mode = SETUP_MODE_MAIN;
5240     DrawSetupScreen();
5241     InitJoysticks();
5242
5243     return;
5244   }
5245
5246   if (mx || my)         /* mouse input */
5247   {
5248     x = (mx - mSX) / 32;
5249     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
5250   }
5251   else if (dx || dy)    /* keyboard input */
5252   {
5253     if (dx && choice == 0)
5254       x = (dx < 0 ? 10 : 12);
5255     else if ((dx && choice == 1) ||
5256              (dx == +1 && choice == 2) ||
5257              (dx == -1 && choice == pos_end))
5258       button = MB_MENU_CHOICE;
5259     else if (dy)
5260       y = choice + dy;
5261
5262     if (y >= pos_empty1 && y <= pos_empty2)
5263       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
5264   }
5265
5266   if (y == 0 && dx != 0 && button)
5267   {
5268     HandleSetupScreen_Input_Player(1, dx < 0 ? -1 : +1);
5269   }
5270   else if (IN_VIS_FIELD(x, y) &&
5271            y >= pos_start && y <= pos_end &&
5272            !(y >= pos_empty1 && y <= pos_empty2))
5273   {
5274     if (button)
5275     {
5276       if (y != choice)
5277       {
5278         DrawCursorAndText_Setup(choice, FALSE);
5279         DrawCursorAndText_Setup(y, TRUE);
5280
5281         drawPlayerSetupInputInfo(input_player_nr, (y == 2));
5282
5283         choice = y;
5284       }
5285     }
5286     else
5287     {
5288       if (y == 1)
5289       {
5290         char *device_name = setup.input[input_player_nr].joy.device_name;
5291
5292         if (!setup.input[input_player_nr].use_joystick)
5293         {
5294           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
5295
5296           setJoystickDeviceToNr(device_name, new_device_nr);
5297           setup.input[input_player_nr].use_joystick = TRUE;
5298         }
5299         else
5300         {
5301           int device_nr = getJoystickNrFromDeviceName(device_name);
5302           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
5303
5304           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
5305             setup.input[input_player_nr].use_joystick = FALSE;
5306           else
5307             setJoystickDeviceToNr(device_name, new_device_nr);
5308         }
5309
5310         drawPlayerSetupInputInfo(input_player_nr, FALSE);
5311       }
5312       else if (y == 2)
5313       {
5314         if (setup.input[input_player_nr].use_joystick)
5315         {
5316           InitJoysticks();
5317           CalibrateJoystick(input_player_nr);
5318         }
5319         else
5320           CustomizeKeyboard(input_player_nr);
5321       }
5322       else if (y == pos_end)
5323       {
5324         InitJoysticks();
5325
5326         FadeSetLeaveMenu();
5327
5328         setup_mode = SETUP_MODE_MAIN;
5329         DrawSetupScreen();
5330       }
5331     }
5332   }
5333 }
5334
5335 void CustomizeKeyboard(int player_nr)
5336 {
5337   int i;
5338   int step_nr;
5339   boolean finished = FALSE;
5340   static struct SetupKeyboardInfo custom_key;
5341   static struct
5342   {
5343     Key *key;
5344     char *text;
5345   } customize_step[] =
5346   {
5347     { &custom_key.left,  "Move Left"    },
5348     { &custom_key.right, "Move Right"   },
5349     { &custom_key.up,    "Move Up"      },
5350     { &custom_key.down,  "Move Down"    },
5351     { &custom_key.snap,  "Snap Field"   },
5352     { &custom_key.drop,  "Drop Element" }
5353   };
5354
5355   /* read existing key bindings from player setup */
5356   custom_key = setup.input[player_nr].key;
5357
5358   FadeSetEnterMenu();
5359   FadeOut(REDRAW_FIELD);
5360
5361   ClearField();
5362
5363   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Keyboard Input");
5364
5365 #if 0
5366   BackToFront();
5367   InitAnimation();
5368 #endif
5369
5370   step_nr = 0;
5371   DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
5372            customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
5373   DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
5374            "Key:", FONT_INPUT_1_ACTIVE);
5375   DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5376            getKeyNameFromKey(*customize_step[step_nr].key), FONT_VALUE_OLD);
5377
5378 #if 1
5379   FadeIn(REDRAW_FIELD);
5380
5381   InitAnimation();
5382 #endif
5383
5384   while (!finished)
5385   {
5386     if (PendingEvent())         /* got event */
5387     {
5388       Event event;
5389
5390       NextEvent(&event);
5391
5392       switch (event.type)
5393       {
5394         case EVENT_KEYPRESS:
5395           {
5396             Key key = GetEventKey((KeyEvent *)&event, FALSE);
5397
5398             if (key == KSYM_Escape || (key == KSYM_Return && step_nr == 6))
5399             {
5400               if (key == KSYM_Escape)
5401                 FadeSkipNextFadeIn();
5402
5403               finished = TRUE;
5404               break;
5405             }
5406
5407             /* all keys configured -- wait for "Escape" or "Return" key */
5408             if (step_nr == 6)
5409               break;
5410
5411             /* press 'Enter' to keep the existing key binding */
5412             if (key == KSYM_Return)
5413               key = *customize_step[step_nr].key;
5414
5415             /* check if key already used */
5416             for (i = 0; i < step_nr; i++)
5417               if (*customize_step[i].key == key)
5418                 break;
5419             if (i < step_nr)
5420               break;
5421
5422             /* got new key binding */
5423             *customize_step[step_nr].key = key;
5424             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5425                      "             ", FONT_VALUE_1);
5426             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5427                      getKeyNameFromKey(key), FONT_VALUE_1);
5428             step_nr++;
5429
5430             /* un-highlight last query */
5431             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1)) * 32,
5432                      customize_step[step_nr - 1].text, FONT_MENU_1);
5433             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1) + 1) * 32,
5434                      "Key:", FONT_MENU_1);
5435
5436             /* press 'Enter' to leave */
5437             if (step_nr == 6)
5438             {
5439               DrawText(mSX + 16, mSY + 15 * 32 + 16,
5440                        "Press Enter", FONT_TITLE_1);
5441               break;
5442             }
5443
5444             /* query next key binding */
5445             DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
5446                      customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
5447             DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
5448                      "Key:", FONT_INPUT_1_ACTIVE);
5449             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5450                      getKeyNameFromKey(*customize_step[step_nr].key),
5451                      FONT_VALUE_OLD);
5452           }
5453           break;
5454
5455         case EVENT_KEYRELEASE:
5456           key_joystick_mapping = 0;
5457           break;
5458
5459         default:
5460           HandleOtherEvents(&event);
5461           break;
5462       }
5463     }
5464
5465     DoAnimation();
5466     BackToFront();
5467
5468     /* don't eat all CPU time */
5469     Delay(10);
5470   }
5471
5472   /* write new key bindings back to player setup */
5473   setup.input[player_nr].key = custom_key;
5474
5475   StopAnimation();
5476   DrawSetupScreen_Input();
5477 }
5478
5479 static boolean CalibrateJoystickMain(int player_nr)
5480 {
5481   int new_joystick_xleft = JOYSTICK_XMIDDLE;
5482   int new_joystick_xright = JOYSTICK_XMIDDLE;
5483   int new_joystick_yupper = JOYSTICK_YMIDDLE;
5484   int new_joystick_ylower = JOYSTICK_YMIDDLE;
5485   int new_joystick_xmiddle, new_joystick_ymiddle;
5486
5487   int joystick_fd = joystick.fd[player_nr];
5488   int x, y, last_x, last_y, xpos = 8, ypos = 3;
5489   boolean check[3][3];
5490   int check_remaining = 3 * 3;
5491   int joy_x, joy_y;
5492   int joy_value;
5493   int result = -1;
5494
5495   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
5496     return FALSE;
5497
5498   if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
5499     return FALSE;
5500
5501   FadeSetEnterMenu();
5502   FadeOut(REDRAW_FIELD);
5503
5504   ClearField();
5505
5506   for (y = 0; y < 3; y++)
5507   {
5508     for (x = 0; x < 3; x++)
5509     {
5510       DrawFixedGraphic(xpos + x - 1, ypos + y - 1, IMG_MENU_CALIBRATE_BLUE, 0);
5511       check[x][y] = FALSE;
5512     }
5513   }
5514
5515   DrawTextSCentered(mSY - SY +  6 * 32, FONT_TITLE_1, "Rotate joystick");
5516   DrawTextSCentered(mSY - SY +  7 * 32, FONT_TITLE_1, "in all directions");
5517   DrawTextSCentered(mSY - SY +  9 * 32, FONT_TITLE_1, "if all balls");
5518   DrawTextSCentered(mSY - SY + 10 * 32, FONT_TITLE_1, "are marked,");
5519   DrawTextSCentered(mSY - SY + 11 * 32, FONT_TITLE_1, "center joystick");
5520   DrawTextSCentered(mSY - SY + 12 * 32, FONT_TITLE_1, "and");
5521   DrawTextSCentered(mSY - SY + 13 * 32, FONT_TITLE_1, "press any button!");
5522
5523   joy_value = Joystick(player_nr);
5524   last_x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
5525   last_y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
5526
5527   /* eventually uncalibrated center position (joystick could be uncentered) */
5528   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
5529     return FALSE;
5530
5531   new_joystick_xmiddle = joy_x;
5532   new_joystick_ymiddle = joy_y;
5533
5534   DrawFixedGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_RED, 0);
5535
5536   FadeIn(REDRAW_FIELD);
5537
5538   while (Joystick(player_nr) & JOY_BUTTON);     /* wait for released button */
5539   InitAnimation();
5540
5541   while (result < 0)
5542   {
5543     if (PendingEvent())         /* got event */
5544     {
5545       Event event;
5546
5547       NextEvent(&event);
5548
5549       switch (event.type)
5550       {
5551         case EVENT_KEYPRESS:
5552           switch (GetEventKey((KeyEvent *)&event, TRUE))
5553           {
5554             case KSYM_Return:
5555               if (check_remaining == 0)
5556                 result = 1;
5557               break;
5558
5559             case KSYM_Escape:
5560               FadeSkipNextFadeIn();
5561               result = 0;
5562               break;
5563
5564             default:
5565               break;
5566           }
5567           break;
5568
5569         case EVENT_KEYRELEASE:
5570           key_joystick_mapping = 0;
5571           break;
5572
5573         default:
5574           HandleOtherEvents(&event);
5575           break;
5576       }
5577     }
5578
5579     if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
5580       return FALSE;
5581
5582     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_x);
5583     new_joystick_xright = MAX(new_joystick_xright, joy_x);
5584     new_joystick_yupper = MIN(new_joystick_yupper, joy_y);
5585     new_joystick_ylower = MAX(new_joystick_ylower, joy_y);
5586
5587     setup.input[player_nr].joy.xleft = new_joystick_xleft;
5588     setup.input[player_nr].joy.yupper = new_joystick_yupper;
5589     setup.input[player_nr].joy.xright = new_joystick_xright;
5590     setup.input[player_nr].joy.ylower = new_joystick_ylower;
5591     setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
5592     setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
5593
5594     CheckJoystickData();
5595
5596     joy_value = Joystick(player_nr);
5597
5598     if (joy_value & JOY_BUTTON && check_remaining == 0)
5599       result = 1;
5600
5601     x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
5602     y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
5603
5604     if (x != last_x || y != last_y)
5605     {
5606       DrawFixedGraphic(xpos + last_x, ypos + last_y,
5607                        IMG_MENU_CALIBRATE_YELLOW, 0);
5608       DrawFixedGraphic(xpos + x,      ypos + y,
5609                        IMG_MENU_CALIBRATE_RED,    0);
5610
5611       last_x = x;
5612       last_y = y;
5613
5614       if (check_remaining > 0 && !check[x+1][y+1])
5615       {
5616         check[x+1][y+1] = TRUE;
5617         check_remaining--;
5618       }
5619
5620 #if 0
5621 #ifdef DEBUG
5622       printf("LEFT / MIDDLE / RIGHT == %d / %d / %d\n",
5623              setup.input[player_nr].joy.xleft,
5624              setup.input[player_nr].joy.xmiddle,
5625              setup.input[player_nr].joy.xright);
5626       printf("UP / MIDDLE / DOWN == %d / %d / %d\n",
5627              setup.input[player_nr].joy.yupper,
5628              setup.input[player_nr].joy.ymiddle,
5629              setup.input[player_nr].joy.ylower);
5630 #endif
5631 #endif
5632
5633     }
5634
5635     DoAnimation();
5636     BackToFront();
5637
5638     /* don't eat all CPU time */
5639     Delay(10);
5640   }
5641
5642   /* calibrated center position (joystick should now be centered) */
5643   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
5644     return FALSE;
5645
5646   new_joystick_xmiddle = joy_x;
5647   new_joystick_ymiddle = joy_y;
5648
5649   StopAnimation();
5650
5651 #if 0
5652   DrawSetupScreen_Input();
5653 #endif
5654
5655   /* wait until the last pressed button was released */
5656   while (Joystick(player_nr) & JOY_BUTTON)
5657   {
5658     if (PendingEvent())         /* got event */
5659     {
5660       Event event;
5661
5662       NextEvent(&event);
5663       HandleOtherEvents(&event);
5664
5665       Delay(10);
5666     }
5667   }
5668
5669   return TRUE;
5670 }
5671
5672 void CalibrateJoystick(int player_nr)
5673 {
5674   if (!CalibrateJoystickMain(player_nr))
5675   {
5676     char *device_name = setup.input[player_nr].joy.device_name;
5677     int nr = getJoystickNrFromDeviceName(device_name) + 1;
5678     int xpos = mSX - SX;
5679     int ypos = mSY - SY;
5680
5681     ClearField();
5682
5683     DrawTextF(xpos + 16, ypos + 6 * 32, FONT_TITLE_1, "   JOYSTICK %d   ", nr);
5684     DrawTextF(xpos + 16, ypos + 7 * 32, FONT_TITLE_1, " NOT AVAILABLE! ");
5685     BackToFront();
5686
5687     Delay(2000);                /* show error message for a short time */
5688
5689     ClearEventQueue();
5690   }
5691
5692 #if 1
5693   DrawSetupScreen_Input();
5694 #endif
5695 }
5696
5697 void DrawSetupScreen()
5698 {
5699   DeactivateJoystick();
5700
5701   SetMainBackgroundImage(IMG_BACKGROUND_SETUP);
5702
5703   if (setup_mode == SETUP_MODE_INPUT)
5704     DrawSetupScreen_Input();
5705   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
5706     DrawChooseTree(&game_speed_current);
5707   else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE)
5708     DrawChooseTree(&screen_mode_current);
5709   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
5710     DrawChooseTree(&scroll_delay_current);
5711   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
5712     DrawChooseTree(&artwork.gfx_current);
5713   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
5714     DrawChooseTree(&artwork.snd_current);
5715   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
5716     DrawChooseTree(&artwork.mus_current);
5717   else
5718     DrawSetupScreen_Generic();
5719
5720   PlayMenuSound();
5721   PlayMenuMusic();
5722 }
5723
5724 void RedrawSetupScreenAfterFullscreenToggle()
5725 {
5726   if (setup_mode == SETUP_MODE_GRAPHICS)
5727     DrawSetupScreen();
5728 }
5729
5730 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
5731 {
5732   if (setup_mode == SETUP_MODE_INPUT)
5733     HandleSetupScreen_Input(mx, my, dx, dy, button);
5734   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
5735     HandleChooseTree(mx, my, dx, dy, button, &game_speed_current);
5736   else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE)
5737     HandleChooseTree(mx, my, dx, dy, button, &screen_mode_current);
5738   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
5739     HandleChooseTree(mx, my, dx, dy, button, &scroll_delay_current);
5740   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
5741     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
5742   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
5743     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
5744   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
5745     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
5746   else
5747     HandleSetupScreen_Generic(mx, my, dx, dy, button);
5748
5749   DoAnimation();
5750 }
5751
5752 void HandleGameActions()
5753 {
5754   if (game_status != GAME_MODE_PLAYING)
5755     return;
5756
5757   GameActions();        /* main game loop */
5758
5759   if (tape.auto_play && !tape.playing)
5760     AutoPlayTape();     /* continue automatically playing next tape */
5761 }
5762
5763
5764 /* ---------- new screen button stuff -------------------------------------- */
5765
5766 static void getScreenMenuButtonPos(int *x, int *y, int gadget_id)
5767 {
5768   switch (gadget_id)
5769   {
5770 #if 1
5771     case SCREEN_CTRL_ID_PREV_LEVEL:
5772       *x = mSX + menu.main.button.prev_level.x;
5773       *y = mSY + menu.main.button.prev_level.y;
5774       break;
5775
5776     case SCREEN_CTRL_ID_NEXT_LEVEL:
5777       *x = mSX + menu.main.button.next_level.x;
5778       *y = mSY + menu.main.button.next_level.y;
5779       break;
5780 #else
5781     case SCREEN_CTRL_ID_PREV_LEVEL:
5782       *x = mSX + TILEX * getPrevlevelButtonPos();
5783       *y = mSY + TILEY * (MENU_SCREEN_START_YPOS + 1);
5784       break;
5785
5786     case SCREEN_CTRL_ID_NEXT_LEVEL:
5787       *x = mSX + TILEX * getNextLevelButtonPos();
5788       *y = mSY + TILEY * (MENU_SCREEN_START_YPOS + 1);
5789       break;
5790 #endif
5791
5792     case SCREEN_CTRL_ID_PREV_PLAYER:
5793       *x = mSX + TILEX * 10;
5794       *y = mSY + TILEY * MENU_SCREEN_START_YPOS;
5795       break;
5796
5797     case SCREEN_CTRL_ID_NEXT_PLAYER:
5798       *x = mSX + TILEX * 12;
5799       *y = mSY + TILEY * MENU_SCREEN_START_YPOS;
5800       break;
5801
5802     default:
5803       Error(ERR_EXIT, "unknown gadget ID %d", gadget_id);
5804   }
5805 }
5806
5807 static struct
5808 {
5809   int gfx_unpressed, gfx_pressed;
5810   void (*get_gadget_position)(int *, int *, int);
5811   int gadget_id;
5812   int screen_mask;
5813   char *infotext;
5814 } menubutton_info[NUM_SCREEN_MENUBUTTONS] =
5815 {
5816   {
5817     IMG_MENU_BUTTON_PREV_LEVEL, IMG_MENU_BUTTON_PREV_LEVEL_ACTIVE,
5818     getScreenMenuButtonPos,
5819     SCREEN_CTRL_ID_PREV_LEVEL,
5820     SCREEN_MASK_MAIN,
5821     "last level"
5822   },
5823   {
5824     IMG_MENU_BUTTON_NEXT_LEVEL, IMG_MENU_BUTTON_NEXT_LEVEL_ACTIVE,
5825     getScreenMenuButtonPos,
5826     SCREEN_CTRL_ID_NEXT_LEVEL,
5827     SCREEN_MASK_MAIN,
5828     "next level"
5829   },
5830   {
5831     IMG_MENU_BUTTON_LEFT, IMG_MENU_BUTTON_LEFT_ACTIVE,
5832     getScreenMenuButtonPos,
5833     SCREEN_CTRL_ID_PREV_PLAYER,
5834     SCREEN_MASK_INPUT,
5835     "last player"
5836   },
5837   {
5838     IMG_MENU_BUTTON_RIGHT, IMG_MENU_BUTTON_RIGHT_ACTIVE,
5839     getScreenMenuButtonPos,
5840     SCREEN_CTRL_ID_NEXT_PLAYER,
5841     SCREEN_MASK_INPUT,
5842     "next player"
5843   },
5844 };
5845
5846 static struct
5847 {
5848   int gfx_unpressed, gfx_pressed;
5849   int x, y;
5850   int gadget_id;
5851   char *infotext;
5852 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
5853 {
5854   {
5855     IMG_MENU_BUTTON_UP, IMG_MENU_BUTTON_UP_ACTIVE,
5856 #if 1
5857     -1, -1,     /* these values are not constant, but can change at runtime */
5858 #else
5859     SC_SCROLL_UP_XPOS, SC_SCROLL_UP_YPOS,
5860 #endif
5861     SCREEN_CTRL_ID_SCROLL_UP,
5862     "scroll up"
5863   },
5864   {
5865     IMG_MENU_BUTTON_DOWN, IMG_MENU_BUTTON_DOWN_ACTIVE,
5866 #if 1
5867     -1, -1,     /* these values are not constant, but can change at runtime */
5868 #else
5869     SC_SCROLL_DOWN_XPOS, SC_SCROLL_DOWN_YPOS,
5870 #endif
5871     SCREEN_CTRL_ID_SCROLL_DOWN,
5872     "scroll down"
5873   }
5874 };
5875
5876 static struct
5877 {
5878 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
5879   Bitmap **gfx_unpressed, **gfx_pressed;
5880 #else
5881   int gfx_unpressed, gfx_pressed;
5882 #endif
5883   int x, y;
5884   int width, height;
5885   int type;
5886   int gadget_id;
5887   char *infotext;
5888 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
5889 {
5890   {
5891 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
5892     &scrollbar_bitmap[0], &scrollbar_bitmap[1],
5893 #else
5894     IMG_MENU_SCROLLBAR, IMG_MENU_SCROLLBAR_ACTIVE,
5895 #endif
5896 #if 1
5897     -1, -1,     /* these values are not constant, but can change at runtime */
5898     -1, -1,     /* these values are not constant, but can change at runtime */
5899 #else
5900     SC_SCROLL_VERTICAL_XPOS, SC_SCROLL_VERTICAL_YPOS,
5901     SC_SCROLL_VERTICAL_XSIZE, SC_SCROLL_VERTICAL_YSIZE,
5902 #endif
5903     GD_TYPE_SCROLLBAR_VERTICAL,
5904     SCREEN_CTRL_ID_SCROLL_VERTICAL,
5905     "scroll level series vertically"
5906   }
5907 };
5908
5909 static void CreateScreenMenubuttons()
5910 {
5911   struct GadgetInfo *gi;
5912   unsigned long event_mask;
5913   int i;
5914
5915   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
5916   {
5917     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
5918     int gfx_unpressed, gfx_pressed;
5919     int x, y, width, height;
5920     int gd_x1, gd_x2, gd_y1, gd_y2;
5921     int id = menubutton_info[i].gadget_id;
5922
5923     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
5924
5925     menubutton_info[i].get_gadget_position(&x, &y, id);
5926
5927     width = SC_MENUBUTTON_XSIZE;
5928     height = SC_MENUBUTTON_YSIZE;
5929
5930     gfx_unpressed = menubutton_info[i].gfx_unpressed;
5931     gfx_pressed   = menubutton_info[i].gfx_pressed;
5932     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
5933     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
5934     gd_x1 = graphic_info[gfx_unpressed].src_x;
5935     gd_y1 = graphic_info[gfx_unpressed].src_y;
5936     gd_x2 = graphic_info[gfx_pressed].src_x;
5937     gd_y2 = graphic_info[gfx_pressed].src_y;
5938
5939     gi = CreateGadget(GDI_CUSTOM_ID, id,
5940                       GDI_CUSTOM_TYPE_ID, i,
5941                       GDI_INFO_TEXT, menubutton_info[i].infotext,
5942                       GDI_X, x,
5943                       GDI_Y, y,
5944                       GDI_WIDTH, width,
5945                       GDI_HEIGHT, height,
5946                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
5947                       GDI_STATE, GD_BUTTON_UNPRESSED,
5948                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
5949                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
5950                       GDI_DIRECT_DRAW, FALSE,
5951                       GDI_EVENT_MASK, event_mask,
5952                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
5953                       GDI_END);
5954
5955     if (gi == NULL)
5956       Error(ERR_EXIT, "cannot create gadget");
5957
5958     screen_gadget[id] = gi;
5959   }
5960 }
5961
5962 static void CreateScreenScrollbuttons()
5963 {
5964   struct GadgetInfo *gi;
5965   unsigned long event_mask;
5966   int i;
5967
5968   /* these values are not constant, but can change at runtime */
5969   scrollbutton_info[0].x = SC_SCROLL_UP_XPOS;
5970   scrollbutton_info[0].y = SC_SCROLL_UP_YPOS;
5971   scrollbutton_info[1].x = SC_SCROLL_DOWN_XPOS;
5972   scrollbutton_info[1].y = SC_SCROLL_DOWN_YPOS;
5973
5974   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
5975   {
5976     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
5977     int gfx_unpressed, gfx_pressed;
5978     int x, y, width, height;
5979     int gd_x1, gd_x2, gd_y1, gd_y2;
5980     int id = scrollbutton_info[i].gadget_id;
5981
5982     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
5983
5984     x = mSX + scrollbutton_info[i].x + menu.scrollbar_xoffset;
5985     y = mSY + scrollbutton_info[i].y;
5986     width = SC_SCROLLBUTTON_XSIZE;
5987     height = SC_SCROLLBUTTON_YSIZE;
5988
5989     if (id == SCREEN_CTRL_ID_SCROLL_DOWN)
5990       y = mSY + (SC_SCROLL_VERTICAL_YPOS +
5991                  (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE);
5992
5993     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
5994     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
5995     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
5996     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
5997     gd_x1 = graphic_info[gfx_unpressed].src_x;
5998     gd_y1 = graphic_info[gfx_unpressed].src_y;
5999     gd_x2 = graphic_info[gfx_pressed].src_x;
6000     gd_y2 = graphic_info[gfx_pressed].src_y;
6001
6002     gi = CreateGadget(GDI_CUSTOM_ID, id,
6003                       GDI_CUSTOM_TYPE_ID, i,
6004                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
6005                       GDI_X, x,
6006                       GDI_Y, y,
6007                       GDI_WIDTH, width,
6008                       GDI_HEIGHT, height,
6009                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
6010                       GDI_STATE, GD_BUTTON_UNPRESSED,
6011                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6012                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6013                       GDI_DIRECT_DRAW, FALSE,
6014                       GDI_EVENT_MASK, event_mask,
6015                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6016                       GDI_END);
6017
6018     if (gi == NULL)
6019       Error(ERR_EXIT, "cannot create gadget");
6020
6021     screen_gadget[id] = gi;
6022   }
6023 }
6024
6025 static void CreateScreenScrollbars()
6026 {
6027   int i;
6028
6029   /* these values are not constant, but can change at runtime */
6030   scrollbar_info[0].x = SC_SCROLL_VERTICAL_XPOS;
6031   scrollbar_info[0].y = SC_SCROLL_VERTICAL_YPOS;
6032   scrollbar_info[0].width  = SC_SCROLL_VERTICAL_XSIZE;
6033   scrollbar_info[0].height = SC_SCROLL_VERTICAL_YSIZE;
6034
6035   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
6036   {
6037     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6038 #if !defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6039     int gfx_unpressed, gfx_pressed;
6040 #endif
6041     int x, y, width, height;
6042     int gd_x1, gd_x2, gd_y1, gd_y2;
6043     struct GadgetInfo *gi;
6044     int items_max, items_visible, item_position;
6045     unsigned long event_mask;
6046     int num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
6047     int id = scrollbar_info[i].gadget_id;
6048
6049     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
6050
6051     x = mSX + scrollbar_info[i].x + menu.scrollbar_xoffset;
6052     y = mSY + scrollbar_info[i].y;
6053     width  = scrollbar_info[i].width;
6054     height = scrollbar_info[i].height;
6055
6056     if (id == SCREEN_CTRL_ID_SCROLL_VERTICAL)
6057       height = (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE;
6058
6059     items_max = num_page_entries;
6060     items_visible = num_page_entries;
6061     item_position = 0;
6062
6063 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6064     gd_bitmap_unpressed = *scrollbar_info[i].gfx_unpressed;
6065     gd_bitmap_pressed   = *scrollbar_info[i].gfx_pressed;
6066     gd_x1 = 0;
6067     gd_y1 = 0;
6068     gd_x2 = 0;
6069     gd_y2 = 0;
6070 #else
6071     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
6072     gfx_pressed   = scrollbar_info[i].gfx_pressed;
6073     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6074     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6075     gd_x1 = graphic_info[gfx_unpressed].src_x;
6076     gd_y1 = graphic_info[gfx_unpressed].src_y;
6077     gd_x2 = graphic_info[gfx_pressed].src_x;
6078     gd_y2 = graphic_info[gfx_pressed].src_y;
6079 #endif
6080
6081     gi = CreateGadget(GDI_CUSTOM_ID, id,
6082                       GDI_CUSTOM_TYPE_ID, i,
6083                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
6084                       GDI_X, x,
6085                       GDI_Y, y,
6086                       GDI_WIDTH, width,
6087                       GDI_HEIGHT, height,
6088                       GDI_TYPE, scrollbar_info[i].type,
6089                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
6090                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
6091                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
6092 #if 1
6093                       GDI_WHEEL_AREA_X, SX,
6094                       GDI_WHEEL_AREA_Y, SY,
6095                       GDI_WHEEL_AREA_WIDTH, SXSIZE,
6096                       GDI_WHEEL_AREA_HEIGHT, SYSIZE,
6097 #else
6098                       GDI_WHEEL_AREA_X, 0,
6099                       GDI_WHEEL_AREA_Y, 0,
6100                       GDI_WHEEL_AREA_WIDTH, WIN_XSIZE,
6101                       GDI_WHEEL_AREA_HEIGHT, WIN_YSIZE,
6102 #endif
6103                       GDI_STATE, GD_BUTTON_UNPRESSED,
6104                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6105                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6106                       GDI_BORDER_SIZE, SC_BORDER_SIZE, SC_BORDER_SIZE,
6107                       GDI_DIRECT_DRAW, FALSE,
6108                       GDI_EVENT_MASK, event_mask,
6109                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6110                       GDI_END);
6111
6112     if (gi == NULL)
6113       Error(ERR_EXIT, "cannot create gadget");
6114
6115     screen_gadget[id] = gi;
6116   }
6117 }
6118
6119 void CreateScreenGadgets()
6120 {
6121   int last_game_status = game_status;   /* save current game status */
6122
6123 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6124   int i;
6125
6126   for (i = 0; i < NUM_SCROLLBAR_BITMAPS; i++)
6127   {
6128     scrollbar_bitmap[i] = CreateBitmap(TILEX, TILEY, DEFAULT_DEPTH);
6129
6130     /* copy pointers to clip mask and GC */
6131     scrollbar_bitmap[i]->clip_mask =
6132       graphic_info[IMG_MENU_SCROLLBAR + i].clip_mask;
6133     scrollbar_bitmap[i]->stored_clip_gc =
6134       graphic_info[IMG_MENU_SCROLLBAR + i].clip_gc;
6135
6136     BlitBitmap(graphic_info[IMG_MENU_SCROLLBAR + i].bitmap,
6137                scrollbar_bitmap[i],
6138                graphic_info[IMG_MENU_SCROLLBAR + i].src_x,
6139                graphic_info[IMG_MENU_SCROLLBAR + i].src_y,
6140                TILEX, TILEY, 0, 0);
6141   }
6142 #endif
6143
6144   CreateScreenMenubuttons();
6145
6146 #if 0
6147   /* force LEVELS draw offset for scrollbar / scrollbutton gadgets */
6148   game_status = GAME_MODE_LEVELS;
6149 #endif
6150
6151   CreateScreenScrollbuttons();
6152   CreateScreenScrollbars();
6153
6154   game_status = last_game_status;       /* restore current game status */
6155 }
6156
6157 void FreeScreenGadgets()
6158 {
6159   int i;
6160
6161 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6162   for (i = 0; i < NUM_SCROLLBAR_BITMAPS; i++)
6163   {
6164     /* prevent freeing clip mask and GC twice */
6165     scrollbar_bitmap[i]->clip_mask = None;
6166     scrollbar_bitmap[i]->stored_clip_gc = None;
6167
6168     FreeBitmap(scrollbar_bitmap[i]);
6169   }
6170 #endif
6171
6172   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
6173     FreeGadget(screen_gadget[i]);
6174 }
6175
6176 void MapScreenMenuGadgets(int screen_mask)
6177 {
6178   int i;
6179
6180   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
6181     if (screen_mask & menubutton_info[i].screen_mask)
6182       MapGadget(screen_gadget[menubutton_info[i].gadget_id]);
6183 }
6184
6185 void MapScreenTreeGadgets(TreeInfo *ti)
6186 {
6187   int num_entries = numTreeInfoInGroup(ti);
6188   int i;
6189
6190   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
6191     return;
6192
6193   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
6194     MapGadget(screen_gadget[scrollbutton_info[i].gadget_id]);
6195
6196   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
6197     MapGadget(screen_gadget[scrollbar_info[i].gadget_id]);
6198 }
6199
6200 static void HandleScreenGadgets(struct GadgetInfo *gi)
6201 {
6202   int id = gi->custom_id;
6203   int button = gi->event.button;
6204   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
6205
6206   switch (id)
6207   {
6208     case SCREEN_CTRL_ID_PREV_LEVEL:
6209       HandleMainMenu_SelectLevel(step, -1);
6210       break;
6211
6212     case SCREEN_CTRL_ID_NEXT_LEVEL:
6213       HandleMainMenu_SelectLevel(step, +1);
6214       break;
6215
6216     case SCREEN_CTRL_ID_PREV_PLAYER:
6217       HandleSetupScreen_Input_Player(step, -1);
6218       break;
6219
6220     case SCREEN_CTRL_ID_NEXT_PLAYER:
6221       HandleSetupScreen_Input_Player(step, +1);
6222       break;
6223
6224     case SCREEN_CTRL_ID_SCROLL_UP:
6225       if (game_status == GAME_MODE_LEVELS)
6226         HandleChooseLevelSet(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6227       else if (game_status == GAME_MODE_LEVELNR)
6228         HandleChooseLevelNr(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6229       else if (game_status == GAME_MODE_SETUP)
6230         HandleSetupScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6231       break;
6232
6233     case SCREEN_CTRL_ID_SCROLL_DOWN:
6234       if (game_status == GAME_MODE_LEVELS)
6235         HandleChooseLevelSet(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
6236       else if (game_status == GAME_MODE_LEVELNR)
6237         HandleChooseLevelNr(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
6238       else if (game_status == GAME_MODE_SETUP)
6239         HandleSetupScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
6240       break;
6241
6242     case SCREEN_CTRL_ID_SCROLL_VERTICAL:
6243       if (game_status == GAME_MODE_LEVELS)
6244         HandleChooseLevelSet(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
6245       else if (game_status == GAME_MODE_LEVELNR)
6246         HandleChooseLevelNr(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
6247       else if (game_status == GAME_MODE_SETUP)
6248         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
6249       break;
6250
6251     default:
6252       break;
6253   }
6254 }