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