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