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