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