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