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