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