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