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