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