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