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