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