rnd-20140106-1-src
[rocksndiamonds.git] / src / screens.c
1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back!               *
3 *----------------------------------------------------------*
4 * (c) 1995-2006 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * screens.c                                                *
12 ***********************************************************/
13
14 #include "libgame/libgame.h"
15
16 #include "screens.h"
17 #include "events.h"
18 #include "game.h"
19 #include "tools.h"
20 #include "editor.h"
21 #include "files.h"
22 #include "tape.h"
23 #include "cartoons.h"
24 #include "network.h"
25 #include "init.h"
26 #include "config.h"
27
28 /* screens on the info screen */
29 #define INFO_MODE_MAIN                  0
30 #define INFO_MODE_TITLE                 1
31 #define INFO_MODE_ELEMENTS              2
32 #define INFO_MODE_MUSIC                 3
33 #define INFO_MODE_CREDITS               4
34 #define INFO_MODE_PROGRAM               5
35 #define INFO_MODE_VERSION               6
36 #define INFO_MODE_LEVELSET              7
37
38 #define MAX_INFO_MODES                  8
39
40 /* screens on the setup screen */
41 #define SETUP_MODE_MAIN                 0
42 #define SETUP_MODE_GAME                 1
43 #define SETUP_MODE_EDITOR               2
44 #define SETUP_MODE_GRAPHICS             3
45 #define SETUP_MODE_SOUND                4
46 #define SETUP_MODE_ARTWORK              5
47 #define SETUP_MODE_INPUT                6
48 #define SETUP_MODE_SHORTCUTS            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   // if (screen_modes == NULL && video.fullscreen_available)
4165   if (screen_modes == NULL && video.fullscreen_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   if (screen_mode_current != NULL)
4215   {
4216     setup.fullscreen_mode = screen_mode_current->identifier;
4217
4218     /* needed for displaying screen mode name instead of identifier */
4219     screen_mode_text = screen_mode_current->name;
4220   }
4221
4222   if (window_sizes == NULL)
4223   {
4224     int i;
4225
4226     for (i = 0; window_sizes_list[i].value != -1; i++)
4227     {
4228       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4229       char identifier[32], name[32];
4230       int value = window_sizes_list[i].value;
4231       char *text = window_sizes_list[i].text;
4232
4233       ti->node_top = &window_sizes;
4234       ti->sort_priority = value;
4235
4236       sprintf(identifier, "%d", value);
4237       sprintf(name, "%s", text);
4238
4239       setString(&ti->identifier, identifier);
4240       setString(&ti->name, name);
4241       setString(&ti->name_sorting, name);
4242       setString(&ti->infotext, "Window Scaling");
4243
4244       pushTreeInfo(&window_sizes, ti);
4245     }
4246
4247     /* sort window size values to start with lowest window size value */
4248     sortTreeInfo(&window_sizes);
4249
4250     /* set current window size value to configured window size value */
4251     window_size_current =
4252       getTreeInfoFromIdentifier(window_sizes,
4253                                 i_to_a(setup.window_scaling_percent));
4254
4255     /* if that fails, set current window size to reliable default value */
4256     if (window_size_current == NULL)
4257       window_size_current =
4258         getTreeInfoFromIdentifier(window_sizes,
4259                                   i_to_a(STD_WINDOW_SCALING_PERCENT));
4260
4261     /* if that also fails, set current window size to first available value */
4262     if (window_size_current == NULL)
4263       window_size_current = window_sizes;
4264   }
4265
4266   setup.window_scaling_percent = atoi(window_size_current->identifier);
4267
4268   /* needed for displaying window size text instead of identifier */
4269   window_size_text = window_size_current->name;
4270
4271 #if 1
4272   if (scroll_delays == NULL)
4273   {
4274     int i;
4275
4276     for (i = 0; scroll_delays_list[i].value != -1; i++)
4277     {
4278       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4279       char identifier[32], name[32];
4280       int value = scroll_delays_list[i].value;
4281       char *text = scroll_delays_list[i].text;
4282
4283       ti->node_top = &scroll_delays;
4284       ti->sort_priority = value;
4285
4286       sprintf(identifier, "%d", value);
4287       sprintf(name, "%s", text);
4288
4289       setString(&ti->identifier, identifier);
4290       setString(&ti->name, name);
4291       setString(&ti->name_sorting, name);
4292       setString(&ti->infotext, "Scroll Delay");
4293
4294       pushTreeInfo(&scroll_delays, ti);
4295     }
4296
4297     /* sort scroll delay values to start with lowest scroll delay value */
4298     sortTreeInfo(&scroll_delays);
4299
4300     /* set current scroll delay value to configured scroll delay value */
4301     scroll_delay_current =
4302       getTreeInfoFromIdentifier(scroll_delays,i_to_a(setup.scroll_delay_value));
4303
4304     /* if that fails, set current scroll delay to reliable default value */
4305     if (scroll_delay_current == NULL)
4306       scroll_delay_current =
4307         getTreeInfoFromIdentifier(scroll_delays, i_to_a(STD_SCROLL_DELAY));
4308
4309     /* if that also fails, set current scroll delay to first available value */
4310     if (scroll_delay_current == NULL)
4311       scroll_delay_current = scroll_delays;
4312   }
4313
4314   setup.scroll_delay_value = atoi(scroll_delay_current->identifier);
4315
4316   /* needed for displaying scroll delay text instead of identifier */
4317   scroll_delay_text = scroll_delay_current->name;
4318 #endif
4319
4320   setup_mode = SETUP_MODE_GRAPHICS;
4321
4322   DrawSetupScreen();
4323 }
4324
4325 #if !defined(PLATFORM_ANDROID)
4326 #if defined(TARGET_SDL2)
4327 static void execSetupChooseWindowSize()
4328 {
4329 #if 0
4330   if (!video.window_scaling_available)
4331     return;
4332 #endif
4333
4334   setup_mode = SETUP_MODE_CHOOSE_WINDOW_SIZE;
4335
4336   DrawSetupScreen();
4337 }
4338 #else
4339 static void execSetupChooseScreenMode()
4340 {
4341   if (!video.fullscreen_available)
4342     return;
4343
4344   setup_mode = SETUP_MODE_CHOOSE_SCREEN_MODE;
4345
4346   DrawSetupScreen();
4347 }
4348 #endif
4349 #endif
4350
4351 static void execSetupChooseScrollDelay()
4352 {
4353   setup_mode = SETUP_MODE_CHOOSE_SCROLL_DELAY;
4354
4355   DrawSetupScreen();
4356 }
4357
4358 static void execSetupChooseVolumeSimple()
4359 {
4360   setup_mode = SETUP_MODE_CHOOSE_VOLUME_SIMPLE;
4361
4362   DrawSetupScreen();
4363 }
4364
4365 static void execSetupChooseVolumeLoops()
4366 {
4367   setup_mode = SETUP_MODE_CHOOSE_VOLUME_LOOPS;
4368
4369   DrawSetupScreen();
4370 }
4371
4372 static void execSetupChooseVolumeMusic()
4373 {
4374   setup_mode = SETUP_MODE_CHOOSE_VOLUME_MUSIC;
4375
4376   DrawSetupScreen();
4377 }
4378
4379 static void execSetupSound()
4380 {
4381 #if 1
4382   if (volumes_simple == NULL)
4383   {
4384     int i;
4385
4386     for (i = 0; volumes_list[i].value != -1; i++)
4387     {
4388       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4389       char identifier[32], name[32];
4390       int value = volumes_list[i].value;
4391       char *text = volumes_list[i].text;
4392
4393       ti->node_top = &volumes_simple;
4394       ti->sort_priority = value;
4395
4396       sprintf(identifier, "%d", value);
4397       sprintf(name, "%s", text);
4398
4399       setString(&ti->identifier, identifier);
4400       setString(&ti->name, name);
4401       setString(&ti->name_sorting, name);
4402       setString(&ti->infotext, "Sound Volume");
4403
4404       pushTreeInfo(&volumes_simple, ti);
4405     }
4406
4407     /* sort volume values to start with lowest volume value */
4408     sortTreeInfo(&volumes_simple);
4409
4410     /* set current volume value to configured volume value */
4411     volume_simple_current =
4412       getTreeInfoFromIdentifier(volumes_simple,i_to_a(setup.volume_simple));
4413
4414     /* if that fails, set current volume to reliable default value */
4415     if (volume_simple_current == NULL)
4416       volume_simple_current =
4417         getTreeInfoFromIdentifier(volumes_simple, i_to_a(100));
4418
4419     /* if that also fails, set current volume to first available value */
4420     if (volume_simple_current == NULL)
4421       volume_simple_current = volumes_simple;
4422   }
4423
4424   if (volumes_loops == NULL)
4425   {
4426     int i;
4427
4428     for (i = 0; volumes_list[i].value != -1; i++)
4429     {
4430       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4431       char identifier[32], name[32];
4432       int value = volumes_list[i].value;
4433       char *text = volumes_list[i].text;
4434
4435       ti->node_top = &volumes_loops;
4436       ti->sort_priority = value;
4437
4438       sprintf(identifier, "%d", value);
4439       sprintf(name, "%s", text);
4440
4441       setString(&ti->identifier, identifier);
4442       setString(&ti->name, name);
4443       setString(&ti->name_sorting, name);
4444       setString(&ti->infotext, "Loops Volume");
4445
4446       pushTreeInfo(&volumes_loops, ti);
4447     }
4448
4449     /* sort volume values to start with lowest volume value */
4450     sortTreeInfo(&volumes_loops);
4451
4452     /* set current volume value to configured volume value */
4453     volume_loops_current =
4454       getTreeInfoFromIdentifier(volumes_loops,i_to_a(setup.volume_loops));
4455
4456     /* if that fails, set current volume to reliable default value */
4457     if (volume_loops_current == NULL)
4458       volume_loops_current =
4459         getTreeInfoFromIdentifier(volumes_loops, i_to_a(100));
4460
4461     /* if that also fails, set current volume to first available value */
4462     if (volume_loops_current == NULL)
4463       volume_loops_current = volumes_loops;
4464   }
4465
4466   if (volumes_music == NULL)
4467   {
4468     int i;
4469
4470     for (i = 0; volumes_list[i].value != -1; i++)
4471     {
4472       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4473       char identifier[32], name[32];
4474       int value = volumes_list[i].value;
4475       char *text = volumes_list[i].text;
4476
4477       ti->node_top = &volumes_music;
4478       ti->sort_priority = value;
4479
4480       sprintf(identifier, "%d", value);
4481       sprintf(name, "%s", text);
4482
4483       setString(&ti->identifier, identifier);
4484       setString(&ti->name, name);
4485       setString(&ti->name_sorting, name);
4486       setString(&ti->infotext, "Music Volume");
4487
4488       pushTreeInfo(&volumes_music, ti);
4489     }
4490
4491     /* sort volume values to start with lowest volume value */
4492     sortTreeInfo(&volumes_music);
4493
4494     /* set current volume value to configured volume value */
4495     volume_music_current =
4496       getTreeInfoFromIdentifier(volumes_music,i_to_a(setup.volume_music));
4497
4498     /* if that fails, set current volume to reliable default value */
4499     if (volume_music_current == NULL)
4500       volume_music_current =
4501         getTreeInfoFromIdentifier(volumes_music, i_to_a(100));
4502
4503     /* if that also fails, set current volume to first available value */
4504     if (volume_music_current == NULL)
4505       volume_music_current = volumes_music;
4506   }
4507
4508   setup.volume_simple = atoi(volume_simple_current->identifier);
4509   setup.volume_loops  = atoi(volume_loops_current->identifier);
4510   setup.volume_music  = atoi(volume_music_current->identifier);
4511
4512   /* needed for displaying volume text instead of identifier */
4513   volume_simple_text = volume_simple_current->name;
4514   volume_loops_text = volume_loops_current->name;
4515   volume_music_text = volume_music_current->name;
4516 #endif
4517
4518   setup_mode = SETUP_MODE_SOUND;
4519
4520   DrawSetupScreen();
4521 }
4522
4523 static void execSetupArtwork()
4524 {
4525 #if 0
4526   printf("::: '%s', '%s', '%s'\n",
4527          artwork.gfx_current->subdir,
4528          artwork.gfx_current->fullpath,
4529          artwork.gfx_current->basepath);
4530 #endif
4531
4532   setup.graphics_set = artwork.gfx_current->identifier;
4533   setup.sounds_set = artwork.snd_current->identifier;
4534   setup.music_set = artwork.mus_current->identifier;
4535
4536   /* needed if last screen (setup choice) changed graphics, sounds or music */
4537   ReloadCustomArtwork(0);
4538
4539   /* needed for displaying artwork name instead of artwork identifier */
4540   graphics_set_name = artwork.gfx_current->name;
4541   sounds_set_name = artwork.snd_current->name;
4542   music_set_name = artwork.mus_current->name;
4543
4544   setup_mode = SETUP_MODE_ARTWORK;
4545
4546   DrawSetupScreen();
4547 }
4548
4549 static void execSetupChooseGraphics()
4550 {
4551   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
4552
4553   DrawSetupScreen();
4554 }
4555
4556 static void execSetupChooseSounds()
4557 {
4558   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
4559
4560   DrawSetupScreen();
4561 }
4562
4563 static void execSetupChooseMusic()
4564 {
4565   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
4566
4567   DrawSetupScreen();
4568 }
4569
4570 static void execSetupInput()
4571 {
4572   setup_mode = SETUP_MODE_INPUT;
4573
4574   DrawSetupScreen();
4575 }
4576
4577 static void execSetupShortcuts()
4578 {
4579   setup_mode = SETUP_MODE_SHORTCUTS;
4580
4581   DrawSetupScreen();
4582 }
4583
4584 static void execSetupShortcuts1()
4585 {
4586   setup_mode = SETUP_MODE_SHORTCUTS_1;
4587
4588   DrawSetupScreen();
4589 }
4590
4591 static void execSetupShortcuts2()
4592 {
4593   setup_mode = SETUP_MODE_SHORTCUTS_2;
4594
4595   DrawSetupScreen();
4596 }
4597
4598 static void execSetupShortcuts3()
4599 {
4600   setup_mode = SETUP_MODE_SHORTCUTS_3;
4601
4602   DrawSetupScreen();
4603 }
4604
4605 static void execSetupShortcuts4()
4606 {
4607   setup_mode = SETUP_MODE_SHORTCUTS_4;
4608
4609   DrawSetupScreen();
4610 }
4611
4612 static void execSetupShortcuts5()
4613 {
4614   setup_mode = SETUP_MODE_SHORTCUTS_5;
4615
4616   DrawSetupScreen();
4617 }
4618
4619 static void execExitSetup()
4620 {
4621   game_status = GAME_MODE_MAIN;
4622
4623   DrawMainMenuExt(REDRAW_FIELD, FALSE);
4624 }
4625
4626 static void execSaveAndExitSetup()
4627 {
4628   SaveSetup();
4629   execExitSetup();
4630 }
4631
4632 static struct TokenInfo setup_info_main[] =
4633 {
4634   { TYPE_ENTER_MENU,    execSetupGame,          "Game & Menu"           },
4635   { TYPE_ENTER_MENU,    execSetupEditor,        "Editor"                },
4636   { TYPE_ENTER_MENU,    execSetupGraphics,      "Graphics"              },
4637   { TYPE_ENTER_MENU,    execSetupSound,         "Sound & Music"         },
4638   { TYPE_ENTER_MENU,    execSetupArtwork,       "Custom Artwork"        },
4639   { TYPE_ENTER_MENU,    execSetupInput,         "Input Devices"         },
4640   { TYPE_ENTER_MENU,    execSetupShortcuts,     "Key Shortcuts"         },
4641   { TYPE_EMPTY,         NULL,                   ""                      },
4642   { TYPE_LEAVE_MENU,    execExitSetup,          "Exit"                  },
4643   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   "Save and Exit"         },
4644
4645   { 0,                  NULL,                   NULL                    }
4646 };
4647
4648 static struct TokenInfo setup_info_game[] =
4649 {
4650   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode (Multi-Player):" },
4651   { TYPE_YES_NO,        &setup.input_on_focus,  "Only Move Focussed Player:" },
4652   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
4653   { TYPE_SWITCH,        &setup.skip_levels,     "Skip Unsolved Levels:" },
4654   { TYPE_SWITCH,        &setup.time_limit,      "Time Limit:"           },
4655   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record Tapes:"    },
4656   { TYPE_ENTER_LIST,    execSetupChooseGameSpeed, "Game Speed:"         },
4657   { TYPE_STRING,        &game_speed_text,       ""                      },
4658   { TYPE_EMPTY,         NULL,                   ""                      },
4659   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4660
4661   { 0,                  NULL,                   NULL                    }
4662 };
4663
4664 static struct TokenInfo setup_info_editor[] =
4665 {
4666 #if 0
4667   { TYPE_SWITCH,        &setup.editor.el_boulderdash,   "Boulder Dash:" },
4668   { TYPE_SWITCH,        &setup.editor.el_emerald_mine,  "Emerald Mine:" },
4669   { TYPE_SWITCH, &setup.editor.el_emerald_mine_club,    "Emerald Mine Club:" },
4670   { TYPE_SWITCH,        &setup.editor.el_more,          "Rocks'n'Diamonds:" },
4671   { TYPE_SWITCH,        &setup.editor.el_sokoban,       "Sokoban:"      },
4672   { TYPE_SWITCH,        &setup.editor.el_supaplex,      "Supaplex:"     },
4673   { TYPE_SWITCH,        &setup.editor.el_diamond_caves, "Diamond Caves II:" },
4674   { TYPE_SWITCH,        &setup.editor.el_dx_boulderdash,"DX-Boulderdash:" },
4675 #endif
4676   { TYPE_SWITCH,        &setup.editor.el_chars,         "Text Characters:" },
4677   { TYPE_SWITCH, &setup.editor.el_steel_chars, "Text Characters (Steel):" },
4678   { TYPE_SWITCH,        &setup.editor.el_custom,  "Custom & Group Elements:" },
4679 #if 0
4680   { TYPE_SWITCH,        &setup.editor.el_headlines,     "Headlines:"    },
4681 #endif
4682   { TYPE_SWITCH, &setup.editor.el_user_defined, "User defined element list:" },
4683   { TYPE_SWITCH,        &setup.editor.el_dynamic,  "Dynamic level elements:" },
4684   { TYPE_EMPTY,         NULL,                   ""                      },
4685 #if 0
4686   { TYPE_SWITCH,        &setup.editor.el_by_game,   "Show elements by game:" },
4687   { TYPE_SWITCH,        &setup.editor.el_by_type,   "Show elements by type:" },
4688   { TYPE_EMPTY,         NULL,                   ""                      },
4689 #endif
4690   { TYPE_SWITCH, &setup.editor.show_element_token,      "Show element token:" },
4691   { TYPE_EMPTY,         NULL,                   ""                      },
4692   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4693
4694   { 0,                  NULL,                   NULL                    }
4695 };
4696
4697 static struct TokenInfo setup_info_graphics[] =
4698 {
4699 #if !defined(PLATFORM_ANDROID)
4700   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen:"           },
4701 #if defined(TARGET_SDL2)
4702   { TYPE_ENTER_LIST,    execSetupChooseWindowSize, "Window Scaling:"    },
4703   { TYPE_STRING,        &window_size_text,      ""                      },
4704 #else
4705   { TYPE_ENTER_LIST,    execSetupChooseScreenMode, "Fullscreen Mode:"   },
4706   { TYPE_STRING,        &screen_mode_text,      ""                      },
4707 #endif
4708 #endif
4709 #if 0
4710   { TYPE_SWITCH,        &setup.scroll_delay,    "Scroll Delay:"         },
4711 #endif
4712   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay Value:" },
4713   { TYPE_STRING,        &scroll_delay_text,     ""                      },
4714 #if 0
4715   { TYPE_SWITCH,        &setup.soft_scrolling,  "Soft Scrolling:"       },
4716 #endif
4717   { TYPE_SWITCH,        &setup.fade_screens,    "Fade Screens:"         },
4718   { TYPE_SWITCH,        &setup.quick_switch,    "Quick Player Focus Switch:" },
4719   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Menu Doors:"     },
4720   { TYPE_SWITCH,        &setup.show_titlescreen,"Show Title Screens:"   },
4721   { TYPE_SWITCH,        &setup.toons,           "Show Toons:"           },
4722   { TYPE_ECS_AGA,       &setup.prefer_aga_graphics,"EMC graphics preference:" },
4723   { TYPE_SWITCH, &setup.sp_show_border_elements,"Supaplex Border Elements:" },
4724   { TYPE_SWITCH,        &setup.small_game_graphics, "Small Game Graphics:" },
4725   { TYPE_EMPTY,         NULL,                   ""                      },
4726   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4727
4728   { 0,                  NULL,                   NULL                    }
4729 };
4730
4731 static struct TokenInfo setup_info_sound[] =
4732 {
4733   { TYPE_SWITCH,        &setup.sound_simple,    "Sound Effects (Normal):"  },
4734   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Effects (Looping):" },
4735   { TYPE_SWITCH,        &setup.sound_music,     "Music:"                },
4736   { TYPE_EMPTY,         NULL,                   ""                      },
4737   { TYPE_ENTER_LIST,    execSetupChooseVolumeSimple, "Sound Volume (Normal):" },
4738   { TYPE_STRING,        &volume_simple_text,    ""                      },
4739   { TYPE_ENTER_LIST,    execSetupChooseVolumeLoops, "Sound Volume (Looping):" },
4740   { TYPE_STRING,        &volume_loops_text,     ""                      },
4741   { TYPE_ENTER_LIST,    execSetupChooseVolumeMusic, "Music Volume:"     },
4742   { TYPE_STRING,        &volume_music_text,     ""                      },
4743   { TYPE_EMPTY,         NULL,                   ""                      },
4744   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4745
4746   { 0,                  NULL,                   NULL                    }
4747 };
4748
4749 static struct TokenInfo setup_info_artwork[] =
4750 {
4751   { TYPE_ENTER_LIST,    execSetupChooseGraphics,"Custom Graphics:"      },
4752   { TYPE_STRING,        &graphics_set_name,     ""                      },
4753   { TYPE_ENTER_LIST,    execSetupChooseSounds,  "Custom Sounds:"        },
4754   { TYPE_STRING,        &sounds_set_name,       ""                      },
4755   { TYPE_ENTER_LIST,    execSetupChooseMusic,   "Custom Music:"         },
4756   { TYPE_STRING,        &music_set_name,        ""                      },
4757   { TYPE_EMPTY,         NULL,                   ""                      },
4758 #if 1
4759 #if 1
4760   { TYPE_YES_NO_AUTO,&setup.override_level_graphics,"Override Level Graphics:"},
4761   { TYPE_YES_NO_AUTO,&setup.override_level_sounds,  "Override Level Sounds:"  },
4762   { TYPE_YES_NO_AUTO,&setup.override_level_music,   "Override Level Music:"   },
4763 #else
4764   { TYPE_YES_NO, &setup.override_level_graphics,"Override Level Graphics:" },
4765   { TYPE_YES_NO, &setup.override_level_sounds,  "Override Level Sounds:"   },
4766   { TYPE_YES_NO, &setup.override_level_music,   "Override Level Music:"    },
4767   { TYPE_YES_NO, &setup.auto_override_artwork,  "Auto-Override Non-CE Sets:" },
4768 #endif
4769 #else
4770   { TYPE_STRING,        NULL,                   "Override Level Artwork:"},
4771   { TYPE_YES_NO,        &setup.override_level_graphics, "Graphics:"     },
4772   { TYPE_YES_NO,        &setup.override_level_sounds,   "Sounds:"       },
4773   { TYPE_YES_NO,        &setup.override_level_music,    "Music:"        },
4774 #endif
4775   { TYPE_EMPTY,         NULL,                   ""                      },
4776   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4777
4778   { 0,                  NULL,                   NULL                    }
4779 };
4780
4781 static struct TokenInfo setup_info_input[] =
4782 {
4783   { TYPE_SWITCH,        NULL,                   "Player:"               },
4784   { TYPE_SWITCH,        NULL,                   "Device:"               },
4785   { TYPE_ENTER_MENU,    NULL,                   ""                      },
4786   { TYPE_EMPTY,         NULL,                   ""                      },
4787   { TYPE_EMPTY,         NULL,                   ""                      },
4788   { TYPE_EMPTY,         NULL,                   ""                      },
4789   { TYPE_EMPTY,         NULL,                   ""                      },
4790   { TYPE_EMPTY,         NULL,                   ""                      },
4791   { TYPE_EMPTY,         NULL,                   ""                      },
4792   { TYPE_EMPTY,         NULL,                   ""                      },
4793   { TYPE_EMPTY,         NULL,                   ""                      },
4794   { TYPE_EMPTY,         NULL,                   ""                      },
4795   { TYPE_EMPTY,         NULL,                   ""                      },
4796   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4797
4798   { 0,                  NULL,                   NULL                    }
4799 };
4800
4801 static struct TokenInfo setup_info_shortcuts[] =
4802 {
4803   { TYPE_ENTER_MENU,    execSetupShortcuts1,    "Various Keys"          },
4804   { TYPE_ENTER_MENU,    execSetupShortcuts2,    "Player Focus"          },
4805   { TYPE_ENTER_MENU,    execSetupShortcuts3,    "Tape Buttons"          },
4806   { TYPE_ENTER_MENU,    execSetupShortcuts4,    "Sound & Music"         },
4807   { TYPE_ENTER_MENU,    execSetupShortcuts5,    "TAS Snap Keys"         },
4808   { TYPE_EMPTY,         NULL,                   ""                      },
4809   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
4810
4811   { 0,                  NULL,                   NULL                    }
4812 };
4813
4814 static struct TokenInfo setup_info_shortcuts_1[] =
4815 {
4816   { TYPE_KEYTEXT,       NULL,           "Quick Save Game to Tape:",     },
4817   { TYPE_KEY,           &setup.shortcut.save_game, ""                   },
4818   { TYPE_KEYTEXT,       NULL,           "Quick Load Game from Tape:",   },
4819   { TYPE_KEY,           &setup.shortcut.load_game, ""                   },
4820   { TYPE_KEYTEXT,       NULL,           "Start Game & Toggle Pause:",   },
4821   { TYPE_KEY,           &setup.shortcut.toggle_pause, ""                },
4822   { TYPE_EMPTY,         NULL,                   ""                      },
4823   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on 'Esc' Key:"     },
4824   { TYPE_YES_NO, &setup.ask_on_escape_editor,   "Ask on 'Esc' Key (Editor):" },
4825   { TYPE_EMPTY,         NULL,                   ""                      },
4826   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4827
4828   { 0,                  NULL,                   NULL                    }
4829 };
4830
4831 static struct TokenInfo setup_info_shortcuts_2[] =
4832 {
4833   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 1:",       },
4834   { TYPE_KEY,           &setup.shortcut.focus_player[0], ""             },
4835   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 2:",       },
4836   { TYPE_KEY,           &setup.shortcut.focus_player[1], ""             },
4837   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 3:",       },
4838   { TYPE_KEY,           &setup.shortcut.focus_player[2], ""             },
4839   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 4:",       },
4840   { TYPE_KEY,           &setup.shortcut.focus_player[3], ""             },
4841   { TYPE_KEYTEXT,       NULL,           "Set Focus to All Players:",    },
4842   { TYPE_KEY,           &setup.shortcut.focus_player_all, ""            },
4843   { TYPE_EMPTY,         NULL,                   ""                      },
4844   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4845
4846   { 0,                  NULL,                   NULL                    }
4847 };
4848
4849 static struct TokenInfo setup_info_shortcuts_3[] =
4850 {
4851   { TYPE_KEYTEXT,       NULL,                   "Eject Tape:",          },
4852   { TYPE_KEY,           &setup.shortcut.tape_eject, ""                  },
4853   { TYPE_KEYTEXT,       NULL,                   "Warp / Single Step:",  },
4854   { TYPE_KEY,           &setup.shortcut.tape_extra, ""                  },
4855   { TYPE_KEYTEXT,       NULL,                   "Stop Tape:",           },
4856   { TYPE_KEY,           &setup.shortcut.tape_stop, ""                   },
4857   { TYPE_KEYTEXT,       NULL,                   "Pause / Unpause Tape:",},
4858   { TYPE_KEY,           &setup.shortcut.tape_pause, ""                  },
4859   { TYPE_KEYTEXT,       NULL,                   "Record Tape:",         },
4860   { TYPE_KEY,           &setup.shortcut.tape_record, ""                 },
4861   { TYPE_KEYTEXT,       NULL,                   "Play Tape:",           },
4862   { TYPE_KEY,           &setup.shortcut.tape_play, ""                   },
4863   { TYPE_EMPTY,         NULL,                   ""                      },
4864   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4865
4866   { 0,                  NULL,                   NULL                    }
4867 };
4868
4869 static struct TokenInfo setup_info_shortcuts_4[] =
4870 {
4871   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Normal):", },
4872   { TYPE_KEY,           &setup.shortcut.sound_simple, ""                },
4873   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Looping):", },
4874   { TYPE_KEY,           &setup.shortcut.sound_loops, ""                 },
4875   { TYPE_KEYTEXT,       NULL,           "Toggle Music:",                },
4876   { TYPE_KEY,           &setup.shortcut.sound_music, ""                 },
4877   { TYPE_EMPTY,         NULL,                   ""                      },
4878   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4879
4880   { 0,                  NULL,                   NULL                    }
4881 };
4882
4883 static struct TokenInfo setup_info_shortcuts_5[] =
4884 {
4885   { TYPE_KEYTEXT,       NULL,                   "Snap Left:",           },
4886   { TYPE_KEY,           &setup.shortcut.snap_left, ""                   },
4887   { TYPE_KEYTEXT,       NULL,                   "Snap Right:",          },
4888   { TYPE_KEY,           &setup.shortcut.snap_right, ""                  },
4889   { TYPE_KEYTEXT,       NULL,                   "Snap Up:",             },
4890   { TYPE_KEY,           &setup.shortcut.snap_up, ""                     },
4891   { TYPE_KEYTEXT,       NULL,                   "Snap Down:",           },
4892   { TYPE_KEY,           &setup.shortcut.snap_down, ""                   },
4893   { TYPE_EMPTY,         NULL,                   ""                      },
4894   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
4895
4896   { 0,                  NULL,                   NULL                    }
4897 };
4898
4899 static Key getSetupKey()
4900 {
4901   Key key = KSYM_UNDEFINED;
4902   boolean got_key_event = FALSE;
4903
4904   while (!got_key_event)
4905   {
4906     if (PendingEvent())         /* got event */
4907     {
4908       Event event;
4909
4910       NextEvent(&event);
4911
4912       switch (event.type)
4913       {
4914         case EVENT_KEYPRESS:
4915           {
4916             key = GetEventKey((KeyEvent *)&event, TRUE);
4917
4918             /* press 'Escape' or 'Enter' to keep the existing key binding */
4919             if (key == KSYM_Escape || key == KSYM_Return)
4920               key = KSYM_UNDEFINED;     /* keep old value */
4921
4922             got_key_event = TRUE;
4923           }
4924           break;
4925
4926         case EVENT_KEYRELEASE:
4927           key_joystick_mapping = 0;
4928           break;
4929
4930         default:
4931           HandleOtherEvents(&event);
4932           break;
4933       }
4934     }
4935
4936     DoAnimation();
4937     BackToFront();
4938
4939     /* don't eat all CPU time */
4940     Delay(10);
4941   }
4942
4943   return key;
4944 }
4945
4946 static int getSetupTextFont(int type)
4947 {
4948   if (type & (TYPE_SWITCH       |
4949               TYPE_YES_NO       |
4950               TYPE_YES_NO_AUTO  |
4951               TYPE_STRING       |
4952               TYPE_ECS_AGA      |
4953               TYPE_KEYTEXT      |
4954               TYPE_ENTER_LIST))
4955     return FONT_MENU_2;
4956   else
4957     return FONT_MENU_1;
4958 }
4959
4960 static int getSetupValueFont(int type, void *value)
4961 {
4962   if (type & TYPE_KEY)
4963     return (type & TYPE_QUERY ? FONT_INPUT_1_ACTIVE : FONT_VALUE_1);
4964   else if (type & TYPE_STRING)
4965     return FONT_VALUE_2;
4966   else if (type & TYPE_ECS_AGA)
4967     return FONT_VALUE_1;
4968   else if (type & TYPE_BOOLEAN_STYLE)
4969     return (*(boolean *)value ? FONT_OPTION_ON : FONT_OPTION_OFF);
4970   else if (type & TYPE_YES_NO_AUTO)
4971     return (*(int *)value == AUTO  ? FONT_OPTION_ON :
4972             *(int *)value == FALSE ? FONT_OPTION_OFF : FONT_OPTION_ON);
4973   else
4974     return FONT_VALUE_1;
4975 }
4976
4977 static void drawSetupValue(int pos)
4978 {
4979   boolean font_draw_xoffset_modified = FALSE;
4980   int font_draw_xoffset_old = -1;
4981   int xpos = MENU_SCREEN_VALUE_XPOS;
4982   int ypos = MENU_SCREEN_START_YPOS + pos;
4983   int startx = mSX + xpos * 32;
4984   int starty = mSY + ypos * 32;
4985   int font_nr, font_width;
4986 #if 0
4987   int font_height;
4988 #endif
4989   int type = setup_info[pos].type;
4990   void *value = setup_info[pos].value;
4991   char *value_string = getSetupValue(type, value);
4992 #if 1
4993   int i;
4994 #endif
4995
4996   if (value_string == NULL)
4997     return;
4998
4999   if (type & TYPE_KEY)
5000   {
5001     xpos = MENU_SCREEN_START_XPOS;
5002
5003     if (type & TYPE_QUERY)
5004       value_string = "<press key>";
5005   }
5006   else if (type & TYPE_STRING)
5007   {
5008     int max_value_len = (SCR_FIELDX - 2) * 2;
5009
5010     xpos = MENU_SCREEN_START_XPOS;
5011
5012     if (strlen(value_string) > max_value_len)
5013       value_string[max_value_len] = '\0';
5014   }
5015   else if (type & TYPE_YES_NO_AUTO)
5016   {
5017     xpos = MENU_SCREEN_VALUE_XPOS - 1;
5018   }
5019
5020   startx = mSX + xpos * 32;
5021   starty = mSY + ypos * 32;
5022   font_nr = getSetupValueFont(type, value);
5023   font_width = getFontWidth(font_nr);
5024 #if 0
5025   font_height = getFontHeight(font_nr);
5026 #endif
5027
5028   /* downward compatibility correction for Juergen Bonhagen's menu settings */
5029   if (setup_mode != SETUP_MODE_INPUT)
5030   {
5031     int check_font_nr = FONT_OPTION_ON; /* known font that needs correction */
5032     int font1_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
5033     int font2_xoffset = getFontBitmapInfo(check_font_nr)->draw_xoffset;
5034     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
5035     int text_font_nr = getSetupTextFont(FONT_MENU_2);
5036     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
5037     int text_width = MAX_MENU_TEXT_LENGTH_MEDIUM * getFontWidth(text_font_nr);
5038     boolean correct_font_draw_xoffset = FALSE;
5039
5040     if (xpos == MENU_SCREEN_START_XPOS &&
5041         startx + font1_xoffset < text_startx + text_font_xoffset)
5042       correct_font_draw_xoffset = TRUE;
5043
5044     if (xpos == MENU_SCREEN_VALUE_XPOS &&
5045         startx + font2_xoffset < text_startx + text_width + text_font_xoffset)
5046       correct_font_draw_xoffset = TRUE;
5047
5048     /* check if setup value would overlap with setup text when printed */
5049     /* (this can happen for extreme/wrong values for font draw offset) */
5050     if (correct_font_draw_xoffset)
5051     {
5052       font_draw_xoffset_old = getFontBitmapInfo(font_nr)->draw_xoffset;
5053       font_draw_xoffset_modified = TRUE;
5054
5055       if (type & TYPE_KEY)
5056         getFontBitmapInfo(font_nr)->draw_xoffset += 2 * getFontWidth(font_nr);
5057       else if (!(type & TYPE_STRING))
5058         getFontBitmapInfo(font_nr)->draw_xoffset = text_font_xoffset + 20 -
5059           MAX_MENU_TEXT_LENGTH_MEDIUM * (16 - getFontWidth(text_font_nr));
5060     }
5061   }
5062
5063 #if 0
5064   DrawBackground(startx, starty, SX + SXSIZE - startx, font_height);
5065 #else
5066   for (i = 0; i <= MENU_SCREEN_MAX_XPOS - xpos; i++)
5067     DrawText(startx + i * font_width, starty, " ", font_nr);
5068 #endif
5069
5070   DrawText(startx, starty, value_string, font_nr);
5071
5072   if (font_draw_xoffset_modified)
5073     getFontBitmapInfo(font_nr)->draw_xoffset = font_draw_xoffset_old;
5074 }
5075
5076 static void changeSetupValue(int pos, int dx)
5077 {
5078   if (setup_info[pos].type & TYPE_BOOLEAN_STYLE)
5079   {
5080     *(boolean *)setup_info[pos].value ^= TRUE;
5081   }
5082   else if (setup_info[pos].type & TYPE_YES_NO_AUTO)
5083   {
5084     *(int *)setup_info[pos].value =
5085       (dx == -1 ?
5086        (*(int *)setup_info[pos].value == AUTO ? TRUE :
5087         *(int *)setup_info[pos].value == TRUE ? FALSE : AUTO) :
5088        (*(int *)setup_info[pos].value == TRUE ? AUTO :
5089         *(int *)setup_info[pos].value == AUTO ? FALSE : TRUE));
5090   }
5091   else if (setup_info[pos].type & TYPE_KEY)
5092   {
5093     Key key;
5094
5095     setup_info[pos].type |= TYPE_QUERY;
5096     drawSetupValue(pos);
5097     setup_info[pos].type &= ~TYPE_QUERY;
5098
5099     key = getSetupKey();
5100     if (key != KSYM_UNDEFINED)
5101       *(Key *)setup_info[pos].value = key;
5102   }
5103
5104   drawSetupValue(pos);
5105 }
5106
5107 static void DrawCursorAndText_Setup(int pos, boolean active)
5108 {
5109   int xpos = MENU_SCREEN_START_XPOS;
5110   int ypos = MENU_SCREEN_START_YPOS + pos;
5111   int font_nr = getSetupTextFont(setup_info[pos].type);
5112
5113   if (setup_info == setup_info_input)
5114     font_nr = FONT_MENU_1;
5115
5116   if (active)
5117     font_nr = FONT_ACTIVE(font_nr);
5118
5119   DrawText(mSX + xpos * 32, mSY + ypos * 32, setup_info[pos].text, font_nr);
5120
5121   if (setup_info[pos].type & ~TYPE_SKIP_ENTRY)
5122     drawCursor(pos, active);
5123 }
5124
5125 static void DrawSetupScreen_Generic()
5126 {
5127   boolean redraw_all = FALSE;
5128   char *title_string = NULL;
5129   int i;
5130
5131   UnmapAllGadgets();
5132   CloseDoor(DOOR_CLOSE_2);
5133
5134   if (redraw_mask & REDRAW_ALL)
5135     redraw_all = TRUE;
5136
5137 #if 0
5138   printf("::: %s\n", (redraw_mask & REDRAW_FIELD ? "REDRAW_FIELD" :
5139                       redraw_mask & REDRAW_ALL ? "REDRAW_ALL" :
5140                       int2str(0, redraw_mask)));
5141 #endif
5142
5143 #if 0
5144   /* !!! usually REDRAW_NONE => DOES NOT WORK (with fade) => CHECK THIS !!! */
5145   FadeOut(redraw_mask);
5146 #else
5147   FadeOut(REDRAW_FIELD);
5148 #endif
5149
5150   ClearField();
5151
5152   if (setup_mode == SETUP_MODE_MAIN)
5153   {
5154     setup_info = setup_info_main;
5155     title_string = "Setup";
5156   }
5157   else if (setup_mode == SETUP_MODE_GAME)
5158   {
5159     setup_info = setup_info_game;
5160     title_string = "Setup Game";
5161   }
5162   else if (setup_mode == SETUP_MODE_EDITOR)
5163   {
5164     setup_info = setup_info_editor;
5165     title_string = "Setup Editor";
5166   }
5167   else if (setup_mode == SETUP_MODE_GRAPHICS)
5168   {
5169     setup_info = setup_info_graphics;
5170     title_string = "Setup Graphics";
5171   }
5172   else if (setup_mode == SETUP_MODE_SOUND)
5173   {
5174     setup_info = setup_info_sound;
5175     title_string = "Setup Sound";
5176   }
5177   else if (setup_mode == SETUP_MODE_ARTWORK)
5178   {
5179     setup_info = setup_info_artwork;
5180     title_string = "Custom Artwork";
5181   }
5182   else if (setup_mode == SETUP_MODE_SHORTCUTS)
5183   {
5184     setup_info = setup_info_shortcuts;
5185     title_string = "Setup Shortcuts";
5186   }
5187   else if (setup_mode == SETUP_MODE_SHORTCUTS_1)
5188   {
5189     setup_info = setup_info_shortcuts_1;
5190     title_string = "Setup Shortcuts";
5191   }
5192   else if (setup_mode == SETUP_MODE_SHORTCUTS_2)
5193   {
5194     setup_info = setup_info_shortcuts_2;
5195     title_string = "Setup Shortcuts";
5196   }
5197   else if (setup_mode == SETUP_MODE_SHORTCUTS_3)
5198   {
5199     setup_info = setup_info_shortcuts_3;
5200     title_string = "Setup Shortcuts";
5201   }
5202   else if (setup_mode == SETUP_MODE_SHORTCUTS_4)
5203   {
5204     setup_info = setup_info_shortcuts_4;
5205     title_string = "Setup Shortcuts";
5206   }
5207   else if (setup_mode == SETUP_MODE_SHORTCUTS_5)
5208   {
5209     setup_info = setup_info_shortcuts_5;
5210     title_string = "Setup Shortcuts";
5211   }
5212
5213   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, title_string);
5214
5215   num_setup_info = 0;
5216 #if 1
5217   for (i = 0; setup_info[i].type != 0 && i < MAX_MENU_ENTRIES_ON_SCREEN; i++)
5218 #else
5219   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
5220 #endif
5221   {
5222     void *value_ptr = setup_info[i].value;
5223
5224     /* set some entries to "unchangeable" according to other variables */
5225     if ((value_ptr == &setup.sound_simple && !audio.sound_available) ||
5226         (value_ptr == &setup.sound_loops  && !audio.loops_available) ||
5227         (value_ptr == &setup.sound_music  && !audio.music_available) ||
5228         (value_ptr == &setup.fullscreen   && !video.fullscreen_available) ||
5229         (value_ptr == &screen_mode_text   && !video.fullscreen_available) ||
5230         (value_ptr == &window_size_text   && !video.window_scaling_available))
5231       setup_info[i].type |= TYPE_GHOSTED;
5232
5233     if (setup_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
5234       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
5235     else if (setup_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
5236       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
5237     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
5238       initCursor(i, IMG_MENU_BUTTON);
5239
5240     DrawCursorAndText_Setup(i, FALSE);
5241
5242     if (setup_info[i].type & TYPE_VALUE)
5243       drawSetupValue(i);
5244
5245     num_setup_info++;
5246   }
5247
5248 #if 0
5249   DrawTextSCentered(SYSIZE - 20, FONT_TEXT_4,
5250                     "Joysticks deactivated in setup menu");
5251 #endif
5252
5253 #if 1
5254   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
5255 #endif
5256
5257   if (redraw_all)
5258     redraw_mask = REDRAW_ALL;
5259
5260 #if 1
5261   FadeIn(redraw_mask);
5262 #else
5263   FadeIn(REDRAW_FIELD);
5264 #endif
5265
5266   InitAnimation();
5267 #if 0
5268   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
5269 #endif
5270 }
5271
5272 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
5273 {
5274   static int choice_store[MAX_SETUP_MODES];
5275   int choice = choice_store[setup_mode];        /* always starts with 0 */
5276   int x = 0;
5277   int y = choice;
5278
5279   if (button == MB_MENU_INITIALIZE)
5280   {
5281     /* advance to first valid menu entry */
5282     while (choice < num_setup_info &&
5283            setup_info[choice].type & TYPE_SKIP_ENTRY)
5284       choice++;
5285     choice_store[setup_mode] = choice;
5286
5287     DrawCursorAndText_Setup(choice, TRUE);
5288
5289     return;
5290   }
5291   else if (button == MB_MENU_LEAVE)
5292   {
5293     PlaySound(SND_MENU_ITEM_SELECTING);
5294
5295     for (y = 0; y < num_setup_info; y++)
5296     {
5297       if (setup_info[y].type & TYPE_LEAVE_MENU)
5298       {
5299         void (*menu_callback_function)(void) = setup_info[y].value;
5300
5301         FadeSetLeaveMenu();
5302
5303         menu_callback_function();
5304
5305         break;  /* absolutely needed because function changes 'setup_info'! */
5306       }
5307     }
5308
5309     return;
5310   }
5311
5312   if (mx || my)         /* mouse input */
5313   {
5314     x = (mx - mSX) / 32;
5315     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
5316   }
5317   else if (dx || dy)    /* keyboard input */
5318   {
5319     if (dx)
5320     {
5321       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE : TYPE_ENTER);
5322
5323       if (setup_info[choice].type & menu_navigation_type ||
5324           setup_info[choice].type & TYPE_BOOLEAN_STYLE ||
5325           setup_info[choice].type & TYPE_YES_NO_AUTO)
5326         button = MB_MENU_CHOICE;
5327     }
5328     else if (dy)
5329       y = choice + dy;
5330
5331     /* jump to next non-empty menu entry (up or down) */
5332     while (y > 0 && y < num_setup_info - 1 &&
5333            setup_info[y].type & TYPE_SKIP_ENTRY)
5334       y += dy;
5335   }
5336
5337   if (IN_VIS_FIELD(x, y) && y >= 0 && y < num_setup_info)
5338   {
5339     if (button)
5340     {
5341       if (y != choice && setup_info[y].type & ~TYPE_SKIP_ENTRY)
5342       {
5343         PlaySound(SND_MENU_ITEM_ACTIVATING);
5344
5345         DrawCursorAndText_Setup(choice, FALSE);
5346         DrawCursorAndText_Setup(y, TRUE);
5347
5348         choice = choice_store[setup_mode] = y;
5349       }
5350     }
5351     else if (!(setup_info[y].type & TYPE_GHOSTED))
5352     {
5353       PlaySound(SND_MENU_ITEM_SELECTING);
5354
5355       /* when selecting key headline, execute function for key value change */
5356       if (setup_info[y].type & TYPE_KEYTEXT &&
5357           setup_info[y + 1].type & TYPE_KEY)
5358         y++;
5359
5360       /* when selecting string value, execute function for list selection */
5361       if (setup_info[y].type & TYPE_STRING && y > 0 &&
5362           setup_info[y - 1].type & TYPE_ENTER_LIST)
5363         y--;
5364
5365       if (setup_info[y].type & TYPE_ENTER_OR_LEAVE)
5366       {
5367         void (*menu_callback_function)(void) = setup_info[y].value;
5368
5369         FadeSetFromType(setup_info[y].type);
5370
5371         menu_callback_function();
5372       }
5373       else
5374       {
5375         if (setup_info[y].type & TYPE_VALUE)
5376           changeSetupValue(y, dx);
5377       }
5378     }
5379   }
5380 }
5381
5382 void DrawSetupScreen_Input()
5383 {
5384   int i;
5385
5386 #if 1
5387   FadeOut(REDRAW_FIELD);
5388 #endif
5389
5390   ClearField();
5391
5392 #if 1
5393   setup_info = setup_info_input;
5394 #endif
5395
5396   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Setup Input");
5397
5398 #if 1
5399 #if 1
5400   DrawTextSCentered(SYSIZE - 20, FONT_TITLE_2,
5401                     "Joysticks deactivated on this screen");
5402 #else
5403   DrawTextSCentered(SYSIZE - 20, FONT_TEXT_4,
5404                     "Joysticks deactivated on this screen");
5405 #endif
5406 #endif
5407
5408 #if 1
5409   for (i = 0; setup_info[i].type != 0 && i < MAX_MENU_ENTRIES_ON_SCREEN; i++)
5410 #else
5411   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
5412 #endif
5413   {
5414     if (setup_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
5415       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
5416     else if (setup_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
5417       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
5418     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
5419       initCursor(i, IMG_MENU_BUTTON);
5420
5421     DrawCursorAndText_Setup(i, FALSE);
5422   }
5423
5424 #if 0
5425   DeactivateJoystickForCalibration();
5426 #endif
5427
5428 #if 0
5429 #if 1
5430   DrawTextSCentered(SYSIZE - 20, FONT_TITLE_2,
5431                     "Joysticks deactivated on this screen");
5432 #else
5433   DrawTextSCentered(SYSIZE - 20, FONT_TEXT_4,
5434                     "Joysticks deactivated on this screen");
5435 #endif
5436 #endif
5437
5438   /* create gadgets for setup input menu screen */
5439   FreeScreenGadgets();
5440   CreateScreenGadgets();
5441
5442   /* map gadgets for setup input menu screen */
5443   MapScreenMenuGadgets(SCREEN_MASK_INPUT);
5444
5445   HandleSetupScreen_Input(0, 0, 0, 0, MB_MENU_INITIALIZE);
5446
5447 #if 1
5448   FadeIn(REDRAW_FIELD);
5449 #endif
5450
5451   InitAnimation();
5452 }
5453
5454 static void setJoystickDeviceToNr(char *device_name, int device_nr)
5455 {
5456   if (device_name == NULL)
5457     return;
5458
5459   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
5460     device_nr = 0;
5461
5462   if (strlen(device_name) > 1)
5463   {
5464     char c1 = device_name[strlen(device_name) - 1];
5465     char c2 = device_name[strlen(device_name) - 2];
5466
5467     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
5468       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
5469   }
5470   else
5471     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
5472             strlen(device_name));
5473 }
5474
5475 static void drawPlayerSetupInputInfo(int player_nr, boolean active)
5476 {
5477   int i;
5478   static struct SetupKeyboardInfo custom_key;
5479   static struct
5480   {
5481     Key *key;
5482     char *text;
5483   } custom[] =
5484   {
5485     { &custom_key.left,  "Joystick Left"  },
5486     { &custom_key.right, "Joystick Right" },
5487     { &custom_key.up,    "Joystick Up"    },
5488     { &custom_key.down,  "Joystick Down"  },
5489     { &custom_key.snap,  "Button 1"       },
5490     { &custom_key.drop,  "Button 2"       }
5491   };
5492   static char *joystick_name[MAX_PLAYERS] =
5493   {
5494     "Joystick1",
5495     "Joystick2",
5496     "Joystick3",
5497     "Joystick4"
5498   };
5499   int text_font_nr = (active ? FONT_MENU_1_ACTIVE : FONT_MENU_1);
5500
5501   InitJoysticks();
5502
5503   custom_key = setup.input[player_nr].key;
5504
5505   DrawText(mSX + 11 * 32, mSY + 2 * 32, int2str(player_nr + 1, 1),
5506            FONT_INPUT_1_ACTIVE);
5507
5508   ClearRectangleOnBackground(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
5509                              TILEX, TILEY);
5510   DrawFixedGraphicThruMaskExt(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
5511                               PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
5512
5513   if (setup.input[player_nr].use_joystick)
5514   {
5515     char *device_name = setup.input[player_nr].joy.device_name;
5516     char *text = joystick_name[getJoystickNrFromDeviceName(device_name)];
5517     int font_nr = (joystick.fd[player_nr] < 0 ? FONT_VALUE_OLD : FONT_VALUE_1);
5518
5519     DrawText(mSX + 8 * 32, mSY + 3 * 32, text, font_nr);
5520     DrawText(mSX + 32, mSY + 4 * 32, "Calibrate", text_font_nr);
5521   }
5522   else
5523   {
5524     DrawText(mSX + 8 * 32, mSY + 3 * 32, "Keyboard ", FONT_VALUE_1);
5525     DrawText(mSX + 1 * 32, mSY + 4 * 32, "Customize", text_font_nr);
5526   }
5527
5528   DrawText(mSX + 32, mSY + 5 * 32, "Actual Settings:", FONT_MENU_1);
5529
5530   drawCursorXY(1, 4, IMG_MENU_BUTTON_LEFT);
5531   drawCursorXY(1, 5, IMG_MENU_BUTTON_RIGHT);
5532   drawCursorXY(1, 6, IMG_MENU_BUTTON_UP);
5533   drawCursorXY(1, 7, IMG_MENU_BUTTON_DOWN);
5534
5535   DrawText(mSX + 2 * 32, mSY +  6 * 32, ":", FONT_VALUE_OLD);
5536   DrawText(mSX + 2 * 32, mSY +  7 * 32, ":", FONT_VALUE_OLD);
5537   DrawText(mSX + 2 * 32, mSY +  8 * 32, ":", FONT_VALUE_OLD);
5538   DrawText(mSX + 2 * 32, mSY +  9 * 32, ":", FONT_VALUE_OLD);
5539   DrawText(mSX + 1 * 32, mSY + 10 * 32, "Snap Field:", FONT_VALUE_OLD);
5540   DrawText(mSX + 1 * 32, mSY + 12 * 32, "Drop Element:", FONT_VALUE_OLD);
5541
5542   for (i = 0; i < 6; i++)
5543   {
5544     int ypos = 6 + i + (i > 3 ? i-3 : 0);
5545
5546     DrawText(mSX + 3 * 32, mSY + ypos * 32,
5547              "              ", FONT_VALUE_1);
5548     DrawText(mSX + 3 * 32, mSY + ypos * 32,
5549              (setup.input[player_nr].use_joystick ?
5550               custom[i].text :
5551               getKeyNameFromKey(*custom[i].key)), FONT_VALUE_1);
5552   }
5553 }
5554
5555 static int input_player_nr = 0;
5556
5557 void HandleSetupScreen_Input_Player(int step, int direction)
5558 {
5559   int old_player_nr = input_player_nr;
5560   int new_player_nr;
5561
5562   new_player_nr = old_player_nr + step * direction;
5563   if (new_player_nr < 0)
5564     new_player_nr = 0;
5565   if (new_player_nr > MAX_PLAYERS - 1)
5566     new_player_nr = MAX_PLAYERS - 1;
5567
5568   if (new_player_nr != old_player_nr)
5569   {
5570     input_player_nr = new_player_nr;
5571
5572     drawPlayerSetupInputInfo(input_player_nr, FALSE);
5573   }
5574 }
5575
5576 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
5577 {
5578   static int choice = 0;
5579   int x = 0;
5580   int y = choice;
5581   int pos_start  = SETUPINPUT_SCREEN_POS_START;
5582   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
5583   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
5584   int pos_end    = SETUPINPUT_SCREEN_POS_END;
5585
5586   if (button == MB_MENU_INITIALIZE)
5587   {
5588     drawPlayerSetupInputInfo(input_player_nr, (choice == 2));
5589
5590     DrawCursorAndText_Setup(choice, TRUE);
5591
5592     return;
5593   }
5594   else if (button == MB_MENU_LEAVE)
5595   {
5596     setup_mode = SETUP_MODE_MAIN;
5597     DrawSetupScreen();
5598     InitJoysticks();
5599
5600     return;
5601   }
5602
5603   if (mx || my)         /* mouse input */
5604   {
5605     x = (mx - mSX) / 32;
5606     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
5607   }
5608   else if (dx || dy)    /* keyboard input */
5609   {
5610     if (dx && choice == 0)
5611       x = (dx < 0 ? 10 : 12);
5612     else if ((dx && choice == 1) ||
5613              (dx == +1 && choice == 2) ||
5614              (dx == -1 && choice == pos_end))
5615       button = MB_MENU_CHOICE;
5616     else if (dy)
5617       y = choice + dy;
5618
5619     if (y >= pos_empty1 && y <= pos_empty2)
5620       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
5621   }
5622
5623   if (y == 0 && dx != 0 && button)
5624   {
5625     HandleSetupScreen_Input_Player(1, dx < 0 ? -1 : +1);
5626   }
5627   else if (IN_VIS_FIELD(x, y) &&
5628            y >= pos_start && y <= pos_end &&
5629            !(y >= pos_empty1 && y <= pos_empty2))
5630   {
5631     if (button)
5632     {
5633       if (y != choice)
5634       {
5635         DrawCursorAndText_Setup(choice, FALSE);
5636         DrawCursorAndText_Setup(y, TRUE);
5637
5638         drawPlayerSetupInputInfo(input_player_nr, (y == 2));
5639
5640         choice = y;
5641       }
5642     }
5643     else
5644     {
5645       if (y == 1)
5646       {
5647         char *device_name = setup.input[input_player_nr].joy.device_name;
5648
5649         if (!setup.input[input_player_nr].use_joystick)
5650         {
5651           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
5652
5653           setJoystickDeviceToNr(device_name, new_device_nr);
5654           setup.input[input_player_nr].use_joystick = TRUE;
5655         }
5656         else
5657         {
5658           int device_nr = getJoystickNrFromDeviceName(device_name);
5659           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
5660
5661           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
5662             setup.input[input_player_nr].use_joystick = FALSE;
5663           else
5664             setJoystickDeviceToNr(device_name, new_device_nr);
5665         }
5666
5667         drawPlayerSetupInputInfo(input_player_nr, FALSE);
5668       }
5669       else if (y == 2)
5670       {
5671         if (setup.input[input_player_nr].use_joystick)
5672         {
5673           InitJoysticks();
5674           CalibrateJoystick(input_player_nr);
5675         }
5676         else
5677           CustomizeKeyboard(input_player_nr);
5678       }
5679       else if (y == pos_end)
5680       {
5681         InitJoysticks();
5682
5683         FadeSetLeaveMenu();
5684
5685         setup_mode = SETUP_MODE_MAIN;
5686         DrawSetupScreen();
5687       }
5688     }
5689   }
5690 }
5691
5692 void CustomizeKeyboard(int player_nr)
5693 {
5694   int i;
5695   int step_nr;
5696   boolean finished = FALSE;
5697   static struct SetupKeyboardInfo custom_key;
5698   static struct
5699   {
5700     Key *key;
5701     char *text;
5702   } customize_step[] =
5703   {
5704     { &custom_key.left,  "Move Left"    },
5705     { &custom_key.right, "Move Right"   },
5706     { &custom_key.up,    "Move Up"      },
5707     { &custom_key.down,  "Move Down"    },
5708     { &custom_key.snap,  "Snap Field"   },
5709     { &custom_key.drop,  "Drop Element" }
5710   };
5711
5712   /* read existing key bindings from player setup */
5713   custom_key = setup.input[player_nr].key;
5714
5715   FadeSetEnterMenu();
5716   FadeOut(REDRAW_FIELD);
5717
5718   ClearField();
5719
5720   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Keyboard Input");
5721
5722 #if 0
5723   BackToFront();
5724   InitAnimation();
5725 #endif
5726
5727   step_nr = 0;
5728   DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
5729            customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
5730   DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
5731            "Key:", FONT_INPUT_1_ACTIVE);
5732   DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5733            getKeyNameFromKey(*customize_step[step_nr].key), FONT_VALUE_OLD);
5734
5735 #if 1
5736   FadeIn(REDRAW_FIELD);
5737
5738   InitAnimation();
5739 #endif
5740
5741   while (!finished)
5742   {
5743     if (PendingEvent())         /* got event */
5744     {
5745       Event event;
5746
5747       NextEvent(&event);
5748
5749       switch (event.type)
5750       {
5751         case EVENT_KEYPRESS:
5752           {
5753             Key key = GetEventKey((KeyEvent *)&event, FALSE);
5754
5755             if (key == KSYM_Escape || (key == KSYM_Return && step_nr == 6))
5756             {
5757               if (key == KSYM_Escape)
5758                 FadeSkipNextFadeIn();
5759
5760               finished = TRUE;
5761               break;
5762             }
5763
5764             /* all keys configured -- wait for "Escape" or "Return" key */
5765             if (step_nr == 6)
5766               break;
5767
5768             /* press 'Enter' to keep the existing key binding */
5769             if (key == KSYM_Return)
5770               key = *customize_step[step_nr].key;
5771
5772             /* check if key already used */
5773             for (i = 0; i < step_nr; i++)
5774               if (*customize_step[i].key == key)
5775                 break;
5776             if (i < step_nr)
5777               break;
5778
5779             /* got new key binding */
5780             *customize_step[step_nr].key = key;
5781             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5782                      "             ", FONT_VALUE_1);
5783             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5784                      getKeyNameFromKey(key), FONT_VALUE_1);
5785             step_nr++;
5786
5787             /* un-highlight last query */
5788             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1)) * 32,
5789                      customize_step[step_nr - 1].text, FONT_MENU_1);
5790             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1) + 1) * 32,
5791                      "Key:", FONT_MENU_1);
5792
5793             /* press 'Enter' to leave */
5794             if (step_nr == 6)
5795             {
5796               DrawText(mSX + 16, mSY + 15 * 32 + 16,
5797                        "Press Enter", FONT_TITLE_1);
5798               break;
5799             }
5800
5801             /* query next key binding */
5802             DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
5803                      customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
5804             DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
5805                      "Key:", FONT_INPUT_1_ACTIVE);
5806             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
5807                      getKeyNameFromKey(*customize_step[step_nr].key),
5808                      FONT_VALUE_OLD);
5809           }
5810           break;
5811
5812         case EVENT_KEYRELEASE:
5813           key_joystick_mapping = 0;
5814           break;
5815
5816         default:
5817           HandleOtherEvents(&event);
5818           break;
5819       }
5820     }
5821
5822     DoAnimation();
5823     BackToFront();
5824
5825     /* don't eat all CPU time */
5826     Delay(10);
5827   }
5828
5829   /* write new key bindings back to player setup */
5830   setup.input[player_nr].key = custom_key;
5831
5832   StopAnimation();
5833   DrawSetupScreen_Input();
5834 }
5835
5836 static boolean CalibrateJoystickMain(int player_nr)
5837 {
5838   int new_joystick_xleft = JOYSTICK_XMIDDLE;
5839   int new_joystick_xright = JOYSTICK_XMIDDLE;
5840   int new_joystick_yupper = JOYSTICK_YMIDDLE;
5841   int new_joystick_ylower = JOYSTICK_YMIDDLE;
5842   int new_joystick_xmiddle, new_joystick_ymiddle;
5843
5844   int joystick_fd = joystick.fd[player_nr];
5845   int x, y, last_x, last_y, xpos = 8, ypos = 3;
5846   boolean check[3][3];
5847   int check_remaining = 3 * 3;
5848   int joy_x, joy_y;
5849   int joy_value;
5850   int result = -1;
5851
5852   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
5853     return FALSE;
5854
5855   if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
5856     return FALSE;
5857
5858   FadeSetEnterMenu();
5859   FadeOut(REDRAW_FIELD);
5860
5861   ClearField();
5862
5863   for (y = 0; y < 3; y++)
5864   {
5865     for (x = 0; x < 3; x++)
5866     {
5867       DrawFixedGraphic(xpos + x - 1, ypos + y - 1, IMG_MENU_CALIBRATE_BLUE, 0);
5868       check[x][y] = FALSE;
5869     }
5870   }
5871
5872   DrawTextSCentered(mSY - SY +  6 * 32, FONT_TITLE_1, "Rotate joystick");
5873   DrawTextSCentered(mSY - SY +  7 * 32, FONT_TITLE_1, "in all directions");
5874   DrawTextSCentered(mSY - SY +  9 * 32, FONT_TITLE_1, "if all balls");
5875   DrawTextSCentered(mSY - SY + 10 * 32, FONT_TITLE_1, "are marked,");
5876   DrawTextSCentered(mSY - SY + 11 * 32, FONT_TITLE_1, "center joystick");
5877   DrawTextSCentered(mSY - SY + 12 * 32, FONT_TITLE_1, "and");
5878   DrawTextSCentered(mSY - SY + 13 * 32, FONT_TITLE_1, "press any button!");
5879
5880   joy_value = Joystick(player_nr);
5881   last_x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
5882   last_y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
5883
5884   /* eventually uncalibrated center position (joystick could be uncentered) */
5885   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
5886     return FALSE;
5887
5888   new_joystick_xmiddle = joy_x;
5889   new_joystick_ymiddle = joy_y;
5890
5891   DrawFixedGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_RED, 0);
5892
5893   FadeIn(REDRAW_FIELD);
5894
5895   while (Joystick(player_nr) & JOY_BUTTON);     /* wait for released button */
5896   InitAnimation();
5897
5898   while (result < 0)
5899   {
5900     if (PendingEvent())         /* got event */
5901     {
5902       Event event;
5903
5904       NextEvent(&event);
5905
5906       switch (event.type)
5907       {
5908         case EVENT_KEYPRESS:
5909           switch (GetEventKey((KeyEvent *)&event, TRUE))
5910           {
5911             case KSYM_Return:
5912               if (check_remaining == 0)
5913                 result = 1;
5914               break;
5915
5916             case KSYM_Escape:
5917               FadeSkipNextFadeIn();
5918               result = 0;
5919               break;
5920
5921             default:
5922               break;
5923           }
5924           break;
5925
5926         case EVENT_KEYRELEASE:
5927           key_joystick_mapping = 0;
5928           break;
5929
5930         default:
5931           HandleOtherEvents(&event);
5932           break;
5933       }
5934     }
5935
5936     if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
5937       return FALSE;
5938
5939     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_x);
5940     new_joystick_xright = MAX(new_joystick_xright, joy_x);
5941     new_joystick_yupper = MIN(new_joystick_yupper, joy_y);
5942     new_joystick_ylower = MAX(new_joystick_ylower, joy_y);
5943
5944     setup.input[player_nr].joy.xleft = new_joystick_xleft;
5945     setup.input[player_nr].joy.yupper = new_joystick_yupper;
5946     setup.input[player_nr].joy.xright = new_joystick_xright;
5947     setup.input[player_nr].joy.ylower = new_joystick_ylower;
5948     setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
5949     setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
5950
5951     CheckJoystickData();
5952
5953     joy_value = Joystick(player_nr);
5954
5955     if (joy_value & JOY_BUTTON && check_remaining == 0)
5956       result = 1;
5957
5958     x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
5959     y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
5960
5961     if (x != last_x || y != last_y)
5962     {
5963       DrawFixedGraphic(xpos + last_x, ypos + last_y,
5964                        IMG_MENU_CALIBRATE_YELLOW, 0);
5965       DrawFixedGraphic(xpos + x,      ypos + y,
5966                        IMG_MENU_CALIBRATE_RED,    0);
5967
5968       last_x = x;
5969       last_y = y;
5970
5971       if (check_remaining > 0 && !check[x+1][y+1])
5972       {
5973         check[x+1][y+1] = TRUE;
5974         check_remaining--;
5975       }
5976
5977 #if 0
5978 #ifdef DEBUG
5979       printf("LEFT / MIDDLE / RIGHT == %d / %d / %d\n",
5980              setup.input[player_nr].joy.xleft,
5981              setup.input[player_nr].joy.xmiddle,
5982              setup.input[player_nr].joy.xright);
5983       printf("UP / MIDDLE / DOWN == %d / %d / %d\n",
5984              setup.input[player_nr].joy.yupper,
5985              setup.input[player_nr].joy.ymiddle,
5986              setup.input[player_nr].joy.ylower);
5987 #endif
5988 #endif
5989
5990     }
5991
5992     DoAnimation();
5993     BackToFront();
5994
5995     /* don't eat all CPU time */
5996     Delay(10);
5997   }
5998
5999   /* calibrated center position (joystick should now be centered) */
6000   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
6001     return FALSE;
6002
6003   new_joystick_xmiddle = joy_x;
6004   new_joystick_ymiddle = joy_y;
6005
6006   StopAnimation();
6007
6008 #if 0
6009   DrawSetupScreen_Input();
6010 #endif
6011
6012   /* wait until the last pressed button was released */
6013   while (Joystick(player_nr) & JOY_BUTTON)
6014   {
6015     if (PendingEvent())         /* got event */
6016     {
6017       Event event;
6018
6019       NextEvent(&event);
6020       HandleOtherEvents(&event);
6021
6022       Delay(10);
6023     }
6024   }
6025
6026   return TRUE;
6027 }
6028
6029 void CalibrateJoystick(int player_nr)
6030 {
6031   if (!CalibrateJoystickMain(player_nr))
6032   {
6033     char *device_name = setup.input[player_nr].joy.device_name;
6034     int nr = getJoystickNrFromDeviceName(device_name) + 1;
6035     int xpos = mSX - SX;
6036     int ypos = mSY - SY;
6037
6038     ClearField();
6039
6040     DrawTextF(xpos + 16, ypos + 6 * 32, FONT_TITLE_1, "   JOYSTICK %d   ", nr);
6041     DrawTextF(xpos + 16, ypos + 7 * 32, FONT_TITLE_1, " NOT AVAILABLE! ");
6042     BackToFront();
6043
6044     Delay(2000);                /* show error message for a short time */
6045
6046     ClearEventQueue();
6047   }
6048
6049 #if 1
6050   DrawSetupScreen_Input();
6051 #endif
6052 }
6053
6054 void DrawSetupScreen()
6055 {
6056   DeactivateJoystick();
6057
6058   SetMainBackgroundImage(IMG_BACKGROUND_SETUP);
6059
6060   if (setup_mode == SETUP_MODE_INPUT)
6061     DrawSetupScreen_Input();
6062   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
6063     DrawChooseTree(&game_speed_current);
6064   else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE)
6065     DrawChooseTree(&screen_mode_current);
6066   else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
6067     DrawChooseTree(&window_size_current);
6068   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
6069     DrawChooseTree(&scroll_delay_current);
6070   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
6071     DrawChooseTree(&artwork.gfx_current);
6072   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
6073     DrawChooseTree(&artwork.snd_current);
6074   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
6075     DrawChooseTree(&artwork.mus_current);
6076   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE)
6077     DrawChooseTree(&volume_simple_current);
6078   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS)
6079     DrawChooseTree(&volume_loops_current);
6080   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
6081     DrawChooseTree(&volume_music_current);
6082   else
6083     DrawSetupScreen_Generic();
6084
6085   PlayMenuSound();
6086   PlayMenuMusic();
6087 }
6088
6089 void RedrawSetupScreenAfterFullscreenToggle()
6090 {
6091   if (setup_mode == SETUP_MODE_GRAPHICS)
6092     DrawSetupScreen();
6093 }
6094
6095 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
6096 {
6097   if (setup_mode == SETUP_MODE_INPUT)
6098     HandleSetupScreen_Input(mx, my, dx, dy, button);
6099   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
6100     HandleChooseTree(mx, my, dx, dy, button, &game_speed_current);
6101   else if (setup_mode == SETUP_MODE_CHOOSE_SCREEN_MODE)
6102     HandleChooseTree(mx, my, dx, dy, button, &screen_mode_current);
6103   else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
6104     HandleChooseTree(mx, my, dx, dy, button, &window_size_current);
6105   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
6106     HandleChooseTree(mx, my, dx, dy, button, &scroll_delay_current);
6107   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
6108     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
6109   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
6110     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
6111   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
6112     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
6113   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE)
6114     HandleChooseTree(mx, my, dx, dy, button, &volume_simple_current);
6115   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS)
6116     HandleChooseTree(mx, my, dx, dy, button, &volume_loops_current);
6117   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
6118     HandleChooseTree(mx, my, dx, dy, button, &volume_music_current);
6119   else
6120     HandleSetupScreen_Generic(mx, my, dx, dy, button);
6121
6122   DoAnimation();
6123 }
6124
6125 void HandleGameActions()
6126 {
6127   if (game_status != GAME_MODE_PLAYING)
6128     return;
6129
6130   GameActions();        /* main game loop */
6131
6132   if (tape.auto_play && !tape.playing)
6133     AutoPlayTape();     /* continue automatically playing next tape */
6134 }
6135
6136
6137 /* ---------- new screen button stuff -------------------------------------- */
6138
6139 static void getScreenMenuButtonPos(int *x, int *y, int gadget_id)
6140 {
6141   switch (gadget_id)
6142   {
6143 #if 1
6144     case SCREEN_CTRL_ID_PREV_LEVEL:
6145       *x = mSX + menu.main.button.prev_level.x;
6146       *y = mSY + menu.main.button.prev_level.y;
6147       break;
6148
6149     case SCREEN_CTRL_ID_NEXT_LEVEL:
6150       *x = mSX + menu.main.button.next_level.x;
6151       *y = mSY + menu.main.button.next_level.y;
6152       break;
6153 #else
6154     case SCREEN_CTRL_ID_PREV_LEVEL:
6155       *x = mSX + TILEX * getPrevlevelButtonPos();
6156       *y = mSY + TILEY * (MENU_SCREEN_START_YPOS + 1);
6157       break;
6158
6159     case SCREEN_CTRL_ID_NEXT_LEVEL:
6160       *x = mSX + TILEX * getNextLevelButtonPos();
6161       *y = mSY + TILEY * (MENU_SCREEN_START_YPOS + 1);
6162       break;
6163 #endif
6164
6165     case SCREEN_CTRL_ID_PREV_PLAYER:
6166       *x = mSX + TILEX * 10;
6167       *y = mSY + TILEY * MENU_SCREEN_START_YPOS;
6168       break;
6169
6170     case SCREEN_CTRL_ID_NEXT_PLAYER:
6171       *x = mSX + TILEX * 12;
6172       *y = mSY + TILEY * MENU_SCREEN_START_YPOS;
6173       break;
6174
6175     default:
6176       Error(ERR_EXIT, "unknown gadget ID %d", gadget_id);
6177   }
6178 }
6179
6180 static struct
6181 {
6182   int gfx_unpressed, gfx_pressed;
6183   void (*get_gadget_position)(int *, int *, int);
6184   int gadget_id;
6185   int screen_mask;
6186   char *infotext;
6187 } menubutton_info[NUM_SCREEN_MENUBUTTONS] =
6188 {
6189   {
6190     IMG_MENU_BUTTON_PREV_LEVEL, IMG_MENU_BUTTON_PREV_LEVEL_ACTIVE,
6191     getScreenMenuButtonPos,
6192     SCREEN_CTRL_ID_PREV_LEVEL,
6193     SCREEN_MASK_MAIN,
6194     "last level"
6195   },
6196   {
6197     IMG_MENU_BUTTON_NEXT_LEVEL, IMG_MENU_BUTTON_NEXT_LEVEL_ACTIVE,
6198     getScreenMenuButtonPos,
6199     SCREEN_CTRL_ID_NEXT_LEVEL,
6200     SCREEN_MASK_MAIN,
6201     "next level"
6202   },
6203   {
6204     IMG_MENU_BUTTON_LEFT, IMG_MENU_BUTTON_LEFT_ACTIVE,
6205     getScreenMenuButtonPos,
6206     SCREEN_CTRL_ID_PREV_PLAYER,
6207     SCREEN_MASK_INPUT,
6208     "last player"
6209   },
6210   {
6211     IMG_MENU_BUTTON_RIGHT, IMG_MENU_BUTTON_RIGHT_ACTIVE,
6212     getScreenMenuButtonPos,
6213     SCREEN_CTRL_ID_NEXT_PLAYER,
6214     SCREEN_MASK_INPUT,
6215     "next player"
6216   },
6217 };
6218
6219 static struct
6220 {
6221   int gfx_unpressed, gfx_pressed;
6222   int x, y;
6223   int gadget_id;
6224   char *infotext;
6225 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
6226 {
6227   {
6228     IMG_MENU_BUTTON_UP, IMG_MENU_BUTTON_UP_ACTIVE,
6229 #if 1
6230     -1, -1,     /* these values are not constant, but can change at runtime */
6231 #else
6232     SC_SCROLL_UP_XPOS, SC_SCROLL_UP_YPOS,
6233 #endif
6234     SCREEN_CTRL_ID_SCROLL_UP,
6235     "scroll up"
6236   },
6237   {
6238     IMG_MENU_BUTTON_DOWN, IMG_MENU_BUTTON_DOWN_ACTIVE,
6239 #if 1
6240     -1, -1,     /* these values are not constant, but can change at runtime */
6241 #else
6242     SC_SCROLL_DOWN_XPOS, SC_SCROLL_DOWN_YPOS,
6243 #endif
6244     SCREEN_CTRL_ID_SCROLL_DOWN,
6245     "scroll down"
6246   }
6247 };
6248
6249 static struct
6250 {
6251 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6252   Bitmap **gfx_unpressed, **gfx_pressed;
6253 #else
6254   int gfx_unpressed, gfx_pressed;
6255 #endif
6256   int x, y;
6257   int width, height;
6258   int type;
6259   int gadget_id;
6260   char *infotext;
6261 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
6262 {
6263   {
6264 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6265     &scrollbar_bitmap[0], &scrollbar_bitmap[1],
6266 #else
6267     IMG_MENU_SCROLLBAR, IMG_MENU_SCROLLBAR_ACTIVE,
6268 #endif
6269 #if 1
6270     -1, -1,     /* these values are not constant, but can change at runtime */
6271     -1, -1,     /* these values are not constant, but can change at runtime */
6272 #else
6273     SC_SCROLL_VERTICAL_XPOS, SC_SCROLL_VERTICAL_YPOS,
6274     SC_SCROLL_VERTICAL_XSIZE, SC_SCROLL_VERTICAL_YSIZE,
6275 #endif
6276     GD_TYPE_SCROLLBAR_VERTICAL,
6277     SCREEN_CTRL_ID_SCROLL_VERTICAL,
6278     "scroll level series vertically"
6279   }
6280 };
6281
6282 static void CreateScreenMenubuttons()
6283 {
6284   struct GadgetInfo *gi;
6285   unsigned int event_mask;
6286   int i;
6287
6288   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
6289   {
6290     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6291     int gfx_unpressed, gfx_pressed;
6292     int x, y, width, height;
6293     int gd_x1, gd_x2, gd_y1, gd_y2;
6294     int id = menubutton_info[i].gadget_id;
6295
6296     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
6297
6298     menubutton_info[i].get_gadget_position(&x, &y, id);
6299
6300     width = SC_MENUBUTTON_XSIZE;
6301     height = SC_MENUBUTTON_YSIZE;
6302
6303     gfx_unpressed = menubutton_info[i].gfx_unpressed;
6304     gfx_pressed   = menubutton_info[i].gfx_pressed;
6305     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6306     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6307     gd_x1 = graphic_info[gfx_unpressed].src_x;
6308     gd_y1 = graphic_info[gfx_unpressed].src_y;
6309     gd_x2 = graphic_info[gfx_pressed].src_x;
6310     gd_y2 = graphic_info[gfx_pressed].src_y;
6311
6312     gi = CreateGadget(GDI_CUSTOM_ID, id,
6313                       GDI_CUSTOM_TYPE_ID, i,
6314                       GDI_INFO_TEXT, menubutton_info[i].infotext,
6315                       GDI_X, x,
6316                       GDI_Y, y,
6317                       GDI_WIDTH, width,
6318                       GDI_HEIGHT, height,
6319                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
6320                       GDI_STATE, GD_BUTTON_UNPRESSED,
6321                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6322                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6323                       GDI_DIRECT_DRAW, FALSE,
6324                       GDI_EVENT_MASK, event_mask,
6325                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6326                       GDI_END);
6327
6328     if (gi == NULL)
6329       Error(ERR_EXIT, "cannot create gadget");
6330
6331     screen_gadget[id] = gi;
6332   }
6333 }
6334
6335 static void CreateScreenScrollbuttons()
6336 {
6337   struct GadgetInfo *gi;
6338   unsigned int event_mask;
6339   int i;
6340
6341   /* these values are not constant, but can change at runtime */
6342   scrollbutton_info[0].x = SC_SCROLL_UP_XPOS;
6343   scrollbutton_info[0].y = SC_SCROLL_UP_YPOS;
6344   scrollbutton_info[1].x = SC_SCROLL_DOWN_XPOS;
6345   scrollbutton_info[1].y = SC_SCROLL_DOWN_YPOS;
6346
6347   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
6348   {
6349     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6350     int gfx_unpressed, gfx_pressed;
6351     int x, y, width, height;
6352     int gd_x1, gd_x2, gd_y1, gd_y2;
6353     int id = scrollbutton_info[i].gadget_id;
6354
6355     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
6356
6357     x = mSX + scrollbutton_info[i].x + menu.scrollbar_xoffset;
6358     y = mSY + scrollbutton_info[i].y;
6359     width = SC_SCROLLBUTTON_XSIZE;
6360     height = SC_SCROLLBUTTON_YSIZE;
6361
6362     if (id == SCREEN_CTRL_ID_SCROLL_DOWN)
6363       y = mSY + (SC_SCROLL_VERTICAL_YPOS +
6364                  (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE);
6365
6366     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
6367     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
6368     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6369     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6370     gd_x1 = graphic_info[gfx_unpressed].src_x;
6371     gd_y1 = graphic_info[gfx_unpressed].src_y;
6372     gd_x2 = graphic_info[gfx_pressed].src_x;
6373     gd_y2 = graphic_info[gfx_pressed].src_y;
6374
6375     gi = CreateGadget(GDI_CUSTOM_ID, id,
6376                       GDI_CUSTOM_TYPE_ID, i,
6377                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
6378                       GDI_X, x,
6379                       GDI_Y, y,
6380                       GDI_WIDTH, width,
6381                       GDI_HEIGHT, height,
6382                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
6383                       GDI_STATE, GD_BUTTON_UNPRESSED,
6384                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6385                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6386                       GDI_DIRECT_DRAW, FALSE,
6387                       GDI_EVENT_MASK, event_mask,
6388                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6389                       GDI_END);
6390
6391     if (gi == NULL)
6392       Error(ERR_EXIT, "cannot create gadget");
6393
6394     screen_gadget[id] = gi;
6395   }
6396 }
6397
6398 static void CreateScreenScrollbars()
6399 {
6400   int i;
6401
6402   /* these values are not constant, but can change at runtime */
6403   scrollbar_info[0].x = SC_SCROLL_VERTICAL_XPOS;
6404   scrollbar_info[0].y = SC_SCROLL_VERTICAL_YPOS;
6405   scrollbar_info[0].width  = SC_SCROLL_VERTICAL_XSIZE;
6406   scrollbar_info[0].height = SC_SCROLL_VERTICAL_YSIZE;
6407
6408   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
6409   {
6410     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
6411 #if !defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6412     int gfx_unpressed, gfx_pressed;
6413 #endif
6414     int x, y, width, height;
6415     int gd_x1, gd_x2, gd_y1, gd_y2;
6416     struct GadgetInfo *gi;
6417     int items_max, items_visible, item_position;
6418     unsigned int event_mask;
6419     int num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
6420     int id = scrollbar_info[i].gadget_id;
6421
6422     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
6423
6424     x = mSX + scrollbar_info[i].x + menu.scrollbar_xoffset;
6425     y = mSY + scrollbar_info[i].y;
6426     width  = scrollbar_info[i].width;
6427     height = scrollbar_info[i].height;
6428
6429     if (id == SCREEN_CTRL_ID_SCROLL_VERTICAL)
6430       height = (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE;
6431
6432     items_max = num_page_entries;
6433     items_visible = num_page_entries;
6434     item_position = 0;
6435
6436 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6437     gd_bitmap_unpressed = *scrollbar_info[i].gfx_unpressed;
6438     gd_bitmap_pressed   = *scrollbar_info[i].gfx_pressed;
6439     gd_x1 = 0;
6440     gd_y1 = 0;
6441     gd_x2 = 0;
6442     gd_y2 = 0;
6443 #else
6444     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
6445     gfx_pressed   = scrollbar_info[i].gfx_pressed;
6446     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
6447     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
6448     gd_x1 = graphic_info[gfx_unpressed].src_x;
6449     gd_y1 = graphic_info[gfx_unpressed].src_y;
6450     gd_x2 = graphic_info[gfx_pressed].src_x;
6451     gd_y2 = graphic_info[gfx_pressed].src_y;
6452 #endif
6453
6454     gi = CreateGadget(GDI_CUSTOM_ID, id,
6455                       GDI_CUSTOM_TYPE_ID, i,
6456                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
6457                       GDI_X, x,
6458                       GDI_Y, y,
6459                       GDI_WIDTH, width,
6460                       GDI_HEIGHT, height,
6461                       GDI_TYPE, scrollbar_info[i].type,
6462                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
6463                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
6464                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
6465 #if 1
6466                       GDI_WHEEL_AREA_X, SX,
6467                       GDI_WHEEL_AREA_Y, SY,
6468                       GDI_WHEEL_AREA_WIDTH, SXSIZE,
6469                       GDI_WHEEL_AREA_HEIGHT, SYSIZE,
6470 #else
6471                       GDI_WHEEL_AREA_X, 0,
6472                       GDI_WHEEL_AREA_Y, 0,
6473                       GDI_WHEEL_AREA_WIDTH, WIN_XSIZE,
6474                       GDI_WHEEL_AREA_HEIGHT, WIN_YSIZE,
6475 #endif
6476                       GDI_STATE, GD_BUTTON_UNPRESSED,
6477                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
6478                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
6479                       GDI_BORDER_SIZE, SC_BORDER_SIZE, SC_BORDER_SIZE,
6480                       GDI_DIRECT_DRAW, FALSE,
6481                       GDI_EVENT_MASK, event_mask,
6482                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
6483                       GDI_END);
6484
6485     if (gi == NULL)
6486       Error(ERR_EXIT, "cannot create gadget");
6487
6488     screen_gadget[id] = gi;
6489   }
6490 }
6491
6492 void CreateScreenGadgets()
6493 {
6494   int last_game_status = game_status;   /* save current game status */
6495
6496 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6497   int i;
6498
6499   for (i = 0; i < NUM_SCROLLBAR_BITMAPS; i++)
6500   {
6501     scrollbar_bitmap[i] = CreateBitmap(TILEX, TILEY, DEFAULT_DEPTH);
6502
6503     /* copy pointers to clip mask and GC */
6504     scrollbar_bitmap[i]->clip_mask =
6505       graphic_info[IMG_MENU_SCROLLBAR + i].clip_mask;
6506     scrollbar_bitmap[i]->stored_clip_gc =
6507       graphic_info[IMG_MENU_SCROLLBAR + i].clip_gc;
6508
6509     BlitBitmap(graphic_info[IMG_MENU_SCROLLBAR + i].bitmap,
6510                scrollbar_bitmap[i],
6511                graphic_info[IMG_MENU_SCROLLBAR + i].src_x,
6512                graphic_info[IMG_MENU_SCROLLBAR + i].src_y,
6513                TILEX, TILEY, 0, 0);
6514   }
6515 #endif
6516
6517   CreateScreenMenubuttons();
6518
6519 #if 0
6520   /* force LEVELS draw offset for scrollbar / scrollbutton gadgets */
6521   game_status = GAME_MODE_LEVELS;
6522 #endif
6523
6524   CreateScreenScrollbuttons();
6525   CreateScreenScrollbars();
6526
6527   game_status = last_game_status;       /* restore current game status */
6528 }
6529
6530 void FreeScreenGadgets()
6531 {
6532   int i;
6533
6534 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
6535   for (i = 0; i < NUM_SCROLLBAR_BITMAPS; i++)
6536   {
6537     /* prevent freeing clip mask and GC twice */
6538     scrollbar_bitmap[i]->clip_mask = None;
6539     scrollbar_bitmap[i]->stored_clip_gc = None;
6540
6541     FreeBitmap(scrollbar_bitmap[i]);
6542   }
6543 #endif
6544
6545   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
6546     FreeGadget(screen_gadget[i]);
6547 }
6548
6549 void MapScreenMenuGadgets(int screen_mask)
6550 {
6551   int i;
6552
6553   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
6554     if (screen_mask & menubutton_info[i].screen_mask)
6555       MapGadget(screen_gadget[menubutton_info[i].gadget_id]);
6556 }
6557
6558 void MapScreenTreeGadgets(TreeInfo *ti)
6559 {
6560   int num_entries = numTreeInfoInGroup(ti);
6561   int i;
6562
6563   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
6564     return;
6565
6566   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
6567     MapGadget(screen_gadget[scrollbutton_info[i].gadget_id]);
6568
6569   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
6570     MapGadget(screen_gadget[scrollbar_info[i].gadget_id]);
6571 }
6572
6573 static void HandleScreenGadgets(struct GadgetInfo *gi)
6574 {
6575   int id = gi->custom_id;
6576   int button = gi->event.button;
6577   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
6578
6579   switch (id)
6580   {
6581     case SCREEN_CTRL_ID_PREV_LEVEL:
6582       HandleMainMenu_SelectLevel(step, -1);
6583       break;
6584
6585     case SCREEN_CTRL_ID_NEXT_LEVEL:
6586       HandleMainMenu_SelectLevel(step, +1);
6587       break;
6588
6589     case SCREEN_CTRL_ID_PREV_PLAYER:
6590       HandleSetupScreen_Input_Player(step, -1);
6591       break;
6592
6593     case SCREEN_CTRL_ID_NEXT_PLAYER:
6594       HandleSetupScreen_Input_Player(step, +1);
6595       break;
6596
6597     case SCREEN_CTRL_ID_SCROLL_UP:
6598       if (game_status == GAME_MODE_LEVELS)
6599         HandleChooseLevelSet(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6600       else if (game_status == GAME_MODE_LEVELNR)
6601         HandleChooseLevelNr(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6602       else if (game_status == GAME_MODE_SETUP)
6603         HandleSetupScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
6604       break;
6605
6606     case SCREEN_CTRL_ID_SCROLL_DOWN:
6607       if (game_status == GAME_MODE_LEVELS)
6608         HandleChooseLevelSet(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
6609       else if (game_status == GAME_MODE_LEVELNR)
6610         HandleChooseLevelNr(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
6611       else if (game_status == GAME_MODE_SETUP)
6612         HandleSetupScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
6613       break;
6614
6615     case SCREEN_CTRL_ID_SCROLL_VERTICAL:
6616       if (game_status == GAME_MODE_LEVELS)
6617         HandleChooseLevelSet(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
6618       else if (game_status == GAME_MODE_LEVELNR)
6619         HandleChooseLevelNr(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
6620       else if (game_status == GAME_MODE_SETUP)
6621         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
6622       break;
6623
6624     default:
6625       break;
6626   }
6627 }