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