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