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