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