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