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