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