rnd-20100719-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     ti->color = (level.no_valid_file ? FC_BLUE :
3771                  LevelStats_getSolved(i) ? FC_GREEN :
3772                  LevelStats_getPlayed(i) ? FC_YELLOW : FC_RED);
3773
3774     sprintf(identifier, "%d", value);
3775     sprintf(name, "%03d: %s", value,
3776             (level.no_valid_file ? "(no file)" : level.name));
3777
3778     setString(&ti->identifier, identifier);
3779     setString(&ti->name, name);
3780     setString(&ti->name_sorting, name);
3781
3782     pushTreeInfo(&level_number, ti);
3783   }
3784
3785   /* sort level number values to start with lowest level number */
3786   sortTreeInfo(&level_number);
3787
3788   /* set current level number to current level number */
3789   level_number_current =
3790     getTreeInfoFromIdentifier(level_number, i_to_a(level_nr));
3791
3792   /* if that also fails, set current level number to first available level */
3793   if (level_number_current == NULL)
3794     level_number_current = level_number;
3795
3796   SetMainBackgroundImage(IMG_BACKGROUND_LEVELNR);
3797
3798 #if 1
3799   DrawChooseTree(&level_number_current);
3800 #else
3801   DrawChooseTree(&leveldir_current);
3802 #endif
3803
3804   PlayMenuSound();
3805   PlayMenuMusic();
3806 }
3807
3808 void HandleChooseLevelNr(int mx, int my, int dx, int dy, int button)
3809 {
3810 #if 1
3811   HandleChooseTree(mx, my, dx, dy, button, &level_number_current);
3812 #else
3813   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
3814 #endif
3815
3816   DoAnimation();
3817 }
3818
3819 void DrawHallOfFame(int highlight_position)
3820 {
3821   UnmapAllGadgets();
3822   FadeSoundsAndMusic();
3823
3824   /* (this is needed when called from GameEnd() after winning a game) */
3825   KeyboardAutoRepeatOn();
3826   ActivateJoystick();
3827
3828   /* (this is needed when called from GameEnd() after winning a game) */
3829   SetDrawDeactivationMask(REDRAW_NONE);
3830   SetDrawBackgroundMask(REDRAW_FIELD);
3831
3832   CloseDoor(DOOR_CLOSE_2);
3833
3834   if (highlight_position < 0) 
3835     LoadScore(level_nr);
3836
3837   FadeSetEnterScreen();
3838
3839   // printf("::: %d: %d\n", game_status, menu.enter_screen[game_status]);
3840
3841 #if 1
3842   FadeOut(REDRAW_FIELD);
3843 #endif
3844
3845   InitAnimation();
3846
3847   PlayMenuSound();
3848   PlayMenuMusic();
3849
3850   HandleHallOfFame(highlight_position, 0, 0, 0, MB_MENU_INITIALIZE);
3851
3852 #if 1
3853   FadeIn(REDRAW_FIELD);
3854 #endif
3855 }
3856
3857 static void drawHallOfFameList(int first_entry, int highlight_position)
3858 {
3859   int i;
3860
3861   SetMainBackgroundImage(IMG_BACKGROUND_SCORES);
3862   ClearField();
3863
3864   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, "Hall Of Fame");
3865   DrawTextFCentered(MENU_TITLE2_YPOS, FONT_TITLE_2,
3866                     "HighScores of Level %d", level_nr);
3867
3868   for (i = 0; i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
3869   {
3870     int entry = first_entry + i;
3871     boolean active = (entry == highlight_position);
3872     int font_nr1 = (active ? FONT_TEXT_1_ACTIVE : FONT_TEXT_1);
3873     int font_nr2 = (active ? FONT_TEXT_2_ACTIVE : FONT_TEXT_2);
3874     int font_nr3 = (active ? FONT_TEXT_3_ACTIVE : FONT_TEXT_3);
3875     int font_nr4 = (active ? FONT_TEXT_4_ACTIVE : FONT_TEXT_4);
3876     int dx1 = 3 * getFontWidth(font_nr1);
3877     int dx2 = dx1 + getFontWidth(font_nr1);
3878     int dx3 = dx2 + 25 * getFontWidth(font_nr3);
3879     int sy = mSY + 64 + i * 32;
3880
3881     DrawText(mSX, sy, int2str(entry + 1, 3), font_nr1);
3882     DrawText(mSX + dx1, sy, ".", font_nr1);
3883     DrawText(mSX + dx2, sy, ".........................", font_nr3);
3884
3885     if (!strEqual(highscore[entry].Name, EMPTY_PLAYER_NAME))
3886       DrawText(mSX + dx2, sy, highscore[entry].Name, font_nr2);
3887
3888     DrawText(mSX + dx3, sy, int2str(highscore[entry].Score, 5), font_nr4);
3889   }
3890
3891   redraw_mask |= REDRAW_FIELD;
3892 }
3893
3894 void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
3895 {
3896   static int first_entry = 0;
3897   static int highlight_position = 0;
3898   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
3899
3900   if (button == MB_MENU_INITIALIZE)
3901   {
3902     first_entry = 0;
3903     highlight_position = mx;
3904     drawHallOfFameList(first_entry, highlight_position);
3905
3906     return;
3907   }
3908
3909   if (ABS(dy) == SCROLL_PAGE)           /* handle scrolling one page */
3910     step = NUM_MENU_ENTRIES_ON_SCREEN - 1;
3911
3912   if (dy < 0)
3913   {
3914     if (first_entry > 0)
3915     {
3916       first_entry -= step;
3917       if (first_entry < 0)
3918         first_entry = 0;
3919
3920       drawHallOfFameList(first_entry, highlight_position);
3921     }
3922   }
3923   else if (dy > 0)
3924   {
3925     if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
3926     {
3927       first_entry += step;
3928       if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
3929         first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
3930
3931       drawHallOfFameList(first_entry, highlight_position);
3932     }
3933   }
3934   else if (button == MB_MENU_LEAVE)
3935   {
3936     PlaySound(SND_MENU_ITEM_SELECTING);
3937
3938     FadeSound(SND_BACKGROUND_SCORES);
3939
3940     game_status = GAME_MODE_MAIN;
3941
3942     DrawMainMenu();
3943   }
3944   else if (button == MB_MENU_CHOICE)
3945   {
3946     PlaySound(SND_MENU_ITEM_SELECTING);
3947
3948     FadeSound(SND_BACKGROUND_SCORES);
3949
3950 #if 0
3951     FadeOut(REDRAW_FIELD);
3952 #endif
3953
3954     game_status = GAME_MODE_MAIN;
3955
3956     DrawAndFadeInMainMenu(REDRAW_FIELD);
3957   }
3958
3959   if (game_status == GAME_MODE_SCORES)
3960     PlayMenuSoundIfLoop();
3961
3962   DoAnimation();
3963 }
3964
3965
3966 /* ========================================================================= */
3967 /* setup screen functions                                                    */
3968 /* ========================================================================= */
3969
3970 static struct TokenInfo *setup_info;
3971 static int num_setup_info;
3972
3973 static char *screen_mode_text;
3974 static char *scroll_delay_text;
3975 static char *game_speed_text;
3976 static char *graphics_set_name;
3977 static char *sounds_set_name;
3978 static char *music_set_name;
3979
3980 static void execSetupMain()
3981 {
3982   setup_mode = SETUP_MODE_MAIN;
3983
3984   DrawSetupScreen();
3985 }
3986
3987 static void execSetupGame()
3988 {
3989   if (game_speeds == NULL)
3990   {
3991     int i;
3992
3993     for (i = 0; game_speeds_list[i].value != -1; i++)
3994     {
3995       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
3996       char identifier[32], name[32];
3997       int value = game_speeds_list[i].value;
3998       char *text = game_speeds_list[i].text;
3999
4000       ti->node_top = &game_speeds;
4001       ti->sort_priority = 10000 - value;
4002
4003       sprintf(identifier, "%d", value);
4004       sprintf(name, "%s", text);
4005
4006       setString(&ti->identifier, identifier);
4007       setString(&ti->name, name);
4008       setString(&ti->name_sorting, name);
4009       setString(&ti->infotext, "Game Speed");
4010
4011       pushTreeInfo(&game_speeds, ti);
4012     }
4013
4014     /* sort game speed values to start with slowest game speed */
4015     sortTreeInfo(&game_speeds);
4016
4017     /* set current game speed to configured game speed value */
4018     game_speed_current =
4019       getTreeInfoFromIdentifier(game_speeds, i_to_a(setup.game_frame_delay));
4020
4021     /* if that fails, set current game speed to reliable default value */
4022     if (game_speed_current == NULL)
4023       game_speed_current =
4024         getTreeInfoFromIdentifier(game_speeds, i_to_a(GAME_FRAME_DELAY));
4025
4026     /* if that also fails, set current game speed to first available speed */
4027     if (game_speed_current == NULL)
4028       game_speed_current = game_speeds;
4029   }
4030
4031   setup.game_frame_delay = atoi(game_speed_current->identifier);
4032
4033   /* needed for displaying game speed text instead of identifier */
4034   game_speed_text = game_speed_current->name;
4035
4036   setup_mode = SETUP_MODE_GAME;
4037
4038   DrawSetupScreen();
4039 }
4040
4041 static void execSetupChooseGameSpeed()
4042 {
4043   setup_mode = SETUP_MODE_CHOOSE_GAME_SPEED;
4044
4045   DrawSetupScreen();
4046 }
4047
4048 static void execSetupEditor()
4049 {
4050   setup_mode = SETUP_MODE_EDITOR;
4051
4052   DrawSetupScreen();
4053 }
4054
4055 static void execSetupGraphics()
4056 {
4057   if (video.fullscreen_available && screen_modes == NULL)
4058   {
4059     int i;
4060
4061     for (i = 0; video.fullscreen_modes[i].width != -1; i++)
4062     {
4063       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4064       char identifier[32], name[32];
4065       int x = video.fullscreen_modes[i].width;
4066       int y = video.fullscreen_modes[i].height;
4067       int xx, yy;
4068
4069       get_aspect_ratio_from_screen_mode(&video.fullscreen_modes[i], &xx, &yy);
4070
4071       ti->node_top = &screen_modes;
4072       ti->sort_priority = x * 10000 + y;
4073
4074       sprintf(identifier, "%dx%d", x, y);
4075       sprintf(name, "%d x %d [%d:%d]", x, y, xx, yy);
4076
4077       setString(&ti->identifier, identifier);
4078       setString(&ti->name, name);
4079       setString(&ti->name_sorting, name);
4080       setString(&ti->infotext, "Fullscreen Mode");
4081
4082       pushTreeInfo(&screen_modes, ti);
4083     }
4084
4085     /* sort fullscreen modes to start with lowest available screen resolution */
4086     sortTreeInfo(&screen_modes);
4087
4088     /* set current screen mode for fullscreen mode to configured setup value */
4089     screen_mode_current = getTreeInfoFromIdentifier(screen_modes,
4090                                                     setup.fullscreen_mode);
4091
4092     /* if that fails, set current screen mode to reliable default value */
4093     if (screen_mode_current == NULL)
4094       screen_mode_current = getTreeInfoFromIdentifier(screen_modes,
4095                                                       DEFAULT_FULLSCREEN_MODE);
4096
4097     /* if that also fails, set current screen mode to first available mode */
4098     if (screen_mode_current == NULL)
4099       screen_mode_current = screen_modes;
4100
4101     if (screen_mode_current == NULL)
4102       video.fullscreen_available = FALSE;
4103   }
4104
4105   if (video.fullscreen_available)
4106   {
4107     setup.fullscreen_mode = screen_mode_current->identifier;
4108
4109     /* needed for displaying screen mode name instead of identifier */
4110     screen_mode_text = screen_mode_current->name;
4111   }
4112
4113 #if 1
4114   if (scroll_delays == NULL)
4115   {
4116     int i;
4117
4118     for (i = 0; scroll_delays_list[i].value != -1; i++)
4119     {
4120       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4121       char identifier[32], name[32];
4122       int value = scroll_delays_list[i].value;
4123       char *text = scroll_delays_list[i].text;
4124
4125       ti->node_top = &scroll_delays;
4126       ti->sort_priority = value;
4127
4128       sprintf(identifier, "%d", value);
4129       sprintf(name, "%s", text);
4130
4131       setString(&ti->identifier, identifier);
4132       setString(&ti->name, name);
4133       setString(&ti->name_sorting, name);
4134       setString(&ti->infotext, "Scroll Delay");
4135
4136       pushTreeInfo(&scroll_delays, ti);
4137     }
4138
4139     /* sort scroll delay values to start with lowest scroll delay value */
4140     sortTreeInfo(&scroll_delays);
4141
4142     /* set current scroll delay value to configured scroll delay value */
4143     scroll_delay_current =
4144       getTreeInfoFromIdentifier(scroll_delays,i_to_a(setup.scroll_delay_value));
4145
4146     /* if that fails, set current scroll delay to reliable default value */
4147     if (scroll_delay_current == NULL)
4148       scroll_delay_current =
4149         getTreeInfoFromIdentifier(scroll_delays, i_to_a(STD_SCROLL_DELAY));
4150
4151     /* if that also fails, set current scroll delay to first available value */
4152     if (scroll_delay_current == NULL)
4153       scroll_delay_current = scroll_delays;
4154   }
4155
4156   setup.scroll_delay_value = atoi(scroll_delay_current->identifier);
4157
4158   /* needed for displaying scroll delay text instead of identifier */
4159   scroll_delay_text = scroll_delay_current->name;
4160 #endif
4161
4162   setup_mode = SETUP_MODE_GRAPHICS;
4163   DrawSetupScreen();
4164 }
4165
4166 static void execSetupChooseScreenMode()
4167 {
4168   if (!video.fullscreen_available)
4169     return;
4170
4171   setup_mode = SETUP_MODE_CHOOSE_SCREEN_MODE;
4172
4173   DrawSetupScreen();
4174 }
4175
4176 static void execSetupChooseScrollDelay()
4177 {
4178   setup_mode = SETUP_MODE_CHOOSE_SCROLL_DELAY;
4179
4180   DrawSetupScreen();
4181 }
4182
4183 static void execSetupSound()
4184 {
4185   setup_mode = SETUP_MODE_SOUND;
4186
4187   DrawSetupScreen();
4188 }
4189
4190 static void execSetupArtwork()
4191 {
4192 #if 0
4193   printf("::: '%s', '%s', '%s'\n",
4194          artwork.gfx_current->subdir,
4195          artwork.gfx_current->fullpath,
4196          artwork.gfx_current->basepath);
4197 #endif
4198
4199   setup.graphics_set = artwork.gfx_current->identifier;
4200   setup.sounds_set = artwork.snd_current->identifier;
4201   setup.music_set = artwork.mus_current->identifier;
4202
4203   /* needed if last screen (setup choice) changed graphics, sounds or music */
4204   ReloadCustomArtwork(0);
4205
4206   /* needed for displaying artwork name instead of artwork identifier */
4207   graphics_set_name = artwork.gfx_current->name;
4208   sounds_set_name = artwork.snd_current->name;
4209   music_set_name = artwork.mus_current->name;
4210
4211   setup_mode = SETUP_MODE_ARTWORK;
4212
4213   DrawSetupScreen();
4214 }
4215
4216 static void execSetupChooseGraphics()
4217 {
4218   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
4219
4220   DrawSetupScreen();
4221 }
4222
4223 static void execSetupChooseSounds()
4224 {
4225   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
4226
4227   DrawSetupScreen();
4228 }
4229
4230 static void execSetupChooseMusic()
4231 {
4232   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
4233
4234   DrawSetupScreen();
4235 }
4236
4237 static void execSetupInput()
4238 {
4239   setup_mode = SETUP_MODE_INPUT;
4240
4241   DrawSetupScreen();
4242 }
4243
4244 static void execSetupShortcuts()
4245 {
4246   setup_mode = SETUP_MODE_SHORTCUTS;
4247
4248   DrawSetupScreen();
4249 }
4250
4251 static void execSetupShortcuts1()
4252 {
4253   setup_mode = SETUP_MODE_SHORTCUTS_1;
4254
4255   DrawSetupScreen();
4256 }
4257
4258 static void execSetupShortcuts2()
4259 {
4260   setup_mode = SETUP_MODE_SHORTCUTS_2;
4261
4262   DrawSetupScreen();
4263 }
4264
4265 static void execSetupShortcuts3()
4266 {
4267   setup_mode = SETUP_MODE_SHORTCUTS_3;
4268
4269   DrawSetupScreen();
4270 }
4271
4272 static void execSetupShortcuts4()
4273 {
4274   setup_mode = SETUP_MODE_SHORTCUTS_4;
4275
4276   DrawSetupScreen();
4277 }
4278
4279 static void execSetupShortcuts5()
4280 {
4281   setup_mode = SETUP_MODE_SHORTCUTS_5;
4282
4283   DrawSetupScreen();
4284 }
4285
4286 static void execExitSetup()
4287 {
4288   game_status = GAME_MODE_MAIN;
4289
4290   DrawMainMenuExt(REDRAW_FIELD, FALSE);
4291 }
4292
4293 static void execSaveAndExitSetup()
4294 {
4295   SaveSetup();
4296   execExitSetup();
4297 }
4298
4299 static struct TokenInfo setup_info_main[] =
4300 {
4301   { TYPE_ENTER_MENU,    execSetupGame,          "Game & Menu"           },
4302   { TYPE_ENTER_MENU,    execSetupEditor,        "Editor"                },
4303   { TYPE_ENTER_MENU,    execSetupGraphics,      "Graphics"              },
4304   { TYPE_ENTER_MENU,    execSetupSound,         "Sound & Music"         },
4305   { TYPE_ENTER_MENU,    execSetupArtwork,       "Custom Artwork"        },
4306   { TYPE_ENTER_MENU,    execSetupInput,         "Input Devices"         },
4307   { TYPE_ENTER_MENU,    execSetupShortcuts,     "Key Shortcuts"         },
4308   { TYPE_EMPTY,         NULL,                   ""                      },
4309   { TYPE_LEAVE_MENU,    execExitSetup,          "Exit"                  },
4310   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   "Save and Exit"         },
4311
4312   { 0,                  NULL,                   NULL                    }
4313 };
4314
4315 static struct TokenInfo setup_info_game[] =
4316 {
4317   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode (Multi-Player):" },
4318   { TYPE_YES_NO,        &setup.input_on_focus,  "Only Move Focussed Player:" },
4319   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
4320   { TYPE_SWITCH,        &setup.skip_levels,     "Skip Unsolved Levels:" },
4321   { TYPE_SWITCH,        &setup.time_limit,      "Time Limit:"           },
4322   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record Tapes:"    },
4323   { TYPE_ENTER_LIST,    execSetupChooseGameSpeed, "Game Speed:"         },
4324   { TYPE_STRING,        &game_speed_text,       ""                      },
4325   { TYPE_EMPTY,         NULL,                   ""                      },
4326   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4327
4328   { 0,                  NULL,                   NULL                    }
4329 };
4330
4331 static struct TokenInfo setup_info_editor[] =
4332 {
4333 #if 0
4334   { TYPE_SWITCH,        &setup.editor.el_boulderdash,   "Boulder Dash:" },
4335   { TYPE_SWITCH,        &setup.editor.el_emerald_mine,  "Emerald Mine:" },
4336   { TYPE_SWITCH, &setup.editor.el_emerald_mine_club,    "Emerald Mine Club:" },
4337   { TYPE_SWITCH,        &setup.editor.el_more,          "Rocks'n'Diamonds:" },
4338   { TYPE_SWITCH,        &setup.editor.el_sokoban,       "Sokoban:"      },
4339   { TYPE_SWITCH,        &setup.editor.el_supaplex,      "Supaplex:"     },
4340   { TYPE_SWITCH,        &setup.editor.el_diamond_caves, "Diamond Caves II:" },
4341   { TYPE_SWITCH,        &setup.editor.el_dx_boulderdash,"DX-Boulderdash:" },
4342 #endif
4343   { TYPE_SWITCH,        &setup.editor.el_chars,         "Text Characters:" },
4344   { TYPE_SWITCH, &setup.editor.el_steel_chars, "Text Characters (Steel):" },
4345   { TYPE_SWITCH,        &setup.editor.el_custom,  "Custom & Group Elements:" },
4346 #if 0
4347   { TYPE_SWITCH,        &setup.editor.el_headlines,     "Headlines:"    },
4348 #endif
4349   { TYPE_SWITCH, &setup.editor.el_user_defined, "User defined element list:" },
4350   { TYPE_SWITCH,        &setup.editor.el_dynamic,  "Dynamic level elements:" },
4351   { TYPE_EMPTY,         NULL,                   ""                      },
4352 #if 0
4353   { TYPE_SWITCH,        &setup.editor.el_by_game,   "Show elements by game:" },
4354   { TYPE_SWITCH,        &setup.editor.el_by_type,   "Show elements by type:" },
4355   { TYPE_EMPTY,         NULL,                   ""                      },
4356 #endif
4357   { TYPE_SWITCH, &setup.editor.show_element_token,      "Show element token:" },
4358   { TYPE_EMPTY,         NULL,                   ""                      },
4359   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4360
4361   { 0,                  NULL,                   NULL                    }
4362 };
4363
4364 static struct TokenInfo setup_info_graphics[] =
4365 {
4366   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen:"           },
4367   { TYPE_ENTER_LIST,    execSetupChooseScreenMode, "Fullscreen Mode:"   },
4368   { TYPE_STRING,        &screen_mode_text,      ""                      },
4369 #if 0
4370   { TYPE_SWITCH,        &setup.scroll_delay,    "Scroll Delay:"         },
4371 #endif
4372   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay Value:" },
4373   { TYPE_STRING,        &scroll_delay_text,     ""                      },
4374 #if 0
4375   { TYPE_SWITCH,        &setup.soft_scrolling,  "Soft Scrolling:"       },
4376 #endif
4377   { TYPE_SWITCH,        &setup.fade_screens,    "Fade Screens:"         },
4378   { TYPE_SWITCH,        &setup.quick_switch,    "Quick Player Focus Switch:" },
4379   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Menu Doors:"     },
4380   { TYPE_SWITCH,        &setup.show_titlescreen,"Show Title Screens:"   },
4381   { TYPE_SWITCH,        &setup.toons,           "Show Toons:"           },
4382   { TYPE_ECS_AGA,       &setup.prefer_aga_graphics,"EMC graphics preference:" },
4383   { TYPE_SWITCH, &setup.sp_show_border_elements,"Supaplex Border Elements:" },
4384   { TYPE_SWITCH,        &setup.small_game_graphics, "Small Game Graphics:" },
4385   { TYPE_EMPTY,         NULL,                   ""                      },
4386   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4387
4388   { 0,                  NULL,                   NULL                    }
4389 };
4390
4391 static struct TokenInfo setup_info_sound[] =
4392 {
4393   { TYPE_SWITCH,        &setup.sound_simple,    "Sound Effects (Normal):"  },
4394   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Effects (Looping):" },
4395   { TYPE_SWITCH,        &setup.sound_music,     "Music:"                },
4396   { TYPE_EMPTY,         NULL,                   ""                      },
4397   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4398
4399   { 0,                  NULL,                   NULL                    }
4400 };
4401
4402 static struct TokenInfo setup_info_artwork[] =
4403 {
4404   { TYPE_ENTER_LIST,    execSetupChooseGraphics,"Custom Graphics:"      },
4405   { TYPE_STRING,        &graphics_set_name,     ""                      },
4406   { TYPE_ENTER_LIST,    execSetupChooseSounds,  "Custom Sounds:"        },
4407   { TYPE_STRING,        &sounds_set_name,       ""                      },
4408   { TYPE_ENTER_LIST,    execSetupChooseMusic,   "Custom Music:"         },
4409   { TYPE_STRING,        &music_set_name,        ""                      },
4410   { TYPE_EMPTY,         NULL,                   ""                      },
4411 #if 1
4412 #if 1
4413   { TYPE_YES_NO_AUTO,&setup.override_level_graphics,"Override Level Graphics:"},
4414   { TYPE_YES_NO_AUTO,&setup.override_level_sounds,  "Override Level Sounds:"  },
4415   { TYPE_YES_NO_AUTO,&setup.override_level_music,   "Override Level Music:"   },
4416 #else
4417   { TYPE_YES_NO, &setup.override_level_graphics,"Override Level Graphics:" },
4418   { TYPE_YES_NO, &setup.override_level_sounds,  "Override Level Sounds:"   },
4419   { TYPE_YES_NO, &setup.override_level_music,   "Override Level Music:"    },
4420   { TYPE_YES_NO, &setup.auto_override_artwork,  "Auto-Override Non-CE Sets:" },
4421 #endif
4422 #else
4423   { TYPE_STRING,        NULL,                   "Override Level Artwork:"},
4424   { TYPE_YES_NO,        &setup.override_level_graphics, "Graphics:"     },
4425   { TYPE_YES_NO,        &setup.override_level_sounds,   "Sounds:"       },
4426   { TYPE_YES_NO,        &setup.override_level_music,    "Music:"        },
4427 #endif
4428   { TYPE_EMPTY,         NULL,                   ""                      },
4429   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4430
4431   { 0,                  NULL,                   NULL                    }
4432 };
4433
4434 static struct TokenInfo setup_info_input[] =
4435 {
4436   { TYPE_SWITCH,        NULL,                   "Player:"               },
4437   { TYPE_SWITCH,        NULL,                   "Device:"               },
4438   { TYPE_ENTER_MENU,    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_EMPTY,         NULL,                   ""                      },
4446   { TYPE_EMPTY,         NULL,                   ""                      },
4447   { TYPE_EMPTY,         NULL,                   ""                      },
4448   { TYPE_EMPTY,         NULL,                   ""                      },
4449   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4450
4451   { 0,                  NULL,                   NULL                    }
4452 };
4453
4454 static struct TokenInfo setup_info_shortcuts[] =
4455 {
4456   { TYPE_ENTER_MENU,    execSetupShortcuts1,    "Various Keys"          },
4457   { TYPE_ENTER_MENU,    execSetupShortcuts2,    "Player Focus"          },
4458   { TYPE_ENTER_MENU,    execSetupShortcuts3,    "Tape Buttons"          },
4459   { TYPE_ENTER_MENU,    execSetupShortcuts4,    "Sound & Music"         },
4460   { TYPE_ENTER_MENU,    execSetupShortcuts5,    "TAS Snap Keys"         },
4461   { TYPE_EMPTY,         NULL,                   ""                      },
4462   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4463
4464   { 0,                  NULL,                   NULL                    }
4465 };
4466
4467 static struct TokenInfo setup_info_shortcuts_1[] =
4468 {
4469   { TYPE_KEYTEXT,       NULL,           "Quick Save Game to Tape:",     },
4470   { TYPE_KEY,           &setup.shortcut.save_game, ""                   },
4471   { TYPE_KEYTEXT,       NULL,           "Quick Load Game from Tape:",   },
4472   { TYPE_KEY,           &setup.shortcut.load_game, ""                   },
4473   { TYPE_KEYTEXT,       NULL,           "Start Game & Toggle Pause:",   },
4474   { TYPE_KEY,           &setup.shortcut.toggle_pause, ""                },
4475   { TYPE_EMPTY,         NULL,                   ""                      },
4476   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on 'Esc' Key:"     },
4477   { TYPE_YES_NO, &setup.ask_on_escape_editor,   "Ask on 'Esc' Key (Editor):" },
4478   { TYPE_EMPTY,         NULL,                   ""                      },
4479   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4480
4481   { 0,                  NULL,                   NULL                    }
4482 };
4483
4484 static struct TokenInfo setup_info_shortcuts_2[] =
4485 {
4486   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 1:",       },
4487   { TYPE_KEY,           &setup.shortcut.focus_player[0], ""             },
4488   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 2:",       },
4489   { TYPE_KEY,           &setup.shortcut.focus_player[1], ""             },
4490   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 3:",       },
4491   { TYPE_KEY,           &setup.shortcut.focus_player[2], ""             },
4492   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 4:",       },
4493   { TYPE_KEY,           &setup.shortcut.focus_player[3], ""             },
4494   { TYPE_KEYTEXT,       NULL,           "Set Focus to All Players:",    },
4495   { TYPE_KEY,           &setup.shortcut.focus_player_all, ""            },
4496   { TYPE_EMPTY,         NULL,                   ""                      },
4497   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4498
4499   { 0,                  NULL,                   NULL                    }
4500 };
4501
4502 static struct TokenInfo setup_info_shortcuts_3[] =
4503 {
4504   { TYPE_KEYTEXT,       NULL,                   "Eject Tape:",          },
4505   { TYPE_KEY,           &setup.shortcut.tape_eject, ""                  },
4506   { TYPE_KEYTEXT,       NULL,                   "Warp / Single Step:",  },
4507   { TYPE_KEY,           &setup.shortcut.tape_extra, ""                  },
4508   { TYPE_KEYTEXT,       NULL,                   "Stop Tape:",           },
4509   { TYPE_KEY,           &setup.shortcut.tape_stop, ""                   },
4510   { TYPE_KEYTEXT,       NULL,                   "Pause / Unpause Tape:",},
4511   { TYPE_KEY,           &setup.shortcut.tape_pause, ""                  },
4512   { TYPE_KEYTEXT,       NULL,                   "Record Tape:",         },
4513   { TYPE_KEY,           &setup.shortcut.tape_record, ""                 },
4514   { TYPE_KEYTEXT,       NULL,                   "Play Tape:",           },
4515   { TYPE_KEY,           &setup.shortcut.tape_play, ""                   },
4516   { TYPE_EMPTY,         NULL,                   ""                      },
4517   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4518
4519   { 0,                  NULL,                   NULL                    }
4520 };
4521
4522 static struct TokenInfo setup_info_shortcuts_4[] =
4523 {
4524   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Normal):", },
4525   { TYPE_KEY,           &setup.shortcut.sound_simple, ""                },
4526   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Looping):", },
4527   { TYPE_KEY,           &setup.shortcut.sound_loops, ""                 },
4528   { TYPE_KEYTEXT,       NULL,           "Toggle Music:",                },
4529   { TYPE_KEY,           &setup.shortcut.sound_music, ""                 },
4530   { TYPE_EMPTY,         NULL,                   ""                      },
4531   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4532
4533   { 0,                  NULL,                   NULL                    }
4534 };
4535
4536 static struct TokenInfo setup_info_shortcuts_5[] =
4537 {
4538   { TYPE_KEYTEXT,       NULL,                   "Snap Left:",           },
4539   { TYPE_KEY,           &setup.shortcut.snap_left, ""                   },
4540   { TYPE_KEYTEXT,       NULL,                   "Snap Right:",          },
4541   { TYPE_KEY,           &setup.shortcut.snap_right, ""                  },
4542   { TYPE_KEYTEXT,       NULL,                   "Snap Up:",             },
4543   { TYPE_KEY,           &setup.shortcut.snap_up, ""                     },
4544   { TYPE_KEYTEXT,       NULL,                   "Snap Down:",           },
4545   { TYPE_KEY,           &setup.shortcut.snap_down, ""                   },
4546   { TYPE_EMPTY,         NULL,                   ""                      },
4547   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4548
4549   { 0,                  NULL,                   NULL                    }
4550 };
4551
4552 static Key getSetupKey()
4553 {
4554   Key key = KSYM_UNDEFINED;
4555   boolean got_key_event = FALSE;
4556
4557   while (!got_key_event)
4558   {
4559     if (PendingEvent())         /* got event */
4560     {
4561       Event event;
4562
4563       NextEvent(&event);
4564
4565       switch (event.type)
4566       {
4567         case EVENT_KEYPRESS:
4568           {
4569             key = GetEventKey((KeyEvent *)&event, TRUE);
4570
4571             /* press 'Escape' or 'Enter' to keep the existing key binding */
4572             if (key == KSYM_Escape || key == KSYM_Return)
4573               key = KSYM_UNDEFINED;     /* keep old value */
4574
4575             got_key_event = TRUE;
4576           }
4577           break;
4578
4579         case EVENT_KEYRELEASE:
4580           key_joystick_mapping = 0;
4581           break;
4582
4583         default:
4584           HandleOtherEvents(&event);
4585           break;
4586       }
4587     }
4588
4589     DoAnimation();
4590     BackToFront();
4591
4592     /* don't eat all CPU time */
4593     Delay(10);
4594   }
4595
4596   return key;
4597 }
4598
4599 static int getSetupTextFont(int type)
4600 {
4601   if (type & (TYPE_SWITCH       |
4602               TYPE_YES_NO       |
4603               TYPE_YES_NO_AUTO  |
4604               TYPE_STRING       |
4605               TYPE_ECS_AGA      |
4606               TYPE_KEYTEXT      |
4607               TYPE_ENTER_LIST))
4608     return FONT_MENU_2;
4609   else
4610     return FONT_MENU_1;
4611 }
4612
4613 static int getSetupValueFont(int type, void *value)
4614 {
4615   if (type & TYPE_KEY)
4616     return (type & TYPE_QUERY ? FONT_INPUT_1_ACTIVE : FONT_VALUE_1);
4617   else if (type & TYPE_STRING)
4618     return FONT_VALUE_2;
4619   else if (type & TYPE_ECS_AGA)
4620     return FONT_VALUE_1;
4621   else if (type & TYPE_BOOLEAN_STYLE)
4622     return (*(boolean *)value ? FONT_OPTION_ON : FONT_OPTION_OFF);
4623   else if (type & TYPE_YES_NO_AUTO)
4624     return (*(int *)value == AUTO  ? FONT_OPTION_ON :
4625             *(int *)value == FALSE ? FONT_OPTION_OFF : FONT_OPTION_ON);
4626   else
4627     return FONT_VALUE_1;
4628 }
4629
4630 static void drawSetupValue(int pos)
4631 {
4632   boolean font_draw_xoffset_modified = FALSE;
4633   int font_draw_xoffset_old = -1;
4634   int xpos = MENU_SCREEN_VALUE_XPOS;
4635   int ypos = MENU_SCREEN_START_YPOS + pos;
4636   int startx = mSX + xpos * 32;
4637   int starty = mSY + ypos * 32;
4638   int font_nr, font_width, font_height;
4639   int type = setup_info[pos].type;
4640   void *value = setup_info[pos].value;
4641   char *value_string = getSetupValue(type, value);
4642 #if 1
4643   int i;
4644 #endif
4645
4646   if (value_string == NULL)
4647     return;
4648
4649   if (type & TYPE_KEY)
4650   {
4651     xpos = MENU_SCREEN_START_XPOS;
4652
4653     if (type & TYPE_QUERY)
4654       value_string = "<press key>";
4655   }
4656   else if (type & TYPE_STRING)
4657   {
4658     int max_value_len = (SCR_FIELDX - 2) * 2;
4659
4660     xpos = MENU_SCREEN_START_XPOS;
4661
4662     if (strlen(value_string) > max_value_len)
4663       value_string[max_value_len] = '\0';
4664   }
4665   else if (type & TYPE_YES_NO_AUTO)
4666   {
4667     xpos = MENU_SCREEN_VALUE_XPOS - 1;
4668   }
4669
4670   startx = mSX + xpos * 32;
4671   starty = mSY + ypos * 32;
4672   font_nr = getSetupValueFont(type, value);
4673   font_width = getFontWidth(font_nr);
4674   font_height = getFontHeight(font_nr);
4675
4676   /* downward compatibility correction for Juergen Bonhagen's menu settings */
4677   if (setup_mode != SETUP_MODE_INPUT)
4678   {
4679     int check_font_nr = FONT_OPTION_ON; /* known font that needs correction */
4680     int font1_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
4681     int font2_xoffset = getFontBitmapInfo(check_font_nr)->draw_xoffset;
4682     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
4683     int text_font_nr = getSetupTextFont(FONT_MENU_2);
4684     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
4685     int text_width = MAX_MENU_TEXT_LENGTH_MEDIUM * getFontWidth(text_font_nr);
4686     boolean correct_font_draw_xoffset = FALSE;
4687
4688     if (xpos == MENU_SCREEN_START_XPOS &&
4689         startx + font1_xoffset < text_startx + text_font_xoffset)
4690       correct_font_draw_xoffset = TRUE;
4691
4692     if (xpos == MENU_SCREEN_VALUE_XPOS &&
4693         startx + font2_xoffset < text_startx + text_width + text_font_xoffset)
4694       correct_font_draw_xoffset = TRUE;
4695
4696     /* check if setup value would overlap with setup text when printed */
4697     /* (this can happen for extreme/wrong values for font draw offset) */
4698     if (correct_font_draw_xoffset)
4699     {
4700       font_draw_xoffset_old = getFontBitmapInfo(font_nr)->draw_xoffset;
4701       font_draw_xoffset_modified = TRUE;
4702
4703       if (type & TYPE_KEY)
4704         getFontBitmapInfo(font_nr)->draw_xoffset += 2 * getFontWidth(font_nr);
4705       else if (!(type & TYPE_STRING))
4706         getFontBitmapInfo(font_nr)->draw_xoffset = text_font_xoffset + 20 -
4707           MAX_MENU_TEXT_LENGTH_MEDIUM * (16 - getFontWidth(text_font_nr));
4708     }
4709   }
4710
4711 #if 0
4712   DrawBackground(startx, starty, SX + SXSIZE - startx, font_height);
4713 #else
4714   for (i = 0; i <= MENU_SCREEN_MAX_XPOS - xpos; i++)
4715     DrawText(startx + i * font_width, starty, " ", font_nr);
4716 #endif
4717
4718   DrawText(startx, starty, value_string, font_nr);
4719
4720   if (font_draw_xoffset_modified)
4721     getFontBitmapInfo(font_nr)->draw_xoffset = font_draw_xoffset_old;
4722 }
4723
4724 static void changeSetupValue(int pos, int dx)
4725 {
4726   if (setup_info[pos].type & TYPE_BOOLEAN_STYLE)
4727   {
4728     *(boolean *)setup_info[pos].value ^= TRUE;
4729   }
4730   else if (setup_info[pos].type & TYPE_YES_NO_AUTO)
4731   {
4732     *(int *)setup_info[pos].value =
4733       (dx == -1 ?
4734        (*(int *)setup_info[pos].value == AUTO ? TRUE :
4735         *(int *)setup_info[pos].value == TRUE ? FALSE : AUTO) :
4736        (*(int *)setup_info[pos].value == TRUE ? AUTO :
4737         *(int *)setup_info[pos].value == AUTO ? FALSE : TRUE));
4738   }
4739   else if (setup_info[pos].type & TYPE_KEY)
4740   {
4741     Key key;
4742
4743     setup_info[pos].type |= TYPE_QUERY;
4744     drawSetupValue(pos);
4745     setup_info[pos].type &= ~TYPE_QUERY;
4746
4747     key = getSetupKey();
4748     if (key != KSYM_UNDEFINED)
4749       *(Key *)setup_info[pos].value = key;
4750   }
4751
4752   drawSetupValue(pos);
4753 }
4754
4755 static void DrawCursorAndText_Setup(int pos, boolean active)
4756 {
4757   int xpos = MENU_SCREEN_START_XPOS;
4758   int ypos = MENU_SCREEN_START_YPOS + pos;
4759   int font_nr = getSetupTextFont(setup_info[pos].type);
4760
4761   if (setup_info == setup_info_input)
4762     font_nr = FONT_MENU_1;
4763
4764   if (active)
4765     font_nr = FONT_ACTIVE(font_nr);
4766
4767   DrawText(mSX + xpos * 32, mSY + ypos * 32, setup_info[pos].text, font_nr);
4768
4769   if (setup_info[pos].type & ~TYPE_SKIP_ENTRY)
4770     drawCursor(pos, active);
4771 }
4772
4773 static void DrawSetupScreen_Generic()
4774 {
4775   boolean redraw_all = FALSE;
4776   char *title_string = NULL;
4777   int i;
4778
4779   UnmapAllGadgets();
4780   CloseDoor(DOOR_CLOSE_2);
4781
4782   if (redraw_mask & REDRAW_ALL)
4783     redraw_all = TRUE;
4784
4785 #if 0
4786   printf("::: %s\n", (redraw_mask & REDRAW_FIELD ? "REDRAW_FIELD" :
4787                       redraw_mask & REDRAW_ALL ? "REDRAW_ALL" :
4788                       int2str(0, redraw_mask)));
4789 #endif
4790
4791 #if 0
4792   /* !!! usually REDRAW_NONE => DOES NOT WORK (with fade) => CHECK THIS !!! */
4793   FadeOut(redraw_mask);
4794 #else
4795   FadeOut(REDRAW_FIELD);
4796 #endif
4797
4798   ClearField();
4799
4800   if (setup_mode == SETUP_MODE_MAIN)
4801   {
4802     setup_info = setup_info_main;
4803     title_string = "Setup";
4804   }
4805   else if (setup_mode == SETUP_MODE_GAME)
4806   {
4807     setup_info = setup_info_game;
4808     title_string = "Setup Game";
4809   }
4810   else if (setup_mode == SETUP_MODE_EDITOR)
4811   {
4812     setup_info = setup_info_editor;
4813     title_string = "Setup Editor";
4814   }
4815   else if (setup_mode == SETUP_MODE_GRAPHICS)
4816   {
4817     setup_info = setup_info_graphics;
4818     title_string = "Setup Graphics";
4819   }
4820   else if (setup_mode == SETUP_MODE_SOUND)
4821   {
4822     setup_info = setup_info_sound;
4823     title_string = "Setup Sound";
4824   }
4825   else if (setup_mode == SETUP_MODE_ARTWORK)
4826   {
4827     setup_info = setup_info_artwork;
4828     title_string = "Custom Artwork";
4829   }
4830   else if (setup_mode == SETUP_MODE_SHORTCUTS)
4831   {
4832     setup_info = setup_info_shortcuts;
4833     title_string = "Setup Shortcuts";
4834   }
4835   else if (setup_mode == SETUP_MODE_SHORTCUTS_1)
4836   {
4837     setup_info = setup_info_shortcuts_1;
4838     title_string = "Setup Shortcuts";
4839   }
4840   else if (setup_mode == SETUP_MODE_SHORTCUTS_2)
4841   {
4842     setup_info = setup_info_shortcuts_2;
4843     title_string = "Setup Shortcuts";
4844   }
4845   else if (setup_mode == SETUP_MODE_SHORTCUTS_3)
4846   {
4847     setup_info = setup_info_shortcuts_3;
4848     title_string = "Setup Shortcuts";
4849   }
4850   else if (setup_mode == SETUP_MODE_SHORTCUTS_4)
4851   {
4852     setup_info = setup_info_shortcuts_4;
4853     title_string = "Setup Shortcuts";
4854   }
4855   else if (setup_mode == SETUP_MODE_SHORTCUTS_5)
4856   {
4857     setup_info = setup_info_shortcuts_5;
4858     title_string = "Setup Shortcuts";
4859   }
4860
4861   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, title_string);
4862
4863   num_setup_info = 0;
4864 #if 1
4865   for (i = 0; setup_info[i].type != 0 && i < MAX_MENU_ENTRIES_ON_SCREEN; i++)
4866 #else
4867   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
4868 #endif
4869   {
4870     void *value_ptr = setup_info[i].value;
4871
4872     /* set some entries to "unchangeable" according to other variables */
4873     if ((value_ptr == &setup.sound_simple && !audio.sound_available) ||
4874         (value_ptr == &setup.sound_loops  && !audio.loops_available) ||
4875         (value_ptr == &setup.sound_music  && !audio.music_available) ||
4876         (value_ptr == &setup.fullscreen   && !video.fullscreen_available) ||
4877         (value_ptr == &screen_mode_text   && !video.fullscreen_available))
4878       setup_info[i].type |= TYPE_GHOSTED;
4879
4880     if (setup_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
4881       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
4882     else if (setup_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
4883       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
4884     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
4885       initCursor(i, IMG_MENU_BUTTON);
4886
4887     DrawCursorAndText_Setup(i, FALSE);
4888
4889     if (setup_info[i].type & TYPE_VALUE)
4890       drawSetupValue(i);
4891
4892     num_setup_info++;
4893   }
4894
4895 #if 0
4896   DrawTextSCentered(SYSIZE - 20, FONT_TEXT_4,
4897                     "Joysticks deactivated in setup menu");
4898 #endif
4899
4900 #if 1
4901   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
4902 #endif
4903
4904   if (redraw_all)
4905     redraw_mask = REDRAW_ALL;
4906
4907 #if 1
4908   FadeIn(redraw_mask);
4909 #else
4910   FadeIn(REDRAW_FIELD);
4911 #endif
4912
4913   InitAnimation();
4914 #if 0
4915   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
4916 #endif
4917 }
4918
4919 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
4920 {
4921   static int choice_store[MAX_SETUP_MODES];
4922   int choice = choice_store[setup_mode];        /* always starts with 0 */
4923   int x = 0;
4924   int y = choice;
4925
4926   if (button == MB_MENU_INITIALIZE)
4927   {
4928     /* advance to first valid menu entry */
4929     while (choice < num_setup_info &&
4930            setup_info[choice].type & TYPE_SKIP_ENTRY)
4931       choice++;
4932     choice_store[setup_mode] = choice;
4933
4934     DrawCursorAndText_Setup(choice, TRUE);
4935
4936     return;
4937   }
4938   else if (button == MB_MENU_LEAVE)
4939   {
4940     PlaySound(SND_MENU_ITEM_SELECTING);
4941
4942     for (y = 0; y < num_setup_info; y++)
4943     {
4944       if (setup_info[y].type & TYPE_LEAVE_MENU)
4945       {
4946         void (*menu_callback_function)(void) = setup_info[y].value;
4947
4948         FadeSetLeaveMenu();
4949
4950         menu_callback_function();
4951
4952         break;  /* absolutely needed because function changes 'setup_info'! */
4953       }
4954     }
4955
4956     return;
4957   }
4958
4959   if (mx || my)         /* mouse input */
4960   {
4961     x = (mx - mSX) / 32;
4962     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
4963   }
4964   else if (dx || dy)    /* keyboard input */
4965   {
4966     if (dx)
4967     {
4968       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE : TYPE_ENTER);
4969
4970       if (setup_info[choice].type & menu_navigation_type ||
4971           setup_info[choice].type & TYPE_BOOLEAN_STYLE ||
4972           setup_info[choice].type & TYPE_YES_NO_AUTO)
4973         button = MB_MENU_CHOICE;
4974     }
4975     else if (dy)
4976       y = choice + dy;
4977
4978     /* jump to next non-empty menu entry (up or down) */
4979     while (y > 0 && y < num_setup_info - 1 &&
4980            setup_info[y].type & TYPE_SKIP_ENTRY)
4981       y += dy;
4982   }
4983
4984   if (IN_VIS_FIELD(x, y) && y >= 0 && y < num_setup_info)
4985   {
4986     if (button)
4987     {
4988       if (y != choice && setup_info[y].type & ~TYPE_SKIP_ENTRY)
4989       {
4990         PlaySound(SND_MENU_ITEM_ACTIVATING);
4991
4992         DrawCursorAndText_Setup(choice, FALSE);
4993         DrawCursorAndText_Setup(y, TRUE);
4994
4995         choice = choice_store[setup_mode] = y;
4996       }
4997     }
4998     else if (!(setup_info[y].type & TYPE_GHOSTED))
4999     {
5000       PlaySound(SND_MENU_ITEM_SELECTING);
5001
5002       /* when selecting key headline, execute function for key value change */
5003       if (setup_info[y].type & TYPE_KEYTEXT &&
5004           setup_info[y + 1].type & TYPE_KEY)
5005         y++;
5006
5007       /* when selecting string value, execute function for list selection */
5008       if (setup_info[y].type & TYPE_STRING && y > 0 &&
5009           setup_info[y - 1].type & TYPE_ENTER_LIST)
5010         y--;
5011
5012       if (setup_info[y].type & TYPE_ENTER_OR_LEAVE)
5013       {
5014         void (*menu_callback_function)(void) = setup_info[y].value;
5015
5016         FadeSetFromType(setup_info[y].type);
5017
5018         menu_callback_function();
5019       }
5020       else
5021       {
5022         if (setup_info[y].type & TYPE_VALUE)
5023           changeSetupValue(y, dx);
5024       }
5025     }
5026   }
5027 }
5028
5029 void DrawSetupScreen_Input()
5030 {
5031   int i;
5032
5033 #if 1
5034   FadeOut(REDRAW_FIELD);
5035 #endif
5036
5037   ClearField();
5038
5039 #if 1
5040   setup_info = setup_info_input;
5041 #endif
5042
5043   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Setup Input");
5044
5045 #if 1
5046 #if 1
5047   DrawTextSCentered(SYSIZE - 20, FONT_TITLE_2,
5048                     "Joysticks deactivated on this screen");
5049 #else
5050   DrawTextSCentered(SYSIZE - 20, FONT_TEXT_4,
5051                     "Joysticks deactivated on this screen");
5052 #endif
5053 #endif
5054
5055 #if 1
5056   for (i = 0; setup_info[i].type != 0 && i < MAX_MENU_ENTRIES_ON_SCREEN; i++)
5057 #else
5058   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
5059 #endif
5060   {
5061     if (setup_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
5062       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
5063     else if (setup_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
5064       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
5065     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
5066       initCursor(i, IMG_MENU_BUTTON);
5067
5068     DrawCursorAndText_Setup(i, FALSE);
5069   }
5070
5071 #if 0
5072   DeactivateJoystickForCalibration();
5073 #endif
5074
5075 #if 0
5076 #if 1
5077   DrawTextSCentered(SYSIZE - 20, FONT_TITLE_2,
5078                     "Joysticks deactivated on this screen");
5079 #else
5080   DrawTextSCentered(SYSIZE - 20, FONT_TEXT_4,
5081                     "Joysticks deactivated on this screen");
5082 #endif
5083 #endif
5084
5085   /* create gadgets for setup input menu screen */
5086   FreeScreenGadgets();
5087   CreateScreenGadgets();
5088
5089   /* map gadgets for setup input menu screen */
5090   MapScreenMenuGadgets(SCREEN_MASK_INPUT);
5091
5092   HandleSetupScreen_Input(0, 0, 0, 0, MB_MENU_INITIALIZE);
5093
5094 #if 1
5095   FadeIn(REDRAW_FIELD);
5096 #endif
5097
5098   InitAnimation();
5099 }
5100
5101 static void setJoystickDeviceToNr(char *device_name, int device_nr)
5102 {
5103   if (device_name == NULL)
5104     return;
5105
5106   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
5107     device_nr = 0;
5108
5109   if (strlen(device_name) > 1)
5110   {
5111     char c1 = device_name[strlen(device_name) - 1];
5112     char c2 = device_name[strlen(device_name) - 2];
5113
5114     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
5115       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
5116   }
5117   else
5118     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
5119             strlen(device_name));
5120 }
5121
5122 static void drawPlayerSetupInputInfo(int player_nr, boolean active)
5123 {
5124   int i;
5125   static struct SetupKeyboardInfo custom_key;
5126   static struct
5127   {
5128     Key *key;
5129     char *text;
5130   } custom[] =
5131   {
5132     { &custom_key.left,  "Joystick Left"  },
5133     { &custom_key.right, "Joystick Right" },
5134     { &custom_key.up,    "Joystick Up"    },
5135     { &custom_key.down,  "Joystick Down"  },
5136     { &custom_key.snap,  "Button 1"       },
5137     { &custom_key.drop,  "Button 2"       }
5138   };
5139   static char *joystick_name[MAX_PLAYERS] =
5140   {
5141     "Joystick1",
5142     "Joystick2",
5143     "Joystick3",
5144     "Joystick4"
5145   };
5146   int text_font_nr = (active ? FONT_MENU_1_ACTIVE : FONT_MENU_1);
5147
5148   InitJoysticks();
5149
5150   custom_key = setup.input[player_nr].key;
5151
5152   DrawText(mSX + 11 * 32, mSY + 2 * 32, int2str(player_nr + 1, 1),
5153            FONT_INPUT_1_ACTIVE);
5154
5155   ClearRectangleOnBackground(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
5156                              TILEX, TILEY);
5157   DrawFixedGraphicThruMaskExt(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
5158                               PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
5159
5160   if (setup.input[player_nr].use_joystick)
5161   {
5162     char *device_name = setup.input[player_nr].joy.device_name;
5163     char *text = joystick_name[getJoystickNrFromDeviceName(device_name)];
5164     int font_nr = (joystick.fd[player_nr] < 0 ? FONT_VALUE_OLD : FONT_VALUE_1);
5165
5166     DrawText(mSX + 8 * 32, mSY + 3 * 32, text, font_nr);
5167     DrawText(mSX + 32, mSY + 4 * 32, "Calibrate", text_font_nr);
5168   }
5169   else
5170   {
5171     DrawText(mSX + 8 * 32, mSY + 3 * 32, "Keyboard ", FONT_VALUE_1);
5172     DrawText(mSX + 1 * 32, mSY + 4 * 32, "Customize", text_font_nr);
5173   }
5174
5175   DrawText(mSX + 32, mSY + 5 * 32, "Actual Settings:", FONT_MENU_1);
5176
5177   drawCursorXY(1, 4, IMG_MENU_BUTTON_LEFT);
5178   drawCursorXY(1, 5, IMG_MENU_BUTTON_RIGHT);
5179   drawCursorXY(1, 6, IMG_MENU_BUTTON_UP);
5180   drawCursorXY(1, 7, IMG_MENU_BUTTON_DOWN);
5181
5182   DrawText(mSX + 2 * 32, mSY +  6 * 32, ":", FONT_VALUE_OLD);
5183   DrawText(mSX + 2 * 32, mSY +  7 * 32, ":", FONT_VALUE_OLD);
5184   DrawText(mSX + 2 * 32, mSY +  8 * 32, ":", FONT_VALUE_OLD);
5185   DrawText(mSX + 2 * 32, mSY +  9 * 32, ":", FONT_VALUE_OLD);
5186   DrawText(mSX + 1 * 32, mSY + 10 * 32, "Snap Field:", FONT_VALUE_OLD);
5187   DrawText(mSX + 1 * 32, mSY + 12 * 32, "Drop Element:", FONT_VALUE_OLD);
5188
5189   for (i = 0; i < 6; i++)
5190   {
5191     int ypos = 6 + i + (i > 3 ? i-3 : 0);
5192
5193     DrawText(mSX + 3 * 32, mSY + ypos * 32,
5194              "              ", FONT_VALUE_1);
5195     DrawText(mSX + 3 * 32, mSY + ypos * 32,
5196              (setup.input[player_nr].use_joystick ?
5197               custom[i].text :
5198               getKeyNameFromKey(*custom[i].key)), FONT_VALUE_1);
5199   }
5200 }
5201
5202 static int input_player_nr = 0;
5203
5204 void HandleSetupScreen_Input_Player(int step, int direction)
5205 {
5206   int old_player_nr = input_player_nr;
5207   int new_player_nr;
5208
5209   new_player_nr = old_player_nr + step * direction;
5210   if (new_player_nr < 0)
5211     new_player_nr = 0;
5212   if (new_player_nr > MAX_PLAYERS - 1)
5213     new_player_nr = MAX_PLAYERS - 1;
5214
5215   if (new_player_nr != old_player_nr)
5216   {
5217     input_player_nr = new_player_nr;
5218
5219     drawPlayerSetupInputInfo(input_player_nr, FALSE);
5220   }
5221 }
5222
5223 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
5224 {
5225   static int choice = 0;
5226   int x = 0;
5227   int y = choice;
5228   int pos_start  = SETUPINPUT_SCREEN_POS_START;
5229   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
5230   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
5231   int pos_end    = SETUPINPUT_SCREEN_POS_END;
5232
5233   if (button == MB_MENU_INITIALIZE)
5234   {
5235     drawPlayerSetupInputInfo(input_player_nr, (choice == 2));
5236
5237     DrawCursorAndText_Setup(choice, TRUE);
5238
5239     return;
5240   }
5241   else if (button == MB_MENU_LEAVE)
5242   {
5243     setup_mode = SETUP_MODE_MAIN;
5244     DrawSetupScreen();
5245     InitJoysticks();
5246
5247     return;
5248   }
5249
5250   if (mx || my)         /* mouse input */
5251   {
5252     x = (mx - mSX) / 32;
5253     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
5254   }
5255   else if (dx || dy)    /* keyboard input */
5256   {
5257     if (dx && choice == 0)
5258       x = (dx < 0 ? 10 : 12);
5259     else if ((dx && choice == 1) ||
5260              (dx == +1 && choice == 2) ||
5261              (dx == -1 && choice == pos_end))
5262       button = MB_MENU_CHOICE;
5263     else if (dy)
5264       y = choice + dy;
5265
5266     if (y >= pos_empty1 && y <= pos_empty2)
5267       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
5268   }
5269
5270   if (y == 0 && dx != 0 && button)
5271   {
5272     HandleSetupScreen_Input_Player(1, dx < 0 ? -1 : +1);
5273   }
5274   else if (IN_VIS_FIELD(x, y) &&
5275            y >= pos_start && y <= pos_end &&
5276            !(y >= pos_empty1 && y <= pos_empty2))
5277   {
5278     if (button)
5279     {
5280       if (y != choice)
5281       {
5282         DrawCursorAndText_Setup(choice, FALSE);
5283         DrawCursorAndText_Setup(y, TRUE);
5284
5285         drawPlayerSetupInputInfo(input_player_nr, (y == 2));
5286
5287         choice = y;
5288       }
5289     }
5290     else
5291     {
5292       if (y == 1)
5293       {
5294         char *device_name = setup.input[input_player_nr].joy.device_name;
5295
5296         if (!setup.input[input_player_nr].use_joystick)
5297         {
5298           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
5299
5300           setJoystickDeviceToNr(device_name, new_device_nr);
5301           setup.input[input_player_nr].use_joystick = TRUE;
5302         }
5303         else
5304         {
5305           int device_nr = getJoystickNrFromDeviceName(device_name);
5306           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
5307
5308           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
5309             setup.input[input_player_nr].use_joystick = FALSE;
5310           else
5311             setJoystickDeviceToNr(device_name, new_device_nr);
5312         }
5313
5314         drawPlayerSetupInputInfo(input_player_nr, FALSE);
5315       }
5316       else if (y == 2)
5317       {
5318         if (setup.input[input_player_nr].use_joystick)
5319         {
5320           InitJoysticks();
5321           CalibrateJoystick(input_player_nr);
5322         }
5323         else
5324           CustomizeKeyboard(input_player_nr);
5325       }
5326       else if (y == pos_end)
5327       {
5328         InitJoysticks();
5329
5330         FadeSetLeaveMenu();
5331
5332         setup_mode = SETUP_MODE_MAIN;
5333         DrawSetupScreen();
5334       }
5335     }
5336   }
5337 }
5338
5339 void CustomizeKeyboard(int player_nr)
5340 {
5341   int i;
5342   int step_nr;
5343   boolean finished = FALSE;
5344   static struct SetupKeyboardInfo custom_key;
5345   static struct
5346   {
5347     Key *key;
5348     char *text;
5349   } customize_step[] =
5350   {
5351     { &custom_key.left,  "Move Left"    },
5352     { &custom_key.right, "Move Right"   },
5353     { &custom_key.up,    "Move Up"      },
5354     { &custom_key.down,  "Move Down"    },
5355     { &custom_key.snap,  "Snap Field"   },
5356     { &custom_key.drop,  "Drop Element" }
5357   };
5358
5359   /* read existing key bindings from player setup */
5360   custom_key = setup.input[player_nr].key;
5361
5362   FadeSetEnterMenu();
5363   FadeOut(REDRAW_FIELD);
5364
5365   ClearField();
5366
5367   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Keyboard Input");
5368
5369 #if 0
5370   BackToFront();
5371   InitAnimation();
5372 #endif
5373
5374   step_nr = 0;
5375   DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
5376            customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
5377   DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
5378            "Key:", FONT_INPUT_1_ACTIVE);
5379   DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5380            getKeyNameFromKey(*customize_step[step_nr].key), FONT_VALUE_OLD);
5381
5382 #if 1
5383   FadeIn(REDRAW_FIELD);
5384
5385   InitAnimation();
5386 #endif
5387
5388   while (!finished)
5389   {
5390     if (PendingEvent())         /* got event */
5391     {
5392       Event event;
5393
5394       NextEvent(&event);
5395
5396       switch (event.type)
5397       {
5398         case EVENT_KEYPRESS:
5399           {
5400             Key key = GetEventKey((KeyEvent *)&event, FALSE);
5401
5402             if (key == KSYM_Escape || (key == KSYM_Return && step_nr == 6))
5403             {
5404               if (key == KSYM_Escape)
5405                 FadeSkipNextFadeIn();
5406
5407               finished = TRUE;
5408               break;
5409             }
5410
5411             /* all keys configured -- wait for "Escape" or "Return" key */
5412             if (step_nr == 6)
5413               break;
5414
5415             /* press 'Enter' to keep the existing key binding */
5416             if (key == KSYM_Return)
5417               key = *customize_step[step_nr].key;
5418
5419             /* check if key already used */
5420             for (i = 0; i < step_nr; i++)
5421               if (*customize_step[i].key == key)
5422                 break;
5423             if (i < step_nr)
5424               break;
5425
5426             /* got new key binding */
5427             *customize_step[step_nr].key = key;
5428             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5429                      "             ", FONT_VALUE_1);
5430             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5431                      getKeyNameFromKey(key), FONT_VALUE_1);
5432             step_nr++;
5433
5434             /* un-highlight last query */
5435             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1)) * 32,
5436                      customize_step[step_nr - 1].text, FONT_MENU_1);
5437             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1) + 1) * 32,
5438                      "Key:", FONT_MENU_1);
5439
5440             /* press 'Enter' to leave */
5441             if (step_nr == 6)
5442             {
5443               DrawText(mSX + 16, mSY + 15 * 32 + 16,
5444                        "Press Enter", FONT_TITLE_1);
5445               break;
5446             }
5447
5448             /* query next key binding */
5449             DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
5450                      customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
5451             DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
5452                      "Key:", FONT_INPUT_1_ACTIVE);
5453             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5454                      getKeyNameFromKey(*customize_step[step_nr].key),
5455                      FONT_VALUE_OLD);
5456           }
5457           break;
5458
5459         case EVENT_KEYRELEASE:
5460           key_joystick_mapping = 0;
5461           break;
5462
5463         default:
5464           HandleOtherEvents(&event);
5465           break;
5466       }
5467     }
5468
5469     DoAnimation();
5470     BackToFront();
5471
5472     /* don't eat all CPU time */
5473     Delay(10);
5474   }
5475
5476   /* write new key bindings back to player setup */
5477   setup.input[player_nr].key = custom_key;
5478
5479   StopAnimation();
5480   DrawSetupScreen_Input();
5481 }
5482
5483 static boolean CalibrateJoystickMain(int player_nr)
5484 {
5485   int new_joystick_xleft = JOYSTICK_XMIDDLE;
5486   int new_joystick_xright = JOYSTICK_XMIDDLE;
5487   int new_joystick_yupper = JOYSTICK_YMIDDLE;
5488   int new_joystick_ylower = JOYSTICK_YMIDDLE;
5489   int new_joystick_xmiddle, new_joystick_ymiddle;
5490
5491   int joystick_fd = joystick.fd[player_nr];
5492   int x, y, last_x, last_y, xpos = 8, ypos = 3;
5493   boolean check[3][3];
5494   int check_remaining = 3 * 3;
5495   int joy_x, joy_y;
5496   int joy_value;
5497   int result = -1;
5498
5499   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
5500     return FALSE;
5501
5502   if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
5503     return FALSE;
5504
5505   FadeSetEnterMenu();
5506   FadeOut(REDRAW_FIELD);
5507
5508   ClearField();
5509
5510   for (y = 0; y < 3; y++)
5511   {
5512     for (x = 0; x < 3; x++)
5513     {
5514       DrawFixedGraphic(xpos + x - 1, ypos + y - 1, IMG_MENU_CALIBRATE_BLUE, 0);
5515       check[x][y] = FALSE;
5516     }
5517   }
5518
5519   DrawTextSCentered(mSY - SY +  6 * 32, FONT_TITLE_1, "Rotate joystick");
5520   DrawTextSCentered(mSY - SY +  7 * 32, FONT_TITLE_1, "in all directions");
5521   DrawTextSCentered(mSY - SY +  9 * 32, FONT_TITLE_1, "if all balls");
5522   DrawTextSCentered(mSY - SY + 10 * 32, FONT_TITLE_1, "are marked,");
5523   DrawTextSCentered(mSY - SY + 11 * 32, FONT_TITLE_1, "center joystick");
5524   DrawTextSCentered(mSY - SY + 12 * 32, FONT_TITLE_1, "and");
5525   DrawTextSCentered(mSY - SY + 13 * 32, FONT_TITLE_1, "press any button!");
5526
5527   joy_value = Joystick(player_nr);
5528   last_x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
5529   last_y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
5530
5531   /* eventually uncalibrated center position (joystick could be uncentered) */
5532   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
5533     return FALSE;
5534
5535   new_joystick_xmiddle = joy_x;
5536   new_joystick_ymiddle = joy_y;
5537
5538   DrawFixedGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_RED, 0);
5539
5540   FadeIn(REDRAW_FIELD);
5541
5542   while (Joystick(player_nr) & JOY_BUTTON);     /* wait for released button */
5543   InitAnimation();
5544
5545   while (result < 0)
5546   {
5547     if (PendingEvent())         /* got event */
5548     {
5549       Event event;
5550
5551       NextEvent(&event);
5552
5553       switch (event.type)
5554       {
5555         case EVENT_KEYPRESS:
5556           switch (GetEventKey((KeyEvent *)&event, TRUE))
5557           {
5558             case KSYM_Return:
5559               if (check_remaining == 0)
5560                 result = 1;
5561               break;
5562
5563             case KSYM_Escape:
5564               FadeSkipNextFadeIn();
5565               result = 0;
5566               break;
5567
5568             default:
5569               break;
5570           }
5571           break;
5572
5573         case EVENT_KEYRELEASE:
5574           key_joystick_mapping = 0;
5575           break;
5576
5577         default:
5578           HandleOtherEvents(&event);
5579           break;
5580       }
5581     }
5582
5583     if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
5584       return FALSE;
5585
5586     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_x);
5587     new_joystick_xright = MAX(new_joystick_xright, joy_x);
5588     new_joystick_yupper = MIN(new_joystick_yupper, joy_y);
5589     new_joystick_ylower = MAX(new_joystick_ylower, joy_y);
5590
5591     setup.input[player_nr].joy.xleft = new_joystick_xleft;
5592     setup.input[player_nr].joy.yupper = new_joystick_yupper;
5593     setup.input[player_nr].joy.xright = new_joystick_xright;
5594     setup.input[player_nr].joy.ylower = new_joystick_ylower;
5595     setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
5596     setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
5597
5598     CheckJoystickData();
5599
5600     joy_value = Joystick(player_nr);
5601
5602     if (joy_value & JOY_BUTTON && check_remaining == 0)
5603       result = 1;
5604
5605     x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
5606     y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
5607
5608     if (x != last_x || y != last_y)
5609     {
5610       DrawFixedGraphic(xpos + last_x, ypos + last_y,
5611                        IMG_MENU_CALIBRATE_YELLOW, 0);
5612       DrawFixedGraphic(xpos + x,      ypos + y,
5613                        IMG_MENU_CALIBRATE_RED,    0);
5614
5615       last_x = x;
5616       last_y = y;
5617
5618       if (check_remaining > 0 && !check[x+1][y+1])
5619       {
5620         check[x+1][y+1] = TRUE;
5621         check_remaining--;
5622       }
5623
5624 #if 0
5625 #ifdef DEBUG
5626       printf("LEFT / MIDDLE / RIGHT == %d / %d / %d\n",
5627              setup.input[player_nr].joy.xleft,
5628              setup.input[player_nr].joy.xmiddle,
5629              setup.input[player_nr].joy.xright);
5630       printf("UP / MIDDLE / DOWN == %d / %d / %d\n",
5631              setup.input[player_nr].joy.yupper,
5632              setup.input[player_nr].joy.ymiddle,
5633              setup.input[player_nr].joy.ylower);
5634 #endif
5635 #endif
5636
5637     }
5638
5639     DoAnimation();
5640     BackToFront();
5641
5642     /* don't eat all CPU time */
5643     Delay(10);
5644   }
5645
5646   /* calibrated center position (joystick should now be centered) */
5647   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
5648     return FALSE;
5649
5650   new_joystick_xmiddle = joy_x;
5651   new_joystick_ymiddle = joy_y;
5652
5653   StopAnimation();
5654
5655 #if 0
5656   DrawSetupScreen_Input();
5657 #endif
5658
5659   /* wait until the last pressed button was released */
5660   while (Joystick(player_nr) & JOY_BUTTON)
5661   {
5662     if (PendingEvent())         /* got event */
5663     {
5664       Event event;
5665
5666       NextEvent(&event);
5667       HandleOtherEvents(&event);
5668
5669       Delay(10);
5670     }
5671   }
5672
5673   return TRUE;
5674 }
5675
5676 void CalibrateJoystick(int player_nr)
5677 {
5678   if (!CalibrateJoystickMain(player_nr))
5679   {
5680     char *device_name = setup.input[player_nr].joy.device_name;
5681     int nr = getJoystickNrFromDeviceName(device_name) + 1;
5682     int xpos = mSX - SX;
5683     int ypos = mSY - SY;
5684
5685     ClearField();
5686
5687     DrawTextF(xpos + 16, ypos + 6 * 32, FONT_TITLE_1, "   JOYSTICK %d   ", nr);
5688     DrawTextF(xpos + 16, ypos + 7 * 32, FONT_TITLE_1, " NOT AVAILABLE! ");
5689     BackToFront();
5690
5691     Delay(2000);                /* show error message for a short time */
5692
5693     ClearEventQueue();
5694   }
5695
5696 #if 1
5697   DrawSetupScreen_Input();
5698 #endif
5699 }
5700
5701 void DrawSetupScreen()
5702 {
5703   DeactivateJoystick();
5704
5705   SetMainBackgroundImage(IMG_BACKGROUND_SETUP);
5706
5707   if (setup_mode == SETUP_MODE_INPUT)
5708     DrawSetupScreen_Input();
5709   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
5710     DrawChooseTree(&game_speed_current);
5711   else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE)
5712     DrawChooseTree(&screen_mode_current);
5713   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
5714     DrawChooseTree(&scroll_delay_current);
5715   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
5716     DrawChooseTree(&artwork.gfx_current);
5717   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
5718     DrawChooseTree(&artwork.snd_current);
5719   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
5720     DrawChooseTree(&artwork.mus_current);
5721   else
5722     DrawSetupScreen_Generic();
5723
5724   PlayMenuSound();
5725   PlayMenuMusic();
5726 }
5727
5728 void RedrawSetupScreenAfterFullscreenToggle()
5729 {
5730   if (setup_mode == SETUP_MODE_GRAPHICS)
5731     DrawSetupScreen();
5732 }
5733
5734 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
5735 {
5736   if (setup_mode == SETUP_MODE_INPUT)
5737     HandleSetupScreen_Input(mx, my, dx, dy, button);
5738   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
5739     HandleChooseTree(mx, my, dx, dy, button, &game_speed_current);
5740   else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE)
5741     HandleChooseTree(mx, my, dx, dy, button, &screen_mode_current);
5742   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
5743     HandleChooseTree(mx, my, dx, dy, button, &scroll_delay_current);
5744   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
5745     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
5746   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
5747     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
5748   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
5749     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
5750   else
5751     HandleSetupScreen_Generic(mx, my, dx, dy, button);
5752
5753   DoAnimation();
5754 }
5755
5756 void HandleGameActions()
5757 {
5758   if (game_status != GAME_MODE_PLAYING)
5759     return;
5760
5761   GameActions();        /* main game loop */
5762
5763   if (tape.auto_play && !tape.playing)
5764     AutoPlayTape();     /* continue automatically playing next tape */
5765 }
5766
5767
5768 /* ---------- new screen button stuff -------------------------------------- */
5769
5770 static void getScreenMenuButtonPos(int *x, int *y, int gadget_id)
5771 {
5772   switch (gadget_id)
5773   {
5774 #if 1
5775     case SCREEN_CTRL_ID_PREV_LEVEL:
5776       *x = mSX + menu.main.button.prev_level.x;
5777       *y = mSY + menu.main.button.prev_level.y;
5778       break;
5779
5780     case SCREEN_CTRL_ID_NEXT_LEVEL:
5781       *x = mSX + menu.main.button.next_level.x;
5782       *y = mSY + menu.main.button.next_level.y;
5783       break;
5784 #else
5785     case SCREEN_CTRL_ID_PREV_LEVEL:
5786       *x = mSX + TILEX * getPrevlevelButtonPos();
5787       *y = mSY + TILEY * (MENU_SCREEN_START_YPOS + 1);
5788       break;
5789
5790     case SCREEN_CTRL_ID_NEXT_LEVEL:
5791       *x = mSX + TILEX * getNextLevelButtonPos();
5792       *y = mSY + TILEY * (MENU_SCREEN_START_YPOS + 1);
5793       break;
5794 #endif
5795
5796     case SCREEN_CTRL_ID_PREV_PLAYER:
5797       *x = mSX + TILEX * 10;
5798       *y = mSY + TILEY * MENU_SCREEN_START_YPOS;
5799       break;
5800
5801     case SCREEN_CTRL_ID_NEXT_PLAYER:
5802       *x = mSX + TILEX * 12;
5803       *y = mSY + TILEY * MENU_SCREEN_START_YPOS;
5804       break;
5805
5806     default:
5807       Error(ERR_EXIT, "unknown gadget ID %d", gadget_id);
5808   }
5809 }
5810
5811 static struct
5812 {
5813   int gfx_unpressed, gfx_pressed;
5814   void (*get_gadget_position)(int *, int *, int);
5815   int gadget_id;
5816   int screen_mask;
5817   char *infotext;
5818 } menubutton_info[NUM_SCREEN_MENUBUTTONS] =
5819 {
5820   {
5821     IMG_MENU_BUTTON_PREV_LEVEL, IMG_MENU_BUTTON_PREV_LEVEL_ACTIVE,
5822     getScreenMenuButtonPos,
5823     SCREEN_CTRL_ID_PREV_LEVEL,
5824     SCREEN_MASK_MAIN,
5825     "last level"
5826   },
5827   {
5828     IMG_MENU_BUTTON_NEXT_LEVEL, IMG_MENU_BUTTON_NEXT_LEVEL_ACTIVE,
5829     getScreenMenuButtonPos,
5830     SCREEN_CTRL_ID_NEXT_LEVEL,
5831     SCREEN_MASK_MAIN,
5832     "next level"
5833   },
5834   {
5835     IMG_MENU_BUTTON_LEFT, IMG_MENU_BUTTON_LEFT_ACTIVE,
5836     getScreenMenuButtonPos,
5837     SCREEN_CTRL_ID_PREV_PLAYER,
5838     SCREEN_MASK_INPUT,
5839     "last player"
5840   },
5841   {
5842     IMG_MENU_BUTTON_RIGHT, IMG_MENU_BUTTON_RIGHT_ACTIVE,
5843     getScreenMenuButtonPos,
5844     SCREEN_CTRL_ID_NEXT_PLAYER,
5845     SCREEN_MASK_INPUT,
5846     "next player"
5847   },
5848 };
5849
5850 static struct
5851 {
5852   int gfx_unpressed, gfx_pressed;
5853   int x, y;
5854   int gadget_id;
5855   char *infotext;
5856 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
5857 {
5858   {
5859     IMG_MENU_BUTTON_UP, IMG_MENU_BUTTON_UP_ACTIVE,
5860 #if 1
5861     -1, -1,     /* these values are not constant, but can change at runtime */
5862 #else
5863     SC_SCROLL_UP_XPOS, SC_SCROLL_UP_YPOS,
5864 #endif
5865     SCREEN_CTRL_ID_SCROLL_UP,
5866     "scroll up"
5867   },
5868   {
5869     IMG_MENU_BUTTON_DOWN, IMG_MENU_BUTTON_DOWN_ACTIVE,
5870 #if 1
5871     -1, -1,     /* these values are not constant, but can change at runtime */
5872 #else
5873     SC_SCROLL_DOWN_XPOS, SC_SCROLL_DOWN_YPOS,
5874 #endif
5875     SCREEN_CTRL_ID_SCROLL_DOWN,
5876     "scroll down"
5877   }
5878 };
5879
5880 static struct
5881 {
5882 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
5883   Bitmap **gfx_unpressed, **gfx_pressed;
5884 #else
5885   int gfx_unpressed, gfx_pressed;
5886 #endif
5887   int x, y;
5888   int width, height;
5889   int type;
5890   int gadget_id;
5891   char *infotext;
5892 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
5893 {
5894   {
5895 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
5896     &scrollbar_bitmap[0], &scrollbar_bitmap[1],
5897 #else
5898     IMG_MENU_SCROLLBAR, IMG_MENU_SCROLLBAR_ACTIVE,
5899 #endif
5900 #if 1
5901     -1, -1,     /* these values are not constant, but can change at runtime */
5902     -1, -1,     /* these values are not constant, but can change at runtime */
5903 #else
5904     SC_SCROLL_VERTICAL_XPOS, SC_SCROLL_VERTICAL_YPOS,
5905     SC_SCROLL_VERTICAL_XSIZE, SC_SCROLL_VERTICAL_YSIZE,
5906 #endif
5907     GD_TYPE_SCROLLBAR_VERTICAL,
5908     SCREEN_CTRL_ID_SCROLL_VERTICAL,
5909     "scroll level series vertically"
5910   }
5911 };
5912
5913 static void CreateScreenMenubuttons()
5914 {
5915   struct GadgetInfo *gi;
5916   unsigned long event_mask;
5917   int i;
5918
5919   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
5920   {
5921     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
5922     int gfx_unpressed, gfx_pressed;
5923     int x, y, width, height;
5924     int gd_x1, gd_x2, gd_y1, gd_y2;
5925     int id = menubutton_info[i].gadget_id;
5926
5927     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
5928
5929     menubutton_info[i].get_gadget_position(&x, &y, id);
5930
5931     width = SC_MENUBUTTON_XSIZE;
5932     height = SC_MENUBUTTON_YSIZE;
5933
5934     gfx_unpressed = menubutton_info[i].gfx_unpressed;
5935     gfx_pressed   = menubutton_info[i].gfx_pressed;
5936     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
5937     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
5938     gd_x1 = graphic_info[gfx_unpressed].src_x;
5939     gd_y1 = graphic_info[gfx_unpressed].src_y;
5940     gd_x2 = graphic_info[gfx_pressed].src_x;
5941     gd_y2 = graphic_info[gfx_pressed].src_y;
5942
5943     gi = CreateGadget(GDI_CUSTOM_ID, id,
5944                       GDI_CUSTOM_TYPE_ID, i,
5945                       GDI_INFO_TEXT, menubutton_info[i].infotext,
5946                       GDI_X, x,
5947                       GDI_Y, y,
5948                       GDI_WIDTH, width,
5949                       GDI_HEIGHT, height,
5950                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
5951                       GDI_STATE, GD_BUTTON_UNPRESSED,
5952                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
5953                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
5954                       GDI_DIRECT_DRAW, FALSE,
5955                       GDI_EVENT_MASK, event_mask,
5956                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
5957                       GDI_END);
5958
5959     if (gi == NULL)
5960       Error(ERR_EXIT, "cannot create gadget");
5961
5962     screen_gadget[id] = gi;
5963   }
5964 }
5965
5966 static void CreateScreenScrollbuttons()
5967 {
5968   struct GadgetInfo *gi;
5969   unsigned long event_mask;
5970   int i;
5971
5972   /* these values are not constant, but can change at runtime */
5973   scrollbutton_info[0].x = SC_SCROLL_UP_XPOS;
5974   scrollbutton_info[0].y = SC_SCROLL_UP_YPOS;
5975   scrollbutton_info[1].x = SC_SCROLL_DOWN_XPOS;
5976   scrollbutton_info[1].y = SC_SCROLL_DOWN_YPOS;
5977
5978   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
5979   {
5980     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
5981     int gfx_unpressed, gfx_pressed;
5982     int x, y, width, height;
5983     int gd_x1, gd_x2, gd_y1, gd_y2;
5984     int id = scrollbutton_info[i].gadget_id;
5985
5986     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
5987
5988     x = mSX + scrollbutton_info[i].x + menu.scrollbar_xoffset;
5989     y = mSY + scrollbutton_info[i].y;
5990     width = SC_SCROLLBUTTON_XSIZE;
5991     height = SC_SCROLLBUTTON_YSIZE;
5992
5993     if (id == SCREEN_CTRL_ID_SCROLL_DOWN)
5994       y = mSY + (SC_SCROLL_VERTICAL_YPOS +
5995                  (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE);
5996
5997     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
5998     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
5999     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6000     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6001     gd_x1 = graphic_info[gfx_unpressed].src_x;
6002     gd_y1 = graphic_info[gfx_unpressed].src_y;
6003     gd_x2 = graphic_info[gfx_pressed].src_x;
6004     gd_y2 = graphic_info[gfx_pressed].src_y;
6005
6006     gi = CreateGadget(GDI_CUSTOM_ID, id,
6007                       GDI_CUSTOM_TYPE_ID, i,
6008                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
6009                       GDI_X, x,
6010                       GDI_Y, y,
6011                       GDI_WIDTH, width,
6012                       GDI_HEIGHT, height,
6013                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
6014                       GDI_STATE, GD_BUTTON_UNPRESSED,
6015                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6016                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6017                       GDI_DIRECT_DRAW, FALSE,
6018                       GDI_EVENT_MASK, event_mask,
6019                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6020                       GDI_END);
6021
6022     if (gi == NULL)
6023       Error(ERR_EXIT, "cannot create gadget");
6024
6025     screen_gadget[id] = gi;
6026   }
6027 }
6028
6029 static void CreateScreenScrollbars()
6030 {
6031   int i;
6032
6033   /* these values are not constant, but can change at runtime */
6034   scrollbar_info[0].x = SC_SCROLL_VERTICAL_XPOS;
6035   scrollbar_info[0].y = SC_SCROLL_VERTICAL_YPOS;
6036   scrollbar_info[0].width  = SC_SCROLL_VERTICAL_XSIZE;
6037   scrollbar_info[0].height = SC_SCROLL_VERTICAL_YSIZE;
6038
6039   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
6040   {
6041     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6042 #if !defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6043     int gfx_unpressed, gfx_pressed;
6044 #endif
6045     int x, y, width, height;
6046     int gd_x1, gd_x2, gd_y1, gd_y2;
6047     struct GadgetInfo *gi;
6048     int items_max, items_visible, item_position;
6049     unsigned long event_mask;
6050     int num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
6051     int id = scrollbar_info[i].gadget_id;
6052
6053     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
6054
6055     x = mSX + scrollbar_info[i].x + menu.scrollbar_xoffset;
6056     y = mSY + scrollbar_info[i].y;
6057     width  = scrollbar_info[i].width;
6058     height = scrollbar_info[i].height;
6059
6060     if (id == SCREEN_CTRL_ID_SCROLL_VERTICAL)
6061       height = (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE;
6062
6063     items_max = num_page_entries;
6064     items_visible = num_page_entries;
6065     item_position = 0;
6066
6067 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6068     gd_bitmap_unpressed = *scrollbar_info[i].gfx_unpressed;
6069     gd_bitmap_pressed   = *scrollbar_info[i].gfx_pressed;
6070     gd_x1 = 0;
6071     gd_y1 = 0;
6072     gd_x2 = 0;
6073     gd_y2 = 0;
6074 #else
6075     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
6076     gfx_pressed   = scrollbar_info[i].gfx_pressed;
6077     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6078     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6079     gd_x1 = graphic_info[gfx_unpressed].src_x;
6080     gd_y1 = graphic_info[gfx_unpressed].src_y;
6081     gd_x2 = graphic_info[gfx_pressed].src_x;
6082     gd_y2 = graphic_info[gfx_pressed].src_y;
6083 #endif
6084
6085     gi = CreateGadget(GDI_CUSTOM_ID, id,
6086                       GDI_CUSTOM_TYPE_ID, i,
6087                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
6088                       GDI_X, x,
6089                       GDI_Y, y,
6090                       GDI_WIDTH, width,
6091                       GDI_HEIGHT, height,
6092                       GDI_TYPE, scrollbar_info[i].type,
6093                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
6094                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
6095                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
6096 #if 1
6097                       GDI_WHEEL_AREA_X, SX,
6098                       GDI_WHEEL_AREA_Y, SY,
6099                       GDI_WHEEL_AREA_WIDTH, SXSIZE,
6100                       GDI_WHEEL_AREA_HEIGHT, SYSIZE,
6101 #else
6102                       GDI_WHEEL_AREA_X, 0,
6103                       GDI_WHEEL_AREA_Y, 0,
6104                       GDI_WHEEL_AREA_WIDTH, WIN_XSIZE,
6105                       GDI_WHEEL_AREA_HEIGHT, WIN_YSIZE,
6106 #endif
6107                       GDI_STATE, GD_BUTTON_UNPRESSED,
6108                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6109                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6110                       GDI_BORDER_SIZE, SC_BORDER_SIZE, SC_BORDER_SIZE,
6111                       GDI_DIRECT_DRAW, FALSE,
6112                       GDI_EVENT_MASK, event_mask,
6113                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6114                       GDI_END);
6115
6116     if (gi == NULL)
6117       Error(ERR_EXIT, "cannot create gadget");
6118
6119     screen_gadget[id] = gi;
6120   }
6121 }
6122
6123 void CreateScreenGadgets()
6124 {
6125   int last_game_status = game_status;   /* save current game status */
6126
6127 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6128   int i;
6129
6130   for (i = 0; i < NUM_SCROLLBAR_BITMAPS; i++)
6131   {
6132     scrollbar_bitmap[i] = CreateBitmap(TILEX, TILEY, DEFAULT_DEPTH);
6133
6134     /* copy pointers to clip mask and GC */
6135     scrollbar_bitmap[i]->clip_mask =
6136       graphic_info[IMG_MENU_SCROLLBAR + i].clip_mask;
6137     scrollbar_bitmap[i]->stored_clip_gc =
6138       graphic_info[IMG_MENU_SCROLLBAR + i].clip_gc;
6139
6140     BlitBitmap(graphic_info[IMG_MENU_SCROLLBAR + i].bitmap,
6141                scrollbar_bitmap[i],
6142                graphic_info[IMG_MENU_SCROLLBAR + i].src_x,
6143                graphic_info[IMG_MENU_SCROLLBAR + i].src_y,
6144                TILEX, TILEY, 0, 0);
6145   }
6146 #endif
6147
6148   CreateScreenMenubuttons();
6149
6150 #if 0
6151   /* force LEVELS draw offset for scrollbar / scrollbutton gadgets */
6152   game_status = GAME_MODE_LEVELS;
6153 #endif
6154
6155   CreateScreenScrollbuttons();
6156   CreateScreenScrollbars();
6157
6158   game_status = last_game_status;       /* restore current game status */
6159 }
6160
6161 void FreeScreenGadgets()
6162 {
6163   int i;
6164
6165 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6166   for (i = 0; i < NUM_SCROLLBAR_BITMAPS; i++)
6167   {
6168     /* prevent freeing clip mask and GC twice */
6169     scrollbar_bitmap[i]->clip_mask = None;
6170     scrollbar_bitmap[i]->stored_clip_gc = None;
6171
6172     FreeBitmap(scrollbar_bitmap[i]);
6173   }
6174 #endif
6175
6176   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
6177     FreeGadget(screen_gadget[i]);
6178 }
6179
6180 void MapScreenMenuGadgets(int screen_mask)
6181 {
6182   int i;
6183
6184   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
6185     if (screen_mask & menubutton_info[i].screen_mask)
6186       MapGadget(screen_gadget[menubutton_info[i].gadget_id]);
6187 }
6188
6189 void MapScreenTreeGadgets(TreeInfo *ti)
6190 {
6191   int num_entries = numTreeInfoInGroup(ti);
6192   int i;
6193
6194   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
6195     return;
6196
6197   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
6198     MapGadget(screen_gadget[scrollbutton_info[i].gadget_id]);
6199
6200   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
6201     MapGadget(screen_gadget[scrollbar_info[i].gadget_id]);
6202 }
6203
6204 static void HandleScreenGadgets(struct GadgetInfo *gi)
6205 {
6206   int id = gi->custom_id;
6207   int button = gi->event.button;
6208   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
6209
6210   switch (id)
6211   {
6212     case SCREEN_CTRL_ID_PREV_LEVEL:
6213       HandleMainMenu_SelectLevel(step, -1);
6214       break;
6215
6216     case SCREEN_CTRL_ID_NEXT_LEVEL:
6217       HandleMainMenu_SelectLevel(step, +1);
6218       break;
6219
6220     case SCREEN_CTRL_ID_PREV_PLAYER:
6221       HandleSetupScreen_Input_Player(step, -1);
6222       break;
6223
6224     case SCREEN_CTRL_ID_NEXT_PLAYER:
6225       HandleSetupScreen_Input_Player(step, +1);
6226       break;
6227
6228     case SCREEN_CTRL_ID_SCROLL_UP:
6229       if (game_status == GAME_MODE_LEVELS)
6230         HandleChooseLevelSet(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6231       else if (game_status == GAME_MODE_LEVELNR)
6232         HandleChooseLevelNr(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6233       else if (game_status == GAME_MODE_SETUP)
6234         HandleSetupScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6235       break;
6236
6237     case SCREEN_CTRL_ID_SCROLL_DOWN:
6238       if (game_status == GAME_MODE_LEVELS)
6239         HandleChooseLevelSet(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
6240       else if (game_status == GAME_MODE_LEVELNR)
6241         HandleChooseLevelNr(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
6242       else if (game_status == GAME_MODE_SETUP)
6243         HandleSetupScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
6244       break;
6245
6246     case SCREEN_CTRL_ID_SCROLL_VERTICAL:
6247       if (game_status == GAME_MODE_LEVELS)
6248         HandleChooseLevelSet(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
6249       else if (game_status == GAME_MODE_LEVELNR)
6250         HandleChooseLevelNr(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
6251       else if (game_status == GAME_MODE_SETUP)
6252         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
6253       break;
6254
6255     default:
6256       break;
6257   }
6258 }