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