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