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