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