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