403e4247a0b33e878a9d956b36ad938238dc817f
[rocksndiamonds.git] / src / screens.c
1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // screens.c
10 // ============================================================================
11
12 #include "libgame/libgame.h"
13
14 #include "screens.h"
15 #include "events.h"
16 #include "game.h"
17 #include "tools.h"
18 #include "editor.h"
19 #include "files.h"
20 #include "tape.h"
21 #include "anim.h"
22 #include "network.h"
23 #include "init.h"
24 #include "config.h"
25
26
27 #define DEBUG_JOYSTICKS         0
28
29
30 // screens on the info screen
31 #define INFO_MODE_MAIN                  0
32 #define INFO_MODE_TITLE                 1
33 #define INFO_MODE_ELEMENTS              2
34 #define INFO_MODE_MUSIC                 3
35 #define INFO_MODE_CREDITS               4
36 #define INFO_MODE_PROGRAM               5
37 #define INFO_MODE_VERSION               6
38 #define INFO_MODE_LEVELSET              7
39
40 #define MAX_INFO_MODES                  8
41
42 // screens on the setup screen
43 // (must match GFX_SPECIAL_ARG_SETUP_* values as defined in src/main.h)
44 // (should also match corresponding entries in src/conf_gfx.c)
45 #define SETUP_MODE_MAIN                 0
46 #define SETUP_MODE_GAME                 1
47 #define SETUP_MODE_ENGINES              2
48 #define SETUP_MODE_EDITOR               3
49 #define SETUP_MODE_GRAPHICS             4
50 #define SETUP_MODE_SOUND                5
51 #define SETUP_MODE_ARTWORK              6
52 #define SETUP_MODE_INPUT                7
53 #define SETUP_MODE_TOUCH                8
54 #define SETUP_MODE_SHORTCUTS            9
55 #define SETUP_MODE_SHORTCUTS_1          10
56 #define SETUP_MODE_SHORTCUTS_2          11
57 #define SETUP_MODE_SHORTCUTS_3          12
58 #define SETUP_MODE_SHORTCUTS_4          13
59 #define SETUP_MODE_SHORTCUTS_5          14
60
61 // sub-screens on the setup screen (generic)
62 #define SETUP_MODE_CHOOSE_ARTWORK       15
63 #define SETUP_MODE_CHOOSE_OTHER         16
64
65 // sub-screens on the setup screen (specific)
66 #define SETUP_MODE_CHOOSE_GAME_SPEED    17
67 #define SETUP_MODE_CHOOSE_SCROLL_DELAY  18
68 #define SETUP_MODE_CHOOSE_SNAPSHOT_MODE 19
69 #define SETUP_MODE_CHOOSE_WINDOW_SIZE   20
70 #define SETUP_MODE_CHOOSE_SCALING_TYPE  21
71 #define SETUP_MODE_CHOOSE_RENDERING     22
72 #define SETUP_MODE_CHOOSE_VSYNC         23
73 #define SETUP_MODE_CHOOSE_GRAPHICS      24
74 #define SETUP_MODE_CHOOSE_SOUNDS        25
75 #define SETUP_MODE_CHOOSE_MUSIC         26
76 #define SETUP_MODE_CHOOSE_VOLUME_SIMPLE 27
77 #define SETUP_MODE_CHOOSE_VOLUME_LOOPS  28
78 #define SETUP_MODE_CHOOSE_VOLUME_MUSIC  29
79 #define SETUP_MODE_CHOOSE_TOUCH_CONTROL 30
80 #define SETUP_MODE_CHOOSE_MOVE_DISTANCE 31
81 #define SETUP_MODE_CHOOSE_DROP_DISTANCE 32
82 #define SETUP_MODE_CHOOSE_TRANSPARENCY  33
83 #define SETUP_MODE_CHOOSE_GRID_XSIZE_0  34
84 #define SETUP_MODE_CHOOSE_GRID_YSIZE_0  35
85 #define SETUP_MODE_CHOOSE_GRID_XSIZE_1  36
86 #define SETUP_MODE_CHOOSE_GRID_YSIZE_1  37
87 #define SETUP_MODE_CONFIG_VIRT_BUTTONS  38
88
89 #define MAX_SETUP_MODES                 39
90
91 #define MAX_MENU_MODES                  MAX(MAX_INFO_MODES, MAX_SETUP_MODES)
92
93 // setup screen titles
94 #define STR_SETUP_MAIN                  "Setup"
95 #define STR_SETUP_GAME                  "Game & Menu"
96 #define STR_SETUP_ENGINES               "Game Engines"
97 #define STR_SETUP_EDITOR                "Editor"
98 #define STR_SETUP_GRAPHICS              "Graphics"
99 #define STR_SETUP_SOUND                 "Sound & Music"
100 #define STR_SETUP_ARTWORK               "Custom Artwork"
101 #define STR_SETUP_INPUT                 "Input Devices"
102 #define STR_SETUP_TOUCH                 "Touch Controls"
103 #define STR_SETUP_SHORTCUTS             "Key Shortcuts"
104 #define STR_SETUP_EXIT                  "Exit"
105 #define STR_SETUP_SAVE_AND_EXIT         "Save and Exit"
106
107 #define STR_SETUP_CHOOSE_GAME_SPEED     "Game Speed"
108 #define STR_SETUP_CHOOSE_SCROLL_DELAY   "Scroll Delay"
109 #define STR_SETUP_CHOOSE_SNAPSHOT_MODE  "Snapshot Mode"
110 #define STR_SETUP_CHOOSE_WINDOW_SIZE    "Window Scaling"
111 #define STR_SETUP_CHOOSE_SCALING_TYPE   "Anti-Aliasing"
112 #define STR_SETUP_CHOOSE_RENDERING      "Rendering Mode"
113 #define STR_SETUP_CHOOSE_VSYNC          "VSync Mode"
114 #define STR_SETUP_CHOOSE_VOLUME_SIMPLE  "Sound Volume"
115 #define STR_SETUP_CHOOSE_VOLUME_LOOPS   "Loops Volume"
116 #define STR_SETUP_CHOOSE_VOLUME_MUSIC   "Music Volume"
117 #define STR_SETUP_CHOOSE_TOUCH_CONTROL  "Control Type"
118 #define STR_SETUP_CHOOSE_MOVE_DISTANCE  "Move Distance"
119 #define STR_SETUP_CHOOSE_DROP_DISTANCE  "Drop Distance"
120 #define STR_SETUP_CHOOSE_TRANSPARENCY   "Transparency"
121 #define STR_SETUP_CHOOSE_GRID_XSIZE_0   "Horiz. Buttons"
122 #define STR_SETUP_CHOOSE_GRID_YSIZE_0   "Vert. Buttons"
123 #define STR_SETUP_CHOOSE_GRID_XSIZE_1   "Horiz. Buttons"
124 #define STR_SETUP_CHOOSE_GRID_YSIZE_1   "Vert. Buttons"
125
126 // for input setup functions
127 #define SETUPINPUT_SCREEN_POS_START     0
128 #define SETUPINPUT_SCREEN_POS_EMPTY1    3
129 #define SETUPINPUT_SCREEN_POS_EMPTY2    12
130 #define SETUPINPUT_SCREEN_POS_END       13
131
132 #define MENU_SETUP_FONT_TITLE           FONT_TEXT_1
133 #define MENU_SETUP_FONT_TEXT            FONT_TITLE_2
134
135 #define MAX_SETUP_TEXT_INPUT_LEN        28
136
137 // for various menu stuff
138 #define MENU_SCREEN_START_XPOS          1
139 #define MENU_SCREEN_START_YPOS          2
140 #define MENU_SCREEN_VALUE_XPOS          (SCR_FIELDX - 3)
141 #define MENU_SCREEN_MAX_XPOS            (SCR_FIELDX - 1)
142 #define MENU_TITLE1_YPOS                8
143 #define MENU_TITLE2_YPOS                46
144 #define MENU_INFO_FONT_TITLE            FONT_TEXT_1
145 #define MENU_INFO_FONT_HEAD             FONT_TEXT_2
146 #define MENU_INFO_FONT_TEXT             FONT_TEXT_3
147 #define MENU_INFO_FONT_FOOT             FONT_TEXT_4
148 #define MENU_INFO_SPACE_HEAD            (menu.headline2_spacing_info[info_mode])
149 #define MENU_SCREEN_INFO_SPACE_LEFT     (menu.left_spacing_info[info_mode])
150 #define MENU_SCREEN_INFO_SPACE_RIGHT    (menu.right_spacing_info[info_mode])
151 #define MENU_SCREEN_INFO_SPACE_TOP      (menu.top_spacing_info[info_mode])
152 #define MENU_SCREEN_INFO_SPACE_BOTTOM   (menu.bottom_spacing_info[info_mode])
153 #define MENU_SCREEN_INFO_YSTART1        MENU_SCREEN_INFO_SPACE_TOP
154 #define MENU_SCREEN_INFO_YSTART2        (MENU_SCREEN_INFO_YSTART1 +            \
155                                          getMenuTextStep(MENU_INFO_SPACE_HEAD, \
156                                                          MENU_INFO_FONT_TITLE))
157 #define MENU_SCREEN_INFO_YSTEP          (TILEY + 4)
158 #define MENU_SCREEN_INFO_YBOTTOM        (SYSIZE - MENU_SCREEN_INFO_SPACE_BOTTOM)
159 #define MENU_SCREEN_INFO_YSIZE          (MENU_SCREEN_INFO_YBOTTOM -     \
160                                          MENU_SCREEN_INFO_YSTART2 -     \
161                                          TILEY / 2)
162 #define MAX_INFO_ELEMENTS_ON_SCREEN     128
163 #define STD_INFO_ELEMENTS_ON_SCREEN     (MENU_SCREEN_INFO_YSIZE /       \
164                                          MENU_SCREEN_INFO_YSTEP)
165 #define NUM_INFO_ELEMENTS_FROM_CONF     \
166   (menu.list_size_info[GFX_SPECIAL_ARG_INFO_ELEMENTS] > 0 ?             \
167    menu.list_size_info[GFX_SPECIAL_ARG_INFO_ELEMENTS] :                 \
168    MAX_MENU_ENTRIES_ON_SCREEN)
169 #define NUM_INFO_ELEMENTS_ON_SCREEN     MIN(MIN(STD_INFO_ELEMENTS_ON_SCREEN, \
170                                                 MAX_INFO_ELEMENTS_ON_SCREEN), \
171                                             NUM_INFO_ELEMENTS_FROM_CONF)
172 #define MAX_MENU_ENTRIES_ON_SCREEN      (SCR_FIELDY - MENU_SCREEN_START_YPOS)
173 #define MAX_MENU_TEXT_LENGTH_BIG        13
174 #define MAX_MENU_TEXT_LENGTH_MEDIUM     (MAX_MENU_TEXT_LENGTH_BIG * 2)
175
176 // screen gadget identifiers
177 #define SCREEN_CTRL_ID_PREV_LEVEL       0
178 #define SCREEN_CTRL_ID_NEXT_LEVEL       1
179 #define SCREEN_CTRL_ID_FIRST_LEVEL      2
180 #define SCREEN_CTRL_ID_LAST_LEVEL       3
181 #define SCREEN_CTRL_ID_LEVEL_NUMBER     4
182 #define SCREEN_CTRL_ID_PREV_PLAYER      5
183 #define SCREEN_CTRL_ID_NEXT_PLAYER      6
184 #define SCREEN_CTRL_ID_INSERT_SOLUTION  7
185 #define SCREEN_CTRL_ID_PLAY_SOLUTION    8
186 #define SCREEN_CTRL_ID_SWITCH_ECS_AGA   9
187 #define SCREEN_CTRL_ID_TOUCH_PREV_PAGE  10
188 #define SCREEN_CTRL_ID_TOUCH_NEXT_PAGE  11
189 #define SCREEN_CTRL_ID_TOUCH_PREV_PAGE2 12
190 #define SCREEN_CTRL_ID_TOUCH_NEXT_PAGE2 13
191 #define SCREEN_CTRL_ID_SCROLL_UP        14
192 #define SCREEN_CTRL_ID_SCROLL_DOWN      15
193 #define SCREEN_CTRL_ID_SCROLL_VERTICAL  16
194 #define SCREEN_CTRL_ID_NETWORK_SERVER   17
195
196 #define NUM_SCREEN_GADGETS              18
197
198 #define NUM_SCREEN_MENUBUTTONS          14
199 #define NUM_SCREEN_SCROLLBUTTONS        2
200 #define NUM_SCREEN_SCROLLBARS           1
201 #define NUM_SCREEN_TEXTINPUT            1
202
203 #define SCREEN_MASK_MAIN                (1 << 0)
204 #define SCREEN_MASK_MAIN_HAS_SOLUTION   (1 << 1)
205 #define SCREEN_MASK_INPUT               (1 << 2)
206 #define SCREEN_MASK_TOUCH               (1 << 3)
207 #define SCREEN_MASK_TOUCH2              (1 << 4)
208
209 // graphic position and size values for buttons and scrollbars
210 #define SC_MENUBUTTON_XSIZE             TILEX
211 #define SC_MENUBUTTON_YSIZE             TILEY
212
213 #define SC_SCROLLBUTTON_XSIZE           TILEX
214 #define SC_SCROLLBUTTON_YSIZE           TILEY
215
216 #define SC_SCROLLBAR_XPOS               (SXSIZE - SC_SCROLLBUTTON_XSIZE)
217
218 #define SC_SCROLL_VERTICAL_XSIZE        SC_SCROLLBUTTON_XSIZE
219 #define SC_SCROLL_VERTICAL_YSIZE        ((MAX_MENU_ENTRIES_ON_SCREEN - 2) * \
220                                          SC_SCROLLBUTTON_YSIZE)
221
222 #define SC_SCROLL_UP_XPOS               SC_SCROLLBAR_XPOS
223 #define SC_SCROLL_UP_YPOS               (2 * SC_SCROLLBUTTON_YSIZE)
224
225 #define SC_SCROLL_VERTICAL_XPOS         SC_SCROLLBAR_XPOS
226 #define SC_SCROLL_VERTICAL_YPOS         (SC_SCROLL_UP_YPOS + \
227                                          SC_SCROLLBUTTON_YSIZE)
228
229 #define SC_SCROLL_DOWN_XPOS             SC_SCROLLBAR_XPOS
230 #define SC_SCROLL_DOWN_YPOS             (SC_SCROLL_VERTICAL_YPOS + \
231                                          SC_SCROLL_VERTICAL_YSIZE)
232
233 #define SC_BORDER_SIZE                  14
234
235
236 // forward declarations of internal functions
237 static void HandleScreenGadgets(struct GadgetInfo *);
238 static void HandleSetupScreen_Generic(int, int, int, int, int);
239 static void HandleSetupScreen_Input(int, int, int, int, int);
240 static void CustomizeKeyboard(int);
241 static void ConfigureJoystick(int);
242 static void ConfigureVirtualButtons(void);
243 static void execSetupGame(void);
244 static void execSetupEngines(void);
245 static void execSetupGraphics(void);
246 static void execSetupSound(void);
247 static void execSetupTouch(void);
248 static void execSetupArtwork(void);
249 static void HandleChooseTree(int, int, int, int, int, TreeInfo **);
250
251 static void DrawChoosePlayerName(void);
252 static void DrawChooseLevelSet(void);
253 static void DrawChooseLevelNr(void);
254 static void DrawInfoScreen(void);
255 static void DrawSetupScreen(void);
256 static void DrawTypeName(void);
257
258 static void DrawInfoScreen_NotAvailable(char *, char *);
259 static void DrawInfoScreen_HelpAnim(int, int, boolean);
260 static void DrawInfoScreen_HelpText(int, int, int, int);
261 static void HandleInfoScreen_Main(int, int, int, int, int);
262 static void HandleInfoScreen_TitleScreen(int);
263 static void HandleInfoScreen_Elements(int);
264 static void HandleInfoScreen_Music(int);
265 static void HandleInfoScreen_Credits(int);
266 static void HandleInfoScreen_Program(int);
267 static void HandleInfoScreen_Version(int);
268
269 static void ModifyGameSpeedIfNeeded(void);
270 static void DisableVsyncIfNeeded(void);
271
272 static void MapScreenMenuGadgets(int);
273 static void UnmapScreenMenuGadgets(int);
274 static void MapScreenGadgets(int);
275 static void MapScreenTreeGadgets(TreeInfo *);
276
277 static void UpdateScreenMenuGadgets(int, boolean);
278
279 static struct GadgetInfo *screen_gadget[NUM_SCREEN_GADGETS];
280
281 static int info_mode = INFO_MODE_MAIN;
282 static int setup_mode = SETUP_MODE_MAIN;
283
284 static TreeInfo *window_sizes = NULL;
285 static TreeInfo *window_size_current = NULL;
286
287 static TreeInfo *scaling_types = NULL;
288 static TreeInfo *scaling_type_current = NULL;
289
290 static TreeInfo *rendering_modes = NULL;
291 static TreeInfo *rendering_mode_current = NULL;
292
293 static TreeInfo *vsync_modes = NULL;
294 static TreeInfo *vsync_mode_current = NULL;
295
296 static TreeInfo *scroll_delays = NULL;
297 static TreeInfo *scroll_delay_current = NULL;
298
299 static TreeInfo *snapshot_modes = NULL;
300 static TreeInfo *snapshot_mode_current = NULL;
301
302 static TreeInfo *game_speeds_normal = NULL;
303 static TreeInfo *game_speeds_extended = NULL;
304 static TreeInfo *game_speeds = NULL;
305 static TreeInfo *game_speed_current = NULL;
306
307 static TreeInfo *volumes_simple = NULL;
308 static TreeInfo *volume_simple_current = NULL;
309
310 static TreeInfo *volumes_loops = NULL;
311 static TreeInfo *volume_loops_current = NULL;
312
313 static TreeInfo *volumes_music = NULL;
314 static TreeInfo *volume_music_current = NULL;
315
316 static TreeInfo *touch_controls = NULL;
317 static TreeInfo *touch_control_current = NULL;
318
319 static TreeInfo *move_distances = NULL;
320 static TreeInfo *move_distance_current = NULL;
321
322 static TreeInfo *drop_distances = NULL;
323 static TreeInfo *drop_distance_current = NULL;
324
325 static TreeInfo *transparencies = NULL;
326 static TreeInfo *transparency_current = NULL;
327
328 static TreeInfo *grid_sizes[2][2] = { { NULL, NULL }, { NULL, NULL } };
329 static TreeInfo *grid_size_current[2][2] = { { NULL, NULL }, { NULL, NULL } };
330
331 static TreeInfo *player_name = NULL;
332 static TreeInfo *player_name_current = NULL;
333
334 static TreeInfo *level_number = NULL;
335 static TreeInfo *level_number_current = NULL;
336
337 static struct ValueTextInfo window_sizes_list[] =
338 {
339   {     50,     "50 %"                          },
340   {     80,     "80 %"                          },
341   {     90,     "90 %"                          },
342   {     100,    "100 % (Default)"               },
343   {     110,    "110 %"                         },
344   {     120,    "120 %"                         },
345   {     130,    "130 %"                         },
346   {     140,    "140 %"                         },
347   {     150,    "150 %"                         },
348   {     200,    "200 %"                         },
349   {     250,    "250 %"                         },
350   {     300,    "300 %"                         },
351
352   {     -1,     NULL                            },
353 };
354
355 static struct StringValueTextInfo scaling_types_list[] =
356 {
357   {     SCALING_QUALITY_NEAREST, "Off"          },
358   {     SCALING_QUALITY_LINEAR,  "Linear"       },
359   {     SCALING_QUALITY_BEST,    "Anisotropic"  },
360
361   {     NULL,                    NULL           },
362 };
363
364 static struct StringValueTextInfo rendering_modes_list[] =
365 {
366   {     STR_SPECIAL_RENDERING_OFF,      "Off (May show artifacts, fast)" },
367   {     STR_SPECIAL_RENDERING_BITMAP,   "Bitmap/Texture mode (slower)"   },
368 #if DEBUG
369   // this mode may work under certain conditions, but does not work on Windows
370   {     STR_SPECIAL_RENDERING_TARGET,   "Target Texture mode (slower)"   },
371 #endif
372   {     STR_SPECIAL_RENDERING_DOUBLE,   "Double Texture mode (slower)"   },
373
374   {     NULL,                            NULL                            },
375 };
376
377 static struct StringValueTextInfo vsync_modes_list[] =
378 {
379   {     STR_VSYNC_MODE_OFF,             "Off"           },
380   {     STR_VSYNC_MODE_NORMAL,          "Normal"        },
381   {     STR_VSYNC_MODE_ADAPTIVE,        "Adaptive"      },
382
383   {     NULL,                            NULL           },
384 };
385
386 static struct ValueTextInfo game_speeds_list_normal[] =
387 {
388   {     30,     "Very Slow"                     },
389   {     25,     "Slow"                          },
390   {     20,     "Normal"                        },
391   {     15,     "Fast"                          },
392   {     10,     "Very Fast"                     },
393
394   {     -1,     NULL                            },
395 };
396
397 static struct ValueTextInfo game_speeds_list_extended[] =
398 {
399   {     1000,   "1 fps (Extremely Slow)"        },
400   {     500,    "2 fps"                         },
401   {     200,    "5 fps"                         },
402   {     100,    "10 fps"                        },
403   {     50,     "20 fps"                        },
404   {     29,     "35 fps (Original Supaplex)"    },
405   {     25,     "40 fps"                        },
406   {     20,     "50 fps (=== Normal Speed ===)" },
407   {     16,     "60 fps (60 Hz VSync Speed)"    },
408   {     14,     "70 fps (Maximum Supaplex)"     },
409   {     10,     "100 fps"                       },
410   {     5,      "200 fps"                       },
411   {     2,      "500 fps"                       },
412   {     1,      "1000 fps (Extremely Fast)"     },
413
414   {     -1,     NULL                            },
415 };
416
417 static struct ValueTextInfo *game_speeds_list;
418
419 static struct ValueTextInfo scroll_delays_list[] =
420 {
421   {     0,      "0 Tiles (No Scroll Delay)"     },
422   {     1,      "1 Tile"                        },
423   {     2,      "2 Tiles"                       },
424   {     3,      "3 Tiles (Default)"             },
425   {     4,      "4 Tiles"                       },
426   {     5,      "5 Tiles"                       },
427   {     6,      "6 Tiles"                       },
428   {     7,      "7 Tiles"                       },
429   {     8,      "8 Tiles (Maximum Scroll Delay)"},
430
431   {     -1,     NULL                            },
432 };
433
434 static struct StringValueTextInfo snapshot_modes_list[] =
435 {
436   {     STR_SNAPSHOT_MODE_OFF,                  "Off"           },
437   {     STR_SNAPSHOT_MODE_EVERY_STEP,           "Every Step"    },
438   {     STR_SNAPSHOT_MODE_EVERY_MOVE,           "Every Move"    },
439   {     STR_SNAPSHOT_MODE_EVERY_COLLECT,        "Every Collect" },
440
441   {     NULL,                                   NULL            },
442 };
443
444 static struct ValueTextInfo volumes_list[] =
445 {
446   {     0,      "0 %"                           },
447   {     1,      "1 %"                           },
448   {     2,      "2 %"                           },
449   {     5,      "5 %"                           },
450   {     10,     "10 %"                          },
451   {     20,     "20 %"                          },
452   {     30,     "30 %"                          },
453   {     40,     "40 %"                          },
454   {     50,     "50 %"                          },
455   {     60,     "60 %"                          },
456   {     70,     "70 %"                          },
457   {     80,     "80 %"                          },
458   {     90,     "90 %"                          },
459   {     100,    "100 %"                         },
460
461   {     -1,     NULL                            },
462 };
463
464 static struct StringValueTextInfo touch_controls_list[] =
465 {
466   {     TOUCH_CONTROL_OFF,              "Off"                   },
467   {     TOUCH_CONTROL_VIRTUAL_BUTTONS,  "Virtual Buttons"       },
468   {     TOUCH_CONTROL_WIPE_GESTURES,    "Wipe Gestures"         },
469   {     TOUCH_CONTROL_FOLLOW_FINGER,    "Follow Finger"         },
470
471   {     NULL,                           NULL                    },
472 };
473
474 static struct ValueTextInfo distances_list[] =
475 {
476   {     1,      "1 %"                           },
477   {     2,      "2 %"                           },
478   {     3,      "3 %"                           },
479   {     4,      "4 %"                           },
480   {     5,      "5 %"                           },
481   {     10,     "10 %"                          },
482   {     15,     "15 %"                          },
483   {     20,     "20 %"                          },
484   {     25,     "25 %"                          },
485
486   {     -1,     NULL                            },
487 };
488
489 static struct ValueTextInfo transparencies_list[] =
490 {
491   {     0,      "0 % (Opaque)"                  },
492   {     10,     "10 %"                          },
493   {     20,     "20 %"                          },
494   {     30,     "30 %"                          },
495   {     40,     "40 %"                          },
496   {     50,     "50 %"                          },
497   {     60,     "60 %"                          },
498   {     70,     "70 %"                          },
499   {     80,     "80 %"                          },
500   {     90,     "90 %"                          },
501   {     100,    "100 % (Invisible)"             },
502
503   {     -1,     NULL                            },
504 };
505
506 static struct ValueTextInfo grid_sizes_list[] =
507 {
508   {     3,      "3"                             },
509   {     4,      "4"                             },
510   {     5,      "5"                             },
511   {     6,      "6"                             },
512   {     7,      "7"                             },
513   {     8,      "8"                             },
514   {     9,      "9"                             },
515   {     10,     "10"                            },
516   {     11,     "11"                            },
517   {     12,     "12"                            },
518   {     13,     "13"                            },
519   {     14,     "14"                            },
520   {     15,     "15"                            },
521   {     16,     "16"                            },
522   {     17,     "17"                            },
523   {     18,     "18"                            },
524   {     19,     "19"                            },
525   {     20,     "20"                            },
526   {     21,     "21"                            },
527   {     22,     "22"                            },
528   {     23,     "23"                            },
529   {     24,     "24"                            },
530   {     25,     "25"                            },
531   {     26,     "26"                            },
532   {     27,     "27"                            },
533   {     28,     "28"                            },
534   {     29,     "29"                            },
535   {     30,     "30"                            },
536   {     31,     "31"                            },
537   {     32,     "32"                            },
538
539   {     -1,     NULL                            },
540 };
541
542 static int align_xoffset = 0;
543 static int align_yoffset = 0;
544
545 #define DRAW_MODE(s)            ((s) >= GAME_MODE_MAIN &&               \
546                                  (s) <= GAME_MODE_SETUP ? (s) :         \
547                                  (s) == GAME_MODE_PSEUDO_TYPENAME ?     \
548                                  GAME_MODE_MAIN :                       \
549                                  (s) == GAME_MODE_PSEUDO_TYPENAMES ?    \
550                                  GAME_MODE_NAMES : GAME_MODE_DEFAULT)
551
552 // (there are no draw offset definitions needed for INFO_MODE_TITLE)
553 #define DRAW_MODE_INFO(i)       ((i) >= INFO_MODE_TITLE &&              \
554                                  (i) <= INFO_MODE_LEVELSET ? (i) :      \
555                                  INFO_MODE_MAIN)
556
557 #define DRAW_MODE_SETUP(i)      ((i) >= SETUP_MODE_MAIN &&              \
558                                  (i) <= SETUP_MODE_SHORTCUTS_5 ? (i) :  \
559                                  (i) >= SETUP_MODE_CHOOSE_GRAPHICS &&   \
560                                  (i) <= SETUP_MODE_CHOOSE_MUSIC ?       \
561                                  SETUP_MODE_CHOOSE_ARTWORK :            \
562                                  SETUP_MODE_CHOOSE_OTHER)
563
564 #define DRAW_XOFFSET_INFO(i)    (DRAW_MODE_INFO(i) == INFO_MODE_MAIN ?  \
565                                  menu.draw_xoffset[GAME_MODE_INFO] :    \
566                                  menu.draw_xoffset_info[DRAW_MODE_INFO(i)])
567 #define DRAW_YOFFSET_INFO(i)    (DRAW_MODE_INFO(i) == INFO_MODE_MAIN ?  \
568                                  menu.draw_yoffset[GAME_MODE_INFO] :    \
569                                  menu.draw_yoffset_info[DRAW_MODE_INFO(i)])
570 #define EXTRA_SPACING_INFO(i)   (DRAW_MODE_INFO(i) == INFO_MODE_MAIN ? \
571                                  menu.extra_spacing[GAME_MODE_INFO] :   \
572                                  menu.extra_spacing_info[DRAW_MODE_INFO(i)])
573
574 #define DRAW_XOFFSET_SETUP(i)   (DRAW_MODE_SETUP(i) == SETUP_MODE_MAIN ? \
575                                  menu.draw_xoffset[GAME_MODE_SETUP] :   \
576                                  menu.draw_xoffset_setup[DRAW_MODE_SETUP(i)])
577 #define DRAW_YOFFSET_SETUP(i)   (DRAW_MODE_SETUP(i) == SETUP_MODE_MAIN ? \
578                                  menu.draw_yoffset[GAME_MODE_SETUP] :   \
579                                  menu.draw_yoffset_setup[DRAW_MODE_SETUP(i)])
580 #define EXTRA_SPACING_SETUP(i)  (DRAW_MODE_SETUP(i) == SETUP_MODE_MAIN ? \
581                                  menu.extra_spacing[GAME_MODE_SETUP] :  \
582                                  menu.extra_spacing_setup[DRAW_MODE_SETUP(i)])
583
584 #define DRAW_XOFFSET(s)         ((s) == GAME_MODE_INFO ?                \
585                                  DRAW_XOFFSET_INFO(info_mode) :         \
586                                  (s) == GAME_MODE_SETUP ?               \
587                                  DRAW_XOFFSET_SETUP(setup_mode) :       \
588                                  menu.draw_xoffset[DRAW_MODE(s)])
589 #define DRAW_YOFFSET(s)         ((s) == GAME_MODE_INFO ?                \
590                                  DRAW_YOFFSET_INFO(info_mode) :         \
591                                  (s) == GAME_MODE_SETUP ?               \
592                                  DRAW_YOFFSET_SETUP(setup_mode) :       \
593                                  menu.draw_yoffset[DRAW_MODE(s)])
594 #define EXTRA_SPACING(s)        ((s) == GAME_MODE_INFO ?                \
595                                  EXTRA_SPACING_INFO(info_mode) :        \
596                                  (s) == GAME_MODE_SETUP ?               \
597                                  EXTRA_SPACING_SETUP(setup_mode) :      \
598                                  menu.extra_spacing[DRAW_MODE(s)])
599
600 #define mSX                     (SX + DRAW_XOFFSET(game_status))
601 #define mSY                     (SY + DRAW_YOFFSET(game_status))
602
603 #define amSX                    (mSX + align_xoffset)
604 #define amSY                    (mSY + align_yoffset)
605
606 #define NUM_MENU_ENTRIES_ON_SCREEN (menu.list_size[game_status] > 2 ?   \
607                                     menu.list_size[game_status] :       \
608                                     MAX_MENU_ENTRIES_ON_SCREEN)
609
610 #define IN_VIS_MENU(x, y)       IN_FIELD(x, y, SCR_FIELDX,              \
611                                          NUM_MENU_ENTRIES_ON_SCREEN)
612
613
614 // title display and control definitions
615
616 #define MAX_NUM_TITLE_SCREENS   (2 * MAX_NUM_TITLE_IMAGES +             \
617                                  2 * MAX_NUM_TITLE_MESSAGES)
618
619 #define NO_DIRECT_LEVEL_SELECT  (-1)
620
621
622 static int num_title_screens = 0;
623
624 struct TitleControlInfo
625 {
626   boolean is_image;
627   boolean initial;
628   boolean first;
629   int local_nr;
630   int sort_priority;
631 };
632
633 struct TitleControlInfo title_controls[MAX_NUM_TITLE_SCREENS];
634
635 // main menu display and control definitions
636
637 #define MAIN_CONTROL_NAME                       0
638 #define MAIN_CONTROL_LEVELS                     1
639 #define MAIN_CONTROL_SCORES                     2
640 #define MAIN_CONTROL_EDITOR                     3
641 #define MAIN_CONTROL_INFO                       4
642 #define MAIN_CONTROL_GAME                       5
643 #define MAIN_CONTROL_SETUP                      6
644 #define MAIN_CONTROL_QUIT                       7
645 #define MAIN_CONTROL_PREV_LEVEL                 8
646 #define MAIN_CONTROL_NEXT_LEVEL                 9
647 #define MAIN_CONTROL_FIRST_LEVEL                10
648 #define MAIN_CONTROL_LAST_LEVEL                 11
649 #define MAIN_CONTROL_LEVEL_NUMBER               12
650 #define MAIN_CONTROL_LEVEL_INFO_1               13
651 #define MAIN_CONTROL_LEVEL_INFO_2               14
652 #define MAIN_CONTROL_LEVEL_NAME                 15
653 #define MAIN_CONTROL_LEVEL_AUTHOR               16
654 #define MAIN_CONTROL_LEVEL_YEAR                 17
655 #define MAIN_CONTROL_LEVEL_IMPORTED_FROM        18
656 #define MAIN_CONTROL_LEVEL_IMPORTED_BY          19
657 #define MAIN_CONTROL_LEVEL_TESTED_BY            20
658 #define MAIN_CONTROL_TITLE_1                    21
659 #define MAIN_CONTROL_TITLE_2                    22
660 #define MAIN_CONTROL_TITLE_3                    23
661
662 static char str_main_text_name[10];
663 static char str_main_text_first_level[10];
664 static char str_main_text_last_level[10];
665 static char str_main_text_level_number[10];
666
667 static char network_server_hostname[MAX_SETUP_TEXT_INPUT_LEN + 1];
668
669 static char *main_text_name                     = str_main_text_name;
670 static char *main_text_first_level              = str_main_text_first_level;
671 static char *main_text_last_level               = str_main_text_last_level;
672 static char *main_text_level_number             = str_main_text_level_number;
673 static char *main_text_levels                   = "Levelset";
674 static char *main_text_scores                   = "Hall Of Fame";
675 static char *main_text_editor                   = "Level Creator";
676 static char *main_text_info                     = "Info Screen";
677 static char *main_text_game                     = "Start Game";
678 static char *main_text_setup                    = "Setup";
679 static char *main_text_quit                     = "Quit";
680 static char *main_text_level_name               = level.name;
681 static char *main_text_level_author             = level.author;
682 static char *main_text_level_year               = NULL;
683 static char *main_text_level_imported_from      = NULL;
684 static char *main_text_level_imported_by        = NULL;
685 static char *main_text_level_tested_by          = NULL;
686 static char *main_text_title_1                  = NULL;
687 static char *main_text_title_2                  = NULL;
688 static char *main_text_title_3                  = NULL;
689
690 extern char debug_xsn_mode[];
691
692 struct MainControlInfo
693 {
694   int nr;
695
696   struct MenuPosInfo *pos_button;
697   int button_graphic;
698
699   struct TextPosInfo *pos_text;
700   char **text;
701
702   struct TextPosInfo *pos_input;
703   char **input;
704 };
705
706 static struct MainControlInfo main_controls[] =
707 {
708   {
709     MAIN_CONTROL_NAME,
710     &menu.main.button.name,             IMG_MENU_BUTTON_NAME,
711     &menu.main.text.name,               &main_text_name,
712     &menu.main.input.name,              &setup.player_name,
713   },
714   {
715     MAIN_CONTROL_LEVELS,
716     &menu.main.button.levels,           IMG_MENU_BUTTON_LEVELS,
717     &menu.main.text.levels,             &main_text_levels,
718     NULL,                               NULL,
719   },
720   {
721     MAIN_CONTROL_SCORES,
722     &menu.main.button.scores,           IMG_MENU_BUTTON_SCORES,
723     &menu.main.text.scores,             &main_text_scores,
724     NULL,                               NULL,
725   },
726   {
727     MAIN_CONTROL_EDITOR,
728     &menu.main.button.editor,           IMG_MENU_BUTTON_EDITOR,
729     &menu.main.text.editor,             &main_text_editor,
730     NULL,                               NULL,
731   },
732   {
733     MAIN_CONTROL_INFO,
734     &menu.main.button.info,             IMG_MENU_BUTTON_INFO,
735     &menu.main.text.info,               &main_text_info,
736     NULL,                               NULL,
737   },
738   {
739     MAIN_CONTROL_GAME,
740     &menu.main.button.game,             IMG_MENU_BUTTON_GAME,
741     &menu.main.text.game,               &main_text_game,
742     NULL,                               NULL,
743   },
744   {
745     MAIN_CONTROL_SETUP,
746     &menu.main.button.setup,            IMG_MENU_BUTTON_SETUP,
747     &menu.main.text.setup,              &main_text_setup,
748     NULL,                               NULL,
749   },
750   {
751     MAIN_CONTROL_QUIT,
752     &menu.main.button.quit,             IMG_MENU_BUTTON_QUIT,
753     &menu.main.text.quit,               &main_text_quit,
754     NULL,                               NULL,
755   },
756   {
757     MAIN_CONTROL_PREV_LEVEL,
758     NULL,                               -1,
759     NULL,                               NULL,
760     NULL,                               NULL,
761   },
762   {
763     MAIN_CONTROL_NEXT_LEVEL,
764     NULL,                               -1,
765     NULL,                               NULL,
766     NULL,                               NULL,
767   },
768   {
769     MAIN_CONTROL_FIRST_LEVEL,
770     NULL,                               -1,
771     &menu.main.text.first_level,        &main_text_first_level,
772     NULL,                               NULL,
773   },
774   {
775     MAIN_CONTROL_LAST_LEVEL,
776     NULL,                               -1,
777     &menu.main.text.last_level,         &main_text_last_level,
778     NULL,                               NULL,
779   },
780   {
781     MAIN_CONTROL_LEVEL_NUMBER,
782     NULL,                               -1,
783     &menu.main.text.level_number,       &main_text_level_number,
784     NULL,                               NULL,
785   },
786   {
787     MAIN_CONTROL_LEVEL_INFO_1,
788     NULL,                               -1,
789     &menu.main.text.level_info_1,       NULL,
790     NULL,                               NULL,
791   },
792   {
793     MAIN_CONTROL_LEVEL_INFO_2,
794     NULL,                               -1,
795     &menu.main.text.level_info_2,       NULL,
796     NULL,                               NULL,
797   },
798   {
799     MAIN_CONTROL_LEVEL_NAME,
800     NULL,                               -1,
801     &menu.main.text.level_name,         &main_text_level_name,
802     NULL,                               NULL,
803   },
804   {
805     MAIN_CONTROL_LEVEL_AUTHOR,
806     NULL,                               -1,
807     &menu.main.text.level_author,       &main_text_level_author,
808     NULL,                               NULL,
809   },
810   {
811     MAIN_CONTROL_LEVEL_YEAR,
812     NULL,                               -1,
813     &menu.main.text.level_year,         &main_text_level_year,
814     NULL,                               NULL,
815   },
816   {
817     MAIN_CONTROL_LEVEL_IMPORTED_FROM,
818     NULL,                               -1,
819     &menu.main.text.level_imported_from, &main_text_level_imported_from,
820     NULL,                               NULL,
821   },
822   {
823     MAIN_CONTROL_LEVEL_IMPORTED_BY,
824     NULL,                               -1,
825     &menu.main.text.level_imported_by,  &main_text_level_imported_by,
826     NULL,                               NULL,
827   },
828   {
829     MAIN_CONTROL_LEVEL_TESTED_BY,
830     NULL,                               -1,
831     &menu.main.text.level_tested_by,    &main_text_level_tested_by,
832     NULL,                               NULL,
833   },
834   {
835     MAIN_CONTROL_TITLE_1,
836     NULL,                               -1,
837     &menu.main.text.title_1,            &main_text_title_1,
838     NULL,                               NULL,
839   },
840   {
841     MAIN_CONTROL_TITLE_2,
842     NULL,                               -1,
843     &menu.main.text.title_2,            &main_text_title_2,
844     NULL,                               NULL,
845   },
846   {
847     MAIN_CONTROL_TITLE_3,
848     NULL,                               -1,
849     &menu.main.text.title_3,            &main_text_title_3,
850     NULL,                               NULL,
851   },
852
853   {
854     -1,
855     NULL,                               -1,
856     NULL,                               NULL,
857     NULL,                               NULL,
858   }
859 };
860
861
862 static int getTitleScreenGraphic(int nr, boolean initial)
863 {
864   return (initial ? IMG_TITLESCREEN_INITIAL_1 : IMG_TITLESCREEN_1) + nr;
865 }
866
867 static struct TitleMessageInfo *getTitleMessageInfo(int nr, boolean initial)
868 {
869   return (initial ? &titlemessage_initial[nr] : &titlemessage[nr]);
870 }
871
872 #if 0
873 static int getTitleScreenGameMode(boolean initial)
874 {
875   return (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
876 }
877 #endif
878
879 static int getTitleMessageGameMode(boolean initial)
880 {
881   return (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
882 }
883
884 static int getTitleAnimMode(struct TitleControlInfo *tci)
885 {
886   int base = (tci->initial ? GAME_MODE_TITLE_INITIAL_1 : GAME_MODE_TITLE_1);
887
888   return base + tci->local_nr;
889 }
890
891 #if 0
892 static int getTitleScreenBackground(boolean initial)
893 {
894   return (initial ? IMG_BACKGROUND_TITLE_INITIAL : IMG_BACKGROUND_TITLE);
895 }
896 #endif
897
898 #if 0
899 static int getTitleMessageBackground(int nr, boolean initial)
900 {
901   return (initial ? IMG_BACKGROUND_TITLE_INITIAL : IMG_BACKGROUND_TITLE);
902 }
903 #endif
904
905 static int getTitleBackground(int nr, boolean initial, boolean is_image)
906 {
907   int base = (is_image ?
908               (initial ? IMG_BACKGROUND_TITLESCREEN_INITIAL_1 :
909                          IMG_BACKGROUND_TITLESCREEN_1) :
910               (initial ? IMG_BACKGROUND_TITLEMESSAGE_INITIAL_1 :
911                          IMG_BACKGROUND_TITLEMESSAGE_1));
912   int graphic_global = (initial ? IMG_BACKGROUND_TITLE_INITIAL :
913                                   IMG_BACKGROUND_TITLE);
914   int graphic_local = base + nr;
915
916   if (graphic_info[graphic_local].bitmap != NULL)
917     return graphic_local;
918
919   if (graphic_info[graphic_global].bitmap != NULL)
920     return graphic_global;
921
922   return IMG_UNDEFINED;
923 }
924
925 static int getTitleSound(struct TitleControlInfo *tci)
926 {
927   boolean is_image = tci->is_image;
928   int initial = tci->initial;
929   int nr = tci->local_nr;
930   int mode = (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
931   int base = (is_image ?
932               (initial ? SND_BACKGROUND_TITLESCREEN_INITIAL_1 :
933                          SND_BACKGROUND_TITLESCREEN_1) :
934               (initial ? SND_BACKGROUND_TITLEMESSAGE_INITIAL_1 :
935                          SND_BACKGROUND_TITLEMESSAGE_1));
936   int sound_global = menu.sound[mode];
937   int sound_local = base + nr;
938
939 #if 0
940   Debug("screens:getTitleSound", "%d, %d, %d: %d ['%s'], %d ['%s']",
941         nr, initial, is_image,
942         sound_global, getSoundListEntry(sound_global)->filename,
943         sound_local, getSoundListEntry(sound_local)->filename);
944 #endif
945
946   if (!strEqual(getSoundListEntry(sound_local)->filename, UNDEFINED_FILENAME))
947     return sound_local;
948
949   if (!strEqual(getSoundListEntry(sound_global)->filename, UNDEFINED_FILENAME))
950     return sound_global;
951
952   return SND_UNDEFINED;
953 }
954
955 static int getTitleMusic(struct TitleControlInfo *tci)
956 {
957   boolean is_image = tci->is_image;
958   int initial = tci->initial;
959   int nr = tci->local_nr;
960   int mode = (initial ? GAME_MODE_TITLE_INITIAL : GAME_MODE_TITLE);
961   int base = (is_image ?
962               (initial ? MUS_BACKGROUND_TITLESCREEN_INITIAL_1 :
963                          MUS_BACKGROUND_TITLESCREEN_1) :
964               (initial ? MUS_BACKGROUND_TITLEMESSAGE_INITIAL_1 :
965                          MUS_BACKGROUND_TITLEMESSAGE_1));
966   int music_global = menu.music[mode];
967   int music_local = base + nr;
968
969 #if 0
970   Debug("screens:getTitleMusic", "%d, %d, %d: %d ['%s'], %d ['%s']",
971         nr, initial, is_image,
972         music_global, getMusicListEntry(music_global)->filename,
973         music_local, getMusicListEntry(music_local)->filename);
974 #endif
975
976   if (!strEqual(getMusicListEntry(music_local)->filename, UNDEFINED_FILENAME))
977     return music_local;
978
979   if (!strEqual(getMusicListEntry(music_global)->filename, UNDEFINED_FILENAME))
980     return music_global;
981
982   return MUS_UNDEFINED;
983 }
984
985 static struct TitleFadingInfo getTitleFading(struct TitleControlInfo *tci)
986 {
987   boolean is_image = tci->is_image;
988   boolean initial = tci->initial;
989   boolean first = tci->first;
990   int nr = tci->local_nr;
991   struct TitleMessageInfo tmi;
992   struct TitleFadingInfo ti;
993
994   tmi = (is_image ? (initial ? (first ?
995                                 titlescreen_initial_first[nr] :
996                                 titlescreen_initial[nr])
997                              : (first ?
998                                 titlescreen_first[nr] :
999                                 titlescreen[nr]))
1000                   : (initial ? (first ?
1001                                 titlemessage_initial_first[nr] :
1002                                 titlemessage_initial[nr])
1003                              : (first ?
1004                                 titlemessage_first[nr] :
1005                                 titlemessage[nr])));
1006
1007   ti.fade_mode  = tmi.fade_mode;
1008   ti.fade_delay = tmi.fade_delay;
1009   ti.post_delay = tmi.post_delay;
1010   ti.auto_delay = tmi.auto_delay;
1011   ti.auto_delay_unit = tmi.auto_delay_unit;
1012
1013   return ti;
1014 }
1015
1016 static int compareTitleControlInfo(const void *object1, const void *object2)
1017 {
1018   const struct TitleControlInfo *tci1 = (struct TitleControlInfo *)object1;
1019   const struct TitleControlInfo *tci2 = (struct TitleControlInfo *)object2;
1020   int compare_result;
1021
1022   if (tci1->initial != tci2->initial)
1023     compare_result = (tci1->initial ? -1 : +1);
1024   else if (tci1->sort_priority != tci2->sort_priority)
1025     compare_result = tci1->sort_priority - tci2->sort_priority;
1026   else if (tci1->is_image != tci2->is_image)
1027     compare_result = (tci1->is_image ? -1 : +1);
1028   else
1029     compare_result = tci1->local_nr - tci2->local_nr;
1030
1031   return compare_result;
1032 }
1033
1034 static void InitializeTitleControlsExt_AddTitleInfo(boolean is_image,
1035                                                     boolean initial,
1036                                                     int nr, int sort_priority)
1037 {
1038   title_controls[num_title_screens].is_image = is_image;
1039   title_controls[num_title_screens].initial = initial;
1040   title_controls[num_title_screens].local_nr = nr;
1041   title_controls[num_title_screens].sort_priority = sort_priority;
1042
1043   title_controls[num_title_screens].first = FALSE;      // will be set later
1044
1045   num_title_screens++;
1046 }
1047
1048 static void InitializeTitleControls_CheckTitleInfo(boolean initial)
1049 {
1050   int i;
1051
1052   for (i = 0; i < MAX_NUM_TITLE_IMAGES; i++)
1053   {
1054     int graphic = getTitleScreenGraphic(i, initial);
1055     Bitmap *bitmap = graphic_info[graphic].bitmap;
1056     int sort_priority = graphic_info[graphic].sort_priority;
1057
1058     if (bitmap != NULL)
1059       InitializeTitleControlsExt_AddTitleInfo(TRUE, initial, i, sort_priority);
1060   }
1061
1062   for (i = 0; i < MAX_NUM_TITLE_MESSAGES; i++)
1063   {
1064     struct TitleMessageInfo *tmi = getTitleMessageInfo(i, initial);
1065     char *filename = getLevelSetTitleMessageFilename(i, initial);
1066     int sort_priority = tmi->sort_priority;
1067
1068     if (filename != NULL)
1069       InitializeTitleControlsExt_AddTitleInfo(FALSE, initial, i, sort_priority);
1070   }
1071 }
1072
1073 static void InitializeTitleControls(boolean show_title_initial)
1074 {
1075   num_title_screens = 0;
1076
1077   // 1st step: initialize title screens for game start (only when starting)
1078   if (show_title_initial)
1079     InitializeTitleControls_CheckTitleInfo(TRUE);
1080
1081   // 2nd step: initialize title screens for current level set
1082   InitializeTitleControls_CheckTitleInfo(FALSE);
1083
1084   // sort title screens according to sort_priority and title number
1085   qsort(title_controls, num_title_screens, sizeof(struct TitleControlInfo),
1086         compareTitleControlInfo);
1087
1088   // mark first title screen
1089   title_controls[0].first = TRUE;
1090 }
1091
1092 static boolean visibleMenuPos(struct MenuPosInfo *pos)
1093 {
1094   return (pos != NULL && pos->x != -1 && pos->y != -1);
1095 }
1096
1097 static boolean visibleTextPos(struct TextPosInfo *pos)
1098 {
1099   return (pos != NULL && pos->x != -1 && pos->y != -1);
1100 }
1101
1102 static void InitializeMainControls(void)
1103 {
1104   boolean local_team_mode = (!network.enabled && setup.team_mode);
1105   int i;
1106
1107   // set main control text values to dynamically determined values
1108   sprintf(main_text_name,         "%s",   local_team_mode ? "Team:" : "Name:");
1109
1110   strcpy(main_text_first_level,  int2str(leveldir_current->first_level,
1111                                          menu.main.text.first_level.size));
1112   strcpy(main_text_last_level,   int2str(leveldir_current->last_level,
1113                                          menu.main.text.last_level.size));
1114   strcpy(main_text_level_number, int2str(level_nr,
1115                                          menu.main.text.level_number.size));
1116
1117   main_text_level_year          = leveldir_current->year;
1118   main_text_level_imported_from = leveldir_current->imported_from;
1119   main_text_level_imported_by   = leveldir_current->imported_by;
1120   main_text_level_tested_by     = leveldir_current->tested_by;
1121
1122   main_text_title_1 = getConfigProgramTitleString();
1123   main_text_title_2 = getConfigProgramCopyrightString();
1124   main_text_title_3 = getConfigProgramCompanyString();
1125
1126   // set main control screen positions to dynamically determined values
1127   for (i = 0; main_controls[i].nr != -1; i++)
1128   {
1129     struct MainControlInfo *mci = &main_controls[i];
1130     int nr                         = mci->nr;
1131     struct MenuPosInfo *pos_button = mci->pos_button;
1132     struct TextPosInfo *pos_text   = mci->pos_text;
1133     struct TextPosInfo *pos_input  = mci->pos_input;
1134     char *text                     = (mci->text  ? *mci->text  : NULL);
1135     char *input                    = (mci->input ? *mci->input : NULL);
1136     int button_graphic             = mci->button_graphic;
1137     int font_text                  = (pos_text  ? pos_text->font  : -1);
1138     int font_input                 = (pos_input ? pos_input->font : -1);
1139
1140     int font_text_width   = (font_text  != -1 ? getFontWidth(font_text)   : 0);
1141     int font_text_height  = (font_text  != -1 ? getFontHeight(font_text)  : 0);
1142     int font_input_width  = (font_input != -1 ? getFontWidth(font_input)  : 0);
1143     int font_input_height = (font_input != -1 ? getFontHeight(font_input) : 0);
1144     int text_chars  = (text  != NULL ? strlen(text)  : 0);
1145     int input_chars = (input != NULL ? strlen(input) : 0);
1146
1147     int button_width =
1148       (button_graphic != -1 ? graphic_info[button_graphic].width  : 0);
1149     int button_height =
1150       (button_graphic != -1 ? graphic_info[button_graphic].height : 0);
1151     int text_width   = font_text_width * text_chars;
1152     int text_height  = font_text_height;
1153     int input_width  = font_input_width * input_chars;
1154     int input_height = font_input_height;
1155
1156     if (nr == MAIN_CONTROL_NAME)
1157     {
1158       menu.main.input.name.width  = input_width;
1159       menu.main.input.name.height = input_height;
1160     }
1161
1162     if (pos_button != NULL)             // (x/y may be -1/-1 here)
1163     {
1164       pos_button->width  = button_width;
1165       pos_button->height = button_height;
1166     }
1167
1168     if (pos_text != NULL)               // (x/y may be -1/-1 here)
1169     {
1170       // calculate text size -- needed for text alignment
1171       boolean calculate_text_size = (text != NULL);
1172
1173       if (pos_text->width == -1 || calculate_text_size)
1174         pos_text->width = text_width;
1175       if (pos_text->height == -1 || calculate_text_size)
1176         pos_text->height = text_height;
1177
1178       if (visibleMenuPos(pos_button))
1179       {
1180         if (pos_text->x == -1)
1181           pos_text->x = pos_button->x + pos_button->width;
1182         if (pos_text->y == -1)
1183           pos_text->y =
1184             pos_button->y + (pos_button->height - pos_text->height) / 2;
1185       }
1186     }
1187
1188     if (pos_input != NULL)              // (x/y may be -1/-1 here)
1189     {
1190       if (visibleTextPos(pos_text))
1191       {
1192         if (pos_input->x == -1)
1193           pos_input->x = pos_text->x + pos_text->width;
1194         if (pos_input->y == -1)
1195           pos_input->y = pos_text->y;
1196       }
1197
1198       if (pos_input->width == -1)
1199         pos_input->width = input_width;
1200       if (pos_input->height == -1)
1201         pos_input->height = input_height;
1202     }
1203   }
1204 }
1205
1206 static void DrawPressedGraphicThruMask(int dst_x, int dst_y,
1207                                        int graphic, boolean pressed)
1208 {
1209   struct GraphicInfo *g = &graphic_info[graphic];
1210   Bitmap *src_bitmap;
1211   int src_x, src_y;
1212   int xoffset = (pressed ? g->pressed_xoffset : 0);
1213   int yoffset = (pressed ? g->pressed_yoffset : 0);
1214
1215   getFixedGraphicSource(graphic, 0, &src_bitmap, &src_x, &src_y);
1216
1217   BlitBitmapMasked(src_bitmap, drawto, src_x + xoffset, src_y + yoffset,
1218                    g->width, g->height, dst_x, dst_y);
1219 }
1220
1221 static void DrawCursorAndText_Main_Ext(int nr, boolean active_text,
1222                                        boolean active_input,
1223                                        boolean pressed_button)
1224 {
1225   int i;
1226
1227   for (i = 0; main_controls[i].nr != -1; i++)
1228   {
1229     struct MainControlInfo *mci = &main_controls[i];
1230
1231     if (mci->nr == nr || nr == -1)
1232     {
1233       struct MenuPosInfo *pos_button = mci->pos_button;
1234       struct TextPosInfo *pos_text   = mci->pos_text;
1235       struct TextPosInfo *pos_input  = mci->pos_input;
1236       char *text                     = (mci->text  ? *mci->text  : NULL);
1237       char *input                    = (mci->input ? *mci->input : NULL);
1238       int button_graphic             = mci->button_graphic;
1239       int font_text                  = (pos_text  ? pos_text->font  : -1);
1240       int font_input                 = (pos_input ? pos_input->font : -1);
1241
1242       if (active_text)
1243       {
1244         button_graphic = BUTTON_ACTIVE(button_graphic);
1245         font_text = FONT_ACTIVE(font_text);
1246       }
1247
1248       if (active_input)
1249       {
1250         font_input = FONT_ACTIVE(font_input);
1251       }
1252
1253       if (visibleMenuPos(pos_button))
1254       {
1255         struct MenuPosInfo *pos = pos_button;
1256         int x = mSX + pos->x;
1257         int y = mSY + pos->y;
1258
1259         DrawBackgroundForGraphic(x, y, pos->width, pos->height, button_graphic);
1260         DrawPressedGraphicThruMask(x, y, button_graphic, pressed_button);
1261       }
1262
1263       if (visibleTextPos(pos_text) && text != NULL)
1264       {
1265         struct TextPosInfo *pos = pos_text;
1266         int x = mSX + ALIGNED_TEXT_XPOS(pos);
1267         int y = mSY + ALIGNED_TEXT_YPOS(pos);
1268
1269 #if 1
1270         // (check why/if this is needed)
1271         DrawBackgroundForFont(x, y, pos->width, pos->height, font_text);
1272 #endif
1273         DrawText(x, y, text, font_text);
1274       }
1275
1276       if (visibleTextPos(pos_input) && input != NULL)
1277       {
1278         struct TextPosInfo *pos = pos_input;
1279         int x = mSX + ALIGNED_TEXT_XPOS(pos);
1280         int y = mSY + ALIGNED_TEXT_YPOS(pos);
1281
1282 #if 1
1283         // (check why/if this is needed)
1284         DrawBackgroundForFont(x, y, pos->width, pos->height, font_input);
1285 #endif
1286         DrawText(x, y, input, font_input);
1287       }
1288     }
1289   }
1290 }
1291
1292 static void DrawCursorAndText_Main(int nr, boolean active_text,
1293                                    boolean pressed_button)
1294 {
1295   DrawCursorAndText_Main_Ext(nr, active_text, FALSE, pressed_button);
1296 }
1297
1298 #if 0
1299 static void DrawCursorAndText_Main_Input(int nr, boolean active_text,
1300                                          boolean pressed_button)
1301 {
1302   DrawCursorAndText_Main_Ext(nr, active_text, TRUE, pressed_button);
1303 }
1304 #endif
1305
1306 static struct MainControlInfo *getMainControlInfo(int nr)
1307 {
1308   int i;
1309
1310   for (i = 0; main_controls[i].nr != -1; i++)
1311     if (main_controls[i].nr == nr)
1312       return &main_controls[i];
1313
1314   return NULL;
1315 }
1316
1317 static boolean insideMenuPosRect(struct MenuPosInfo *rect, int x, int y)
1318 {
1319   if (rect == NULL)
1320     return FALSE;
1321
1322   int rect_x = ALIGNED_TEXT_XPOS(rect);
1323   int rect_y = ALIGNED_TEXT_YPOS(rect);
1324
1325   return (x >= rect_x && x < rect_x + rect->width &&
1326           y >= rect_y && y < rect_y + rect->height);
1327 }
1328
1329 static boolean insideTextPosRect(struct TextPosInfo *rect, int x, int y)
1330 {
1331   if (rect == NULL)
1332     return FALSE;
1333
1334   int rect_x = ALIGNED_TEXT_XPOS(rect);
1335   int rect_y = ALIGNED_TEXT_YPOS(rect);
1336
1337 #if 0
1338   Debug("screens:insideTextPosRect",
1339         "(%d, %d), (%d, %d) [%d, %d] (%d, %d) => %d",
1340         x, y, rect_x, rect_y, rect->x, rect->y, rect->width, rect->height,
1341         (x >= rect_x && x < rect_x + rect->width &&
1342          y >= rect_y && y < rect_y + rect->height));
1343 #endif
1344
1345   return (x >= rect_x && x < rect_x + rect->width &&
1346           y >= rect_y && y < rect_y + rect->height);
1347 }
1348
1349 static boolean insidePreviewRect(struct PreviewInfo *preview, int x, int y)
1350 {
1351   int rect_width  = preview->xsize * preview->tile_size;
1352   int rect_height = preview->ysize * preview->tile_size;
1353   int rect_x = ALIGNED_XPOS(preview->x, rect_width,  preview->align);
1354   int rect_y = ALIGNED_YPOS(preview->y, rect_height, preview->valign);
1355
1356   return (x >= rect_x && x < rect_x + rect_width &&
1357           y >= rect_y && y < rect_y + rect_height);
1358 }
1359
1360 static void AdjustScrollbar(int id, int items_max, int items_visible,
1361                             int item_position)
1362 {
1363   struct GadgetInfo *gi = screen_gadget[id];
1364
1365   if (item_position > items_max - items_visible)
1366     item_position = items_max - items_visible;
1367
1368   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
1369                GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
1370                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
1371 }
1372
1373 static void AdjustChooseTreeScrollbar(int id, int first_entry, TreeInfo *ti)
1374 {
1375   AdjustScrollbar(id, numTreeInfoInGroup(ti), NUM_MENU_ENTRIES_ON_SCREEN,
1376                   first_entry);
1377 }
1378
1379 static void clearMenuListArea(void)
1380 {
1381   int scrollbar_xpos = mSX + SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
1382
1383   // correct scrollbar position if placed outside menu (playfield) area
1384   if (scrollbar_xpos > SX + SC_SCROLLBAR_XPOS)
1385     scrollbar_xpos = SX + SC_SCROLLBAR_XPOS;
1386
1387   // clear menu list area, but not title or scrollbar
1388   DrawBackground(mSX, mSY + MENU_SCREEN_START_YPOS * 32,
1389                  scrollbar_xpos - mSX, NUM_MENU_ENTRIES_ON_SCREEN * 32);
1390 }
1391
1392 static void drawCursorExt(int xpos, int ypos, boolean active, int graphic)
1393 {
1394   static int cursor_array[MAX_LEV_FIELDY];
1395   int x = amSX + TILEX * xpos;
1396   int y = amSY + TILEY * (MENU_SCREEN_START_YPOS + ypos);
1397
1398   if (xpos == 0)
1399   {
1400     if (graphic != -1)
1401       cursor_array[ypos] = graphic;
1402     else
1403       graphic = cursor_array[ypos];
1404   }
1405
1406   if (active)
1407     graphic = BUTTON_ACTIVE(graphic);
1408
1409   DrawBackgroundForGraphic(x, y, TILEX, TILEY, graphic);
1410   DrawFixedGraphicThruMaskExt(drawto, x, y, graphic, 0);
1411 }
1412
1413 static void initCursor(int ypos, int graphic)
1414 {
1415   drawCursorExt(0, ypos, FALSE, graphic);
1416 }
1417
1418 static void drawCursor(int ypos, boolean active)
1419 {
1420   drawCursorExt(0, ypos, active, -1);
1421 }
1422
1423 static void drawCursorXY(int xpos, int ypos, int graphic)
1424 {
1425   drawCursorExt(xpos, ypos, FALSE, graphic);
1426 }
1427
1428 static void drawChooseTreeCursor(int ypos, boolean active)
1429 {
1430   drawCursorExt(0, ypos, active, -1);
1431 }
1432
1433 static void DrawHeadline(void)
1434 {
1435   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, main_text_title_1);
1436   DrawTextSCentered(MENU_TITLE2_YPOS, FONT_TITLE_2, main_text_title_2);
1437 }
1438
1439 static void DrawTitleScreenImage(int nr, boolean initial)
1440 {
1441   int graphic = getTitleScreenGraphic(nr, initial);
1442   Bitmap *bitmap = graphic_info[graphic].bitmap;
1443   int width  = graphic_info[graphic].width;
1444   int height = graphic_info[graphic].height;
1445   int src_x = graphic_info[graphic].src_x;
1446   int src_y = graphic_info[graphic].src_y;
1447   int dst_x, dst_y;
1448
1449   if (bitmap == NULL)
1450     return;
1451
1452   if (width > WIN_XSIZE)
1453   {
1454     // image width too large for window => center image horizontally
1455     src_x = (width - WIN_XSIZE) / 2;
1456     width = WIN_XSIZE;
1457   }
1458
1459   if (height > WIN_YSIZE)
1460   {
1461     // image height too large for window => center image vertically
1462     src_y = (height - WIN_YSIZE) / 2;
1463     height = WIN_YSIZE;
1464   }
1465
1466   // always display title screens centered
1467   dst_x = (WIN_XSIZE - width) / 2;
1468   dst_y = (WIN_YSIZE - height) / 2;
1469
1470   SetDrawBackgroundMask(REDRAW_ALL);
1471   SetWindowBackgroundImage(getTitleBackground(nr, initial, TRUE));
1472
1473   ClearRectangleOnBackground(drawto, 0, 0, WIN_XSIZE, WIN_YSIZE);
1474
1475   if (DrawingOnBackground(dst_x, dst_y))
1476     BlitBitmapMasked(bitmap, drawto, src_x, src_y, width, height, dst_x, dst_y);
1477   else
1478     BlitBitmap(bitmap, drawto, src_x, src_y, width, height, dst_x, dst_y);
1479
1480   redraw_mask = REDRAW_ALL;
1481 }
1482
1483 static void DrawTitleScreenMessage(int nr, boolean initial)
1484 {
1485   char *filename = getLevelSetTitleMessageFilename(nr, initial);
1486   struct TitleMessageInfo *tmi = getTitleMessageInfo(nr, initial);
1487
1488   if (filename == NULL)
1489     return;
1490
1491   // force TITLE font on title message screen
1492   SetFontStatus(getTitleMessageGameMode(initial));
1493
1494   // if chars *and* width set to "-1", automatically determine width
1495   if (tmi->chars == -1 && tmi->width == -1)
1496     tmi->width = viewport.window[game_status].width;
1497
1498   // if lines *and* height set to "-1", automatically determine height
1499   if (tmi->lines == -1 && tmi->height == -1)
1500     tmi->height = viewport.window[game_status].height;
1501
1502   // if chars set to "-1", automatically determine by text and font width
1503   if (tmi->chars == -1)
1504     tmi->chars = tmi->width / getFontWidth(tmi->font);
1505   else
1506     tmi->width = tmi->chars * getFontWidth(tmi->font);
1507
1508   // if lines set to "-1", automatically determine by text and font height
1509   if (tmi->lines == -1)
1510     tmi->lines = tmi->height / getFontHeight(tmi->font);
1511   else
1512     tmi->height = tmi->lines * getFontHeight(tmi->font);
1513
1514   // if x set to "-1", automatically determine by width and alignment
1515   if (tmi->x == -1)
1516     tmi->x = -1 * ALIGNED_XPOS(0, tmi->width, tmi->align);
1517
1518   // if y set to "-1", automatically determine by height and alignment
1519   if (tmi->y == -1)
1520     tmi->y = -1 * ALIGNED_YPOS(0, tmi->height, tmi->valign);
1521
1522   SetDrawBackgroundMask(REDRAW_ALL);
1523   SetWindowBackgroundImage(getTitleBackground(nr, initial, FALSE));
1524
1525   ClearRectangleOnBackground(drawto, 0, 0, WIN_XSIZE, WIN_YSIZE);
1526
1527   DrawTextFile(ALIGNED_TEXT_XPOS(tmi), ALIGNED_TEXT_YPOS(tmi),
1528                filename, tmi->font, tmi->chars, -1, tmi->lines, 0, -1,
1529                tmi->autowrap, tmi->centered, tmi->parse_comments);
1530
1531   ResetFontStatus();
1532 }
1533
1534 static void DrawTitleScreen(void)
1535 {
1536   KeyboardAutoRepeatOff();
1537
1538   HandleTitleScreen(0, 0, 0, 0, MB_MENU_INITIALIZE);
1539 }
1540
1541 static boolean CheckTitleScreen(boolean levelset_has_changed)
1542 {
1543   static boolean show_title_initial = TRUE;
1544   boolean show_titlescreen = FALSE;
1545
1546   // needed to be able to skip title screen, if no image or message defined
1547   InitializeTitleControls(show_title_initial);
1548
1549   if (setup.show_titlescreen && (show_title_initial || levelset_has_changed))
1550     show_titlescreen = TRUE;
1551
1552   // show initial title images and messages only once at program start
1553   show_title_initial = FALSE;
1554
1555   return (show_titlescreen && num_title_screens > 0);
1556 }
1557
1558 void DrawMainMenu(void)
1559 {
1560   static LevelDirTree *leveldir_last_valid = NULL;
1561   boolean levelset_has_changed = FALSE;
1562   int fade_mask = REDRAW_FIELD;
1563
1564   LimitScreenUpdates(FALSE);
1565
1566   FadeSetLeaveScreen();
1567
1568   // do not fade out here -- function may continue and fade on editor screen
1569
1570   UnmapAllGadgets();
1571   FadeMenuSoundsAndMusic();
1572
1573   ExpireSoundLoops(FALSE);
1574
1575   KeyboardAutoRepeatOn();
1576
1577   audio.sound_deactivated = FALSE;
1578
1579   GetPlayerConfig();
1580
1581   // needed if last screen was the playing screen, invoked from level editor
1582   if (level_editor_test_game)
1583   {
1584     CloseDoor(DOOR_CLOSE_ALL);
1585
1586     SetGameStatus(GAME_MODE_EDITOR);
1587
1588     DrawLevelEd();
1589
1590     return;
1591   }
1592
1593   // leveldir_current may be invalid (level group, parent link)
1594   if (!validLevelSeries(leveldir_current))
1595     leveldir_current = getFirstValidTreeInfoEntry(leveldir_last_valid);
1596
1597   if (leveldir_current != leveldir_last_valid)
1598     levelset_has_changed = TRUE;
1599
1600   // store valid level series information
1601   leveldir_last_valid = leveldir_current;
1602
1603   init_last = init;                     // switch to new busy animation
1604
1605   // needed if last screen (level choice) changed graphics, sounds or music
1606   ReloadCustomArtwork(0);
1607
1608   if (CheckTitleScreen(levelset_has_changed))
1609   {
1610     SetGameStatus(GAME_MODE_TITLE);
1611
1612     DrawTitleScreen();
1613
1614     return;
1615   }
1616
1617   if (redraw_mask & REDRAW_ALL)
1618     fade_mask = REDRAW_ALL;
1619
1620   if (CheckFadeAll())
1621     fade_mask = REDRAW_ALL;
1622
1623   FadeOut(fade_mask);
1624
1625   // needed if different viewport properties defined for menues
1626   ChangeViewportPropertiesIfNeeded();
1627
1628   SetDrawtoField(DRAW_TO_BACKBUFFER);
1629
1630   // level_nr may have been set to value over handicap with level editor
1631   if (setup.handicap && level_nr > leveldir_current->handicap_level)
1632     level_nr = leveldir_current->handicap_level;
1633
1634   LoadLevel(level_nr);
1635   LoadScore(level_nr);
1636
1637   SaveLevelSetup_SeriesInfo();
1638
1639   // set this after "ChangeViewportPropertiesIfNeeded()" (which may reset it)
1640   SetDrawDeactivationMask(REDRAW_NONE);
1641   SetDrawBackgroundMask(REDRAW_FIELD);
1642
1643   SetMainBackgroundImage(IMG_BACKGROUND_MAIN);
1644
1645 #if 0
1646   if (fade_mask == REDRAW_ALL)
1647     RedrawGlobalBorder();
1648 #endif
1649
1650   ClearField();
1651
1652   InitializeMainControls();
1653
1654   DrawCursorAndText_Main(-1, FALSE, FALSE);
1655   DrawPreviewLevelInitial();
1656   DrawNetworkPlayers();
1657
1658   HandleMainMenu(0, 0, 0, 0, MB_MENU_INITIALIZE);
1659
1660   TapeStop();
1661   if (TAPE_IS_EMPTY(tape))
1662     LoadTape(level_nr);
1663   DrawCompleteVideoDisplay();
1664
1665   PlayMenuSoundsAndMusic();
1666
1667   // create gadgets for main menu screen
1668   FreeScreenGadgets();
1669   CreateScreenGadgets();
1670
1671   // may be required if audio buttons shown on tape and changed in setup menu
1672   FreeGameButtons();
1673   CreateGameButtons();
1674
1675   // map gadgets for main menu screen
1676   MapTapeButtons();
1677   MapScreenMenuGadgets(SCREEN_MASK_MAIN);
1678   UpdateScreenMenuGadgets(SCREEN_MASK_MAIN_HAS_SOLUTION, hasSolutionTape());
1679
1680   // copy actual game door content to door double buffer for OpenDoor()
1681   BlitBitmap(drawto, bitmap_db_door_1, DX, DY, DXSIZE, DYSIZE, 0, 0);
1682   BlitBitmap(drawto, bitmap_db_door_2, VX, VY, VXSIZE, VYSIZE, 0, 0);
1683
1684   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
1685
1686   DrawMaskedBorder(fade_mask);
1687
1688   FadeIn(fade_mask);
1689   FadeSetEnterMenu();
1690
1691   // update screen area with special editor door
1692   redraw_mask |= REDRAW_ALL;
1693   BackToFront();
1694
1695   SetMouseCursor(CURSOR_DEFAULT);
1696
1697   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
1698 }
1699
1700 static void gotoTopLevelDir(void)
1701 {
1702   // move upwards until inside (but not above) top level directory
1703   while (leveldir_current->node_parent &&
1704          !strEqual(leveldir_current->node_parent->subdir, STRING_TOP_DIRECTORY))
1705   {
1706     // write a "path" into level tree for easy navigation to last level
1707     if (leveldir_current->node_parent->node_group->cl_first == -1)
1708     {
1709       int num_leveldirs = numTreeInfoInGroup(leveldir_current);
1710       int leveldir_pos = posTreeInfo(leveldir_current);
1711       int num_page_entries = MIN(num_leveldirs, NUM_MENU_ENTRIES_ON_SCREEN);
1712       int cl_first, cl_cursor;
1713
1714       cl_first = MAX(0, leveldir_pos - num_page_entries + 1);
1715       cl_cursor = leveldir_pos - cl_first;
1716
1717       leveldir_current->node_parent->node_group->cl_first = cl_first;
1718       leveldir_current->node_parent->node_group->cl_cursor = cl_cursor;
1719     }
1720
1721     leveldir_current = leveldir_current->node_parent;
1722   }
1723 }
1724
1725 static unsigned int getAutoDelayCounter(struct TitleFadingInfo *fi)
1726 {
1727   boolean use_frame_counter = (fi->auto_delay_unit == AUTO_DELAY_UNIT_FRAMES);
1728
1729   return (use_frame_counter ? video.frame_counter : Counter());
1730 }
1731
1732 static boolean TitleAutoDelayReached(unsigned int *counter_var,
1733                                      struct TitleFadingInfo *fi)
1734 {
1735   return DelayReachedExt(counter_var, fi->auto_delay, getAutoDelayCounter(fi));
1736 }
1737
1738 static void ResetTitleAutoDelay(unsigned int *counter_var,
1739                                 struct TitleFadingInfo *fi)
1740 {
1741   *counter_var = getAutoDelayCounter(fi);
1742 }
1743
1744 void HandleTitleScreen(int mx, int my, int dx, int dy, int button)
1745 {
1746   static unsigned int title_delay = 0;
1747   static int title_screen_nr = 0;
1748   static int last_sound = -1, last_music = -1;
1749   boolean return_to_main_menu = FALSE;
1750   struct TitleControlInfo *tci;
1751   int sound, music;
1752
1753   if (button == MB_MENU_INITIALIZE)
1754   {
1755     title_delay = 0;
1756     title_screen_nr = 0;
1757     tci = &title_controls[title_screen_nr];
1758
1759     SetAnimStatus(getTitleAnimMode(tci));
1760
1761     last_sound = SND_UNDEFINED;
1762     last_music = MUS_UNDEFINED;
1763
1764     if (num_title_screens != 0)
1765     {
1766       FadeSetEnterScreen();
1767
1768       // use individual title fading instead of global "enter screen" fading
1769       fading = getTitleFading(tci);
1770     }
1771
1772     if (game_status_last_screen == GAME_MODE_INFO)
1773     {
1774       if (num_title_screens == 0)
1775       {
1776         // switch game mode from title screen mode back to info screen mode
1777         SetGameStatus(GAME_MODE_INFO);
1778
1779         // store that last screen was info screen, not main menu screen
1780         game_status_last_screen = GAME_MODE_INFO;
1781
1782         DrawInfoScreen_NotAvailable("Title screen information:",
1783                                     "No title screen for this level set.");
1784         return;
1785       }
1786     }
1787
1788     FadeMenuSoundsAndMusic();
1789
1790     FadeOut(REDRAW_ALL);
1791
1792     // title screens may have different window size
1793     ChangeViewportPropertiesIfNeeded();
1794
1795     // only required to update logic for redrawing global border
1796     ClearField();
1797
1798     if (tci->is_image)
1799       DrawTitleScreenImage(tci->local_nr, tci->initial);
1800     else
1801       DrawTitleScreenMessage(tci->local_nr, tci->initial);
1802
1803     sound = getTitleSound(tci);
1804     music = getTitleMusic(tci);
1805
1806     if (sound != last_sound)
1807       PlayMenuSoundExt(sound);
1808     if (music != last_music)
1809       PlayMenuMusicExt(music);
1810
1811     last_sound = sound;
1812     last_music = music;
1813
1814     SetMouseCursor(CURSOR_NONE);
1815
1816     FadeIn(REDRAW_ALL);
1817
1818     ResetTitleAutoDelay(&title_delay, &fading);
1819
1820     return;
1821   }
1822
1823   if (fading.auto_delay > 0 && TitleAutoDelayReached(&title_delay, &fading))
1824     button = MB_MENU_CHOICE;
1825
1826   if (button == MB_MENU_LEAVE)
1827   {
1828     return_to_main_menu = TRUE;
1829   }
1830   else if (button == MB_MENU_CHOICE)
1831   {
1832     if (game_status_last_screen == GAME_MODE_INFO && num_title_screens == 0)
1833     {
1834       SetGameStatus(GAME_MODE_INFO);
1835
1836       info_mode = INFO_MODE_MAIN;
1837
1838       DrawInfoScreen();
1839
1840       return;
1841     }
1842
1843     title_screen_nr++;
1844
1845     if (title_screen_nr < num_title_screens)
1846     {
1847       tci = &title_controls[title_screen_nr];
1848
1849       SetAnimStatus(getTitleAnimMode(tci));
1850
1851       sound = getTitleSound(tci);
1852       music = getTitleMusic(tci);
1853
1854       if (last_sound != SND_UNDEFINED && sound != last_sound)
1855         FadeSound(last_sound);
1856       if (last_music != MUS_UNDEFINED && music != last_music)
1857         FadeMusic();
1858
1859       fading = getTitleFading(tci);
1860
1861       FadeOut(REDRAW_ALL);
1862
1863       if (tci->is_image)
1864         DrawTitleScreenImage(tci->local_nr, tci->initial);
1865       else
1866         DrawTitleScreenMessage(tci->local_nr, tci->initial);
1867
1868       sound = getTitleSound(tci);
1869       music = getTitleMusic(tci);
1870
1871       if (sound != last_sound)
1872         PlayMenuSoundExt(sound);
1873       if (music != last_music)
1874         PlayMenuMusicExt(music);
1875
1876       last_sound = sound;
1877       last_music = music;
1878
1879       FadeIn(REDRAW_ALL);
1880
1881       ResetTitleAutoDelay(&title_delay, &fading);
1882     }
1883     else
1884     {
1885       FadeMenuSoundsAndMusic();
1886
1887       return_to_main_menu = TRUE;
1888     }
1889   }
1890
1891   if (return_to_main_menu)
1892   {
1893     SetMouseCursor(CURSOR_DEFAULT);
1894
1895     // force full menu screen redraw after displaying title screens
1896     redraw_mask = REDRAW_ALL;
1897
1898     if (game_status_last_screen == GAME_MODE_INFO)
1899     {
1900       SetGameStatus(GAME_MODE_INFO);
1901
1902       info_mode = INFO_MODE_MAIN;
1903
1904       DrawInfoScreen();
1905     }
1906     else        // default: return to main menu
1907     {
1908       SetGameStatus(GAME_MODE_MAIN);
1909
1910       DrawMainMenu();
1911     }
1912   }
1913 }
1914
1915 static void HandleMainMenu_SelectLevel(int step, int direction,
1916                                        int selected_level_nr)
1917 {
1918   int old_level_nr = level_nr;
1919   int new_level_nr;
1920
1921   if (selected_level_nr != NO_DIRECT_LEVEL_SELECT)
1922     new_level_nr = selected_level_nr;
1923   else
1924     new_level_nr = old_level_nr + step * direction;
1925
1926   if (new_level_nr < leveldir_current->first_level)
1927     new_level_nr = leveldir_current->first_level;
1928   if (new_level_nr > leveldir_current->last_level)
1929     new_level_nr = leveldir_current->last_level;
1930
1931   if (setup.handicap && new_level_nr > leveldir_current->handicap_level)
1932   {
1933     // skipping levels is only allowed when trying to skip single level
1934     if (setup.skip_levels && new_level_nr == old_level_nr + 1 &&
1935         Request("Level still unsolved! Skip despite handicap?", REQ_ASK))
1936     {
1937       leveldir_current->handicap_level++;
1938       SaveLevelSetup_SeriesInfo();
1939     }
1940
1941     new_level_nr = leveldir_current->handicap_level;
1942   }
1943
1944   if (new_level_nr != old_level_nr)
1945   {
1946     struct MainControlInfo *mci = getMainControlInfo(MAIN_CONTROL_LEVEL_NUMBER);
1947
1948     PlaySound(SND_MENU_ITEM_SELECTING);
1949
1950     level_nr = new_level_nr;
1951
1952     DrawText(mSX + mci->pos_text->x, mSY + mci->pos_text->y,
1953              int2str(level_nr, menu.main.text.level_number.size),
1954              mci->pos_text->font);
1955
1956     LoadLevel(level_nr);
1957     DrawPreviewLevelInitial();
1958
1959     TapeErase();
1960     LoadTape(level_nr);
1961     DrawCompleteVideoDisplay();
1962
1963     SaveLevelSetup_SeriesInfo();
1964
1965     UpdateScreenMenuGadgets(SCREEN_MASK_MAIN_HAS_SOLUTION, hasSolutionTape());
1966
1967     // force redraw of playfield area (may be reset at this point)
1968     redraw_mask |= REDRAW_FIELD;
1969   }
1970 }
1971
1972 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
1973 {
1974   static int choice = MAIN_CONTROL_GAME;
1975   static boolean button_pressed_last = FALSE;
1976   boolean button_pressed = FALSE;
1977   int pos = choice;
1978   int i = 0;    // needed to prevent compiler warning due to bad code below
1979
1980   if (button == MB_MENU_INITIALIZE)
1981   {
1982     DrawCursorAndText_Main(choice, TRUE, FALSE);
1983
1984     return;
1985   }
1986
1987   if (mx || my)         // mouse input
1988   {
1989     pos = -1;
1990
1991     for (i = 0; main_controls[i].nr != -1; i++)
1992     {
1993       if (insideMenuPosRect(main_controls[i].pos_button, mx - mSX, my - mSY) ||
1994           insideTextPosRect(main_controls[i].pos_text,   mx - mSX, my - mSY) ||
1995           insideTextPosRect(main_controls[i].pos_input,  mx - mSX, my - mSY))
1996       {
1997         pos = main_controls[i].nr;
1998
1999         break;
2000       }
2001     }
2002
2003     // check if level preview was clicked
2004     if (insidePreviewRect(&preview, mx - SX, my - SY))
2005       pos = MAIN_CONTROL_GAME;
2006
2007     // handle pressed/unpressed state for active/inactive menu buttons
2008     // (if pos != -1, "i" contains index position corresponding to "pos")
2009     if (button &&
2010         pos >= MAIN_CONTROL_NAME && pos <= MAIN_CONTROL_QUIT &&
2011         insideMenuPosRect(main_controls[i].pos_button, mx - mSX, my - mSY))
2012       button_pressed = TRUE;
2013
2014     if (button_pressed != button_pressed_last)
2015     {
2016       DrawCursorAndText_Main(choice, TRUE, button_pressed);
2017
2018       if (button_pressed)
2019         PlaySound(SND_MENU_BUTTON_PRESSING);
2020       else
2021         PlaySound(SND_MENU_BUTTON_RELEASING);
2022     }
2023   }
2024   else if (dx || dy)    // keyboard input
2025   {
2026     if (dx > 0 && (choice == MAIN_CONTROL_INFO ||
2027                    choice == MAIN_CONTROL_SETUP))
2028       button = MB_MENU_CHOICE;
2029     else if (dy)
2030       pos = choice + dy;
2031   }
2032
2033   if (pos == MAIN_CONTROL_FIRST_LEVEL && !button)
2034   {
2035     HandleMainMenu_SelectLevel(MAX_LEVELS, -1, NO_DIRECT_LEVEL_SELECT);
2036   }
2037   else if (pos == MAIN_CONTROL_LAST_LEVEL && !button)
2038   {
2039     HandleMainMenu_SelectLevel(MAX_LEVELS, +1, NO_DIRECT_LEVEL_SELECT);
2040   }
2041   else if (pos == MAIN_CONTROL_LEVEL_NUMBER && !button)
2042   {
2043     CloseDoor(DOOR_CLOSE_2);
2044
2045     SetGameStatus(GAME_MODE_LEVELNR);
2046
2047     DrawChooseLevelNr();
2048   }
2049   else if (pos >= MAIN_CONTROL_NAME && pos <= MAIN_CONTROL_QUIT)
2050   {
2051     if (button)
2052     {
2053       if (pos != choice)
2054       {
2055         PlaySound(SND_MENU_ITEM_ACTIVATING);
2056
2057         DrawCursorAndText_Main(choice, FALSE, FALSE);
2058         DrawCursorAndText_Main(pos, TRUE, button_pressed);
2059
2060         choice = pos;
2061       }
2062       else if (dx != 0)
2063       {
2064         if (choice != MAIN_CONTROL_INFO &&
2065             choice != MAIN_CONTROL_SETUP)
2066           HandleMainMenu_SelectLevel(1, dx, NO_DIRECT_LEVEL_SELECT);
2067       }
2068     }
2069     else
2070     {
2071       PlaySound(SND_MENU_ITEM_SELECTING);
2072
2073       if (pos == MAIN_CONTROL_NAME)
2074       {
2075         if ((mx || my) &&
2076             insideTextPosRect(main_controls[i].pos_text, mx - mSX, my - mSY))
2077         {
2078           // special case: menu text "name/team" clicked -- toggle team mode
2079           setup.team_mode = !setup.team_mode;
2080
2081           InitializeMainControls();
2082           DrawCursorAndText_Main(choice, TRUE, FALSE);
2083
2084           DrawPreviewPlayers();
2085         }
2086         else
2087         {
2088           if (0 && setup.multiple_users)        // (not used yet)
2089           {
2090             CloseDoor(DOOR_CLOSE_2);
2091
2092             SetGameStatus(GAME_MODE_NAMES);
2093
2094             DrawChoosePlayerName();
2095           }
2096           else
2097           {
2098             SetGameStatus(GAME_MODE_PSEUDO_TYPENAME);
2099
2100             DrawTypeName();
2101           }
2102         }
2103       }
2104       else if (pos == MAIN_CONTROL_LEVELS)
2105       {
2106         if (leveldir_first)
2107         {
2108           CloseDoor(DOOR_CLOSE_2);
2109
2110           SetGameStatus(GAME_MODE_LEVELS);
2111
2112           SaveLevelSetup_LastSeries();
2113           SaveLevelSetup_SeriesInfo();
2114
2115           if (setup.internal.choose_from_top_leveldir)
2116             gotoTopLevelDir();
2117
2118           DrawChooseLevelSet();
2119         }
2120       }
2121       else if (pos == MAIN_CONTROL_SCORES)
2122       {
2123         CloseDoor(DOOR_CLOSE_2);
2124
2125         SetGameStatus(GAME_MODE_SCORES);
2126
2127         DrawHallOfFame(level_nr, -1);
2128       }
2129       else if (pos == MAIN_CONTROL_EDITOR)
2130       {
2131         if (leveldir_current->readonly &&
2132             !strEqual(setup.player_name, "Artsoft"))
2133           Request("This level is read only!", REQ_CONFIRM);
2134
2135         CloseDoor(DOOR_CLOSE_2);
2136
2137         SetGameStatus(GAME_MODE_EDITOR);
2138
2139         FadeSetEnterScreen();
2140
2141         DrawLevelEd();
2142       }
2143       else if (pos == MAIN_CONTROL_INFO)
2144       {
2145         CloseDoor(DOOR_CLOSE_2);
2146
2147         SetGameStatus(GAME_MODE_INFO);
2148
2149         info_mode = INFO_MODE_MAIN;
2150
2151         DrawInfoScreen();
2152       }
2153       else if (pos == MAIN_CONTROL_GAME)
2154       {
2155         StartGameActions(network.enabled, setup.autorecord, level.random_seed);
2156       }
2157       else if (pos == MAIN_CONTROL_SETUP)
2158       {
2159         CloseDoor(DOOR_CLOSE_2);
2160
2161         SetGameStatus(GAME_MODE_SETUP);
2162
2163         setup_mode = SETUP_MODE_MAIN;
2164
2165         DrawSetupScreen();
2166       }
2167       else if (pos == MAIN_CONTROL_QUIT)
2168       {
2169         SaveLevelSetup_LastSeries();
2170         SaveLevelSetup_SeriesInfo();
2171
2172         if (Request("Do you really want to quit?", REQ_ASK | REQ_STAY_CLOSED))
2173           SetGameStatus(GAME_MODE_QUIT);
2174       }
2175     }
2176   }
2177
2178   button_pressed_last = button_pressed;
2179 }
2180
2181
2182 // ============================================================================
2183 // info screen functions
2184 // ============================================================================
2185
2186 static struct TokenInfo *info_info;
2187 static int num_info_info;       // number of info entries shown on screen
2188 static int max_info_info;       // total number of info entries in list
2189
2190 static void execInfoTitleScreen(void)
2191 {
2192   info_mode = INFO_MODE_TITLE;
2193
2194   DrawInfoScreen();
2195 }
2196
2197 static void execInfoElements(void)
2198 {
2199   info_mode = INFO_MODE_ELEMENTS;
2200
2201   DrawInfoScreen();
2202 }
2203
2204 static void execInfoMusic(void)
2205 {
2206   info_mode = INFO_MODE_MUSIC;
2207
2208   DrawInfoScreen();
2209 }
2210
2211 static void execInfoCredits(void)
2212 {
2213   info_mode = INFO_MODE_CREDITS;
2214
2215   DrawInfoScreen();
2216 }
2217
2218 static void execInfoProgram(void)
2219 {
2220   info_mode = INFO_MODE_PROGRAM;
2221
2222   DrawInfoScreen();
2223 }
2224
2225 static void execInfoVersion(void)
2226 {
2227   info_mode = INFO_MODE_VERSION;
2228
2229   DrawInfoScreen();
2230 }
2231
2232 static void execInfoLevelSet(void)
2233 {
2234   info_mode = INFO_MODE_LEVELSET;
2235
2236   DrawInfoScreen();
2237 }
2238
2239 static void execExitInfo(void)
2240 {
2241   SetGameStatus(GAME_MODE_MAIN);
2242
2243   DrawMainMenu();
2244 }
2245
2246 static struct TokenInfo info_info_main[] =
2247 {
2248   { TYPE_ENTER_SCREEN,  execInfoTitleScreen,    "Title Screen"          },
2249   { TYPE_ENTER_SCREEN,  execInfoElements,       "Elements Info"         },
2250   { TYPE_ENTER_SCREEN,  execInfoMusic,          "Music Info"            },
2251   { TYPE_ENTER_SCREEN,  execInfoCredits,        "Credits"               },
2252   { TYPE_ENTER_SCREEN,  execInfoProgram,        "Program Info"          },
2253   { TYPE_ENTER_SCREEN,  execInfoVersion,        "Version Info"          },
2254   { TYPE_ENTER_SCREEN,  execInfoLevelSet,       "Level Set Info"        },
2255   { TYPE_EMPTY,         NULL,                   ""                      },
2256   { TYPE_LEAVE_MENU,    execExitInfo,           "Exit"                  },
2257
2258   { 0,                  NULL,                   NULL                    }
2259 };
2260
2261 static int getMenuTextFont(int type)
2262 {
2263   if (type & (TYPE_SWITCH       |
2264               TYPE_YES_NO       |
2265               TYPE_YES_NO_AUTO  |
2266               TYPE_STRING       |
2267               TYPE_PLAYER       |
2268               TYPE_ECS_AGA      |
2269               TYPE_KEYTEXT      |
2270               TYPE_ENTER_LIST   |
2271               TYPE_TEXT_INPUT))
2272     return FONT_MENU_2;
2273   else
2274     return FONT_MENU_1;
2275 }
2276
2277 static struct TokenInfo *setup_info;
2278 static struct TokenInfo setup_info_input[];
2279
2280 static struct TokenInfo *menu_info;
2281
2282 static void DrawCursorAndText_Menu_Ext(struct TokenInfo *token_info,
2283                                        int screen_pos, int menu_info_pos_raw,
2284                                        boolean active)
2285 {
2286   int pos = (menu_info_pos_raw < 0 ? screen_pos : menu_info_pos_raw);
2287   struct TokenInfo *ti = &token_info[pos];
2288   int xpos = MENU_SCREEN_START_XPOS;
2289   int ypos = MENU_SCREEN_START_YPOS + screen_pos;
2290   int font_nr = getMenuTextFont(ti->type);
2291
2292   if (setup_mode == SETUP_MODE_INPUT)
2293     font_nr = FONT_MENU_1;
2294
2295   if (active)
2296     font_nr = FONT_ACTIVE(font_nr);
2297
2298   DrawText(mSX + xpos * 32, mSY + ypos * 32, ti->text, font_nr);
2299
2300   if (ti->type & ~TYPE_SKIP_ENTRY)
2301     drawCursor(screen_pos, active);
2302 }
2303
2304 static void DrawCursorAndText_Menu(int screen_pos, int menu_info_pos_raw,
2305                                    boolean active)
2306 {
2307   DrawCursorAndText_Menu_Ext(menu_info, screen_pos, menu_info_pos_raw, active);
2308 }
2309
2310 static void DrawCursorAndText_Setup(int screen_pos, int menu_info_pos_raw,
2311                                     boolean active)
2312 {
2313   DrawCursorAndText_Menu_Ext(setup_info, screen_pos, menu_info_pos_raw, active);
2314 }
2315
2316 static char *window_size_text;
2317 static char *scaling_type_text;
2318 static char *network_server_text;
2319
2320 static void drawSetupValue(int, int);
2321
2322 static void drawMenuInfoList(int first_entry, int num_page_entries,
2323                              int max_page_entries)
2324 {
2325   int i;
2326
2327   if (first_entry + num_page_entries > max_page_entries)
2328     first_entry = 0;
2329
2330   clearMenuListArea();
2331
2332   for (i = 0; i < num_page_entries; i++)
2333   {
2334     int menu_info_pos = first_entry + i;
2335     struct TokenInfo *si = &menu_info[menu_info_pos];
2336     void *value_ptr = si->value;
2337
2338     // set some entries to "unchangeable" according to other variables
2339     if ((value_ptr == &setup.sound_simple && !audio.sound_available) ||
2340         (value_ptr == &setup.sound_loops  && !audio.loops_available) ||
2341         (value_ptr == &setup.sound_music  && !audio.music_available) ||
2342         (value_ptr == &setup.fullscreen   && !video.fullscreen_available) ||
2343         (value_ptr == &window_size_text   && !video.window_scaling_available) ||
2344         (value_ptr == &scaling_type_text  && !video.window_scaling_available))
2345       si->type |= TYPE_GHOSTED;
2346
2347     if (si->type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
2348       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
2349     else if (si->type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
2350       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
2351     else if (si->type & ~TYPE_SKIP_ENTRY)
2352       initCursor(i, IMG_MENU_BUTTON);
2353
2354     DrawCursorAndText_Menu(i, menu_info_pos, FALSE);
2355
2356     if (si->type & TYPE_STRING)
2357     {
2358       int gadget_id = -1;
2359
2360       if (value_ptr == &network_server_text)
2361         gadget_id = SCREEN_CTRL_ID_NETWORK_SERVER;
2362
2363       if (gadget_id != -1)
2364       {
2365         struct GadgetInfo *gi = screen_gadget[gadget_id];
2366         int xpos = MENU_SCREEN_START_XPOS;
2367         int ypos = MENU_SCREEN_START_YPOS + i;
2368         int x = mSX + xpos * 32;
2369         int y = mSY + ypos * 32;
2370
2371         ModifyGadget(gi, GDI_X, x, GDI_Y, y, GDI_END);
2372       }
2373     }
2374
2375     if (si->type & TYPE_VALUE &&
2376         menu_info == setup_info)
2377       drawSetupValue(i, menu_info_pos);
2378   }
2379 }
2380
2381 static void DrawInfoScreen_Main(void)
2382 {
2383   int fade_mask = REDRAW_FIELD;
2384   int i;
2385
2386   if (redraw_mask & REDRAW_ALL)
2387     fade_mask = REDRAW_ALL;
2388
2389   if (CheckFadeAll())
2390     fade_mask = REDRAW_ALL;
2391
2392   UnmapAllGadgets();
2393   FadeMenuSoundsAndMusic();
2394
2395   FreeScreenGadgets();
2396   CreateScreenGadgets();
2397
2398   // (needed after displaying title screens which disable auto repeat)
2399   KeyboardAutoRepeatOn();
2400
2401   FadeSetLeaveScreen();
2402
2403   FadeOut(fade_mask);
2404
2405   // needed if different viewport properties defined for info screen
2406   ChangeViewportPropertiesIfNeeded();
2407
2408   SetMainBackgroundImage(IMG_BACKGROUND_INFO);
2409
2410   ClearField();
2411
2412   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
2413
2414   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Info Screen");
2415
2416   info_info = info_info_main;
2417
2418   // determine maximal number of info entries that can be displayed on screen
2419   num_info_info = 0;
2420   for (i = 0; info_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
2421     num_info_info++;
2422
2423   // determine maximal number of info entries available for menu of info screen
2424   max_info_info = 0;
2425   for (i = 0; info_info[i].type != 0; i++)
2426     max_info_info++;
2427
2428   HandleInfoScreen_Main(0, 0, 0, 0, MB_MENU_INITIALIZE);
2429
2430   MapScreenGadgets(max_info_info);
2431
2432   PlayMenuSoundsAndMusic();
2433
2434   DrawMaskedBorder(fade_mask);
2435
2436   FadeIn(fade_mask);
2437 }
2438
2439 static void changeSetupValue(int, int, int);
2440
2441 static void HandleMenuScreen(int mx, int my, int dx, int dy, int button,
2442                              int mode, int num_page_entries,
2443                              int max_page_entries)
2444 {
2445   static int num_page_entries_all_last[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
2446   static int choice_stores[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
2447   static int first_entry_stores[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
2448   boolean has_scrollbar = screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->mapped;
2449   int mx_scrollbar = screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x;
2450   int mx_right_border = (has_scrollbar ? mx_scrollbar : SX + SXSIZE);
2451   int *num_page_entries_last = num_page_entries_all_last[game_status];
2452   int *choice_store = choice_stores[game_status];
2453   int *first_entry_store = first_entry_stores[game_status];
2454   int choice = choice_store[mode];              // starts with 0
2455   int first_entry = first_entry_store[mode];    // starts with 0
2456   int x = 0;
2457   int y = choice - first_entry;
2458   int y_old = y;
2459   boolean position_set_by_scrollbar = (dx == 999);
2460   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
2461   int i;
2462
2463   if (button == MB_MENU_INITIALIZE)
2464   {
2465     // check if number of menu page entries has changed (may happen by change
2466     // of custom artwork definition value for 'list_size' for this menu screen)
2467     // (in this case, the last menu position most probably has to be corrected)
2468     if (num_page_entries != num_page_entries_last[mode])
2469     {
2470       choice_store[mode] = first_entry_store[mode] = 0;
2471
2472       choice = first_entry = 0;
2473       y = y_old = 0;
2474
2475       num_page_entries_last[mode] = num_page_entries;
2476     }
2477
2478     // advance to first valid menu entry
2479     while (choice < num_page_entries &&
2480            menu_info[choice].type & TYPE_SKIP_ENTRY)
2481       choice++;
2482
2483     if (position_set_by_scrollbar)
2484       first_entry = first_entry_store[mode] = dy;
2485     else
2486       AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_page_entries,
2487                       NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
2488
2489     drawMenuInfoList(first_entry, num_page_entries, max_page_entries);
2490
2491     if (choice < first_entry)
2492     {
2493       choice = first_entry;
2494
2495       if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2496         choice++;
2497     }
2498     else if (choice > first_entry + num_page_entries - 1)
2499     {
2500       choice = first_entry + num_page_entries - 1;
2501
2502       if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2503         choice--;
2504     }
2505
2506     choice_store[mode] = choice;
2507
2508     DrawCursorAndText_Menu(choice - first_entry, choice, TRUE);
2509
2510     return;
2511   }
2512   else if (button == MB_MENU_LEAVE)
2513   {
2514     PlaySound(SND_MENU_ITEM_SELECTING);
2515
2516     for (i = 0; i < max_page_entries; i++)
2517     {
2518       if (menu_info[i].type & TYPE_LEAVE_MENU)
2519       {
2520         void (*menu_callback_function)(void) = menu_info[i].value;
2521
2522         FadeSetLeaveMenu();
2523
2524         menu_callback_function();
2525
2526         break;  // absolutely needed because function changes 'menu_info'!
2527       }
2528     }
2529
2530     return;
2531   }
2532
2533   if (mx || my)         // mouse input
2534   {
2535     x = (mx - mSX) / 32;
2536     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2537   }
2538   else if (dx || dy)    // keyboard or scrollbar/scrollbutton input
2539   {
2540     // move cursor instead of scrolling when already at start/end of list
2541     if (dy == -1 * SCROLL_LINE && first_entry == 0)
2542       dy = -1;
2543     else if (dy == +1 * SCROLL_LINE &&
2544              first_entry + num_page_entries == max_page_entries)
2545       dy = 1;
2546
2547     // handle scrolling screen one line or page
2548     if (y + dy < 0 ||
2549         y + dy > num_page_entries - 1)
2550     {
2551       boolean redraw = FALSE;
2552
2553       if (ABS(dy) == SCROLL_PAGE)
2554         step = num_page_entries - 1;
2555
2556       if (dy < 0 && first_entry > 0)
2557       {
2558         // scroll page/line up
2559
2560         first_entry -= step;
2561         if (first_entry < 0)
2562           first_entry = 0;
2563
2564         redraw = TRUE;
2565       }
2566       else if (dy > 0 && first_entry + num_page_entries < max_page_entries)
2567       {
2568         // scroll page/line down
2569
2570         first_entry += step;
2571         if (first_entry + num_page_entries > max_page_entries)
2572           first_entry = MAX(0, max_page_entries - num_page_entries);
2573
2574         redraw = TRUE;
2575       }
2576
2577       if (redraw)
2578       {
2579         choice += first_entry - first_entry_store[mode];
2580
2581         if (choice < first_entry)
2582         {
2583           choice = first_entry;
2584
2585           if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2586             choice++;
2587         }
2588         else if (choice > first_entry + num_page_entries - 1)
2589         {
2590           choice = first_entry + num_page_entries - 1;
2591
2592           if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2593             choice--;
2594         }
2595         else if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2596         {
2597           choice += SIGN(dy);
2598
2599           if (choice < first_entry ||
2600               choice > first_entry + num_page_entries - 1)
2601           first_entry += SIGN(dy);
2602         }
2603
2604         first_entry_store[mode] = first_entry;
2605         choice_store[mode] = choice;
2606
2607         drawMenuInfoList(first_entry, num_page_entries, max_page_entries);
2608
2609         DrawCursorAndText_Menu(choice - first_entry, choice, TRUE);
2610
2611         AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_page_entries,
2612                         NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
2613       }
2614
2615       return;
2616     }
2617
2618     if (dx)
2619     {
2620       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE : TYPE_ENTER);
2621
2622       if (menu_info[choice].type & menu_navigation_type ||
2623           menu_info[choice].type & TYPE_BOOLEAN_STYLE ||
2624           menu_info[choice].type & TYPE_YES_NO_AUTO ||
2625           menu_info[choice].type & TYPE_PLAYER)
2626         button = MB_MENU_CHOICE;
2627     }
2628     else if (dy)
2629       y += dy;
2630
2631     // jump to next non-empty menu entry (up or down)
2632     while (first_entry + y > 0 &&
2633            first_entry + y < max_page_entries - 1 &&
2634            menu_info[first_entry + y].type & TYPE_SKIP_ENTRY)
2635       y += dy;
2636
2637     if (!IN_VIS_MENU(x, y))
2638     {
2639       choice += y - y_old;
2640
2641       if (choice < first_entry)
2642         first_entry = choice;
2643       else if (choice > first_entry + num_page_entries - 1)
2644         first_entry = choice - num_page_entries + 1;
2645
2646       if (first_entry >= 0 &&
2647           first_entry + num_page_entries <= max_page_entries)
2648       {
2649         first_entry_store[mode] = first_entry;
2650
2651         if (choice < first_entry)
2652           choice = first_entry;
2653         else if (choice > first_entry + num_page_entries - 1)
2654           choice = first_entry + num_page_entries - 1;
2655
2656         choice_store[mode] = choice;
2657
2658         drawMenuInfoList(first_entry, num_page_entries, max_page_entries);
2659
2660         DrawCursorAndText_Menu(choice - first_entry, choice, TRUE);
2661
2662         AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_page_entries,
2663                         NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
2664       }
2665
2666       return;
2667     }
2668   }
2669
2670   if (!anyScrollbarGadgetActive() &&
2671       IN_VIS_MENU(x, y) &&
2672       mx < mx_right_border &&
2673       y >= 0 && y < num_page_entries)
2674   {
2675     if (button)
2676     {
2677       if (first_entry + y != choice &&
2678           menu_info[first_entry + y].type & ~TYPE_SKIP_ENTRY)
2679       {
2680         PlaySound(SND_MENU_ITEM_ACTIVATING);
2681
2682         DrawCursorAndText_Menu(choice - first_entry, choice, FALSE);
2683         DrawCursorAndText_Menu(y, first_entry + y, TRUE);
2684
2685         choice = choice_store[mode] = first_entry + y;
2686       }
2687       else if (dx < 0)
2688       {
2689         PlaySound(SND_MENU_ITEM_SELECTING);
2690
2691         for (i = 0; menu_info[i].type != 0; i++)
2692         {
2693           if (menu_info[i].type & TYPE_LEAVE_MENU)
2694           {
2695             void (*menu_callback_function)(void) = menu_info[i].value;
2696
2697             FadeSetLeaveMenu();
2698
2699             menu_callback_function();
2700
2701             // absolutely needed because function changes 'menu_info'!
2702             break;
2703           }
2704         }
2705
2706         return;
2707       }
2708     }
2709     else if (!(menu_info[first_entry + y].type & TYPE_GHOSTED))
2710     {
2711       PlaySound(SND_MENU_ITEM_SELECTING);
2712
2713       // when selecting key headline, execute function for key value change
2714       if (menu_info[first_entry + y].type & TYPE_KEYTEXT &&
2715           menu_info[first_entry + y + 1].type & TYPE_KEY)
2716         y++;
2717
2718       // when selecting string value, execute function for list selection
2719       if (menu_info[first_entry + y].type & TYPE_STRING && y > 0 &&
2720           menu_info[first_entry + y - 1].type & TYPE_ENTER_LIST)
2721         y--;
2722
2723       // when selecting string value, execute function for text input gadget
2724       if (menu_info[first_entry + y].type & TYPE_STRING && y > 0 &&
2725           menu_info[first_entry + y - 1].type & TYPE_TEXT_INPUT)
2726         y--;
2727
2728       if (menu_info[first_entry + y].type & TYPE_ENTER_OR_LEAVE)
2729       {
2730         void (*menu_callback_function)(void) =
2731           menu_info[first_entry + y].value;
2732
2733         FadeSetFromType(menu_info[first_entry + y].type);
2734
2735         menu_callback_function();
2736       }
2737       else if (menu_info[first_entry + y].type & TYPE_TEXT_INPUT)
2738       {
2739         void (*gadget_callback_function)(void) =
2740           menu_info[first_entry + y].value;
2741
2742         gadget_callback_function();
2743       }
2744       else if (menu_info[first_entry + y].type & TYPE_VALUE &&
2745                menu_info == setup_info)
2746       {
2747         changeSetupValue(y, first_entry + y, dx);
2748       }
2749     }
2750   }
2751 }
2752
2753 void HandleInfoScreen_Main(int mx, int my, int dx, int dy, int button)
2754 {
2755   menu_info = info_info;
2756
2757   HandleMenuScreen(mx, my, dx, dy, button,
2758                    info_mode, num_info_info, max_info_info);
2759 }
2760
2761 static int getMenuFontSpacing(int spacing_height, int font_nr)
2762 {
2763   int font_spacing = getFontHeight(font_nr) + EXTRA_SPACING(game_status);
2764
2765   return (spacing_height < 0 ? ABS(spacing_height) * font_spacing :
2766           spacing_height);
2767 }
2768
2769 static int getMenuTextSpacing(int spacing_height, int font_nr)
2770 {
2771   return (getMenuFontSpacing(spacing_height, font_nr) +
2772           EXTRA_SPACING(game_status));
2773 }
2774
2775 static int getMenuTextStep(int spacing_height, int font_nr)
2776 {
2777   return getFontHeight(font_nr) + getMenuTextSpacing(spacing_height, font_nr);
2778 }
2779
2780 void DrawInfoScreen_NotAvailable(char *text_title, char *text_error)
2781 {
2782   int font_title = MENU_INFO_FONT_TITLE;
2783   int font_error = FONT_TEXT_2;
2784   int font_foot  = MENU_INFO_FONT_FOOT;
2785   int spacing_title = menu.headline1_spacing_info[info_mode];
2786   int ystep_title = getMenuTextStep(spacing_title, font_title);
2787   int ystart1 = mSY - SY + MENU_SCREEN_INFO_YSTART1;
2788   int ystart2 = ystart1 + ystep_title;
2789   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
2790
2791   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO);
2792
2793   FadeOut(REDRAW_FIELD);
2794
2795   ClearField();
2796   DrawHeadline();
2797
2798   DrawTextSCentered(ystart1, font_title, text_title);
2799   DrawTextSCentered(ystart2, font_error, text_error);
2800
2801   DrawTextSCentered(ybottom, font_foot,
2802                     "Press any key or button for info menu");
2803
2804   FadeIn(REDRAW_FIELD);
2805 }
2806
2807 void DrawInfoScreen_HelpAnim(int start, int max_anims, boolean init)
2808 {
2809   static int infoscreen_step[MAX_INFO_ELEMENTS_ON_SCREEN];
2810   static int infoscreen_frame[MAX_INFO_ELEMENTS_ON_SCREEN];
2811   int font_title = MENU_INFO_FONT_TITLE;
2812   int font_foot  = MENU_INFO_FONT_FOOT;
2813   int xstart  = mSX + MENU_SCREEN_INFO_SPACE_LEFT;
2814   int ystart1 = mSY - SY + MENU_SCREEN_INFO_YSTART1;
2815   int ystart2 = mSY + MENU_SCREEN_INFO_YSTART2;
2816   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
2817   int ystep = MENU_SCREEN_INFO_YSTEP;
2818   int element, action, direction;
2819   int graphic;
2820   int delay;
2821   int sync_frame;
2822   int i, j;
2823
2824   if (init)
2825   {
2826     for (i = 0; i < NUM_INFO_ELEMENTS_ON_SCREEN; i++)
2827       infoscreen_step[i] = infoscreen_frame[i] = 0;
2828
2829     ClearField();
2830     DrawHeadline();
2831
2832     DrawTextSCentered(ystart1, font_title, "The Game Elements:");
2833
2834     DrawTextSCentered(ybottom, font_foot,
2835                       "Press any key or button for next page");
2836
2837     FrameCounter = 0;
2838   }
2839
2840   i = j = 0;
2841   while (helpanim_info[j].element != HELPANIM_LIST_END)
2842   {
2843     if (i >= start + NUM_INFO_ELEMENTS_ON_SCREEN ||
2844         i >= max_anims)
2845       break;
2846     else if (i < start)
2847     {
2848       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
2849         j++;
2850
2851       j++;
2852       i++;
2853
2854       continue;
2855     }
2856
2857     j += infoscreen_step[i - start];
2858
2859     element = helpanim_info[j].element;
2860     action = helpanim_info[j].action;
2861     direction = helpanim_info[j].direction;
2862
2863     if (element < 0)
2864       element = EL_UNKNOWN;
2865
2866     if (action != -1 && direction != -1)
2867       graphic = el_act_dir2img(element, action, direction);
2868     else if (action != -1)
2869       graphic = el_act2img(element, action);
2870     else if (direction != -1)
2871       graphic = el_dir2img(element, direction);
2872     else
2873       graphic = el2img(element);
2874
2875     delay = helpanim_info[j++].delay;
2876
2877     if (delay == -1)
2878       delay = 1000000;
2879
2880     if (infoscreen_frame[i - start] == 0)
2881     {
2882       sync_frame = 0;
2883       infoscreen_frame[i - start] = delay - 1;
2884     }
2885     else
2886     {
2887       sync_frame = delay - infoscreen_frame[i - start];
2888       infoscreen_frame[i - start]--;
2889     }
2890
2891     if (helpanim_info[j].element == HELPANIM_LIST_NEXT)
2892     {
2893       if (!infoscreen_frame[i - start])
2894         infoscreen_step[i - start] = 0;
2895     }
2896     else
2897     {
2898       if (!infoscreen_frame[i - start])
2899         infoscreen_step[i - start]++;
2900       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
2901         j++;
2902     }
2903
2904     j++;
2905
2906     ClearRectangleOnBackground(drawto, xstart, ystart2 + (i - start) * ystep,
2907                                TILEX, TILEY);
2908     DrawFixedGraphicAnimationExt(drawto, xstart, ystart2 + (i - start) * ystep,
2909                                  graphic, sync_frame, USE_MASKING);
2910
2911     if (init)
2912       DrawInfoScreen_HelpText(element, action, direction, i - start);
2913
2914     i++;
2915   }
2916
2917   redraw_mask |= REDRAW_FIELD;
2918
2919   FrameCounter++;
2920 }
2921
2922 static char *getHelpText(int element, int action, int direction)
2923 {
2924   char token[MAX_LINE_LEN];
2925
2926   strcpy(token, element_info[element].token_name);
2927
2928   if (action != -1)
2929     strcat(token, element_action_info[action].suffix);
2930
2931   if (direction != -1)
2932     strcat(token, element_direction_info[MV_DIR_TO_BIT(direction)].suffix);
2933
2934   return getHashEntry(helptext_info, token);
2935 }
2936
2937 void DrawInfoScreen_HelpText(int element, int action, int direction, int ypos)
2938 {
2939   int font_nr = FONT_INFO_ELEMENTS;
2940   int font_width = getFontWidth(font_nr);
2941   int font_height = getFontHeight(font_nr);
2942   int yoffset = (TILEX - 2 * font_height) / 2;
2943   int xstart = mSX + MENU_SCREEN_INFO_SPACE_LEFT + TILEX + MINI_TILEX;
2944   int ystart = mSY + MENU_SCREEN_INFO_YSTART2 + yoffset;
2945   int ystep = TILEY + 4;
2946   int pad_left = xstart - SX;
2947   int pad_right = MENU_SCREEN_INFO_SPACE_RIGHT;
2948   int max_chars_per_line = (SXSIZE - pad_left - pad_right) / font_width;
2949   int max_lines_per_text = 2;    
2950   char *text = NULL;
2951
2952   if (action != -1 && direction != -1)          // element.action.direction
2953     text = getHelpText(element, action, direction);
2954
2955   if (text == NULL && action != -1)             // element.action
2956     text = getHelpText(element, action, -1);
2957
2958   if (text == NULL && direction != -1)          // element.direction
2959     text = getHelpText(element, -1, direction);
2960
2961   if (text == NULL)                             // base element
2962     text = getHelpText(element, -1, -1);
2963
2964   if (text == NULL)                             // not found
2965     text = "No description available";
2966
2967   if (strlen(text) <= max_chars_per_line)       // only one line of text
2968     ystart += getFontHeight(font_nr) / 2;
2969
2970   DrawTextBuffer(xstart, ystart + ypos * ystep, text, font_nr,
2971                  max_chars_per_line, -1, max_lines_per_text, 0, -1,
2972                  TRUE, FALSE, FALSE);
2973 }
2974
2975 static void DrawInfoScreen_TitleScreen(void)
2976 {
2977   SetGameStatus(GAME_MODE_TITLE);
2978
2979   DrawTitleScreen();
2980 }
2981
2982 void HandleInfoScreen_TitleScreen(int button)
2983 {
2984   HandleTitleScreen(0, 0, 0, 0, button);
2985 }
2986
2987 static void DrawInfoScreen_Elements(void)
2988 {
2989   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_ELEMENTS);
2990
2991   FadeOut(REDRAW_FIELD);
2992
2993   LoadHelpAnimInfo();
2994   LoadHelpTextInfo();
2995
2996   HandleInfoScreen_Elements(MB_MENU_INITIALIZE);
2997
2998   FadeIn(REDRAW_FIELD);
2999 }
3000
3001 void HandleInfoScreen_Elements(int button)
3002 {
3003   static unsigned int info_delay = 0;
3004   static int num_anims;
3005   static int num_pages;
3006   static int page;
3007   int anims_per_page = NUM_INFO_ELEMENTS_ON_SCREEN;
3008   int i;
3009
3010   if (button == MB_MENU_INITIALIZE)
3011   {
3012     boolean new_element = TRUE;
3013
3014     num_anims = 0;
3015
3016     for (i = 0; helpanim_info[i].element != HELPANIM_LIST_END; i++)
3017     {
3018       if (helpanim_info[i].element == HELPANIM_LIST_NEXT)
3019         new_element = TRUE;
3020       else if (new_element)
3021       {
3022         num_anims++;
3023         new_element = FALSE;
3024       }
3025     }
3026
3027     num_pages = (num_anims + anims_per_page - 1) / anims_per_page;
3028     page = 0;
3029   }
3030
3031   if (button == MB_MENU_LEAVE)
3032   {
3033     PlaySound(SND_MENU_ITEM_SELECTING);
3034
3035     info_mode = INFO_MODE_MAIN;
3036     DrawInfoScreen();
3037
3038     return;
3039   }
3040   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
3041   {
3042     if (button != MB_MENU_INITIALIZE)
3043     {
3044       PlaySound(SND_MENU_ITEM_SELECTING);
3045
3046       page++;
3047     }
3048
3049     if (page >= num_pages)
3050     {
3051       FadeMenuSoundsAndMusic();
3052
3053       info_mode = INFO_MODE_MAIN;
3054       DrawInfoScreen();
3055
3056       return;
3057     }
3058
3059     if (page > 0)
3060       FadeSetNextScreen();
3061
3062     if (button != MB_MENU_INITIALIZE)
3063       FadeOut(REDRAW_FIELD);
3064
3065     DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, TRUE);
3066
3067     if (button != MB_MENU_INITIALIZE)
3068       FadeIn(REDRAW_FIELD);
3069   }
3070   else
3071   {
3072     if (DelayReached(&info_delay, GameFrameDelay))
3073       if (page < num_pages)
3074         DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, FALSE);
3075
3076     PlayMenuSoundIfLoop();
3077   }
3078 }
3079
3080 static void DrawInfoScreen_Music(void)
3081 {
3082   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_MUSIC);
3083
3084   FadeOut(REDRAW_FIELD);
3085
3086   ClearField();
3087   DrawHeadline();
3088
3089   LoadMusicInfo();
3090
3091   HandleInfoScreen_Music(MB_MENU_INITIALIZE);
3092
3093   FadeIn(REDRAW_FIELD);
3094 }
3095
3096 void HandleInfoScreen_Music(int button)
3097 {
3098   static struct MusicFileInfo *list = NULL;
3099   int font_title = MENU_INFO_FONT_TITLE;
3100   int font_head  = MENU_INFO_FONT_HEAD;
3101   int font_text  = MENU_INFO_FONT_TEXT;
3102   int font_foot  = MENU_INFO_FONT_FOOT;
3103   int spacing_title = menu.headline1_spacing_info[info_mode];
3104   int spacing_head  = menu.headline2_spacing_info[info_mode];
3105   int ystep_title = getMenuTextStep(spacing_title, font_title);
3106   int ystep_head  = getMenuTextStep(spacing_head,  font_head);
3107   int ystart  = mSY - SY + MENU_SCREEN_INFO_YSTART1;
3108   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
3109
3110   if (button == MB_MENU_INITIALIZE)
3111   {
3112     list = music_file_info;
3113
3114     if (list == NULL)
3115     {
3116       FadeMenuSoundsAndMusic();
3117
3118       ClearField();
3119       DrawHeadline();
3120
3121       DrawTextSCentered(ystart, font_title,
3122                         "No music info for this level set.");
3123
3124       DrawTextSCentered(ybottom, font_foot,
3125                         "Press any key or button for info menu");
3126
3127       return;
3128     }
3129   }
3130
3131   if (button == MB_MENU_LEAVE)
3132   {
3133     PlaySound(SND_MENU_ITEM_SELECTING);
3134
3135     FadeMenuSoundsAndMusic();
3136
3137     info_mode = INFO_MODE_MAIN;
3138     DrawInfoScreen();
3139
3140     return;
3141   }
3142   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
3143   {
3144     if (button != MB_MENU_INITIALIZE)
3145     {
3146       PlaySound(SND_MENU_ITEM_SELECTING);
3147
3148       if (list != NULL)
3149         list = list->next;
3150     }
3151
3152     if (list == NULL)
3153     {
3154       FadeMenuSoundsAndMusic();
3155
3156       info_mode = INFO_MODE_MAIN;
3157       DrawInfoScreen();
3158
3159       return;
3160     }
3161
3162     FadeMenuSoundsAndMusic();
3163
3164     if (list != music_file_info)
3165       FadeSetNextScreen();
3166
3167     if (button != MB_MENU_INITIALIZE)
3168       FadeOut(REDRAW_FIELD);
3169
3170     ClearField();
3171     DrawHeadline();
3172
3173     if (list->is_sound)
3174     {
3175       int sound = list->music;
3176
3177       if (sound_info[sound].loop)
3178         PlaySoundLoop(sound);
3179       else
3180         PlaySound(sound);
3181
3182       DrawTextSCentered(ystart, font_title, "The Game Background Sounds:");
3183     }
3184     else
3185     {
3186       int music = list->music;
3187
3188       if (music_info[music].loop)
3189         PlayMusicLoop(music);
3190       else
3191         PlayMusic(music);
3192
3193       DrawTextSCentered(ystart, font_title, "The Game Background Music:");
3194     }
3195
3196     ystart += ystep_title;
3197
3198     if (!strEqual(list->title, UNKNOWN_NAME))
3199     {
3200       if (!strEqual(list->title_header, UNKNOWN_NAME))
3201       {
3202         DrawTextSCentered(ystart, font_head, list->title_header);
3203         ystart += ystep_head;
3204       }
3205
3206       DrawTextFCentered(ystart, font_text, "\"%s\"", list->title);
3207       ystart += ystep_head;
3208     }
3209
3210     if (!strEqual(list->artist, UNKNOWN_NAME))
3211     {
3212       if (!strEqual(list->artist_header, UNKNOWN_NAME))
3213         DrawTextSCentered(ystart, font_head, list->artist_header);
3214       else
3215         DrawTextSCentered(ystart, font_head, "by");
3216
3217       ystart += ystep_head;
3218
3219       DrawTextFCentered(ystart, font_text, "%s", list->artist);
3220       ystart += ystep_head;
3221     }
3222
3223     if (!strEqual(list->album, UNKNOWN_NAME))
3224     {
3225       if (!strEqual(list->album_header, UNKNOWN_NAME))
3226         DrawTextSCentered(ystart, font_head, list->album_header);
3227       else
3228         DrawTextSCentered(ystart, font_head, "from the album");
3229
3230       ystart += ystep_head;
3231
3232       DrawTextFCentered(ystart, font_text, "\"%s\"", list->album);
3233       ystart += ystep_head;
3234     }
3235
3236     if (!strEqual(list->year, UNKNOWN_NAME))
3237     {
3238       if (!strEqual(list->year_header, UNKNOWN_NAME))
3239         DrawTextSCentered(ystart, font_head, list->year_header);
3240       else
3241         DrawTextSCentered(ystart, font_head, "from the year");
3242
3243       ystart += ystep_head;
3244
3245       DrawTextFCentered(ystart, font_text, "%s", list->year);
3246       ystart += ystep_head;
3247     }
3248
3249     DrawTextSCentered(ybottom, FONT_TEXT_4,
3250                       "Press any key or button for next page");
3251
3252     if (button != MB_MENU_INITIALIZE)
3253       FadeIn(REDRAW_FIELD);
3254   }
3255
3256   if (list != NULL && list->is_sound && sound_info[list->music].loop)
3257     PlaySoundLoop(list->music);
3258 }
3259
3260 static void DrawInfoScreen_CreditsScreen(int screen_nr)
3261 {
3262   int font_title = MENU_INFO_FONT_TITLE;
3263   int font_head  = MENU_INFO_FONT_HEAD;
3264   int font_text  = MENU_INFO_FONT_TEXT;
3265   int font_foot  = MENU_INFO_FONT_FOOT;
3266   int spacing_title = menu.headline1_spacing_info[info_mode];
3267   int spacing_head  = menu.headline2_spacing_info[info_mode];
3268   int spacing_para  = menu.paragraph_spacing_info[info_mode];
3269   int spacing_line  = menu.line_spacing_info[info_mode];
3270   int ystep_title = getMenuTextStep(spacing_title, font_title);
3271   int ystep_head  = getMenuTextStep(spacing_head,  font_head);
3272   int ystep_para  = getMenuTextStep(spacing_para,  font_text);
3273   int ystep_line  = getMenuTextStep(spacing_line,  font_text);
3274   int ystart  = mSY - SY + MENU_SCREEN_INFO_YSTART1;
3275   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
3276
3277   ClearField();
3278   DrawHeadline();
3279
3280   DrawTextSCentered(ystart, font_title, "Credits:");
3281   ystart += ystep_title;
3282
3283   if (screen_nr == 0)
3284   {
3285     DrawTextSCentered(ystart, font_head,
3286                       "Special thanks to");
3287     ystart += ystep_head;
3288     DrawTextSCentered(ystart, font_text,
3289                       "Peter Liepa");
3290     ystart += ystep_head;
3291     DrawTextSCentered(ystart, font_head,
3292                       "for creating");
3293     ystart += ystep_head;
3294     DrawTextSCentered(ystart, font_text,
3295                       "\"Boulder Dash\"");
3296     ystart += ystep_head;
3297     DrawTextSCentered(ystart, font_head,
3298                       "in the year");
3299     ystart += ystep_head;
3300     DrawTextSCentered(ystart, font_text,
3301                       "1984");
3302     ystart += ystep_head;
3303     DrawTextSCentered(ystart, font_head,
3304                       "published by");
3305     ystart += ystep_head;
3306     DrawTextSCentered(ystart, font_text,
3307                       "First Star Software");
3308   }
3309   else if (screen_nr == 1)
3310   {
3311     DrawTextSCentered(ystart, font_head,
3312                       "Special thanks to");
3313     ystart += ystep_head;
3314     DrawTextSCentered(ystart, font_text,
3315                       "Klaus Heinz & Volker Wertich");
3316     ystart += ystep_head;
3317     DrawTextSCentered(ystart, font_head,
3318                       "for creating");
3319     ystart += ystep_head;
3320     DrawTextSCentered(ystart, font_text,
3321                       "\"Emerald Mine\"");
3322     ystart += ystep_head;
3323     DrawTextSCentered(ystart, font_head,
3324                       "in the year");
3325     ystart += ystep_head;
3326     DrawTextSCentered(ystart, font_text,
3327                       "1987");
3328     ystart += ystep_head;
3329     DrawTextSCentered(ystart, font_head,
3330                       "published by");
3331     ystart += ystep_head;
3332     DrawTextSCentered(ystart, font_text,
3333                       "Kingsoft");
3334   }
3335   else if (screen_nr == 2)
3336   {
3337     DrawTextSCentered(ystart, font_head,
3338                       "Special thanks to");
3339     ystart += ystep_head;
3340     DrawTextSCentered(ystart, font_text,
3341                       "Michael Stopp & Philip Jespersen");
3342     ystart += ystep_head;
3343     DrawTextSCentered(ystart, font_head,
3344                       "for creating");
3345     ystart += ystep_head;
3346     DrawTextSCentered(ystart, font_text,
3347                       "\"Supaplex\"");
3348     ystart += ystep_head;
3349     DrawTextSCentered(ystart, font_head,
3350                       "in the year");
3351     ystart += ystep_head;
3352     DrawTextSCentered(ystart, font_text,
3353                       "1991");
3354     ystart += ystep_head;
3355     DrawTextSCentered(ystart, font_head,
3356                       "published by");
3357     ystart += ystep_head;
3358     DrawTextSCentered(ystart, font_text,
3359                       "Digital Integration");
3360   }
3361   else if (screen_nr == 3)
3362   {
3363     DrawTextSCentered(ystart, font_head,
3364                       "Special thanks to");
3365     ystart += ystep_head;
3366     DrawTextSCentered(ystart, font_text,
3367                       "Hiroyuki Imabayashi");
3368     ystart += ystep_head;
3369     DrawTextSCentered(ystart, font_head,
3370                       "for creating");
3371     ystart += ystep_head;
3372     DrawTextSCentered(ystart, font_text,
3373                       "\"Sokoban\"");
3374     ystart += ystep_head;
3375     DrawTextSCentered(ystart, font_head,
3376                       "in the year");
3377     ystart += ystep_head;
3378     DrawTextSCentered(ystart, font_text,
3379                       "1982");
3380     ystart += ystep_head;
3381     DrawTextSCentered(ystart, font_head,
3382                       "published by");
3383     ystart += ystep_head;
3384     DrawTextSCentered(ystart, font_text,
3385                       "Thinking Rabbit");
3386   }
3387   else if (screen_nr == 4)
3388   {
3389     DrawTextSCentered(ystart, font_head,
3390                       "Special thanks to");
3391     ystart += ystep_head;
3392     DrawTextSCentered(ystart, font_text,
3393                       "Alan Bond");
3394     ystart += ystep_head;
3395     DrawTextSCentered(ystart, font_head,
3396                       "and");
3397     ystart += ystep_head;
3398     DrawTextSCentered(ystart, font_text,
3399                       "J\xfcrgen Bonhagen");
3400     ystart += ystep_head;
3401     DrawTextSCentered(ystart, font_head,
3402                       "for the continuous creation");
3403     ystart += ystep_line;
3404     DrawTextSCentered(ystart, font_head,
3405                       "of outstanding level sets");
3406   }
3407   else if (screen_nr == 5)
3408   {
3409     DrawTextSCentered(ystart, font_head,
3410                       "Thanks to");
3411     ystart += ystep_head;
3412     DrawTextSCentered(ystart, font_text,
3413                       "Peter Elzner");
3414     ystart += ystep_head;
3415     DrawTextSCentered(ystart, font_head,
3416                       "for ideas and inspiration by");
3417     ystart += ystep_head;
3418     DrawTextSCentered(ystart, font_text,
3419                       "Diamond Caves");
3420     ystart += ystep_para;
3421
3422     DrawTextSCentered(ystart, font_head,
3423                       "Thanks to");
3424     ystart += ystep_head;
3425     DrawTextSCentered(ystart, font_text,
3426                       "Steffest");
3427     ystart += ystep_head;
3428     DrawTextSCentered(ystart, font_head,
3429                       "for ideas and inspiration by");
3430     ystart += ystep_head;
3431     DrawTextSCentered(ystart, font_text,
3432                       "DX-Boulderdash");
3433   }
3434   else if (screen_nr == 6)
3435   {
3436     DrawTextSCentered(ystart, font_head,
3437                       "Thanks to");
3438     ystart += ystep_head;
3439     DrawTextSCentered(ystart, font_text,
3440                       "David Tritscher");
3441     ystart += ystep_head;
3442     DrawTextSCentered(ystart, font_head,
3443                       "for the code base used for the");
3444     ystart += ystep_line;
3445     DrawTextSCentered(ystart, font_head,
3446                       "native Emerald Mine engine");
3447   }
3448   else if (screen_nr == 7)
3449   {
3450     DrawTextSCentered(ystart, font_head,
3451                       "Thanks to");
3452     ystart += ystep_head;
3453     DrawTextSCentered(ystart, font_text,
3454                       "Guido Schulz");
3455     ystart += ystep_head;
3456     DrawTextSCentered(ystart, font_head,
3457                       "for the initial DOS port");
3458     ystart += ystep_para;
3459
3460     DrawTextSCentered(ystart, font_head,
3461                       "Thanks to");
3462     ystart += ystep_head;
3463     DrawTextSCentered(ystart, font_text,
3464                       "Karl H\xf6rnell");
3465     ystart += ystep_head;
3466     DrawTextSCentered(ystart, font_head,
3467                       "for some additional toons");
3468   }
3469   else if (screen_nr == 8)
3470   {
3471     DrawTextSCentered(ystart, font_head,
3472                       "And not to forget:");
3473     ystart += ystep_head;
3474     DrawTextSCentered(ystart, font_head,
3475                       "Many thanks to");
3476     ystart += ystep_head;
3477     DrawTextSCentered(ystart, font_text,
3478                       "All those who contributed");
3479     ystart += ystep_line;
3480     DrawTextSCentered(ystart, font_text,
3481                       "levels to this game");
3482     ystart += ystep_line;
3483     DrawTextSCentered(ystart, font_text,
3484                       "since 1995");
3485   }
3486
3487   DrawTextSCentered(ybottom, font_foot,
3488                     "Press any key or button for next page");
3489 }
3490
3491 static void DrawInfoScreen_Credits(void)
3492 {
3493   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_CREDITS);
3494
3495   FadeMenuSoundsAndMusic();
3496
3497   FadeOut(REDRAW_FIELD);
3498
3499   HandleInfoScreen_Credits(MB_MENU_INITIALIZE);
3500
3501   FadeIn(REDRAW_FIELD);
3502 }
3503
3504 void HandleInfoScreen_Credits(int button)
3505 {
3506   static int screen_nr = 0;
3507   int num_screens = 9;
3508
3509   if (button == MB_MENU_INITIALIZE)
3510   {
3511     screen_nr = 0;
3512
3513     // DrawInfoScreen_CreditsScreen(screen_nr);
3514   }
3515
3516   if (button == MB_MENU_LEAVE)
3517   {
3518     PlaySound(SND_MENU_ITEM_SELECTING);
3519
3520     info_mode = INFO_MODE_MAIN;
3521     DrawInfoScreen();
3522
3523     return;
3524   }
3525   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
3526   {
3527     if (button != MB_MENU_INITIALIZE)
3528     {
3529       PlaySound(SND_MENU_ITEM_SELECTING);
3530
3531       screen_nr++;
3532     }
3533
3534     if (screen_nr >= num_screens)
3535     {
3536       FadeMenuSoundsAndMusic();
3537
3538       info_mode = INFO_MODE_MAIN;
3539       DrawInfoScreen();
3540
3541       return;
3542     }
3543
3544     if (screen_nr > 0)
3545       FadeSetNextScreen();
3546
3547     if (button != MB_MENU_INITIALIZE)
3548       FadeOut(REDRAW_FIELD);
3549
3550     DrawInfoScreen_CreditsScreen(screen_nr);
3551
3552     if (button != MB_MENU_INITIALIZE)
3553       FadeIn(REDRAW_FIELD);
3554   }
3555   else
3556   {
3557     PlayMenuSoundIfLoop();
3558   }
3559 }
3560
3561 static void DrawInfoScreen_Program(void)
3562 {
3563   int font_title = MENU_INFO_FONT_TITLE;
3564   int font_head  = MENU_INFO_FONT_HEAD;
3565   int font_text  = MENU_INFO_FONT_TEXT;
3566   int font_foot  = MENU_INFO_FONT_FOOT;
3567   int spacing_title = menu.headline1_spacing_info[info_mode];
3568   int spacing_head  = menu.headline2_spacing_info[info_mode];
3569   int spacing_para  = menu.paragraph_spacing_info[info_mode];
3570   int spacing_line  = menu.line_spacing_info[info_mode];
3571   int ystep_title = getMenuTextStep(spacing_title, font_title);
3572   int ystep_head  = getMenuTextStep(spacing_head,  font_head);
3573   int ystep_para  = getMenuTextStep(spacing_para,  font_text);
3574   int ystep_line  = getMenuTextStep(spacing_line,  font_text);
3575   int ystart  = mSY - SY + MENU_SCREEN_INFO_YSTART1;
3576   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
3577
3578   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_PROGRAM);
3579
3580   FadeOut(REDRAW_FIELD);
3581
3582   ClearField();
3583   DrawHeadline();
3584
3585   DrawTextSCentered(ystart, font_title, "Program Information:");
3586   ystart += ystep_title;
3587
3588   DrawTextSCentered(ystart, font_head,
3589                     "This game is Freeware!");
3590   ystart += ystep_head;
3591   DrawTextSCentered(ystart, font_head,
3592                     "If you like it, send e-mail to:");
3593   ystart += ystep_head;
3594   DrawTextSCentered(ystart, font_text,
3595                     setup.internal.program_email);
3596   ystart += ystep_para;
3597
3598   DrawTextSCentered(ystart, font_head,
3599                     "More information and levels:");
3600   ystart += ystep_head;
3601   DrawTextSCentered(ystart, font_text,
3602                     setup.internal.program_website);
3603   ystart += ystep_para;
3604
3605   DrawTextSCentered(ystart, font_head,
3606                     "If you have created new levels,");
3607   ystart += ystep_line;
3608   DrawTextSCentered(ystart, font_head,
3609                     "send them to me to include them!");
3610   ystart += ystep_head;
3611   DrawTextSCentered(ystart, font_head,
3612                     ":-)");
3613
3614   DrawTextSCentered(ybottom, font_foot,
3615                     "Press any key or button for info menu");
3616
3617   FadeIn(REDRAW_FIELD);
3618 }
3619
3620 void HandleInfoScreen_Program(int button)
3621 {
3622   if (button == MB_MENU_LEAVE)
3623   {
3624     PlaySound(SND_MENU_ITEM_SELECTING);
3625
3626     info_mode = INFO_MODE_MAIN;
3627     DrawInfoScreen();
3628
3629     return;
3630   }
3631   else if (button == MB_MENU_CHOICE)
3632   {
3633     PlaySound(SND_MENU_ITEM_SELECTING);
3634
3635     FadeMenuSoundsAndMusic();
3636
3637     info_mode = INFO_MODE_MAIN;
3638     DrawInfoScreen();
3639   }
3640   else
3641   {
3642     PlayMenuSoundIfLoop();
3643   }
3644 }
3645
3646 static void DrawInfoScreen_Version(void)
3647 {
3648   int font_title = MENU_INFO_FONT_TITLE;
3649   int font_head  = MENU_INFO_FONT_HEAD;
3650   int font_text  = MENU_INFO_FONT_TEXT;
3651   int font_foot  = MENU_INFO_FONT_FOOT;
3652   int spacing_title = menu.headline1_spacing_info[info_mode];
3653   int spacing_head  = menu.headline2_spacing_info[info_mode];
3654   int spacing_para  = menu.paragraph_spacing_info[info_mode];
3655   int spacing_line  = menu.line_spacing_info[info_mode];
3656   int xstep = getFontWidth(font_text);
3657   int ystep_title = getMenuTextStep(spacing_title, font_title);
3658   int ystep_head  = getMenuTextStep(spacing_head,  font_head);
3659   int ystep_para  = getMenuTextStep(spacing_para,  font_text);
3660   int ystep_line  = getMenuTextStep(spacing_line,  font_text);
3661   int ystart  = mSY - SY + MENU_SCREEN_INFO_YSTART1;
3662   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
3663   int xstart1 = mSX - SX + 2 * xstep;
3664   int xstart2 = mSX - SX + 18 * xstep;
3665   int xstart3 = mSX - SX + 28 * xstep;
3666   SDL_version sdl_version_compiled;
3667   const SDL_version *sdl_version_linked;
3668   int driver_name_len = 10;
3669   SDL_version sdl_version_linked_ext;
3670   const char *driver_name = NULL;
3671
3672   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_VERSION);
3673
3674   FadeOut(REDRAW_FIELD);
3675
3676   ClearField();
3677   DrawHeadline();
3678
3679   DrawTextSCentered(ystart, font_title, "Version Information:");
3680   ystart += ystep_title;
3681
3682   DrawTextF(xstart1, ystart, font_head, "Name");
3683   DrawTextF(xstart2, ystart, font_text, getProgramTitleString());
3684   ystart += ystep_line;
3685
3686   if (!strEqual(getProgramVersionString(), getProgramRealVersionString()))
3687   {
3688     DrawTextF(xstart1, ystart, font_head, "Version (fake)");
3689     DrawTextF(xstart2, ystart, font_text, getProgramVersionString());
3690     ystart += ystep_line;
3691
3692     DrawTextF(xstart1, ystart, font_head, "Version (real)");
3693     DrawTextF(xstart2, ystart, font_text, getProgramRealVersionString());
3694     ystart += ystep_line;
3695   }
3696   else
3697   {
3698     DrawTextF(xstart1, ystart, font_head, "Version");
3699     DrawTextF(xstart2, ystart, font_text, getProgramVersionString());
3700     ystart += ystep_line;
3701   }
3702
3703   DrawTextF(xstart1, ystart, font_head, "Platform");
3704   DrawTextF(xstart2, ystart, font_text, "%s (%s)",
3705             PLATFORM_STRING,
3706             PLATFORM_XX_BIT_STRING);
3707   ystart += ystep_line;
3708
3709   DrawTextF(xstart1, ystart, font_head, "Target");
3710   DrawTextF(xstart2, ystart, font_text, TARGET_STRING);
3711   ystart += ystep_line;
3712
3713   DrawTextF(xstart1, ystart, font_head, "Source date");
3714   DrawTextF(xstart2, ystart, font_text, getSourceDateString());
3715   ystart += ystep_line;
3716
3717   DrawTextF(xstart1, ystart, font_head, "Commit hash");
3718   DrawTextF(xstart2, ystart, font_text, getSourceHashString());
3719   ystart += ystep_para;
3720
3721   DrawTextF(xstart1, ystart, font_head, "Library");
3722   DrawTextF(xstart2, ystart, font_head, "compiled");
3723   DrawTextF(xstart3, ystart, font_head, "linked");
3724   ystart += ystep_head;
3725
3726   SDL_VERSION(&sdl_version_compiled);
3727   SDL_GetVersion(&sdl_version_linked_ext);
3728   sdl_version_linked = &sdl_version_linked_ext;
3729
3730   DrawTextF(xstart1, ystart, font_text, "SDL");
3731   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3732             sdl_version_compiled.major,
3733             sdl_version_compiled.minor,
3734             sdl_version_compiled.patch);
3735   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3736             sdl_version_linked->major,
3737             sdl_version_linked->minor,
3738             sdl_version_linked->patch);
3739   ystart += ystep_line;
3740
3741   SDL_IMAGE_VERSION(&sdl_version_compiled);
3742   sdl_version_linked = IMG_Linked_Version();
3743
3744   DrawTextF(xstart1, ystart, font_text, "SDL_image");
3745   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3746             sdl_version_compiled.major,
3747             sdl_version_compiled.minor,
3748             sdl_version_compiled.patch);
3749   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3750             sdl_version_linked->major,
3751             sdl_version_linked->minor,
3752             sdl_version_linked->patch);
3753   ystart += ystep_line;
3754
3755   SDL_MIXER_VERSION(&sdl_version_compiled);
3756   sdl_version_linked = Mix_Linked_Version();
3757
3758   DrawTextF(xstart1, ystart, font_text, "SDL_mixer");
3759   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3760             sdl_version_compiled.major,
3761             sdl_version_compiled.minor,
3762             sdl_version_compiled.patch);
3763   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3764             sdl_version_linked->major,
3765             sdl_version_linked->minor,
3766             sdl_version_linked->patch);
3767   ystart += ystep_line;
3768
3769   SDL_NET_VERSION(&sdl_version_compiled);
3770   sdl_version_linked = SDLNet_Linked_Version();
3771
3772   DrawTextF(xstart1, ystart, font_text, "SDL_net");
3773   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3774             sdl_version_compiled.major,
3775             sdl_version_compiled.minor,
3776             sdl_version_compiled.patch);
3777   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3778             sdl_version_linked->major,
3779             sdl_version_linked->minor,
3780             sdl_version_linked->patch);
3781   ystart += ystep_para;
3782
3783   DrawTextF(xstart1, ystart, font_head, "Driver");
3784   DrawTextF(xstart2, ystart, font_head, "Requested");
3785   DrawTextF(xstart3, ystart, font_head, "Used");
3786   ystart += ystep_head;
3787
3788   driver_name =
3789     getStringCopyNStatic(SDLGetRendererName(), driver_name_len);
3790
3791   DrawTextF(xstart1, ystart, font_text, "Render Driver");
3792   DrawTextF(xstart2, ystart, font_text, "%s", setup.system.sdl_renderdriver);
3793   DrawTextF(xstart3, ystart, font_text, "%s", driver_name);
3794   ystart += ystep_line;
3795
3796   driver_name =
3797     getStringCopyNStatic(SDL_GetCurrentVideoDriver(), driver_name_len);
3798
3799   DrawTextF(xstart1, ystart, font_text, "Video Driver");
3800   DrawTextF(xstart2, ystart, font_text, "%s", setup.system.sdl_videodriver);
3801   DrawTextF(xstart3, ystart, font_text, "%s", driver_name);
3802   ystart += ystep_line;
3803
3804   driver_name =
3805     getStringCopyNStatic(SDL_GetCurrentAudioDriver(), driver_name_len);
3806
3807   DrawTextF(xstart1, ystart, font_text, "Audio Driver");
3808   DrawTextF(xstart2, ystart, font_text, "%s", setup.system.sdl_audiodriver);
3809   DrawTextF(xstart3, ystart, font_text, "%s", driver_name);
3810
3811   DrawTextSCentered(ybottom, font_foot,
3812                     "Press any key or button for info menu");
3813
3814   FadeIn(REDRAW_FIELD);
3815 }
3816
3817 void HandleInfoScreen_Version(int button)
3818 {
3819   if (button == MB_MENU_LEAVE)
3820   {
3821     PlaySound(SND_MENU_ITEM_SELECTING);
3822
3823     info_mode = INFO_MODE_MAIN;
3824     DrawInfoScreen();
3825
3826     return;
3827   }
3828   else if (button == MB_MENU_CHOICE)
3829   {
3830     PlaySound(SND_MENU_ITEM_SELECTING);
3831
3832     FadeMenuSoundsAndMusic();
3833
3834     info_mode = INFO_MODE_MAIN;
3835     DrawInfoScreen();
3836   }
3837   else
3838   {
3839     PlayMenuSoundIfLoop();
3840   }
3841 }
3842
3843 static void DrawInfoScreen_LevelSet(void)
3844 {
3845   struct TitleMessageInfo *tmi = &readme;
3846   char *filename = getLevelSetInfoFilename();
3847   char *title = "Level Set Information:";
3848   int ystart  = mSY - SY + MENU_SCREEN_INFO_YSTART1;
3849   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
3850
3851   if (filename == NULL)
3852   {
3853     DrawInfoScreen_NotAvailable(title, "No information for this level set.");
3854
3855     return;
3856   }
3857
3858   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_LEVELSET);
3859
3860   FadeOut(REDRAW_FIELD);
3861
3862   ClearField();
3863   DrawHeadline();
3864
3865   DrawTextSCentered(ystart, FONT_TEXT_1, title);
3866
3867   // if x position set to "-1", automatically determine by playfield width
3868   if (tmi->x == -1)
3869     tmi->x = SXSIZE / 2;
3870
3871   // if y position set to "-1", use static default value
3872   if (tmi->y == -1)
3873     tmi->y = 150;
3874
3875   // if width set to "-1", automatically determine by playfield width
3876   if (tmi->width == -1)
3877     tmi->width = SXSIZE - 2 * TILEX;
3878
3879   // if height set to "-1", automatically determine by playfield height
3880   if (tmi->height == -1)
3881     tmi->height = MENU_SCREEN_INFO_YBOTTOM - tmi->y - 10;
3882
3883   // if chars set to "-1", automatically determine by text and font width
3884   if (tmi->chars == -1)
3885     tmi->chars = tmi->width / getFontWidth(tmi->font);
3886   else
3887     tmi->width = tmi->chars * getFontWidth(tmi->font);
3888
3889   // if lines set to "-1", automatically determine by text and font height
3890   if (tmi->lines == -1)
3891     tmi->lines = tmi->height / getFontHeight(tmi->font);
3892   else
3893     tmi->height = tmi->lines * getFontHeight(tmi->font);
3894
3895   DrawTextFile(mSX + ALIGNED_TEXT_XPOS(tmi), mSY + ALIGNED_TEXT_YPOS(tmi),
3896                filename, tmi->font, tmi->chars, -1, tmi->lines, 0, -1,
3897                tmi->autowrap, tmi->centered, tmi->parse_comments);
3898
3899   DrawTextSCentered(ybottom, FONT_TEXT_4,
3900                     "Press any key or button for info menu");
3901
3902   FadeIn(REDRAW_FIELD);
3903 }
3904
3905 static void HandleInfoScreen_LevelSet(int button)
3906 {
3907   if (button == MB_MENU_LEAVE)
3908   {
3909     PlaySound(SND_MENU_ITEM_SELECTING);
3910
3911     info_mode = INFO_MODE_MAIN;
3912     DrawInfoScreen();
3913
3914     return;
3915   }
3916   else if (button == MB_MENU_CHOICE)
3917   {
3918     PlaySound(SND_MENU_ITEM_SELECTING);
3919
3920     FadeMenuSoundsAndMusic();
3921
3922     info_mode = INFO_MODE_MAIN;
3923     DrawInfoScreen();
3924   }
3925   else
3926   {
3927     PlayMenuSoundIfLoop();
3928   }
3929 }
3930
3931 static void DrawInfoScreen(void)
3932 {
3933   if (info_mode == INFO_MODE_TITLE)
3934     DrawInfoScreen_TitleScreen();
3935   else if (info_mode == INFO_MODE_ELEMENTS)
3936     DrawInfoScreen_Elements();
3937   else if (info_mode == INFO_MODE_MUSIC)
3938     DrawInfoScreen_Music();
3939   else if (info_mode == INFO_MODE_CREDITS)
3940     DrawInfoScreen_Credits();
3941   else if (info_mode == INFO_MODE_PROGRAM)
3942     DrawInfoScreen_Program();
3943   else if (info_mode == INFO_MODE_VERSION)
3944     DrawInfoScreen_Version();
3945   else if (info_mode == INFO_MODE_LEVELSET)
3946     DrawInfoScreen_LevelSet();
3947   else
3948     DrawInfoScreen_Main();
3949
3950   if (info_mode != INFO_MODE_MAIN &&
3951       info_mode != INFO_MODE_TITLE &&
3952       info_mode != INFO_MODE_MUSIC)
3953     PlayMenuSoundsAndMusic();
3954 }
3955
3956 void HandleInfoScreen(int mx, int my, int dx, int dy, int button)
3957 {
3958   if (info_mode == INFO_MODE_TITLE)
3959     HandleInfoScreen_TitleScreen(button);
3960   else if (info_mode == INFO_MODE_ELEMENTS)
3961     HandleInfoScreen_Elements(button);
3962   else if (info_mode == INFO_MODE_MUSIC)
3963     HandleInfoScreen_Music(button);
3964   else if (info_mode == INFO_MODE_CREDITS)
3965     HandleInfoScreen_Credits(button);
3966   else if (info_mode == INFO_MODE_PROGRAM)
3967     HandleInfoScreen_Program(button);
3968   else if (info_mode == INFO_MODE_VERSION)
3969     HandleInfoScreen_Version(button);
3970   else if (info_mode == INFO_MODE_LEVELSET)
3971     HandleInfoScreen_LevelSet(button);
3972   else
3973     HandleInfoScreen_Main(mx, my, dx, dy, button);
3974 }
3975
3976
3977 // ============================================================================
3978 // type name functions
3979 // ============================================================================
3980
3981 static void HandleTypeNameExt(boolean initialize, Key key)
3982 {
3983   static char last_player_name[MAX_PLAYER_NAME_LEN + 1];
3984   struct MainControlInfo *mci = getMainControlInfo(MAIN_CONTROL_NAME);
3985   struct TextPosInfo *pos = mci->pos_input;
3986   int sx = mSX + ALIGNED_TEXT_XPOS(pos);
3987   int sy = mSY + ALIGNED_TEXT_YPOS(pos);
3988   static int xpos = 0;
3989   int font_nr = pos->font;
3990   int font_active_nr = FONT_ACTIVE(font_nr);
3991   int font_width = getFontWidth(font_active_nr);
3992   char key_char = getValidConfigValueChar(getCharFromKey(key));
3993   boolean is_valid_key_char = (key_char != 0 && (key_char != ' ' || xpos > 0));
3994   boolean is_active = TRUE;
3995
3996   DrawBackgroundForFont(sx, sy, pos->width, pos->height, font_active_nr);
3997
3998   if (initialize)
3999   {
4000     strcpy(last_player_name, setup.player_name);
4001
4002     xpos = strlen(setup.player_name);
4003
4004     StartTextInput(sx, sy, pos->width, pos->height);
4005   }
4006   else if (is_valid_key_char && xpos < MAX_PLAYER_NAME_LEN)
4007   {
4008     setup.player_name[xpos] = key_char;
4009     setup.player_name[xpos + 1] = 0;
4010
4011     xpos++;
4012   }
4013   else if ((key == KSYM_Delete || key == KSYM_BackSpace) && xpos > 0)
4014   {
4015     xpos--;
4016
4017     setup.player_name[xpos] = 0;
4018   }
4019   else if (key == KSYM_Return && xpos > 0)
4020   {
4021     SaveSetup();
4022
4023     is_active = FALSE;
4024   }
4025   else if (key == KSYM_Escape)
4026   {
4027     strcpy(setup.player_name, last_player_name);
4028
4029     is_active = FALSE;
4030   }
4031
4032   if (is_active)
4033   {
4034     pos->width = (strlen(setup.player_name) + 1) * font_width;
4035     sx = mSX + ALIGNED_TEXT_XPOS(pos);
4036
4037     DrawText(sx, sy, setup.player_name, font_active_nr);
4038     DrawText(sx + xpos * font_width, sy, "_", font_active_nr);
4039   }
4040   else
4041   {
4042     SetGameStatus(GAME_MODE_MAIN);
4043
4044     pos->width = strlen(setup.player_name) * font_width;
4045     sx = mSX + ALIGNED_TEXT_XPOS(pos);
4046
4047     DrawText(sx, sy, setup.player_name, font_nr);
4048
4049     StopTextInput();
4050   }
4051 }
4052
4053 static void DrawTypeName(void)
4054 {
4055   HandleTypeNameExt(TRUE, 0);
4056 }
4057
4058 void HandleTypeName(Key key)
4059 {
4060   HandleTypeNameExt(FALSE, key);
4061 }
4062
4063
4064 // ============================================================================
4065 // tree menu functions
4066 // ============================================================================
4067
4068 static int getAlignXOffsetFromTreeInfo(TreeInfo *ti)
4069 {
4070   if (game_status != GAME_MODE_SETUP ||
4071       DRAW_MODE_SETUP(setup_mode) != SETUP_MODE_CHOOSE_OTHER)
4072     return 0;
4073
4074   int max_text_size = 0;
4075   TreeInfo *node;
4076
4077   for (node = getTreeInfoFirstGroupEntry(ti); node != NULL; node = node->next)
4078     max_text_size = MAX(max_text_size, strlen(node->name));
4079
4080   int num_entries = numTreeInfoInGroup(ti);
4081   boolean scrollbar_needed = (num_entries > NUM_MENU_ENTRIES_ON_SCREEN);
4082   int text_width = max_text_size * getFontWidth(FONT_TEXT_1);
4083   int button_width = SC_MENUBUTTON_XSIZE;
4084   int scrollbar_xpos = SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
4085   int screen_width = (scrollbar_needed ? scrollbar_xpos : SXSIZE);
4086   int align = menu.list_setup[SETUP_MODE_CHOOSE_OTHER].align;
4087   int x = ALIGNED_XPOS(0, screen_width, align) * -1;
4088   int align_xoffset_raw = ALIGNED_XPOS(x, button_width + text_width, align);
4089   int align_xoffset = MAX(0, align_xoffset_raw);
4090
4091   return align_xoffset;
4092 }
4093
4094 static int getAlignYOffsetFromTreeInfo(TreeInfo *ti)
4095 {
4096   if (game_status != GAME_MODE_SETUP ||
4097       DRAW_MODE_SETUP(setup_mode) != SETUP_MODE_CHOOSE_OTHER)
4098     return 0;
4099
4100   int num_entries = numTreeInfoInGroup(ti);
4101   int num_page_entries = MIN(num_entries, NUM_MENU_ENTRIES_ON_SCREEN);
4102   int font_height = getFontHeight(FONT_TEXT_1);
4103   int text_height = font_height * num_page_entries;
4104   int page_height = font_height * NUM_MENU_ENTRIES_ON_SCREEN;
4105   int align = menu.list_setup[SETUP_MODE_CHOOSE_OTHER].valign;
4106   int y = ALIGNED_YPOS(0, page_height, align) * -1;
4107   int align_yoffset_raw = ALIGNED_YPOS(y, text_height, align);
4108   int align_yoffset = MAX(0, align_yoffset_raw);
4109
4110   return align_yoffset;
4111 }
4112
4113 static void DrawChooseTree(TreeInfo **ti_ptr)
4114 {
4115   int fade_mask = REDRAW_FIELD;
4116
4117   if (CheckFadeAll())
4118     fade_mask = REDRAW_ALL;
4119
4120   if (strEqual((*ti_ptr)->subdir, STRING_TOP_DIRECTORY))
4121   {
4122     SetGameStatus(GAME_MODE_MAIN);
4123
4124     DrawMainMenu();
4125
4126     return;
4127   }
4128
4129   UnmapAllGadgets();
4130
4131   FreeScreenGadgets();
4132   CreateScreenGadgets();
4133
4134   FadeOut(fade_mask);
4135
4136   // needed if different viewport properties defined for choosing level (set)
4137   ChangeViewportPropertiesIfNeeded();
4138
4139   if (game_status == GAME_MODE_NAMES)
4140     SetMainBackgroundImage(IMG_BACKGROUND_NAMES);
4141   else if (game_status == GAME_MODE_LEVELNR)
4142     SetMainBackgroundImage(IMG_BACKGROUND_LEVELNR);
4143   else if (game_status == GAME_MODE_LEVELS)
4144     SetMainBackgroundImage(IMG_BACKGROUND_LEVELS);
4145
4146   ClearField();
4147
4148   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
4149
4150   HandleChooseTree(0, 0, 0, 0, MB_MENU_INITIALIZE, ti_ptr);
4151   MapScreenTreeGadgets(*ti_ptr);
4152
4153   DrawMaskedBorder(fade_mask);
4154
4155   FadeIn(fade_mask);
4156 }
4157
4158 static void drawChooseTreeList(int first_entry, int num_page_entries,
4159                                TreeInfo *ti)
4160 {
4161   int num_entries = numTreeInfoInGroup(ti);
4162   boolean scrollbar_needed = (num_entries > NUM_MENU_ENTRIES_ON_SCREEN);
4163   int scrollbar_xpos = SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
4164   int screen_width = (scrollbar_needed ? scrollbar_xpos : SXSIZE);
4165   int font_nr = FONT_TEXT_1;
4166   int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
4167   int i;
4168   char *title_string = NULL;
4169   int yoffset_sets = MENU_TITLE1_YPOS;
4170   int yoffset_setup = 16;
4171   int yoffset = (ti->type == TREE_TYPE_LEVEL_DIR ||
4172                  ti->type == TREE_TYPE_LEVEL_NR ? yoffset_sets : yoffset_setup);
4173
4174   title_string = ti->infotext;
4175
4176   DrawTextSCentered(mSY - SY + yoffset, FONT_TITLE_1, title_string);
4177
4178   clearMenuListArea();
4179
4180   for (i = 0; i < num_page_entries; i++)
4181   {
4182     TreeInfo *node, *node_first;
4183     int entry_pos = first_entry + i;
4184     int xpos = MENU_SCREEN_START_XPOS;
4185     int ypos = MENU_SCREEN_START_YPOS + i;
4186     int startx = amSX + xpos * 32;
4187     int starty = amSY + ypos * 32;
4188     int startx_text = startx + font_xoffset;
4189     int endx_text = amSX + screen_width;
4190     int max_text_size = endx_text - startx_text;
4191     int max_buffer_len = max_text_size / getFontWidth(font_nr);
4192     char buffer[max_buffer_len + 1];
4193
4194     node_first = getTreeInfoFirstGroupEntry(ti);
4195     node = getTreeInfoFromPos(node_first, entry_pos);
4196
4197     strncpy(buffer, node->name, max_buffer_len);
4198     buffer[max_buffer_len] = '\0';
4199
4200     DrawText(startx, starty, buffer, font_nr + node->color);
4201
4202     if (node->parent_link)
4203       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
4204     else if (node->level_group)
4205       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
4206     else
4207       initCursor(i, IMG_MENU_BUTTON);
4208   }
4209
4210   redraw_mask |= REDRAW_FIELD;
4211 }
4212
4213 static void drawChooseTreeInfo(int entry_pos, TreeInfo *ti)
4214 {
4215   TreeInfo *node, *node_first;
4216   int x, last_redraw_mask = redraw_mask;
4217   int ypos = MENU_TITLE2_YPOS;
4218   int font_nr = FONT_TITLE_2;
4219
4220   if (ti->type == TREE_TYPE_LEVEL_NR)
4221     DrawTextFCentered(ypos, font_nr, leveldir_current->name);
4222
4223   if (ti->type != TREE_TYPE_LEVEL_DIR)
4224     return;
4225
4226   node_first = getTreeInfoFirstGroupEntry(ti);
4227   node = getTreeInfoFromPos(node_first, entry_pos);
4228
4229   DrawBackgroundForFont(SX, SY + ypos, SXSIZE, getFontHeight(font_nr), font_nr);
4230
4231   if (node->parent_link)
4232     DrawTextFCentered(ypos, font_nr, "leave \"%s\"",
4233                       node->node_parent->name);
4234   else if (node->level_group)
4235     DrawTextFCentered(ypos, font_nr, "enter \"%s\"",
4236                       node->name);
4237   else if (ti->type == TREE_TYPE_LEVEL_DIR)
4238     DrawTextFCentered(ypos, font_nr, "%3d %s (%s)",
4239                       node->levels, (node->levels > 1 ? "levels" : "level"),
4240                       node->class_desc);
4241
4242   // let BackToFront() redraw only what is needed
4243   redraw_mask = last_redraw_mask;
4244   for (x = 0; x < SCR_FIELDX; x++)
4245     MarkTileDirty(x, 1);
4246 }
4247
4248 static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
4249                              TreeInfo **ti_ptr)
4250 {
4251   TreeInfo *ti = *ti_ptr;
4252   boolean has_scrollbar = screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->mapped;
4253   int mx_scrollbar = screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x;
4254   int mx_right_border = (has_scrollbar ? mx_scrollbar : SX + SXSIZE);
4255   int x = 0;
4256   int y = ti->cl_cursor;
4257   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
4258   int num_entries = numTreeInfoInGroup(ti);
4259   int num_page_entries = MIN(num_entries, NUM_MENU_ENTRIES_ON_SCREEN);
4260   boolean position_set_by_scrollbar = (dx == 999);
4261
4262   if (button == MB_MENU_INITIALIZE)
4263   {
4264     int num_entries = numTreeInfoInGroup(ti);
4265     int entry_pos = posTreeInfo(ti);
4266
4267     align_xoffset = getAlignXOffsetFromTreeInfo(ti);
4268     align_yoffset = getAlignYOffsetFromTreeInfo(ti);
4269
4270     if (ti->cl_first == -1)
4271     {
4272       // only on initialization
4273       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
4274       ti->cl_cursor = entry_pos - ti->cl_first;
4275     }
4276     else if (ti->cl_cursor >= num_page_entries ||
4277              (num_entries > num_page_entries &&
4278               num_entries - ti->cl_first < num_page_entries))
4279     {
4280       // only after change of list size (by custom graphic configuration)
4281       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
4282       ti->cl_cursor = entry_pos - ti->cl_first;
4283     }
4284
4285     if (position_set_by_scrollbar)
4286       ti->cl_first = dy;
4287     else
4288       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
4289                                 ti->cl_first, ti);
4290
4291     drawChooseTreeList(ti->cl_first, num_page_entries, ti);
4292     drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
4293     drawChooseTreeCursor(ti->cl_cursor, TRUE);
4294
4295     return;
4296   }
4297   else if (button == MB_MENU_LEAVE)
4298   {
4299     FadeSetLeaveMenu();
4300
4301     PlaySound(SND_MENU_ITEM_SELECTING);
4302
4303     if (ti->node_parent)
4304     {
4305       *ti_ptr = ti->node_parent;
4306       DrawChooseTree(ti_ptr);
4307     }
4308     else if (game_status == GAME_MODE_SETUP)
4309     {
4310       if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
4311           setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY ||
4312           setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
4313         execSetupGame();
4314       else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
4315                setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
4316                setup_mode == SETUP_MODE_CHOOSE_RENDERING ||
4317                setup_mode == SETUP_MODE_CHOOSE_VSYNC)
4318         execSetupGraphics();
4319       else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
4320                setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
4321                setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
4322         execSetupSound();
4323       else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
4324                setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
4325                setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE ||
4326                setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY ||
4327                setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_0 ||
4328                setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_0 ||
4329                setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_1 ||
4330                setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_1)
4331         execSetupTouch();
4332       else
4333         execSetupArtwork();
4334     }
4335     else
4336     {
4337       if (game_status == GAME_MODE_LEVELNR)
4338       {
4339         int new_level_nr = atoi(level_number_current->identifier);
4340
4341         HandleMainMenu_SelectLevel(0, 0, new_level_nr);
4342       }
4343
4344       SetGameStatus(GAME_MODE_MAIN);
4345
4346       DrawMainMenu();
4347     }
4348
4349     return;
4350   }
4351
4352   if (mx || my)         // mouse input
4353   {
4354     x = (mx - amSX) / 32;
4355     y = (my - amSY) / 32 - MENU_SCREEN_START_YPOS;
4356   }
4357   else if (dx || dy)    // keyboard or scrollbar/scrollbutton input
4358   {
4359     // move cursor instead of scrolling when already at start/end of list
4360     if (dy == -1 * SCROLL_LINE && ti->cl_first == 0)
4361       dy = -1;
4362     else if (dy == +1 * SCROLL_LINE &&
4363              ti->cl_first + num_page_entries == num_entries)
4364       dy = 1;
4365
4366     // handle scrolling screen one line or page
4367     if (ti->cl_cursor + dy < 0 ||
4368         ti->cl_cursor + dy > num_page_entries - 1)
4369     {
4370       boolean redraw = FALSE;
4371
4372       if (ABS(dy) == SCROLL_PAGE)
4373         step = num_page_entries - 1;
4374
4375       if (dy < 0 && ti->cl_first > 0)
4376       {
4377         // scroll page/line up
4378
4379         ti->cl_first -= step;
4380         if (ti->cl_first < 0)
4381           ti->cl_first = 0;
4382
4383         redraw = TRUE;
4384       }
4385       else if (dy > 0 && ti->cl_first + num_page_entries < num_entries)
4386       {
4387         // scroll page/line down
4388
4389         ti->cl_first += step;
4390         if (ti->cl_first + num_page_entries > num_entries)
4391           ti->cl_first = MAX(0, num_entries - num_page_entries);
4392
4393         redraw = TRUE;
4394       }
4395
4396       if (redraw)
4397       {
4398         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
4399         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
4400         drawChooseTreeCursor(ti->cl_cursor, TRUE);
4401
4402         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
4403                                   ti->cl_first, ti);
4404       }
4405
4406       return;
4407     }
4408
4409     // handle moving cursor one line
4410     y = ti->cl_cursor + dy;
4411   }
4412
4413   if (dx == 1)
4414   {
4415     TreeInfo *node_first, *node_cursor;
4416     int entry_pos = ti->cl_first + y;
4417
4418     node_first = getTreeInfoFirstGroupEntry(ti);
4419     node_cursor = getTreeInfoFromPos(node_first, entry_pos);
4420
4421     if (node_cursor->node_group)
4422     {
4423       FadeSetEnterMenu();
4424
4425       PlaySound(SND_MENU_ITEM_SELECTING);
4426
4427       node_cursor->cl_first = ti->cl_first;
4428       node_cursor->cl_cursor = ti->cl_cursor;
4429       *ti_ptr = node_cursor->node_group;
4430       DrawChooseTree(ti_ptr);
4431
4432       return;
4433     }
4434   }
4435   else if (dx == -1 && ti->node_parent)
4436   {
4437     FadeSetLeaveMenu();
4438
4439     PlaySound(SND_MENU_ITEM_SELECTING);
4440
4441     *ti_ptr = ti->node_parent;
4442     DrawChooseTree(ti_ptr);
4443
4444     return;
4445   }
4446
4447   if (!anyScrollbarGadgetActive() &&
4448       IN_VIS_MENU(x, y) &&
4449       mx < mx_right_border &&
4450       y >= 0 && y < num_page_entries)
4451   {
4452     if (button)
4453     {
4454       if (y != ti->cl_cursor)
4455       {
4456         PlaySound(SND_MENU_ITEM_ACTIVATING);
4457
4458         drawChooseTreeCursor(ti->cl_cursor, FALSE);
4459         drawChooseTreeCursor(y, TRUE);
4460         drawChooseTreeInfo(ti->cl_first + y, ti);
4461
4462         ti->cl_cursor = y;
4463       }
4464       else if (dx < 0)
4465       {
4466         if (game_status == GAME_MODE_SETUP)
4467         {
4468           if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
4469               setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY ||
4470               setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
4471             execSetupGame();
4472           else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
4473                    setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
4474                    setup_mode == SETUP_MODE_CHOOSE_RENDERING ||
4475                    setup_mode == SETUP_MODE_CHOOSE_VSYNC)
4476             execSetupGraphics();
4477           else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
4478                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
4479                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
4480             execSetupSound();
4481           else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
4482                    setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
4483                    setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE ||
4484                    setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY ||
4485                    setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_0 ||
4486                    setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_0 ||
4487                    setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_1 ||
4488                    setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_1)
4489             execSetupTouch();
4490           else
4491             execSetupArtwork();
4492         }
4493       }
4494     }
4495     else
4496     {
4497       TreeInfo *node_first, *node_cursor;
4498       int entry_pos = ti->cl_first + y;
4499
4500       PlaySound(SND_MENU_ITEM_SELECTING);
4501
4502       node_first = getTreeInfoFirstGroupEntry(ti);
4503       node_cursor = getTreeInfoFromPos(node_first, entry_pos);
4504
4505       if (node_cursor->node_group)
4506       {
4507         FadeSetEnterMenu();
4508
4509         node_cursor->cl_first = ti->cl_first;
4510         node_cursor->cl_cursor = ti->cl_cursor;
4511         *ti_ptr = node_cursor->node_group;
4512         DrawChooseTree(ti_ptr);
4513       }
4514       else if (node_cursor->parent_link)
4515       {
4516         FadeSetLeaveMenu();
4517
4518         *ti_ptr = node_cursor->node_parent;
4519         DrawChooseTree(ti_ptr);
4520       }
4521       else
4522       {
4523         FadeSetEnterMenu();
4524
4525         node_cursor->cl_first = ti->cl_first;
4526         node_cursor->cl_cursor = ti->cl_cursor;
4527         *ti_ptr = node_cursor;
4528
4529         if (ti->type == TREE_TYPE_LEVEL_DIR)
4530         {
4531           LoadLevelSetup_SeriesInfo();
4532
4533           SaveLevelSetup_LastSeries();
4534           SaveLevelSetup_SeriesInfo();
4535           TapeErase();
4536         }
4537
4538         if (game_status == GAME_MODE_SETUP)
4539         {
4540           if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
4541               setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY ||
4542               setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
4543             execSetupGame();
4544           else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
4545                    setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
4546                    setup_mode == SETUP_MODE_CHOOSE_RENDERING ||
4547                    setup_mode == SETUP_MODE_CHOOSE_VSYNC)
4548             execSetupGraphics();
4549           else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
4550                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
4551                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
4552             execSetupSound();
4553           else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
4554                    setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
4555                    setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE ||
4556                    setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY ||
4557                    setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_0 ||
4558                    setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_0 ||
4559                    setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_1 ||
4560                    setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_1)
4561             execSetupTouch();
4562           else
4563             execSetupArtwork();
4564         }
4565         else
4566         {
4567           if (game_status == GAME_MODE_LEVELNR)
4568           {
4569             int new_level_nr = atoi(level_number_current->identifier);
4570
4571             HandleMainMenu_SelectLevel(0, 0, new_level_nr);
4572           }
4573
4574           SetGameStatus(GAME_MODE_MAIN);
4575
4576           DrawMainMenu();
4577         }
4578       }
4579     }
4580   }
4581 }
4582
4583 void DrawChoosePlayerName(void)
4584 {
4585   int i;
4586
4587   FadeMenuSoundsAndMusic();
4588
4589   if (player_name != NULL)
4590   {
4591     freeTreeInfo(player_name);
4592
4593     player_name = NULL;
4594   }
4595
4596   for (i = 0; i < MAX_PLAYER_NAMES; i++)
4597   {
4598     boolean team_mode = (!network.enabled && setup.team_mode);
4599     int tree_type = (team_mode ? TREE_TYPE_PLAYER_TEAM : TREE_TYPE_PLAYER_NAME);
4600     TreeInfo *ti = newTreeInfo_setDefaults(tree_type);
4601     char identifier[32], name[MAX_PLAYER_NAME_LEN + 1];
4602     int value = i;
4603
4604     ti->node_top = &player_name;
4605     ti->sort_priority = 10000 + value;
4606     ti->color = FC_RED;
4607
4608     if (strEqual(global.user_names[i], EMPTY_PLAYER_NAME))
4609       ti->color = FC_BLUE;
4610
4611     snprintf(identifier, sizeof(identifier), "%d", value);
4612     snprintf(name, sizeof(name), "%s", global.user_names[i]);
4613
4614     setString(&ti->identifier, identifier);
4615     setString(&ti->name, name);
4616     setString(&ti->name_sorting, name);
4617
4618     pushTreeInfo(&player_name, ti);
4619   }
4620
4621   // sort player entries by player number
4622   sortTreeInfo(&player_name);
4623
4624   // set current player entry to selected player entry
4625   player_name_current =
4626     getTreeInfoFromIdentifier(player_name, i_to_a(user.nr));
4627
4628   // if that fails, set current player name to first available name
4629   if (player_name_current == NULL)
4630     player_name_current = player_name;
4631
4632   DrawChooseTree(&player_name_current);
4633
4634   PlayMenuSoundsAndMusic();
4635 }
4636
4637 void HandleChoosePlayerName(int mx, int my, int dx, int dy, int button)
4638 {
4639   HandleChooseTree(mx, my, dx, dy, button, &player_name_current);
4640 }
4641
4642 void DrawChooseLevelSet(void)
4643 {
4644   FadeMenuSoundsAndMusic();
4645
4646   DrawChooseTree(&leveldir_current);
4647
4648   PlayMenuSoundsAndMusic();
4649 }
4650
4651 void HandleChooseLevelSet(int mx, int my, int dx, int dy, int button)
4652 {
4653   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
4654 }
4655
4656 void DrawChooseLevelNr(void)
4657 {
4658   int i;
4659
4660   FadeMenuSoundsAndMusic();
4661
4662   if (level_number != NULL)
4663   {
4664     freeTreeInfo(level_number);
4665
4666     level_number = NULL;
4667   }
4668
4669   for (i = leveldir_current->first_level; i <= leveldir_current->last_level;i++)
4670   {
4671     TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_LEVEL_NR);
4672     char identifier[32], name[64];
4673     int value = i;
4674
4675     // temporarily load level info to get level name
4676     LoadLevelInfoOnly(i);
4677
4678     ti->node_top = &level_number;
4679     ti->sort_priority = 10000 + value;
4680     ti->color = (level.no_level_file ? FC_BLUE :
4681                  LevelStats_getSolved(i) ? FC_GREEN :
4682                  LevelStats_getPlayed(i) ? FC_YELLOW : FC_RED);
4683
4684     snprintf(identifier, sizeof(identifier), "%d", value);
4685     snprintf(name, sizeof(name), "%03d: %s", value,
4686              (level.no_level_file ? "(no file)" : level.name));
4687
4688     setString(&ti->identifier, identifier);
4689     setString(&ti->name, name);
4690     setString(&ti->name_sorting, name);
4691
4692     pushTreeInfo(&level_number, ti);
4693   }
4694
4695   // sort level number values to start with lowest level number
4696   sortTreeInfo(&level_number);
4697
4698   // set current level number to current level number
4699   level_number_current =
4700     getTreeInfoFromIdentifier(level_number, i_to_a(level_nr));
4701
4702   // if that also fails, set current level number to first available level
4703   if (level_number_current == NULL)
4704     level_number_current = level_number;
4705
4706   DrawChooseTree(&level_number_current);
4707
4708   PlayMenuSoundsAndMusic();
4709 }
4710
4711 void HandleChooseLevelNr(int mx, int my, int dx, int dy, int button)
4712 {
4713   HandleChooseTree(mx, my, dx, dy, button, &level_number_current);
4714 }
4715
4716 void DrawHallOfFame(int level_nr, int highlight_position)
4717 {
4718   int fade_mask = REDRAW_FIELD;
4719
4720   if (CheckFadeAll())
4721     fade_mask = REDRAW_ALL;
4722
4723   UnmapAllGadgets();
4724   FadeMenuSoundsAndMusic();
4725
4726   // (this is needed when called from GameEnd() after winning a game)
4727   KeyboardAutoRepeatOn();
4728
4729   // (this is needed when called from GameEnd() after winning a game)
4730   SetDrawDeactivationMask(REDRAW_NONE);
4731   SetDrawBackgroundMask(REDRAW_FIELD);
4732
4733   if (highlight_position < 0) 
4734     LoadScore(level_nr);
4735   else
4736     SetAnimStatus(GAME_MODE_PSEUDO_SCORESNEW);
4737
4738   FadeSetEnterScreen();
4739
4740   FadeOut(fade_mask);
4741
4742   // needed if different viewport properties defined for scores
4743   ChangeViewportPropertiesIfNeeded();
4744
4745   PlayMenuSoundsAndMusic();
4746
4747   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
4748
4749   HandleHallOfFame(level_nr, highlight_position, 0, 0, MB_MENU_INITIALIZE);
4750
4751   DrawMaskedBorder(fade_mask);
4752
4753   FadeIn(fade_mask);
4754 }
4755
4756 static void drawHallOfFameList(int level_nr, int first_entry,
4757                                int highlight_position)
4758 {
4759   int i, j;
4760
4761   SetMainBackgroundImage(IMG_BACKGROUND_SCORES);
4762   ClearField();
4763
4764   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, "Hall Of Fame");
4765   DrawTextFCentered(MENU_TITLE2_YPOS, FONT_TITLE_2,
4766                     "HighScores of Level %d", level_nr);
4767
4768   for (i = 0; i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
4769   {
4770     int entry = first_entry + i;
4771     boolean active = (entry == highlight_position);
4772     int font_nr1 = (active ? FONT_TEXT_1_ACTIVE : FONT_TEXT_1);
4773     int font_nr2 = (active ? FONT_TEXT_2_ACTIVE : FONT_TEXT_2);
4774     int font_nr3 = (active ? FONT_TEXT_3_ACTIVE : FONT_TEXT_3);
4775     int font_nr4 = (active ? FONT_TEXT_4_ACTIVE : FONT_TEXT_4);
4776     int dxoff = getFontDrawOffsetX(font_nr1);
4777     int dx1 = 3 * getFontWidth(font_nr1);
4778     int dx2 = dx1 + getFontWidth(font_nr1);
4779     int dx3 = SXSIZE - 2 * (mSX - SX + dxoff) - 5 * getFontWidth(font_nr4);
4780     int num_dots = (dx3 - dx2) / getFontWidth(font_nr3);
4781     int sy = mSY + 64 + i * 32;
4782
4783     DrawText(mSX, sy, int2str(entry + 1, 3), font_nr1);
4784     DrawText(mSX + dx1, sy, ".", font_nr1);
4785
4786     for (j = 0; j < num_dots; j++)
4787       DrawText(mSX + dx2 + j * getFontWidth(font_nr3), sy, ".", font_nr3);
4788
4789     if (!strEqual(highscore[entry].Name, EMPTY_PLAYER_NAME))
4790       DrawText(mSX + dx2, sy, highscore[entry].Name, font_nr2);
4791
4792     DrawText(mSX + dx3, sy, int2str(highscore[entry].Score, 5), font_nr4);
4793   }
4794
4795   redraw_mask |= REDRAW_FIELD;
4796 }
4797
4798 void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
4799 {
4800   static int level_nr = 0;
4801   static int first_entry = 0;
4802   static int highlight_position = 0;
4803   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
4804
4805   if (button == MB_MENU_INITIALIZE)
4806   {
4807     level_nr = mx;
4808     first_entry = 0;
4809     highlight_position = my;
4810
4811     drawHallOfFameList(level_nr, first_entry, highlight_position);
4812
4813     return;
4814   }
4815
4816   if (ABS(dy) == SCROLL_PAGE)           // handle scrolling one page
4817     step = NUM_MENU_ENTRIES_ON_SCREEN - 1;
4818
4819   if (dy < 0)
4820   {
4821     if (first_entry > 0)
4822     {
4823       first_entry -= step;
4824       if (first_entry < 0)
4825         first_entry = 0;
4826
4827       drawHallOfFameList(level_nr, first_entry, highlight_position);
4828     }
4829   }
4830   else if (dy > 0)
4831   {
4832     if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
4833     {
4834       first_entry += step;
4835       if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
4836         first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
4837
4838       drawHallOfFameList(level_nr, first_entry, highlight_position);
4839     }
4840   }
4841   else if (button == MB_MENU_LEAVE || button == MB_MENU_CHOICE)
4842   {
4843     PlaySound(SND_MENU_ITEM_SELECTING);
4844
4845     FadeSound(SND_BACKGROUND_SCORES);
4846
4847     if (button == MB_MENU_CHOICE &&
4848         game_status_last_screen == GAME_MODE_PLAYING &&
4849         setup.auto_play_next_level && setup.increment_levels &&
4850         level_nr < leveldir_current->last_level &&
4851         !network_playing)
4852     {
4853       StartGameActions(network.enabled, setup.autorecord, level.random_seed);
4854     }
4855     else
4856     {
4857       SetGameStatus(GAME_MODE_MAIN);
4858
4859       DrawMainMenu();
4860     }
4861   }
4862
4863   if (game_status == GAME_MODE_SCORES)
4864     PlayMenuSoundIfLoop();
4865 }
4866
4867
4868 // ============================================================================
4869 // setup screen functions
4870 // ============================================================================
4871
4872 static struct TokenInfo *setup_info;
4873 static int num_setup_info;      // number of setup entries shown on screen
4874 static int max_setup_info;      // total number of setup entries in list
4875
4876 static char *window_size_text;
4877 static char *scaling_type_text;
4878 static char *rendering_mode_text;
4879 static char *vsync_mode_text;
4880 static char *scroll_delay_text;
4881 static char *snapshot_mode_text;
4882 static char *game_speed_text;
4883 static char *network_server_text;
4884 static char *graphics_set_name;
4885 static char *sounds_set_name;
4886 static char *music_set_name;
4887 static char *volume_simple_text;
4888 static char *volume_loops_text;
4889 static char *volume_music_text;
4890 static char *touch_controls_text;
4891 static char *move_distance_text;
4892 static char *drop_distance_text;
4893 static char *transparency_text;
4894 static char *grid_size_text[2][2];
4895
4896 static void execSetupMain(void)
4897 {
4898   setup_mode = SETUP_MODE_MAIN;
4899
4900   DrawSetupScreen();
4901 }
4902
4903 static void execSetupGame_setGameSpeeds(boolean update_value)
4904 {
4905   if (setup.game_speed_extended)
4906   {
4907     game_speeds_list = game_speeds_list_extended;
4908     game_speeds      = game_speeds_extended;
4909   }
4910   else
4911   {
4912     game_speeds_list = game_speeds_list_normal;
4913     game_speeds      = game_speeds_normal;
4914   }
4915
4916   if (game_speeds == NULL)
4917   {
4918     int i;
4919
4920     for (i = 0; game_speeds_list[i].value != -1; i++)
4921     {
4922       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4923       char identifier[32], name[32];
4924       int value = game_speeds_list[i].value;
4925       char *text = game_speeds_list[i].text;
4926
4927       ti->node_top = &game_speeds;
4928       ti->sort_priority = 10000 - value;
4929
4930       sprintf(identifier, "%d", value);
4931       sprintf(name, "%s", text);
4932
4933       setString(&ti->identifier, identifier);
4934       setString(&ti->name, name);
4935       setString(&ti->name_sorting, name);
4936       setString(&ti->infotext, STR_SETUP_CHOOSE_GAME_SPEED);
4937
4938       pushTreeInfo(&game_speeds, ti);
4939     }
4940
4941     // sort game speed values to start with slowest game speed
4942     sortTreeInfo(&game_speeds);
4943
4944     update_value = TRUE;
4945   }
4946
4947   if (update_value)
4948   {
4949     // set current game speed to configured game speed value
4950     game_speed_current =
4951       getTreeInfoFromIdentifier(game_speeds, i_to_a(setup.game_frame_delay));
4952
4953     // if that fails, set current game speed to reliable default value
4954     if (game_speed_current == NULL)
4955       game_speed_current =
4956         getTreeInfoFromIdentifier(game_speeds, i_to_a(GAME_FRAME_DELAY));
4957
4958     // if that also fails, set current game speed to first available speed
4959     if (game_speed_current == NULL)
4960       game_speed_current = game_speeds;
4961
4962     if (setup.game_speed_extended)
4963       game_speeds_extended = game_speeds;
4964     else
4965       game_speeds_normal = game_speeds;
4966   }
4967
4968   setup.game_frame_delay = atoi(game_speed_current->identifier);
4969
4970   // needed for displaying game speed text instead of identifier
4971   game_speed_text = game_speed_current->name;
4972 }
4973
4974 static void execSetupGame_setScrollDelays(void)
4975 {
4976   if (scroll_delays == NULL)
4977   {
4978     int i;
4979
4980     for (i = 0; scroll_delays_list[i].value != -1; i++)
4981     {
4982       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4983       char identifier[32], name[32];
4984       int value = scroll_delays_list[i].value;
4985       char *text = scroll_delays_list[i].text;
4986
4987       ti->node_top = &scroll_delays;
4988       ti->sort_priority = value;
4989
4990       sprintf(identifier, "%d", value);
4991       sprintf(name, "%s", text);
4992
4993       setString(&ti->identifier, identifier);
4994       setString(&ti->name, name);
4995       setString(&ti->name_sorting, name);
4996       setString(&ti->infotext, STR_SETUP_CHOOSE_SCROLL_DELAY);
4997
4998       pushTreeInfo(&scroll_delays, ti);
4999     }
5000
5001     // sort scroll delay values to start with lowest scroll delay value
5002     sortTreeInfo(&scroll_delays);
5003
5004     // set current scroll delay value to configured scroll delay value
5005     scroll_delay_current =
5006       getTreeInfoFromIdentifier(scroll_delays,i_to_a(setup.scroll_delay_value));
5007
5008     // if that fails, set current scroll delay to reliable default value
5009     if (scroll_delay_current == NULL)
5010       scroll_delay_current =
5011         getTreeInfoFromIdentifier(scroll_delays, i_to_a(STD_SCROLL_DELAY));
5012
5013     // if that also fails, set current scroll delay to first available value
5014     if (scroll_delay_current == NULL)
5015       scroll_delay_current = scroll_delays;
5016   }
5017
5018   setup.scroll_delay_value = atoi(scroll_delay_current->identifier);
5019
5020   // needed for displaying scroll delay text instead of identifier
5021   scroll_delay_text = scroll_delay_current->name;
5022 }
5023
5024 static void execSetupGame_setSnapshotModes(void)
5025 {
5026   if (snapshot_modes == NULL)
5027   {
5028     int i;
5029
5030     for (i = 0; snapshot_modes_list[i].value != NULL; i++)
5031     {
5032       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5033       char identifier[32], name[32];
5034       char *value = snapshot_modes_list[i].value;
5035       char *text = snapshot_modes_list[i].text;
5036
5037       ti->node_top = &snapshot_modes;
5038       ti->sort_priority = i;
5039
5040       sprintf(identifier, "%s", value);
5041       sprintf(name, "%s", text);
5042
5043       setString(&ti->identifier, identifier);
5044       setString(&ti->name, name);
5045       setString(&ti->name_sorting, name);
5046       setString(&ti->infotext, STR_SETUP_CHOOSE_SNAPSHOT_MODE);
5047
5048       pushTreeInfo(&snapshot_modes, ti);
5049     }
5050
5051     // sort snapshot mode values to start with lowest snapshot mode value
5052     sortTreeInfo(&snapshot_modes);
5053
5054     // set current snapshot mode value to configured snapshot mode value
5055     snapshot_mode_current =
5056       getTreeInfoFromIdentifier(snapshot_modes, setup.engine_snapshot_mode);
5057
5058     // if that fails, set current snapshot mode to reliable default value
5059     if (snapshot_mode_current == NULL)
5060       snapshot_mode_current =
5061         getTreeInfoFromIdentifier(snapshot_modes, STR_SNAPSHOT_MODE_DEFAULT);
5062
5063     // if that also fails, set current snapshot mode to first available value
5064     if (snapshot_mode_current == NULL)
5065       snapshot_mode_current = snapshot_modes;
5066   }
5067
5068   setup.engine_snapshot_mode = snapshot_mode_current->identifier;
5069
5070   // needed for displaying snapshot mode text instead of identifier
5071   snapshot_mode_text = snapshot_mode_current->name;
5072 }
5073
5074 static void execSetupGame_setNetworkServerText(void)
5075 {
5076   if (strEqual(setup.network_server_hostname, STR_NETWORK_AUTO_DETECT))
5077   {
5078     strcpy(network_server_hostname, STR_NETWORK_AUTO_DETECT_SETUP);
5079   }
5080   else
5081   {
5082     strncpy(network_server_hostname, setup.network_server_hostname,
5083             MAX_SETUP_TEXT_INPUT_LEN);
5084     network_server_hostname[MAX_SETUP_TEXT_INPUT_LEN] = '\0';
5085   }
5086
5087   // needed for displaying network server text instead of identifier
5088   network_server_text = network_server_hostname;
5089 }
5090
5091 static void execSetupGame(void)
5092 {
5093   boolean check_vsync_mode = (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED);
5094
5095   execSetupGame_setGameSpeeds(FALSE);
5096   execSetupGame_setScrollDelays();
5097   execSetupGame_setSnapshotModes();
5098
5099   execSetupGame_setNetworkServerText();
5100
5101   setup_mode = SETUP_MODE_GAME;
5102
5103   DrawSetupScreen();
5104
5105   // check if vsync needs to be disabled for this game speed to work
5106   if (check_vsync_mode)
5107     DisableVsyncIfNeeded();
5108 }
5109
5110 static void execSetupChooseGameSpeed(void)
5111 {
5112   setup_mode = SETUP_MODE_CHOOSE_GAME_SPEED;
5113
5114   DrawSetupScreen();
5115 }
5116
5117 static void execSetupChooseScrollDelay(void)
5118 {
5119   setup_mode = SETUP_MODE_CHOOSE_SCROLL_DELAY;
5120
5121   DrawSetupScreen();
5122 }
5123
5124 static void execSetupChooseSnapshotMode(void)
5125 {
5126   setup_mode = SETUP_MODE_CHOOSE_SNAPSHOT_MODE;
5127
5128   DrawSetupScreen();
5129 }
5130
5131 static void execSetupEngines(void)
5132 {
5133   setup_mode = SETUP_MODE_ENGINES;
5134
5135   DrawSetupScreen();
5136 }
5137
5138 static void execSetupEditor(void)
5139 {
5140   setup_mode = SETUP_MODE_EDITOR;
5141
5142   DrawSetupScreen();
5143 }
5144
5145 static void execSetupGraphics_setWindowSizes(boolean update_list)
5146 {
5147   if (window_sizes != NULL && update_list)
5148   {
5149     freeTreeInfo(window_sizes);
5150
5151     window_sizes = NULL;
5152   }
5153
5154   if (window_sizes == NULL)
5155   {
5156     boolean current_window_size_found = FALSE;
5157     int i;
5158
5159     for (i = 0; window_sizes_list[i].value != -1; i++)
5160     {
5161       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5162       char identifier[32], name[32];
5163       int value = window_sizes_list[i].value;
5164       char *text = window_sizes_list[i].text;
5165
5166       ti->node_top = &window_sizes;
5167       ti->sort_priority = value;
5168
5169       sprintf(identifier, "%d", value);
5170       sprintf(name, "%s", text);
5171
5172       setString(&ti->identifier, identifier);
5173       setString(&ti->name, name);
5174       setString(&ti->name_sorting, name);
5175       setString(&ti->infotext, STR_SETUP_CHOOSE_WINDOW_SIZE);
5176
5177       pushTreeInfo(&window_sizes, ti);
5178
5179       if (value == setup.window_scaling_percent)
5180         current_window_size_found = TRUE;
5181     }
5182
5183     if (!current_window_size_found)
5184     {
5185       // add entry for non-preset window scaling value
5186
5187       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5188       char identifier[32], name[32];
5189       int value = setup.window_scaling_percent;
5190
5191       ti->node_top = &window_sizes;
5192       ti->sort_priority = value;
5193
5194       sprintf(identifier, "%d", value);
5195       sprintf(name, "%d %% (Current)", value);
5196
5197       setString(&ti->identifier, identifier);
5198       setString(&ti->name, name);
5199       setString(&ti->name_sorting, name);
5200       setString(&ti->infotext, STR_SETUP_CHOOSE_WINDOW_SIZE);
5201
5202       pushTreeInfo(&window_sizes, ti);
5203     }
5204
5205     // sort window size values to start with lowest window size value
5206     sortTreeInfo(&window_sizes);
5207
5208     // set current window size value to configured window size value
5209     window_size_current =
5210       getTreeInfoFromIdentifier(window_sizes,
5211                                 i_to_a(setup.window_scaling_percent));
5212
5213     // if that fails, set current window size to reliable default value
5214     if (window_size_current == NULL)
5215       window_size_current =
5216         getTreeInfoFromIdentifier(window_sizes,
5217                                   i_to_a(STD_WINDOW_SCALING_PERCENT));
5218
5219     // if that also fails, set current window size to first available value
5220     if (window_size_current == NULL)
5221       window_size_current = window_sizes;
5222   }
5223
5224   setup.window_scaling_percent = atoi(window_size_current->identifier);
5225
5226   // needed for displaying window size text instead of identifier
5227   window_size_text = window_size_current->name;
5228 }
5229
5230 static void execSetupGraphics_setScalingTypes(void)
5231 {
5232   if (scaling_types == NULL)
5233   {
5234     int i;
5235
5236     for (i = 0; scaling_types_list[i].value != NULL; i++)
5237     {
5238       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5239       char identifier[32], name[32];
5240       char *value = scaling_types_list[i].value;
5241       char *text = scaling_types_list[i].text;
5242
5243       ti->node_top = &scaling_types;
5244       ti->sort_priority = i;
5245
5246       sprintf(identifier, "%s", value);
5247       sprintf(name, "%s", text);
5248
5249       setString(&ti->identifier, identifier);
5250       setString(&ti->name, name);
5251       setString(&ti->name_sorting, name);
5252       setString(&ti->infotext, STR_SETUP_CHOOSE_SCALING_TYPE);
5253
5254       pushTreeInfo(&scaling_types, ti);
5255     }
5256
5257     // sort scaling type values to start with lowest scaling type value
5258     sortTreeInfo(&scaling_types);
5259
5260     // set current scaling type value to configured scaling type value
5261     scaling_type_current =
5262       getTreeInfoFromIdentifier(scaling_types, setup.window_scaling_quality);
5263
5264     // if that fails, set current scaling type to reliable default value
5265     if (scaling_type_current == NULL)
5266       scaling_type_current =
5267         getTreeInfoFromIdentifier(scaling_types, SCALING_QUALITY_DEFAULT);
5268
5269     // if that also fails, set current scaling type to first available value
5270     if (scaling_type_current == NULL)
5271       scaling_type_current = scaling_types;
5272   }
5273
5274   setup.window_scaling_quality = scaling_type_current->identifier;
5275
5276   // needed for displaying scaling type text instead of identifier
5277   scaling_type_text = scaling_type_current->name;
5278 }
5279
5280 static void execSetupGraphics_setRenderingModes(void)
5281 {
5282   if (rendering_modes == NULL)
5283   {
5284     int i;
5285
5286     for (i = 0; rendering_modes_list[i].value != NULL; i++)
5287     {
5288       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5289       char identifier[32], name[32];
5290       char *value = rendering_modes_list[i].value;
5291       char *text = rendering_modes_list[i].text;
5292
5293       ti->node_top = &rendering_modes;
5294       ti->sort_priority = i;
5295
5296       sprintf(identifier, "%s", value);
5297       sprintf(name, "%s", text);
5298
5299       setString(&ti->identifier, identifier);
5300       setString(&ti->name, name);
5301       setString(&ti->name_sorting, name);
5302       setString(&ti->infotext, STR_SETUP_CHOOSE_RENDERING);
5303
5304       pushTreeInfo(&rendering_modes, ti);
5305     }
5306
5307     // sort rendering mode values to start with lowest rendering mode value
5308     sortTreeInfo(&rendering_modes);
5309
5310     // set current rendering mode value to configured rendering mode value
5311     rendering_mode_current =
5312       getTreeInfoFromIdentifier(rendering_modes, setup.screen_rendering_mode);
5313
5314     // if that fails, set current rendering mode to reliable default value
5315     if (rendering_mode_current == NULL)
5316       rendering_mode_current =
5317         getTreeInfoFromIdentifier(rendering_modes,
5318                                   STR_SPECIAL_RENDERING_DEFAULT);
5319
5320     // if that also fails, set current rendering mode to first available one
5321     if (rendering_mode_current == NULL)
5322       rendering_mode_current = rendering_modes;
5323   }
5324
5325   setup.screen_rendering_mode = rendering_mode_current->identifier;
5326
5327   // needed for displaying rendering mode text instead of identifier
5328   rendering_mode_text = rendering_mode_current->name;
5329 }
5330
5331 static void execSetupGraphics_setVsyncModes(boolean update_value)
5332 {
5333   if (vsync_modes == NULL)
5334   {
5335     int i;
5336
5337     for (i = 0; vsync_modes_list[i].value != NULL; i++)
5338     {
5339       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5340       char identifier[32], name[32];
5341       char *value = vsync_modes_list[i].value;
5342       char *text = vsync_modes_list[i].text;
5343
5344       ti->node_top = &vsync_modes;
5345       ti->sort_priority = i;
5346
5347       sprintf(identifier, "%s", value);
5348       sprintf(name, "%s", text);
5349
5350       setString(&ti->identifier, identifier);
5351       setString(&ti->name, name);
5352       setString(&ti->name_sorting, name);
5353       setString(&ti->infotext, STR_SETUP_CHOOSE_VSYNC);
5354
5355       pushTreeInfo(&vsync_modes, ti);
5356     }
5357
5358     // sort vsync mode values to start with lowest vsync mode value
5359     sortTreeInfo(&vsync_modes);
5360
5361     update_value = TRUE;
5362   }
5363
5364   if (update_value)
5365   {
5366     // set current vsync mode value to configured vsync mode value
5367     vsync_mode_current =
5368       getTreeInfoFromIdentifier(vsync_modes, setup.vsync_mode);
5369
5370     // if that fails, set current vsync mode to reliable default value
5371     if (vsync_mode_current == NULL)
5372       vsync_mode_current =
5373         getTreeInfoFromIdentifier(vsync_modes, STR_VSYNC_MODE_DEFAULT);
5374
5375     // if that also fails, set current vsync mode to first available one
5376     if (vsync_mode_current == NULL)
5377       vsync_mode_current = vsync_modes;
5378   }
5379
5380   setup.vsync_mode = vsync_mode_current->identifier;
5381
5382   // needed for displaying vsync mode text instead of identifier
5383   vsync_mode_text = vsync_mode_current->name;
5384 }
5385
5386 static void execSetupGraphics(void)
5387 {
5388   boolean check_game_speed = (setup_mode == SETUP_MODE_CHOOSE_VSYNC);
5389
5390   // update "setup.window_scaling_percent" from list selection
5391   // (in this case, window scaling was changed on setup screen)
5392   if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
5393     execSetupGraphics_setWindowSizes(FALSE);
5394
5395   // update "setup.vsync_mode" from list selection
5396   // (in this case, vsync mode was changed on setup screen)
5397   if (setup_mode == SETUP_MODE_CHOOSE_VSYNC)
5398     execSetupGraphics_setVsyncModes(FALSE);
5399
5400   // update list selection from "setup.window_scaling_percent"
5401   // (window scaling may have changed by resizing the window)
5402   execSetupGraphics_setWindowSizes(TRUE);
5403
5404   // update list selection from "setup.vsync_mode"
5405   // (vsync_mode may have changed by re-creating the renderer)
5406   execSetupGraphics_setVsyncModes(TRUE);
5407
5408   execSetupGraphics_setScalingTypes();
5409   execSetupGraphics_setRenderingModes();
5410
5411   setup_mode = SETUP_MODE_GRAPHICS;
5412
5413   DrawSetupScreen();
5414
5415   // check if game speed is high enough for 60 Hz vsync to work
5416   if (check_game_speed)
5417     ModifyGameSpeedIfNeeded();
5418
5419   // window scaling may have changed at this point
5420   ChangeWindowScalingIfNeeded();
5421
5422   // window scaling quality may have changed at this point
5423   if (!strEqual(setup.window_scaling_quality, video.window_scaling_quality))
5424     SDLSetWindowScalingQuality(setup.window_scaling_quality);
5425
5426   // screen rendering mode may have changed at this point
5427   SDLSetScreenRenderingMode(setup.screen_rendering_mode);
5428
5429   int setup_vsync_mode = VSYNC_MODE_STR_TO_INT(setup.vsync_mode);
5430   int video_vsync_mode = video.vsync_mode;
5431
5432   // screen vsync mode may have changed at this point
5433   ChangeVsyncModeIfNeeded();
5434
5435   // check if setting vsync mode to selected value failed
5436   if (setup_vsync_mode != video_vsync_mode &&
5437       setup_vsync_mode != video.vsync_mode)
5438   {
5439     // changing vsync mode to selected value failed -- reset displayed value
5440     execSetupGraphics_setVsyncModes(TRUE);
5441
5442     Request("Setting VSync failed!", REQ_CONFIRM);
5443
5444     DrawSetupScreen();
5445   }
5446 }
5447
5448 static void execSetupChooseWindowSize(void)
5449 {
5450   setup_mode = SETUP_MODE_CHOOSE_WINDOW_SIZE;
5451
5452   DrawSetupScreen();
5453 }
5454
5455 static void execSetupChooseScalingType(void)
5456 {
5457   setup_mode = SETUP_MODE_CHOOSE_SCALING_TYPE;
5458
5459   DrawSetupScreen();
5460 }
5461
5462 static void execSetupChooseRenderingMode(void)
5463 {
5464   setup_mode = SETUP_MODE_CHOOSE_RENDERING;
5465
5466   DrawSetupScreen();
5467 }
5468
5469 static void execSetupChooseVsyncMode(void)
5470 {
5471   setup_mode = SETUP_MODE_CHOOSE_VSYNC;
5472
5473   DrawSetupScreen();
5474 }
5475
5476 static void execSetupChooseVolumeSimple(void)
5477 {
5478   setup_mode = SETUP_MODE_CHOOSE_VOLUME_SIMPLE;
5479
5480   DrawSetupScreen();
5481 }
5482
5483 static void execSetupChooseVolumeLoops(void)
5484 {
5485   setup_mode = SETUP_MODE_CHOOSE_VOLUME_LOOPS;
5486
5487   DrawSetupScreen();
5488 }
5489
5490 static void execSetupChooseVolumeMusic(void)
5491 {
5492   setup_mode = SETUP_MODE_CHOOSE_VOLUME_MUSIC;
5493
5494   DrawSetupScreen();
5495 }
5496
5497 static void execSetupSound(void)
5498 {
5499   if (volumes_simple == NULL)
5500   {
5501     boolean current_volume_simple_found = FALSE;
5502     int i;
5503
5504     for (i = 0; volumes_list[i].value != -1; i++)
5505     {
5506       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5507       char identifier[32], name[32];
5508       int value = volumes_list[i].value;
5509       char *text = volumes_list[i].text;
5510
5511       ti->node_top = &volumes_simple;
5512       ti->sort_priority = value;
5513
5514       sprintf(identifier, "%d", value);
5515       sprintf(name, "%s", text);
5516
5517       setString(&ti->identifier, identifier);
5518       setString(&ti->name, name);
5519       setString(&ti->name_sorting, name);
5520       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_SIMPLE);
5521
5522       pushTreeInfo(&volumes_simple, ti);
5523
5524       if (value == setup.volume_simple)
5525         current_volume_simple_found = TRUE;
5526     }
5527
5528     if (!current_volume_simple_found)
5529     {
5530       // add entry for non-preset volume value
5531
5532       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5533       char identifier[32], name[32];
5534       int value = setup.volume_simple;
5535
5536       ti->node_top = &volumes_simple;
5537       ti->sort_priority = value;
5538
5539       sprintf(identifier, "%d", value);
5540       sprintf(name, "%d %% (Current)", value);
5541
5542       setString(&ti->identifier, identifier);
5543       setString(&ti->name, name);
5544       setString(&ti->name_sorting, name);
5545       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_SIMPLE);
5546
5547       pushTreeInfo(&volumes_simple, ti);
5548     }
5549
5550     // sort volume values to start with lowest volume value
5551     sortTreeInfo(&volumes_simple);
5552
5553     // set current volume value to configured volume value
5554     volume_simple_current =
5555       getTreeInfoFromIdentifier(volumes_simple,i_to_a(setup.volume_simple));
5556
5557     // if that fails, set current volume to reliable default value
5558     if (volume_simple_current == NULL)
5559       volume_simple_current =
5560         getTreeInfoFromIdentifier(volumes_simple, i_to_a(100));
5561
5562     // if that also fails, set current volume to first available value
5563     if (volume_simple_current == NULL)
5564       volume_simple_current = volumes_simple;
5565   }
5566
5567   if (volumes_loops == NULL)
5568   {
5569     boolean current_volume_loops_found = FALSE;
5570     int i;
5571
5572     for (i = 0; volumes_list[i].value != -1; i++)
5573     {
5574       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5575       char identifier[32], name[32];
5576       int value = volumes_list[i].value;
5577       char *text = volumes_list[i].text;
5578
5579       ti->node_top = &volumes_loops;
5580       ti->sort_priority = value;
5581
5582       sprintf(identifier, "%d", value);
5583       sprintf(name, "%s", text);
5584
5585       setString(&ti->identifier, identifier);
5586       setString(&ti->name, name);
5587       setString(&ti->name_sorting, name);
5588       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_LOOPS);
5589
5590       pushTreeInfo(&volumes_loops, ti);
5591
5592       if (value == setup.volume_loops)
5593         current_volume_loops_found = TRUE;
5594     }
5595
5596     if (!current_volume_loops_found)
5597     {
5598       // add entry for non-preset volume value
5599
5600       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5601       char identifier[32], name[32];
5602       int value = setup.volume_loops;
5603
5604       ti->node_top = &volumes_loops;
5605       ti->sort_priority = value;
5606
5607       sprintf(identifier, "%d", value);
5608       sprintf(name, "%d %% (Current)", value);
5609
5610       setString(&ti->identifier, identifier);
5611       setString(&ti->name, name);
5612       setString(&ti->name_sorting, name);
5613       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_LOOPS);
5614
5615       pushTreeInfo(&volumes_loops, ti);
5616     }
5617
5618     // sort volume values to start with lowest volume value
5619     sortTreeInfo(&volumes_loops);
5620
5621     // set current volume value to configured volume value
5622     volume_loops_current =
5623       getTreeInfoFromIdentifier(volumes_loops,i_to_a(setup.volume_loops));
5624
5625     // if that fails, set current volume to reliable default value
5626     if (volume_loops_current == NULL)
5627       volume_loops_current =
5628         getTreeInfoFromIdentifier(volumes_loops, i_to_a(100));
5629
5630     // if that also fails, set current volume to first available value
5631     if (volume_loops_current == NULL)
5632       volume_loops_current = volumes_loops;
5633   }
5634
5635   if (volumes_music == NULL)
5636   {
5637     boolean current_volume_music_found = FALSE;
5638     int i;
5639
5640     for (i = 0; volumes_list[i].value != -1; i++)
5641     {
5642       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5643       char identifier[32], name[32];
5644       int value = volumes_list[i].value;
5645       char *text = volumes_list[i].text;
5646
5647       ti->node_top = &volumes_music;
5648       ti->sort_priority = value;
5649
5650       sprintf(identifier, "%d", value);
5651       sprintf(name, "%s", text);
5652
5653       setString(&ti->identifier, identifier);
5654       setString(&ti->name, name);
5655       setString(&ti->name_sorting, name);
5656       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_MUSIC);
5657
5658       pushTreeInfo(&volumes_music, ti);
5659
5660       if (value == setup.volume_music)
5661         current_volume_music_found = TRUE;
5662     }
5663
5664     if (!current_volume_music_found)
5665     {
5666       // add entry for non-preset volume value
5667
5668       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5669       char identifier[32], name[32];
5670       int value = setup.volume_music;
5671
5672       ti->node_top = &volumes_music;
5673       ti->sort_priority = value;
5674
5675       sprintf(identifier, "%d", value);
5676       sprintf(name, "%d %% (Current)", value);
5677
5678       setString(&ti->identifier, identifier);
5679       setString(&ti->name, name);
5680       setString(&ti->name_sorting, name);
5681       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_MUSIC);
5682
5683       pushTreeInfo(&volumes_music, ti);
5684     }
5685
5686     // sort volume values to start with lowest volume value
5687     sortTreeInfo(&volumes_music);
5688
5689     // set current volume value to configured volume value
5690     volume_music_current =
5691       getTreeInfoFromIdentifier(volumes_music,i_to_a(setup.volume_music));
5692
5693     // if that fails, set current volume to reliable default value
5694     if (volume_music_current == NULL)
5695       volume_music_current =
5696         getTreeInfoFromIdentifier(volumes_music, i_to_a(100));
5697
5698     // if that also fails, set current volume to first available value
5699     if (volume_music_current == NULL)
5700       volume_music_current = volumes_music;
5701   }
5702
5703   setup.volume_simple = atoi(volume_simple_current->identifier);
5704   setup.volume_loops  = atoi(volume_loops_current->identifier);
5705   setup.volume_music  = atoi(volume_music_current->identifier);
5706
5707   // needed for displaying volume text instead of identifier
5708   volume_simple_text = volume_simple_current->name;
5709   volume_loops_text = volume_loops_current->name;
5710   volume_music_text = volume_music_current->name;
5711
5712   setup_mode = SETUP_MODE_SOUND;
5713
5714   DrawSetupScreen();
5715 }
5716
5717 static void execSetupChooseTouchControls(void)
5718 {
5719   setup_mode = SETUP_MODE_CHOOSE_TOUCH_CONTROL;
5720
5721   DrawSetupScreen();
5722 }
5723
5724 static void execSetupChooseMoveDistance(void)
5725 {
5726   setup_mode = SETUP_MODE_CHOOSE_MOVE_DISTANCE;
5727
5728   DrawSetupScreen();
5729 }
5730
5731 static void execSetupChooseDropDistance(void)
5732 {
5733   setup_mode = SETUP_MODE_CHOOSE_DROP_DISTANCE;
5734
5735   DrawSetupScreen();
5736 }
5737
5738 static void execSetupChooseTransparency(void)
5739 {
5740   setup_mode = SETUP_MODE_CHOOSE_TRANSPARENCY;
5741
5742   DrawSetupScreen();
5743 }
5744
5745 static void execSetupChooseGridXSize_0(void)
5746 {
5747   setup_mode = SETUP_MODE_CHOOSE_GRID_XSIZE_0;
5748
5749   DrawSetupScreen();
5750 }
5751
5752 static void execSetupChooseGridYSize_0(void)
5753 {
5754   setup_mode = SETUP_MODE_CHOOSE_GRID_YSIZE_0;
5755
5756   DrawSetupScreen();
5757 }
5758
5759 static void execSetupChooseGridXSize_1(void)
5760 {
5761   setup_mode = SETUP_MODE_CHOOSE_GRID_XSIZE_1;
5762
5763   DrawSetupScreen();
5764 }
5765
5766 static void execSetupChooseGridYSize_1(void)
5767 {
5768   setup_mode = SETUP_MODE_CHOOSE_GRID_YSIZE_1;
5769
5770   DrawSetupScreen();
5771 }
5772
5773 static void execSetupConfigureVirtualButtons(void)
5774 {
5775   setup_mode = SETUP_MODE_CONFIG_VIRT_BUTTONS;
5776
5777   ConfigureVirtualButtons();
5778
5779   setup_mode = SETUP_MODE_TOUCH;
5780
5781   DrawSetupScreen();
5782 }
5783
5784 static void execSetupTouch(void)
5785 {
5786   int i, j, k;
5787
5788   if (touch_controls == NULL)
5789   {
5790     for (i = 0; touch_controls_list[i].value != NULL; i++)
5791     {
5792       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5793       char identifier[32], name[32];
5794       char *value = touch_controls_list[i].value;
5795       char *text = touch_controls_list[i].text;
5796
5797       ti->node_top = &touch_controls;
5798       ti->sort_priority = i;
5799
5800       sprintf(identifier, "%s", value);
5801       sprintf(name, "%s", text);
5802
5803       setString(&ti->identifier, identifier);
5804       setString(&ti->name, name);
5805       setString(&ti->name_sorting, name);
5806       setString(&ti->infotext, STR_SETUP_CHOOSE_TOUCH_CONTROL);
5807
5808       pushTreeInfo(&touch_controls, ti);
5809     }
5810
5811     // sort touch control values to start with lowest touch control value
5812     sortTreeInfo(&touch_controls);
5813
5814     // set current touch control value to configured touch control value
5815     touch_control_current =
5816       getTreeInfoFromIdentifier(touch_controls, setup.touch.control_type);
5817
5818     // if that fails, set current touch control to reliable default value
5819     if (touch_control_current == NULL)
5820       touch_control_current =
5821         getTreeInfoFromIdentifier(touch_controls, TOUCH_CONTROL_DEFAULT);
5822
5823     // if that also fails, set current touch control to first available value
5824     if (touch_control_current == NULL)
5825       touch_control_current = touch_controls;
5826   }
5827
5828   if (move_distances == NULL)
5829   {
5830     for (i = 0; distances_list[i].value != -1; i++)
5831     {
5832       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5833       char identifier[32], name[32];
5834       int value = distances_list[i].value;
5835       char *text = distances_list[i].text;
5836
5837       ti->node_top = &move_distances;
5838       ti->sort_priority = value;
5839
5840       sprintf(identifier, "%d", value);
5841       sprintf(name, "%s", text);
5842
5843       setString(&ti->identifier, identifier);
5844       setString(&ti->name, name);
5845       setString(&ti->name_sorting, name);
5846       setString(&ti->infotext, STR_SETUP_CHOOSE_MOVE_DISTANCE);
5847
5848       pushTreeInfo(&move_distances, ti);
5849     }
5850
5851     // sort distance values to start with lowest distance value
5852     sortTreeInfo(&move_distances);
5853
5854     // set current distance value to configured distance value
5855     move_distance_current =
5856       getTreeInfoFromIdentifier(move_distances,
5857                                 i_to_a(setup.touch.move_distance));
5858
5859     // if that fails, set current distance to reliable default value
5860     if (move_distance_current == NULL)
5861       move_distance_current =
5862         getTreeInfoFromIdentifier(move_distances,
5863                                   i_to_a(TOUCH_MOVE_DISTANCE_DEFAULT));
5864
5865     // if that also fails, set current distance to first available value
5866     if (move_distance_current == NULL)
5867       move_distance_current = move_distances;
5868   }
5869
5870   if (drop_distances == NULL)
5871   {
5872     for (i = 0; distances_list[i].value != -1; i++)
5873     {
5874       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5875       char identifier[32], name[32];
5876       int value = distances_list[i].value;
5877       char *text = distances_list[i].text;
5878
5879       ti->node_top = &drop_distances;
5880       ti->sort_priority = value;
5881
5882       sprintf(identifier, "%d", value);
5883       sprintf(name, "%s", text);
5884
5885       setString(&ti->identifier, identifier);
5886       setString(&ti->name, name);
5887       setString(&ti->name_sorting, name);
5888       setString(&ti->infotext, STR_SETUP_CHOOSE_DROP_DISTANCE);
5889
5890       pushTreeInfo(&drop_distances, ti);
5891     }
5892
5893     // sort distance values to start with lowest distance value
5894     sortTreeInfo(&drop_distances);
5895
5896     // set current distance value to configured distance value
5897     drop_distance_current =
5898       getTreeInfoFromIdentifier(drop_distances,
5899                                 i_to_a(setup.touch.drop_distance));
5900
5901     // if that fails, set current distance to reliable default value
5902     if (drop_distance_current == NULL)
5903       drop_distance_current =
5904         getTreeInfoFromIdentifier(drop_distances,
5905                                   i_to_a(TOUCH_DROP_DISTANCE_DEFAULT));
5906
5907     // if that also fails, set current distance to first available value
5908     if (drop_distance_current == NULL)
5909       drop_distance_current = drop_distances;
5910   }
5911
5912   if (transparencies == NULL)
5913   {
5914     for (i = 0; transparencies_list[i].value != -1; i++)
5915     {
5916       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5917       char identifier[32], name[32];
5918       int value = transparencies_list[i].value;
5919       char *text = transparencies_list[i].text;
5920
5921       ti->node_top = &transparencies;
5922       ti->sort_priority = value;
5923
5924       sprintf(identifier, "%d", value);
5925       sprintf(name, "%s", text);
5926
5927       setString(&ti->identifier, identifier);
5928       setString(&ti->name, name);
5929       setString(&ti->name_sorting, name);
5930       setString(&ti->infotext, STR_SETUP_CHOOSE_TRANSPARENCY);
5931
5932       pushTreeInfo(&transparencies, ti);
5933     }
5934
5935     // sort transparency values to start with lowest transparency value
5936     sortTreeInfo(&transparencies);
5937
5938     // set current transparency value to configured transparency value
5939     transparency_current =
5940       getTreeInfoFromIdentifier(transparencies,
5941                                 i_to_a(setup.touch.transparency));
5942
5943     // if that fails, set current transparency to reliable default value
5944     if (transparency_current == NULL)
5945       transparency_current =
5946         getTreeInfoFromIdentifier(transparencies,
5947                                   i_to_a(TOUCH_TRANSPARENCY_DEFAULT));
5948
5949     // if that also fails, set current transparency to first available value
5950     if (transparency_current == NULL)
5951       transparency_current = transparencies;
5952   }
5953
5954   for (i = 0; i < 2; i++)
5955   {
5956     for (j = 0; j < 2; j++)
5957     {
5958       if (grid_sizes[i][j] == NULL)
5959       {
5960         for (k = 0; grid_sizes_list[k].value != -1; k++)
5961         {
5962           TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5963           char identifier[32], name[32];
5964           int value = grid_sizes_list[k].value;
5965           char *text = grid_sizes_list[k].text;
5966
5967           ti->node_top = &grid_sizes[i][j];
5968           ti->sort_priority = value;
5969
5970           sprintf(identifier, "%d", value);
5971           sprintf(name, "%s", text);
5972
5973           setString(&ti->identifier, identifier);
5974           setString(&ti->name, name);
5975           setString(&ti->name_sorting, name);
5976           setString(&ti->infotext,
5977                     (i == 0 ?
5978                      (j == 0 ?
5979                       STR_SETUP_CHOOSE_GRID_XSIZE_0 :
5980                       STR_SETUP_CHOOSE_GRID_YSIZE_0) :
5981                      (j == 0 ?
5982                       STR_SETUP_CHOOSE_GRID_XSIZE_1 :
5983                       STR_SETUP_CHOOSE_GRID_YSIZE_1)));
5984
5985           pushTreeInfo(&grid_sizes[i][j], ti);
5986         }
5987
5988         // sort grid size values to start with lowest grid size value
5989         sortTreeInfo(&grid_sizes[i][j]);
5990
5991         // set current grid size value to configured grid size value
5992         grid_size_current[i][j] =
5993           getTreeInfoFromIdentifier(grid_sizes[i][j],
5994                                     i_to_a(j == 0 ?
5995                                            setup.touch.grid_xsize[i] :
5996                                            setup.touch.grid_ysize[i]));
5997
5998         // if that fails, set current grid size to reliable default value
5999         if (grid_size_current[i][j] == NULL)
6000           grid_size_current[i][j] =
6001             getTreeInfoFromIdentifier(grid_sizes[i][j],
6002                                       i_to_a(j == 0 ?
6003                                              DEFAULT_GRID_XSIZE(i) :
6004                                              DEFAULT_GRID_YSIZE(i)));
6005
6006         // if that also fails, set current grid size to first available value
6007         if (grid_size_current[i][j] == NULL)
6008           grid_size_current[i][j] = grid_sizes[i][j];
6009       }
6010     }
6011   }
6012
6013   setup.touch.control_type = touch_control_current->identifier;
6014   setup.touch.move_distance = atoi(move_distance_current->identifier);
6015   setup.touch.drop_distance = atoi(drop_distance_current->identifier);
6016   setup.touch.transparency = atoi(transparency_current->identifier);
6017
6018   for (i = 0; i < 2; i++)
6019   {
6020     setup.touch.grid_xsize[i] = atoi(grid_size_current[i][0]->identifier);
6021     setup.touch.grid_ysize[i] = atoi(grid_size_current[i][1]->identifier);
6022
6023     if (i == GRID_ACTIVE_NR())
6024     {
6025       overlay.grid_xsize = setup.touch.grid_xsize[i];
6026       overlay.grid_ysize = setup.touch.grid_ysize[i];
6027     }
6028   }
6029
6030   // needed for displaying value text instead of identifier
6031   touch_controls_text = touch_control_current->name;
6032   move_distance_text = move_distance_current->name;
6033   drop_distance_text = drop_distance_current->name;
6034   transparency_text = transparency_current->name;
6035
6036   for (i = 0; i < 2; i++)
6037     for (j = 0; j < 2; j++)
6038       grid_size_text[i][j] = grid_size_current[i][j]->name;
6039
6040   setup_mode = SETUP_MODE_TOUCH;
6041
6042   DrawSetupScreen();
6043 }
6044
6045 static void execSetupArtwork(void)
6046 {
6047 #if 0
6048   Debug("screens:execSetupArtwork", "'%s', '%s', '%s'",
6049         artwork.gfx_current->subdir,
6050         artwork.gfx_current->fullpath,
6051         artwork.gfx_current->basepath);
6052 #endif
6053
6054   setup.graphics_set = artwork.gfx_current->identifier;
6055   setup.sounds_set = artwork.snd_current->identifier;
6056   setup.music_set = artwork.mus_current->identifier;
6057
6058   // needed if last screen (setup choice) changed graphics, sounds or music
6059   ReloadCustomArtwork(0);
6060
6061   // needed for displaying artwork name instead of artwork identifier
6062   graphics_set_name = artwork.gfx_current->name;
6063   sounds_set_name = artwork.snd_current->name;
6064   music_set_name = artwork.mus_current->name;
6065
6066   setup_mode = SETUP_MODE_ARTWORK;
6067
6068   DrawSetupScreen();
6069 }
6070
6071 static void execSetupChooseGraphics(void)
6072 {
6073   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
6074
6075   DrawSetupScreen();
6076 }
6077
6078 static void execSetupChooseSounds(void)
6079 {
6080   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
6081
6082   DrawSetupScreen();
6083 }
6084
6085 static void execSetupChooseMusic(void)
6086 {
6087   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
6088
6089   DrawSetupScreen();
6090 }
6091
6092 static void execSetupInput(void)
6093 {
6094   setup_mode = SETUP_MODE_INPUT;
6095
6096   DrawSetupScreen();
6097 }
6098
6099 static void execSetupShortcuts(void)
6100 {
6101   setup_mode = SETUP_MODE_SHORTCUTS;
6102
6103   DrawSetupScreen();
6104 }
6105
6106 static void execSetupShortcuts1(void)
6107 {
6108   setup_mode = SETUP_MODE_SHORTCUTS_1;
6109
6110   DrawSetupScreen();
6111 }
6112
6113 static void execSetupShortcuts2(void)
6114 {
6115   setup_mode = SETUP_MODE_SHORTCUTS_2;
6116
6117   DrawSetupScreen();
6118 }
6119
6120 static void execSetupShortcuts3(void)
6121 {
6122   setup_mode = SETUP_MODE_SHORTCUTS_3;
6123
6124   DrawSetupScreen();
6125 }
6126
6127 static void execSetupShortcuts4(void)
6128 {
6129   setup_mode = SETUP_MODE_SHORTCUTS_4;
6130
6131   DrawSetupScreen();
6132 }
6133
6134 static void execSetupShortcuts5(void)
6135 {
6136   setup_mode = SETUP_MODE_SHORTCUTS_5;
6137
6138   DrawSetupScreen();
6139 }
6140
6141 static void execExitSetup(void)
6142 {
6143   SetGameStatus(GAME_MODE_MAIN);
6144
6145   DrawMainMenu();
6146 }
6147
6148 static void execSaveAndExitSetup(void)
6149 {
6150   SaveSetup();
6151   execExitSetup();
6152 }
6153
6154 static void execGadgetNetworkServer(void)
6155 {
6156   int gadget_id = SCREEN_CTRL_ID_NETWORK_SERVER;
6157   struct GadgetInfo *gi = screen_gadget[gadget_id];
6158
6159   if (strEqual(setup.network_server_hostname, STR_NETWORK_AUTO_DETECT))
6160     network_server_hostname[0] = '\0';
6161
6162   ModifyGadget(gi, GDI_TEXT_VALUE, network_server_hostname, GDI_END);
6163
6164   MapGadget(gi);
6165
6166   ClickOnGadget(gi, MB_LEFTBUTTON);
6167 }
6168
6169 static void ToggleNetworkModeIfNeeded(void)
6170 {
6171   int font_title = FONT_TITLE_1;
6172   int font_foot = FC_BLUE;
6173   int ystart  = mSY - SY + 16;
6174   int ybottom = mSY - SY + SYSIZE - 20;
6175   char *text = (setup.network_mode ? "Start Network" : "Stop Network");
6176
6177   if (setup.network_mode == network.enabled)
6178     return;
6179
6180   network.enabled = setup.network_mode;
6181
6182   FadeOut(REDRAW_ALL);
6183
6184   ClearField();
6185
6186   DrawTextSCentered(ystart, font_title, text);
6187
6188   FadeIn(REDRAW_ALL);
6189
6190   if (network.enabled)
6191     InitNetworkServer();
6192   else
6193     DisconnectFromNetworkServer();
6194
6195   DrawTextSCentered(ybottom, font_foot,
6196                     "Press any key or button for setup menu");
6197
6198   WaitForEventToContinue();
6199
6200   DrawSetupScreen();
6201 }
6202
6203 static void ToggleGameSpeedsListIfNeeded(void)
6204 {
6205   boolean using_game_speeds_extended = (game_speeds == game_speeds_extended);
6206
6207   if (setup.game_speed_extended == using_game_speeds_extended)
6208     return;
6209
6210   // try to match similar values when changing game speeds list
6211   if (setup.game_speed_extended)
6212     setup.game_frame_delay = (setup.game_frame_delay == 15 ? 16 :
6213                               setup.game_frame_delay == 30 ? 29 :
6214                               setup.game_frame_delay);
6215   else
6216     setup.game_frame_delay = (setup.game_frame_delay == 14 ? 15 :
6217                               setup.game_frame_delay == 16 ? 15 :
6218                               setup.game_frame_delay >= 29 ? 30 :
6219                               setup.game_frame_delay <= 10 ? 10 :
6220                               setup.game_frame_delay);
6221
6222   execSetupGame_setGameSpeeds(TRUE);
6223
6224   DrawSetupScreen();
6225 }
6226
6227 static void ModifyGameSpeedIfNeeded(void)
6228 {
6229   if (strEqual(setup.vsync_mode, STR_VSYNC_MODE_OFF) ||
6230       setup.game_frame_delay <= MAX_VSYNC_FRAME_DELAY)
6231     return;
6232
6233   char message[100];
6234   char *game_speed_text = "Fast";
6235   int game_speed_value = 15;
6236
6237   if (setup.game_speed_extended)
6238   {
6239     game_speed_text = "60 fps";
6240     game_speed_value = 16;
6241   }
6242
6243   sprintf(message, "Game speed set to %s for VSync to work!", game_speed_text);
6244
6245   // set game speed to existing list value that is fast enough for vsync
6246   setup.game_frame_delay = game_speed_value;
6247
6248   execSetupGame_setGameSpeeds(TRUE);
6249
6250   Request(message, REQ_CONFIRM);
6251 }
6252
6253 static void DisableVsyncIfNeeded(void)
6254 {
6255   if (strEqual(setup.vsync_mode, STR_VSYNC_MODE_OFF) ||
6256       (setup.game_frame_delay >= MIN_VSYNC_FRAME_DELAY &&
6257        setup.game_frame_delay <= MAX_VSYNC_FRAME_DELAY))
6258     return;
6259
6260   // disable vsync for the selected game speed to work
6261   setup.vsync_mode = STR_VSYNC_MODE_OFF;
6262
6263   execSetupGraphics_setVsyncModes(TRUE);
6264
6265   Request("VSync disabled for this game speed to work!", REQ_CONFIRM);
6266 }
6267
6268 static struct
6269 {
6270   void *value;
6271   void *related_value;
6272 } hide_related_entry_list[] =
6273 {
6274   { &setup.game_frame_delay,            execSetupChooseGameSpeed        },
6275   { &setup.game_frame_delay,            &game_speed_text                },
6276
6277   { &setup.scroll_delay_value,          execSetupChooseScrollDelay      },
6278   { &setup.scroll_delay_value,          &scroll_delay_text              },
6279
6280   { &setup.engine_snapshot_mode,        execSetupChooseSnapshotMode     },
6281   { &setup.engine_snapshot_mode,        &snapshot_mode_text             },
6282
6283   { &setup.window_scaling_percent,      execSetupChooseWindowSize       },
6284   { &setup.window_scaling_percent,      &window_size_text               },
6285
6286   { &setup.window_scaling_quality,      execSetupChooseScalingType      },
6287   { &setup.window_scaling_quality,      &scaling_type_text              },
6288
6289   { &setup.screen_rendering_mode,       execSetupChooseRenderingMode    },
6290   { &setup.screen_rendering_mode,       &rendering_mode_text            },
6291
6292   { &setup.vsync_mode,                  execSetupChooseVsyncMode        },
6293   { &setup.vsync_mode,                  &vsync_mode_text                },
6294
6295   { &setup.graphics_set,                execSetupChooseGraphics         },
6296   { &setup.graphics_set,                &graphics_set_name              },
6297
6298   { &setup.sounds_set,                  execSetupChooseSounds           },
6299   { &setup.sounds_set,                  &sounds_set_name                },
6300
6301   { &setup.music_set,                   execSetupChooseMusic            },
6302   { &setup.music_set,                   &music_set_name                 },
6303
6304   { &setup.volume_simple,               execSetupChooseVolumeSimple     },
6305   { &setup.volume_simple,               &volume_simple_text             },
6306
6307   { &setup.volume_loops,                execSetupChooseVolumeLoops      },
6308   { &setup.volume_loops,                &volume_loops_text              },
6309
6310   { &setup.volume_music,                execSetupChooseVolumeMusic      },
6311   { &setup.volume_music,                &volume_music_text              },
6312
6313   { &setup.touch.control_type,          execSetupChooseTouchControls    },
6314   { &setup.touch.control_type,          &touch_controls_text            },
6315
6316   { &setup.touch.move_distance,         execSetupChooseMoveDistance     },
6317   { &setup.touch.move_distance,         &move_distance_text             },
6318
6319   { &setup.touch.drop_distance,         execSetupChooseDropDistance     },
6320   { &setup.touch.drop_distance,         &drop_distance_text             },
6321
6322   { &setup.touch.transparency,          execSetupChooseTransparency     },
6323   { &setup.touch.transparency,          &transparency_text              },
6324
6325   { &setup.touch.grid_xsize[0],         execSetupChooseGridXSize_0      },
6326   { &setup.touch.grid_xsize[0],         &grid_size_text[0][0]           },
6327
6328   { &setup.touch.grid_ysize[0],         execSetupChooseGridYSize_0      },
6329   { &setup.touch.grid_ysize[0],         &grid_size_text[0][1]           },
6330
6331   { &setup.touch.grid_xsize[1],         execSetupChooseGridXSize_1      },
6332   { &setup.touch.grid_xsize[1],         &grid_size_text[1][0]           },
6333
6334   { &setup.touch.grid_ysize[1],         execSetupChooseGridYSize_1      },
6335   { &setup.touch.grid_ysize[1],         &grid_size_text[1][1]           },
6336
6337   { &setup.internal.menu_game,          execSetupGame                   },
6338   { &setup.internal.menu_engines,       execSetupEngines                },
6339   { &setup.internal.menu_editor,        execSetupEditor                 },
6340   { &setup.internal.menu_graphics,      execSetupGraphics               },
6341   { &setup.internal.menu_sound,         execSetupSound                  },
6342   { &setup.internal.menu_artwork,       execSetupArtwork                },
6343   { &setup.internal.menu_input,         execSetupInput                  },
6344   { &setup.internal.menu_touch,         execSetupTouch                  },
6345   { &setup.internal.menu_shortcuts,     execSetupShortcuts              },
6346   { &setup.internal.menu_exit,          execExitSetup                   },
6347   { &setup.internal.menu_save_and_exit, execSaveAndExitSetup            },
6348
6349   { NULL,                               NULL                            }
6350 };
6351
6352 void setHideRelatedSetupEntries(void)
6353 {
6354   int i;
6355
6356   for (i = 0; hide_related_entry_list[i].value != NULL; i++)
6357     if (hideSetupEntry(hide_related_entry_list[i].value))
6358       setHideSetupEntry(hide_related_entry_list[i].related_value);
6359 }
6360
6361 static struct TokenInfo setup_info_main[] =
6362 {
6363   { TYPE_ENTER_MENU,    execSetupGame,          STR_SETUP_GAME          },
6364   { TYPE_ENTER_MENU,    execSetupEngines,       STR_SETUP_ENGINES       },
6365   { TYPE_ENTER_MENU,    execSetupEditor,        STR_SETUP_EDITOR        },
6366   { TYPE_ENTER_MENU,    execSetupGraphics,      STR_SETUP_GRAPHICS      },
6367   { TYPE_ENTER_MENU,    execSetupSound,         STR_SETUP_SOUND         },
6368   { TYPE_ENTER_MENU,    execSetupArtwork,       STR_SETUP_ARTWORK       },
6369   { TYPE_ENTER_MENU,    execSetupInput,         STR_SETUP_INPUT         },
6370   { TYPE_ENTER_MENU,    execSetupTouch,         STR_SETUP_TOUCH         },
6371   { TYPE_ENTER_MENU,    execSetupShortcuts,     STR_SETUP_SHORTCUTS     },
6372   { TYPE_EMPTY,         NULL,                   ""                      },
6373   { TYPE_LEAVE_MENU,    execExitSetup,          STR_SETUP_EXIT          },
6374   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   STR_SETUP_SAVE_AND_EXIT },
6375
6376   { 0,                  NULL,                   NULL                    }
6377 };
6378
6379 static struct TokenInfo setup_info_game[] =
6380 {
6381   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode (Multi-Player):" },
6382   { TYPE_SWITCH,        &setup.network_mode,    "Network Multi-Player Mode:" },
6383   { TYPE_PLAYER,        &setup.network_player_nr,"Preferred Network Player:" },
6384   { TYPE_TEXT_INPUT,    execGadgetNetworkServer, "Network Server Hostname:" },
6385   { TYPE_STRING,        &network_server_text,   ""                      },
6386   { TYPE_SWITCH,        &setup.multiple_users,  "Multiple Users/Teams:" },
6387   { TYPE_YES_NO,        &setup.input_on_focus,  "Only Move Focussed Player:" },
6388   { TYPE_SWITCH,        &setup.time_limit,      "Time Limit:"           },
6389   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
6390   { TYPE_SWITCH,        &setup.skip_levels,     "Skip Unsolved Levels:" },
6391   { TYPE_SWITCH,        &setup.increment_levels,"Increment Solved Levels:" },
6392   { TYPE_SWITCH,        &setup.auto_play_next_level,"Auto-play Next Level:" },
6393   { TYPE_SWITCH,        &setup.skip_scores_after_game,"Skip Scores After Game:" },
6394   { TYPE_YES_NO,        &setup.ask_on_game_over, "Ask on Game Over:"    },
6395   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record Tapes:"    },
6396   { TYPE_ENTER_LIST,    execSetupChooseGameSpeed, "Game Speed:"         },
6397   { TYPE_STRING,        &game_speed_text,       ""                      },
6398   { TYPE_SWITCH,        &setup.game_speed_extended, "Game Speed Extended List:" },
6399 #if 1
6400   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay:"     },
6401   { TYPE_STRING,        &scroll_delay_text,     ""                      },
6402 #endif
6403   { TYPE_ENTER_LIST, execSetupChooseSnapshotMode,"Game Engine Snapshot Mode:" },
6404   { TYPE_STRING,        &snapshot_mode_text,    ""                      },
6405   { TYPE_SWITCH,        &setup.show_snapshot_buttons,"Show Snapshot Buttons:" },
6406   { TYPE_EMPTY,         NULL,                   ""                      },
6407   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6408
6409   { 0,                  NULL,                   NULL                    }
6410 };
6411
6412 static struct TokenInfo setup_info_engines[] =
6413 {
6414   { TYPE_HEADLINE,      NULL,                   "Emerald Mine"          },
6415   { TYPE_SWITCH,        &setup.forced_scroll_delay, "Scroll Delay:"     },
6416   { TYPE_ECS_AGA,       &setup.prefer_aga_graphics, "Amiga Graphics Chipset:" },
6417   { TYPE_SWITCH,        &setup.prefer_lowpass_sounds,"Low-Pass Filter Sounds:" },
6418   { TYPE_EMPTY,         NULL,                   ""                      },
6419   { TYPE_HEADLINE,      NULL,                   "Supaplex"              },
6420   { TYPE_SWITCH,        &setup.sp_show_border_elements, "Border Elements:" },
6421   { TYPE_EMPTY,         NULL,                   ""                      },
6422   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6423
6424   { 0,                  NULL,                   NULL                    }
6425 };
6426
6427 static struct TokenInfo setup_info_editor[] =
6428 {
6429 #if 0
6430   { TYPE_SWITCH,        &setup.editor.el_boulderdash,   "Boulder Dash:" },
6431   { TYPE_SWITCH,        &setup.editor.el_emerald_mine,  "Emerald Mine:" },
6432   { TYPE_SWITCH, &setup.editor.el_emerald_mine_club,    "Emerald Mine Club:" },
6433   { TYPE_SWITCH,        &setup.editor.el_more,          "Rocks'n'Diamonds:" },
6434   { TYPE_SWITCH,        &setup.editor.el_sokoban,       "Sokoban:"      },
6435   { TYPE_SWITCH,        &setup.editor.el_supaplex,      "Supaplex:"     },
6436   { TYPE_SWITCH,        &setup.editor.el_diamond_caves, "Diamond Caves II:" },
6437   { TYPE_SWITCH,        &setup.editor.el_dx_boulderdash,"DX-Boulderdash:" },
6438   { TYPE_SWITCH,        &setup.editor.el_chars,         "Text Characters:" },
6439   { TYPE_SWITCH, &setup.editor.el_steel_chars, "Text Characters (Steel):" },
6440 #endif
6441   { TYPE_SWITCH,        &setup.editor.el_classic,  "Classic Elements:" },
6442   { TYPE_SWITCH,        &setup.editor.el_custom,  "Custom & Group Elements:" },
6443 #if 0
6444   { TYPE_SWITCH,        &setup.editor.el_headlines,     "Headlines:"    },
6445 #endif
6446   { TYPE_SWITCH, &setup.editor.el_user_defined, "User defined element list:" },
6447   { TYPE_SWITCH,        &setup.editor.el_dynamic,  "Dynamic level elements:" },
6448   { TYPE_EMPTY,         NULL,                   ""                      },
6449 #if 0
6450   { TYPE_SWITCH,        &setup.editor.el_by_game,   "Show elements by game:" },
6451   { TYPE_SWITCH,        &setup.editor.el_by_type,   "Show elements by type:" },
6452   { TYPE_EMPTY,         NULL,                   ""                      },
6453 #endif
6454   { TYPE_SWITCH, &setup.editor.show_element_token,      "Show element token:" },
6455   { TYPE_EMPTY,         NULL,                   ""                      },
6456   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6457
6458   { 0,                  NULL,                   NULL                    }
6459 };
6460
6461 static struct TokenInfo setup_info_graphics[] =
6462 {
6463 #if !defined(PLATFORM_ANDROID)
6464   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen:"           },
6465   { TYPE_ENTER_LIST,    execSetupChooseWindowSize, "Window Scaling:"    },
6466   { TYPE_STRING,        &window_size_text,      ""                      },
6467   { TYPE_ENTER_LIST,    execSetupChooseScalingType, "Anti-Aliasing:"    },
6468   { TYPE_STRING,        &scaling_type_text,     ""                      },
6469   { TYPE_ENTER_LIST,    execSetupChooseRenderingMode, "Special Rendering:" },
6470   { TYPE_STRING,        &rendering_mode_text,   ""                      },
6471 #endif
6472 #if 0
6473   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay:"     },
6474   { TYPE_STRING,        &scroll_delay_text,     ""                      },
6475 #endif
6476   { TYPE_ENTER_LIST,    execSetupChooseVsyncMode, "Vertical Sync (VSync):" },
6477   { TYPE_STRING,        &vsync_mode_text,       ""                      },
6478   { TYPE_SWITCH,        &setup.fade_screens,    "Fade Screens:"         },
6479   { TYPE_SWITCH,        &setup.quick_switch,    "Quick Player Focus Switch:" },
6480   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Menu Doors:"     },
6481   { TYPE_SWITCH,        &setup.show_titlescreen,"Show Title Screens:"   },
6482   { TYPE_SWITCH,        &setup.toons,           "Show Menu Animations:" },
6483   { TYPE_SWITCH,        &setup.small_game_graphics, "Small Game Graphics:" },
6484   { TYPE_YES_NO_AUTO,   &setup.debug.xsn_mode,  debug_xsn_mode          },
6485   { TYPE_EMPTY,         NULL,                   ""                      },
6486   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6487
6488   { 0,                  NULL,                   NULL                    }
6489 };
6490
6491 static struct TokenInfo setup_info_sound[] =
6492 {
6493   { TYPE_SWITCH,        &setup.sound_simple,    "Sound Effects (Normal):"  },
6494   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Effects (Looping):" },
6495   { TYPE_SWITCH,        &setup.sound_music,     "Music:"                },
6496   { TYPE_EMPTY,         NULL,                   ""                      },
6497   { TYPE_ENTER_LIST,    execSetupChooseVolumeSimple, "Sound Volume (Normal):" },
6498   { TYPE_STRING,        &volume_simple_text,    ""                      },
6499   { TYPE_ENTER_LIST,    execSetupChooseVolumeLoops, "Sound Volume (Looping):" },
6500   { TYPE_STRING,        &volume_loops_text,     ""                      },
6501   { TYPE_ENTER_LIST,    execSetupChooseVolumeMusic, "Music Volume:"     },
6502   { TYPE_STRING,        &volume_music_text,     ""                      },
6503   { TYPE_EMPTY,         NULL,                   ""                      },
6504   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6505
6506   { 0,                  NULL,                   NULL                    }
6507 };
6508
6509 static struct TokenInfo setup_info_artwork[] =
6510 {
6511   { TYPE_ENTER_LIST,    execSetupChooseGraphics,"Custom Graphics:"      },
6512   { TYPE_STRING,        &graphics_set_name,     ""                      },
6513   { TYPE_ENTER_LIST,    execSetupChooseSounds,  "Custom Sounds:"        },
6514   { TYPE_STRING,        &sounds_set_name,       ""                      },
6515   { TYPE_ENTER_LIST,    execSetupChooseMusic,   "Custom Music:"         },
6516   { TYPE_STRING,        &music_set_name,        ""                      },
6517   { TYPE_EMPTY,         NULL,                   ""                      },
6518   { TYPE_YES_NO_AUTO,&setup.override_level_graphics,"Override Level Graphics:"},
6519   { TYPE_YES_NO_AUTO,&setup.override_level_sounds,  "Override Level Sounds:"  },
6520   { TYPE_YES_NO_AUTO,&setup.override_level_music,   "Override Level Music:"   },
6521   { TYPE_EMPTY,         NULL,                   ""                      },
6522   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6523
6524   { 0,                  NULL,                   NULL                    }
6525 };
6526
6527 static struct TokenInfo setup_info_input[] =
6528 {
6529   { TYPE_SWITCH,        NULL,                   "Player:"               },
6530   { TYPE_SWITCH,        NULL,                   "Device:"               },
6531   { TYPE_SWITCH,        NULL,                   ""                      },
6532   { TYPE_SKIPPABLE,     NULL,                   ""                      },
6533   { TYPE_EMPTY,         NULL,                   ""                      },
6534   { TYPE_EMPTY,         NULL,                   ""                      },
6535   { TYPE_EMPTY,         NULL,                   ""                      },
6536   { TYPE_EMPTY,         NULL,                   ""                      },
6537   { TYPE_EMPTY,         NULL,                   ""                      },
6538   { TYPE_EMPTY,         NULL,                   ""                      },
6539   { TYPE_EMPTY,         NULL,                   ""                      },
6540   { TYPE_EMPTY,         NULL,                   ""                      },
6541   { TYPE_SKIPPABLE,     NULL,                   ""                      },
6542   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6543
6544   { 0,                  NULL,                   NULL                    }
6545 };
6546
6547 static struct TokenInfo setup_info_touch[] =
6548 {
6549   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
6550   { TYPE_STRING,        &touch_controls_text,   ""                      },
6551   { TYPE_EMPTY,         NULL,                   ""                      },
6552   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6553
6554   { 0,                  NULL,                   NULL                    }
6555 };
6556
6557 static struct TokenInfo setup_info_touch_virtual_buttons_0[] =
6558 {
6559   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
6560   { TYPE_STRING,        &touch_controls_text,   ""                      },
6561   { TYPE_EMPTY,         NULL,                   ""                      },
6562   { TYPE_ENTER_LIST,    execSetupChooseGridXSize_0, "Horizontal Buttons (Landscape):"   },
6563   { TYPE_STRING,        &grid_size_text[0][0],  ""                      },
6564   { TYPE_ENTER_LIST,    execSetupChooseGridYSize_0, "Vertical Buttons (Landscape):"     },
6565   { TYPE_STRING,        &grid_size_text[0][1],  ""                      },
6566   { TYPE_ENTER_LIST,    execSetupChooseTransparency, "Button Transparency:" },
6567   { TYPE_STRING,        &transparency_text,     ""                      },
6568   { TYPE_SWITCH,        &setup.touch.draw_outlined, "Draw Buttons Outlined:" },
6569   { TYPE_SWITCH,        &setup.touch.draw_pressed, "Highlight Pressed Buttons:" },
6570   { TYPE_EMPTY,         NULL,                   ""                      },
6571   { TYPE_ENTER_LIST,    execSetupConfigureVirtualButtons, "Configure Virtual Buttons" },
6572   { TYPE_EMPTY,         NULL,                   ""                      },
6573   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6574
6575   { 0,                  NULL,                   NULL                    }
6576 };
6577
6578 static struct TokenInfo setup_info_touch_virtual_buttons_1[] =
6579 {
6580   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
6581   { TYPE_STRING,        &touch_controls_text,   ""                      },
6582   { TYPE_EMPTY,         NULL,                   ""                      },
6583   { TYPE_ENTER_LIST,    execSetupChooseGridXSize_1, "Horizontal Buttons (Portrait):"    },
6584   { TYPE_STRING,        &grid_size_text[1][0],  ""                      },
6585   { TYPE_ENTER_LIST,    execSetupChooseGridYSize_1, "Vertical Buttons (Portrait):"      },
6586   { TYPE_STRING,        &grid_size_text[1][1],  ""                      },
6587   { TYPE_ENTER_LIST,    execSetupChooseTransparency, "Button Transparency:" },
6588   { TYPE_STRING,        &transparency_text,     ""                      },
6589   { TYPE_SWITCH,        &setup.touch.draw_outlined, "Draw Buttons Outlined:" },
6590   { TYPE_SWITCH,        &setup.touch.draw_pressed, "Highlight Pressed Buttons:" },
6591   { TYPE_EMPTY,         NULL,                   ""                      },
6592   { TYPE_ENTER_LIST,    execSetupConfigureVirtualButtons, "Configure Virtual Buttons" },
6593   { TYPE_EMPTY,         NULL,                   ""                      },
6594   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6595
6596   { 0,                  NULL,                   NULL                    }
6597 };
6598
6599 static struct TokenInfo *setup_info_touch_virtual_buttons[] =
6600 {
6601   setup_info_touch_virtual_buttons_0,
6602   setup_info_touch_virtual_buttons_1
6603 };
6604
6605 static struct TokenInfo setup_info_touch_wipe_gestures[] =
6606 {
6607   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
6608   { TYPE_STRING,        &touch_controls_text,   ""                      },
6609   { TYPE_EMPTY,         NULL,                   ""                      },
6610   { TYPE_ENTER_LIST,    execSetupChooseMoveDistance, "Move Trigger Distance:" },
6611   { TYPE_STRING,        &move_distance_text,    ""                      },
6612   { TYPE_ENTER_LIST,    execSetupChooseDropDistance, "Drop Trigger Distance:" },
6613   { TYPE_STRING,        &drop_distance_text,    ""                      },
6614   { TYPE_EMPTY,         NULL,                   ""                      },
6615   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6616
6617   { 0,                  NULL,                   NULL                    }
6618 };
6619
6620 static struct TokenInfo setup_info_shortcuts[] =
6621 {
6622   { TYPE_ENTER_MENU,    execSetupShortcuts1,    "Various Keys"          },
6623   { TYPE_ENTER_MENU,    execSetupShortcuts2,    "Player Focus"          },
6624   { TYPE_ENTER_MENU,    execSetupShortcuts3,    "Tape Buttons"          },
6625   { TYPE_ENTER_MENU,    execSetupShortcuts4,    "Sound & Music"         },
6626   { TYPE_ENTER_MENU,    execSetupShortcuts5,    "TAS Snap Keys"         },
6627   { TYPE_EMPTY,         NULL,                   ""                      },
6628   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6629
6630   { 0,                  NULL,                   NULL                    }
6631 };
6632
6633 static struct TokenInfo setup_info_shortcuts_1[] =
6634 {
6635   { TYPE_KEYTEXT,       NULL,           "Quick Save Game to Tape:",     },
6636   { TYPE_KEY,           &setup.shortcut.save_game, ""                   },
6637   { TYPE_KEYTEXT,       NULL,           "Quick Load Game from Tape:",   },
6638   { TYPE_KEY,           &setup.shortcut.load_game, ""                   },
6639   { TYPE_KEYTEXT,       NULL,           "Start Game & Toggle Pause:",   },
6640   { TYPE_KEY,           &setup.shortcut.toggle_pause, ""                },
6641   { TYPE_EMPTY,         NULL,                   ""                      },
6642   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on 'Esc' Key:"     },
6643   { TYPE_YES_NO, &setup.ask_on_escape_editor,   "Ask on 'Esc' Key (Editor):" },
6644   { TYPE_EMPTY,         NULL,                   ""                      },
6645   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
6646
6647   { 0,                  NULL,                   NULL                    }
6648 };
6649
6650 static struct TokenInfo setup_info_shortcuts_2[] =
6651 {
6652   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 1:",       },
6653   { TYPE_KEY,           &setup.shortcut.focus_player[0], ""             },
6654   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 2:",       },
6655   { TYPE_KEY,           &setup.shortcut.focus_player[1], ""             },
6656   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 3:",       },
6657   { TYPE_KEY,           &setup.shortcut.focus_player[2], ""             },
6658   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 4:",       },
6659   { TYPE_KEY,           &setup.shortcut.focus_player[3], ""             },
6660   { TYPE_KEYTEXT,       NULL,           "Set Focus to All Players:",    },
6661   { TYPE_KEY,           &setup.shortcut.focus_player_all, ""            },
6662   { TYPE_EMPTY,         NULL,                   ""                      },
6663   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
6664
6665   { 0,                  NULL,                   NULL                    }
6666 };
6667
6668 static struct TokenInfo setup_info_shortcuts_3[] =
6669 {
6670   { TYPE_KEYTEXT,       NULL,                   "Eject Tape:",          },
6671   { TYPE_KEY,           &setup.shortcut.tape_eject, ""                  },
6672   { TYPE_KEYTEXT,       NULL,                   "Warp / Single Step:",  },
6673   { TYPE_KEY,           &setup.shortcut.tape_extra, ""                  },
6674   { TYPE_KEYTEXT,       NULL,                   "Stop Tape:",           },
6675   { TYPE_KEY,           &setup.shortcut.tape_stop, ""                   },
6676   { TYPE_KEYTEXT,       NULL,                   "Pause / Unpause Tape:",},
6677   { TYPE_KEY,           &setup.shortcut.tape_pause, ""                  },
6678   { TYPE_KEYTEXT,       NULL,                   "Record Tape:",         },
6679   { TYPE_KEY,           &setup.shortcut.tape_record, ""                 },
6680   { TYPE_KEYTEXT,       NULL,                   "Play Tape:",           },
6681   { TYPE_KEY,           &setup.shortcut.tape_play, ""                   },
6682   { TYPE_EMPTY,         NULL,                   ""                      },
6683   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
6684
6685   { 0,                  NULL,                   NULL                    }
6686 };
6687
6688 static struct TokenInfo setup_info_shortcuts_4[] =
6689 {
6690   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Normal):", },
6691   { TYPE_KEY,           &setup.shortcut.sound_simple, ""                },
6692   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Looping):", },
6693   { TYPE_KEY,           &setup.shortcut.sound_loops, ""                 },
6694   { TYPE_KEYTEXT,       NULL,           "Toggle Music:",                },
6695   { TYPE_KEY,           &setup.shortcut.sound_music, ""                 },
6696   { TYPE_EMPTY,         NULL,                   ""                      },
6697   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
6698
6699   { 0,                  NULL,                   NULL                    }
6700 };
6701
6702 static struct TokenInfo setup_info_shortcuts_5[] =
6703 {
6704   { TYPE_KEYTEXT,       NULL,                   "Snap Left:",           },
6705   { TYPE_KEY,           &setup.shortcut.snap_left, ""                   },
6706   { TYPE_KEYTEXT,       NULL,                   "Snap Right:",          },
6707   { TYPE_KEY,           &setup.shortcut.snap_right, ""                  },
6708   { TYPE_KEYTEXT,       NULL,                   "Snap Up:",             },
6709   { TYPE_KEY,           &setup.shortcut.snap_up, ""                     },
6710   { TYPE_KEYTEXT,       NULL,                   "Snap Down:",           },
6711   { TYPE_KEY,           &setup.shortcut.snap_down, ""                   },
6712   { TYPE_EMPTY,         NULL,                   ""                      },
6713   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
6714
6715   { 0,                  NULL,                   NULL                    }
6716 };
6717
6718 static Key getSetupKey(void)
6719 {
6720   Key key = KSYM_UNDEFINED;
6721   boolean got_key_event = FALSE;
6722
6723   while (!got_key_event)
6724   {
6725     Event event;
6726
6727     if (NextValidEvent(&event))
6728     {
6729       switch (event.type)
6730       {
6731         case EVENT_KEYPRESS:
6732           {
6733             key = GetEventKey((KeyEvent *)&event, TRUE);
6734
6735             // press 'Escape' or 'Enter' to keep the existing key binding
6736             if (key == KSYM_Escape || key == KSYM_Return)
6737               key = KSYM_UNDEFINED;     // keep old value
6738
6739             got_key_event = TRUE;
6740           }
6741           break;
6742
6743         case EVENT_KEYRELEASE:
6744           key_joystick_mapping = 0;
6745           break;
6746
6747         default:
6748           HandleOtherEvents(&event);
6749           break;
6750       }
6751     }
6752
6753     BackToFront();
6754   }
6755
6756   return key;
6757 }
6758
6759 static int getSetupValueFont(int type, void *value)
6760 {
6761   if (type & TYPE_GHOSTED)
6762     return FONT_OPTION_OFF;
6763   else if (type & TYPE_KEY)
6764     return (type & TYPE_QUERY ? FONT_INPUT_1_ACTIVE : FONT_VALUE_1);
6765   else if (type & TYPE_STRING)
6766     return FONT_VALUE_2;
6767   else if (type & TYPE_ECS_AGA)
6768     return FONT_VALUE_1;
6769   else if (type & TYPE_BOOLEAN_STYLE)
6770     return (*(boolean *)value ? FONT_OPTION_ON : FONT_OPTION_OFF);
6771   else if (type & TYPE_YES_NO_AUTO)
6772     return (*(int *)value == AUTO  ? FONT_OPTION_ON :
6773             *(int *)value == FALSE ? FONT_OPTION_OFF : FONT_OPTION_ON);
6774   else if (type & TYPE_PLAYER)
6775     return FONT_VALUE_1;
6776   else
6777     return FONT_VALUE_1;
6778 }
6779
6780 static int getSetupValueFontNarrow(int type, int font_nr)
6781 {
6782   return (font_nr == FONT_VALUE_1    ? FONT_VALUE_NARROW :
6783           font_nr == FONT_OPTION_ON  ? FONT_OPTION_ON_NARROW :
6784           font_nr == FONT_OPTION_OFF ? FONT_OPTION_OFF_NARROW :
6785           font_nr);
6786 }
6787
6788 static void drawSetupValue(int screen_pos, int setup_info_pos_raw)
6789 {
6790   int si_pos = (setup_info_pos_raw < 0 ? screen_pos : setup_info_pos_raw);
6791   struct TokenInfo *si = &setup_info[si_pos];
6792   boolean font_draw_xoffset_modified = FALSE;
6793   boolean scrollbar_needed = (num_setup_info < max_setup_info);
6794   int font_draw_xoffset_old = -1;
6795   int xoffset = (scrollbar_needed ? -1 : 0);
6796   int menu_screen_value_xpos = MENU_SCREEN_VALUE_XPOS + xoffset;
6797   int menu_screen_max_xpos = MENU_SCREEN_MAX_XPOS + xoffset;
6798   int xpos = menu_screen_value_xpos;
6799   int ypos = MENU_SCREEN_START_YPOS + screen_pos;
6800   int startx = mSX + xpos * 32;
6801   int starty = mSY + ypos * 32;
6802   int type = si->type;
6803   void *value = si->value;
6804   char *value_string = getSetupValue(type, value);
6805   int font_nr_default = getSetupValueFont(type, value);
6806   int font_width_default = getFontWidth(font_nr_default);
6807   int font_nr = font_nr_default;
6808   int i;
6809
6810   if (value_string == NULL)
6811     return;
6812
6813   if (type & TYPE_KEY)
6814   {
6815     xpos = MENU_SCREEN_START_XPOS;
6816
6817     if (type & TYPE_QUERY)
6818       value_string = "<press key>";
6819   }
6820   else if (type & TYPE_STRING)
6821   {
6822     int max_value_len = (SXSIZE - 2 * TILEX) / font_width_default;
6823
6824     xpos = MENU_SCREEN_START_XPOS;
6825
6826     if (strlen(value_string) > max_value_len)
6827       value_string[max_value_len] = '\0';
6828   }
6829   else if (type & TYPE_YES_NO_AUTO)
6830   {
6831     xpos = menu_screen_value_xpos - 1;
6832   }
6833   else if (type & TYPE_PLAYER)
6834   {
6835     int displayed_player_nr = *(int *)value + 1;
6836
6837     value_string = getSetupValue(TYPE_INTEGER, (void *)&displayed_player_nr);
6838   }
6839
6840   startx = mSX + xpos * 32;
6841   starty = mSY + ypos * 32;
6842
6843   // special check if right-side setup values moved left due to scrollbar
6844   if (scrollbar_needed && xpos > MENU_SCREEN_START_XPOS)
6845   {
6846     int max_menu_text_length = 26;      // maximum text length for classic menu
6847     int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
6848     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
6849     int text_font_nr = getMenuTextFont(FONT_MENU_2);
6850     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
6851     int text_width = max_menu_text_length * getFontWidth(text_font_nr);
6852
6853     if (startx + font_xoffset < text_startx + text_width + text_font_xoffset)
6854     {
6855       // when using narrow font, left-shifting text "auto" not needed
6856       if (type & TYPE_YES_NO_AUTO)
6857         xpos += 1;
6858
6859       xpos += 1;
6860       startx = mSX + xpos * 32;
6861
6862       font_nr = getSetupValueFontNarrow(type, font_nr);
6863     }
6864   }
6865
6866   // downward compatibility correction for Juergen Bonhagen's menu settings
6867   if (setup_mode != SETUP_MODE_INPUT)
6868   {
6869     int max_menu_text_length_big = (menu_screen_value_xpos -
6870                                     MENU_SCREEN_START_XPOS);
6871     int max_menu_text_length_medium = max_menu_text_length_big * 2;
6872     int check_font_nr = FONT_OPTION_ON; // known font that needs correction
6873     int font1_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
6874     int font2_xoffset = getFontBitmapInfo(check_font_nr)->draw_xoffset;
6875     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
6876     int text_font_nr = getMenuTextFont(FONT_MENU_2);
6877     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
6878     int text_width = max_menu_text_length_medium * getFontWidth(text_font_nr);
6879     boolean correct_font_draw_xoffset = FALSE;
6880
6881     if (xpos == MENU_SCREEN_START_XPOS &&
6882         startx + font1_xoffset < text_startx + text_font_xoffset)
6883       correct_font_draw_xoffset = TRUE;
6884
6885     if (xpos == menu_screen_value_xpos &&
6886         startx + font2_xoffset < text_startx + text_width + text_font_xoffset)
6887       correct_font_draw_xoffset = TRUE;
6888
6889     // check if setup value would overlap with setup text when printed
6890     // (this can happen for extreme/wrong values for font draw offset)
6891     if (correct_font_draw_xoffset)
6892     {
6893       font_draw_xoffset_old = getFontBitmapInfo(font_nr)->draw_xoffset;
6894       font_draw_xoffset_modified = TRUE;
6895
6896       if (type & TYPE_KEY)
6897         getFontBitmapInfo(font_nr)->draw_xoffset += 2 * getFontWidth(font_nr);
6898       else if (!(type & TYPE_STRING))
6899         getFontBitmapInfo(font_nr)->draw_xoffset = text_font_xoffset + 20 -
6900           max_menu_text_length_medium * (16 - getFontWidth(text_font_nr));
6901     }
6902   }
6903
6904   for (i = 0; i <= menu_screen_max_xpos - xpos; i++)
6905     DrawText(startx + i * font_width_default, starty, " ", font_nr_default);
6906
6907   DrawText(startx, starty, value_string, font_nr);
6908
6909   if (type & TYPE_PLAYER)
6910   {
6911     struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
6912     int player_nr = *(int *)value;
6913     int xoff = font->draw_xoffset + getFontWidth(font_nr);
6914     int yoff = font->draw_yoffset + (getFontHeight(font_nr) - TILEY) / 2;
6915     int startx2 = startx + xoff;
6916     int starty2 = starty + yoff;
6917
6918     if (DrawingOnBackground(startx2, starty2))
6919       ClearRectangleOnBackground(drawto, startx2, starty2, TILEX, TILEY);
6920
6921     DrawFixedGraphicThruMaskExt(drawto, startx2, starty2,
6922                                 PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
6923   }
6924
6925   if (font_draw_xoffset_modified)
6926     getFontBitmapInfo(font_nr)->draw_xoffset = font_draw_xoffset_old;
6927 }
6928
6929 static void changeSetupValue(int screen_pos, int setup_info_pos_raw, int dx)
6930 {
6931   int si_pos = (setup_info_pos_raw < 0 ? screen_pos : setup_info_pos_raw);
6932   struct TokenInfo *si = &setup_info[si_pos];
6933
6934   if (si->type & TYPE_BOOLEAN_STYLE)
6935   {
6936     *(boolean *)si->value ^= TRUE;
6937   }
6938   else if (si->type & TYPE_YES_NO_AUTO)
6939   {
6940     *(int *)si->value =
6941       (dx == -1 ?
6942        (*(int *)si->value == AUTO ? TRUE :
6943         *(int *)si->value == TRUE ? FALSE : AUTO) :
6944        (*(int *)si->value == TRUE ? AUTO :
6945         *(int *)si->value == AUTO ? FALSE : TRUE));
6946   }
6947   else if (si->type & TYPE_KEY)
6948   {
6949     Key key;
6950
6951     si->type |= TYPE_QUERY;
6952     drawSetupValue(screen_pos, setup_info_pos_raw);
6953     si->type &= ~TYPE_QUERY;
6954
6955     key = getSetupKey();
6956     if (key != KSYM_UNDEFINED)
6957       *(Key *)si->value = key;
6958   }
6959   else if (si->type & TYPE_PLAYER)
6960   {
6961     int player_nr = *(int *)si->value;
6962
6963     if (dx)
6964       player_nr += dx;
6965     else
6966       player_nr = Request("Choose player", REQ_PLAYER) - 1;
6967
6968     *(int *)si->value = MIN(MAX(0, player_nr), MAX_PLAYERS - 1);
6969   }
6970
6971   drawSetupValue(screen_pos, setup_info_pos_raw);
6972
6973   // fullscreen state may have changed at this point
6974   if (si->value == &setup.fullscreen)
6975     ToggleFullscreenIfNeeded();
6976
6977   // network mode may have changed at this point
6978   if (si->value == &setup.network_mode)
6979     ToggleNetworkModeIfNeeded();
6980
6981   // game speed list may have changed at this point
6982   if (si->value == &setup.game_speed_extended)
6983     ToggleGameSpeedsListIfNeeded();
6984 }
6985
6986 static struct TokenInfo *getSetupInfoFinal(struct TokenInfo *setup_info_orig)
6987 {
6988   static struct TokenInfo *setup_info_final = NULL;
6989   int list_size = 0;
6990   int list_pos = 0;
6991   int i;
6992
6993   // determine maximum list size of target list
6994   while (setup_info_orig[list_size++].type != 0);
6995
6996   // free, allocate and clear memory for target list
6997   checked_free(setup_info_final);
6998   setup_info_final = checked_calloc(list_size * sizeof(struct TokenInfo));
6999
7000   // copy setup info list without setup entries marked as hidden
7001   for (i = 0; setup_info_orig[i].type != 0; i++)
7002   {
7003     // skip setup entries configured to be hidden
7004     if (hideSetupEntry(setup_info_orig[i].value))
7005       continue;
7006
7007     // skip skippable setup entries if screen is lower than usual
7008     if (SCR_FIELDY < SCR_FIELDY_DEFAULT &&
7009         setup_info_orig[i].type == TYPE_SKIPPABLE)
7010       continue;
7011
7012     setup_info_final[list_pos++] = setup_info_orig[i];
7013   }
7014
7015   return setup_info_final;
7016 }
7017
7018 static void DrawSetupScreen_Generic(void)
7019 {
7020   int fade_mask = REDRAW_FIELD;
7021   boolean redraw_all = FALSE;
7022   char *title_string = NULL;
7023   int i;
7024
7025   if (CheckFadeAll())
7026     fade_mask = REDRAW_ALL;
7027
7028   UnmapAllGadgets();
7029   FadeMenuSoundsAndMusic();
7030
7031   FreeScreenGadgets();
7032   CreateScreenGadgets();
7033
7034   if (redraw_mask & REDRAW_ALL)
7035     redraw_all = TRUE;
7036
7037   FadeOut(fade_mask);
7038
7039   // needed if different viewport properties defined for setup screen
7040   ChangeViewportPropertiesIfNeeded();
7041
7042   SetMainBackgroundImage(IMG_BACKGROUND_SETUP);
7043
7044   ClearField();
7045
7046   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
7047
7048   if (setup_mode == SETUP_MODE_MAIN)
7049   {
7050     setup_info = setup_info_main;
7051     title_string = STR_SETUP_MAIN;
7052   }
7053   else if (setup_mode == SETUP_MODE_GAME)
7054   {
7055     setup_info = setup_info_game;
7056     title_string = STR_SETUP_GAME;
7057   }
7058   else if (setup_mode == SETUP_MODE_ENGINES)
7059   {
7060     setup_info = setup_info_engines;
7061     title_string = STR_SETUP_ENGINES;
7062   }
7063   else if (setup_mode == SETUP_MODE_EDITOR)
7064   {
7065     setup_info = setup_info_editor;
7066     title_string = STR_SETUP_EDITOR;
7067   }
7068   else if (setup_mode == SETUP_MODE_GRAPHICS)
7069   {
7070     setup_info = setup_info_graphics;
7071     title_string = STR_SETUP_GRAPHICS;
7072   }
7073   else if (setup_mode == SETUP_MODE_SOUND)
7074   {
7075     setup_info = setup_info_sound;
7076     title_string = STR_SETUP_SOUND;
7077   }
7078   else if (setup_mode == SETUP_MODE_ARTWORK)
7079   {
7080     setup_info = setup_info_artwork;
7081     title_string = STR_SETUP_ARTWORK;
7082   }
7083   else if (setup_mode == SETUP_MODE_TOUCH)
7084   {
7085     setup_info = setup_info_touch;
7086     title_string = STR_SETUP_TOUCH;
7087
7088     if (strEqual(setup.touch.control_type, TOUCH_CONTROL_VIRTUAL_BUTTONS))
7089       setup_info = setup_info_touch_virtual_buttons[GRID_ACTIVE_NR()];
7090     else if (strEqual(setup.touch.control_type, TOUCH_CONTROL_WIPE_GESTURES))
7091       setup_info = setup_info_touch_wipe_gestures;
7092   }
7093   else if (setup_mode == SETUP_MODE_SHORTCUTS)
7094   {
7095     setup_info = setup_info_shortcuts;
7096     title_string = STR_SETUP_SHORTCUTS;
7097   }
7098   else if (setup_mode == SETUP_MODE_SHORTCUTS_1)
7099   {
7100     setup_info = setup_info_shortcuts_1;
7101     title_string = STR_SETUP_SHORTCUTS;
7102   }
7103   else if (setup_mode == SETUP_MODE_SHORTCUTS_2)
7104   {
7105     setup_info = setup_info_shortcuts_2;
7106     title_string = STR_SETUP_SHORTCUTS;
7107   }
7108   else if (setup_mode == SETUP_MODE_SHORTCUTS_3)
7109   {
7110     setup_info = setup_info_shortcuts_3;
7111     title_string = STR_SETUP_SHORTCUTS;
7112   }
7113   else if (setup_mode == SETUP_MODE_SHORTCUTS_4)
7114   {
7115     setup_info = setup_info_shortcuts_4;
7116     title_string = STR_SETUP_SHORTCUTS;
7117   }
7118   else if (setup_mode == SETUP_MODE_SHORTCUTS_5)
7119   {
7120     setup_info = setup_info_shortcuts_5;
7121     title_string = STR_SETUP_SHORTCUTS;
7122   }
7123
7124   // use modified setup info without setup entries marked as hidden
7125   setup_info = getSetupInfoFinal(setup_info);
7126
7127   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, title_string);
7128
7129   // determine maximal number of setup entries that can be displayed on screen
7130   num_setup_info = 0;
7131   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
7132     num_setup_info++;
7133
7134   // determine maximal number of setup entries available for this setup screen
7135   max_setup_info = 0;
7136   for (i = 0; setup_info[i].type != 0; i++)
7137     max_setup_info++;
7138
7139   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
7140
7141   MapScreenGadgets(max_setup_info);
7142
7143   if (redraw_all)
7144     redraw_mask = fade_mask = REDRAW_ALL;
7145
7146   DrawMaskedBorder(fade_mask);
7147
7148   FadeIn(fade_mask);
7149 }
7150
7151 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
7152 {
7153   menu_info = setup_info;
7154
7155   HandleMenuScreen(mx, my, dx, dy, button,
7156                    setup_mode, num_setup_info, max_setup_info);
7157 }
7158
7159 static void DrawSetupScreen_Input(void)
7160 {
7161   int i;
7162
7163   FadeOut(REDRAW_FIELD);
7164
7165   ClearField();
7166
7167   setup_info = getSetupInfoFinal(setup_info_input);
7168
7169   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, STR_SETUP_INPUT);
7170
7171   for (i = 0; setup_info[i].type != 0; i++)
7172   {
7173     if (setup_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
7174       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
7175     else if (setup_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
7176       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
7177     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
7178       initCursor(i, IMG_MENU_BUTTON);
7179
7180     DrawCursorAndText_Setup(i, -1, FALSE);
7181   }
7182
7183   // create gadgets for setup input menu screen
7184   FreeScreenGadgets();
7185   CreateScreenGadgets();
7186
7187   // map gadgets for setup input menu screen
7188   MapScreenMenuGadgets(SCREEN_MASK_INPUT);
7189
7190   HandleSetupScreen_Input(0, 0, 0, 0, MB_MENU_INITIALIZE);
7191
7192   FadeIn(REDRAW_FIELD);
7193 }
7194
7195 static void setJoystickDeviceToNr(char *device_name, int device_nr)
7196 {
7197   if (device_name == NULL)
7198     return;
7199
7200   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
7201     device_nr = 0;
7202
7203   if (strlen(device_name) > 1)
7204   {
7205     char c1 = device_name[strlen(device_name) - 1];
7206     char c2 = device_name[strlen(device_name) - 2];
7207
7208     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
7209       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
7210   }
7211   else
7212     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
7213             strlen(device_name));
7214 }
7215
7216 static void drawPlayerSetupInputInfo(int player_nr, boolean active)
7217 {
7218   int i;
7219   static struct SetupKeyboardInfo custom_key;
7220   static struct
7221   {
7222     Key *key;
7223     char *text;
7224   } custom[] =
7225   {
7226     { &custom_key.left,  "Axis/Pad Left"  },
7227     { &custom_key.right, "Axis/Pad Right" },
7228     { &custom_key.up,    "Axis/Pad Up"    },
7229     { &custom_key.down,  "Axis/Pad Down"  },
7230     { &custom_key.snap,  "Button 1/A/X"   },
7231     { &custom_key.drop,  "Button 2/B/Y"   }
7232   };
7233   static char *joystick_name[MAX_PLAYERS] =
7234   {
7235     "Joystick1",
7236     "Joystick2",
7237     "Joystick3",
7238     "Joystick4"
7239   };
7240   int font_nr_menu = (active ? FONT_MENU_1_ACTIVE : FONT_MENU_1);
7241   int font_nr_info = FONT_MENU_1;
7242   int font_nr_name = FONT_VALUE_OLD;
7243   int font_nr_on   = FONT_VALUE_1;
7244   int font_nr_off  = FONT_VALUE_OLD;
7245   int pos = 4;
7246
7247   if (SCR_FIELDX < SCR_FIELDX_DEFAULT)
7248   {
7249     font_nr_info = FONT_MENU_2;
7250     font_nr_on   = FONT_VALUE_NARROW;
7251     font_nr_off  = FONT_VALUE_OLD_NARROW;
7252   }
7253
7254   custom_key = setup.input[player_nr].key;
7255
7256   DrawText(mSX + 11 * 32, mSY + 2 * 32, int2str(player_nr + 1, 1),
7257            FONT_INPUT_1_ACTIVE);
7258
7259   ClearRectangleOnBackground(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
7260                              TILEX, TILEY);
7261   DrawFixedGraphicThruMaskExt(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
7262                               PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
7263
7264   if (setup.input[player_nr].use_joystick)
7265   {
7266     char *device_name = setup.input[player_nr].joy.device_name;
7267     int joystick_nr = getJoystickNrFromDeviceName(device_name);
7268     boolean joystick_active = CheckJoystickOpened(joystick_nr);
7269     char *text = joystick_name[joystick_nr];
7270     int font_nr = (joystick_active ? font_nr_on : font_nr_off);
7271
7272     DrawText(mSX + 8 * 32, mSY + 3 * 32, text, font_nr);
7273     DrawText(mSX + 32, mSY + 4 * 32, "Configure", font_nr_menu);
7274   }
7275   else
7276   {
7277     DrawText(mSX + 8 * 32, mSY + 3 * 32, "Keyboard ", font_nr_on);
7278     DrawText(mSX + 1 * 32, mSY + 4 * 32, "Customize", font_nr_menu);
7279   }
7280
7281   if (SCR_FIELDY >= SCR_FIELDY_DEFAULT)
7282     DrawText(mSX + 32, mSY + 5 * 32, "Actual Settings:", font_nr_info);
7283   else
7284     pos = 3;
7285
7286   drawCursorXY(1, pos + 0, IMG_MENU_BUTTON_LEFT);
7287   drawCursorXY(1, pos + 1, IMG_MENU_BUTTON_RIGHT);
7288   drawCursorXY(1, pos + 2, IMG_MENU_BUTTON_UP);
7289   drawCursorXY(1, pos + 3, IMG_MENU_BUTTON_DOWN);
7290
7291   DrawText(mSX + 2 * 32, mSY + (pos + 2) * 32, ":", font_nr_name);
7292   DrawText(mSX + 2 * 32, mSY + (pos + 3) * 32, ":", font_nr_name);
7293   DrawText(mSX + 2 * 32, mSY + (pos + 4) * 32, ":", font_nr_name);
7294   DrawText(mSX + 2 * 32, mSY + (pos + 5) * 32, ":", font_nr_name);
7295   DrawText(mSX + 1 * 32, mSY + (pos + 6) * 32, "Snap Field:", font_nr_name);
7296   DrawText(mSX + 1 * 32, mSY + (pos + 8) * 32, "Drop Element:", font_nr_name);
7297
7298   for (i = 0; i < 6; i++)
7299   {
7300     int ypos = (pos + 2) + i + (i > 3 ? i - 3 : 0);
7301
7302     DrawText(mSX + 3 * 32, mSY + ypos * 32,
7303              "              ", font_nr_on);
7304     DrawText(mSX + 3 * 32, mSY + ypos * 32,
7305              (setup.input[player_nr].use_joystick ?
7306               custom[i].text :
7307               getKeyNameFromKey(*custom[i].key)), font_nr_on);
7308   }
7309 }
7310
7311 static int input_player_nr = 0;
7312
7313 static void HandleSetupScreen_Input_Player(int step, int direction)
7314 {
7315   int old_player_nr = input_player_nr;
7316   int new_player_nr;
7317
7318   new_player_nr = old_player_nr + step * direction;
7319   if (new_player_nr < 0)
7320     new_player_nr = 0;
7321   if (new_player_nr > MAX_PLAYERS - 1)
7322     new_player_nr = MAX_PLAYERS - 1;
7323
7324   if (new_player_nr != old_player_nr)
7325   {
7326     input_player_nr = new_player_nr;
7327
7328     drawPlayerSetupInputInfo(input_player_nr, FALSE);
7329   }
7330 }
7331
7332 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
7333 {
7334   static int choice = 0;
7335   int x = 0;
7336   int y = choice;
7337   int pos_start  = SETUPINPUT_SCREEN_POS_START;
7338   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
7339   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
7340   int pos_end    = SETUPINPUT_SCREEN_POS_END;
7341
7342   if (SCR_FIELDY < SCR_FIELDY_DEFAULT)
7343   {
7344     int i;
7345
7346     for (i = 0; setup_info_input[i].type != 0; i++)
7347     {
7348       // adjust menu structure according to skipped setup entries
7349       if (setup_info_input[i].type == TYPE_SKIPPABLE)
7350       {
7351         pos_empty2--;
7352         pos_end--;
7353       }
7354     }
7355   }
7356
7357   if (button == MB_MENU_INITIALIZE)
7358   {
7359     // input setup menu may have changed size due to graphics configuration
7360     if (choice >= pos_empty1)
7361       choice = pos_end;
7362
7363     drawPlayerSetupInputInfo(input_player_nr, (choice == 2));
7364
7365     DrawCursorAndText_Setup(choice, -1, TRUE);
7366
7367     return;
7368   }
7369   else if (button == MB_MENU_LEAVE)
7370   {
7371     setup_mode = SETUP_MODE_MAIN;
7372     DrawSetupScreen();
7373     InitJoysticks();
7374
7375     return;
7376   }
7377
7378   if (mx || my)         // mouse input
7379   {
7380     x = (mx - mSX) / 32;
7381     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
7382   }
7383   else if (dx || dy)    // keyboard input
7384   {
7385     if (dx && choice == 0)
7386       x = (dx < 0 ? 10 : 12);
7387     else if ((dx && choice == 1) ||
7388              (dx == -1 && choice == pos_end))
7389       button = MB_MENU_CHOICE;
7390     else if (dy)
7391       y = choice + dy;
7392
7393     if (y >= pos_empty1 && y <= pos_empty2)
7394       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
7395   }
7396
7397   if (y == 0 && dx != 0 && button)
7398   {
7399     HandleSetupScreen_Input_Player(1, dx < 0 ? -1 : +1);
7400   }
7401   else if (IN_VIS_FIELD(x, y) &&        // (does not use "IN_VIS_MENU()" yet)
7402            y >= pos_start && y <= pos_end &&
7403            !(y >= pos_empty1 && y <= pos_empty2))
7404   {
7405     if (button)
7406     {
7407       if (y != choice)
7408       {
7409         DrawCursorAndText_Setup(choice, -1, FALSE);
7410         DrawCursorAndText_Setup(y, -1, TRUE);
7411
7412         drawPlayerSetupInputInfo(input_player_nr, (y == 2));
7413
7414         choice = y;
7415       }
7416     }
7417     else
7418     {
7419       if (y == 1)
7420       {
7421         char *device_name = setup.input[input_player_nr].joy.device_name;
7422
7423         if (!setup.input[input_player_nr].use_joystick)
7424         {
7425           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
7426
7427           setJoystickDeviceToNr(device_name, new_device_nr);
7428           setup.input[input_player_nr].use_joystick = TRUE;
7429         }
7430         else
7431         {
7432           int device_nr = getJoystickNrFromDeviceName(device_name);
7433           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
7434
7435           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
7436             setup.input[input_player_nr].use_joystick = FALSE;
7437           else
7438             setJoystickDeviceToNr(device_name, new_device_nr);
7439         }
7440
7441         drawPlayerSetupInputInfo(input_player_nr, FALSE);
7442       }
7443       else if (y == 2)
7444       {
7445         if (setup.input[input_player_nr].use_joystick)
7446           ConfigureJoystick(input_player_nr);
7447         else
7448           CustomizeKeyboard(input_player_nr);
7449       }
7450       else if (y == pos_end)
7451       {
7452         InitJoysticks();
7453
7454         FadeSetLeaveMenu();
7455
7456         setup_mode = SETUP_MODE_MAIN;
7457         DrawSetupScreen();
7458       }
7459     }
7460   }
7461 }
7462
7463 static boolean CustomizeKeyboardMain(int player_nr)
7464 {
7465   int i;
7466   int step_nr;
7467   boolean finished = FALSE;
7468   static struct SetupKeyboardInfo custom_key;
7469   static struct
7470   {
7471     Key *key;
7472     char *text;
7473   } customize_step[] =
7474   {
7475     { &custom_key.left,  "Move Left"    },
7476     { &custom_key.right, "Move Right"   },
7477     { &custom_key.up,    "Move Up"      },
7478     { &custom_key.down,  "Move Down"    },
7479     { &custom_key.snap,  "Snap Field"   },
7480     { &custom_key.drop,  "Drop Element" }
7481   };
7482   int font_nr_old = FONT_VALUE_OLD;
7483   int font_nr_new = FONT_VALUE_1;
7484   boolean success = FALSE;
7485
7486   if (SCR_FIELDX < SCR_FIELDX_DEFAULT)
7487   {
7488     font_nr_old = FONT_VALUE_OLD_NARROW;
7489     font_nr_new = FONT_VALUE_NARROW;
7490   }
7491
7492   // read existing key bindings from player setup
7493   custom_key = setup.input[player_nr].key;
7494
7495   FadeSetEnterMenu();
7496   FadeOut(REDRAW_FIELD);
7497
7498   ClearField();
7499
7500   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Keyboard Input");
7501
7502   step_nr = 0;
7503   DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
7504            customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
7505   DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
7506            "Key:", FONT_INPUT_1_ACTIVE);
7507   DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
7508            getKeyNameFromKey(*customize_step[step_nr].key), font_nr_old);
7509
7510   FadeIn(REDRAW_FIELD);
7511
7512   while (!finished)
7513   {
7514     Event event;
7515
7516     if (NextValidEvent(&event))
7517     {
7518       switch (event.type)
7519       {
7520         case EVENT_KEYPRESS:
7521           {
7522             Key key = GetEventKey((KeyEvent *)&event, FALSE);
7523
7524             // press 'Escape' to abort and keep the old key bindings
7525             if (key == KSYM_Escape)
7526             {
7527               FadeSkipNextFadeIn();
7528
7529               finished = TRUE;
7530
7531               break;
7532             }
7533
7534             // press 'Enter' to keep the existing key binding
7535             if (key == KSYM_Return)
7536               key = *customize_step[step_nr].key;
7537
7538             // check if key already used
7539             for (i = 0; i < step_nr; i++)
7540               if (*customize_step[i].key == key)
7541                 break;
7542             if (i < step_nr)
7543               break;
7544
7545             // got new key binding
7546             *customize_step[step_nr].key = key;
7547             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
7548                      "             ", font_nr_new);
7549             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
7550                      getKeyNameFromKey(key), font_nr_new);
7551             step_nr++;
7552
7553             // un-highlight last query
7554             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1)) * 32,
7555                      customize_step[step_nr - 1].text, FONT_MENU_1);
7556             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1) + 1) * 32,
7557                      "Key:", FONT_MENU_1);
7558
7559             // all keys configured
7560             if (step_nr == 6)
7561             {
7562               finished = TRUE;
7563               success = TRUE;
7564
7565               break;
7566             }
7567
7568             // query next key binding
7569             DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
7570                      customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
7571             DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
7572                      "Key:", FONT_INPUT_1_ACTIVE);
7573             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
7574                      getKeyNameFromKey(*customize_step[step_nr].key),
7575                      font_nr_old);
7576           }
7577           break;
7578
7579         case EVENT_KEYRELEASE:
7580           key_joystick_mapping = 0;
7581           break;
7582
7583         default:
7584           HandleOtherEvents(&event);
7585           break;
7586       }
7587     }
7588
7589     BackToFront();
7590   }
7591
7592   // write new key bindings back to player setup, if successfully finished
7593   if (success)
7594     setup.input[player_nr].key = custom_key;
7595
7596   return success;
7597 }
7598
7599 void CustomizeKeyboard(int player_nr)
7600 {
7601   boolean success = CustomizeKeyboardMain(player_nr);
7602
7603   if (success)
7604   {
7605     int font_nr = FONT_TITLE_1;
7606     int font_height = getFontHeight(font_nr);
7607     int ypos1 = SYSIZE / 2 - font_height * 2;
7608     int ypos2 = SYSIZE / 2 - font_height * 1;
7609     unsigned int wait_frame_delay = 0;
7610     unsigned int wait_frame_delay_value = 2000;
7611
7612     ResetDelayCounter(&wait_frame_delay);
7613
7614     ClearField();
7615
7616     DrawTextSCentered(ypos1, font_nr, "Keyboard");
7617     DrawTextSCentered(ypos2, font_nr, "configured!");
7618
7619     while (!DelayReached(&wait_frame_delay, wait_frame_delay_value))
7620       BackToFront();
7621
7622     ClearEventQueue();
7623   }
7624
7625   DrawSetupScreen_Input();
7626 }
7627
7628 // game controller mapping generator by Gabriel Jacobo <gabomdq@gmail.com>
7629
7630 #define MARKER_BUTTON           1
7631 #define MARKER_AXIS_X           2
7632 #define MARKER_AXIS_Y           3
7633
7634 static boolean ConfigureJoystickMapButtonsAndAxes(SDL_Joystick *joystick)
7635 {
7636   static boolean bitmaps_initialized = FALSE;
7637   boolean screen_initialized = FALSE;
7638   static Bitmap *controller, *button, *axis_x, *axis_y;
7639   char *name;
7640   boolean success = TRUE;
7641   boolean done = FALSE, next = FALSE;
7642   Event event;
7643   int alpha = 200, alpha_step = -1;
7644   int alpha_ticks = 0;
7645   char mapping[4096], temp[256];
7646   int font_name = MENU_SETUP_FONT_TITLE;
7647   int font_info = MENU_SETUP_FONT_TEXT;
7648   int spacing_name = menu.line_spacing_setup[SETUP_MODE_INPUT];
7649   int spacing_line = menu.line_spacing_setup[SETUP_MODE_INPUT];
7650   int spacing_para = menu.paragraph_spacing_setup[SETUP_MODE_INPUT];
7651   int ystep_name = getMenuTextStep(spacing_name, font_name);
7652   int ystep_line = getMenuTextStep(spacing_line, font_info);
7653   int ystep_para = getMenuTextStep(spacing_para, font_info);
7654   int i, j;
7655
7656   struct
7657   {
7658     int x, y;
7659     int marker;
7660     char *field;
7661     int axis, button, hat, hat_value;
7662     char mapping[4096];
7663   }
7664   *step, *prev_step, steps[] =
7665   {
7666     { 356, 155, MARKER_BUTTON, "a",             },
7667     { 396, 122, MARKER_BUTTON, "b",             },
7668     { 320, 125, MARKER_BUTTON, "x",             },
7669     { 358,  95, MARKER_BUTTON, "y",             },
7670     { 162, 125, MARKER_BUTTON, "back",          },
7671     { 216, 125, MARKER_BUTTON, "guide",         },
7672     { 271, 125, MARKER_BUTTON, "start",         },
7673     { 110, 200, MARKER_BUTTON, "dpleft",        },
7674     { 146, 228, MARKER_BUTTON, "dpdown",        },
7675     { 178, 200, MARKER_BUTTON, "dpright",       },
7676     { 146, 172, MARKER_BUTTON, "dpup",          },
7677     {  50,  40, MARKER_BUTTON, "leftshoulder",  },
7678     {  88, -10, MARKER_AXIS_Y, "lefttrigger",   },
7679     { 382,  40, MARKER_BUTTON, "rightshoulder", },
7680     { 346, -10, MARKER_AXIS_Y, "righttrigger",  },
7681     {  73, 141, MARKER_BUTTON, "leftstick",     },
7682     { 282, 210, MARKER_BUTTON, "rightstick",    },
7683     {  73, 141, MARKER_AXIS_X, "leftx",         },
7684     {  73, 141, MARKER_AXIS_Y, "lefty",         },
7685     { 282, 210, MARKER_AXIS_X, "rightx",        },
7686     { 282, 210, MARKER_AXIS_Y, "righty",        },
7687   };
7688
7689   unsigned int event_frame_delay = 0;
7690   unsigned int event_frame_delay_value = GAME_FRAME_DELAY;
7691
7692   ResetDelayCounter(&event_frame_delay);
7693
7694   if (!bitmaps_initialized)
7695   {
7696     controller = LoadCustomImage("joystick/controller.png");
7697     button     = LoadCustomImage("joystick/button.png");
7698     axis_x     = LoadCustomImage("joystick/axis_x.png");
7699     axis_y     = LoadCustomImage("joystick/axis_y.png");
7700
7701     bitmaps_initialized = TRUE;
7702   }
7703
7704   name = getFormattedJoystickName(SDL_JoystickName(joystick));
7705
7706 #if DEBUG_JOYSTICKS
7707   // print info about the joystick we are watching
7708   Debug("joystick", "watching joystick %d: (%s)",
7709         SDL_JoystickInstanceID(joystick), name);
7710   Debug("joystick", "joystick has %d axes, %d hats, %d balls, and %d buttons",
7711         SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
7712         SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
7713 #endif
7714
7715   // initialize mapping with GUID and name
7716   SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), temp, sizeof(temp));
7717
7718   snprintf(mapping, sizeof(mapping), "%s,%s,platform:%s,",
7719            temp, name, SDL_GetPlatform());
7720
7721   // loop through all steps (buttons and axes), getting joystick events
7722   for (i = 0; i < SDL_arraysize(steps) && !done;)
7723   {
7724     Bitmap *marker = button;    // initialize with reliable default value
7725
7726     step = &steps[i];
7727     strcpy(step->mapping, mapping);
7728     step->axis = -1;
7729     step->button = -1;
7730     step->hat = -1;
7731     step->hat_value = -1;
7732
7733     marker = (step->marker == MARKER_BUTTON ? button :
7734               step->marker == MARKER_AXIS_X ? axis_x :
7735               step->marker == MARKER_AXIS_Y ? axis_y : marker);
7736
7737     next = FALSE;
7738
7739     while (!done && !next)
7740     {
7741       alpha += alpha_step * (int)(SDL_GetTicks() - alpha_ticks) / 5;
7742       alpha_ticks = SDL_GetTicks();
7743
7744       if (alpha >= 255)
7745       {
7746         alpha = 255;
7747         alpha_step = -1;
7748       }
7749       else if (alpha < 128)
7750       {
7751         alpha = 127;
7752         alpha_step = 1;
7753       }
7754
7755       int controller_x = SX + (SXSIZE - controller->width) / 2;
7756       int controller_y = SY + ystep_line;
7757
7758       int marker_x = controller_x + step->x;
7759       int marker_y = controller_y + step->y;
7760
7761       int ystart1 = mSY - 2 * SY + controller_y + controller->height;
7762       int ystart2 = ystart1 + ystep_name + ystep_line;
7763
7764       ClearField();
7765
7766       DrawTextSCentered(ystart1, font_name, name);
7767
7768       DrawTextSCentered(ystart2, font_info,
7769                         "Press buttons and move axes on");
7770       ystart2 += ystep_line;
7771       DrawTextSCentered(ystart2, font_info,
7772                         "your controller when indicated.");
7773       ystart2 += ystep_line;
7774       DrawTextSCentered(ystart2, font_info,
7775                         "(Your controller may look different.)");
7776       ystart2 += ystep_para;
7777
7778 #if defined(PLATFORM_ANDROID)
7779       DrawTextSCentered(ystart2, font_info,
7780                         "To correct a mistake,");
7781       ystart2 += ystep_line;
7782       DrawTextSCentered(ystart2, font_info,
7783                         "press the 'back' button.");
7784       ystart2 += ystep_line;
7785       DrawTextSCentered(ystart2, font_info,
7786                         "To skip a button or axis,");
7787       ystart2 += ystep_line;
7788       DrawTextSCentered(ystart2, font_info,
7789                         "press the 'menu' button.");
7790 #else
7791       DrawTextSCentered(ystart2, font_info,
7792                         "To correct a mistake,");
7793       ystart2 += ystep_line;
7794       DrawTextSCentered(ystart2, font_info,
7795                         "press the 'backspace' key.");
7796       ystart2 += ystep_line;
7797       DrawTextSCentered(ystart2, font_info,
7798                         "To skip a button or axis,");
7799       ystart2 += ystep_line;
7800       DrawTextSCentered(ystart2, font_info,
7801                         "press the 'return' key.");
7802       ystart2 += ystep_line;
7803       DrawTextSCentered(ystart2, font_info,
7804                         "To exit, press the 'escape' key.");
7805 #endif
7806
7807       BlitBitmapMasked(controller, drawto, 0, 0,
7808                        controller->width, controller->height,
7809                        controller_x, controller_y);
7810
7811       SDL_SetSurfaceBlendMode(marker->surface_masked, SDL_BLENDMODE_BLEND);
7812       SDL_SetSurfaceAlphaMod(marker->surface_masked, alpha);
7813
7814       BlitBitmapMasked(marker, drawto, 0, 0,
7815                        marker->width, marker->height,
7816                        marker_x, marker_y);
7817
7818       if (!screen_initialized)
7819         FadeIn(REDRAW_FIELD);
7820       else
7821         BackToFront();
7822
7823       screen_initialized = TRUE;
7824
7825       while (NextValidEvent(&event))
7826       {
7827         switch (event.type)
7828         {
7829           case SDL_JOYAXISMOTION:
7830             if (event.jaxis.value > 20000 ||
7831                 event.jaxis.value < -20000)
7832             {
7833               for (j = 0; j < i; j++)
7834                 if (steps[j].axis == event.jaxis.axis)
7835                   break;
7836
7837               if (j == i)
7838               {
7839                 if (step->marker != MARKER_AXIS_X &&
7840                     step->marker != MARKER_AXIS_Y)
7841                   break;
7842
7843                 step->axis = event.jaxis.axis;
7844                 strcat(mapping, step->field);
7845                 snprintf(temp, sizeof(temp), ":a%u,", event.jaxis.axis);
7846                 strcat(mapping, temp);
7847                 i++;
7848                 next = TRUE;
7849               }
7850             }
7851
7852             break;
7853
7854           case SDL_JOYHATMOTION:
7855             // ignore centering; we're probably just coming back
7856             // to the center from the previous item we set
7857             if (event.jhat.value == SDL_HAT_CENTERED)
7858               break;
7859
7860             for (j = 0; j < i; j++)
7861               if (steps[j].hat == event.jhat.hat &&
7862                   steps[j].hat_value == event.jhat.value)
7863                 break;
7864
7865             if (j == i)
7866             {
7867               step->hat = event.jhat.hat;
7868               step->hat_value = event.jhat.value;
7869               strcat(mapping, step->field);
7870               snprintf(temp, sizeof(temp), ":h%u.%u,",
7871                        event.jhat.hat, event.jhat.value );
7872               strcat(mapping, temp);
7873               i++;
7874               next = TRUE;
7875             }
7876
7877             break;
7878
7879           case SDL_JOYBALLMOTION:
7880             break;
7881
7882           case SDL_JOYBUTTONUP:
7883             for (j = 0; j < i; j++)
7884               if (steps[j].button == event.jbutton.button)
7885                 break;
7886
7887             if (j == i)
7888             {
7889               step->button = event.jbutton.button;
7890               strcat(mapping, step->field);
7891               snprintf(temp, sizeof(temp), ":b%u,", event.jbutton.button);
7892               strcat(mapping, temp);
7893               i++;
7894               next = TRUE;
7895             }
7896
7897             break;
7898
7899           case SDL_FINGERDOWN:
7900           case SDL_MOUSEBUTTONDOWN:
7901             // skip this step
7902             i++;
7903             next = TRUE;
7904
7905             break;
7906
7907           case SDL_KEYDOWN:
7908             if (event.key.keysym.sym == KSYM_BackSpace ||
7909                 event.key.keysym.sym == KSYM_Back)
7910             {
7911               if (i == 0)
7912               {
7913                 // leave screen
7914                 success = FALSE;
7915                 done = TRUE;
7916
7917                 break;
7918               }
7919
7920               // undo this step
7921               prev_step = &steps[i - 1];
7922               strcpy(mapping, prev_step->mapping);
7923               i--;
7924               next = TRUE;
7925
7926               break;
7927             }
7928
7929             if (event.key.keysym.sym == KSYM_space ||
7930                 event.key.keysym.sym == KSYM_Return ||
7931                 event.key.keysym.sym == KSYM_Menu)
7932             {
7933               // skip this step
7934               i++;
7935               next = TRUE;
7936
7937               break;
7938             }
7939
7940             if (event.key.keysym.sym == KSYM_Escape)
7941             {
7942               // leave screen
7943               success = FALSE;
7944               done = TRUE;
7945             }
7946
7947             break;
7948
7949           case SDL_QUIT:
7950             program.exit_function(0);
7951             break;
7952
7953           default:
7954             break;
7955         }
7956
7957         // do not handle events for longer than standard frame delay period
7958         if (DelayReached(&event_frame_delay, event_frame_delay_value))
7959           break;
7960       }
7961     }
7962   }
7963
7964   if (success)
7965   {
7966 #if DEBUG_JOYSTICKS
7967     Debug("joystick", "New game controller mapping:\n\n%s\n\n", mapping);
7968 #endif
7969
7970     // activate mapping for this game
7971     SDL_GameControllerAddMapping(mapping);
7972
7973     // save mapping to personal mappings
7974     SaveSetup_AddGameControllerMapping(mapping);
7975   }
7976
7977   // wait until the last pending event was removed from event queue
7978   while (NextValidEvent(&event));
7979
7980   return success;
7981 }
7982
7983 static int ConfigureJoystickMain(int player_nr)
7984 {
7985   char *device_name = setup.input[player_nr].joy.device_name;
7986   int joystick_nr = getJoystickNrFromDeviceName(device_name);
7987   boolean joystick_active = CheckJoystickOpened(joystick_nr);
7988   int success = FALSE;
7989   int i;
7990
7991   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
7992     return JOYSTICK_NOT_AVAILABLE;
7993
7994   if (!joystick_active || !setup.input[player_nr].use_joystick)
7995     return JOYSTICK_NOT_AVAILABLE;
7996
7997   FadeSetEnterMenu();
7998   FadeOut(REDRAW_FIELD);
7999
8000   // close all joystick devices (potentially opened as game controllers)
8001   for (i = 0; i < SDL_NumJoysticks(); i++)
8002     SDLCloseJoystick(i);
8003
8004   // open joystick device as plain joystick to configure as game controller
8005   SDL_Joystick *joystick = SDL_JoystickOpen(joystick_nr);
8006
8007   // as the joystick was successfully opened before, this should not happen
8008   if (joystick == NULL)
8009     return FALSE;
8010
8011   // create new game controller mapping (buttons and axes) for joystick device
8012   success = ConfigureJoystickMapButtonsAndAxes(joystick);
8013
8014   // close joystick (and maybe re-open as configured game controller later)
8015   SDL_JoystickClose(joystick);
8016
8017   // re-open all joystick devices (potentially as game controllers)
8018   for (i = 0; i < SDL_NumJoysticks(); i++)
8019     SDLOpenJoystick(i);
8020
8021   // clear all joystick input actions for all joystick devices
8022   SDLClearJoystickState();
8023
8024   return (success ? JOYSTICK_CONFIGURED : JOYSTICK_NOT_CONFIGURED);
8025 }
8026
8027 void ConfigureJoystick(int player_nr)
8028 {
8029   boolean state = ConfigureJoystickMain(player_nr);
8030
8031   if (state != JOYSTICK_NOT_CONFIGURED)
8032   {
8033     boolean success = (state == JOYSTICK_CONFIGURED);
8034     char message1[MAX_OUTPUT_LINESIZE + 1];
8035     char *message2 = (success ? "configured!" : "not available!");
8036     char *device_name = setup.input[player_nr].joy.device_name;
8037     int nr = getJoystickNrFromDeviceName(device_name) + 1;
8038     int font_nr = FONT_TITLE_1;
8039     int font_height = getFontHeight(font_nr);
8040     int ypos1 = SYSIZE / 2 - font_height * 2;
8041     int ypos2 = SYSIZE / 2 - font_height * 1;
8042     unsigned int wait_frame_delay = 0;
8043     unsigned int wait_frame_delay_value = 2000;
8044
8045     ResetDelayCounter(&wait_frame_delay);
8046
8047     ClearField();
8048
8049     sprintf(message1, "Joystick %d", nr);
8050
8051     DrawTextSCentered(ypos1, font_nr, message1);
8052     DrawTextSCentered(ypos2, font_nr, message2);
8053
8054     while (!DelayReached(&wait_frame_delay, wait_frame_delay_value))
8055       BackToFront();
8056
8057     ClearEventQueue();
8058   }
8059
8060   DrawSetupScreen_Input();
8061 }
8062
8063 static void MapScreenMenuGadgets_OverlayTouchButtons(int y)
8064 {
8065   if (y < video.screen_height / 3)
8066   {
8067     // remap touch gadgets to access upper part of the screen
8068     UnmapScreenMenuGadgets(SCREEN_MASK_TOUCH);
8069     MapScreenMenuGadgets(SCREEN_MASK_TOUCH2);
8070   }
8071   else if (y > 2 * video.screen_height / 3)
8072   {
8073     // remap touch gadgets to access lower part of the screen
8074     MapScreenMenuGadgets(SCREEN_MASK_TOUCH);
8075     UnmapScreenMenuGadgets(SCREEN_MASK_TOUCH2);
8076   }
8077 }
8078
8079 static boolean ConfigureVirtualButtonsMain(void)
8080 {
8081   static char *customize_step_text[] =
8082   {
8083     "Move Left",
8084     "Move Right",
8085     "Move Up",
8086     "Move Down",
8087     "Snap Field",
8088     "Drop Element"
8089   };
8090   char grid_button[] =
8091   {
8092     CHAR_GRID_BUTTON_LEFT,
8093     CHAR_GRID_BUTTON_RIGHT,
8094     CHAR_GRID_BUTTON_UP,
8095     CHAR_GRID_BUTTON_DOWN,
8096     CHAR_GRID_BUTTON_SNAP,
8097     CHAR_GRID_BUTTON_DROP
8098   };
8099   enum
8100   {
8101     ACTION_NONE,
8102     ACTION_ESCAPE,
8103     ACTION_BACK,
8104     ACTION_NEXT
8105   };
8106   int font_nr = FONT_INPUT_1_ACTIVE;
8107   int font_height = getFontHeight(font_nr);
8108   int ypos1 = SYSIZE / 2 - font_height * 2;
8109   int ypos2 = SYSIZE / 2 - font_height * 1;
8110   boolean success = FALSE;
8111   boolean finished = FALSE;
8112   int step_nr = 0;
8113   char grid_button_draw = CHAR_GRID_BUTTON_NONE;
8114   char grid_button_old[MAX_GRID_XSIZE][MAX_GRID_YSIZE];
8115   char grid_button_tmp[MAX_GRID_XSIZE][MAX_GRID_YSIZE];
8116   boolean set_grid_button = FALSE;
8117   int nr = GRID_ACTIVE_NR();
8118   int x, y;
8119
8120   for (x = 0; x < MAX_GRID_XSIZE; x++)
8121     for (y = 0; y < MAX_GRID_YSIZE; y++)
8122       grid_button_old[x][y] = grid_button_tmp[x][y] = overlay.grid_button[x][y];
8123
8124   overlay.grid_button_highlight = grid_button[step_nr];
8125
8126   UnmapAllGadgets();
8127
8128   FadeSetEnterMenu();
8129   FadeOut(REDRAW_FIELD);
8130
8131   ClearField();
8132
8133   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Virtual Buttons");
8134   DrawTextSCentered(ypos1, font_nr, "Select tiles to");
8135   DrawTextSCentered(ypos2, font_nr, customize_step_text[step_nr]);
8136
8137   FadeIn(REDRAW_FIELD);
8138
8139   SetOverlayShowGrid(TRUE);
8140
8141   // map gadgets for setup touch buttons menu screen
8142   MapScreenMenuGadgets(SCREEN_MASK_TOUCH);
8143
8144   while (!finished)
8145   {
8146     Event event;
8147
8148     while (NextValidEvent(&event))
8149     {
8150       int action = ACTION_NONE;
8151
8152       // ---------- handle events and set the resulting action ----------
8153
8154       switch (event.type)
8155       {
8156         case EVENT_USER:
8157           {
8158             UserEvent *user = (UserEvent *)&event;
8159             int id = user->value1;
8160
8161             action = (id == SCREEN_CTRL_ID_TOUCH_PREV_PAGE ||
8162                       id == SCREEN_CTRL_ID_TOUCH_PREV_PAGE2 ? ACTION_BACK :
8163                       id == SCREEN_CTRL_ID_TOUCH_NEXT_PAGE ||
8164                       id == SCREEN_CTRL_ID_TOUCH_NEXT_PAGE2 ? ACTION_NEXT :
8165                       ACTION_NONE);
8166           }
8167           break;
8168
8169         case EVENT_KEYPRESS:
8170           {
8171             Key key = GetEventKey((KeyEvent *)&event, FALSE);
8172
8173             action = (key == KSYM_Escape ?      ACTION_ESCAPE :
8174                       key == KSYM_BackSpace ||
8175                       key == KSYM_Back ?        ACTION_BACK :
8176                       key == KSYM_Return ||
8177                       key == KSYM_Menu ||
8178                       key == KSYM_space ?       ACTION_NEXT :
8179                       ACTION_NONE);
8180           }
8181           break;
8182
8183         case EVENT_KEYRELEASE:
8184           key_joystick_mapping = 0;
8185           break;
8186
8187         case EVENT_BUTTONPRESS:
8188         case EVENT_BUTTONRELEASE:
8189           {
8190             ButtonEvent *button = (ButtonEvent *)&event;
8191
8192             motion_status = FALSE;
8193
8194             if (button->type == EVENT_BUTTONPRESS)
8195               button_status = button->button;
8196             else
8197               button_status = MB_RELEASED;
8198
8199             if (HandleGadgets(button->x, button->y, button_status))
8200             {
8201               // do not handle this button event anymore
8202               break;
8203             }
8204
8205             button->x += video.screen_xoffset;
8206             button->y += video.screen_yoffset;
8207
8208             x = button->x * overlay.grid_xsize / video.screen_width;
8209             y = button->y * overlay.grid_ysize / video.screen_height;
8210
8211             if (button->type == EVENT_BUTTONPRESS)
8212             {
8213               grid_button_draw =
8214                 (overlay.grid_button[x][y] != grid_button[step_nr] ?
8215                  grid_button[step_nr] : CHAR_GRID_BUTTON_NONE);
8216
8217               set_grid_button = TRUE;
8218             }
8219
8220             MapScreenMenuGadgets_OverlayTouchButtons(button->y);
8221           }
8222           break;
8223
8224         case EVENT_MOTIONNOTIFY:
8225           {
8226             MotionEvent *motion = (MotionEvent *)&event;
8227
8228             motion_status = TRUE;
8229
8230             if (HandleGadgets(motion->x, motion->y, button_status))
8231             {
8232               // do not handle this button event anymore
8233               break;
8234             }
8235
8236             motion->x += video.screen_xoffset;
8237             motion->y += video.screen_yoffset;
8238
8239             x = motion->x * overlay.grid_xsize / video.screen_width;
8240             y = motion->y * overlay.grid_ysize / video.screen_height;
8241
8242             set_grid_button = TRUE;
8243
8244             MapScreenMenuGadgets_OverlayTouchButtons(motion->y);
8245           }
8246           break;
8247
8248         case SDL_WINDOWEVENT:
8249           HandleWindowEvent((WindowEvent *) &event);
8250
8251           // check if device has been rotated
8252           if (nr != GRID_ACTIVE_NR())
8253           {
8254             nr = GRID_ACTIVE_NR();
8255
8256             for (x = 0; x < MAX_GRID_XSIZE; x++)
8257               for (y = 0; y < MAX_GRID_YSIZE; y++)
8258                 grid_button_old[x][y] = grid_button_tmp[x][y] =
8259                   overlay.grid_button[x][y];
8260           }
8261
8262           break;
8263
8264         case SDL_APP_WILLENTERBACKGROUND:
8265         case SDL_APP_DIDENTERBACKGROUND:
8266         case SDL_APP_WILLENTERFOREGROUND:
8267         case SDL_APP_DIDENTERFOREGROUND:
8268           HandlePauseResumeEvent((PauseResumeEvent *) &event);
8269           break;
8270
8271         default:
8272           HandleOtherEvents(&event);
8273           break;
8274       }
8275
8276       // ---------- perform action set by handling events ----------
8277
8278       if (action == ACTION_ESCAPE)
8279       {
8280         // abort and restore the old key bindings
8281
8282         for (x = 0; x < MAX_GRID_XSIZE; x++)
8283           for (y = 0; y < MAX_GRID_YSIZE; y++)
8284             overlay.grid_button[x][y] = grid_button_old[x][y];
8285
8286         FadeSkipNextFadeIn();
8287
8288         finished = TRUE;
8289       }
8290       else if (action == ACTION_BACK)
8291       {
8292         // keep the configured key bindings and go to previous page
8293
8294         step_nr--;
8295
8296         if (step_nr < 0)
8297         {
8298           FadeSkipNextFadeIn();
8299
8300           finished = TRUE;
8301         }
8302       }
8303       else if (action == ACTION_NEXT)
8304       {
8305         // keep the configured key bindings and go to next page
8306
8307         step_nr++;
8308
8309         // all virtual buttons configured
8310         if (step_nr == 6)
8311         {
8312           finished = TRUE;
8313           success = TRUE;
8314         }
8315       }
8316
8317       if (action != ACTION_NONE && !finished)
8318       {
8319         for (x = 0; x < MAX_GRID_XSIZE; x++)
8320           for (y = 0; y < MAX_GRID_YSIZE; y++)
8321             grid_button_tmp[x][y] = overlay.grid_button[x][y];
8322
8323         overlay.grid_button_highlight = grid_button[step_nr];
8324
8325         // configure next virtual button
8326
8327         ClearField();
8328
8329         DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Virtual Buttons");
8330         DrawTextSCentered(ypos1, font_nr, "Select tiles to");
8331         DrawTextSCentered(ypos2, font_nr, customize_step_text[step_nr]);
8332       }
8333
8334       if (set_grid_button)
8335       {
8336         overlay.grid_button[x][y] =
8337           (grid_button_draw != CHAR_GRID_BUTTON_NONE ? grid_button_draw :
8338            grid_button_tmp[x][y] == grid_button[step_nr] ? CHAR_GRID_BUTTON_NONE :
8339            grid_button_tmp[x][y]);
8340
8341         set_grid_button = FALSE;
8342       }
8343     }
8344
8345     BackToFront();
8346   }
8347
8348   for (x = 0; x < MAX_GRID_XSIZE; x++)
8349     for (y = 0; y < MAX_GRID_YSIZE; y++)
8350       setup.touch.grid_button[nr][x][y] = overlay.grid_button[x][y];
8351
8352   overlay.grid_button_highlight = CHAR_GRID_BUTTON_NONE;
8353
8354   SetOverlayShowGrid(FALSE);
8355
8356   return success;
8357 }
8358
8359 void ConfigureVirtualButtons(void)
8360 {
8361   boolean success = ConfigureVirtualButtonsMain();
8362
8363   UnmapScreenMenuGadgets(SCREEN_MASK_TOUCH |
8364                          SCREEN_MASK_TOUCH2);
8365
8366   if (success)
8367   {
8368     int font_nr = FONT_TITLE_1;
8369     int font_height = getFontHeight(font_nr);
8370     int ypos1 = SYSIZE / 2 - font_height * 2;
8371     int ypos2 = SYSIZE / 2 - font_height * 1;
8372     unsigned int wait_frame_delay = 0;
8373     unsigned int wait_frame_delay_value = 2000;
8374
8375     ResetDelayCounter(&wait_frame_delay);
8376
8377     ClearField();
8378
8379     DrawTextSCentered(ypos1, font_nr, "Virtual buttons");
8380     DrawTextSCentered(ypos2, font_nr, "configured!");
8381
8382     while (!DelayReached(&wait_frame_delay, wait_frame_delay_value))
8383       BackToFront();
8384
8385     ClearEventQueue();
8386   }
8387 }
8388
8389 void DrawSetupScreen(void)
8390 {
8391   align_xoffset = 0;
8392   align_yoffset = 0;
8393
8394   if (setup_mode == SETUP_MODE_INPUT)
8395     DrawSetupScreen_Input();
8396   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
8397     DrawChooseTree(&game_speed_current);
8398   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
8399     DrawChooseTree(&scroll_delay_current);
8400   else if (setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
8401     DrawChooseTree(&snapshot_mode_current);
8402   else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
8403     DrawChooseTree(&window_size_current);
8404   else if (setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
8405     DrawChooseTree(&scaling_type_current);
8406   else if (setup_mode == SETUP_MODE_CHOOSE_RENDERING)
8407     DrawChooseTree(&rendering_mode_current);
8408   else if (setup_mode == SETUP_MODE_CHOOSE_VSYNC)
8409     DrawChooseTree(&vsync_mode_current);
8410   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
8411     DrawChooseTree(&artwork.gfx_current);
8412   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
8413     DrawChooseTree(&artwork.snd_current);
8414   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
8415     DrawChooseTree(&artwork.mus_current);
8416   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE)
8417     DrawChooseTree(&volume_simple_current);
8418   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS)
8419     DrawChooseTree(&volume_loops_current);
8420   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
8421     DrawChooseTree(&volume_music_current);
8422   else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL)
8423     DrawChooseTree(&touch_control_current);
8424   else if (setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE)
8425     DrawChooseTree(&move_distance_current);
8426   else if (setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
8427     DrawChooseTree(&drop_distance_current);
8428   else if (setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY)
8429     DrawChooseTree(&transparency_current);
8430   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_0)
8431     DrawChooseTree(&grid_size_current[0][0]);
8432   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_0)
8433     DrawChooseTree(&grid_size_current[0][1]);
8434   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_1)
8435     DrawChooseTree(&grid_size_current[1][0]);
8436   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_1)
8437     DrawChooseTree(&grid_size_current[1][1]);
8438   else
8439     DrawSetupScreen_Generic();
8440
8441   PlayMenuSoundsAndMusic();
8442 }
8443
8444 void RedrawSetupScreenAfterFullscreenToggle(void)
8445 {
8446   if (setup_mode == SETUP_MODE_GRAPHICS ||
8447       setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
8448   {
8449     // update list selection from "setup.window_scaling_percent"
8450     execSetupGraphics_setWindowSizes(TRUE);
8451
8452     DrawSetupScreen();
8453   }
8454 }
8455
8456 void RedrawSetupScreenAfterScreenRotation(int nr)
8457 {
8458   int x, y;
8459
8460   if (setup_mode == SETUP_MODE_TOUCH)
8461   {
8462     // update virtual button settings (depending on screen orientation)
8463     DrawSetupScreen();
8464   }
8465   else if (setup_mode == SETUP_MODE_CONFIG_VIRT_BUTTONS)
8466   {
8467     // save already configured virtual buttons
8468     for (x = 0; x < MAX_GRID_XSIZE; x++)
8469       for (y = 0; y < MAX_GRID_YSIZE; y++)
8470         setup.touch.grid_button[nr][x][y] = overlay.grid_button[x][y];
8471   }
8472 }
8473
8474 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
8475 {
8476   if (setup_mode == SETUP_MODE_INPUT)
8477     HandleSetupScreen_Input(mx, my, dx, dy, button);
8478   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
8479     HandleChooseTree(mx, my, dx, dy, button, &game_speed_current);
8480   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
8481     HandleChooseTree(mx, my, dx, dy, button, &scroll_delay_current);
8482   else if (setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
8483     HandleChooseTree(mx, my, dx, dy, button, &snapshot_mode_current);
8484   else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
8485     HandleChooseTree(mx, my, dx, dy, button, &window_size_current);
8486   else if (setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
8487     HandleChooseTree(mx, my, dx, dy, button, &scaling_type_current);
8488   else if (setup_mode == SETUP_MODE_CHOOSE_RENDERING)
8489     HandleChooseTree(mx, my, dx, dy, button, &rendering_mode_current);
8490   else if (setup_mode == SETUP_MODE_CHOOSE_VSYNC)
8491     HandleChooseTree(mx, my, dx, dy, button, &vsync_mode_current);
8492   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
8493     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
8494   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
8495     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
8496   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
8497     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
8498   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE)
8499     HandleChooseTree(mx, my, dx, dy, button, &volume_simple_current);
8500   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS)
8501     HandleChooseTree(mx, my, dx, dy, button, &volume_loops_current);
8502   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
8503     HandleChooseTree(mx, my, dx, dy, button, &volume_music_current);
8504   else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL)
8505     HandleChooseTree(mx, my, dx, dy, button, &touch_control_current);
8506   else if (setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE)
8507     HandleChooseTree(mx, my, dx, dy, button, &move_distance_current);
8508   else if (setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
8509     HandleChooseTree(mx, my, dx, dy, button, &drop_distance_current);
8510   else if (setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY)
8511     HandleChooseTree(mx, my, dx, dy, button, &transparency_current);
8512   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_0)
8513     HandleChooseTree(mx, my, dx, dy, button, &grid_size_current[0][0]);
8514   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_0)
8515     HandleChooseTree(mx, my, dx, dy, button, &grid_size_current[0][1]);
8516   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_1)
8517     HandleChooseTree(mx, my, dx, dy, button, &grid_size_current[1][0]);
8518   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_1)
8519     HandleChooseTree(mx, my, dx, dy, button, &grid_size_current[1][1]);
8520   else
8521     HandleSetupScreen_Generic(mx, my, dx, dy, button);
8522 }
8523
8524 void HandleGameActions(void)
8525 {
8526   if (setup.ask_on_game_over)
8527     CheckGameOver();
8528
8529   if (game.restart_game_message != NULL)
8530   {
8531     RequestRestartGame(game.restart_game_message);
8532
8533     return;
8534   }
8535
8536   if (game_status != GAME_MODE_PLAYING)
8537     return;
8538
8539   GameActions();        // main game loop
8540
8541   if (tape.auto_play && !tape.playing)
8542     AutoPlayTapes();    // continue automatically playing next tape
8543 }
8544
8545
8546 // ---------- new screen button stuff --------------------------------------
8547
8548 static struct
8549 {
8550   int gfx_unpressed, gfx_pressed;
8551   struct MenuPosInfo *pos;
8552   boolean *check_value;
8553   int gadget_id;
8554   int screen_mask;
8555   unsigned int event_mask;
8556   boolean is_touch_button;
8557   char *infotext;
8558 } menubutton_info[NUM_SCREEN_MENUBUTTONS] =
8559 {
8560   {
8561     IMG_MENU_BUTTON_PREV_LEVEL, IMG_MENU_BUTTON_PREV_LEVEL_ACTIVE,
8562     &menu.main.button.prev_level, NULL,
8563     SCREEN_CTRL_ID_PREV_LEVEL,
8564     SCREEN_MASK_MAIN,
8565     GD_EVENT_PRESSED | GD_EVENT_REPEATED,
8566     FALSE, "previous level"
8567   },
8568   {
8569     IMG_MENU_BUTTON_NEXT_LEVEL, IMG_MENU_BUTTON_NEXT_LEVEL_ACTIVE,
8570     &menu.main.button.next_level, NULL,
8571     SCREEN_CTRL_ID_NEXT_LEVEL,
8572     SCREEN_MASK_MAIN,
8573     GD_EVENT_PRESSED | GD_EVENT_REPEATED,
8574     FALSE, "next level"
8575   },
8576   {
8577     IMG_MENU_BUTTON_FIRST_LEVEL, IMG_MENU_BUTTON_FIRST_LEVEL_ACTIVE,
8578     &menu.main.button.first_level, NULL,
8579     SCREEN_CTRL_ID_FIRST_LEVEL,
8580     SCREEN_MASK_MAIN,
8581     GD_EVENT_RELEASED,
8582     FALSE, "first level"
8583   },
8584   {
8585     IMG_MENU_BUTTON_LAST_LEVEL, IMG_MENU_BUTTON_LAST_LEVEL_ACTIVE,
8586     &menu.main.button.last_level, NULL,
8587     SCREEN_CTRL_ID_LAST_LEVEL,
8588     SCREEN_MASK_MAIN,
8589     GD_EVENT_RELEASED,
8590     FALSE, "last level"
8591   },
8592   {
8593     IMG_MENU_BUTTON_LEVEL_NUMBER, IMG_MENU_BUTTON_LEVEL_NUMBER_ACTIVE,
8594     &menu.main.button.level_number, NULL,
8595     SCREEN_CTRL_ID_LEVEL_NUMBER,
8596     SCREEN_MASK_MAIN,
8597     GD_EVENT_RELEASED,
8598     FALSE, "level number"
8599   },
8600   {
8601     IMG_MENU_BUTTON_LEFT, IMG_MENU_BUTTON_LEFT_ACTIVE,
8602     &menu.setup.button.prev_player, NULL,
8603     SCREEN_CTRL_ID_PREV_PLAYER,
8604     SCREEN_MASK_INPUT,
8605     GD_EVENT_PRESSED | GD_EVENT_REPEATED,
8606     FALSE, "previous player"
8607   },
8608   {
8609     IMG_MENU_BUTTON_RIGHT, IMG_MENU_BUTTON_RIGHT_ACTIVE,
8610     &menu.setup.button.next_player, NULL,
8611     SCREEN_CTRL_ID_NEXT_PLAYER,
8612     SCREEN_MASK_INPUT,
8613     GD_EVENT_PRESSED | GD_EVENT_REPEATED,
8614     FALSE, "next player"
8615   },
8616   {
8617     IMG_MENU_BUTTON_INSERT_SOLUTION, IMG_MENU_BUTTON_INSERT_SOLUTION_ACTIVE,
8618     &menu.main.button.insert_solution, NULL,
8619     SCREEN_CTRL_ID_INSERT_SOLUTION,
8620     SCREEN_MASK_MAIN_HAS_SOLUTION,
8621     GD_EVENT_RELEASED,
8622     FALSE, "insert solution tape"
8623   },
8624   {
8625     IMG_MENU_BUTTON_PLAY_SOLUTION, IMG_MENU_BUTTON_PLAY_SOLUTION_ACTIVE,
8626     &menu.main.button.play_solution, NULL,
8627     SCREEN_CTRL_ID_PLAY_SOLUTION,
8628     SCREEN_MASK_MAIN_HAS_SOLUTION,
8629     GD_EVENT_RELEASED,
8630     FALSE, "play solution tape"
8631   },
8632   {
8633     IMG_MENU_BUTTON_SWITCH_ECS_AGA, IMG_MENU_BUTTON_SWITCH_ECS_AGA_ACTIVE,
8634     &menu.main.button.switch_ecs_aga, &setup.prefer_aga_graphics,
8635     SCREEN_CTRL_ID_SWITCH_ECS_AGA,
8636     SCREEN_MASK_MAIN,
8637     GD_EVENT_RELEASED | GD_EVENT_OFF_BORDERS,
8638     FALSE, "switch ECS/AGA chipset"
8639   },
8640   {
8641     IMG_MENU_BUTTON_TOUCH_BACK, IMG_MENU_BUTTON_TOUCH_BACK,
8642     &menu.setup.button.touch_back, NULL,
8643     SCREEN_CTRL_ID_TOUCH_PREV_PAGE,
8644     SCREEN_MASK_TOUCH,
8645     GD_EVENT_RELEASED,
8646     TRUE, "previous page"
8647   },
8648   {
8649     IMG_MENU_BUTTON_TOUCH_NEXT, IMG_MENU_BUTTON_TOUCH_NEXT,
8650     &menu.setup.button.touch_next, NULL,
8651     SCREEN_CTRL_ID_TOUCH_NEXT_PAGE,
8652     SCREEN_MASK_TOUCH,
8653     GD_EVENT_RELEASED,
8654     TRUE, "next page"
8655   },
8656   {
8657     IMG_MENU_BUTTON_TOUCH_BACK2, IMG_MENU_BUTTON_TOUCH_BACK2,
8658     &menu.setup.button.touch_back2, NULL,
8659     SCREEN_CTRL_ID_TOUCH_PREV_PAGE2,
8660     SCREEN_MASK_TOUCH2,
8661     GD_EVENT_RELEASED,
8662     TRUE, "previous page"
8663   },
8664   {
8665     IMG_MENU_BUTTON_TOUCH_NEXT2, IMG_MENU_BUTTON_TOUCH_NEXT2,
8666     &menu.setup.button.touch_next2, NULL,
8667     SCREEN_CTRL_ID_TOUCH_NEXT_PAGE2,
8668     SCREEN_MASK_TOUCH2,
8669     GD_EVENT_RELEASED,
8670     TRUE, "next page"
8671   },
8672 };
8673
8674 static struct
8675 {
8676   int gfx_unpressed, gfx_pressed;
8677   int x, y;
8678   int gadget_id;
8679   char *infotext;
8680 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
8681 {
8682   {
8683     IMG_MENU_BUTTON_UP, IMG_MENU_BUTTON_UP_ACTIVE,
8684     -1, -1,     // these values are not constant, but can change at runtime
8685     SCREEN_CTRL_ID_SCROLL_UP,
8686     "scroll up"
8687   },
8688   {
8689     IMG_MENU_BUTTON_DOWN, IMG_MENU_BUTTON_DOWN_ACTIVE,
8690     -1, -1,     // these values are not constant, but can change at runtime
8691     SCREEN_CTRL_ID_SCROLL_DOWN,
8692     "scroll down"
8693   }
8694 };
8695
8696 static struct
8697 {
8698   int gfx_unpressed, gfx_pressed;
8699   int x, y;
8700   int width, height;
8701   int type;
8702   int gadget_id;
8703   char *infotext;
8704 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
8705 {
8706   {
8707     IMG_MENU_SCROLLBAR, IMG_MENU_SCROLLBAR_ACTIVE,
8708     -1, -1,     // these values are not constant, but can change at runtime
8709     -1, -1,     // these values are not constant, but can change at runtime
8710     GD_TYPE_SCROLLBAR_VERTICAL,
8711     SCREEN_CTRL_ID_SCROLL_VERTICAL,
8712     "scroll level series vertically"
8713   }
8714 };
8715
8716 static struct
8717 {
8718   int graphic;
8719   int gadget_id;
8720   int x, y;
8721   int size;
8722   char *value;
8723   char *infotext;
8724 } textinput_info[NUM_SCREEN_TEXTINPUT] =
8725 {
8726   {
8727     IMG_SETUP_INPUT_TEXT,
8728     SCREEN_CTRL_ID_NETWORK_SERVER,
8729     -1, -1,     // these values are not constant, but can change at runtime
8730     MAX_SETUP_TEXT_INPUT_LEN,
8731     network_server_hostname,
8732     "Network Server Hostname / IP"
8733   },
8734 };
8735
8736 static void CreateScreenMenubuttons(void)
8737 {
8738   struct GadgetInfo *gi;
8739   unsigned int event_mask;
8740   int i;
8741
8742   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
8743   {
8744     struct MenuPosInfo *pos = menubutton_info[i].pos;
8745     boolean is_touch_button = menubutton_info[i].is_touch_button;
8746     boolean is_check_button = menubutton_info[i].check_value != NULL;
8747     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
8748     int gfx_unpressed, gfx_pressed;
8749     int x, y, width, height;
8750     int gd_x1, gd_x2, gd_y1, gd_y2;
8751     int gd_x1a, gd_x2a, gd_y1a, gd_y2a;
8752     int id = menubutton_info[i].gadget_id;
8753     int type = GD_TYPE_NORMAL_BUTTON;
8754     boolean checked = FALSE;
8755
8756     event_mask = menubutton_info[i].event_mask;
8757
8758     x = (is_touch_button ? pos->x : mSX + GDI_ACTIVE_POS(pos->x));
8759     y = (is_touch_button ? pos->y : mSY + GDI_ACTIVE_POS(pos->y));
8760
8761     width  = graphic_info[menubutton_info[i].gfx_pressed].width;
8762     height = graphic_info[menubutton_info[i].gfx_pressed].height;
8763
8764     gfx_unpressed = menubutton_info[i].gfx_unpressed;
8765     gfx_pressed   = menubutton_info[i].gfx_pressed;
8766     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
8767     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
8768     gd_x1 = graphic_info[gfx_unpressed].src_x;
8769     gd_y1 = graphic_info[gfx_unpressed].src_y;
8770     gd_x2 = graphic_info[gfx_pressed].src_x;
8771     gd_y2 = graphic_info[gfx_pressed].src_y;
8772     gd_x1a = gd_x1;
8773     gd_y1a = gd_y1;
8774     gd_x2a = gd_x2;
8775     gd_y2a = gd_y2;
8776
8777     if (is_touch_button)
8778     {
8779       gd_x2 += graphic_info[gfx_pressed].pressed_xoffset;
8780       gd_y2 += graphic_info[gfx_pressed].pressed_yoffset;
8781     }
8782
8783     if (is_check_button)
8784     {
8785       gd_x1a += graphic_info[gfx_unpressed].active_xoffset;
8786       gd_y1a += graphic_info[gfx_unpressed].active_yoffset;
8787       gd_x2a += graphic_info[gfx_pressed].active_xoffset;
8788       gd_y2a += graphic_info[gfx_pressed].active_yoffset;
8789
8790       type = GD_TYPE_CHECK_BUTTON;
8791       checked = *menubutton_info[i].check_value;
8792     }
8793
8794     gi = CreateGadget(GDI_CUSTOM_ID, id,
8795                       GDI_CUSTOM_TYPE_ID, i,
8796                       GDI_IMAGE_ID, gfx_unpressed,
8797                       GDI_INFO_TEXT, menubutton_info[i].infotext,
8798                       GDI_X, x,
8799                       GDI_Y, y,
8800                       GDI_WIDTH, width,
8801                       GDI_HEIGHT, height,
8802                       GDI_TYPE, type,
8803                       GDI_STATE, GD_BUTTON_UNPRESSED,
8804                       GDI_CHECKED, checked,
8805                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
8806                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
8807                       GDI_ALT_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1a, gd_y1a,
8808                       GDI_ALT_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2a, gd_y2a,
8809                       GDI_DIRECT_DRAW, FALSE,
8810                       GDI_OVERLAY_TOUCH_BUTTON, is_touch_button,
8811                       GDI_EVENT_MASK, event_mask,
8812                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
8813                       GDI_END);
8814
8815     if (gi == NULL)
8816       Fail("cannot create gadget");
8817
8818     screen_gadget[id] = gi;
8819   }
8820 }
8821
8822 static void CreateScreenScrollbuttons(void)
8823 {
8824   struct GadgetInfo *gi;
8825   unsigned int event_mask;
8826   int i;
8827
8828   // these values are not constant, but can change at runtime
8829   scrollbutton_info[0].x = SC_SCROLL_UP_XPOS;
8830   scrollbutton_info[0].y = SC_SCROLL_UP_YPOS;
8831   scrollbutton_info[1].x = SC_SCROLL_DOWN_XPOS;
8832   scrollbutton_info[1].y = SC_SCROLL_DOWN_YPOS;
8833
8834   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
8835   {
8836     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
8837     int gfx_unpressed, gfx_pressed;
8838     int x, y, width, height;
8839     int gd_x1, gd_x2, gd_y1, gd_y2;
8840     int id = scrollbutton_info[i].gadget_id;
8841
8842     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
8843
8844     x = mSX + scrollbutton_info[i].x + menu.scrollbar_xoffset;
8845     y = mSY + scrollbutton_info[i].y;
8846     width = SC_SCROLLBUTTON_XSIZE;
8847     height = SC_SCROLLBUTTON_YSIZE;
8848
8849     // correct scrollbar position if placed outside menu (playfield) area
8850     if (x > SX + SC_SCROLL_UP_XPOS)
8851       x = SX + SC_SCROLL_UP_XPOS;
8852
8853     if (id == SCREEN_CTRL_ID_SCROLL_DOWN)
8854       y = mSY + (SC_SCROLL_VERTICAL_YPOS +
8855                  (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE);
8856
8857     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
8858     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
8859     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
8860     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
8861     gd_x1 = graphic_info[gfx_unpressed].src_x;
8862     gd_y1 = graphic_info[gfx_unpressed].src_y;
8863     gd_x2 = graphic_info[gfx_pressed].src_x;
8864     gd_y2 = graphic_info[gfx_pressed].src_y;
8865
8866     gi = CreateGadget(GDI_CUSTOM_ID, id,
8867                       GDI_CUSTOM_TYPE_ID, i,
8868                       GDI_IMAGE_ID, gfx_unpressed,
8869                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
8870                       GDI_X, x,
8871                       GDI_Y, y,
8872                       GDI_WIDTH, width,
8873                       GDI_HEIGHT, height,
8874                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
8875                       GDI_STATE, GD_BUTTON_UNPRESSED,
8876                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
8877                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
8878                       GDI_DIRECT_DRAW, FALSE,
8879                       GDI_EVENT_MASK, event_mask,
8880                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
8881                       GDI_END);
8882
8883     if (gi == NULL)
8884       Fail("cannot create gadget");
8885
8886     screen_gadget[id] = gi;
8887   }
8888 }
8889
8890 static void CreateScreenScrollbars(void)
8891 {
8892   int i;
8893
8894   // these values are not constant, but can change at runtime
8895   scrollbar_info[0].x = SC_SCROLL_VERTICAL_XPOS;
8896   scrollbar_info[0].y = SC_SCROLL_VERTICAL_YPOS;
8897   scrollbar_info[0].width  = SC_SCROLL_VERTICAL_XSIZE;
8898   scrollbar_info[0].height = SC_SCROLL_VERTICAL_YSIZE;
8899
8900   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
8901   {
8902     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
8903     int gfx_unpressed, gfx_pressed;
8904     int x, y, width, height;
8905     int gd_x1, gd_x2, gd_y1, gd_y2;
8906     struct GadgetInfo *gi;
8907     int items_max, items_visible, item_position;
8908     unsigned int event_mask;
8909     int num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
8910     int id = scrollbar_info[i].gadget_id;
8911
8912     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
8913
8914     x = mSX + scrollbar_info[i].x + menu.scrollbar_xoffset;
8915     y = mSY + scrollbar_info[i].y;
8916     width  = scrollbar_info[i].width;
8917     height = scrollbar_info[i].height;
8918
8919     // correct scrollbar position if placed outside menu (playfield) area
8920     if (x > SX + SC_SCROLL_VERTICAL_XPOS)
8921       x = SX + SC_SCROLL_VERTICAL_XPOS;
8922
8923     if (id == SCREEN_CTRL_ID_SCROLL_VERTICAL)
8924       height = (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE;
8925
8926     items_max = num_page_entries;
8927     items_visible = num_page_entries;
8928     item_position = 0;
8929
8930     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
8931     gfx_pressed   = scrollbar_info[i].gfx_pressed;
8932     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
8933     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
8934     gd_x1 = graphic_info[gfx_unpressed].src_x;
8935     gd_y1 = graphic_info[gfx_unpressed].src_y;
8936     gd_x2 = graphic_info[gfx_pressed].src_x;
8937     gd_y2 = graphic_info[gfx_pressed].src_y;
8938
8939     gi = CreateGadget(GDI_CUSTOM_ID, id,
8940                       GDI_CUSTOM_TYPE_ID, i,
8941                       GDI_IMAGE_ID, gfx_unpressed,
8942                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
8943                       GDI_X, x,
8944                       GDI_Y, y,
8945                       GDI_WIDTH, width,
8946                       GDI_HEIGHT, height,
8947                       GDI_TYPE, scrollbar_info[i].type,
8948                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
8949                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
8950                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
8951                       GDI_WHEEL_AREA_X, SX,
8952                       GDI_WHEEL_AREA_Y, SY,
8953                       GDI_WHEEL_AREA_WIDTH, SXSIZE,
8954                       GDI_WHEEL_AREA_HEIGHT, SYSIZE,
8955                       GDI_STATE, GD_BUTTON_UNPRESSED,
8956                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
8957                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
8958                       GDI_BORDER_SIZE, SC_BORDER_SIZE, SC_BORDER_SIZE,
8959                       GDI_DIRECT_DRAW, FALSE,
8960                       GDI_EVENT_MASK, event_mask,
8961                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
8962                       GDI_END);
8963
8964     if (gi == NULL)
8965       Fail("cannot create gadget");
8966
8967     screen_gadget[id] = gi;
8968   }
8969 }
8970
8971 static void CreateScreenTextInputGadgets(void)
8972 {
8973   int i;
8974
8975   for (i = 0; i < NUM_SCREEN_TEXTINPUT; i++)
8976   {
8977     int graphic = textinput_info[i].graphic;
8978     struct GraphicInfo *gd = &graphic_info[graphic];
8979     int gd_x1 = gd->src_x;
8980     int gd_y1 = gd->src_y;
8981     int gd_x2 = gd->src_x + gd->active_xoffset;
8982     int gd_y2 = gd->src_y + gd->active_yoffset;
8983     struct GadgetInfo *gi;
8984     unsigned int event_mask;
8985     int id = textinput_info[i].gadget_id;
8986     int x = textinput_info[i].x;
8987     int y = textinput_info[i].y;
8988
8989     event_mask = GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
8990
8991     gi = CreateGadget(GDI_CUSTOM_ID, id,
8992                       GDI_CUSTOM_TYPE_ID, i,
8993                       GDI_INFO_TEXT, textinput_info[i].infotext,
8994                       GDI_X, SX + x,
8995                       GDI_Y, SY + y,
8996                       GDI_TYPE, GD_TYPE_TEXT_INPUT_ALPHANUMERIC,
8997                       GDI_TEXT_VALUE, textinput_info[i].value,
8998                       GDI_TEXT_SIZE, textinput_info[i].size,
8999                       GDI_TEXT_FONT, getSetupValueFont(TYPE_STRING, NULL),
9000                       GDI_TEXT_FONT_ACTIVE, FONT_TEXT_1,
9001                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
9002                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
9003                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
9004                       GDI_DESIGN_WIDTH, gd->width,
9005                       GDI_EVENT_MASK, event_mask,
9006                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
9007                       GDI_CALLBACK_ACTION_ALWAYS, TRUE,
9008                       GDI_END);
9009
9010     if (gi == NULL)
9011       Fail("cannot create gadget");
9012
9013     screen_gadget[id] = gi;
9014   }
9015 }
9016
9017 void CreateScreenGadgets(void)
9018 {
9019   CreateScreenMenubuttons();
9020
9021   CreateScreenScrollbuttons();
9022   CreateScreenScrollbars();
9023
9024   CreateScreenTextInputGadgets();
9025 }
9026
9027 void FreeScreenGadgets(void)
9028 {
9029   int i;
9030
9031   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
9032     FreeGadget(screen_gadget[i]);
9033 }
9034
9035 static void MapScreenMenuGadgets(int screen_mask)
9036 {
9037   int i;
9038
9039   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
9040     if (screen_mask & menubutton_info[i].screen_mask)
9041       MapGadget(screen_gadget[menubutton_info[i].gadget_id]);
9042 }
9043
9044 static void UnmapScreenMenuGadgets(int screen_mask)
9045 {
9046   int i;
9047
9048   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
9049   {
9050     if (screen_mask & menubutton_info[i].screen_mask)
9051     {
9052       UnmapGadget(screen_gadget[menubutton_info[i].gadget_id]);
9053
9054       if (screen_mask & SCREEN_MASK_MAIN_HAS_SOLUTION)
9055         DrawBackground(screen_gadget[menubutton_info[i].gadget_id]->x,
9056                        screen_gadget[menubutton_info[i].gadget_id]->y,
9057                        screen_gadget[menubutton_info[i].gadget_id]->width,
9058                        screen_gadget[menubutton_info[i].gadget_id]->height);
9059     }
9060   }
9061 }
9062
9063 static void UpdateScreenMenuGadgets(int screen_mask, boolean map_gadgets)
9064 {
9065   if (map_gadgets)
9066     MapScreenMenuGadgets(screen_mask);
9067   else
9068     UnmapScreenMenuGadgets(screen_mask);
9069 }
9070
9071 static void MapScreenGadgets(int num_entries)
9072 {
9073   int i;
9074
9075   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
9076     return;
9077
9078   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
9079     MapGadget(screen_gadget[scrollbutton_info[i].gadget_id]);
9080
9081   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
9082     MapGadget(screen_gadget[scrollbar_info[i].gadget_id]);
9083 }
9084
9085 static void MapScreenTreeGadgets(TreeInfo *ti)
9086 {
9087   MapScreenGadgets(numTreeInfoInGroup(ti));
9088 }
9089
9090 static void HandleScreenGadgets(struct GadgetInfo *gi)
9091 {
9092   int id = gi->custom_id;
9093   int button = gi->event.button;
9094   int step = (button == MB_LEFTBUTTON   ? 1 :
9095               button == MB_MIDDLEBUTTON ? 5 :
9096               button == MB_RIGHTBUTTON  ? 10 : 1);
9097
9098   switch (id)
9099   {
9100     case SCREEN_CTRL_ID_PREV_LEVEL:
9101       HandleMainMenu_SelectLevel(step, -1, NO_DIRECT_LEVEL_SELECT);
9102       break;
9103
9104     case SCREEN_CTRL_ID_NEXT_LEVEL:
9105       HandleMainMenu_SelectLevel(step, +1, NO_DIRECT_LEVEL_SELECT);
9106       break;
9107
9108     case SCREEN_CTRL_ID_FIRST_LEVEL:
9109       HandleMainMenu_SelectLevel(MAX_LEVELS, -1, NO_DIRECT_LEVEL_SELECT);
9110       break;
9111
9112     case SCREEN_CTRL_ID_LAST_LEVEL:
9113       HandleMainMenu_SelectLevel(MAX_LEVELS, +1, NO_DIRECT_LEVEL_SELECT);
9114       break;
9115
9116     case SCREEN_CTRL_ID_LEVEL_NUMBER:
9117       CloseDoor(DOOR_CLOSE_2);
9118       SetGameStatus(GAME_MODE_LEVELNR);
9119       DrawChooseLevelNr();
9120       break;
9121
9122     case SCREEN_CTRL_ID_PREV_PLAYER:
9123       HandleSetupScreen_Input_Player(step, -1);
9124       break;
9125
9126     case SCREEN_CTRL_ID_NEXT_PLAYER:
9127       HandleSetupScreen_Input_Player(step, +1);
9128       break;
9129
9130     case SCREEN_CTRL_ID_INSERT_SOLUTION:
9131       InsertSolutionTape();
9132       break;
9133
9134     case SCREEN_CTRL_ID_PLAY_SOLUTION:
9135       PlaySolutionTape();
9136       break;
9137
9138     case SCREEN_CTRL_ID_SWITCH_ECS_AGA:
9139       setup.prefer_aga_graphics = !setup.prefer_aga_graphics;
9140       DrawMainMenu();
9141       break;
9142
9143     case SCREEN_CTRL_ID_TOUCH_PREV_PAGE:
9144     case SCREEN_CTRL_ID_TOUCH_NEXT_PAGE:
9145     case SCREEN_CTRL_ID_TOUCH_PREV_PAGE2:
9146     case SCREEN_CTRL_ID_TOUCH_NEXT_PAGE2:
9147       PushUserEvent(USEREVENT_GADGET_PRESSED, id, 0);
9148       break;
9149
9150     case SCREEN_CTRL_ID_SCROLL_UP:
9151       if (game_status == GAME_MODE_NAMES)
9152         HandleChoosePlayerName(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
9153       else if (game_status == GAME_MODE_LEVELS)
9154         HandleChooseLevelSet(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
9155       else if (game_status == GAME_MODE_LEVELNR)
9156         HandleChooseLevelNr(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
9157       else if (game_status == GAME_MODE_SETUP)
9158         HandleSetupScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
9159       else if (game_status == GAME_MODE_INFO)
9160         HandleInfoScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
9161       break;
9162
9163     case SCREEN_CTRL_ID_SCROLL_DOWN:
9164       if (game_status == GAME_MODE_NAMES)
9165         HandleChoosePlayerName(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
9166       else if (game_status == GAME_MODE_LEVELS)
9167         HandleChooseLevelSet(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
9168       else if (game_status == GAME_MODE_LEVELNR)
9169         HandleChooseLevelNr(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
9170       else if (game_status == GAME_MODE_SETUP)
9171         HandleSetupScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
9172       else if (game_status == GAME_MODE_INFO)
9173         HandleInfoScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
9174       break;
9175
9176     case SCREEN_CTRL_ID_SCROLL_VERTICAL:
9177       if (game_status == GAME_MODE_NAMES)
9178         HandleChoosePlayerName(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
9179       else if (game_status == GAME_MODE_LEVELS)
9180         HandleChooseLevelSet(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
9181       else if (game_status == GAME_MODE_LEVELNR)
9182         HandleChooseLevelNr(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
9183       else if (game_status == GAME_MODE_SETUP)
9184         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
9185       else if (game_status == GAME_MODE_INFO)
9186         HandleInfoScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
9187       break;
9188
9189     case SCREEN_CTRL_ID_NETWORK_SERVER:
9190     {
9191       if (!strEqual(gi->textinput.value, ""))
9192       {
9193         setString(&setup.network_server_hostname, gi->textinput.value);
9194
9195         network.server_host = setup.network_server_hostname;
9196       }
9197       else
9198       {
9199         setString(&setup.network_server_hostname, STR_NETWORK_AUTO_DETECT);
9200
9201         network.server_host = NULL;
9202       }
9203
9204       if (strEqual(network.server_host, STR_NETWORK_AUTO_DETECT))
9205         network.server_host = NULL;
9206
9207       execSetupGame_setNetworkServerText();
9208
9209       DrawSetupScreen();
9210
9211       break;
9212     }
9213
9214     default:
9215       break;
9216   }
9217 }
9218
9219 void DumpScreenIdentifiers(void)
9220 {
9221   int i;
9222
9223   Print("Active screen elements on current screen:\n");
9224
9225   for (i = 0; main_controls[i].nr != -1; i++)
9226   {
9227     struct MainControlInfo *mci = &main_controls[i];
9228
9229     if (mci->button_graphic != -1)
9230     {
9231       char *token = getTokenFromImageID(mci->button_graphic);
9232
9233       Print("- '%s'\n", token);
9234     }
9235   }
9236
9237   Print("Done.\n");
9238 }
9239
9240 boolean DoScreenAction(int image_id)
9241 {
9242   int i;
9243
9244   if (game_status != GAME_MODE_MAIN)
9245     return FALSE;
9246
9247   for (i = 0; main_controls[i].nr != -1; i++)
9248   {
9249     struct MainControlInfo *mci = &main_controls[i];
9250     struct MenuPosInfo *pos = mci->pos_button;
9251
9252     if (mci->button_graphic == image_id)
9253     {
9254       int x = mSX + pos->x;
9255       int y = mSY + pos->y;
9256
9257       HandleMainMenu(x, y, 0, 0, MB_MENU_CHOICE);
9258
9259       return TRUE;
9260     }
9261   }
9262
9263   return FALSE;
9264 }
9265
9266 void DrawScreenAfterAddingSet(char *tree_subdir_new, int tree_type)
9267 {
9268   // get tree info node of newly added level or artwork set
9269   TreeInfo *tree_node_first = TREE_FIRST_NODE(tree_type);
9270   TreeInfo *tree_node_new = getTreeInfoFromIdentifier(tree_node_first,
9271                                                       tree_subdir_new);
9272   if (tree_node_new == NULL)    // should not happen
9273     return;
9274
9275   // if request dialog is active, do nothing
9276   if (game.request_active)
9277     return;
9278
9279   if (game_status == GAME_MODE_MAIN &&
9280       tree_type == TREE_TYPE_LEVEL_DIR)
9281   {
9282     // when adding new level set in main menu, select it as current level set
9283
9284     // change current level set to newly added level set from zip file
9285     leveldir_current = tree_node_new;
9286
9287     // change current level number to first level of newly added level set
9288     level_nr = leveldir_current->first_level;
9289
9290     // redraw screen to reflect changed level set
9291     DrawMainMenu();
9292
9293     // save this level set and level number as last selected level set
9294     SaveLevelSetup_LastSeries();
9295     SaveLevelSetup_SeriesInfo();
9296   }
9297   else if (game_status == GAME_MODE_LEVELS &&
9298            tree_type == TREE_TYPE_LEVEL_DIR)
9299   {
9300     // when adding new level set in level set menu, set cursor and update screen
9301
9302     leveldir_current = tree_node_new;
9303
9304     DrawChooseTree(&leveldir_current);
9305   }
9306   else if (game_status == GAME_MODE_SETUP)
9307   {
9308     // when adding new artwork set in setup menu, set cursor and update screen
9309
9310     if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS &&
9311         tree_type == TREE_TYPE_GRAPHICS_DIR)
9312     {
9313       artwork.gfx_current = tree_node_new;
9314
9315       DrawChooseTree(&artwork.gfx_current);
9316     }
9317     else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS &&
9318              tree_type == TREE_TYPE_SOUNDS_DIR)
9319     {
9320       artwork.snd_current = tree_node_new;
9321
9322       DrawChooseTree(&artwork.snd_current);
9323     }
9324     else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC &&
9325              tree_type == TREE_TYPE_MUSIC_DIR)
9326     {
9327       artwork.mus_current = tree_node_new;
9328
9329       DrawChooseTree(&artwork.mus_current);
9330     }
9331   }
9332 }