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