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