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