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