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