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