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