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