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