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