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