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