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