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