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