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