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