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