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