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