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