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