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