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