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