added configurability of transparency for virtual buttons
[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, i_to_a(1));
5386
5387     /* if that also fails, set current distance to first available value */
5388     if (move_distance_current == NULL)
5389       move_distance_current = move_distances;
5390   }
5391
5392   if (drop_distances == NULL)
5393   {
5394     int i;
5395
5396     for (i = 0; distances_list[i].value != -1; i++)
5397     {
5398       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5399       char identifier[32], name[32];
5400       int value = distances_list[i].value;
5401       char *text = distances_list[i].text;
5402
5403       ti->node_top = &drop_distances;
5404       ti->sort_priority = value;
5405
5406       sprintf(identifier, "%d", value);
5407       sprintf(name, "%s", text);
5408
5409       setString(&ti->identifier, identifier);
5410       setString(&ti->name, name);
5411       setString(&ti->name_sorting, name);
5412       setString(&ti->infotext, STR_SETUP_CHOOSE_DROP_DISTANCE);
5413
5414       pushTreeInfo(&drop_distances, ti);
5415     }
5416
5417     /* sort distance values to start with lowest distance value */
5418     sortTreeInfo(&drop_distances);
5419
5420     /* set current distance value to configured distance value */
5421     drop_distance_current =
5422       getTreeInfoFromIdentifier(drop_distances,
5423                                 i_to_a(setup.touch.drop_distance));
5424
5425     /* if that fails, set current distance to reliable default value */
5426     if (drop_distance_current == NULL)
5427       drop_distance_current =
5428         getTreeInfoFromIdentifier(drop_distances, i_to_a(1));
5429
5430     /* if that also fails, set current distance to first available value */
5431     if (drop_distance_current == NULL)
5432       drop_distance_current = drop_distances;
5433   }
5434
5435   if (transparencies == NULL)
5436   {
5437     int i;
5438
5439     for (i = 0; transparencies_list[i].value != -1; i++)
5440     {
5441       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5442       char identifier[32], name[32];
5443       int value = transparencies_list[i].value;
5444       char *text = transparencies_list[i].text;
5445
5446       ti->node_top = &transparencies;
5447       ti->sort_priority = value;
5448
5449       sprintf(identifier, "%d", value);
5450       sprintf(name, "%s", text);
5451
5452       setString(&ti->identifier, identifier);
5453       setString(&ti->name, name);
5454       setString(&ti->name_sorting, name);
5455       setString(&ti->infotext, STR_SETUP_CHOOSE_TRANSPARENCY);
5456
5457       pushTreeInfo(&transparencies, ti);
5458     }
5459
5460     /* sort transparency values to start with lowest transparency value */
5461     sortTreeInfo(&transparencies);
5462
5463     /* set current transparency value to configured transparency value */
5464     transparency_current =
5465       getTreeInfoFromIdentifier(transparencies,
5466                                 i_to_a(setup.touch.transparency));
5467
5468     /* if that fails, set current transparency to reliable default value */
5469     if (transparency_current == NULL)
5470       transparency_current =
5471         getTreeInfoFromIdentifier(transparencies,
5472                                   i_to_a(TOUCH_TRANSPARENCY_DEFAULT));
5473
5474     /* if that also fails, set current transparency to first available value */
5475     if (transparency_current == NULL)
5476       transparency_current = transparencies;
5477   }
5478
5479   setup.touch.control_type = touch_control_current->identifier;
5480   setup.touch.move_distance = atoi(move_distance_current->identifier);
5481   setup.touch.drop_distance = atoi(drop_distance_current->identifier);
5482   setup.touch.transparency = atoi(transparency_current->identifier);
5483
5484   /* needed for displaying volume text instead of identifier */
5485   touch_controls_text = touch_control_current->name;
5486   move_distance_text = move_distance_current->name;
5487   drop_distance_text = drop_distance_current->name;
5488   transparency_text = transparency_current->name;
5489
5490   setup_mode = SETUP_MODE_TOUCH;
5491
5492   DrawSetupScreen();
5493 }
5494
5495 static void execSetupArtwork()
5496 {
5497 #if 0
5498   printf("::: '%s', '%s', '%s'\n",
5499          artwork.gfx_current->subdir,
5500          artwork.gfx_current->fullpath,
5501          artwork.gfx_current->basepath);
5502 #endif
5503
5504   setup.graphics_set = artwork.gfx_current->identifier;
5505   setup.sounds_set = artwork.snd_current->identifier;
5506   setup.music_set = artwork.mus_current->identifier;
5507
5508   /* needed if last screen (setup choice) changed graphics, sounds or music */
5509   ReloadCustomArtwork(0);
5510
5511   /* needed for displaying artwork name instead of artwork identifier */
5512   graphics_set_name = artwork.gfx_current->name;
5513   sounds_set_name = artwork.snd_current->name;
5514   music_set_name = artwork.mus_current->name;
5515
5516   setup_mode = SETUP_MODE_ARTWORK;
5517
5518   DrawSetupScreen();
5519 }
5520
5521 static void execSetupChooseGraphics()
5522 {
5523   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
5524
5525   DrawSetupScreen();
5526 }
5527
5528 static void execSetupChooseSounds()
5529 {
5530   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
5531
5532   DrawSetupScreen();
5533 }
5534
5535 static void execSetupChooseMusic()
5536 {
5537   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
5538
5539   DrawSetupScreen();
5540 }
5541
5542 static void execSetupInput()
5543 {
5544   setup_mode = SETUP_MODE_INPUT;
5545
5546   DrawSetupScreen();
5547 }
5548
5549 static void execSetupShortcuts()
5550 {
5551   setup_mode = SETUP_MODE_SHORTCUTS;
5552
5553   DrawSetupScreen();
5554 }
5555
5556 static void execSetupShortcuts1()
5557 {
5558   setup_mode = SETUP_MODE_SHORTCUTS_1;
5559
5560   DrawSetupScreen();
5561 }
5562
5563 static void execSetupShortcuts2()
5564 {
5565   setup_mode = SETUP_MODE_SHORTCUTS_2;
5566
5567   DrawSetupScreen();
5568 }
5569
5570 static void execSetupShortcuts3()
5571 {
5572   setup_mode = SETUP_MODE_SHORTCUTS_3;
5573
5574   DrawSetupScreen();
5575 }
5576
5577 static void execSetupShortcuts4()
5578 {
5579   setup_mode = SETUP_MODE_SHORTCUTS_4;
5580
5581   DrawSetupScreen();
5582 }
5583
5584 static void execSetupShortcuts5()
5585 {
5586   setup_mode = SETUP_MODE_SHORTCUTS_5;
5587
5588   DrawSetupScreen();
5589 }
5590
5591 static void execExitSetup()
5592 {
5593   SetGameStatus(GAME_MODE_MAIN);
5594
5595   DrawMainMenu();
5596 }
5597
5598 static void execSaveAndExitSetup()
5599 {
5600   SaveSetup();
5601   execExitSetup();
5602 }
5603
5604 static struct
5605 {
5606   void *value;
5607   void *related_value;
5608 } hide_related_entry_list[] =
5609 {
5610   { &setup.game_frame_delay,            execSetupChooseGameSpeed        },
5611   { &setup.game_frame_delay,            &game_speed_text                },
5612
5613   { &setup.scroll_delay_value,          execSetupChooseScrollDelay      },
5614   { &setup.scroll_delay_value,          &scroll_delay_text              },
5615
5616   { &setup.engine_snapshot_mode,        execSetupChooseSnapshotMode     },
5617   { &setup.engine_snapshot_mode,        &snapshot_mode_text             },
5618
5619   { &setup.window_scaling_percent,      execSetupChooseWindowSize       },
5620   { &setup.window_scaling_percent,      &window_size_text               },
5621
5622   { &setup.window_scaling_quality,      execSetupChooseScalingType      },
5623   { &setup.window_scaling_quality,      &scaling_type_text              },
5624
5625   { &setup.screen_rendering_mode,       execSetupChooseRenderingMode    },
5626   { &setup.screen_rendering_mode,       &rendering_mode_text            },
5627
5628   { &setup.graphics_set,                execSetupChooseGraphics         },
5629   { &setup.graphics_set,                &graphics_set_name              },
5630
5631   { &setup.sounds_set,                  execSetupChooseSounds           },
5632   { &setup.sounds_set,                  &sounds_set_name                },
5633
5634   { &setup.music_set,                   execSetupChooseMusic            },
5635   { &setup.music_set,                   &music_set_name                 },
5636
5637   { &setup.volume_simple,               execSetupChooseVolumeSimple     },
5638   { &setup.volume_simple,               &volume_simple_text             },
5639
5640   { &setup.volume_loops,                execSetupChooseVolumeLoops      },
5641   { &setup.volume_loops,                &volume_loops_text              },
5642
5643   { &setup.volume_music,                execSetupChooseVolumeMusic      },
5644   { &setup.volume_music,                &volume_music_text              },
5645
5646   { &setup.touch.control_type,          execSetupChooseTouchControls    },
5647   { &setup.touch.control_type,          &touch_controls_text            },
5648
5649   { &setup.touch.move_distance,         execSetupChooseMoveDistance     },
5650   { &setup.touch.move_distance,         &move_distance_text             },
5651
5652   { &setup.touch.drop_distance,         execSetupChooseDropDistance     },
5653   { &setup.touch.drop_distance,         &drop_distance_text             },
5654
5655   { &setup.touch.transparency,          execSetupChooseTransparency     },
5656   { &setup.touch.transparency,          &transparency_text              },
5657
5658   { NULL,                               NULL                            }
5659 };
5660
5661 void setHideRelatedSetupEntries()
5662 {
5663   int i;
5664
5665   for (i = 0; hide_related_entry_list[i].value != NULL; i++)
5666     if (hideSetupEntry(hide_related_entry_list[i].value))
5667       setHideSetupEntry(hide_related_entry_list[i].related_value);
5668 }
5669
5670 static struct TokenInfo setup_info_main[] =
5671 {
5672   { TYPE_ENTER_MENU,    execSetupGame,          STR_SETUP_GAME          },
5673   { TYPE_ENTER_MENU,    execSetupEditor,        STR_SETUP_EDITOR        },
5674   { TYPE_ENTER_MENU,    execSetupGraphics,      STR_SETUP_GRAPHICS      },
5675   { TYPE_ENTER_MENU,    execSetupSound,         STR_SETUP_SOUND         },
5676   { TYPE_ENTER_MENU,    execSetupArtwork,       STR_SETUP_ARTWORK       },
5677   { TYPE_ENTER_MENU,    execSetupInput,         STR_SETUP_INPUT         },
5678   { TYPE_ENTER_MENU,    execSetupTouch,         STR_SETUP_TOUCH         },
5679   { TYPE_ENTER_MENU,    execSetupShortcuts,     STR_SETUP_SHORTCUTS     },
5680   { TYPE_EMPTY,         NULL,                   ""                      },
5681   { TYPE_LEAVE_MENU,    execExitSetup,          STR_SETUP_EXIT          },
5682   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   STR_SETUP_SAVE_AND_EXIT },
5683
5684   { 0,                  NULL,                   NULL                    }
5685 };
5686
5687 static struct TokenInfo setup_info_game[] =
5688 {
5689   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode (Multi-Player):" },
5690   { TYPE_YES_NO,        &setup.input_on_focus,  "Only Move Focussed Player:" },
5691   { TYPE_SWITCH,        &setup.time_limit,      "Time Limit:"           },
5692   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
5693   { TYPE_SWITCH,        &setup.skip_levels,     "Skip Unsolved Levels:" },
5694   { TYPE_SWITCH,        &setup.increment_levels,"Increment Solved Levels:" },
5695   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record Tapes:"    },
5696   { TYPE_ENTER_LIST,    execSetupChooseGameSpeed, "Game Speed:"         },
5697   { TYPE_STRING,        &game_speed_text,       ""                      },
5698 #if 1
5699   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay:"     },
5700   { TYPE_STRING,        &scroll_delay_text,     ""                      },
5701 #endif
5702   { TYPE_ENTER_LIST, execSetupChooseSnapshotMode,"Game Engine Snapshot Mode:" },
5703   { TYPE_STRING,        &snapshot_mode_text,    ""                      },
5704   { TYPE_SWITCH,        &setup.show_snapshot_buttons,"Show Snapshot Buttons:" },
5705   { TYPE_EMPTY,         NULL,                   ""                      },
5706   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5707
5708   { 0,                  NULL,                   NULL                    }
5709 };
5710
5711 static struct TokenInfo setup_info_editor[] =
5712 {
5713 #if 0
5714   { TYPE_SWITCH,        &setup.editor.el_boulderdash,   "Boulder Dash:" },
5715   { TYPE_SWITCH,        &setup.editor.el_emerald_mine,  "Emerald Mine:" },
5716   { TYPE_SWITCH, &setup.editor.el_emerald_mine_club,    "Emerald Mine Club:" },
5717   { TYPE_SWITCH,        &setup.editor.el_more,          "Rocks'n'Diamonds:" },
5718   { TYPE_SWITCH,        &setup.editor.el_sokoban,       "Sokoban:"      },
5719   { TYPE_SWITCH,        &setup.editor.el_supaplex,      "Supaplex:"     },
5720   { TYPE_SWITCH,        &setup.editor.el_diamond_caves, "Diamond Caves II:" },
5721   { TYPE_SWITCH,        &setup.editor.el_dx_boulderdash,"DX-Boulderdash:" },
5722   { TYPE_SWITCH,        &setup.editor.el_chars,         "Text Characters:" },
5723   { TYPE_SWITCH, &setup.editor.el_steel_chars, "Text Characters (Steel):" },
5724 #endif
5725   { TYPE_SWITCH,        &setup.editor.el_classic,  "Classic Elements:" },
5726   { TYPE_SWITCH,        &setup.editor.el_custom,  "Custom & Group Elements:" },
5727 #if 0
5728   { TYPE_SWITCH,        &setup.editor.el_headlines,     "Headlines:"    },
5729 #endif
5730   { TYPE_SWITCH, &setup.editor.el_user_defined, "User defined element list:" },
5731   { TYPE_SWITCH,        &setup.editor.el_dynamic,  "Dynamic level elements:" },
5732   { TYPE_EMPTY,         NULL,                   ""                      },
5733 #if 0
5734   { TYPE_SWITCH,        &setup.editor.el_by_game,   "Show elements by game:" },
5735   { TYPE_SWITCH,        &setup.editor.el_by_type,   "Show elements by type:" },
5736   { TYPE_EMPTY,         NULL,                   ""                      },
5737 #endif
5738   { TYPE_SWITCH, &setup.editor.show_element_token,      "Show element token:" },
5739   { TYPE_EMPTY,         NULL,                   ""                      },
5740   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5741
5742   { 0,                  NULL,                   NULL                    }
5743 };
5744
5745 static struct TokenInfo setup_info_graphics[] =
5746 {
5747 #if defined(TARGET_SDL2) && !defined(PLATFORM_ANDROID)
5748   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen:"           },
5749   { TYPE_ENTER_LIST,    execSetupChooseWindowSize, "Window Scaling:"    },
5750   { TYPE_STRING,        &window_size_text,      ""                      },
5751   { TYPE_ENTER_LIST,    execSetupChooseScalingType, "Anti-Aliasing:"    },
5752   { TYPE_STRING,        &scaling_type_text,     ""                      },
5753   { TYPE_ENTER_LIST,    execSetupChooseRenderingMode, "Special Rendering:" },
5754   { TYPE_STRING,        &rendering_mode_text,   ""                      },
5755 #endif
5756 #if 0
5757   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay:"     },
5758   { TYPE_STRING,        &scroll_delay_text,     ""                      },
5759 #endif
5760   { TYPE_SWITCH,        &setup.fade_screens,    "Fade Screens:"         },
5761   { TYPE_SWITCH,        &setup.quick_switch,    "Quick Player Focus Switch:" },
5762   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Menu Doors:"     },
5763   { TYPE_SWITCH,        &setup.show_titlescreen,"Show Title Screens:"   },
5764   { TYPE_SWITCH,        &setup.toons,           "Show Menu Animations:" },
5765   { TYPE_ECS_AGA,       &setup.prefer_aga_graphics,"EMC graphics preference:" },
5766   { TYPE_SWITCH, &setup.sp_show_border_elements,"Supaplex Border Elements:" },
5767   { TYPE_SWITCH,        &setup.small_game_graphics, "Small Game Graphics:" },
5768   { TYPE_EMPTY,         NULL,                   ""                      },
5769   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5770
5771   { 0,                  NULL,                   NULL                    }
5772 };
5773
5774 static struct TokenInfo setup_info_sound[] =
5775 {
5776   { TYPE_SWITCH,        &setup.sound_simple,    "Sound Effects (Normal):"  },
5777   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Effects (Looping):" },
5778   { TYPE_SWITCH,        &setup.sound_music,     "Music:"                },
5779   { TYPE_EMPTY,         NULL,                   ""                      },
5780   { TYPE_ENTER_LIST,    execSetupChooseVolumeSimple, "Sound Volume (Normal):" },
5781   { TYPE_STRING,        &volume_simple_text,    ""                      },
5782   { TYPE_ENTER_LIST,    execSetupChooseVolumeLoops, "Sound Volume (Looping):" },
5783   { TYPE_STRING,        &volume_loops_text,     ""                      },
5784   { TYPE_ENTER_LIST,    execSetupChooseVolumeMusic, "Music Volume:"     },
5785   { TYPE_STRING,        &volume_music_text,     ""                      },
5786   { TYPE_EMPTY,         NULL,                   ""                      },
5787   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5788
5789   { 0,                  NULL,                   NULL                    }
5790 };
5791
5792 static struct TokenInfo setup_info_artwork[] =
5793 {
5794   { TYPE_ENTER_LIST,    execSetupChooseGraphics,"Custom Graphics:"      },
5795   { TYPE_STRING,        &graphics_set_name,     ""                      },
5796   { TYPE_ENTER_LIST,    execSetupChooseSounds,  "Custom Sounds:"        },
5797   { TYPE_STRING,        &sounds_set_name,       ""                      },
5798   { TYPE_ENTER_LIST,    execSetupChooseMusic,   "Custom Music:"         },
5799   { TYPE_STRING,        &music_set_name,        ""                      },
5800   { TYPE_EMPTY,         NULL,                   ""                      },
5801   { TYPE_YES_NO_AUTO,&setup.override_level_graphics,"Override Level Graphics:"},
5802   { TYPE_YES_NO_AUTO,&setup.override_level_sounds,  "Override Level Sounds:"  },
5803   { TYPE_YES_NO_AUTO,&setup.override_level_music,   "Override Level Music:"   },
5804   { TYPE_EMPTY,         NULL,                   ""                      },
5805   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5806
5807   { 0,                  NULL,                   NULL                    }
5808 };
5809
5810 static struct TokenInfo setup_info_input[] =
5811 {
5812   { TYPE_SWITCH,        NULL,                   "Player:"               },
5813   { TYPE_SWITCH,        NULL,                   "Device:"               },
5814   { TYPE_SWITCH,        NULL,                   ""                      },
5815   { TYPE_SKIPPABLE,     NULL,                   ""                      },
5816   { TYPE_EMPTY,         NULL,                   ""                      },
5817   { TYPE_EMPTY,         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_SKIPPABLE,     NULL,                   ""                      },
5825   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5826
5827   { 0,                  NULL,                   NULL                    }
5828 };
5829
5830 static struct TokenInfo setup_info_touch[] =
5831 {
5832   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
5833   { TYPE_STRING,        &touch_controls_text,   ""                      },
5834   { TYPE_EMPTY,         NULL,                   ""                      },
5835   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5836
5837   { 0,                  NULL,                   NULL                    }
5838 };
5839
5840 static struct TokenInfo setup_info_touch_virtual_buttons[] =
5841 {
5842   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
5843   { TYPE_STRING,        &touch_controls_text,   ""                      },
5844   { TYPE_EMPTY,         NULL,                   ""                      },
5845   { TYPE_ENTER_LIST,    execSetupChooseTransparency, "Transparency:"    },
5846   { TYPE_STRING,        &transparency_text,     ""                      },
5847   { TYPE_EMPTY,         NULL,                   ""                      },
5848   { TYPE_ENTER_LIST,    execSetupConfigureVirtualButtons, "Configure Virtual Buttons" },
5849   { TYPE_EMPTY,         NULL,                   ""                      },
5850   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5851
5852   { 0,                  NULL,                   NULL                    }
5853 };
5854
5855 static struct TokenInfo setup_info_touch_wipe_gestures[] =
5856 {
5857   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
5858   { TYPE_STRING,        &touch_controls_text,   ""                      },
5859   { TYPE_EMPTY,         NULL,                   ""                      },
5860   { TYPE_ENTER_LIST,    execSetupChooseMoveDistance, "Move Trigger Distance:" },
5861   { TYPE_STRING,        &move_distance_text,    ""                      },
5862   { TYPE_ENTER_LIST,    execSetupChooseDropDistance, "Drop Trigger Distance:" },
5863   { TYPE_STRING,        &drop_distance_text,    ""                      },
5864   { TYPE_EMPTY,         NULL,                   ""                      },
5865   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5866
5867   { 0,                  NULL,                   NULL                    }
5868 };
5869
5870 static struct TokenInfo setup_info_shortcuts[] =
5871 {
5872   { TYPE_ENTER_MENU,    execSetupShortcuts1,    "Various Keys"          },
5873   { TYPE_ENTER_MENU,    execSetupShortcuts2,    "Player Focus"          },
5874   { TYPE_ENTER_MENU,    execSetupShortcuts3,    "Tape Buttons"          },
5875   { TYPE_ENTER_MENU,    execSetupShortcuts4,    "Sound & Music"         },
5876   { TYPE_ENTER_MENU,    execSetupShortcuts5,    "TAS Snap Keys"         },
5877   { TYPE_EMPTY,         NULL,                   ""                      },
5878   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5879
5880   { 0,                  NULL,                   NULL                    }
5881 };
5882
5883 static struct TokenInfo setup_info_shortcuts_1[] =
5884 {
5885   { TYPE_KEYTEXT,       NULL,           "Quick Save Game to Tape:",     },
5886   { TYPE_KEY,           &setup.shortcut.save_game, ""                   },
5887   { TYPE_KEYTEXT,       NULL,           "Quick Load Game from Tape:",   },
5888   { TYPE_KEY,           &setup.shortcut.load_game, ""                   },
5889   { TYPE_KEYTEXT,       NULL,           "Start Game & Toggle Pause:",   },
5890   { TYPE_KEY,           &setup.shortcut.toggle_pause, ""                },
5891   { TYPE_EMPTY,         NULL,                   ""                      },
5892   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on 'Esc' Key:"     },
5893   { TYPE_YES_NO, &setup.ask_on_escape_editor,   "Ask on 'Esc' Key (Editor):" },
5894   { TYPE_EMPTY,         NULL,                   ""                      },
5895   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
5896
5897   { 0,                  NULL,                   NULL                    }
5898 };
5899
5900 static struct TokenInfo setup_info_shortcuts_2[] =
5901 {
5902   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 1:",       },
5903   { TYPE_KEY,           &setup.shortcut.focus_player[0], ""             },
5904   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 2:",       },
5905   { TYPE_KEY,           &setup.shortcut.focus_player[1], ""             },
5906   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 3:",       },
5907   { TYPE_KEY,           &setup.shortcut.focus_player[2], ""             },
5908   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 4:",       },
5909   { TYPE_KEY,           &setup.shortcut.focus_player[3], ""             },
5910   { TYPE_KEYTEXT,       NULL,           "Set Focus to All Players:",    },
5911   { TYPE_KEY,           &setup.shortcut.focus_player_all, ""            },
5912   { TYPE_EMPTY,         NULL,                   ""                      },
5913   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
5914
5915   { 0,                  NULL,                   NULL                    }
5916 };
5917
5918 static struct TokenInfo setup_info_shortcuts_3[] =
5919 {
5920   { TYPE_KEYTEXT,       NULL,                   "Eject Tape:",          },
5921   { TYPE_KEY,           &setup.shortcut.tape_eject, ""                  },
5922   { TYPE_KEYTEXT,       NULL,                   "Warp / Single Step:",  },
5923   { TYPE_KEY,           &setup.shortcut.tape_extra, ""                  },
5924   { TYPE_KEYTEXT,       NULL,                   "Stop Tape:",           },
5925   { TYPE_KEY,           &setup.shortcut.tape_stop, ""                   },
5926   { TYPE_KEYTEXT,       NULL,                   "Pause / Unpause Tape:",},
5927   { TYPE_KEY,           &setup.shortcut.tape_pause, ""                  },
5928   { TYPE_KEYTEXT,       NULL,                   "Record Tape:",         },
5929   { TYPE_KEY,           &setup.shortcut.tape_record, ""                 },
5930   { TYPE_KEYTEXT,       NULL,                   "Play Tape:",           },
5931   { TYPE_KEY,           &setup.shortcut.tape_play, ""                   },
5932   { TYPE_EMPTY,         NULL,                   ""                      },
5933   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
5934
5935   { 0,                  NULL,                   NULL                    }
5936 };
5937
5938 static struct TokenInfo setup_info_shortcuts_4[] =
5939 {
5940   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Normal):", },
5941   { TYPE_KEY,           &setup.shortcut.sound_simple, ""                },
5942   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Looping):", },
5943   { TYPE_KEY,           &setup.shortcut.sound_loops, ""                 },
5944   { TYPE_KEYTEXT,       NULL,           "Toggle Music:",                },
5945   { TYPE_KEY,           &setup.shortcut.sound_music, ""                 },
5946   { TYPE_EMPTY,         NULL,                   ""                      },
5947   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
5948
5949   { 0,                  NULL,                   NULL                    }
5950 };
5951
5952 static struct TokenInfo setup_info_shortcuts_5[] =
5953 {
5954   { TYPE_KEYTEXT,       NULL,                   "Snap Left:",           },
5955   { TYPE_KEY,           &setup.shortcut.snap_left, ""                   },
5956   { TYPE_KEYTEXT,       NULL,                   "Snap Right:",          },
5957   { TYPE_KEY,           &setup.shortcut.snap_right, ""                  },
5958   { TYPE_KEYTEXT,       NULL,                   "Snap Up:",             },
5959   { TYPE_KEY,           &setup.shortcut.snap_up, ""                     },
5960   { TYPE_KEYTEXT,       NULL,                   "Snap Down:",           },
5961   { TYPE_KEY,           &setup.shortcut.snap_down, ""                   },
5962   { TYPE_EMPTY,         NULL,                   ""                      },
5963   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
5964
5965   { 0,                  NULL,                   NULL                    }
5966 };
5967
5968 static Key getSetupKey()
5969 {
5970   Key key = KSYM_UNDEFINED;
5971   boolean got_key_event = FALSE;
5972
5973   while (!got_key_event)
5974   {
5975     Event event;
5976
5977     if (NextValidEvent(&event))
5978     {
5979       switch (event.type)
5980       {
5981         case EVENT_KEYPRESS:
5982           {
5983             key = GetEventKey((KeyEvent *)&event, TRUE);
5984
5985             /* press 'Escape' or 'Enter' to keep the existing key binding */
5986             if (key == KSYM_Escape || key == KSYM_Return)
5987               key = KSYM_UNDEFINED;     /* keep old value */
5988
5989             got_key_event = TRUE;
5990           }
5991           break;
5992
5993         case EVENT_KEYRELEASE:
5994           key_joystick_mapping = 0;
5995           break;
5996
5997         default:
5998           HandleOtherEvents(&event);
5999           break;
6000       }
6001     }
6002
6003     BackToFront();
6004   }
6005
6006   return key;
6007 }
6008
6009 static int getSetupValueFont(int type, void *value)
6010 {
6011   if (type & TYPE_GHOSTED)
6012     return FONT_OPTION_OFF;
6013   else if (type & TYPE_KEY)
6014     return (type & TYPE_QUERY ? FONT_INPUT_1_ACTIVE : FONT_VALUE_1);
6015   else if (type & TYPE_STRING)
6016     return FONT_VALUE_2;
6017   else if (type & TYPE_ECS_AGA)
6018     return FONT_VALUE_1;
6019   else if (type & TYPE_BOOLEAN_STYLE)
6020     return (*(boolean *)value ? FONT_OPTION_ON : FONT_OPTION_OFF);
6021   else if (type & TYPE_YES_NO_AUTO)
6022     return (*(int *)value == AUTO  ? FONT_OPTION_ON :
6023             *(int *)value == FALSE ? FONT_OPTION_OFF : FONT_OPTION_ON);
6024   else
6025     return FONT_VALUE_1;
6026 }
6027
6028 static int getSetupValueFontNarrow(int type, int font_nr)
6029 {
6030   return (font_nr == FONT_VALUE_1    ? FONT_VALUE_NARROW :
6031           font_nr == FONT_OPTION_ON  ? FONT_OPTION_ON_NARROW :
6032           font_nr == FONT_OPTION_OFF ? FONT_OPTION_OFF_NARROW :
6033           font_nr);
6034 }
6035
6036 static void drawSetupValue(int screen_pos, int setup_info_pos_raw)
6037 {
6038   int si_pos = (setup_info_pos_raw < 0 ? screen_pos : setup_info_pos_raw);
6039   struct TokenInfo *si = &setup_info[si_pos];
6040   boolean font_draw_xoffset_modified = FALSE;
6041   boolean scrollbar_needed = (num_setup_info < max_setup_info);
6042   int font_draw_xoffset_old = -1;
6043   int xoffset = (scrollbar_needed ? -1 : 0);
6044   int menu_screen_value_xpos = MENU_SCREEN_VALUE_XPOS + xoffset;
6045   int menu_screen_max_xpos = MENU_SCREEN_MAX_XPOS + xoffset;
6046   int xpos = menu_screen_value_xpos;
6047   int ypos = MENU_SCREEN_START_YPOS + screen_pos;
6048   int startx = mSX + xpos * 32;
6049   int starty = mSY + ypos * 32;
6050   int font_nr, font_nr_default, font_width_default;
6051   int type = si->type;
6052   void *value = si->value;
6053   char *value_string = getSetupValue(type, value);
6054   int i;
6055
6056   if (value_string == NULL)
6057     return;
6058
6059   if (type & TYPE_KEY)
6060   {
6061     xpos = MENU_SCREEN_START_XPOS;
6062
6063     if (type & TYPE_QUERY)
6064       value_string = "<press key>";
6065   }
6066   else if (type & TYPE_STRING)
6067   {
6068     int max_value_len = (SCR_FIELDX - 2) * 2;
6069
6070     xpos = MENU_SCREEN_START_XPOS;
6071
6072     if (strlen(value_string) > max_value_len)
6073       value_string[max_value_len] = '\0';
6074   }
6075   else if (type & TYPE_YES_NO_AUTO)
6076   {
6077     xpos = menu_screen_value_xpos - 1;
6078   }
6079
6080   startx = mSX + xpos * 32;
6081   starty = mSY + ypos * 32;
6082   font_nr_default = getSetupValueFont(type, value);
6083   font_width_default = getFontWidth(font_nr_default);
6084
6085   font_nr = font_nr_default;
6086
6087   // special check if right-side setup values moved left due to scrollbar
6088   if (scrollbar_needed && xpos > MENU_SCREEN_START_XPOS)
6089   {
6090     int max_menu_text_length = 26;      // maximum text length for classic menu
6091     int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
6092     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
6093     int text_font_nr = getMenuTextFont(FONT_MENU_2);
6094     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
6095     int text_width = max_menu_text_length * getFontWidth(text_font_nr);
6096
6097     if (startx + font_xoffset < text_startx + text_width + text_font_xoffset)
6098     {
6099       xpos += 1;
6100       startx = mSX + xpos * 32;
6101
6102       font_nr = getSetupValueFontNarrow(type, font_nr);
6103     }
6104   }
6105
6106   /* downward compatibility correction for Juergen Bonhagen's menu settings */
6107   if (setup_mode != SETUP_MODE_INPUT)
6108   {
6109     int max_menu_text_length_big = (menu_screen_value_xpos -
6110                                     MENU_SCREEN_START_XPOS);
6111     int max_menu_text_length_medium = max_menu_text_length_big * 2;
6112     int check_font_nr = FONT_OPTION_ON; /* known font that needs correction */
6113     int font1_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
6114     int font2_xoffset = getFontBitmapInfo(check_font_nr)->draw_xoffset;
6115     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
6116     int text_font_nr = getMenuTextFont(FONT_MENU_2);
6117     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
6118     int text_width = max_menu_text_length_medium * getFontWidth(text_font_nr);
6119     boolean correct_font_draw_xoffset = FALSE;
6120
6121     if (xpos == MENU_SCREEN_START_XPOS &&
6122         startx + font1_xoffset < text_startx + text_font_xoffset)
6123       correct_font_draw_xoffset = TRUE;
6124
6125     if (xpos == menu_screen_value_xpos &&
6126         startx + font2_xoffset < text_startx + text_width + text_font_xoffset)
6127       correct_font_draw_xoffset = TRUE;
6128
6129     /* check if setup value would overlap with setup text when printed */
6130     /* (this can happen for extreme/wrong values for font draw offset) */
6131     if (correct_font_draw_xoffset)
6132     {
6133       font_draw_xoffset_old = getFontBitmapInfo(font_nr)->draw_xoffset;
6134       font_draw_xoffset_modified = TRUE;
6135
6136       if (type & TYPE_KEY)
6137         getFontBitmapInfo(font_nr)->draw_xoffset += 2 * getFontWidth(font_nr);
6138       else if (!(type & TYPE_STRING))
6139         getFontBitmapInfo(font_nr)->draw_xoffset = text_font_xoffset + 20 -
6140           max_menu_text_length_medium * (16 - getFontWidth(text_font_nr));
6141     }
6142   }
6143
6144   for (i = 0; i <= menu_screen_max_xpos - xpos; i++)
6145     DrawText(startx + i * font_width_default, starty, " ", font_nr_default);
6146
6147   DrawText(startx, starty, value_string, font_nr);
6148
6149   if (font_draw_xoffset_modified)
6150     getFontBitmapInfo(font_nr)->draw_xoffset = font_draw_xoffset_old;
6151 }
6152
6153 static void changeSetupValue(int screen_pos, int setup_info_pos_raw, int dx)
6154 {
6155   int si_pos = (setup_info_pos_raw < 0 ? screen_pos : setup_info_pos_raw);
6156   struct TokenInfo *si = &setup_info[si_pos];
6157
6158   if (si->type & TYPE_BOOLEAN_STYLE)
6159   {
6160     *(boolean *)si->value ^= TRUE;
6161   }
6162   else if (si->type & TYPE_YES_NO_AUTO)
6163   {
6164     *(int *)si->value =
6165       (dx == -1 ?
6166        (*(int *)si->value == AUTO ? TRUE :
6167         *(int *)si->value == TRUE ? FALSE : AUTO) :
6168        (*(int *)si->value == TRUE ? AUTO :
6169         *(int *)si->value == AUTO ? FALSE : TRUE));
6170   }
6171   else if (si->type & TYPE_KEY)
6172   {
6173     Key key;
6174
6175     si->type |= TYPE_QUERY;
6176     drawSetupValue(screen_pos, setup_info_pos_raw);
6177     si->type &= ~TYPE_QUERY;
6178
6179     key = getSetupKey();
6180     if (key != KSYM_UNDEFINED)
6181       *(Key *)si->value = key;
6182   }
6183
6184   drawSetupValue(screen_pos, setup_info_pos_raw);
6185
6186   // fullscreen state may have changed at this point
6187   if (si->value == &setup.fullscreen)
6188     ToggleFullscreenOrChangeWindowScalingIfNeeded();
6189 }
6190
6191 static struct TokenInfo *getSetupInfoFinal(struct TokenInfo *setup_info_orig)
6192 {
6193   static struct TokenInfo *setup_info_final = NULL;
6194   int list_size = 0;
6195   int list_pos = 0;
6196   int i;
6197
6198   /* determine maximum list size of target list */
6199   while (setup_info_orig[list_size++].type != 0);
6200
6201   /* free, allocate and clear memory for target list */
6202   checked_free(setup_info_final);
6203   setup_info_final = checked_calloc(list_size * sizeof(struct TokenInfo));
6204
6205   /* copy setup info list without setup entries marked as hidden */
6206   for (i = 0; setup_info_orig[i].type != 0; i++)
6207   {
6208     /* skip setup entries configured to be hidden */
6209     if (hideSetupEntry(setup_info_orig[i].value))
6210       continue;
6211
6212     /* skip skippable setup entries if screen is lower than usual */
6213     if (SCR_FIELDY < SCR_FIELDY_DEFAULT &&
6214         setup_info_orig[i].type == TYPE_SKIPPABLE)
6215       continue;
6216
6217     setup_info_final[list_pos++] = setup_info_orig[i];
6218   }
6219
6220   return setup_info_final;
6221 }
6222
6223 static void DrawSetupScreen_Generic()
6224 {
6225   int fade_mask = REDRAW_FIELD;
6226   boolean redraw_all = FALSE;
6227   char *title_string = NULL;
6228   int i;
6229
6230   if (CheckIfGlobalBorderOrPlayfieldViewportHasChanged())
6231     fade_mask = REDRAW_ALL;
6232
6233   UnmapAllGadgets();
6234   FadeMenuSoundsAndMusic();
6235
6236   FreeScreenGadgets();
6237   CreateScreenGadgets();
6238
6239   if (redraw_mask & REDRAW_ALL)
6240     redraw_all = TRUE;
6241
6242   FadeOut(fade_mask);
6243
6244   /* needed if different viewport properties defined for setup screen */
6245   ChangeViewportPropertiesIfNeeded();
6246
6247   SetMainBackgroundImage(IMG_BACKGROUND_SETUP);
6248
6249   ClearField();
6250
6251   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
6252
6253   if (setup_mode == SETUP_MODE_MAIN)
6254   {
6255     setup_info = setup_info_main;
6256     title_string = STR_SETUP_MAIN;
6257   }
6258   else if (setup_mode == SETUP_MODE_GAME)
6259   {
6260     setup_info = setup_info_game;
6261     title_string = STR_SETUP_GAME;
6262   }
6263   else if (setup_mode == SETUP_MODE_EDITOR)
6264   {
6265     setup_info = setup_info_editor;
6266     title_string = STR_SETUP_EDITOR;
6267   }
6268   else if (setup_mode == SETUP_MODE_GRAPHICS)
6269   {
6270     setup_info = setup_info_graphics;
6271     title_string = STR_SETUP_GRAPHICS;
6272   }
6273   else if (setup_mode == SETUP_MODE_SOUND)
6274   {
6275     setup_info = setup_info_sound;
6276     title_string = STR_SETUP_SOUND;
6277   }
6278   else if (setup_mode == SETUP_MODE_ARTWORK)
6279   {
6280     setup_info = setup_info_artwork;
6281     title_string = STR_SETUP_ARTWORK;
6282   }
6283   else if (setup_mode == SETUP_MODE_TOUCH)
6284   {
6285     setup_info = setup_info_touch;
6286     title_string = STR_SETUP_TOUCH;
6287
6288     if (strEqual(setup.touch.control_type, TOUCH_CONTROL_VIRTUAL_BUTTONS))
6289       setup_info = setup_info_touch_virtual_buttons;
6290     else if (strEqual(setup.touch.control_type, TOUCH_CONTROL_WIPE_GESTURES))
6291       setup_info = setup_info_touch_wipe_gestures;
6292   }
6293   else if (setup_mode == SETUP_MODE_SHORTCUTS)
6294   {
6295     setup_info = setup_info_shortcuts;
6296     title_string = STR_SETUP_SHORTCUTS;
6297   }
6298   else if (setup_mode == SETUP_MODE_SHORTCUTS_1)
6299   {
6300     setup_info = setup_info_shortcuts_1;
6301     title_string = STR_SETUP_SHORTCUTS;
6302   }
6303   else if (setup_mode == SETUP_MODE_SHORTCUTS_2)
6304   {
6305     setup_info = setup_info_shortcuts_2;
6306     title_string = STR_SETUP_SHORTCUTS;
6307   }
6308   else if (setup_mode == SETUP_MODE_SHORTCUTS_3)
6309   {
6310     setup_info = setup_info_shortcuts_3;
6311     title_string = STR_SETUP_SHORTCUTS;
6312   }
6313   else if (setup_mode == SETUP_MODE_SHORTCUTS_4)
6314   {
6315     setup_info = setup_info_shortcuts_4;
6316     title_string = STR_SETUP_SHORTCUTS;
6317   }
6318   else if (setup_mode == SETUP_MODE_SHORTCUTS_5)
6319   {
6320     setup_info = setup_info_shortcuts_5;
6321     title_string = STR_SETUP_SHORTCUTS;
6322   }
6323
6324   /* use modified setup info without setup entries marked as hidden */
6325   setup_info = getSetupInfoFinal(setup_info);
6326
6327   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, title_string);
6328
6329   // determine maximal number of setup entries that can be displayed on screen
6330   num_setup_info = 0;
6331   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
6332     num_setup_info++;
6333
6334   // determine maximal number of setup entries available for this setup screen
6335   max_setup_info = 0;
6336   for (i = 0; setup_info[i].type != 0; i++)
6337     max_setup_info++;
6338
6339   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
6340
6341   MapScreenGadgets(max_setup_info);
6342
6343   if (redraw_all)
6344     redraw_mask = fade_mask = REDRAW_ALL;
6345
6346   DrawMaskedBorder(fade_mask);
6347
6348   FadeIn(fade_mask);
6349 }
6350
6351 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
6352 {
6353   menu_info = setup_info;
6354
6355   HandleMenuScreen(mx, my, dx, dy, button,
6356                    setup_mode, num_setup_info, max_setup_info);
6357 }
6358
6359 void DrawSetupScreen_Input()
6360 {
6361   int i;
6362
6363   FadeOut(REDRAW_FIELD);
6364
6365   ClearField();
6366
6367   setup_info = getSetupInfoFinal(setup_info_input);
6368
6369   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, STR_SETUP_INPUT);
6370
6371   for (i = 0; setup_info[i].type != 0; i++)
6372   {
6373     if (setup_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
6374       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
6375     else if (setup_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
6376       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
6377     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
6378       initCursor(i, IMG_MENU_BUTTON);
6379
6380     DrawCursorAndText_Setup(i, -1, FALSE);
6381   }
6382
6383   /* create gadgets for setup input menu screen */
6384   FreeScreenGadgets();
6385   CreateScreenGadgets();
6386
6387   /* map gadgets for setup input menu screen */
6388   MapScreenMenuGadgets(SCREEN_MASK_INPUT);
6389
6390   HandleSetupScreen_Input(0, 0, 0, 0, MB_MENU_INITIALIZE);
6391
6392   FadeIn(REDRAW_FIELD);
6393 }
6394
6395 static void setJoystickDeviceToNr(char *device_name, int device_nr)
6396 {
6397   if (device_name == NULL)
6398     return;
6399
6400   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
6401     device_nr = 0;
6402
6403   if (strlen(device_name) > 1)
6404   {
6405     char c1 = device_name[strlen(device_name) - 1];
6406     char c2 = device_name[strlen(device_name) - 2];
6407
6408     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
6409       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
6410   }
6411   else
6412     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
6413             strlen(device_name));
6414 }
6415
6416 static void drawPlayerSetupInputInfo(int player_nr, boolean active)
6417 {
6418   int i;
6419   static struct SetupKeyboardInfo custom_key;
6420   static struct
6421   {
6422     Key *key;
6423     char *text;
6424   } custom[] =
6425   {
6426     { &custom_key.left,  "Axis/Pad Left"  },
6427     { &custom_key.right, "Axis/Pad Right" },
6428     { &custom_key.up,    "Axis/Pad Up"    },
6429     { &custom_key.down,  "Axis/Pad Down"  },
6430     { &custom_key.snap,  "Button 1/A/X"   },
6431     { &custom_key.drop,  "Button 2/B/Y"   }
6432   };
6433   static char *joystick_name[MAX_PLAYERS] =
6434   {
6435     "Joystick1",
6436     "Joystick2",
6437     "Joystick3",
6438     "Joystick4"
6439   };
6440   int font_nr_menu = (active ? FONT_MENU_1_ACTIVE : FONT_MENU_1);
6441   int font_nr_info = FONT_MENU_1;
6442   int font_nr_name = FONT_VALUE_OLD;
6443   int font_nr_on   = FONT_VALUE_1;
6444   int font_nr_off  = FONT_VALUE_OLD;
6445   int pos = 4;
6446
6447   if (SCR_FIELDX < SCR_FIELDX_DEFAULT)
6448   {
6449     font_nr_info = FONT_MENU_2;
6450     font_nr_on   = FONT_VALUE_NARROW;
6451     font_nr_off  = FONT_VALUE_OLD_NARROW;
6452   }
6453
6454   custom_key = setup.input[player_nr].key;
6455
6456   DrawText(mSX + 11 * 32, mSY + 2 * 32, int2str(player_nr + 1, 1),
6457            FONT_INPUT_1_ACTIVE);
6458
6459   ClearRectangleOnBackground(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
6460                              TILEX, TILEY);
6461   DrawFixedGraphicThruMaskExt(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
6462                               PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
6463
6464   if (setup.input[player_nr].use_joystick)
6465   {
6466     char *device_name = setup.input[player_nr].joy.device_name;
6467     int joystick_nr = getJoystickNrFromDeviceName(device_name);
6468     boolean joystick_active = CheckJoystickOpened(joystick_nr);
6469     char *text = joystick_name[joystick_nr];
6470     int font_nr = (joystick_active ? font_nr_on : font_nr_off);
6471
6472     DrawText(mSX + 8 * 32, mSY + 3 * 32, text, font_nr);
6473     DrawText(mSX + 32, mSY + 4 * 32, "Configure", font_nr_menu);
6474   }
6475   else
6476   {
6477     DrawText(mSX + 8 * 32, mSY + 3 * 32, "Keyboard ", font_nr_on);
6478     DrawText(mSX + 1 * 32, mSY + 4 * 32, "Customize", font_nr_menu);
6479   }
6480
6481   if (SCR_FIELDY >= SCR_FIELDY_DEFAULT)
6482     DrawText(mSX + 32, mSY + 5 * 32, "Actual Settings:", font_nr_info);
6483   else
6484     pos = 3;
6485
6486   drawCursorXY(1, pos + 0, IMG_MENU_BUTTON_LEFT);
6487   drawCursorXY(1, pos + 1, IMG_MENU_BUTTON_RIGHT);
6488   drawCursorXY(1, pos + 2, IMG_MENU_BUTTON_UP);
6489   drawCursorXY(1, pos + 3, IMG_MENU_BUTTON_DOWN);
6490
6491   DrawText(mSX + 2 * 32, mSY + (pos + 2) * 32, ":", font_nr_name);
6492   DrawText(mSX + 2 * 32, mSY + (pos + 3) * 32, ":", font_nr_name);
6493   DrawText(mSX + 2 * 32, mSY + (pos + 4) * 32, ":", font_nr_name);
6494   DrawText(mSX + 2 * 32, mSY + (pos + 5) * 32, ":", font_nr_name);
6495   DrawText(mSX + 1 * 32, mSY + (pos + 6) * 32, "Snap Field:", font_nr_name);
6496   DrawText(mSX + 1 * 32, mSY + (pos + 8) * 32, "Drop Element:", font_nr_name);
6497
6498   for (i = 0; i < 6; i++)
6499   {
6500     int ypos = (pos + 2) + i + (i > 3 ? i - 3 : 0);
6501
6502     DrawText(mSX + 3 * 32, mSY + ypos * 32,
6503              "              ", font_nr_on);
6504     DrawText(mSX + 3 * 32, mSY + ypos * 32,
6505              (setup.input[player_nr].use_joystick ?
6506               custom[i].text :
6507               getKeyNameFromKey(*custom[i].key)), font_nr_on);
6508   }
6509 }
6510
6511 static int input_player_nr = 0;
6512
6513 void HandleSetupScreen_Input_Player(int step, int direction)
6514 {
6515   int old_player_nr = input_player_nr;
6516   int new_player_nr;
6517
6518   new_player_nr = old_player_nr + step * direction;
6519   if (new_player_nr < 0)
6520     new_player_nr = 0;
6521   if (new_player_nr > MAX_PLAYERS - 1)
6522     new_player_nr = MAX_PLAYERS - 1;
6523
6524   if (new_player_nr != old_player_nr)
6525   {
6526     input_player_nr = new_player_nr;
6527
6528     drawPlayerSetupInputInfo(input_player_nr, FALSE);
6529   }
6530 }
6531
6532 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
6533 {
6534   static int choice = 0;
6535   int x = 0;
6536   int y = choice;
6537   int pos_start  = SETUPINPUT_SCREEN_POS_START;
6538   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
6539   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
6540   int pos_end    = SETUPINPUT_SCREEN_POS_END;
6541
6542   if (SCR_FIELDY < SCR_FIELDY_DEFAULT)
6543   {
6544     int i;
6545
6546     for (i = 0; setup_info_input[i].type != 0; i++)
6547     {
6548       /* adjust menu structure according to skipped setup entries */
6549       if (setup_info_input[i].type == TYPE_SKIPPABLE)
6550       {
6551         pos_empty2--;
6552         pos_end--;
6553       }
6554     }
6555   }
6556
6557   if (button == MB_MENU_INITIALIZE)
6558   {
6559     /* input setup menu may have changed size due to graphics configuration */
6560     if (choice >= pos_empty1)
6561       choice = pos_end;
6562
6563     drawPlayerSetupInputInfo(input_player_nr, (choice == 2));
6564
6565     DrawCursorAndText_Setup(choice, -1, TRUE);
6566
6567     return;
6568   }
6569   else if (button == MB_MENU_LEAVE)
6570   {
6571     setup_mode = SETUP_MODE_MAIN;
6572     DrawSetupScreen();
6573     InitJoysticks();
6574
6575     return;
6576   }
6577
6578   if (mx || my)         /* mouse input */
6579   {
6580     x = (mx - mSX) / 32;
6581     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
6582   }
6583   else if (dx || dy)    /* keyboard input */
6584   {
6585     if (dx && choice == 0)
6586       x = (dx < 0 ? 10 : 12);
6587     else if ((dx && choice == 1) ||
6588              (dx == -1 && choice == pos_end))
6589       button = MB_MENU_CHOICE;
6590     else if (dy)
6591       y = choice + dy;
6592
6593     if (y >= pos_empty1 && y <= pos_empty2)
6594       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
6595   }
6596
6597   if (y == 0 && dx != 0 && button)
6598   {
6599     HandleSetupScreen_Input_Player(1, dx < 0 ? -1 : +1);
6600   }
6601   else if (IN_VIS_FIELD(x, y) &&        // (does not use "IN_VIS_MENU()" yet)
6602            y >= pos_start && y <= pos_end &&
6603            !(y >= pos_empty1 && y <= pos_empty2))
6604   {
6605     if (button)
6606     {
6607       if (y != choice)
6608       {
6609         DrawCursorAndText_Setup(choice, -1, FALSE);
6610         DrawCursorAndText_Setup(y, -1, TRUE);
6611
6612         drawPlayerSetupInputInfo(input_player_nr, (y == 2));
6613
6614         choice = y;
6615       }
6616     }
6617     else
6618     {
6619       if (y == 1)
6620       {
6621         char *device_name = setup.input[input_player_nr].joy.device_name;
6622
6623         if (!setup.input[input_player_nr].use_joystick)
6624         {
6625           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
6626
6627           setJoystickDeviceToNr(device_name, new_device_nr);
6628           setup.input[input_player_nr].use_joystick = TRUE;
6629         }
6630         else
6631         {
6632           int device_nr = getJoystickNrFromDeviceName(device_name);
6633           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
6634
6635           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
6636             setup.input[input_player_nr].use_joystick = FALSE;
6637           else
6638             setJoystickDeviceToNr(device_name, new_device_nr);
6639         }
6640
6641         drawPlayerSetupInputInfo(input_player_nr, FALSE);
6642       }
6643       else if (y == 2)
6644       {
6645         if (setup.input[input_player_nr].use_joystick)
6646           ConfigureJoystick(input_player_nr);
6647         else
6648           CustomizeKeyboard(input_player_nr);
6649       }
6650       else if (y == pos_end)
6651       {
6652         InitJoysticks();
6653
6654         FadeSetLeaveMenu();
6655
6656         setup_mode = SETUP_MODE_MAIN;
6657         DrawSetupScreen();
6658       }
6659     }
6660   }
6661 }
6662
6663 static boolean CustomizeKeyboardMain(int player_nr)
6664 {
6665   int i;
6666   int step_nr;
6667   boolean finished = FALSE;
6668   static struct SetupKeyboardInfo custom_key;
6669   static struct
6670   {
6671     Key *key;
6672     char *text;
6673   } customize_step[] =
6674   {
6675     { &custom_key.left,  "Move Left"    },
6676     { &custom_key.right, "Move Right"   },
6677     { &custom_key.up,    "Move Up"      },
6678     { &custom_key.down,  "Move Down"    },
6679     { &custom_key.snap,  "Snap Field"   },
6680     { &custom_key.drop,  "Drop Element" }
6681   };
6682   int font_nr_old = FONT_VALUE_OLD;
6683   int font_nr_new = FONT_VALUE_1;
6684   boolean success = FALSE;
6685
6686   if (SCR_FIELDX < SCR_FIELDX_DEFAULT)
6687   {
6688     font_nr_old = FONT_VALUE_OLD_NARROW;
6689     font_nr_new = FONT_VALUE_NARROW;
6690   }
6691
6692   /* read existing key bindings from player setup */
6693   custom_key = setup.input[player_nr].key;
6694
6695   FadeSetEnterMenu();
6696   FadeOut(REDRAW_FIELD);
6697
6698   ClearField();
6699
6700   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Keyboard Input");
6701
6702   step_nr = 0;
6703   DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
6704            customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
6705   DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
6706            "Key:", FONT_INPUT_1_ACTIVE);
6707   DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
6708            getKeyNameFromKey(*customize_step[step_nr].key), font_nr_old);
6709
6710   FadeIn(REDRAW_FIELD);
6711
6712   while (!finished)
6713   {
6714     Event event;
6715
6716     if (NextValidEvent(&event))
6717     {
6718       switch (event.type)
6719       {
6720         case EVENT_KEYPRESS:
6721           {
6722             Key key = GetEventKey((KeyEvent *)&event, FALSE);
6723
6724             /* press 'Escape' to abort and keep the old key bindings */
6725             if (key == KSYM_Escape)
6726             {
6727               FadeSkipNextFadeIn();
6728
6729               finished = TRUE;
6730
6731               break;
6732             }
6733
6734             /* press 'Enter' to keep the existing key binding */
6735             if (key == KSYM_Return)
6736               key = *customize_step[step_nr].key;
6737
6738             /* check if key already used */
6739             for (i = 0; i < step_nr; i++)
6740               if (*customize_step[i].key == key)
6741                 break;
6742             if (i < step_nr)
6743               break;
6744
6745             /* got new key binding */
6746             *customize_step[step_nr].key = key;
6747             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
6748                      "             ", font_nr_new);
6749             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
6750                      getKeyNameFromKey(key), font_nr_new);
6751             step_nr++;
6752
6753             /* un-highlight last query */
6754             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1)) * 32,
6755                      customize_step[step_nr - 1].text, FONT_MENU_1);
6756             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1) + 1) * 32,
6757                      "Key:", FONT_MENU_1);
6758
6759             /* all keys configured */
6760             if (step_nr == 6)
6761             {
6762               finished = TRUE;
6763               success = TRUE;
6764
6765               break;
6766             }
6767
6768             /* query next key binding */
6769             DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
6770                      customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
6771             DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
6772                      "Key:", FONT_INPUT_1_ACTIVE);
6773             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
6774                      getKeyNameFromKey(*customize_step[step_nr].key),
6775                      font_nr_old);
6776           }
6777           break;
6778
6779         case EVENT_KEYRELEASE:
6780           key_joystick_mapping = 0;
6781           break;
6782
6783         default:
6784           HandleOtherEvents(&event);
6785           break;
6786       }
6787     }
6788
6789     BackToFront();
6790   }
6791
6792   /* write new key bindings back to player setup, if successfully finished */
6793   if (success)
6794     setup.input[player_nr].key = custom_key;
6795
6796   return success;
6797 }
6798
6799 void CustomizeKeyboard(int player_nr)
6800 {
6801   boolean success = CustomizeKeyboardMain(player_nr);
6802
6803   if (success)
6804   {
6805     int font_nr = FONT_TITLE_1;
6806     int font_height = getFontHeight(font_nr);
6807     int ypos1 = SYSIZE / 2 - font_height * 2;
6808     int ypos2 = SYSIZE / 2 - font_height * 1;
6809     unsigned int wait_frame_delay = 0;
6810     unsigned int wait_frame_delay_value = 2000;
6811
6812     ResetDelayCounter(&wait_frame_delay);
6813
6814     ClearField();
6815
6816     DrawTextSCentered(ypos1, font_nr, "Keyboard");
6817     DrawTextSCentered(ypos2, font_nr, "configured!");
6818
6819     while (!DelayReached(&wait_frame_delay, wait_frame_delay_value))
6820       BackToFront();
6821
6822     ClearEventQueue();
6823   }
6824
6825   DrawSetupScreen_Input();
6826 }
6827
6828 /* game controller mapping generator by Gabriel Jacobo <gabomdq@gmail.com> */
6829
6830 #define MARKER_BUTTON           1
6831 #define MARKER_AXIS_X           2
6832 #define MARKER_AXIS_Y           3
6833
6834 static boolean ConfigureJoystickMapButtonsAndAxes(SDL_Joystick *joystick)
6835 {
6836 #if defined(TARGET_SDL2)
6837   static boolean bitmaps_initialized = FALSE;
6838   boolean screen_initialized = FALSE;
6839   static Bitmap *controller, *button, *axis_x, *axis_y;
6840   char *name;
6841   boolean success = TRUE;
6842   boolean done = FALSE, next = FALSE;
6843   Event event;
6844   int alpha = 200, alpha_step = -1;
6845   int alpha_ticks = 0;
6846   char mapping[4096], temp[4096];
6847   int font_name = MENU_SETUP_FONT_TITLE;
6848   int font_info = MENU_SETUP_FONT_TEXT;
6849   int spacing_name = menu.line_spacing_setup[SETUP_MODE_INPUT];
6850   int spacing_line = menu.line_spacing_setup[SETUP_MODE_INPUT];
6851   int spacing_para = menu.paragraph_spacing_setup[SETUP_MODE_INPUT];
6852   int ystep_name = getMenuTextStep(spacing_name, font_name);
6853   int ystep_line = getMenuTextStep(spacing_line, font_info);
6854   int ystep_para = getMenuTextStep(spacing_para, font_info);
6855   int i, j;
6856
6857   struct
6858   {
6859     int x, y;
6860     int marker;
6861     char *field;
6862     int axis, button, hat, hat_value;
6863     char mapping[4096];
6864   }
6865   *step, *prev_step, steps[] =
6866   {
6867     { 356, 155, MARKER_BUTTON, "a",             },
6868     { 396, 122, MARKER_BUTTON, "b",             },
6869     { 320, 125, MARKER_BUTTON, "x",             },
6870     { 358,  95, MARKER_BUTTON, "y",             },
6871     { 162, 125, MARKER_BUTTON, "back",          },
6872     { 216, 125, MARKER_BUTTON, "guide",         },
6873     { 271, 125, MARKER_BUTTON, "start",         },
6874     { 110, 200, MARKER_BUTTON, "dpleft",        },
6875     { 146, 228, MARKER_BUTTON, "dpdown",        },
6876     { 178, 200, MARKER_BUTTON, "dpright",       },
6877     { 146, 172, MARKER_BUTTON, "dpup",          },
6878     {  50,  40, MARKER_BUTTON, "leftshoulder",  },
6879     {  88, -10, MARKER_AXIS_Y, "lefttrigger",   },
6880     { 382,  40, MARKER_BUTTON, "rightshoulder", },
6881     { 346, -10, MARKER_AXIS_Y, "righttrigger",  },
6882     {  73, 141, MARKER_BUTTON, "leftstick",     },
6883     { 282, 210, MARKER_BUTTON, "rightstick",    },
6884     {  73, 141, MARKER_AXIS_X, "leftx",         },
6885     {  73, 141, MARKER_AXIS_Y, "lefty",         },
6886     { 282, 210, MARKER_AXIS_X, "rightx",        },
6887     { 282, 210, MARKER_AXIS_Y, "righty",        },
6888   };
6889
6890   unsigned int event_frame_delay = 0;
6891   unsigned int event_frame_delay_value = GAME_FRAME_DELAY;
6892
6893   ResetDelayCounter(&event_frame_delay);
6894
6895   if (!bitmaps_initialized)
6896   {
6897     controller = LoadCustomImage("joystick/controller.png");
6898     button     = LoadCustomImage("joystick/button.png");
6899     axis_x     = LoadCustomImage("joystick/axis_x.png");
6900     axis_y     = LoadCustomImage("joystick/axis_y.png");
6901
6902     bitmaps_initialized = TRUE;
6903   }
6904
6905   name = getFormattedJoystickName(SDL_JoystickName(joystick));
6906
6907 #if DEBUG_JOYSTICKS
6908   /* print info about the joystick we are watching */
6909   Error(ERR_DEBUG, "watching joystick %d: (%s)\n",
6910         SDL_JoystickInstanceID(joystick), name);
6911   Error(ERR_DEBUG, "joystick has %d axes, %d hats, %d balls, and %d buttons\n",
6912         SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
6913         SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
6914 #endif
6915
6916   /* initialize mapping with GUID and name */
6917   SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), temp, sizeof(temp));
6918
6919   snprintf(mapping, sizeof(mapping), "%s,%s,platform:%s,",
6920            temp, name, SDL_GetPlatform());
6921
6922   /* loop through all steps (buttons and axes), getting joystick events */
6923   for (i = 0; i < SDL_arraysize(steps) && !done;)
6924   {
6925     Bitmap *marker = button;    /* initialize with reliable default value */
6926
6927     step = &steps[i];
6928     strcpy(step->mapping, mapping);
6929     step->axis = -1;
6930     step->button = -1;
6931     step->hat = -1;
6932     step->hat_value = -1;
6933
6934     marker = (step->marker == MARKER_BUTTON ? button :
6935               step->marker == MARKER_AXIS_X ? axis_x :
6936               step->marker == MARKER_AXIS_Y ? axis_y : marker);
6937
6938     next = FALSE;
6939
6940     while (!done && !next)
6941     {
6942       alpha += alpha_step * (int)(SDL_GetTicks() - alpha_ticks) / 5;
6943       alpha_ticks = SDL_GetTicks();
6944
6945       if (alpha >= 255)
6946       {
6947         alpha = 255;
6948         alpha_step = -1;
6949       }
6950       else if (alpha < 128)
6951       {
6952         alpha = 127;
6953         alpha_step = 1;
6954       }
6955
6956       int controller_x = SX + (SXSIZE - controller->width) / 2;
6957       int controller_y = SY + ystep_line;
6958
6959       int marker_x = controller_x + step->x;
6960       int marker_y = controller_y + step->y;
6961
6962       int ystart1 = mSY - 2 * SY + controller_y + controller->height;
6963       int ystart2 = ystart1 + ystep_name + ystep_line;
6964
6965       ClearField();
6966
6967       DrawTextSCentered(ystart1, font_name, name);
6968
6969       DrawTextSCentered(ystart2, font_info,
6970                         "Press buttons and move axes on");
6971       ystart2 += ystep_line;
6972       DrawTextSCentered(ystart2, font_info,
6973                         "your controller when indicated.");
6974       ystart2 += ystep_line;
6975       DrawTextSCentered(ystart2, font_info,
6976                         "(Your controller may look different.)");
6977       ystart2 += ystep_para;
6978
6979 #if defined(PLATFORM_ANDROID)
6980       DrawTextSCentered(ystart2, font_info,
6981                         "To correct a mistake,");
6982       ystart2 += ystep_line;
6983       DrawTextSCentered(ystart2, font_info,
6984                         "press the 'back' button.");
6985       ystart2 += ystep_line;
6986       DrawTextSCentered(ystart2, font_info,
6987                         "To skip a button or axis,");
6988       ystart2 += ystep_line;
6989       DrawTextSCentered(ystart2, font_info,
6990                         "press the 'menu' button.");
6991 #else
6992       DrawTextSCentered(ystart2, font_info,
6993                         "To correct a mistake,");
6994       ystart2 += ystep_line;
6995       DrawTextSCentered(ystart2, font_info,
6996                         "press the 'backspace' key.");
6997       ystart2 += ystep_line;
6998       DrawTextSCentered(ystart2, font_info,
6999                         "To skip a button or axis,");
7000       ystart2 += ystep_line;
7001       DrawTextSCentered(ystart2, font_info,
7002                         "press the 'return' key.");
7003       ystart2 += ystep_line;
7004       DrawTextSCentered(ystart2, font_info,
7005                         "To exit, press the 'escape' key.");
7006 #endif
7007
7008       BlitBitmapMasked(controller, drawto, 0, 0,
7009                        controller->width, controller->height,
7010                        controller_x, controller_y);
7011
7012       SDL_SetSurfaceBlendMode(marker->surface_masked, SDL_BLENDMODE_BLEND);
7013       SDL_SetSurfaceAlphaMod(marker->surface_masked, alpha);
7014
7015       BlitBitmapMasked(marker, drawto, 0, 0,
7016                        marker->width, marker->height,
7017                        marker_x, marker_y);
7018
7019       if (!screen_initialized)
7020         FadeIn(REDRAW_FIELD);
7021       else
7022         BackToFront();
7023
7024       screen_initialized = TRUE;
7025
7026       while (NextValidEvent(&event))
7027       {
7028         switch (event.type)
7029         {
7030           case SDL_JOYAXISMOTION:
7031             if (event.jaxis.value > 20000 ||
7032                 event.jaxis.value < -20000)
7033             {
7034               for (j = 0; j < i; j++)
7035                 if (steps[j].axis == event.jaxis.axis)
7036                   break;
7037
7038               if (j == i)
7039               {
7040                 if (step->marker != MARKER_AXIS_X &&
7041                     step->marker != MARKER_AXIS_Y)
7042                   break;
7043
7044                 step->axis = event.jaxis.axis;
7045                 strcat(mapping, step->field);
7046                 snprintf(temp, sizeof(temp), ":a%u,", event.jaxis.axis);
7047                 strcat(mapping, temp);
7048                 i++;
7049                 next = TRUE;
7050               }
7051             }
7052
7053             break;
7054
7055           case SDL_JOYHATMOTION:
7056             /* ignore centering; we're probably just coming back
7057                to the center from the previous item we set */
7058             if (event.jhat.value == SDL_HAT_CENTERED)
7059               break;
7060
7061             for (j = 0; j < i; j++)
7062               if (steps[j].hat == event.jhat.hat &&
7063                   steps[j].hat_value == event.jhat.value)
7064                 break;
7065
7066             if (j == i)
7067             {
7068               step->hat = event.jhat.hat;
7069               step->hat_value = event.jhat.value;
7070               strcat(mapping, step->field);
7071               snprintf(temp, sizeof(temp), ":h%u.%u,",
7072                        event.jhat.hat, event.jhat.value );
7073               strcat(mapping, temp);
7074               i++;
7075               next = TRUE;
7076             }
7077
7078             break;
7079
7080           case SDL_JOYBALLMOTION:
7081             break;
7082
7083           case SDL_JOYBUTTONUP:
7084             for (j = 0; j < i; j++)
7085               if (steps[j].button == event.jbutton.button)
7086                 break;
7087
7088             if (j == i)
7089             {
7090               step->button = event.jbutton.button;
7091               strcat(mapping, step->field);
7092               snprintf(temp, sizeof(temp), ":b%u,", event.jbutton.button);
7093               strcat(mapping, temp);
7094               i++;
7095               next = TRUE;
7096             }
7097
7098             break;
7099
7100           case SDL_FINGERDOWN:
7101           case SDL_MOUSEBUTTONDOWN:
7102             /* skip this step */
7103             i++;
7104             next = TRUE;
7105
7106             break;
7107
7108           case SDL_KEYDOWN:
7109             if (event.key.keysym.sym == KSYM_BackSpace ||
7110                 event.key.keysym.sym == KSYM_Back)
7111             {
7112               if (i == 0)
7113               {
7114                 /* leave screen */
7115                 success = FALSE;
7116                 done = TRUE;
7117
7118                 break;
7119               }
7120
7121               /* undo this step */
7122               prev_step = &steps[i - 1];
7123               strcpy(mapping, prev_step->mapping);
7124               i--;
7125               next = TRUE;
7126
7127               break;
7128             }
7129
7130             if (event.key.keysym.sym == KSYM_space ||
7131                 event.key.keysym.sym == KSYM_Return ||
7132                 event.key.keysym.sym == KSYM_Menu)
7133             {
7134               /* skip this step */
7135               i++;
7136               next = TRUE;
7137
7138               break;
7139             }
7140
7141             if (event.key.keysym.sym == KSYM_Escape)
7142             {
7143               /* leave screen */
7144               success = FALSE;
7145               done = TRUE;
7146             }
7147
7148             break;
7149
7150           case SDL_QUIT:
7151             program.exit_function(0);
7152             break;
7153
7154           default:
7155             break;
7156         }
7157
7158         // do not handle events for longer than standard frame delay period
7159         if (DelayReached(&event_frame_delay, event_frame_delay_value))
7160           break;
7161       }
7162     }
7163   }
7164
7165   if (success)
7166   {
7167 #if DEBUG_JOYSTICKS
7168     Error(ERR_DEBUG, "New game controller mapping:\n\n%s\n\n", mapping);
7169 #endif
7170
7171     // activate mapping for this game
7172     SDL_GameControllerAddMapping(mapping);
7173
7174     // save mapping to personal mappings
7175     SaveSetup_AddGameControllerMapping(mapping);
7176   }
7177
7178   /* wait until the last pending event was removed from event queue */
7179   while (NextValidEvent(&event));
7180
7181   return success;
7182 #else
7183   return TRUE;
7184 #endif
7185 }
7186
7187 static int ConfigureJoystickMain(int player_nr)
7188 {
7189   char *device_name = setup.input[player_nr].joy.device_name;
7190   int joystick_nr = getJoystickNrFromDeviceName(device_name);
7191   boolean joystick_active = CheckJoystickOpened(joystick_nr);
7192   int success = FALSE;
7193   int i;
7194
7195   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
7196     return JOYSTICK_NOT_AVAILABLE;
7197
7198   if (!joystick_active || !setup.input[player_nr].use_joystick)
7199     return JOYSTICK_NOT_AVAILABLE;
7200
7201   FadeSetEnterMenu();
7202   FadeOut(REDRAW_FIELD);
7203
7204   // close all joystick devices (potentially opened as game controllers)
7205   for (i = 0; i < SDL_NumJoysticks(); i++)
7206     SDLCloseJoystick(i);
7207
7208   // open joystick device as plain joystick to configure as game controller
7209   SDL_Joystick *joystick = SDL_JoystickOpen(joystick_nr);
7210
7211   // as the joystick was successfully opened before, this should not happen
7212   if (joystick == NULL)
7213     return FALSE;
7214
7215   // create new game controller mapping (buttons and axes) for joystick device
7216   success = ConfigureJoystickMapButtonsAndAxes(joystick);
7217
7218   // close joystick (and maybe re-open as configured game controller later)
7219   SDL_JoystickClose(joystick);
7220
7221   // re-open all joystick devices (potentially as game controllers)
7222   for (i = 0; i < SDL_NumJoysticks(); i++)
7223     SDLOpenJoystick(i);
7224
7225   // clear all joystick input actions for all joystick devices
7226   SDLClearJoystickState();
7227
7228   return (success ? JOYSTICK_CONFIGURED : JOYSTICK_NOT_CONFIGURED);
7229 }
7230
7231 void ConfigureJoystick(int player_nr)
7232 {
7233   boolean state = ConfigureJoystickMain(player_nr);
7234
7235   if (state != JOYSTICK_NOT_CONFIGURED)
7236   {
7237     boolean success = (state == JOYSTICK_CONFIGURED);
7238     char message1[MAX_OUTPUT_LINESIZE + 1];
7239     char *message2 = (success ? "configured!" : "not available!");
7240     char *device_name = setup.input[player_nr].joy.device_name;
7241     int nr = getJoystickNrFromDeviceName(device_name) + 1;
7242     int font_nr = FONT_TITLE_1;
7243     int font_height = getFontHeight(font_nr);
7244     int ypos1 = SYSIZE / 2 - font_height * 2;
7245     int ypos2 = SYSIZE / 2 - font_height * 1;
7246     unsigned int wait_frame_delay = 0;
7247     unsigned int wait_frame_delay_value = 2000;
7248
7249     ResetDelayCounter(&wait_frame_delay);
7250
7251     ClearField();
7252
7253     sprintf(message1, "Joystick %d", nr);
7254
7255     DrawTextSCentered(ypos1, font_nr, message1);
7256     DrawTextSCentered(ypos2, font_nr, message2);
7257
7258     while (!DelayReached(&wait_frame_delay, wait_frame_delay_value))
7259       BackToFront();
7260
7261     ClearEventQueue();
7262   }
7263
7264   DrawSetupScreen_Input();
7265 }
7266
7267 boolean ConfigureVirtualButtonsMain()
7268 {
7269   static char *customize_step_text[] =
7270   {
7271     "Move Left",
7272     "Move Right",
7273     "Move Up",
7274     "Move Down",
7275     "Snap Field",
7276     "Drop Element"
7277   };
7278   char grid_button[] =
7279   {
7280     CHAR_GRID_BUTTON_LEFT,
7281     CHAR_GRID_BUTTON_RIGHT,
7282     CHAR_GRID_BUTTON_UP,
7283     CHAR_GRID_BUTTON_DOWN,
7284     CHAR_GRID_BUTTON_SNAP,
7285     CHAR_GRID_BUTTON_DROP
7286   };
7287   int font_nr = FONT_INPUT_1_ACTIVE;
7288   int font_height = getFontHeight(font_nr);
7289   int ypos1 = SYSIZE / 2 - font_height * 2;
7290   int ypos2 = SYSIZE / 2 - font_height * 1;
7291   boolean success = FALSE;
7292   boolean finished = FALSE;
7293   int step_nr = 0;
7294   char grid_button_draw = CHAR_GRID_BUTTON_NONE;
7295   char grid_button_old[MAX_GRID_XSIZE][MAX_GRID_YSIZE];
7296   char grid_button_tmp[MAX_GRID_XSIZE][MAX_GRID_YSIZE];
7297   boolean set_grid_button = FALSE;
7298   int nr = GRID_ACTIVE_NR();
7299   int x, y;
7300
7301   for (x = 0; x < MAX_GRID_XSIZE; x++)
7302     for (y = 0; y < MAX_GRID_YSIZE; y++)
7303       grid_button_old[x][y] = grid_button_tmp[x][y] = overlay.grid_button[x][y];
7304
7305   overlay.grid_button_highlight = grid_button[step_nr];
7306
7307   FadeSetEnterMenu();
7308   FadeOut(REDRAW_FIELD);
7309
7310   ClearField();
7311
7312   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Virtual Buttons");
7313   DrawTextSCentered(ypos1, font_nr, "Select tiles to");
7314   DrawTextSCentered(ypos2, font_nr, customize_step_text[step_nr]);
7315
7316   FadeIn(REDRAW_FIELD);
7317
7318   SetOverlayShowGrid(TRUE);
7319
7320   while (!finished)
7321   {
7322     Event event;
7323
7324     while (NextValidEvent(&event))
7325     {
7326       switch (event.type)
7327       {
7328         case EVENT_KEYPRESS:
7329           {
7330             Key key = GetEventKey((KeyEvent *)&event, FALSE);
7331
7332             /* press 'Escape' to abort and keep the old key bindings */
7333             if (key == KSYM_Escape)
7334             {
7335               for (x = 0; x < MAX_GRID_XSIZE; x++)
7336                 for (y = 0; y < MAX_GRID_YSIZE; y++)
7337                   overlay.grid_button[x][y] = grid_button_old[x][y];
7338
7339               FadeSkipNextFadeIn();
7340
7341               finished = TRUE;
7342
7343               break;
7344             }
7345
7346             /* press 'Enter' to keep the existing key binding */
7347             if (key == KSYM_Return ||
7348                 key == KSYM_Menu ||
7349                 key == KSYM_space)
7350             {
7351               step_nr++;
7352             }
7353             else if (key == KSYM_BackSpace ||
7354                      key == KSYM_Back)
7355             {
7356               if (step_nr == 0)
7357               {
7358                 FadeSkipNextFadeIn();
7359
7360                 finished = TRUE;
7361
7362                 break;
7363               }
7364
7365               step_nr--;
7366             }
7367             else
7368             {
7369               break;
7370             }
7371
7372             /* all virtual buttons configured */
7373             if (step_nr == 6)
7374             {
7375               finished = TRUE;
7376               success = TRUE;
7377
7378               break;
7379             }
7380
7381             for (x = 0; x < MAX_GRID_XSIZE; x++)
7382               for (y = 0; y < MAX_GRID_YSIZE; y++)
7383                 grid_button_tmp[x][y] = overlay.grid_button[x][y];
7384
7385             overlay.grid_button_highlight = grid_button[step_nr];
7386
7387             /* query next virtual button */
7388
7389             ClearField();
7390
7391             DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Virtual Buttons");
7392             DrawTextSCentered(ypos1, font_nr, "Select tiles to");
7393             DrawTextSCentered(ypos2, font_nr, customize_step_text[step_nr]);
7394           }
7395           break;
7396
7397         case EVENT_KEYRELEASE:
7398           key_joystick_mapping = 0;
7399           break;
7400
7401         case EVENT_BUTTONPRESS:
7402         case EVENT_BUTTONRELEASE:
7403           {
7404             ButtonEvent *button = (ButtonEvent *)&event;
7405
7406             button->x += video.screen_xoffset;
7407             button->y += video.screen_yoffset;
7408
7409             x = button->x * overlay.grid_xsize / video.screen_width;
7410             y = button->y * overlay.grid_ysize / video.screen_height;
7411
7412             if (button->type == EVENT_BUTTONPRESS)
7413             {
7414               button_status = button->button;
7415
7416               grid_button_draw =
7417                 (overlay.grid_button[x][y] != grid_button[step_nr] ?
7418                  grid_button[step_nr] : CHAR_GRID_BUTTON_NONE);
7419
7420               set_grid_button = TRUE;
7421             }
7422             else
7423             {
7424               button_status = MB_RELEASED;
7425             }
7426           }
7427           break;
7428
7429         case EVENT_MOTIONNOTIFY:
7430           {
7431             MotionEvent *motion = (MotionEvent *)&event;
7432
7433             motion->x += video.screen_xoffset;
7434             motion->y += video.screen_yoffset;
7435
7436             x = motion->x * overlay.grid_xsize / video.screen_width;
7437             y = motion->y * overlay.grid_ysize / video.screen_height;
7438
7439             set_grid_button = TRUE;
7440           }
7441           break;
7442
7443 #if defined(TARGET_SDL2)
7444         case SDL_WINDOWEVENT:
7445           HandleWindowEvent((WindowEvent *) &event);
7446
7447           // check if device has been rotated
7448           if (nr != GRID_ACTIVE_NR())
7449           {
7450             nr = GRID_ACTIVE_NR();
7451
7452             for (x = 0; x < MAX_GRID_XSIZE; x++)
7453               for (y = 0; y < MAX_GRID_YSIZE; y++)
7454                 grid_button_old[x][y] = grid_button_tmp[x][y] =
7455                   overlay.grid_button[x][y];
7456           }
7457
7458           break;
7459
7460         case SDL_APP_WILLENTERBACKGROUND:
7461         case SDL_APP_DIDENTERBACKGROUND:
7462         case SDL_APP_WILLENTERFOREGROUND:
7463         case SDL_APP_DIDENTERFOREGROUND:
7464           HandlePauseResumeEvent((PauseResumeEvent *) &event);
7465           break;
7466 #endif
7467
7468         default:
7469           HandleOtherEvents(&event);
7470           break;
7471       }
7472
7473       if (set_grid_button)
7474       {
7475         overlay.grid_button[x][y] =
7476           (grid_button_draw != CHAR_GRID_BUTTON_NONE ? grid_button_draw :
7477            grid_button_tmp[x][y] == grid_button[step_nr] ? CHAR_GRID_BUTTON_NONE :
7478            grid_button_tmp[x][y]);
7479
7480         set_grid_button = FALSE;
7481       }
7482     }
7483
7484     BackToFront();
7485   }
7486
7487   for (x = 0; x < MAX_GRID_XSIZE; x++)
7488     for (y = 0; y < MAX_GRID_YSIZE; y++)
7489       setup.touch.grid_button[nr][x][y] = overlay.grid_button[x][y];
7490
7491   overlay.grid_button_highlight = CHAR_GRID_BUTTON_NONE;
7492
7493   SetOverlayShowGrid(FALSE);
7494
7495   return success;
7496 }
7497
7498 void ConfigureVirtualButtons()
7499 {
7500   boolean success = ConfigureVirtualButtonsMain();
7501
7502   if (success)
7503   {
7504     int font_nr = FONT_TITLE_1;
7505     int font_height = getFontHeight(font_nr);
7506     int ypos1 = SYSIZE / 2 - font_height * 2;
7507     int ypos2 = SYSIZE / 2 - font_height * 1;
7508     unsigned int wait_frame_delay = 0;
7509     unsigned int wait_frame_delay_value = 2000;
7510
7511     ResetDelayCounter(&wait_frame_delay);
7512
7513     ClearField();
7514
7515     DrawTextSCentered(ypos1, font_nr, "Virtual buttons");
7516     DrawTextSCentered(ypos2, font_nr, "configured!");
7517
7518     while (!DelayReached(&wait_frame_delay, wait_frame_delay_value))
7519       BackToFront();
7520
7521     ClearEventQueue();
7522   }
7523 }
7524
7525 void DrawSetupScreen()
7526 {
7527   if (setup_mode == SETUP_MODE_INPUT)
7528     DrawSetupScreen_Input();
7529   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
7530     DrawChooseTree(&game_speed_current);
7531   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
7532     DrawChooseTree(&scroll_delay_current);
7533   else if (setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
7534     DrawChooseTree(&snapshot_mode_current);
7535   else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
7536     DrawChooseTree(&window_size_current);
7537   else if (setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
7538     DrawChooseTree(&scaling_type_current);
7539   else if (setup_mode == SETUP_MODE_CHOOSE_RENDERING)
7540     DrawChooseTree(&rendering_mode_current);
7541   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
7542     DrawChooseTree(&artwork.gfx_current);
7543   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
7544     DrawChooseTree(&artwork.snd_current);
7545   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
7546     DrawChooseTree(&artwork.mus_current);
7547   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE)
7548     DrawChooseTree(&volume_simple_current);
7549   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS)
7550     DrawChooseTree(&volume_loops_current);
7551   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
7552     DrawChooseTree(&volume_music_current);
7553   else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL)
7554     DrawChooseTree(&touch_control_current);
7555   else if (setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE)
7556     DrawChooseTree(&move_distance_current);
7557   else if (setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
7558     DrawChooseTree(&drop_distance_current);
7559   else if (setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY)
7560     DrawChooseTree(&transparency_current);
7561   else
7562     DrawSetupScreen_Generic();
7563
7564   PlayMenuSoundsAndMusic();
7565 }
7566
7567 void RedrawSetupScreenAfterFullscreenToggle()
7568 {
7569   if (setup_mode == SETUP_MODE_GRAPHICS ||
7570       setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
7571   {
7572     // update list selection from "setup.window_scaling_percent"
7573     execSetupGraphics_setWindowSizes(TRUE);
7574
7575     DrawSetupScreen();
7576   }
7577 }
7578
7579 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
7580 {
7581   if (setup_mode == SETUP_MODE_INPUT)
7582     HandleSetupScreen_Input(mx, my, dx, dy, button);
7583   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
7584     HandleChooseTree(mx, my, dx, dy, button, &game_speed_current);
7585   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
7586     HandleChooseTree(mx, my, dx, dy, button, &scroll_delay_current);
7587   else if (setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
7588     HandleChooseTree(mx, my, dx, dy, button, &snapshot_mode_current);
7589   else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
7590     HandleChooseTree(mx, my, dx, dy, button, &window_size_current);
7591   else if (setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
7592     HandleChooseTree(mx, my, dx, dy, button, &scaling_type_current);
7593   else if (setup_mode == SETUP_MODE_CHOOSE_RENDERING)
7594     HandleChooseTree(mx, my, dx, dy, button, &rendering_mode_current);
7595   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
7596     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
7597   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
7598     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
7599   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
7600     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
7601   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE)
7602     HandleChooseTree(mx, my, dx, dy, button, &volume_simple_current);
7603   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS)
7604     HandleChooseTree(mx, my, dx, dy, button, &volume_loops_current);
7605   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
7606     HandleChooseTree(mx, my, dx, dy, button, &volume_music_current);
7607   else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL)
7608     HandleChooseTree(mx, my, dx, dy, button, &touch_control_current);
7609   else if (setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE)
7610     HandleChooseTree(mx, my, dx, dy, button, &move_distance_current);
7611   else if (setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
7612     HandleChooseTree(mx, my, dx, dy, button, &drop_distance_current);
7613   else if (setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY)
7614     HandleChooseTree(mx, my, dx, dy, button, &transparency_current);
7615   else
7616     HandleSetupScreen_Generic(mx, my, dx, dy, button);
7617 }
7618
7619 void HandleGameActions()
7620 {
7621   if (game.restart_game_message != NULL)
7622     RequestRestartGame(game.restart_game_message);
7623
7624   if (game_status != GAME_MODE_PLAYING)
7625     return;
7626
7627   GameActions();        /* main game loop */
7628
7629   if (tape.auto_play && !tape.playing)
7630     AutoPlayTape();     /* continue automatically playing next tape */
7631 }
7632
7633
7634 /* ---------- new screen button stuff -------------------------------------- */
7635
7636 static void getScreenMenuButtonPos(int *x, int *y, int gadget_id)
7637 {
7638   switch (gadget_id)
7639   {
7640     case SCREEN_CTRL_ID_PREV_LEVEL:
7641       *x = mSX + GDI_ACTIVE_POS(menu.main.button.prev_level.x);
7642       *y = mSY + GDI_ACTIVE_POS(menu.main.button.prev_level.y);
7643       break;
7644
7645     case SCREEN_CTRL_ID_NEXT_LEVEL:
7646       *x = mSX + GDI_ACTIVE_POS(menu.main.button.next_level.x);
7647       *y = mSY + GDI_ACTIVE_POS(menu.main.button.next_level.y);
7648       break;
7649
7650     case SCREEN_CTRL_ID_PREV_PLAYER:
7651       *x = mSX + TILEX * 10;
7652       *y = mSY + TILEY * MENU_SCREEN_START_YPOS;
7653       break;
7654
7655     case SCREEN_CTRL_ID_NEXT_PLAYER:
7656       *x = mSX + TILEX * 12;
7657       *y = mSY + TILEY * MENU_SCREEN_START_YPOS;
7658       break;
7659
7660     default:
7661       Error(ERR_EXIT, "unknown gadget ID %d", gadget_id);
7662   }
7663 }
7664
7665 static struct
7666 {
7667   int gfx_unpressed, gfx_pressed;
7668   void (*get_gadget_position)(int *, int *, int);
7669   int gadget_id;
7670   int screen_mask;
7671   char *infotext;
7672 } menubutton_info[NUM_SCREEN_MENUBUTTONS] =
7673 {
7674   {
7675     IMG_MENU_BUTTON_PREV_LEVEL, IMG_MENU_BUTTON_PREV_LEVEL_ACTIVE,
7676     getScreenMenuButtonPos,
7677     SCREEN_CTRL_ID_PREV_LEVEL,
7678     SCREEN_MASK_MAIN,
7679     "last level"
7680   },
7681   {
7682     IMG_MENU_BUTTON_NEXT_LEVEL, IMG_MENU_BUTTON_NEXT_LEVEL_ACTIVE,
7683     getScreenMenuButtonPos,
7684     SCREEN_CTRL_ID_NEXT_LEVEL,
7685     SCREEN_MASK_MAIN,
7686     "next level"
7687   },
7688   {
7689     IMG_MENU_BUTTON_LEFT, IMG_MENU_BUTTON_LEFT_ACTIVE,
7690     getScreenMenuButtonPos,
7691     SCREEN_CTRL_ID_PREV_PLAYER,
7692     SCREEN_MASK_INPUT,
7693     "last player"
7694   },
7695   {
7696     IMG_MENU_BUTTON_RIGHT, IMG_MENU_BUTTON_RIGHT_ACTIVE,
7697     getScreenMenuButtonPos,
7698     SCREEN_CTRL_ID_NEXT_PLAYER,
7699     SCREEN_MASK_INPUT,
7700     "next player"
7701   },
7702 };
7703
7704 static struct
7705 {
7706   int gfx_unpressed, gfx_pressed;
7707   int x, y;
7708   int gadget_id;
7709   char *infotext;
7710 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
7711 {
7712   {
7713     IMG_MENU_BUTTON_UP, IMG_MENU_BUTTON_UP_ACTIVE,
7714     -1, -1,     /* these values are not constant, but can change at runtime */
7715     SCREEN_CTRL_ID_SCROLL_UP,
7716     "scroll up"
7717   },
7718   {
7719     IMG_MENU_BUTTON_DOWN, IMG_MENU_BUTTON_DOWN_ACTIVE,
7720     -1, -1,     /* these values are not constant, but can change at runtime */
7721     SCREEN_CTRL_ID_SCROLL_DOWN,
7722     "scroll down"
7723   }
7724 };
7725
7726 static struct
7727 {
7728   int gfx_unpressed, gfx_pressed;
7729   int x, y;
7730   int width, height;
7731   int type;
7732   int gadget_id;
7733   char *infotext;
7734 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
7735 {
7736   {
7737     IMG_MENU_SCROLLBAR, IMG_MENU_SCROLLBAR_ACTIVE,
7738     -1, -1,     /* these values are not constant, but can change at runtime */
7739     -1, -1,     /* these values are not constant, but can change at runtime */
7740     GD_TYPE_SCROLLBAR_VERTICAL,
7741     SCREEN_CTRL_ID_SCROLL_VERTICAL,
7742     "scroll level series vertically"
7743   }
7744 };
7745
7746 static void CreateScreenMenubuttons()
7747 {
7748   struct GadgetInfo *gi;
7749   unsigned int event_mask;
7750   int i;
7751
7752   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
7753   {
7754     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
7755     int gfx_unpressed, gfx_pressed;
7756     int x, y, width, height;
7757     int gd_x1, gd_x2, gd_y1, gd_y2;
7758     int id = menubutton_info[i].gadget_id;
7759
7760     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
7761
7762     menubutton_info[i].get_gadget_position(&x, &y, id);
7763
7764     width = SC_MENUBUTTON_XSIZE;
7765     height = SC_MENUBUTTON_YSIZE;
7766
7767     gfx_unpressed = menubutton_info[i].gfx_unpressed;
7768     gfx_pressed   = menubutton_info[i].gfx_pressed;
7769     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
7770     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
7771     gd_x1 = graphic_info[gfx_unpressed].src_x;
7772     gd_y1 = graphic_info[gfx_unpressed].src_y;
7773     gd_x2 = graphic_info[gfx_pressed].src_x;
7774     gd_y2 = graphic_info[gfx_pressed].src_y;
7775
7776     gi = CreateGadget(GDI_CUSTOM_ID, id,
7777                       GDI_CUSTOM_TYPE_ID, i,
7778                       GDI_INFO_TEXT, menubutton_info[i].infotext,
7779                       GDI_X, x,
7780                       GDI_Y, y,
7781                       GDI_WIDTH, width,
7782                       GDI_HEIGHT, height,
7783                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7784                       GDI_STATE, GD_BUTTON_UNPRESSED,
7785                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
7786                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
7787                       GDI_DIRECT_DRAW, FALSE,
7788                       GDI_EVENT_MASK, event_mask,
7789                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
7790                       GDI_END);
7791
7792     if (gi == NULL)
7793       Error(ERR_EXIT, "cannot create gadget");
7794
7795     screen_gadget[id] = gi;
7796   }
7797 }
7798
7799 static void CreateScreenScrollbuttons()
7800 {
7801   struct GadgetInfo *gi;
7802   unsigned int event_mask;
7803   int i;
7804
7805   /* these values are not constant, but can change at runtime */
7806   scrollbutton_info[0].x = SC_SCROLL_UP_XPOS;
7807   scrollbutton_info[0].y = SC_SCROLL_UP_YPOS;
7808   scrollbutton_info[1].x = SC_SCROLL_DOWN_XPOS;
7809   scrollbutton_info[1].y = SC_SCROLL_DOWN_YPOS;
7810
7811   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
7812   {
7813     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
7814     int gfx_unpressed, gfx_pressed;
7815     int x, y, width, height;
7816     int gd_x1, gd_x2, gd_y1, gd_y2;
7817     int id = scrollbutton_info[i].gadget_id;
7818
7819     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
7820
7821     x = mSX + scrollbutton_info[i].x + menu.scrollbar_xoffset;
7822     y = mSY + scrollbutton_info[i].y;
7823     width = SC_SCROLLBUTTON_XSIZE;
7824     height = SC_SCROLLBUTTON_YSIZE;
7825
7826     /* correct scrollbar position if placed outside menu (playfield) area */
7827     if (x > SX + SC_SCROLL_UP_XPOS)
7828       x = SX + SC_SCROLL_UP_XPOS;
7829
7830     if (id == SCREEN_CTRL_ID_SCROLL_DOWN)
7831       y = mSY + (SC_SCROLL_VERTICAL_YPOS +
7832                  (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE);
7833
7834     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
7835     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
7836     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
7837     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
7838     gd_x1 = graphic_info[gfx_unpressed].src_x;
7839     gd_y1 = graphic_info[gfx_unpressed].src_y;
7840     gd_x2 = graphic_info[gfx_pressed].src_x;
7841     gd_y2 = graphic_info[gfx_pressed].src_y;
7842
7843     gi = CreateGadget(GDI_CUSTOM_ID, id,
7844                       GDI_CUSTOM_TYPE_ID, i,
7845                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
7846                       GDI_X, x,
7847                       GDI_Y, y,
7848                       GDI_WIDTH, width,
7849                       GDI_HEIGHT, height,
7850                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7851                       GDI_STATE, GD_BUTTON_UNPRESSED,
7852                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
7853                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
7854                       GDI_DIRECT_DRAW, FALSE,
7855                       GDI_EVENT_MASK, event_mask,
7856                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
7857                       GDI_END);
7858
7859     if (gi == NULL)
7860       Error(ERR_EXIT, "cannot create gadget");
7861
7862     screen_gadget[id] = gi;
7863   }
7864 }
7865
7866 static void CreateScreenScrollbars()
7867 {
7868   int i;
7869
7870   /* these values are not constant, but can change at runtime */
7871   scrollbar_info[0].x = SC_SCROLL_VERTICAL_XPOS;
7872   scrollbar_info[0].y = SC_SCROLL_VERTICAL_YPOS;
7873   scrollbar_info[0].width  = SC_SCROLL_VERTICAL_XSIZE;
7874   scrollbar_info[0].height = SC_SCROLL_VERTICAL_YSIZE;
7875
7876   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
7877   {
7878     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
7879     int gfx_unpressed, gfx_pressed;
7880     int x, y, width, height;
7881     int gd_x1, gd_x2, gd_y1, gd_y2;
7882     struct GadgetInfo *gi;
7883     int items_max, items_visible, item_position;
7884     unsigned int event_mask;
7885     int num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
7886     int id = scrollbar_info[i].gadget_id;
7887
7888     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
7889
7890     x = mSX + scrollbar_info[i].x + menu.scrollbar_xoffset;
7891     y = mSY + scrollbar_info[i].y;
7892     width  = scrollbar_info[i].width;
7893     height = scrollbar_info[i].height;
7894
7895     /* correct scrollbar position if placed outside menu (playfield) area */
7896     if (x > SX + SC_SCROLL_VERTICAL_XPOS)
7897       x = SX + SC_SCROLL_VERTICAL_XPOS;
7898
7899     if (id == SCREEN_CTRL_ID_SCROLL_VERTICAL)
7900       height = (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE;
7901
7902     items_max = num_page_entries;
7903     items_visible = num_page_entries;
7904     item_position = 0;
7905
7906     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
7907     gfx_pressed   = scrollbar_info[i].gfx_pressed;
7908     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
7909     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
7910     gd_x1 = graphic_info[gfx_unpressed].src_x;
7911     gd_y1 = graphic_info[gfx_unpressed].src_y;
7912     gd_x2 = graphic_info[gfx_pressed].src_x;
7913     gd_y2 = graphic_info[gfx_pressed].src_y;
7914
7915     gi = CreateGadget(GDI_CUSTOM_ID, id,
7916                       GDI_CUSTOM_TYPE_ID, i,
7917                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
7918                       GDI_X, x,
7919                       GDI_Y, y,
7920                       GDI_WIDTH, width,
7921                       GDI_HEIGHT, height,
7922                       GDI_TYPE, scrollbar_info[i].type,
7923                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
7924                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
7925                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
7926                       GDI_WHEEL_AREA_X, SX,
7927                       GDI_WHEEL_AREA_Y, SY,
7928                       GDI_WHEEL_AREA_WIDTH, SXSIZE,
7929                       GDI_WHEEL_AREA_HEIGHT, SYSIZE,
7930                       GDI_STATE, GD_BUTTON_UNPRESSED,
7931                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
7932                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
7933                       GDI_BORDER_SIZE, SC_BORDER_SIZE, SC_BORDER_SIZE,
7934                       GDI_DIRECT_DRAW, FALSE,
7935                       GDI_EVENT_MASK, event_mask,
7936                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
7937                       GDI_END);
7938
7939     if (gi == NULL)
7940       Error(ERR_EXIT, "cannot create gadget");
7941
7942     screen_gadget[id] = gi;
7943   }
7944 }
7945
7946 void CreateScreenGadgets()
7947 {
7948   CreateScreenMenubuttons();
7949
7950   CreateScreenScrollbuttons();
7951   CreateScreenScrollbars();
7952 }
7953
7954 void FreeScreenGadgets()
7955 {
7956   int i;
7957
7958   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
7959     FreeGadget(screen_gadget[i]);
7960 }
7961
7962 void MapScreenMenuGadgets(int screen_mask)
7963 {
7964   int i;
7965
7966   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
7967     if (screen_mask & menubutton_info[i].screen_mask)
7968       MapGadget(screen_gadget[menubutton_info[i].gadget_id]);
7969 }
7970
7971 void MapScreenGadgets(int num_entries)
7972 {
7973   int i;
7974
7975   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
7976     return;
7977
7978   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
7979     MapGadget(screen_gadget[scrollbutton_info[i].gadget_id]);
7980
7981   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
7982     MapGadget(screen_gadget[scrollbar_info[i].gadget_id]);
7983 }
7984
7985 void MapScreenTreeGadgets(TreeInfo *ti)
7986 {
7987   MapScreenGadgets(numTreeInfoInGroup(ti));
7988 }
7989
7990 static void HandleScreenGadgets(struct GadgetInfo *gi)
7991 {
7992   int id = gi->custom_id;
7993   int button = gi->event.button;
7994   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
7995
7996   switch (id)
7997   {
7998     case SCREEN_CTRL_ID_PREV_LEVEL:
7999       HandleMainMenu_SelectLevel(step, -1, NO_DIRECT_LEVEL_SELECT);
8000       break;
8001
8002     case SCREEN_CTRL_ID_NEXT_LEVEL:
8003       HandleMainMenu_SelectLevel(step, +1, NO_DIRECT_LEVEL_SELECT);
8004       break;
8005
8006     case SCREEN_CTRL_ID_PREV_PLAYER:
8007       HandleSetupScreen_Input_Player(step, -1);
8008       break;
8009
8010     case SCREEN_CTRL_ID_NEXT_PLAYER:
8011       HandleSetupScreen_Input_Player(step, +1);
8012       break;
8013
8014     case SCREEN_CTRL_ID_SCROLL_UP:
8015       if (game_status == GAME_MODE_LEVELS)
8016         HandleChooseLevelSet(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
8017       else if (game_status == GAME_MODE_LEVELNR)
8018         HandleChooseLevelNr(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
8019       else if (game_status == GAME_MODE_SETUP)
8020         HandleSetupScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
8021       else if (game_status == GAME_MODE_INFO)
8022         HandleInfoScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
8023       break;
8024
8025     case SCREEN_CTRL_ID_SCROLL_DOWN:
8026       if (game_status == GAME_MODE_LEVELS)
8027         HandleChooseLevelSet(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
8028       else if (game_status == GAME_MODE_LEVELNR)
8029         HandleChooseLevelNr(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
8030       else if (game_status == GAME_MODE_SETUP)
8031         HandleSetupScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
8032       else if (game_status == GAME_MODE_INFO)
8033         HandleInfoScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
8034       break;
8035
8036     case SCREEN_CTRL_ID_SCROLL_VERTICAL:
8037       if (game_status == GAME_MODE_LEVELS)
8038         HandleChooseLevelSet(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
8039       else if (game_status == GAME_MODE_LEVELNR)
8040         HandleChooseLevelNr(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
8041       else if (game_status == GAME_MODE_SETUP)
8042         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
8043       else if (game_status == GAME_MODE_INFO)
8044         HandleInfoScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
8045       break;
8046
8047     default:
8048       break;
8049   }
8050 }