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