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