rnd-20031116-1-src
[rocksndiamonds.git] / src / screens.c
1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back!               *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 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
27 /* screens in the setup menu */
28 #define SETUP_MODE_MAIN                 0
29 #define SETUP_MODE_GAME                 1
30 #define SETUP_MODE_EDITOR               2
31 #define SETUP_MODE_INPUT                3
32 #define SETUP_MODE_SHORTCUT             4
33 #define SETUP_MODE_GRAPHICS             5
34 #define SETUP_MODE_SOUND                6
35 #define SETUP_MODE_ARTWORK              7
36 #define SETUP_MODE_CHOOSE_GRAPHICS      8
37 #define SETUP_MODE_CHOOSE_SOUNDS        9
38 #define SETUP_MODE_CHOOSE_MUSIC         10
39
40 #define MAX_SETUP_MODES                 11
41
42 /* for input setup functions */
43 #define SETUPINPUT_SCREEN_POS_START     0
44 #define SETUPINPUT_SCREEN_POS_END       (SCR_FIELDY - 4)
45 #define SETUPINPUT_SCREEN_POS_EMPTY1    (SETUPINPUT_SCREEN_POS_START + 3)
46 #define SETUPINPUT_SCREEN_POS_EMPTY2    (SETUPINPUT_SCREEN_POS_END - 1)
47
48 /* for various menu stuff  */
49 #define MAX_MENU_ENTRIES_ON_SCREEN      (SCR_FIELDY - 2)
50 #define MENU_SCREEN_START_YPOS          2
51 #define MENU_SCREEN_VALUE_XPOS          14
52
53 /* buttons and scrollbars identifiers */
54 #define SCREEN_CTRL_ID_SCROLL_UP        0
55 #define SCREEN_CTRL_ID_SCROLL_DOWN      1
56 #define SCREEN_CTRL_ID_SCROLL_VERTICAL  2
57
58 #define NUM_SCREEN_SCROLLBUTTONS        2
59 #define NUM_SCREEN_SCROLLBARS           1
60 #define NUM_SCREEN_GADGETS              3
61
62 /* forward declarations of internal functions */
63 static void HandleScreenGadgets(struct GadgetInfo *);
64 static void HandleSetupScreen_Generic(int, int, int, int, int);
65 static void HandleSetupScreen_Input(int, int, int, int, int);
66 static void CustomizeKeyboard(int);
67 static void CalibrateJoystick(int);
68 static void execSetupArtwork(void);
69 static void HandleChooseTree(int, int, int, int, int, TreeInfo **);
70
71 static struct GadgetInfo *screen_gadget[NUM_SCREEN_GADGETS];
72 static int setup_mode = SETUP_MODE_MAIN;
73
74 #define mSX (SX + (game_status >= GAME_MODE_MAIN &&     \
75                    game_status <= GAME_MODE_SETUP ?     \
76                    menu.draw_xoffset[game_status] : menu.draw_xoffset_default))
77 #define mSY (SY + (game_status >= GAME_MODE_MAIN &&     \
78                    game_status <= GAME_MODE_SETUP ?     \
79                    menu.draw_yoffset[game_status] : menu.draw_yoffset_default))
80
81 #define NUM_MENU_ENTRIES_ON_SCREEN (menu.list_size[game_status] > 2 ?   \
82                                     menu.list_size[game_status] :       \
83                                     MAX_MENU_ENTRIES_ON_SCREEN)
84
85 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
86 #define NUM_SCROLLBAR_BITMAPS           2
87 static Bitmap *scrollbar_bitmap[NUM_SCROLLBAR_BITMAPS];
88 #endif
89
90
91 static void drawCursorExt(int xpos, int ypos, int color, int graphic)
92 {
93   static int cursor_array[SCR_FIELDY];
94
95   if (xpos == 0)
96   {
97     if (graphic != 0)
98       cursor_array[ypos] = graphic;
99     else
100       graphic = cursor_array[ypos];
101   }
102
103   if (color == FC_RED)
104     graphic = (graphic == IMG_MENU_BUTTON_LEFT  ? IMG_MENU_BUTTON_LEFT_ACTIVE :
105                graphic == IMG_MENU_BUTTON_RIGHT ? IMG_MENU_BUTTON_RIGHT_ACTIVE:
106                IMG_MENU_BUTTON_ACTIVE);
107
108   ypos += MENU_SCREEN_START_YPOS;
109
110   DrawBackground(mSX + xpos * TILEX, mSY + ypos * TILEY, TILEX, TILEY);
111   DrawGraphicThruMaskExt(drawto, mSX + xpos * TILEX, mSY + ypos * TILEY,
112                          graphic, 0);
113 }
114
115 static void initCursor(int ypos, int graphic)
116 {
117   drawCursorExt(0, ypos, FC_BLUE, graphic);
118 }
119
120 static void drawCursor(int ypos, int color)
121 {
122   drawCursorExt(0, ypos, color, 0);
123 }
124
125 static void drawCursorXY(int xpos, int ypos, int graphic)
126 {
127   drawCursorExt(xpos, ypos, -1, graphic);
128 }
129
130 static void drawChooseTreeCursor(int ypos, int color)
131 {
132   int last_game_status = game_status;   /* save current game status */
133
134   /* force LEVELS draw offset on artwork setup screen */
135   game_status = GAME_MODE_LEVELS;
136
137   drawCursorExt(0, ypos, color, 0);
138
139   game_status = last_game_status;       /* restore current game status */
140 }
141
142 static void PlaySound_Menu_Start(int sound)
143 {
144   if (sound_info[sound].loop)
145     PlaySoundLoop(sound);
146   else
147     PlaySound(sound);
148 }
149
150 static void PlaySound_Menu_Continue(int sound)
151 {
152   if (sound_info[sound].loop)
153     PlaySoundLoop(sound);
154 }
155
156 void DrawHeadline()
157 {
158   int text1_width = getTextWidth(PROGRAM_TITLE_STRING,     FONT_TITLE_1);
159   int text2_width = getTextWidth(PROGRAM_COPYRIGHT_STRING, FONT_TITLE_2);
160   int x1 = SX + (SXSIZE - text1_width) / 2;
161   int x2 = SX + (SXSIZE - text2_width) / 2;
162
163   DrawText(x1, SY + 8,  PROGRAM_TITLE_STRING,     FONT_TITLE_1);
164   DrawText(x2, SY + 46, PROGRAM_COPYRIGHT_STRING, FONT_TITLE_2);
165 }
166
167 static void ToggleFullscreenIfNeeded()
168 {
169   if (setup.fullscreen != video.fullscreen_enabled)
170   {
171     /* save old door content */
172     BlitBitmap(backbuffer, bitmap_db_door,
173                DX, DY, DXSIZE, DYSIZE, DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
174
175     /* toggle fullscreen */
176     ChangeVideoModeIfNeeded(setup.fullscreen);
177     setup.fullscreen = video.fullscreen_enabled;
178
179     /* redraw background to newly created backbuffer */
180     BlitBitmap(graphic_info[IMG_GLOBAL_BORDER].bitmap, backbuffer,
181                0,0, WIN_XSIZE,WIN_YSIZE, 0,0);
182
183     /* restore old door content */
184     BlitBitmap(bitmap_db_door, backbuffer,
185                DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1, DXSIZE, DYSIZE, DX, DY);
186
187     redraw_mask = REDRAW_ALL;
188   }
189 }
190
191 void DrawMainMenu()
192 {
193   static LevelDirTree *leveldir_last_valid = NULL;
194   char *name_text = (!options.network && setup.team_mode ? "Team:" : "Name:");
195   int name_width  = getTextWidth("Name:",  FONT_MENU_1);
196   int level_width = getTextWidth("Level:", FONT_MENU_1);
197   int i;
198
199   UnmapAllGadgets();
200   FadeSoundsAndMusic();
201
202   KeyboardAutoRepeatOn();
203   ActivateJoystick();
204
205   SetDrawDeactivationMask(REDRAW_NONE);
206   SetDrawBackgroundMask(REDRAW_FIELD);
207
208   audio.sound_deactivated = FALSE;
209
210   /* needed if last screen was the playing screen, invoked from level editor */
211   if (level_editor_test_game)
212   {
213     game_status = GAME_MODE_EDITOR;
214     DrawLevelEd();
215
216     return;
217   }
218
219   /* needed if last screen was the editor screen */
220   UndrawSpecialEditorDoor();
221
222   /* needed if last screen was the setup screen and fullscreen state changed */
223   ToggleFullscreenIfNeeded();
224
225   /* leveldir_current may be invalid (level group, parent link) */
226   if (!validLevelSeries(leveldir_current))
227     leveldir_current = getFirstValidTreeInfoEntry(leveldir_last_valid);
228
229   /* store valid level series information */
230   leveldir_last_valid = leveldir_current;
231
232   /* needed if last screen (level choice) changed graphics, sounds or music */
233   ReloadCustomArtwork();
234
235 #ifdef TARGET_SDL
236   SetDrawtoField(DRAW_BACKBUFFER);
237 #endif
238
239   /* map gadgets for main menu screen */
240   MapTapeButtons();
241
242   /* level_nr may have been set to value over handicap with level editor */
243   if (setup.handicap && level_nr > leveldir_current->handicap_level)
244     level_nr = leveldir_current->handicap_level;
245
246   GetPlayerConfig();
247   LoadLevel(level_nr);
248
249   SetMainBackgroundImage(IMG_BACKGROUND_MAIN);
250   ClearWindow();
251
252   DrawHeadline();
253
254   DrawText(mSX + 32, mSY + 2*32, name_text, FONT_MENU_1);
255   DrawText(mSX + 32, mSY + 3*32, "Level:", FONT_MENU_1);
256   DrawText(mSX + 32, mSY + 4*32, "Hall Of Fame", FONT_MENU_1);
257   DrawText(mSX + 32, mSY + 5*32, "Level Creator", FONT_MENU_1);
258   DrawText(mSX + 32, mSY + 6*32, "Info Screen", FONT_MENU_1);
259   DrawText(mSX + 32, mSY + 7*32, "Start Game", FONT_MENU_1);
260   DrawText(mSX + 32, mSY + 8*32, "Setup", FONT_MENU_1);
261   DrawText(mSX + 32, mSY + 9*32, "Quit", FONT_MENU_1);
262
263   DrawText(mSX + 32 + name_width, mSY + 2*32, setup.player_name, FONT_INPUT_1);
264   DrawText(mSX + level_width + 5 * 32, mSY + 3*32, int2str(level_nr,3),
265            FONT_VALUE_1);
266
267   DrawMicroLevel(MICROLEV_XPOS, MICROLEV_YPOS, TRUE);
268
269   DrawTextF(mSX + 32 + level_width - 2, mSY + 3*32 + 1, FONT_TEXT_3, "%d-%d",
270             leveldir_current->first_level, leveldir_current->last_level);
271
272   if (leveldir_current->readonly)
273   {
274     DrawTextF(mSX + level_width + 9 * 32 - 2,
275               mSY + 3 * 32 + 1 - 7, FONT_TEXT_3, "READ");
276     DrawTextF(mSX + level_width + 9 * 32 - 2,
277               mSY + 3 * 32 + 1 + 7, FONT_TEXT_3, "ONLY");
278   }
279
280   for(i=0; i<8; i++)
281     initCursor(i, (i == 1 || i == 6 ? IMG_MENU_BUTTON_RIGHT :IMG_MENU_BUTTON));
282
283   drawCursorXY(level_width/32 + 4, 1, IMG_MENU_BUTTON_LEFT);
284   drawCursorXY(level_width/32 + 8, 1, IMG_MENU_BUTTON_RIGHT);
285
286   DrawText(SX + 56, SY + 326, "A Game by Artsoft Entertainment", FONT_TITLE_2);
287
288   FadeToFront();
289   InitAnimation();
290   HandleMainMenu(0,0, 0,0, MB_MENU_INITIALIZE);
291
292   TapeStop();
293   if (TAPE_IS_EMPTY(tape))
294     LoadTape(level_nr);
295   DrawCompleteVideoDisplay();
296
297   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
298
299 #if 0
300   ClearEventQueue();
301 #endif
302 }
303
304 static void gotoTopLevelDir()
305 {
306   /* move upwards to top level directory */
307   while (leveldir_current->node_parent)
308   {
309     /* write a "path" into level tree for easy navigation to last level */
310     if (leveldir_current->node_parent->node_group->cl_first == -1)
311     {
312       int num_leveldirs = numTreeInfoInGroup(leveldir_current);
313       int leveldir_pos = posTreeInfo(leveldir_current);
314       int num_page_entries;
315       int cl_first, cl_cursor;
316
317       if (num_leveldirs <= NUM_MENU_ENTRIES_ON_SCREEN)
318         num_page_entries = num_leveldirs;
319       else
320         num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
321
322       cl_first = MAX(0, leveldir_pos - num_page_entries + 1);
323       cl_cursor = leveldir_pos - cl_first;
324
325       leveldir_current->node_parent->node_group->cl_first = cl_first;
326       leveldir_current->node_parent->node_group->cl_cursor = cl_cursor;
327     }
328
329     leveldir_current = leveldir_current->node_parent;
330   }
331 }
332
333 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
334 {
335   static int choice = 5;
336   int x = 0;
337   int y = choice;
338
339   if (button == MB_MENU_INITIALIZE)
340   {
341     drawCursor(choice, FC_RED);
342     return;
343   }
344
345   if (mx || my)         /* mouse input */
346   {
347     x = (mx - mSX) / 32;
348     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
349   }
350   else if (dx || dy)    /* keyboard input */
351   {
352     if (dx && choice == 1)
353       x = (dx < 0 ? 10 : 14);
354     else if (dy)
355       y = choice + dy;
356   }
357
358   if (y == 1 && ((x == 10 && level_nr > leveldir_current->first_level) ||
359                  (x == 14 && level_nr < leveldir_current->last_level)) &&
360       button)
361   {
362     static unsigned long level_delay = 0;
363     int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
364     int new_level_nr, old_level_nr = level_nr;
365
366     new_level_nr = level_nr + (x == 10 ? -step : +step);
367     if (new_level_nr < leveldir_current->first_level)
368       new_level_nr = leveldir_current->first_level;
369     if (new_level_nr > leveldir_current->last_level)
370       new_level_nr = leveldir_current->last_level;
371
372     if (setup.handicap && new_level_nr > leveldir_current->handicap_level)
373       new_level_nr = leveldir_current->handicap_level;
374
375     if (old_level_nr == new_level_nr ||
376         !DelayReached(&level_delay, GADGET_FRAME_DELAY))
377       goto out;
378
379     level_nr = new_level_nr;
380
381     DrawText(mSX + 11 * 32, mSY + 3 * 32, int2str(level_nr, 3), FONT_VALUE_1);
382
383     LoadLevel(level_nr);
384     DrawMicroLevel(MICROLEV_XPOS, MICROLEV_YPOS, TRUE);
385
386     TapeErase();
387     LoadTape(level_nr);
388     DrawCompleteVideoDisplay();
389
390     /* needed because DrawMicroLevel() takes some time */
391     BackToFront();
392     SyncDisplay();
393     DelayReached(&level_delay, 0);      /* reset delay counter */
394   }
395   else if (x == 0 && y >= 0 && y <= 7)
396   {
397     if (button)
398     {
399       if (y != choice)
400       {
401         drawCursor(y, FC_RED);
402         drawCursor(choice, FC_BLUE);
403         choice = y;
404       }
405     }
406     else
407     {
408       if (y == 0)
409       {
410         game_status = GAME_MODE_PSEUDO_TYPENAME;
411         HandleTypeName(strlen(setup.player_name), 0);
412       }
413       else if (y == 1)
414       {
415         if (leveldir_first)
416         {
417           game_status = GAME_MODE_LEVELS;
418           SaveLevelSetup_LastSeries();
419           SaveLevelSetup_SeriesInfo();
420
421           gotoTopLevelDir();
422
423           DrawChooseLevel();
424         }
425       }
426       else if (y == 2)
427       {
428         game_status = GAME_MODE_SCORES;
429         DrawHallOfFame(-1);
430       }
431       else if (y == 3)
432       {
433         if (leveldir_current->readonly &&
434             strcmp(setup.player_name, "Artsoft") != 0)
435           Request("This level is read only !", REQ_CONFIRM);
436         game_status = GAME_MODE_EDITOR;
437         DrawLevelEd();
438       }
439       else if (y == 4)
440       {
441         game_status = GAME_MODE_INFO;
442         DrawHelpScreen();
443       }
444       else if (y == 5)
445       {
446         if (setup.autorecord)
447           TapeStartRecording();
448
449 #if defined(PLATFORM_UNIX)
450         if (options.network)
451           SendToServer_StartPlaying();
452         else
453 #endif
454         {
455           game_status = GAME_MODE_PLAYING;
456           StopAnimation();
457           InitGame();
458         }
459       }
460       else if (y == 6)
461       {
462         game_status = GAME_MODE_SETUP;
463         setup_mode = SETUP_MODE_MAIN;
464         DrawSetupScreen();
465       }
466       else if (y == 7)
467       {
468         SaveLevelSetup_LastSeries();
469         SaveLevelSetup_SeriesInfo();
470         if (Request("Do you really want to quit ?", REQ_ASK | REQ_STAY_CLOSED))
471           game_status = GAME_MODE_QUIT;
472       }
473     }
474   }
475
476   out:
477
478   if (game_status == GAME_MODE_MAIN)
479   {
480     DrawMicroLevel(MICROLEV_XPOS, MICROLEV_YPOS, FALSE);
481     DoAnimation();
482   }
483
484   BackToFront();
485 }
486
487
488 #define MAX_HELPSCREEN_ELS      10
489 #define HA_NEXT                 -999
490 #define HA_END                  -1000
491
492 static long helpscreen_state;
493 static int helpscreen_step[MAX_HELPSCREEN_ELS];
494 static int helpscreen_frame[MAX_HELPSCREEN_ELS];
495
496 static int helpscreen_action[] =
497 {
498   IMG_PLAYER_1_MOVING_DOWN,             16,
499   IMG_PLAYER_1_MOVING_UP,               16,
500   IMG_PLAYER_1_MOVING_LEFT,             16,
501   IMG_PLAYER_1_MOVING_RIGHT,            16,
502   IMG_PLAYER_1_PUSHING_LEFT,            16,
503   IMG_PLAYER_1_PUSHING_RIGHT,           16,                     HA_NEXT,
504
505   IMG_SAND,                             -1,                     HA_NEXT,
506
507   IMG_EMPTY_SPACE,                      -1,                     HA_NEXT,
508
509   IMG_QUICKSAND_EMPTY,                  -1,                     HA_NEXT,
510
511   IMG_STEELWALL,                        -1,                     HA_NEXT,
512
513   IMG_WALL,                             -1,                     HA_NEXT,
514
515   IMG_EXPANDABLE_WALL_GROWING_LEFT,     20,
516   IMG_WALL,                             50,
517   IMG_EMPTY_SPACE,                      20,
518   IMG_EXPANDABLE_WALL_GROWING_RIGHT,    20,
519   IMG_WALL,                             50,
520   IMG_EMPTY_SPACE,                      20,
521   IMG_EXPANDABLE_WALL_GROWING_UP,       20,
522   IMG_WALL,                             50,
523   IMG_EMPTY_SPACE,                      20,
524   IMG_EXPANDABLE_WALL_GROWING_DOWN,     20,
525   IMG_WALL,                             50,
526   IMG_EMPTY_SPACE,                      20,                     HA_NEXT,
527
528   IMG_INVISIBLE_WALL,                   -1,                     HA_NEXT,
529
530   IMG_WALL_SLIPPERY,                    -1,                     HA_NEXT,
531
532   IMG_FONT_GAME_INFO,                   -1,                     HA_NEXT,
533
534   IMG_EMERALD,                          -1,                     HA_NEXT,
535
536   IMG_DIAMOND,                          -1,                     HA_NEXT,
537
538   IMG_BD_DIAMOND,                       -1,                     HA_NEXT,
539
540   IMG_EMERALD_YELLOW,                   50,
541   IMG_EMERALD_RED,                      50,
542   IMG_EMERALD_PURPLE,                   50,                     HA_NEXT,
543
544   IMG_BD_ROCK,                          -1,                     HA_NEXT,
545
546   IMG_BOMB,                             100,
547   IMG_EXPLOSION,                        16,
548   IMG_EMPTY_SPACE,                      10,                     HA_NEXT,
549
550   IMG_NUT,                              100,
551   IMG_NUT_BREAKING,                     6,
552   IMG_EMERALD,                          20,                     HA_NEXT,
553
554   IMG_WALL_EMERALD,                     100,
555   IMG_EXPLOSION,                        16,
556   IMG_EMERALD,                          20,                     HA_NEXT,
557
558   IMG_WALL_DIAMOND,                     100,
559   IMG_EXPLOSION,                        16,
560   IMG_DIAMOND,                          20,                     HA_NEXT,
561
562   IMG_WALL_BD_DIAMOND,                  100,
563   IMG_EXPLOSION,                        16,
564   IMG_BD_DIAMOND,                       20,                     HA_NEXT,
565
566   IMG_WALL_EMERALD_YELLOW,              100,
567   IMG_EXPLOSION,                        16,
568   IMG_EMERALD_YELLOW,                   20,
569   IMG_WALL_EMERALD_RED,                 100,
570   IMG_EXPLOSION,                        16,
571   IMG_EMERALD_RED,                      20,
572   IMG_WALL_EMERALD_PURPLE,              100,
573   IMG_EXPLOSION,                        16,
574   IMG_EMERALD_PURPLE,                   20,                     HA_NEXT,
575
576   IMG_ACID,                             -1,                     HA_NEXT,
577
578   IMG_KEY_1,                            50,
579   IMG_KEY_2,                            50,
580   IMG_KEY_3,                            50,
581   IMG_KEY_4,                            50,                     HA_NEXT,
582
583   IMG_GATE_1,                           50,
584   IMG_GATE_2,                           50,
585   IMG_GATE_3,                           50,
586   IMG_GATE_4,                           50,                     HA_NEXT,
587
588   IMG_GATE_1_GRAY,                      50,
589   IMG_GATE_2_GRAY,                      50,
590   IMG_GATE_3_GRAY,                      50,
591   IMG_GATE_4_GRAY,                      50,                     HA_NEXT,
592
593   IMG_DYNAMITE,                         -1,                     HA_NEXT,
594
595   IMG_DYNAMITE_ACTIVE,                  96,
596   IMG_EXPLOSION,                        16,
597   IMG_EMPTY_SPACE,                      20,                     HA_NEXT,
598
599   IMG_DYNABOMB_ACTIVE,                  100,
600   IMG_EXPLOSION,                        16,
601   IMG_EMPTY_SPACE,                      20,                     HA_NEXT,
602
603   IMG_DYNABOMB_INCREASE_NUMBER,         -1,                     HA_NEXT,
604
605   IMG_DYNABOMB_INCREASE_SIZE,           -1,                     HA_NEXT,
606
607   IMG_DYNABOMB_INCREASE_POWER,          -1,                     HA_NEXT,
608
609   IMG_SPACESHIP_RIGHT,                  16,
610   IMG_SPACESHIP_UP,                     16,
611   IMG_SPACESHIP_LEFT,                   16,
612   IMG_SPACESHIP_DOWN,                   16,                     HA_NEXT,
613
614   IMG_BUG_RIGHT,                        16,
615   IMG_BUG_UP,                           16,
616   IMG_BUG_LEFT,                         16,
617   IMG_BUG_DOWN,                         16,                     HA_NEXT,
618
619   IMG_BD_BUTTERFLY,                     -1,                     HA_NEXT,
620
621   IMG_BD_FIREFLY,                       -1,                     HA_NEXT,
622
623   IMG_PACMAN_RIGHT,                     16,
624   IMG_PACMAN_UP,                        16,
625   IMG_PACMAN_LEFT,                      16,
626   IMG_PACMAN_DOWN,                      16,                     HA_NEXT,
627
628   IMG_YAMYAM,                           -1,                     HA_NEXT,
629
630   IMG_DARK_YAMYAM,                      -1,                     HA_NEXT,
631
632   IMG_ROBOT,                            -1,                     HA_NEXT,
633
634   IMG_MOLE_MOVING_RIGHT,                16,
635   IMG_MOLE_MOVING_UP,                   16,
636   IMG_MOLE_MOVING_LEFT,                 16,
637   IMG_MOLE_MOVING_DOWN,                 16,                     HA_NEXT,
638
639   IMG_PENGUIN_MOVING_RIGHT,             16,
640   IMG_PENGUIN_MOVING_UP,                16,
641   IMG_PENGUIN_MOVING_LEFT,              16,
642   IMG_PENGUIN_MOVING_DOWN,              16,                     HA_NEXT,
643
644   IMG_PIG_MOVING_RIGHT,                 16,
645   IMG_PIG_MOVING_UP,                    16,
646   IMG_PIG_MOVING_LEFT,                  16,
647   IMG_PIG_MOVING_DOWN,                  16,                     HA_NEXT,
648
649   IMG_DRAGON_MOVING_RIGHT,              16,
650   IMG_DRAGON_MOVING_UP,                 16,
651   IMG_DRAGON_MOVING_LEFT,               16,
652   IMG_DRAGON_MOVING_DOWN,               16,                     HA_NEXT,
653
654   IMG_SATELLITE,                        -1,                     HA_NEXT,
655
656   IMG_ROBOT_WHEEL,                      50,
657   IMG_ROBOT_WHEEL_ACTIVE,               100,                    HA_NEXT,
658
659   IMG_LAMP,                             50,
660   IMG_LAMP_ACTIVE,                      50,                     HA_NEXT,
661
662   IMG_TIME_ORB_FULL,                    50,
663   IMG_TIME_ORB_EMPTY,                   50,                     HA_NEXT,
664
665   IMG_AMOEBA_DROP,                      50,
666   IMG_AMOEBA_GROWING,                   6,
667   IMG_AMOEBA_WET,                       20,                     HA_NEXT,
668
669   IMG_AMOEBA_DEAD,                      -1,                     HA_NEXT,
670
671   IMG_AMOEBA_WET,                       -1,                     HA_NEXT,
672
673   IMG_AMOEBA_WET,                       100,
674   IMG_AMOEBA_GROWING,                   6,                      HA_NEXT,
675
676   IMG_AMOEBA_FULL,                      50,
677   IMG_AMOEBA_DEAD,                      50,
678   IMG_EXPLOSION,                        16,
679   IMG_DIAMOND,                          20,                     HA_NEXT,
680
681   IMG_GAME_OF_LIFE,                     -1,                     HA_NEXT,
682
683   IMG_BIOMAZE,                          -1,                     HA_NEXT,
684
685   IMG_MAGIC_WALL_ACTIVE,                -1,                     HA_NEXT,
686
687   IMG_BD_MAGIC_WALL_ACTIVE,             -1,                     HA_NEXT,
688
689   IMG_EXIT_CLOSED,                      200,
690   IMG_EXIT_OPENING,                     16,
691   IMG_EXIT_OPEN,                        100,                    HA_NEXT,
692
693   IMG_EXIT_OPEN,                        -1,                     HA_NEXT,
694
695   IMG_SOKOBAN_OBJECT,                   -1,                     HA_NEXT,
696
697   IMG_SOKOBAN_FIELD_EMPTY,              -1,                     HA_NEXT,
698
699   IMG_SOKOBAN_FIELD_FULL,               -1,                     HA_NEXT,
700
701   IMG_SPEED_PILL,                       -1,                     HA_NEXT,
702
703   HA_END
704 };
705 static char *helpscreen_eltext[][2] =
706 {
707  {"THE HERO:",                          "(Is _this_ guy good old Rockford?)"},
708  {"Normal sand:",                       "You can dig through it"},
709  {"Empty field:",                       "You can walk through it"},
710  {"Quicksand: You cannot pass it,",     "but rocks can fall through it"},
711  {"Massive Wall:",                      "Nothing can go through it"},
712  {"Normal Wall: You can't go through",  "it, but you can bomb it away"},
713  {"Growing Wall: Grows in several di-", "rections if there is an empty field"},
714  {"Invisible Wall: Behaves like normal","wall, but is invisible"},
715  {"Old Wall: Like normal wall, but",    "some things can fall down from it"},
716  {"Letter Wall: Looks like a letter,",  "behaves like a normal wall"},
717  {"Emerald: You must collect enough of","them to finish a level"},
718  {"Diamond: Counts as 3 emeralds, but", "can be destroyed by rocks"},
719  {"Diamond (BD style): Counts like one","emerald and behaves a bit different"},
720  {"Colorful Gems:",                     "Seem to behave like Emeralds"},
721  {"Rock: Smashes several things;",      "Can be moved by the player"},
722  {"Bomb: You can move it, but be",      "careful when dropping it"},
723  {"Nut: Throw a rock on it to open it;","Each nut contains an emerald"},
724  {"Wall with an emerald inside:",       "Bomb the wall away to get it"},
725  {"Wall with a diamond inside:",        "Bomb the wall away to get it"},
726  {"Wall with BD style diamond inside:", "Bomb the wall away to get it"},
727  {"Wall with colorful gem inside:",     "Bomb the wall away to get it"},
728  {"Acid: Things that fall in are gone", "forever (including our hero)"},
729  {"Key: Opens the door that has the",   "same color (red/yellow/green/blue)"},
730  {"Door: Can be opened by the key",     "with the same color"},
731  {"Door: You have to find out the",     "right color of the key for it"},
732  {"Dynamite: Collect it and use it to", "destroy walls or kill enemies"},
733  {"Dynamite: This one explodes after",  "a few seconds"},
734  {"Dyna Bomb: Explodes in 4 directions","with variable explosion size"},
735  {"Dyna Bomb: Increases the number of", "dyna bombs available at a time"},
736  {"Dyna Bomb: Increases the size of",   "explosion of dyna bombs"},
737  {"Dyna Bomb: Increases the power of",  "explosion of dyna bombs"},
738  {"Spaceship: Moves at the left side",  "of walls; don't touch it!"},
739  {"Bug: Moves at the right side",       "of walls; don't touch it!"},
740  {"Butterfly: Moves at the right side", "of walls; don't touch it!"},
741  {"Firefly: Moves at the left side",    "of walls; don't touch it!"},
742  {"Pacman: Eats the amoeba and you,",   "if you're not careful"},
743  {"Cruncher: Eats diamonds and you,",   "if you're not careful"},
744  {"Cruncher (BD style):",               "Eats almost everything"},
745  {"Robot: Tries to kill the player",    ""},
746  {"The mole: Eats the amoeba and turns","empty space into normal sand"},
747  {"The penguin: Guide him to the exit,","but keep him away from monsters!"},
748  {"The Pig: Harmless, but eats all",    "gems it can get"},
749  {"The Dragon: Breathes fire,",         "especially to some monsters"},
750  {"Sonde: Follows you everywhere;",     "harmless, but may block your way"},
751  {"Magic Wheel: Touch it to get rid of","the robots for some seconds"},
752  {"Light Bulb: All of them must be",    "switched on to finish a level"},
753  {"Extra Time Orb: Adds some seconds",  "to the time available for the level"},
754  {"Amoeba Drop: Grows to an amoeba on", "the ground - don't touch it"},
755  {"Dead Amoeba: Does not grow, but",    "can still kill bugs and spaceships"},
756  {"Normal Amoeba: Grows through empty", "fields, sand and quicksand"},
757  {"Dropping Amoeba: This one makes",    "drops that grow to a new amoeba"},
758  {"Living Amoeba (BD style): Contains", "other element, when surrounded"},
759  {"Game Of Life: Behaves like the well","known 'Game Of Life' (2333 style)"},
760  {"Biomaze: A bit like the 'Game Of",   "Life', but builds crazy mazes"},
761  {"Magic Wall: Changes rocks, emeralds","and diamonds when they pass it"},
762  {"Magic Wall (BD style):",             "Changes rocks and BD style diamonds"},
763  {"Exit door: Opens if you have enough","emeralds to finish the level"},
764  {"Open exit door: Enter here to leave","the level and exit the actual game"},
765  {"Sokoban element: Object which must", "be pushed to an empty field"},
766  {"Sokoban element: Empty field where", "a Sokoban object can be placed on"},
767  {"Sokoban element: Field with object", "which can be pushed away"},
768  {"Speed pill: Lets the player run",    "twice as fast as normally"},
769 };
770 static int num_helpscreen_els = sizeof(helpscreen_eltext) / (2*sizeof(char *));
771
772 static char *helpscreen_music[][3] =
773 {
774   { "Alchemy",                  "Ian Boddy",            "Drive" },
775   { "The Chase",                "Propaganda",           "A Secret Wish" },
776   { "Network 23",               "Tangerine Dream",      "Exit" },
777   { "Czardasz",                 "Robert Pieculewicz",   "Czardasz" },
778   { "21st Century Common Man",  "Tangerine Dream",      "Tyger" },
779   { "Voyager",                  "The Alan Parsons Project","Pyramid" },
780   { "Twilight Painter",         "Tangerine Dream",      "Heartbreakers" }
781 };
782 static int num_helpscreen_music = 7;
783 static int helpscreen_musicpos;
784
785 #if 0
786 void OLD_DrawHelpScreenElAction(int start)
787 {
788   int i = 0, j = 0;
789   int frame, graphic;
790   int xstart = SX+16, ystart = SY+64+2*32, ystep = TILEY+4;
791
792   while(helpscreen_action[j] != HA_END)
793   {
794     if (i>=start+MAX_HELPSCREEN_ELS || i>=num_helpscreen_els)
795       break;
796     else if (i<start || helpscreen_delay[i-start])
797     {
798       if (i>=start && helpscreen_delay[i-start])
799         helpscreen_delay[i-start]--;
800
801       while(helpscreen_action[j] != HA_NEXT)
802         j++;
803       j++;
804       i++;
805       continue;
806     }
807
808     j += 3*helpscreen_step[i-start];
809     graphic = helpscreen_action[j++];
810
811     if (helpscreen_frame[i-start])
812     {
813       frame = helpscreen_action[j++] - helpscreen_frame[i-start];
814       helpscreen_frame[i-start]--;
815     }
816     else
817     {
818       frame = 0;
819       helpscreen_frame[i-start] = helpscreen_action[j++]-1;
820     }
821
822     helpscreen_delay[i-start] = helpscreen_action[j++] - 1;
823
824     if (helpscreen_action[j] == HA_NEXT)
825     {
826       if (!helpscreen_frame[i-start])
827         helpscreen_step[i-start] = 0;
828     }
829     else
830     {
831       if (!helpscreen_frame[i-start])
832         helpscreen_step[i-start]++;
833       while(helpscreen_action[j] != HA_NEXT)
834         j++;
835     }
836     j++;
837
838     DrawOldGraphicExt(drawto, xstart, ystart+(i-start)*ystep, graphic+frame);
839     i++;
840   }
841
842   for(i=2;i<16;i++)
843   {
844     MarkTileDirty(0,i);
845     MarkTileDirty(1,i);
846   }
847 }
848 #endif
849
850 void DrawHelpScreenElAction(int start)
851 {
852   int i = 0, j = 0;
853   int xstart = mSX + 16;
854   int ystart = mSY + 64 + 2 * 32;
855   int ystep = TILEY + 4;
856   int graphic;
857   int frame_count;
858   int sync_frame;
859
860   while (helpscreen_action[j] != HA_END)
861   {
862     if (i >= start + MAX_HELPSCREEN_ELS || i >= num_helpscreen_els)
863       break;
864     else if (i < start)
865     {
866       while (helpscreen_action[j] != HA_NEXT)
867         j++;
868
869       j++;
870       i++;
871
872       continue;
873     }
874
875     j += 2 * helpscreen_step[i-start];
876     graphic = helpscreen_action[j++];
877     frame_count = helpscreen_action[j++];
878     if (frame_count == -1)
879       frame_count = 1000000;
880
881     if (helpscreen_frame[i-start] == 0)
882     {
883       sync_frame = 0;
884       helpscreen_frame[i-start] = frame_count - 1;
885     }
886     else
887     {
888       sync_frame = frame_count - helpscreen_frame[i-start];
889       helpscreen_frame[i-start]--;
890     }
891
892     if (helpscreen_action[j] == HA_NEXT)
893     {
894       if (!helpscreen_frame[i-start])
895         helpscreen_step[i-start] = 0;
896     }
897     else
898     {
899       if (!helpscreen_frame[i-start])
900         helpscreen_step[i-start]++;
901       while(helpscreen_action[j] != HA_NEXT)
902         j++;
903     }
904     j++;
905
906 #if 1
907     ClearRectangleOnBackground(drawto, xstart, ystart + (i - start) * ystep,
908                                TILEX, TILEY);
909     DrawGraphicAnimationExt(drawto, xstart, ystart + (i - start) * ystep,
910                             graphic, sync_frame, USE_MASKING);
911 #else
912     frame = getGraphicAnimationFrame(graphic, sync_frame);
913
914     DrawGraphicExt(drawto, xstart, ystart + (i-start) * ystep,
915                    graphic, frame);
916 #endif
917
918     i++;
919   }
920
921 #if 1
922   redraw_mask |= REDRAW_FIELD;
923 #else
924   for(i=2; i<16; i++)
925   {
926     MarkTileDirty(0, i);
927     MarkTileDirty(1, i);
928   }
929 #endif
930
931   FrameCounter++;
932 }
933
934 void DrawHelpScreenElText(int start)
935 {
936   int i;
937   int xstart = mSX + 56, ystart = mSY + 65 + 2 * 32, ystep = TILEY + 4;
938   int ybottom = SYSIZE - 20;
939
940   SetMainBackgroundImage(IMG_BACKGROUND_INFO);
941   ClearWindow();
942   DrawHeadline();
943
944   DrawTextFCentered(100, FONT_TEXT_1, "The game elements:");
945
946   for(i=start; i < start + MAX_HELPSCREEN_ELS && i < num_helpscreen_els; i++)
947   {
948     DrawText(xstart,
949              ystart + (i - start) * ystep + (*helpscreen_eltext[i][1] ? 0 : 8),
950              helpscreen_eltext[i][0], FONT_TEXT_2);
951     DrawText(xstart, ystart + (i - start) * ystep + 16,
952              helpscreen_eltext[i][1], FONT_TEXT_2);
953   }
954
955   DrawTextFCentered(ybottom, FONT_TEXT_4,
956                     "Press any key or button for next page");
957 }
958
959 void DrawHelpScreenMusicText(int num)
960 {
961   int ystart = 150, ystep = 30;
962   int ybottom = SYSIZE - 20;
963
964   FadeSoundsAndMusic();
965   ClearWindow();
966   DrawHeadline();
967
968   DrawTextFCentered(100, FONT_TEXT_1, "The game background music loops:");
969
970   DrawTextFCentered(ystart + 0 * ystep, FONT_TEXT_2, "Excerpt from");
971   DrawTextFCentered(ystart + 1 * ystep, FONT_TEXT_3,
972                     "\"%s\"", helpscreen_music[num][0]);
973   DrawTextFCentered(ystart + 2 * ystep, FONT_TEXT_2, "by");
974   DrawTextFCentered(ystart + 3 * ystep, FONT_TEXT_3,
975                     "%s", helpscreen_music[num][1]);
976   DrawTextFCentered(ystart + 4 * ystep, FONT_TEXT_2, "from the album");
977   DrawTextFCentered(ystart + 5 * ystep, FONT_TEXT_3,
978                     "\"%s\"", helpscreen_music[num][2]);
979
980   DrawTextFCentered(ybottom, FONT_TEXT_4,
981                     "Press any key or button for next page");
982
983 #if 0
984   PlaySoundLoop(background_loop[num]);
985 #endif
986 }
987
988 void DrawHelpScreenCreditsText()
989 {
990   int ystart = 150, ystep = 30;
991   int ybottom = SYSIZE - 20;
992
993   FadeSoundsAndMusic();
994   ClearWindow();
995   DrawHeadline();
996
997   DrawTextFCentered(100, FONT_TEXT_1, "Credits:");
998   DrawTextFCentered(ystart + 0 * ystep, FONT_TEXT_2, "DOS port of the game:");
999   DrawTextFCentered(ystart + 1 * ystep, FONT_TEXT_3, "Guido Schulz");
1000   DrawTextFCentered(ystart + 2 * ystep, FONT_TEXT_2, "Additional toons:");
1001   DrawTextFCentered(ystart + 3 * ystep, FONT_TEXT_3, "Karl Hörnell");
1002   DrawTextFCentered(ystart + 5 * ystep, FONT_TEXT_2,
1003                     "...and many thanks to all contributors");
1004   DrawTextFCentered(ystart + 6 * ystep, FONT_TEXT_2, "of new levels!");
1005
1006   DrawTextFCentered(ybottom, FONT_TEXT_4,
1007                     "Press any key or button for next page");
1008 }
1009
1010 void DrawHelpScreenContactText()
1011 {
1012   int ystart = 150, ystep = 30;
1013   int ybottom = SYSIZE - 20;
1014
1015   ClearWindow();
1016   DrawHeadline();
1017
1018   DrawTextFCentered(100, FONT_TEXT_1, "Program information:");
1019
1020   DrawTextFCentered(ystart + 0 * ystep, FONT_TEXT_2,
1021                     "This game is Freeware!");
1022   DrawTextFCentered(ystart + 1 * ystep, FONT_TEXT_2,
1023                     "If you like it, send e-mail to:");
1024   DrawTextFCentered(ystart + 2 * ystep, FONT_TEXT_3,
1025                     "info@artsoft.org");
1026   DrawTextFCentered(ystart + 3 * ystep, FONT_TEXT_2,
1027                     "or SnailMail to:");
1028   DrawTextFCentered(ystart + 4 * ystep + 0, FONT_TEXT_3,
1029                     "Holger Schemel");
1030   DrawTextFCentered(ystart + 4 * ystep + 20, FONT_TEXT_3,
1031                     "Detmolder Strasse 189");
1032   DrawTextFCentered(ystart + 4 * ystep + 40, FONT_TEXT_3,
1033                     "33604 Bielefeld");
1034   DrawTextFCentered(ystart + 4 * ystep + 60, FONT_TEXT_3,
1035                     "Germany");
1036
1037   DrawTextFCentered(ystart + 7 * ystep, FONT_TEXT_2,
1038                     "If you have created new levels,");
1039   DrawTextFCentered(ystart + 8 * ystep, FONT_TEXT_2,
1040                     "send them to me to include them!");
1041   DrawTextFCentered(ystart + 9 * ystep, FONT_TEXT_2,
1042                     ":-)");
1043
1044   DrawTextFCentered(ybottom, FONT_TEXT_4,
1045                     "Press any key or button for main menu");
1046 }
1047
1048 void DrawHelpScreen()
1049 {
1050   int i;
1051
1052   UnmapAllGadgets();
1053   CloseDoor(DOOR_CLOSE_2);
1054
1055   for(i=0;i<MAX_HELPSCREEN_ELS;i++)
1056     helpscreen_step[i] = helpscreen_frame[i] = 0;
1057   helpscreen_musicpos = 0;
1058   helpscreen_state = 0;
1059
1060   DrawHelpScreenElText(0);
1061   DrawHelpScreenElAction(0);
1062
1063   FadeToFront();
1064   InitAnimation();
1065
1066 #if 1
1067   PlaySound(SND_BACKGROUND_INFO);
1068   PlayMusic(MUS_BACKGROUND_INFO);
1069 #else
1070 #if 0
1071   PlaySoundLoop(SND_BACKGROUND_INFO);
1072 #else
1073   PlaySound_Menu_Start(SND_BACKGROUND_INFO);
1074 #endif
1075 #endif
1076 }
1077
1078 void HandleHelpScreen(int button)
1079 {
1080   static unsigned long hs_delay = 0;
1081   int num_helpscreen_els_pages =
1082     (num_helpscreen_els + MAX_HELPSCREEN_ELS-1) / MAX_HELPSCREEN_ELS;
1083   int button_released = !button;
1084   int i;
1085
1086   if (button_released)
1087   {
1088     if (helpscreen_state < num_helpscreen_els_pages - 1)
1089     {
1090       for(i=0;i<MAX_HELPSCREEN_ELS;i++)
1091         helpscreen_step[i] = helpscreen_frame[i] = 0;
1092       helpscreen_state++;
1093
1094       FrameCounter = 0;
1095       DrawHelpScreenElText(helpscreen_state * MAX_HELPSCREEN_ELS);
1096       DrawHelpScreenElAction(helpscreen_state * MAX_HELPSCREEN_ELS);
1097     }
1098     else if (helpscreen_state <
1099              num_helpscreen_els_pages + num_helpscreen_music - 1)
1100     {
1101       helpscreen_state++;
1102       DrawHelpScreenMusicText(helpscreen_state - num_helpscreen_els_pages);
1103     }
1104     else if (helpscreen_state ==
1105              num_helpscreen_els_pages + num_helpscreen_music - 1)
1106     {
1107       helpscreen_state++;
1108       DrawHelpScreenCreditsText();
1109     }
1110     else if (helpscreen_state ==
1111              num_helpscreen_els_pages + num_helpscreen_music)
1112     {
1113       helpscreen_state++;
1114       DrawHelpScreenContactText();
1115     }
1116     else
1117     {
1118       FadeSoundsAndMusic();
1119
1120       game_status = GAME_MODE_MAIN;
1121       DrawMainMenu();
1122     }
1123   }
1124   else
1125   {
1126     if (DelayReached(&hs_delay, GAME_FRAME_DELAY))
1127     {
1128       if (helpscreen_state < num_helpscreen_els_pages)
1129         DrawHelpScreenElAction(helpscreen_state * MAX_HELPSCREEN_ELS);
1130     }
1131
1132     /* !!! workaround for playing "music" that is really a sound loop (and
1133        must therefore periodically be reactivated with the current sound
1134        engine !!! */
1135 #if 0
1136 #if 0
1137     PlaySoundLoop(SND_BACKGROUND_INFO);
1138 #else
1139     PlaySound_Menu_Continue(SND_BACKGROUND_INFO);
1140 #endif
1141 #endif
1142   }
1143
1144   DoAnimation();
1145   BackToFront();
1146 }
1147
1148 void HandleTypeName(int newxpos, Key key)
1149 {
1150   static int xpos = 0, ypos = 2;
1151   int font_width = getFontWidth(FONT_INPUT_1_ACTIVE);
1152   int name_width = getFontWidth(FONT_MENU_1) * strlen("Name:");
1153   int startx = mSX + 32 + name_width;
1154   int starty = mSY + ypos * 32;
1155
1156   if (newxpos)
1157   {
1158     xpos = newxpos;
1159
1160     DrawText(startx, starty, setup.player_name, FONT_INPUT_1_ACTIVE);
1161     DrawText(startx + xpos * font_width, starty, "_", FONT_INPUT_1_ACTIVE);
1162
1163     return;
1164   }
1165
1166   if (((key >= KSYM_A && key <= KSYM_Z) ||
1167        (key >= KSYM_a && key <= KSYM_z)) && 
1168       xpos < MAX_PLAYER_NAME_LEN)
1169   {
1170     char ascii;
1171
1172     if (key >= KSYM_A && key <= KSYM_Z)
1173       ascii = 'A' + (char)(key - KSYM_A);
1174     else
1175       ascii = 'a' + (char)(key - KSYM_a);
1176
1177     setup.player_name[xpos] = ascii;
1178     setup.player_name[xpos + 1] = 0;
1179     xpos++;
1180
1181     DrawText(startx, starty, setup.player_name, FONT_INPUT_1_ACTIVE);
1182     DrawText(startx + xpos * font_width, starty, "_", FONT_INPUT_1_ACTIVE);
1183   }
1184   else if ((key == KSYM_Delete || key == KSYM_BackSpace) && xpos > 0)
1185   {
1186     xpos--;
1187     setup.player_name[xpos] = 0;
1188
1189     DrawText(startx + xpos * font_width, starty, "_ ", FONT_INPUT_1_ACTIVE);
1190   }
1191   else if (key == KSYM_Return && xpos > 0)
1192   {
1193     DrawText(startx, starty, setup.player_name, FONT_INPUT_1);
1194     DrawText(startx + xpos * font_width, starty, " ", FONT_INPUT_1_ACTIVE);
1195
1196     SaveSetup();
1197     game_status = GAME_MODE_MAIN;
1198   }
1199
1200   BackToFront();
1201 }
1202
1203 static void DrawChooseTree(TreeInfo **ti_ptr)
1204 {
1205   UnmapAllGadgets();
1206
1207   FreeScreenGadgets();
1208   CreateScreenGadgets();
1209
1210   CloseDoor(DOOR_CLOSE_2);
1211
1212   ClearWindow();
1213
1214   HandleChooseTree(0,0, 0,0, MB_MENU_INITIALIZE, ti_ptr);
1215   MapChooseTreeGadgets(*ti_ptr);
1216
1217   FadeToFront();
1218   InitAnimation();
1219 }
1220
1221 static void AdjustChooseTreeScrollbar(int id, int first_entry, TreeInfo *ti)
1222 {
1223   struct GadgetInfo *gi = screen_gadget[id];
1224   int items_max, items_visible, item_position;
1225
1226   items_max = numTreeInfoInGroup(ti);
1227   items_visible = NUM_MENU_ENTRIES_ON_SCREEN;
1228   item_position = first_entry;
1229
1230   if (item_position > items_max - items_visible)
1231     item_position = items_max - items_visible;
1232
1233   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
1234                GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
1235                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
1236 }
1237
1238 static void drawChooseTreeList(int first_entry, int num_page_entries,
1239                                TreeInfo *ti)
1240 {
1241   int i;
1242   char buffer[SCR_FIELDX * 2];
1243   int max_buffer_len = (SCR_FIELDX - 2) * 2;
1244   char *title_string = NULL;
1245   int xoffset_setup = 16;
1246   int yoffset_setup = 16;
1247   int xoffset = (ti->type == TREE_TYPE_LEVEL_DIR ? 0 : xoffset_setup);
1248   int yoffset = (ti->type == TREE_TYPE_LEVEL_DIR ? 0 : yoffset_setup);
1249   int last_game_status = game_status;   /* save current game status */
1250
1251   title_string =
1252     (ti->type == TREE_TYPE_LEVEL_DIR ? "Level Directories" :
1253      ti->type == TREE_TYPE_GRAPHICS_DIR ? "Custom Graphics" :
1254      ti->type == TREE_TYPE_SOUNDS_DIR ? "Custom Sounds" :
1255      ti->type == TREE_TYPE_MUSIC_DIR ? "Custom Music" : "");
1256
1257   DrawText(SX + xoffset, SY + yoffset, title_string, FONT_TITLE_1);
1258
1259   /* force LEVELS font on artwork setup screen */
1260   game_status = GAME_MODE_LEVELS;
1261
1262   /* clear tree list area, but not title or scrollbar */
1263   DrawBackground(mSX, mSY + MENU_SCREEN_START_YPOS * 32,
1264                  SXSIZE - 32 + menu.scrollbar_xoffset,
1265                  MAX_MENU_ENTRIES_ON_SCREEN * 32);
1266
1267   for(i=0; i<num_page_entries; i++)
1268   {
1269     TreeInfo *node, *node_first;
1270     int entry_pos = first_entry + i;
1271     int ypos = MENU_SCREEN_START_YPOS + i;
1272
1273     node_first = getTreeInfoFirstGroupEntry(ti);
1274     node = getTreeInfoFromPos(node_first, entry_pos);
1275
1276     strncpy(buffer, node->name , max_buffer_len);
1277     buffer[max_buffer_len] = '\0';
1278
1279     DrawText(mSX + 32, mSY + ypos * 32, buffer, FONT_TEXT_1 + node->color);
1280
1281     if (node->parent_link)
1282       initCursor(i, IMG_MENU_BUTTON_LEFT);
1283     else if (node->level_group)
1284       initCursor(i, IMG_MENU_BUTTON_RIGHT);
1285     else
1286       initCursor(i, IMG_MENU_BUTTON);
1287   }
1288
1289   game_status = last_game_status;       /* restore current game status */
1290
1291   redraw_mask |= REDRAW_FIELD;
1292 }
1293
1294 static void drawChooseTreeInfo(int entry_pos, TreeInfo *ti)
1295 {
1296   TreeInfo *node, *node_first;
1297   int x, last_redraw_mask = redraw_mask;
1298
1299   if (ti->type != TREE_TYPE_LEVEL_DIR)
1300     return;
1301
1302   node_first = getTreeInfoFirstGroupEntry(ti);
1303   node = getTreeInfoFromPos(node_first, entry_pos);
1304
1305   DrawBackground(SX + 32, SY + 32, SXSIZE - 64, 32);
1306
1307   if (node->parent_link)
1308     DrawTextFCentered(40, FONT_TITLE_2, "leave group \"%s\"",
1309                       node->class_desc);
1310   else if (node->level_group)
1311     DrawTextFCentered(40, FONT_TITLE_2, "enter group \"%s\"",
1312                       node->class_desc);
1313   else if (ti->type == TREE_TYPE_LEVEL_DIR)
1314     DrawTextFCentered(40, FONT_TITLE_2, "%3d levels (%s)",
1315                       node->levels, node->class_desc);
1316
1317   /* let BackToFront() redraw only what is needed */
1318   redraw_mask = last_redraw_mask | REDRAW_TILES;
1319   for (x=0; x<SCR_FIELDX; x++)
1320     MarkTileDirty(x, 1);
1321 }
1322
1323 static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
1324                              TreeInfo **ti_ptr)
1325 {
1326   TreeInfo *ti = *ti_ptr;
1327   int x = 0;
1328   int y = ti->cl_cursor;
1329   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
1330   int num_entries = numTreeInfoInGroup(ti);
1331   int num_page_entries;
1332   int last_game_status = game_status;   /* save current game status */
1333
1334   /* force LEVELS draw offset on choose level and artwork setup screen */
1335   game_status = GAME_MODE_LEVELS;
1336
1337   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
1338     num_page_entries = num_entries;
1339   else
1340     num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
1341
1342   game_status = last_game_status;       /* restore current game status */
1343
1344   if (button == MB_MENU_INITIALIZE)
1345   {
1346     int num_entries = numTreeInfoInGroup(ti);
1347     int entry_pos = posTreeInfo(ti);
1348
1349     if (ti->cl_first == -1)
1350     {
1351       /* only on initialization */
1352       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
1353       ti->cl_cursor = entry_pos - ti->cl_first;
1354     }
1355     else if (ti->cl_cursor >= num_page_entries ||
1356              (num_entries > num_page_entries &&
1357               num_entries - ti->cl_first < num_page_entries))
1358     {
1359       /* only after change of list size (by custom graphic configuration) */
1360       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
1361       ti->cl_cursor = entry_pos - ti->cl_first;
1362     }
1363
1364     if (dx == 999)      /* first entry is set by scrollbar position */
1365       ti->cl_first = dy;
1366     else
1367       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1368                                 ti->cl_first, ti);
1369
1370     drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1371     drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1372     drawChooseTreeCursor(ti->cl_cursor, FC_RED);
1373
1374     return;
1375   }
1376   else if (button == MB_MENU_LEAVE)
1377   {
1378     if (ti->node_parent)
1379     {
1380       *ti_ptr = ti->node_parent;
1381       DrawChooseTree(ti_ptr);
1382     }
1383     else if (game_status == GAME_MODE_SETUP)
1384     {
1385       execSetupArtwork();
1386     }
1387     else
1388     {
1389       game_status = GAME_MODE_MAIN;
1390       DrawMainMenu();
1391     }
1392
1393     return;
1394   }
1395
1396   if (mx || my)         /* mouse input */
1397   {
1398     int last_game_status = game_status; /* save current game status */
1399
1400     /* force LEVELS draw offset on artwork setup screen */
1401     game_status = GAME_MODE_LEVELS;
1402
1403     x = (mx - mSX) / 32;
1404     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
1405
1406     game_status = last_game_status;     /* restore current game status */
1407   }
1408   else if (dx || dy)    /* keyboard or scrollbar/scrollbutton input */
1409   {
1410     /* move cursor instead of scrolling when already at start/end of list */
1411     if (dy == -1 * SCROLL_LINE && ti->cl_first == 0)
1412       dy = -1;
1413     else if (dy == +1 * SCROLL_LINE &&
1414              ti->cl_first + num_page_entries == num_entries)
1415       dy = 1;
1416
1417     /* handle scrolling screen one line or page */
1418     if (ti->cl_cursor + dy < 0 ||
1419         ti->cl_cursor + dy > num_page_entries - 1)
1420     {
1421       if (ABS(dy) == SCROLL_PAGE)
1422         step = num_page_entries - 1;
1423
1424       if (dy < 0 && ti->cl_first > 0)
1425       {
1426         /* scroll page/line up */
1427
1428         ti->cl_first -= step;
1429         if (ti->cl_first < 0)
1430           ti->cl_first = 0;
1431
1432         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1433         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1434         drawChooseTreeCursor(ti->cl_cursor, FC_RED);
1435         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1436                                   ti->cl_first, ti);
1437       }
1438       else if (dy > 0 && ti->cl_first + num_page_entries < num_entries)
1439       {
1440         /* scroll page/line down */
1441
1442         ti->cl_first += step;
1443         if (ti->cl_first + num_page_entries > num_entries)
1444           ti->cl_first = MAX(0, num_entries - num_page_entries);
1445
1446         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
1447         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
1448         drawChooseTreeCursor(ti->cl_cursor, FC_RED);
1449         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
1450                                   ti->cl_first, ti);
1451       }
1452
1453       return;
1454     }
1455
1456     /* handle moving cursor one line */
1457     y = ti->cl_cursor + dy;
1458   }
1459
1460   if (dx == 1)
1461   {
1462     TreeInfo *node_first, *node_cursor;
1463     int entry_pos = ti->cl_first + y;
1464
1465     node_first = getTreeInfoFirstGroupEntry(ti);
1466     node_cursor = getTreeInfoFromPos(node_first, entry_pos);
1467
1468     if (node_cursor->node_group)
1469     {
1470       node_cursor->cl_first = ti->cl_first;
1471       node_cursor->cl_cursor = ti->cl_cursor;
1472       *ti_ptr = node_cursor->node_group;
1473       DrawChooseTree(ti_ptr);
1474
1475       return;
1476     }
1477   }
1478   else if (dx == -1 && ti->node_parent)
1479   {
1480     *ti_ptr = ti->node_parent;
1481     DrawChooseTree(ti_ptr);
1482
1483     return;
1484   }
1485
1486   if (x == 0 && y >= 0 && y < num_page_entries)
1487   {
1488     if (button)
1489     {
1490       if (y != ti->cl_cursor)
1491       {
1492         drawChooseTreeCursor(y, FC_RED);
1493         drawChooseTreeCursor(ti->cl_cursor, FC_BLUE);
1494         drawChooseTreeInfo(ti->cl_first + y, ti);
1495         ti->cl_cursor = y;
1496       }
1497     }
1498     else
1499     {
1500       TreeInfo *node_first, *node_cursor;
1501       int entry_pos = ti->cl_first + y;
1502
1503       node_first = getTreeInfoFirstGroupEntry(ti);
1504       node_cursor = getTreeInfoFromPos(node_first, entry_pos);
1505
1506       if (node_cursor->node_group)
1507       {
1508         node_cursor->cl_first = ti->cl_first;
1509         node_cursor->cl_cursor = ti->cl_cursor;
1510         *ti_ptr = node_cursor->node_group;
1511         DrawChooseTree(ti_ptr);
1512       }
1513       else if (node_cursor->parent_link)
1514       {
1515         *ti_ptr = node_cursor->node_parent;
1516         DrawChooseTree(ti_ptr);
1517       }
1518       else
1519       {
1520         node_cursor->cl_first = ti->cl_first;
1521         node_cursor->cl_cursor = ti->cl_cursor;
1522         *ti_ptr = node_cursor;
1523
1524         if (ti->type == TREE_TYPE_LEVEL_DIR)
1525         {
1526           LoadLevelSetup_SeriesInfo();
1527
1528           SaveLevelSetup_LastSeries();
1529           SaveLevelSetup_SeriesInfo();
1530           TapeErase();
1531         }
1532
1533         if (game_status == GAME_MODE_SETUP)
1534         {
1535           execSetupArtwork();
1536         }
1537         else
1538         {
1539           game_status = GAME_MODE_MAIN;
1540           DrawMainMenu();
1541         }
1542       }
1543     }
1544   }
1545
1546 #if 0
1547   if (game_status == GAME_MODE_LEVELS || game_status == GAME_MODE_SETUP)
1548     DoAnimation();
1549
1550   BackToFront();
1551 #endif
1552 }
1553
1554 void DrawChooseLevel()
1555 {
1556   SetMainBackgroundImage(IMG_BACKGROUND_LEVELS);
1557
1558   DrawChooseTree(&leveldir_current);
1559 }
1560
1561 void HandleChooseLevel(int mx, int my, int dx, int dy, int button)
1562 {
1563   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
1564
1565   DoAnimation();
1566   BackToFront();
1567 }
1568
1569 void DrawHallOfFame(int highlight_position)
1570 {
1571   UnmapAllGadgets();
1572   FadeSoundsAndMusic();
1573   CloseDoor(DOOR_CLOSE_2);
1574
1575   if (highlight_position < 0) 
1576     LoadScore(level_nr);
1577
1578   FadeToFront();
1579   InitAnimation();
1580
1581   HandleHallOfFame(highlight_position,0, 0,0, MB_MENU_INITIALIZE);
1582
1583 #if 1
1584   PlaySound(SND_BACKGROUND_SCORES);
1585   PlayMusic(MUS_BACKGROUND_SCORES);
1586 #else
1587 #if 0
1588   PlaySound(SND_BACKGROUND_SCORES);
1589 #else
1590   PlaySound_Menu_Start(SND_BACKGROUND_SCORES);
1591 #endif
1592 #endif
1593 }
1594
1595 static void drawHallOfFameList(int first_entry, int highlight_position)
1596 {
1597   int i;
1598
1599   SetMainBackgroundImage(IMG_BACKGROUND_SCORES);
1600   ClearWindow();
1601
1602   DrawText(mSX + 80, mSY + 8, "Hall Of Fame", FONT_TITLE_1);
1603   DrawTextFCentered(46, FONT_TITLE_2, "HighScores of Level %d", level_nr);
1604
1605   for(i=0; i<NUM_MENU_ENTRIES_ON_SCREEN; i++)
1606   {
1607     int entry = first_entry + i;
1608     boolean active = (entry == highlight_position);
1609     int font_nr1 = (active ? FONT_TEXT_1_ACTIVE : FONT_TEXT_1);
1610     int font_nr2 = (active ? FONT_TEXT_2_ACTIVE : FONT_TEXT_2);
1611     int font_nr3 = (active ? FONT_TEXT_3_ACTIVE : FONT_TEXT_3);
1612     int font_nr4 = (active ? FONT_TEXT_4_ACTIVE : FONT_TEXT_4);
1613     int dx1 = 3 * getFontWidth(font_nr1);
1614     int dx2 = dx1 + getFontWidth(font_nr1);
1615     int dx3 = dx2 + 25 * getFontWidth(font_nr3);
1616     int sy = mSY + 64 + i * 32;
1617
1618     DrawText(mSX, sy, int2str(entry + 1, 3), font_nr1);
1619     DrawText(mSX + dx1, sy, ".", font_nr1);
1620     DrawText(mSX + dx2, sy, ".........................", font_nr3);
1621     if (strcmp(highscore[entry].Name, EMPTY_PLAYER_NAME) != 0)
1622       DrawText(mSX + dx2, sy, highscore[entry].Name, font_nr2);
1623     DrawText(mSX + dx3, sy, int2str(highscore[entry].Score, 5), font_nr4);
1624   }
1625
1626   redraw_mask |= REDRAW_FIELD;
1627 }
1628
1629 void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
1630 {
1631   static int first_entry = 0;
1632   static int highlight_position = 0;
1633   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
1634   int button_released = !button;
1635
1636   if (button == MB_MENU_INITIALIZE)
1637   {
1638     first_entry = 0;
1639     highlight_position = mx;
1640     drawHallOfFameList(first_entry, highlight_position);
1641
1642     return;
1643   }
1644
1645   if (ABS(dy) == SCROLL_PAGE)           /* handle scrolling one page */
1646     step = NUM_MENU_ENTRIES_ON_SCREEN - 1;
1647
1648   if (dy < 0)
1649   {
1650     if (first_entry > 0)
1651     {
1652       first_entry -= step;
1653       if (first_entry < 0)
1654         first_entry = 0;
1655
1656       drawHallOfFameList(first_entry, highlight_position);
1657     }
1658   }
1659   else if (dy > 0)
1660   {
1661     if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
1662     {
1663       first_entry += step;
1664       if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
1665         first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
1666
1667       drawHallOfFameList(first_entry, highlight_position);
1668     }
1669   }
1670   else if (button_released)
1671   {
1672     FadeSound(SND_BACKGROUND_SCORES);
1673     game_status = GAME_MODE_MAIN;
1674     DrawMainMenu();
1675   }
1676
1677 #if 0
1678 #if 1
1679   if (game_status == GAME_MODE_SCORES)
1680     PlaySound_Menu_Continue(SND_BACKGROUND_SCORES);
1681 #endif
1682 #endif
1683
1684   DoAnimation();
1685   BackToFront();
1686 }
1687
1688
1689 /* ========================================================================= */
1690 /* setup screen functions                                                    */
1691 /* ========================================================================= */
1692
1693 static struct TokenInfo *setup_info;
1694 static int num_setup_info;
1695
1696 static char *graphics_set_name;
1697 static char *sounds_set_name;
1698 static char *music_set_name;
1699
1700 static void execSetupMain()
1701 {
1702   setup_mode = SETUP_MODE_MAIN;
1703   DrawSetupScreen();
1704 }
1705
1706 static void execSetupGame()
1707 {
1708   setup_mode = SETUP_MODE_GAME;
1709   DrawSetupScreen();
1710 }
1711
1712 static void execSetupEditor()
1713 {
1714   setup_mode = SETUP_MODE_EDITOR;
1715   DrawSetupScreen();
1716 }
1717
1718 static void execSetupGraphics()
1719 {
1720   setup_mode = SETUP_MODE_GRAPHICS;
1721   DrawSetupScreen();
1722 }
1723
1724 static void execSetupSound()
1725 {
1726   setup_mode = SETUP_MODE_SOUND;
1727   DrawSetupScreen();
1728 }
1729
1730 static void execSetupArtwork()
1731 {
1732   setup.graphics_set = artwork.gfx_current->identifier;
1733   setup.sounds_set = artwork.snd_current->identifier;
1734   setup.music_set = artwork.mus_current->identifier;
1735
1736   /* needed if last screen (setup choice) changed graphics, sounds or music */
1737   ReloadCustomArtwork();
1738
1739   /* needed for displaying artwork name instead of artwork identifier */
1740   graphics_set_name = artwork.gfx_current->name;
1741   sounds_set_name = artwork.snd_current->name;
1742   music_set_name = artwork.mus_current->name;
1743
1744   setup_mode = SETUP_MODE_ARTWORK;
1745   DrawSetupScreen();
1746 }
1747
1748 static void execSetupChooseGraphics()
1749 {
1750   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
1751   DrawSetupScreen();
1752 }
1753
1754 static void execSetupChooseSounds()
1755 {
1756   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
1757   DrawSetupScreen();
1758 }
1759
1760 static void execSetupChooseMusic()
1761 {
1762   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
1763   DrawSetupScreen();
1764 }
1765
1766 static void execSetupInput()
1767 {
1768   setup_mode = SETUP_MODE_INPUT;
1769   DrawSetupScreen();
1770 }
1771
1772 static void execSetupShortcut()
1773 {
1774   setup_mode = SETUP_MODE_SHORTCUT;
1775   DrawSetupScreen();
1776 }
1777
1778 static void execExitSetup()
1779 {
1780   game_status = GAME_MODE_MAIN;
1781   DrawMainMenu();
1782 }
1783
1784 static void execSaveAndExitSetup()
1785 {
1786   SaveSetup();
1787   execExitSetup();
1788 }
1789
1790 static struct TokenInfo setup_info_main[] =
1791 {
1792   { TYPE_ENTER_MENU,    execSetupGame,          "Game Settings"         },
1793   { TYPE_ENTER_MENU,    execSetupEditor,        "Editor Settings"       },
1794   { TYPE_ENTER_MENU,    execSetupGraphics,      "Graphics"              },
1795   { TYPE_ENTER_MENU,    execSetupSound,         "Sound & Music"         },
1796   { TYPE_ENTER_MENU,    execSetupArtwork,       "Custom Artwork"        },
1797   { TYPE_ENTER_MENU,    execSetupInput,         "Input Devices"         },
1798   { TYPE_ENTER_MENU,    execSetupShortcut,      "Key Shortcuts"         },
1799   { TYPE_EMPTY,         NULL,                   ""                      },
1800   { TYPE_LEAVE_MENU,    execExitSetup,          "Exit"                  },
1801   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   "Save and Exit"         },
1802   { 0,                  NULL,                   NULL                    }
1803 };
1804
1805 static struct TokenInfo setup_info_game[] =
1806 {
1807   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode:"            },
1808   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
1809   { TYPE_SWITCH,        &setup.time_limit,      "Timelimit:"            },
1810   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record:"          },
1811   { TYPE_EMPTY,         NULL,                   ""                      },
1812   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1813   { 0,                  NULL,                   NULL                    }
1814 };
1815
1816 static struct TokenInfo setup_info_editor[] =
1817 {
1818   { TYPE_STRING,        NULL,                   "Offer Special Elements:"},
1819   { TYPE_SWITCH,        &setup.editor.el_boulderdash,   "BoulderDash:"  },
1820   { TYPE_SWITCH,        &setup.editor.el_emerald_mine,  "Emerald Mine:" },
1821   { TYPE_SWITCH,        &setup.editor.el_more,          "More:"         },
1822   { TYPE_SWITCH,        &setup.editor.el_sokoban,       "Sokoban:"      },
1823   { TYPE_SWITCH,        &setup.editor.el_supaplex,      "Supaplex:"     },
1824   { TYPE_SWITCH,        &setup.editor.el_diamond_caves, "Diamd. Caves:" },
1825   { TYPE_SWITCH,        &setup.editor.el_dx_boulderdash,"DX Boulderd.:" },
1826   { TYPE_SWITCH,        &setup.editor.el_chars,         "Characters:"   },
1827   { TYPE_SWITCH,        &setup.editor.el_custom,        "Custom:"       },
1828   { TYPE_SWITCH,        &setup.editor.el_custom_more,   "More Custom:"  },
1829   { TYPE_SWITCH,        &setup.editor.el_headlines,     "Headlines:"    },
1830   { TYPE_EMPTY,         NULL,                   ""                      },
1831   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1832   { 0,                  NULL,                   NULL                    }
1833 };
1834
1835 static struct TokenInfo setup_info_graphics[] =
1836 {
1837   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen:"           },
1838   { TYPE_SWITCH,        &setup.scroll_delay,    "Scroll Delay:"         },
1839   { TYPE_SWITCH,        &setup.soft_scrolling,  "Soft Scroll.:"         },
1840 #if 0
1841   { TYPE_SWITCH,        &setup.double_buffering,"Buffered gfx:"         },
1842   { TYPE_SWITCH,        &setup.fading,          "Fading:"               },
1843 #endif
1844   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Doors:"          },
1845   { TYPE_SWITCH,        &setup.toons,           "Toons:"                },
1846   { TYPE_EMPTY,         NULL,                   ""                      },
1847   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1848   { 0,                  NULL,                   NULL                    }
1849 };
1850
1851 static struct TokenInfo setup_info_sound[] =
1852 {
1853   { TYPE_SWITCH,        &setup.sound_simple,    "Simple Sound:"         },
1854   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Loops:"          },
1855   { TYPE_SWITCH,        &setup.sound_music,     "Game Music:"           },
1856   { TYPE_EMPTY,         NULL,                   ""                      },
1857   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1858   { 0,                  NULL,                   NULL                    }
1859 };
1860
1861 static struct TokenInfo setup_info_artwork[] =
1862 {
1863   { TYPE_ENTER_MENU,    execSetupChooseGraphics,"Custom Graphics"       },
1864   { TYPE_STRING,        &graphics_set_name,     ""                      },
1865   { TYPE_ENTER_MENU,    execSetupChooseSounds,  "Custom Sounds"         },
1866   { TYPE_STRING,        &sounds_set_name,       ""                      },
1867   { TYPE_ENTER_MENU,    execSetupChooseMusic,   "Custom Music"          },
1868   { TYPE_STRING,        &music_set_name,        ""                      },
1869   { TYPE_EMPTY,         NULL,                   ""                      },
1870   { TYPE_STRING,        NULL,                   "Override Level Artwork:"},
1871   { TYPE_YES_NO,        &setup.override_level_graphics, "Graphics:"     },
1872   { TYPE_YES_NO,        &setup.override_level_sounds,   "Sounds:"       },
1873   { TYPE_YES_NO,        &setup.override_level_music,    "Music:"        },
1874   { TYPE_EMPTY,         NULL,                   ""                      },
1875   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1876   { 0,                  NULL,                   NULL                    }
1877 };
1878
1879 static struct TokenInfo setup_info_shortcut[] =
1880 {
1881   { TYPE_KEYTEXT,       NULL,                   "Quick Save Game:",     },
1882   { TYPE_KEY,           &setup.shortcut.save_game,      ""              },
1883   { TYPE_KEYTEXT,       NULL,                   "Quick Load Game:",     },
1884   { TYPE_KEY,           &setup.shortcut.load_game,      ""              },
1885   { TYPE_KEYTEXT,       NULL,                   "Toggle Pause:",        },
1886   { TYPE_KEY,           &setup.shortcut.toggle_pause,   ""              },
1887   { TYPE_EMPTY,         NULL,                   ""                      },
1888   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on Esc:"           },
1889   { TYPE_EMPTY,         NULL,                   ""                      },
1890   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
1891   { 0,                  NULL,                   NULL                    }
1892 };
1893
1894 static Key getSetupKey()
1895 {
1896   Key key = KSYM_UNDEFINED;
1897   boolean got_key_event = FALSE;
1898
1899   while (!got_key_event)
1900   {
1901     if (PendingEvent())         /* got event */
1902     {
1903       Event event;
1904
1905       NextEvent(&event);
1906
1907       switch(event.type)
1908       {
1909         case EVENT_KEYPRESS:
1910           {
1911             key = GetEventKey((KeyEvent *)&event, TRUE);
1912
1913             /* press 'Escape' or 'Enter' to keep the existing key binding */
1914             if (key == KSYM_Escape || key == KSYM_Return)
1915               key = KSYM_UNDEFINED;     /* keep old value */
1916
1917             got_key_event = TRUE;
1918           }
1919           break;
1920
1921         case EVENT_KEYRELEASE:
1922           key_joystick_mapping = 0;
1923           break;
1924
1925         default:
1926           HandleOtherEvents(&event);
1927           break;
1928       }
1929     }
1930
1931     DoAnimation();
1932     BackToFront();
1933
1934     /* don't eat all CPU time */
1935     Delay(10);
1936   }
1937
1938   return key;
1939 }
1940
1941 static void drawSetupValue(int pos)
1942 {
1943   int xpos = MENU_SCREEN_VALUE_XPOS;
1944   int ypos = MENU_SCREEN_START_YPOS + pos;
1945   int font_nr = FONT_VALUE_1;
1946   int type = setup_info[pos].type;
1947   void *value = setup_info[pos].value;
1948   char *value_string = (!(type & TYPE_GHOSTED) ? getSetupValue(type, value) :
1949                         "n/a");
1950
1951   if (value_string == NULL)
1952     return;
1953
1954   if (type & TYPE_KEY)
1955   {
1956     xpos = 3;
1957
1958     if (type & TYPE_QUERY)
1959     {
1960       value_string = "<press key>";
1961       font_nr = FONT_INPUT_1_ACTIVE;
1962     }
1963   }
1964   else if (type & TYPE_STRING)
1965   {
1966     int max_value_len = (SCR_FIELDX - 2) * 2;
1967
1968     xpos = 1;
1969     font_nr = FONT_VALUE_2;
1970
1971     if (strlen(value_string) > max_value_len)
1972       value_string[max_value_len] = '\0';
1973   }
1974   else if (type & TYPE_BOOLEAN_STYLE)
1975   {
1976     font_nr = (*(boolean *)value ? FONT_OPTION_ON : FONT_OPTION_OFF);
1977   }
1978
1979   DrawText(mSX + xpos * 32, mSY + ypos * 32,
1980            (xpos == 3 ? "              " : "   "), font_nr);
1981   DrawText(mSX + xpos * 32, mSY + ypos * 32, value_string, font_nr);
1982 }
1983
1984 static void changeSetupValue(int pos)
1985 {
1986   if (setup_info[pos].type & TYPE_BOOLEAN_STYLE)
1987   {
1988     *(boolean *)setup_info[pos].value ^= TRUE;
1989   }
1990   else if (setup_info[pos].type & TYPE_KEY)
1991   {
1992     Key key;
1993
1994     setup_info[pos].type |= TYPE_QUERY;
1995     drawSetupValue(pos);
1996     setup_info[pos].type &= ~TYPE_QUERY;
1997
1998     key = getSetupKey();
1999     if (key != KSYM_UNDEFINED)
2000       *(Key *)setup_info[pos].value = key;
2001   }
2002
2003   drawSetupValue(pos);
2004 }
2005
2006 static void DrawSetupScreen_Generic()
2007 {
2008   char *title_string = NULL;
2009   int i;
2010
2011   UnmapAllGadgets();
2012   CloseDoor(DOOR_CLOSE_2);
2013
2014   ClearWindow();
2015
2016   if (setup_mode == SETUP_MODE_MAIN)
2017   {
2018     setup_info = setup_info_main;
2019     title_string = "Setup";
2020   }
2021   else if (setup_mode == SETUP_MODE_GAME)
2022   {
2023     setup_info = setup_info_game;
2024     title_string = "Setup Game";
2025   }
2026   else if (setup_mode == SETUP_MODE_EDITOR)
2027   {
2028     setup_info = setup_info_editor;
2029     title_string = "Setup Editor";
2030   }
2031   else if (setup_mode == SETUP_MODE_GRAPHICS)
2032   {
2033     setup_info = setup_info_graphics;
2034     title_string = "Setup Graphics";
2035   }
2036   else if (setup_mode == SETUP_MODE_SOUND)
2037   {
2038     setup_info = setup_info_sound;
2039     title_string = "Setup Sound";
2040   }
2041   else if (setup_mode == SETUP_MODE_ARTWORK)
2042   {
2043     setup_info = setup_info_artwork;
2044     title_string = "Custom Artwork";
2045   }
2046   else if (setup_mode == SETUP_MODE_SHORTCUT)
2047   {
2048     setup_info = setup_info_shortcut;
2049     title_string = "Setup Shortcuts";
2050   }
2051
2052   DrawText(mSX + 16, mSY + 16, title_string, FONT_TITLE_1);
2053
2054   num_setup_info = 0;
2055   for(i=0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
2056   {
2057     void *value_ptr = setup_info[i].value;
2058     int ypos = MENU_SCREEN_START_YPOS + i;
2059     int font_nr = FONT_MENU_1;
2060
2061     /* set some entries to "unchangeable" according to other variables */
2062     if ((value_ptr == &setup.sound_simple && !audio.sound_available) ||
2063         (value_ptr == &setup.sound_loops  && !audio.loops_available) ||
2064         (value_ptr == &setup.sound_music  && !audio.music_available) ||
2065         (value_ptr == &setup.fullscreen   && !video.fullscreen_available))
2066       setup_info[i].type |= TYPE_GHOSTED;
2067
2068     if (setup_info[i].type & TYPE_STRING)
2069       font_nr = FONT_MENU_2;
2070
2071     DrawText(mSX + 32, mSY + ypos * 32, setup_info[i].text, font_nr);
2072
2073     if (setup_info[i].type & TYPE_ENTER_MENU)
2074       initCursor(i, IMG_MENU_BUTTON_RIGHT);
2075     else if (setup_info[i].type & TYPE_LEAVE_MENU)
2076       initCursor(i, IMG_MENU_BUTTON_LEFT);
2077     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
2078       initCursor(i, IMG_MENU_BUTTON);
2079
2080     if (setup_info[i].type & TYPE_VALUE)
2081       drawSetupValue(i);
2082
2083     num_setup_info++;
2084   }
2085
2086   FadeToFront();
2087   InitAnimation();
2088   HandleSetupScreen_Generic(0,0,0,0,MB_MENU_INITIALIZE);
2089 }
2090
2091 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
2092 {
2093   static int choice_store[MAX_SETUP_MODES];
2094   int choice = choice_store[setup_mode];        /* always starts with 0 */
2095   int x = 0;
2096   int y = choice;
2097
2098   if (button == MB_MENU_INITIALIZE)
2099   {
2100     /* advance to first valid menu entry */
2101     while (choice < num_setup_info &&
2102            (setup_info[choice].type & TYPE_SKIP_ENTRY))
2103       choice++;
2104     choice_store[setup_mode] = choice;
2105
2106     drawCursor(choice, FC_RED);
2107     return;
2108   }
2109   else if (button == MB_MENU_LEAVE)
2110   {
2111     for (y=0; y<num_setup_info; y++)
2112     {
2113       if (setup_info[y].type & TYPE_LEAVE_MENU)
2114       {
2115         void (*menu_callback_function)(void) = setup_info[y].value;
2116
2117         menu_callback_function();
2118         break;  /* absolutely needed because function changes 'setup_info'! */
2119       }
2120     }
2121
2122     return;
2123   }
2124
2125   if (mx || my)         /* mouse input */
2126   {
2127     x = (mx - mSX) / 32;
2128     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2129   }
2130   else if (dx || dy)    /* keyboard input */
2131   {
2132     if (dx)
2133     {
2134       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE_MENU : TYPE_ENTER_MENU);
2135
2136       if ((setup_info[choice].type & menu_navigation_type) ||
2137           (setup_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_setup_info - 1 &&
2145            (setup_info[y].type & TYPE_SKIP_ENTRY))
2146       y += dy;
2147   }
2148
2149   if (x == 0 && y >= 0 && y < num_setup_info &&
2150       (setup_info[y].type & ~TYPE_SKIP_ENTRY))
2151   {
2152     if (button)
2153     {
2154       if (y != choice)
2155       {
2156         drawCursor(y, FC_RED);
2157         drawCursor(choice, FC_BLUE);
2158         choice = choice_store[setup_mode] = y;
2159       }
2160     }
2161     else if (!(setup_info[y].type & TYPE_GHOSTED))
2162     {
2163       if (setup_info[y].type & TYPE_ENTER_OR_LEAVE_MENU)
2164       {
2165         void (*menu_callback_function)(void) = setup_info[choice].value;
2166
2167         menu_callback_function();
2168       }
2169       else
2170       {
2171         if ((setup_info[y].type & TYPE_KEYTEXT) &&
2172             (setup_info[y + 1].type & TYPE_KEY))
2173           y++;
2174
2175         if (setup_info[y].type & TYPE_VALUE)
2176           changeSetupValue(y);
2177       }
2178     }
2179   }
2180
2181 #if 0
2182   BackToFront();
2183
2184   if (game_status == GAME_MODE_SETUP)
2185     DoAnimation();
2186 #endif
2187 }
2188
2189 void DrawSetupScreen_Input()
2190 {
2191   ClearWindow();
2192
2193   DrawText(mSX+16, mSY+16, "Setup Input", FONT_TITLE_1);
2194
2195   initCursor(0, IMG_MENU_BUTTON);
2196   initCursor(1, IMG_MENU_BUTTON);
2197   initCursor(2, IMG_MENU_BUTTON_RIGHT);
2198   initCursor(13, IMG_MENU_BUTTON_LEFT);
2199
2200   drawCursorXY(10, 0, IMG_MENU_BUTTON_LEFT);
2201   drawCursorXY(12, 0, IMG_MENU_BUTTON_RIGHT);
2202
2203   DrawText(mSX+32, mSY+2*32, "Player:", FONT_MENU_1);
2204   DrawText(mSX+32, mSY+3*32, "Device:", FONT_MENU_1);
2205   DrawText(mSX+32, mSY+15*32, "Back",   FONT_MENU_1);
2206
2207 #if 0
2208   DeactivateJoystickForCalibration();
2209   DrawTextFCentered(SYSIZE - 20, FONT_TEXT_4,
2210                     "Joysticks deactivated on this screen");
2211 #endif
2212
2213   HandleSetupScreen_Input(0,0, 0,0, MB_MENU_INITIALIZE);
2214   FadeToFront();
2215   InitAnimation();
2216 }
2217
2218 static void setJoystickDeviceToNr(char *device_name, int device_nr)
2219 {
2220   if (device_name == NULL)
2221     return;
2222
2223   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
2224     device_nr = 0;
2225
2226   if (strlen(device_name) > 1)
2227   {
2228     char c1 = device_name[strlen(device_name) - 1];
2229     char c2 = device_name[strlen(device_name) - 2];
2230
2231     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
2232       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
2233   }
2234   else
2235     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
2236             strlen(device_name));
2237 }
2238
2239 static void drawPlayerSetupInputInfo(int player_nr)
2240 {
2241   int i;
2242   static struct SetupKeyboardInfo custom_key;
2243   static struct
2244   {
2245     Key *key;
2246     char *text;
2247   } custom[] =
2248   {
2249     { &custom_key.left,  "Joystick Left"  },
2250     { &custom_key.right, "Joystick Right" },
2251     { &custom_key.up,    "Joystick Up"    },
2252     { &custom_key.down,  "Joystick Down"  },
2253     { &custom_key.snap,  "Button 1"       },
2254     { &custom_key.bomb,  "Button 2"       }
2255   };
2256   static char *joystick_name[MAX_PLAYERS] =
2257   {
2258     "Joystick1",
2259     "Joystick2",
2260     "Joystick3",
2261     "Joystick4"
2262   };
2263
2264   custom_key = setup.input[player_nr].key;
2265
2266   DrawText(mSX+11*32, mSY+2*32, int2str(player_nr +1, 1), FONT_INPUT_1_ACTIVE);
2267 #if 1
2268   DrawGraphicThruMaskExt(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
2269                          PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
2270 #else
2271   DrawGraphicThruMask(8, 2, PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
2272 #endif
2273
2274   if (setup.input[player_nr].use_joystick)
2275   {
2276     char *device_name = setup.input[player_nr].joy.device_name;
2277
2278     DrawText(mSX+8*32, mSY+3*32,
2279              joystick_name[getJoystickNrFromDeviceName(device_name)],
2280              FONT_VALUE_1);
2281     DrawText(mSX+32, mSY+4*32, "Calibrate", FONT_MENU_1);
2282   }
2283   else
2284   {
2285     DrawText(mSX+8*32, mSY+3*32, "Keyboard ", FONT_VALUE_1);
2286     DrawText(mSX+32,   mSY+4*32, "Customize", FONT_MENU_1);
2287   }
2288
2289   DrawText(mSX+32, mSY+5*32, "Actual Settings:", FONT_MENU_1);
2290   drawCursorXY(1, 4, IMG_MENU_BUTTON_LEFT);
2291   drawCursorXY(1, 5, IMG_MENU_BUTTON_RIGHT);
2292   drawCursorXY(1, 6, IMG_MENU_BUTTON_UP);
2293   drawCursorXY(1, 7, IMG_MENU_BUTTON_DOWN);
2294   DrawText(mSX+2*32, mSY+6*32, ":", FONT_VALUE_OLD);
2295   DrawText(mSX+2*32, mSY+7*32, ":", FONT_VALUE_OLD);
2296   DrawText(mSX+2*32, mSY+8*32, ":", FONT_VALUE_OLD);
2297   DrawText(mSX+2*32, mSY+9*32, ":", FONT_VALUE_OLD);
2298   DrawText(mSX+32, mSY+10*32, "Snap Field:", FONT_VALUE_OLD);
2299   DrawText(mSX+32, mSY+12*32, "Place Bomb:", FONT_VALUE_OLD);
2300
2301   for (i=0; i<6; i++)
2302   {
2303     int ypos = 6 + i + (i > 3 ? i-3 : 0);
2304
2305     DrawText(mSX + 3*32, mSY + ypos*32,
2306              "              ", FONT_VALUE_1);
2307     DrawText(mSX + 3*32, mSY + ypos*32,
2308              (setup.input[player_nr].use_joystick ?
2309               custom[i].text :
2310               getKeyNameFromKey(*custom[i].key)), FONT_VALUE_1);
2311   }
2312 }
2313
2314 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
2315 {
2316   static int choice = 0;
2317   static int player_nr = 0;
2318   int x = 0;
2319   int y = choice;
2320   int pos_start  = SETUPINPUT_SCREEN_POS_START;
2321   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
2322   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
2323   int pos_end    = SETUPINPUT_SCREEN_POS_END;
2324
2325   if (button == MB_MENU_INITIALIZE)
2326   {
2327     drawPlayerSetupInputInfo(player_nr);
2328     drawCursor(choice, FC_RED);
2329     return;
2330   }
2331   else if (button == MB_MENU_LEAVE)
2332   {
2333     setup_mode = SETUP_MODE_MAIN;
2334     DrawSetupScreen();
2335     InitJoysticks();
2336   }
2337
2338   if (mx || my)         /* mouse input */
2339   {
2340     x = (mx - mSX) / 32;
2341     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2342   }
2343   else if (dx || dy)    /* keyboard input */
2344   {
2345     if (dx && choice == 0)
2346       x = (dx < 0 ? 10 : 12);
2347     else if ((dx && choice == 1) ||
2348              (dx == +1 && choice == 2) ||
2349              (dx == -1 && choice == pos_end))
2350       button = MB_MENU_CHOICE;
2351     else if (dy)
2352       y = choice + dy;
2353
2354     if (y >= pos_empty1 && y <= pos_empty2)
2355       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
2356   }
2357
2358   if (y == 0 && ((x == 0 && !button) || ((x == 10 || x == 12) && button)))
2359   {
2360     static unsigned long delay = 0;
2361
2362     if (!DelayReached(&delay, GADGET_FRAME_DELAY))
2363 #if 1
2364       return;
2365 #else
2366       goto out;
2367 #endif
2368
2369     player_nr = (player_nr + (x == 10 ? -1 : +1) + MAX_PLAYERS) % MAX_PLAYERS;
2370
2371     drawPlayerSetupInputInfo(player_nr);
2372   }
2373   else if (x == 0 && y >= pos_start && y <= pos_end &&
2374            !(y >= pos_empty1 && y <= pos_empty2))
2375   {
2376     if (button)
2377     {
2378       if (y != choice)
2379       {
2380         drawCursor(y, FC_RED);
2381         drawCursor(choice, FC_BLUE);
2382         choice = y;
2383       }
2384     }
2385     else
2386     {
2387       if (y == 1)
2388       {
2389         char *device_name = setup.input[player_nr].joy.device_name;
2390
2391         if (!setup.input[player_nr].use_joystick)
2392         {
2393           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
2394
2395           setJoystickDeviceToNr(device_name, new_device_nr);
2396           setup.input[player_nr].use_joystick = TRUE;
2397         }
2398         else
2399         {
2400           int device_nr = getJoystickNrFromDeviceName(device_name);
2401           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
2402
2403           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
2404             setup.input[player_nr].use_joystick = FALSE;
2405           else
2406             setJoystickDeviceToNr(device_name, new_device_nr);
2407         }
2408
2409         drawPlayerSetupInputInfo(player_nr);
2410       }
2411       else if (y == 2)
2412       {
2413         if (setup.input[player_nr].use_joystick)
2414         {
2415           InitJoysticks();
2416           CalibrateJoystick(player_nr);
2417         }
2418         else
2419           CustomizeKeyboard(player_nr);
2420       }
2421       else if (y == pos_end)
2422       {
2423         InitJoysticks();
2424
2425         setup_mode = SETUP_MODE_MAIN;
2426         DrawSetupScreen();
2427       }
2428     }
2429   }
2430
2431 #if 0
2432   BackToFront();
2433
2434   out:
2435
2436   if (game_status == GAME_MODE_SETUP)
2437     DoAnimation();
2438 #endif
2439 }
2440
2441 void CustomizeKeyboard(int player_nr)
2442 {
2443   int i;
2444   int step_nr;
2445   boolean finished = FALSE;
2446   static struct SetupKeyboardInfo custom_key;
2447   static struct
2448   {
2449     Key *key;
2450     char *text;
2451   } customize_step[] =
2452   {
2453     { &custom_key.left,  "Move Left"  },
2454     { &custom_key.right, "Move Right" },
2455     { &custom_key.up,    "Move Up"    },
2456     { &custom_key.down,  "Move Down"  },
2457     { &custom_key.snap,  "Snap Field" },
2458     { &custom_key.bomb,  "Place Bomb" }
2459   };
2460
2461   /* read existing key bindings from player setup */
2462   custom_key = setup.input[player_nr].key;
2463
2464   ClearWindow();
2465   DrawText(mSX + 16, mSY + 16, "Keyboard Input", FONT_TITLE_1);
2466
2467   BackToFront();
2468   InitAnimation();
2469
2470   step_nr = 0;
2471   DrawText(mSX, mSY + (2+2*step_nr)*32,
2472            customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
2473   DrawText(mSX, mSY + (2+2*step_nr+1)*32,
2474            "Key:", FONT_INPUT_1_ACTIVE);
2475   DrawText(mSX + 4*32, mSY + (2+2*step_nr+1)*32,
2476            getKeyNameFromKey(*customize_step[step_nr].key), FONT_VALUE_OLD);
2477
2478   while(!finished)
2479   {
2480     if (PendingEvent())         /* got event */
2481     {
2482       Event event;
2483
2484       NextEvent(&event);
2485
2486       switch(event.type)
2487       {
2488         case EVENT_KEYPRESS:
2489           {
2490             Key key = GetEventKey((KeyEvent *)&event, FALSE);
2491
2492             if (key == KSYM_Escape || (key == KSYM_Return && step_nr == 6))
2493             {
2494               finished = TRUE;
2495               break;
2496             }
2497
2498             /* all keys configured -- wait for "Escape" or "Return" key */
2499             if (step_nr == 6)
2500               break;
2501
2502             /* press 'Enter' to keep the existing key binding */
2503             if (key == KSYM_Return)
2504               key = *customize_step[step_nr].key;
2505
2506             /* check if key already used */
2507             for (i=0; i<step_nr; i++)
2508               if (*customize_step[i].key == key)
2509                 break;
2510             if (i < step_nr)
2511               break;
2512
2513             /* got new key binding */
2514             *customize_step[step_nr].key = key;
2515             DrawText(mSX + 4*32, mSY + (2+2*step_nr+1)*32,
2516                      "             ", FONT_VALUE_1);
2517             DrawText(mSX + 4*32, mSY + (2+2*step_nr+1)*32,
2518                      getKeyNameFromKey(key), FONT_VALUE_1);
2519             step_nr++;
2520
2521             /* un-highlight last query */
2522             DrawText(mSX, mSY+(2+2*(step_nr-1))*32,
2523                      customize_step[step_nr-1].text, FONT_MENU_1);
2524             DrawText(mSX, mSY+(2+2*(step_nr-1)+1)*32,
2525                      "Key:", FONT_MENU_1);
2526
2527             /* press 'Enter' to leave */
2528             if (step_nr == 6)
2529             {
2530               DrawText(mSX + 16, mSY + 15*32+16,
2531                        "Press Enter", FONT_TITLE_1);
2532               break;
2533             }
2534
2535             /* query next key binding */
2536             DrawText(mSX, mSY+(2+2*step_nr)*32,
2537                      customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
2538             DrawText(mSX, mSY+(2+2*step_nr+1)*32,
2539                      "Key:", FONT_INPUT_1_ACTIVE);
2540             DrawText(mSX + 4*32, mSY+(2+2*step_nr+1)*32,
2541                      getKeyNameFromKey(*customize_step[step_nr].key),
2542                      FONT_VALUE_OLD);
2543           }
2544           break;
2545
2546         case EVENT_KEYRELEASE:
2547           key_joystick_mapping = 0;
2548           break;
2549
2550         default:
2551           HandleOtherEvents(&event);
2552           break;
2553       }
2554     }
2555
2556     DoAnimation();
2557     BackToFront();
2558
2559     /* don't eat all CPU time */
2560     Delay(10);
2561   }
2562
2563   /* write new key bindings back to player setup */
2564   setup.input[player_nr].key = custom_key;
2565
2566   StopAnimation();
2567   DrawSetupScreen_Input();
2568 }
2569
2570 static boolean CalibrateJoystickMain(int player_nr)
2571 {
2572   int new_joystick_xleft = JOYSTICK_XMIDDLE;
2573   int new_joystick_xright = JOYSTICK_XMIDDLE;
2574   int new_joystick_yupper = JOYSTICK_YMIDDLE;
2575   int new_joystick_ylower = JOYSTICK_YMIDDLE;
2576   int new_joystick_xmiddle, new_joystick_ymiddle;
2577
2578   int joystick_fd = joystick.fd[player_nr];
2579   int x, y, last_x, last_y, xpos = 8, ypos = 3;
2580   boolean check[3][3];
2581   int check_remaining = 3 * 3;
2582   int joy_x, joy_y;
2583   int joy_value;
2584   int result = -1;
2585
2586   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
2587     return FALSE;
2588
2589   if (joystick_fd < 0 || !setup.input[player_nr].use_joystick)
2590     return FALSE;
2591
2592   ClearWindow();
2593
2594   for(y=0; y < 3; y++)
2595   {
2596     for(x=0; x < 3; x++)
2597     {
2598       DrawGraphic(xpos + x - 1, ypos + y - 1, IMG_MENU_CALIBRATE_BLUE, 0);
2599       check[x][y] = FALSE;
2600     }
2601   }
2602
2603   DrawText(mSX,      mSY +  6 * 32, " ROTATE JOYSTICK ", FONT_TITLE_1);
2604   DrawText(mSX,      mSY +  7 * 32, "IN ALL DIRECTIONS", FONT_TITLE_1);
2605   DrawText(mSX + 16, mSY +  9 * 32, "  IF ALL BALLS  ",  FONT_TITLE_1);
2606   DrawText(mSX,      mSY + 10 * 32, "   ARE YELLOW,   ", FONT_TITLE_1);
2607   DrawText(mSX,      mSY + 11 * 32, " CENTER JOYSTICK ", FONT_TITLE_1);
2608   DrawText(mSX,      mSY + 12 * 32, "       AND       ", FONT_TITLE_1);
2609   DrawText(mSX,      mSY + 13 * 32, "PRESS ANY BUTTON!", FONT_TITLE_1);
2610
2611   joy_value = Joystick(player_nr);
2612   last_x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
2613   last_y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
2614
2615   /* eventually uncalibrated center position (joystick could be uncentered) */
2616   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
2617     return FALSE;
2618
2619   new_joystick_xmiddle = joy_x;
2620   new_joystick_ymiddle = joy_y;
2621
2622   DrawGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_RED, 0);
2623   BackToFront();
2624
2625   while(Joystick(player_nr) & JOY_BUTTON);      /* wait for released button */
2626   InitAnimation();
2627
2628   while(result < 0)
2629   {
2630     if (PendingEvent())         /* got event */
2631     {
2632       Event event;
2633
2634       NextEvent(&event);
2635
2636       switch(event.type)
2637       {
2638         case EVENT_KEYPRESS:
2639           switch(GetEventKey((KeyEvent *)&event, TRUE))
2640           {
2641             case KSYM_Return:
2642               if (check_remaining == 0)
2643                 result = 1;
2644               break;
2645
2646             case KSYM_Escape:
2647               result = 0;
2648               break;
2649
2650             default:
2651               break;
2652           }
2653           break;
2654
2655         case EVENT_KEYRELEASE:
2656           key_joystick_mapping = 0;
2657           break;
2658
2659         default:
2660           HandleOtherEvents(&event);
2661           break;
2662       }
2663     }
2664
2665     if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
2666       return FALSE;
2667
2668     new_joystick_xleft  = MIN(new_joystick_xleft,  joy_x);
2669     new_joystick_xright = MAX(new_joystick_xright, joy_x);
2670     new_joystick_yupper = MIN(new_joystick_yupper, joy_y);
2671     new_joystick_ylower = MAX(new_joystick_ylower, joy_y);
2672
2673     setup.input[player_nr].joy.xleft = new_joystick_xleft;
2674     setup.input[player_nr].joy.yupper = new_joystick_yupper;
2675     setup.input[player_nr].joy.xright = new_joystick_xright;
2676     setup.input[player_nr].joy.ylower = new_joystick_ylower;
2677     setup.input[player_nr].joy.xmiddle = new_joystick_xmiddle;
2678     setup.input[player_nr].joy.ymiddle = new_joystick_ymiddle;
2679
2680     CheckJoystickData();
2681
2682     joy_value = Joystick(player_nr);
2683
2684     if (joy_value & JOY_BUTTON && check_remaining == 0)
2685       result = 1;
2686
2687     x = (joy_value & JOY_LEFT ? -1 : joy_value & JOY_RIGHT ? +1 : 0);
2688     y = (joy_value & JOY_UP   ? -1 : joy_value & JOY_DOWN  ? +1 : 0);
2689
2690     if (x != last_x || y != last_y)
2691     {
2692       DrawGraphic(xpos + last_x, ypos + last_y, IMG_MENU_CALIBRATE_YELLOW, 0);
2693       DrawGraphic(xpos + x,      ypos + y,      IMG_MENU_CALIBRATE_RED,    0);
2694
2695       last_x = x;
2696       last_y = y;
2697
2698       if (check_remaining > 0 && !check[x+1][y+1])
2699       {
2700         check[x+1][y+1] = TRUE;
2701         check_remaining--;
2702       }
2703
2704 #if 0
2705 #ifdef DEBUG
2706       printf("LEFT / MIDDLE / RIGHT == %d / %d / %d\n",
2707              setup.input[player_nr].joy.xleft,
2708              setup.input[player_nr].joy.xmiddle,
2709              setup.input[player_nr].joy.xright);
2710       printf("UP / MIDDLE / DOWN == %d / %d / %d\n",
2711              setup.input[player_nr].joy.yupper,
2712              setup.input[player_nr].joy.ymiddle,
2713              setup.input[player_nr].joy.ylower);
2714 #endif
2715 #endif
2716
2717     }
2718
2719     DoAnimation();
2720     BackToFront();
2721
2722     /* don't eat all CPU time */
2723     Delay(10);
2724   }
2725
2726   /* calibrated center position (joystick should now be centered) */
2727   if (!ReadJoystick(joystick_fd, &joy_x, &joy_y, NULL, NULL))
2728     return FALSE;
2729
2730   new_joystick_xmiddle = joy_x;
2731   new_joystick_ymiddle = joy_y;
2732
2733   StopAnimation();
2734
2735   DrawSetupScreen_Input();
2736
2737   /* wait until the last pressed button was released */
2738   while (Joystick(player_nr) & JOY_BUTTON)
2739   {
2740     if (PendingEvent())         /* got event */
2741     {
2742       Event event;
2743
2744       NextEvent(&event);
2745       HandleOtherEvents(&event);
2746
2747       Delay(10);
2748     }
2749   }
2750
2751   return TRUE;
2752 }
2753
2754 void CalibrateJoystick(int player_nr)
2755 {
2756   if (!CalibrateJoystickMain(player_nr))
2757   {
2758     ClearWindow();
2759
2760     DrawText(mSX + 16, mSY + 6*32, "  JOYSTICK NOT  ",  FONT_TITLE_1);
2761     DrawText(mSX,      mSY + 7*32, "    AVAILABLE    ", FONT_TITLE_1);
2762     BackToFront();
2763     Delay(2000);        /* show error message for two seconds */
2764   }
2765 }
2766
2767 void DrawSetupScreen()
2768 {
2769   DeactivateJoystick();
2770
2771   SetMainBackgroundImage(IMG_BACKGROUND_SETUP);
2772
2773   if (setup_mode == SETUP_MODE_INPUT)
2774     DrawSetupScreen_Input();
2775   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
2776     DrawChooseTree(&artwork.gfx_current);
2777   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
2778     DrawChooseTree(&artwork.snd_current);
2779   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
2780     DrawChooseTree(&artwork.mus_current);
2781   else
2782     DrawSetupScreen_Generic();
2783 }
2784
2785 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
2786 {
2787   if (setup_mode == SETUP_MODE_INPUT)
2788     HandleSetupScreen_Input(mx, my, dx, dy, button);
2789   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
2790     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
2791   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
2792     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
2793   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
2794     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
2795   else
2796     HandleSetupScreen_Generic(mx, my, dx, dy, button);
2797
2798   DoAnimation();
2799   BackToFront();
2800 }
2801
2802 void HandleGameActions()
2803 {
2804   if (game_status != GAME_MODE_PLAYING)
2805     return;
2806
2807   if (local_player->LevelSolved)
2808     GameWon();
2809
2810   if (AllPlayersGone && !TAPE_IS_STOPPED(tape))
2811     TapeStop();
2812
2813   GameActions();
2814
2815   BackToFront();
2816
2817 #if 1
2818   if (tape.auto_play && !tape.playing)
2819     AutoPlayTape();     /* continue automatically playing next tape */
2820 #endif
2821 }
2822
2823 /* ---------- new screen button stuff -------------------------------------- */
2824
2825 /* graphic position and size values for buttons and scrollbars */
2826 #define SC_SCROLLBUTTON_XSIZE           TILEX
2827 #define SC_SCROLLBUTTON_YSIZE           TILEY
2828
2829 #define SC_SCROLL_VERTICAL_XSIZE        SC_SCROLLBUTTON_XSIZE
2830 #define SC_SCROLL_VERTICAL_YSIZE        ((MAX_MENU_ENTRIES_ON_SCREEN - 2) * \
2831                                          SC_SCROLLBUTTON_YSIZE)
2832 #define SC_SCROLL_UP_XPOS               (SXSIZE - SC_SCROLLBUTTON_XSIZE)
2833 #define SC_SCROLL_UP_YPOS               (2 * SC_SCROLLBUTTON_YSIZE)
2834 #define SC_SCROLL_VERTICAL_XPOS         SC_SCROLL_UP_XPOS
2835 #define SC_SCROLL_VERTICAL_YPOS         (SC_SCROLL_UP_YPOS + \
2836                                          SC_SCROLLBUTTON_YSIZE)
2837 #define SC_SCROLL_DOWN_XPOS             SC_SCROLL_UP_XPOS
2838 #define SC_SCROLL_DOWN_YPOS             (SC_SCROLL_VERTICAL_YPOS + \
2839                                          SC_SCROLL_VERTICAL_YSIZE)
2840
2841 #define SC_BORDER_SIZE                  14
2842
2843 static struct
2844 {
2845   int gfx_unpressed, gfx_pressed;
2846   int x, y;
2847   int gadget_id;
2848   char *infotext;
2849 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
2850 {
2851   {
2852     IMG_MENU_BUTTON_UP, IMG_MENU_BUTTON_UP_ACTIVE,
2853     SC_SCROLL_UP_XPOS, SC_SCROLL_UP_YPOS,
2854     SCREEN_CTRL_ID_SCROLL_UP,
2855     "scroll up"
2856   },
2857   {
2858     IMG_MENU_BUTTON_DOWN, IMG_MENU_BUTTON_DOWN_ACTIVE,
2859     SC_SCROLL_DOWN_XPOS, SC_SCROLL_DOWN_YPOS,
2860     SCREEN_CTRL_ID_SCROLL_DOWN,
2861     "scroll down"
2862   }
2863 };
2864
2865 static struct
2866 {
2867 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2868   Bitmap **gfx_unpressed, **gfx_pressed;
2869 #else
2870   int gfx_unpressed, gfx_pressed;
2871 #endif
2872   int x, y;
2873   int width, height;
2874   int type;
2875   int gadget_id;
2876   char *infotext;
2877 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
2878 {
2879   {
2880 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2881     &scrollbar_bitmap[0], &scrollbar_bitmap[1],
2882 #else
2883     IMG_MENU_SCROLLBAR, IMG_MENU_SCROLLBAR_ACTIVE,
2884 #endif
2885     SC_SCROLL_VERTICAL_XPOS, SC_SCROLL_VERTICAL_YPOS,
2886     SC_SCROLL_VERTICAL_XSIZE, SC_SCROLL_VERTICAL_YSIZE,
2887     GD_TYPE_SCROLLBAR_VERTICAL,
2888     SCREEN_CTRL_ID_SCROLL_VERTICAL,
2889     "scroll level series vertically"
2890   }
2891 };
2892
2893 static void CreateScreenScrollbuttons()
2894 {
2895   struct GadgetInfo *gi;
2896   unsigned long event_mask;
2897   int i;
2898
2899   for (i=0; i<NUM_SCREEN_SCROLLBUTTONS; i++)
2900   {
2901     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
2902     int gfx_unpressed, gfx_pressed;
2903     int x, y, width, height;
2904     int gd_x1, gd_x2, gd_y1, gd_y2;
2905     int id = scrollbutton_info[i].gadget_id;
2906
2907     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
2908
2909     x = mSX + scrollbutton_info[i].x + menu.scrollbar_xoffset;
2910     y = mSY + scrollbutton_info[i].y;
2911     width = SC_SCROLLBUTTON_XSIZE;
2912     height = SC_SCROLLBUTTON_YSIZE;
2913
2914     if (id == SCREEN_CTRL_ID_SCROLL_DOWN)
2915       y = mSY + (SC_SCROLL_VERTICAL_YPOS +
2916                  (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE);
2917
2918     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
2919     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
2920     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
2921     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
2922     gd_x1 = graphic_info[gfx_unpressed].src_x;
2923     gd_y1 = graphic_info[gfx_unpressed].src_y;
2924     gd_x2 = graphic_info[gfx_pressed].src_x;
2925     gd_y2 = graphic_info[gfx_pressed].src_y;
2926
2927     gi = CreateGadget(GDI_CUSTOM_ID, id,
2928                       GDI_CUSTOM_TYPE_ID, i,
2929                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
2930                       GDI_X, x,
2931                       GDI_Y, y,
2932                       GDI_WIDTH, width,
2933                       GDI_HEIGHT, height,
2934                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
2935                       GDI_STATE, GD_BUTTON_UNPRESSED,
2936                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
2937                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
2938                       GDI_DIRECT_DRAW, FALSE,
2939                       GDI_EVENT_MASK, event_mask,
2940                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
2941                       GDI_END);
2942
2943     if (gi == NULL)
2944       Error(ERR_EXIT, "cannot create gadget");
2945
2946     screen_gadget[id] = gi;
2947   }
2948 }
2949
2950 static void CreateScreenScrollbars()
2951 {
2952   int i;
2953
2954   for (i=0; i<NUM_SCREEN_SCROLLBARS; i++)
2955   {
2956     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
2957 #if !defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2958     int gfx_unpressed, gfx_pressed;
2959 #endif
2960     int x, y, width, height;
2961     int gd_x1, gd_x2, gd_y1, gd_y2;
2962     struct GadgetInfo *gi;
2963     int items_max, items_visible, item_position;
2964     unsigned long event_mask;
2965     int num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
2966     int id = scrollbar_info[i].gadget_id;
2967
2968     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
2969
2970     x = mSX + scrollbar_info[i].x + menu.scrollbar_xoffset;
2971     y = mSY + scrollbar_info[i].y;
2972     width  = scrollbar_info[i].width;
2973     height = scrollbar_info[i].height;
2974
2975     if (id == SCREEN_CTRL_ID_SCROLL_VERTICAL)
2976       height = (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE;
2977
2978     items_max = num_page_entries;
2979     items_visible = num_page_entries;
2980     item_position = 0;
2981
2982 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
2983     gd_bitmap_unpressed = *scrollbar_info[i].gfx_unpressed;
2984     gd_bitmap_pressed   = *scrollbar_info[i].gfx_pressed;
2985     gd_x1 = 0;
2986     gd_y1 = 0;
2987     gd_x2 = 0;
2988     gd_y2 = 0;
2989 #else
2990     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
2991     gfx_pressed   = scrollbar_info[i].gfx_pressed;
2992     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
2993     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
2994     gd_x1 = graphic_info[gfx_unpressed].src_x;
2995     gd_y1 = graphic_info[gfx_unpressed].src_y;
2996     gd_x2 = graphic_info[gfx_pressed].src_x;
2997     gd_y2 = graphic_info[gfx_pressed].src_y;
2998 #endif
2999
3000     gi = CreateGadget(GDI_CUSTOM_ID, id,
3001                       GDI_CUSTOM_TYPE_ID, i,
3002                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
3003                       GDI_X, x,
3004                       GDI_Y, y,
3005                       GDI_WIDTH, width,
3006                       GDI_HEIGHT, height,
3007                       GDI_TYPE, scrollbar_info[i].type,
3008                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
3009                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
3010                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
3011                       GDI_STATE, GD_BUTTON_UNPRESSED,
3012                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
3013                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
3014                       GDI_BORDER_SIZE, SC_BORDER_SIZE, SC_BORDER_SIZE,
3015                       GDI_DIRECT_DRAW, FALSE,
3016                       GDI_EVENT_MASK, event_mask,
3017                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
3018                       GDI_END);
3019
3020     if (gi == NULL)
3021       Error(ERR_EXIT, "cannot create gadget");
3022
3023     screen_gadget[id] = gi;
3024   }
3025 }
3026
3027 void CreateScreenGadgets()
3028 {
3029   int last_game_status = game_status;   /* save current game status */
3030
3031 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
3032   int i;
3033
3034   for (i=0; i < NUM_SCROLLBAR_BITMAPS; i++)
3035   {
3036     scrollbar_bitmap[i] = CreateBitmap(TILEX, TILEY, DEFAULT_DEPTH);
3037
3038     /* copy pointers to clip mask and GC */
3039     scrollbar_bitmap[i]->clip_mask =
3040       graphic_info[IMG_MENU_SCROLLBAR + i].clip_mask;
3041     scrollbar_bitmap[i]->stored_clip_gc =
3042       graphic_info[IMG_MENU_SCROLLBAR + i].clip_gc;
3043
3044     BlitBitmap(graphic_info[IMG_MENU_SCROLLBAR + i].bitmap,
3045                scrollbar_bitmap[i],
3046                graphic_info[IMG_MENU_SCROLLBAR + i].src_x,
3047                graphic_info[IMG_MENU_SCROLLBAR + i].src_y,
3048                TILEX, TILEY, 0, 0);
3049   }
3050 #endif
3051
3052   /* force LEVELS draw offset for scrollbar / scrollbutton gadgets */
3053   game_status = GAME_MODE_LEVELS;
3054
3055   CreateScreenScrollbuttons();
3056   CreateScreenScrollbars();
3057
3058   game_status = last_game_status;       /* restore current game status */
3059 }
3060
3061 void FreeScreenGadgets()
3062 {
3063   int i;
3064
3065 #if defined(TARGET_X11_NATIVE_PERFORMANCE_WORKAROUND)
3066   for (i=0; i < NUM_SCROLLBAR_BITMAPS; i++)
3067   {
3068     /* prevent freeing clip mask and GC twice */
3069     scrollbar_bitmap[i]->clip_mask = None;
3070     scrollbar_bitmap[i]->stored_clip_gc = None;
3071
3072     FreeBitmap(scrollbar_bitmap[i]);
3073   }
3074 #endif
3075
3076   for (i=0; i<NUM_SCREEN_GADGETS; i++)
3077     FreeGadget(screen_gadget[i]);
3078 }
3079
3080 void MapChooseTreeGadgets(TreeInfo *ti)
3081 {
3082   int num_entries = numTreeInfoInGroup(ti);
3083   int i;
3084
3085   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
3086     return;
3087
3088   for (i=0; i<NUM_SCREEN_GADGETS; i++)
3089     MapGadget(screen_gadget[i]);
3090 }
3091
3092 void UnmapChooseTreeGadgets()
3093 {
3094   int i;
3095
3096   for (i=0; i<NUM_SCREEN_GADGETS; i++)
3097     UnmapGadget(screen_gadget[i]);
3098 }
3099
3100 static void HandleScreenGadgets(struct GadgetInfo *gi)
3101 {
3102   int id = gi->custom_id;
3103
3104   if (game_status != GAME_MODE_LEVELS && game_status != GAME_MODE_SETUP)
3105     return;
3106
3107   switch (id)
3108   {
3109     case SCREEN_CTRL_ID_SCROLL_UP:
3110       if (game_status == GAME_MODE_LEVELS)
3111         HandleChooseLevel(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
3112       else if (game_status == GAME_MODE_SETUP)
3113         HandleSetupScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
3114       break;
3115
3116     case SCREEN_CTRL_ID_SCROLL_DOWN:
3117       if (game_status == GAME_MODE_LEVELS)
3118         HandleChooseLevel(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
3119       else if (game_status == GAME_MODE_SETUP)
3120         HandleSetupScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
3121       break;
3122
3123     case SCREEN_CTRL_ID_SCROLL_VERTICAL:
3124       if (game_status == GAME_MODE_LEVELS)
3125         HandleChooseLevel(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
3126       else if (game_status == GAME_MODE_SETUP)
3127         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
3128       break;
3129
3130     default:
3131       break;
3132   }
3133 }