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