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