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