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