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