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