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