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