fixed text on game engines setup menu screen
[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   int *num_page_entries_last = num_page_entries_all_last[game_status];
2432   int *choice_store = choice_stores[game_status];
2433   int *first_entry_store = first_entry_stores[game_status];
2434   int choice = choice_store[mode];              // starts with 0
2435   int first_entry = first_entry_store[mode];    // starts with 0
2436   int x = 0;
2437   int y = choice - first_entry;
2438   int y_old = y;
2439   boolean position_set_by_scrollbar = (dx == 999);
2440   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
2441   int i;
2442
2443   if (button == MB_MENU_INITIALIZE)
2444   {
2445     // check if number of menu page entries has changed (may happen by change
2446     // of custom artwork definition value for 'list_size' for this menu screen)
2447     // (in this case, the last menu position most probably has to be corrected)
2448     if (num_page_entries != num_page_entries_last[mode])
2449     {
2450       choice_store[mode] = first_entry_store[mode] = 0;
2451
2452       choice = first_entry = 0;
2453       y = y_old = 0;
2454
2455       num_page_entries_last[mode] = num_page_entries;
2456     }
2457
2458     // advance to first valid menu entry
2459     while (choice < num_page_entries &&
2460            menu_info[choice].type & TYPE_SKIP_ENTRY)
2461       choice++;
2462
2463     if (position_set_by_scrollbar)
2464       first_entry = first_entry_store[mode] = dy;
2465     else
2466       AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_page_entries,
2467                       NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
2468
2469     drawMenuInfoList(first_entry, num_page_entries, max_page_entries);
2470
2471     if (choice < first_entry)
2472     {
2473       choice = first_entry;
2474
2475       if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2476         choice++;
2477     }
2478     else if (choice > first_entry + num_page_entries - 1)
2479     {
2480       choice = first_entry + num_page_entries - 1;
2481
2482       if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2483         choice--;
2484     }
2485
2486     choice_store[mode] = choice;
2487
2488     DrawCursorAndText_Menu(choice - first_entry, choice, TRUE);
2489
2490     return;
2491   }
2492   else if (button == MB_MENU_LEAVE)
2493   {
2494     PlaySound(SND_MENU_ITEM_SELECTING);
2495
2496     for (i = 0; i < max_page_entries; i++)
2497     {
2498       if (menu_info[i].type & TYPE_LEAVE_MENU)
2499       {
2500         void (*menu_callback_function)(void) = menu_info[i].value;
2501
2502         FadeSetLeaveMenu();
2503
2504         menu_callback_function();
2505
2506         break;  // absolutely needed because function changes 'menu_info'!
2507       }
2508     }
2509
2510     return;
2511   }
2512
2513   if (mx || my)         // mouse input
2514   {
2515     x = (mx - mSX) / 32;
2516     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
2517   }
2518   else if (dx || dy)    // keyboard or scrollbar/scrollbutton input
2519   {
2520     // move cursor instead of scrolling when already at start/end of list
2521     if (dy == -1 * SCROLL_LINE && first_entry == 0)
2522       dy = -1;
2523     else if (dy == +1 * SCROLL_LINE &&
2524              first_entry + num_page_entries == max_page_entries)
2525       dy = 1;
2526
2527     // handle scrolling screen one line or page
2528     if (y + dy < 0 ||
2529         y + dy > num_page_entries - 1)
2530     {
2531       boolean redraw = FALSE;
2532
2533       if (ABS(dy) == SCROLL_PAGE)
2534         step = num_page_entries - 1;
2535
2536       if (dy < 0 && first_entry > 0)
2537       {
2538         // scroll page/line up
2539
2540         first_entry -= step;
2541         if (first_entry < 0)
2542           first_entry = 0;
2543
2544         redraw = TRUE;
2545       }
2546       else if (dy > 0 && first_entry + num_page_entries < max_page_entries)
2547       {
2548         // scroll page/line down
2549
2550         first_entry += step;
2551         if (first_entry + num_page_entries > max_page_entries)
2552           first_entry = MAX(0, max_page_entries - num_page_entries);
2553
2554         redraw = TRUE;
2555       }
2556
2557       if (redraw)
2558       {
2559         choice += first_entry - first_entry_store[mode];
2560
2561         if (choice < first_entry)
2562         {
2563           choice = first_entry;
2564
2565           if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2566             choice++;
2567         }
2568         else if (choice > first_entry + num_page_entries - 1)
2569         {
2570           choice = first_entry + num_page_entries - 1;
2571
2572           if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2573             choice--;
2574         }
2575         else if (menu_info[choice].type & TYPE_SKIP_ENTRY)
2576         {
2577           choice += SIGN(dy);
2578
2579           if (choice < first_entry ||
2580               choice > first_entry + num_page_entries - 1)
2581           first_entry += SIGN(dy);
2582         }
2583
2584         first_entry_store[mode] = first_entry;
2585         choice_store[mode] = choice;
2586
2587         drawMenuInfoList(first_entry, num_page_entries, max_page_entries);
2588
2589         DrawCursorAndText_Menu(choice - first_entry, choice, TRUE);
2590
2591         AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_page_entries,
2592                         NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
2593       }
2594
2595       return;
2596     }
2597
2598     if (dx)
2599     {
2600       int menu_navigation_type = (dx < 0 ? TYPE_LEAVE : TYPE_ENTER);
2601
2602       if (menu_info[choice].type & menu_navigation_type ||
2603           menu_info[choice].type & TYPE_BOOLEAN_STYLE ||
2604           menu_info[choice].type & TYPE_YES_NO_AUTO ||
2605           menu_info[choice].type & TYPE_PLAYER)
2606         button = MB_MENU_CHOICE;
2607     }
2608     else if (dy)
2609       y += dy;
2610
2611     // jump to next non-empty menu entry (up or down)
2612     while (first_entry + y > 0 &&
2613            first_entry + y < max_page_entries - 1 &&
2614            menu_info[first_entry + y].type & TYPE_SKIP_ENTRY)
2615       y += dy;
2616
2617     if (!IN_VIS_MENU(x, y))
2618     {
2619       choice += y - y_old;
2620
2621       if (choice < first_entry)
2622         first_entry = choice;
2623       else if (choice > first_entry + num_page_entries - 1)
2624         first_entry = choice - num_page_entries + 1;
2625
2626       if (first_entry >= 0 &&
2627           first_entry + num_page_entries <= max_page_entries)
2628       {
2629         first_entry_store[mode] = first_entry;
2630
2631         if (choice < first_entry)
2632           choice = first_entry;
2633         else if (choice > first_entry + num_page_entries - 1)
2634           choice = first_entry + num_page_entries - 1;
2635
2636         choice_store[mode] = choice;
2637
2638         drawMenuInfoList(first_entry, num_page_entries, max_page_entries);
2639
2640         DrawCursorAndText_Menu(choice - first_entry, choice, TRUE);
2641
2642         AdjustScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL, max_page_entries,
2643                         NUM_MENU_ENTRIES_ON_SCREEN, first_entry);
2644       }
2645
2646       return;
2647     }
2648   }
2649
2650   if (!anyScrollbarGadgetActive() &&
2651       IN_VIS_MENU(x, y) &&
2652       mx < screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x &&
2653       y >= 0 && y < num_page_entries)
2654   {
2655     if (button)
2656     {
2657       if (first_entry + y != choice &&
2658           menu_info[first_entry + y].type & ~TYPE_SKIP_ENTRY)
2659       {
2660         PlaySound(SND_MENU_ITEM_ACTIVATING);
2661
2662         DrawCursorAndText_Menu(choice - first_entry, choice, FALSE);
2663         DrawCursorAndText_Menu(y, first_entry + y, TRUE);
2664
2665         choice = choice_store[mode] = first_entry + y;
2666       }
2667       else if (dx < 0)
2668       {
2669         PlaySound(SND_MENU_ITEM_SELECTING);
2670
2671         for (i = 0; menu_info[i].type != 0; i++)
2672         {
2673           if (menu_info[i].type & TYPE_LEAVE_MENU)
2674           {
2675             void (*menu_callback_function)(void) = menu_info[i].value;
2676
2677             FadeSetLeaveMenu();
2678
2679             menu_callback_function();
2680
2681             // absolutely needed because function changes 'menu_info'!
2682             break;
2683           }
2684         }
2685
2686         return;
2687       }
2688     }
2689     else if (!(menu_info[first_entry + y].type & TYPE_GHOSTED))
2690     {
2691       PlaySound(SND_MENU_ITEM_SELECTING);
2692
2693       // when selecting key headline, execute function for key value change
2694       if (menu_info[first_entry + y].type & TYPE_KEYTEXT &&
2695           menu_info[first_entry + y + 1].type & TYPE_KEY)
2696         y++;
2697
2698       // when selecting string value, execute function for list selection
2699       if (menu_info[first_entry + y].type & TYPE_STRING && y > 0 &&
2700           menu_info[first_entry + y - 1].type & TYPE_ENTER_LIST)
2701         y--;
2702
2703       // when selecting string value, execute function for text input gadget
2704       if (menu_info[first_entry + y].type & TYPE_STRING && y > 0 &&
2705           menu_info[first_entry + y - 1].type & TYPE_TEXT_INPUT)
2706         y--;
2707
2708       if (menu_info[first_entry + y].type & TYPE_ENTER_OR_LEAVE)
2709       {
2710         void (*menu_callback_function)(void) =
2711           menu_info[first_entry + y].value;
2712
2713         FadeSetFromType(menu_info[first_entry + y].type);
2714
2715         menu_callback_function();
2716       }
2717       else if (menu_info[first_entry + y].type & TYPE_TEXT_INPUT)
2718       {
2719         void (*gadget_callback_function)(void) =
2720           menu_info[first_entry + y].value;
2721
2722         gadget_callback_function();
2723       }
2724       else if (menu_info[first_entry + y].type & TYPE_VALUE &&
2725                menu_info == setup_info)
2726       {
2727         changeSetupValue(y, first_entry + y, dx);
2728       }
2729     }
2730   }
2731 }
2732
2733 void HandleInfoScreen_Main(int mx, int my, int dx, int dy, int button)
2734 {
2735   menu_info = info_info;
2736
2737   HandleMenuScreen(mx, my, dx, dy, button,
2738                    info_mode, num_info_info, max_info_info);
2739 }
2740
2741 static int getMenuFontSpacing(int spacing_height, int font_nr)
2742 {
2743   int font_spacing = getFontHeight(font_nr) + EXTRA_SPACING(game_status);
2744
2745   return (spacing_height < 0 ? ABS(spacing_height) * font_spacing :
2746           spacing_height);
2747 }
2748
2749 static int getMenuTextSpacing(int spacing_height, int font_nr)
2750 {
2751   return (getMenuFontSpacing(spacing_height, font_nr) +
2752           EXTRA_SPACING(game_status));
2753 }
2754
2755 static int getMenuTextStep(int spacing_height, int font_nr)
2756 {
2757   return getFontHeight(font_nr) + getMenuTextSpacing(spacing_height, font_nr);
2758 }
2759
2760 void DrawInfoScreen_NotAvailable(char *text_title, char *text_error)
2761 {
2762   int font_title = MENU_INFO_FONT_TITLE;
2763   int font_error = FONT_TEXT_2;
2764   int font_foot  = MENU_INFO_FONT_FOOT;
2765   int spacing_title = menu.headline1_spacing_info[info_mode];
2766   int ystep_title = getMenuTextStep(spacing_title, font_title);
2767   int ystart1 = mSY - SY + MENU_SCREEN_INFO_YSTART1;
2768   int ystart2 = ystart1 + ystep_title;
2769   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
2770
2771   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO);
2772
2773   FadeOut(REDRAW_FIELD);
2774
2775   ClearField();
2776   DrawHeadline();
2777
2778   DrawTextSCentered(ystart1, font_title, text_title);
2779   DrawTextSCentered(ystart2, font_error, text_error);
2780
2781   DrawTextSCentered(ybottom, font_foot,
2782                     "Press any key or button for info menu");
2783
2784   FadeIn(REDRAW_FIELD);
2785 }
2786
2787 void DrawInfoScreen_HelpAnim(int start, int max_anims, boolean init)
2788 {
2789   static int infoscreen_step[MAX_INFO_ELEMENTS_ON_SCREEN];
2790   static int infoscreen_frame[MAX_INFO_ELEMENTS_ON_SCREEN];
2791   int font_title = MENU_INFO_FONT_TITLE;
2792   int font_foot  = MENU_INFO_FONT_FOOT;
2793   int xstart  = mSX + MENU_SCREEN_INFO_SPACE_LEFT;
2794   int ystart1 = mSY - SY + MENU_SCREEN_INFO_YSTART1;
2795   int ystart2 = mSY + MENU_SCREEN_INFO_YSTART2;
2796   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
2797   int ystep = MENU_SCREEN_INFO_YSTEP;
2798   int element, action, direction;
2799   int graphic;
2800   int delay;
2801   int sync_frame;
2802   int i, j;
2803
2804   if (init)
2805   {
2806     for (i = 0; i < NUM_INFO_ELEMENTS_ON_SCREEN; i++)
2807       infoscreen_step[i] = infoscreen_frame[i] = 0;
2808
2809     ClearField();
2810     DrawHeadline();
2811
2812     DrawTextSCentered(ystart1, font_title, "The Game Elements:");
2813
2814     DrawTextSCentered(ybottom, font_foot,
2815                       "Press any key or button for next page");
2816
2817     FrameCounter = 0;
2818   }
2819
2820   i = j = 0;
2821   while (helpanim_info[j].element != HELPANIM_LIST_END)
2822   {
2823     if (i >= start + NUM_INFO_ELEMENTS_ON_SCREEN ||
2824         i >= max_anims)
2825       break;
2826     else if (i < start)
2827     {
2828       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
2829         j++;
2830
2831       j++;
2832       i++;
2833
2834       continue;
2835     }
2836
2837     j += infoscreen_step[i - start];
2838
2839     element = helpanim_info[j].element;
2840     action = helpanim_info[j].action;
2841     direction = helpanim_info[j].direction;
2842
2843     if (element < 0)
2844       element = EL_UNKNOWN;
2845
2846     if (action != -1 && direction != -1)
2847       graphic = el_act_dir2img(element, action, direction);
2848     else if (action != -1)
2849       graphic = el_act2img(element, action);
2850     else if (direction != -1)
2851       graphic = el_dir2img(element, direction);
2852     else
2853       graphic = el2img(element);
2854
2855     delay = helpanim_info[j++].delay;
2856
2857     if (delay == -1)
2858       delay = 1000000;
2859
2860     if (infoscreen_frame[i - start] == 0)
2861     {
2862       sync_frame = 0;
2863       infoscreen_frame[i - start] = delay - 1;
2864     }
2865     else
2866     {
2867       sync_frame = delay - infoscreen_frame[i - start];
2868       infoscreen_frame[i - start]--;
2869     }
2870
2871     if (helpanim_info[j].element == HELPANIM_LIST_NEXT)
2872     {
2873       if (!infoscreen_frame[i - start])
2874         infoscreen_step[i - start] = 0;
2875     }
2876     else
2877     {
2878       if (!infoscreen_frame[i - start])
2879         infoscreen_step[i - start]++;
2880       while (helpanim_info[j].element != HELPANIM_LIST_NEXT)
2881         j++;
2882     }
2883
2884     j++;
2885
2886     ClearRectangleOnBackground(drawto, xstart, ystart2 + (i - start) * ystep,
2887                                TILEX, TILEY);
2888     DrawFixedGraphicAnimationExt(drawto, xstart, ystart2 + (i - start) * ystep,
2889                                  graphic, sync_frame, USE_MASKING);
2890
2891     if (init)
2892       DrawInfoScreen_HelpText(element, action, direction, i - start);
2893
2894     i++;
2895   }
2896
2897   redraw_mask |= REDRAW_FIELD;
2898
2899   FrameCounter++;
2900 }
2901
2902 static char *getHelpText(int element, int action, int direction)
2903 {
2904   char token[MAX_LINE_LEN];
2905
2906   strcpy(token, element_info[element].token_name);
2907
2908   if (action != -1)
2909     strcat(token, element_action_info[action].suffix);
2910
2911   if (direction != -1)
2912     strcat(token, element_direction_info[MV_DIR_TO_BIT(direction)].suffix);
2913
2914   return getHashEntry(helptext_info, token);
2915 }
2916
2917 void DrawInfoScreen_HelpText(int element, int action, int direction, int ypos)
2918 {
2919   int font_nr = FONT_INFO_ELEMENTS;
2920   int font_width = getFontWidth(font_nr);
2921   int font_height = getFontHeight(font_nr);
2922   int yoffset = (TILEX - 2 * font_height) / 2;
2923   int xstart = mSX + MENU_SCREEN_INFO_SPACE_LEFT + TILEX + MINI_TILEX;
2924   int ystart = mSY + MENU_SCREEN_INFO_YSTART2 + yoffset;
2925   int ystep = TILEY + 4;
2926   int pad_left = xstart - SX;
2927   int pad_right = MENU_SCREEN_INFO_SPACE_RIGHT;
2928   int max_chars_per_line = (SXSIZE - pad_left - pad_right) / font_width;
2929   int max_lines_per_text = 2;    
2930   char *text = NULL;
2931
2932   if (action != -1 && direction != -1)          // element.action.direction
2933     text = getHelpText(element, action, direction);
2934
2935   if (text == NULL && action != -1)             // element.action
2936     text = getHelpText(element, action, -1);
2937
2938   if (text == NULL && direction != -1)          // element.direction
2939     text = getHelpText(element, -1, direction);
2940
2941   if (text == NULL)                             // base element
2942     text = getHelpText(element, -1, -1);
2943
2944   if (text == NULL)                             // not found
2945     text = "No description available";
2946
2947   if (strlen(text) <= max_chars_per_line)       // only one line of text
2948     ystart += getFontHeight(font_nr) / 2;
2949
2950   DrawTextBuffer(xstart, ystart + ypos * ystep, text, font_nr,
2951                  max_chars_per_line, -1, max_lines_per_text, 0, -1,
2952                  TRUE, FALSE, FALSE);
2953 }
2954
2955 static void DrawInfoScreen_TitleScreen(void)
2956 {
2957   SetGameStatus(GAME_MODE_TITLE);
2958
2959   DrawTitleScreen();
2960 }
2961
2962 void HandleInfoScreen_TitleScreen(int button)
2963 {
2964   HandleTitleScreen(0, 0, 0, 0, button);
2965 }
2966
2967 static void DrawInfoScreen_Elements(void)
2968 {
2969   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_ELEMENTS);
2970
2971   FadeOut(REDRAW_FIELD);
2972
2973   LoadHelpAnimInfo();
2974   LoadHelpTextInfo();
2975
2976   HandleInfoScreen_Elements(MB_MENU_INITIALIZE);
2977
2978   FadeIn(REDRAW_FIELD);
2979 }
2980
2981 void HandleInfoScreen_Elements(int button)
2982 {
2983   static unsigned int info_delay = 0;
2984   static int num_anims;
2985   static int num_pages;
2986   static int page;
2987   int anims_per_page = NUM_INFO_ELEMENTS_ON_SCREEN;
2988   int i;
2989
2990   if (button == MB_MENU_INITIALIZE)
2991   {
2992     boolean new_element = TRUE;
2993
2994     num_anims = 0;
2995
2996     for (i = 0; helpanim_info[i].element != HELPANIM_LIST_END; i++)
2997     {
2998       if (helpanim_info[i].element == HELPANIM_LIST_NEXT)
2999         new_element = TRUE;
3000       else if (new_element)
3001       {
3002         num_anims++;
3003         new_element = FALSE;
3004       }
3005     }
3006
3007     num_pages = (num_anims + anims_per_page - 1) / anims_per_page;
3008     page = 0;
3009   }
3010
3011   if (button == MB_MENU_LEAVE)
3012   {
3013     PlaySound(SND_MENU_ITEM_SELECTING);
3014
3015     info_mode = INFO_MODE_MAIN;
3016     DrawInfoScreen();
3017
3018     return;
3019   }
3020   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
3021   {
3022     if (button != MB_MENU_INITIALIZE)
3023     {
3024       PlaySound(SND_MENU_ITEM_SELECTING);
3025
3026       page++;
3027     }
3028
3029     if (page >= num_pages)
3030     {
3031       FadeMenuSoundsAndMusic();
3032
3033       info_mode = INFO_MODE_MAIN;
3034       DrawInfoScreen();
3035
3036       return;
3037     }
3038
3039     if (page > 0)
3040       FadeSetNextScreen();
3041
3042     if (button != MB_MENU_INITIALIZE)
3043       FadeOut(REDRAW_FIELD);
3044
3045     DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, TRUE);
3046
3047     if (button != MB_MENU_INITIALIZE)
3048       FadeIn(REDRAW_FIELD);
3049   }
3050   else
3051   {
3052     if (DelayReached(&info_delay, GameFrameDelay))
3053       if (page < num_pages)
3054         DrawInfoScreen_HelpAnim(page * anims_per_page, num_anims, FALSE);
3055
3056     PlayMenuSoundIfLoop();
3057   }
3058 }
3059
3060 static void DrawInfoScreen_Music(void)
3061 {
3062   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_MUSIC);
3063
3064   FadeOut(REDRAW_FIELD);
3065
3066   ClearField();
3067   DrawHeadline();
3068
3069   LoadMusicInfo();
3070
3071   HandleInfoScreen_Music(MB_MENU_INITIALIZE);
3072
3073   FadeIn(REDRAW_FIELD);
3074 }
3075
3076 void HandleInfoScreen_Music(int button)
3077 {
3078   static struct MusicFileInfo *list = NULL;
3079   int font_title = MENU_INFO_FONT_TITLE;
3080   int font_head  = MENU_INFO_FONT_HEAD;
3081   int font_text  = MENU_INFO_FONT_TEXT;
3082   int font_foot  = MENU_INFO_FONT_FOOT;
3083   int spacing_title = menu.headline1_spacing_info[info_mode];
3084   int spacing_head  = menu.headline2_spacing_info[info_mode];
3085   int ystep_title = getMenuTextStep(spacing_title, font_title);
3086   int ystep_head  = getMenuTextStep(spacing_head,  font_head);
3087   int ystart  = mSY - SY + MENU_SCREEN_INFO_YSTART1;
3088   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
3089
3090   if (button == MB_MENU_INITIALIZE)
3091   {
3092     list = music_file_info;
3093
3094     if (list == NULL)
3095     {
3096       FadeMenuSoundsAndMusic();
3097
3098       ClearField();
3099       DrawHeadline();
3100
3101       DrawTextSCentered(ystart, font_title,
3102                         "No music info for this level set.");
3103
3104       DrawTextSCentered(ybottom, font_foot,
3105                         "Press any key or button for info menu");
3106
3107       return;
3108     }
3109   }
3110
3111   if (button == MB_MENU_LEAVE)
3112   {
3113     PlaySound(SND_MENU_ITEM_SELECTING);
3114
3115     FadeMenuSoundsAndMusic();
3116
3117     info_mode = INFO_MODE_MAIN;
3118     DrawInfoScreen();
3119
3120     return;
3121   }
3122   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
3123   {
3124     if (button != MB_MENU_INITIALIZE)
3125     {
3126       PlaySound(SND_MENU_ITEM_SELECTING);
3127
3128       if (list != NULL)
3129         list = list->next;
3130     }
3131
3132     if (list == NULL)
3133     {
3134       FadeMenuSoundsAndMusic();
3135
3136       info_mode = INFO_MODE_MAIN;
3137       DrawInfoScreen();
3138
3139       return;
3140     }
3141
3142     FadeMenuSoundsAndMusic();
3143
3144     if (list != music_file_info)
3145       FadeSetNextScreen();
3146
3147     if (button != MB_MENU_INITIALIZE)
3148       FadeOut(REDRAW_FIELD);
3149
3150     ClearField();
3151     DrawHeadline();
3152
3153     if (list->is_sound)
3154     {
3155       int sound = list->music;
3156
3157       if (sound_info[sound].loop)
3158         PlaySoundLoop(sound);
3159       else
3160         PlaySound(sound);
3161
3162       DrawTextSCentered(ystart, font_title, "The Game Background Sounds:");
3163     }
3164     else
3165     {
3166       int music = list->music;
3167
3168       if (music_info[music].loop)
3169         PlayMusicLoop(music);
3170       else
3171         PlayMusic(music);
3172
3173       DrawTextSCentered(ystart, font_title, "The Game Background Music:");
3174     }
3175
3176     ystart += ystep_title;
3177
3178     if (!strEqual(list->title, UNKNOWN_NAME))
3179     {
3180       if (!strEqual(list->title_header, UNKNOWN_NAME))
3181       {
3182         DrawTextSCentered(ystart, font_head, list->title_header);
3183         ystart += ystep_head;
3184       }
3185
3186       DrawTextFCentered(ystart, font_text, "\"%s\"", list->title);
3187       ystart += ystep_head;
3188     }
3189
3190     if (!strEqual(list->artist, UNKNOWN_NAME))
3191     {
3192       if (!strEqual(list->artist_header, UNKNOWN_NAME))
3193         DrawTextSCentered(ystart, font_head, list->artist_header);
3194       else
3195         DrawTextSCentered(ystart, font_head, "by");
3196
3197       ystart += ystep_head;
3198
3199       DrawTextFCentered(ystart, font_text, "%s", list->artist);
3200       ystart += ystep_head;
3201     }
3202
3203     if (!strEqual(list->album, UNKNOWN_NAME))
3204     {
3205       if (!strEqual(list->album_header, UNKNOWN_NAME))
3206         DrawTextSCentered(ystart, font_head, list->album_header);
3207       else
3208         DrawTextSCentered(ystart, font_head, "from the album");
3209
3210       ystart += ystep_head;
3211
3212       DrawTextFCentered(ystart, font_text, "\"%s\"", list->album);
3213       ystart += ystep_head;
3214     }
3215
3216     if (!strEqual(list->year, UNKNOWN_NAME))
3217     {
3218       if (!strEqual(list->year_header, UNKNOWN_NAME))
3219         DrawTextSCentered(ystart, font_head, list->year_header);
3220       else
3221         DrawTextSCentered(ystart, font_head, "from the year");
3222
3223       ystart += ystep_head;
3224
3225       DrawTextFCentered(ystart, font_text, "%s", list->year);
3226       ystart += ystep_head;
3227     }
3228
3229     DrawTextSCentered(ybottom, FONT_TEXT_4,
3230                       "Press any key or button for next page");
3231
3232     if (button != MB_MENU_INITIALIZE)
3233       FadeIn(REDRAW_FIELD);
3234   }
3235
3236   if (list != NULL && list->is_sound && sound_info[list->music].loop)
3237     PlaySoundLoop(list->music);
3238 }
3239
3240 static void DrawInfoScreen_CreditsScreen(int screen_nr)
3241 {
3242   int font_title = MENU_INFO_FONT_TITLE;
3243   int font_head  = MENU_INFO_FONT_HEAD;
3244   int font_text  = MENU_INFO_FONT_TEXT;
3245   int font_foot  = MENU_INFO_FONT_FOOT;
3246   int spacing_title = menu.headline1_spacing_info[info_mode];
3247   int spacing_head  = menu.headline2_spacing_info[info_mode];
3248   int spacing_para  = menu.paragraph_spacing_info[info_mode];
3249   int spacing_line  = menu.line_spacing_info[info_mode];
3250   int ystep_title = getMenuTextStep(spacing_title, font_title);
3251   int ystep_head  = getMenuTextStep(spacing_head,  font_head);
3252   int ystep_para  = getMenuTextStep(spacing_para,  font_text);
3253   int ystep_line  = getMenuTextStep(spacing_line,  font_text);
3254   int ystart  = mSY - SY + MENU_SCREEN_INFO_YSTART1;
3255   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
3256
3257   ClearField();
3258   DrawHeadline();
3259
3260   DrawTextSCentered(ystart, font_title, "Credits:");
3261   ystart += ystep_title;
3262
3263   if (screen_nr == 0)
3264   {
3265     DrawTextSCentered(ystart, font_head,
3266                       "Special thanks to");
3267     ystart += ystep_head;
3268     DrawTextSCentered(ystart, font_text,
3269                       "Peter Liepa");
3270     ystart += ystep_head;
3271     DrawTextSCentered(ystart, font_head,
3272                       "for creating");
3273     ystart += ystep_head;
3274     DrawTextSCentered(ystart, font_text,
3275                       "\"Boulder Dash\"");
3276     ystart += ystep_head;
3277     DrawTextSCentered(ystart, font_head,
3278                       "in the year");
3279     ystart += ystep_head;
3280     DrawTextSCentered(ystart, font_text,
3281                       "1984");
3282     ystart += ystep_head;
3283     DrawTextSCentered(ystart, font_head,
3284                       "published by");
3285     ystart += ystep_head;
3286     DrawTextSCentered(ystart, font_text,
3287                       "First Star Software");
3288   }
3289   else if (screen_nr == 1)
3290   {
3291     DrawTextSCentered(ystart, font_head,
3292                       "Special thanks to");
3293     ystart += ystep_head;
3294     DrawTextSCentered(ystart, font_text,
3295                       "Klaus Heinz & Volker Wertich");
3296     ystart += ystep_head;
3297     DrawTextSCentered(ystart, font_head,
3298                       "for creating");
3299     ystart += ystep_head;
3300     DrawTextSCentered(ystart, font_text,
3301                       "\"Emerald Mine\"");
3302     ystart += ystep_head;
3303     DrawTextSCentered(ystart, font_head,
3304                       "in the year");
3305     ystart += ystep_head;
3306     DrawTextSCentered(ystart, font_text,
3307                       "1987");
3308     ystart += ystep_head;
3309     DrawTextSCentered(ystart, font_head,
3310                       "published by");
3311     ystart += ystep_head;
3312     DrawTextSCentered(ystart, font_text,
3313                       "Kingsoft");
3314   }
3315   else if (screen_nr == 2)
3316   {
3317     DrawTextSCentered(ystart, font_head,
3318                       "Special thanks to");
3319     ystart += ystep_head;
3320     DrawTextSCentered(ystart, font_text,
3321                       "Michael Stopp & Philip Jespersen");
3322     ystart += ystep_head;
3323     DrawTextSCentered(ystart, font_head,
3324                       "for creating");
3325     ystart += ystep_head;
3326     DrawTextSCentered(ystart, font_text,
3327                       "\"Supaplex\"");
3328     ystart += ystep_head;
3329     DrawTextSCentered(ystart, font_head,
3330                       "in the year");
3331     ystart += ystep_head;
3332     DrawTextSCentered(ystart, font_text,
3333                       "1991");
3334     ystart += ystep_head;
3335     DrawTextSCentered(ystart, font_head,
3336                       "published by");
3337     ystart += ystep_head;
3338     DrawTextSCentered(ystart, font_text,
3339                       "Digital Integration");
3340   }
3341   else if (screen_nr == 3)
3342   {
3343     DrawTextSCentered(ystart, font_head,
3344                       "Special thanks to");
3345     ystart += ystep_head;
3346     DrawTextSCentered(ystart, font_text,
3347                       "Hiroyuki Imabayashi");
3348     ystart += ystep_head;
3349     DrawTextSCentered(ystart, font_head,
3350                       "for creating");
3351     ystart += ystep_head;
3352     DrawTextSCentered(ystart, font_text,
3353                       "\"Sokoban\"");
3354     ystart += ystep_head;
3355     DrawTextSCentered(ystart, font_head,
3356                       "in the year");
3357     ystart += ystep_head;
3358     DrawTextSCentered(ystart, font_text,
3359                       "1982");
3360     ystart += ystep_head;
3361     DrawTextSCentered(ystart, font_head,
3362                       "published by");
3363     ystart += ystep_head;
3364     DrawTextSCentered(ystart, font_text,
3365                       "Thinking Rabbit");
3366   }
3367   else if (screen_nr == 4)
3368   {
3369     DrawTextSCentered(ystart, font_head,
3370                       "Special thanks to");
3371     ystart += ystep_head;
3372     DrawTextSCentered(ystart, font_text,
3373                       "Alan Bond");
3374     ystart += ystep_head;
3375     DrawTextSCentered(ystart, font_head,
3376                       "and");
3377     ystart += ystep_head;
3378     DrawTextSCentered(ystart, font_text,
3379                       "J\xfcrgen Bonhagen");
3380     ystart += ystep_head;
3381     DrawTextSCentered(ystart, font_head,
3382                       "for the continuous creation");
3383     ystart += ystep_line;
3384     DrawTextSCentered(ystart, font_head,
3385                       "of outstanding level sets");
3386   }
3387   else if (screen_nr == 5)
3388   {
3389     DrawTextSCentered(ystart, font_head,
3390                       "Thanks to");
3391     ystart += ystep_head;
3392     DrawTextSCentered(ystart, font_text,
3393                       "Peter Elzner");
3394     ystart += ystep_head;
3395     DrawTextSCentered(ystart, font_head,
3396                       "for ideas and inspiration by");
3397     ystart += ystep_head;
3398     DrawTextSCentered(ystart, font_text,
3399                       "Diamond Caves");
3400     ystart += ystep_para;
3401
3402     DrawTextSCentered(ystart, font_head,
3403                       "Thanks to");
3404     ystart += ystep_head;
3405     DrawTextSCentered(ystart, font_text,
3406                       "Steffest");
3407     ystart += ystep_head;
3408     DrawTextSCentered(ystart, font_head,
3409                       "for ideas and inspiration by");
3410     ystart += ystep_head;
3411     DrawTextSCentered(ystart, font_text,
3412                       "DX-Boulderdash");
3413   }
3414   else if (screen_nr == 6)
3415   {
3416     DrawTextSCentered(ystart, font_head,
3417                       "Thanks to");
3418     ystart += ystep_head;
3419     DrawTextSCentered(ystart, font_text,
3420                       "David Tritscher");
3421     ystart += ystep_head;
3422     DrawTextSCentered(ystart, font_head,
3423                       "for the code base used for the");
3424     ystart += ystep_line;
3425     DrawTextSCentered(ystart, font_head,
3426                       "native Emerald Mine engine");
3427   }
3428   else if (screen_nr == 7)
3429   {
3430     DrawTextSCentered(ystart, font_head,
3431                       "Thanks to");
3432     ystart += ystep_head;
3433     DrawTextSCentered(ystart, font_text,
3434                       "Guido Schulz");
3435     ystart += ystep_head;
3436     DrawTextSCentered(ystart, font_head,
3437                       "for the initial DOS port");
3438     ystart += ystep_para;
3439
3440     DrawTextSCentered(ystart, font_head,
3441                       "Thanks to");
3442     ystart += ystep_head;
3443     DrawTextSCentered(ystart, font_text,
3444                       "Karl H\xf6rnell");
3445     ystart += ystep_head;
3446     DrawTextSCentered(ystart, font_head,
3447                       "for some additional toons");
3448   }
3449   else if (screen_nr == 8)
3450   {
3451     DrawTextSCentered(ystart, font_head,
3452                       "And not to forget:");
3453     ystart += ystep_head;
3454     DrawTextSCentered(ystart, font_head,
3455                       "Many thanks to");
3456     ystart += ystep_head;
3457     DrawTextSCentered(ystart, font_text,
3458                       "All those who contributed");
3459     ystart += ystep_line;
3460     DrawTextSCentered(ystart, font_text,
3461                       "levels to this game");
3462     ystart += ystep_line;
3463     DrawTextSCentered(ystart, font_text,
3464                       "since 1995");
3465   }
3466
3467   DrawTextSCentered(ybottom, font_foot,
3468                     "Press any key or button for next page");
3469 }
3470
3471 static void DrawInfoScreen_Credits(void)
3472 {
3473   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_CREDITS);
3474
3475   FadeMenuSoundsAndMusic();
3476
3477   FadeOut(REDRAW_FIELD);
3478
3479   HandleInfoScreen_Credits(MB_MENU_INITIALIZE);
3480
3481   FadeIn(REDRAW_FIELD);
3482 }
3483
3484 void HandleInfoScreen_Credits(int button)
3485 {
3486   static int screen_nr = 0;
3487   int num_screens = 9;
3488
3489   if (button == MB_MENU_INITIALIZE)
3490   {
3491     screen_nr = 0;
3492
3493     // DrawInfoScreen_CreditsScreen(screen_nr);
3494   }
3495
3496   if (button == MB_MENU_LEAVE)
3497   {
3498     PlaySound(SND_MENU_ITEM_SELECTING);
3499
3500     info_mode = INFO_MODE_MAIN;
3501     DrawInfoScreen();
3502
3503     return;
3504   }
3505   else if (button == MB_MENU_CHOICE || button == MB_MENU_INITIALIZE)
3506   {
3507     if (button != MB_MENU_INITIALIZE)
3508     {
3509       PlaySound(SND_MENU_ITEM_SELECTING);
3510
3511       screen_nr++;
3512     }
3513
3514     if (screen_nr >= num_screens)
3515     {
3516       FadeMenuSoundsAndMusic();
3517
3518       info_mode = INFO_MODE_MAIN;
3519       DrawInfoScreen();
3520
3521       return;
3522     }
3523
3524     if (screen_nr > 0)
3525       FadeSetNextScreen();
3526
3527     if (button != MB_MENU_INITIALIZE)
3528       FadeOut(REDRAW_FIELD);
3529
3530     DrawInfoScreen_CreditsScreen(screen_nr);
3531
3532     if (button != MB_MENU_INITIALIZE)
3533       FadeIn(REDRAW_FIELD);
3534   }
3535   else
3536   {
3537     PlayMenuSoundIfLoop();
3538   }
3539 }
3540
3541 static void DrawInfoScreen_Program(void)
3542 {
3543   int font_title = MENU_INFO_FONT_TITLE;
3544   int font_head  = MENU_INFO_FONT_HEAD;
3545   int font_text  = MENU_INFO_FONT_TEXT;
3546   int font_foot  = MENU_INFO_FONT_FOOT;
3547   int spacing_title = menu.headline1_spacing_info[info_mode];
3548   int spacing_head  = menu.headline2_spacing_info[info_mode];
3549   int spacing_para  = menu.paragraph_spacing_info[info_mode];
3550   int spacing_line  = menu.line_spacing_info[info_mode];
3551   int ystep_title = getMenuTextStep(spacing_title, font_title);
3552   int ystep_head  = getMenuTextStep(spacing_head,  font_head);
3553   int ystep_para  = getMenuTextStep(spacing_para,  font_text);
3554   int ystep_line  = getMenuTextStep(spacing_line,  font_text);
3555   int ystart  = mSY - SY + MENU_SCREEN_INFO_YSTART1;
3556   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
3557
3558   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_PROGRAM);
3559
3560   FadeOut(REDRAW_FIELD);
3561
3562   ClearField();
3563   DrawHeadline();
3564
3565   DrawTextSCentered(ystart, font_title, "Program Information:");
3566   ystart += ystep_title;
3567
3568   DrawTextSCentered(ystart, font_head,
3569                     "This game is Freeware!");
3570   ystart += ystep_head;
3571   DrawTextSCentered(ystart, font_head,
3572                     "If you like it, send e-mail to:");
3573   ystart += ystep_head;
3574   DrawTextSCentered(ystart, font_text,
3575                     setup.internal.program_email);
3576   ystart += ystep_para;
3577
3578   DrawTextSCentered(ystart, font_head,
3579                     "More information and levels:");
3580   ystart += ystep_head;
3581   DrawTextSCentered(ystart, font_text,
3582                     setup.internal.program_website);
3583   ystart += ystep_para;
3584
3585   DrawTextSCentered(ystart, font_head,
3586                     "If you have created new levels,");
3587   ystart += ystep_line;
3588   DrawTextSCentered(ystart, font_head,
3589                     "send them to me to include them!");
3590   ystart += ystep_head;
3591   DrawTextSCentered(ystart, font_head,
3592                     ":-)");
3593
3594   DrawTextSCentered(ybottom, font_foot,
3595                     "Press any key or button for info menu");
3596
3597   FadeIn(REDRAW_FIELD);
3598 }
3599
3600 void HandleInfoScreen_Program(int button)
3601 {
3602   if (button == MB_MENU_LEAVE)
3603   {
3604     PlaySound(SND_MENU_ITEM_SELECTING);
3605
3606     info_mode = INFO_MODE_MAIN;
3607     DrawInfoScreen();
3608
3609     return;
3610   }
3611   else if (button == MB_MENU_CHOICE)
3612   {
3613     PlaySound(SND_MENU_ITEM_SELECTING);
3614
3615     FadeMenuSoundsAndMusic();
3616
3617     info_mode = INFO_MODE_MAIN;
3618     DrawInfoScreen();
3619   }
3620   else
3621   {
3622     PlayMenuSoundIfLoop();
3623   }
3624 }
3625
3626 static void DrawInfoScreen_Version(void)
3627 {
3628   int font_title = MENU_INFO_FONT_TITLE;
3629   int font_head  = MENU_INFO_FONT_HEAD;
3630   int font_text  = MENU_INFO_FONT_TEXT;
3631   int font_foot  = MENU_INFO_FONT_FOOT;
3632   int spacing_title = menu.headline1_spacing_info[info_mode];
3633   int spacing_head  = menu.headline2_spacing_info[info_mode];
3634   int spacing_para  = menu.paragraph_spacing_info[info_mode];
3635   int spacing_line  = menu.line_spacing_info[info_mode];
3636   int xstep = getFontWidth(font_text);
3637   int ystep_title = getMenuTextStep(spacing_title, font_title);
3638   int ystep_head  = getMenuTextStep(spacing_head,  font_head);
3639   int ystep_para  = getMenuTextStep(spacing_para,  font_text);
3640   int ystep_line  = getMenuTextStep(spacing_line,  font_text);
3641   int ystart  = mSY - SY + MENU_SCREEN_INFO_YSTART1;
3642   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
3643   int xstart1 = mSX - SX + 2 * xstep;
3644   int xstart2 = mSX - SX + 18 * xstep;
3645   int xstart3 = mSX - SX + 28 * xstep;
3646   SDL_version sdl_version_compiled;
3647   const SDL_version *sdl_version_linked;
3648   int driver_name_len = 10;
3649   SDL_version sdl_version_linked_ext;
3650   const char *driver_name = NULL;
3651
3652   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_VERSION);
3653
3654   FadeOut(REDRAW_FIELD);
3655
3656   ClearField();
3657   DrawHeadline();
3658
3659   DrawTextSCentered(ystart, font_title, "Version Information:");
3660   ystart += ystep_title;
3661
3662   DrawTextF(xstart1, ystart, font_head, "Name");
3663   DrawTextF(xstart2, ystart, font_text, getProgramTitleString());
3664   ystart += ystep_line;
3665
3666   if (!strEqual(getProgramVersionString(), getProgramRealVersionString()))
3667   {
3668     DrawTextF(xstart1, ystart, font_head, "Version (fake)");
3669     DrawTextF(xstart2, ystart, font_text, getProgramVersionString());
3670     ystart += ystep_line;
3671
3672     DrawTextF(xstart1, ystart, font_head, "Version (real)");
3673     DrawTextF(xstart2, ystart, font_text, getProgramRealVersionString());
3674     ystart += ystep_line;
3675   }
3676   else
3677   {
3678     DrawTextF(xstart1, ystart, font_head, "Version");
3679     DrawTextF(xstart2, ystart, font_text, getProgramVersionString());
3680     ystart += ystep_line;
3681   }
3682
3683   DrawTextF(xstart1, ystart, font_head, "Platform");
3684   DrawTextF(xstart2, ystart, font_text, PLATFORM_STRING);
3685   ystart += ystep_line;
3686
3687   DrawTextF(xstart1, ystart, font_head, "Target");
3688   DrawTextF(xstart2, ystart, font_text, TARGET_STRING);
3689   ystart += ystep_line;
3690
3691   DrawTextF(xstart1, ystart, font_head, "Source date");
3692   DrawTextF(xstart2, ystart, font_text, getSourceDateString());
3693   ystart += ystep_para;
3694
3695   DrawTextF(xstart1, ystart, font_head, "Library");
3696   DrawTextF(xstart2, ystart, font_head, "compiled");
3697   DrawTextF(xstart3, ystart, font_head, "linked");
3698   ystart += ystep_head;
3699
3700   SDL_VERSION(&sdl_version_compiled);
3701   SDL_GetVersion(&sdl_version_linked_ext);
3702   sdl_version_linked = &sdl_version_linked_ext;
3703
3704   DrawTextF(xstart1, ystart, font_text, "SDL");
3705   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3706             sdl_version_compiled.major,
3707             sdl_version_compiled.minor,
3708             sdl_version_compiled.patch);
3709   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3710             sdl_version_linked->major,
3711             sdl_version_linked->minor,
3712             sdl_version_linked->patch);
3713   ystart += ystep_line;
3714
3715   SDL_IMAGE_VERSION(&sdl_version_compiled);
3716   sdl_version_linked = IMG_Linked_Version();
3717
3718   DrawTextF(xstart1, ystart, font_text, "SDL_image");
3719   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3720             sdl_version_compiled.major,
3721             sdl_version_compiled.minor,
3722             sdl_version_compiled.patch);
3723   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3724             sdl_version_linked->major,
3725             sdl_version_linked->minor,
3726             sdl_version_linked->patch);
3727   ystart += ystep_line;
3728
3729   SDL_MIXER_VERSION(&sdl_version_compiled);
3730   sdl_version_linked = Mix_Linked_Version();
3731
3732   DrawTextF(xstart1, ystart, font_text, "SDL_mixer");
3733   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3734             sdl_version_compiled.major,
3735             sdl_version_compiled.minor,
3736             sdl_version_compiled.patch);
3737   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3738             sdl_version_linked->major,
3739             sdl_version_linked->minor,
3740             sdl_version_linked->patch);
3741   ystart += ystep_line;
3742
3743   SDL_NET_VERSION(&sdl_version_compiled);
3744   sdl_version_linked = SDLNet_Linked_Version();
3745
3746   DrawTextF(xstart1, ystart, font_text, "SDL_net");
3747   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3748             sdl_version_compiled.major,
3749             sdl_version_compiled.minor,
3750             sdl_version_compiled.patch);
3751   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3752             sdl_version_linked->major,
3753             sdl_version_linked->minor,
3754             sdl_version_linked->patch);
3755   ystart += ystep_para;
3756
3757   DrawTextF(xstart1, ystart, font_head, "Driver");
3758   DrawTextF(xstart2, ystart, font_head, "Requested");
3759   DrawTextF(xstart3, ystart, font_head, "Used");
3760   ystart += ystep_head;
3761
3762   driver_name = getStringCopyNStatic(SDL_GetVideoDriver(0), driver_name_len);
3763
3764   DrawTextF(xstart1, ystart, font_text, "SDL_VideoDriver");
3765   DrawTextF(xstart2, ystart, font_text, "%s", setup.system.sdl_videodriver);
3766   DrawTextF(xstart3, ystart, font_text, "%s", driver_name);
3767   ystart += ystep_line;
3768
3769   driver_name = getStringCopyNStatic(SDL_GetAudioDriver(0), driver_name_len);
3770
3771   DrawTextF(xstart1, ystart, font_text, "SDL_AudioDriver");
3772   DrawTextF(xstart2, ystart, font_text, "%s", setup.system.sdl_audiodriver);
3773   DrawTextF(xstart3, ystart, font_text, "%s", driver_name);
3774
3775   DrawTextSCentered(ybottom, font_foot,
3776                     "Press any key or button for info menu");
3777
3778   FadeIn(REDRAW_FIELD);
3779 }
3780
3781 void HandleInfoScreen_Version(int button)
3782 {
3783   if (button == MB_MENU_LEAVE)
3784   {
3785     PlaySound(SND_MENU_ITEM_SELECTING);
3786
3787     info_mode = INFO_MODE_MAIN;
3788     DrawInfoScreen();
3789
3790     return;
3791   }
3792   else if (button == MB_MENU_CHOICE)
3793   {
3794     PlaySound(SND_MENU_ITEM_SELECTING);
3795
3796     FadeMenuSoundsAndMusic();
3797
3798     info_mode = INFO_MODE_MAIN;
3799     DrawInfoScreen();
3800   }
3801   else
3802   {
3803     PlayMenuSoundIfLoop();
3804   }
3805 }
3806
3807 static void DrawInfoScreen_LevelSet(void)
3808 {
3809   struct TitleMessageInfo *tmi = &readme;
3810   char *filename = getLevelSetInfoFilename();
3811   char *title = "Level Set Information:";
3812   int ystart  = mSY - SY + MENU_SCREEN_INFO_YSTART1;
3813   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
3814
3815   if (filename == NULL)
3816   {
3817     DrawInfoScreen_NotAvailable(title, "No information for this level set.");
3818
3819     return;
3820   }
3821
3822   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_LEVELSET);
3823
3824   FadeOut(REDRAW_FIELD);
3825
3826   ClearField();
3827   DrawHeadline();
3828
3829   DrawTextSCentered(ystart, FONT_TEXT_1, title);
3830
3831   // if x position set to "-1", automatically determine by playfield width
3832   if (tmi->x == -1)
3833     tmi->x = SXSIZE / 2;
3834
3835   // if y position set to "-1", use static default value
3836   if (tmi->y == -1)
3837     tmi->y = 150;
3838
3839   // if width set to "-1", automatically determine by playfield width
3840   if (tmi->width == -1)
3841     tmi->width = SXSIZE - 2 * TILEX;
3842
3843   // if height set to "-1", automatically determine by playfield height
3844   if (tmi->height == -1)
3845     tmi->height = MENU_SCREEN_INFO_YBOTTOM - tmi->y - 10;
3846
3847   // if chars set to "-1", automatically determine by text and font width
3848   if (tmi->chars == -1)
3849     tmi->chars = tmi->width / getFontWidth(tmi->font);
3850   else
3851     tmi->width = tmi->chars * getFontWidth(tmi->font);
3852
3853   // if lines set to "-1", automatically determine by text and font height
3854   if (tmi->lines == -1)
3855     tmi->lines = tmi->height / getFontHeight(tmi->font);
3856   else
3857     tmi->height = tmi->lines * getFontHeight(tmi->font);
3858
3859   DrawTextFile(mSX + ALIGNED_TEXT_XPOS(tmi), mSY + ALIGNED_TEXT_YPOS(tmi),
3860                filename, tmi->font, tmi->chars, -1, tmi->lines, 0, -1,
3861                tmi->autowrap, tmi->centered, tmi->parse_comments);
3862
3863   DrawTextSCentered(ybottom, FONT_TEXT_4,
3864                     "Press any key or button for info menu");
3865
3866   FadeIn(REDRAW_FIELD);
3867 }
3868
3869 static void HandleInfoScreen_LevelSet(int button)
3870 {
3871   if (button == MB_MENU_LEAVE)
3872   {
3873     PlaySound(SND_MENU_ITEM_SELECTING);
3874
3875     info_mode = INFO_MODE_MAIN;
3876     DrawInfoScreen();
3877
3878     return;
3879   }
3880   else if (button == MB_MENU_CHOICE)
3881   {
3882     PlaySound(SND_MENU_ITEM_SELECTING);
3883
3884     FadeMenuSoundsAndMusic();
3885
3886     info_mode = INFO_MODE_MAIN;
3887     DrawInfoScreen();
3888   }
3889   else
3890   {
3891     PlayMenuSoundIfLoop();
3892   }
3893 }
3894
3895 static void DrawInfoScreen(void)
3896 {
3897   if (info_mode == INFO_MODE_TITLE)
3898     DrawInfoScreen_TitleScreen();
3899   else if (info_mode == INFO_MODE_ELEMENTS)
3900     DrawInfoScreen_Elements();
3901   else if (info_mode == INFO_MODE_MUSIC)
3902     DrawInfoScreen_Music();
3903   else if (info_mode == INFO_MODE_CREDITS)
3904     DrawInfoScreen_Credits();
3905   else if (info_mode == INFO_MODE_PROGRAM)
3906     DrawInfoScreen_Program();
3907   else if (info_mode == INFO_MODE_VERSION)
3908     DrawInfoScreen_Version();
3909   else if (info_mode == INFO_MODE_LEVELSET)
3910     DrawInfoScreen_LevelSet();
3911   else
3912     DrawInfoScreen_Main();
3913
3914   if (info_mode != INFO_MODE_MAIN &&
3915       info_mode != INFO_MODE_TITLE &&
3916       info_mode != INFO_MODE_MUSIC)
3917     PlayMenuSoundsAndMusic();
3918 }
3919
3920 void HandleInfoScreen(int mx, int my, int dx, int dy, int button)
3921 {
3922   if (info_mode == INFO_MODE_TITLE)
3923     HandleInfoScreen_TitleScreen(button);
3924   else if (info_mode == INFO_MODE_ELEMENTS)
3925     HandleInfoScreen_Elements(button);
3926   else if (info_mode == INFO_MODE_MUSIC)
3927     HandleInfoScreen_Music(button);
3928   else if (info_mode == INFO_MODE_CREDITS)
3929     HandleInfoScreen_Credits(button);
3930   else if (info_mode == INFO_MODE_PROGRAM)
3931     HandleInfoScreen_Program(button);
3932   else if (info_mode == INFO_MODE_VERSION)
3933     HandleInfoScreen_Version(button);
3934   else if (info_mode == INFO_MODE_LEVELSET)
3935     HandleInfoScreen_LevelSet(button);
3936   else
3937     HandleInfoScreen_Main(mx, my, dx, dy, button);
3938 }
3939
3940
3941 // ============================================================================
3942 // type name functions
3943 // ============================================================================
3944
3945 void HandleTypeName(int newxpos, Key key)
3946 {
3947   static char last_player_name[MAX_PLAYER_NAME_LEN + 1];
3948   struct MainControlInfo *mci = getMainControlInfo(MAIN_CONTROL_NAME);
3949   struct TextPosInfo *pos = mci->pos_input;
3950   int startx = mSX + ALIGNED_TEXT_XPOS(pos);
3951   int starty = mSY + ALIGNED_TEXT_YPOS(pos);
3952   static int xpos = 0;
3953   int font_nr = pos->font;
3954   int font_active_nr = FONT_ACTIVE(font_nr);
3955   int font_width = getFontWidth(font_active_nr);
3956   char key_char = getValidConfigValueChar(getCharFromKey(key));
3957   boolean is_valid_key_char = (key_char != 0 && (key_char != ' ' || xpos > 0));
3958   boolean is_active = TRUE;
3959
3960   DrawBackgroundForFont(startx,starty, pos->width, pos->height, font_active_nr);
3961
3962   if (newxpos)
3963   {
3964     strcpy(last_player_name, setup.player_name);
3965
3966     xpos = newxpos;
3967
3968     StartTextInput(startx, starty, pos->width, pos->height);
3969   }
3970   else if (is_valid_key_char && xpos < MAX_PLAYER_NAME_LEN)
3971   {
3972     setup.player_name[xpos] = key_char;
3973     setup.player_name[xpos + 1] = 0;
3974
3975     xpos++;
3976   }
3977   else if ((key == KSYM_Delete || key == KSYM_BackSpace) && xpos > 0)
3978   {
3979     xpos--;
3980
3981     setup.player_name[xpos] = 0;
3982   }
3983   else if (key == KSYM_Return && xpos > 0)
3984   {
3985     SaveSetup();
3986
3987     is_active = FALSE;
3988
3989     SetGameStatus(GAME_MODE_MAIN);
3990   }
3991   else if (key == KSYM_Escape)
3992   {
3993     strcpy(setup.player_name, last_player_name);
3994
3995     is_active = FALSE;
3996
3997     SetGameStatus(GAME_MODE_MAIN);
3998   }
3999
4000   if (is_active)
4001   {
4002     pos->width = (strlen(setup.player_name) + 1) * font_width;
4003     startx = mSX + ALIGNED_TEXT_XPOS(pos);
4004
4005     DrawText(startx, starty, setup.player_name, font_active_nr);
4006     DrawText(startx + xpos * font_width, starty, "_", font_active_nr);
4007   }
4008   else
4009   {
4010     pos->width = strlen(setup.player_name) * font_width;
4011     startx = mSX + ALIGNED_TEXT_XPOS(pos);
4012
4013     DrawText(startx, starty, setup.player_name, font_nr);
4014
4015     StopTextInput();
4016   }
4017 }
4018
4019
4020 // ============================================================================
4021 // tree menu functions
4022 // ============================================================================
4023
4024 static int getAlignXOffsetFromTreeInfo(TreeInfo *ti)
4025 {
4026   if (game_status != GAME_MODE_SETUP ||
4027       DRAW_MODE_SETUP(setup_mode) != SETUP_MODE_CHOOSE_OTHER)
4028     return 0;
4029
4030   int max_text_size = 0;
4031   TreeInfo *node;
4032
4033   for (node = getTreeInfoFirstGroupEntry(ti); node != NULL; node = node->next)
4034     max_text_size = MAX(max_text_size, strlen(node->name));
4035
4036   int num_entries = numTreeInfoInGroup(ti);
4037   boolean scrollbar_needed = (num_entries > NUM_MENU_ENTRIES_ON_SCREEN);
4038   int text_width = max_text_size * getFontWidth(FONT_TEXT_1);
4039   int button_width = SC_MENUBUTTON_XSIZE;
4040   int scrollbar_xpos = SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
4041   int screen_width = (scrollbar_needed ? scrollbar_xpos : SXSIZE);
4042   int align = menu.list_setup[SETUP_MODE_CHOOSE_OTHER].align;
4043   int x = ALIGNED_XPOS(0, screen_width, align) * -1;
4044   int align_xoffset_raw = ALIGNED_XPOS(x, button_width + text_width, align);
4045   int align_xoffset = MAX(0, align_xoffset_raw);
4046
4047   return align_xoffset;
4048 }
4049
4050 static int getAlignYOffsetFromTreeInfo(TreeInfo *ti)
4051 {
4052   if (game_status != GAME_MODE_SETUP ||
4053       DRAW_MODE_SETUP(setup_mode) != SETUP_MODE_CHOOSE_OTHER)
4054     return 0;
4055
4056   int num_entries = numTreeInfoInGroup(ti);
4057   int num_page_entries = MIN(num_entries, NUM_MENU_ENTRIES_ON_SCREEN);
4058   int font_height = getFontHeight(FONT_TEXT_1);
4059   int text_height = font_height * num_page_entries;
4060   int page_height = font_height * NUM_MENU_ENTRIES_ON_SCREEN;
4061   int align = menu.list_setup[SETUP_MODE_CHOOSE_OTHER].valign;
4062   int y = ALIGNED_YPOS(0, page_height, align) * -1;
4063   int align_yoffset_raw = ALIGNED_YPOS(y, text_height, align);
4064   int align_yoffset = MAX(0, align_yoffset_raw);
4065
4066   return align_yoffset;
4067 }
4068
4069 static void DrawChooseTree(TreeInfo **ti_ptr)
4070 {
4071   int fade_mask = REDRAW_FIELD;
4072
4073   if (CheckFadeAll())
4074     fade_mask = REDRAW_ALL;
4075
4076   if (strEqual((*ti_ptr)->subdir, STRING_TOP_DIRECTORY))
4077   {
4078     SetGameStatus(GAME_MODE_MAIN);
4079
4080     DrawMainMenu();
4081
4082     return;
4083   }
4084
4085   UnmapAllGadgets();
4086
4087   FreeScreenGadgets();
4088   CreateScreenGadgets();
4089
4090   FadeOut(fade_mask);
4091
4092   // needed if different viewport properties defined for choosing level (set)
4093   ChangeViewportPropertiesIfNeeded();
4094
4095   if (game_status == GAME_MODE_LEVELNR)
4096     SetMainBackgroundImage(IMG_BACKGROUND_LEVELNR);
4097   else if (game_status == GAME_MODE_LEVELS)
4098     SetMainBackgroundImage(IMG_BACKGROUND_LEVELS);
4099
4100   ClearField();
4101
4102   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
4103
4104   HandleChooseTree(0, 0, 0, 0, MB_MENU_INITIALIZE, ti_ptr);
4105   MapScreenTreeGadgets(*ti_ptr);
4106
4107   DrawMaskedBorder(fade_mask);
4108
4109   FadeIn(fade_mask);
4110 }
4111
4112 static void drawChooseTreeList(int first_entry, int num_page_entries,
4113                                TreeInfo *ti)
4114 {
4115   int num_entries = numTreeInfoInGroup(ti);
4116   boolean scrollbar_needed = (num_entries > NUM_MENU_ENTRIES_ON_SCREEN);
4117   int scrollbar_xpos = SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
4118   int screen_width = (scrollbar_needed ? scrollbar_xpos : SXSIZE);
4119   int font_nr = FONT_TEXT_1;
4120   int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
4121   int i;
4122   char *title_string = NULL;
4123   int yoffset_sets = MENU_TITLE1_YPOS;
4124   int yoffset_setup = 16;
4125   int yoffset = (ti->type == TREE_TYPE_LEVEL_DIR ||
4126                  ti->type == TREE_TYPE_LEVEL_NR ? yoffset_sets : yoffset_setup);
4127
4128   title_string = ti->infotext;
4129
4130   DrawTextSCentered(mSY - SY + yoffset, FONT_TITLE_1, title_string);
4131
4132   clearMenuListArea();
4133
4134   for (i = 0; i < num_page_entries; i++)
4135   {
4136     TreeInfo *node, *node_first;
4137     int entry_pos = first_entry + i;
4138     int xpos = MENU_SCREEN_START_XPOS;
4139     int ypos = MENU_SCREEN_START_YPOS + i;
4140     int startx = amSX + xpos * 32;
4141     int starty = amSY + ypos * 32;
4142     int startx_text = startx + font_xoffset;
4143     int endx_text = amSX + screen_width;
4144     int max_text_size = endx_text - startx_text;
4145     int max_buffer_len = max_text_size / getFontWidth(font_nr);
4146     char buffer[max_buffer_len + 1];
4147
4148     node_first = getTreeInfoFirstGroupEntry(ti);
4149     node = getTreeInfoFromPos(node_first, entry_pos);
4150
4151     strncpy(buffer, node->name, max_buffer_len);
4152     buffer[max_buffer_len] = '\0';
4153
4154     DrawText(startx, starty, buffer, font_nr + node->color);
4155
4156     if (node->parent_link)
4157       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
4158     else if (node->level_group)
4159       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
4160     else
4161       initCursor(i, IMG_MENU_BUTTON);
4162   }
4163
4164   redraw_mask |= REDRAW_FIELD;
4165 }
4166
4167 static void drawChooseTreeInfo(int entry_pos, TreeInfo *ti)
4168 {
4169   TreeInfo *node, *node_first;
4170   int x, last_redraw_mask = redraw_mask;
4171   int ypos = MENU_TITLE2_YPOS;
4172   int font_nr = FONT_TITLE_2;
4173
4174   if (ti->type == TREE_TYPE_LEVEL_NR)
4175     DrawTextFCentered(ypos, font_nr, leveldir_current->name);
4176
4177   if (ti->type != TREE_TYPE_LEVEL_DIR)
4178     return;
4179
4180   node_first = getTreeInfoFirstGroupEntry(ti);
4181   node = getTreeInfoFromPos(node_first, entry_pos);
4182
4183   DrawBackgroundForFont(SX, SY + ypos, SXSIZE, getFontHeight(font_nr), font_nr);
4184
4185   if (node->parent_link)
4186     DrawTextFCentered(ypos, font_nr, "leave \"%s\"",
4187                       node->node_parent->name);
4188   else if (node->level_group)
4189     DrawTextFCentered(ypos, font_nr, "enter \"%s\"",
4190                       node->name);
4191   else if (ti->type == TREE_TYPE_LEVEL_DIR)
4192     DrawTextFCentered(ypos, font_nr, "%3d %s (%s)",
4193                       node->levels, (node->levels > 1 ? "levels" : "level"),
4194                       node->class_desc);
4195
4196   // let BackToFront() redraw only what is needed
4197   redraw_mask = last_redraw_mask;
4198   for (x = 0; x < SCR_FIELDX; x++)
4199     MarkTileDirty(x, 1);
4200 }
4201
4202 static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
4203                              TreeInfo **ti_ptr)
4204 {
4205   TreeInfo *ti = *ti_ptr;
4206   int x = 0;
4207   int y = ti->cl_cursor;
4208   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
4209   int num_entries = numTreeInfoInGroup(ti);
4210   int num_page_entries = MIN(num_entries, NUM_MENU_ENTRIES_ON_SCREEN);
4211   boolean position_set_by_scrollbar = (dx == 999);
4212
4213   if (button == MB_MENU_INITIALIZE)
4214   {
4215     int num_entries = numTreeInfoInGroup(ti);
4216     int entry_pos = posTreeInfo(ti);
4217
4218     align_xoffset = getAlignXOffsetFromTreeInfo(ti);
4219     align_yoffset = getAlignYOffsetFromTreeInfo(ti);
4220
4221     if (ti->cl_first == -1)
4222     {
4223       // only on initialization
4224       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
4225       ti->cl_cursor = entry_pos - ti->cl_first;
4226     }
4227     else if (ti->cl_cursor >= num_page_entries ||
4228              (num_entries > num_page_entries &&
4229               num_entries - ti->cl_first < num_page_entries))
4230     {
4231       // only after change of list size (by custom graphic configuration)
4232       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
4233       ti->cl_cursor = entry_pos - ti->cl_first;
4234     }
4235
4236     if (position_set_by_scrollbar)
4237       ti->cl_first = dy;
4238     else
4239       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
4240                                 ti->cl_first, ti);
4241
4242     drawChooseTreeList(ti->cl_first, num_page_entries, ti);
4243     drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
4244     drawChooseTreeCursor(ti->cl_cursor, TRUE);
4245
4246     return;
4247   }
4248   else if (button == MB_MENU_LEAVE)
4249   {
4250     FadeSetLeaveMenu();
4251
4252     PlaySound(SND_MENU_ITEM_SELECTING);
4253
4254     if (ti->node_parent)
4255     {
4256       *ti_ptr = ti->node_parent;
4257       DrawChooseTree(ti_ptr);
4258     }
4259     else if (game_status == GAME_MODE_SETUP)
4260     {
4261       if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
4262           setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY ||
4263           setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
4264         execSetupGame();
4265       else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
4266                setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
4267                setup_mode == SETUP_MODE_CHOOSE_RENDERING ||
4268                setup_mode == SETUP_MODE_CHOOSE_VSYNC)
4269         execSetupGraphics();
4270       else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
4271                setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
4272                setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
4273         execSetupSound();
4274       else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
4275                setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
4276                setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE ||
4277                setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY ||
4278                setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_0 ||
4279                setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_0 ||
4280                setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_1 ||
4281                setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_1)
4282         execSetupTouch();
4283       else
4284         execSetupArtwork();
4285     }
4286     else
4287     {
4288       if (game_status == GAME_MODE_LEVELNR)
4289       {
4290         int new_level_nr = atoi(level_number_current->identifier);
4291
4292         HandleMainMenu_SelectLevel(0, 0, new_level_nr);
4293       }
4294
4295       SetGameStatus(GAME_MODE_MAIN);
4296
4297       DrawMainMenu();
4298     }
4299
4300     return;
4301   }
4302
4303   if (mx || my)         // mouse input
4304   {
4305     x = (mx - amSX) / 32;
4306     y = (my - amSY) / 32 - MENU_SCREEN_START_YPOS;
4307   }
4308   else if (dx || dy)    // keyboard or scrollbar/scrollbutton input
4309   {
4310     // move cursor instead of scrolling when already at start/end of list
4311     if (dy == -1 * SCROLL_LINE && ti->cl_first == 0)
4312       dy = -1;
4313     else if (dy == +1 * SCROLL_LINE &&
4314              ti->cl_first + num_page_entries == num_entries)
4315       dy = 1;
4316
4317     // handle scrolling screen one line or page
4318     if (ti->cl_cursor + dy < 0 ||
4319         ti->cl_cursor + dy > num_page_entries - 1)
4320     {
4321       boolean redraw = FALSE;
4322
4323       if (ABS(dy) == SCROLL_PAGE)
4324         step = num_page_entries - 1;
4325
4326       if (dy < 0 && ti->cl_first > 0)
4327       {
4328         // scroll page/line up
4329
4330         ti->cl_first -= step;
4331         if (ti->cl_first < 0)
4332           ti->cl_first = 0;
4333
4334         redraw = TRUE;
4335       }
4336       else if (dy > 0 && ti->cl_first + num_page_entries < num_entries)
4337       {
4338         // scroll page/line down
4339
4340         ti->cl_first += step;
4341         if (ti->cl_first + num_page_entries > num_entries)
4342           ti->cl_first = MAX(0, num_entries - num_page_entries);
4343
4344         redraw = TRUE;
4345       }
4346
4347       if (redraw)
4348       {
4349         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
4350         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
4351         drawChooseTreeCursor(ti->cl_cursor, TRUE);
4352
4353         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
4354                                   ti->cl_first, ti);
4355       }
4356
4357       return;
4358     }
4359
4360     // handle moving cursor one line
4361     y = ti->cl_cursor + dy;
4362   }
4363
4364   if (dx == 1)
4365   {
4366     TreeInfo *node_first, *node_cursor;
4367     int entry_pos = ti->cl_first + y;
4368
4369     node_first = getTreeInfoFirstGroupEntry(ti);
4370     node_cursor = getTreeInfoFromPos(node_first, entry_pos);
4371
4372     if (node_cursor->node_group)
4373     {
4374       FadeSetEnterMenu();
4375
4376       PlaySound(SND_MENU_ITEM_SELECTING);
4377
4378       node_cursor->cl_first = ti->cl_first;
4379       node_cursor->cl_cursor = ti->cl_cursor;
4380       *ti_ptr = node_cursor->node_group;
4381       DrawChooseTree(ti_ptr);
4382
4383       return;
4384     }
4385   }
4386   else if (dx == -1 && ti->node_parent)
4387   {
4388     FadeSetLeaveMenu();
4389
4390     PlaySound(SND_MENU_ITEM_SELECTING);
4391
4392     *ti_ptr = ti->node_parent;
4393     DrawChooseTree(ti_ptr);
4394
4395     return;
4396   }
4397
4398   if (!anyScrollbarGadgetActive() &&
4399       IN_VIS_MENU(x, y) &&
4400       mx < screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x &&
4401       y >= 0 && y < num_page_entries)
4402   {
4403     if (button)
4404     {
4405       if (y != ti->cl_cursor)
4406       {
4407         PlaySound(SND_MENU_ITEM_ACTIVATING);
4408
4409         drawChooseTreeCursor(ti->cl_cursor, FALSE);
4410         drawChooseTreeCursor(y, TRUE);
4411         drawChooseTreeInfo(ti->cl_first + y, ti);
4412
4413         ti->cl_cursor = y;
4414       }
4415       else if (dx < 0)
4416       {
4417         if (game_status == GAME_MODE_SETUP)
4418         {
4419           if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
4420               setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY ||
4421               setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
4422             execSetupGame();
4423           else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
4424                    setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
4425                    setup_mode == SETUP_MODE_CHOOSE_RENDERING ||
4426                    setup_mode == SETUP_MODE_CHOOSE_VSYNC)
4427             execSetupGraphics();
4428           else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
4429                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
4430                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
4431             execSetupSound();
4432           else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
4433                    setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
4434                    setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE ||
4435                    setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY ||
4436                    setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_0 ||
4437                    setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_0 ||
4438                    setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_1 ||
4439                    setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_1)
4440             execSetupTouch();
4441           else
4442             execSetupArtwork();
4443         }
4444       }
4445     }
4446     else
4447     {
4448       TreeInfo *node_first, *node_cursor;
4449       int entry_pos = ti->cl_first + y;
4450
4451       PlaySound(SND_MENU_ITEM_SELECTING);
4452
4453       node_first = getTreeInfoFirstGroupEntry(ti);
4454       node_cursor = getTreeInfoFromPos(node_first, entry_pos);
4455
4456       if (node_cursor->node_group)
4457       {
4458         FadeSetEnterMenu();
4459
4460         node_cursor->cl_first = ti->cl_first;
4461         node_cursor->cl_cursor = ti->cl_cursor;
4462         *ti_ptr = node_cursor->node_group;
4463         DrawChooseTree(ti_ptr);
4464       }
4465       else if (node_cursor->parent_link)
4466       {
4467         FadeSetLeaveMenu();
4468
4469         *ti_ptr = node_cursor->node_parent;
4470         DrawChooseTree(ti_ptr);
4471       }
4472       else
4473       {
4474         FadeSetEnterMenu();
4475
4476         node_cursor->cl_first = ti->cl_first;
4477         node_cursor->cl_cursor = ti->cl_cursor;
4478         *ti_ptr = node_cursor;
4479
4480         if (ti->type == TREE_TYPE_LEVEL_DIR)
4481         {
4482           LoadLevelSetup_SeriesInfo();
4483
4484           SaveLevelSetup_LastSeries();
4485           SaveLevelSetup_SeriesInfo();
4486           TapeErase();
4487         }
4488
4489         if (game_status == GAME_MODE_SETUP)
4490         {
4491           if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
4492               setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY ||
4493               setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
4494             execSetupGame();
4495           else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
4496                    setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
4497                    setup_mode == SETUP_MODE_CHOOSE_RENDERING ||
4498                    setup_mode == SETUP_MODE_CHOOSE_VSYNC)
4499             execSetupGraphics();
4500           else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
4501                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
4502                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
4503             execSetupSound();
4504           else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
4505                    setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
4506                    setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE ||
4507                    setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY ||
4508                    setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_0 ||
4509                    setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_0 ||
4510                    setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_1 ||
4511                    setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_1)
4512             execSetupTouch();
4513           else
4514             execSetupArtwork();
4515         }
4516         else
4517         {
4518           if (game_status == GAME_MODE_LEVELNR)
4519           {
4520             int new_level_nr = atoi(level_number_current->identifier);
4521
4522             HandleMainMenu_SelectLevel(0, 0, new_level_nr);
4523           }
4524
4525           SetGameStatus(GAME_MODE_MAIN);
4526
4527           DrawMainMenu();
4528         }
4529       }
4530     }
4531   }
4532 }
4533
4534 void DrawChooseLevelSet(void)
4535 {
4536   FadeMenuSoundsAndMusic();
4537
4538   DrawChooseTree(&leveldir_current);
4539
4540   PlayMenuSoundsAndMusic();
4541 }
4542
4543 void HandleChooseLevelSet(int mx, int my, int dx, int dy, int button)
4544 {
4545   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
4546 }
4547
4548 void DrawChooseLevelNr(void)
4549 {
4550   int i;
4551
4552   FadeMenuSoundsAndMusic();
4553
4554   if (level_number != NULL)
4555   {
4556     freeTreeInfo(level_number);
4557
4558     level_number = NULL;
4559   }
4560
4561   for (i = leveldir_current->first_level; i <= leveldir_current->last_level;i++)
4562   {
4563     TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_LEVEL_NR);
4564     char identifier[32], name[64];
4565     int value = i;
4566
4567     // temporarily load level info to get level name
4568     LoadLevelInfoOnly(i);
4569
4570     ti->node_top = &level_number;
4571     ti->sort_priority = 10000 + value;
4572     ti->color = (level.no_level_file ? FC_BLUE :
4573                  LevelStats_getSolved(i) ? FC_GREEN :
4574                  LevelStats_getPlayed(i) ? FC_YELLOW : FC_RED);
4575
4576     snprintf(identifier, sizeof(identifier), "%d", value);
4577     snprintf(name, sizeof(name), "%03d: %s", value,
4578              (level.no_level_file ? "(no file)" : level.name));
4579
4580     setString(&ti->identifier, identifier);
4581     setString(&ti->name, name);
4582     setString(&ti->name_sorting, name);
4583
4584     pushTreeInfo(&level_number, ti);
4585   }
4586
4587   // sort level number values to start with lowest level number
4588   sortTreeInfo(&level_number);
4589
4590   // set current level number to current level number
4591   level_number_current =
4592     getTreeInfoFromIdentifier(level_number, i_to_a(level_nr));
4593
4594   // if that also fails, set current level number to first available level
4595   if (level_number_current == NULL)
4596     level_number_current = level_number;
4597
4598   DrawChooseTree(&level_number_current);
4599
4600   PlayMenuSoundsAndMusic();
4601 }
4602
4603 void HandleChooseLevelNr(int mx, int my, int dx, int dy, int button)
4604 {
4605   HandleChooseTree(mx, my, dx, dy, button, &level_number_current);
4606 }
4607
4608 void DrawHallOfFame(int level_nr, int highlight_position)
4609 {
4610   int fade_mask = REDRAW_FIELD;
4611
4612   if (CheckFadeAll())
4613     fade_mask = REDRAW_ALL;
4614
4615   UnmapAllGadgets();
4616   FadeMenuSoundsAndMusic();
4617
4618   // (this is needed when called from GameEnd() after winning a game)
4619   KeyboardAutoRepeatOn();
4620
4621   // (this is needed when called from GameEnd() after winning a game)
4622   SetDrawDeactivationMask(REDRAW_NONE);
4623   SetDrawBackgroundMask(REDRAW_FIELD);
4624
4625   if (highlight_position < 0) 
4626     LoadScore(level_nr);
4627   else
4628     SetAnimStatus(GAME_MODE_PSEUDO_SCORESNEW);
4629
4630   FadeSetEnterScreen();
4631
4632   FadeOut(fade_mask);
4633
4634   // needed if different viewport properties defined for scores
4635   ChangeViewportPropertiesIfNeeded();
4636
4637   PlayMenuSoundsAndMusic();
4638
4639   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
4640
4641   HandleHallOfFame(level_nr, highlight_position, 0, 0, MB_MENU_INITIALIZE);
4642
4643   DrawMaskedBorder(fade_mask);
4644
4645   FadeIn(fade_mask);
4646 }
4647
4648 static void drawHallOfFameList(int level_nr, int first_entry,
4649                                int highlight_position)
4650 {
4651   int i, j;
4652
4653   SetMainBackgroundImage(IMG_BACKGROUND_SCORES);
4654   ClearField();
4655
4656   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, "Hall Of Fame");
4657   DrawTextFCentered(MENU_TITLE2_YPOS, FONT_TITLE_2,
4658                     "HighScores of Level %d", level_nr);
4659
4660   for (i = 0; i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
4661   {
4662     int entry = first_entry + i;
4663     boolean active = (entry == highlight_position);
4664     int font_nr1 = (active ? FONT_TEXT_1_ACTIVE : FONT_TEXT_1);
4665     int font_nr2 = (active ? FONT_TEXT_2_ACTIVE : FONT_TEXT_2);
4666     int font_nr3 = (active ? FONT_TEXT_3_ACTIVE : FONT_TEXT_3);
4667     int font_nr4 = (active ? FONT_TEXT_4_ACTIVE : FONT_TEXT_4);
4668     int dxoff = getFontDrawOffsetX(font_nr1);
4669     int dx1 = 3 * getFontWidth(font_nr1);
4670     int dx2 = dx1 + getFontWidth(font_nr1);
4671     int dx3 = SXSIZE - 2 * (mSX - SX + dxoff) - 5 * getFontWidth(font_nr4);
4672     int num_dots = (dx3 - dx2) / getFontWidth(font_nr3);
4673     int sy = mSY + 64 + i * 32;
4674
4675     DrawText(mSX, sy, int2str(entry + 1, 3), font_nr1);
4676     DrawText(mSX + dx1, sy, ".", font_nr1);
4677
4678     for (j = 0; j < num_dots; j++)
4679       DrawText(mSX + dx2 + j * getFontWidth(font_nr3), sy, ".", font_nr3);
4680
4681     if (!strEqual(highscore[entry].Name, EMPTY_PLAYER_NAME))
4682       DrawText(mSX + dx2, sy, highscore[entry].Name, font_nr2);
4683
4684     DrawText(mSX + dx3, sy, int2str(highscore[entry].Score, 5), font_nr4);
4685   }
4686
4687   redraw_mask |= REDRAW_FIELD;
4688 }
4689
4690 void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
4691 {
4692   static int level_nr = 0;
4693   static int first_entry = 0;
4694   static int highlight_position = 0;
4695   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
4696
4697   if (button == MB_MENU_INITIALIZE)
4698   {
4699     level_nr = mx;
4700     first_entry = 0;
4701     highlight_position = my;
4702
4703     drawHallOfFameList(level_nr, first_entry, highlight_position);
4704
4705     return;
4706   }
4707
4708   if (ABS(dy) == SCROLL_PAGE)           // handle scrolling one page
4709     step = NUM_MENU_ENTRIES_ON_SCREEN - 1;
4710
4711   if (dy < 0)
4712   {
4713     if (first_entry > 0)
4714     {
4715       first_entry -= step;
4716       if (first_entry < 0)
4717         first_entry = 0;
4718
4719       drawHallOfFameList(level_nr, first_entry, highlight_position);
4720     }
4721   }
4722   else if (dy > 0)
4723   {
4724     if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
4725     {
4726       first_entry += step;
4727       if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
4728         first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
4729
4730       drawHallOfFameList(level_nr, first_entry, highlight_position);
4731     }
4732   }
4733   else if (button == MB_MENU_LEAVE || button == MB_MENU_CHOICE)
4734   {
4735     PlaySound(SND_MENU_ITEM_SELECTING);
4736
4737     FadeSound(SND_BACKGROUND_SCORES);
4738
4739     if (button == MB_MENU_CHOICE &&
4740         game_status_last_screen == GAME_MODE_PLAYING &&
4741         setup.auto_play_next_level && setup.increment_levels &&
4742         level_nr < leveldir_current->last_level &&
4743         !network_playing)
4744     {
4745       StartGameActions(network.enabled, setup.autorecord, level.random_seed);
4746     }
4747     else
4748     {
4749       SetGameStatus(GAME_MODE_MAIN);
4750
4751       DrawMainMenu();
4752     }
4753   }
4754
4755   if (game_status == GAME_MODE_SCORES)
4756     PlayMenuSoundIfLoop();
4757 }
4758
4759
4760 // ============================================================================
4761 // setup screen functions
4762 // ============================================================================
4763
4764 static struct TokenInfo *setup_info;
4765 static int num_setup_info;      // number of setup entries shown on screen
4766 static int max_setup_info;      // total number of setup entries in list
4767
4768 static char *window_size_text;
4769 static char *scaling_type_text;
4770 static char *rendering_mode_text;
4771 static char *vsync_mode_text;
4772 static char *scroll_delay_text;
4773 static char *snapshot_mode_text;
4774 static char *game_speed_text;
4775 static char *network_server_text;
4776 static char *graphics_set_name;
4777 static char *sounds_set_name;
4778 static char *music_set_name;
4779 static char *volume_simple_text;
4780 static char *volume_loops_text;
4781 static char *volume_music_text;
4782 static char *touch_controls_text;
4783 static char *move_distance_text;
4784 static char *drop_distance_text;
4785 static char *transparency_text;
4786 static char *grid_size_text[2][2];
4787
4788 static void execSetupMain(void)
4789 {
4790   setup_mode = SETUP_MODE_MAIN;
4791
4792   DrawSetupScreen();
4793 }
4794
4795 static void execSetupGame_setGameSpeeds(boolean update_value)
4796 {
4797   if (setup.game_speed_extended)
4798   {
4799     game_speeds_list = game_speeds_list_extended;
4800     game_speeds      = game_speeds_extended;
4801   }
4802   else
4803   {
4804     game_speeds_list = game_speeds_list_normal;
4805     game_speeds      = game_speeds_normal;
4806   }
4807
4808   if (game_speeds == NULL)
4809   {
4810     int i;
4811
4812     for (i = 0; game_speeds_list[i].value != -1; i++)
4813     {
4814       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4815       char identifier[32], name[32];
4816       int value = game_speeds_list[i].value;
4817       char *text = game_speeds_list[i].text;
4818
4819       ti->node_top = &game_speeds;
4820       ti->sort_priority = 10000 - value;
4821
4822       sprintf(identifier, "%d", value);
4823       sprintf(name, "%s", text);
4824
4825       setString(&ti->identifier, identifier);
4826       setString(&ti->name, name);
4827       setString(&ti->name_sorting, name);
4828       setString(&ti->infotext, STR_SETUP_CHOOSE_GAME_SPEED);
4829
4830       pushTreeInfo(&game_speeds, ti);
4831     }
4832
4833     // sort game speed values to start with slowest game speed
4834     sortTreeInfo(&game_speeds);
4835
4836     update_value = TRUE;
4837   }
4838
4839   if (update_value)
4840   {
4841     // set current game speed to configured game speed value
4842     game_speed_current =
4843       getTreeInfoFromIdentifier(game_speeds, i_to_a(setup.game_frame_delay));
4844
4845     // if that fails, set current game speed to reliable default value
4846     if (game_speed_current == NULL)
4847       game_speed_current =
4848         getTreeInfoFromIdentifier(game_speeds, i_to_a(GAME_FRAME_DELAY));
4849
4850     // if that also fails, set current game speed to first available speed
4851     if (game_speed_current == NULL)
4852       game_speed_current = game_speeds;
4853
4854     if (setup.game_speed_extended)
4855       game_speeds_extended = game_speeds;
4856     else
4857       game_speeds_normal = game_speeds;
4858   }
4859
4860   setup.game_frame_delay = atoi(game_speed_current->identifier);
4861
4862   // needed for displaying game speed text instead of identifier
4863   game_speed_text = game_speed_current->name;
4864 }
4865
4866 static void execSetupGame_setScrollDelays(void)
4867 {
4868   if (scroll_delays == NULL)
4869   {
4870     int i;
4871
4872     for (i = 0; scroll_delays_list[i].value != -1; i++)
4873     {
4874       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4875       char identifier[32], name[32];
4876       int value = scroll_delays_list[i].value;
4877       char *text = scroll_delays_list[i].text;
4878
4879       ti->node_top = &scroll_delays;
4880       ti->sort_priority = value;
4881
4882       sprintf(identifier, "%d", value);
4883       sprintf(name, "%s", text);
4884
4885       setString(&ti->identifier, identifier);
4886       setString(&ti->name, name);
4887       setString(&ti->name_sorting, name);
4888       setString(&ti->infotext, STR_SETUP_CHOOSE_SCROLL_DELAY);
4889
4890       pushTreeInfo(&scroll_delays, ti);
4891     }
4892
4893     // sort scroll delay values to start with lowest scroll delay value
4894     sortTreeInfo(&scroll_delays);
4895
4896     // set current scroll delay value to configured scroll delay value
4897     scroll_delay_current =
4898       getTreeInfoFromIdentifier(scroll_delays,i_to_a(setup.scroll_delay_value));
4899
4900     // if that fails, set current scroll delay to reliable default value
4901     if (scroll_delay_current == NULL)
4902       scroll_delay_current =
4903         getTreeInfoFromIdentifier(scroll_delays, i_to_a(STD_SCROLL_DELAY));
4904
4905     // if that also fails, set current scroll delay to first available value
4906     if (scroll_delay_current == NULL)
4907       scroll_delay_current = scroll_delays;
4908   }
4909
4910   setup.scroll_delay_value = atoi(scroll_delay_current->identifier);
4911
4912   // needed for displaying scroll delay text instead of identifier
4913   scroll_delay_text = scroll_delay_current->name;
4914 }
4915
4916 static void execSetupGame_setSnapshotModes(void)
4917 {
4918   if (snapshot_modes == NULL)
4919   {
4920     int i;
4921
4922     for (i = 0; snapshot_modes_list[i].value != NULL; i++)
4923     {
4924       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4925       char identifier[32], name[32];
4926       char *value = snapshot_modes_list[i].value;
4927       char *text = snapshot_modes_list[i].text;
4928
4929       ti->node_top = &snapshot_modes;
4930       ti->sort_priority = i;
4931
4932       sprintf(identifier, "%s", value);
4933       sprintf(name, "%s", text);
4934
4935       setString(&ti->identifier, identifier);
4936       setString(&ti->name, name);
4937       setString(&ti->name_sorting, name);
4938       setString(&ti->infotext, STR_SETUP_CHOOSE_SNAPSHOT_MODE);
4939
4940       pushTreeInfo(&snapshot_modes, ti);
4941     }
4942
4943     // sort snapshot mode values to start with lowest snapshot mode value
4944     sortTreeInfo(&snapshot_modes);
4945
4946     // set current snapshot mode value to configured snapshot mode value
4947     snapshot_mode_current =
4948       getTreeInfoFromIdentifier(snapshot_modes, setup.engine_snapshot_mode);
4949
4950     // if that fails, set current snapshot mode to reliable default value
4951     if (snapshot_mode_current == NULL)
4952       snapshot_mode_current =
4953         getTreeInfoFromIdentifier(snapshot_modes, STR_SNAPSHOT_MODE_DEFAULT);
4954
4955     // if that also fails, set current snapshot mode to first available value
4956     if (snapshot_mode_current == NULL)
4957       snapshot_mode_current = snapshot_modes;
4958   }
4959
4960   setup.engine_snapshot_mode = snapshot_mode_current->identifier;
4961
4962   // needed for displaying snapshot mode text instead of identifier
4963   snapshot_mode_text = snapshot_mode_current->name;
4964 }
4965
4966 static void execSetupGame_setNetworkServerText(void)
4967 {
4968   if (strEqual(setup.network_server_hostname, STR_NETWORK_AUTO_DETECT))
4969   {
4970     strcpy(network_server_hostname, STR_NETWORK_AUTO_DETECT_SETUP);
4971   }
4972   else
4973   {
4974     strncpy(network_server_hostname, setup.network_server_hostname,
4975             MAX_SETUP_TEXT_INPUT_LEN);
4976     network_server_hostname[MAX_SETUP_TEXT_INPUT_LEN] = '\0';
4977   }
4978
4979   // needed for displaying network server text instead of identifier
4980   network_server_text = network_server_hostname;
4981 }
4982
4983 static void execSetupGame(void)
4984 {
4985   boolean check_vsync_mode = (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED);
4986
4987   execSetupGame_setGameSpeeds(FALSE);
4988   execSetupGame_setScrollDelays();
4989   execSetupGame_setSnapshotModes();
4990
4991   execSetupGame_setNetworkServerText();
4992
4993   setup_mode = SETUP_MODE_GAME;
4994
4995   DrawSetupScreen();
4996
4997   // check if vsync needs to be disabled for this game speed to work
4998   if (check_vsync_mode)
4999     DisableVsyncIfNeeded();
5000 }
5001
5002 static void execSetupChooseGameSpeed(void)
5003 {
5004   setup_mode = SETUP_MODE_CHOOSE_GAME_SPEED;
5005
5006   DrawSetupScreen();
5007 }
5008
5009 static void execSetupChooseScrollDelay(void)
5010 {
5011   setup_mode = SETUP_MODE_CHOOSE_SCROLL_DELAY;
5012
5013   DrawSetupScreen();
5014 }
5015
5016 static void execSetupChooseSnapshotMode(void)
5017 {
5018   setup_mode = SETUP_MODE_CHOOSE_SNAPSHOT_MODE;
5019
5020   DrawSetupScreen();
5021 }
5022
5023 static void execSetupEngines(void)
5024 {
5025   setup_mode = SETUP_MODE_ENGINES;
5026
5027   DrawSetupScreen();
5028 }
5029
5030 static void execSetupEditor(void)
5031 {
5032   setup_mode = SETUP_MODE_EDITOR;
5033
5034   DrawSetupScreen();
5035 }
5036
5037 static void execSetupGraphics_setWindowSizes(boolean update_list)
5038 {
5039   if (window_sizes != NULL && update_list)
5040   {
5041     freeTreeInfo(window_sizes);
5042
5043     window_sizes = NULL;
5044   }
5045
5046   if (window_sizes == NULL)
5047   {
5048     boolean current_window_size_found = FALSE;
5049     int i;
5050
5051     for (i = 0; window_sizes_list[i].value != -1; i++)
5052     {
5053       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5054       char identifier[32], name[32];
5055       int value = window_sizes_list[i].value;
5056       char *text = window_sizes_list[i].text;
5057
5058       ti->node_top = &window_sizes;
5059       ti->sort_priority = value;
5060
5061       sprintf(identifier, "%d", value);
5062       sprintf(name, "%s", text);
5063
5064       setString(&ti->identifier, identifier);
5065       setString(&ti->name, name);
5066       setString(&ti->name_sorting, name);
5067       setString(&ti->infotext, STR_SETUP_CHOOSE_WINDOW_SIZE);
5068
5069       pushTreeInfo(&window_sizes, ti);
5070
5071       if (value == setup.window_scaling_percent)
5072         current_window_size_found = TRUE;
5073     }
5074
5075     if (!current_window_size_found)
5076     {
5077       // add entry for non-preset window scaling value
5078
5079       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5080       char identifier[32], name[32];
5081       int value = setup.window_scaling_percent;
5082
5083       ti->node_top = &window_sizes;
5084       ti->sort_priority = value;
5085
5086       sprintf(identifier, "%d", value);
5087       sprintf(name, "%d %% (Current)", value);
5088
5089       setString(&ti->identifier, identifier);
5090       setString(&ti->name, name);
5091       setString(&ti->name_sorting, name);
5092       setString(&ti->infotext, STR_SETUP_CHOOSE_WINDOW_SIZE);
5093
5094       pushTreeInfo(&window_sizes, ti);
5095     }
5096
5097     // sort window size values to start with lowest window size value
5098     sortTreeInfo(&window_sizes);
5099
5100     // set current window size value to configured window size value
5101     window_size_current =
5102       getTreeInfoFromIdentifier(window_sizes,
5103                                 i_to_a(setup.window_scaling_percent));
5104
5105     // if that fails, set current window size to reliable default value
5106     if (window_size_current == NULL)
5107       window_size_current =
5108         getTreeInfoFromIdentifier(window_sizes,
5109                                   i_to_a(STD_WINDOW_SCALING_PERCENT));
5110
5111     // if that also fails, set current window size to first available value
5112     if (window_size_current == NULL)
5113       window_size_current = window_sizes;
5114   }
5115
5116   setup.window_scaling_percent = atoi(window_size_current->identifier);
5117
5118   // needed for displaying window size text instead of identifier
5119   window_size_text = window_size_current->name;
5120 }
5121
5122 static void execSetupGraphics_setScalingTypes(void)
5123 {
5124   if (scaling_types == NULL)
5125   {
5126     int i;
5127
5128     for (i = 0; scaling_types_list[i].value != NULL; i++)
5129     {
5130       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5131       char identifier[32], name[32];
5132       char *value = scaling_types_list[i].value;
5133       char *text = scaling_types_list[i].text;
5134
5135       ti->node_top = &scaling_types;
5136       ti->sort_priority = i;
5137
5138       sprintf(identifier, "%s", value);
5139       sprintf(name, "%s", text);
5140
5141       setString(&ti->identifier, identifier);
5142       setString(&ti->name, name);
5143       setString(&ti->name_sorting, name);
5144       setString(&ti->infotext, STR_SETUP_CHOOSE_SCALING_TYPE);
5145
5146       pushTreeInfo(&scaling_types, ti);
5147     }
5148
5149     // sort scaling type values to start with lowest scaling type value
5150     sortTreeInfo(&scaling_types);
5151
5152     // set current scaling type value to configured scaling type value
5153     scaling_type_current =
5154       getTreeInfoFromIdentifier(scaling_types, setup.window_scaling_quality);
5155
5156     // if that fails, set current scaling type to reliable default value
5157     if (scaling_type_current == NULL)
5158       scaling_type_current =
5159         getTreeInfoFromIdentifier(scaling_types, SCALING_QUALITY_DEFAULT);
5160
5161     // if that also fails, set current scaling type to first available value
5162     if (scaling_type_current == NULL)
5163       scaling_type_current = scaling_types;
5164   }
5165
5166   setup.window_scaling_quality = scaling_type_current->identifier;
5167
5168   // needed for displaying scaling type text instead of identifier
5169   scaling_type_text = scaling_type_current->name;
5170 }
5171
5172 static void execSetupGraphics_setRenderingModes(void)
5173 {
5174   if (rendering_modes == NULL)
5175   {
5176     int i;
5177
5178     for (i = 0; rendering_modes_list[i].value != NULL; i++)
5179     {
5180       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5181       char identifier[32], name[32];
5182       char *value = rendering_modes_list[i].value;
5183       char *text = rendering_modes_list[i].text;
5184
5185       ti->node_top = &rendering_modes;
5186       ti->sort_priority = i;
5187
5188       sprintf(identifier, "%s", value);
5189       sprintf(name, "%s", text);
5190
5191       setString(&ti->identifier, identifier);
5192       setString(&ti->name, name);
5193       setString(&ti->name_sorting, name);
5194       setString(&ti->infotext, STR_SETUP_CHOOSE_RENDERING);
5195
5196       pushTreeInfo(&rendering_modes, ti);
5197     }
5198
5199     // sort rendering mode values to start with lowest rendering mode value
5200     sortTreeInfo(&rendering_modes);
5201
5202     // set current rendering mode value to configured rendering mode value
5203     rendering_mode_current =
5204       getTreeInfoFromIdentifier(rendering_modes, setup.screen_rendering_mode);
5205
5206     // if that fails, set current rendering mode to reliable default value
5207     if (rendering_mode_current == NULL)
5208       rendering_mode_current =
5209         getTreeInfoFromIdentifier(rendering_modes,
5210                                   STR_SPECIAL_RENDERING_DEFAULT);
5211
5212     // if that also fails, set current rendering mode to first available one
5213     if (rendering_mode_current == NULL)
5214       rendering_mode_current = rendering_modes;
5215   }
5216
5217   setup.screen_rendering_mode = rendering_mode_current->identifier;
5218
5219   // needed for displaying rendering mode text instead of identifier
5220   rendering_mode_text = rendering_mode_current->name;
5221 }
5222
5223 static void execSetupGraphics_setVsyncModes(boolean update_value)
5224 {
5225   if (vsync_modes == NULL)
5226   {
5227     int i;
5228
5229     for (i = 0; vsync_modes_list[i].value != NULL; i++)
5230     {
5231       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5232       char identifier[32], name[32];
5233       char *value = vsync_modes_list[i].value;
5234       char *text = vsync_modes_list[i].text;
5235
5236       ti->node_top = &vsync_modes;
5237       ti->sort_priority = i;
5238
5239       sprintf(identifier, "%s", value);
5240       sprintf(name, "%s", text);
5241
5242       setString(&ti->identifier, identifier);
5243       setString(&ti->name, name);
5244       setString(&ti->name_sorting, name);
5245       setString(&ti->infotext, STR_SETUP_CHOOSE_VSYNC);
5246
5247       pushTreeInfo(&vsync_modes, ti);
5248     }
5249
5250     // sort vsync mode values to start with lowest vsync mode value
5251     sortTreeInfo(&vsync_modes);
5252
5253     update_value = TRUE;
5254   }
5255
5256   if (update_value)
5257   {
5258     // set current vsync mode value to configured vsync mode value
5259     vsync_mode_current =
5260       getTreeInfoFromIdentifier(vsync_modes, setup.vsync_mode);
5261
5262     // if that fails, set current vsync mode to reliable default value
5263     if (vsync_mode_current == NULL)
5264       vsync_mode_current =
5265         getTreeInfoFromIdentifier(vsync_modes, STR_VSYNC_MODE_DEFAULT);
5266
5267     // if that also fails, set current vsync mode to first available one
5268     if (vsync_mode_current == NULL)
5269       vsync_mode_current = vsync_modes;
5270   }
5271
5272   setup.vsync_mode = vsync_mode_current->identifier;
5273
5274   // needed for displaying vsync mode text instead of identifier
5275   vsync_mode_text = vsync_mode_current->name;
5276 }
5277
5278 static void execSetupGraphics(void)
5279 {
5280   boolean check_game_speed = (setup_mode == SETUP_MODE_CHOOSE_VSYNC);
5281
5282   // update "setup.window_scaling_percent" from list selection
5283   // (in this case, window scaling was changed on setup screen)
5284   if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
5285     execSetupGraphics_setWindowSizes(FALSE);
5286
5287   // update list selection from "setup.window_scaling_percent"
5288   // (window scaling may have changed by resizing the window)
5289   execSetupGraphics_setWindowSizes(TRUE);
5290
5291   execSetupGraphics_setScalingTypes();
5292   execSetupGraphics_setRenderingModes();
5293   execSetupGraphics_setVsyncModes(FALSE);
5294
5295   setup_mode = SETUP_MODE_GRAPHICS;
5296
5297   DrawSetupScreen();
5298
5299   // check if game speed is high enough for 60 Hz vsync to work
5300   if (check_game_speed)
5301     ModifyGameSpeedIfNeeded();
5302
5303   // window scaling may have changed at this point
5304   ToggleFullscreenOrChangeWindowScalingIfNeeded();
5305
5306   // window scaling quality may have changed at this point
5307   if (!strEqual(setup.window_scaling_quality, video.window_scaling_quality))
5308     SDLSetWindowScalingQuality(setup.window_scaling_quality);
5309
5310   // screen rendering mode may have changed at this point
5311   SDLSetScreenRenderingMode(setup.screen_rendering_mode);
5312
5313   // screen vsync mode may have changed at this point
5314   SDLSetScreenVsyncMode(setup.vsync_mode);
5315 }
5316
5317 static void execSetupChooseWindowSize(void)
5318 {
5319   setup_mode = SETUP_MODE_CHOOSE_WINDOW_SIZE;
5320
5321   DrawSetupScreen();
5322 }
5323
5324 static void execSetupChooseScalingType(void)
5325 {
5326   setup_mode = SETUP_MODE_CHOOSE_SCALING_TYPE;
5327
5328   DrawSetupScreen();
5329 }
5330
5331 static void execSetupChooseRenderingMode(void)
5332 {
5333   setup_mode = SETUP_MODE_CHOOSE_RENDERING;
5334
5335   DrawSetupScreen();
5336 }
5337
5338 static void execSetupChooseVsyncMode(void)
5339 {
5340   setup_mode = SETUP_MODE_CHOOSE_VSYNC;
5341
5342   DrawSetupScreen();
5343 }
5344
5345 static void execSetupChooseVolumeSimple(void)
5346 {
5347   setup_mode = SETUP_MODE_CHOOSE_VOLUME_SIMPLE;
5348
5349   DrawSetupScreen();
5350 }
5351
5352 static void execSetupChooseVolumeLoops(void)
5353 {
5354   setup_mode = SETUP_MODE_CHOOSE_VOLUME_LOOPS;
5355
5356   DrawSetupScreen();
5357 }
5358
5359 static void execSetupChooseVolumeMusic(void)
5360 {
5361   setup_mode = SETUP_MODE_CHOOSE_VOLUME_MUSIC;
5362
5363   DrawSetupScreen();
5364 }
5365
5366 static void execSetupSound(void)
5367 {
5368   if (volumes_simple == NULL)
5369   {
5370     boolean current_volume_simple_found = FALSE;
5371     int i;
5372
5373     for (i = 0; volumes_list[i].value != -1; i++)
5374     {
5375       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5376       char identifier[32], name[32];
5377       int value = volumes_list[i].value;
5378       char *text = volumes_list[i].text;
5379
5380       ti->node_top = &volumes_simple;
5381       ti->sort_priority = value;
5382
5383       sprintf(identifier, "%d", value);
5384       sprintf(name, "%s", text);
5385
5386       setString(&ti->identifier, identifier);
5387       setString(&ti->name, name);
5388       setString(&ti->name_sorting, name);
5389       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_SIMPLE);
5390
5391       pushTreeInfo(&volumes_simple, ti);
5392
5393       if (value == setup.volume_simple)
5394         current_volume_simple_found = TRUE;
5395     }
5396
5397     if (!current_volume_simple_found)
5398     {
5399       // add entry for non-preset volume value
5400
5401       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5402       char identifier[32], name[32];
5403       int value = setup.volume_simple;
5404
5405       ti->node_top = &volumes_simple;
5406       ti->sort_priority = value;
5407
5408       sprintf(identifier, "%d", value);
5409       sprintf(name, "%d %% (Current)", value);
5410
5411       setString(&ti->identifier, identifier);
5412       setString(&ti->name, name);
5413       setString(&ti->name_sorting, name);
5414       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_SIMPLE);
5415
5416       pushTreeInfo(&volumes_simple, ti);
5417     }
5418
5419     // sort volume values to start with lowest volume value
5420     sortTreeInfo(&volumes_simple);
5421
5422     // set current volume value to configured volume value
5423     volume_simple_current =
5424       getTreeInfoFromIdentifier(volumes_simple,i_to_a(setup.volume_simple));
5425
5426     // if that fails, set current volume to reliable default value
5427     if (volume_simple_current == NULL)
5428       volume_simple_current =
5429         getTreeInfoFromIdentifier(volumes_simple, i_to_a(100));
5430
5431     // if that also fails, set current volume to first available value
5432     if (volume_simple_current == NULL)
5433       volume_simple_current = volumes_simple;
5434   }
5435
5436   if (volumes_loops == NULL)
5437   {
5438     boolean current_volume_loops_found = FALSE;
5439     int i;
5440
5441     for (i = 0; volumes_list[i].value != -1; i++)
5442     {
5443       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5444       char identifier[32], name[32];
5445       int value = volumes_list[i].value;
5446       char *text = volumes_list[i].text;
5447
5448       ti->node_top = &volumes_loops;
5449       ti->sort_priority = value;
5450
5451       sprintf(identifier, "%d", value);
5452       sprintf(name, "%s", text);
5453
5454       setString(&ti->identifier, identifier);
5455       setString(&ti->name, name);
5456       setString(&ti->name_sorting, name);
5457       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_LOOPS);
5458
5459       pushTreeInfo(&volumes_loops, ti);
5460
5461       if (value == setup.volume_loops)
5462         current_volume_loops_found = TRUE;
5463     }
5464
5465     if (!current_volume_loops_found)
5466     {
5467       // add entry for non-preset volume value
5468
5469       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5470       char identifier[32], name[32];
5471       int value = setup.volume_loops;
5472
5473       ti->node_top = &volumes_loops;
5474       ti->sort_priority = value;
5475
5476       sprintf(identifier, "%d", value);
5477       sprintf(name, "%d %% (Current)", value);
5478
5479       setString(&ti->identifier, identifier);
5480       setString(&ti->name, name);
5481       setString(&ti->name_sorting, name);
5482       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_LOOPS);
5483
5484       pushTreeInfo(&volumes_loops, ti);
5485     }
5486
5487     // sort volume values to start with lowest volume value
5488     sortTreeInfo(&volumes_loops);
5489
5490     // set current volume value to configured volume value
5491     volume_loops_current =
5492       getTreeInfoFromIdentifier(volumes_loops,i_to_a(setup.volume_loops));
5493
5494     // if that fails, set current volume to reliable default value
5495     if (volume_loops_current == NULL)
5496       volume_loops_current =
5497         getTreeInfoFromIdentifier(volumes_loops, i_to_a(100));
5498
5499     // if that also fails, set current volume to first available value
5500     if (volume_loops_current == NULL)
5501       volume_loops_current = volumes_loops;
5502   }
5503
5504   if (volumes_music == NULL)
5505   {
5506     boolean current_volume_music_found = FALSE;
5507     int i;
5508
5509     for (i = 0; volumes_list[i].value != -1; i++)
5510     {
5511       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5512       char identifier[32], name[32];
5513       int value = volumes_list[i].value;
5514       char *text = volumes_list[i].text;
5515
5516       ti->node_top = &volumes_music;
5517       ti->sort_priority = value;
5518
5519       sprintf(identifier, "%d", value);
5520       sprintf(name, "%s", text);
5521
5522       setString(&ti->identifier, identifier);
5523       setString(&ti->name, name);
5524       setString(&ti->name_sorting, name);
5525       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_MUSIC);
5526
5527       pushTreeInfo(&volumes_music, ti);
5528
5529       if (value == setup.volume_music)
5530         current_volume_music_found = TRUE;
5531     }
5532
5533     if (!current_volume_music_found)
5534     {
5535       // add entry for non-preset volume value
5536
5537       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5538       char identifier[32], name[32];
5539       int value = setup.volume_music;
5540
5541       ti->node_top = &volumes_music;
5542       ti->sort_priority = value;
5543
5544       sprintf(identifier, "%d", value);
5545       sprintf(name, "%d %% (Current)", value);
5546
5547       setString(&ti->identifier, identifier);
5548       setString(&ti->name, name);
5549       setString(&ti->name_sorting, name);
5550       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_MUSIC);
5551
5552       pushTreeInfo(&volumes_music, ti);
5553     }
5554
5555     // sort volume values to start with lowest volume value
5556     sortTreeInfo(&volumes_music);
5557
5558     // set current volume value to configured volume value
5559     volume_music_current =
5560       getTreeInfoFromIdentifier(volumes_music,i_to_a(setup.volume_music));
5561
5562     // if that fails, set current volume to reliable default value
5563     if (volume_music_current == NULL)
5564       volume_music_current =
5565         getTreeInfoFromIdentifier(volumes_music, i_to_a(100));
5566
5567     // if that also fails, set current volume to first available value
5568     if (volume_music_current == NULL)
5569       volume_music_current = volumes_music;
5570   }
5571
5572   setup.volume_simple = atoi(volume_simple_current->identifier);
5573   setup.volume_loops  = atoi(volume_loops_current->identifier);
5574   setup.volume_music  = atoi(volume_music_current->identifier);
5575
5576   // needed for displaying volume text instead of identifier
5577   volume_simple_text = volume_simple_current->name;
5578   volume_loops_text = volume_loops_current->name;
5579   volume_music_text = volume_music_current->name;
5580
5581   setup_mode = SETUP_MODE_SOUND;
5582
5583   DrawSetupScreen();
5584 }
5585
5586 static void execSetupChooseTouchControls(void)
5587 {
5588   setup_mode = SETUP_MODE_CHOOSE_TOUCH_CONTROL;
5589
5590   DrawSetupScreen();
5591 }
5592
5593 static void execSetupChooseMoveDistance(void)
5594 {
5595   setup_mode = SETUP_MODE_CHOOSE_MOVE_DISTANCE;
5596
5597   DrawSetupScreen();
5598 }
5599
5600 static void execSetupChooseDropDistance(void)
5601 {
5602   setup_mode = SETUP_MODE_CHOOSE_DROP_DISTANCE;
5603
5604   DrawSetupScreen();
5605 }
5606
5607 static void execSetupChooseTransparency(void)
5608 {
5609   setup_mode = SETUP_MODE_CHOOSE_TRANSPARENCY;
5610
5611   DrawSetupScreen();
5612 }
5613
5614 static void execSetupChooseGridXSize_0(void)
5615 {
5616   setup_mode = SETUP_MODE_CHOOSE_GRID_XSIZE_0;
5617
5618   DrawSetupScreen();
5619 }
5620
5621 static void execSetupChooseGridYSize_0(void)
5622 {
5623   setup_mode = SETUP_MODE_CHOOSE_GRID_YSIZE_0;
5624
5625   DrawSetupScreen();
5626 }
5627
5628 static void execSetupChooseGridXSize_1(void)
5629 {
5630   setup_mode = SETUP_MODE_CHOOSE_GRID_XSIZE_1;
5631
5632   DrawSetupScreen();
5633 }
5634
5635 static void execSetupChooseGridYSize_1(void)
5636 {
5637   setup_mode = SETUP_MODE_CHOOSE_GRID_YSIZE_1;
5638
5639   DrawSetupScreen();
5640 }
5641
5642 static void execSetupConfigureVirtualButtons(void)
5643 {
5644   setup_mode = SETUP_MODE_CONFIG_VIRT_BUTTONS;
5645
5646   ConfigureVirtualButtons();
5647
5648   setup_mode = SETUP_MODE_TOUCH;
5649
5650   DrawSetupScreen();
5651 }
5652
5653 static void execSetupTouch(void)
5654 {
5655   int i, j, k;
5656
5657   if (touch_controls == NULL)
5658   {
5659     for (i = 0; touch_controls_list[i].value != NULL; i++)
5660     {
5661       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5662       char identifier[32], name[32];
5663       char *value = touch_controls_list[i].value;
5664       char *text = touch_controls_list[i].text;
5665
5666       ti->node_top = &touch_controls;
5667       ti->sort_priority = i;
5668
5669       sprintf(identifier, "%s", value);
5670       sprintf(name, "%s", text);
5671
5672       setString(&ti->identifier, identifier);
5673       setString(&ti->name, name);
5674       setString(&ti->name_sorting, name);
5675       setString(&ti->infotext, STR_SETUP_CHOOSE_TOUCH_CONTROL);
5676
5677       pushTreeInfo(&touch_controls, ti);
5678     }
5679
5680     // sort touch control values to start with lowest touch control value
5681     sortTreeInfo(&touch_controls);
5682
5683     // set current touch control value to configured touch control value
5684     touch_control_current =
5685       getTreeInfoFromIdentifier(touch_controls, setup.touch.control_type);
5686
5687     // if that fails, set current touch control to reliable default value
5688     if (touch_control_current == NULL)
5689       touch_control_current =
5690         getTreeInfoFromIdentifier(touch_controls, TOUCH_CONTROL_DEFAULT);
5691
5692     // if that also fails, set current touch control to first available value
5693     if (touch_control_current == NULL)
5694       touch_control_current = touch_controls;
5695   }
5696
5697   if (move_distances == NULL)
5698   {
5699     for (i = 0; distances_list[i].value != -1; i++)
5700     {
5701       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5702       char identifier[32], name[32];
5703       int value = distances_list[i].value;
5704       char *text = distances_list[i].text;
5705
5706       ti->node_top = &move_distances;
5707       ti->sort_priority = value;
5708
5709       sprintf(identifier, "%d", value);
5710       sprintf(name, "%s", text);
5711
5712       setString(&ti->identifier, identifier);
5713       setString(&ti->name, name);
5714       setString(&ti->name_sorting, name);
5715       setString(&ti->infotext, STR_SETUP_CHOOSE_MOVE_DISTANCE);
5716
5717       pushTreeInfo(&move_distances, ti);
5718     }
5719
5720     // sort distance values to start with lowest distance value
5721     sortTreeInfo(&move_distances);
5722
5723     // set current distance value to configured distance value
5724     move_distance_current =
5725       getTreeInfoFromIdentifier(move_distances,
5726                                 i_to_a(setup.touch.move_distance));
5727
5728     // if that fails, set current distance to reliable default value
5729     if (move_distance_current == NULL)
5730       move_distance_current =
5731         getTreeInfoFromIdentifier(move_distances,
5732                                   i_to_a(TOUCH_MOVE_DISTANCE_DEFAULT));
5733
5734     // if that also fails, set current distance to first available value
5735     if (move_distance_current == NULL)
5736       move_distance_current = move_distances;
5737   }
5738
5739   if (drop_distances == NULL)
5740   {
5741     for (i = 0; distances_list[i].value != -1; i++)
5742     {
5743       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5744       char identifier[32], name[32];
5745       int value = distances_list[i].value;
5746       char *text = distances_list[i].text;
5747
5748       ti->node_top = &drop_distances;
5749       ti->sort_priority = value;
5750
5751       sprintf(identifier, "%d", value);
5752       sprintf(name, "%s", text);
5753
5754       setString(&ti->identifier, identifier);
5755       setString(&ti->name, name);
5756       setString(&ti->name_sorting, name);
5757       setString(&ti->infotext, STR_SETUP_CHOOSE_DROP_DISTANCE);
5758
5759       pushTreeInfo(&drop_distances, ti);
5760     }
5761
5762     // sort distance values to start with lowest distance value
5763     sortTreeInfo(&drop_distances);
5764
5765     // set current distance value to configured distance value
5766     drop_distance_current =
5767       getTreeInfoFromIdentifier(drop_distances,
5768                                 i_to_a(setup.touch.drop_distance));
5769
5770     // if that fails, set current distance to reliable default value
5771     if (drop_distance_current == NULL)
5772       drop_distance_current =
5773         getTreeInfoFromIdentifier(drop_distances,
5774                                   i_to_a(TOUCH_DROP_DISTANCE_DEFAULT));
5775
5776     // if that also fails, set current distance to first available value
5777     if (drop_distance_current == NULL)
5778       drop_distance_current = drop_distances;
5779   }
5780
5781   if (transparencies == NULL)
5782   {
5783     for (i = 0; transparencies_list[i].value != -1; i++)
5784     {
5785       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5786       char identifier[32], name[32];
5787       int value = transparencies_list[i].value;
5788       char *text = transparencies_list[i].text;
5789
5790       ti->node_top = &transparencies;
5791       ti->sort_priority = value;
5792
5793       sprintf(identifier, "%d", value);
5794       sprintf(name, "%s", text);
5795
5796       setString(&ti->identifier, identifier);
5797       setString(&ti->name, name);
5798       setString(&ti->name_sorting, name);
5799       setString(&ti->infotext, STR_SETUP_CHOOSE_TRANSPARENCY);
5800
5801       pushTreeInfo(&transparencies, ti);
5802     }
5803
5804     // sort transparency values to start with lowest transparency value
5805     sortTreeInfo(&transparencies);
5806
5807     // set current transparency value to configured transparency value
5808     transparency_current =
5809       getTreeInfoFromIdentifier(transparencies,
5810                                 i_to_a(setup.touch.transparency));
5811
5812     // if that fails, set current transparency to reliable default value
5813     if (transparency_current == NULL)
5814       transparency_current =
5815         getTreeInfoFromIdentifier(transparencies,
5816                                   i_to_a(TOUCH_TRANSPARENCY_DEFAULT));
5817
5818     // if that also fails, set current transparency to first available value
5819     if (transparency_current == NULL)
5820       transparency_current = transparencies;
5821   }
5822
5823   for (i = 0; i < 2; i++)
5824   {
5825     for (j = 0; j < 2; j++)
5826     {
5827       if (grid_sizes[i][j] == NULL)
5828       {
5829         for (k = 0; grid_sizes_list[k].value != -1; k++)
5830         {
5831           TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5832           char identifier[32], name[32];
5833           int value = grid_sizes_list[k].value;
5834           char *text = grid_sizes_list[k].text;
5835
5836           ti->node_top = &grid_sizes[i][j];
5837           ti->sort_priority = value;
5838
5839           sprintf(identifier, "%d", value);
5840           sprintf(name, "%s", text);
5841
5842           setString(&ti->identifier, identifier);
5843           setString(&ti->name, name);
5844           setString(&ti->name_sorting, name);
5845           setString(&ti->infotext,
5846                     (i == 0 ?
5847                      (j == 0 ?
5848                       STR_SETUP_CHOOSE_GRID_XSIZE_0 :
5849                       STR_SETUP_CHOOSE_GRID_YSIZE_0) :
5850                      (j == 0 ?
5851                       STR_SETUP_CHOOSE_GRID_XSIZE_1 :
5852                       STR_SETUP_CHOOSE_GRID_YSIZE_1)));
5853
5854           pushTreeInfo(&grid_sizes[i][j], ti);
5855         }
5856
5857         // sort grid size values to start with lowest grid size value
5858         sortTreeInfo(&grid_sizes[i][j]);
5859
5860         // set current grid size value to configured grid size value
5861         grid_size_current[i][j] =
5862           getTreeInfoFromIdentifier(grid_sizes[i][j],
5863                                     i_to_a(j == 0 ?
5864                                            setup.touch.grid_xsize[i] :
5865                                            setup.touch.grid_ysize[i]));
5866
5867         // if that fails, set current grid size to reliable default value
5868         if (grid_size_current[i][j] == NULL)
5869           grid_size_current[i][j] =
5870             getTreeInfoFromIdentifier(grid_sizes[i][j],
5871                                       i_to_a(j == 0 ?
5872                                              DEFAULT_GRID_XSIZE(i) :
5873                                              DEFAULT_GRID_YSIZE(i)));
5874
5875         // if that also fails, set current grid size to first available value
5876         if (grid_size_current[i][j] == NULL)
5877           grid_size_current[i][j] = grid_sizes[i][j];
5878       }
5879     }
5880   }
5881
5882   setup.touch.control_type = touch_control_current->identifier;
5883   setup.touch.move_distance = atoi(move_distance_current->identifier);
5884   setup.touch.drop_distance = atoi(drop_distance_current->identifier);
5885   setup.touch.transparency = atoi(transparency_current->identifier);
5886
5887   for (i = 0; i < 2; i++)
5888   {
5889     setup.touch.grid_xsize[i] = atoi(grid_size_current[i][0]->identifier);
5890     setup.touch.grid_ysize[i] = atoi(grid_size_current[i][1]->identifier);
5891
5892     if (i == GRID_ACTIVE_NR())
5893     {
5894       overlay.grid_xsize = setup.touch.grid_xsize[i];
5895       overlay.grid_ysize = setup.touch.grid_ysize[i];
5896     }
5897   }
5898
5899   // needed for displaying value text instead of identifier
5900   touch_controls_text = touch_control_current->name;
5901   move_distance_text = move_distance_current->name;
5902   drop_distance_text = drop_distance_current->name;
5903   transparency_text = transparency_current->name;
5904
5905   for (i = 0; i < 2; i++)
5906     for (j = 0; j < 2; j++)
5907       grid_size_text[i][j] = grid_size_current[i][j]->name;
5908
5909   setup_mode = SETUP_MODE_TOUCH;
5910
5911   DrawSetupScreen();
5912 }
5913
5914 static void execSetupArtwork(void)
5915 {
5916 #if 0
5917   printf("::: '%s', '%s', '%s'\n",
5918          artwork.gfx_current->subdir,
5919          artwork.gfx_current->fullpath,
5920          artwork.gfx_current->basepath);
5921 #endif
5922
5923   setup.graphics_set = artwork.gfx_current->identifier;
5924   setup.sounds_set = artwork.snd_current->identifier;
5925   setup.music_set = artwork.mus_current->identifier;
5926
5927   // needed if last screen (setup choice) changed graphics, sounds or music
5928   ReloadCustomArtwork(0);
5929
5930   // needed for displaying artwork name instead of artwork identifier
5931   graphics_set_name = artwork.gfx_current->name;
5932   sounds_set_name = artwork.snd_current->name;
5933   music_set_name = artwork.mus_current->name;
5934
5935   setup_mode = SETUP_MODE_ARTWORK;
5936
5937   DrawSetupScreen();
5938 }
5939
5940 static void execSetupChooseGraphics(void)
5941 {
5942   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
5943
5944   DrawSetupScreen();
5945 }
5946
5947 static void execSetupChooseSounds(void)
5948 {
5949   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
5950
5951   DrawSetupScreen();
5952 }
5953
5954 static void execSetupChooseMusic(void)
5955 {
5956   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
5957
5958   DrawSetupScreen();
5959 }
5960
5961 static void execSetupInput(void)
5962 {
5963   setup_mode = SETUP_MODE_INPUT;
5964
5965   DrawSetupScreen();
5966 }
5967
5968 static void execSetupShortcuts(void)
5969 {
5970   setup_mode = SETUP_MODE_SHORTCUTS;
5971
5972   DrawSetupScreen();
5973 }
5974
5975 static void execSetupShortcuts1(void)
5976 {
5977   setup_mode = SETUP_MODE_SHORTCUTS_1;
5978
5979   DrawSetupScreen();
5980 }
5981
5982 static void execSetupShortcuts2(void)
5983 {
5984   setup_mode = SETUP_MODE_SHORTCUTS_2;
5985
5986   DrawSetupScreen();
5987 }
5988
5989 static void execSetupShortcuts3(void)
5990 {
5991   setup_mode = SETUP_MODE_SHORTCUTS_3;
5992
5993   DrawSetupScreen();
5994 }
5995
5996 static void execSetupShortcuts4(void)
5997 {
5998   setup_mode = SETUP_MODE_SHORTCUTS_4;
5999
6000   DrawSetupScreen();
6001 }
6002
6003 static void execSetupShortcuts5(void)
6004 {
6005   setup_mode = SETUP_MODE_SHORTCUTS_5;
6006
6007   DrawSetupScreen();
6008 }
6009
6010 static void execExitSetup(void)
6011 {
6012   SetGameStatus(GAME_MODE_MAIN);
6013
6014   DrawMainMenu();
6015 }
6016
6017 static void execSaveAndExitSetup(void)
6018 {
6019   SaveSetup();
6020   execExitSetup();
6021 }
6022
6023 static void execGadgetNetworkServer(void)
6024 {
6025   int gadget_id = SCREEN_CTRL_ID_NETWORK_SERVER;
6026   struct GadgetInfo *gi = screen_gadget[gadget_id];
6027
6028   if (strEqual(setup.network_server_hostname, STR_NETWORK_AUTO_DETECT))
6029     network_server_hostname[0] = '\0';
6030
6031   ModifyGadget(gi, GDI_TEXT_VALUE, network_server_hostname, GDI_END);
6032
6033   MapGadget(gi);
6034
6035   ClickOnGadget(gi, MB_LEFTBUTTON);
6036 }
6037
6038 static void ToggleNetworkModeIfNeeded(void)
6039 {
6040   int font_title = FONT_TITLE_1;
6041   int font_foot = FC_BLUE;
6042   int ystart  = mSY - SY + 16;
6043   int ybottom = mSY - SY + SYSIZE - 20;
6044   char *text = (setup.network_mode ? "Start Network" : "Stop Network");
6045
6046   if (setup.network_mode == network.enabled)
6047     return;
6048
6049   network.enabled = setup.network_mode;
6050
6051   FadeOut(REDRAW_ALL);
6052
6053   ClearField();
6054
6055   DrawTextSCentered(ystart, font_title, text);
6056
6057   FadeIn(REDRAW_ALL);
6058
6059   if (network.enabled)
6060     InitNetworkServer();
6061   else
6062     DisconnectFromNetworkServer();
6063
6064   DrawTextSCentered(ybottom, font_foot,
6065                     "Press any key or button for setup menu");
6066
6067   WaitForEventToContinue();
6068
6069   DrawSetupScreen();
6070 }
6071
6072 static void ToggleGameSpeedsListIfNeeded(void)
6073 {
6074   boolean using_game_speeds_extended = (game_speeds == game_speeds_extended);
6075
6076   if (setup.game_speed_extended == using_game_speeds_extended)
6077     return;
6078
6079   // try to match similar values when changing game speeds list
6080   if (setup.game_speed_extended)
6081     setup.game_frame_delay = (setup.game_frame_delay == 15 ? 16 :
6082                               setup.game_frame_delay == 30 ? 29 :
6083                               setup.game_frame_delay);
6084   else
6085     setup.game_frame_delay = (setup.game_frame_delay == 14 ? 15 :
6086                               setup.game_frame_delay == 16 ? 15 :
6087                               setup.game_frame_delay >= 29 ? 30 :
6088                               setup.game_frame_delay <= 10 ? 10 :
6089                               setup.game_frame_delay);
6090
6091   execSetupGame_setGameSpeeds(TRUE);
6092
6093   DrawSetupScreen();
6094 }
6095
6096 static void ModifyGameSpeedIfNeeded(void)
6097 {
6098   if (strEqual(setup.vsync_mode, STR_VSYNC_MODE_OFF) ||
6099       setup.game_frame_delay <= MAX_VSYNC_FRAME_DELAY)
6100     return;
6101
6102   char message[100];
6103   char *game_speed_text = "Fast";
6104   int game_speed_value = 15;
6105
6106   if (setup.game_speed_extended)
6107   {
6108     game_speed_text = "60 fps";
6109     game_speed_value = 16;
6110   }
6111
6112   sprintf(message, "Game speed set to %s for VSync to work!", game_speed_text);
6113
6114   // set game speed to existing list value that is fast enough for vsync
6115   setup.game_frame_delay = game_speed_value;
6116
6117   execSetupGame_setGameSpeeds(TRUE);
6118
6119   Request(message, REQ_CONFIRM);
6120 }
6121
6122 static void DisableVsyncIfNeeded(void)
6123 {
6124   if (strEqual(setup.vsync_mode, STR_VSYNC_MODE_OFF) ||
6125       (setup.game_frame_delay >= MIN_VSYNC_FRAME_DELAY &&
6126        setup.game_frame_delay <= MAX_VSYNC_FRAME_DELAY))
6127     return;
6128
6129   // disable vsync for the selected game speed to work
6130   setup.vsync_mode = STR_VSYNC_MODE_OFF;
6131
6132   execSetupGraphics_setVsyncModes(TRUE);
6133
6134   Request("VSync disabled for this game speed to work!", REQ_CONFIRM);
6135 }
6136
6137 static struct
6138 {
6139   void *value;
6140   void *related_value;
6141 } hide_related_entry_list[] =
6142 {
6143   { &setup.game_frame_delay,            execSetupChooseGameSpeed        },
6144   { &setup.game_frame_delay,            &game_speed_text                },
6145
6146   { &setup.scroll_delay_value,          execSetupChooseScrollDelay      },
6147   { &setup.scroll_delay_value,          &scroll_delay_text              },
6148
6149   { &setup.engine_snapshot_mode,        execSetupChooseSnapshotMode     },
6150   { &setup.engine_snapshot_mode,        &snapshot_mode_text             },
6151
6152   { &setup.window_scaling_percent,      execSetupChooseWindowSize       },
6153   { &setup.window_scaling_percent,      &window_size_text               },
6154
6155   { &setup.window_scaling_quality,      execSetupChooseScalingType      },
6156   { &setup.window_scaling_quality,      &scaling_type_text              },
6157
6158   { &setup.screen_rendering_mode,       execSetupChooseRenderingMode    },
6159   { &setup.screen_rendering_mode,       &rendering_mode_text            },
6160
6161   { &setup.vsync_mode,                  execSetupChooseVsyncMode        },
6162   { &setup.vsync_mode,                  &vsync_mode_text                },
6163
6164   { &setup.graphics_set,                execSetupChooseGraphics         },
6165   { &setup.graphics_set,                &graphics_set_name              },
6166
6167   { &setup.sounds_set,                  execSetupChooseSounds           },
6168   { &setup.sounds_set,                  &sounds_set_name                },
6169
6170   { &setup.music_set,                   execSetupChooseMusic            },
6171   { &setup.music_set,                   &music_set_name                 },
6172
6173   { &setup.volume_simple,               execSetupChooseVolumeSimple     },
6174   { &setup.volume_simple,               &volume_simple_text             },
6175
6176   { &setup.volume_loops,                execSetupChooseVolumeLoops      },
6177   { &setup.volume_loops,                &volume_loops_text              },
6178
6179   { &setup.volume_music,                execSetupChooseVolumeMusic      },
6180   { &setup.volume_music,                &volume_music_text              },
6181
6182   { &setup.touch.control_type,          execSetupChooseTouchControls    },
6183   { &setup.touch.control_type,          &touch_controls_text            },
6184
6185   { &setup.touch.move_distance,         execSetupChooseMoveDistance     },
6186   { &setup.touch.move_distance,         &move_distance_text             },
6187
6188   { &setup.touch.drop_distance,         execSetupChooseDropDistance     },
6189   { &setup.touch.drop_distance,         &drop_distance_text             },
6190
6191   { &setup.touch.transparency,          execSetupChooseTransparency     },
6192   { &setup.touch.transparency,          &transparency_text              },
6193
6194   { &setup.touch.grid_xsize[0],         execSetupChooseGridXSize_0      },
6195   { &setup.touch.grid_xsize[0],         &grid_size_text[0][0]           },
6196
6197   { &setup.touch.grid_ysize[0],         execSetupChooseGridYSize_0      },
6198   { &setup.touch.grid_ysize[0],         &grid_size_text[0][1]           },
6199
6200   { &setup.touch.grid_xsize[1],         execSetupChooseGridXSize_1      },
6201   { &setup.touch.grid_xsize[1],         &grid_size_text[1][0]           },
6202
6203   { &setup.touch.grid_ysize[1],         execSetupChooseGridYSize_1      },
6204   { &setup.touch.grid_ysize[1],         &grid_size_text[1][1]           },
6205
6206   { &setup.internal.menu_game,          execSetupGame                   },
6207   { &setup.internal.menu_engines,       execSetupEngines                },
6208   { &setup.internal.menu_editor,        execSetupEditor                 },
6209   { &setup.internal.menu_graphics,      execSetupGraphics               },
6210   { &setup.internal.menu_sound,         execSetupSound                  },
6211   { &setup.internal.menu_artwork,       execSetupArtwork                },
6212   { &setup.internal.menu_input,         execSetupInput                  },
6213   { &setup.internal.menu_touch,         execSetupTouch                  },
6214   { &setup.internal.menu_shortcuts,     execSetupShortcuts              },
6215   { &setup.internal.menu_exit,          execExitSetup                   },
6216   { &setup.internal.menu_save_and_exit, execSaveAndExitSetup            },
6217
6218   { NULL,                               NULL                            }
6219 };
6220
6221 void setHideRelatedSetupEntries(void)
6222 {
6223   int i;
6224
6225   for (i = 0; hide_related_entry_list[i].value != NULL; i++)
6226     if (hideSetupEntry(hide_related_entry_list[i].value))
6227       setHideSetupEntry(hide_related_entry_list[i].related_value);
6228 }
6229
6230 static struct TokenInfo setup_info_main[] =
6231 {
6232   { TYPE_ENTER_MENU,    execSetupGame,          STR_SETUP_GAME          },
6233   { TYPE_ENTER_MENU,    execSetupEngines,       STR_SETUP_ENGINES       },
6234   { TYPE_ENTER_MENU,    execSetupEditor,        STR_SETUP_EDITOR        },
6235   { TYPE_ENTER_MENU,    execSetupGraphics,      STR_SETUP_GRAPHICS      },
6236   { TYPE_ENTER_MENU,    execSetupSound,         STR_SETUP_SOUND         },
6237   { TYPE_ENTER_MENU,    execSetupArtwork,       STR_SETUP_ARTWORK       },
6238   { TYPE_ENTER_MENU,    execSetupInput,         STR_SETUP_INPUT         },
6239   { TYPE_ENTER_MENU,    execSetupTouch,         STR_SETUP_TOUCH         },
6240   { TYPE_ENTER_MENU,    execSetupShortcuts,     STR_SETUP_SHORTCUTS     },
6241   { TYPE_EMPTY,         NULL,                   ""                      },
6242   { TYPE_LEAVE_MENU,    execExitSetup,          STR_SETUP_EXIT          },
6243   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   STR_SETUP_SAVE_AND_EXIT },
6244
6245   { 0,                  NULL,                   NULL                    }
6246 };
6247
6248 static struct TokenInfo setup_info_game[] =
6249 {
6250   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode (Multi-Player):" },
6251   { TYPE_SWITCH,        &setup.network_mode,    "Network Multi-Player Mode:" },
6252   { TYPE_PLAYER,        &setup.network_player_nr,"Preferred Network Player:" },
6253   { TYPE_TEXT_INPUT,    execGadgetNetworkServer, "Network Server Hostname:" },
6254   { TYPE_STRING,        &network_server_text,   ""                      },
6255   { TYPE_YES_NO,        &setup.input_on_focus,  "Only Move Focussed Player:" },
6256   { TYPE_SWITCH,        &setup.time_limit,      "Time Limit:"           },
6257   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
6258   { TYPE_SWITCH,        &setup.skip_levels,     "Skip Unsolved Levels:" },
6259   { TYPE_SWITCH,        &setup.increment_levels,"Increment Solved Levels:" },
6260   { TYPE_SWITCH,        &setup.auto_play_next_level,"Auto-play Next Level:" },
6261   { TYPE_SWITCH,        &setup.skip_scores_after_game,"Skip Scores After Game:" },
6262   { TYPE_YES_NO,        &setup.ask_on_game_over, "Ask on Game Over:"    },
6263   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record Tapes:"    },
6264   { TYPE_ENTER_LIST,    execSetupChooseGameSpeed, "Game Speed:"         },
6265   { TYPE_STRING,        &game_speed_text,       ""                      },
6266   { TYPE_SWITCH,        &setup.game_speed_extended, "Game Speed Extended List:" },
6267 #if 1
6268   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay:"     },
6269   { TYPE_STRING,        &scroll_delay_text,     ""                      },
6270 #endif
6271   { TYPE_ENTER_LIST, execSetupChooseSnapshotMode,"Game Engine Snapshot Mode:" },
6272   { TYPE_STRING,        &snapshot_mode_text,    ""                      },
6273   { TYPE_SWITCH,        &setup.show_snapshot_buttons,"Show Snapshot Buttons:" },
6274   { TYPE_EMPTY,         NULL,                   ""                      },
6275   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6276
6277   { 0,                  NULL,                   NULL                    }
6278 };
6279
6280 static struct TokenInfo setup_info_engines[] =
6281 {
6282   { TYPE_HEADLINE,      NULL,                   "Emerald Mine"          },
6283   { TYPE_SWITCH,        &setup.forced_scroll_delay, "Scroll Delay:"     },
6284   { TYPE_ECS_AGA,       &setup.prefer_aga_graphics, "Graphics preference:" },
6285   { TYPE_EMPTY,         NULL,                   ""                      },
6286   { TYPE_HEADLINE,      NULL,                   "Supaplex"              },
6287   { TYPE_SWITCH, &setup.sp_show_border_elements,"Border Elements:"      },
6288   { TYPE_EMPTY,         NULL,                   ""                      },
6289   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6290
6291   { 0,                  NULL,                   NULL                    }
6292 };
6293
6294 static struct TokenInfo setup_info_editor[] =
6295 {
6296 #if 0
6297   { TYPE_SWITCH,        &setup.editor.el_boulderdash,   "Boulder Dash:" },
6298   { TYPE_SWITCH,        &setup.editor.el_emerald_mine,  "Emerald Mine:" },
6299   { TYPE_SWITCH, &setup.editor.el_emerald_mine_club,    "Emerald Mine Club:" },
6300   { TYPE_SWITCH,        &setup.editor.el_more,          "Rocks'n'Diamonds:" },
6301   { TYPE_SWITCH,        &setup.editor.el_sokoban,       "Sokoban:"      },
6302   { TYPE_SWITCH,        &setup.editor.el_supaplex,      "Supaplex:"     },
6303   { TYPE_SWITCH,        &setup.editor.el_diamond_caves, "Diamond Caves II:" },
6304   { TYPE_SWITCH,        &setup.editor.el_dx_boulderdash,"DX-Boulderdash:" },
6305   { TYPE_SWITCH,        &setup.editor.el_chars,         "Text Characters:" },
6306   { TYPE_SWITCH, &setup.editor.el_steel_chars, "Text Characters (Steel):" },
6307 #endif
6308   { TYPE_SWITCH,        &setup.editor.el_classic,  "Classic Elements:" },
6309   { TYPE_SWITCH,        &setup.editor.el_custom,  "Custom & Group Elements:" },
6310 #if 0
6311   { TYPE_SWITCH,        &setup.editor.el_headlines,     "Headlines:"    },
6312 #endif
6313   { TYPE_SWITCH, &setup.editor.el_user_defined, "User defined element list:" },
6314   { TYPE_SWITCH,        &setup.editor.el_dynamic,  "Dynamic level elements:" },
6315   { TYPE_EMPTY,         NULL,                   ""                      },
6316 #if 0
6317   { TYPE_SWITCH,        &setup.editor.el_by_game,   "Show elements by game:" },
6318   { TYPE_SWITCH,        &setup.editor.el_by_type,   "Show elements by type:" },
6319   { TYPE_EMPTY,         NULL,                   ""                      },
6320 #endif
6321   { TYPE_SWITCH, &setup.editor.show_element_token,      "Show element token:" },
6322   { TYPE_EMPTY,         NULL,                   ""                      },
6323   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6324
6325   { 0,                  NULL,                   NULL                    }
6326 };
6327
6328 static struct TokenInfo setup_info_graphics[] =
6329 {
6330 #if !defined(PLATFORM_ANDROID)
6331   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen:"           },
6332   { TYPE_ENTER_LIST,    execSetupChooseWindowSize, "Window Scaling:"    },
6333   { TYPE_STRING,        &window_size_text,      ""                      },
6334   { TYPE_ENTER_LIST,    execSetupChooseScalingType, "Anti-Aliasing:"    },
6335   { TYPE_STRING,        &scaling_type_text,     ""                      },
6336   { TYPE_ENTER_LIST,    execSetupChooseRenderingMode, "Special Rendering:" },
6337   { TYPE_STRING,        &rendering_mode_text,   ""                      },
6338 #endif
6339 #if 0
6340   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay:"     },
6341   { TYPE_STRING,        &scroll_delay_text,     ""                      },
6342 #endif
6343   { TYPE_ENTER_LIST,    execSetupChooseVsyncMode, "Vertical Sync (VSync):" },
6344   { TYPE_STRING,        &vsync_mode_text,       ""                      },
6345   { TYPE_SWITCH,        &setup.fade_screens,    "Fade Screens:"         },
6346   { TYPE_SWITCH,        &setup.quick_switch,    "Quick Player Focus Switch:" },
6347   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Menu Doors:"     },
6348   { TYPE_SWITCH,        &setup.show_titlescreen,"Show Title Screens:"   },
6349   { TYPE_SWITCH,        &setup.toons,           "Show Menu Animations:" },
6350   { TYPE_SWITCH,        &setup.small_game_graphics, "Small Game Graphics:" },
6351   { TYPE_EMPTY,         NULL,                   ""                      },
6352   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6353
6354   { 0,                  NULL,                   NULL                    }
6355 };
6356
6357 static struct TokenInfo setup_info_sound[] =
6358 {
6359   { TYPE_SWITCH,        &setup.sound_simple,    "Sound Effects (Normal):"  },
6360   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Effects (Looping):" },
6361   { TYPE_SWITCH,        &setup.sound_music,     "Music:"                },
6362   { TYPE_EMPTY,         NULL,                   ""                      },
6363   { TYPE_ENTER_LIST,    execSetupChooseVolumeSimple, "Sound Volume (Normal):" },
6364   { TYPE_STRING,        &volume_simple_text,    ""                      },
6365   { TYPE_ENTER_LIST,    execSetupChooseVolumeLoops, "Sound Volume (Looping):" },
6366   { TYPE_STRING,        &volume_loops_text,     ""                      },
6367   { TYPE_ENTER_LIST,    execSetupChooseVolumeMusic, "Music Volume:"     },
6368   { TYPE_STRING,        &volume_music_text,     ""                      },
6369   { TYPE_EMPTY,         NULL,                   ""                      },
6370   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6371
6372   { 0,                  NULL,                   NULL                    }
6373 };
6374
6375 static struct TokenInfo setup_info_artwork[] =
6376 {
6377   { TYPE_ENTER_LIST,    execSetupChooseGraphics,"Custom Graphics:"      },
6378   { TYPE_STRING,        &graphics_set_name,     ""                      },
6379   { TYPE_ENTER_LIST,    execSetupChooseSounds,  "Custom Sounds:"        },
6380   { TYPE_STRING,        &sounds_set_name,       ""                      },
6381   { TYPE_ENTER_LIST,    execSetupChooseMusic,   "Custom Music:"         },
6382   { TYPE_STRING,        &music_set_name,        ""                      },
6383   { TYPE_EMPTY,         NULL,                   ""                      },
6384   { TYPE_YES_NO_AUTO,&setup.override_level_graphics,"Override Level Graphics:"},
6385   { TYPE_YES_NO_AUTO,&setup.override_level_sounds,  "Override Level Sounds:"  },
6386   { TYPE_YES_NO_AUTO,&setup.override_level_music,   "Override Level Music:"   },
6387   { TYPE_EMPTY,         NULL,                   ""                      },
6388   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6389
6390   { 0,                  NULL,                   NULL                    }
6391 };
6392
6393 static struct TokenInfo setup_info_input[] =
6394 {
6395   { TYPE_SWITCH,        NULL,                   "Player:"               },
6396   { TYPE_SWITCH,        NULL,                   "Device:"               },
6397   { TYPE_SWITCH,        NULL,                   ""                      },
6398   { TYPE_SKIPPABLE,     NULL,                   ""                      },
6399   { TYPE_EMPTY,         NULL,                   ""                      },
6400   { TYPE_EMPTY,         NULL,                   ""                      },
6401   { TYPE_EMPTY,         NULL,                   ""                      },
6402   { TYPE_EMPTY,         NULL,                   ""                      },
6403   { TYPE_EMPTY,         NULL,                   ""                      },
6404   { TYPE_EMPTY,         NULL,                   ""                      },
6405   { TYPE_EMPTY,         NULL,                   ""                      },
6406   { TYPE_EMPTY,         NULL,                   ""                      },
6407   { TYPE_SKIPPABLE,     NULL,                   ""                      },
6408   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6409
6410   { 0,                  NULL,                   NULL                    }
6411 };
6412
6413 static struct TokenInfo setup_info_touch[] =
6414 {
6415   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
6416   { TYPE_STRING,        &touch_controls_text,   ""                      },
6417   { TYPE_EMPTY,         NULL,                   ""                      },
6418   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6419
6420   { 0,                  NULL,                   NULL                    }
6421 };
6422
6423 static struct TokenInfo setup_info_touch_virtual_buttons_0[] =
6424 {
6425   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
6426   { TYPE_STRING,        &touch_controls_text,   ""                      },
6427   { TYPE_EMPTY,         NULL,                   ""                      },
6428   { TYPE_ENTER_LIST,    execSetupChooseGridXSize_0, "Horizontal Buttons (Landscape):"   },
6429   { TYPE_STRING,        &grid_size_text[0][0],  ""                      },
6430   { TYPE_ENTER_LIST,    execSetupChooseGridYSize_0, "Vertical Buttons (Landscape):"     },
6431   { TYPE_STRING,        &grid_size_text[0][1],  ""                      },
6432   { TYPE_ENTER_LIST,    execSetupChooseTransparency, "Button Transparency:" },
6433   { TYPE_STRING,        &transparency_text,     ""                      },
6434   { TYPE_SWITCH,        &setup.touch.draw_outlined, "Draw Buttons Outlined:" },
6435   { TYPE_SWITCH,        &setup.touch.draw_pressed, "Highlight Pressed Buttons:" },
6436   { TYPE_EMPTY,         NULL,                   ""                      },
6437   { TYPE_ENTER_LIST,    execSetupConfigureVirtualButtons, "Configure Virtual Buttons" },
6438   { TYPE_EMPTY,         NULL,                   ""                      },
6439   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6440
6441   { 0,                  NULL,                   NULL                    }
6442 };
6443
6444 static struct TokenInfo setup_info_touch_virtual_buttons_1[] =
6445 {
6446   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
6447   { TYPE_STRING,        &touch_controls_text,   ""                      },
6448   { TYPE_EMPTY,         NULL,                   ""                      },
6449   { TYPE_ENTER_LIST,    execSetupChooseGridXSize_1, "Horizontal Buttons (Portrait):"    },
6450   { TYPE_STRING,        &grid_size_text[1][0],  ""                      },
6451   { TYPE_ENTER_LIST,    execSetupChooseGridYSize_1, "Vertical Buttons (Portrait):"      },
6452   { TYPE_STRING,        &grid_size_text[1][1],  ""                      },
6453   { TYPE_ENTER_LIST,    execSetupChooseTransparency, "Button Transparency:" },
6454   { TYPE_STRING,        &transparency_text,     ""                      },
6455   { TYPE_SWITCH,        &setup.touch.draw_outlined, "Draw Buttons Outlined:" },
6456   { TYPE_SWITCH,        &setup.touch.draw_pressed, "Highlight Pressed Buttons:" },
6457   { TYPE_EMPTY,         NULL,                   ""                      },
6458   { TYPE_ENTER_LIST,    execSetupConfigureVirtualButtons, "Configure Virtual Buttons" },
6459   { TYPE_EMPTY,         NULL,                   ""                      },
6460   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6461
6462   { 0,                  NULL,                   NULL                    }
6463 };
6464
6465 static struct TokenInfo *setup_info_touch_virtual_buttons[] =
6466 {
6467   setup_info_touch_virtual_buttons_0,
6468   setup_info_touch_virtual_buttons_1
6469 };
6470
6471 static struct TokenInfo setup_info_touch_wipe_gestures[] =
6472 {
6473   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
6474   { TYPE_STRING,        &touch_controls_text,   ""                      },
6475   { TYPE_EMPTY,         NULL,                   ""                      },
6476   { TYPE_ENTER_LIST,    execSetupChooseMoveDistance, "Move Trigger Distance:" },
6477   { TYPE_STRING,        &move_distance_text,    ""                      },
6478   { TYPE_ENTER_LIST,    execSetupChooseDropDistance, "Drop Trigger Distance:" },
6479   { TYPE_STRING,        &drop_distance_text,    ""                      },
6480   { TYPE_EMPTY,         NULL,                   ""                      },
6481   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6482
6483   { 0,                  NULL,                   NULL                    }
6484 };
6485
6486 static struct TokenInfo setup_info_shortcuts[] =
6487 {
6488   { TYPE_ENTER_MENU,    execSetupShortcuts1,    "Various Keys"          },
6489   { TYPE_ENTER_MENU,    execSetupShortcuts2,    "Player Focus"          },
6490   { TYPE_ENTER_MENU,    execSetupShortcuts3,    "Tape Buttons"          },
6491   { TYPE_ENTER_MENU,    execSetupShortcuts4,    "Sound & Music"         },
6492   { TYPE_ENTER_MENU,    execSetupShortcuts5,    "TAS Snap Keys"         },
6493   { TYPE_EMPTY,         NULL,                   ""                      },
6494   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6495
6496   { 0,                  NULL,                   NULL                    }
6497 };
6498
6499 static struct TokenInfo setup_info_shortcuts_1[] =
6500 {
6501   { TYPE_KEYTEXT,       NULL,           "Quick Save Game to Tape:",     },
6502   { TYPE_KEY,           &setup.shortcut.save_game, ""                   },
6503   { TYPE_KEYTEXT,       NULL,           "Quick Load Game from Tape:",   },
6504   { TYPE_KEY,           &setup.shortcut.load_game, ""                   },
6505   { TYPE_KEYTEXT,       NULL,           "Start Game & Toggle Pause:",   },
6506   { TYPE_KEY,           &setup.shortcut.toggle_pause, ""                },
6507   { TYPE_EMPTY,         NULL,                   ""                      },
6508   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on 'Esc' Key:"     },
6509   { TYPE_YES_NO, &setup.ask_on_escape_editor,   "Ask on 'Esc' Key (Editor):" },
6510   { TYPE_EMPTY,         NULL,                   ""                      },
6511   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
6512
6513   { 0,                  NULL,                   NULL                    }
6514 };
6515
6516 static struct TokenInfo setup_info_shortcuts_2[] =
6517 {
6518   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 1:",       },
6519   { TYPE_KEY,           &setup.shortcut.focus_player[0], ""             },
6520   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 2:",       },
6521   { TYPE_KEY,           &setup.shortcut.focus_player[1], ""             },
6522   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 3:",       },
6523   { TYPE_KEY,           &setup.shortcut.focus_player[2], ""             },
6524   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 4:",       },
6525   { TYPE_KEY,           &setup.shortcut.focus_player[3], ""             },
6526   { TYPE_KEYTEXT,       NULL,           "Set Focus to All Players:",    },
6527   { TYPE_KEY,           &setup.shortcut.focus_player_all, ""            },
6528   { TYPE_EMPTY,         NULL,                   ""                      },
6529   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
6530
6531   { 0,                  NULL,                   NULL                    }
6532 };
6533
6534 static struct TokenInfo setup_info_shortcuts_3[] =
6535 {
6536   { TYPE_KEYTEXT,       NULL,                   "Eject Tape:",          },
6537   { TYPE_KEY,           &setup.shortcut.tape_eject, ""                  },
6538   { TYPE_KEYTEXT,       NULL,                   "Warp / Single Step:",  },
6539   { TYPE_KEY,           &setup.shortcut.tape_extra, ""                  },
6540   { TYPE_KEYTEXT,       NULL,                   "Stop Tape:",           },
6541   { TYPE_KEY,           &setup.shortcut.tape_stop, ""                   },
6542   { TYPE_KEYTEXT,       NULL,                   "Pause / Unpause Tape:",},
6543   { TYPE_KEY,           &setup.shortcut.tape_pause, ""                  },
6544   { TYPE_KEYTEXT,       NULL,                   "Record Tape:",         },
6545   { TYPE_KEY,           &setup.shortcut.tape_record, ""                 },
6546   { TYPE_KEYTEXT,       NULL,                   "Play Tape:",           },
6547   { TYPE_KEY,           &setup.shortcut.tape_play, ""                   },
6548   { TYPE_EMPTY,         NULL,                   ""                      },
6549   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
6550
6551   { 0,                  NULL,                   NULL                    }
6552 };
6553
6554 static struct TokenInfo setup_info_shortcuts_4[] =
6555 {
6556   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Normal):", },
6557   { TYPE_KEY,           &setup.shortcut.sound_simple, ""                },
6558   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Looping):", },
6559   { TYPE_KEY,           &setup.shortcut.sound_loops, ""                 },
6560   { TYPE_KEYTEXT,       NULL,           "Toggle Music:",                },
6561   { TYPE_KEY,           &setup.shortcut.sound_music, ""                 },
6562   { TYPE_EMPTY,         NULL,                   ""                      },
6563   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
6564
6565   { 0,                  NULL,                   NULL                    }
6566 };
6567
6568 static struct TokenInfo setup_info_shortcuts_5[] =
6569 {
6570   { TYPE_KEYTEXT,       NULL,                   "Snap Left:",           },
6571   { TYPE_KEY,           &setup.shortcut.snap_left, ""                   },
6572   { TYPE_KEYTEXT,       NULL,                   "Snap Right:",          },
6573   { TYPE_KEY,           &setup.shortcut.snap_right, ""                  },
6574   { TYPE_KEYTEXT,       NULL,                   "Snap Up:",             },
6575   { TYPE_KEY,           &setup.shortcut.snap_up, ""                     },
6576   { TYPE_KEYTEXT,       NULL,                   "Snap Down:",           },
6577   { TYPE_KEY,           &setup.shortcut.snap_down, ""                   },
6578   { TYPE_EMPTY,         NULL,                   ""                      },
6579   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
6580
6581   { 0,                  NULL,                   NULL                    }
6582 };
6583
6584 static Key getSetupKey(void)
6585 {
6586   Key key = KSYM_UNDEFINED;
6587   boolean got_key_event = FALSE;
6588
6589   while (!got_key_event)
6590   {
6591     Event event;
6592
6593     if (NextValidEvent(&event))
6594     {
6595       switch (event.type)
6596       {
6597         case EVENT_KEYPRESS:
6598           {
6599             key = GetEventKey((KeyEvent *)&event, TRUE);
6600
6601             // press 'Escape' or 'Enter' to keep the existing key binding
6602             if (key == KSYM_Escape || key == KSYM_Return)
6603               key = KSYM_UNDEFINED;     // keep old value
6604
6605             got_key_event = TRUE;
6606           }
6607           break;
6608
6609         case EVENT_KEYRELEASE:
6610           key_joystick_mapping = 0;
6611           break;
6612
6613         default:
6614           HandleOtherEvents(&event);
6615           break;
6616       }
6617     }
6618
6619     BackToFront();
6620   }
6621
6622   return key;
6623 }
6624
6625 static int getSetupValueFont(int type, void *value)
6626 {
6627   if (type & TYPE_GHOSTED)
6628     return FONT_OPTION_OFF;
6629   else if (type & TYPE_KEY)
6630     return (type & TYPE_QUERY ? FONT_INPUT_1_ACTIVE : FONT_VALUE_1);
6631   else if (type & TYPE_STRING)
6632     return FONT_VALUE_2;
6633   else if (type & TYPE_ECS_AGA)
6634     return FONT_VALUE_1;
6635   else if (type & TYPE_BOOLEAN_STYLE)
6636     return (*(boolean *)value ? FONT_OPTION_ON : FONT_OPTION_OFF);
6637   else if (type & TYPE_YES_NO_AUTO)
6638     return (*(int *)value == AUTO  ? FONT_OPTION_ON :
6639             *(int *)value == FALSE ? FONT_OPTION_OFF : FONT_OPTION_ON);
6640   else if (type & TYPE_PLAYER)
6641     return FONT_VALUE_1;
6642   else
6643     return FONT_VALUE_1;
6644 }
6645
6646 static int getSetupValueFontNarrow(int type, int font_nr)
6647 {
6648   return (font_nr == FONT_VALUE_1    ? FONT_VALUE_NARROW :
6649           font_nr == FONT_OPTION_ON  ? FONT_OPTION_ON_NARROW :
6650           font_nr == FONT_OPTION_OFF ? FONT_OPTION_OFF_NARROW :
6651           font_nr);
6652 }
6653
6654 static void drawSetupValue(int screen_pos, int setup_info_pos_raw)
6655 {
6656   int si_pos = (setup_info_pos_raw < 0 ? screen_pos : setup_info_pos_raw);
6657   struct TokenInfo *si = &setup_info[si_pos];
6658   boolean font_draw_xoffset_modified = FALSE;
6659   boolean scrollbar_needed = (num_setup_info < max_setup_info);
6660   int font_draw_xoffset_old = -1;
6661   int xoffset = (scrollbar_needed ? -1 : 0);
6662   int menu_screen_value_xpos = MENU_SCREEN_VALUE_XPOS + xoffset;
6663   int menu_screen_max_xpos = MENU_SCREEN_MAX_XPOS + xoffset;
6664   int xpos = menu_screen_value_xpos;
6665   int ypos = MENU_SCREEN_START_YPOS + screen_pos;
6666   int startx = mSX + xpos * 32;
6667   int starty = mSY + ypos * 32;
6668   int type = si->type;
6669   void *value = si->value;
6670   char *value_string = getSetupValue(type, value);
6671   int font_nr_default = getSetupValueFont(type, value);
6672   int font_width_default = getFontWidth(font_nr_default);
6673   int font_nr = font_nr_default;
6674   int i;
6675
6676   if (value_string == NULL)
6677     return;
6678
6679   if (type & TYPE_KEY)
6680   {
6681     xpos = MENU_SCREEN_START_XPOS;
6682
6683     if (type & TYPE_QUERY)
6684       value_string = "<press key>";
6685   }
6686   else if (type & TYPE_STRING)
6687   {
6688     int max_value_len = (SXSIZE - 2 * TILEX) / font_width_default;
6689
6690     xpos = MENU_SCREEN_START_XPOS;
6691
6692     if (strlen(value_string) > max_value_len)
6693       value_string[max_value_len] = '\0';
6694   }
6695   else if (type & TYPE_YES_NO_AUTO)
6696   {
6697     xpos = menu_screen_value_xpos - 1;
6698   }
6699   else if (type & TYPE_PLAYER)
6700   {
6701     int displayed_player_nr = *(int *)value + 1;
6702
6703     value_string = getSetupValue(TYPE_INTEGER, (void *)&displayed_player_nr);
6704   }
6705
6706   startx = mSX + xpos * 32;
6707   starty = mSY + ypos * 32;
6708
6709   // special check if right-side setup values moved left due to scrollbar
6710   if (scrollbar_needed && xpos > MENU_SCREEN_START_XPOS)
6711   {
6712     int max_menu_text_length = 26;      // maximum text length for classic menu
6713     int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
6714     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
6715     int text_font_nr = getMenuTextFont(FONT_MENU_2);
6716     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
6717     int text_width = max_menu_text_length * getFontWidth(text_font_nr);
6718
6719     if (startx + font_xoffset < text_startx + text_width + text_font_xoffset)
6720     {
6721       xpos += 1;
6722       startx = mSX + xpos * 32;
6723
6724       font_nr = getSetupValueFontNarrow(type, font_nr);
6725     }
6726   }
6727
6728   // downward compatibility correction for Juergen Bonhagen's menu settings
6729   if (setup_mode != SETUP_MODE_INPUT)
6730   {
6731     int max_menu_text_length_big = (menu_screen_value_xpos -
6732                                     MENU_SCREEN_START_XPOS);
6733     int max_menu_text_length_medium = max_menu_text_length_big * 2;
6734     int check_font_nr = FONT_OPTION_ON; // known font that needs correction
6735     int font1_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
6736     int font2_xoffset = getFontBitmapInfo(check_font_nr)->draw_xoffset;
6737     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
6738     int text_font_nr = getMenuTextFont(FONT_MENU_2);
6739     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
6740     int text_width = max_menu_text_length_medium * getFontWidth(text_font_nr);
6741     boolean correct_font_draw_xoffset = FALSE;
6742
6743     if (xpos == MENU_SCREEN_START_XPOS &&
6744         startx + font1_xoffset < text_startx + text_font_xoffset)
6745       correct_font_draw_xoffset = TRUE;
6746
6747     if (xpos == menu_screen_value_xpos &&
6748         startx + font2_xoffset < text_startx + text_width + text_font_xoffset)
6749       correct_font_draw_xoffset = TRUE;
6750
6751     // check if setup value would overlap with setup text when printed
6752     // (this can happen for extreme/wrong values for font draw offset)
6753     if (correct_font_draw_xoffset)
6754     {
6755       font_draw_xoffset_old = getFontBitmapInfo(font_nr)->draw_xoffset;
6756       font_draw_xoffset_modified = TRUE;
6757
6758       if (type & TYPE_KEY)
6759         getFontBitmapInfo(font_nr)->draw_xoffset += 2 * getFontWidth(font_nr);
6760       else if (!(type & TYPE_STRING))
6761         getFontBitmapInfo(font_nr)->draw_xoffset = text_font_xoffset + 20 -
6762           max_menu_text_length_medium * (16 - getFontWidth(text_font_nr));
6763     }
6764   }
6765
6766   for (i = 0; i <= menu_screen_max_xpos - xpos; i++)
6767     DrawText(startx + i * font_width_default, starty, " ", font_nr_default);
6768
6769   DrawText(startx, starty, value_string, font_nr);
6770
6771   if (type & TYPE_PLAYER)
6772   {
6773     struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
6774     int player_nr = *(int *)value;
6775     int xoff = font->draw_xoffset + getFontWidth(font_nr);
6776     int yoff = font->draw_yoffset + (getFontHeight(font_nr) - TILEY) / 2;
6777     int startx2 = startx + xoff;
6778     int starty2 = starty + yoff;
6779
6780     if (DrawingOnBackground(startx2, starty2))
6781       ClearRectangleOnBackground(drawto, startx2, starty2, TILEX, TILEY);
6782
6783     DrawFixedGraphicThruMaskExt(drawto, startx2, starty2,
6784                                 PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
6785   }
6786
6787   if (font_draw_xoffset_modified)
6788     getFontBitmapInfo(font_nr)->draw_xoffset = font_draw_xoffset_old;
6789 }
6790
6791 static void changeSetupValue(int screen_pos, int setup_info_pos_raw, int dx)
6792 {
6793   int si_pos = (setup_info_pos_raw < 0 ? screen_pos : setup_info_pos_raw);
6794   struct TokenInfo *si = &setup_info[si_pos];
6795
6796   if (si->type & TYPE_BOOLEAN_STYLE)
6797   {
6798     *(boolean *)si->value ^= TRUE;
6799   }
6800   else if (si->type & TYPE_YES_NO_AUTO)
6801   {
6802     *(int *)si->value =
6803       (dx == -1 ?
6804        (*(int *)si->value == AUTO ? TRUE :
6805         *(int *)si->value == TRUE ? FALSE : AUTO) :
6806        (*(int *)si->value == TRUE ? AUTO :
6807         *(int *)si->value == AUTO ? FALSE : TRUE));
6808   }
6809   else if (si->type & TYPE_KEY)
6810   {
6811     Key key;
6812
6813     si->type |= TYPE_QUERY;
6814     drawSetupValue(screen_pos, setup_info_pos_raw);
6815     si->type &= ~TYPE_QUERY;
6816
6817     key = getSetupKey();
6818     if (key != KSYM_UNDEFINED)
6819       *(Key *)si->value = key;
6820   }
6821   else if (si->type & TYPE_PLAYER)
6822   {
6823     int player_nr = *(int *)si->value;
6824
6825     if (dx)
6826       player_nr += dx;
6827     else
6828       player_nr = Request("Choose player", REQ_PLAYER) - 1;
6829
6830     *(int *)si->value = MIN(MAX(0, player_nr), MAX_PLAYERS - 1);
6831   }
6832
6833   drawSetupValue(screen_pos, setup_info_pos_raw);
6834
6835   // fullscreen state may have changed at this point
6836   if (si->value == &setup.fullscreen)
6837     ToggleFullscreenOrChangeWindowScalingIfNeeded();
6838
6839   // network mode may have changed at this point
6840   if (si->value == &setup.network_mode)
6841     ToggleNetworkModeIfNeeded();
6842
6843   // game speed list may have changed at this point
6844   if (si->value == &setup.game_speed_extended)
6845     ToggleGameSpeedsListIfNeeded();
6846 }
6847
6848 static struct TokenInfo *getSetupInfoFinal(struct TokenInfo *setup_info_orig)
6849 {
6850   static struct TokenInfo *setup_info_final = NULL;
6851   int list_size = 0;
6852   int list_pos = 0;
6853   int i;
6854
6855   // determine maximum list size of target list
6856   while (setup_info_orig[list_size++].type != 0);
6857
6858   // free, allocate and clear memory for target list
6859   checked_free(setup_info_final);
6860   setup_info_final = checked_calloc(list_size * sizeof(struct TokenInfo));
6861
6862   // copy setup info list without setup entries marked as hidden
6863   for (i = 0; setup_info_orig[i].type != 0; i++)
6864   {
6865     // skip setup entries configured to be hidden
6866     if (hideSetupEntry(setup_info_orig[i].value))
6867       continue;
6868
6869     // skip skippable setup entries if screen is lower than usual
6870     if (SCR_FIELDY < SCR_FIELDY_DEFAULT &&
6871         setup_info_orig[i].type == TYPE_SKIPPABLE)
6872       continue;
6873
6874     setup_info_final[list_pos++] = setup_info_orig[i];
6875   }
6876
6877   return setup_info_final;
6878 }
6879
6880 static void DrawSetupScreen_Generic(void)
6881 {
6882   int fade_mask = REDRAW_FIELD;
6883   boolean redraw_all = FALSE;
6884   char *title_string = NULL;
6885   int i;
6886
6887   if (CheckFadeAll())
6888     fade_mask = REDRAW_ALL;
6889
6890   UnmapAllGadgets();
6891   FadeMenuSoundsAndMusic();
6892
6893   FreeScreenGadgets();
6894   CreateScreenGadgets();
6895
6896   if (redraw_mask & REDRAW_ALL)
6897     redraw_all = TRUE;
6898
6899   FadeOut(fade_mask);
6900
6901   // needed if different viewport properties defined for setup screen
6902   ChangeViewportPropertiesIfNeeded();
6903
6904   SetMainBackgroundImage(IMG_BACKGROUND_SETUP);
6905
6906   ClearField();
6907
6908   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
6909
6910   if (setup_mode == SETUP_MODE_MAIN)
6911   {
6912     setup_info = setup_info_main;
6913     title_string = STR_SETUP_MAIN;
6914   }
6915   else if (setup_mode == SETUP_MODE_GAME)
6916   {
6917     setup_info = setup_info_game;
6918     title_string = STR_SETUP_GAME;
6919   }
6920   else if (setup_mode == SETUP_MODE_ENGINES)
6921   {
6922     setup_info = setup_info_engines;
6923     title_string = STR_SETUP_ENGINES;
6924   }
6925   else if (setup_mode == SETUP_MODE_EDITOR)
6926   {
6927     setup_info = setup_info_editor;
6928     title_string = STR_SETUP_EDITOR;
6929   }
6930   else if (setup_mode == SETUP_MODE_GRAPHICS)
6931   {
6932     setup_info = setup_info_graphics;
6933     title_string = STR_SETUP_GRAPHICS;
6934   }
6935   else if (setup_mode == SETUP_MODE_SOUND)
6936   {
6937     setup_info = setup_info_sound;
6938     title_string = STR_SETUP_SOUND;
6939   }
6940   else if (setup_mode == SETUP_MODE_ARTWORK)
6941   {
6942     setup_info = setup_info_artwork;
6943     title_string = STR_SETUP_ARTWORK;
6944   }
6945   else if (setup_mode == SETUP_MODE_TOUCH)
6946   {
6947     setup_info = setup_info_touch;
6948     title_string = STR_SETUP_TOUCH;
6949
6950     if (strEqual(setup.touch.control_type, TOUCH_CONTROL_VIRTUAL_BUTTONS))
6951       setup_info = setup_info_touch_virtual_buttons[GRID_ACTIVE_NR()];
6952     else if (strEqual(setup.touch.control_type, TOUCH_CONTROL_WIPE_GESTURES))
6953       setup_info = setup_info_touch_wipe_gestures;
6954   }
6955   else if (setup_mode == SETUP_MODE_SHORTCUTS)
6956   {
6957     setup_info = setup_info_shortcuts;
6958     title_string = STR_SETUP_SHORTCUTS;
6959   }
6960   else if (setup_mode == SETUP_MODE_SHORTCUTS_1)
6961   {
6962     setup_info = setup_info_shortcuts_1;
6963     title_string = STR_SETUP_SHORTCUTS;
6964   }
6965   else if (setup_mode == SETUP_MODE_SHORTCUTS_2)
6966   {
6967     setup_info = setup_info_shortcuts_2;
6968     title_string = STR_SETUP_SHORTCUTS;
6969   }
6970   else if (setup_mode == SETUP_MODE_SHORTCUTS_3)
6971   {
6972     setup_info = setup_info_shortcuts_3;
6973     title_string = STR_SETUP_SHORTCUTS;
6974   }
6975   else if (setup_mode == SETUP_MODE_SHORTCUTS_4)
6976   {
6977     setup_info = setup_info_shortcuts_4;
6978     title_string = STR_SETUP_SHORTCUTS;
6979   }
6980   else if (setup_mode == SETUP_MODE_SHORTCUTS_5)
6981   {
6982     setup_info = setup_info_shortcuts_5;
6983     title_string = STR_SETUP_SHORTCUTS;
6984   }
6985
6986   // use modified setup info without setup entries marked as hidden
6987   setup_info = getSetupInfoFinal(setup_info);
6988
6989   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, title_string);
6990
6991   // determine maximal number of setup entries that can be displayed on screen
6992   num_setup_info = 0;
6993   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
6994     num_setup_info++;
6995
6996   // determine maximal number of setup entries available for this setup screen
6997   max_setup_info = 0;
6998   for (i = 0; setup_info[i].type != 0; i++)
6999     max_setup_info++;
7000
7001   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
7002
7003   MapScreenGadgets(max_setup_info);
7004
7005   if (redraw_all)
7006     redraw_mask = fade_mask = REDRAW_ALL;
7007
7008   DrawMaskedBorder(fade_mask);
7009
7010   FadeIn(fade_mask);
7011 }
7012
7013 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
7014 {
7015   menu_info = setup_info;
7016
7017   HandleMenuScreen(mx, my, dx, dy, button,
7018                    setup_mode, num_setup_info, max_setup_info);
7019 }
7020
7021 static void DrawSetupScreen_Input(void)
7022 {
7023   int i;
7024
7025   FadeOut(REDRAW_FIELD);
7026
7027   ClearField();
7028
7029   setup_info = getSetupInfoFinal(setup_info_input);
7030
7031   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, STR_SETUP_INPUT);
7032
7033   for (i = 0; setup_info[i].type != 0; i++)
7034   {
7035     if (setup_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
7036       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
7037     else if (setup_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
7038       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
7039     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
7040       initCursor(i, IMG_MENU_BUTTON);
7041
7042     DrawCursorAndText_Setup(i, -1, FALSE);
7043   }
7044
7045   // create gadgets for setup input menu screen
7046   FreeScreenGadgets();
7047   CreateScreenGadgets();
7048
7049   // map gadgets for setup input menu screen
7050   MapScreenMenuGadgets(SCREEN_MASK_INPUT);
7051
7052   HandleSetupScreen_Input(0, 0, 0, 0, MB_MENU_INITIALIZE);
7053
7054   FadeIn(REDRAW_FIELD);
7055 }
7056
7057 static void setJoystickDeviceToNr(char *device_name, int device_nr)
7058 {
7059   if (device_name == NULL)
7060     return;
7061
7062   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
7063     device_nr = 0;
7064
7065   if (strlen(device_name) > 1)
7066   {
7067     char c1 = device_name[strlen(device_name) - 1];
7068     char c2 = device_name[strlen(device_name) - 2];
7069
7070     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
7071       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
7072   }
7073   else
7074     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
7075             strlen(device_name));
7076 }
7077
7078 static void drawPlayerSetupInputInfo(int player_nr, boolean active)
7079 {
7080   int i;
7081   static struct SetupKeyboardInfo custom_key;
7082   static struct
7083   {
7084     Key *key;
7085     char *text;
7086   } custom[] =
7087   {
7088     { &custom_key.left,  "Axis/Pad Left"  },
7089     { &custom_key.right, "Axis/Pad Right" },
7090     { &custom_key.up,    "Axis/Pad Up"    },
7091     { &custom_key.down,  "Axis/Pad Down"  },
7092     { &custom_key.snap,  "Button 1/A/X"   },
7093     { &custom_key.drop,  "Button 2/B/Y"   }
7094   };
7095   static char *joystick_name[MAX_PLAYERS] =
7096   {
7097     "Joystick1",
7098     "Joystick2",
7099     "Joystick3",
7100     "Joystick4"
7101   };
7102   int font_nr_menu = (active ? FONT_MENU_1_ACTIVE : FONT_MENU_1);
7103   int font_nr_info = FONT_MENU_1;
7104   int font_nr_name = FONT_VALUE_OLD;
7105   int font_nr_on   = FONT_VALUE_1;
7106   int font_nr_off  = FONT_VALUE_OLD;
7107   int pos = 4;
7108
7109   if (SCR_FIELDX < SCR_FIELDX_DEFAULT)
7110   {
7111     font_nr_info = FONT_MENU_2;
7112     font_nr_on   = FONT_VALUE_NARROW;
7113     font_nr_off  = FONT_VALUE_OLD_NARROW;
7114   }
7115
7116   custom_key = setup.input[player_nr].key;
7117
7118   DrawText(mSX + 11 * 32, mSY + 2 * 32, int2str(player_nr + 1, 1),
7119            FONT_INPUT_1_ACTIVE);
7120
7121   ClearRectangleOnBackground(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
7122                              TILEX, TILEY);
7123   DrawFixedGraphicThruMaskExt(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
7124                               PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
7125
7126   if (setup.input[player_nr].use_joystick)
7127   {
7128     char *device_name = setup.input[player_nr].joy.device_name;
7129     int joystick_nr = getJoystickNrFromDeviceName(device_name);
7130     boolean joystick_active = CheckJoystickOpened(joystick_nr);
7131     char *text = joystick_name[joystick_nr];
7132     int font_nr = (joystick_active ? font_nr_on : font_nr_off);
7133
7134     DrawText(mSX + 8 * 32, mSY + 3 * 32, text, font_nr);
7135     DrawText(mSX + 32, mSY + 4 * 32, "Configure", font_nr_menu);
7136   }
7137   else
7138   {
7139     DrawText(mSX + 8 * 32, mSY + 3 * 32, "Keyboard ", font_nr_on);
7140     DrawText(mSX + 1 * 32, mSY + 4 * 32, "Customize", font_nr_menu);
7141   }
7142
7143   if (SCR_FIELDY >= SCR_FIELDY_DEFAULT)
7144     DrawText(mSX + 32, mSY + 5 * 32, "Actual Settings:", font_nr_info);
7145   else
7146     pos = 3;
7147
7148   drawCursorXY(1, pos + 0, IMG_MENU_BUTTON_LEFT);
7149   drawCursorXY(1, pos + 1, IMG_MENU_BUTTON_RIGHT);
7150   drawCursorXY(1, pos + 2, IMG_MENU_BUTTON_UP);
7151   drawCursorXY(1, pos + 3, IMG_MENU_BUTTON_DOWN);
7152
7153   DrawText(mSX + 2 * 32, mSY + (pos + 2) * 32, ":", font_nr_name);
7154   DrawText(mSX + 2 * 32, mSY + (pos + 3) * 32, ":", font_nr_name);
7155   DrawText(mSX + 2 * 32, mSY + (pos + 4) * 32, ":", font_nr_name);
7156   DrawText(mSX + 2 * 32, mSY + (pos + 5) * 32, ":", font_nr_name);
7157   DrawText(mSX + 1 * 32, mSY + (pos + 6) * 32, "Snap Field:", font_nr_name);
7158   DrawText(mSX + 1 * 32, mSY + (pos + 8) * 32, "Drop Element:", font_nr_name);
7159
7160   for (i = 0; i < 6; i++)
7161   {
7162     int ypos = (pos + 2) + i + (i > 3 ? i - 3 : 0);
7163
7164     DrawText(mSX + 3 * 32, mSY + ypos * 32,
7165              "              ", font_nr_on);
7166     DrawText(mSX + 3 * 32, mSY + ypos * 32,
7167              (setup.input[player_nr].use_joystick ?
7168               custom[i].text :
7169               getKeyNameFromKey(*custom[i].key)), font_nr_on);
7170   }
7171 }
7172
7173 static int input_player_nr = 0;
7174
7175 static void HandleSetupScreen_Input_Player(int step, int direction)
7176 {
7177   int old_player_nr = input_player_nr;
7178   int new_player_nr;
7179
7180   new_player_nr = old_player_nr + step * direction;
7181   if (new_player_nr < 0)
7182     new_player_nr = 0;
7183   if (new_player_nr > MAX_PLAYERS - 1)
7184     new_player_nr = MAX_PLAYERS - 1;
7185
7186   if (new_player_nr != old_player_nr)
7187   {
7188     input_player_nr = new_player_nr;
7189
7190     drawPlayerSetupInputInfo(input_player_nr, FALSE);
7191   }
7192 }
7193
7194 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
7195 {
7196   static int choice = 0;
7197   int x = 0;
7198   int y = choice;
7199   int pos_start  = SETUPINPUT_SCREEN_POS_START;
7200   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
7201   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
7202   int pos_end    = SETUPINPUT_SCREEN_POS_END;
7203
7204   if (SCR_FIELDY < SCR_FIELDY_DEFAULT)
7205   {
7206     int i;
7207
7208     for (i = 0; setup_info_input[i].type != 0; i++)
7209     {
7210       // adjust menu structure according to skipped setup entries
7211       if (setup_info_input[i].type == TYPE_SKIPPABLE)
7212       {
7213         pos_empty2--;
7214         pos_end--;
7215       }
7216     }
7217   }
7218
7219   if (button == MB_MENU_INITIALIZE)
7220   {
7221     // input setup menu may have changed size due to graphics configuration
7222     if (choice >= pos_empty1)
7223       choice = pos_end;
7224
7225     drawPlayerSetupInputInfo(input_player_nr, (choice == 2));
7226
7227     DrawCursorAndText_Setup(choice, -1, TRUE);
7228
7229     return;
7230   }
7231   else if (button == MB_MENU_LEAVE)
7232   {
7233     setup_mode = SETUP_MODE_MAIN;
7234     DrawSetupScreen();
7235     InitJoysticks();
7236
7237     return;
7238   }
7239
7240   if (mx || my)         // mouse input
7241   {
7242     x = (mx - mSX) / 32;
7243     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
7244   }
7245   else if (dx || dy)    // keyboard input
7246   {
7247     if (dx && choice == 0)
7248       x = (dx < 0 ? 10 : 12);
7249     else if ((dx && choice == 1) ||
7250              (dx == -1 && choice == pos_end))
7251       button = MB_MENU_CHOICE;
7252     else if (dy)
7253       y = choice + dy;
7254
7255     if (y >= pos_empty1 && y <= pos_empty2)
7256       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
7257   }
7258
7259   if (y == 0 && dx != 0 && button)
7260   {
7261     HandleSetupScreen_Input_Player(1, dx < 0 ? -1 : +1);
7262   }
7263   else if (IN_VIS_FIELD(x, y) &&        // (does not use "IN_VIS_MENU()" yet)
7264            y >= pos_start && y <= pos_end &&
7265            !(y >= pos_empty1 && y <= pos_empty2))
7266   {
7267     if (button)
7268     {
7269       if (y != choice)
7270       {
7271         DrawCursorAndText_Setup(choice, -1, FALSE);
7272         DrawCursorAndText_Setup(y, -1, TRUE);
7273
7274         drawPlayerSetupInputInfo(input_player_nr, (y == 2));
7275
7276         choice = y;
7277       }
7278     }
7279     else
7280     {
7281       if (y == 1)
7282       {
7283         char *device_name = setup.input[input_player_nr].joy.device_name;
7284
7285         if (!setup.input[input_player_nr].use_joystick)
7286         {
7287           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
7288
7289           setJoystickDeviceToNr(device_name, new_device_nr);
7290           setup.input[input_player_nr].use_joystick = TRUE;
7291         }
7292         else
7293         {
7294           int device_nr = getJoystickNrFromDeviceName(device_name);
7295           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
7296
7297           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
7298             setup.input[input_player_nr].use_joystick = FALSE;
7299           else
7300             setJoystickDeviceToNr(device_name, new_device_nr);
7301         }
7302
7303         drawPlayerSetupInputInfo(input_player_nr, FALSE);
7304       }
7305       else if (y == 2)
7306       {
7307         if (setup.input[input_player_nr].use_joystick)
7308           ConfigureJoystick(input_player_nr);
7309         else
7310           CustomizeKeyboard(input_player_nr);
7311       }
7312       else if (y == pos_end)
7313       {
7314         InitJoysticks();
7315
7316         FadeSetLeaveMenu();
7317
7318         setup_mode = SETUP_MODE_MAIN;
7319         DrawSetupScreen();
7320       }
7321     }
7322   }
7323 }
7324
7325 static boolean CustomizeKeyboardMain(int player_nr)
7326 {
7327   int i;
7328   int step_nr;
7329   boolean finished = FALSE;
7330   static struct SetupKeyboardInfo custom_key;
7331   static struct
7332   {
7333     Key *key;
7334     char *text;
7335   } customize_step[] =
7336   {
7337     { &custom_key.left,  "Move Left"    },
7338     { &custom_key.right, "Move Right"   },
7339     { &custom_key.up,    "Move Up"      },
7340     { &custom_key.down,  "Move Down"    },
7341     { &custom_key.snap,  "Snap Field"   },
7342     { &custom_key.drop,  "Drop Element" }
7343   };
7344   int font_nr_old = FONT_VALUE_OLD;
7345   int font_nr_new = FONT_VALUE_1;
7346   boolean success = FALSE;
7347
7348   if (SCR_FIELDX < SCR_FIELDX_DEFAULT)
7349   {
7350     font_nr_old = FONT_VALUE_OLD_NARROW;
7351     font_nr_new = FONT_VALUE_NARROW;
7352   }
7353
7354   // read existing key bindings from player setup
7355   custom_key = setup.input[player_nr].key;
7356
7357   FadeSetEnterMenu();
7358   FadeOut(REDRAW_FIELD);
7359
7360   ClearField();
7361
7362   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Keyboard Input");
7363
7364   step_nr = 0;
7365   DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
7366            customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
7367   DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
7368            "Key:", FONT_INPUT_1_ACTIVE);
7369   DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
7370            getKeyNameFromKey(*customize_step[step_nr].key), font_nr_old);
7371
7372   FadeIn(REDRAW_FIELD);
7373
7374   while (!finished)
7375   {
7376     Event event;
7377
7378     if (NextValidEvent(&event))
7379     {
7380       switch (event.type)
7381       {
7382         case EVENT_KEYPRESS:
7383           {
7384             Key key = GetEventKey((KeyEvent *)&event, FALSE);
7385
7386             // press 'Escape' to abort and keep the old key bindings
7387             if (key == KSYM_Escape)
7388             {
7389               FadeSkipNextFadeIn();
7390
7391               finished = TRUE;
7392
7393               break;
7394             }
7395
7396             // press 'Enter' to keep the existing key binding
7397             if (key == KSYM_Return)
7398               key = *customize_step[step_nr].key;
7399
7400             // check if key already used
7401             for (i = 0; i < step_nr; i++)
7402               if (*customize_step[i].key == key)
7403                 break;
7404             if (i < step_nr)
7405               break;
7406
7407             // got new key binding
7408             *customize_step[step_nr].key = key;
7409             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
7410                      "             ", font_nr_new);
7411             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
7412                      getKeyNameFromKey(key), font_nr_new);
7413             step_nr++;
7414
7415             // un-highlight last query
7416             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1)) * 32,
7417                      customize_step[step_nr - 1].text, FONT_MENU_1);
7418             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1) + 1) * 32,
7419                      "Key:", FONT_MENU_1);
7420
7421             // all keys configured
7422             if (step_nr == 6)
7423             {
7424               finished = TRUE;
7425               success = TRUE;
7426
7427               break;
7428             }
7429
7430             // query next key binding
7431             DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
7432                      customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
7433             DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
7434                      "Key:", FONT_INPUT_1_ACTIVE);
7435             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
7436                      getKeyNameFromKey(*customize_step[step_nr].key),
7437                      font_nr_old);
7438           }
7439           break;
7440
7441         case EVENT_KEYRELEASE:
7442           key_joystick_mapping = 0;
7443           break;
7444
7445         default:
7446           HandleOtherEvents(&event);
7447           break;
7448       }
7449     }
7450
7451     BackToFront();
7452   }
7453
7454   // write new key bindings back to player setup, if successfully finished
7455   if (success)
7456     setup.input[player_nr].key = custom_key;
7457
7458   return success;
7459 }
7460
7461 void CustomizeKeyboard(int player_nr)
7462 {
7463   boolean success = CustomizeKeyboardMain(player_nr);
7464
7465   if (success)
7466   {
7467     int font_nr = FONT_TITLE_1;
7468     int font_height = getFontHeight(font_nr);
7469     int ypos1 = SYSIZE / 2 - font_height * 2;
7470     int ypos2 = SYSIZE / 2 - font_height * 1;
7471     unsigned int wait_frame_delay = 0;
7472     unsigned int wait_frame_delay_value = 2000;
7473
7474     ResetDelayCounter(&wait_frame_delay);
7475
7476     ClearField();
7477
7478     DrawTextSCentered(ypos1, font_nr, "Keyboard");
7479     DrawTextSCentered(ypos2, font_nr, "configured!");
7480
7481     while (!DelayReached(&wait_frame_delay, wait_frame_delay_value))
7482       BackToFront();
7483
7484     ClearEventQueue();
7485   }
7486
7487   DrawSetupScreen_Input();
7488 }
7489
7490 // game controller mapping generator by Gabriel Jacobo <gabomdq@gmail.com>
7491
7492 #define MARKER_BUTTON           1
7493 #define MARKER_AXIS_X           2
7494 #define MARKER_AXIS_Y           3
7495
7496 static boolean ConfigureJoystickMapButtonsAndAxes(SDL_Joystick *joystick)
7497 {
7498   static boolean bitmaps_initialized = FALSE;
7499   boolean screen_initialized = FALSE;
7500   static Bitmap *controller, *button, *axis_x, *axis_y;
7501   char *name;
7502   boolean success = TRUE;
7503   boolean done = FALSE, next = FALSE;
7504   Event event;
7505   int alpha = 200, alpha_step = -1;
7506   int alpha_ticks = 0;
7507   char mapping[4096], temp[4096];
7508   int font_name = MENU_SETUP_FONT_TITLE;
7509   int font_info = MENU_SETUP_FONT_TEXT;
7510   int spacing_name = menu.line_spacing_setup[SETUP_MODE_INPUT];
7511   int spacing_line = menu.line_spacing_setup[SETUP_MODE_INPUT];
7512   int spacing_para = menu.paragraph_spacing_setup[SETUP_MODE_INPUT];
7513   int ystep_name = getMenuTextStep(spacing_name, font_name);
7514   int ystep_line = getMenuTextStep(spacing_line, font_info);
7515   int ystep_para = getMenuTextStep(spacing_para, font_info);
7516   int i, j;
7517
7518   struct
7519   {
7520     int x, y;
7521     int marker;
7522     char *field;
7523     int axis, button, hat, hat_value;
7524     char mapping[4096];
7525   }
7526   *step, *prev_step, steps[] =
7527   {
7528     { 356, 155, MARKER_BUTTON, "a",             },
7529     { 396, 122, MARKER_BUTTON, "b",             },
7530     { 320, 125, MARKER_BUTTON, "x",             },
7531     { 358,  95, MARKER_BUTTON, "y",             },
7532     { 162, 125, MARKER_BUTTON, "back",          },
7533     { 216, 125, MARKER_BUTTON, "guide",         },
7534     { 271, 125, MARKER_BUTTON, "start",         },
7535     { 110, 200, MARKER_BUTTON, "dpleft",        },
7536     { 146, 228, MARKER_BUTTON, "dpdown",        },
7537     { 178, 200, MARKER_BUTTON, "dpright",       },
7538     { 146, 172, MARKER_BUTTON, "dpup",          },
7539     {  50,  40, MARKER_BUTTON, "leftshoulder",  },
7540     {  88, -10, MARKER_AXIS_Y, "lefttrigger",   },
7541     { 382,  40, MARKER_BUTTON, "rightshoulder", },
7542     { 346, -10, MARKER_AXIS_Y, "righttrigger",  },
7543     {  73, 141, MARKER_BUTTON, "leftstick",     },
7544     { 282, 210, MARKER_BUTTON, "rightstick",    },
7545     {  73, 141, MARKER_AXIS_X, "leftx",         },
7546     {  73, 141, MARKER_AXIS_Y, "lefty",         },
7547     { 282, 210, MARKER_AXIS_X, "rightx",        },
7548     { 282, 210, MARKER_AXIS_Y, "righty",        },
7549   };
7550
7551   unsigned int event_frame_delay = 0;
7552   unsigned int event_frame_delay_value = GAME_FRAME_DELAY;
7553
7554   ResetDelayCounter(&event_frame_delay);
7555
7556   if (!bitmaps_initialized)
7557   {
7558     controller = LoadCustomImage("joystick/controller.png");
7559     button     = LoadCustomImage("joystick/button.png");
7560     axis_x     = LoadCustomImage("joystick/axis_x.png");
7561     axis_y     = LoadCustomImage("joystick/axis_y.png");
7562
7563     bitmaps_initialized = TRUE;
7564   }
7565
7566   name = getFormattedJoystickName(SDL_JoystickName(joystick));
7567
7568 #if DEBUG_JOYSTICKS
7569   // print info about the joystick we are watching
7570   Error(ERR_DEBUG, "watching joystick %d: (%s)\n",
7571         SDL_JoystickInstanceID(joystick), name);
7572   Error(ERR_DEBUG, "joystick has %d axes, %d hats, %d balls, and %d buttons\n",
7573         SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
7574         SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
7575 #endif
7576
7577   // initialize mapping with GUID and name
7578   SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), temp, sizeof(temp));
7579
7580   snprintf(mapping, sizeof(mapping), "%s,%s,platform:%s,",
7581            temp, name, SDL_GetPlatform());
7582
7583   // loop through all steps (buttons and axes), getting joystick events
7584   for (i = 0; i < SDL_arraysize(steps) && !done;)
7585   {
7586     Bitmap *marker = button;    // initialize with reliable default value
7587
7588     step = &steps[i];
7589     strcpy(step->mapping, mapping);
7590     step->axis = -1;
7591     step->button = -1;
7592     step->hat = -1;
7593     step->hat_value = -1;
7594
7595     marker = (step->marker == MARKER_BUTTON ? button :
7596               step->marker == MARKER_AXIS_X ? axis_x :
7597               step->marker == MARKER_AXIS_Y ? axis_y : marker);
7598
7599     next = FALSE;
7600
7601     while (!done && !next)
7602     {
7603       alpha += alpha_step * (int)(SDL_GetTicks() - alpha_ticks) / 5;
7604       alpha_ticks = SDL_GetTicks();
7605
7606       if (alpha >= 255)
7607       {
7608         alpha = 255;
7609         alpha_step = -1;
7610       }
7611       else if (alpha < 128)
7612       {
7613         alpha = 127;
7614         alpha_step = 1;
7615       }
7616
7617       int controller_x = SX + (SXSIZE - controller->width) / 2;
7618       int controller_y = SY + ystep_line;
7619
7620       int marker_x = controller_x + step->x;
7621       int marker_y = controller_y + step->y;
7622
7623       int ystart1 = mSY - 2 * SY + controller_y + controller->height;
7624       int ystart2 = ystart1 + ystep_name + ystep_line;
7625
7626       ClearField();
7627
7628       DrawTextSCentered(ystart1, font_name, name);
7629
7630       DrawTextSCentered(ystart2, font_info,
7631                         "Press buttons and move axes on");
7632       ystart2 += ystep_line;
7633       DrawTextSCentered(ystart2, font_info,
7634                         "your controller when indicated.");
7635       ystart2 += ystep_line;
7636       DrawTextSCentered(ystart2, font_info,
7637                         "(Your controller may look different.)");
7638       ystart2 += ystep_para;
7639
7640 #if defined(PLATFORM_ANDROID)
7641       DrawTextSCentered(ystart2, font_info,
7642                         "To correct a mistake,");
7643       ystart2 += ystep_line;
7644       DrawTextSCentered(ystart2, font_info,
7645                         "press the 'back' button.");
7646       ystart2 += ystep_line;
7647       DrawTextSCentered(ystart2, font_info,
7648                         "To skip a button or axis,");
7649       ystart2 += ystep_line;
7650       DrawTextSCentered(ystart2, font_info,
7651                         "press the 'menu' button.");
7652 #else
7653       DrawTextSCentered(ystart2, font_info,
7654                         "To correct a mistake,");
7655       ystart2 += ystep_line;
7656       DrawTextSCentered(ystart2, font_info,
7657                         "press the 'backspace' key.");
7658       ystart2 += ystep_line;
7659       DrawTextSCentered(ystart2, font_info,
7660                         "To skip a button or axis,");
7661       ystart2 += ystep_line;
7662       DrawTextSCentered(ystart2, font_info,
7663                         "press the 'return' key.");
7664       ystart2 += ystep_line;
7665       DrawTextSCentered(ystart2, font_info,
7666                         "To exit, press the 'escape' key.");
7667 #endif
7668
7669       BlitBitmapMasked(controller, drawto, 0, 0,
7670                        controller->width, controller->height,
7671                        controller_x, controller_y);
7672
7673       SDL_SetSurfaceBlendMode(marker->surface_masked, SDL_BLENDMODE_BLEND);
7674       SDL_SetSurfaceAlphaMod(marker->surface_masked, alpha);
7675
7676       BlitBitmapMasked(marker, drawto, 0, 0,
7677                        marker->width, marker->height,
7678                        marker_x, marker_y);
7679
7680       if (!screen_initialized)
7681         FadeIn(REDRAW_FIELD);
7682       else
7683         BackToFront();
7684
7685       screen_initialized = TRUE;
7686
7687       while (NextValidEvent(&event))
7688       {
7689         switch (event.type)
7690         {
7691           case SDL_JOYAXISMOTION:
7692             if (event.jaxis.value > 20000 ||
7693                 event.jaxis.value < -20000)
7694             {
7695               for (j = 0; j < i; j++)
7696                 if (steps[j].axis == event.jaxis.axis)
7697                   break;
7698
7699               if (j == i)
7700               {
7701                 if (step->marker != MARKER_AXIS_X &&
7702                     step->marker != MARKER_AXIS_Y)
7703                   break;
7704
7705                 step->axis = event.jaxis.axis;
7706                 strcat(mapping, step->field);
7707                 snprintf(temp, sizeof(temp), ":a%u,", event.jaxis.axis);
7708                 strcat(mapping, temp);
7709                 i++;
7710                 next = TRUE;
7711               }
7712             }
7713
7714             break;
7715
7716           case SDL_JOYHATMOTION:
7717             // ignore centering; we're probably just coming back
7718             // to the center from the previous item we set
7719             if (event.jhat.value == SDL_HAT_CENTERED)
7720               break;
7721
7722             for (j = 0; j < i; j++)
7723               if (steps[j].hat == event.jhat.hat &&
7724                   steps[j].hat_value == event.jhat.value)
7725                 break;
7726
7727             if (j == i)
7728             {
7729               step->hat = event.jhat.hat;
7730               step->hat_value = event.jhat.value;
7731               strcat(mapping, step->field);
7732               snprintf(temp, sizeof(temp), ":h%u.%u,",
7733                        event.jhat.hat, event.jhat.value );
7734               strcat(mapping, temp);
7735               i++;
7736               next = TRUE;
7737             }
7738
7739             break;
7740
7741           case SDL_JOYBALLMOTION:
7742             break;
7743
7744           case SDL_JOYBUTTONUP:
7745             for (j = 0; j < i; j++)
7746               if (steps[j].button == event.jbutton.button)
7747                 break;
7748
7749             if (j == i)
7750             {
7751               step->button = event.jbutton.button;
7752               strcat(mapping, step->field);
7753               snprintf(temp, sizeof(temp), ":b%u,", event.jbutton.button);
7754               strcat(mapping, temp);
7755               i++;
7756               next = TRUE;
7757             }
7758
7759             break;
7760
7761           case SDL_FINGERDOWN:
7762           case SDL_MOUSEBUTTONDOWN:
7763             // skip this step
7764             i++;
7765             next = TRUE;
7766
7767             break;
7768
7769           case SDL_KEYDOWN:
7770             if (event.key.keysym.sym == KSYM_BackSpace ||
7771                 event.key.keysym.sym == KSYM_Back)
7772             {
7773               if (i == 0)
7774               {
7775                 // leave screen
7776                 success = FALSE;
7777                 done = TRUE;
7778
7779                 break;
7780               }
7781
7782               // undo this step
7783               prev_step = &steps[i - 1];
7784               strcpy(mapping, prev_step->mapping);
7785               i--;
7786               next = TRUE;
7787
7788               break;
7789             }
7790
7791             if (event.key.keysym.sym == KSYM_space ||
7792                 event.key.keysym.sym == KSYM_Return ||
7793                 event.key.keysym.sym == KSYM_Menu)
7794             {
7795               // skip this step
7796               i++;
7797               next = TRUE;
7798
7799               break;
7800             }
7801
7802             if (event.key.keysym.sym == KSYM_Escape)
7803             {
7804               // leave screen
7805               success = FALSE;
7806               done = TRUE;
7807             }
7808
7809             break;
7810
7811           case SDL_QUIT:
7812             program.exit_function(0);
7813             break;
7814
7815           default:
7816             break;
7817         }
7818
7819         // do not handle events for longer than standard frame delay period
7820         if (DelayReached(&event_frame_delay, event_frame_delay_value))
7821           break;
7822       }
7823     }
7824   }
7825
7826   if (success)
7827   {
7828 #if DEBUG_JOYSTICKS
7829     Error(ERR_DEBUG, "New game controller mapping:\n\n%s\n\n", mapping);
7830 #endif
7831
7832     // activate mapping for this game
7833     SDL_GameControllerAddMapping(mapping);
7834
7835     // save mapping to personal mappings
7836     SaveSetup_AddGameControllerMapping(mapping);
7837   }
7838
7839   // wait until the last pending event was removed from event queue
7840   while (NextValidEvent(&event));
7841
7842   return success;
7843 }
7844
7845 static int ConfigureJoystickMain(int player_nr)
7846 {
7847   char *device_name = setup.input[player_nr].joy.device_name;
7848   int joystick_nr = getJoystickNrFromDeviceName(device_name);
7849   boolean joystick_active = CheckJoystickOpened(joystick_nr);
7850   int success = FALSE;
7851   int i;
7852
7853   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
7854     return JOYSTICK_NOT_AVAILABLE;
7855
7856   if (!joystick_active || !setup.input[player_nr].use_joystick)
7857     return JOYSTICK_NOT_AVAILABLE;
7858
7859   FadeSetEnterMenu();
7860   FadeOut(REDRAW_FIELD);
7861
7862   // close all joystick devices (potentially opened as game controllers)
7863   for (i = 0; i < SDL_NumJoysticks(); i++)
7864     SDLCloseJoystick(i);
7865
7866   // open joystick device as plain joystick to configure as game controller
7867   SDL_Joystick *joystick = SDL_JoystickOpen(joystick_nr);
7868
7869   // as the joystick was successfully opened before, this should not happen
7870   if (joystick == NULL)
7871     return FALSE;
7872
7873   // create new game controller mapping (buttons and axes) for joystick device
7874   success = ConfigureJoystickMapButtonsAndAxes(joystick);
7875
7876   // close joystick (and maybe re-open as configured game controller later)
7877   SDL_JoystickClose(joystick);
7878
7879   // re-open all joystick devices (potentially as game controllers)
7880   for (i = 0; i < SDL_NumJoysticks(); i++)
7881     SDLOpenJoystick(i);
7882
7883   // clear all joystick input actions for all joystick devices
7884   SDLClearJoystickState();
7885
7886   return (success ? JOYSTICK_CONFIGURED : JOYSTICK_NOT_CONFIGURED);
7887 }
7888
7889 void ConfigureJoystick(int player_nr)
7890 {
7891   boolean state = ConfigureJoystickMain(player_nr);
7892
7893   if (state != JOYSTICK_NOT_CONFIGURED)
7894   {
7895     boolean success = (state == JOYSTICK_CONFIGURED);
7896     char message1[MAX_OUTPUT_LINESIZE + 1];
7897     char *message2 = (success ? "configured!" : "not available!");
7898     char *device_name = setup.input[player_nr].joy.device_name;
7899     int nr = getJoystickNrFromDeviceName(device_name) + 1;
7900     int font_nr = FONT_TITLE_1;
7901     int font_height = getFontHeight(font_nr);
7902     int ypos1 = SYSIZE / 2 - font_height * 2;
7903     int ypos2 = SYSIZE / 2 - font_height * 1;
7904     unsigned int wait_frame_delay = 0;
7905     unsigned int wait_frame_delay_value = 2000;
7906
7907     ResetDelayCounter(&wait_frame_delay);
7908
7909     ClearField();
7910
7911     sprintf(message1, "Joystick %d", nr);
7912
7913     DrawTextSCentered(ypos1, font_nr, message1);
7914     DrawTextSCentered(ypos2, font_nr, message2);
7915
7916     while (!DelayReached(&wait_frame_delay, wait_frame_delay_value))
7917       BackToFront();
7918
7919     ClearEventQueue();
7920   }
7921
7922   DrawSetupScreen_Input();
7923 }
7924
7925 static void MapScreenMenuGadgets_OverlayTouchButtons(int y)
7926 {
7927   if (y < video.screen_height / 3)
7928   {
7929     // remap touch gadgets to access upper part of the screen
7930     UnmapScreenMenuGadgets(SCREEN_MASK_TOUCH);
7931     MapScreenMenuGadgets(SCREEN_MASK_TOUCH2);
7932   }
7933   else if (y > 2 * video.screen_height / 3)
7934   {
7935     // remap touch gadgets to access lower part of the screen
7936     MapScreenMenuGadgets(SCREEN_MASK_TOUCH);
7937     UnmapScreenMenuGadgets(SCREEN_MASK_TOUCH2);
7938   }
7939 }
7940
7941 static boolean ConfigureVirtualButtonsMain(void)
7942 {
7943   static char *customize_step_text[] =
7944   {
7945     "Move Left",
7946     "Move Right",
7947     "Move Up",
7948     "Move Down",
7949     "Snap Field",
7950     "Drop Element"
7951   };
7952   char grid_button[] =
7953   {
7954     CHAR_GRID_BUTTON_LEFT,
7955     CHAR_GRID_BUTTON_RIGHT,
7956     CHAR_GRID_BUTTON_UP,
7957     CHAR_GRID_BUTTON_DOWN,
7958     CHAR_GRID_BUTTON_SNAP,
7959     CHAR_GRID_BUTTON_DROP
7960   };
7961   enum
7962   {
7963     ACTION_NONE,
7964     ACTION_ESCAPE,
7965     ACTION_BACK,
7966     ACTION_NEXT
7967   };
7968   int font_nr = FONT_INPUT_1_ACTIVE;
7969   int font_height = getFontHeight(font_nr);
7970   int ypos1 = SYSIZE / 2 - font_height * 2;
7971   int ypos2 = SYSIZE / 2 - font_height * 1;
7972   boolean success = FALSE;
7973   boolean finished = FALSE;
7974   int step_nr = 0;
7975   char grid_button_draw = CHAR_GRID_BUTTON_NONE;
7976   char grid_button_old[MAX_GRID_XSIZE][MAX_GRID_YSIZE];
7977   char grid_button_tmp[MAX_GRID_XSIZE][MAX_GRID_YSIZE];
7978   boolean set_grid_button = FALSE;
7979   int nr = GRID_ACTIVE_NR();
7980   int x, y;
7981
7982   for (x = 0; x < MAX_GRID_XSIZE; x++)
7983     for (y = 0; y < MAX_GRID_YSIZE; y++)
7984       grid_button_old[x][y] = grid_button_tmp[x][y] = overlay.grid_button[x][y];
7985
7986   overlay.grid_button_highlight = grid_button[step_nr];
7987
7988   FadeSetEnterMenu();
7989   FadeOut(REDRAW_FIELD);
7990
7991   ClearField();
7992
7993   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Virtual Buttons");
7994   DrawTextSCentered(ypos1, font_nr, "Select tiles to");
7995   DrawTextSCentered(ypos2, font_nr, customize_step_text[step_nr]);
7996
7997   FadeIn(REDRAW_FIELD);
7998
7999   SetOverlayShowGrid(TRUE);
8000
8001   // map gadgets for setup touch buttons menu screen
8002   MapScreenMenuGadgets(SCREEN_MASK_TOUCH);
8003
8004   while (!finished)
8005   {
8006     Event event;
8007
8008     while (NextValidEvent(&event))
8009     {
8010       int action = ACTION_NONE;
8011
8012       // ---------- handle events and set the resulting action ----------
8013
8014       switch (event.type)
8015       {
8016         case EVENT_USER:
8017           {
8018             UserEvent *user = (UserEvent *)&event;
8019             int id = user->value1;
8020
8021             action = (id == SCREEN_CTRL_ID_TOUCH_PREV_PAGE ||
8022                       id == SCREEN_CTRL_ID_TOUCH_PREV_PAGE2 ? ACTION_BACK :
8023                       id == SCREEN_CTRL_ID_TOUCH_NEXT_PAGE ||
8024                       id == SCREEN_CTRL_ID_TOUCH_NEXT_PAGE2 ? ACTION_NEXT :
8025                       ACTION_NONE);
8026           }
8027           break;
8028
8029         case EVENT_KEYPRESS:
8030           {
8031             Key key = GetEventKey((KeyEvent *)&event, FALSE);
8032
8033             action = (key == KSYM_Escape ?      ACTION_ESCAPE :
8034                       key == KSYM_BackSpace ||
8035                       key == KSYM_Back ?        ACTION_BACK :
8036                       key == KSYM_Return ||
8037                       key == KSYM_Menu ||
8038                       key == KSYM_space ?       ACTION_NEXT :
8039                       ACTION_NONE);
8040           }
8041           break;
8042
8043         case EVENT_KEYRELEASE:
8044           key_joystick_mapping = 0;
8045           break;
8046
8047         case EVENT_BUTTONPRESS:
8048         case EVENT_BUTTONRELEASE:
8049           {
8050             ButtonEvent *button = (ButtonEvent *)&event;
8051
8052             motion_status = FALSE;
8053
8054             if (button->type == EVENT_BUTTONPRESS)
8055               button_status = button->button;
8056             else
8057               button_status = MB_RELEASED;
8058
8059             if (HandleGadgets(button->x, button->y, button_status))
8060             {
8061               // do not handle this button event anymore
8062               break;
8063             }
8064
8065             button->x += video.screen_xoffset;
8066             button->y += video.screen_yoffset;
8067
8068             x = button->x * overlay.grid_xsize / video.screen_width;
8069             y = button->y * overlay.grid_ysize / video.screen_height;
8070
8071             if (button->type == EVENT_BUTTONPRESS)
8072             {
8073               grid_button_draw =
8074                 (overlay.grid_button[x][y] != grid_button[step_nr] ?
8075                  grid_button[step_nr] : CHAR_GRID_BUTTON_NONE);
8076
8077               set_grid_button = TRUE;
8078             }
8079
8080             MapScreenMenuGadgets_OverlayTouchButtons(button->y);
8081           }
8082           break;
8083
8084         case EVENT_MOTIONNOTIFY:
8085           {
8086             MotionEvent *motion = (MotionEvent *)&event;
8087
8088             motion_status = TRUE;
8089
8090             if (HandleGadgets(motion->x, motion->y, button_status))
8091             {
8092               // do not handle this button event anymore
8093               break;
8094             }
8095
8096             motion->x += video.screen_xoffset;
8097             motion->y += video.screen_yoffset;
8098
8099             x = motion->x * overlay.grid_xsize / video.screen_width;
8100             y = motion->y * overlay.grid_ysize / video.screen_height;
8101
8102             set_grid_button = TRUE;
8103
8104             MapScreenMenuGadgets_OverlayTouchButtons(motion->y);
8105           }
8106           break;
8107
8108         case SDL_WINDOWEVENT:
8109           HandleWindowEvent((WindowEvent *) &event);
8110
8111           // check if device has been rotated
8112           if (nr != GRID_ACTIVE_NR())
8113           {
8114             nr = GRID_ACTIVE_NR();
8115
8116             for (x = 0; x < MAX_GRID_XSIZE; x++)
8117               for (y = 0; y < MAX_GRID_YSIZE; y++)
8118                 grid_button_old[x][y] = grid_button_tmp[x][y] =
8119                   overlay.grid_button[x][y];
8120           }
8121
8122           break;
8123
8124         case SDL_APP_WILLENTERBACKGROUND:
8125         case SDL_APP_DIDENTERBACKGROUND:
8126         case SDL_APP_WILLENTERFOREGROUND:
8127         case SDL_APP_DIDENTERFOREGROUND:
8128           HandlePauseResumeEvent((PauseResumeEvent *) &event);
8129           break;
8130
8131         default:
8132           HandleOtherEvents(&event);
8133           break;
8134       }
8135
8136       // ---------- perform action set by handling events ----------
8137
8138       if (action == ACTION_ESCAPE)
8139       {
8140         // abort and restore the old key bindings
8141
8142         for (x = 0; x < MAX_GRID_XSIZE; x++)
8143           for (y = 0; y < MAX_GRID_YSIZE; y++)
8144             overlay.grid_button[x][y] = grid_button_old[x][y];
8145
8146         FadeSkipNextFadeIn();
8147
8148         finished = TRUE;
8149       }
8150       else if (action == ACTION_BACK)
8151       {
8152         // keep the configured key bindings and go to previous page
8153
8154         step_nr--;
8155
8156         if (step_nr < 0)
8157         {
8158           FadeSkipNextFadeIn();
8159
8160           finished = TRUE;
8161         }
8162       }
8163       else if (action == ACTION_NEXT)
8164       {
8165         // keep the configured key bindings and go to next page
8166
8167         step_nr++;
8168
8169         // all virtual buttons configured
8170         if (step_nr == 6)
8171         {
8172           finished = TRUE;
8173           success = TRUE;
8174         }
8175       }
8176
8177       if (action != ACTION_NONE && !finished)
8178       {
8179         for (x = 0; x < MAX_GRID_XSIZE; x++)
8180           for (y = 0; y < MAX_GRID_YSIZE; y++)
8181             grid_button_tmp[x][y] = overlay.grid_button[x][y];
8182
8183         overlay.grid_button_highlight = grid_button[step_nr];
8184
8185         // configure next virtual button
8186
8187         ClearField();
8188
8189         DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Virtual Buttons");
8190         DrawTextSCentered(ypos1, font_nr, "Select tiles to");
8191         DrawTextSCentered(ypos2, font_nr, customize_step_text[step_nr]);
8192       }
8193
8194       if (set_grid_button)
8195       {
8196         overlay.grid_button[x][y] =
8197           (grid_button_draw != CHAR_GRID_BUTTON_NONE ? grid_button_draw :
8198            grid_button_tmp[x][y] == grid_button[step_nr] ? CHAR_GRID_BUTTON_NONE :
8199            grid_button_tmp[x][y]);
8200
8201         set_grid_button = FALSE;
8202       }
8203     }
8204
8205     BackToFront();
8206   }
8207
8208   for (x = 0; x < MAX_GRID_XSIZE; x++)
8209     for (y = 0; y < MAX_GRID_YSIZE; y++)
8210       setup.touch.grid_button[nr][x][y] = overlay.grid_button[x][y];
8211
8212   overlay.grid_button_highlight = CHAR_GRID_BUTTON_NONE;
8213
8214   SetOverlayShowGrid(FALSE);
8215
8216   return success;
8217 }
8218
8219 void ConfigureVirtualButtons(void)
8220 {
8221   boolean success = ConfigureVirtualButtonsMain();
8222
8223   UnmapScreenMenuGadgets(SCREEN_MASK_TOUCH |
8224                          SCREEN_MASK_TOUCH2);
8225
8226   if (success)
8227   {
8228     int font_nr = FONT_TITLE_1;
8229     int font_height = getFontHeight(font_nr);
8230     int ypos1 = SYSIZE / 2 - font_height * 2;
8231     int ypos2 = SYSIZE / 2 - font_height * 1;
8232     unsigned int wait_frame_delay = 0;
8233     unsigned int wait_frame_delay_value = 2000;
8234
8235     ResetDelayCounter(&wait_frame_delay);
8236
8237     ClearField();
8238
8239     DrawTextSCentered(ypos1, font_nr, "Virtual buttons");
8240     DrawTextSCentered(ypos2, font_nr, "configured!");
8241
8242     while (!DelayReached(&wait_frame_delay, wait_frame_delay_value))
8243       BackToFront();
8244
8245     ClearEventQueue();
8246   }
8247 }
8248
8249 void DrawSetupScreen(void)
8250 {
8251   align_xoffset = 0;
8252   align_yoffset = 0;
8253
8254   if (setup_mode == SETUP_MODE_INPUT)
8255     DrawSetupScreen_Input();
8256   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
8257     DrawChooseTree(&game_speed_current);
8258   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
8259     DrawChooseTree(&scroll_delay_current);
8260   else if (setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
8261     DrawChooseTree(&snapshot_mode_current);
8262   else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
8263     DrawChooseTree(&window_size_current);
8264   else if (setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
8265     DrawChooseTree(&scaling_type_current);
8266   else if (setup_mode == SETUP_MODE_CHOOSE_RENDERING)
8267     DrawChooseTree(&rendering_mode_current);
8268   else if (setup_mode == SETUP_MODE_CHOOSE_VSYNC)
8269     DrawChooseTree(&vsync_mode_current);
8270   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
8271     DrawChooseTree(&artwork.gfx_current);
8272   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
8273     DrawChooseTree(&artwork.snd_current);
8274   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
8275     DrawChooseTree(&artwork.mus_current);
8276   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE)
8277     DrawChooseTree(&volume_simple_current);
8278   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS)
8279     DrawChooseTree(&volume_loops_current);
8280   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
8281     DrawChooseTree(&volume_music_current);
8282   else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL)
8283     DrawChooseTree(&touch_control_current);
8284   else if (setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE)
8285     DrawChooseTree(&move_distance_current);
8286   else if (setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
8287     DrawChooseTree(&drop_distance_current);
8288   else if (setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY)
8289     DrawChooseTree(&transparency_current);
8290   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_0)
8291     DrawChooseTree(&grid_size_current[0][0]);
8292   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_0)
8293     DrawChooseTree(&grid_size_current[0][1]);
8294   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_1)
8295     DrawChooseTree(&grid_size_current[1][0]);
8296   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_1)
8297     DrawChooseTree(&grid_size_current[1][1]);
8298   else
8299     DrawSetupScreen_Generic();
8300
8301   PlayMenuSoundsAndMusic();
8302 }
8303
8304 void RedrawSetupScreenAfterFullscreenToggle(void)
8305 {
8306   if (setup_mode == SETUP_MODE_GRAPHICS ||
8307       setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
8308   {
8309     // update list selection from "setup.window_scaling_percent"
8310     execSetupGraphics_setWindowSizes(TRUE);
8311
8312     DrawSetupScreen();
8313   }
8314 }
8315
8316 void RedrawSetupScreenAfterScreenRotation(int nr)
8317 {
8318   int x, y;
8319
8320   if (setup_mode == SETUP_MODE_TOUCH)
8321   {
8322     // update virtual button settings (depending on screen orientation)
8323     DrawSetupScreen();
8324   }
8325   else if (setup_mode == SETUP_MODE_CONFIG_VIRT_BUTTONS)
8326   {
8327     // save already configured virtual buttons
8328     for (x = 0; x < MAX_GRID_XSIZE; x++)
8329       for (y = 0; y < MAX_GRID_YSIZE; y++)
8330         setup.touch.grid_button[nr][x][y] = overlay.grid_button[x][y];
8331   }
8332 }
8333
8334 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
8335 {
8336   if (setup_mode == SETUP_MODE_INPUT)
8337     HandleSetupScreen_Input(mx, my, dx, dy, button);
8338   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
8339     HandleChooseTree(mx, my, dx, dy, button, &game_speed_current);
8340   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
8341     HandleChooseTree(mx, my, dx, dy, button, &scroll_delay_current);
8342   else if (setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
8343     HandleChooseTree(mx, my, dx, dy, button, &snapshot_mode_current);
8344   else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
8345     HandleChooseTree(mx, my, dx, dy, button, &window_size_current);
8346   else if (setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
8347     HandleChooseTree(mx, my, dx, dy, button, &scaling_type_current);
8348   else if (setup_mode == SETUP_MODE_CHOOSE_RENDERING)
8349     HandleChooseTree(mx, my, dx, dy, button, &rendering_mode_current);
8350   else if (setup_mode == SETUP_MODE_CHOOSE_VSYNC)
8351     HandleChooseTree(mx, my, dx, dy, button, &vsync_mode_current);
8352   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
8353     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
8354   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
8355     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
8356   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
8357     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
8358   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE)
8359     HandleChooseTree(mx, my, dx, dy, button, &volume_simple_current);
8360   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS)
8361     HandleChooseTree(mx, my, dx, dy, button, &volume_loops_current);
8362   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
8363     HandleChooseTree(mx, my, dx, dy, button, &volume_music_current);
8364   else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL)
8365     HandleChooseTree(mx, my, dx, dy, button, &touch_control_current);
8366   else if (setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE)
8367     HandleChooseTree(mx, my, dx, dy, button, &move_distance_current);
8368   else if (setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
8369     HandleChooseTree(mx, my, dx, dy, button, &drop_distance_current);
8370   else if (setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY)
8371     HandleChooseTree(mx, my, dx, dy, button, &transparency_current);
8372   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_0)
8373     HandleChooseTree(mx, my, dx, dy, button, &grid_size_current[0][0]);
8374   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_0)
8375     HandleChooseTree(mx, my, dx, dy, button, &grid_size_current[0][1]);
8376   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_1)
8377     HandleChooseTree(mx, my, dx, dy, button, &grid_size_current[1][0]);
8378   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_1)
8379     HandleChooseTree(mx, my, dx, dy, button, &grid_size_current[1][1]);
8380   else
8381     HandleSetupScreen_Generic(mx, my, dx, dy, button);
8382 }
8383
8384 void HandleGameActions(void)
8385 {
8386   if (setup.ask_on_game_over)
8387     CheckGameOver();
8388
8389   if (game.restart_game_message != NULL)
8390   {
8391     RequestRestartGame(game.restart_game_message);
8392
8393     return;
8394   }
8395
8396   if (game_status != GAME_MODE_PLAYING)
8397     return;
8398
8399   GameActions();        // main game loop
8400
8401   if (tape.auto_play && !tape.playing)
8402     AutoPlayTape();     // continue automatically playing next tape
8403 }
8404
8405
8406 // ---------- new screen button stuff --------------------------------------
8407
8408 static struct
8409 {
8410   int gfx_unpressed, gfx_pressed;
8411   struct MenuPosInfo *pos;
8412   boolean *check_value;
8413   int gadget_id;
8414   int screen_mask;
8415   unsigned int event_mask;
8416   boolean is_touch_button;
8417   char *infotext;
8418 } menubutton_info[NUM_SCREEN_MENUBUTTONS] =
8419 {
8420   {
8421     IMG_MENU_BUTTON_PREV_LEVEL, IMG_MENU_BUTTON_PREV_LEVEL_ACTIVE,
8422     &menu.main.button.prev_level, NULL,
8423     SCREEN_CTRL_ID_PREV_LEVEL,
8424     SCREEN_MASK_MAIN,
8425     GD_EVENT_PRESSED | GD_EVENT_REPEATED,
8426     FALSE, "previous level"
8427   },
8428   {
8429     IMG_MENU_BUTTON_NEXT_LEVEL, IMG_MENU_BUTTON_NEXT_LEVEL_ACTIVE,
8430     &menu.main.button.next_level, NULL,
8431     SCREEN_CTRL_ID_NEXT_LEVEL,
8432     SCREEN_MASK_MAIN,
8433     GD_EVENT_PRESSED | GD_EVENT_REPEATED,
8434     FALSE, "next level"
8435   },
8436   {
8437     IMG_MENU_BUTTON_FIRST_LEVEL, IMG_MENU_BUTTON_FIRST_LEVEL_ACTIVE,
8438     &menu.main.button.first_level, NULL,
8439     SCREEN_CTRL_ID_FIRST_LEVEL,
8440     SCREEN_MASK_MAIN,
8441     GD_EVENT_RELEASED,
8442     FALSE, "first level"
8443   },
8444   {
8445     IMG_MENU_BUTTON_LAST_LEVEL, IMG_MENU_BUTTON_LAST_LEVEL_ACTIVE,
8446     &menu.main.button.last_level, NULL,
8447     SCREEN_CTRL_ID_LAST_LEVEL,
8448     SCREEN_MASK_MAIN,
8449     GD_EVENT_RELEASED,
8450     FALSE, "last level"
8451   },
8452   {
8453     IMG_MENU_BUTTON_LEVEL_NUMBER, IMG_MENU_BUTTON_LEVEL_NUMBER_ACTIVE,
8454     &menu.main.button.level_number, NULL,
8455     SCREEN_CTRL_ID_LEVEL_NUMBER,
8456     SCREEN_MASK_MAIN,
8457     GD_EVENT_RELEASED,
8458     FALSE, "level number"
8459   },
8460   {
8461     IMG_MENU_BUTTON_LEFT, IMG_MENU_BUTTON_LEFT_ACTIVE,
8462     &menu.setup.button.prev_player, NULL,
8463     SCREEN_CTRL_ID_PREV_PLAYER,
8464     SCREEN_MASK_INPUT,
8465     GD_EVENT_PRESSED | GD_EVENT_REPEATED,
8466     FALSE, "previous player"
8467   },
8468   {
8469     IMG_MENU_BUTTON_RIGHT, IMG_MENU_BUTTON_RIGHT_ACTIVE,
8470     &menu.setup.button.next_player, NULL,
8471     SCREEN_CTRL_ID_NEXT_PLAYER,
8472     SCREEN_MASK_INPUT,
8473     GD_EVENT_PRESSED | GD_EVENT_REPEATED,
8474     FALSE, "next player"
8475   },
8476   {
8477     IMG_MENU_BUTTON_INSERT_SOLUTION, IMG_MENU_BUTTON_INSERT_SOLUTION_ACTIVE,
8478     &menu.main.button.insert_solution, NULL,
8479     SCREEN_CTRL_ID_INSERT_SOLUTION,
8480     SCREEN_MASK_MAIN_HAS_SOLUTION,
8481     GD_EVENT_RELEASED,
8482     FALSE, "insert solution tape"
8483   },
8484   {
8485     IMG_MENU_BUTTON_PLAY_SOLUTION, IMG_MENU_BUTTON_PLAY_SOLUTION_ACTIVE,
8486     &menu.main.button.play_solution, NULL,
8487     SCREEN_CTRL_ID_PLAY_SOLUTION,
8488     SCREEN_MASK_MAIN_HAS_SOLUTION,
8489     GD_EVENT_RELEASED,
8490     FALSE, "play solution tape"
8491   },
8492   {
8493     IMG_MENU_BUTTON_SWITCH_ECS_AGA, IMG_MENU_BUTTON_SWITCH_ECS_AGA_ACTIVE,
8494     &menu.main.button.switch_ecs_aga, &setup.prefer_aga_graphics,
8495     SCREEN_CTRL_ID_SWITCH_ECS_AGA,
8496     SCREEN_MASK_MAIN,
8497     GD_EVENT_RELEASED | GD_EVENT_OFF_BORDERS,
8498     FALSE, "switch ECS/AGA chipset"
8499   },
8500   {
8501     IMG_MENU_BUTTON_TOUCH_BACK, IMG_MENU_BUTTON_TOUCH_BACK,
8502     &menu.setup.button.touch_back, NULL,
8503     SCREEN_CTRL_ID_TOUCH_PREV_PAGE,
8504     SCREEN_MASK_TOUCH,
8505     GD_EVENT_RELEASED,
8506     TRUE, "previous page"
8507   },
8508   {
8509     IMG_MENU_BUTTON_TOUCH_NEXT, IMG_MENU_BUTTON_TOUCH_NEXT,
8510     &menu.setup.button.touch_next, NULL,
8511     SCREEN_CTRL_ID_TOUCH_NEXT_PAGE,
8512     SCREEN_MASK_TOUCH,
8513     GD_EVENT_RELEASED,
8514     TRUE, "next page"
8515   },
8516   {
8517     IMG_MENU_BUTTON_TOUCH_BACK2, IMG_MENU_BUTTON_TOUCH_BACK2,
8518     &menu.setup.button.touch_back2, NULL,
8519     SCREEN_CTRL_ID_TOUCH_PREV_PAGE2,
8520     SCREEN_MASK_TOUCH2,
8521     GD_EVENT_RELEASED,
8522     TRUE, "previous page"
8523   },
8524   {
8525     IMG_MENU_BUTTON_TOUCH_NEXT2, IMG_MENU_BUTTON_TOUCH_NEXT2,
8526     &menu.setup.button.touch_next2, NULL,
8527     SCREEN_CTRL_ID_TOUCH_NEXT_PAGE2,
8528     SCREEN_MASK_TOUCH2,
8529     GD_EVENT_RELEASED,
8530     TRUE, "next page"
8531   },
8532 };
8533
8534 static struct
8535 {
8536   int gfx_unpressed, gfx_pressed;
8537   int x, y;
8538   int gadget_id;
8539   char *infotext;
8540 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
8541 {
8542   {
8543     IMG_MENU_BUTTON_UP, IMG_MENU_BUTTON_UP_ACTIVE,
8544     -1, -1,     // these values are not constant, but can change at runtime
8545     SCREEN_CTRL_ID_SCROLL_UP,
8546     "scroll up"
8547   },
8548   {
8549     IMG_MENU_BUTTON_DOWN, IMG_MENU_BUTTON_DOWN_ACTIVE,
8550     -1, -1,     // these values are not constant, but can change at runtime
8551     SCREEN_CTRL_ID_SCROLL_DOWN,
8552     "scroll down"
8553   }
8554 };
8555
8556 static struct
8557 {
8558   int gfx_unpressed, gfx_pressed;
8559   int x, y;
8560   int width, height;
8561   int type;
8562   int gadget_id;
8563   char *infotext;
8564 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
8565 {
8566   {
8567     IMG_MENU_SCROLLBAR, IMG_MENU_SCROLLBAR_ACTIVE,
8568     -1, -1,     // these values are not constant, but can change at runtime
8569     -1, -1,     // these values are not constant, but can change at runtime
8570     GD_TYPE_SCROLLBAR_VERTICAL,
8571     SCREEN_CTRL_ID_SCROLL_VERTICAL,
8572     "scroll level series vertically"
8573   }
8574 };
8575
8576 static struct
8577 {
8578   int graphic;
8579   int gadget_id;
8580   int x, y;
8581   int size;
8582   char *value;
8583   char *infotext;
8584 } textinput_info[NUM_SCREEN_TEXTINPUT] =
8585 {
8586   {
8587     IMG_SETUP_INPUT_TEXT,
8588     SCREEN_CTRL_ID_NETWORK_SERVER,
8589     -1, -1,     // these values are not constant, but can change at runtime
8590     MAX_SETUP_TEXT_INPUT_LEN,
8591     network_server_hostname,
8592     "Network Server Hostname / IP"
8593   },
8594 };
8595
8596 static void CreateScreenMenubuttons(void)
8597 {
8598   struct GadgetInfo *gi;
8599   unsigned int event_mask;
8600   int i;
8601
8602   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
8603   {
8604     struct MenuPosInfo *pos = menubutton_info[i].pos;
8605     boolean is_touch_button = menubutton_info[i].is_touch_button;
8606     boolean is_check_button = menubutton_info[i].check_value != NULL;
8607     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
8608     int gfx_unpressed, gfx_pressed;
8609     int x, y, width, height;
8610     int gd_x1, gd_x2, gd_y1, gd_y2;
8611     int gd_x1a, gd_x2a, gd_y1a, gd_y2a;
8612     int id = menubutton_info[i].gadget_id;
8613     int type = GD_TYPE_NORMAL_BUTTON;
8614     boolean checked = FALSE;
8615
8616     event_mask = menubutton_info[i].event_mask;
8617
8618     x = (is_touch_button ? pos->x : mSX + GDI_ACTIVE_POS(pos->x));
8619     y = (is_touch_button ? pos->y : mSY + GDI_ACTIVE_POS(pos->y));
8620
8621     width  = graphic_info[menubutton_info[i].gfx_pressed].width;
8622     height = graphic_info[menubutton_info[i].gfx_pressed].height;
8623
8624     gfx_unpressed = menubutton_info[i].gfx_unpressed;
8625     gfx_pressed   = menubutton_info[i].gfx_pressed;
8626     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
8627     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
8628     gd_x1 = graphic_info[gfx_unpressed].src_x;
8629     gd_y1 = graphic_info[gfx_unpressed].src_y;
8630     gd_x2 = graphic_info[gfx_pressed].src_x;
8631     gd_y2 = graphic_info[gfx_pressed].src_y;
8632     gd_x1a = gd_x1;
8633     gd_y1a = gd_y1;
8634     gd_x2a = gd_x2;
8635     gd_y2a = gd_y2;
8636
8637     if (is_touch_button)
8638     {
8639       gd_x2 += graphic_info[gfx_pressed].pressed_xoffset;
8640       gd_y2 += graphic_info[gfx_pressed].pressed_yoffset;
8641     }
8642
8643     if (is_check_button)
8644     {
8645       gd_x1a += graphic_info[gfx_unpressed].active_xoffset;
8646       gd_y1a += graphic_info[gfx_unpressed].active_yoffset;
8647       gd_x2a += graphic_info[gfx_pressed].active_xoffset;
8648       gd_y2a += graphic_info[gfx_pressed].active_yoffset;
8649
8650       type = GD_TYPE_CHECK_BUTTON;
8651       checked = *menubutton_info[i].check_value;
8652     }
8653
8654     gi = CreateGadget(GDI_CUSTOM_ID, id,
8655                       GDI_CUSTOM_TYPE_ID, i,
8656                       GDI_IMAGE_ID, gfx_unpressed,
8657                       GDI_INFO_TEXT, menubutton_info[i].infotext,
8658                       GDI_X, x,
8659                       GDI_Y, y,
8660                       GDI_WIDTH, width,
8661                       GDI_HEIGHT, height,
8662                       GDI_TYPE, type,
8663                       GDI_STATE, GD_BUTTON_UNPRESSED,
8664                       GDI_CHECKED, checked,
8665                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
8666                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
8667                       GDI_ALT_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1a, gd_y1a,
8668                       GDI_ALT_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2a, gd_y2a,
8669                       GDI_DIRECT_DRAW, FALSE,
8670                       GDI_OVERLAY_TOUCH_BUTTON, is_touch_button,
8671                       GDI_EVENT_MASK, event_mask,
8672                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
8673                       GDI_END);
8674
8675     if (gi == NULL)
8676       Error(ERR_EXIT, "cannot create gadget");
8677
8678     screen_gadget[id] = gi;
8679   }
8680 }
8681
8682 static void CreateScreenScrollbuttons(void)
8683 {
8684   struct GadgetInfo *gi;
8685   unsigned int event_mask;
8686   int i;
8687
8688   // these values are not constant, but can change at runtime
8689   scrollbutton_info[0].x = SC_SCROLL_UP_XPOS;
8690   scrollbutton_info[0].y = SC_SCROLL_UP_YPOS;
8691   scrollbutton_info[1].x = SC_SCROLL_DOWN_XPOS;
8692   scrollbutton_info[1].y = SC_SCROLL_DOWN_YPOS;
8693
8694   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
8695   {
8696     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
8697     int gfx_unpressed, gfx_pressed;
8698     int x, y, width, height;
8699     int gd_x1, gd_x2, gd_y1, gd_y2;
8700     int id = scrollbutton_info[i].gadget_id;
8701
8702     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
8703
8704     x = mSX + scrollbutton_info[i].x + menu.scrollbar_xoffset;
8705     y = mSY + scrollbutton_info[i].y;
8706     width = SC_SCROLLBUTTON_XSIZE;
8707     height = SC_SCROLLBUTTON_YSIZE;
8708
8709     // correct scrollbar position if placed outside menu (playfield) area
8710     if (x > SX + SC_SCROLL_UP_XPOS)
8711       x = SX + SC_SCROLL_UP_XPOS;
8712
8713     if (id == SCREEN_CTRL_ID_SCROLL_DOWN)
8714       y = mSY + (SC_SCROLL_VERTICAL_YPOS +
8715                  (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE);
8716
8717     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
8718     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
8719     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
8720     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
8721     gd_x1 = graphic_info[gfx_unpressed].src_x;
8722     gd_y1 = graphic_info[gfx_unpressed].src_y;
8723     gd_x2 = graphic_info[gfx_pressed].src_x;
8724     gd_y2 = graphic_info[gfx_pressed].src_y;
8725
8726     gi = CreateGadget(GDI_CUSTOM_ID, id,
8727                       GDI_CUSTOM_TYPE_ID, i,
8728                       GDI_IMAGE_ID, gfx_unpressed,
8729                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
8730                       GDI_X, x,
8731                       GDI_Y, y,
8732                       GDI_WIDTH, width,
8733                       GDI_HEIGHT, height,
8734                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
8735                       GDI_STATE, GD_BUTTON_UNPRESSED,
8736                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
8737                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
8738                       GDI_DIRECT_DRAW, FALSE,
8739                       GDI_EVENT_MASK, event_mask,
8740                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
8741                       GDI_END);
8742
8743     if (gi == NULL)
8744       Error(ERR_EXIT, "cannot create gadget");
8745
8746     screen_gadget[id] = gi;
8747   }
8748 }
8749
8750 static void CreateScreenScrollbars(void)
8751 {
8752   int i;
8753
8754   // these values are not constant, but can change at runtime
8755   scrollbar_info[0].x = SC_SCROLL_VERTICAL_XPOS;
8756   scrollbar_info[0].y = SC_SCROLL_VERTICAL_YPOS;
8757   scrollbar_info[0].width  = SC_SCROLL_VERTICAL_XSIZE;
8758   scrollbar_info[0].height = SC_SCROLL_VERTICAL_YSIZE;
8759
8760   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
8761   {
8762     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
8763     int gfx_unpressed, gfx_pressed;
8764     int x, y, width, height;
8765     int gd_x1, gd_x2, gd_y1, gd_y2;
8766     struct GadgetInfo *gi;
8767     int items_max, items_visible, item_position;
8768     unsigned int event_mask;
8769     int num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
8770     int id = scrollbar_info[i].gadget_id;
8771
8772     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
8773
8774     x = mSX + scrollbar_info[i].x + menu.scrollbar_xoffset;
8775     y = mSY + scrollbar_info[i].y;
8776     width  = scrollbar_info[i].width;
8777     height = scrollbar_info[i].height;
8778
8779     // correct scrollbar position if placed outside menu (playfield) area
8780     if (x > SX + SC_SCROLL_VERTICAL_XPOS)
8781       x = SX + SC_SCROLL_VERTICAL_XPOS;
8782
8783     if (id == SCREEN_CTRL_ID_SCROLL_VERTICAL)
8784       height = (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE;
8785
8786     items_max = num_page_entries;
8787     items_visible = num_page_entries;
8788     item_position = 0;
8789
8790     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
8791     gfx_pressed   = scrollbar_info[i].gfx_pressed;
8792     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
8793     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
8794     gd_x1 = graphic_info[gfx_unpressed].src_x;
8795     gd_y1 = graphic_info[gfx_unpressed].src_y;
8796     gd_x2 = graphic_info[gfx_pressed].src_x;
8797     gd_y2 = graphic_info[gfx_pressed].src_y;
8798
8799     gi = CreateGadget(GDI_CUSTOM_ID, id,
8800                       GDI_CUSTOM_TYPE_ID, i,
8801                       GDI_IMAGE_ID, gfx_unpressed,
8802                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
8803                       GDI_X, x,
8804                       GDI_Y, y,
8805                       GDI_WIDTH, width,
8806                       GDI_HEIGHT, height,
8807                       GDI_TYPE, scrollbar_info[i].type,
8808                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
8809                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
8810                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
8811                       GDI_WHEEL_AREA_X, SX,
8812                       GDI_WHEEL_AREA_Y, SY,
8813                       GDI_WHEEL_AREA_WIDTH, SXSIZE,
8814                       GDI_WHEEL_AREA_HEIGHT, SYSIZE,
8815                       GDI_STATE, GD_BUTTON_UNPRESSED,
8816                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
8817                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
8818                       GDI_BORDER_SIZE, SC_BORDER_SIZE, SC_BORDER_SIZE,
8819                       GDI_DIRECT_DRAW, FALSE,
8820                       GDI_EVENT_MASK, event_mask,
8821                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
8822                       GDI_END);
8823
8824     if (gi == NULL)
8825       Error(ERR_EXIT, "cannot create gadget");
8826
8827     screen_gadget[id] = gi;
8828   }
8829 }
8830
8831 static void CreateScreenTextInputGadgets(void)
8832 {
8833   int i;
8834
8835   for (i = 0; i < NUM_SCREEN_TEXTINPUT; i++)
8836   {
8837     int graphic = textinput_info[i].graphic;
8838     struct GraphicInfo *gd = &graphic_info[graphic];
8839     int gd_x1 = gd->src_x;
8840     int gd_y1 = gd->src_y;
8841     int gd_x2 = gd->src_x + gd->active_xoffset;
8842     int gd_y2 = gd->src_y + gd->active_yoffset;
8843     struct GadgetInfo *gi;
8844     unsigned int event_mask;
8845     int id = textinput_info[i].gadget_id;
8846     int x = textinput_info[i].x;
8847     int y = textinput_info[i].y;
8848
8849     event_mask = GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
8850
8851     gi = CreateGadget(GDI_CUSTOM_ID, id,
8852                       GDI_CUSTOM_TYPE_ID, i,
8853                       GDI_INFO_TEXT, textinput_info[i].infotext,
8854                       GDI_X, SX + x,
8855                       GDI_Y, SY + y,
8856                       GDI_TYPE, GD_TYPE_TEXT_INPUT_ALPHANUMERIC,
8857                       GDI_TEXT_VALUE, textinput_info[i].value,
8858                       GDI_TEXT_SIZE, textinput_info[i].size,
8859                       GDI_TEXT_FONT, getSetupValueFont(TYPE_STRING, NULL),
8860                       GDI_TEXT_FONT_ACTIVE, FONT_TEXT_1,
8861                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
8862                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
8863                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
8864                       GDI_DESIGN_WIDTH, gd->width,
8865                       GDI_EVENT_MASK, event_mask,
8866                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
8867                       GDI_CALLBACK_ACTION_ALWAYS, TRUE,
8868                       GDI_END);
8869
8870     if (gi == NULL)
8871       Error(ERR_EXIT, "cannot create gadget");
8872
8873     screen_gadget[id] = gi;
8874   }
8875 }
8876
8877 void CreateScreenGadgets(void)
8878 {
8879   CreateScreenMenubuttons();
8880
8881   CreateScreenScrollbuttons();
8882   CreateScreenScrollbars();
8883
8884   CreateScreenTextInputGadgets();
8885 }
8886
8887 void FreeScreenGadgets(void)
8888 {
8889   int i;
8890
8891   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
8892     FreeGadget(screen_gadget[i]);
8893 }
8894
8895 static void MapScreenMenuGadgets(int screen_mask)
8896 {
8897   int i;
8898
8899   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
8900     if (screen_mask & menubutton_info[i].screen_mask)
8901       MapGadget(screen_gadget[menubutton_info[i].gadget_id]);
8902 }
8903
8904 static void UnmapScreenMenuGadgets(int screen_mask)
8905 {
8906   int i;
8907
8908   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
8909   {
8910     if (screen_mask & menubutton_info[i].screen_mask)
8911     {
8912       UnmapGadget(screen_gadget[menubutton_info[i].gadget_id]);
8913
8914       if (screen_mask & SCREEN_MASK_MAIN_HAS_SOLUTION)
8915         DrawBackground(screen_gadget[menubutton_info[i].gadget_id]->x,
8916                        screen_gadget[menubutton_info[i].gadget_id]->y,
8917                        screen_gadget[menubutton_info[i].gadget_id]->width,
8918                        screen_gadget[menubutton_info[i].gadget_id]->height);
8919     }
8920   }
8921 }
8922
8923 static void UpdateScreenMenuGadgets(int screen_mask, boolean map_gadgets)
8924 {
8925   if (map_gadgets)
8926     MapScreenMenuGadgets(screen_mask);
8927   else
8928     UnmapScreenMenuGadgets(screen_mask);
8929 }
8930
8931 static void MapScreenGadgets(int num_entries)
8932 {
8933   int i;
8934
8935   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
8936     return;
8937
8938   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
8939     MapGadget(screen_gadget[scrollbutton_info[i].gadget_id]);
8940
8941   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
8942     MapGadget(screen_gadget[scrollbar_info[i].gadget_id]);
8943 }
8944
8945 static void MapScreenTreeGadgets(TreeInfo *ti)
8946 {
8947   MapScreenGadgets(numTreeInfoInGroup(ti));
8948 }
8949
8950 static void HandleScreenGadgets(struct GadgetInfo *gi)
8951 {
8952   int id = gi->custom_id;
8953   int button = gi->event.button;
8954   int step = (button == MB_LEFTBUTTON   ? 1 :
8955               button == MB_MIDDLEBUTTON ? 5 :
8956               button == MB_RIGHTBUTTON  ? 10 : 1);
8957
8958   switch (id)
8959   {
8960     case SCREEN_CTRL_ID_PREV_LEVEL:
8961       HandleMainMenu_SelectLevel(step, -1, NO_DIRECT_LEVEL_SELECT);
8962       break;
8963
8964     case SCREEN_CTRL_ID_NEXT_LEVEL:
8965       HandleMainMenu_SelectLevel(step, +1, NO_DIRECT_LEVEL_SELECT);
8966       break;
8967
8968     case SCREEN_CTRL_ID_FIRST_LEVEL:
8969       HandleMainMenu_SelectLevel(MAX_LEVELS, -1, NO_DIRECT_LEVEL_SELECT);
8970       break;
8971
8972     case SCREEN_CTRL_ID_LAST_LEVEL:
8973       HandleMainMenu_SelectLevel(MAX_LEVELS, +1, NO_DIRECT_LEVEL_SELECT);
8974       break;
8975
8976     case SCREEN_CTRL_ID_LEVEL_NUMBER:
8977       CloseDoor(DOOR_CLOSE_2);
8978       SetGameStatus(GAME_MODE_LEVELNR);
8979       DrawChooseLevelNr();
8980       break;
8981
8982     case SCREEN_CTRL_ID_PREV_PLAYER:
8983       HandleSetupScreen_Input_Player(step, -1);
8984       break;
8985
8986     case SCREEN_CTRL_ID_NEXT_PLAYER:
8987       HandleSetupScreen_Input_Player(step, +1);
8988       break;
8989
8990     case SCREEN_CTRL_ID_INSERT_SOLUTION:
8991       InsertSolutionTape();
8992       break;
8993
8994     case SCREEN_CTRL_ID_PLAY_SOLUTION:
8995       PlaySolutionTape();
8996       break;
8997
8998     case SCREEN_CTRL_ID_SWITCH_ECS_AGA:
8999       setup.prefer_aga_graphics = !setup.prefer_aga_graphics;
9000       DrawMainMenu();
9001       break;
9002
9003     case SCREEN_CTRL_ID_TOUCH_PREV_PAGE:
9004     case SCREEN_CTRL_ID_TOUCH_NEXT_PAGE:
9005     case SCREEN_CTRL_ID_TOUCH_PREV_PAGE2:
9006     case SCREEN_CTRL_ID_TOUCH_NEXT_PAGE2:
9007       PushUserEvent(USEREVENT_GADGET_PRESSED, id, 0);
9008       break;
9009
9010     case SCREEN_CTRL_ID_SCROLL_UP:
9011       if (game_status == GAME_MODE_LEVELS)
9012         HandleChooseLevelSet(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
9013       else if (game_status == GAME_MODE_LEVELNR)
9014         HandleChooseLevelNr(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
9015       else if (game_status == GAME_MODE_SETUP)
9016         HandleSetupScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
9017       else if (game_status == GAME_MODE_INFO)
9018         HandleInfoScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
9019       break;
9020
9021     case SCREEN_CTRL_ID_SCROLL_DOWN:
9022       if (game_status == GAME_MODE_LEVELS)
9023         HandleChooseLevelSet(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
9024       else if (game_status == GAME_MODE_LEVELNR)
9025         HandleChooseLevelNr(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
9026       else if (game_status == GAME_MODE_SETUP)
9027         HandleSetupScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
9028       else if (game_status == GAME_MODE_INFO)
9029         HandleInfoScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
9030       break;
9031
9032     case SCREEN_CTRL_ID_SCROLL_VERTICAL:
9033       if (game_status == GAME_MODE_LEVELS)
9034         HandleChooseLevelSet(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
9035       else if (game_status == GAME_MODE_LEVELNR)
9036         HandleChooseLevelNr(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
9037       else if (game_status == GAME_MODE_SETUP)
9038         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
9039       else if (game_status == GAME_MODE_INFO)
9040         HandleInfoScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
9041       break;
9042
9043     case SCREEN_CTRL_ID_NETWORK_SERVER:
9044     {
9045       if (!strEqual(gi->textinput.value, ""))
9046       {
9047         setString(&setup.network_server_hostname, gi->textinput.value);
9048
9049         network.server_host = setup.network_server_hostname;
9050       }
9051       else
9052       {
9053         setString(&setup.network_server_hostname, STR_NETWORK_AUTO_DETECT);
9054
9055         network.server_host = NULL;
9056       }
9057
9058       if (strEqual(network.server_host, STR_NETWORK_AUTO_DETECT))
9059         network.server_host = NULL;
9060
9061       execSetupGame_setNetworkServerText();
9062
9063       DrawSetupScreen();
9064
9065       break;
9066     }
9067
9068     default:
9069       break;
9070   }
9071 }
9072
9073 void DumpScreenIdentifiers(void)
9074 {
9075   int i;
9076
9077   Print("Active screen elements on current screen:\n");
9078
9079   for (i = 0; main_controls[i].nr != -1; i++)
9080   {
9081     struct MainControlInfo *mci = &main_controls[i];
9082
9083     if (mci->button_graphic != -1)
9084     {
9085       char *token = getTokenFromImageID(mci->button_graphic);
9086
9087       Print("- '%s'\n", token);
9088     }
9089   }
9090
9091   Print("Done.\n");
9092 }
9093
9094 boolean DoScreenAction(int image_id)
9095 {
9096   int i;
9097
9098   if (game_status != GAME_MODE_MAIN)
9099     return FALSE;
9100
9101   for (i = 0; main_controls[i].nr != -1; i++)
9102   {
9103     struct MainControlInfo *mci = &main_controls[i];
9104     struct MenuPosInfo *pos = mci->pos_button;
9105
9106     if (mci->button_graphic == image_id)
9107     {
9108       int x = mSX + pos->x;
9109       int y = mSY + pos->y;
9110
9111       HandleMainMenu(x, y, 0, 0, MB_MENU_CHOICE);
9112
9113       return TRUE;
9114     }
9115   }
9116
9117   return FALSE;
9118 }
9119
9120 void DrawScreenAfterAddingSet(char *tree_subdir_new, int tree_type)
9121 {
9122   // get tree info node of newly added level or artwork set
9123   TreeInfo *tree_node_first = TREE_FIRST_NODE(tree_type);
9124   TreeInfo *tree_node_new = getTreeInfoFromIdentifier(tree_node_first,
9125                                                       tree_subdir_new);
9126   if (tree_node_new == NULL)    // should not happen
9127     return;
9128
9129   // if request dialog is active, do nothing
9130   if (game.request_active)
9131     return;
9132
9133   if (game_status == GAME_MODE_MAIN &&
9134       tree_type == TREE_TYPE_LEVEL_DIR)
9135   {
9136     // when adding new level set in main menu, select it as current level set
9137
9138     // change current level set to newly added level set from zip file
9139     leveldir_current = tree_node_new;
9140
9141     // change current level number to first level of newly added level set
9142     level_nr = leveldir_current->first_level;
9143
9144     // redraw screen to reflect changed level set
9145     DrawMainMenu();
9146
9147     // save this level set and level number as last selected level set
9148     SaveLevelSetup_LastSeries();
9149     SaveLevelSetup_SeriesInfo();
9150   }
9151   else if (game_status == GAME_MODE_LEVELS &&
9152            tree_type == TREE_TYPE_LEVEL_DIR)
9153   {
9154     // when adding new level set in level set menu, set cursor and update screen
9155
9156     leveldir_current = tree_node_new;
9157
9158     DrawChooseTree(&leveldir_current);
9159   }
9160   else if (game_status == GAME_MODE_SETUP)
9161   {
9162     // when adding new artwork set in setup menu, set cursor and update screen
9163
9164     if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS &&
9165         tree_type == TREE_TYPE_GRAPHICS_DIR)
9166     {
9167       artwork.gfx_current = tree_node_new;
9168
9169       DrawChooseTree(&artwork.gfx_current);
9170     }
9171     else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS &&
9172              tree_type == TREE_TYPE_SOUNDS_DIR)
9173     {
9174       artwork.snd_current = tree_node_new;
9175
9176       DrawChooseTree(&artwork.snd_current);
9177     }
9178     else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC &&
9179              tree_type == TREE_TYPE_MUSIC_DIR)
9180     {
9181       artwork.mus_current = tree_node_new;
9182
9183       DrawChooseTree(&artwork.mus_current);
9184     }
9185   }
9186 }