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