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