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