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