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