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