added support for gadget-like pressable menu buttons on main screen
[rocksndiamonds.git] / src / screens.c
1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // screens.c
10 // ============================================================================
11
12 #include "libgame/libgame.h"
13
14 #include "screens.h"
15 #include "events.h"
16 #include "game.h"
17 #include "tools.h"
18 #include "editor.h"
19 #include "files.h"
20 #include "tape.h"
21 #include "cartoons.h"
22 #include "network.h"
23 #include "init.h"
24 #include "config.h"
25
26 /* screens on the info screen */
27 #define INFO_MODE_MAIN                  0
28 #define INFO_MODE_TITLE                 1
29 #define INFO_MODE_ELEMENTS              2
30 #define INFO_MODE_MUSIC                 3
31 #define INFO_MODE_CREDITS               4
32 #define INFO_MODE_PROGRAM               5
33 #define INFO_MODE_VERSION               6
34 #define INFO_MODE_LEVELSET              7
35
36 #define MAX_INFO_MODES                  8
37
38 /* screens on the setup screen */
39 /* (must match GFX_SPECIAL_ARG_SETUP_* values as defined in src/main.h) */
40 /* (should also match corresponding entries in src/conf_gfx.c) */
41 #define SETUP_MODE_MAIN                 0
42 #define SETUP_MODE_GAME                 1
43 #define SETUP_MODE_EDITOR               2
44 #define SETUP_MODE_GRAPHICS             3
45 #define SETUP_MODE_SOUND                4
46 #define SETUP_MODE_ARTWORK              5
47 #define SETUP_MODE_INPUT                6
48 #define SETUP_MODE_TOUCH                7
49 #define SETUP_MODE_SHORTCUTS            8
50 #define SETUP_MODE_SHORTCUTS_1          9
51 #define SETUP_MODE_SHORTCUTS_2          10
52 #define SETUP_MODE_SHORTCUTS_3          11
53 #define SETUP_MODE_SHORTCUTS_4          12
54 #define SETUP_MODE_SHORTCUTS_5          13
55
56 /* sub-screens on the setup screen (generic) */
57 #define SETUP_MODE_CHOOSE_ARTWORK       14
58 #define SETUP_MODE_CHOOSE_OTHER         15
59
60 /* sub-screens on the setup screen (specific) */
61 #define SETUP_MODE_CHOOSE_GAME_SPEED    16
62 #define SETUP_MODE_CHOOSE_SCROLL_DELAY  17
63 #define SETUP_MODE_CHOOSE_SCREEN_MODE   18
64 #define SETUP_MODE_CHOOSE_WINDOW_SIZE   19
65 #define SETUP_MODE_CHOOSE_SCALING_TYPE  20
66 #define SETUP_MODE_CHOOSE_GRAPHICS      21
67 #define SETUP_MODE_CHOOSE_SOUNDS        22
68 #define SETUP_MODE_CHOOSE_MUSIC         23
69 #define SETUP_MODE_CHOOSE_VOLUME_SIMPLE 24
70 #define SETUP_MODE_CHOOSE_VOLUME_LOOPS  25
71 #define SETUP_MODE_CHOOSE_VOLUME_MUSIC  26
72 #define SETUP_MODE_CHOOSE_TOUCH_CONTROL 27
73 #define SETUP_MODE_CHOOSE_MOVE_DISTANCE 28
74 #define SETUP_MODE_CHOOSE_DROP_DISTANCE 29
75
76 #define MAX_SETUP_MODES                 30
77
78 /* for input setup functions */
79 #define SETUPINPUT_SCREEN_POS_START     0
80 #define SETUPINPUT_SCREEN_POS_END       (SCR_FIELDY - 4)
81 #define SETUPINPUT_SCREEN_POS_EMPTY1    (SETUPINPUT_SCREEN_POS_START + 3)
82 #define SETUPINPUT_SCREEN_POS_EMPTY2    (SETUPINPUT_SCREEN_POS_END - 1)
83
84 /* for various menu stuff  */
85 #define MENU_SCREEN_START_XPOS          1
86 #define MENU_SCREEN_START_YPOS          2
87 #define MENU_SCREEN_VALUE_XPOS          (SCR_FIELDX - 3)
88 #define MENU_SCREEN_MAX_XPOS            (SCR_FIELDX - 1)
89 #define MENU_TITLE1_YPOS                8
90 #define MENU_TITLE2_YPOS                46
91 #define MENU_SCREEN_INFO_XSTART         16
92 #define MENU_SCREEN_INFO_YSTART1        100
93 #define MENU_SCREEN_INFO_YSTART2        128
94 #define MENU_SCREEN_INFO_YSTEP          (TILEY + 4)
95 #define MENU_SCREEN_INFO_YBOTTOM        (SYSIZE - 20)
96 #define MENU_SCREEN_INFO_YSIZE          (MENU_SCREEN_INFO_YBOTTOM -     \
97                                          MENU_SCREEN_INFO_YSTART2 -     \
98                                          TILEY / 2)
99 #define MAX_INFO_ELEMENTS_ON_SCREEN     128
100 #define NUM_INFO_ELEMENTS_ON_SCREEN     MIN(MENU_SCREEN_INFO_YSIZE /    \
101                                             MENU_SCREEN_INFO_YSTEP,     \
102                                             MAX_INFO_ELEMENTS_ON_SCREEN)
103 #define MAX_MENU_ENTRIES_ON_SCREEN      (SCR_FIELDY - MENU_SCREEN_START_YPOS)
104 #define MAX_MENU_TEXT_LENGTH_BIG        13
105 #define MAX_MENU_TEXT_LENGTH_MEDIUM     (MAX_MENU_TEXT_LENGTH_BIG * 2)
106
107 /* buttons and scrollbars identifiers */
108 #define SCREEN_CTRL_ID_PREV_LEVEL       0
109 #define SCREEN_CTRL_ID_NEXT_LEVEL       1
110 #define SCREEN_CTRL_ID_PREV_PLAYER      2
111 #define SCREEN_CTRL_ID_NEXT_PLAYER      3
112 #define SCREEN_CTRL_ID_SCROLL_UP        4
113 #define SCREEN_CTRL_ID_SCROLL_DOWN      5
114 #define SCREEN_CTRL_ID_SCROLL_VERTICAL  6
115
116 #define NUM_SCREEN_GADGETS              7
117
118 #define NUM_SCREEN_MENUBUTTONS          4
119 #define NUM_SCREEN_SCROLLBUTTONS        2
120 #define NUM_SCREEN_SCROLLBARS           1
121
122 #define SCREEN_MASK_MAIN                (1 << 0)
123 #define SCREEN_MASK_INPUT               (1 << 1)
124
125 /* graphic position and size values for buttons and scrollbars */
126 #define SC_MENUBUTTON_XSIZE             TILEX
127 #define SC_MENUBUTTON_YSIZE             TILEY
128
129 #define SC_SCROLLBUTTON_XSIZE           TILEX
130 #define SC_SCROLLBUTTON_YSIZE           TILEY
131
132 #define SC_SCROLLBAR_XPOS               (SXSIZE - SC_SCROLLBUTTON_XSIZE)
133
134 #define SC_SCROLL_VERTICAL_XSIZE        SC_SCROLLBUTTON_XSIZE
135 #define SC_SCROLL_VERTICAL_YSIZE        ((MAX_MENU_ENTRIES_ON_SCREEN - 2) * \
136                                          SC_SCROLLBUTTON_YSIZE)
137
138 #define SC_SCROLL_UP_XPOS               SC_SCROLLBAR_XPOS
139 #define SC_SCROLL_UP_YPOS               (2 * SC_SCROLLBUTTON_YSIZE)
140
141 #define SC_SCROLL_VERTICAL_XPOS         SC_SCROLLBAR_XPOS
142 #define SC_SCROLL_VERTICAL_YPOS         (SC_SCROLL_UP_YPOS + \
143                                          SC_SCROLLBUTTON_YSIZE)
144
145 #define SC_SCROLL_DOWN_XPOS             SC_SCROLLBAR_XPOS
146 #define SC_SCROLL_DOWN_YPOS             (SC_SCROLL_VERTICAL_YPOS + \
147                                          SC_SCROLL_VERTICAL_YSIZE)
148
149 #define SC_BORDER_SIZE                  14
150
151
152 /* forward declarations of internal functions */
153 static void HandleScreenGadgets(struct GadgetInfo *);
154 static void HandleSetupScreen_Generic(int, int, int, int, int);
155 static void HandleSetupScreen_Input(int, int, int, int, int);
156 static void CustomizeKeyboard(int);
157 static void CalibrateJoystick(int);
158 static void execSetupGame(void);
159 static void execSetupGraphics(void);
160 static void execSetupSound(void);
161 static void execSetupTouch(void);
162 static void execSetupArtwork(void);
163 static void HandleChooseTree(int, int, int, int, int, TreeInfo **);
164
165 static void DrawChooseLevelSet(void);
166 static void DrawChooseLevelNr(void);
167 static void DrawInfoScreen(void);
168 static void DrawAndFadeInInfoScreen(int);
169 static void DrawSetupScreen(void);
170
171 static void DrawInfoScreenExt(int, int);
172 static void DrawInfoScreen_NotAvailable(char *, char *);
173 static void DrawInfoScreen_HelpAnim(int, int, boolean);
174 static void DrawInfoScreen_HelpText(int, int, int, int);
175 static void HandleInfoScreen_Main(int, int, int, int, int);
176 static void HandleInfoScreen_TitleScreen(int);
177 static void HandleInfoScreen_Elements(int);
178 static void HandleInfoScreen_Music(int);
179 static void HandleInfoScreen_Credits(int);
180 static void HandleInfoScreen_Program(int);
181 static void HandleInfoScreen_Version(int);
182
183 static void MapScreenMenuGadgets(int);
184 static void MapScreenGadgets(int);
185 static void MapScreenTreeGadgets(TreeInfo *);
186
187 static struct GadgetInfo *screen_gadget[NUM_SCREEN_GADGETS];
188
189 static int info_mode = INFO_MODE_MAIN;
190 static int setup_mode = SETUP_MODE_MAIN;
191
192 static TreeInfo *screen_modes = NULL;
193 static TreeInfo *screen_mode_current = NULL;
194
195 static TreeInfo *window_sizes = NULL;
196 static TreeInfo *window_size_current = NULL;
197
198 static TreeInfo *scaling_types = NULL;
199 static TreeInfo *scaling_type_current = NULL;
200
201 static TreeInfo *scroll_delays = NULL;
202 static TreeInfo *scroll_delay_current = NULL;
203
204 static TreeInfo *game_speeds = NULL;
205 static TreeInfo *game_speed_current = NULL;
206
207 static TreeInfo *volumes_simple = NULL;
208 static TreeInfo *volume_simple_current = NULL;
209
210 static TreeInfo *volumes_loops = NULL;
211 static TreeInfo *volume_loops_current = NULL;
212
213 static TreeInfo *volumes_music = NULL;
214 static TreeInfo *volume_music_current = NULL;
215
216 static TreeInfo *touch_controls = NULL;
217 static TreeInfo *touch_control_current = NULL;
218
219 static TreeInfo *move_distances = NULL;
220 static TreeInfo *move_distance_current = NULL;
221
222 static TreeInfo *drop_distances = NULL;
223 static TreeInfo *drop_distance_current = NULL;
224
225 static TreeInfo *level_number = NULL;
226 static TreeInfo *level_number_current = NULL;
227
228 static struct
229 {
230   int value;
231   char *text;
232 } window_sizes_list[] =
233 {
234   {     50,     "50 %"                          },
235   {     80,     "80 %"                          },
236   {     90,     "90 %"                          },
237   {     100,    "100 % (Default)"               },
238   {     110,    "110 %"                         },
239   {     120,    "120 %"                         },
240   {     130,    "130 %"                         },
241   {     140,    "140 %"                         },
242   {     150,    "150 %"                         },
243   {     200,    "200 %"                         },
244   {     250,    "250 %"                         },
245   {     300,    "300 %"                         },
246
247   {     -1,     NULL                            },
248 };
249
250 static struct
251 {
252   char *value;
253   char *text;
254 } scaling_types_list[] =
255 {
256   {     SCALING_QUALITY_NEAREST, "None"         },
257   {     SCALING_QUALITY_LINEAR,  "Linear"       },
258   {     SCALING_QUALITY_BEST,    "Anisotropic"  },
259
260   {     NULL,                    NULL           },
261 };
262
263 static struct
264 {
265   int value;
266   char *text;
267 } game_speeds_list[] =
268 {
269 #if 1
270   {     30,     "Very Slow"                     },
271   {     25,     "Slow"                          },
272   {     20,     "Normal"                        },
273   {     15,     "Fast"                          },
274   {     10,     "Very Fast"                     },
275 #else
276   {     1000,   "1/1s (Extremely Slow)"         },
277   {     500,    "1/2s"                          },
278   {     200,    "1/5s"                          },
279   {     100,    "1/10s"                         },
280   {     50,     "1/20s"                         },
281   {     29,     "1/35s (Original Supaplex)"     },
282   {     25,     "1/40s"                         },
283   {     20,     "1/50s (Normal Speed)"          },
284   {     14,     "1/70s (Maximum Supaplex)"      },
285   {     10,     "1/100s"                        },
286   {     5,      "1/200s"                        },
287   {     2,      "1/500s"                        },
288   {     1,      "1/1000s (Extremely Fast)"      },
289 #endif
290
291   {     -1,     NULL                            },
292 };
293
294 static struct
295 {
296   int value;
297   char *text;
298 } scroll_delays_list[] =
299 {
300   {     0,      "0 Tiles (No Scroll Delay)"     },
301   {     1,      "1 Tile"                        },
302   {     2,      "2 Tiles"                       },
303   {     3,      "3 Tiles (Default)"             },
304   {     4,      "4 Tiles"                       },
305   {     5,      "5 Tiles"                       },
306   {     6,      "6 Tiles"                       },
307   {     7,      "7 Tiles"                       },
308   {     8,      "8 Tiles (Maximum Scroll Delay)"},
309
310   {     -1,     NULL                            },
311 };
312
313 static struct
314 {
315   int value;
316   char *text;
317 } volumes_list[] =
318 {
319   {     0,      "0 %"                           },
320   {     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   UnmapAllGadgets();
3294
3295   FreeScreenGadgets();
3296   CreateScreenGadgets();
3297
3298   CloseDoor(DOOR_CLOSE_2);
3299
3300   FadeOut(fade_mask);
3301
3302   ClearField();
3303
3304   HandleChooseTree(0, 0, 0, 0, MB_MENU_INITIALIZE, ti_ptr);
3305   MapScreenTreeGadgets(*ti_ptr);
3306
3307   FadeIn(fade_mask);
3308
3309   InitAnimation();
3310 }
3311
3312 static void AdjustScrollbar(int id, int items_max, int items_visible,
3313                             int item_position)
3314 {
3315   struct GadgetInfo *gi = screen_gadget[id];
3316
3317   if (item_position > items_max - items_visible)
3318     item_position = items_max - items_visible;
3319
3320   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
3321                GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
3322                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
3323 }
3324
3325 static void AdjustChooseTreeScrollbar(int id, int first_entry, TreeInfo *ti)
3326 {
3327   AdjustScrollbar(id, numTreeInfoInGroup(ti), NUM_MENU_ENTRIES_ON_SCREEN,
3328                   first_entry);
3329 }
3330
3331 static void clearMenuListArea()
3332 {
3333   int scrollbar_xpos = mSX + SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
3334
3335   /* correct scrollbar position if placed outside menu (playfield) area */
3336   if (scrollbar_xpos > SX + SC_SCROLLBAR_XPOS)
3337     scrollbar_xpos = SX + SC_SCROLLBAR_XPOS;
3338
3339   /* clear menu list area, but not title or scrollbar */
3340   DrawBackground(mSX, mSY + MENU_SCREEN_START_YPOS * 32,
3341                  scrollbar_xpos - mSX, NUM_MENU_ENTRIES_ON_SCREEN * 32);
3342 }
3343
3344 static void drawChooseTreeList(int first_entry, int num_page_entries,
3345                                TreeInfo *ti)
3346 {
3347   int i;
3348   char *title_string = NULL;
3349   int yoffset_sets = MENU_TITLE1_YPOS;
3350   int yoffset_setup = 16;
3351   int yoffset = (ti->type == TREE_TYPE_LEVEL_DIR ||
3352                  ti->type == TREE_TYPE_LEVEL_NR ? yoffset_sets : yoffset_setup);
3353   int last_game_status = game_status;   /* save current game status */
3354
3355   title_string = ti->infotext;
3356
3357   DrawTextSCentered(mSY - SY + yoffset, FONT_TITLE_1, title_string);
3358
3359   clearMenuListArea();
3360
3361   for (i = 0; i < num_page_entries; i++)
3362   {
3363     TreeInfo *node, *node_first;
3364     int entry_pos = first_entry + i;
3365     int xpos = MENU_SCREEN_START_XPOS;
3366     int ypos = MENU_SCREEN_START_YPOS + i;
3367     int startx = mSX + xpos * 32;
3368     int starty = mSY + ypos * 32;
3369     int font_nr = FONT_TEXT_1;
3370     int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
3371     int startx_text = startx + font_xoffset;
3372     int startx_scrollbar = mSX + SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
3373     int text_size = startx_scrollbar - startx_text;
3374     int max_buffer_len = text_size / getFontWidth(font_nr);
3375     char buffer[max_buffer_len + 1];
3376
3377     node_first = getTreeInfoFirstGroupEntry(ti);
3378     node = getTreeInfoFromPos(node_first, entry_pos);
3379
3380     strncpy(buffer, node->name, max_buffer_len);
3381     buffer[max_buffer_len] = '\0';
3382
3383     DrawText(startx, starty, buffer, font_nr + node->color);
3384
3385     if (node->parent_link)
3386       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
3387     else if (node->level_group)
3388       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
3389     else
3390       initCursor(i, IMG_MENU_BUTTON);
3391   }
3392
3393   game_status = last_game_status;       /* restore current game status */
3394
3395   redraw_mask |= REDRAW_FIELD;
3396 }
3397
3398 static void drawChooseTreeInfo(int entry_pos, TreeInfo *ti)
3399 {
3400   TreeInfo *node, *node_first;
3401   int x, last_redraw_mask = redraw_mask;
3402   int ypos = MENU_TITLE2_YPOS;
3403   int font_nr = FONT_TITLE_2;
3404
3405   if (ti->type == TREE_TYPE_LEVEL_NR)
3406     DrawTextFCentered(ypos, font_nr, leveldir_current->name);
3407
3408   if (ti->type != TREE_TYPE_LEVEL_DIR)
3409     return;
3410
3411   node_first = getTreeInfoFirstGroupEntry(ti);
3412   node = getTreeInfoFromPos(node_first, entry_pos);
3413
3414   DrawBackgroundForFont(SX, SY + ypos, SXSIZE, getFontHeight(font_nr), font_nr);
3415
3416   if (node->parent_link)
3417     DrawTextFCentered(ypos, font_nr, "leave \"%s\"",
3418                       node->node_parent->name);
3419   else if (node->level_group)
3420     DrawTextFCentered(ypos, font_nr, "enter \"%s\"",
3421                       node->name);
3422   else if (ti->type == TREE_TYPE_LEVEL_DIR)
3423     DrawTextFCentered(ypos, font_nr, "%3d %s (%s)",
3424                       node->levels, (node->levels > 1 ? "levels" : "level"),
3425                       node->class_desc);
3426
3427   /* let BackToFront() redraw only what is needed */
3428   redraw_mask = last_redraw_mask | REDRAW_TILES;
3429   for (x = 0; x < SCR_FIELDX; x++)
3430     MarkTileDirty(x, 1);
3431 }
3432
3433 static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
3434                              TreeInfo **ti_ptr)
3435 {
3436   TreeInfo *ti = *ti_ptr;
3437   int x = 0;
3438   int y = ti->cl_cursor;
3439   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
3440   int num_entries = numTreeInfoInGroup(ti);
3441   int num_page_entries;
3442   int last_game_status = game_status;   /* save current game status */
3443   boolean position_set_by_scrollbar = (dx == 999);
3444
3445   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
3446     num_page_entries = num_entries;
3447   else
3448     num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
3449
3450   game_status = last_game_status;       /* restore current game status */
3451
3452   if (button == MB_MENU_INITIALIZE)
3453   {
3454     int num_entries = numTreeInfoInGroup(ti);
3455     int entry_pos = posTreeInfo(ti);
3456
3457     if (ti->cl_first == -1)
3458     {
3459       /* only on initialization */
3460       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
3461       ti->cl_cursor = entry_pos - ti->cl_first;
3462     }
3463     else if (ti->cl_cursor >= num_page_entries ||
3464              (num_entries > num_page_entries &&
3465               num_entries - ti->cl_first < num_page_entries))
3466     {
3467       /* only after change of list size (by custom graphic configuration) */
3468       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
3469       ti->cl_cursor = entry_pos - ti->cl_first;
3470     }
3471
3472     if (position_set_by_scrollbar)
3473       ti->cl_first = dy;
3474     else
3475       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
3476                                 ti->cl_first, ti);
3477
3478     drawChooseTreeList(ti->cl_first, num_page_entries, ti);
3479     drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
3480     drawChooseTreeCursor(ti->cl_cursor, TRUE);
3481
3482     return;
3483   }
3484   else if (button == MB_MENU_LEAVE)
3485   {
3486     FadeSetLeaveMenu();
3487
3488     PlaySound(SND_MENU_ITEM_SELECTING);
3489
3490     if (ti->node_parent)
3491     {
3492       *ti_ptr = ti->node_parent;
3493       DrawChooseTree(ti_ptr);
3494     }
3495     else if (game_status == GAME_MODE_SETUP)
3496     {
3497       if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
3498           setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
3499         execSetupGame();
3500       else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE ||
3501                setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
3502                setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
3503         execSetupGraphics();
3504       else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
3505                setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
3506                setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
3507         execSetupSound();
3508       else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
3509                setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
3510                setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
3511         execSetupTouch();
3512       else
3513         execSetupArtwork();
3514     }
3515     else
3516     {
3517       if (game_status == GAME_MODE_LEVELNR)
3518       {
3519         int new_level_nr = atoi(level_number_current->identifier);
3520
3521         HandleMainMenu_SelectLevel(0, 0, new_level_nr);
3522       }
3523
3524       game_status = GAME_MODE_MAIN;
3525
3526       DrawMainMenuExt(REDRAW_FIELD, FALSE);
3527     }
3528
3529     return;
3530   }
3531
3532   if (mx || my)         /* mouse input */
3533   {
3534     int last_game_status = game_status; /* save current game status */
3535
3536     x = (mx - mSX) / 32;
3537     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
3538
3539     game_status = last_game_status;     /* restore current game status */
3540   }
3541   else if (dx || dy)    /* keyboard or scrollbar/scrollbutton input */
3542   {
3543     /* move cursor instead of scrolling when already at start/end of list */
3544     if (dy == -1 * SCROLL_LINE && ti->cl_first == 0)
3545       dy = -1;
3546     else if (dy == +1 * SCROLL_LINE &&
3547              ti->cl_first + num_page_entries == num_entries)
3548       dy = 1;
3549
3550     /* handle scrolling screen one line or page */
3551     if (ti->cl_cursor + dy < 0 ||
3552         ti->cl_cursor + dy > num_page_entries - 1)
3553     {
3554       boolean redraw = FALSE;
3555
3556       if (ABS(dy) == SCROLL_PAGE)
3557         step = num_page_entries - 1;
3558
3559       if (dy < 0 && ti->cl_first > 0)
3560       {
3561         /* scroll page/line up */
3562
3563         ti->cl_first -= step;
3564         if (ti->cl_first < 0)
3565           ti->cl_first = 0;
3566
3567         redraw = TRUE;
3568       }
3569       else if (dy > 0 && ti->cl_first + num_page_entries < num_entries)
3570       {
3571         /* scroll page/line down */
3572
3573         ti->cl_first += step;
3574         if (ti->cl_first + num_page_entries > num_entries)
3575           ti->cl_first = MAX(0, num_entries - num_page_entries);
3576
3577         redraw = TRUE;
3578       }
3579
3580       if (redraw)
3581       {
3582         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
3583         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
3584         drawChooseTreeCursor(ti->cl_cursor, TRUE);
3585
3586         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
3587                                   ti->cl_first, ti);
3588       }
3589
3590
3591       return;
3592     }
3593
3594     /* handle moving cursor one line */
3595     y = ti->cl_cursor + dy;
3596   }
3597
3598   if (dx == 1)
3599   {
3600     TreeInfo *node_first, *node_cursor;
3601     int entry_pos = ti->cl_first + y;
3602
3603     node_first = getTreeInfoFirstGroupEntry(ti);
3604     node_cursor = getTreeInfoFromPos(node_first, entry_pos);
3605
3606     if (node_cursor->node_group)
3607     {
3608       FadeSetEnterMenu();
3609
3610       PlaySound(SND_MENU_ITEM_SELECTING);
3611
3612       node_cursor->cl_first = ti->cl_first;
3613       node_cursor->cl_cursor = ti->cl_cursor;
3614       *ti_ptr = node_cursor->node_group;
3615       DrawChooseTree(ti_ptr);
3616
3617       return;
3618     }
3619   }
3620   else if (dx == -1 && ti->node_parent)
3621   {
3622     FadeSetLeaveMenu();
3623
3624     PlaySound(SND_MENU_ITEM_SELECTING);
3625
3626     *ti_ptr = ti->node_parent;
3627     DrawChooseTree(ti_ptr);
3628
3629     return;
3630   }
3631
3632   if (!anyScrollbarGadgetActive() &&
3633       IN_VIS_MENU(x, y) &&
3634       mx < screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x &&
3635       y >= 0 && y < num_page_entries)
3636   {
3637     if (button)
3638     {
3639       if (y != ti->cl_cursor)
3640       {
3641         PlaySound(SND_MENU_ITEM_ACTIVATING);
3642
3643         drawChooseTreeCursor(ti->cl_cursor, FALSE);
3644         drawChooseTreeCursor(y, TRUE);
3645         drawChooseTreeInfo(ti->cl_first + y, ti);
3646
3647         ti->cl_cursor = y;
3648       }
3649     }
3650     else
3651     {
3652       TreeInfo *node_first, *node_cursor;
3653       int entry_pos = ti->cl_first + y;
3654
3655       PlaySound(SND_MENU_ITEM_SELECTING);
3656
3657       node_first = getTreeInfoFirstGroupEntry(ti);
3658       node_cursor = getTreeInfoFromPos(node_first, entry_pos);
3659
3660       if (node_cursor->node_group)
3661       {
3662         FadeSetEnterMenu();
3663
3664         node_cursor->cl_first = ti->cl_first;
3665         node_cursor->cl_cursor = ti->cl_cursor;
3666         *ti_ptr = node_cursor->node_group;
3667         DrawChooseTree(ti_ptr);
3668       }
3669       else if (node_cursor->parent_link)
3670       {
3671         FadeSetLeaveMenu();
3672
3673         *ti_ptr = node_cursor->node_parent;
3674         DrawChooseTree(ti_ptr);
3675       }
3676       else
3677       {
3678         FadeSetEnterMenu();
3679
3680         node_cursor->cl_first = ti->cl_first;
3681         node_cursor->cl_cursor = ti->cl_cursor;
3682         *ti_ptr = node_cursor;
3683
3684         if (ti->type == TREE_TYPE_LEVEL_DIR)
3685         {
3686           LoadLevelSetup_SeriesInfo();
3687
3688           SaveLevelSetup_LastSeries();
3689           SaveLevelSetup_SeriesInfo();
3690           TapeErase();
3691         }
3692
3693         if (game_status == GAME_MODE_SETUP)
3694         {
3695           if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
3696               setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
3697             execSetupGame();
3698           else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE ||
3699                    setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
3700                    setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
3701             execSetupGraphics();
3702           else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
3703                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
3704                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
3705             execSetupSound();
3706           else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
3707                    setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
3708                    setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
3709             execSetupTouch();
3710           else
3711             execSetupArtwork();
3712         }
3713         else
3714         {
3715           if (game_status == GAME_MODE_LEVELNR)
3716           {
3717             int new_level_nr = atoi(level_number_current->identifier);
3718
3719             HandleMainMenu_SelectLevel(0, 0, new_level_nr);
3720           }
3721
3722           game_status = GAME_MODE_MAIN;
3723
3724           DrawMainMenu();
3725         }
3726       }
3727     }
3728   }
3729 }
3730
3731 void DrawChooseLevelSet()
3732 {
3733   SetMainBackgroundImage(IMG_BACKGROUND_LEVELS);
3734
3735   DrawChooseTree(&leveldir_current);
3736
3737   PlayMenuSound();
3738   PlayMenuMusic();
3739 }
3740
3741 void HandleChooseLevelSet(int mx, int my, int dx, int dy, int button)
3742 {
3743   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
3744 }
3745
3746 void DrawChooseLevelNr()
3747 {
3748   int i;
3749
3750   if (level_number != NULL)
3751   {
3752     freeTreeInfo(level_number);
3753
3754     level_number = NULL;
3755   }
3756
3757   for (i = leveldir_current->first_level; i <= leveldir_current->last_level;i++)
3758   {
3759     TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_LEVEL_NR);
3760     char identifier[32], name[32];
3761     int value = i;
3762
3763     /* temporarily load level info to get level name */
3764     LoadLevelInfoOnly(i);
3765
3766     ti->node_top = &level_number;
3767     ti->sort_priority = 10000 + value;
3768     ti->color = (level.no_valid_file ? FC_BLUE :
3769                  LevelStats_getSolved(i) ? FC_GREEN :
3770                  LevelStats_getPlayed(i) ? FC_YELLOW : FC_RED);
3771
3772     sprintf(identifier, "%d", value);
3773     sprintf(name, "%03d: %s", value,
3774             (level.no_valid_file ? "(no file)" : level.name));
3775
3776     setString(&ti->identifier, identifier);
3777     setString(&ti->name, name);
3778     setString(&ti->name_sorting, name);
3779
3780     pushTreeInfo(&level_number, ti);
3781   }
3782
3783   /* sort level number values to start with lowest level number */
3784   sortTreeInfo(&level_number);
3785
3786   /* set current level number to current level number */
3787   level_number_current =
3788     getTreeInfoFromIdentifier(level_number, i_to_a(level_nr));
3789
3790   /* if that also fails, set current level number to first available level */
3791   if (level_number_current == NULL)
3792     level_number_current = level_number;
3793
3794   SetMainBackgroundImage(IMG_BACKGROUND_LEVELNR);
3795
3796   DrawChooseTree(&level_number_current);
3797
3798   PlayMenuSound();
3799   PlayMenuMusic();
3800 }
3801
3802 void HandleChooseLevelNr(int mx, int my, int dx, int dy, int button)
3803 {
3804   HandleChooseTree(mx, my, dx, dy, button, &level_number_current);
3805 }
3806
3807 void DrawHallOfFame(int highlight_position)
3808 {
3809   int fade_mask = (DrawingAreaChanged() ? REDRAW_ALL : REDRAW_FIELD);
3810
3811   UnmapAllGadgets();
3812   FadeSoundsAndMusic();
3813
3814   /* (this is needed when called from GameEnd() after winning a game) */
3815   KeyboardAutoRepeatOn();
3816   ActivateJoystick();
3817
3818   /* (this is needed when called from GameEnd() after winning a game) */
3819   SetDrawDeactivationMask(REDRAW_NONE);
3820   SetDrawBackgroundMask(REDRAW_FIELD);
3821
3822   CloseDoor(DOOR_CLOSE_2);
3823
3824   if (highlight_position < 0) 
3825     LoadScore(level_nr);
3826
3827   FadeSetEnterScreen();
3828
3829   FadeOut(fade_mask);
3830
3831   InitAnimation();
3832
3833   PlayMenuSound();
3834   PlayMenuMusic();
3835
3836   HandleHallOfFame(highlight_position, 0, 0, 0, MB_MENU_INITIALIZE);
3837
3838   FadeIn(fade_mask);
3839 }
3840
3841 static void drawHallOfFameList(int first_entry, int highlight_position)
3842 {
3843   int i, j;
3844
3845   SetMainBackgroundImage(IMG_BACKGROUND_SCORES);
3846   ClearField();
3847
3848   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, "Hall Of Fame");
3849   DrawTextFCentered(MENU_TITLE2_YPOS, FONT_TITLE_2,
3850                     "HighScores of Level %d", level_nr);
3851
3852   for (i = 0; i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
3853   {
3854     int entry = first_entry + i;
3855     boolean active = (entry == highlight_position);
3856     int font_nr1 = (active ? FONT_TEXT_1_ACTIVE : FONT_TEXT_1);
3857     int font_nr2 = (active ? FONT_TEXT_2_ACTIVE : FONT_TEXT_2);
3858     int font_nr3 = (active ? FONT_TEXT_3_ACTIVE : FONT_TEXT_3);
3859     int font_nr4 = (active ? FONT_TEXT_4_ACTIVE : FONT_TEXT_4);
3860     int dx1 = 3 * getFontWidth(font_nr1);
3861     int dx2 = dx1 + getFontWidth(font_nr1);
3862     int dx3 = SXSIZE - 2 * (mSX - SX) - 5 * getFontWidth(font_nr4);
3863     int num_dots = (dx3 - dx2) / getFontWidth(font_nr3);
3864     int sy = mSY + 64 + i * 32;
3865
3866     DrawText(mSX, sy, int2str(entry + 1, 3), font_nr1);
3867     DrawText(mSX + dx1, sy, ".", font_nr1);
3868
3869     for (j = 0; j < num_dots; j++)
3870       DrawText(mSX + dx2 + j * getFontWidth(font_nr3), sy, ".", font_nr3);
3871
3872     if (!strEqual(highscore[entry].Name, EMPTY_PLAYER_NAME))
3873       DrawText(mSX + dx2, sy, highscore[entry].Name, font_nr2);
3874
3875     DrawText(mSX + dx3, sy, int2str(highscore[entry].Score, 5), font_nr4);
3876   }
3877
3878   redraw_mask |= REDRAW_FIELD;
3879 }
3880
3881 void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
3882 {
3883   static int first_entry = 0;
3884   static int highlight_position = 0;
3885   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
3886
3887   if (button == MB_MENU_INITIALIZE)
3888   {
3889     first_entry = 0;
3890     highlight_position = mx;
3891     drawHallOfFameList(first_entry, highlight_position);
3892
3893     return;
3894   }
3895
3896   if (ABS(dy) == SCROLL_PAGE)           /* handle scrolling one page */
3897     step = NUM_MENU_ENTRIES_ON_SCREEN - 1;
3898
3899   if (dy < 0)
3900   {
3901     if (first_entry > 0)
3902     {
3903       first_entry -= step;
3904       if (first_entry < 0)
3905         first_entry = 0;
3906
3907       drawHallOfFameList(first_entry, highlight_position);
3908     }
3909   }
3910   else if (dy > 0)
3911   {
3912     if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
3913     {
3914       first_entry += step;
3915       if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
3916         first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
3917
3918       drawHallOfFameList(first_entry, highlight_position);
3919     }
3920   }
3921   else if (button == MB_MENU_LEAVE)
3922   {
3923     PlaySound(SND_MENU_ITEM_SELECTING);
3924
3925     FadeSound(SND_BACKGROUND_SCORES);
3926
3927     game_status = GAME_MODE_MAIN;
3928
3929     DrawMainMenu();
3930   }
3931   else if (button == MB_MENU_CHOICE)
3932   {
3933     PlaySound(SND_MENU_ITEM_SELECTING);
3934
3935     FadeSound(SND_BACKGROUND_SCORES);
3936
3937     game_status = GAME_MODE_MAIN;
3938
3939     DrawAndFadeInMainMenu(REDRAW_FIELD);
3940   }
3941
3942   if (game_status == GAME_MODE_SCORES)
3943     PlayMenuSoundIfLoop();
3944 }
3945
3946
3947 /* ========================================================================= */
3948 /* setup screen functions                                                    */
3949 /* ========================================================================= */
3950
3951 static struct TokenInfo *setup_info;
3952 static int num_setup_info;      /* number of setup entries shown on screen */
3953 static int max_setup_info;      /* total number of setup entries in list */
3954
3955 static char *screen_mode_text;
3956 static char *window_size_text;
3957 static char *scaling_type_text;
3958 static char *scroll_delay_text;
3959 static char *game_speed_text;
3960 static char *graphics_set_name;
3961 static char *sounds_set_name;
3962 static char *music_set_name;
3963 static char *volume_simple_text;
3964 static char *volume_loops_text;
3965 static char *volume_music_text;
3966 static char *touch_controls_text;
3967 static char *move_distance_text;
3968 static char *drop_distance_text;
3969
3970 static void execSetupMain()
3971 {
3972   setup_mode = SETUP_MODE_MAIN;
3973
3974   DrawSetupScreen();
3975 }
3976
3977 static void execSetupGame_setGameSpeeds()
3978 {
3979   if (game_speeds == NULL)
3980   {
3981     int i;
3982
3983     for (i = 0; game_speeds_list[i].value != -1; i++)
3984     {
3985       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
3986       char identifier[32], name[32];
3987       int value = game_speeds_list[i].value;
3988       char *text = game_speeds_list[i].text;
3989
3990       ti->node_top = &game_speeds;
3991       ti->sort_priority = 10000 - value;
3992
3993       sprintf(identifier, "%d", value);
3994       sprintf(name, "%s", text);
3995
3996       setString(&ti->identifier, identifier);
3997       setString(&ti->name, name);
3998       setString(&ti->name_sorting, name);
3999       setString(&ti->infotext, "Game Speed");
4000
4001       pushTreeInfo(&game_speeds, ti);
4002     }
4003
4004     /* sort game speed values to start with slowest game speed */
4005     sortTreeInfo(&game_speeds);
4006
4007     /* set current game speed to configured game speed value */
4008     game_speed_current =
4009       getTreeInfoFromIdentifier(game_speeds, i_to_a(setup.game_frame_delay));
4010
4011     /* if that fails, set current game speed to reliable default value */
4012     if (game_speed_current == NULL)
4013       game_speed_current =
4014         getTreeInfoFromIdentifier(game_speeds, i_to_a(GAME_FRAME_DELAY));
4015
4016     /* if that also fails, set current game speed to first available speed */
4017     if (game_speed_current == NULL)
4018       game_speed_current = game_speeds;
4019   }
4020
4021   setup.game_frame_delay = atoi(game_speed_current->identifier);
4022
4023   /* needed for displaying game speed text instead of identifier */
4024   game_speed_text = game_speed_current->name;
4025 }
4026
4027 static void execSetupGame_setScrollDelays()
4028 {
4029   if (scroll_delays == NULL)
4030   {
4031     int i;
4032
4033     for (i = 0; scroll_delays_list[i].value != -1; i++)
4034     {
4035       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4036       char identifier[32], name[32];
4037       int value = scroll_delays_list[i].value;
4038       char *text = scroll_delays_list[i].text;
4039
4040       ti->node_top = &scroll_delays;
4041       ti->sort_priority = value;
4042
4043       sprintf(identifier, "%d", value);
4044       sprintf(name, "%s", text);
4045
4046       setString(&ti->identifier, identifier);
4047       setString(&ti->name, name);
4048       setString(&ti->name_sorting, name);
4049       setString(&ti->infotext, "Scaling Type");
4050
4051       pushTreeInfo(&scroll_delays, ti);
4052     }
4053
4054     /* sort scaling type values to start with lowest scaling type value */
4055     sortTreeInfo(&scroll_delays);
4056
4057     /* set current scaling type value to configured scaling type value */
4058     scroll_delay_current =
4059       getTreeInfoFromIdentifier(scroll_delays,i_to_a(setup.scroll_delay_value));
4060
4061     /* if that fails, set current scaling type to reliable default value */
4062     if (scroll_delay_current == NULL)
4063       scroll_delay_current =
4064         getTreeInfoFromIdentifier(scroll_delays, i_to_a(STD_SCROLL_DELAY));
4065
4066     /* if that also fails, set current scaling type to first available value */
4067     if (scroll_delay_current == NULL)
4068       scroll_delay_current = scroll_delays;
4069   }
4070
4071   setup.scroll_delay_value = atoi(scroll_delay_current->identifier);
4072
4073   /* needed for displaying scaling type text instead of identifier */
4074   scroll_delay_text = scroll_delay_current->name;
4075 }
4076
4077 static void execSetupGame()
4078 {
4079   execSetupGame_setGameSpeeds();
4080   execSetupGame_setScrollDelays();
4081
4082   setup_mode = SETUP_MODE_GAME;
4083
4084   DrawSetupScreen();
4085 }
4086
4087 static void execSetupChooseGameSpeed()
4088 {
4089   setup_mode = SETUP_MODE_CHOOSE_GAME_SPEED;
4090
4091   DrawSetupScreen();
4092 }
4093
4094 static void execSetupChooseScrollDelay()
4095 {
4096   setup_mode = SETUP_MODE_CHOOSE_SCROLL_DELAY;
4097
4098   DrawSetupScreen();
4099 }
4100
4101 static void execSetupEditor()
4102 {
4103   setup_mode = SETUP_MODE_EDITOR;
4104
4105   DrawSetupScreen();
4106 }
4107
4108 static void execSetupGraphics_setWindowSizes(boolean update_list)
4109 {
4110   if (window_sizes != NULL && update_list)
4111   {
4112     freeTreeInfo(window_sizes);
4113
4114     window_sizes = NULL;
4115   }
4116
4117   if (window_sizes == NULL)
4118   {
4119     boolean current_window_size_found = FALSE;
4120     int i;
4121
4122     for (i = 0; window_sizes_list[i].value != -1; i++)
4123     {
4124       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4125       char identifier[32], name[32];
4126       int value = window_sizes_list[i].value;
4127       char *text = window_sizes_list[i].text;
4128
4129       ti->node_top = &window_sizes;
4130       ti->sort_priority = value;
4131
4132       sprintf(identifier, "%d", value);
4133       sprintf(name, "%s", text);
4134
4135       setString(&ti->identifier, identifier);
4136       setString(&ti->name, name);
4137       setString(&ti->name_sorting, name);
4138       setString(&ti->infotext, "Window Scaling");
4139
4140       pushTreeInfo(&window_sizes, ti);
4141
4142       if (value == setup.window_scaling_percent)
4143         current_window_size_found = TRUE;
4144     }
4145
4146     if (!current_window_size_found)
4147     {
4148       // add entry for non-preset window scaling value
4149
4150       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4151       char identifier[32], name[32];
4152       int value = setup.window_scaling_percent;
4153
4154       ti->node_top = &window_sizes;
4155       ti->sort_priority = value;
4156
4157       sprintf(identifier, "%d", value);
4158       sprintf(name, "%d %% (Current)", value);
4159
4160       setString(&ti->identifier, identifier);
4161       setString(&ti->name, name);
4162       setString(&ti->name_sorting, name);
4163       setString(&ti->infotext, "Window Scaling");
4164
4165       pushTreeInfo(&window_sizes, ti);
4166     }
4167
4168     /* sort window size values to start with lowest window size value */
4169     sortTreeInfo(&window_sizes);
4170
4171     /* set current window size value to configured window size value */
4172     window_size_current =
4173       getTreeInfoFromIdentifier(window_sizes,
4174                                 i_to_a(setup.window_scaling_percent));
4175
4176     /* if that fails, set current window size to reliable default value */
4177     if (window_size_current == NULL)
4178       window_size_current =
4179         getTreeInfoFromIdentifier(window_sizes,
4180                                   i_to_a(STD_WINDOW_SCALING_PERCENT));
4181
4182     /* if that also fails, set current window size to first available value */
4183     if (window_size_current == NULL)
4184       window_size_current = window_sizes;
4185   }
4186
4187   setup.window_scaling_percent = atoi(window_size_current->identifier);
4188
4189   /* needed for displaying window size text instead of identifier */
4190   window_size_text = window_size_current->name;
4191 }
4192
4193 static void execSetupGraphics_setScalingTypes()
4194 {
4195   if (scaling_types == NULL)
4196   {
4197     int i;
4198
4199     for (i = 0; scaling_types_list[i].value != NULL; i++)
4200     {
4201       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4202       char identifier[32], name[32];
4203       char *value = scaling_types_list[i].value;
4204       char *text = scaling_types_list[i].text;
4205
4206       ti->node_top = &scaling_types;
4207       ti->sort_priority = i;
4208
4209       sprintf(identifier, "%s", value);
4210       sprintf(name, "%s", text);
4211
4212       setString(&ti->identifier, identifier);
4213       setString(&ti->name, name);
4214       setString(&ti->name_sorting, name);
4215       setString(&ti->infotext, "Anti-Aliasing");
4216
4217       pushTreeInfo(&scaling_types, ti);
4218     }
4219
4220     /* sort scaling type values to start with lowest scaling type value */
4221     sortTreeInfo(&scaling_types);
4222
4223     /* set current scaling type value to configured scaling type value */
4224     scaling_type_current =
4225       getTreeInfoFromIdentifier(scaling_types, setup.window_scaling_quality);
4226
4227     /* if that fails, set current scaling type to reliable default value */
4228     if (scaling_type_current == NULL)
4229       scaling_type_current =
4230         getTreeInfoFromIdentifier(scaling_types, SCALING_QUALITY_DEFAULT);
4231
4232     /* if that also fails, set current scaling type to first available value */
4233     if (scaling_type_current == NULL)
4234       scaling_type_current = scaling_types;
4235   }
4236
4237   setup.window_scaling_quality = scaling_type_current->identifier;
4238
4239   /* needed for displaying scaling type text instead of identifier */
4240   scaling_type_text = scaling_type_current->name;
4241 }
4242
4243 static void execSetupGraphics_setScreenModes()
4244 {
4245   // if (screen_modes == NULL && video.fullscreen_available)
4246   if (screen_modes == NULL && video.fullscreen_modes != NULL)
4247   {
4248     int i;
4249
4250     for (i = 0; video.fullscreen_modes[i].width != -1; i++)
4251     {
4252       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4253       char identifier[32], name[32];
4254       int x = video.fullscreen_modes[i].width;
4255       int y = video.fullscreen_modes[i].height;
4256       int xx, yy;
4257
4258       get_aspect_ratio_from_screen_mode(&video.fullscreen_modes[i], &xx, &yy);
4259
4260       ti->node_top = &screen_modes;
4261       ti->sort_priority = x * 10000 + y;
4262
4263       sprintf(identifier, "%dx%d", x, y);
4264       sprintf(name, "%d x %d [%d:%d]", x, y, xx, yy);
4265
4266       setString(&ti->identifier, identifier);
4267       setString(&ti->name, name);
4268       setString(&ti->name_sorting, name);
4269       setString(&ti->infotext, "Fullscreen Mode");
4270
4271       pushTreeInfo(&screen_modes, ti);
4272     }
4273
4274     /* sort fullscreen modes to start with lowest available screen resolution */
4275     sortTreeInfo(&screen_modes);
4276
4277     /* set current screen mode for fullscreen mode to configured setup value */
4278     screen_mode_current = getTreeInfoFromIdentifier(screen_modes,
4279                                                     setup.fullscreen_mode);
4280
4281     /* if that fails, set current screen mode to reliable default value */
4282     if (screen_mode_current == NULL)
4283       screen_mode_current = getTreeInfoFromIdentifier(screen_modes,
4284                                                       DEFAULT_FULLSCREEN_MODE);
4285
4286     /* if that also fails, set current screen mode to first available mode */
4287     if (screen_mode_current == NULL)
4288       screen_mode_current = screen_modes;
4289
4290     if (screen_mode_current == NULL)
4291       video.fullscreen_available = FALSE;
4292   }
4293
4294   // if (video.fullscreen_available)
4295   if (screen_mode_current != NULL)
4296   {
4297     setup.fullscreen_mode = screen_mode_current->identifier;
4298
4299     /* needed for displaying screen mode name instead of identifier */
4300     screen_mode_text = screen_mode_current->name;
4301   }
4302 }
4303
4304 static void execSetupGraphics()
4305 {
4306   if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
4307   {
4308     // update "setup.window_scaling_percent" from list selection
4309     execSetupGraphics_setWindowSizes(FALSE);
4310   }
4311   else
4312   {
4313     // update list selection from "setup.window_scaling_percent"
4314     execSetupGraphics_setWindowSizes(TRUE);
4315   }
4316
4317   execSetupGraphics_setScalingTypes();
4318   execSetupGraphics_setScreenModes();
4319
4320   setup_mode = SETUP_MODE_GRAPHICS;
4321
4322   DrawSetupScreen();
4323
4324 #if defined(TARGET_SDL2)
4325   // window scaling may have changed at this point
4326   ToggleFullscreenOrChangeWindowScalingIfNeeded();
4327
4328   // window scaling quality may have changed at this point
4329   if (!strEqual(setup.window_scaling_quality, video.window_scaling_quality))
4330     SDLSetWindowScalingQuality(setup.window_scaling_quality);
4331 #endif
4332 }
4333
4334 #if !defined(PLATFORM_ANDROID)
4335 #if defined(TARGET_SDL2)
4336 static void execSetupChooseWindowSize()
4337 {
4338   setup_mode = SETUP_MODE_CHOOSE_WINDOW_SIZE;
4339
4340   DrawSetupScreen();
4341 }
4342
4343 static void execSetupChooseScalingType()
4344 {
4345   setup_mode = SETUP_MODE_CHOOSE_SCALING_TYPE;
4346
4347   DrawSetupScreen();
4348 }
4349 #else
4350 static void execSetupChooseScreenMode()
4351 {
4352   if (!video.fullscreen_available)
4353     return;
4354
4355   setup_mode = SETUP_MODE_CHOOSE_SCREEN_MODE;
4356
4357   DrawSetupScreen();
4358 }
4359 #endif
4360 #endif
4361
4362 static void execSetupChooseVolumeSimple()
4363 {
4364   setup_mode = SETUP_MODE_CHOOSE_VOLUME_SIMPLE;
4365
4366   DrawSetupScreen();
4367 }
4368
4369 static void execSetupChooseVolumeLoops()
4370 {
4371   setup_mode = SETUP_MODE_CHOOSE_VOLUME_LOOPS;
4372
4373   DrawSetupScreen();
4374 }
4375
4376 static void execSetupChooseVolumeMusic()
4377 {
4378   setup_mode = SETUP_MODE_CHOOSE_VOLUME_MUSIC;
4379
4380   DrawSetupScreen();
4381 }
4382
4383 static void execSetupSound()
4384 {
4385   if (volumes_simple == NULL)
4386   {
4387     boolean current_volume_simple_found = FALSE;
4388     int i;
4389
4390     for (i = 0; volumes_list[i].value != -1; i++)
4391     {
4392       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4393       char identifier[32], name[32];
4394       int value = volumes_list[i].value;
4395       char *text = volumes_list[i].text;
4396
4397       ti->node_top = &volumes_simple;
4398       ti->sort_priority = value;
4399
4400       sprintf(identifier, "%d", value);
4401       sprintf(name, "%s", text);
4402
4403       setString(&ti->identifier, identifier);
4404       setString(&ti->name, name);
4405       setString(&ti->name_sorting, name);
4406       setString(&ti->infotext, "Sound Volume");
4407
4408       pushTreeInfo(&volumes_simple, ti);
4409
4410       if (value == setup.volume_simple)
4411         current_volume_simple_found = TRUE;
4412     }
4413
4414     if (!current_volume_simple_found)
4415     {
4416       // add entry for non-preset volume value
4417
4418       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4419       char identifier[32], name[32];
4420       int value = setup.volume_simple;
4421
4422       ti->node_top = &volumes_simple;
4423       ti->sort_priority = value;
4424
4425       sprintf(identifier, "%d", value);
4426       sprintf(name, "%d %% (Current)", value);
4427
4428       setString(&ti->identifier, identifier);
4429       setString(&ti->name, name);
4430       setString(&ti->name_sorting, name);
4431       setString(&ti->infotext, "Sound Volume");
4432
4433       pushTreeInfo(&volumes_simple, ti);
4434     }
4435
4436     /* sort volume values to start with lowest volume value */
4437     sortTreeInfo(&volumes_simple);
4438
4439     /* set current volume value to configured volume value */
4440     volume_simple_current =
4441       getTreeInfoFromIdentifier(volumes_simple,i_to_a(setup.volume_simple));
4442
4443     /* if that fails, set current volume to reliable default value */
4444     if (volume_simple_current == NULL)
4445       volume_simple_current =
4446         getTreeInfoFromIdentifier(volumes_simple, i_to_a(100));
4447
4448     /* if that also fails, set current volume to first available value */
4449     if (volume_simple_current == NULL)
4450       volume_simple_current = volumes_simple;
4451   }
4452
4453   if (volumes_loops == NULL)
4454   {
4455     boolean current_volume_loops_found = FALSE;
4456     int i;
4457
4458     for (i = 0; volumes_list[i].value != -1; i++)
4459     {
4460       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4461       char identifier[32], name[32];
4462       int value = volumes_list[i].value;
4463       char *text = volumes_list[i].text;
4464
4465       ti->node_top = &volumes_loops;
4466       ti->sort_priority = value;
4467
4468       sprintf(identifier, "%d", value);
4469       sprintf(name, "%s", text);
4470
4471       setString(&ti->identifier, identifier);
4472       setString(&ti->name, name);
4473       setString(&ti->name_sorting, name);
4474       setString(&ti->infotext, "Loops Volume");
4475
4476       pushTreeInfo(&volumes_loops, ti);
4477
4478       if (value == setup.volume_loops)
4479         current_volume_loops_found = TRUE;
4480     }
4481
4482     if (!current_volume_loops_found)
4483     {
4484       // add entry for non-preset volume value
4485
4486       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4487       char identifier[32], name[32];
4488       int value = setup.volume_loops;
4489
4490       ti->node_top = &volumes_loops;
4491       ti->sort_priority = value;
4492
4493       sprintf(identifier, "%d", value);
4494       sprintf(name, "%d %% (Current)", value);
4495
4496       setString(&ti->identifier, identifier);
4497       setString(&ti->name, name);
4498       setString(&ti->name_sorting, name);
4499       setString(&ti->infotext, "Loops Volume");
4500
4501       pushTreeInfo(&volumes_loops, ti);
4502     }
4503
4504     /* sort volume values to start with lowest volume value */
4505     sortTreeInfo(&volumes_loops);
4506
4507     /* set current volume value to configured volume value */
4508     volume_loops_current =
4509       getTreeInfoFromIdentifier(volumes_loops,i_to_a(setup.volume_loops));
4510
4511     /* if that fails, set current volume to reliable default value */
4512     if (volume_loops_current == NULL)
4513       volume_loops_current =
4514         getTreeInfoFromIdentifier(volumes_loops, i_to_a(100));
4515
4516     /* if that also fails, set current volume to first available value */
4517     if (volume_loops_current == NULL)
4518       volume_loops_current = volumes_loops;
4519   }
4520
4521   if (volumes_music == NULL)
4522   {
4523     boolean current_volume_music_found = FALSE;
4524     int i;
4525
4526     for (i = 0; volumes_list[i].value != -1; i++)
4527     {
4528       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4529       char identifier[32], name[32];
4530       int value = volumes_list[i].value;
4531       char *text = volumes_list[i].text;
4532
4533       ti->node_top = &volumes_music;
4534       ti->sort_priority = value;
4535
4536       sprintf(identifier, "%d", value);
4537       sprintf(name, "%s", text);
4538
4539       setString(&ti->identifier, identifier);
4540       setString(&ti->name, name);
4541       setString(&ti->name_sorting, name);
4542       setString(&ti->infotext, "Music Volume");
4543
4544       pushTreeInfo(&volumes_music, ti);
4545
4546       if (value == setup.volume_music)
4547         current_volume_music_found = TRUE;
4548     }
4549
4550     if (!current_volume_music_found)
4551     {
4552       // add entry for non-preset volume value
4553
4554       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4555       char identifier[32], name[32];
4556       int value = setup.volume_music;
4557
4558       ti->node_top = &volumes_music;
4559       ti->sort_priority = value;
4560
4561       sprintf(identifier, "%d", value);
4562       sprintf(name, "%d %% (Current)", value);
4563
4564       setString(&ti->identifier, identifier);
4565       setString(&ti->name, name);
4566       setString(&ti->name_sorting, name);
4567       setString(&ti->infotext, "Music Volume");
4568
4569       pushTreeInfo(&volumes_music, ti);
4570     }
4571
4572     /* sort volume values to start with lowest volume value */
4573     sortTreeInfo(&volumes_music);
4574
4575     /* set current volume value to configured volume value */
4576     volume_music_current =
4577       getTreeInfoFromIdentifier(volumes_music,i_to_a(setup.volume_music));
4578
4579     /* if that fails, set current volume to reliable default value */
4580     if (volume_music_current == NULL)
4581       volume_music_current =
4582         getTreeInfoFromIdentifier(volumes_music, i_to_a(100));
4583
4584     /* if that also fails, set current volume to first available value */
4585     if (volume_music_current == NULL)
4586       volume_music_current = volumes_music;
4587   }
4588
4589   setup.volume_simple = atoi(volume_simple_current->identifier);
4590   setup.volume_loops  = atoi(volume_loops_current->identifier);
4591   setup.volume_music  = atoi(volume_music_current->identifier);
4592
4593   /* needed for displaying volume text instead of identifier */
4594   volume_simple_text = volume_simple_current->name;
4595   volume_loops_text = volume_loops_current->name;
4596   volume_music_text = volume_music_current->name;
4597
4598   setup_mode = SETUP_MODE_SOUND;
4599
4600   DrawSetupScreen();
4601 }
4602
4603 static void execSetupChooseTouchControls()
4604 {
4605   setup_mode = SETUP_MODE_CHOOSE_TOUCH_CONTROL;
4606
4607   DrawSetupScreen();
4608 }
4609
4610 static void execSetupChooseMoveDistance()
4611 {
4612   setup_mode = SETUP_MODE_CHOOSE_MOVE_DISTANCE;
4613
4614   DrawSetupScreen();
4615 }
4616
4617 static void execSetupChooseDropDistance()
4618 {
4619   setup_mode = SETUP_MODE_CHOOSE_DROP_DISTANCE;
4620
4621   DrawSetupScreen();
4622 }
4623
4624 static void execSetupTouch()
4625 {
4626   if (touch_controls == NULL)
4627   {
4628     int i;
4629
4630     for (i = 0; touch_controls_list[i].value != NULL; i++)
4631     {
4632       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4633       char identifier[32], name[32];
4634       char *value = touch_controls_list[i].value;
4635       char *text = touch_controls_list[i].text;
4636
4637       ti->node_top = &touch_controls;
4638       ti->sort_priority = i;
4639
4640       sprintf(identifier, "%s", value);
4641       sprintf(name, "%s", text);
4642
4643       setString(&ti->identifier, identifier);
4644       setString(&ti->name, name);
4645       setString(&ti->name_sorting, name);
4646       setString(&ti->infotext, "Control Type");
4647
4648       pushTreeInfo(&touch_controls, ti);
4649     }
4650
4651     /* sort touch control values to start with lowest touch control value */
4652     sortTreeInfo(&touch_controls);
4653
4654     /* set current touch control value to configured touch control value */
4655     touch_control_current =
4656       getTreeInfoFromIdentifier(touch_controls, setup.touch.control_type);
4657
4658     /* if that fails, set current touch control to reliable default value */
4659     if (touch_control_current == NULL)
4660       touch_control_current =
4661         getTreeInfoFromIdentifier(touch_controls, TOUCH_CONTROL_DEFAULT);
4662
4663     /* if that also fails, set current touch control to first available value */
4664     if (touch_control_current == NULL)
4665       touch_control_current = touch_controls;
4666   }
4667
4668   if (move_distances == NULL)
4669   {
4670     int i;
4671
4672     for (i = 0; distances_list[i].value != -1; i++)
4673     {
4674       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4675       char identifier[32], name[32];
4676       int value = distances_list[i].value;
4677       char *text = distances_list[i].text;
4678
4679       ti->node_top = &move_distances;
4680       ti->sort_priority = value;
4681
4682       sprintf(identifier, "%d", value);
4683       sprintf(name, "%s", text);
4684
4685       setString(&ti->identifier, identifier);
4686       setString(&ti->name, name);
4687       setString(&ti->name_sorting, name);
4688       setString(&ti->infotext, "Move Distance");
4689
4690       pushTreeInfo(&move_distances, ti);
4691     }
4692
4693     /* sort distance values to start with lowest distance value */
4694     sortTreeInfo(&move_distances);
4695
4696     /* set current distance value to configured distance value */
4697     move_distance_current =
4698       getTreeInfoFromIdentifier(move_distances,
4699                                 i_to_a(setup.touch.move_distance));
4700
4701     /* if that fails, set current distance to reliable default value */
4702     if (move_distance_current == NULL)
4703       move_distance_current =
4704         getTreeInfoFromIdentifier(move_distances, i_to_a(1));
4705
4706     /* if that also fails, set current distance to first available value */
4707     if (move_distance_current == NULL)
4708       move_distance_current = move_distances;
4709   }
4710
4711   if (drop_distances == NULL)
4712   {
4713     int i;
4714
4715     for (i = 0; distances_list[i].value != -1; i++)
4716     {
4717       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4718       char identifier[32], name[32];
4719       int value = distances_list[i].value;
4720       char *text = distances_list[i].text;
4721
4722       ti->node_top = &drop_distances;
4723       ti->sort_priority = value;
4724
4725       sprintf(identifier, "%d", value);
4726       sprintf(name, "%s", text);
4727
4728       setString(&ti->identifier, identifier);
4729       setString(&ti->name, name);
4730       setString(&ti->name_sorting, name);
4731       setString(&ti->infotext, "Drop Distance");
4732
4733       pushTreeInfo(&drop_distances, ti);
4734     }
4735
4736     /* sort distance values to start with lowest distance value */
4737     sortTreeInfo(&drop_distances);
4738
4739     /* set current distance value to configured distance value */
4740     drop_distance_current =
4741       getTreeInfoFromIdentifier(drop_distances,
4742                                 i_to_a(setup.touch.drop_distance));
4743
4744     /* if that fails, set current distance to reliable default value */
4745     if (drop_distance_current == NULL)
4746       drop_distance_current =
4747         getTreeInfoFromIdentifier(drop_distances, i_to_a(1));
4748
4749     /* if that also fails, set current distance to first available value */
4750     if (drop_distance_current == NULL)
4751       drop_distance_current = drop_distances;
4752   }
4753
4754   setup.touch.control_type = touch_control_current->identifier;
4755   setup.touch.move_distance = atoi(move_distance_current->identifier);
4756   setup.touch.drop_distance = atoi(drop_distance_current->identifier);
4757
4758   /* needed for displaying volume text instead of identifier */
4759   touch_controls_text = touch_control_current->name;
4760   move_distance_text = move_distance_current->name;
4761   drop_distance_text = drop_distance_current->name;
4762
4763   setup_mode = SETUP_MODE_TOUCH;
4764
4765   DrawSetupScreen();
4766 }
4767
4768 static void execSetupArtwork()
4769 {
4770 #if 0
4771   printf("::: '%s', '%s', '%s'\n",
4772          artwork.gfx_current->subdir,
4773          artwork.gfx_current->fullpath,
4774          artwork.gfx_current->basepath);
4775 #endif
4776
4777   setup.graphics_set = artwork.gfx_current->identifier;
4778   setup.sounds_set = artwork.snd_current->identifier;
4779   setup.music_set = artwork.mus_current->identifier;
4780
4781   /* needed if last screen (setup choice) changed graphics, sounds or music */
4782   ReloadCustomArtwork(0);
4783
4784   /* needed for displaying artwork name instead of artwork identifier */
4785   graphics_set_name = artwork.gfx_current->name;
4786   sounds_set_name = artwork.snd_current->name;
4787   music_set_name = artwork.mus_current->name;
4788
4789   setup_mode = SETUP_MODE_ARTWORK;
4790
4791   DrawSetupScreen();
4792 }
4793
4794 static void execSetupChooseGraphics()
4795 {
4796   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
4797
4798   DrawSetupScreen();
4799 }
4800
4801 static void execSetupChooseSounds()
4802 {
4803   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
4804
4805   DrawSetupScreen();
4806 }
4807
4808 static void execSetupChooseMusic()
4809 {
4810   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
4811
4812   DrawSetupScreen();
4813 }
4814
4815 #if !defined(PLATFORM_ANDROID)
4816 static void execSetupInput()
4817 {
4818   setup_mode = SETUP_MODE_INPUT;
4819
4820   DrawSetupScreen();
4821 }
4822 #endif
4823
4824 static void execSetupShortcuts()
4825 {
4826   setup_mode = SETUP_MODE_SHORTCUTS;
4827
4828   DrawSetupScreen();
4829 }
4830
4831 static void execSetupShortcuts1()
4832 {
4833   setup_mode = SETUP_MODE_SHORTCUTS_1;
4834
4835   DrawSetupScreen();
4836 }
4837
4838 static void execSetupShortcuts2()
4839 {
4840   setup_mode = SETUP_MODE_SHORTCUTS_2;
4841
4842   DrawSetupScreen();
4843 }
4844
4845 static void execSetupShortcuts3()
4846 {
4847   setup_mode = SETUP_MODE_SHORTCUTS_3;
4848
4849   DrawSetupScreen();
4850 }
4851
4852 static void execSetupShortcuts4()
4853 {
4854   setup_mode = SETUP_MODE_SHORTCUTS_4;
4855
4856   DrawSetupScreen();
4857 }
4858
4859 static void execSetupShortcuts5()
4860 {
4861   setup_mode = SETUP_MODE_SHORTCUTS_5;
4862
4863   DrawSetupScreen();
4864 }
4865
4866 static void execExitSetup()
4867 {
4868   game_status = GAME_MODE_MAIN;
4869
4870   DrawMainMenuExt(REDRAW_FIELD, FALSE);
4871 }
4872
4873 static void execSaveAndExitSetup()
4874 {
4875   SaveSetup();
4876   execExitSetup();
4877 }
4878
4879 static struct TokenInfo setup_info_main[] =
4880 {
4881   { TYPE_ENTER_MENU,    execSetupGame,          "Game & Menu"           },
4882   { TYPE_ENTER_MENU,    execSetupEditor,        "Editor"                },
4883   { TYPE_ENTER_MENU,    execSetupGraphics,      "Graphics"              },
4884   { TYPE_ENTER_MENU,    execSetupSound,         "Sound & Music"         },
4885   { TYPE_ENTER_MENU,    execSetupArtwork,       "Custom Artwork"        },
4886 #if !defined(PLATFORM_ANDROID)
4887   { TYPE_ENTER_MENU,    execSetupInput,         "Input Devices"         },
4888   { TYPE_ENTER_MENU,    execSetupTouch,         "Touch Controls"        },
4889 #else
4890   { TYPE_ENTER_MENU,    execSetupTouch,         "Touch Controls"        },
4891 #endif
4892   { TYPE_ENTER_MENU,    execSetupShortcuts,     "Key Shortcuts"         },
4893   { TYPE_EMPTY,         NULL,                   ""                      },
4894   { TYPE_LEAVE_MENU,    execExitSetup,          "Exit"                  },
4895   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   "Save and Exit"         },
4896
4897   { 0,                  NULL,                   NULL                    }
4898 };
4899
4900 static struct TokenInfo setup_info_game[] =
4901 {
4902   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode (Multi-Player):" },
4903   { TYPE_YES_NO,        &setup.input_on_focus,  "Only Move Focussed Player:" },
4904   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
4905   { TYPE_SWITCH,        &setup.skip_levels,     "Skip Unsolved Levels:" },
4906   { TYPE_SWITCH,        &setup.time_limit,      "Time Limit:"           },
4907   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record Tapes:"    },
4908   { TYPE_ENTER_LIST,    execSetupChooseGameSpeed, "Game Speed:"         },
4909   { TYPE_STRING,        &game_speed_text,       ""                      },
4910 #if 1
4911   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay:"     },
4912   { TYPE_STRING,        &scroll_delay_text,     ""                      },
4913 #endif
4914   { TYPE_EMPTY,         NULL,                   ""                      },
4915   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4916
4917   { 0,                  NULL,                   NULL                    }
4918 };
4919
4920 static struct TokenInfo setup_info_editor[] =
4921 {
4922 #if 0
4923   { TYPE_SWITCH,        &setup.editor.el_boulderdash,   "Boulder Dash:" },
4924   { TYPE_SWITCH,        &setup.editor.el_emerald_mine,  "Emerald Mine:" },
4925   { TYPE_SWITCH, &setup.editor.el_emerald_mine_club,    "Emerald Mine Club:" },
4926   { TYPE_SWITCH,        &setup.editor.el_more,          "Rocks'n'Diamonds:" },
4927   { TYPE_SWITCH,        &setup.editor.el_sokoban,       "Sokoban:"      },
4928   { TYPE_SWITCH,        &setup.editor.el_supaplex,      "Supaplex:"     },
4929   { TYPE_SWITCH,        &setup.editor.el_diamond_caves, "Diamond Caves II:" },
4930   { TYPE_SWITCH,        &setup.editor.el_dx_boulderdash,"DX-Boulderdash:" },
4931 #endif
4932   { TYPE_SWITCH,        &setup.editor.el_chars,         "Text Characters:" },
4933   { TYPE_SWITCH, &setup.editor.el_steel_chars, "Text Characters (Steel):" },
4934   { TYPE_SWITCH,        &setup.editor.el_custom,  "Custom & Group Elements:" },
4935 #if 0
4936   { TYPE_SWITCH,        &setup.editor.el_headlines,     "Headlines:"    },
4937 #endif
4938   { TYPE_SWITCH, &setup.editor.el_user_defined, "User defined element list:" },
4939   { TYPE_SWITCH,        &setup.editor.el_dynamic,  "Dynamic level elements:" },
4940   { TYPE_EMPTY,         NULL,                   ""                      },
4941 #if 0
4942   { TYPE_SWITCH,        &setup.editor.el_by_game,   "Show elements by game:" },
4943   { TYPE_SWITCH,        &setup.editor.el_by_type,   "Show elements by type:" },
4944   { TYPE_EMPTY,         NULL,                   ""                      },
4945 #endif
4946   { TYPE_SWITCH, &setup.editor.show_element_token,      "Show element token:" },
4947   { TYPE_EMPTY,         NULL,                   ""                      },
4948   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4949
4950   { 0,                  NULL,                   NULL                    }
4951 };
4952
4953 static struct TokenInfo setup_info_graphics[] =
4954 {
4955 #if !defined(PLATFORM_ANDROID)
4956   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen:"           },
4957 #if defined(TARGET_SDL2)
4958   { TYPE_ENTER_LIST,    execSetupChooseWindowSize, "Window Scaling:"    },
4959   { TYPE_STRING,        &window_size_text,      ""                      },
4960   { TYPE_ENTER_LIST,    execSetupChooseScalingType, "Anti-Aliasing:"    },
4961   { TYPE_STRING,        &scaling_type_text,     ""                      },
4962 #else
4963   { TYPE_ENTER_LIST,    execSetupChooseScreenMode, "Fullscreen Mode:"   },
4964   { TYPE_STRING,        &screen_mode_text,      ""                      },
4965 #endif
4966 #endif
4967 #if 0
4968   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay:"     },
4969   { TYPE_STRING,        &scroll_delay_text,     ""                      },
4970 #endif
4971   { TYPE_SWITCH,        &setup.fade_screens,    "Fade Screens:"         },
4972   { TYPE_SWITCH,        &setup.quick_switch,    "Quick Player Focus Switch:" },
4973   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Menu Doors:"     },
4974   { TYPE_SWITCH,        &setup.show_titlescreen,"Show Title Screens:"   },
4975   { TYPE_SWITCH,        &setup.toons,           "Show Toons:"           },
4976   { TYPE_ECS_AGA,       &setup.prefer_aga_graphics,"EMC graphics preference:" },
4977   { TYPE_SWITCH, &setup.sp_show_border_elements,"Supaplex Border Elements:" },
4978   { TYPE_SWITCH,        &setup.small_game_graphics, "Small Game Graphics:" },
4979   { TYPE_EMPTY,         NULL,                   ""                      },
4980   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4981
4982   { 0,                  NULL,                   NULL                    }
4983 };
4984
4985 static struct TokenInfo setup_info_sound[] =
4986 {
4987   { TYPE_SWITCH,        &setup.sound_simple,    "Sound Effects (Normal):"  },
4988   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Effects (Looping):" },
4989   { TYPE_SWITCH,        &setup.sound_music,     "Music:"                },
4990   { TYPE_EMPTY,         NULL,                   ""                      },
4991   { TYPE_ENTER_LIST,    execSetupChooseVolumeSimple, "Sound Volume (Normal):" },
4992   { TYPE_STRING,        &volume_simple_text,    ""                      },
4993   { TYPE_ENTER_LIST,    execSetupChooseVolumeLoops, "Sound Volume (Looping):" },
4994   { TYPE_STRING,        &volume_loops_text,     ""                      },
4995   { TYPE_ENTER_LIST,    execSetupChooseVolumeMusic, "Music Volume:"     },
4996   { TYPE_STRING,        &volume_music_text,     ""                      },
4997   { TYPE_EMPTY,         NULL,                   ""                      },
4998   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4999
5000   { 0,                  NULL,                   NULL                    }
5001 };
5002
5003 static struct TokenInfo setup_info_artwork[] =
5004 {
5005   { TYPE_ENTER_LIST,    execSetupChooseGraphics,"Custom Graphics:"      },
5006   { TYPE_STRING,        &graphics_set_name,     ""                      },
5007   { TYPE_ENTER_LIST,    execSetupChooseSounds,  "Custom Sounds:"        },
5008   { TYPE_STRING,        &sounds_set_name,       ""                      },
5009   { TYPE_ENTER_LIST,    execSetupChooseMusic,   "Custom Music:"         },
5010   { TYPE_STRING,        &music_set_name,        ""                      },
5011   { TYPE_EMPTY,         NULL,                   ""                      },
5012   { TYPE_YES_NO_AUTO,&setup.override_level_graphics,"Override Level Graphics:"},
5013   { TYPE_YES_NO_AUTO,&setup.override_level_sounds,  "Override Level Sounds:"  },
5014   { TYPE_YES_NO_AUTO,&setup.override_level_music,   "Override Level Music:"   },
5015   { TYPE_EMPTY,         NULL,                   ""                      },
5016   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5017
5018   { 0,                  NULL,                   NULL                    }
5019 };
5020
5021 static struct TokenInfo setup_info_input[] =
5022 {
5023   { TYPE_SWITCH,        NULL,                   "Player:"               },
5024   { TYPE_SWITCH,        NULL,                   "Device:"               },
5025   { TYPE_ENTER_MENU,    NULL,                   ""                      },
5026   { TYPE_EMPTY,         NULL,                   ""                      },
5027   { TYPE_EMPTY,         NULL,                   ""                      },
5028   { TYPE_EMPTY,         NULL,                   ""                      },
5029   { TYPE_EMPTY,         NULL,                   ""                      },
5030   { TYPE_EMPTY,         NULL,                   ""                      },
5031   { TYPE_EMPTY,         NULL,                   ""                      },
5032   { TYPE_EMPTY,         NULL,                   ""                      },
5033   { TYPE_EMPTY,         NULL,                   ""                      },
5034   { TYPE_EMPTY,         NULL,                   ""                      },
5035   { TYPE_EMPTY,         NULL,                   ""                      },
5036   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5037
5038   { 0,                  NULL,                   NULL                    }
5039 };
5040
5041 static struct TokenInfo setup_info_touch[] =
5042 {
5043   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
5044   { TYPE_STRING,        &touch_controls_text,   ""                      },
5045   { TYPE_EMPTY,         NULL,                   ""                      },
5046   { TYPE_ENTER_LIST,    execSetupChooseMoveDistance, "Move Trigger Distance:" },
5047   { TYPE_STRING,        &move_distance_text,    ""                      },
5048   { TYPE_ENTER_LIST,    execSetupChooseDropDistance, "Drop Trigger Distance:" },
5049   { TYPE_STRING,        &drop_distance_text,    ""                      },
5050   { TYPE_EMPTY,         NULL,                   ""                      },
5051   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5052
5053   { 0,                  NULL,                   NULL                    }
5054 };
5055
5056 static struct TokenInfo setup_info_shortcuts[] =
5057 {
5058   { TYPE_ENTER_MENU,    execSetupShortcuts1,    "Various Keys"          },
5059   { TYPE_ENTER_MENU,    execSetupShortcuts2,    "Player Focus"          },
5060   { TYPE_ENTER_MENU,    execSetupShortcuts3,    "Tape Buttons"          },
5061   { TYPE_ENTER_MENU,    execSetupShortcuts4,    "Sound & Music"         },
5062   { TYPE_ENTER_MENU,    execSetupShortcuts5,    "TAS Snap Keys"         },
5063   { TYPE_EMPTY,         NULL,                   ""                      },
5064   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5065
5066   { 0,                  NULL,                   NULL                    }
5067 };
5068
5069 static struct TokenInfo setup_info_shortcuts_1[] =
5070 {
5071   { TYPE_KEYTEXT,       NULL,           "Quick Save Game to Tape:",     },
5072   { TYPE_KEY,           &setup.shortcut.save_game, ""                   },
5073   { TYPE_KEYTEXT,       NULL,           "Quick Load Game from Tape:",   },
5074   { TYPE_KEY,           &setup.shortcut.load_game, ""                   },
5075   { TYPE_KEYTEXT,       NULL,           "Start Game & Toggle Pause:",   },
5076   { TYPE_KEY,           &setup.shortcut.toggle_pause, ""                },
5077   { TYPE_EMPTY,         NULL,                   ""                      },
5078   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on 'Esc' Key:"     },
5079   { TYPE_YES_NO, &setup.ask_on_escape_editor,   "Ask on 'Esc' Key (Editor):" },
5080   { TYPE_EMPTY,         NULL,                   ""                      },
5081   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
5082
5083   { 0,                  NULL,                   NULL                    }
5084 };
5085
5086 static struct TokenInfo setup_info_shortcuts_2[] =
5087 {
5088   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 1:",       },
5089   { TYPE_KEY,           &setup.shortcut.focus_player[0], ""             },
5090   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 2:",       },
5091   { TYPE_KEY,           &setup.shortcut.focus_player[1], ""             },
5092   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 3:",       },
5093   { TYPE_KEY,           &setup.shortcut.focus_player[2], ""             },
5094   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 4:",       },
5095   { TYPE_KEY,           &setup.shortcut.focus_player[3], ""             },
5096   { TYPE_KEYTEXT,       NULL,           "Set Focus to All Players:",    },
5097   { TYPE_KEY,           &setup.shortcut.focus_player_all, ""            },
5098   { TYPE_EMPTY,         NULL,                   ""                      },
5099   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
5100
5101   { 0,                  NULL,                   NULL                    }
5102 };
5103
5104 static struct TokenInfo setup_info_shortcuts_3[] =
5105 {
5106   { TYPE_KEYTEXT,       NULL,                   "Eject Tape:",          },
5107   { TYPE_KEY,           &setup.shortcut.tape_eject, ""                  },
5108   { TYPE_KEYTEXT,       NULL,                   "Warp / Single Step:",  },
5109   { TYPE_KEY,           &setup.shortcut.tape_extra, ""                  },
5110   { TYPE_KEYTEXT,       NULL,                   "Stop Tape:",           },
5111   { TYPE_KEY,           &setup.shortcut.tape_stop, ""                   },
5112   { TYPE_KEYTEXT,       NULL,                   "Pause / Unpause Tape:",},
5113   { TYPE_KEY,           &setup.shortcut.tape_pause, ""                  },
5114   { TYPE_KEYTEXT,       NULL,                   "Record Tape:",         },
5115   { TYPE_KEY,           &setup.shortcut.tape_record, ""                 },
5116   { TYPE_KEYTEXT,       NULL,                   "Play Tape:",           },
5117   { TYPE_KEY,           &setup.shortcut.tape_play, ""                   },
5118   { TYPE_EMPTY,         NULL,                   ""                      },
5119   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
5120
5121   { 0,                  NULL,                   NULL                    }
5122 };
5123
5124 static struct TokenInfo setup_info_shortcuts_4[] =
5125 {
5126   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Normal):", },
5127   { TYPE_KEY,           &setup.shortcut.sound_simple, ""                },
5128   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Looping):", },
5129   { TYPE_KEY,           &setup.shortcut.sound_loops, ""                 },
5130   { TYPE_KEYTEXT,       NULL,           "Toggle Music:",                },
5131   { TYPE_KEY,           &setup.shortcut.sound_music, ""                 },
5132   { TYPE_EMPTY,         NULL,                   ""                      },
5133   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
5134
5135   { 0,                  NULL,                   NULL                    }
5136 };
5137
5138 static struct TokenInfo setup_info_shortcuts_5[] =
5139 {
5140   { TYPE_KEYTEXT,       NULL,                   "Snap Left:",           },
5141   { TYPE_KEY,           &setup.shortcut.snap_left, ""                   },
5142   { TYPE_KEYTEXT,       NULL,                   "Snap Right:",          },
5143   { TYPE_KEY,           &setup.shortcut.snap_right, ""                  },
5144   { TYPE_KEYTEXT,       NULL,                   "Snap Up:",             },
5145   { TYPE_KEY,           &setup.shortcut.snap_up, ""                     },
5146   { TYPE_KEYTEXT,       NULL,                   "Snap Down:",           },
5147   { TYPE_KEY,           &setup.shortcut.snap_down, ""                   },
5148   { TYPE_EMPTY,         NULL,                   ""                      },
5149   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
5150
5151   { 0,                  NULL,                   NULL                    }
5152 };
5153
5154 static Key getSetupKey()
5155 {
5156   Key key = KSYM_UNDEFINED;
5157   boolean got_key_event = FALSE;
5158
5159   while (!got_key_event)
5160   {
5161     if (PendingEvent())         /* got event */
5162     {
5163       Event event;
5164
5165       NextEvent(&event);
5166
5167       switch (event.type)
5168       {
5169         case EVENT_KEYPRESS:
5170           {
5171             key = GetEventKey((KeyEvent *)&event, TRUE);
5172
5173             /* press 'Escape' or 'Enter' to keep the existing key binding */
5174             if (key == KSYM_Escape || key == KSYM_Return)
5175               key = KSYM_UNDEFINED;     /* keep old value */
5176
5177             got_key_event = TRUE;
5178           }
5179           break;
5180
5181         case EVENT_KEYRELEASE:
5182           key_joystick_mapping = 0;
5183           break;
5184
5185         default:
5186           HandleOtherEvents(&event);
5187           break;
5188       }
5189     }
5190
5191     DoAnimation();
5192     BackToFront();
5193
5194     /* don't eat all CPU time */
5195     Delay(10);
5196   }
5197
5198   return key;
5199 }
5200
5201 static int getSetupTextFont(int type)
5202 {
5203   if (type & (TYPE_SWITCH       |
5204               TYPE_YES_NO       |
5205               TYPE_YES_NO_AUTO  |
5206               TYPE_STRING       |
5207               TYPE_ECS_AGA      |
5208               TYPE_KEYTEXT      |
5209               TYPE_ENTER_LIST))
5210     return FONT_MENU_2;
5211   else
5212     return FONT_MENU_1;
5213 }
5214
5215 static int getSetupValueFont(int type, void *value)
5216 {
5217   if (type & TYPE_KEY)
5218     return (type & TYPE_QUERY ? FONT_INPUT_1_ACTIVE : FONT_VALUE_1);
5219   else if (type & TYPE_STRING)
5220     return FONT_VALUE_2;
5221   else if (type & TYPE_ECS_AGA)
5222     return FONT_VALUE_1;
5223   else if (type & TYPE_BOOLEAN_STYLE)
5224     return (*(boolean *)value ? FONT_OPTION_ON : FONT_OPTION_OFF);
5225   else if (type & TYPE_YES_NO_AUTO)
5226     return (*(int *)value == AUTO  ? FONT_OPTION_ON :
5227             *(int *)value == FALSE ? FONT_OPTION_OFF : FONT_OPTION_ON);
5228   else
5229     return FONT_VALUE_1;
5230 }
5231
5232 static void drawSetupValue(int screen_pos, int setup_info_pos_raw)
5233 {
5234   int si_pos = (setup_info_pos_raw < 0 ? screen_pos : setup_info_pos_raw);
5235   struct TokenInfo *si = &setup_info[si_pos];
5236   boolean font_draw_xoffset_modified = FALSE;
5237   int font_draw_xoffset_old = -1;
5238   int xoffset = (num_setup_info < max_setup_info ? -1 : 0);
5239   int menu_screen_value_xpos = MENU_SCREEN_VALUE_XPOS + xoffset;
5240   int menu_screen_max_xpos = MENU_SCREEN_MAX_XPOS + xoffset;
5241   int xpos = menu_screen_value_xpos;
5242   int ypos = MENU_SCREEN_START_YPOS + screen_pos;
5243   int startx = mSX + xpos * 32;
5244   int starty = mSY + ypos * 32;
5245   int font_nr, font_width;
5246   int type = si->type;
5247   void *value = si->value;
5248   char *value_string = getSetupValue(type, value);
5249   int i;
5250
5251   if (value_string == NULL)
5252     return;
5253
5254   if (type & TYPE_KEY)
5255   {
5256     xpos = MENU_SCREEN_START_XPOS;
5257
5258     if (type & TYPE_QUERY)
5259       value_string = "<press key>";
5260   }
5261   else if (type & TYPE_STRING)
5262   {
5263     int max_value_len = (SCR_FIELDX - 2) * 2;
5264
5265     xpos = MENU_SCREEN_START_XPOS;
5266
5267     if (strlen(value_string) > max_value_len)
5268       value_string[max_value_len] = '\0';
5269   }
5270   else if (type & TYPE_YES_NO_AUTO)
5271   {
5272     xpos = menu_screen_value_xpos - 1;
5273   }
5274
5275   startx = mSX + xpos * 32;
5276   starty = mSY + ypos * 32;
5277   font_nr = getSetupValueFont(type, value);
5278   font_width = getFontWidth(font_nr);
5279
5280   /* downward compatibility correction for Juergen Bonhagen's menu settings */
5281   if (setup_mode != SETUP_MODE_INPUT)
5282   {
5283     int max_menu_text_length_big = (menu_screen_value_xpos -
5284                                     MENU_SCREEN_START_XPOS);
5285     int max_menu_text_length_medium = max_menu_text_length_big * 2;
5286     int check_font_nr = FONT_OPTION_ON; /* known font that needs correction */
5287     int font1_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
5288     int font2_xoffset = getFontBitmapInfo(check_font_nr)->draw_xoffset;
5289     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
5290     int text_font_nr = getSetupTextFont(FONT_MENU_2);
5291     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
5292     int text_width = max_menu_text_length_medium * getFontWidth(text_font_nr);
5293     boolean correct_font_draw_xoffset = FALSE;
5294
5295     if (xpos == MENU_SCREEN_START_XPOS &&
5296         startx + font1_xoffset < text_startx + text_font_xoffset)
5297       correct_font_draw_xoffset = TRUE;
5298
5299     if (xpos == menu_screen_value_xpos &&
5300         startx + font2_xoffset < text_startx + text_width + text_font_xoffset)
5301       correct_font_draw_xoffset = TRUE;
5302
5303     /* check if setup value would overlap with setup text when printed */
5304     /* (this can happen for extreme/wrong values for font draw offset) */
5305     if (correct_font_draw_xoffset)
5306     {
5307       font_draw_xoffset_old = getFontBitmapInfo(font_nr)->draw_xoffset;
5308       font_draw_xoffset_modified = TRUE;
5309
5310       if (type & TYPE_KEY)
5311         getFontBitmapInfo(font_nr)->draw_xoffset += 2 * getFontWidth(font_nr);
5312       else if (!(type & TYPE_STRING))
5313         getFontBitmapInfo(font_nr)->draw_xoffset = text_font_xoffset + 20 -
5314           max_menu_text_length_medium * (16 - getFontWidth(text_font_nr));
5315     }
5316   }
5317
5318   for (i = 0; i <= menu_screen_max_xpos - xpos; i++)
5319     DrawText(startx + i * font_width, starty, " ", font_nr);
5320
5321   DrawText(startx, starty, value_string, font_nr);
5322
5323   if (font_draw_xoffset_modified)
5324     getFontBitmapInfo(font_nr)->draw_xoffset = font_draw_xoffset_old;
5325 }
5326
5327 static void changeSetupValue(int screen_pos, int setup_info_pos_raw, int dx)
5328 {
5329   int si_pos = (setup_info_pos_raw < 0 ? screen_pos : setup_info_pos_raw);
5330   struct TokenInfo *si = &setup_info[si_pos];
5331
5332   if (si->type & TYPE_BOOLEAN_STYLE)
5333   {
5334     *(boolean *)si->value ^= TRUE;
5335   }
5336   else if (si->type & TYPE_YES_NO_AUTO)
5337   {
5338     *(int *)si->value =
5339       (dx == -1 ?
5340        (*(int *)si->value == AUTO ? TRUE :
5341         *(int *)si->value == TRUE ? FALSE : AUTO) :
5342        (*(int *)si->value == TRUE ? AUTO :
5343         *(int *)si->value == AUTO ? FALSE : TRUE));
5344   }
5345   else if (si->type & TYPE_KEY)
5346   {
5347     Key key;
5348
5349     si->type |= TYPE_QUERY;
5350     drawSetupValue(screen_pos, setup_info_pos_raw);
5351     si->type &= ~TYPE_QUERY;
5352
5353     key = getSetupKey();
5354     if (key != KSYM_UNDEFINED)
5355       *(Key *)si->value = key;
5356   }
5357
5358   drawSetupValue(screen_pos, setup_info_pos_raw);
5359
5360   // fullscreen state may have changed at this point
5361   if (si->value == &setup.fullscreen)
5362     ToggleFullscreenOrChangeWindowScalingIfNeeded();
5363 }
5364
5365 static void DrawCursorAndText_Setup(int screen_pos, int setup_info_pos_raw,
5366                                     boolean active)
5367 {
5368   int si_pos = (setup_info_pos_raw < 0 ? screen_pos : setup_info_pos_raw);
5369   struct TokenInfo *si = &setup_info[si_pos];
5370   int xpos = MENU_SCREEN_START_XPOS;
5371   int ypos = MENU_SCREEN_START_YPOS + screen_pos;
5372   int font_nr = getSetupTextFont(si->type);
5373
5374   if (setup_info == setup_info_input)
5375     font_nr = FONT_MENU_1;
5376
5377   if (active)
5378     font_nr = FONT_ACTIVE(font_nr);
5379
5380   DrawText(mSX + xpos * 32, mSY + ypos * 32, si->text, font_nr);
5381
5382   if (si->type & ~TYPE_SKIP_ENTRY)
5383     drawCursor(screen_pos, active);
5384 }
5385
5386 static void drawSetupInfoList(struct TokenInfo *setup_info,
5387                               int first_entry, int num_page_entries)
5388 {
5389   int i;
5390
5391   if (num_page_entries > NUM_MENU_ENTRIES_ON_SCREEN)
5392     num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
5393
5394   if (num_page_entries > max_setup_info)
5395     num_page_entries = max_setup_info;
5396
5397   if (first_entry + num_page_entries > max_setup_info)
5398     first_entry = 0;
5399
5400   clearMenuListArea();
5401
5402   for (i = 0; i < num_page_entries; i++)
5403   {
5404     int setup_info_pos = first_entry + i;
5405     struct TokenInfo *si = &setup_info[setup_info_pos];
5406     void *value_ptr = si->value;
5407
5408     /* set some entries to "unchangeable" according to other variables */
5409     if ((value_ptr == &setup.sound_simple && !audio.sound_available) ||
5410         (value_ptr == &setup.sound_loops  && !audio.loops_available) ||
5411         (value_ptr == &setup.sound_music  && !audio.music_available) ||
5412         (value_ptr == &setup.fullscreen   && !video.fullscreen_available) ||
5413         (value_ptr == &screen_mode_text   && !video.fullscreen_available) ||
5414         (value_ptr == &window_size_text   && !video.window_scaling_available) ||
5415         (value_ptr == &scaling_type_text  && !video.window_scaling_available))
5416       si->type |= TYPE_GHOSTED;
5417
5418     if (si->type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
5419       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
5420     else if (si->type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
5421       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
5422     else if (si->type & ~TYPE_SKIP_ENTRY)
5423       initCursor(i, IMG_MENU_BUTTON);
5424
5425     DrawCursorAndText_Setup(i, setup_info_pos, FALSE);
5426
5427     if (si->type & TYPE_VALUE)
5428       drawSetupValue(i, setup_info_pos);
5429   }
5430 }
5431
5432 static void DrawSetupScreen_Generic()
5433 {
5434   int fade_mask = (DrawingAreaChanged() ? REDRAW_ALL : REDRAW_FIELD);
5435   boolean redraw_all = FALSE;
5436   char *title_string = NULL;
5437   int i;
5438
5439   UnmapAllGadgets();
5440
5441   FreeScreenGadgets();
5442   CreateScreenGadgets();
5443
5444   CloseDoor(DOOR_CLOSE_2);
5445
5446   if (redraw_mask & REDRAW_ALL)
5447     redraw_all = TRUE;
5448
5449   FadeOut(fade_mask);
5450
5451   ClearField();
5452
5453   if (setup_mode == SETUP_MODE_MAIN)
5454   {
5455     setup_info = setup_info_main;
5456     title_string = "Setup";
5457   }
5458   else if (setup_mode == SETUP_MODE_GAME)
5459   {
5460     setup_info = setup_info_game;
5461     title_string = "Setup Game";
5462   }
5463   else if (setup_mode == SETUP_MODE_EDITOR)
5464   {
5465     setup_info = setup_info_editor;
5466     title_string = "Setup Editor";
5467   }
5468   else if (setup_mode == SETUP_MODE_GRAPHICS)
5469   {
5470     setup_info = setup_info_graphics;
5471     title_string = "Setup Graphics";
5472   }
5473   else if (setup_mode == SETUP_MODE_SOUND)
5474   {
5475     setup_info = setup_info_sound;
5476     title_string = "Setup Sound";
5477   }
5478   else if (setup_mode == SETUP_MODE_ARTWORK)
5479   {
5480     setup_info = setup_info_artwork;
5481     title_string = "Custom Artwork";
5482   }
5483   else if (setup_mode == SETUP_MODE_TOUCH)
5484   {
5485     setup_info = setup_info_touch;
5486     title_string = "Setup Touch Ctrls";
5487   }
5488   else if (setup_mode == SETUP_MODE_SHORTCUTS)
5489   {
5490     setup_info = setup_info_shortcuts;
5491     title_string = "Setup Shortcuts";
5492   }
5493   else if (setup_mode == SETUP_MODE_SHORTCUTS_1)
5494   {
5495     setup_info = setup_info_shortcuts_1;
5496     title_string = "Setup Shortcuts";
5497   }
5498   else if (setup_mode == SETUP_MODE_SHORTCUTS_2)
5499   {
5500     setup_info = setup_info_shortcuts_2;
5501     title_string = "Setup Shortcuts";
5502   }
5503   else if (setup_mode == SETUP_MODE_SHORTCUTS_3)
5504   {
5505     setup_info = setup_info_shortcuts_3;
5506     title_string = "Setup Shortcuts";
5507   }
5508   else if (setup_mode == SETUP_MODE_SHORTCUTS_4)
5509   {
5510     setup_info = setup_info_shortcuts_4;
5511     title_string = "Setup Shortcuts";
5512   }
5513   else if (setup_mode == SETUP_MODE_SHORTCUTS_5)
5514   {
5515     setup_info = setup_info_shortcuts_5;
5516     title_string = "Setup Shortcuts";
5517   }
5518
5519   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, title_string);
5520
5521   // determine maximal number of setup entries that can be displayed on screen
5522   num_setup_info = 0;
5523   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
5524     num_setup_info++;
5525
5526   // determine maximal number of setup entries available for this setup screen
5527   max_setup_info = 0;
5528   for (i = 0; setup_info[i].type != 0; i++)
5529     max_setup_info++;
5530
5531   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
5532
5533   MapScreenGadgets(max_setup_info);
5534
5535   if (redraw_all)
5536     redraw_mask = fade_mask = REDRAW_ALL;
5537
5538   FadeIn(fade_mask);
5539
5540   InitAnimation();
5541 }
5542
5543 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
5544 {
5545   static int choice_store[MAX_SETUP_MODES];
5546   static int first_entry_store[MAX_SETUP_MODES];
5547   int choice = choice_store[setup_mode];                /* starts with 0 */
5548   int first_entry = first_entry_store[setup_mode];      /* starts with 0 */
5549   int x = 0;
5550   int y = choice - first_entry;
5551   int y_old = y;
5552   boolean position_set_by_scrollbar = (dx == 999);
5553   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
5554   int num_page_entries;
5555
5556   num_page_entries = MIN(max_setup_info, NUM_MENU_ENTRIES_ON_SCREEN);
5557
5558   if (button == MB_MENU_INITIALIZE)
5559   {
5560     /* advance to first valid menu entry */
5561     while (choice < num_setup_info &&
5562            setup_info[choice].type & TYPE_SKIP_ENTRY)
5563       choice++;
5564
5565     if (position_set_by_scrollbar)
5566       first_entry = first_entry_store[setup_mode] = dy;
5567     else
5568       AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_setup_info,
5569                       NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
5570
5571     drawSetupInfoList(setup_info, first_entry, NUM_MENU_ENTRIES_ON_SCREEN);
5572
5573     if (choice < first_entry)
5574     {
5575       choice = first_entry;
5576
5577       if (setup_info[choice].type & TYPE_SKIP_ENTRY)
5578         choice++;
5579     }
5580     else if (choice > first_entry + num_page_entries - 1)
5581     {
5582       choice = first_entry + num_page_entries - 1;
5583
5584       if (setup_info[choice].type & TYPE_SKIP_ENTRY)
5585         choice--;
5586     }
5587
5588     choice_store[setup_mode] = choice;
5589
5590     DrawCursorAndText_Setup(choice - first_entry, choice, TRUE);
5591
5592     return;
5593   }
5594   else if (button == MB_MENU_LEAVE)
5595   {
5596     int i;
5597
5598     PlaySound(SND_MENU_ITEM_SELECTING);
5599
5600     for (i = 0; setup_info[i].type != 0; i++)
5601     {
5602       if (setup_info[i].type & TYPE_LEAVE_MENU)
5603       {
5604         void (*menu_callback_function)(void) = setup_info[i].value;
5605
5606         FadeSetLeaveMenu();
5607
5608         menu_callback_function();
5609
5610         break;  /* absolutely needed because function changes 'setup_info'! */
5611       }
5612     }
5613
5614     return;
5615   }
5616
5617   if (mx || my)         /* mouse input */
5618   {
5619     x = (mx - mSX) / 32;
5620     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
5621   }
5622   else if (dx || dy)    /* keyboard or scrollbar/scrollbutton input */
5623   {
5624     /* move cursor instead of scrolling when already at start/end of list */
5625     if (dy == -1 * SCROLL_LINE && first_entry == 0)
5626       dy = -1;
5627     else if (dy == +1 * SCROLL_LINE &&
5628              first_entry + num_page_entries == max_setup_info)
5629       dy = 1;
5630
5631     /* handle scrolling screen one line or page */
5632     if (y + dy < 0 ||
5633         y + dy > num_page_entries - 1)
5634     {
5635       boolean redraw = FALSE;
5636
5637       if (ABS(dy) == SCROLL_PAGE)
5638         step = num_page_entries - 1;
5639
5640       if (dy < 0 && first_entry > 0)
5641       {
5642         /* scroll page/line up */
5643
5644         first_entry -= step;
5645         if (first_entry < 0)
5646           first_entry = 0;
5647
5648         redraw = TRUE;
5649       }
5650       else if (dy > 0 && first_entry + num_page_entries < max_setup_info)
5651       {
5652         /* scroll page/line down */
5653
5654         first_entry += step;
5655         if (first_entry + num_page_entries > max_setup_info)
5656           first_entry = MAX(0, max_setup_info - num_page_entries);
5657
5658         redraw = TRUE;
5659       }
5660
5661       if (redraw)
5662       {
5663         choice += first_entry - first_entry_store[setup_mode];
5664
5665         if (choice < first_entry)
5666         {
5667           choice = first_entry;
5668
5669           if (setup_info[choice].type & TYPE_SKIP_ENTRY)
5670             choice++;
5671         }
5672         else if (choice > first_entry + num_page_entries - 1)
5673         {
5674           choice = first_entry + num_page_entries - 1;
5675
5676           if (setup_info[choice].type & TYPE_SKIP_ENTRY)
5677             choice--;
5678         }
5679         else if (setup_info[choice].type & TYPE_SKIP_ENTRY)
5680         {
5681           choice += SIGN(dy);
5682
5683           if (choice < first_entry ||
5684               choice > first_entry + num_page_entries - 1)
5685           first_entry += SIGN(dy);
5686         }
5687
5688         first_entry_store[setup_mode] = first_entry;
5689         choice_store[setup_mode] = choice;
5690
5691         drawSetupInfoList(setup_info, first_entry, NUM_MENU_ENTRIES_ON_SCREEN);
5692
5693         DrawCursorAndText_Setup(choice - first_entry, choice, TRUE);
5694
5695         AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_setup_info,
5696                         NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
5697       }
5698
5699       return;
5700     }
5701
5702     if (dx)
5703     {
5704       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE : TYPE_ENTER);
5705
5706       if (setup_info[choice].type & menu_navigation_type ||
5707           setup_info[choice].type & TYPE_BOOLEAN_STYLE ||
5708           setup_info[choice].type & TYPE_YES_NO_AUTO)
5709         button = MB_MENU_CHOICE;
5710     }
5711     else if (dy)
5712       y += dy;
5713
5714     /* jump to next non-empty menu entry (up or down) */
5715     while (first_entry + y > 0 &&
5716            first_entry + y < max_setup_info - 1 &&
5717            setup_info[first_entry + y].type & TYPE_SKIP_ENTRY)
5718       y += dy;
5719
5720     if (!IN_VIS_MENU(x, y))
5721     {
5722       choice += y - y_old;
5723
5724       if (choice < first_entry)
5725         first_entry = choice;
5726       else if (choice > first_entry + num_page_entries - 1)
5727         first_entry = choice - num_page_entries + 1;
5728
5729       if (first_entry >= 0 &&
5730           first_entry + num_page_entries <= max_setup_info)
5731       {
5732         first_entry_store[setup_mode] = first_entry;
5733
5734         if (choice < first_entry)
5735           choice = first_entry;
5736         else if (choice > first_entry + num_page_entries - 1)
5737           choice = first_entry + num_page_entries - 1;
5738
5739         choice_store[setup_mode] = choice;
5740
5741         drawSetupInfoList(setup_info, first_entry, NUM_MENU_ENTRIES_ON_SCREEN);
5742
5743         DrawCursorAndText_Setup(choice - first_entry, choice, TRUE);
5744
5745         AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_setup_info,
5746                         NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
5747       }
5748
5749       return;
5750     }
5751   }
5752
5753   if (!anyScrollbarGadgetActive() &&
5754       IN_VIS_MENU(x, y) &&
5755       mx < screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x &&
5756       y >= 0 && y < num_page_entries)
5757   {
5758     if (button)
5759     {
5760       if (first_entry + y != choice &&
5761           setup_info[first_entry + y].type & ~TYPE_SKIP_ENTRY)
5762       {
5763         PlaySound(SND_MENU_ITEM_ACTIVATING);
5764
5765         DrawCursorAndText_Setup(choice - first_entry, choice, FALSE);
5766         DrawCursorAndText_Setup(y, first_entry + y, TRUE);
5767
5768         choice = choice_store[setup_mode] = first_entry + y;
5769       }
5770     }
5771     else if (!(setup_info[first_entry + y].type & TYPE_GHOSTED))
5772     {
5773       PlaySound(SND_MENU_ITEM_SELECTING);
5774
5775       /* when selecting key headline, execute function for key value change */
5776       if (setup_info[first_entry + y].type & TYPE_KEYTEXT &&
5777           setup_info[first_entry + y + 1].type & TYPE_KEY)
5778         y++;
5779
5780       /* when selecting string value, execute function for list selection */
5781       if (setup_info[first_entry + y].type & TYPE_STRING && y > 0 &&
5782           setup_info[first_entry + y - 1].type & TYPE_ENTER_LIST)
5783         y--;
5784
5785       if (setup_info[first_entry + y].type & TYPE_ENTER_OR_LEAVE)
5786       {
5787         void (*menu_callback_function)(void) =
5788           setup_info[first_entry + y].value;
5789
5790         FadeSetFromType(setup_info[first_entry + y].type);
5791
5792         menu_callback_function();
5793       }
5794       else
5795       {
5796         if (setup_info[first_entry + y].type & TYPE_VALUE)
5797           changeSetupValue(y, first_entry + y, dx);
5798       }
5799     }
5800   }
5801 }
5802
5803 void DrawSetupScreen_Input()
5804 {
5805   int i;
5806
5807   FadeOut(REDRAW_FIELD);
5808
5809   ClearField();
5810
5811   setup_info = setup_info_input;
5812
5813   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Setup Input");
5814
5815   DrawTextSCentered(SYSIZE - 20, FONT_TITLE_2,
5816                     "Joysticks deactivated on this screen");
5817
5818   for (i = 0; setup_info[i].type != 0 && i < MAX_MENU_ENTRIES_ON_SCREEN; i++)
5819   {
5820     if (setup_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
5821       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
5822     else if (setup_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
5823       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
5824     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
5825       initCursor(i, IMG_MENU_BUTTON);
5826
5827     DrawCursorAndText_Setup(i, -1, FALSE);
5828   }
5829
5830   /* create gadgets for setup input menu screen */
5831   FreeScreenGadgets();
5832   CreateScreenGadgets();
5833
5834   /* map gadgets for setup input menu screen */
5835   MapScreenMenuGadgets(SCREEN_MASK_INPUT);
5836
5837   HandleSetupScreen_Input(0, 0, 0, 0, MB_MENU_INITIALIZE);
5838
5839   FadeIn(REDRAW_FIELD);
5840
5841   InitAnimation();
5842 }
5843
5844 static void setJoystickDeviceToNr(char *device_name, int device_nr)
5845 {
5846   if (device_name == NULL)
5847     return;
5848
5849   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
5850     device_nr = 0;
5851
5852   if (strlen(device_name) > 1)
5853   {
5854     char c1 = device_name[strlen(device_name) - 1];
5855     char c2 = device_name[strlen(device_name) - 2];
5856
5857     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
5858       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
5859   }
5860   else
5861     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
5862             strlen(device_name));
5863 }
5864
5865 static void drawPlayerSetupInputInfo(int player_nr, boolean active)
5866 {
5867   int i;
5868   static struct SetupKeyboardInfo custom_key;
5869   static struct
5870   {
5871     Key *key;
5872     char *text;
5873   } custom[] =
5874   {
5875     { &custom_key.left,  "Joystick Left"  },
5876     { &custom_key.right, "Joystick Right" },
5877     { &custom_key.up,    "Joystick Up"    },
5878     { &custom_key.down,  "Joystick Down"  },
5879     { &custom_key.snap,  "Button 1"       },
5880     { &custom_key.drop,  "Button 2"       }
5881   };
5882   static char *joystick_name[MAX_PLAYERS] =
5883   {
5884     "Joystick1",
5885     "Joystick2",
5886     "Joystick3",
5887     "Joystick4"
5888   };
5889   int text_font_nr = (active ? FONT_MENU_1_ACTIVE : FONT_MENU_1);
5890
5891   InitJoysticks();
5892
5893   custom_key = setup.input[player_nr].key;
5894
5895   DrawText(mSX + 11 * 32, mSY + 2 * 32, int2str(player_nr + 1, 1),
5896            FONT_INPUT_1_ACTIVE);
5897
5898   ClearRectangleOnBackground(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
5899                              TILEX, TILEY);
5900   DrawFixedGraphicThruMaskExt(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
5901                               PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
5902
5903   if (setup.input[player_nr].use_joystick)
5904   {
5905     char *device_name = setup.input[player_nr].joy.device_name;
5906     char *text = joystick_name[getJoystickNrFromDeviceName(device_name)];
5907     int font_nr = (joystick.fd[player_nr] < 0 ? FONT_VALUE_OLD : FONT_VALUE_1);
5908
5909     DrawText(mSX + 8 * 32, mSY + 3 * 32, text, font_nr);
5910     DrawText(mSX + 32, mSY + 4 * 32, "Calibrate", text_font_nr);
5911   }
5912   else
5913   {
5914     DrawText(mSX + 8 * 32, mSY + 3 * 32, "Keyboard ", FONT_VALUE_1);
5915     DrawText(mSX + 1 * 32, mSY + 4 * 32, "Customize", text_font_nr);
5916   }
5917
5918   DrawText(mSX + 32, mSY + 5 * 32, "Actual Settings:", FONT_MENU_1);
5919
5920   drawCursorXY(1, 4, IMG_MENU_BUTTON_LEFT);
5921   drawCursorXY(1, 5, IMG_MENU_BUTTON_RIGHT);
5922   drawCursorXY(1, 6, IMG_MENU_BUTTON_UP);
5923   drawCursorXY(1, 7, IMG_MENU_BUTTON_DOWN);
5924
5925   DrawText(mSX + 2 * 32, mSY +  6 * 32, ":", FONT_VALUE_OLD);
5926   DrawText(mSX + 2 * 32, mSY +  7 * 32, ":", FONT_VALUE_OLD);
5927   DrawText(mSX + 2 * 32, mSY +  8 * 32, ":", FONT_VALUE_OLD);
5928   DrawText(mSX + 2 * 32, mSY +  9 * 32, ":", FONT_VALUE_OLD);
5929   DrawText(mSX + 1 * 32, mSY + 10 * 32, "Snap Field:", FONT_VALUE_OLD);
5930   DrawText(mSX + 1 * 32, mSY + 12 * 32, "Drop Element:", FONT_VALUE_OLD);
5931
5932   for (i = 0; i < 6; i++)
5933   {
5934     int ypos = 6 + i + (i > 3 ? i-3 : 0);
5935
5936     DrawText(mSX + 3 * 32, mSY + ypos * 32,
5937              "              ", FONT_VALUE_1);
5938     DrawText(mSX + 3 * 32, mSY + ypos * 32,
5939              (setup.input[player_nr].use_joystick ?
5940               custom[i].text :
5941               getKeyNameFromKey(*custom[i].key)), FONT_VALUE_1);
5942   }
5943 }
5944
5945 static int input_player_nr = 0;
5946
5947 void HandleSetupScreen_Input_Player(int step, int direction)
5948 {
5949   int old_player_nr = input_player_nr;
5950   int new_player_nr;
5951
5952   new_player_nr = old_player_nr + step * direction;
5953   if (new_player_nr < 0)
5954     new_player_nr = 0;
5955   if (new_player_nr > MAX_PLAYERS - 1)
5956     new_player_nr = MAX_PLAYERS - 1;
5957
5958   if (new_player_nr != old_player_nr)
5959   {
5960     input_player_nr = new_player_nr;
5961
5962     drawPlayerSetupInputInfo(input_player_nr, FALSE);
5963   }
5964 }
5965
5966 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
5967 {
5968   static int choice = 0;
5969   int x = 0;
5970   int y = choice;
5971   int pos_start  = SETUPINPUT_SCREEN_POS_START;
5972   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
5973   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
5974   int pos_end    = SETUPINPUT_SCREEN_POS_END;
5975
5976   if (button == MB_MENU_INITIALIZE)
5977   {
5978     drawPlayerSetupInputInfo(input_player_nr, (choice == 2));
5979
5980     DrawCursorAndText_Setup(choice, -1, TRUE);
5981
5982     return;
5983   }
5984   else if (button == MB_MENU_LEAVE)
5985   {
5986     setup_mode = SETUP_MODE_MAIN;
5987     DrawSetupScreen();
5988     InitJoysticks();
5989
5990     return;
5991   }
5992
5993   if (mx || my)         /* mouse input */
5994   {
5995     x = (mx - mSX) / 32;
5996     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
5997   }
5998   else if (dx || dy)    /* keyboard input */
5999   {
6000     if (dx && choice == 0)
6001       x = (dx < 0 ? 10 : 12);
6002     else if ((dx && choice == 1) ||
6003              (dx == +1 && choice == 2) ||
6004              (dx == -1 && choice == pos_end))
6005       button = MB_MENU_CHOICE;
6006     else if (dy)
6007       y = choice + dy;
6008
6009     if (y >= pos_empty1 && y <= pos_empty2)
6010       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
6011   }
6012
6013   if (y == 0 && dx != 0 && button)
6014   {
6015     HandleSetupScreen_Input_Player(1, dx < 0 ? -1 : +1);
6016   }
6017   else if (IN_VIS_FIELD(x, y) &&        // (does not use "IN_VIS_MENU()" yet)
6018            y >= pos_start && y <= pos_end &&
6019            !(y >= pos_empty1 && y <= pos_empty2))
6020   {
6021     if (button)
6022     {
6023       if (y != choice)
6024       {
6025         DrawCursorAndText_Setup(choice, -1, FALSE);
6026         DrawCursorAndText_Setup(y, -1, TRUE);
6027
6028         drawPlayerSetupInputInfo(input_player_nr, (y == 2));
6029
6030         choice = y;
6031       }
6032     }
6033     else
6034     {
6035       if (y == 1)
6036       {
6037         char *device_name = setup.input[input_player_nr].joy.device_name;
6038
6039         if (!setup.input[input_player_nr].use_joystick)
6040         {
6041           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
6042
6043           setJoystickDeviceToNr(device_name, new_device_nr);
6044           setup.input[input_player_nr].use_joystick = TRUE;
6045         }
6046         else
6047         {
6048           int device_nr = getJoystickNrFromDeviceName(device_name);
6049           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
6050
6051           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
6052             setup.input[input_player_nr].use_joystick = FALSE;
6053           else
6054             setJoystickDeviceToNr(device_name, new_device_nr);
6055         }
6056
6057         drawPlayerSetupInputInfo(input_player_nr, FALSE);
6058       }
6059       else if (y == 2)
6060       {
6061         if (setup.input[input_player_nr].use_joystick)
6062         {
6063           InitJoysticks();
6064           CalibrateJoystick(input_player_nr);
6065         }
6066         else
6067           CustomizeKeyboard(input_player_nr);
6068       }
6069       else if (y == pos_end)
6070       {
6071         InitJoysticks();
6072
6073         FadeSetLeaveMenu();
6074
6075         setup_mode = SETUP_MODE_MAIN;
6076         DrawSetupScreen();
6077       }
6078     }
6079   }
6080 }
6081
6082 void CustomizeKeyboard(int player_nr)
6083 {
6084   int i;
6085   int step_nr;
6086   boolean finished = FALSE;
6087   static struct SetupKeyboardInfo custom_key;
6088   static struct
6089   {
6090     Key *key;
6091     char *text;
6092   } customize_step[] =
6093   {
6094     { &custom_key.left,  "Move Left"    },
6095     { &custom_key.right, "Move Right"   },
6096     { &custom_key.up,    "Move Up"      },
6097     { &custom_key.down,  "Move Down"    },
6098     { &custom_key.snap,  "Snap Field"   },
6099     { &custom_key.drop,  "Drop Element" }
6100   };
6101
6102   /* read existing key bindings from player setup */
6103   custom_key = setup.input[player_nr].key;
6104
6105   FadeSetEnterMenu();
6106   FadeOut(REDRAW_FIELD);
6107
6108   ClearField();
6109
6110   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Keyboard Input");
6111
6112   step_nr = 0;
6113   DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
6114            customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
6115   DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
6116            "Key:", FONT_INPUT_1_ACTIVE);
6117   DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
6118            getKeyNameFromKey(*customize_step[step_nr].key), FONT_VALUE_OLD);
6119
6120   FadeIn(REDRAW_FIELD);
6121
6122   InitAnimation();
6123
6124   while (!finished)
6125   {
6126     if (PendingEvent())         /* got event */
6127     {
6128       Event event;
6129
6130       NextEvent(&event);
6131
6132       switch (event.type)
6133       {
6134         case EVENT_KEYPRESS:
6135           {
6136             Key key = GetEventKey((KeyEvent *)&event, FALSE);
6137
6138             if (key == KSYM_Escape || (key == KSYM_Return && step_nr == 6))
6139             {
6140               if (key == KSYM_Escape)
6141                 FadeSkipNextFadeIn();
6142
6143               finished = TRUE;
6144               break;
6145             }
6146
6147             /* all keys configured -- wait for "Escape" or "Return" key */
6148             if (step_nr == 6)
6149               break;
6150
6151             /* press 'Enter' to keep the existing key binding */
6152             if (key == KSYM_Return)
6153               key = *customize_step[step_nr].key;
6154
6155             /* check if key already used */
6156             for (i = 0; i < step_nr; i++)
6157               if (*customize_step[i].key == key)
6158                 break;
6159             if (i < step_nr)
6160               break;
6161
6162             /* got new key binding */
6163             *customize_step[step_nr].key = key;
6164             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
6165                      "             ", FONT_VALUE_1);
6166             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
6167                      getKeyNameFromKey(key), FONT_VALUE_1);
6168             step_nr++;
6169
6170             /* un-highlight last query */
6171             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1)) * 32,
6172                      customize_step[step_nr - 1].text, FONT_MENU_1);
6173             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1) + 1) * 32,
6174                      "Key:", FONT_MENU_1);
6175
6176             /* press 'Enter' to leave */
6177             if (step_nr == 6)
6178             {
6179               DrawText(mSX + 16, mSY + 15 * 32 + 16,
6180                        "Press Enter", FONT_TITLE_1);
6181               break;
6182             }
6183
6184             /* query next key binding */
6185             DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
6186                      customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
6187             DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
6188                      "Key:", FONT_INPUT_1_ACTIVE);
6189             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
6190                      getKeyNameFromKey(*customize_step[step_nr].key),
6191                      FONT_VALUE_OLD);
6192           }
6193           break;
6194
6195         case EVENT_KEYRELEASE:
6196           key_joystick_mapping = 0;
6197           break;
6198
6199         default:
6200           HandleOtherEvents(&event);
6201           break;
6202       }
6203     }
6204
6205     DoAnimation();
6206     BackToFront();
6207
6208     /* don't eat all CPU time */
6209     Delay(10);
6210   }
6211
6212   /* write new key bindings back to player setup */
6213   setup.input[player_nr].key = custom_key;
6214
6215   StopAnimation();
6216   DrawSetupScreen_Input();
6217 }
6218
6219 static boolean CalibrateJoystickMain(int player_nr)
6220 {
6221   int new_joystick_xleft = JOYSTICK_XMIDDLE;
6222   int new_joystick_xright = JOYSTICK_XMIDDLE;
6223   int new_joystick_yupper = JOYSTICK_YMIDDLE;
6224   int new_joystick_ylower = JOYSTICK_YMIDDLE;
6225   int new_joystick_xmiddle, new_joystick_ymiddle;
6226
6227   int joystick_fd = joystick.fd[player_nr];
6228   int x, y, last_x, last_y, xpos = 8, ypos = 3;
6229   boolean check[3][3];
6230   int check_remaining = 3 * 3;
6231   int joy_x, joy_y;
6232   int joy_value;
6233   int result = -1;
6234
6235   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
6236     return FALSE;
6237
6238   if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
6239     return FALSE;
6240
6241   FadeSetEnterMenu();
6242   FadeOut(REDRAW_FIELD);
6243
6244   ClearField();
6245
6246   for (y = 0; y < 3; y++)
6247   {
6248     for (x = 0; x < 3; x++)
6249     {
6250       DrawFixedGraphic(xpos + x - 1, ypos + y - 1, IMG_MENU_CALIBRATE_BLUE, 0);
6251       check[x][y] = FALSE;
6252     }
6253   }
6254
6255   DrawTextSCentered(mSY - SY +  6 * 32, FONT_TITLE_1, "Rotate joystick");
6256   DrawTextSCentered(mSY - SY +  7 * 32, FONT_TITLE_1, "in all directions");
6257   DrawTextSCentered(mSY - SY +  9 * 32, FONT_TITLE_1, "if all balls");
6258   DrawTextSCentered(mSY - SY + 10 * 32, FONT_TITLE_1, "are marked,");
6259   DrawTextSCentered(mSY - SY + 11 * 32, FONT_TITLE_1, "center joystick");
6260   DrawTextSCentered(mSY - SY + 12 * 32, FONT_TITLE_1, "and");
6261   DrawTextSCentered(mSY - SY + 13 * 32, FONT_TITLE_1, "press any button!");
6262
6263   joy_value = Joystick(player_nr);
6264   last_x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
6265   last_y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
6266
6267   /* eventually uncalibrated center position (joystick could be uncentered) */
6268   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
6269     return FALSE;
6270
6271   new_joystick_xmiddle = joy_x;
6272   new_joystick_ymiddle = joy_y;
6273
6274   DrawFixedGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_RED, 0);
6275
6276   FadeIn(REDRAW_FIELD);
6277
6278   while (Joystick(player_nr) & JOY_BUTTON);     /* wait for released button */
6279   InitAnimation();
6280
6281   while (result < 0)
6282   {
6283     if (PendingEvent())         /* got event */
6284     {
6285       Event event;
6286
6287       NextEvent(&event);
6288
6289       switch (event.type)
6290       {
6291         case EVENT_KEYPRESS:
6292           switch (GetEventKey((KeyEvent *)&event, TRUE))
6293           {
6294             case KSYM_Return:
6295               if (check_remaining == 0)
6296                 result = 1;
6297               break;
6298
6299             case KSYM_Escape:
6300               FadeSkipNextFadeIn();
6301               result = 0;
6302               break;
6303
6304             default:
6305               break;
6306           }
6307           break;
6308
6309         case EVENT_KEYRELEASE:
6310           key_joystick_mapping = 0;
6311           break;
6312
6313         default:
6314           HandleOtherEvents(&event);
6315           break;
6316       }
6317     }
6318
6319     if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
6320       return FALSE;
6321
6322     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_x);
6323     new_joystick_xright = MAX(new_joystick_xright, joy_x);
6324     new_joystick_yupper = MIN(new_joystick_yupper, joy_y);
6325     new_joystick_ylower = MAX(new_joystick_ylower, joy_y);
6326
6327     setup.input[player_nr].joy.xleft = new_joystick_xleft;
6328     setup.input[player_nr].joy.yupper = new_joystick_yupper;
6329     setup.input[player_nr].joy.xright = new_joystick_xright;
6330     setup.input[player_nr].joy.ylower = new_joystick_ylower;
6331     setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
6332     setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
6333
6334     CheckJoystickData();
6335
6336     joy_value = Joystick(player_nr);
6337
6338     if (joy_value & JOY_BUTTON && check_remaining == 0)
6339       result = 1;
6340
6341     x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
6342     y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
6343
6344     if (x != last_x || y != last_y)
6345     {
6346       DrawFixedGraphic(xpos + last_x, ypos + last_y,
6347                        IMG_MENU_CALIBRATE_YELLOW, 0);
6348       DrawFixedGraphic(xpos + x,      ypos + y,
6349                        IMG_MENU_CALIBRATE_RED,    0);
6350
6351       last_x = x;
6352       last_y = y;
6353
6354       if (check_remaining > 0 && !check[x+1][y+1])
6355       {
6356         check[x+1][y+1] = TRUE;
6357         check_remaining--;
6358       }
6359     }
6360
6361     DoAnimation();
6362     BackToFront();
6363
6364     /* don't eat all CPU time */
6365     Delay(10);
6366   }
6367
6368   /* calibrated center position (joystick should now be centered) */
6369   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
6370     return FALSE;
6371
6372   new_joystick_xmiddle = joy_x;
6373   new_joystick_ymiddle = joy_y;
6374
6375   StopAnimation();
6376
6377   /* wait until the last pressed button was released */
6378   while (Joystick(player_nr) & JOY_BUTTON)
6379   {
6380     if (PendingEvent())         /* got event */
6381     {
6382       Event event;
6383
6384       NextEvent(&event);
6385       HandleOtherEvents(&event);
6386
6387       Delay(10);
6388     }
6389   }
6390
6391   return TRUE;
6392 }
6393
6394 void CalibrateJoystick(int player_nr)
6395 {
6396   if (!CalibrateJoystickMain(player_nr))
6397   {
6398     char *device_name = setup.input[player_nr].joy.device_name;
6399     int nr = getJoystickNrFromDeviceName(device_name) + 1;
6400     int xpos = mSX - SX;
6401     int ypos = mSY - SY;
6402
6403     ClearField();
6404
6405     DrawTextF(xpos + 16, ypos + 6 * 32, FONT_TITLE_1, "   JOYSTICK %d   ", nr);
6406     DrawTextF(xpos + 16, ypos + 7 * 32, FONT_TITLE_1, " NOT AVAILABLE! ");
6407     BackToFront();
6408
6409     Delay(2000);                /* show error message for a short time */
6410
6411     ClearEventQueue();
6412   }
6413
6414   DrawSetupScreen_Input();
6415 }
6416
6417 void DrawSetupScreen()
6418 {
6419   DeactivateJoystick();
6420
6421   SetMainBackgroundImage(IMG_BACKGROUND_SETUP);
6422
6423   if (setup_mode == SETUP_MODE_INPUT)
6424     DrawSetupScreen_Input();
6425   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
6426     DrawChooseTree(&game_speed_current);
6427   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
6428     DrawChooseTree(&scroll_delay_current);
6429   else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE)
6430     DrawChooseTree(&screen_mode_current);
6431   else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
6432     DrawChooseTree(&window_size_current);
6433   else if (setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
6434     DrawChooseTree(&scaling_type_current);
6435   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
6436     DrawChooseTree(&artwork.gfx_current);
6437   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
6438     DrawChooseTree(&artwork.snd_current);
6439   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
6440     DrawChooseTree(&artwork.mus_current);
6441   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE)
6442     DrawChooseTree(&volume_simple_current);
6443   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS)
6444     DrawChooseTree(&volume_loops_current);
6445   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
6446     DrawChooseTree(&volume_music_current);
6447   else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL)
6448     DrawChooseTree(&touch_control_current);
6449   else if (setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE)
6450     DrawChooseTree(&move_distance_current);
6451   else if (setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
6452     DrawChooseTree(&drop_distance_current);
6453   else
6454     DrawSetupScreen_Generic();
6455
6456   PlayMenuSound();
6457   PlayMenuMusic();
6458 }
6459
6460 void RedrawSetupScreenAfterFullscreenToggle()
6461 {
6462   if (setup_mode == SETUP_MODE_GRAPHICS ||
6463       setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
6464   {
6465     // update list selection from "setup.window_scaling_percent"
6466     execSetupGraphics_setWindowSizes(TRUE);
6467
6468     DrawSetupScreen();
6469   }
6470 }
6471
6472 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
6473 {
6474   if (setup_mode == SETUP_MODE_INPUT)
6475     HandleSetupScreen_Input(mx, my, dx, dy, button);
6476   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
6477     HandleChooseTree(mx, my, dx, dy, button, &game_speed_current);
6478   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
6479     HandleChooseTree(mx, my, dx, dy, button, &scroll_delay_current);
6480   else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE)
6481     HandleChooseTree(mx, my, dx, dy, button, &screen_mode_current);
6482   else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
6483     HandleChooseTree(mx, my, dx, dy, button, &window_size_current);
6484   else if (setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
6485     HandleChooseTree(mx, my, dx, dy, button, &scaling_type_current);
6486   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
6487     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
6488   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
6489     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
6490   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
6491     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
6492   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE)
6493     HandleChooseTree(mx, my, dx, dy, button, &volume_simple_current);
6494   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS)
6495     HandleChooseTree(mx, my, dx, dy, button, &volume_loops_current);
6496   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
6497     HandleChooseTree(mx, my, dx, dy, button, &volume_music_current);
6498   else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL)
6499     HandleChooseTree(mx, my, dx, dy, button, &touch_control_current);
6500   else if (setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE)
6501     HandleChooseTree(mx, my, dx, dy, button, &move_distance_current);
6502   else if (setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
6503     HandleChooseTree(mx, my, dx, dy, button, &drop_distance_current);
6504   else
6505     HandleSetupScreen_Generic(mx, my, dx, dy, button);
6506 }
6507
6508 void HandleGameActions()
6509 {
6510   if (game_status != GAME_MODE_PLAYING)
6511     return;
6512
6513   GameActions();        /* main game loop */
6514
6515   if (tape.auto_play && !tape.playing)
6516     AutoPlayTape();     /* continue automatically playing next tape */
6517 }
6518
6519
6520 /* ---------- new screen button stuff -------------------------------------- */
6521
6522 static void getScreenMenuButtonPos(int *x, int *y, int gadget_id)
6523 {
6524   switch (gadget_id)
6525   {
6526     case SCREEN_CTRL_ID_PREV_LEVEL:
6527       *x = mSX + GDI_ACTIVE_POS(menu.main.button.prev_level.x);
6528       *y = mSY + GDI_ACTIVE_POS(menu.main.button.prev_level.y);
6529       break;
6530
6531     case SCREEN_CTRL_ID_NEXT_LEVEL:
6532       *x = mSX + GDI_ACTIVE_POS(menu.main.button.next_level.x);
6533       *y = mSY + GDI_ACTIVE_POS(menu.main.button.next_level.y);
6534       break;
6535
6536     case SCREEN_CTRL_ID_PREV_PLAYER:
6537       *x = mSX + TILEX * 10;
6538       *y = mSY + TILEY * MENU_SCREEN_START_YPOS;
6539       break;
6540
6541     case SCREEN_CTRL_ID_NEXT_PLAYER:
6542       *x = mSX + TILEX * 12;
6543       *y = mSY + TILEY * MENU_SCREEN_START_YPOS;
6544       break;
6545
6546     default:
6547       Error(ERR_EXIT, "unknown gadget ID %d", gadget_id);
6548   }
6549 }
6550
6551 static struct
6552 {
6553   int gfx_unpressed, gfx_pressed;
6554   void (*get_gadget_position)(int *, int *, int);
6555   int gadget_id;
6556   int screen_mask;
6557   char *infotext;
6558 } menubutton_info[NUM_SCREEN_MENUBUTTONS] =
6559 {
6560   {
6561     IMG_MENU_BUTTON_PREV_LEVEL, IMG_MENU_BUTTON_PREV_LEVEL_ACTIVE,
6562     getScreenMenuButtonPos,
6563     SCREEN_CTRL_ID_PREV_LEVEL,
6564     SCREEN_MASK_MAIN,
6565     "last level"
6566   },
6567   {
6568     IMG_MENU_BUTTON_NEXT_LEVEL, IMG_MENU_BUTTON_NEXT_LEVEL_ACTIVE,
6569     getScreenMenuButtonPos,
6570     SCREEN_CTRL_ID_NEXT_LEVEL,
6571     SCREEN_MASK_MAIN,
6572     "next level"
6573   },
6574   {
6575     IMG_MENU_BUTTON_LEFT, IMG_MENU_BUTTON_LEFT_ACTIVE,
6576     getScreenMenuButtonPos,
6577     SCREEN_CTRL_ID_PREV_PLAYER,
6578     SCREEN_MASK_INPUT,
6579     "last player"
6580   },
6581   {
6582     IMG_MENU_BUTTON_RIGHT, IMG_MENU_BUTTON_RIGHT_ACTIVE,
6583     getScreenMenuButtonPos,
6584     SCREEN_CTRL_ID_NEXT_PLAYER,
6585     SCREEN_MASK_INPUT,
6586     "next player"
6587   },
6588 };
6589
6590 static struct
6591 {
6592   int gfx_unpressed, gfx_pressed;
6593   int x, y;
6594   int gadget_id;
6595   char *infotext;
6596 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
6597 {
6598   {
6599     IMG_MENU_BUTTON_UP, IMG_MENU_BUTTON_UP_ACTIVE,
6600     -1, -1,     /* these values are not constant, but can change at runtime */
6601     SCREEN_CTRL_ID_SCROLL_UP,
6602     "scroll up"
6603   },
6604   {
6605     IMG_MENU_BUTTON_DOWN, IMG_MENU_BUTTON_DOWN_ACTIVE,
6606     -1, -1,     /* these values are not constant, but can change at runtime */
6607     SCREEN_CTRL_ID_SCROLL_DOWN,
6608     "scroll down"
6609   }
6610 };
6611
6612 static struct
6613 {
6614   int gfx_unpressed, gfx_pressed;
6615   int x, y;
6616   int width, height;
6617   int type;
6618   int gadget_id;
6619   char *infotext;
6620 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
6621 {
6622   {
6623     IMG_MENU_SCROLLBAR, IMG_MENU_SCROLLBAR_ACTIVE,
6624     -1, -1,     /* these values are not constant, but can change at runtime */
6625     -1, -1,     /* these values are not constant, but can change at runtime */
6626     GD_TYPE_SCROLLBAR_VERTICAL,
6627     SCREEN_CTRL_ID_SCROLL_VERTICAL,
6628     "scroll level series vertically"
6629   }
6630 };
6631
6632 static void CreateScreenMenubuttons()
6633 {
6634   struct GadgetInfo *gi;
6635   unsigned int event_mask;
6636   int i;
6637
6638   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
6639   {
6640     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6641     int gfx_unpressed, gfx_pressed;
6642     int x, y, width, height;
6643     int gd_x1, gd_x2, gd_y1, gd_y2;
6644     int id = menubutton_info[i].gadget_id;
6645
6646     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
6647
6648     menubutton_info[i].get_gadget_position(&x, &y, id);
6649
6650     width = SC_MENUBUTTON_XSIZE;
6651     height = SC_MENUBUTTON_YSIZE;
6652
6653     gfx_unpressed = menubutton_info[i].gfx_unpressed;
6654     gfx_pressed   = menubutton_info[i].gfx_pressed;
6655     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6656     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6657     gd_x1 = graphic_info[gfx_unpressed].src_x;
6658     gd_y1 = graphic_info[gfx_unpressed].src_y;
6659     gd_x2 = graphic_info[gfx_pressed].src_x;
6660     gd_y2 = graphic_info[gfx_pressed].src_y;
6661
6662     gi = CreateGadget(GDI_CUSTOM_ID, id,
6663                       GDI_CUSTOM_TYPE_ID, i,
6664                       GDI_INFO_TEXT, menubutton_info[i].infotext,
6665                       GDI_X, x,
6666                       GDI_Y, y,
6667                       GDI_WIDTH, width,
6668                       GDI_HEIGHT, height,
6669                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
6670                       GDI_STATE, GD_BUTTON_UNPRESSED,
6671                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6672                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6673                       GDI_DIRECT_DRAW, FALSE,
6674                       GDI_EVENT_MASK, event_mask,
6675                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6676                       GDI_END);
6677
6678     if (gi == NULL)
6679       Error(ERR_EXIT, "cannot create gadget");
6680
6681     screen_gadget[id] = gi;
6682   }
6683 }
6684
6685 static void CreateScreenScrollbuttons()
6686 {
6687   struct GadgetInfo *gi;
6688   unsigned int event_mask;
6689   int i;
6690
6691   /* these values are not constant, but can change at runtime */
6692   scrollbutton_info[0].x = SC_SCROLL_UP_XPOS;
6693   scrollbutton_info[0].y = SC_SCROLL_UP_YPOS;
6694   scrollbutton_info[1].x = SC_SCROLL_DOWN_XPOS;
6695   scrollbutton_info[1].y = SC_SCROLL_DOWN_YPOS;
6696
6697   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
6698   {
6699     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6700     int gfx_unpressed, gfx_pressed;
6701     int x, y, width, height;
6702     int gd_x1, gd_x2, gd_y1, gd_y2;
6703     int id = scrollbutton_info[i].gadget_id;
6704
6705     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
6706
6707     x = mSX + scrollbutton_info[i].x + menu.scrollbar_xoffset;
6708     y = mSY + scrollbutton_info[i].y;
6709     width = SC_SCROLLBUTTON_XSIZE;
6710     height = SC_SCROLLBUTTON_YSIZE;
6711
6712     /* correct scrollbar position if placed outside menu (playfield) area */
6713     if (x > SX + SC_SCROLL_UP_XPOS)
6714       x = SX + SC_SCROLL_UP_XPOS;
6715
6716     if (id == SCREEN_CTRL_ID_SCROLL_DOWN)
6717       y = mSY + (SC_SCROLL_VERTICAL_YPOS +
6718                  (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE);
6719
6720     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
6721     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
6722     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6723     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6724     gd_x1 = graphic_info[gfx_unpressed].src_x;
6725     gd_y1 = graphic_info[gfx_unpressed].src_y;
6726     gd_x2 = graphic_info[gfx_pressed].src_x;
6727     gd_y2 = graphic_info[gfx_pressed].src_y;
6728
6729     gi = CreateGadget(GDI_CUSTOM_ID, id,
6730                       GDI_CUSTOM_TYPE_ID, i,
6731                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
6732                       GDI_X, x,
6733                       GDI_Y, y,
6734                       GDI_WIDTH, width,
6735                       GDI_HEIGHT, height,
6736                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
6737                       GDI_STATE, GD_BUTTON_UNPRESSED,
6738                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6739                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6740                       GDI_DIRECT_DRAW, FALSE,
6741                       GDI_EVENT_MASK, event_mask,
6742                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6743                       GDI_END);
6744
6745     if (gi == NULL)
6746       Error(ERR_EXIT, "cannot create gadget");
6747
6748     screen_gadget[id] = gi;
6749   }
6750 }
6751
6752 static void CreateScreenScrollbars()
6753 {
6754   int i;
6755
6756   /* these values are not constant, but can change at runtime */
6757   scrollbar_info[0].x = SC_SCROLL_VERTICAL_XPOS;
6758   scrollbar_info[0].y = SC_SCROLL_VERTICAL_YPOS;
6759   scrollbar_info[0].width  = SC_SCROLL_VERTICAL_XSIZE;
6760   scrollbar_info[0].height = SC_SCROLL_VERTICAL_YSIZE;
6761
6762   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
6763   {
6764     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6765     int gfx_unpressed, gfx_pressed;
6766     int x, y, width, height;
6767     int gd_x1, gd_x2, gd_y1, gd_y2;
6768     struct GadgetInfo *gi;
6769     int items_max, items_visible, item_position;
6770     unsigned int event_mask;
6771     int num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
6772     int id = scrollbar_info[i].gadget_id;
6773
6774     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
6775
6776     x = mSX + scrollbar_info[i].x + menu.scrollbar_xoffset;
6777     y = mSY + scrollbar_info[i].y;
6778     width  = scrollbar_info[i].width;
6779     height = scrollbar_info[i].height;
6780
6781     /* correct scrollbar position if placed outside menu (playfield) area */
6782     if (x > SX + SC_SCROLL_VERTICAL_XPOS)
6783       x = SX + SC_SCROLL_VERTICAL_XPOS;
6784
6785     if (id == SCREEN_CTRL_ID_SCROLL_VERTICAL)
6786       height = (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE;
6787
6788     items_max = num_page_entries;
6789     items_visible = num_page_entries;
6790     item_position = 0;
6791
6792     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
6793     gfx_pressed   = scrollbar_info[i].gfx_pressed;
6794     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6795     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6796     gd_x1 = graphic_info[gfx_unpressed].src_x;
6797     gd_y1 = graphic_info[gfx_unpressed].src_y;
6798     gd_x2 = graphic_info[gfx_pressed].src_x;
6799     gd_y2 = graphic_info[gfx_pressed].src_y;
6800
6801     gi = CreateGadget(GDI_CUSTOM_ID, id,
6802                       GDI_CUSTOM_TYPE_ID, i,
6803                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
6804                       GDI_X, x,
6805                       GDI_Y, y,
6806                       GDI_WIDTH, width,
6807                       GDI_HEIGHT, height,
6808                       GDI_TYPE, scrollbar_info[i].type,
6809                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
6810                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
6811                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
6812                       GDI_WHEEL_AREA_X, SX,
6813                       GDI_WHEEL_AREA_Y, SY,
6814                       GDI_WHEEL_AREA_WIDTH, SXSIZE,
6815                       GDI_WHEEL_AREA_HEIGHT, SYSIZE,
6816                       GDI_STATE, GD_BUTTON_UNPRESSED,
6817                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6818                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6819                       GDI_BORDER_SIZE, SC_BORDER_SIZE, SC_BORDER_SIZE,
6820                       GDI_DIRECT_DRAW, FALSE,
6821                       GDI_EVENT_MASK, event_mask,
6822                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6823                       GDI_END);
6824
6825     if (gi == NULL)
6826       Error(ERR_EXIT, "cannot create gadget");
6827
6828     screen_gadget[id] = gi;
6829   }
6830 }
6831
6832 void CreateScreenGadgets()
6833 {
6834   int last_game_status = game_status;   /* save current game status */
6835
6836   CreateScreenMenubuttons();
6837
6838   CreateScreenScrollbuttons();
6839   CreateScreenScrollbars();
6840
6841   game_status = last_game_status;       /* restore current game status */
6842 }
6843
6844 void FreeScreenGadgets()
6845 {
6846   int i;
6847
6848   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
6849     FreeGadget(screen_gadget[i]);
6850 }
6851
6852 void MapScreenMenuGadgets(int screen_mask)
6853 {
6854   int i;
6855
6856   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
6857     if (screen_mask & menubutton_info[i].screen_mask)
6858       MapGadget(screen_gadget[menubutton_info[i].gadget_id]);
6859 }
6860
6861 void MapScreenGadgets(int num_entries)
6862 {
6863   int i;
6864
6865   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
6866     return;
6867
6868   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
6869     MapGadget(screen_gadget[scrollbutton_info[i].gadget_id]);
6870
6871   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
6872     MapGadget(screen_gadget[scrollbar_info[i].gadget_id]);
6873 }
6874
6875 void MapScreenTreeGadgets(TreeInfo *ti)
6876 {
6877   MapScreenGadgets(numTreeInfoInGroup(ti));
6878 }
6879
6880 static void HandleScreenGadgets(struct GadgetInfo *gi)
6881 {
6882   int id = gi->custom_id;
6883   int button = gi->event.button;
6884   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
6885
6886   switch (id)
6887   {
6888     case SCREEN_CTRL_ID_PREV_LEVEL:
6889       HandleMainMenu_SelectLevel(step, -1, NO_DIRECT_LEVEL_SELECT);
6890       break;
6891
6892     case SCREEN_CTRL_ID_NEXT_LEVEL:
6893       HandleMainMenu_SelectLevel(step, +1, NO_DIRECT_LEVEL_SELECT);
6894       break;
6895
6896     case SCREEN_CTRL_ID_PREV_PLAYER:
6897       HandleSetupScreen_Input_Player(step, -1);
6898       break;
6899
6900     case SCREEN_CTRL_ID_NEXT_PLAYER:
6901       HandleSetupScreen_Input_Player(step, +1);
6902       break;
6903
6904     case SCREEN_CTRL_ID_SCROLL_UP:
6905       if (game_status == GAME_MODE_LEVELS)
6906         HandleChooseLevelSet(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6907       else if (game_status == GAME_MODE_LEVELNR)
6908         HandleChooseLevelNr(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6909       else if (game_status == GAME_MODE_SETUP)
6910         HandleSetupScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6911       break;
6912
6913     case SCREEN_CTRL_ID_SCROLL_DOWN:
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_VERTICAL:
6923       if (game_status == GAME_MODE_LEVELS)
6924         HandleChooseLevelSet(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
6925       else if (game_status == GAME_MODE_LEVELNR)
6926         HandleChooseLevelNr(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
6927       else if (game_status == GAME_MODE_SETUP)
6928         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
6929       break;
6930
6931     default:
6932       break;
6933   }
6934 }