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