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