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