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