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