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