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