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