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