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