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