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