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