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