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