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