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