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