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