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