changed sound handling to only expire loop sounds during gameplay
[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
2174   FreeScreenGadgets();
2175   CreateScreenGadgets();
2176
2177   /* (needed after displaying title screens which disable auto repeat) */
2178   KeyboardAutoRepeatOn();
2179
2180   FadeSetLeaveScreen();
2181
2182   FadeOut(fade_mask);
2183
2184   ChangeViewportPropertiesIfNeeded();
2185
2186   ClearField();
2187
2188   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
2189
2190   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Info Screen");
2191
2192   info_info = info_info_main;
2193
2194   // determine maximal number of info entries that can be displayed on screen
2195   num_info_info = 0;
2196   for (i = 0; info_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
2197     num_info_info++;
2198
2199   // determine maximal number of info entries available for menu of info screen
2200   max_info_info = 0;
2201   for (i = 0; info_info[i].type != 0; i++)
2202     max_info_info++;
2203
2204   HandleInfoScreen_Main(0, 0, 0, 0, MB_MENU_INITIALIZE);
2205
2206   MapScreenGadgets(max_info_info);
2207
2208   PlayMenuSound();
2209   PlayMenuMusic();
2210
2211   DrawMaskedBorder(fade_mask);
2212
2213   FadeIn(fade_mask);
2214
2215   InitAnimation();
2216 }
2217
2218 static void changeSetupValue(int, int, int);
2219
2220 void HandleMenuScreen(int mx, int my, int dx, int dy, int button,
2221                       int mode, int num_page_entries, int max_page_entries)
2222 {
2223   static int num_page_entries_all_last[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
2224   static int choice_stores[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
2225   static int first_entry_stores[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
2226   int *num_page_entries_last = num_page_entries_all_last[game_status];
2227   int *choice_store = choice_stores[game_status];
2228   int *first_entry_store = first_entry_stores[game_status];
2229   int choice = choice_store[mode];              /* starts with 0 */
2230   int first_entry = first_entry_store[mode];    /* starts with 0 */
2231   int x = 0;
2232   int y = choice - first_entry;
2233   int y_old = y;
2234   boolean position_set_by_scrollbar = (dx == 999);
2235   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
2236   int i;
2237
2238   if (button == MB_MENU_INITIALIZE)
2239   {
2240     // check if number of menu page entries has changed (may happen by change
2241     // of custom artwork definition value for 'list_size' for this menu screen)
2242     // (in this case, the last menu position most probably has to be corrected)
2243     if (num_page_entries != num_page_entries_last[mode])
2244     {
2245       choice_store[mode] = first_entry_store[mode] = 0;
2246
2247       choice = first_entry = 0;
2248       y = y_old = 0;
2249
2250       num_page_entries_last[mode] = num_page_entries;
2251     }
2252
2253     /* advance to first valid menu entry */
2254     while (choice < num_page_entries &&
2255            menu_info[choice].type & TYPE_SKIP_ENTRY)
2256       choice++;
2257
2258     if (position_set_by_scrollbar)
2259       first_entry = first_entry_store[mode] = dy;
2260     else
2261       AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_page_entries,
2262                       NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
2263
2264     drawMenuInfoList(first_entry, num_page_entries, max_page_entries);
2265
2266     if (choice < first_entry)
2267     {
2268       choice = first_entry;
2269
2270       if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2271         choice++;
2272     }
2273     else if (choice > first_entry + num_page_entries - 1)
2274     {
2275       choice = first_entry + num_page_entries - 1;
2276
2277       if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2278         choice--;
2279     }
2280
2281     choice_store[mode] = choice;
2282
2283     DrawCursorAndText_Menu(choice - first_entry, choice, TRUE);
2284
2285     return;
2286   }
2287   else if (button == MB_MENU_LEAVE)
2288   {
2289     PlaySound(SND_MENU_ITEM_SELECTING);
2290
2291     for (i = 0; i < max_page_entries; i++)
2292     {
2293       if (menu_info[i].type & TYPE_LEAVE_MENU)
2294       {
2295         void (*menu_callback_function)(void) = menu_info[i].value;
2296
2297         FadeSetLeaveMenu();
2298
2299         menu_callback_function();
2300
2301         break;  /* absolutely needed because function changes 'menu_info'! */
2302       }
2303     }
2304
2305     return;
2306   }
2307
2308   if (mx || my)         /* mouse input */
2309   {
2310     x = (mx - mSX) / 32;
2311     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2312   }
2313   else if (dx || dy)    /* keyboard or scrollbar/scrollbutton input */
2314   {
2315     /* move cursor instead of scrolling when already at start/end of list */
2316     if (dy == -1 * SCROLL_LINE && first_entry == 0)
2317       dy = -1;
2318     else if (dy == +1 * SCROLL_LINE &&
2319              first_entry + num_page_entries == max_page_entries)
2320       dy = 1;
2321
2322     /* handle scrolling screen one line or page */
2323     if (y + dy < 0 ||
2324         y + dy > num_page_entries - 1)
2325     {
2326       boolean redraw = FALSE;
2327
2328       if (ABS(dy) == SCROLL_PAGE)
2329         step = num_page_entries - 1;
2330
2331       if (dy < 0 && first_entry > 0)
2332       {
2333         /* scroll page/line up */
2334
2335         first_entry -= step;
2336         if (first_entry < 0)
2337           first_entry = 0;
2338
2339         redraw = TRUE;
2340       }
2341       else if (dy > 0 && first_entry + num_page_entries < max_page_entries)
2342       {
2343         /* scroll page/line down */
2344
2345         first_entry += step;
2346         if (first_entry + num_page_entries > max_page_entries)
2347           first_entry = MAX(0, max_page_entries - num_page_entries);
2348
2349         redraw = TRUE;
2350       }
2351
2352       if (redraw)
2353       {
2354         choice += first_entry - first_entry_store[mode];
2355
2356         if (choice < first_entry)
2357         {
2358           choice = first_entry;
2359
2360           if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2361             choice++;
2362         }
2363         else if (choice > first_entry + num_page_entries - 1)
2364         {
2365           choice = first_entry + num_page_entries - 1;
2366
2367           if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2368             choice--;
2369         }
2370         else if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2371         {
2372           choice += SIGN(dy);
2373
2374           if (choice < first_entry ||
2375               choice > first_entry + num_page_entries - 1)
2376           first_entry += SIGN(dy);
2377         }
2378
2379         first_entry_store[mode] = first_entry;
2380         choice_store[mode] = choice;
2381
2382         drawMenuInfoList(first_entry, num_page_entries, max_page_entries);
2383
2384         DrawCursorAndText_Menu(choice - first_entry, choice, TRUE);
2385
2386         AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_page_entries,
2387                         NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
2388       }
2389
2390       return;
2391     }
2392
2393     if (dx)
2394     {
2395       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE : TYPE_ENTER);
2396
2397       if (menu_info[choice].type & menu_navigation_type ||
2398           menu_info[choice].type & TYPE_BOOLEAN_STYLE ||
2399           menu_info[choice].type & TYPE_YES_NO_AUTO)
2400         button = MB_MENU_CHOICE;
2401     }
2402     else if (dy)
2403       y += dy;
2404
2405     /* jump to next non-empty menu entry (up or down) */
2406     while (first_entry + y > 0 &&
2407            first_entry + y < max_page_entries - 1 &&
2408            menu_info[first_entry + y].type & TYPE_SKIP_ENTRY)
2409       y += dy;
2410
2411     if (!IN_VIS_MENU(x, y))
2412     {
2413       choice += y - y_old;
2414
2415       if (choice < first_entry)
2416         first_entry = choice;
2417       else if (choice > first_entry + num_page_entries - 1)
2418         first_entry = choice - num_page_entries + 1;
2419
2420       if (first_entry >= 0 &&
2421           first_entry + num_page_entries <= max_page_entries)
2422       {
2423         first_entry_store[mode] = first_entry;
2424
2425         if (choice < first_entry)
2426           choice = first_entry;
2427         else if (choice > first_entry + num_page_entries - 1)
2428           choice = first_entry + num_page_entries - 1;
2429
2430         choice_store[mode] = choice;
2431
2432         drawMenuInfoList(first_entry, num_page_entries, max_page_entries);
2433
2434         DrawCursorAndText_Menu(choice - first_entry, choice, TRUE);
2435
2436         AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_page_entries,
2437                         NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
2438       }
2439
2440       return;
2441     }
2442   }
2443
2444   if (!anyScrollbarGadgetActive() &&
2445       IN_VIS_MENU(x, y) &&
2446       mx < screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x &&
2447       y >= 0 && y < num_page_entries)
2448   {
2449     if (button)
2450     {
2451       if (first_entry + y != choice &&
2452           menu_info[first_entry + y].type & ~TYPE_SKIP_ENTRY)
2453       {
2454         PlaySound(SND_MENU_ITEM_ACTIVATING);
2455
2456         DrawCursorAndText_Menu(choice - first_entry, choice, FALSE);
2457         DrawCursorAndText_Menu(y, first_entry + y, TRUE);
2458
2459         choice = choice_store[mode] = first_entry + y;
2460       }
2461     }
2462     else if (!(menu_info[first_entry + y].type & TYPE_GHOSTED))
2463     {
2464       PlaySound(SND_MENU_ITEM_SELECTING);
2465
2466       /* when selecting key headline, execute function for key value change */
2467       if (menu_info[first_entry + y].type & TYPE_KEYTEXT &&
2468           menu_info[first_entry + y + 1].type & TYPE_KEY)
2469         y++;
2470
2471       /* when selecting string value, execute function for list selection */
2472       if (menu_info[first_entry + y].type & TYPE_STRING && y > 0 &&
2473           menu_info[first_entry + y - 1].type & TYPE_ENTER_LIST)
2474         y--;
2475
2476       if (menu_info[first_entry + y].type & TYPE_ENTER_OR_LEAVE)
2477       {
2478         void (*menu_callback_function)(void) =
2479           menu_info[first_entry + y].value;
2480
2481         FadeSetFromType(menu_info[first_entry + y].type);
2482
2483         menu_callback_function();
2484       }
2485       else if (menu_info[first_entry + y].type & TYPE_VALUE &&
2486                menu_info == setup_info)
2487       {
2488         changeSetupValue(y, first_entry + y, dx);
2489       }
2490     }
2491   }
2492 }
2493
2494 void HandleInfoScreen_Main(int mx, int my, int dx, int dy, int button)
2495 {
2496   menu_info = info_info;
2497
2498   HandleMenuScreen(mx, my, dx, dy, button,
2499                    info_mode, num_info_info, max_info_info);
2500 }
2501
2502 void DrawInfoScreen_NotAvailable(char *text_title, char *text_error)
2503 {
2504   int ystart1 = mSY - SY + 100;
2505   int ystart2 = mSY - SY + 150;
2506   int ybottom = mSY - SY + SYSIZE - 20;
2507
2508   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO);
2509
2510   FadeOut(REDRAW_FIELD);
2511
2512   ClearField();
2513   DrawHeadline();
2514
2515   DrawTextSCentered(ystart1, FONT_TEXT_1, text_title);
2516   DrawTextSCentered(ystart2, FONT_TEXT_2, text_error);
2517
2518   DrawTextSCentered(ybottom, FONT_TEXT_4,
2519                     "Press any key or button for info menu");
2520
2521   FadeIn(REDRAW_FIELD);
2522 }
2523
2524 void DrawInfoScreen_HelpAnim(int start, int max_anims, boolean init)
2525 {
2526   static int infoscreen_step[MAX_INFO_ELEMENTS_ON_SCREEN];
2527   static int infoscreen_frame[MAX_INFO_ELEMENTS_ON_SCREEN];
2528   int xstart = mSX + MENU_SCREEN_INFO_XSTART;
2529   int ystart1 = mSY - SY + MENU_SCREEN_INFO_YSTART1;
2530   int ystart2 = mSY + MENU_SCREEN_INFO_YSTART2;
2531   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
2532   int ystep = MENU_SCREEN_INFO_YSTEP;
2533   int element, action, direction;
2534   int graphic;
2535   int delay;
2536   int sync_frame;
2537   int i, j;
2538
2539   if (init)
2540   {
2541     for (i = 0; i < NUM_INFO_ELEMENTS_ON_SCREEN; i++)
2542       infoscreen_step[i] = infoscreen_frame[i] = 0;
2543
2544     ClearField();
2545     DrawHeadline();
2546
2547     DrawTextSCentered(ystart1, FONT_TEXT_1, "The Game Elements:");
2548
2549     DrawTextSCentered(ybottom, FONT_TEXT_4,
2550                       "Press any key or button for next page");
2551
2552     FrameCounter = 0;
2553   }
2554
2555   i = j = 0;
2556   while (helpanim_info[j].element != HELPANIM_LIST_END)
2557   {
2558     if (i >= start + NUM_INFO_ELEMENTS_ON_SCREEN ||
2559         i >= max_anims)
2560       break;
2561     else if (i < start)
2562     {
2563       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
2564         j++;
2565
2566       j++;
2567       i++;
2568
2569       continue;
2570     }
2571
2572     j += infoscreen_step[i - start];
2573
2574     element = helpanim_info[j].element;
2575     action = helpanim_info[j].action;
2576     direction = helpanim_info[j].direction;
2577
2578     if (element < 0)
2579       element = EL_UNKNOWN;
2580
2581     if (action != -1 && direction != -1)
2582       graphic = el_act_dir2img(element, action, direction);
2583     else if (action != -1)
2584       graphic = el_act2img(element, action);
2585     else if (direction != -1)
2586       graphic = el_dir2img(element, direction);
2587     else
2588       graphic = el2img(element);
2589
2590     delay = helpanim_info[j++].delay;
2591
2592     if (delay == -1)
2593       delay = 1000000;
2594
2595     if (infoscreen_frame[i - start] == 0)
2596     {
2597       sync_frame = 0;
2598       infoscreen_frame[i - start] = delay - 1;
2599     }
2600     else
2601     {
2602       sync_frame = delay - infoscreen_frame[i - start];
2603       infoscreen_frame[i - start]--;
2604     }
2605
2606     if (helpanim_info[j].element == HELPANIM_LIST_NEXT)
2607     {
2608       if (!infoscreen_frame[i - start])
2609         infoscreen_step[i - start] = 0;
2610     }
2611     else
2612     {
2613       if (!infoscreen_frame[i - start])
2614         infoscreen_step[i - start]++;
2615       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
2616         j++;
2617     }
2618
2619     j++;
2620
2621     ClearRectangleOnBackground(drawto, xstart, ystart2 + (i - start) * ystep,
2622                                TILEX, TILEY);
2623     DrawFixedGraphicAnimationExt(drawto, xstart, ystart2 + (i - start) * ystep,
2624                                  graphic, sync_frame, USE_MASKING);
2625
2626     if (init)
2627       DrawInfoScreen_HelpText(element, action, direction, i - start);
2628
2629     i++;
2630   }
2631
2632   redraw_mask |= REDRAW_FIELD;
2633
2634   FrameCounter++;
2635 }
2636
2637 static char *getHelpText(int element, int action, int direction)
2638 {
2639   char token[MAX_LINE_LEN];
2640
2641   strcpy(token, element_info[element].token_name);
2642
2643   if (action != -1)
2644     strcat(token, element_action_info[action].suffix);
2645
2646   if (direction != -1)
2647     strcat(token, element_direction_info[MV_DIR_TO_BIT(direction)].suffix);
2648
2649   return getHashEntry(helptext_info, token);
2650 }
2651
2652 void DrawInfoScreen_HelpText(int element, int action, int direction, int ypos)
2653 {
2654   int font_nr = FONT_INFO_ELEMENTS;
2655   int font_width = getFontWidth(font_nr);
2656   int sx = mSX + MINI_TILEX + TILEX + MINI_TILEX;
2657   int sy = mSY + 65 + 2 * 32 + 1;
2658   int ystep = TILEY + 4;
2659   int pad_x = sx - SX;
2660   int max_chars_per_line = (SXSIZE - pad_x - MINI_TILEX) / font_width;
2661   int max_lines_per_text = 2;    
2662   char *text = NULL;
2663
2664   if (action != -1 && direction != -1)          /* element.action.direction */
2665     text = getHelpText(element, action, direction);
2666
2667   if (text == NULL && action != -1)             /* element.action */
2668     text = getHelpText(element, action, -1);
2669
2670   if (text == NULL && direction != -1)          /* element.direction */
2671     text = getHelpText(element, -1, direction);
2672
2673   if (text == NULL)                             /* base element */
2674     text = getHelpText(element, -1, -1);
2675
2676   if (text == NULL)                             /* not found */
2677     text = "No description available";
2678
2679   if (strlen(text) <= max_chars_per_line)       /* only one line of text */
2680     sy += getFontHeight(font_nr) / 2;
2681
2682   DrawTextBuffer(sx, sy + ypos * ystep, text, font_nr,
2683                  max_chars_per_line, -1, max_lines_per_text, 0, -1,
2684                  TRUE, FALSE, FALSE);
2685 }
2686
2687 void DrawInfoScreen_TitleScreen()
2688 {
2689   game_status_last_screen = GAME_MODE_INFO;
2690
2691   SetGameStatus(GAME_MODE_TITLE);
2692
2693   DrawTitleScreen();
2694 }
2695
2696 void HandleInfoScreen_TitleScreen(int button)
2697 {
2698   HandleTitleScreen(0, 0, 0, 0, button);
2699 }
2700
2701 void DrawInfoScreen_Elements()
2702 {
2703   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_ELEMENTS);
2704
2705   FadeOut(REDRAW_FIELD);
2706
2707   LoadHelpAnimInfo();
2708   LoadHelpTextInfo();
2709
2710   HandleInfoScreen_Elements(MB_MENU_INITIALIZE);
2711
2712   FadeIn(REDRAW_FIELD);
2713
2714   InitAnimation();
2715 }
2716
2717 void HandleInfoScreen_Elements(int button)
2718 {
2719   static unsigned int info_delay = 0;
2720   static int num_anims;
2721   static int num_pages;
2722   static int page;
2723   int anims_per_page = NUM_INFO_ELEMENTS_ON_SCREEN;
2724   int i;
2725
2726   if (button == MB_MENU_INITIALIZE)
2727   {
2728     boolean new_element = TRUE;
2729
2730     num_anims = 0;
2731
2732     for (i = 0; helpanim_info[i].element != HELPANIM_LIST_END; i++)
2733     {
2734       if (helpanim_info[i].element == HELPANIM_LIST_NEXT)
2735         new_element = TRUE;
2736       else if (new_element)
2737       {
2738         num_anims++;
2739         new_element = FALSE;
2740       }
2741     }
2742
2743     num_pages = (num_anims + anims_per_page - 1) / anims_per_page;
2744     page = 0;
2745   }
2746
2747   if (button == MB_MENU_LEAVE)
2748   {
2749     PlaySound(SND_MENU_ITEM_SELECTING);
2750
2751     info_mode = INFO_MODE_MAIN;
2752     DrawInfoScreen();
2753
2754     return;
2755   }
2756   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
2757   {
2758     if (button != MB_MENU_INITIALIZE)
2759     {
2760       PlaySound(SND_MENU_ITEM_SELECTING);
2761
2762       page++;
2763     }
2764
2765     if (page >= num_pages)
2766     {
2767       FadeSoundsAndMusic();
2768
2769       info_mode = INFO_MODE_MAIN;
2770       DrawInfoScreen();
2771
2772       return;
2773     }
2774
2775     if (page > 0)
2776       FadeSetNextScreen();
2777
2778     if (button != MB_MENU_INITIALIZE)
2779       FadeOut(REDRAW_FIELD);
2780
2781     DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, TRUE);
2782
2783     if (button != MB_MENU_INITIALIZE)
2784       FadeIn(REDRAW_FIELD);
2785   }
2786   else
2787   {
2788     if (DelayReached(&info_delay, GameFrameDelay))
2789       if (page < num_pages)
2790         DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, FALSE);
2791
2792     PlayMenuSoundIfLoop();
2793   }
2794 }
2795
2796 void DrawInfoScreen_Music()
2797 {
2798   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_MUSIC);
2799
2800   FadeOut(REDRAW_FIELD);
2801
2802   ClearField();
2803   DrawHeadline();
2804
2805   LoadMusicInfo();
2806
2807   HandleInfoScreen_Music(MB_MENU_INITIALIZE);
2808
2809   FadeIn(REDRAW_FIELD);
2810 }
2811
2812 void HandleInfoScreen_Music(int button)
2813 {
2814   static struct MusicFileInfo *list = NULL;
2815   int ystart1 = mSY - SY + 100;
2816   int ystart2 = mSY - SY + 150;
2817   int ybottom = mSY - SY + SYSIZE - 20;
2818   int dy = 30;
2819
2820   if (button == MB_MENU_INITIALIZE)
2821   {
2822     list = music_file_info;
2823
2824     if (list == NULL)
2825     {
2826       FadeSoundsAndMusic();
2827
2828       ClearField();
2829       DrawHeadline();
2830
2831       DrawTextSCentered(ystart1, FONT_TEXT_1,
2832                         "No music info for this level set.");
2833
2834       DrawTextSCentered(ybottom, FONT_TEXT_4,
2835                         "Press any key or button for info menu");
2836
2837       return;
2838     }
2839   }
2840
2841   if (button == MB_MENU_LEAVE)
2842   {
2843     PlaySound(SND_MENU_ITEM_SELECTING);
2844
2845     FadeSoundsAndMusic();
2846
2847     info_mode = INFO_MODE_MAIN;
2848     DrawInfoScreen();
2849
2850     return;
2851   }
2852   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
2853   {
2854     int y = 0;
2855
2856     if (button != MB_MENU_INITIALIZE)
2857     {
2858       PlaySound(SND_MENU_ITEM_SELECTING);
2859
2860       if (list != NULL)
2861         list = list->next;
2862     }
2863
2864     if (list == NULL)
2865     {
2866       FadeSoundsAndMusic();
2867
2868       info_mode = INFO_MODE_MAIN;
2869       DrawInfoScreen();
2870
2871       return;
2872     }
2873
2874     FadeSoundsAndMusic();
2875
2876     if (list != music_file_info)
2877       FadeSetNextScreen();
2878
2879     if (button != MB_MENU_INITIALIZE)
2880       FadeOut(REDRAW_FIELD);
2881
2882     ClearField();
2883     DrawHeadline();
2884
2885     if (list->is_sound)
2886     {
2887       int sound = list->music;
2888
2889       if (sound_info[sound].loop)
2890         PlaySoundLoop(sound);
2891       else
2892         PlaySound(sound);
2893
2894       DrawTextSCentered(ystart1, FONT_TEXT_1, "The Game Background Sounds:");
2895     }
2896     else
2897     {
2898       PlayMusic(list->music);
2899
2900       DrawTextSCentered(ystart1, FONT_TEXT_1, "The Game Background Music:");
2901     }
2902
2903     if (!strEqual(list->title, UNKNOWN_NAME))
2904     {
2905       if (!strEqual(list->title_header, UNKNOWN_NAME))
2906         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, list->title_header);
2907
2908       DrawTextFCentered(ystart2 + y++ * dy, FONT_TEXT_3, "\"%s\"", list->title);
2909     }
2910
2911     if (!strEqual(list->artist, UNKNOWN_NAME))
2912     {
2913       if (!strEqual(list->artist_header, UNKNOWN_NAME))
2914         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, list->artist_header);
2915       else
2916         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, "by");
2917
2918       DrawTextFCentered(ystart2 + y++ * dy, FONT_TEXT_3, "%s", list->artist);
2919     }
2920
2921     if (!strEqual(list->album, UNKNOWN_NAME))
2922     {
2923       if (!strEqual(list->album_header, UNKNOWN_NAME))
2924         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, list->album_header);
2925       else
2926         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, "from the album");
2927
2928       DrawTextFCentered(ystart2 + y++ * dy, FONT_TEXT_3, "\"%s\"", list->album);
2929     }
2930
2931     if (!strEqual(list->year, UNKNOWN_NAME))
2932     {
2933       if (!strEqual(list->year_header, UNKNOWN_NAME))
2934         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, list->year_header);
2935       else
2936         DrawTextSCentered(ystart2 + y++ * dy, FONT_TEXT_2, "from the year");
2937
2938       DrawTextFCentered(ystart2 + y++ * dy, FONT_TEXT_3, "%s", list->year);
2939     }
2940
2941     DrawTextSCentered(ybottom, FONT_TEXT_4,
2942                       "Press any key or button for next page");
2943
2944     if (button != MB_MENU_INITIALIZE)
2945       FadeIn(REDRAW_FIELD);
2946   }
2947
2948   if (list != NULL && list->is_sound && sound_info[list->music].loop)
2949     PlaySoundLoop(list->music);
2950 }
2951
2952 static void DrawInfoScreen_CreditsScreen(int screen_nr)
2953 {
2954   int ystart1 = mSY - SY + 100;
2955   int ystart2 = mSY - SY + 150;
2956   int ybottom = mSY - SY + SYSIZE - 20;
2957   int ystep = 30;
2958
2959   ClearField();
2960   DrawHeadline();
2961
2962   DrawTextSCentered(ystart1, FONT_TEXT_1, "Credits:");
2963
2964   if (screen_nr == 0)
2965   {
2966     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2967                       "Special thanks to");
2968     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2969                       "Peter Liepa");
2970     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2971                       "for creating");
2972     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2973                       "\"Boulder Dash\"");
2974     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2975                       "in the year");
2976     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2977                       "1984");
2978     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
2979                       "published by");
2980     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_3,
2981                       "First Star Software");
2982   }
2983   else if (screen_nr == 1)
2984   {
2985     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
2986                       "Special thanks to");
2987     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
2988                       "Klaus Heinz & Volker Wertich");
2989     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
2990                       "for creating");
2991     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
2992                       "\"Emerald Mine\"");
2993     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
2994                       "in the year");
2995     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
2996                       "1987");
2997     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
2998                       "published by");
2999     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_3,
3000                       "Kingsoft");
3001   }
3002   else if (screen_nr == 2)
3003   {
3004     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
3005                       "Special thanks to");
3006     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
3007                       "Michael Stopp & Philip Jespersen");
3008     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
3009                       "for creating");
3010     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
3011                       "\"Supaplex\"");
3012     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
3013                       "in the year");
3014     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
3015                       "1991");
3016     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
3017                       "published by");
3018     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_3,
3019                       "Digital Integration");
3020   }
3021   else if (screen_nr == 3)
3022   {
3023     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
3024                       "Special thanks to");
3025     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
3026                       "Hiroyuki Imabayashi");
3027     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
3028                       "for creating");
3029     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
3030                       "\"Sokoban\"");
3031     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
3032                       "in the year");
3033     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
3034                       "1982");
3035     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
3036                       "published by");
3037     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_3,
3038                       "Thinking Rabbit");
3039   }
3040   else if (screen_nr == 4)
3041   {
3042     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
3043                       "Special thanks to");
3044     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
3045                       "Alan Bond");
3046     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
3047                       "and");
3048     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
3049                       "J\xfcrgen Bonhagen");
3050     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
3051                       "for the continuous creation");
3052     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_2,
3053                       "of outstanding level sets");
3054   }
3055   else if (screen_nr == 5)
3056   {
3057     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
3058                       "Thanks to");
3059     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
3060                       "Peter Elzner");
3061     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
3062                       "for ideas and inspiration by");
3063     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
3064                       "Diamond Caves");
3065
3066     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_2,
3067                       "Thanks to");
3068     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_3,
3069                       "Steffest");
3070     DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_2,
3071                       "for ideas and inspiration by");
3072     DrawTextSCentered(ystart2 + 8 * ystep, FONT_TEXT_3,
3073                       "DX-Boulderdash");
3074   }
3075   else if (screen_nr == 6)
3076   {
3077     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
3078                       "Thanks to");
3079     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
3080                       "David Tritscher");
3081 #if 1
3082     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
3083                       "for the code base used for the");
3084     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_2,
3085                       "native Emerald Mine engine");
3086 #else
3087     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
3088                       "for the new Emerald Mine engine");
3089 #endif
3090   }
3091   else if (screen_nr == 7)
3092   {
3093     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
3094                       "Thanks to");
3095     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_3,
3096                       "Guido Schulz");
3097     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_2,
3098                       "for the initial DOS port");
3099
3100     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
3101                       "Thanks to");
3102     DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
3103                       "Karl H\xf6rnell");
3104     DrawTextSCentered(ystart2 + 6 * ystep, FONT_TEXT_2,
3105                       "for some additional toons");
3106   }
3107   else if (screen_nr == 8)
3108   {
3109     DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
3110                       "And not to forget:");
3111     DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_2,
3112                       "Many thanks to");
3113     DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_3,
3114                       "All those who contributed");
3115     DrawTextSCentered(ystart2 + 3 * ystep, FONT_TEXT_3,
3116                       "levels to this game");
3117     DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_3,
3118                       "since 1995");
3119   }
3120
3121   DrawTextSCentered(ybottom, FONT_TEXT_4,
3122                     "Press any key or button for next page");
3123 }
3124
3125 void DrawInfoScreen_Credits()
3126 {
3127   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_CREDITS);
3128
3129   FadeSoundsAndMusic();
3130
3131   FadeOut(REDRAW_FIELD);
3132
3133   HandleInfoScreen_Credits(MB_MENU_INITIALIZE);
3134
3135   FadeIn(REDRAW_FIELD);
3136 }
3137
3138 void HandleInfoScreen_Credits(int button)
3139 {
3140   static int screen_nr = 0;
3141   int num_screens = 9;
3142
3143   if (button == MB_MENU_INITIALIZE)
3144   {
3145     screen_nr = 0;
3146
3147     // DrawInfoScreen_CreditsScreen(screen_nr);
3148   }
3149
3150   if (button == MB_MENU_LEAVE)
3151   {
3152     PlaySound(SND_MENU_ITEM_SELECTING);
3153
3154     info_mode = INFO_MODE_MAIN;
3155     DrawInfoScreen();
3156
3157     return;
3158   }
3159   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
3160   {
3161     if (button != MB_MENU_INITIALIZE)
3162     {
3163       PlaySound(SND_MENU_ITEM_SELECTING);
3164
3165       screen_nr++;
3166     }
3167
3168     if (screen_nr >= num_screens)
3169     {
3170       FadeSoundsAndMusic();
3171
3172       info_mode = INFO_MODE_MAIN;
3173       DrawInfoScreen();
3174
3175       return;
3176     }
3177
3178     if (screen_nr > 0)
3179       FadeSetNextScreen();
3180
3181     if (button != MB_MENU_INITIALIZE)
3182       FadeOut(REDRAW_FIELD);
3183
3184     DrawInfoScreen_CreditsScreen(screen_nr);
3185
3186     if (button != MB_MENU_INITIALIZE)
3187       FadeIn(REDRAW_FIELD);
3188   }
3189   else
3190   {
3191     PlayMenuSoundIfLoop();
3192   }
3193 }
3194
3195 void DrawInfoScreen_Program()
3196 {
3197   int ystart1 = mSY - SY + 100;
3198   int ystart2 = mSY - SY + 150;
3199   int ybottom = mSY - SY + SYSIZE - 20;
3200   int ystep = 30;
3201
3202   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_PROGRAM);
3203
3204   FadeOut(REDRAW_FIELD);
3205
3206   ClearField();
3207   DrawHeadline();
3208
3209   DrawTextSCentered(ystart1, FONT_TEXT_1, "Program Information:");
3210
3211   DrawTextSCentered(ystart2 + 0 * ystep, FONT_TEXT_2,
3212                     "This game is Freeware!");
3213   DrawTextSCentered(ystart2 + 1 * ystep, FONT_TEXT_2,
3214                     "If you like it, send e-mail to:");
3215   DrawTextSCentered(ystart2 + 2 * ystep, FONT_TEXT_3,
3216                     setup.internal.program_email);
3217   DrawTextSCentered(ystart2 + 4 * ystep, FONT_TEXT_2,
3218                     "More information and levels:");
3219   DrawTextSCentered(ystart2 + 5 * ystep, FONT_TEXT_3,
3220                     setup.internal.program_website);
3221   DrawTextSCentered(ystart2 + 7 * ystep, FONT_TEXT_2,
3222                     "If you have created new levels,");
3223   DrawTextSCentered(ystart2 + 8 * ystep, FONT_TEXT_2,
3224                     "send them to me to include them!");
3225   DrawTextSCentered(ystart2 + 9 * ystep, FONT_TEXT_2,
3226                     ":-)");
3227
3228   DrawTextSCentered(ybottom, FONT_TEXT_4,
3229                     "Press any key or button for info menu");
3230
3231   FadeIn(REDRAW_FIELD);
3232 }
3233
3234 void HandleInfoScreen_Program(int button)
3235 {
3236   if (button == MB_MENU_LEAVE)
3237   {
3238     PlaySound(SND_MENU_ITEM_SELECTING);
3239
3240     info_mode = INFO_MODE_MAIN;
3241     DrawInfoScreen();
3242
3243     return;
3244   }
3245   else if (button == MB_MENU_CHOICE)
3246   {
3247     PlaySound(SND_MENU_ITEM_SELECTING);
3248
3249     FadeSoundsAndMusic();
3250
3251     info_mode = INFO_MODE_MAIN;
3252     DrawInfoScreen();
3253   }
3254   else
3255   {
3256     PlayMenuSoundIfLoop();
3257   }
3258 }
3259
3260 void DrawInfoScreen_Version()
3261 {
3262   int font_header = FONT_TEXT_3;
3263   int font_text = FONT_TEXT_2;
3264   int xstep = getFontWidth(font_text);
3265   int ystep = getFontHeight(font_text);
3266   int ystart1 = mSY - SY + 100;
3267   int ystart2 = mSY - SY + 150;
3268   int ybottom = mSY - SY + SYSIZE - 20;
3269   int xstart1 = mSX - SX + 2 * xstep;
3270   int xstart2 = mSX - SX + 18 * xstep;
3271   int xstart3 = mSX - SX + 28 * xstep;
3272   SDL_version sdl_version_compiled;
3273   const SDL_version *sdl_version_linked;
3274   int driver_name_len = 10;
3275 #if defined(TARGET_SDL2)
3276   SDL_version sdl_version_linked_ext;
3277   const char *driver_name = NULL;
3278 #else
3279   char driver_name[driver_name_len];
3280 #endif
3281
3282   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_VERSION);
3283
3284   FadeOut(REDRAW_FIELD);
3285
3286   ClearField();
3287   DrawHeadline();
3288
3289   DrawTextSCentered(ystart1, FONT_TEXT_1, "Version Information:");
3290
3291   DrawTextF(xstart1, ystart2, font_header, "Name");
3292   DrawTextF(xstart2, ystart2, font_text, getProgramTitleString());
3293
3294   ystart2 += ystep;
3295   DrawTextF(xstart1, ystart2, font_header, "Version");
3296   DrawTextF(xstart2, ystart2, font_text, getProgramVersionString());
3297
3298   ystart2 += ystep;
3299   DrawTextF(xstart1, ystart2, font_header, "Platform");
3300   DrawTextF(xstart2, ystart2, font_text, PLATFORM_STRING);
3301
3302   ystart2 += ystep;
3303   DrawTextF(xstart1, ystart2, font_header, "Target");
3304   DrawTextF(xstart2, ystart2, font_text, TARGET_STRING);
3305
3306   ystart2 += ystep;
3307   DrawTextF(xstart1, ystart2, font_header, "Compile time");
3308   DrawTextF(xstart2, ystart2, font_text, getCompileDateString());
3309
3310   ystart2 += 3 * ystep;
3311   DrawTextF(xstart1, ystart2, font_header, "Library");
3312   DrawTextF(xstart2, ystart2, font_header, "compiled");
3313   DrawTextF(xstart3, ystart2, font_header, "linked");
3314
3315   SDL_VERSION(&sdl_version_compiled);
3316 #if defined(TARGET_SDL2)
3317   SDL_GetVersion(&sdl_version_linked_ext);
3318   sdl_version_linked = &sdl_version_linked_ext;
3319 #else
3320   sdl_version_linked = SDL_Linked_Version();
3321 #endif
3322
3323   ystart2 += 2 * ystep;
3324   DrawTextF(xstart1, ystart2, font_text, "SDL");
3325   DrawTextF(xstart2, ystart2, font_text, "%d.%d.%d",
3326             sdl_version_compiled.major,
3327             sdl_version_compiled.minor,
3328             sdl_version_compiled.patch);
3329   DrawTextF(xstart3, ystart2, font_text, "%d.%d.%d",
3330             sdl_version_linked->major,
3331             sdl_version_linked->minor,
3332             sdl_version_linked->patch);
3333
3334   SDL_IMAGE_VERSION(&sdl_version_compiled);
3335   sdl_version_linked = IMG_Linked_Version();
3336
3337   ystart2 += ystep;
3338   DrawTextF(xstart1, ystart2, font_text, "SDL_image");
3339   DrawTextF(xstart2, ystart2, font_text, "%d.%d.%d",
3340             sdl_version_compiled.major,
3341             sdl_version_compiled.minor,
3342             sdl_version_compiled.patch);
3343   DrawTextF(xstart3, ystart2, font_text, "%d.%d.%d",
3344             sdl_version_linked->major,
3345             sdl_version_linked->minor,
3346             sdl_version_linked->patch);
3347
3348   SDL_MIXER_VERSION(&sdl_version_compiled);
3349   sdl_version_linked = Mix_Linked_Version();
3350
3351   ystart2 += ystep;
3352   DrawTextF(xstart1, ystart2, font_text, "SDL_mixer");
3353   DrawTextF(xstart2, ystart2, font_text, "%d.%d.%d",
3354             sdl_version_compiled.major,
3355             sdl_version_compiled.minor,
3356             sdl_version_compiled.patch);
3357   DrawTextF(xstart3, ystart2, font_text, "%d.%d.%d",
3358             sdl_version_linked->major,
3359             sdl_version_linked->minor,
3360             sdl_version_linked->patch);
3361
3362   SDL_NET_VERSION(&sdl_version_compiled);
3363   sdl_version_linked = SDLNet_Linked_Version();
3364
3365   ystart2 += ystep;
3366   DrawTextF(xstart1, ystart2, font_text, "SDL_net");
3367   DrawTextF(xstart2, ystart2, font_text, "%d.%d.%d",
3368             sdl_version_compiled.major,
3369             sdl_version_compiled.minor,
3370             sdl_version_compiled.patch);
3371   DrawTextF(xstart3, ystart2, font_text, "%d.%d.%d",
3372             sdl_version_linked->major,
3373             sdl_version_linked->minor,
3374             sdl_version_linked->patch);
3375
3376   ystart2 += 3 * ystep;
3377   DrawTextF(xstart1, ystart2, font_header, "Driver");
3378   DrawTextF(xstart2, ystart2, font_header, "Requested");
3379   DrawTextF(xstart3, ystart2, font_header, "Used");
3380
3381 #if defined(TARGET_SDL2)
3382   driver_name = getStringCopyNStatic(SDL_GetVideoDriver(0), driver_name_len);
3383 #else
3384   SDL_VideoDriverName(driver_name, driver_name_len);
3385 #endif
3386
3387   ystart2 += 2 * ystep;
3388   DrawTextF(xstart1, ystart2, font_text, "SDL_VideoDriver");
3389   DrawTextF(xstart2, ystart2, font_text, "%s", setup.system.sdl_videodriver);
3390   DrawTextF(xstart3, ystart2, font_text, "%s", driver_name);
3391
3392 #if defined(TARGET_SDL2)
3393   driver_name = getStringCopyNStatic(SDL_GetAudioDriver(0), driver_name_len);
3394 #else
3395   SDL_AudioDriverName(driver_name, driver_name_len);
3396 #endif
3397
3398   ystart2 += ystep;
3399   DrawTextF(xstart1, ystart2, font_text, "SDL_AudioDriver");
3400   DrawTextF(xstart2, ystart2, font_text, "%s", setup.system.sdl_audiodriver);
3401   DrawTextF(xstart3, ystart2, font_text, "%s", driver_name);
3402
3403   DrawTextSCentered(ybottom, FONT_TEXT_4,
3404                     "Press any key or button for info menu");
3405
3406   FadeIn(REDRAW_FIELD);
3407 }
3408
3409 void HandleInfoScreen_Version(int button)
3410 {
3411   if (button == MB_MENU_LEAVE)
3412   {
3413     PlaySound(SND_MENU_ITEM_SELECTING);
3414
3415     info_mode = INFO_MODE_MAIN;
3416     DrawInfoScreen();
3417
3418     return;
3419   }
3420   else if (button == MB_MENU_CHOICE)
3421   {
3422     PlaySound(SND_MENU_ITEM_SELECTING);
3423
3424     FadeSoundsAndMusic();
3425
3426     info_mode = INFO_MODE_MAIN;
3427     DrawInfoScreen();
3428   }
3429   else
3430   {
3431     PlayMenuSoundIfLoop();
3432   }
3433 }
3434
3435 void DrawInfoScreen_LevelSet()
3436 {
3437   struct TitleMessageInfo *tmi = &readme;
3438   char *filename = getLevelSetInfoFilename();
3439   char *title = "Level Set Information:";
3440   int ystart1 = mSY - SY + 100;
3441   int ybottom = mSY - SY + SYSIZE - 20;
3442
3443   if (filename == NULL)
3444   {
3445     DrawInfoScreen_NotAvailable(title, "No information for this level set.");
3446
3447     return;
3448   }
3449
3450   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_LEVELSET);
3451
3452   FadeOut(REDRAW_FIELD);
3453
3454   ClearField();
3455   DrawHeadline();
3456
3457   DrawTextSCentered(ystart1, FONT_TEXT_1, title);
3458
3459   /* if x position set to "-1", automatically determine by playfield width */
3460   if (tmi->x == -1)
3461     tmi->x = SXSIZE / 2;
3462
3463   /* if y position set to "-1", use static default value */
3464   if (tmi->y == -1)
3465     tmi->y = 150;
3466
3467   /* if width set to "-1", automatically determine by playfield width */
3468   if (tmi->width == -1)
3469     tmi->width = SXSIZE - 2 * TILEX;
3470
3471   /* if height set to "-1", automatically determine by playfield height */
3472   if (tmi->height == -1)
3473     tmi->height = SYSIZE - 20 - tmi->y - 10;
3474
3475   /* if chars set to "-1", automatically determine by text and font width */
3476   if (tmi->chars == -1)
3477     tmi->chars = tmi->width / getFontWidth(tmi->font);
3478   else
3479     tmi->width = tmi->chars * getFontWidth(tmi->font);
3480
3481   /* if lines set to "-1", automatically determine by text and font height */
3482   if (tmi->lines == -1)
3483     tmi->lines = tmi->height / getFontHeight(tmi->font);
3484   else
3485     tmi->height = tmi->lines * getFontHeight(tmi->font);
3486
3487   DrawTextFile(mSX + ALIGNED_TEXT_XPOS(tmi), mSY + ALIGNED_TEXT_YPOS(tmi),
3488                filename, tmi->font, tmi->chars, -1, tmi->lines, 0, -1,
3489                tmi->autowrap, tmi->centered, tmi->parse_comments);
3490
3491   DrawTextSCentered(ybottom, FONT_TEXT_4,
3492                     "Press any key or button for info menu");
3493
3494   FadeIn(REDRAW_FIELD);
3495 }
3496
3497 void HandleInfoScreen_LevelSet(int button)
3498 {
3499   if (button == MB_MENU_LEAVE)
3500   {
3501     PlaySound(SND_MENU_ITEM_SELECTING);
3502
3503     info_mode = INFO_MODE_MAIN;
3504     DrawInfoScreen();
3505
3506     return;
3507   }
3508   else if (button == MB_MENU_CHOICE)
3509   {
3510     PlaySound(SND_MENU_ITEM_SELECTING);
3511
3512     FadeSoundsAndMusic();
3513
3514     info_mode = INFO_MODE_MAIN;
3515     DrawInfoScreen();
3516   }
3517   else
3518   {
3519     PlayMenuSoundIfLoop();
3520   }
3521 }
3522
3523 static void DrawInfoScreen()
3524 {
3525   SetMainBackgroundImage(IMG_BACKGROUND_INFO);
3526
3527   if (info_mode == INFO_MODE_TITLE)
3528     DrawInfoScreen_TitleScreen();
3529   else if (info_mode == INFO_MODE_ELEMENTS)
3530     DrawInfoScreen_Elements();
3531   else if (info_mode == INFO_MODE_MUSIC)
3532     DrawInfoScreen_Music();
3533   else if (info_mode == INFO_MODE_CREDITS)
3534     DrawInfoScreen_Credits();
3535   else if (info_mode == INFO_MODE_PROGRAM)
3536     DrawInfoScreen_Program();
3537   else if (info_mode == INFO_MODE_VERSION)
3538     DrawInfoScreen_Version();
3539   else if (info_mode == INFO_MODE_LEVELSET)
3540     DrawInfoScreen_LevelSet();
3541   else
3542     DrawInfoScreen_Main();
3543
3544   if (info_mode != INFO_MODE_MAIN &&
3545       info_mode != INFO_MODE_TITLE &&
3546       info_mode != INFO_MODE_MUSIC)
3547   {
3548     PlayMenuSound();
3549     PlayMenuMusic();
3550   }
3551 }
3552
3553 void HandleInfoScreen(int mx, int my, int dx, int dy, int button)
3554 {
3555   if (info_mode == INFO_MODE_TITLE)
3556     HandleInfoScreen_TitleScreen(button);
3557   else if (info_mode == INFO_MODE_ELEMENTS)
3558     HandleInfoScreen_Elements(button);
3559   else if (info_mode == INFO_MODE_MUSIC)
3560     HandleInfoScreen_Music(button);
3561   else if (info_mode == INFO_MODE_CREDITS)
3562     HandleInfoScreen_Credits(button);
3563   else if (info_mode == INFO_MODE_PROGRAM)
3564     HandleInfoScreen_Program(button);
3565   else if (info_mode == INFO_MODE_VERSION)
3566     HandleInfoScreen_Version(button);
3567   else if (info_mode == INFO_MODE_LEVELSET)
3568     HandleInfoScreen_LevelSet(button);
3569   else
3570     HandleInfoScreen_Main(mx, my, dx, dy, button);
3571 }
3572
3573
3574 /* ========================================================================= */
3575 /* type name functions                                                       */
3576 /* ========================================================================= */
3577
3578 void HandleTypeName(int newxpos, Key key)
3579 {
3580   static char last_player_name[MAX_PLAYER_NAME_LEN + 1];
3581   struct MainControlInfo *mci = getMainControlInfo(MAIN_CONTROL_NAME);
3582   struct TextPosInfo *pos = mci->pos_input;
3583   int startx = mSX + ALIGNED_TEXT_XPOS(pos);
3584   int starty = mSY + ALIGNED_TEXT_YPOS(pos);
3585   static int xpos = 0;
3586   int font_nr = pos->font;
3587   int font_active_nr = FONT_ACTIVE(font_nr);
3588   int font_width = getFontWidth(font_active_nr);
3589   char key_char = getValidConfigValueChar(getCharFromKey(key));
3590   boolean is_valid_key_char = (key_char != 0 && (key_char != ' ' || xpos > 0));
3591   boolean is_active = TRUE;
3592
3593   DrawBackgroundForFont(startx,starty, pos->width, pos->height, font_active_nr);
3594
3595   if (newxpos)
3596   {
3597     strcpy(last_player_name, setup.player_name);
3598
3599     xpos = newxpos;
3600
3601 #if defined(TARGET_SDL2)
3602     SDL_StartTextInput();
3603 #endif
3604   }
3605   else if (is_valid_key_char && xpos < MAX_PLAYER_NAME_LEN)
3606   {
3607     setup.player_name[xpos] = key_char;
3608     setup.player_name[xpos + 1] = 0;
3609
3610     xpos++;
3611   }
3612   else if ((key == KSYM_Delete || key == KSYM_BackSpace) && xpos > 0)
3613   {
3614     xpos--;
3615
3616     setup.player_name[xpos] = 0;
3617   }
3618   else if (key == KSYM_Return && xpos > 0)
3619   {
3620     SaveSetup();
3621
3622     is_active = FALSE;
3623
3624     SetGameStatus(GAME_MODE_MAIN);
3625   }
3626   else if (key == KSYM_Escape)
3627   {
3628     strcpy(setup.player_name, last_player_name);
3629
3630     is_active = FALSE;
3631
3632     SetGameStatus(GAME_MODE_MAIN);
3633   }
3634
3635   if (is_active)
3636   {
3637     pos->width = (strlen(setup.player_name) + 1) * font_width;
3638     startx = mSX + ALIGNED_TEXT_XPOS(pos);
3639
3640     DrawText(startx, starty, setup.player_name, font_active_nr);
3641     DrawText(startx + xpos * font_width, starty, "_", font_active_nr);
3642   }
3643   else
3644   {
3645     pos->width = strlen(setup.player_name) * font_width;
3646     startx = mSX + ALIGNED_TEXT_XPOS(pos);
3647
3648     DrawText(startx, starty, setup.player_name, font_nr);
3649
3650 #if defined(TARGET_SDL2)
3651     SDL_StopTextInput();
3652 #endif
3653   }
3654 }
3655
3656
3657 /* ========================================================================= */
3658 /* tree menu functions                                                       */
3659 /* ========================================================================= */
3660
3661 static void DrawChooseTree(TreeInfo **ti_ptr)
3662 {
3663   int fade_mask = REDRAW_FIELD;
3664
3665   if (CheckIfGlobalBorderHasChanged())
3666     fade_mask = REDRAW_ALL;
3667
3668   if (strEqual((*ti_ptr)->subdir, STRING_TOP_DIRECTORY))
3669   {
3670     SetGameStatus(GAME_MODE_MAIN);
3671
3672     DrawMainMenu();
3673
3674     return;
3675   }
3676
3677   UnmapAllGadgets();
3678
3679   FreeScreenGadgets();
3680   CreateScreenGadgets();
3681
3682   FadeOut(fade_mask);
3683
3684   ClearField();
3685
3686   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
3687
3688   HandleChooseTree(0, 0, 0, 0, MB_MENU_INITIALIZE, ti_ptr);
3689   MapScreenTreeGadgets(*ti_ptr);
3690
3691   DrawMaskedBorder(fade_mask);
3692
3693   FadeIn(fade_mask);
3694
3695   InitAnimation();
3696 }
3697
3698 static void drawChooseTreeList(int first_entry, int num_page_entries,
3699                                TreeInfo *ti)
3700 {
3701   int i;
3702   char *title_string = NULL;
3703   int yoffset_sets = MENU_TITLE1_YPOS;
3704   int yoffset_setup = 16;
3705   int yoffset = (ti->type == TREE_TYPE_LEVEL_DIR ||
3706                  ti->type == TREE_TYPE_LEVEL_NR ? yoffset_sets : yoffset_setup);
3707
3708   title_string = ti->infotext;
3709
3710   DrawTextSCentered(mSY - SY + yoffset, FONT_TITLE_1, title_string);
3711
3712   clearMenuListArea();
3713
3714   for (i = 0; i < num_page_entries; i++)
3715   {
3716     TreeInfo *node, *node_first;
3717     int entry_pos = first_entry + i;
3718     int xpos = MENU_SCREEN_START_XPOS;
3719     int ypos = MENU_SCREEN_START_YPOS + i;
3720     int startx = mSX + xpos * 32;
3721     int starty = mSY + ypos * 32;
3722     int font_nr = FONT_TEXT_1;
3723     int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
3724     int startx_text = startx + font_xoffset;
3725     int startx_scrollbar = mSX + SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
3726     int text_size = startx_scrollbar - startx_text;
3727     int max_buffer_len = text_size / getFontWidth(font_nr);
3728     char buffer[max_buffer_len + 1];
3729
3730     node_first = getTreeInfoFirstGroupEntry(ti);
3731     node = getTreeInfoFromPos(node_first, entry_pos);
3732
3733     strncpy(buffer, node->name, max_buffer_len);
3734     buffer[max_buffer_len] = '\0';
3735
3736     DrawText(startx, starty, buffer, font_nr + node->color);
3737
3738     if (node->parent_link)
3739       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
3740     else if (node->level_group)
3741       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
3742     else
3743       initCursor(i, IMG_MENU_BUTTON);
3744   }
3745
3746   redraw_mask |= REDRAW_FIELD;
3747 }
3748
3749 static void drawChooseTreeInfo(int entry_pos, TreeInfo *ti)
3750 {
3751   TreeInfo *node, *node_first;
3752   int x, last_redraw_mask = redraw_mask;
3753   int ypos = MENU_TITLE2_YPOS;
3754   int font_nr = FONT_TITLE_2;
3755
3756   if (ti->type == TREE_TYPE_LEVEL_NR)
3757     DrawTextFCentered(ypos, font_nr, leveldir_current->name);
3758
3759   if (ti->type != TREE_TYPE_LEVEL_DIR)
3760     return;
3761
3762   node_first = getTreeInfoFirstGroupEntry(ti);
3763   node = getTreeInfoFromPos(node_first, entry_pos);
3764
3765   DrawBackgroundForFont(SX, SY + ypos, SXSIZE, getFontHeight(font_nr), font_nr);
3766
3767   if (node->parent_link)
3768     DrawTextFCentered(ypos, font_nr, "leave \"%s\"",
3769                       node->node_parent->name);
3770   else if (node->level_group)
3771     DrawTextFCentered(ypos, font_nr, "enter \"%s\"",
3772                       node->name);
3773   else if (ti->type == TREE_TYPE_LEVEL_DIR)
3774     DrawTextFCentered(ypos, font_nr, "%3d %s (%s)",
3775                       node->levels, (node->levels > 1 ? "levels" : "level"),
3776                       node->class_desc);
3777
3778   /* let BackToFront() redraw only what is needed */
3779   redraw_mask = last_redraw_mask;
3780   for (x = 0; x < SCR_FIELDX; x++)
3781     MarkTileDirty(x, 1);
3782 }
3783
3784 static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
3785                              TreeInfo **ti_ptr)
3786 {
3787   TreeInfo *ti = *ti_ptr;
3788   int x = 0;
3789   int y = ti->cl_cursor;
3790   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
3791   int num_entries = numTreeInfoInGroup(ti);
3792   int num_page_entries;
3793   boolean position_set_by_scrollbar = (dx == 999);
3794
3795   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
3796     num_page_entries = num_entries;
3797   else
3798     num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
3799
3800   if (button == MB_MENU_INITIALIZE)
3801   {
3802     int num_entries = numTreeInfoInGroup(ti);
3803     int entry_pos = posTreeInfo(ti);
3804
3805     if (ti->cl_first == -1)
3806     {
3807       /* only on initialization */
3808       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
3809       ti->cl_cursor = entry_pos - ti->cl_first;
3810     }
3811     else if (ti->cl_cursor >= num_page_entries ||
3812              (num_entries > num_page_entries &&
3813               num_entries - ti->cl_first < num_page_entries))
3814     {
3815       /* only after change of list size (by custom graphic configuration) */
3816       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
3817       ti->cl_cursor = entry_pos - ti->cl_first;
3818     }
3819
3820     if (position_set_by_scrollbar)
3821       ti->cl_first = dy;
3822     else
3823       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
3824                                 ti->cl_first, ti);
3825
3826     drawChooseTreeList(ti->cl_first, num_page_entries, ti);
3827     drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
3828     drawChooseTreeCursor(ti->cl_cursor, TRUE);
3829
3830     return;
3831   }
3832   else if (button == MB_MENU_LEAVE)
3833   {
3834     FadeSetLeaveMenu();
3835
3836     PlaySound(SND_MENU_ITEM_SELECTING);
3837
3838     if (ti->node_parent)
3839     {
3840       *ti_ptr = ti->node_parent;
3841       DrawChooseTree(ti_ptr);
3842     }
3843     else if (game_status == GAME_MODE_SETUP)
3844     {
3845       if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
3846           setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY ||
3847           setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
3848         execSetupGame();
3849       else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
3850                setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
3851                setup_mode == SETUP_MODE_CHOOSE_RENDERING)
3852         execSetupGraphics();
3853       else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
3854                setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
3855                setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
3856         execSetupSound();
3857       else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
3858                setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
3859                setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
3860         execSetupTouch();
3861       else
3862         execSetupArtwork();
3863     }
3864     else
3865     {
3866       if (game_status == GAME_MODE_LEVELNR)
3867       {
3868         int new_level_nr = atoi(level_number_current->identifier);
3869
3870         HandleMainMenu_SelectLevel(0, 0, new_level_nr);
3871       }
3872
3873       SetGameStatus(GAME_MODE_MAIN);
3874
3875       DrawMainMenu();
3876     }
3877
3878     return;
3879   }
3880
3881   if (mx || my)         /* mouse input */
3882   {
3883     x = (mx - mSX) / 32;
3884     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
3885   }
3886   else if (dx || dy)    /* keyboard or scrollbar/scrollbutton input */
3887   {
3888     /* move cursor instead of scrolling when already at start/end of list */
3889     if (dy == -1 * SCROLL_LINE && ti->cl_first == 0)
3890       dy = -1;
3891     else if (dy == +1 * SCROLL_LINE &&
3892              ti->cl_first + num_page_entries == num_entries)
3893       dy = 1;
3894
3895     /* handle scrolling screen one line or page */
3896     if (ti->cl_cursor + dy < 0 ||
3897         ti->cl_cursor + dy > num_page_entries - 1)
3898     {
3899       boolean redraw = FALSE;
3900
3901       if (ABS(dy) == SCROLL_PAGE)
3902         step = num_page_entries - 1;
3903
3904       if (dy < 0 && ti->cl_first > 0)
3905       {
3906         /* scroll page/line up */
3907
3908         ti->cl_first -= step;
3909         if (ti->cl_first < 0)
3910           ti->cl_first = 0;
3911
3912         redraw = TRUE;
3913       }
3914       else if (dy > 0 && ti->cl_first + num_page_entries < num_entries)
3915       {
3916         /* scroll page/line down */
3917
3918         ti->cl_first += step;
3919         if (ti->cl_first + num_page_entries > num_entries)
3920           ti->cl_first = MAX(0, num_entries - num_page_entries);
3921
3922         redraw = TRUE;
3923       }
3924
3925       if (redraw)
3926       {
3927         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
3928         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
3929         drawChooseTreeCursor(ti->cl_cursor, TRUE);
3930
3931         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
3932                                   ti->cl_first, ti);
3933       }
3934
3935       return;
3936     }
3937
3938     /* handle moving cursor one line */
3939     y = ti->cl_cursor + dy;
3940   }
3941
3942   if (dx == 1)
3943   {
3944     TreeInfo *node_first, *node_cursor;
3945     int entry_pos = ti->cl_first + y;
3946
3947     node_first = getTreeInfoFirstGroupEntry(ti);
3948     node_cursor = getTreeInfoFromPos(node_first, entry_pos);
3949
3950     if (node_cursor->node_group)
3951     {
3952       FadeSetEnterMenu();
3953
3954       PlaySound(SND_MENU_ITEM_SELECTING);
3955
3956       node_cursor->cl_first = ti->cl_first;
3957       node_cursor->cl_cursor = ti->cl_cursor;
3958       *ti_ptr = node_cursor->node_group;
3959       DrawChooseTree(ti_ptr);
3960
3961       return;
3962     }
3963   }
3964   else if (dx == -1 && ti->node_parent)
3965   {
3966     FadeSetLeaveMenu();
3967
3968     PlaySound(SND_MENU_ITEM_SELECTING);
3969
3970     *ti_ptr = ti->node_parent;
3971     DrawChooseTree(ti_ptr);
3972
3973     return;
3974   }
3975
3976   if (!anyScrollbarGadgetActive() &&
3977       IN_VIS_MENU(x, y) &&
3978       mx < screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x &&
3979       y >= 0 && y < num_page_entries)
3980   {
3981     if (button)
3982     {
3983       if (y != ti->cl_cursor)
3984       {
3985         PlaySound(SND_MENU_ITEM_ACTIVATING);
3986
3987         drawChooseTreeCursor(ti->cl_cursor, FALSE);
3988         drawChooseTreeCursor(y, TRUE);
3989         drawChooseTreeInfo(ti->cl_first + y, ti);
3990
3991         ti->cl_cursor = y;
3992       }
3993     }
3994     else
3995     {
3996       TreeInfo *node_first, *node_cursor;
3997       int entry_pos = ti->cl_first + y;
3998
3999       PlaySound(SND_MENU_ITEM_SELECTING);
4000
4001       node_first = getTreeInfoFirstGroupEntry(ti);
4002       node_cursor = getTreeInfoFromPos(node_first, entry_pos);
4003
4004       if (node_cursor->node_group)
4005       {
4006         FadeSetEnterMenu();
4007
4008         node_cursor->cl_first = ti->cl_first;
4009         node_cursor->cl_cursor = ti->cl_cursor;
4010         *ti_ptr = node_cursor->node_group;
4011         DrawChooseTree(ti_ptr);
4012       }
4013       else if (node_cursor->parent_link)
4014       {
4015         FadeSetLeaveMenu();
4016
4017         *ti_ptr = node_cursor->node_parent;
4018         DrawChooseTree(ti_ptr);
4019       }
4020       else
4021       {
4022         FadeSetEnterMenu();
4023
4024         node_cursor->cl_first = ti->cl_first;
4025         node_cursor->cl_cursor = ti->cl_cursor;
4026         *ti_ptr = node_cursor;
4027
4028         if (ti->type == TREE_TYPE_LEVEL_DIR)
4029         {
4030           LoadLevelSetup_SeriesInfo();
4031
4032           SaveLevelSetup_LastSeries();
4033           SaveLevelSetup_SeriesInfo();
4034           TapeErase();
4035         }
4036
4037         if (game_status == GAME_MODE_SETUP)
4038         {
4039           if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
4040               setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY ||
4041               setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
4042             execSetupGame();
4043           else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
4044                    setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
4045                    setup_mode == SETUP_MODE_CHOOSE_RENDERING)
4046             execSetupGraphics();
4047           else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
4048                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
4049                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
4050             execSetupSound();
4051           else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
4052                    setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
4053                    setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
4054             execSetupTouch();
4055           else
4056             execSetupArtwork();
4057         }
4058         else
4059         {
4060           if (game_status == GAME_MODE_LEVELNR)
4061           {
4062             int new_level_nr = atoi(level_number_current->identifier);
4063
4064             HandleMainMenu_SelectLevel(0, 0, new_level_nr);
4065           }
4066
4067           SetGameStatus(GAME_MODE_MAIN);
4068
4069           DrawMainMenu();
4070         }
4071       }
4072     }
4073   }
4074 }
4075
4076 void DrawChooseLevelSet()
4077 {
4078   SetMainBackgroundImage(IMG_BACKGROUND_LEVELS);
4079
4080   DrawChooseTree(&leveldir_current);
4081
4082   PlayMenuSound();
4083   PlayMenuMusic();
4084 }
4085
4086 void HandleChooseLevelSet(int mx, int my, int dx, int dy, int button)
4087 {
4088   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
4089 }
4090
4091 void DrawChooseLevelNr()
4092 {
4093   int i;
4094
4095   if (level_number != NULL)
4096   {
4097     freeTreeInfo(level_number);
4098
4099     level_number = NULL;
4100   }
4101
4102   for (i = leveldir_current->first_level; i <= leveldir_current->last_level;i++)
4103   {
4104     TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_LEVEL_NR);
4105     char identifier[32], name[32];
4106     int value = i;
4107
4108     /* temporarily load level info to get level name */
4109     LoadLevelInfoOnly(i);
4110
4111     ti->node_top = &level_number;
4112     ti->sort_priority = 10000 + value;
4113     ti->color = (level.no_valid_file ? FC_BLUE :
4114                  LevelStats_getSolved(i) ? FC_GREEN :
4115                  LevelStats_getPlayed(i) ? FC_YELLOW : FC_RED);
4116
4117     sprintf(identifier, "%d", value);
4118     sprintf(name, "%03d: %s", value,
4119             (level.no_valid_file ? "(no file)" : level.name));
4120
4121     setString(&ti->identifier, identifier);
4122     setString(&ti->name, name);
4123     setString(&ti->name_sorting, name);
4124
4125     pushTreeInfo(&level_number, ti);
4126   }
4127
4128   /* sort level number values to start with lowest level number */
4129   sortTreeInfo(&level_number);
4130
4131   /* set current level number to current level number */
4132   level_number_current =
4133     getTreeInfoFromIdentifier(level_number, i_to_a(level_nr));
4134
4135   /* if that also fails, set current level number to first available level */
4136   if (level_number_current == NULL)
4137     level_number_current = level_number;
4138
4139   SetMainBackgroundImage(IMG_BACKGROUND_LEVELNR);
4140
4141   DrawChooseTree(&level_number_current);
4142
4143   PlayMenuSound();
4144   PlayMenuMusic();
4145 }
4146
4147 void HandleChooseLevelNr(int mx, int my, int dx, int dy, int button)
4148 {
4149   HandleChooseTree(mx, my, dx, dy, button, &level_number_current);
4150 }
4151
4152 void DrawHallOfFame(int highlight_position)
4153 {
4154   int fade_mask = REDRAW_FIELD;
4155
4156   /* needed if different viewport properties defined for scores */
4157   ChangeViewportPropertiesIfNeeded();
4158
4159   if (CheckIfGlobalBorderHasChanged())
4160     fade_mask = REDRAW_ALL;
4161
4162   UnmapAllGadgets();
4163   FadeSoundsAndMusic();
4164
4165   /* (this is needed when called from GameEnd() after winning a game) */
4166   KeyboardAutoRepeatOn();
4167   ActivateJoystick();
4168
4169   /* (this is needed when called from GameEnd() after winning a game) */
4170   SetDrawDeactivationMask(REDRAW_NONE);
4171   SetDrawBackgroundMask(REDRAW_FIELD);
4172
4173   if (highlight_position < 0) 
4174     LoadScore(level_nr);
4175
4176   FadeSetEnterScreen();
4177
4178   FadeOut(fade_mask);
4179
4180   InitAnimation();
4181
4182   PlayMenuSound();
4183   PlayMenuMusic();
4184
4185   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
4186
4187   HandleHallOfFame(highlight_position, 0, 0, 0, MB_MENU_INITIALIZE);
4188
4189   DrawMaskedBorder(fade_mask);
4190
4191   FadeIn(fade_mask);
4192 }
4193
4194 static void drawHallOfFameList(int first_entry, int highlight_position)
4195 {
4196   int i, j;
4197
4198   SetMainBackgroundImage(IMG_BACKGROUND_SCORES);
4199   ClearField();
4200
4201   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, "Hall Of Fame");
4202   DrawTextFCentered(MENU_TITLE2_YPOS, FONT_TITLE_2,
4203                     "HighScores of Level %d", level_nr);
4204
4205   for (i = 0; i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
4206   {
4207     int entry = first_entry + i;
4208     boolean active = (entry == highlight_position);
4209     int font_nr1 = (active ? FONT_TEXT_1_ACTIVE : FONT_TEXT_1);
4210     int font_nr2 = (active ? FONT_TEXT_2_ACTIVE : FONT_TEXT_2);
4211     int font_nr3 = (active ? FONT_TEXT_3_ACTIVE : FONT_TEXT_3);
4212     int font_nr4 = (active ? FONT_TEXT_4_ACTIVE : FONT_TEXT_4);
4213     int dx1 = 3 * getFontWidth(font_nr1);
4214     int dx2 = dx1 + getFontWidth(font_nr1);
4215     int dx3 = SXSIZE - 2 * (mSX - SX) - 5 * getFontWidth(font_nr4);
4216     int num_dots = (dx3 - dx2) / getFontWidth(font_nr3);
4217     int sy = mSY + 64 + i * 32;
4218
4219     DrawText(mSX, sy, int2str(entry + 1, 3), font_nr1);
4220     DrawText(mSX + dx1, sy, ".", font_nr1);
4221
4222     for (j = 0; j < num_dots; j++)
4223       DrawText(mSX + dx2 + j * getFontWidth(font_nr3), sy, ".", font_nr3);
4224
4225     if (!strEqual(highscore[entry].Name, EMPTY_PLAYER_NAME))
4226       DrawText(mSX + dx2, sy, highscore[entry].Name, font_nr2);
4227
4228     DrawText(mSX + dx3, sy, int2str(highscore[entry].Score, 5), font_nr4);
4229   }
4230
4231   redraw_mask |= REDRAW_FIELD;
4232 }
4233
4234 void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
4235 {
4236   static int first_entry = 0;
4237   static int highlight_position = 0;
4238   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
4239
4240   if (button == MB_MENU_INITIALIZE)
4241   {
4242     first_entry = 0;
4243     highlight_position = mx;
4244     drawHallOfFameList(first_entry, highlight_position);
4245
4246     return;
4247   }
4248
4249   if (ABS(dy) == SCROLL_PAGE)           /* handle scrolling one page */
4250     step = NUM_MENU_ENTRIES_ON_SCREEN - 1;
4251
4252   if (dy < 0)
4253   {
4254     if (first_entry > 0)
4255     {
4256       first_entry -= step;
4257       if (first_entry < 0)
4258         first_entry = 0;
4259
4260       drawHallOfFameList(first_entry, highlight_position);
4261     }
4262   }
4263   else if (dy > 0)
4264   {
4265     if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
4266     {
4267       first_entry += step;
4268       if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
4269         first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
4270
4271       drawHallOfFameList(first_entry, highlight_position);
4272     }
4273   }
4274   else if (button == MB_MENU_LEAVE)
4275   {
4276     PlaySound(SND_MENU_ITEM_SELECTING);
4277
4278     FadeSound(SND_BACKGROUND_SCORES);
4279
4280     SetGameStatus(GAME_MODE_MAIN);
4281
4282     DrawMainMenu();
4283   }
4284   else if (button == MB_MENU_CHOICE)
4285   {
4286     PlaySound(SND_MENU_ITEM_SELECTING);
4287
4288     FadeSound(SND_BACKGROUND_SCORES);
4289
4290     SetGameStatus(GAME_MODE_MAIN);
4291
4292     DrawMainMenu();
4293   }
4294
4295   if (game_status == GAME_MODE_SCORES)
4296     PlayMenuSoundIfLoop();
4297 }
4298
4299
4300 /* ========================================================================= */
4301 /* setup screen functions                                                    */
4302 /* ========================================================================= */
4303
4304 static struct TokenInfo *setup_info;
4305 static int num_setup_info;      /* number of setup entries shown on screen */
4306 static int max_setup_info;      /* total number of setup entries in list */
4307
4308 static char *window_size_text;
4309 static char *scaling_type_text;
4310 static char *rendering_mode_text;
4311 static char *scroll_delay_text;
4312 static char *snapshot_mode_text;
4313 static char *game_speed_text;
4314 static char *graphics_set_name;
4315 static char *sounds_set_name;
4316 static char *music_set_name;
4317 static char *volume_simple_text;
4318 static char *volume_loops_text;
4319 static char *volume_music_text;
4320 static char *touch_controls_text;
4321 static char *move_distance_text;
4322 static char *drop_distance_text;
4323
4324 static void execSetupMain()
4325 {
4326   setup_mode = SETUP_MODE_MAIN;
4327
4328   DrawSetupScreen();
4329 }
4330
4331 static void execSetupGame_setGameSpeeds()
4332 {
4333   if (game_speeds == NULL)
4334   {
4335     int i;
4336
4337     for (i = 0; game_speeds_list[i].value != -1; i++)
4338     {
4339       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4340       char identifier[32], name[32];
4341       int value = game_speeds_list[i].value;
4342       char *text = game_speeds_list[i].text;
4343
4344       ti->node_top = &game_speeds;
4345       ti->sort_priority = 10000 - value;
4346
4347       sprintf(identifier, "%d", value);
4348       sprintf(name, "%s", text);
4349
4350       setString(&ti->identifier, identifier);
4351       setString(&ti->name, name);
4352       setString(&ti->name_sorting, name);
4353       setString(&ti->infotext, "Game Speed");
4354
4355       pushTreeInfo(&game_speeds, ti);
4356     }
4357
4358     /* sort game speed values to start with slowest game speed */
4359     sortTreeInfo(&game_speeds);
4360
4361     /* set current game speed to configured game speed value */
4362     game_speed_current =
4363       getTreeInfoFromIdentifier(game_speeds, i_to_a(setup.game_frame_delay));
4364
4365     /* if that fails, set current game speed to reliable default value */
4366     if (game_speed_current == NULL)
4367       game_speed_current =
4368         getTreeInfoFromIdentifier(game_speeds, i_to_a(GAME_FRAME_DELAY));
4369
4370     /* if that also fails, set current game speed to first available speed */
4371     if (game_speed_current == NULL)
4372       game_speed_current = game_speeds;
4373   }
4374
4375   setup.game_frame_delay = atoi(game_speed_current->identifier);
4376
4377   /* needed for displaying game speed text instead of identifier */
4378   game_speed_text = game_speed_current->name;
4379 }
4380
4381 static void execSetupGame_setScrollDelays()
4382 {
4383   if (scroll_delays == NULL)
4384   {
4385     int i;
4386
4387     for (i = 0; scroll_delays_list[i].value != -1; i++)
4388     {
4389       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4390       char identifier[32], name[32];
4391       int value = scroll_delays_list[i].value;
4392       char *text = scroll_delays_list[i].text;
4393
4394       ti->node_top = &scroll_delays;
4395       ti->sort_priority = value;
4396
4397       sprintf(identifier, "%d", value);
4398       sprintf(name, "%s", text);
4399
4400       setString(&ti->identifier, identifier);
4401       setString(&ti->name, name);
4402       setString(&ti->name_sorting, name);
4403       setString(&ti->infotext, "Scroll Delay");
4404
4405       pushTreeInfo(&scroll_delays, ti);
4406     }
4407
4408     /* sort scroll delay values to start with lowest scroll delay value */
4409     sortTreeInfo(&scroll_delays);
4410
4411     /* set current scroll delay value to configured scroll delay value */
4412     scroll_delay_current =
4413       getTreeInfoFromIdentifier(scroll_delays,i_to_a(setup.scroll_delay_value));
4414
4415     /* if that fails, set current scroll delay to reliable default value */
4416     if (scroll_delay_current == NULL)
4417       scroll_delay_current =
4418         getTreeInfoFromIdentifier(scroll_delays, i_to_a(STD_SCROLL_DELAY));
4419
4420     /* if that also fails, set current scroll delay to first available value */
4421     if (scroll_delay_current == NULL)
4422       scroll_delay_current = scroll_delays;
4423   }
4424
4425   setup.scroll_delay_value = atoi(scroll_delay_current->identifier);
4426
4427   /* needed for displaying scroll delay text instead of identifier */
4428   scroll_delay_text = scroll_delay_current->name;
4429 }
4430
4431 static void execSetupGame_setSnapshotModes()
4432 {
4433   if (snapshot_modes == NULL)
4434   {
4435     int i;
4436
4437     for (i = 0; snapshot_modes_list[i].value != NULL; i++)
4438     {
4439       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4440       char identifier[32], name[32];
4441       char *value = snapshot_modes_list[i].value;
4442       char *text = snapshot_modes_list[i].text;
4443
4444       ti->node_top = &snapshot_modes;
4445       ti->sort_priority = i;
4446
4447       sprintf(identifier, "%s", value);
4448       sprintf(name, "%s", text);
4449
4450       setString(&ti->identifier, identifier);
4451       setString(&ti->name, name);
4452       setString(&ti->name_sorting, name);
4453       setString(&ti->infotext, "Snapshot Mode");
4454
4455       pushTreeInfo(&snapshot_modes, ti);
4456     }
4457
4458     /* sort snapshot mode values to start with lowest snapshot mode value */
4459     sortTreeInfo(&snapshot_modes);
4460
4461     /* set current snapshot mode value to configured snapshot mode value */
4462     snapshot_mode_current =
4463       getTreeInfoFromIdentifier(snapshot_modes, setup.engine_snapshot_mode);
4464
4465     /* if that fails, set current snapshot mode to reliable default value */
4466     if (snapshot_mode_current == NULL)
4467       snapshot_mode_current =
4468         getTreeInfoFromIdentifier(snapshot_modes, STR_SNAPSHOT_MODE_DEFAULT);
4469
4470     /* if that also fails, set current snapshot mode to first available value */
4471     if (snapshot_mode_current == NULL)
4472       snapshot_mode_current = snapshot_modes;
4473   }
4474
4475   setup.engine_snapshot_mode = snapshot_mode_current->identifier;
4476
4477   /* needed for displaying snapshot mode text instead of identifier */
4478   snapshot_mode_text = snapshot_mode_current->name;
4479 }
4480
4481 static void execSetupGame()
4482 {
4483   execSetupGame_setGameSpeeds();
4484   execSetupGame_setScrollDelays();
4485   execSetupGame_setSnapshotModes();
4486
4487   setup_mode = SETUP_MODE_GAME;
4488
4489   DrawSetupScreen();
4490 }
4491
4492 static void execSetupChooseGameSpeed()
4493 {
4494   setup_mode = SETUP_MODE_CHOOSE_GAME_SPEED;
4495
4496   DrawSetupScreen();
4497 }
4498
4499 static void execSetupChooseScrollDelay()
4500 {
4501   setup_mode = SETUP_MODE_CHOOSE_SCROLL_DELAY;
4502
4503   DrawSetupScreen();
4504 }
4505
4506 static void execSetupChooseSnapshotMode()
4507 {
4508   setup_mode = SETUP_MODE_CHOOSE_SNAPSHOT_MODE;
4509
4510   DrawSetupScreen();
4511 }
4512
4513 static void execSetupEditor()
4514 {
4515   setup_mode = SETUP_MODE_EDITOR;
4516
4517   DrawSetupScreen();
4518 }
4519
4520 static void execSetupGraphics_setWindowSizes(boolean update_list)
4521 {
4522   if (window_sizes != NULL && update_list)
4523   {
4524     freeTreeInfo(window_sizes);
4525
4526     window_sizes = NULL;
4527   }
4528
4529   if (window_sizes == NULL)
4530   {
4531     boolean current_window_size_found = FALSE;
4532     int i;
4533
4534     for (i = 0; window_sizes_list[i].value != -1; i++)
4535     {
4536       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4537       char identifier[32], name[32];
4538       int value = window_sizes_list[i].value;
4539       char *text = window_sizes_list[i].text;
4540
4541       ti->node_top = &window_sizes;
4542       ti->sort_priority = value;
4543
4544       sprintf(identifier, "%d", value);
4545       sprintf(name, "%s", text);
4546
4547       setString(&ti->identifier, identifier);
4548       setString(&ti->name, name);
4549       setString(&ti->name_sorting, name);
4550       setString(&ti->infotext, "Window Scaling");
4551
4552       pushTreeInfo(&window_sizes, ti);
4553
4554       if (value == setup.window_scaling_percent)
4555         current_window_size_found = TRUE;
4556     }
4557
4558     if (!current_window_size_found)
4559     {
4560       // add entry for non-preset window scaling value
4561
4562       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4563       char identifier[32], name[32];
4564       int value = setup.window_scaling_percent;
4565
4566       ti->node_top = &window_sizes;
4567       ti->sort_priority = value;
4568
4569       sprintf(identifier, "%d", value);
4570       sprintf(name, "%d %% (Current)", value);
4571
4572       setString(&ti->identifier, identifier);
4573       setString(&ti->name, name);
4574       setString(&ti->name_sorting, name);
4575       setString(&ti->infotext, "Window Scaling");
4576
4577       pushTreeInfo(&window_sizes, ti);
4578     }
4579
4580     /* sort window size values to start with lowest window size value */
4581     sortTreeInfo(&window_sizes);
4582
4583     /* set current window size value to configured window size value */
4584     window_size_current =
4585       getTreeInfoFromIdentifier(window_sizes,
4586                                 i_to_a(setup.window_scaling_percent));
4587
4588     /* if that fails, set current window size to reliable default value */
4589     if (window_size_current == NULL)
4590       window_size_current =
4591         getTreeInfoFromIdentifier(window_sizes,
4592                                   i_to_a(STD_WINDOW_SCALING_PERCENT));
4593
4594     /* if that also fails, set current window size to first available value */
4595     if (window_size_current == NULL)
4596       window_size_current = window_sizes;
4597   }
4598
4599   setup.window_scaling_percent = atoi(window_size_current->identifier);
4600
4601   /* needed for displaying window size text instead of identifier */
4602   window_size_text = window_size_current->name;
4603 }
4604
4605 static void execSetupGraphics_setScalingTypes()
4606 {
4607   if (scaling_types == NULL)
4608   {
4609     int i;
4610
4611     for (i = 0; scaling_types_list[i].value != NULL; i++)
4612     {
4613       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4614       char identifier[32], name[32];
4615       char *value = scaling_types_list[i].value;
4616       char *text = scaling_types_list[i].text;
4617
4618       ti->node_top = &scaling_types;
4619       ti->sort_priority = i;
4620
4621       sprintf(identifier, "%s", value);
4622       sprintf(name, "%s", text);
4623
4624       setString(&ti->identifier, identifier);
4625       setString(&ti->name, name);
4626       setString(&ti->name_sorting, name);
4627       setString(&ti->infotext, "Anti-Aliasing");
4628
4629       pushTreeInfo(&scaling_types, ti);
4630     }
4631
4632     /* sort scaling type values to start with lowest scaling type value */
4633     sortTreeInfo(&scaling_types);
4634
4635     /* set current scaling type value to configured scaling type value */
4636     scaling_type_current =
4637       getTreeInfoFromIdentifier(scaling_types, setup.window_scaling_quality);
4638
4639     /* if that fails, set current scaling type to reliable default value */
4640     if (scaling_type_current == NULL)
4641       scaling_type_current =
4642         getTreeInfoFromIdentifier(scaling_types, SCALING_QUALITY_DEFAULT);
4643
4644     /* if that also fails, set current scaling type to first available value */
4645     if (scaling_type_current == NULL)
4646       scaling_type_current = scaling_types;
4647   }
4648
4649   setup.window_scaling_quality = scaling_type_current->identifier;
4650
4651   /* needed for displaying scaling type text instead of identifier */
4652   scaling_type_text = scaling_type_current->name;
4653 }
4654
4655 static void execSetupGraphics_setRenderingModes()
4656 {
4657   if (rendering_modes == NULL)
4658   {
4659     int i;
4660
4661     for (i = 0; rendering_modes_list[i].value != NULL; i++)
4662     {
4663       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4664       char identifier[32], name[32];
4665       char *value = rendering_modes_list[i].value;
4666       char *text = rendering_modes_list[i].text;
4667
4668       ti->node_top = &rendering_modes;
4669       ti->sort_priority = i;
4670
4671       sprintf(identifier, "%s", value);
4672       sprintf(name, "%s", text);
4673
4674       setString(&ti->identifier, identifier);
4675       setString(&ti->name, name);
4676       setString(&ti->name_sorting, name);
4677       setString(&ti->infotext, "Special Rendering");
4678
4679       pushTreeInfo(&rendering_modes, ti);
4680     }
4681
4682     /* sort rendering mode values to start with lowest rendering mode value */
4683     sortTreeInfo(&rendering_modes);
4684
4685     /* set current rendering mode value to configured rendering mode value */
4686     rendering_mode_current =
4687       getTreeInfoFromIdentifier(rendering_modes, setup.screen_rendering_mode);
4688
4689     /* if that fails, set current rendering mode to reliable default value */
4690     if (rendering_mode_current == NULL)
4691       rendering_mode_current =
4692         getTreeInfoFromIdentifier(rendering_modes,
4693                                   STR_SPECIAL_RENDERING_DEFAULT);
4694
4695     /* if that also fails, set current rendering mode to first available one */
4696     if (rendering_mode_current == NULL)
4697       rendering_mode_current = rendering_modes;
4698   }
4699
4700   setup.screen_rendering_mode = rendering_mode_current->identifier;
4701
4702   /* needed for displaying rendering mode text instead of identifier */
4703   rendering_mode_text = rendering_mode_current->name;
4704 }
4705
4706 static void execSetupGraphics()
4707 {
4708   if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
4709   {
4710     // update "setup.window_scaling_percent" from list selection
4711     execSetupGraphics_setWindowSizes(FALSE);
4712   }
4713   else
4714   {
4715     // update list selection from "setup.window_scaling_percent"
4716     execSetupGraphics_setWindowSizes(TRUE);
4717   }
4718
4719   execSetupGraphics_setScalingTypes();
4720   execSetupGraphics_setRenderingModes();
4721
4722   setup_mode = SETUP_MODE_GRAPHICS;
4723
4724   DrawSetupScreen();
4725
4726 #if defined(TARGET_SDL2)
4727   // window scaling may have changed at this point
4728   ToggleFullscreenOrChangeWindowScalingIfNeeded();
4729
4730   // window scaling quality may have changed at this point
4731   if (!strEqual(setup.window_scaling_quality, video.window_scaling_quality))
4732     SDLSetWindowScalingQuality(setup.window_scaling_quality);
4733
4734   // screen rendering mode may have changed at this point
4735   SDLSetScreenRenderingMode(setup.screen_rendering_mode);
4736 #endif
4737 }
4738
4739 #if defined(TARGET_SDL2) && !defined(PLATFORM_ANDROID)
4740 static void execSetupChooseWindowSize()
4741 {
4742   setup_mode = SETUP_MODE_CHOOSE_WINDOW_SIZE;
4743
4744   DrawSetupScreen();
4745 }
4746
4747 static void execSetupChooseScalingType()
4748 {
4749   setup_mode = SETUP_MODE_CHOOSE_SCALING_TYPE;
4750
4751   DrawSetupScreen();
4752 }
4753
4754 static void execSetupChooseRenderingMode()
4755 {
4756   setup_mode = SETUP_MODE_CHOOSE_RENDERING;
4757
4758   DrawSetupScreen();
4759 }
4760 #endif
4761
4762 static void execSetupChooseVolumeSimple()
4763 {
4764   setup_mode = SETUP_MODE_CHOOSE_VOLUME_SIMPLE;
4765
4766   DrawSetupScreen();
4767 }
4768
4769 static void execSetupChooseVolumeLoops()
4770 {
4771   setup_mode = SETUP_MODE_CHOOSE_VOLUME_LOOPS;
4772
4773   DrawSetupScreen();
4774 }
4775
4776 static void execSetupChooseVolumeMusic()
4777 {
4778   setup_mode = SETUP_MODE_CHOOSE_VOLUME_MUSIC;
4779
4780   DrawSetupScreen();
4781 }
4782
4783 static void execSetupSound()
4784 {
4785   if (volumes_simple == NULL)
4786   {
4787     boolean current_volume_simple_found = FALSE;
4788     int i;
4789
4790     for (i = 0; volumes_list[i].value != -1; i++)
4791     {
4792       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4793       char identifier[32], name[32];
4794       int value = volumes_list[i].value;
4795       char *text = volumes_list[i].text;
4796
4797       ti->node_top = &volumes_simple;
4798       ti->sort_priority = value;
4799
4800       sprintf(identifier, "%d", value);
4801       sprintf(name, "%s", text);
4802
4803       setString(&ti->identifier, identifier);
4804       setString(&ti->name, name);
4805       setString(&ti->name_sorting, name);
4806       setString(&ti->infotext, "Sound Volume");
4807
4808       pushTreeInfo(&volumes_simple, ti);
4809
4810       if (value == setup.volume_simple)
4811         current_volume_simple_found = TRUE;
4812     }
4813
4814     if (!current_volume_simple_found)
4815     {
4816       // add entry for non-preset volume value
4817
4818       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4819       char identifier[32], name[32];
4820       int value = setup.volume_simple;
4821
4822       ti->node_top = &volumes_simple;
4823       ti->sort_priority = value;
4824
4825       sprintf(identifier, "%d", value);
4826       sprintf(name, "%d %% (Current)", value);
4827
4828       setString(&ti->identifier, identifier);
4829       setString(&ti->name, name);
4830       setString(&ti->name_sorting, name);
4831       setString(&ti->infotext, "Sound Volume");
4832
4833       pushTreeInfo(&volumes_simple, ti);
4834     }
4835
4836     /* sort volume values to start with lowest volume value */
4837     sortTreeInfo(&volumes_simple);
4838
4839     /* set current volume value to configured volume value */
4840     volume_simple_current =
4841       getTreeInfoFromIdentifier(volumes_simple,i_to_a(setup.volume_simple));
4842
4843     /* if that fails, set current volume to reliable default value */
4844     if (volume_simple_current == NULL)
4845       volume_simple_current =
4846         getTreeInfoFromIdentifier(volumes_simple, i_to_a(100));
4847
4848     /* if that also fails, set current volume to first available value */
4849     if (volume_simple_current == NULL)
4850       volume_simple_current = volumes_simple;
4851   }
4852
4853   if (volumes_loops == NULL)
4854   {
4855     boolean current_volume_loops_found = FALSE;
4856     int i;
4857
4858     for (i = 0; volumes_list[i].value != -1; i++)
4859     {
4860       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4861       char identifier[32], name[32];
4862       int value = volumes_list[i].value;
4863       char *text = volumes_list[i].text;
4864
4865       ti->node_top = &volumes_loops;
4866       ti->sort_priority = value;
4867
4868       sprintf(identifier, "%d", value);
4869       sprintf(name, "%s", text);
4870
4871       setString(&ti->identifier, identifier);
4872       setString(&ti->name, name);
4873       setString(&ti->name_sorting, name);
4874       setString(&ti->infotext, "Loops Volume");
4875
4876       pushTreeInfo(&volumes_loops, ti);
4877
4878       if (value == setup.volume_loops)
4879         current_volume_loops_found = TRUE;
4880     }
4881
4882     if (!current_volume_loops_found)
4883     {
4884       // add entry for non-preset volume value
4885
4886       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4887       char identifier[32], name[32];
4888       int value = setup.volume_loops;
4889
4890       ti->node_top = &volumes_loops;
4891       ti->sort_priority = value;
4892
4893       sprintf(identifier, "%d", value);
4894       sprintf(name, "%d %% (Current)", value);
4895
4896       setString(&ti->identifier, identifier);
4897       setString(&ti->name, name);
4898       setString(&ti->name_sorting, name);
4899       setString(&ti->infotext, "Loops Volume");
4900
4901       pushTreeInfo(&volumes_loops, ti);
4902     }
4903
4904     /* sort volume values to start with lowest volume value */
4905     sortTreeInfo(&volumes_loops);
4906
4907     /* set current volume value to configured volume value */
4908     volume_loops_current =
4909       getTreeInfoFromIdentifier(volumes_loops,i_to_a(setup.volume_loops));
4910
4911     /* if that fails, set current volume to reliable default value */
4912     if (volume_loops_current == NULL)
4913       volume_loops_current =
4914         getTreeInfoFromIdentifier(volumes_loops, i_to_a(100));
4915
4916     /* if that also fails, set current volume to first available value */
4917     if (volume_loops_current == NULL)
4918       volume_loops_current = volumes_loops;
4919   }
4920
4921   if (volumes_music == NULL)
4922   {
4923     boolean current_volume_music_found = FALSE;
4924     int i;
4925
4926     for (i = 0; volumes_list[i].value != -1; i++)
4927     {
4928       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4929       char identifier[32], name[32];
4930       int value = volumes_list[i].value;
4931       char *text = volumes_list[i].text;
4932
4933       ti->node_top = &volumes_music;
4934       ti->sort_priority = value;
4935
4936       sprintf(identifier, "%d", value);
4937       sprintf(name, "%s", text);
4938
4939       setString(&ti->identifier, identifier);
4940       setString(&ti->name, name);
4941       setString(&ti->name_sorting, name);
4942       setString(&ti->infotext, "Music Volume");
4943
4944       pushTreeInfo(&volumes_music, ti);
4945
4946       if (value == setup.volume_music)
4947         current_volume_music_found = TRUE;
4948     }
4949
4950     if (!current_volume_music_found)
4951     {
4952       // add entry for non-preset volume value
4953
4954       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4955       char identifier[32], name[32];
4956       int value = setup.volume_music;
4957
4958       ti->node_top = &volumes_music;
4959       ti->sort_priority = value;
4960
4961       sprintf(identifier, "%d", value);
4962       sprintf(name, "%d %% (Current)", value);
4963
4964       setString(&ti->identifier, identifier);
4965       setString(&ti->name, name);
4966       setString(&ti->name_sorting, name);
4967       setString(&ti->infotext, "Music Volume");
4968
4969       pushTreeInfo(&volumes_music, ti);
4970     }
4971
4972     /* sort volume values to start with lowest volume value */
4973     sortTreeInfo(&volumes_music);
4974
4975     /* set current volume value to configured volume value */
4976     volume_music_current =
4977       getTreeInfoFromIdentifier(volumes_music,i_to_a(setup.volume_music));
4978
4979     /* if that fails, set current volume to reliable default value */
4980     if (volume_music_current == NULL)
4981       volume_music_current =
4982         getTreeInfoFromIdentifier(volumes_music, i_to_a(100));
4983
4984     /* if that also fails, set current volume to first available value */
4985     if (volume_music_current == NULL)
4986       volume_music_current = volumes_music;
4987   }
4988
4989   setup.volume_simple = atoi(volume_simple_current->identifier);
4990   setup.volume_loops  = atoi(volume_loops_current->identifier);
4991   setup.volume_music  = atoi(volume_music_current->identifier);
4992
4993   /* needed for displaying volume text instead of identifier */
4994   volume_simple_text = volume_simple_current->name;
4995   volume_loops_text = volume_loops_current->name;
4996   volume_music_text = volume_music_current->name;
4997
4998   setup_mode = SETUP_MODE_SOUND;
4999
5000   DrawSetupScreen();
5001 }
5002
5003 static void execSetupChooseTouchControls()
5004 {
5005   setup_mode = SETUP_MODE_CHOOSE_TOUCH_CONTROL;
5006
5007   DrawSetupScreen();
5008 }
5009
5010 static void execSetupChooseMoveDistance()
5011 {
5012   setup_mode = SETUP_MODE_CHOOSE_MOVE_DISTANCE;
5013
5014   DrawSetupScreen();
5015 }
5016
5017 static void execSetupChooseDropDistance()
5018 {
5019   setup_mode = SETUP_MODE_CHOOSE_DROP_DISTANCE;
5020
5021   DrawSetupScreen();
5022 }
5023
5024 static void execSetupTouch()
5025 {
5026   if (touch_controls == NULL)
5027   {
5028     int i;
5029
5030     for (i = 0; touch_controls_list[i].value != NULL; i++)
5031     {
5032       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5033       char identifier[32], name[32];
5034       char *value = touch_controls_list[i].value;
5035       char *text = touch_controls_list[i].text;
5036
5037       ti->node_top = &touch_controls;
5038       ti->sort_priority = i;
5039
5040       sprintf(identifier, "%s", value);
5041       sprintf(name, "%s", text);
5042
5043       setString(&ti->identifier, identifier);
5044       setString(&ti->name, name);
5045       setString(&ti->name_sorting, name);
5046       setString(&ti->infotext, "Control Type");
5047
5048       pushTreeInfo(&touch_controls, ti);
5049     }
5050
5051     /* sort touch control values to start with lowest touch control value */
5052     sortTreeInfo(&touch_controls);
5053
5054     /* set current touch control value to configured touch control value */
5055     touch_control_current =
5056       getTreeInfoFromIdentifier(touch_controls, setup.touch.control_type);
5057
5058     /* if that fails, set current touch control to reliable default value */
5059     if (touch_control_current == NULL)
5060       touch_control_current =
5061         getTreeInfoFromIdentifier(touch_controls, TOUCH_CONTROL_DEFAULT);
5062
5063     /* if that also fails, set current touch control to first available value */
5064     if (touch_control_current == NULL)
5065       touch_control_current = touch_controls;
5066   }
5067
5068   if (move_distances == NULL)
5069   {
5070     int i;
5071
5072     for (i = 0; distances_list[i].value != -1; i++)
5073     {
5074       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5075       char identifier[32], name[32];
5076       int value = distances_list[i].value;
5077       char *text = distances_list[i].text;
5078
5079       ti->node_top = &move_distances;
5080       ti->sort_priority = value;
5081
5082       sprintf(identifier, "%d", value);
5083       sprintf(name, "%s", text);
5084
5085       setString(&ti->identifier, identifier);
5086       setString(&ti->name, name);
5087       setString(&ti->name_sorting, name);
5088       setString(&ti->infotext, "Move Distance");
5089
5090       pushTreeInfo(&move_distances, ti);
5091     }
5092
5093     /* sort distance values to start with lowest distance value */
5094     sortTreeInfo(&move_distances);
5095
5096     /* set current distance value to configured distance value */
5097     move_distance_current =
5098       getTreeInfoFromIdentifier(move_distances,
5099                                 i_to_a(setup.touch.move_distance));
5100
5101     /* if that fails, set current distance to reliable default value */
5102     if (move_distance_current == NULL)
5103       move_distance_current =
5104         getTreeInfoFromIdentifier(move_distances, i_to_a(1));
5105
5106     /* if that also fails, set current distance to first available value */
5107     if (move_distance_current == NULL)
5108       move_distance_current = move_distances;
5109   }
5110
5111   if (drop_distances == NULL)
5112   {
5113     int i;
5114
5115     for (i = 0; distances_list[i].value != -1; i++)
5116     {
5117       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5118       char identifier[32], name[32];
5119       int value = distances_list[i].value;
5120       char *text = distances_list[i].text;
5121
5122       ti->node_top = &drop_distances;
5123       ti->sort_priority = value;
5124
5125       sprintf(identifier, "%d", value);
5126       sprintf(name, "%s", text);
5127
5128       setString(&ti->identifier, identifier);
5129       setString(&ti->name, name);
5130       setString(&ti->name_sorting, name);
5131       setString(&ti->infotext, "Drop Distance");
5132
5133       pushTreeInfo(&drop_distances, ti);
5134     }
5135
5136     /* sort distance values to start with lowest distance value */
5137     sortTreeInfo(&drop_distances);
5138
5139     /* set current distance value to configured distance value */
5140     drop_distance_current =
5141       getTreeInfoFromIdentifier(drop_distances,
5142                                 i_to_a(setup.touch.drop_distance));
5143
5144     /* if that fails, set current distance to reliable default value */
5145     if (drop_distance_current == NULL)
5146       drop_distance_current =
5147         getTreeInfoFromIdentifier(drop_distances, i_to_a(1));
5148
5149     /* if that also fails, set current distance to first available value */
5150     if (drop_distance_current == NULL)
5151       drop_distance_current = drop_distances;
5152   }
5153
5154   setup.touch.control_type = touch_control_current->identifier;
5155   setup.touch.move_distance = atoi(move_distance_current->identifier);
5156   setup.touch.drop_distance = atoi(drop_distance_current->identifier);
5157
5158   /* needed for displaying volume text instead of identifier */
5159   touch_controls_text = touch_control_current->name;
5160   move_distance_text = move_distance_current->name;
5161   drop_distance_text = drop_distance_current->name;
5162
5163   setup_mode = SETUP_MODE_TOUCH;
5164
5165   DrawSetupScreen();
5166 }
5167
5168 static void execSetupArtwork()
5169 {
5170 #if 0
5171   printf("::: '%s', '%s', '%s'\n",
5172          artwork.gfx_current->subdir,
5173          artwork.gfx_current->fullpath,
5174          artwork.gfx_current->basepath);
5175 #endif
5176
5177   setup.graphics_set = artwork.gfx_current->identifier;
5178   setup.sounds_set = artwork.snd_current->identifier;
5179   setup.music_set = artwork.mus_current->identifier;
5180
5181   /* needed if last screen (setup choice) changed graphics, sounds or music */
5182   ReloadCustomArtwork(0);
5183
5184   /* needed for displaying artwork name instead of artwork identifier */
5185   graphics_set_name = artwork.gfx_current->name;
5186   sounds_set_name = artwork.snd_current->name;
5187   music_set_name = artwork.mus_current->name;
5188
5189   setup_mode = SETUP_MODE_ARTWORK;
5190
5191   DrawSetupScreen();
5192 }
5193
5194 static void execSetupChooseGraphics()
5195 {
5196   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
5197
5198   DrawSetupScreen();
5199 }
5200
5201 static void execSetupChooseSounds()
5202 {
5203   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
5204
5205   DrawSetupScreen();
5206 }
5207
5208 static void execSetupChooseMusic()
5209 {
5210   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
5211
5212   DrawSetupScreen();
5213 }
5214
5215 #if !defined(PLATFORM_ANDROID)
5216 static void execSetupInput()
5217 {
5218   setup_mode = SETUP_MODE_INPUT;
5219
5220   DrawSetupScreen();
5221 }
5222 #endif
5223
5224 static void execSetupShortcuts()
5225 {
5226   setup_mode = SETUP_MODE_SHORTCUTS;
5227
5228   DrawSetupScreen();
5229 }
5230
5231 static void execSetupShortcuts1()
5232 {
5233   setup_mode = SETUP_MODE_SHORTCUTS_1;
5234
5235   DrawSetupScreen();
5236 }
5237
5238 static void execSetupShortcuts2()
5239 {
5240   setup_mode = SETUP_MODE_SHORTCUTS_2;
5241
5242   DrawSetupScreen();
5243 }
5244
5245 static void execSetupShortcuts3()
5246 {
5247   setup_mode = SETUP_MODE_SHORTCUTS_3;
5248
5249   DrawSetupScreen();
5250 }
5251
5252 static void execSetupShortcuts4()
5253 {
5254   setup_mode = SETUP_MODE_SHORTCUTS_4;
5255
5256   DrawSetupScreen();
5257 }
5258
5259 static void execSetupShortcuts5()
5260 {
5261   setup_mode = SETUP_MODE_SHORTCUTS_5;
5262
5263   DrawSetupScreen();
5264 }
5265
5266 static void execExitSetup()
5267 {
5268   SetGameStatus(GAME_MODE_MAIN);
5269
5270   DrawMainMenu();
5271 }
5272
5273 static void execSaveAndExitSetup()
5274 {
5275   SaveSetup();
5276   execExitSetup();
5277 }
5278
5279 static struct TokenInfo setup_info_main[] =
5280 {
5281   { TYPE_ENTER_MENU,    execSetupGame,          "Game & Menu"           },
5282   { TYPE_ENTER_MENU,    execSetupEditor,        "Editor"                },
5283   { TYPE_ENTER_MENU,    execSetupGraphics,      "Graphics"              },
5284   { TYPE_ENTER_MENU,    execSetupSound,         "Sound & Music"         },
5285   { TYPE_ENTER_MENU,    execSetupArtwork,       "Custom Artwork"        },
5286 #if !defined(PLATFORM_ANDROID)
5287   { TYPE_ENTER_MENU,    execSetupInput,         "Input Devices"         },
5288   { TYPE_ENTER_MENU,    execSetupTouch,         "Touch Controls"        },
5289 #else
5290   { TYPE_ENTER_MENU,    execSetupTouch,         "Touch Controls"        },
5291 #endif
5292   { TYPE_ENTER_MENU,    execSetupShortcuts,     "Key Shortcuts"         },
5293   { TYPE_EMPTY,         NULL,                   ""                      },
5294   { TYPE_LEAVE_MENU,    execExitSetup,          "Exit"                  },
5295   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   "Save and Exit"         },
5296
5297   { 0,                  NULL,                   NULL                    }
5298 };
5299
5300 static struct TokenInfo setup_info_game[] =
5301 {
5302   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode (Multi-Player):" },
5303   { TYPE_YES_NO,        &setup.input_on_focus,  "Only Move Focussed Player:" },
5304   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
5305   { TYPE_SWITCH,        &setup.skip_levels,     "Skip Unsolved Levels:" },
5306   { TYPE_SWITCH,        &setup.time_limit,      "Time Limit:"           },
5307   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record Tapes:"    },
5308   { TYPE_ENTER_LIST,    execSetupChooseGameSpeed, "Game Speed:"         },
5309   { TYPE_STRING,        &game_speed_text,       ""                      },
5310 #if 1
5311   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay:"     },
5312   { TYPE_STRING,        &scroll_delay_text,     ""                      },
5313 #endif
5314   { TYPE_ENTER_LIST, execSetupChooseSnapshotMode,"Game Engine Snapshot Mode:" },
5315   { TYPE_STRING,        &snapshot_mode_text,    ""                      },
5316   { TYPE_SWITCH,        &setup.show_snapshot_buttons,"Show Snapshot Buttons:" },
5317   { TYPE_EMPTY,         NULL,                   ""                      },
5318   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5319
5320   { 0,                  NULL,                   NULL                    }
5321 };
5322
5323 static struct TokenInfo setup_info_editor[] =
5324 {
5325 #if 0
5326   { TYPE_SWITCH,        &setup.editor.el_boulderdash,   "Boulder Dash:" },
5327   { TYPE_SWITCH,        &setup.editor.el_emerald_mine,  "Emerald Mine:" },
5328   { TYPE_SWITCH, &setup.editor.el_emerald_mine_club,    "Emerald Mine Club:" },
5329   { TYPE_SWITCH,        &setup.editor.el_more,          "Rocks'n'Diamonds:" },
5330   { TYPE_SWITCH,        &setup.editor.el_sokoban,       "Sokoban:"      },
5331   { TYPE_SWITCH,        &setup.editor.el_supaplex,      "Supaplex:"     },
5332   { TYPE_SWITCH,        &setup.editor.el_diamond_caves, "Diamond Caves II:" },
5333   { TYPE_SWITCH,        &setup.editor.el_dx_boulderdash,"DX-Boulderdash:" },
5334 #endif
5335   { TYPE_SWITCH,        &setup.editor.el_chars,         "Text Characters:" },
5336   { TYPE_SWITCH, &setup.editor.el_steel_chars, "Text Characters (Steel):" },
5337   { TYPE_SWITCH,        &setup.editor.el_custom,  "Custom & Group Elements:" },
5338 #if 0
5339   { TYPE_SWITCH,        &setup.editor.el_headlines,     "Headlines:"    },
5340 #endif
5341   { TYPE_SWITCH, &setup.editor.el_user_defined, "User defined element list:" },
5342   { TYPE_SWITCH,        &setup.editor.el_dynamic,  "Dynamic level elements:" },
5343   { TYPE_EMPTY,         NULL,                   ""                      },
5344 #if 0
5345   { TYPE_SWITCH,        &setup.editor.el_by_game,   "Show elements by game:" },
5346   { TYPE_SWITCH,        &setup.editor.el_by_type,   "Show elements by type:" },
5347   { TYPE_EMPTY,         NULL,                   ""                      },
5348 #endif
5349   { TYPE_SWITCH, &setup.editor.show_element_token,      "Show element token:" },
5350   { TYPE_EMPTY,         NULL,                   ""                      },
5351   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5352
5353   { 0,                  NULL,                   NULL                    }
5354 };
5355
5356 static struct TokenInfo setup_info_graphics[] =
5357 {
5358 #if defined(TARGET_SDL2) && !defined(PLATFORM_ANDROID)
5359   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen:"           },
5360   { TYPE_ENTER_LIST,    execSetupChooseWindowSize, "Window Scaling:"    },
5361   { TYPE_STRING,        &window_size_text,      ""                      },
5362   { TYPE_ENTER_LIST,    execSetupChooseScalingType, "Anti-Aliasing:"    },
5363   { TYPE_STRING,        &scaling_type_text,     ""                      },
5364   { TYPE_ENTER_LIST,    execSetupChooseRenderingMode, "Special Rendering:" },
5365   { TYPE_STRING,        &rendering_mode_text,   ""                      },
5366 #endif
5367 #if 0
5368   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay:"     },
5369   { TYPE_STRING,        &scroll_delay_text,     ""                      },
5370 #endif
5371   { TYPE_SWITCH,        &setup.fade_screens,    "Fade Screens:"         },
5372   { TYPE_SWITCH,        &setup.quick_switch,    "Quick Player Focus Switch:" },
5373   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Menu Doors:"     },
5374   { TYPE_SWITCH,        &setup.show_titlescreen,"Show Title Screens:"   },
5375   { TYPE_SWITCH,        &setup.toons,           "Show Menu Animations:" },
5376   { TYPE_ECS_AGA,       &setup.prefer_aga_graphics,"EMC graphics preference:" },
5377   { TYPE_SWITCH, &setup.sp_show_border_elements,"Supaplex Border Elements:" },
5378   { TYPE_SWITCH,        &setup.small_game_graphics, "Small Game Graphics:" },
5379   { TYPE_EMPTY,         NULL,                   ""                      },
5380   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5381
5382   { 0,                  NULL,                   NULL                    }
5383 };
5384
5385 static struct TokenInfo setup_info_sound[] =
5386 {
5387   { TYPE_SWITCH,        &setup.sound_simple,    "Sound Effects (Normal):"  },
5388   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Effects (Looping):" },
5389   { TYPE_SWITCH,        &setup.sound_music,     "Music:"                },
5390   { TYPE_EMPTY,         NULL,                   ""                      },
5391   { TYPE_ENTER_LIST,    execSetupChooseVolumeSimple, "Sound Volume (Normal):" },
5392   { TYPE_STRING,        &volume_simple_text,    ""                      },
5393   { TYPE_ENTER_LIST,    execSetupChooseVolumeLoops, "Sound Volume (Looping):" },
5394   { TYPE_STRING,        &volume_loops_text,     ""                      },
5395   { TYPE_ENTER_LIST,    execSetupChooseVolumeMusic, "Music Volume:"     },
5396   { TYPE_STRING,        &volume_music_text,     ""                      },
5397   { TYPE_EMPTY,         NULL,                   ""                      },
5398   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5399
5400   { 0,                  NULL,                   NULL                    }
5401 };
5402
5403 static struct TokenInfo setup_info_artwork[] =
5404 {
5405   { TYPE_ENTER_LIST,    execSetupChooseGraphics,"Custom Graphics:"      },
5406   { TYPE_STRING,        &graphics_set_name,     ""                      },
5407   { TYPE_ENTER_LIST,    execSetupChooseSounds,  "Custom Sounds:"        },
5408   { TYPE_STRING,        &sounds_set_name,       ""                      },
5409   { TYPE_ENTER_LIST,    execSetupChooseMusic,   "Custom Music:"         },
5410   { TYPE_STRING,        &music_set_name,        ""                      },
5411   { TYPE_EMPTY,         NULL,                   ""                      },
5412   { TYPE_YES_NO_AUTO,&setup.override_level_graphics,"Override Level Graphics:"},
5413   { TYPE_YES_NO_AUTO,&setup.override_level_sounds,  "Override Level Sounds:"  },
5414   { TYPE_YES_NO_AUTO,&setup.override_level_music,   "Override Level Music:"   },
5415   { TYPE_EMPTY,         NULL,                   ""                      },
5416   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5417
5418   { 0,                  NULL,                   NULL                    }
5419 };
5420
5421 static struct TokenInfo setup_info_input[] =
5422 {
5423   { TYPE_SWITCH,        NULL,                   "Player:"               },
5424   { TYPE_SWITCH,        NULL,                   "Device:"               },
5425   { TYPE_ENTER_MENU,    NULL,                   ""                      },
5426   { TYPE_EMPTY,         NULL,                   ""                      },
5427   { TYPE_EMPTY,         NULL,                   ""                      },
5428   { TYPE_EMPTY,         NULL,                   ""                      },
5429   { TYPE_EMPTY,         NULL,                   ""                      },
5430   { TYPE_EMPTY,         NULL,                   ""                      },
5431   { TYPE_EMPTY,         NULL,                   ""                      },
5432   { TYPE_EMPTY,         NULL,                   ""                      },
5433   { TYPE_EMPTY,         NULL,                   ""                      },
5434   { TYPE_EMPTY,         NULL,                   ""                      },
5435   { TYPE_EMPTY,         NULL,                   ""                      },
5436   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5437
5438   { 0,                  NULL,                   NULL                    }
5439 };
5440
5441 static struct TokenInfo setup_info_touch[] =
5442 {
5443   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
5444   { TYPE_STRING,        &touch_controls_text,   ""                      },
5445   { TYPE_EMPTY,         NULL,                   ""                      },
5446   { TYPE_ENTER_LIST,    execSetupChooseMoveDistance, "Move Trigger Distance:" },
5447   { TYPE_STRING,        &move_distance_text,    ""                      },
5448   { TYPE_ENTER_LIST,    execSetupChooseDropDistance, "Drop Trigger Distance:" },
5449   { TYPE_STRING,        &drop_distance_text,    ""                      },
5450   { TYPE_EMPTY,         NULL,                   ""                      },
5451   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5452
5453   { 0,                  NULL,                   NULL                    }
5454 };
5455
5456 static struct TokenInfo setup_info_shortcuts[] =
5457 {
5458   { TYPE_ENTER_MENU,    execSetupShortcuts1,    "Various Keys"          },
5459   { TYPE_ENTER_MENU,    execSetupShortcuts2,    "Player Focus"          },
5460   { TYPE_ENTER_MENU,    execSetupShortcuts3,    "Tape Buttons"          },
5461   { TYPE_ENTER_MENU,    execSetupShortcuts4,    "Sound & Music"         },
5462   { TYPE_ENTER_MENU,    execSetupShortcuts5,    "TAS Snap Keys"         },
5463   { TYPE_EMPTY,         NULL,                   ""                      },
5464   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
5465
5466   { 0,                  NULL,                   NULL                    }
5467 };
5468
5469 static struct TokenInfo setup_info_shortcuts_1[] =
5470 {
5471   { TYPE_KEYTEXT,       NULL,           "Quick Save Game to Tape:",     },
5472   { TYPE_KEY,           &setup.shortcut.save_game, ""                   },
5473   { TYPE_KEYTEXT,       NULL,           "Quick Load Game from Tape:",   },
5474   { TYPE_KEY,           &setup.shortcut.load_game, ""                   },
5475   { TYPE_KEYTEXT,       NULL,           "Start Game & Toggle Pause:",   },
5476   { TYPE_KEY,           &setup.shortcut.toggle_pause, ""                },
5477   { TYPE_EMPTY,         NULL,                   ""                      },
5478   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on 'Esc' Key:"     },
5479   { TYPE_YES_NO, &setup.ask_on_escape_editor,   "Ask on 'Esc' Key (Editor):" },
5480   { TYPE_EMPTY,         NULL,                   ""                      },
5481   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
5482
5483   { 0,                  NULL,                   NULL                    }
5484 };
5485
5486 static struct TokenInfo setup_info_shortcuts_2[] =
5487 {
5488   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 1:",       },
5489   { TYPE_KEY,           &setup.shortcut.focus_player[0], ""             },
5490   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 2:",       },
5491   { TYPE_KEY,           &setup.shortcut.focus_player[1], ""             },
5492   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 3:",       },
5493   { TYPE_KEY,           &setup.shortcut.focus_player[2], ""             },
5494   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 4:",       },
5495   { TYPE_KEY,           &setup.shortcut.focus_player[3], ""             },
5496   { TYPE_KEYTEXT,       NULL,           "Set Focus to All Players:",    },
5497   { TYPE_KEY,           &setup.shortcut.focus_player_all, ""            },
5498   { TYPE_EMPTY,         NULL,                   ""                      },
5499   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
5500
5501   { 0,                  NULL,                   NULL                    }
5502 };
5503
5504 static struct TokenInfo setup_info_shortcuts_3[] =
5505 {
5506   { TYPE_KEYTEXT,       NULL,                   "Eject Tape:",          },
5507   { TYPE_KEY,           &setup.shortcut.tape_eject, ""                  },
5508   { TYPE_KEYTEXT,       NULL,                   "Warp / Single Step:",  },
5509   { TYPE_KEY,           &setup.shortcut.tape_extra, ""                  },
5510   { TYPE_KEYTEXT,       NULL,                   "Stop Tape:",           },
5511   { TYPE_KEY,           &setup.shortcut.tape_stop, ""                   },
5512   { TYPE_KEYTEXT,       NULL,                   "Pause / Unpause Tape:",},
5513   { TYPE_KEY,           &setup.shortcut.tape_pause, ""                  },
5514   { TYPE_KEYTEXT,       NULL,                   "Record Tape:",         },
5515   { TYPE_KEY,           &setup.shortcut.tape_record, ""                 },
5516   { TYPE_KEYTEXT,       NULL,                   "Play Tape:",           },
5517   { TYPE_KEY,           &setup.shortcut.tape_play, ""                   },
5518   { TYPE_EMPTY,         NULL,                   ""                      },
5519   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
5520
5521   { 0,                  NULL,                   NULL                    }
5522 };
5523
5524 static struct TokenInfo setup_info_shortcuts_4[] =
5525 {
5526   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Normal):", },
5527   { TYPE_KEY,           &setup.shortcut.sound_simple, ""                },
5528   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Looping):", },
5529   { TYPE_KEY,           &setup.shortcut.sound_loops, ""                 },
5530   { TYPE_KEYTEXT,       NULL,           "Toggle Music:",                },
5531   { TYPE_KEY,           &setup.shortcut.sound_music, ""                 },
5532   { TYPE_EMPTY,         NULL,                   ""                      },
5533   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
5534
5535   { 0,                  NULL,                   NULL                    }
5536 };
5537
5538 static struct TokenInfo setup_info_shortcuts_5[] =
5539 {
5540   { TYPE_KEYTEXT,       NULL,                   "Snap Left:",           },
5541   { TYPE_KEY,           &setup.shortcut.snap_left, ""                   },
5542   { TYPE_KEYTEXT,       NULL,                   "Snap Right:",          },
5543   { TYPE_KEY,           &setup.shortcut.snap_right, ""                  },
5544   { TYPE_KEYTEXT,       NULL,                   "Snap Up:",             },
5545   { TYPE_KEY,           &setup.shortcut.snap_up, ""                     },
5546   { TYPE_KEYTEXT,       NULL,                   "Snap Down:",           },
5547   { TYPE_KEY,           &setup.shortcut.snap_down, ""                   },
5548   { TYPE_EMPTY,         NULL,                   ""                      },
5549   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
5550
5551   { 0,                  NULL,                   NULL                    }
5552 };
5553
5554 static Key getSetupKey()
5555 {
5556   Key key = KSYM_UNDEFINED;
5557   boolean got_key_event = FALSE;
5558
5559   while (!got_key_event)
5560   {
5561     if (PendingEvent())         /* got event */
5562     {
5563       Event event;
5564
5565       NextEvent(&event);
5566
5567       switch (event.type)
5568       {
5569         case EVENT_KEYPRESS:
5570           {
5571             key = GetEventKey((KeyEvent *)&event, TRUE);
5572
5573             /* press 'Escape' or 'Enter' to keep the existing key binding */
5574             if (key == KSYM_Escape || key == KSYM_Return)
5575               key = KSYM_UNDEFINED;     /* keep old value */
5576
5577             got_key_event = TRUE;
5578           }
5579           break;
5580
5581         case EVENT_KEYRELEASE:
5582           key_joystick_mapping = 0;
5583           break;
5584
5585         default:
5586           HandleOtherEvents(&event);
5587           break;
5588       }
5589     }
5590
5591     DoAnimation();
5592     BackToFront();
5593
5594     WaitUntilDelayReached(&sync_frame_delay, sync_frame_delay_value);
5595   }
5596
5597   return key;
5598 }
5599
5600 static int getSetupValueFont(int type, void *value)
5601 {
5602   if (type & TYPE_KEY)
5603     return (type & TYPE_QUERY ? FONT_INPUT_1_ACTIVE : FONT_VALUE_1);
5604   else if (type & TYPE_STRING)
5605     return FONT_VALUE_2;
5606   else if (type & TYPE_ECS_AGA)
5607     return FONT_VALUE_1;
5608   else if (type & TYPE_BOOLEAN_STYLE)
5609     return (*(boolean *)value ? FONT_OPTION_ON : FONT_OPTION_OFF);
5610   else if (type & TYPE_YES_NO_AUTO)
5611     return (*(int *)value == AUTO  ? FONT_OPTION_ON :
5612             *(int *)value == FALSE ? FONT_OPTION_OFF : FONT_OPTION_ON);
5613   else
5614     return FONT_VALUE_1;
5615 }
5616
5617 static int getSetupValueFontNarrow(int type, int font_nr)
5618 {
5619   return (font_nr == FONT_VALUE_1    ? FONT_VALUE_NARROW :
5620           font_nr == FONT_OPTION_ON  ? FONT_OPTION_ON_NARROW :
5621           font_nr == FONT_OPTION_OFF ? FONT_OPTION_OFF_NARROW :
5622           font_nr);
5623 }
5624
5625 static void drawSetupValue(int screen_pos, int setup_info_pos_raw)
5626 {
5627   int si_pos = (setup_info_pos_raw < 0 ? screen_pos : setup_info_pos_raw);
5628   struct TokenInfo *si = &setup_info[si_pos];
5629   boolean font_draw_xoffset_modified = FALSE;
5630   boolean scrollbar_needed = (num_setup_info < max_setup_info);
5631   int font_draw_xoffset_old = -1;
5632   int xoffset = (scrollbar_needed ? -1 : 0);
5633   int menu_screen_value_xpos = MENU_SCREEN_VALUE_XPOS + xoffset;
5634   int menu_screen_max_xpos = MENU_SCREEN_MAX_XPOS + xoffset;
5635   int xpos = menu_screen_value_xpos;
5636   int ypos = MENU_SCREEN_START_YPOS + screen_pos;
5637   int startx = mSX + xpos * 32;
5638   int starty = mSY + ypos * 32;
5639   int font_nr, font_width;
5640   int type = si->type;
5641   void *value = si->value;
5642   char *value_string = getSetupValue(type, value);
5643   int i;
5644
5645   if (value_string == NULL)
5646     return;
5647
5648   if (type & TYPE_KEY)
5649   {
5650     xpos = MENU_SCREEN_START_XPOS;
5651
5652     if (type & TYPE_QUERY)
5653       value_string = "<press key>";
5654   }
5655   else if (type & TYPE_STRING)
5656   {
5657     int max_value_len = (SCR_FIELDX - 2) * 2;
5658
5659     xpos = MENU_SCREEN_START_XPOS;
5660
5661     if (strlen(value_string) > max_value_len)
5662       value_string[max_value_len] = '\0';
5663   }
5664   else if (type & TYPE_YES_NO_AUTO)
5665   {
5666     xpos = menu_screen_value_xpos - 1;
5667   }
5668
5669   startx = mSX + xpos * 32;
5670   starty = mSY + ypos * 32;
5671   font_nr = getSetupValueFont(type, value);
5672   font_width = getFontWidth(font_nr);
5673
5674   // special check if right-side setup values moved left due to scrollbar
5675   if (scrollbar_needed && xpos > MENU_SCREEN_START_XPOS)
5676   {
5677     int max_menu_text_length = 26;      // maximum text length for classic menu
5678     int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
5679     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
5680     int text_font_nr = getMenuTextFont(FONT_MENU_2);
5681     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
5682     int text_width = max_menu_text_length * getFontWidth(text_font_nr);
5683
5684     if (startx + font_xoffset < text_startx + text_width + text_font_xoffset)
5685     {
5686       xpos += 1;
5687       startx = mSX + xpos * 32;
5688
5689       font_nr = getSetupValueFontNarrow(type, font_nr);
5690       font_width = getFontWidth(font_nr);
5691     }
5692   }
5693
5694   /* downward compatibility correction for Juergen Bonhagen's menu settings */
5695   if (setup_mode != SETUP_MODE_INPUT)
5696   {
5697     int max_menu_text_length_big = (menu_screen_value_xpos -
5698                                     MENU_SCREEN_START_XPOS);
5699     int max_menu_text_length_medium = max_menu_text_length_big * 2;
5700     int check_font_nr = FONT_OPTION_ON; /* known font that needs correction */
5701     int font1_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
5702     int font2_xoffset = getFontBitmapInfo(check_font_nr)->draw_xoffset;
5703     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
5704     int text_font_nr = getMenuTextFont(FONT_MENU_2);
5705     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
5706     int text_width = max_menu_text_length_medium * getFontWidth(text_font_nr);
5707     boolean correct_font_draw_xoffset = FALSE;
5708
5709     if (xpos == MENU_SCREEN_START_XPOS &&
5710         startx + font1_xoffset < text_startx + text_font_xoffset)
5711       correct_font_draw_xoffset = TRUE;
5712
5713     if (xpos == menu_screen_value_xpos &&
5714         startx + font2_xoffset < text_startx + text_width + text_font_xoffset)
5715       correct_font_draw_xoffset = TRUE;
5716
5717     /* check if setup value would overlap with setup text when printed */
5718     /* (this can happen for extreme/wrong values for font draw offset) */
5719     if (correct_font_draw_xoffset)
5720     {
5721       font_draw_xoffset_old = getFontBitmapInfo(font_nr)->draw_xoffset;
5722       font_draw_xoffset_modified = TRUE;
5723
5724       if (type & TYPE_KEY)
5725         getFontBitmapInfo(font_nr)->draw_xoffset += 2 * getFontWidth(font_nr);
5726       else if (!(type & TYPE_STRING))
5727         getFontBitmapInfo(font_nr)->draw_xoffset = text_font_xoffset + 20 -
5728           max_menu_text_length_medium * (16 - getFontWidth(text_font_nr));
5729     }
5730   }
5731
5732   for (i = 0; i <= menu_screen_max_xpos - xpos; i++)
5733     DrawText(startx + i * font_width, starty, " ", font_nr);
5734
5735   DrawText(startx, starty, value_string, font_nr);
5736
5737   if (font_draw_xoffset_modified)
5738     getFontBitmapInfo(font_nr)->draw_xoffset = font_draw_xoffset_old;
5739 }
5740
5741 static void changeSetupValue(int screen_pos, int setup_info_pos_raw, int dx)
5742 {
5743   int si_pos = (setup_info_pos_raw < 0 ? screen_pos : setup_info_pos_raw);
5744   struct TokenInfo *si = &setup_info[si_pos];
5745
5746   if (si->type & TYPE_BOOLEAN_STYLE)
5747   {
5748     *(boolean *)si->value ^= TRUE;
5749   }
5750   else if (si->type & TYPE_YES_NO_AUTO)
5751   {
5752     *(int *)si->value =
5753       (dx == -1 ?
5754        (*(int *)si->value == AUTO ? TRUE :
5755         *(int *)si->value == TRUE ? FALSE : AUTO) :
5756        (*(int *)si->value == TRUE ? AUTO :
5757         *(int *)si->value == AUTO ? FALSE : TRUE));
5758   }
5759   else if (si->type & TYPE_KEY)
5760   {
5761     Key key;
5762
5763     si->type |= TYPE_QUERY;
5764     drawSetupValue(screen_pos, setup_info_pos_raw);
5765     si->type &= ~TYPE_QUERY;
5766
5767     key = getSetupKey();
5768     if (key != KSYM_UNDEFINED)
5769       *(Key *)si->value = key;
5770   }
5771
5772   drawSetupValue(screen_pos, setup_info_pos_raw);
5773
5774   // fullscreen state may have changed at this point
5775   if (si->value == &setup.fullscreen)
5776     ToggleFullscreenOrChangeWindowScalingIfNeeded();
5777 }
5778
5779 static void DrawSetupScreen_Generic()
5780 {
5781   int fade_mask = REDRAW_FIELD;
5782   boolean redraw_all = FALSE;
5783   char *title_string = NULL;
5784   int i;
5785
5786   if (CheckIfGlobalBorderHasChanged())
5787     fade_mask = REDRAW_ALL;
5788
5789   UnmapAllGadgets();
5790
5791   FreeScreenGadgets();
5792   CreateScreenGadgets();
5793
5794   if (redraw_mask & REDRAW_ALL)
5795     redraw_all = TRUE;
5796
5797   FadeOut(fade_mask);
5798
5799   ClearField();
5800
5801   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
5802
5803   if (setup_mode == SETUP_MODE_MAIN)
5804   {
5805     setup_info = setup_info_main;
5806     title_string = "Setup";
5807   }
5808   else if (setup_mode == SETUP_MODE_GAME)
5809   {
5810     setup_info = setup_info_game;
5811     title_string = "Setup Game";
5812   }
5813   else if (setup_mode == SETUP_MODE_EDITOR)
5814   {
5815     setup_info = setup_info_editor;
5816     title_string = "Setup Editor";
5817   }
5818   else if (setup_mode == SETUP_MODE_GRAPHICS)
5819   {
5820     setup_info = setup_info_graphics;
5821     title_string = "Setup Graphics";
5822   }
5823   else if (setup_mode == SETUP_MODE_SOUND)
5824   {
5825     setup_info = setup_info_sound;
5826     title_string = "Setup Sound";
5827   }
5828   else if (setup_mode == SETUP_MODE_ARTWORK)
5829   {
5830     setup_info = setup_info_artwork;
5831     title_string = "Custom Artwork";
5832   }
5833   else if (setup_mode == SETUP_MODE_TOUCH)
5834   {
5835     setup_info = setup_info_touch;
5836     title_string = "Setup Touch Ctrls";
5837   }
5838   else if (setup_mode == SETUP_MODE_SHORTCUTS)
5839   {
5840     setup_info = setup_info_shortcuts;
5841     title_string = "Setup Shortcuts";
5842   }
5843   else if (setup_mode == SETUP_MODE_SHORTCUTS_1)
5844   {
5845     setup_info = setup_info_shortcuts_1;
5846     title_string = "Setup Shortcuts";
5847   }
5848   else if (setup_mode == SETUP_MODE_SHORTCUTS_2)
5849   {
5850     setup_info = setup_info_shortcuts_2;
5851     title_string = "Setup Shortcuts";
5852   }
5853   else if (setup_mode == SETUP_MODE_SHORTCUTS_3)
5854   {
5855     setup_info = setup_info_shortcuts_3;
5856     title_string = "Setup Shortcuts";
5857   }
5858   else if (setup_mode == SETUP_MODE_SHORTCUTS_4)
5859   {
5860     setup_info = setup_info_shortcuts_4;
5861     title_string = "Setup Shortcuts";
5862   }
5863   else if (setup_mode == SETUP_MODE_SHORTCUTS_5)
5864   {
5865     setup_info = setup_info_shortcuts_5;
5866     title_string = "Setup Shortcuts";
5867   }
5868
5869   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, title_string);
5870
5871   // determine maximal number of setup entries that can be displayed on screen
5872   num_setup_info = 0;
5873   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
5874     num_setup_info++;
5875
5876   // determine maximal number of setup entries available for this setup screen
5877   max_setup_info = 0;
5878   for (i = 0; setup_info[i].type != 0; i++)
5879     max_setup_info++;
5880
5881   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
5882
5883   MapScreenGadgets(max_setup_info);
5884
5885   if (redraw_all)
5886     redraw_mask = fade_mask = REDRAW_ALL;
5887
5888   DrawMaskedBorder(fade_mask);
5889
5890   FadeIn(fade_mask);
5891
5892   InitAnimation();
5893 }
5894
5895 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
5896 {
5897   menu_info = setup_info;
5898
5899   HandleMenuScreen(mx, my, dx, dy, button,
5900                    setup_mode, num_setup_info, max_setup_info);
5901 }
5902
5903 void DrawSetupScreen_Input()
5904 {
5905   int i;
5906
5907   FadeOut(REDRAW_FIELD);
5908
5909   ClearField();
5910
5911   setup_info = setup_info_input;
5912
5913   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Setup Input");
5914
5915   DrawTextSCentered(SYSIZE - 20, FONT_TITLE_2,
5916                     "Joysticks deactivated on this screen");
5917
5918   for (i = 0; setup_info[i].type != 0 && i < MAX_MENU_ENTRIES_ON_SCREEN; i++)
5919   {
5920     if (setup_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
5921       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
5922     else if (setup_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
5923       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
5924     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
5925       initCursor(i, IMG_MENU_BUTTON);
5926
5927     DrawCursorAndText_Setup(i, -1, FALSE);
5928   }
5929
5930   /* create gadgets for setup input menu screen */
5931   FreeScreenGadgets();
5932   CreateScreenGadgets();
5933
5934   /* map gadgets for setup input menu screen */
5935   MapScreenMenuGadgets(SCREEN_MASK_INPUT);
5936
5937   HandleSetupScreen_Input(0, 0, 0, 0, MB_MENU_INITIALIZE);
5938
5939   FadeIn(REDRAW_FIELD);
5940
5941   InitAnimation();
5942 }
5943
5944 static void setJoystickDeviceToNr(char *device_name, int device_nr)
5945 {
5946   if (device_name == NULL)
5947     return;
5948
5949   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
5950     device_nr = 0;
5951
5952   if (strlen(device_name) > 1)
5953   {
5954     char c1 = device_name[strlen(device_name) - 1];
5955     char c2 = device_name[strlen(device_name) - 2];
5956
5957     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
5958       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
5959   }
5960   else
5961     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
5962             strlen(device_name));
5963 }
5964
5965 static void drawPlayerSetupInputInfo(int player_nr, boolean active)
5966 {
5967   int i;
5968   static struct SetupKeyboardInfo custom_key;
5969   static struct
5970   {
5971     Key *key;
5972     char *text;
5973   } custom[] =
5974   {
5975     { &custom_key.left,  "Joystick Left"  },
5976     { &custom_key.right, "Joystick Right" },
5977     { &custom_key.up,    "Joystick Up"    },
5978     { &custom_key.down,  "Joystick Down"  },
5979     { &custom_key.snap,  "Button 1"       },
5980     { &custom_key.drop,  "Button 2"       }
5981   };
5982   static char *joystick_name[MAX_PLAYERS] =
5983   {
5984     "Joystick1",
5985     "Joystick2",
5986     "Joystick3",
5987     "Joystick4"
5988   };
5989   int text_font_nr = (active ? FONT_MENU_1_ACTIVE : FONT_MENU_1);
5990
5991   InitJoysticks();
5992
5993   custom_key = setup.input[player_nr].key;
5994
5995   DrawText(mSX + 11 * 32, mSY + 2 * 32, int2str(player_nr + 1, 1),
5996            FONT_INPUT_1_ACTIVE);
5997
5998   ClearRectangleOnBackground(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
5999                              TILEX, TILEY);
6000   DrawFixedGraphicThruMaskExt(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
6001                               PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
6002
6003   if (setup.input[player_nr].use_joystick)
6004   {
6005     char *device_name = setup.input[player_nr].joy.device_name;
6006     char *text = joystick_name[getJoystickNrFromDeviceName(device_name)];
6007     int font_nr = (joystick.fd[player_nr] < 0 ? FONT_VALUE_OLD : FONT_VALUE_1);
6008
6009     DrawText(mSX + 8 * 32, mSY + 3 * 32, text, font_nr);
6010     DrawText(mSX + 32, mSY + 4 * 32, "Calibrate", text_font_nr);
6011   }
6012   else
6013   {
6014     DrawText(mSX + 8 * 32, mSY + 3 * 32, "Keyboard ", FONT_VALUE_1);
6015     DrawText(mSX + 1 * 32, mSY + 4 * 32, "Customize", text_font_nr);
6016   }
6017
6018   DrawText(mSX + 32, mSY + 5 * 32, "Actual Settings:", FONT_MENU_1);
6019
6020   drawCursorXY(1, 4, IMG_MENU_BUTTON_LEFT);
6021   drawCursorXY(1, 5, IMG_MENU_BUTTON_RIGHT);
6022   drawCursorXY(1, 6, IMG_MENU_BUTTON_UP);
6023   drawCursorXY(1, 7, IMG_MENU_BUTTON_DOWN);
6024
6025   DrawText(mSX + 2 * 32, mSY +  6 * 32, ":", FONT_VALUE_OLD);
6026   DrawText(mSX + 2 * 32, mSY +  7 * 32, ":", FONT_VALUE_OLD);
6027   DrawText(mSX + 2 * 32, mSY +  8 * 32, ":", FONT_VALUE_OLD);
6028   DrawText(mSX + 2 * 32, mSY +  9 * 32, ":", FONT_VALUE_OLD);
6029   DrawText(mSX + 1 * 32, mSY + 10 * 32, "Snap Field:", FONT_VALUE_OLD);
6030   DrawText(mSX + 1 * 32, mSY + 12 * 32, "Drop Element:", FONT_VALUE_OLD);
6031
6032   for (i = 0; i < 6; i++)
6033   {
6034     int ypos = 6 + i + (i > 3 ? i-3 : 0);
6035
6036     DrawText(mSX + 3 * 32, mSY + ypos * 32,
6037              "              ", FONT_VALUE_1);
6038     DrawText(mSX + 3 * 32, mSY + ypos * 32,
6039              (setup.input[player_nr].use_joystick ?
6040               custom[i].text :
6041               getKeyNameFromKey(*custom[i].key)), FONT_VALUE_1);
6042   }
6043 }
6044
6045 static int input_player_nr = 0;
6046
6047 void HandleSetupScreen_Input_Player(int step, int direction)
6048 {
6049   int old_player_nr = input_player_nr;
6050   int new_player_nr;
6051
6052   new_player_nr = old_player_nr + step * direction;
6053   if (new_player_nr < 0)
6054     new_player_nr = 0;
6055   if (new_player_nr > MAX_PLAYERS - 1)
6056     new_player_nr = MAX_PLAYERS - 1;
6057
6058   if (new_player_nr != old_player_nr)
6059   {
6060     input_player_nr = new_player_nr;
6061
6062     drawPlayerSetupInputInfo(input_player_nr, FALSE);
6063   }
6064 }
6065
6066 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
6067 {
6068   static int choice = 0;
6069   int x = 0;
6070   int y = choice;
6071   int pos_start  = SETUPINPUT_SCREEN_POS_START;
6072   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
6073   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
6074   int pos_end    = SETUPINPUT_SCREEN_POS_END;
6075
6076   if (button == MB_MENU_INITIALIZE)
6077   {
6078     drawPlayerSetupInputInfo(input_player_nr, (choice == 2));
6079
6080     DrawCursorAndText_Setup(choice, -1, TRUE);
6081
6082     return;
6083   }
6084   else if (button == MB_MENU_LEAVE)
6085   {
6086     setup_mode = SETUP_MODE_MAIN;
6087     DrawSetupScreen();
6088     InitJoysticks();
6089
6090     return;
6091   }
6092
6093   if (mx || my)         /* mouse input */
6094   {
6095     x = (mx - mSX) / 32;
6096     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
6097   }
6098   else if (dx || dy)    /* keyboard input */
6099   {
6100     if (dx && choice == 0)
6101       x = (dx < 0 ? 10 : 12);
6102     else if ((dx && choice == 1) ||
6103              (dx == +1 && choice == 2) ||
6104              (dx == -1 && choice == pos_end))
6105       button = MB_MENU_CHOICE;
6106     else if (dy)
6107       y = choice + dy;
6108
6109     if (y >= pos_empty1 && y <= pos_empty2)
6110       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
6111   }
6112
6113   if (y == 0 && dx != 0 && button)
6114   {
6115     HandleSetupScreen_Input_Player(1, dx < 0 ? -1 : +1);
6116   }
6117   else if (IN_VIS_FIELD(x, y) &&        // (does not use "IN_VIS_MENU()" yet)
6118            y >= pos_start && y <= pos_end &&
6119            !(y >= pos_empty1 && y <= pos_empty2))
6120   {
6121     if (button)
6122     {
6123       if (y != choice)
6124       {
6125         DrawCursorAndText_Setup(choice, -1, FALSE);
6126         DrawCursorAndText_Setup(y, -1, TRUE);
6127
6128         drawPlayerSetupInputInfo(input_player_nr, (y == 2));
6129
6130         choice = y;
6131       }
6132     }
6133     else
6134     {
6135       if (y == 1)
6136       {
6137         char *device_name = setup.input[input_player_nr].joy.device_name;
6138
6139         if (!setup.input[input_player_nr].use_joystick)
6140         {
6141           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
6142
6143           setJoystickDeviceToNr(device_name, new_device_nr);
6144           setup.input[input_player_nr].use_joystick = TRUE;
6145         }
6146         else
6147         {
6148           int device_nr = getJoystickNrFromDeviceName(device_name);
6149           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
6150
6151           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
6152             setup.input[input_player_nr].use_joystick = FALSE;
6153           else
6154             setJoystickDeviceToNr(device_name, new_device_nr);
6155         }
6156
6157         drawPlayerSetupInputInfo(input_player_nr, FALSE);
6158       }
6159       else if (y == 2)
6160       {
6161         if (setup.input[input_player_nr].use_joystick)
6162         {
6163           InitJoysticks();
6164           CalibrateJoystick(input_player_nr);
6165         }
6166         else
6167           CustomizeKeyboard(input_player_nr);
6168       }
6169       else if (y == pos_end)
6170       {
6171         InitJoysticks();
6172
6173         FadeSetLeaveMenu();
6174
6175         setup_mode = SETUP_MODE_MAIN;
6176         DrawSetupScreen();
6177       }
6178     }
6179   }
6180 }
6181
6182 void CustomizeKeyboard(int player_nr)
6183 {
6184   int i;
6185   int step_nr;
6186   boolean finished = FALSE;
6187   static struct SetupKeyboardInfo custom_key;
6188   static struct
6189   {
6190     Key *key;
6191     char *text;
6192   } customize_step[] =
6193   {
6194     { &custom_key.left,  "Move Left"    },
6195     { &custom_key.right, "Move Right"   },
6196     { &custom_key.up,    "Move Up"      },
6197     { &custom_key.down,  "Move Down"    },
6198     { &custom_key.snap,  "Snap Field"   },
6199     { &custom_key.drop,  "Drop Element" }
6200   };
6201
6202   /* read existing key bindings from player setup */
6203   custom_key = setup.input[player_nr].key;
6204
6205   FadeSetEnterMenu();
6206   FadeOut(REDRAW_FIELD);
6207
6208   ClearField();
6209
6210   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Keyboard Input");
6211
6212   step_nr = 0;
6213   DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
6214            customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
6215   DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
6216            "Key:", FONT_INPUT_1_ACTIVE);
6217   DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
6218            getKeyNameFromKey(*customize_step[step_nr].key), FONT_VALUE_OLD);
6219
6220   FadeIn(REDRAW_FIELD);
6221
6222   InitAnimation();
6223
6224   while (!finished)
6225   {
6226     if (PendingEvent())         /* got event */
6227     {
6228       Event event;
6229
6230       NextEvent(&event);
6231
6232       switch (event.type)
6233       {
6234         case EVENT_KEYPRESS:
6235           {
6236             Key key = GetEventKey((KeyEvent *)&event, FALSE);
6237
6238             if (key == KSYM_Escape || (key == KSYM_Return && step_nr == 6))
6239             {
6240               if (key == KSYM_Escape)
6241                 FadeSkipNextFadeIn();
6242
6243               finished = TRUE;
6244               break;
6245             }
6246
6247             /* all keys configured -- wait for "Escape" or "Return" key */
6248             if (step_nr == 6)
6249               break;
6250
6251             /* press 'Enter' to keep the existing key binding */
6252             if (key == KSYM_Return)
6253               key = *customize_step[step_nr].key;
6254
6255             /* check if key already used */
6256             for (i = 0; i < step_nr; i++)
6257               if (*customize_step[i].key == key)
6258                 break;
6259             if (i < step_nr)
6260               break;
6261
6262             /* got new key binding */
6263             *customize_step[step_nr].key = key;
6264             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
6265                      "             ", FONT_VALUE_1);
6266             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
6267                      getKeyNameFromKey(key), FONT_VALUE_1);
6268             step_nr++;
6269
6270             /* un-highlight last query */
6271             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1)) * 32,
6272                      customize_step[step_nr - 1].text, FONT_MENU_1);
6273             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1) + 1) * 32,
6274                      "Key:", FONT_MENU_1);
6275
6276             /* press 'Enter' to leave */
6277             if (step_nr == 6)
6278             {
6279               DrawText(mSX + 16, mSY + 15 * 32 + 16,
6280                        "Press Enter", FONT_TITLE_1);
6281               break;
6282             }
6283
6284             /* query next key binding */
6285             DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
6286                      customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
6287             DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
6288                      "Key:", FONT_INPUT_1_ACTIVE);
6289             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
6290                      getKeyNameFromKey(*customize_step[step_nr].key),
6291                      FONT_VALUE_OLD);
6292           }
6293           break;
6294
6295         case EVENT_KEYRELEASE:
6296           key_joystick_mapping = 0;
6297           break;
6298
6299         default:
6300           HandleOtherEvents(&event);
6301           break;
6302       }
6303     }
6304
6305     DoAnimation();
6306     BackToFront();
6307
6308     WaitUntilDelayReached(&sync_frame_delay, sync_frame_delay_value);
6309   }
6310
6311   /* write new key bindings back to player setup */
6312   setup.input[player_nr].key = custom_key;
6313
6314   StopAnimation();
6315   DrawSetupScreen_Input();
6316 }
6317
6318 static boolean CalibrateJoystickMain(int player_nr)
6319 {
6320   int new_joystick_xleft = JOYSTICK_XMIDDLE;
6321   int new_joystick_xright = JOYSTICK_XMIDDLE;
6322   int new_joystick_yupper = JOYSTICK_YMIDDLE;
6323   int new_joystick_ylower = JOYSTICK_YMIDDLE;
6324   int new_joystick_xmiddle, new_joystick_ymiddle;
6325
6326   int joystick_fd = joystick.fd[player_nr];
6327   int x, y, last_x, last_y, xpos = 8, ypos = 3;
6328   boolean check[3][3];
6329   int check_remaining = 3 * 3;
6330   int joy_x, joy_y;
6331   int joy_value;
6332   int result = -1;
6333
6334   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
6335     return FALSE;
6336
6337   if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
6338     return FALSE;
6339
6340   FadeSetEnterMenu();
6341   FadeOut(REDRAW_FIELD);
6342
6343   ClearField();
6344
6345   for (y = 0; y < 3; y++)
6346   {
6347     for (x = 0; x < 3; x++)
6348     {
6349       DrawFixedGraphic(xpos + x - 1, ypos + y - 1, IMG_MENU_CALIBRATE_BLUE, 0);
6350       check[x][y] = FALSE;
6351     }
6352   }
6353
6354   DrawTextSCentered(mSY - SY +  6 * 32, FONT_TITLE_1, "Rotate joystick");
6355   DrawTextSCentered(mSY - SY +  7 * 32, FONT_TITLE_1, "in all directions");
6356   DrawTextSCentered(mSY - SY +  9 * 32, FONT_TITLE_1, "if all balls");
6357   DrawTextSCentered(mSY - SY + 10 * 32, FONT_TITLE_1, "are marked,");
6358   DrawTextSCentered(mSY - SY + 11 * 32, FONT_TITLE_1, "center joystick");
6359   DrawTextSCentered(mSY - SY + 12 * 32, FONT_TITLE_1, "and");
6360   DrawTextSCentered(mSY - SY + 13 * 32, FONT_TITLE_1, "press any button!");
6361
6362   joy_value = Joystick(player_nr);
6363   last_x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
6364   last_y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
6365
6366   /* eventually uncalibrated center position (joystick could be uncentered) */
6367   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
6368     return FALSE;
6369
6370   new_joystick_xmiddle = joy_x;
6371   new_joystick_ymiddle = joy_y;
6372
6373   DrawFixedGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_RED, 0);
6374
6375   FadeIn(REDRAW_FIELD);
6376
6377   while (Joystick(player_nr) & JOY_BUTTON);     /* wait for released button */
6378   InitAnimation();
6379
6380   while (result < 0)
6381   {
6382     if (PendingEvent())         /* got event */
6383     {
6384       Event event;
6385
6386       NextEvent(&event);
6387
6388       switch (event.type)
6389       {
6390         case EVENT_KEYPRESS:
6391           switch (GetEventKey((KeyEvent *)&event, TRUE))
6392           {
6393             case KSYM_Return:
6394               if (check_remaining == 0)
6395                 result = 1;
6396               break;
6397
6398             case KSYM_Escape:
6399               FadeSkipNextFadeIn();
6400               result = 0;
6401               break;
6402
6403             default:
6404               break;
6405           }
6406           break;
6407
6408         case EVENT_KEYRELEASE:
6409           key_joystick_mapping = 0;
6410           break;
6411
6412         default:
6413           HandleOtherEvents(&event);
6414           break;
6415       }
6416     }
6417
6418     if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
6419       return FALSE;
6420
6421     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_x);
6422     new_joystick_xright = MAX(new_joystick_xright, joy_x);
6423     new_joystick_yupper = MIN(new_joystick_yupper, joy_y);
6424     new_joystick_ylower = MAX(new_joystick_ylower, joy_y);
6425
6426     setup.input[player_nr].joy.xleft = new_joystick_xleft;
6427     setup.input[player_nr].joy.yupper = new_joystick_yupper;
6428     setup.input[player_nr].joy.xright = new_joystick_xright;
6429     setup.input[player_nr].joy.ylower = new_joystick_ylower;
6430     setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
6431     setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
6432
6433     CheckJoystickData();
6434
6435     joy_value = Joystick(player_nr);
6436
6437     if (joy_value & JOY_BUTTON && check_remaining == 0)
6438       result = 1;
6439
6440     x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
6441     y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
6442
6443     if (x != last_x || y != last_y)
6444     {
6445       DrawFixedGraphic(xpos + last_x, ypos + last_y,
6446                        IMG_MENU_CALIBRATE_YELLOW, 0);
6447       DrawFixedGraphic(xpos + x,      ypos + y,
6448                        IMG_MENU_CALIBRATE_RED,    0);
6449
6450       last_x = x;
6451       last_y = y;
6452
6453       if (check_remaining > 0 && !check[x+1][y+1])
6454       {
6455         check[x+1][y+1] = TRUE;
6456         check_remaining--;
6457       }
6458     }
6459
6460     DoAnimation();
6461     BackToFront();
6462
6463     WaitUntilDelayReached(&sync_frame_delay, sync_frame_delay_value);
6464   }
6465
6466   /* calibrated center position (joystick should now be centered) */
6467   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
6468     return FALSE;
6469
6470   new_joystick_xmiddle = joy_x;
6471   new_joystick_ymiddle = joy_y;
6472
6473   StopAnimation();
6474
6475   /* wait until the last pressed button was released */
6476   while (Joystick(player_nr) & JOY_BUTTON)
6477   {
6478     if (PendingEvent())         /* got event */
6479     {
6480       Event event;
6481
6482       NextEvent(&event);
6483       HandleOtherEvents(&event);
6484
6485       WaitUntilDelayReached(&sync_frame_delay, sync_frame_delay_value);
6486     }
6487   }
6488
6489   return TRUE;
6490 }
6491
6492 void CalibrateJoystick(int player_nr)
6493 {
6494   if (!CalibrateJoystickMain(player_nr))
6495   {
6496     char *device_name = setup.input[player_nr].joy.device_name;
6497     int nr = getJoystickNrFromDeviceName(device_name) + 1;
6498     int xpos = mSX - SX;
6499     int ypos = mSY - SY;
6500
6501     ClearField();
6502
6503     DrawTextF(xpos + 16, ypos + 6 * 32, FONT_TITLE_1, "   JOYSTICK %d   ", nr);
6504     DrawTextF(xpos + 16, ypos + 7 * 32, FONT_TITLE_1, " NOT AVAILABLE! ");
6505     BackToFront();
6506
6507     Delay(2000);                /* show error message for a short time */
6508
6509     ClearEventQueue();
6510   }
6511
6512   DrawSetupScreen_Input();
6513 }
6514
6515 void DrawSetupScreen()
6516 {
6517   DeactivateJoystick();
6518
6519   SetMainBackgroundImage(IMG_BACKGROUND_SETUP);
6520
6521   if (setup_mode == SETUP_MODE_INPUT)
6522     DrawSetupScreen_Input();
6523   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
6524     DrawChooseTree(&game_speed_current);
6525   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
6526     DrawChooseTree(&scroll_delay_current);
6527   else if (setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
6528     DrawChooseTree(&snapshot_mode_current);
6529   else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
6530     DrawChooseTree(&window_size_current);
6531   else if (setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
6532     DrawChooseTree(&scaling_type_current);
6533   else if (setup_mode == SETUP_MODE_CHOOSE_RENDERING)
6534     DrawChooseTree(&rendering_mode_current);
6535   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
6536     DrawChooseTree(&artwork.gfx_current);
6537   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
6538     DrawChooseTree(&artwork.snd_current);
6539   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
6540     DrawChooseTree(&artwork.mus_current);
6541   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE)
6542     DrawChooseTree(&volume_simple_current);
6543   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS)
6544     DrawChooseTree(&volume_loops_current);
6545   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
6546     DrawChooseTree(&volume_music_current);
6547   else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL)
6548     DrawChooseTree(&touch_control_current);
6549   else if (setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE)
6550     DrawChooseTree(&move_distance_current);
6551   else if (setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
6552     DrawChooseTree(&drop_distance_current);
6553   else
6554     DrawSetupScreen_Generic();
6555
6556   PlayMenuSound();
6557   PlayMenuMusic();
6558 }
6559
6560 void RedrawSetupScreenAfterFullscreenToggle()
6561 {
6562   if (setup_mode == SETUP_MODE_GRAPHICS ||
6563       setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
6564   {
6565     // update list selection from "setup.window_scaling_percent"
6566     execSetupGraphics_setWindowSizes(TRUE);
6567
6568     DrawSetupScreen();
6569   }
6570 }
6571
6572 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
6573 {
6574   if (setup_mode == SETUP_MODE_INPUT)
6575     HandleSetupScreen_Input(mx, my, dx, dy, button);
6576   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
6577     HandleChooseTree(mx, my, dx, dy, button, &game_speed_current);
6578   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
6579     HandleChooseTree(mx, my, dx, dy, button, &scroll_delay_current);
6580   else if (setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
6581     HandleChooseTree(mx, my, dx, dy, button, &snapshot_mode_current);
6582   else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
6583     HandleChooseTree(mx, my, dx, dy, button, &window_size_current);
6584   else if (setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
6585     HandleChooseTree(mx, my, dx, dy, button, &scaling_type_current);
6586   else if (setup_mode == SETUP_MODE_CHOOSE_RENDERING)
6587     HandleChooseTree(mx, my, dx, dy, button, &rendering_mode_current);
6588   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
6589     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
6590   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
6591     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
6592   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
6593     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
6594   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE)
6595     HandleChooseTree(mx, my, dx, dy, button, &volume_simple_current);
6596   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS)
6597     HandleChooseTree(mx, my, dx, dy, button, &volume_loops_current);
6598   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
6599     HandleChooseTree(mx, my, dx, dy, button, &volume_music_current);
6600   else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL)
6601     HandleChooseTree(mx, my, dx, dy, button, &touch_control_current);
6602   else if (setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE)
6603     HandleChooseTree(mx, my, dx, dy, button, &move_distance_current);
6604   else if (setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
6605     HandleChooseTree(mx, my, dx, dy, button, &drop_distance_current);
6606   else
6607     HandleSetupScreen_Generic(mx, my, dx, dy, button);
6608 }
6609
6610 void HandleGameActions()
6611 {
6612   if (game_status != GAME_MODE_PLAYING)
6613     return;
6614
6615   GameActions();        /* main game loop */
6616
6617   if (tape.auto_play && !tape.playing)
6618     AutoPlayTape();     /* continue automatically playing next tape */
6619 }
6620
6621
6622 /* ---------- new screen button stuff -------------------------------------- */
6623
6624 static void getScreenMenuButtonPos(int *x, int *y, int gadget_id)
6625 {
6626   switch (gadget_id)
6627   {
6628     case SCREEN_CTRL_ID_PREV_LEVEL:
6629       *x = mSX + GDI_ACTIVE_POS(menu.main.button.prev_level.x);
6630       *y = mSY + GDI_ACTIVE_POS(menu.main.button.prev_level.y);
6631       break;
6632
6633     case SCREEN_CTRL_ID_NEXT_LEVEL:
6634       *x = mSX + GDI_ACTIVE_POS(menu.main.button.next_level.x);
6635       *y = mSY + GDI_ACTIVE_POS(menu.main.button.next_level.y);
6636       break;
6637
6638     case SCREEN_CTRL_ID_PREV_PLAYER:
6639       *x = mSX + TILEX * 10;
6640       *y = mSY + TILEY * MENU_SCREEN_START_YPOS;
6641       break;
6642
6643     case SCREEN_CTRL_ID_NEXT_PLAYER:
6644       *x = mSX + TILEX * 12;
6645       *y = mSY + TILEY * MENU_SCREEN_START_YPOS;
6646       break;
6647
6648     default:
6649       Error(ERR_EXIT, "unknown gadget ID %d", gadget_id);
6650   }
6651 }
6652
6653 static struct
6654 {
6655   int gfx_unpressed, gfx_pressed;
6656   void (*get_gadget_position)(int *, int *, int);
6657   int gadget_id;
6658   int screen_mask;
6659   char *infotext;
6660 } menubutton_info[NUM_SCREEN_MENUBUTTONS] =
6661 {
6662   {
6663     IMG_MENU_BUTTON_PREV_LEVEL, IMG_MENU_BUTTON_PREV_LEVEL_ACTIVE,
6664     getScreenMenuButtonPos,
6665     SCREEN_CTRL_ID_PREV_LEVEL,
6666     SCREEN_MASK_MAIN,
6667     "last level"
6668   },
6669   {
6670     IMG_MENU_BUTTON_NEXT_LEVEL, IMG_MENU_BUTTON_NEXT_LEVEL_ACTIVE,
6671     getScreenMenuButtonPos,
6672     SCREEN_CTRL_ID_NEXT_LEVEL,
6673     SCREEN_MASK_MAIN,
6674     "next level"
6675   },
6676   {
6677     IMG_MENU_BUTTON_LEFT, IMG_MENU_BUTTON_LEFT_ACTIVE,
6678     getScreenMenuButtonPos,
6679     SCREEN_CTRL_ID_PREV_PLAYER,
6680     SCREEN_MASK_INPUT,
6681     "last player"
6682   },
6683   {
6684     IMG_MENU_BUTTON_RIGHT, IMG_MENU_BUTTON_RIGHT_ACTIVE,
6685     getScreenMenuButtonPos,
6686     SCREEN_CTRL_ID_NEXT_PLAYER,
6687     SCREEN_MASK_INPUT,
6688     "next player"
6689   },
6690 };
6691
6692 static struct
6693 {
6694   int gfx_unpressed, gfx_pressed;
6695   int x, y;
6696   int gadget_id;
6697   char *infotext;
6698 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
6699 {
6700   {
6701     IMG_MENU_BUTTON_UP, IMG_MENU_BUTTON_UP_ACTIVE,
6702     -1, -1,     /* these values are not constant, but can change at runtime */
6703     SCREEN_CTRL_ID_SCROLL_UP,
6704     "scroll up"
6705   },
6706   {
6707     IMG_MENU_BUTTON_DOWN, IMG_MENU_BUTTON_DOWN_ACTIVE,
6708     -1, -1,     /* these values are not constant, but can change at runtime */
6709     SCREEN_CTRL_ID_SCROLL_DOWN,
6710     "scroll down"
6711   }
6712 };
6713
6714 static struct
6715 {
6716   int gfx_unpressed, gfx_pressed;
6717   int x, y;
6718   int width, height;
6719   int type;
6720   int gadget_id;
6721   char *infotext;
6722 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
6723 {
6724   {
6725     IMG_MENU_SCROLLBAR, IMG_MENU_SCROLLBAR_ACTIVE,
6726     -1, -1,     /* these values are not constant, but can change at runtime */
6727     -1, -1,     /* these values are not constant, but can change at runtime */
6728     GD_TYPE_SCROLLBAR_VERTICAL,
6729     SCREEN_CTRL_ID_SCROLL_VERTICAL,
6730     "scroll level series vertically"
6731   }
6732 };
6733
6734 static void CreateScreenMenubuttons()
6735 {
6736   struct GadgetInfo *gi;
6737   unsigned int event_mask;
6738   int i;
6739
6740   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
6741   {
6742     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6743     int gfx_unpressed, gfx_pressed;
6744     int x, y, width, height;
6745     int gd_x1, gd_x2, gd_y1, gd_y2;
6746     int id = menubutton_info[i].gadget_id;
6747
6748     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
6749
6750     menubutton_info[i].get_gadget_position(&x, &y, id);
6751
6752     width = SC_MENUBUTTON_XSIZE;
6753     height = SC_MENUBUTTON_YSIZE;
6754
6755     gfx_unpressed = menubutton_info[i].gfx_unpressed;
6756     gfx_pressed   = menubutton_info[i].gfx_pressed;
6757     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6758     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6759     gd_x1 = graphic_info[gfx_unpressed].src_x;
6760     gd_y1 = graphic_info[gfx_unpressed].src_y;
6761     gd_x2 = graphic_info[gfx_pressed].src_x;
6762     gd_y2 = graphic_info[gfx_pressed].src_y;
6763
6764     gi = CreateGadget(GDI_CUSTOM_ID, id,
6765                       GDI_CUSTOM_TYPE_ID, i,
6766                       GDI_INFO_TEXT, menubutton_info[i].infotext,
6767                       GDI_X, x,
6768                       GDI_Y, y,
6769                       GDI_WIDTH, width,
6770                       GDI_HEIGHT, height,
6771                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
6772                       GDI_STATE, GD_BUTTON_UNPRESSED,
6773                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6774                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6775                       GDI_DIRECT_DRAW, FALSE,
6776                       GDI_EVENT_MASK, event_mask,
6777                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6778                       GDI_END);
6779
6780     if (gi == NULL)
6781       Error(ERR_EXIT, "cannot create gadget");
6782
6783     screen_gadget[id] = gi;
6784   }
6785 }
6786
6787 static void CreateScreenScrollbuttons()
6788 {
6789   struct GadgetInfo *gi;
6790   unsigned int event_mask;
6791   int i;
6792
6793   /* these values are not constant, but can change at runtime */
6794   scrollbutton_info[0].x = SC_SCROLL_UP_XPOS;
6795   scrollbutton_info[0].y = SC_SCROLL_UP_YPOS;
6796   scrollbutton_info[1].x = SC_SCROLL_DOWN_XPOS;
6797   scrollbutton_info[1].y = SC_SCROLL_DOWN_YPOS;
6798
6799   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
6800   {
6801     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6802     int gfx_unpressed, gfx_pressed;
6803     int x, y, width, height;
6804     int gd_x1, gd_x2, gd_y1, gd_y2;
6805     int id = scrollbutton_info[i].gadget_id;
6806
6807     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
6808
6809     x = mSX + scrollbutton_info[i].x + menu.scrollbar_xoffset;
6810     y = mSY + scrollbutton_info[i].y;
6811     width = SC_SCROLLBUTTON_XSIZE;
6812     height = SC_SCROLLBUTTON_YSIZE;
6813
6814     /* correct scrollbar position if placed outside menu (playfield) area */
6815     if (x > SX + SC_SCROLL_UP_XPOS)
6816       x = SX + SC_SCROLL_UP_XPOS;
6817
6818     if (id == SCREEN_CTRL_ID_SCROLL_DOWN)
6819       y = mSY + (SC_SCROLL_VERTICAL_YPOS +
6820                  (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE);
6821
6822     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
6823     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
6824     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6825     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6826     gd_x1 = graphic_info[gfx_unpressed].src_x;
6827     gd_y1 = graphic_info[gfx_unpressed].src_y;
6828     gd_x2 = graphic_info[gfx_pressed].src_x;
6829     gd_y2 = graphic_info[gfx_pressed].src_y;
6830
6831     gi = CreateGadget(GDI_CUSTOM_ID, id,
6832                       GDI_CUSTOM_TYPE_ID, i,
6833                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
6834                       GDI_X, x,
6835                       GDI_Y, y,
6836                       GDI_WIDTH, width,
6837                       GDI_HEIGHT, height,
6838                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
6839                       GDI_STATE, GD_BUTTON_UNPRESSED,
6840                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6841                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6842                       GDI_DIRECT_DRAW, FALSE,
6843                       GDI_EVENT_MASK, event_mask,
6844                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6845                       GDI_END);
6846
6847     if (gi == NULL)
6848       Error(ERR_EXIT, "cannot create gadget");
6849
6850     screen_gadget[id] = gi;
6851   }
6852 }
6853
6854 static void CreateScreenScrollbars()
6855 {
6856   int i;
6857
6858   /* these values are not constant, but can change at runtime */
6859   scrollbar_info[0].x = SC_SCROLL_VERTICAL_XPOS;
6860   scrollbar_info[0].y = SC_SCROLL_VERTICAL_YPOS;
6861   scrollbar_info[0].width  = SC_SCROLL_VERTICAL_XSIZE;
6862   scrollbar_info[0].height = SC_SCROLL_VERTICAL_YSIZE;
6863
6864   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
6865   {
6866     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6867     int gfx_unpressed, gfx_pressed;
6868     int x, y, width, height;
6869     int gd_x1, gd_x2, gd_y1, gd_y2;
6870     struct GadgetInfo *gi;
6871     int items_max, items_visible, item_position;
6872     unsigned int event_mask;
6873     int num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
6874     int id = scrollbar_info[i].gadget_id;
6875
6876     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
6877
6878     x = mSX + scrollbar_info[i].x + menu.scrollbar_xoffset;
6879     y = mSY + scrollbar_info[i].y;
6880     width  = scrollbar_info[i].width;
6881     height = scrollbar_info[i].height;
6882
6883     /* correct scrollbar position if placed outside menu (playfield) area */
6884     if (x > SX + SC_SCROLL_VERTICAL_XPOS)
6885       x = SX + SC_SCROLL_VERTICAL_XPOS;
6886
6887     if (id == SCREEN_CTRL_ID_SCROLL_VERTICAL)
6888       height = (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE;
6889
6890     items_max = num_page_entries;
6891     items_visible = num_page_entries;
6892     item_position = 0;
6893
6894     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
6895     gfx_pressed   = scrollbar_info[i].gfx_pressed;
6896     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6897     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6898     gd_x1 = graphic_info[gfx_unpressed].src_x;
6899     gd_y1 = graphic_info[gfx_unpressed].src_y;
6900     gd_x2 = graphic_info[gfx_pressed].src_x;
6901     gd_y2 = graphic_info[gfx_pressed].src_y;
6902
6903     gi = CreateGadget(GDI_CUSTOM_ID, id,
6904                       GDI_CUSTOM_TYPE_ID, i,
6905                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
6906                       GDI_X, x,
6907                       GDI_Y, y,
6908                       GDI_WIDTH, width,
6909                       GDI_HEIGHT, height,
6910                       GDI_TYPE, scrollbar_info[i].type,
6911                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
6912                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
6913                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
6914                       GDI_WHEEL_AREA_X, SX,
6915                       GDI_WHEEL_AREA_Y, SY,
6916                       GDI_WHEEL_AREA_WIDTH, SXSIZE,
6917                       GDI_WHEEL_AREA_HEIGHT, SYSIZE,
6918                       GDI_STATE, GD_BUTTON_UNPRESSED,
6919                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6920                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6921                       GDI_BORDER_SIZE, SC_BORDER_SIZE, SC_BORDER_SIZE,
6922                       GDI_DIRECT_DRAW, FALSE,
6923                       GDI_EVENT_MASK, event_mask,
6924                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6925                       GDI_END);
6926
6927     if (gi == NULL)
6928       Error(ERR_EXIT, "cannot create gadget");
6929
6930     screen_gadget[id] = gi;
6931   }
6932 }
6933
6934 void CreateScreenGadgets()
6935 {
6936   CreateScreenMenubuttons();
6937
6938   CreateScreenScrollbuttons();
6939   CreateScreenScrollbars();
6940 }
6941
6942 void FreeScreenGadgets()
6943 {
6944   int i;
6945
6946   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
6947     FreeGadget(screen_gadget[i]);
6948 }
6949
6950 void MapScreenMenuGadgets(int screen_mask)
6951 {
6952   int i;
6953
6954   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
6955     if (screen_mask & menubutton_info[i].screen_mask)
6956       MapGadget(screen_gadget[menubutton_info[i].gadget_id]);
6957 }
6958
6959 void MapScreenGadgets(int num_entries)
6960 {
6961   int i;
6962
6963   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
6964     return;
6965
6966   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
6967     MapGadget(screen_gadget[scrollbutton_info[i].gadget_id]);
6968
6969   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
6970     MapGadget(screen_gadget[scrollbar_info[i].gadget_id]);
6971 }
6972
6973 void MapScreenTreeGadgets(TreeInfo *ti)
6974 {
6975   MapScreenGadgets(numTreeInfoInGroup(ti));
6976 }
6977
6978 static void HandleScreenGadgets(struct GadgetInfo *gi)
6979 {
6980   int id = gi->custom_id;
6981   int button = gi->event.button;
6982   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
6983
6984   switch (id)
6985   {
6986     case SCREEN_CTRL_ID_PREV_LEVEL:
6987       HandleMainMenu_SelectLevel(step, -1, NO_DIRECT_LEVEL_SELECT);
6988       break;
6989
6990     case SCREEN_CTRL_ID_NEXT_LEVEL:
6991       HandleMainMenu_SelectLevel(step, +1, NO_DIRECT_LEVEL_SELECT);
6992       break;
6993
6994     case SCREEN_CTRL_ID_PREV_PLAYER:
6995       HandleSetupScreen_Input_Player(step, -1);
6996       break;
6997
6998     case SCREEN_CTRL_ID_NEXT_PLAYER:
6999       HandleSetupScreen_Input_Player(step, +1);
7000       break;
7001
7002     case SCREEN_CTRL_ID_SCROLL_UP:
7003       if (game_status == GAME_MODE_LEVELS)
7004         HandleChooseLevelSet(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
7005       else if (game_status == GAME_MODE_LEVELNR)
7006         HandleChooseLevelNr(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
7007       else if (game_status == GAME_MODE_SETUP)
7008         HandleSetupScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
7009       else if (game_status == GAME_MODE_INFO)
7010         HandleInfoScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
7011       break;
7012
7013     case SCREEN_CTRL_ID_SCROLL_DOWN:
7014       if (game_status == GAME_MODE_LEVELS)
7015         HandleChooseLevelSet(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
7016       else if (game_status == GAME_MODE_LEVELNR)
7017         HandleChooseLevelNr(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
7018       else if (game_status == GAME_MODE_SETUP)
7019         HandleSetupScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
7020       else if (game_status == GAME_MODE_INFO)
7021         HandleInfoScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
7022       break;
7023
7024     case SCREEN_CTRL_ID_SCROLL_VERTICAL:
7025       if (game_status == GAME_MODE_LEVELS)
7026         HandleChooseLevelSet(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
7027       else if (game_status == GAME_MODE_LEVELNR)
7028         HandleChooseLevelNr(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
7029       else if (game_status == GAME_MODE_SETUP)
7030         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
7031       else if (game_status == GAME_MODE_INFO)
7032         HandleInfoScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
7033       break;
7034
7035     default:
7036       break;
7037   }
7038 }