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