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