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