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