fixed setup option position when using narrow font
[rocksndiamonds.git] / src / screens.c
1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // screens.c
10 // ============================================================================
11
12 #include "libgame/libgame.h"
13
14 #include "screens.h"
15 #include "events.h"
16 #include "game.h"
17 #include "tools.h"
18 #include "editor.h"
19 #include "files.h"
20 #include "tape.h"
21 #include "anim.h"
22 #include "network.h"
23 #include "init.h"
24 #include "config.h"
25
26
27 #define DEBUG_JOYSTICKS         0
28
29
30 // screens on the info screen
31 #define INFO_MODE_MAIN                  0
32 #define INFO_MODE_TITLE                 1
33 #define INFO_MODE_ELEMENTS              2
34 #define INFO_MODE_MUSIC                 3
35 #define INFO_MODE_CREDITS               4
36 #define INFO_MODE_PROGRAM               5
37 #define INFO_MODE_VERSION               6
38 #define INFO_MODE_LEVELSET              7
39
40 #define MAX_INFO_MODES                  8
41
42 // screens on the setup screen
43 // (must match GFX_SPECIAL_ARG_SETUP_* values as defined in src/main.h)
44 // (should also match corresponding entries in src/conf_gfx.c)
45 #define SETUP_MODE_MAIN                 0
46 #define SETUP_MODE_GAME                 1
47 #define SETUP_MODE_ENGINES              2
48 #define SETUP_MODE_EDITOR               3
49 #define SETUP_MODE_GRAPHICS             4
50 #define SETUP_MODE_SOUND                5
51 #define SETUP_MODE_ARTWORK              6
52 #define SETUP_MODE_INPUT                7
53 #define SETUP_MODE_TOUCH                8
54 #define SETUP_MODE_SHORTCUTS            9
55 #define SETUP_MODE_SHORTCUTS_1          10
56 #define SETUP_MODE_SHORTCUTS_2          11
57 #define SETUP_MODE_SHORTCUTS_3          12
58 #define SETUP_MODE_SHORTCUTS_4          13
59 #define SETUP_MODE_SHORTCUTS_5          14
60
61 // sub-screens on the setup screen (generic)
62 #define SETUP_MODE_CHOOSE_ARTWORK       15
63 #define SETUP_MODE_CHOOSE_OTHER         16
64
65 // sub-screens on the setup screen (specific)
66 #define SETUP_MODE_CHOOSE_GAME_SPEED    17
67 #define SETUP_MODE_CHOOSE_SCROLL_DELAY  18
68 #define SETUP_MODE_CHOOSE_SNAPSHOT_MODE 19
69 #define SETUP_MODE_CHOOSE_WINDOW_SIZE   20
70 #define SETUP_MODE_CHOOSE_SCALING_TYPE  21
71 #define SETUP_MODE_CHOOSE_RENDERING     22
72 #define SETUP_MODE_CHOOSE_VSYNC         23
73 #define SETUP_MODE_CHOOSE_GRAPHICS      24
74 #define SETUP_MODE_CHOOSE_SOUNDS        25
75 #define SETUP_MODE_CHOOSE_MUSIC         26
76 #define SETUP_MODE_CHOOSE_VOLUME_SIMPLE 27
77 #define SETUP_MODE_CHOOSE_VOLUME_LOOPS  28
78 #define SETUP_MODE_CHOOSE_VOLUME_MUSIC  29
79 #define SETUP_MODE_CHOOSE_TOUCH_CONTROL 30
80 #define SETUP_MODE_CHOOSE_MOVE_DISTANCE 31
81 #define SETUP_MODE_CHOOSE_DROP_DISTANCE 32
82 #define SETUP_MODE_CHOOSE_TRANSPARENCY  33
83 #define SETUP_MODE_CHOOSE_GRID_XSIZE_0  34
84 #define SETUP_MODE_CHOOSE_GRID_YSIZE_0  35
85 #define SETUP_MODE_CHOOSE_GRID_XSIZE_1  36
86 #define SETUP_MODE_CHOOSE_GRID_YSIZE_1  37
87 #define SETUP_MODE_CONFIG_VIRT_BUTTONS  38
88
89 #define MAX_SETUP_MODES                 39
90
91 #define MAX_MENU_MODES                  MAX(MAX_INFO_MODES, MAX_SETUP_MODES)
92
93 // setup screen titles
94 #define STR_SETUP_MAIN                  "Setup"
95 #define STR_SETUP_GAME                  "Game & Menu"
96 #define STR_SETUP_ENGINES               "Game Engines"
97 #define STR_SETUP_EDITOR                "Editor"
98 #define STR_SETUP_GRAPHICS              "Graphics"
99 #define STR_SETUP_SOUND                 "Sound & Music"
100 #define STR_SETUP_ARTWORK               "Custom Artwork"
101 #define STR_SETUP_INPUT                 "Input Devices"
102 #define STR_SETUP_TOUCH                 "Touch Controls"
103 #define STR_SETUP_SHORTCUTS             "Key Shortcuts"
104 #define STR_SETUP_EXIT                  "Exit"
105 #define STR_SETUP_SAVE_AND_EXIT         "Save and Exit"
106
107 #define STR_SETUP_CHOOSE_GAME_SPEED     "Game Speed"
108 #define STR_SETUP_CHOOSE_SCROLL_DELAY   "Scroll Delay"
109 #define STR_SETUP_CHOOSE_SNAPSHOT_MODE  "Snapshot Mode"
110 #define STR_SETUP_CHOOSE_WINDOW_SIZE    "Window Scaling"
111 #define STR_SETUP_CHOOSE_SCALING_TYPE   "Anti-Aliasing"
112 #define STR_SETUP_CHOOSE_RENDERING      "Rendering Mode"
113 #define STR_SETUP_CHOOSE_VSYNC          "VSync Mode"
114 #define STR_SETUP_CHOOSE_VOLUME_SIMPLE  "Sound Volume"
115 #define STR_SETUP_CHOOSE_VOLUME_LOOPS   "Loops Volume"
116 #define STR_SETUP_CHOOSE_VOLUME_MUSIC   "Music Volume"
117 #define STR_SETUP_CHOOSE_TOUCH_CONTROL  "Control Type"
118 #define STR_SETUP_CHOOSE_MOVE_DISTANCE  "Move Distance"
119 #define STR_SETUP_CHOOSE_DROP_DISTANCE  "Drop Distance"
120 #define STR_SETUP_CHOOSE_TRANSPARENCY   "Transparency"
121 #define STR_SETUP_CHOOSE_GRID_XSIZE_0   "Horiz. Buttons"
122 #define STR_SETUP_CHOOSE_GRID_YSIZE_0   "Vert. Buttons"
123 #define STR_SETUP_CHOOSE_GRID_XSIZE_1   "Horiz. Buttons"
124 #define STR_SETUP_CHOOSE_GRID_YSIZE_1   "Vert. Buttons"
125
126 // for input setup functions
127 #define SETUPINPUT_SCREEN_POS_START     0
128 #define SETUPINPUT_SCREEN_POS_EMPTY1    3
129 #define SETUPINPUT_SCREEN_POS_EMPTY2    12
130 #define SETUPINPUT_SCREEN_POS_END       13
131
132 #define MENU_SETUP_FONT_TITLE           FONT_TEXT_1
133 #define MENU_SETUP_FONT_TEXT            FONT_TITLE_2
134
135 #define MAX_SETUP_TEXT_INPUT_LEN        28
136
137 // for various menu stuff
138 #define MENU_SCREEN_START_XPOS          1
139 #define MENU_SCREEN_START_YPOS          2
140 #define MENU_SCREEN_VALUE_XPOS          (SCR_FIELDX - 3)
141 #define MENU_SCREEN_MAX_XPOS            (SCR_FIELDX - 1)
142 #define MENU_TITLE1_YPOS                8
143 #define MENU_TITLE2_YPOS                46
144 #define MENU_INFO_FONT_TITLE            FONT_TEXT_1
145 #define MENU_INFO_FONT_HEAD             FONT_TEXT_2
146 #define MENU_INFO_FONT_TEXT             FONT_TEXT_3
147 #define MENU_INFO_FONT_FOOT             FONT_TEXT_4
148 #define MENU_INFO_SPACE_HEAD            (menu.headline2_spacing_info[info_mode])
149 #define MENU_SCREEN_INFO_SPACE_LEFT     (menu.left_spacing_info[info_mode])
150 #define MENU_SCREEN_INFO_SPACE_RIGHT    (menu.right_spacing_info[info_mode])
151 #define MENU_SCREEN_INFO_SPACE_TOP      (menu.top_spacing_info[info_mode])
152 #define MENU_SCREEN_INFO_SPACE_BOTTOM   (menu.bottom_spacing_info[info_mode])
153 #define MENU_SCREEN_INFO_YSTART1        MENU_SCREEN_INFO_SPACE_TOP
154 #define MENU_SCREEN_INFO_YSTART2        (MENU_SCREEN_INFO_YSTART1 +            \
155                                          getMenuTextStep(MENU_INFO_SPACE_HEAD, \
156                                                          MENU_INFO_FONT_TITLE))
157 #define MENU_SCREEN_INFO_YSTEP          (TILEY + 4)
158 #define MENU_SCREEN_INFO_YBOTTOM        (SYSIZE - MENU_SCREEN_INFO_SPACE_BOTTOM)
159 #define MENU_SCREEN_INFO_YSIZE          (MENU_SCREEN_INFO_YBOTTOM -     \
160                                          MENU_SCREEN_INFO_YSTART2 -     \
161                                          TILEY / 2)
162 #define MAX_INFO_ELEMENTS_ON_SCREEN     128
163 #define STD_INFO_ELEMENTS_ON_SCREEN     (MENU_SCREEN_INFO_YSIZE /       \
164                                          MENU_SCREEN_INFO_YSTEP)
165 #define NUM_INFO_ELEMENTS_FROM_CONF     \
166   (menu.list_size_info[GFX_SPECIAL_ARG_INFO_ELEMENTS] > 0 ?             \
167    menu.list_size_info[GFX_SPECIAL_ARG_INFO_ELEMENTS] :                 \
168    MAX_MENU_ENTRIES_ON_SCREEN)
169 #define NUM_INFO_ELEMENTS_ON_SCREEN     MIN(MIN(STD_INFO_ELEMENTS_ON_SCREEN, \
170                                                 MAX_INFO_ELEMENTS_ON_SCREEN), \
171                                             NUM_INFO_ELEMENTS_FROM_CONF)
172 #define MAX_MENU_ENTRIES_ON_SCREEN      (SCR_FIELDY - MENU_SCREEN_START_YPOS)
173 #define MAX_MENU_TEXT_LENGTH_BIG        13
174 #define MAX_MENU_TEXT_LENGTH_MEDIUM     (MAX_MENU_TEXT_LENGTH_BIG * 2)
175
176 // screen gadget identifiers
177 #define SCREEN_CTRL_ID_PREV_LEVEL       0
178 #define SCREEN_CTRL_ID_NEXT_LEVEL       1
179 #define SCREEN_CTRL_ID_FIRST_LEVEL      2
180 #define SCREEN_CTRL_ID_LAST_LEVEL       3
181 #define SCREEN_CTRL_ID_LEVEL_NUMBER     4
182 #define SCREEN_CTRL_ID_PREV_PLAYER      5
183 #define SCREEN_CTRL_ID_NEXT_PLAYER      6
184 #define SCREEN_CTRL_ID_INSERT_SOLUTION  7
185 #define SCREEN_CTRL_ID_PLAY_SOLUTION    8
186 #define SCREEN_CTRL_ID_SWITCH_ECS_AGA   9
187 #define SCREEN_CTRL_ID_TOUCH_PREV_PAGE  10
188 #define SCREEN_CTRL_ID_TOUCH_NEXT_PAGE  11
189 #define SCREEN_CTRL_ID_TOUCH_PREV_PAGE2 12
190 #define SCREEN_CTRL_ID_TOUCH_NEXT_PAGE2 13
191 #define SCREEN_CTRL_ID_SCROLL_UP        14
192 #define SCREEN_CTRL_ID_SCROLL_DOWN      15
193 #define SCREEN_CTRL_ID_SCROLL_VERTICAL  16
194 #define SCREEN_CTRL_ID_NETWORK_SERVER   17
195
196 #define NUM_SCREEN_GADGETS              18
197
198 #define NUM_SCREEN_MENUBUTTONS          14
199 #define NUM_SCREEN_SCROLLBUTTONS        2
200 #define NUM_SCREEN_SCROLLBARS           1
201 #define NUM_SCREEN_TEXTINPUT            1
202
203 #define SCREEN_MASK_MAIN                (1 << 0)
204 #define SCREEN_MASK_MAIN_HAS_SOLUTION   (1 << 1)
205 #define SCREEN_MASK_INPUT               (1 << 2)
206 #define SCREEN_MASK_TOUCH               (1 << 3)
207 #define SCREEN_MASK_TOUCH2              (1 << 4)
208
209 // graphic position and size values for buttons and scrollbars
210 #define SC_MENUBUTTON_XSIZE             TILEX
211 #define SC_MENUBUTTON_YSIZE             TILEY
212
213 #define SC_SCROLLBUTTON_XSIZE           TILEX
214 #define SC_SCROLLBUTTON_YSIZE           TILEY
215
216 #define SC_SCROLLBAR_XPOS               (SXSIZE - SC_SCROLLBUTTON_XSIZE)
217
218 #define SC_SCROLL_VERTICAL_XSIZE        SC_SCROLLBUTTON_XSIZE
219 #define SC_SCROLL_VERTICAL_YSIZE        ((MAX_MENU_ENTRIES_ON_SCREEN - 2) * \
220                                          SC_SCROLLBUTTON_YSIZE)
221
222 #define SC_SCROLL_UP_XPOS               SC_SCROLLBAR_XPOS
223 #define SC_SCROLL_UP_YPOS               (2 * SC_SCROLLBUTTON_YSIZE)
224
225 #define SC_SCROLL_VERTICAL_XPOS         SC_SCROLLBAR_XPOS
226 #define SC_SCROLL_VERTICAL_YPOS         (SC_SCROLL_UP_YPOS + \
227                                          SC_SCROLLBUTTON_YSIZE)
228
229 #define SC_SCROLL_DOWN_XPOS             SC_SCROLLBAR_XPOS
230 #define SC_SCROLL_DOWN_YPOS             (SC_SCROLL_VERTICAL_YPOS + \
231                                          SC_SCROLL_VERTICAL_YSIZE)
232
233 #define SC_BORDER_SIZE                  14
234
235
236 // forward declarations of internal functions
237 static void HandleScreenGadgets(struct GadgetInfo *);
238 static void HandleSetupScreen_Generic(int, int, int, int, int);
239 static void HandleSetupScreen_Input(int, int, int, int, int);
240 static void CustomizeKeyboard(int);
241 static void ConfigureJoystick(int);
242 static void ConfigureVirtualButtons(void);
243 static void execSetupGame(void);
244 static void execSetupEngines(void);
245 static void execSetupGraphics(void);
246 static void execSetupSound(void);
247 static void execSetupTouch(void);
248 static void execSetupArtwork(void);
249 static void HandleChooseTree(int, int, int, int, int, TreeInfo **);
250
251 static void 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   Debug("screens:getTitleSound", "%d, %d, %d: %d ['%s'], %d ['%s']",
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   Debug("screens:getTitleMusic", "%d, %d, %d: %d ['%s'], %d ['%s']",
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   Debug("screens:insideTextPosRect",
1330         "(%d, %d), (%d, %d) [%d, %d] (%d, %d) => %d",
1331         x, y, rect_x, rect_y, rect->x, rect->y, rect->width, rect->height,
1332         (x >= rect_x && x < rect_x + rect->width &&
1333          y >= rect_y && y < rect_y + rect->height));
1334 #endif
1335
1336   return (x >= rect_x && x < rect_x + rect->width &&
1337           y >= rect_y && y < rect_y + rect->height);
1338 }
1339
1340 static boolean insidePreviewRect(struct PreviewInfo *preview, int x, int y)
1341 {
1342   int rect_width  = preview->xsize * preview->tile_size;
1343   int rect_height = preview->ysize * preview->tile_size;
1344   int rect_x = ALIGNED_XPOS(preview->x, rect_width,  preview->align);
1345   int rect_y = ALIGNED_YPOS(preview->y, rect_height, preview->valign);
1346
1347   return (x >= rect_x && x < rect_x + rect_width &&
1348           y >= rect_y && y < rect_y + rect_height);
1349 }
1350
1351 static void AdjustScrollbar(int id, int items_max, int items_visible,
1352                             int item_position)
1353 {
1354   struct GadgetInfo *gi = screen_gadget[id];
1355
1356   if (item_position > items_max - items_visible)
1357     item_position = items_max - items_visible;
1358
1359   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
1360                GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
1361                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
1362 }
1363
1364 static void AdjustChooseTreeScrollbar(int id, int first_entry, TreeInfo *ti)
1365 {
1366   AdjustScrollbar(id, numTreeInfoInGroup(ti), NUM_MENU_ENTRIES_ON_SCREEN,
1367                   first_entry);
1368 }
1369
1370 static void clearMenuListArea(void)
1371 {
1372   int scrollbar_xpos = mSX + SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
1373
1374   // correct scrollbar position if placed outside menu (playfield) area
1375   if (scrollbar_xpos > SX + SC_SCROLLBAR_XPOS)
1376     scrollbar_xpos = SX + SC_SCROLLBAR_XPOS;
1377
1378   // clear menu list area, but not title or scrollbar
1379   DrawBackground(mSX, mSY + MENU_SCREEN_START_YPOS * 32,
1380                  scrollbar_xpos - mSX, NUM_MENU_ENTRIES_ON_SCREEN * 32);
1381 }
1382
1383 static void drawCursorExt(int xpos, int ypos, boolean active, int graphic)
1384 {
1385   static int cursor_array[MAX_LEV_FIELDY];
1386   int x = amSX + TILEX * xpos;
1387   int y = amSY + TILEY * (MENU_SCREEN_START_YPOS + ypos);
1388
1389   if (xpos == 0)
1390   {
1391     if (graphic != -1)
1392       cursor_array[ypos] = graphic;
1393     else
1394       graphic = cursor_array[ypos];
1395   }
1396
1397   if (active)
1398     graphic = BUTTON_ACTIVE(graphic);
1399
1400   DrawBackgroundForGraphic(x, y, TILEX, TILEY, graphic);
1401   DrawFixedGraphicThruMaskExt(drawto, x, y, graphic, 0);
1402 }
1403
1404 static void initCursor(int ypos, int graphic)
1405 {
1406   drawCursorExt(0, ypos, FALSE, graphic);
1407 }
1408
1409 static void drawCursor(int ypos, boolean active)
1410 {
1411   drawCursorExt(0, ypos, active, -1);
1412 }
1413
1414 static void drawCursorXY(int xpos, int ypos, int graphic)
1415 {
1416   drawCursorExt(xpos, ypos, FALSE, graphic);
1417 }
1418
1419 static void drawChooseTreeCursor(int ypos, boolean active)
1420 {
1421   drawCursorExt(0, ypos, active, -1);
1422 }
1423
1424 static void DrawHeadline(void)
1425 {
1426   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, main_text_title_1);
1427   DrawTextSCentered(MENU_TITLE2_YPOS, FONT_TITLE_2, main_text_title_2);
1428 }
1429
1430 static void DrawTitleScreenImage(int nr, boolean initial)
1431 {
1432   int graphic = getTitleScreenGraphic(nr, initial);
1433   Bitmap *bitmap = graphic_info[graphic].bitmap;
1434   int width  = graphic_info[graphic].width;
1435   int height = graphic_info[graphic].height;
1436   int src_x = graphic_info[graphic].src_x;
1437   int src_y = graphic_info[graphic].src_y;
1438   int dst_x, dst_y;
1439
1440   if (bitmap == NULL)
1441     return;
1442
1443   if (width > WIN_XSIZE)
1444   {
1445     // image width too large for window => center image horizontally
1446     src_x = (width - WIN_XSIZE) / 2;
1447     width = WIN_XSIZE;
1448   }
1449
1450   if (height > WIN_YSIZE)
1451   {
1452     // image height too large for window => center image vertically
1453     src_y = (height - WIN_YSIZE) / 2;
1454     height = WIN_YSIZE;
1455   }
1456
1457   // always display title screens centered
1458   dst_x = (WIN_XSIZE - width) / 2;
1459   dst_y = (WIN_YSIZE - height) / 2;
1460
1461   SetDrawBackgroundMask(REDRAW_ALL);
1462   SetWindowBackgroundImage(getTitleBackground(nr, initial, TRUE));
1463
1464   ClearRectangleOnBackground(drawto, 0, 0, WIN_XSIZE, WIN_YSIZE);
1465
1466   if (DrawingOnBackground(dst_x, dst_y))
1467     BlitBitmapMasked(bitmap, drawto, src_x, src_y, width, height, dst_x, dst_y);
1468   else
1469     BlitBitmap(bitmap, drawto, src_x, src_y, width, height, dst_x, dst_y);
1470
1471   redraw_mask = REDRAW_ALL;
1472 }
1473
1474 static void DrawTitleScreenMessage(int nr, boolean initial)
1475 {
1476   char *filename = getLevelSetTitleMessageFilename(nr, initial);
1477   struct TitleMessageInfo *tmi = getTitleMessageInfo(nr, initial);
1478
1479   if (filename == NULL)
1480     return;
1481
1482   // force TITLE font on title message screen
1483   SetFontStatus(getTitleMessageGameMode(initial));
1484
1485   // if chars *and* width set to "-1", automatically determine width
1486   if (tmi->chars == -1 && tmi->width == -1)
1487     tmi->width = viewport.window[game_status].width;
1488
1489   // if lines *and* height set to "-1", automatically determine height
1490   if (tmi->lines == -1 && tmi->height == -1)
1491     tmi->height = viewport.window[game_status].height;
1492
1493   // if chars set to "-1", automatically determine by text and font width
1494   if (tmi->chars == -1)
1495     tmi->chars = tmi->width / getFontWidth(tmi->font);
1496   else
1497     tmi->width = tmi->chars * getFontWidth(tmi->font);
1498
1499   // if lines set to "-1", automatically determine by text and font height
1500   if (tmi->lines == -1)
1501     tmi->lines = tmi->height / getFontHeight(tmi->font);
1502   else
1503     tmi->height = tmi->lines * getFontHeight(tmi->font);
1504
1505   // if x set to "-1", automatically determine by width and alignment
1506   if (tmi->x == -1)
1507     tmi->x = -1 * ALIGNED_XPOS(0, tmi->width, tmi->align);
1508
1509   // if y set to "-1", automatically determine by height and alignment
1510   if (tmi->y == -1)
1511     tmi->y = -1 * ALIGNED_YPOS(0, tmi->height, tmi->valign);
1512
1513   SetDrawBackgroundMask(REDRAW_ALL);
1514   SetWindowBackgroundImage(getTitleBackground(nr, initial, FALSE));
1515
1516   ClearRectangleOnBackground(drawto, 0, 0, WIN_XSIZE, WIN_YSIZE);
1517
1518   DrawTextFile(ALIGNED_TEXT_XPOS(tmi), ALIGNED_TEXT_YPOS(tmi),
1519                filename, tmi->font, tmi->chars, -1, tmi->lines, 0, -1,
1520                tmi->autowrap, tmi->centered, tmi->parse_comments);
1521
1522   ResetFontStatus();
1523 }
1524
1525 static void DrawTitleScreen(void)
1526 {
1527   KeyboardAutoRepeatOff();
1528
1529   HandleTitleScreen(0, 0, 0, 0, MB_MENU_INITIALIZE);
1530 }
1531
1532 static boolean CheckTitleScreen(boolean levelset_has_changed)
1533 {
1534   static boolean show_title_initial = TRUE;
1535   boolean show_titlescreen = FALSE;
1536
1537   // needed to be able to skip title screen, if no image or message defined
1538   InitializeTitleControls(show_title_initial);
1539
1540   if (setup.show_titlescreen && (show_title_initial || levelset_has_changed))
1541     show_titlescreen = TRUE;
1542
1543   // show initial title images and messages only once at program start
1544   show_title_initial = FALSE;
1545
1546   return (show_titlescreen && num_title_screens > 0);
1547 }
1548
1549 void DrawMainMenu(void)
1550 {
1551   static LevelDirTree *leveldir_last_valid = NULL;
1552   boolean levelset_has_changed = FALSE;
1553   int fade_mask = REDRAW_FIELD;
1554
1555   LimitScreenUpdates(FALSE);
1556
1557   FadeSetLeaveScreen();
1558
1559   // do not fade out here -- function may continue and fade on editor screen
1560
1561   UnmapAllGadgets();
1562   FadeMenuSoundsAndMusic();
1563
1564   ExpireSoundLoops(FALSE);
1565
1566   KeyboardAutoRepeatOn();
1567
1568   audio.sound_deactivated = FALSE;
1569
1570   GetPlayerConfig();
1571
1572   // needed if last screen was the playing screen, invoked from level editor
1573   if (level_editor_test_game)
1574   {
1575     CloseDoor(DOOR_CLOSE_ALL);
1576
1577     SetGameStatus(GAME_MODE_EDITOR);
1578
1579     DrawLevelEd();
1580
1581     return;
1582   }
1583
1584   // leveldir_current may be invalid (level group, parent link)
1585   if (!validLevelSeries(leveldir_current))
1586     leveldir_current = getFirstValidTreeInfoEntry(leveldir_last_valid);
1587
1588   if (leveldir_current != leveldir_last_valid)
1589     levelset_has_changed = TRUE;
1590
1591   // store valid level series information
1592   leveldir_last_valid = leveldir_current;
1593
1594   init_last = init;                     // switch to new busy animation
1595
1596   // needed if last screen (level choice) changed graphics, sounds or music
1597   ReloadCustomArtwork(0);
1598
1599   if (CheckTitleScreen(levelset_has_changed))
1600   {
1601     SetGameStatus(GAME_MODE_TITLE);
1602
1603     DrawTitleScreen();
1604
1605     return;
1606   }
1607
1608   if (redraw_mask & REDRAW_ALL)
1609     fade_mask = REDRAW_ALL;
1610
1611   if (CheckFadeAll())
1612     fade_mask = REDRAW_ALL;
1613
1614   FadeOut(fade_mask);
1615
1616   // needed if different viewport properties defined for menues
1617   ChangeViewportPropertiesIfNeeded();
1618
1619   SetDrawtoField(DRAW_TO_BACKBUFFER);
1620
1621   // level_nr may have been set to value over handicap with level editor
1622   if (setup.handicap && level_nr > leveldir_current->handicap_level)
1623     level_nr = leveldir_current->handicap_level;
1624
1625   LoadLevel(level_nr);
1626   LoadScore(level_nr);
1627
1628   SaveLevelSetup_SeriesInfo();
1629
1630   // set this after "ChangeViewportPropertiesIfNeeded()" (which may reset it)
1631   SetDrawDeactivationMask(REDRAW_NONE);
1632   SetDrawBackgroundMask(REDRAW_FIELD);
1633
1634   SetMainBackgroundImage(IMG_BACKGROUND_MAIN);
1635
1636 #if 0
1637   if (fade_mask == REDRAW_ALL)
1638     RedrawGlobalBorder();
1639 #endif
1640
1641   ClearField();
1642
1643   InitializeMainControls();
1644
1645   DrawCursorAndText_Main(-1, FALSE, FALSE);
1646   DrawPreviewLevelInitial();
1647   DrawNetworkPlayers();
1648
1649   HandleMainMenu(0, 0, 0, 0, MB_MENU_INITIALIZE);
1650
1651   TapeStop();
1652   if (TAPE_IS_EMPTY(tape))
1653     LoadTape(level_nr);
1654   DrawCompleteVideoDisplay();
1655
1656   PlayMenuSoundsAndMusic();
1657
1658   // create gadgets for main menu screen
1659   FreeScreenGadgets();
1660   CreateScreenGadgets();
1661
1662   // may be required if audio buttons shown on tape and changed in setup menu
1663   FreeGameButtons();
1664   CreateGameButtons();
1665
1666   // map gadgets for main menu screen
1667   MapTapeButtons();
1668   MapScreenMenuGadgets(SCREEN_MASK_MAIN);
1669   UpdateScreenMenuGadgets(SCREEN_MASK_MAIN_HAS_SOLUTION, hasSolutionTape());
1670
1671   // copy actual game door content to door double buffer for OpenDoor()
1672   BlitBitmap(drawto, bitmap_db_door_1, DX, DY, DXSIZE, DYSIZE, 0, 0);
1673   BlitBitmap(drawto, bitmap_db_door_2, VX, VY, VXSIZE, VYSIZE, 0, 0);
1674
1675   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
1676
1677   DrawMaskedBorder(fade_mask);
1678
1679   FadeIn(fade_mask);
1680   FadeSetEnterMenu();
1681
1682   // update screen area with special editor door
1683   redraw_mask |= REDRAW_ALL;
1684   BackToFront();
1685
1686   SetMouseCursor(CURSOR_DEFAULT);
1687
1688   OpenDoor(DOOR_CLOSE_1 | DOOR_OPEN_2);
1689 }
1690
1691 static void gotoTopLevelDir(void)
1692 {
1693   // move upwards until inside (but not above) top level directory
1694   while (leveldir_current->node_parent &&
1695          !strEqual(leveldir_current->node_parent->subdir, STRING_TOP_DIRECTORY))
1696   {
1697     // write a "path" into level tree for easy navigation to last level
1698     if (leveldir_current->node_parent->node_group->cl_first == -1)
1699     {
1700       int num_leveldirs = numTreeInfoInGroup(leveldir_current);
1701       int leveldir_pos = posTreeInfo(leveldir_current);
1702       int num_page_entries = MIN(num_leveldirs, NUM_MENU_ENTRIES_ON_SCREEN);
1703       int cl_first, cl_cursor;
1704
1705       cl_first = MAX(0, leveldir_pos - num_page_entries + 1);
1706       cl_cursor = leveldir_pos - cl_first;
1707
1708       leveldir_current->node_parent->node_group->cl_first = cl_first;
1709       leveldir_current->node_parent->node_group->cl_cursor = cl_cursor;
1710     }
1711
1712     leveldir_current = leveldir_current->node_parent;
1713   }
1714 }
1715
1716 static unsigned int getAutoDelayCounter(struct TitleFadingInfo *fi)
1717 {
1718   boolean use_frame_counter = (fi->auto_delay_unit == AUTO_DELAY_UNIT_FRAMES);
1719
1720   return (use_frame_counter ? video.frame_counter : Counter());
1721 }
1722
1723 static boolean TitleAutoDelayReached(unsigned int *counter_var,
1724                                      struct TitleFadingInfo *fi)
1725 {
1726   return DelayReachedExt(counter_var, fi->auto_delay, getAutoDelayCounter(fi));
1727 }
1728
1729 static void ResetTitleAutoDelay(unsigned int *counter_var,
1730                                 struct TitleFadingInfo *fi)
1731 {
1732   *counter_var = getAutoDelayCounter(fi);
1733 }
1734
1735 void HandleTitleScreen(int mx, int my, int dx, int dy, int button)
1736 {
1737   static unsigned int title_delay = 0;
1738   static int title_screen_nr = 0;
1739   static int last_sound = -1, last_music = -1;
1740   boolean return_to_main_menu = FALSE;
1741   struct TitleControlInfo *tci;
1742   int sound, music;
1743
1744   if (button == MB_MENU_INITIALIZE)
1745   {
1746     title_delay = 0;
1747     title_screen_nr = 0;
1748     tci = &title_controls[title_screen_nr];
1749
1750     SetAnimStatus(getTitleAnimMode(tci));
1751
1752     last_sound = SND_UNDEFINED;
1753     last_music = MUS_UNDEFINED;
1754
1755     if (num_title_screens != 0)
1756     {
1757       FadeSetEnterScreen();
1758
1759       // use individual title fading instead of global "enter screen" fading
1760       fading = getTitleFading(tci);
1761     }
1762
1763     if (game_status_last_screen == GAME_MODE_INFO)
1764     {
1765       if (num_title_screens == 0)
1766       {
1767         // switch game mode from title screen mode back to info screen mode
1768         SetGameStatus(GAME_MODE_INFO);
1769
1770         // store that last screen was info screen, not main menu screen
1771         game_status_last_screen = GAME_MODE_INFO;
1772
1773         DrawInfoScreen_NotAvailable("Title screen information:",
1774                                     "No title screen for this level set.");
1775         return;
1776       }
1777     }
1778
1779     FadeMenuSoundsAndMusic();
1780
1781     FadeOut(REDRAW_ALL);
1782
1783     // title screens may have different window size
1784     ChangeViewportPropertiesIfNeeded();
1785
1786     // only required to update logic for redrawing global border
1787     ClearField();
1788
1789     if (tci->is_image)
1790       DrawTitleScreenImage(tci->local_nr, tci->initial);
1791     else
1792       DrawTitleScreenMessage(tci->local_nr, tci->initial);
1793
1794     sound = getTitleSound(tci);
1795     music = getTitleMusic(tci);
1796
1797     if (sound != last_sound)
1798       PlayMenuSoundExt(sound);
1799     if (music != last_music)
1800       PlayMenuMusicExt(music);
1801
1802     last_sound = sound;
1803     last_music = music;
1804
1805     SetMouseCursor(CURSOR_NONE);
1806
1807     FadeIn(REDRAW_ALL);
1808
1809     ResetTitleAutoDelay(&title_delay, &fading);
1810
1811     return;
1812   }
1813
1814   if (fading.auto_delay > 0 && TitleAutoDelayReached(&title_delay, &fading))
1815     button = MB_MENU_CHOICE;
1816
1817   if (button == MB_MENU_LEAVE)
1818   {
1819     return_to_main_menu = TRUE;
1820   }
1821   else if (button == MB_MENU_CHOICE)
1822   {
1823     if (game_status_last_screen == GAME_MODE_INFO && num_title_screens == 0)
1824     {
1825       SetGameStatus(GAME_MODE_INFO);
1826
1827       info_mode = INFO_MODE_MAIN;
1828
1829       DrawInfoScreen();
1830
1831       return;
1832     }
1833
1834     title_screen_nr++;
1835
1836     if (title_screen_nr < num_title_screens)
1837     {
1838       tci = &title_controls[title_screen_nr];
1839
1840       SetAnimStatus(getTitleAnimMode(tci));
1841
1842       sound = getTitleSound(tci);
1843       music = getTitleMusic(tci);
1844
1845       if (last_sound != SND_UNDEFINED && sound != last_sound)
1846         FadeSound(last_sound);
1847       if (last_music != MUS_UNDEFINED && music != last_music)
1848         FadeMusic();
1849
1850       fading = getTitleFading(tci);
1851
1852       FadeOut(REDRAW_ALL);
1853
1854       if (tci->is_image)
1855         DrawTitleScreenImage(tci->local_nr, tci->initial);
1856       else
1857         DrawTitleScreenMessage(tci->local_nr, tci->initial);
1858
1859       sound = getTitleSound(tci);
1860       music = getTitleMusic(tci);
1861
1862       if (sound != last_sound)
1863         PlayMenuSoundExt(sound);
1864       if (music != last_music)
1865         PlayMenuMusicExt(music);
1866
1867       last_sound = sound;
1868       last_music = music;
1869
1870       FadeIn(REDRAW_ALL);
1871
1872       ResetTitleAutoDelay(&title_delay, &fading);
1873     }
1874     else
1875     {
1876       FadeMenuSoundsAndMusic();
1877
1878       return_to_main_menu = TRUE;
1879     }
1880   }
1881
1882   if (return_to_main_menu)
1883   {
1884     SetMouseCursor(CURSOR_DEFAULT);
1885
1886     // force full menu screen redraw after displaying title screens
1887     redraw_mask = REDRAW_ALL;
1888
1889     if (game_status_last_screen == GAME_MODE_INFO)
1890     {
1891       SetGameStatus(GAME_MODE_INFO);
1892
1893       info_mode = INFO_MODE_MAIN;
1894
1895       DrawInfoScreen();
1896     }
1897     else        // default: return to main menu
1898     {
1899       SetGameStatus(GAME_MODE_MAIN);
1900
1901       DrawMainMenu();
1902     }
1903   }
1904 }
1905
1906 static void HandleMainMenu_SelectLevel(int step, int direction,
1907                                        int selected_level_nr)
1908 {
1909   int old_level_nr = level_nr;
1910   int new_level_nr;
1911
1912   if (selected_level_nr != NO_DIRECT_LEVEL_SELECT)
1913     new_level_nr = selected_level_nr;
1914   else
1915     new_level_nr = old_level_nr + step * direction;
1916
1917   if (new_level_nr < leveldir_current->first_level)
1918     new_level_nr = leveldir_current->first_level;
1919   if (new_level_nr > leveldir_current->last_level)
1920     new_level_nr = leveldir_current->last_level;
1921
1922   if (setup.handicap && new_level_nr > leveldir_current->handicap_level)
1923   {
1924     // skipping levels is only allowed when trying to skip single level
1925     if (setup.skip_levels && new_level_nr == old_level_nr + 1 &&
1926         Request("Level still unsolved! Skip despite handicap?", REQ_ASK))
1927     {
1928       leveldir_current->handicap_level++;
1929       SaveLevelSetup_SeriesInfo();
1930     }
1931
1932     new_level_nr = leveldir_current->handicap_level;
1933   }
1934
1935   if (new_level_nr != old_level_nr)
1936   {
1937     struct MainControlInfo *mci = getMainControlInfo(MAIN_CONTROL_LEVEL_NUMBER);
1938
1939     PlaySound(SND_MENU_ITEM_SELECTING);
1940
1941     level_nr = new_level_nr;
1942
1943     DrawText(mSX + mci->pos_text->x, mSY + mci->pos_text->y,
1944              int2str(level_nr, menu.main.text.level_number.size),
1945              mci->pos_text->font);
1946
1947     LoadLevel(level_nr);
1948     DrawPreviewLevelInitial();
1949
1950     TapeErase();
1951     LoadTape(level_nr);
1952     DrawCompleteVideoDisplay();
1953
1954     SaveLevelSetup_SeriesInfo();
1955
1956     UpdateScreenMenuGadgets(SCREEN_MASK_MAIN_HAS_SOLUTION, hasSolutionTape());
1957
1958     // force redraw of playfield area (may be reset at this point)
1959     redraw_mask |= REDRAW_FIELD;
1960   }
1961 }
1962
1963 void HandleMainMenu(int mx, int my, int dx, int dy, int button)
1964 {
1965   static int choice = MAIN_CONTROL_GAME;
1966   static boolean button_pressed_last = FALSE;
1967   boolean button_pressed = FALSE;
1968   int pos = choice;
1969   int i = 0;    // needed to prevent compiler warning due to bad code below
1970
1971   if (button == MB_MENU_INITIALIZE)
1972   {
1973     DrawCursorAndText_Main(choice, TRUE, FALSE);
1974
1975     return;
1976   }
1977
1978   if (mx || my)         // mouse input
1979   {
1980     pos = -1;
1981
1982     for (i = 0; main_controls[i].nr != -1; i++)
1983     {
1984       if (insideMenuPosRect(main_controls[i].pos_button, mx - mSX, my - mSY) ||
1985           insideTextPosRect(main_controls[i].pos_text,   mx - mSX, my - mSY) ||
1986           insideTextPosRect(main_controls[i].pos_input,  mx - mSX, my - mSY))
1987       {
1988         pos = main_controls[i].nr;
1989
1990         break;
1991       }
1992     }
1993
1994     // check if level preview was clicked
1995     if (insidePreviewRect(&preview, mx - SX, my - SY))
1996       pos = MAIN_CONTROL_GAME;
1997
1998     // handle pressed/unpressed state for active/inactive menu buttons
1999     // (if pos != -1, "i" contains index position corresponding to "pos")
2000     if (button &&
2001         pos >= MAIN_CONTROL_NAME && pos <= MAIN_CONTROL_QUIT &&
2002         insideMenuPosRect(main_controls[i].pos_button, mx - mSX, my - mSY))
2003       button_pressed = TRUE;
2004
2005     if (button_pressed != button_pressed_last)
2006     {
2007       DrawCursorAndText_Main(choice, TRUE, button_pressed);
2008
2009       if (button_pressed)
2010         PlaySound(SND_MENU_BUTTON_PRESSING);
2011       else
2012         PlaySound(SND_MENU_BUTTON_RELEASING);
2013     }
2014   }
2015   else if (dx || dy)    // keyboard input
2016   {
2017     if (dx > 0 && (choice == MAIN_CONTROL_INFO ||
2018                    choice == MAIN_CONTROL_SETUP))
2019       button = MB_MENU_CHOICE;
2020     else if (dy)
2021       pos = choice + dy;
2022   }
2023
2024   if (pos == MAIN_CONTROL_FIRST_LEVEL && !button)
2025   {
2026     HandleMainMenu_SelectLevel(MAX_LEVELS, -1, NO_DIRECT_LEVEL_SELECT);
2027   }
2028   else if (pos == MAIN_CONTROL_LAST_LEVEL && !button)
2029   {
2030     HandleMainMenu_SelectLevel(MAX_LEVELS, +1, NO_DIRECT_LEVEL_SELECT);
2031   }
2032   else if (pos == MAIN_CONTROL_LEVEL_NUMBER && !button)
2033   {
2034     CloseDoor(DOOR_CLOSE_2);
2035
2036     SetGameStatus(GAME_MODE_LEVELNR);
2037
2038     DrawChooseLevelNr();
2039   }
2040   else if (pos >= MAIN_CONTROL_NAME && pos <= MAIN_CONTROL_QUIT)
2041   {
2042     if (button)
2043     {
2044       if (pos != choice)
2045       {
2046         PlaySound(SND_MENU_ITEM_ACTIVATING);
2047
2048         DrawCursorAndText_Main(choice, FALSE, FALSE);
2049         DrawCursorAndText_Main(pos, TRUE, button_pressed);
2050
2051         choice = pos;
2052       }
2053       else if (dx != 0)
2054       {
2055         if (choice != MAIN_CONTROL_INFO &&
2056             choice != MAIN_CONTROL_SETUP)
2057           HandleMainMenu_SelectLevel(1, dx, NO_DIRECT_LEVEL_SELECT);
2058       }
2059     }
2060     else
2061     {
2062       PlaySound(SND_MENU_ITEM_SELECTING);
2063
2064       if (pos == MAIN_CONTROL_NAME)
2065       {
2066         if ((mx || my) &&
2067             insideTextPosRect(main_controls[i].pos_text, mx - mSX, my - mSY))
2068         {
2069           // special case: menu text "name/team" clicked -- toggle team mode
2070           setup.team_mode = !setup.team_mode;
2071
2072           InitializeMainControls();
2073           DrawCursorAndText_Main(choice, TRUE, FALSE);
2074
2075           DrawPreviewPlayers();
2076         }
2077         else
2078         {
2079           SetGameStatus(GAME_MODE_PSEUDO_TYPENAME);
2080
2081           HandleTypeName(strlen(setup.player_name), 0);
2082         }
2083       }
2084       else if (pos == MAIN_CONTROL_LEVELS)
2085       {
2086         if (leveldir_first)
2087         {
2088           CloseDoor(DOOR_CLOSE_2);
2089
2090           SetGameStatus(GAME_MODE_LEVELS);
2091
2092           SaveLevelSetup_LastSeries();
2093           SaveLevelSetup_SeriesInfo();
2094
2095           if (setup.internal.choose_from_top_leveldir)
2096             gotoTopLevelDir();
2097
2098           DrawChooseLevelSet();
2099         }
2100       }
2101       else if (pos == MAIN_CONTROL_SCORES)
2102       {
2103         CloseDoor(DOOR_CLOSE_2);
2104
2105         SetGameStatus(GAME_MODE_SCORES);
2106
2107         DrawHallOfFame(level_nr, -1);
2108       }
2109       else if (pos == MAIN_CONTROL_EDITOR)
2110       {
2111         if (leveldir_current->readonly &&
2112             !strEqual(setup.player_name, "Artsoft"))
2113           Request("This level is read only!", REQ_CONFIRM);
2114
2115         CloseDoor(DOOR_CLOSE_2);
2116
2117         SetGameStatus(GAME_MODE_EDITOR);
2118
2119         FadeSetEnterScreen();
2120
2121         DrawLevelEd();
2122       }
2123       else if (pos == MAIN_CONTROL_INFO)
2124       {
2125         CloseDoor(DOOR_CLOSE_2);
2126
2127         SetGameStatus(GAME_MODE_INFO);
2128
2129         info_mode = INFO_MODE_MAIN;
2130
2131         DrawInfoScreen();
2132       }
2133       else if (pos == MAIN_CONTROL_GAME)
2134       {
2135         StartGameActions(network.enabled, setup.autorecord, level.random_seed);
2136       }
2137       else if (pos == MAIN_CONTROL_SETUP)
2138       {
2139         CloseDoor(DOOR_CLOSE_2);
2140
2141         SetGameStatus(GAME_MODE_SETUP);
2142
2143         setup_mode = SETUP_MODE_MAIN;
2144
2145         DrawSetupScreen();
2146       }
2147       else if (pos == MAIN_CONTROL_QUIT)
2148       {
2149         SaveLevelSetup_LastSeries();
2150         SaveLevelSetup_SeriesInfo();
2151
2152         if (Request("Do you really want to quit?", REQ_ASK | REQ_STAY_CLOSED))
2153           SetGameStatus(GAME_MODE_QUIT);
2154       }
2155     }
2156   }
2157
2158   button_pressed_last = button_pressed;
2159 }
2160
2161
2162 // ============================================================================
2163 // info screen functions
2164 // ============================================================================
2165
2166 static struct TokenInfo *info_info;
2167 static int num_info_info;       // number of info entries shown on screen
2168 static int max_info_info;       // total number of info entries in list
2169
2170 static void execInfoTitleScreen(void)
2171 {
2172   info_mode = INFO_MODE_TITLE;
2173
2174   DrawInfoScreen();
2175 }
2176
2177 static void execInfoElements(void)
2178 {
2179   info_mode = INFO_MODE_ELEMENTS;
2180
2181   DrawInfoScreen();
2182 }
2183
2184 static void execInfoMusic(void)
2185 {
2186   info_mode = INFO_MODE_MUSIC;
2187
2188   DrawInfoScreen();
2189 }
2190
2191 static void execInfoCredits(void)
2192 {
2193   info_mode = INFO_MODE_CREDITS;
2194
2195   DrawInfoScreen();
2196 }
2197
2198 static void execInfoProgram(void)
2199 {
2200   info_mode = INFO_MODE_PROGRAM;
2201
2202   DrawInfoScreen();
2203 }
2204
2205 static void execInfoVersion(void)
2206 {
2207   info_mode = INFO_MODE_VERSION;
2208
2209   DrawInfoScreen();
2210 }
2211
2212 static void execInfoLevelSet(void)
2213 {
2214   info_mode = INFO_MODE_LEVELSET;
2215
2216   DrawInfoScreen();
2217 }
2218
2219 static void execExitInfo(void)
2220 {
2221   SetGameStatus(GAME_MODE_MAIN);
2222
2223   DrawMainMenu();
2224 }
2225
2226 static struct TokenInfo info_info_main[] =
2227 {
2228   { TYPE_ENTER_SCREEN,  execInfoTitleScreen,    "Title Screen"          },
2229   { TYPE_ENTER_SCREEN,  execInfoElements,       "Elements Info"         },
2230   { TYPE_ENTER_SCREEN,  execInfoMusic,          "Music Info"            },
2231   { TYPE_ENTER_SCREEN,  execInfoCredits,        "Credits"               },
2232   { TYPE_ENTER_SCREEN,  execInfoProgram,        "Program Info"          },
2233   { TYPE_ENTER_SCREEN,  execInfoVersion,        "Version Info"          },
2234   { TYPE_ENTER_SCREEN,  execInfoLevelSet,       "Level Set Info"        },
2235   { TYPE_EMPTY,         NULL,                   ""                      },
2236   { TYPE_LEAVE_MENU,    execExitInfo,           "Exit"                  },
2237
2238   { 0,                  NULL,                   NULL                    }
2239 };
2240
2241 static int getMenuTextFont(int type)
2242 {
2243   if (type & (TYPE_SWITCH       |
2244               TYPE_YES_NO       |
2245               TYPE_YES_NO_AUTO  |
2246               TYPE_STRING       |
2247               TYPE_PLAYER       |
2248               TYPE_ECS_AGA      |
2249               TYPE_KEYTEXT      |
2250               TYPE_ENTER_LIST   |
2251               TYPE_TEXT_INPUT))
2252     return FONT_MENU_2;
2253   else
2254     return FONT_MENU_1;
2255 }
2256
2257 static struct TokenInfo *setup_info;
2258 static struct TokenInfo setup_info_input[];
2259
2260 static struct TokenInfo *menu_info;
2261
2262 static void DrawCursorAndText_Menu_Ext(struct TokenInfo *token_info,
2263                                        int screen_pos, int menu_info_pos_raw,
2264                                        boolean active)
2265 {
2266   int pos = (menu_info_pos_raw < 0 ? screen_pos : menu_info_pos_raw);
2267   struct TokenInfo *ti = &token_info[pos];
2268   int xpos = MENU_SCREEN_START_XPOS;
2269   int ypos = MENU_SCREEN_START_YPOS + screen_pos;
2270   int font_nr = getMenuTextFont(ti->type);
2271
2272   if (setup_mode == SETUP_MODE_INPUT)
2273     font_nr = FONT_MENU_1;
2274
2275   if (active)
2276     font_nr = FONT_ACTIVE(font_nr);
2277
2278   DrawText(mSX + xpos * 32, mSY + ypos * 32, ti->text, font_nr);
2279
2280   if (ti->type & ~TYPE_SKIP_ENTRY)
2281     drawCursor(screen_pos, active);
2282 }
2283
2284 static void DrawCursorAndText_Menu(int screen_pos, int menu_info_pos_raw,
2285                                    boolean active)
2286 {
2287   DrawCursorAndText_Menu_Ext(menu_info, screen_pos, menu_info_pos_raw, active);
2288 }
2289
2290 static void DrawCursorAndText_Setup(int screen_pos, int menu_info_pos_raw,
2291                                     boolean active)
2292 {
2293   DrawCursorAndText_Menu_Ext(setup_info, screen_pos, menu_info_pos_raw, active);
2294 }
2295
2296 static char *window_size_text;
2297 static char *scaling_type_text;
2298 static char *network_server_text;
2299
2300 static void drawSetupValue(int, int);
2301
2302 static void drawMenuInfoList(int first_entry, int num_page_entries,
2303                              int max_page_entries)
2304 {
2305   int i;
2306
2307   if (first_entry + num_page_entries > max_page_entries)
2308     first_entry = 0;
2309
2310   clearMenuListArea();
2311
2312   for (i = 0; i < num_page_entries; i++)
2313   {
2314     int menu_info_pos = first_entry + i;
2315     struct TokenInfo *si = &menu_info[menu_info_pos];
2316     void *value_ptr = si->value;
2317
2318     // set some entries to "unchangeable" according to other variables
2319     if ((value_ptr == &setup.sound_simple && !audio.sound_available) ||
2320         (value_ptr == &setup.sound_loops  && !audio.loops_available) ||
2321         (value_ptr == &setup.sound_music  && !audio.music_available) ||
2322         (value_ptr == &setup.fullscreen   && !video.fullscreen_available) ||
2323         (value_ptr == &window_size_text   && !video.window_scaling_available) ||
2324         (value_ptr == &scaling_type_text  && !video.window_scaling_available))
2325       si->type |= TYPE_GHOSTED;
2326
2327     if (si->type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
2328       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
2329     else if (si->type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
2330       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
2331     else if (si->type & ~TYPE_SKIP_ENTRY)
2332       initCursor(i, IMG_MENU_BUTTON);
2333
2334     DrawCursorAndText_Menu(i, menu_info_pos, FALSE);
2335
2336     if (si->type & TYPE_STRING)
2337     {
2338       int gadget_id = -1;
2339
2340       if (value_ptr == &network_server_text)
2341         gadget_id = SCREEN_CTRL_ID_NETWORK_SERVER;
2342
2343       if (gadget_id != -1)
2344       {
2345         struct GadgetInfo *gi = screen_gadget[gadget_id];
2346         int xpos = MENU_SCREEN_START_XPOS;
2347         int ypos = MENU_SCREEN_START_YPOS + i;
2348         int x = mSX + xpos * 32;
2349         int y = mSY + ypos * 32;
2350
2351         ModifyGadget(gi, GDI_X, x, GDI_Y, y, GDI_END);
2352       }
2353     }
2354
2355     if (si->type & TYPE_VALUE &&
2356         menu_info == setup_info)
2357       drawSetupValue(i, menu_info_pos);
2358   }
2359 }
2360
2361 static void DrawInfoScreen_Main(void)
2362 {
2363   int fade_mask = REDRAW_FIELD;
2364   int i;
2365
2366   if (redraw_mask & REDRAW_ALL)
2367     fade_mask = REDRAW_ALL;
2368
2369   if (CheckFadeAll())
2370     fade_mask = REDRAW_ALL;
2371
2372   UnmapAllGadgets();
2373   FadeMenuSoundsAndMusic();
2374
2375   FreeScreenGadgets();
2376   CreateScreenGadgets();
2377
2378   // (needed after displaying title screens which disable auto repeat)
2379   KeyboardAutoRepeatOn();
2380
2381   FadeSetLeaveScreen();
2382
2383   FadeOut(fade_mask);
2384
2385   // needed if different viewport properties defined for info screen
2386   ChangeViewportPropertiesIfNeeded();
2387
2388   SetMainBackgroundImage(IMG_BACKGROUND_INFO);
2389
2390   ClearField();
2391
2392   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
2393
2394   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Info Screen");
2395
2396   info_info = info_info_main;
2397
2398   // determine maximal number of info entries that can be displayed on screen
2399   num_info_info = 0;
2400   for (i = 0; info_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
2401     num_info_info++;
2402
2403   // determine maximal number of info entries available for menu of info screen
2404   max_info_info = 0;
2405   for (i = 0; info_info[i].type != 0; i++)
2406     max_info_info++;
2407
2408   HandleInfoScreen_Main(0, 0, 0, 0, MB_MENU_INITIALIZE);
2409
2410   MapScreenGadgets(max_info_info);
2411
2412   PlayMenuSoundsAndMusic();
2413
2414   DrawMaskedBorder(fade_mask);
2415
2416   FadeIn(fade_mask);
2417 }
2418
2419 static void changeSetupValue(int, int, int);
2420
2421 static void HandleMenuScreen(int mx, int my, int dx, int dy, int button,
2422                              int mode, int num_page_entries,
2423                              int max_page_entries)
2424 {
2425   static int num_page_entries_all_last[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
2426   static int choice_stores[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
2427   static int first_entry_stores[NUM_SPECIAL_GFX_ARGS][MAX_MENU_MODES];
2428   boolean has_scrollbar = screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->mapped;
2429   int mx_scrollbar = screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x;
2430   int mx_right_border = (has_scrollbar ? mx_scrollbar : SX + SXSIZE);
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 < mx_right_border &&
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, "%s (%s)",
3685             PLATFORM_STRING,
3686             PLATFORM_XX_BIT_STRING);
3687   ystart += ystep_line;
3688
3689   DrawTextF(xstart1, ystart, font_head, "Target");
3690   DrawTextF(xstart2, ystart, font_text, TARGET_STRING);
3691   ystart += ystep_line;
3692
3693   DrawTextF(xstart1, ystart, font_head, "Source date");
3694   DrawTextF(xstart2, ystart, font_text, getSourceDateString());
3695   ystart += ystep_line;
3696
3697   DrawTextF(xstart1, ystart, font_head, "Commit hash");
3698   DrawTextF(xstart2, ystart, font_text, getSourceHashString());
3699   ystart += ystep_para;
3700
3701   DrawTextF(xstart1, ystart, font_head, "Library");
3702   DrawTextF(xstart2, ystart, font_head, "compiled");
3703   DrawTextF(xstart3, ystart, font_head, "linked");
3704   ystart += ystep_head;
3705
3706   SDL_VERSION(&sdl_version_compiled);
3707   SDL_GetVersion(&sdl_version_linked_ext);
3708   sdl_version_linked = &sdl_version_linked_ext;
3709
3710   DrawTextF(xstart1, ystart, font_text, "SDL");
3711   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3712             sdl_version_compiled.major,
3713             sdl_version_compiled.minor,
3714             sdl_version_compiled.patch);
3715   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3716             sdl_version_linked->major,
3717             sdl_version_linked->minor,
3718             sdl_version_linked->patch);
3719   ystart += ystep_line;
3720
3721   SDL_IMAGE_VERSION(&sdl_version_compiled);
3722   sdl_version_linked = IMG_Linked_Version();
3723
3724   DrawTextF(xstart1, ystart, font_text, "SDL_image");
3725   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3726             sdl_version_compiled.major,
3727             sdl_version_compiled.minor,
3728             sdl_version_compiled.patch);
3729   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3730             sdl_version_linked->major,
3731             sdl_version_linked->minor,
3732             sdl_version_linked->patch);
3733   ystart += ystep_line;
3734
3735   SDL_MIXER_VERSION(&sdl_version_compiled);
3736   sdl_version_linked = Mix_Linked_Version();
3737
3738   DrawTextF(xstart1, ystart, font_text, "SDL_mixer");
3739   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3740             sdl_version_compiled.major,
3741             sdl_version_compiled.minor,
3742             sdl_version_compiled.patch);
3743   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3744             sdl_version_linked->major,
3745             sdl_version_linked->minor,
3746             sdl_version_linked->patch);
3747   ystart += ystep_line;
3748
3749   SDL_NET_VERSION(&sdl_version_compiled);
3750   sdl_version_linked = SDLNet_Linked_Version();
3751
3752   DrawTextF(xstart1, ystart, font_text, "SDL_net");
3753   DrawTextF(xstart2, ystart, font_text, "%d.%d.%d",
3754             sdl_version_compiled.major,
3755             sdl_version_compiled.minor,
3756             sdl_version_compiled.patch);
3757   DrawTextF(xstart3, ystart, font_text, "%d.%d.%d",
3758             sdl_version_linked->major,
3759             sdl_version_linked->minor,
3760             sdl_version_linked->patch);
3761   ystart += ystep_para;
3762
3763   DrawTextF(xstart1, ystart, font_head, "Driver");
3764   DrawTextF(xstart2, ystart, font_head, "Requested");
3765   DrawTextF(xstart3, ystart, font_head, "Used");
3766   ystart += ystep_head;
3767
3768   driver_name =
3769     getStringCopyNStatic(SDLGetRendererName(), driver_name_len);
3770
3771   DrawTextF(xstart1, ystart, font_text, "Render Driver");
3772   DrawTextF(xstart2, ystart, font_text, "%s", setup.system.sdl_renderdriver);
3773   DrawTextF(xstart3, ystart, font_text, "%s", driver_name);
3774   ystart += ystep_line;
3775
3776   driver_name =
3777     getStringCopyNStatic(SDL_GetCurrentVideoDriver(), driver_name_len);
3778
3779   DrawTextF(xstart1, ystart, font_text, "Video Driver");
3780   DrawTextF(xstart2, ystart, font_text, "%s", setup.system.sdl_videodriver);
3781   DrawTextF(xstart3, ystart, font_text, "%s", driver_name);
3782   ystart += ystep_line;
3783
3784   driver_name =
3785     getStringCopyNStatic(SDL_GetCurrentAudioDriver(), driver_name_len);
3786
3787   DrawTextF(xstart1, ystart, font_text, "Audio Driver");
3788   DrawTextF(xstart2, ystart, font_text, "%s", setup.system.sdl_audiodriver);
3789   DrawTextF(xstart3, ystart, font_text, "%s", driver_name);
3790
3791   DrawTextSCentered(ybottom, font_foot,
3792                     "Press any key or button for info menu");
3793
3794   FadeIn(REDRAW_FIELD);
3795 }
3796
3797 void HandleInfoScreen_Version(int button)
3798 {
3799   if (button == MB_MENU_LEAVE)
3800   {
3801     PlaySound(SND_MENU_ITEM_SELECTING);
3802
3803     info_mode = INFO_MODE_MAIN;
3804     DrawInfoScreen();
3805
3806     return;
3807   }
3808   else if (button == MB_MENU_CHOICE)
3809   {
3810     PlaySound(SND_MENU_ITEM_SELECTING);
3811
3812     FadeMenuSoundsAndMusic();
3813
3814     info_mode = INFO_MODE_MAIN;
3815     DrawInfoScreen();
3816   }
3817   else
3818   {
3819     PlayMenuSoundIfLoop();
3820   }
3821 }
3822
3823 static void DrawInfoScreen_LevelSet(void)
3824 {
3825   struct TitleMessageInfo *tmi = &readme;
3826   char *filename = getLevelSetInfoFilename();
3827   char *title = "Level Set Information:";
3828   int ystart  = mSY - SY + MENU_SCREEN_INFO_YSTART1;
3829   int ybottom = mSY - SY + MENU_SCREEN_INFO_YBOTTOM;
3830
3831   if (filename == NULL)
3832   {
3833     DrawInfoScreen_NotAvailable(title, "No information for this level set.");
3834
3835     return;
3836   }
3837
3838   SetMainBackgroundImageIfDefined(IMG_BACKGROUND_INFO_LEVELSET);
3839
3840   FadeOut(REDRAW_FIELD);
3841
3842   ClearField();
3843   DrawHeadline();
3844
3845   DrawTextSCentered(ystart, FONT_TEXT_1, title);
3846
3847   // if x position set to "-1", automatically determine by playfield width
3848   if (tmi->x == -1)
3849     tmi->x = SXSIZE / 2;
3850
3851   // if y position set to "-1", use static default value
3852   if (tmi->y == -1)
3853     tmi->y = 150;
3854
3855   // if width set to "-1", automatically determine by playfield width
3856   if (tmi->width == -1)
3857     tmi->width = SXSIZE - 2 * TILEX;
3858
3859   // if height set to "-1", automatically determine by playfield height
3860   if (tmi->height == -1)
3861     tmi->height = MENU_SCREEN_INFO_YBOTTOM - tmi->y - 10;
3862
3863   // if chars set to "-1", automatically determine by text and font width
3864   if (tmi->chars == -1)
3865     tmi->chars = tmi->width / getFontWidth(tmi->font);
3866   else
3867     tmi->width = tmi->chars * getFontWidth(tmi->font);
3868
3869   // if lines set to "-1", automatically determine by text and font height
3870   if (tmi->lines == -1)
3871     tmi->lines = tmi->height / getFontHeight(tmi->font);
3872   else
3873     tmi->height = tmi->lines * getFontHeight(tmi->font);
3874
3875   DrawTextFile(mSX + ALIGNED_TEXT_XPOS(tmi), mSY + ALIGNED_TEXT_YPOS(tmi),
3876                filename, tmi->font, tmi->chars, -1, tmi->lines, 0, -1,
3877                tmi->autowrap, tmi->centered, tmi->parse_comments);
3878
3879   DrawTextSCentered(ybottom, FONT_TEXT_4,
3880                     "Press any key or button for info menu");
3881
3882   FadeIn(REDRAW_FIELD);
3883 }
3884
3885 static void HandleInfoScreen_LevelSet(int button)
3886 {
3887   if (button == MB_MENU_LEAVE)
3888   {
3889     PlaySound(SND_MENU_ITEM_SELECTING);
3890
3891     info_mode = INFO_MODE_MAIN;
3892     DrawInfoScreen();
3893
3894     return;
3895   }
3896   else if (button == MB_MENU_CHOICE)
3897   {
3898     PlaySound(SND_MENU_ITEM_SELECTING);
3899
3900     FadeMenuSoundsAndMusic();
3901
3902     info_mode = INFO_MODE_MAIN;
3903     DrawInfoScreen();
3904   }
3905   else
3906   {
3907     PlayMenuSoundIfLoop();
3908   }
3909 }
3910
3911 static void DrawInfoScreen(void)
3912 {
3913   if (info_mode == INFO_MODE_TITLE)
3914     DrawInfoScreen_TitleScreen();
3915   else if (info_mode == INFO_MODE_ELEMENTS)
3916     DrawInfoScreen_Elements();
3917   else if (info_mode == INFO_MODE_MUSIC)
3918     DrawInfoScreen_Music();
3919   else if (info_mode == INFO_MODE_CREDITS)
3920     DrawInfoScreen_Credits();
3921   else if (info_mode == INFO_MODE_PROGRAM)
3922     DrawInfoScreen_Program();
3923   else if (info_mode == INFO_MODE_VERSION)
3924     DrawInfoScreen_Version();
3925   else if (info_mode == INFO_MODE_LEVELSET)
3926     DrawInfoScreen_LevelSet();
3927   else
3928     DrawInfoScreen_Main();
3929
3930   if (info_mode != INFO_MODE_MAIN &&
3931       info_mode != INFO_MODE_TITLE &&
3932       info_mode != INFO_MODE_MUSIC)
3933     PlayMenuSoundsAndMusic();
3934 }
3935
3936 void HandleInfoScreen(int mx, int my, int dx, int dy, int button)
3937 {
3938   if (info_mode == INFO_MODE_TITLE)
3939     HandleInfoScreen_TitleScreen(button);
3940   else if (info_mode == INFO_MODE_ELEMENTS)
3941     HandleInfoScreen_Elements(button);
3942   else if (info_mode == INFO_MODE_MUSIC)
3943     HandleInfoScreen_Music(button);
3944   else if (info_mode == INFO_MODE_CREDITS)
3945     HandleInfoScreen_Credits(button);
3946   else if (info_mode == INFO_MODE_PROGRAM)
3947     HandleInfoScreen_Program(button);
3948   else if (info_mode == INFO_MODE_VERSION)
3949     HandleInfoScreen_Version(button);
3950   else if (info_mode == INFO_MODE_LEVELSET)
3951     HandleInfoScreen_LevelSet(button);
3952   else
3953     HandleInfoScreen_Main(mx, my, dx, dy, button);
3954 }
3955
3956
3957 // ============================================================================
3958 // type name functions
3959 // ============================================================================
3960
3961 void HandleTypeName(int newxpos, Key key)
3962 {
3963   static char last_player_name[MAX_PLAYER_NAME_LEN + 1];
3964   struct MainControlInfo *mci = getMainControlInfo(MAIN_CONTROL_NAME);
3965   struct TextPosInfo *pos = mci->pos_input;
3966   int startx = mSX + ALIGNED_TEXT_XPOS(pos);
3967   int starty = mSY + ALIGNED_TEXT_YPOS(pos);
3968   static int xpos = 0;
3969   int font_nr = pos->font;
3970   int font_active_nr = FONT_ACTIVE(font_nr);
3971   int font_width = getFontWidth(font_active_nr);
3972   char key_char = getValidConfigValueChar(getCharFromKey(key));
3973   boolean is_valid_key_char = (key_char != 0 && (key_char != ' ' || xpos > 0));
3974   boolean is_active = TRUE;
3975
3976   DrawBackgroundForFont(startx,starty, pos->width, pos->height, font_active_nr);
3977
3978   if (newxpos)
3979   {
3980     strcpy(last_player_name, setup.player_name);
3981
3982     xpos = newxpos;
3983
3984     StartTextInput(startx, starty, pos->width, pos->height);
3985   }
3986   else if (is_valid_key_char && xpos < MAX_PLAYER_NAME_LEN)
3987   {
3988     setup.player_name[xpos] = key_char;
3989     setup.player_name[xpos + 1] = 0;
3990
3991     xpos++;
3992   }
3993   else if ((key == KSYM_Delete || key == KSYM_BackSpace) && xpos > 0)
3994   {
3995     xpos--;
3996
3997     setup.player_name[xpos] = 0;
3998   }
3999   else if (key == KSYM_Return && xpos > 0)
4000   {
4001     SaveSetup();
4002
4003     is_active = FALSE;
4004
4005     SetGameStatus(GAME_MODE_MAIN);
4006   }
4007   else if (key == KSYM_Escape)
4008   {
4009     strcpy(setup.player_name, last_player_name);
4010
4011     is_active = FALSE;
4012
4013     SetGameStatus(GAME_MODE_MAIN);
4014   }
4015
4016   if (is_active)
4017   {
4018     pos->width = (strlen(setup.player_name) + 1) * font_width;
4019     startx = mSX + ALIGNED_TEXT_XPOS(pos);
4020
4021     DrawText(startx, starty, setup.player_name, font_active_nr);
4022     DrawText(startx + xpos * font_width, starty, "_", font_active_nr);
4023   }
4024   else
4025   {
4026     pos->width = strlen(setup.player_name) * font_width;
4027     startx = mSX + ALIGNED_TEXT_XPOS(pos);
4028
4029     DrawText(startx, starty, setup.player_name, font_nr);
4030
4031     StopTextInput();
4032   }
4033 }
4034
4035
4036 // ============================================================================
4037 // tree menu functions
4038 // ============================================================================
4039
4040 static int getAlignXOffsetFromTreeInfo(TreeInfo *ti)
4041 {
4042   if (game_status != GAME_MODE_SETUP ||
4043       DRAW_MODE_SETUP(setup_mode) != SETUP_MODE_CHOOSE_OTHER)
4044     return 0;
4045
4046   int max_text_size = 0;
4047   TreeInfo *node;
4048
4049   for (node = getTreeInfoFirstGroupEntry(ti); node != NULL; node = node->next)
4050     max_text_size = MAX(max_text_size, strlen(node->name));
4051
4052   int num_entries = numTreeInfoInGroup(ti);
4053   boolean scrollbar_needed = (num_entries > NUM_MENU_ENTRIES_ON_SCREEN);
4054   int text_width = max_text_size * getFontWidth(FONT_TEXT_1);
4055   int button_width = SC_MENUBUTTON_XSIZE;
4056   int scrollbar_xpos = SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
4057   int screen_width = (scrollbar_needed ? scrollbar_xpos : SXSIZE);
4058   int align = menu.list_setup[SETUP_MODE_CHOOSE_OTHER].align;
4059   int x = ALIGNED_XPOS(0, screen_width, align) * -1;
4060   int align_xoffset_raw = ALIGNED_XPOS(x, button_width + text_width, align);
4061   int align_xoffset = MAX(0, align_xoffset_raw);
4062
4063   return align_xoffset;
4064 }
4065
4066 static int getAlignYOffsetFromTreeInfo(TreeInfo *ti)
4067 {
4068   if (game_status != GAME_MODE_SETUP ||
4069       DRAW_MODE_SETUP(setup_mode) != SETUP_MODE_CHOOSE_OTHER)
4070     return 0;
4071
4072   int num_entries = numTreeInfoInGroup(ti);
4073   int num_page_entries = MIN(num_entries, NUM_MENU_ENTRIES_ON_SCREEN);
4074   int font_height = getFontHeight(FONT_TEXT_1);
4075   int text_height = font_height * num_page_entries;
4076   int page_height = font_height * NUM_MENU_ENTRIES_ON_SCREEN;
4077   int align = menu.list_setup[SETUP_MODE_CHOOSE_OTHER].valign;
4078   int y = ALIGNED_YPOS(0, page_height, align) * -1;
4079   int align_yoffset_raw = ALIGNED_YPOS(y, text_height, align);
4080   int align_yoffset = MAX(0, align_yoffset_raw);
4081
4082   return align_yoffset;
4083 }
4084
4085 static void DrawChooseTree(TreeInfo **ti_ptr)
4086 {
4087   int fade_mask = REDRAW_FIELD;
4088
4089   if (CheckFadeAll())
4090     fade_mask = REDRAW_ALL;
4091
4092   if (strEqual((*ti_ptr)->subdir, STRING_TOP_DIRECTORY))
4093   {
4094     SetGameStatus(GAME_MODE_MAIN);
4095
4096     DrawMainMenu();
4097
4098     return;
4099   }
4100
4101   UnmapAllGadgets();
4102
4103   FreeScreenGadgets();
4104   CreateScreenGadgets();
4105
4106   FadeOut(fade_mask);
4107
4108   // needed if different viewport properties defined for choosing level (set)
4109   ChangeViewportPropertiesIfNeeded();
4110
4111   if (game_status == GAME_MODE_LEVELNR)
4112     SetMainBackgroundImage(IMG_BACKGROUND_LEVELNR);
4113   else if (game_status == GAME_MODE_LEVELS)
4114     SetMainBackgroundImage(IMG_BACKGROUND_LEVELS);
4115
4116   ClearField();
4117
4118   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
4119
4120   HandleChooseTree(0, 0, 0, 0, MB_MENU_INITIALIZE, ti_ptr);
4121   MapScreenTreeGadgets(*ti_ptr);
4122
4123   DrawMaskedBorder(fade_mask);
4124
4125   FadeIn(fade_mask);
4126 }
4127
4128 static void drawChooseTreeList(int first_entry, int num_page_entries,
4129                                TreeInfo *ti)
4130 {
4131   int num_entries = numTreeInfoInGroup(ti);
4132   boolean scrollbar_needed = (num_entries > NUM_MENU_ENTRIES_ON_SCREEN);
4133   int scrollbar_xpos = SC_SCROLLBAR_XPOS + menu.scrollbar_xoffset;
4134   int screen_width = (scrollbar_needed ? scrollbar_xpos : SXSIZE);
4135   int font_nr = FONT_TEXT_1;
4136   int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
4137   int i;
4138   char *title_string = NULL;
4139   int yoffset_sets = MENU_TITLE1_YPOS;
4140   int yoffset_setup = 16;
4141   int yoffset = (ti->type == TREE_TYPE_LEVEL_DIR ||
4142                  ti->type == TREE_TYPE_LEVEL_NR ? yoffset_sets : yoffset_setup);
4143
4144   title_string = ti->infotext;
4145
4146   DrawTextSCentered(mSY - SY + yoffset, FONT_TITLE_1, title_string);
4147
4148   clearMenuListArea();
4149
4150   for (i = 0; i < num_page_entries; i++)
4151   {
4152     TreeInfo *node, *node_first;
4153     int entry_pos = first_entry + i;
4154     int xpos = MENU_SCREEN_START_XPOS;
4155     int ypos = MENU_SCREEN_START_YPOS + i;
4156     int startx = amSX + xpos * 32;
4157     int starty = amSY + ypos * 32;
4158     int startx_text = startx + font_xoffset;
4159     int endx_text = amSX + screen_width;
4160     int max_text_size = endx_text - startx_text;
4161     int max_buffer_len = max_text_size / getFontWidth(font_nr);
4162     char buffer[max_buffer_len + 1];
4163
4164     node_first = getTreeInfoFirstGroupEntry(ti);
4165     node = getTreeInfoFromPos(node_first, entry_pos);
4166
4167     strncpy(buffer, node->name, max_buffer_len);
4168     buffer[max_buffer_len] = '\0';
4169
4170     DrawText(startx, starty, buffer, font_nr + node->color);
4171
4172     if (node->parent_link)
4173       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
4174     else if (node->level_group)
4175       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
4176     else
4177       initCursor(i, IMG_MENU_BUTTON);
4178   }
4179
4180   redraw_mask |= REDRAW_FIELD;
4181 }
4182
4183 static void drawChooseTreeInfo(int entry_pos, TreeInfo *ti)
4184 {
4185   TreeInfo *node, *node_first;
4186   int x, last_redraw_mask = redraw_mask;
4187   int ypos = MENU_TITLE2_YPOS;
4188   int font_nr = FONT_TITLE_2;
4189
4190   if (ti->type == TREE_TYPE_LEVEL_NR)
4191     DrawTextFCentered(ypos, font_nr, leveldir_current->name);
4192
4193   if (ti->type != TREE_TYPE_LEVEL_DIR)
4194     return;
4195
4196   node_first = getTreeInfoFirstGroupEntry(ti);
4197   node = getTreeInfoFromPos(node_first, entry_pos);
4198
4199   DrawBackgroundForFont(SX, SY + ypos, SXSIZE, getFontHeight(font_nr), font_nr);
4200
4201   if (node->parent_link)
4202     DrawTextFCentered(ypos, font_nr, "leave \"%s\"",
4203                       node->node_parent->name);
4204   else if (node->level_group)
4205     DrawTextFCentered(ypos, font_nr, "enter \"%s\"",
4206                       node->name);
4207   else if (ti->type == TREE_TYPE_LEVEL_DIR)
4208     DrawTextFCentered(ypos, font_nr, "%3d %s (%s)",
4209                       node->levels, (node->levels > 1 ? "levels" : "level"),
4210                       node->class_desc);
4211
4212   // let BackToFront() redraw only what is needed
4213   redraw_mask = last_redraw_mask;
4214   for (x = 0; x < SCR_FIELDX; x++)
4215     MarkTileDirty(x, 1);
4216 }
4217
4218 static void HandleChooseTree(int mx, int my, int dx, int dy, int button,
4219                              TreeInfo **ti_ptr)
4220 {
4221   TreeInfo *ti = *ti_ptr;
4222   boolean has_scrollbar = screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->mapped;
4223   int mx_scrollbar = screen_gadget[SCREEN_CTRL_ID_SCROLL_VERTICAL]->x;
4224   int mx_right_border = (has_scrollbar ? mx_scrollbar : SX + SXSIZE);
4225   int x = 0;
4226   int y = ti->cl_cursor;
4227   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
4228   int num_entries = numTreeInfoInGroup(ti);
4229   int num_page_entries = MIN(num_entries, NUM_MENU_ENTRIES_ON_SCREEN);
4230   boolean position_set_by_scrollbar = (dx == 999);
4231
4232   if (button == MB_MENU_INITIALIZE)
4233   {
4234     int num_entries = numTreeInfoInGroup(ti);
4235     int entry_pos = posTreeInfo(ti);
4236
4237     align_xoffset = getAlignXOffsetFromTreeInfo(ti);
4238     align_yoffset = getAlignYOffsetFromTreeInfo(ti);
4239
4240     if (ti->cl_first == -1)
4241     {
4242       // only on initialization
4243       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
4244       ti->cl_cursor = entry_pos - ti->cl_first;
4245     }
4246     else if (ti->cl_cursor >= num_page_entries ||
4247              (num_entries > num_page_entries &&
4248               num_entries - ti->cl_first < num_page_entries))
4249     {
4250       // only after change of list size (by custom graphic configuration)
4251       ti->cl_first = MAX(0, entry_pos - num_page_entries + 1);
4252       ti->cl_cursor = entry_pos - ti->cl_first;
4253     }
4254
4255     if (position_set_by_scrollbar)
4256       ti->cl_first = dy;
4257     else
4258       AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
4259                                 ti->cl_first, ti);
4260
4261     drawChooseTreeList(ti->cl_first, num_page_entries, ti);
4262     drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
4263     drawChooseTreeCursor(ti->cl_cursor, TRUE);
4264
4265     return;
4266   }
4267   else if (button == MB_MENU_LEAVE)
4268   {
4269     FadeSetLeaveMenu();
4270
4271     PlaySound(SND_MENU_ITEM_SELECTING);
4272
4273     if (ti->node_parent)
4274     {
4275       *ti_ptr = ti->node_parent;
4276       DrawChooseTree(ti_ptr);
4277     }
4278     else if (game_status == GAME_MODE_SETUP)
4279     {
4280       if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
4281           setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY ||
4282           setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
4283         execSetupGame();
4284       else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
4285                setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
4286                setup_mode == SETUP_MODE_CHOOSE_RENDERING ||
4287                setup_mode == SETUP_MODE_CHOOSE_VSYNC)
4288         execSetupGraphics();
4289       else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
4290                setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
4291                setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
4292         execSetupSound();
4293       else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
4294                setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
4295                setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE ||
4296                setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY ||
4297                setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_0 ||
4298                setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_0 ||
4299                setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_1 ||
4300                setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_1)
4301         execSetupTouch();
4302       else
4303         execSetupArtwork();
4304     }
4305     else
4306     {
4307       if (game_status == GAME_MODE_LEVELNR)
4308       {
4309         int new_level_nr = atoi(level_number_current->identifier);
4310
4311         HandleMainMenu_SelectLevel(0, 0, new_level_nr);
4312       }
4313
4314       SetGameStatus(GAME_MODE_MAIN);
4315
4316       DrawMainMenu();
4317     }
4318
4319     return;
4320   }
4321
4322   if (mx || my)         // mouse input
4323   {
4324     x = (mx - amSX) / 32;
4325     y = (my - amSY) / 32 - MENU_SCREEN_START_YPOS;
4326   }
4327   else if (dx || dy)    // keyboard or scrollbar/scrollbutton input
4328   {
4329     // move cursor instead of scrolling when already at start/end of list
4330     if (dy == -1 * SCROLL_LINE && ti->cl_first == 0)
4331       dy = -1;
4332     else if (dy == +1 * SCROLL_LINE &&
4333              ti->cl_first + num_page_entries == num_entries)
4334       dy = 1;
4335
4336     // handle scrolling screen one line or page
4337     if (ti->cl_cursor + dy < 0 ||
4338         ti->cl_cursor + dy > num_page_entries - 1)
4339     {
4340       boolean redraw = FALSE;
4341
4342       if (ABS(dy) == SCROLL_PAGE)
4343         step = num_page_entries - 1;
4344
4345       if (dy < 0 && ti->cl_first > 0)
4346       {
4347         // scroll page/line up
4348
4349         ti->cl_first -= step;
4350         if (ti->cl_first < 0)
4351           ti->cl_first = 0;
4352
4353         redraw = TRUE;
4354       }
4355       else if (dy > 0 && ti->cl_first + num_page_entries < num_entries)
4356       {
4357         // scroll page/line down
4358
4359         ti->cl_first += step;
4360         if (ti->cl_first + num_page_entries > num_entries)
4361           ti->cl_first = MAX(0, num_entries - num_page_entries);
4362
4363         redraw = TRUE;
4364       }
4365
4366       if (redraw)
4367       {
4368         drawChooseTreeList(ti->cl_first, num_page_entries, ti);
4369         drawChooseTreeInfo(ti->cl_first + ti->cl_cursor, ti);
4370         drawChooseTreeCursor(ti->cl_cursor, TRUE);
4371
4372         AdjustChooseTreeScrollbar(SCREEN_CTRL_ID_SCROLL_VERTICAL,
4373                                   ti->cl_first, ti);
4374       }
4375
4376       return;
4377     }
4378
4379     // handle moving cursor one line
4380     y = ti->cl_cursor + dy;
4381   }
4382
4383   if (dx == 1)
4384   {
4385     TreeInfo *node_first, *node_cursor;
4386     int entry_pos = ti->cl_first + y;
4387
4388     node_first = getTreeInfoFirstGroupEntry(ti);
4389     node_cursor = getTreeInfoFromPos(node_first, entry_pos);
4390
4391     if (node_cursor->node_group)
4392     {
4393       FadeSetEnterMenu();
4394
4395       PlaySound(SND_MENU_ITEM_SELECTING);
4396
4397       node_cursor->cl_first = ti->cl_first;
4398       node_cursor->cl_cursor = ti->cl_cursor;
4399       *ti_ptr = node_cursor->node_group;
4400       DrawChooseTree(ti_ptr);
4401
4402       return;
4403     }
4404   }
4405   else if (dx == -1 && ti->node_parent)
4406   {
4407     FadeSetLeaveMenu();
4408
4409     PlaySound(SND_MENU_ITEM_SELECTING);
4410
4411     *ti_ptr = ti->node_parent;
4412     DrawChooseTree(ti_ptr);
4413
4414     return;
4415   }
4416
4417   if (!anyScrollbarGadgetActive() &&
4418       IN_VIS_MENU(x, y) &&
4419       mx < mx_right_border &&
4420       y >= 0 && y < num_page_entries)
4421   {
4422     if (button)
4423     {
4424       if (y != ti->cl_cursor)
4425       {
4426         PlaySound(SND_MENU_ITEM_ACTIVATING);
4427
4428         drawChooseTreeCursor(ti->cl_cursor, FALSE);
4429         drawChooseTreeCursor(y, TRUE);
4430         drawChooseTreeInfo(ti->cl_first + y, ti);
4431
4432         ti->cl_cursor = y;
4433       }
4434       else if (dx < 0)
4435       {
4436         if (game_status == GAME_MODE_SETUP)
4437         {
4438           if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
4439               setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY ||
4440               setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
4441             execSetupGame();
4442           else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
4443                    setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
4444                    setup_mode == SETUP_MODE_CHOOSE_RENDERING ||
4445                    setup_mode == SETUP_MODE_CHOOSE_VSYNC)
4446             execSetupGraphics();
4447           else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
4448                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
4449                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
4450             execSetupSound();
4451           else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
4452                    setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
4453                    setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE ||
4454                    setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY ||
4455                    setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_0 ||
4456                    setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_0 ||
4457                    setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_1 ||
4458                    setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_1)
4459             execSetupTouch();
4460           else
4461             execSetupArtwork();
4462         }
4463       }
4464     }
4465     else
4466     {
4467       TreeInfo *node_first, *node_cursor;
4468       int entry_pos = ti->cl_first + y;
4469
4470       PlaySound(SND_MENU_ITEM_SELECTING);
4471
4472       node_first = getTreeInfoFirstGroupEntry(ti);
4473       node_cursor = getTreeInfoFromPos(node_first, entry_pos);
4474
4475       if (node_cursor->node_group)
4476       {
4477         FadeSetEnterMenu();
4478
4479         node_cursor->cl_first = ti->cl_first;
4480         node_cursor->cl_cursor = ti->cl_cursor;
4481         *ti_ptr = node_cursor->node_group;
4482         DrawChooseTree(ti_ptr);
4483       }
4484       else if (node_cursor->parent_link)
4485       {
4486         FadeSetLeaveMenu();
4487
4488         *ti_ptr = node_cursor->node_parent;
4489         DrawChooseTree(ti_ptr);
4490       }
4491       else
4492       {
4493         FadeSetEnterMenu();
4494
4495         node_cursor->cl_first = ti->cl_first;
4496         node_cursor->cl_cursor = ti->cl_cursor;
4497         *ti_ptr = node_cursor;
4498
4499         if (ti->type == TREE_TYPE_LEVEL_DIR)
4500         {
4501           LoadLevelSetup_SeriesInfo();
4502
4503           SaveLevelSetup_LastSeries();
4504           SaveLevelSetup_SeriesInfo();
4505           TapeErase();
4506         }
4507
4508         if (game_status == GAME_MODE_SETUP)
4509         {
4510           if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED ||
4511               setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY ||
4512               setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
4513             execSetupGame();
4514           else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE ||
4515                    setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE ||
4516                    setup_mode == SETUP_MODE_CHOOSE_RENDERING ||
4517                    setup_mode == SETUP_MODE_CHOOSE_VSYNC)
4518             execSetupGraphics();
4519           else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE ||
4520                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS ||
4521                    setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
4522             execSetupSound();
4523           else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL ||
4524                    setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE ||
4525                    setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE ||
4526                    setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY ||
4527                    setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_0 ||
4528                    setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_0 ||
4529                    setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_1 ||
4530                    setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_1)
4531             execSetupTouch();
4532           else
4533             execSetupArtwork();
4534         }
4535         else
4536         {
4537           if (game_status == GAME_MODE_LEVELNR)
4538           {
4539             int new_level_nr = atoi(level_number_current->identifier);
4540
4541             HandleMainMenu_SelectLevel(0, 0, new_level_nr);
4542           }
4543
4544           SetGameStatus(GAME_MODE_MAIN);
4545
4546           DrawMainMenu();
4547         }
4548       }
4549     }
4550   }
4551 }
4552
4553 void DrawChooseLevelSet(void)
4554 {
4555   FadeMenuSoundsAndMusic();
4556
4557   DrawChooseTree(&leveldir_current);
4558
4559   PlayMenuSoundsAndMusic();
4560 }
4561
4562 void HandleChooseLevelSet(int mx, int my, int dx, int dy, int button)
4563 {
4564   HandleChooseTree(mx, my, dx, dy, button, &leveldir_current);
4565 }
4566
4567 void DrawChooseLevelNr(void)
4568 {
4569   int i;
4570
4571   FadeMenuSoundsAndMusic();
4572
4573   if (level_number != NULL)
4574   {
4575     freeTreeInfo(level_number);
4576
4577     level_number = NULL;
4578   }
4579
4580   for (i = leveldir_current->first_level; i <= leveldir_current->last_level;i++)
4581   {
4582     TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_LEVEL_NR);
4583     char identifier[32], name[64];
4584     int value = i;
4585
4586     // temporarily load level info to get level name
4587     LoadLevelInfoOnly(i);
4588
4589     ti->node_top = &level_number;
4590     ti->sort_priority = 10000 + value;
4591     ti->color = (level.no_level_file ? FC_BLUE :
4592                  LevelStats_getSolved(i) ? FC_GREEN :
4593                  LevelStats_getPlayed(i) ? FC_YELLOW : FC_RED);
4594
4595     snprintf(identifier, sizeof(identifier), "%d", value);
4596     snprintf(name, sizeof(name), "%03d: %s", value,
4597              (level.no_level_file ? "(no file)" : level.name));
4598
4599     setString(&ti->identifier, identifier);
4600     setString(&ti->name, name);
4601     setString(&ti->name_sorting, name);
4602
4603     pushTreeInfo(&level_number, ti);
4604   }
4605
4606   // sort level number values to start with lowest level number
4607   sortTreeInfo(&level_number);
4608
4609   // set current level number to current level number
4610   level_number_current =
4611     getTreeInfoFromIdentifier(level_number, i_to_a(level_nr));
4612
4613   // if that also fails, set current level number to first available level
4614   if (level_number_current == NULL)
4615     level_number_current = level_number;
4616
4617   DrawChooseTree(&level_number_current);
4618
4619   PlayMenuSoundsAndMusic();
4620 }
4621
4622 void HandleChooseLevelNr(int mx, int my, int dx, int dy, int button)
4623 {
4624   HandleChooseTree(mx, my, dx, dy, button, &level_number_current);
4625 }
4626
4627 void DrawHallOfFame(int level_nr, int highlight_position)
4628 {
4629   int fade_mask = REDRAW_FIELD;
4630
4631   if (CheckFadeAll())
4632     fade_mask = REDRAW_ALL;
4633
4634   UnmapAllGadgets();
4635   FadeMenuSoundsAndMusic();
4636
4637   // (this is needed when called from GameEnd() after winning a game)
4638   KeyboardAutoRepeatOn();
4639
4640   // (this is needed when called from GameEnd() after winning a game)
4641   SetDrawDeactivationMask(REDRAW_NONE);
4642   SetDrawBackgroundMask(REDRAW_FIELD);
4643
4644   if (highlight_position < 0) 
4645     LoadScore(level_nr);
4646   else
4647     SetAnimStatus(GAME_MODE_PSEUDO_SCORESNEW);
4648
4649   FadeSetEnterScreen();
4650
4651   FadeOut(fade_mask);
4652
4653   // needed if different viewport properties defined for scores
4654   ChangeViewportPropertiesIfNeeded();
4655
4656   PlayMenuSoundsAndMusic();
4657
4658   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
4659
4660   HandleHallOfFame(level_nr, highlight_position, 0, 0, MB_MENU_INITIALIZE);
4661
4662   DrawMaskedBorder(fade_mask);
4663
4664   FadeIn(fade_mask);
4665 }
4666
4667 static void drawHallOfFameList(int level_nr, int first_entry,
4668                                int highlight_position)
4669 {
4670   int i, j;
4671
4672   SetMainBackgroundImage(IMG_BACKGROUND_SCORES);
4673   ClearField();
4674
4675   DrawTextSCentered(MENU_TITLE1_YPOS, FONT_TITLE_1, "Hall Of Fame");
4676   DrawTextFCentered(MENU_TITLE2_YPOS, FONT_TITLE_2,
4677                     "HighScores of Level %d", level_nr);
4678
4679   for (i = 0; i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
4680   {
4681     int entry = first_entry + i;
4682     boolean active = (entry == highlight_position);
4683     int font_nr1 = (active ? FONT_TEXT_1_ACTIVE : FONT_TEXT_1);
4684     int font_nr2 = (active ? FONT_TEXT_2_ACTIVE : FONT_TEXT_2);
4685     int font_nr3 = (active ? FONT_TEXT_3_ACTIVE : FONT_TEXT_3);
4686     int font_nr4 = (active ? FONT_TEXT_4_ACTIVE : FONT_TEXT_4);
4687     int dxoff = getFontDrawOffsetX(font_nr1);
4688     int dx1 = 3 * getFontWidth(font_nr1);
4689     int dx2 = dx1 + getFontWidth(font_nr1);
4690     int dx3 = SXSIZE - 2 * (mSX - SX + dxoff) - 5 * getFontWidth(font_nr4);
4691     int num_dots = (dx3 - dx2) / getFontWidth(font_nr3);
4692     int sy = mSY + 64 + i * 32;
4693
4694     DrawText(mSX, sy, int2str(entry + 1, 3), font_nr1);
4695     DrawText(mSX + dx1, sy, ".", font_nr1);
4696
4697     for (j = 0; j < num_dots; j++)
4698       DrawText(mSX + dx2 + j * getFontWidth(font_nr3), sy, ".", font_nr3);
4699
4700     if (!strEqual(highscore[entry].Name, EMPTY_PLAYER_NAME))
4701       DrawText(mSX + dx2, sy, highscore[entry].Name, font_nr2);
4702
4703     DrawText(mSX + dx3, sy, int2str(highscore[entry].Score, 5), font_nr4);
4704   }
4705
4706   redraw_mask |= REDRAW_FIELD;
4707 }
4708
4709 void HandleHallOfFame(int mx, int my, int dx, int dy, int button)
4710 {
4711   static int level_nr = 0;
4712   static int first_entry = 0;
4713   static int highlight_position = 0;
4714   int step = (button == 1 ? 1 : button == 2 ? 5 : 10);
4715
4716   if (button == MB_MENU_INITIALIZE)
4717   {
4718     level_nr = mx;
4719     first_entry = 0;
4720     highlight_position = my;
4721
4722     drawHallOfFameList(level_nr, first_entry, highlight_position);
4723
4724     return;
4725   }
4726
4727   if (ABS(dy) == SCROLL_PAGE)           // handle scrolling one page
4728     step = NUM_MENU_ENTRIES_ON_SCREEN - 1;
4729
4730   if (dy < 0)
4731   {
4732     if (first_entry > 0)
4733     {
4734       first_entry -= step;
4735       if (first_entry < 0)
4736         first_entry = 0;
4737
4738       drawHallOfFameList(level_nr, first_entry, highlight_position);
4739     }
4740   }
4741   else if (dy > 0)
4742   {
4743     if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN < MAX_SCORE_ENTRIES)
4744     {
4745       first_entry += step;
4746       if (first_entry + NUM_MENU_ENTRIES_ON_SCREEN > MAX_SCORE_ENTRIES)
4747         first_entry = MAX(0, MAX_SCORE_ENTRIES - NUM_MENU_ENTRIES_ON_SCREEN);
4748
4749       drawHallOfFameList(level_nr, first_entry, highlight_position);
4750     }
4751   }
4752   else if (button == MB_MENU_LEAVE || button == MB_MENU_CHOICE)
4753   {
4754     PlaySound(SND_MENU_ITEM_SELECTING);
4755
4756     FadeSound(SND_BACKGROUND_SCORES);
4757
4758     if (button == MB_MENU_CHOICE &&
4759         game_status_last_screen == GAME_MODE_PLAYING &&
4760         setup.auto_play_next_level && setup.increment_levels &&
4761         level_nr < leveldir_current->last_level &&
4762         !network_playing)
4763     {
4764       StartGameActions(network.enabled, setup.autorecord, level.random_seed);
4765     }
4766     else
4767     {
4768       SetGameStatus(GAME_MODE_MAIN);
4769
4770       DrawMainMenu();
4771     }
4772   }
4773
4774   if (game_status == GAME_MODE_SCORES)
4775     PlayMenuSoundIfLoop();
4776 }
4777
4778
4779 // ============================================================================
4780 // setup screen functions
4781 // ============================================================================
4782
4783 static struct TokenInfo *setup_info;
4784 static int num_setup_info;      // number of setup entries shown on screen
4785 static int max_setup_info;      // total number of setup entries in list
4786
4787 static char *window_size_text;
4788 static char *scaling_type_text;
4789 static char *rendering_mode_text;
4790 static char *vsync_mode_text;
4791 static char *scroll_delay_text;
4792 static char *snapshot_mode_text;
4793 static char *game_speed_text;
4794 static char *network_server_text;
4795 static char *graphics_set_name;
4796 static char *sounds_set_name;
4797 static char *music_set_name;
4798 static char *volume_simple_text;
4799 static char *volume_loops_text;
4800 static char *volume_music_text;
4801 static char *touch_controls_text;
4802 static char *move_distance_text;
4803 static char *drop_distance_text;
4804 static char *transparency_text;
4805 static char *grid_size_text[2][2];
4806
4807 static void execSetupMain(void)
4808 {
4809   setup_mode = SETUP_MODE_MAIN;
4810
4811   DrawSetupScreen();
4812 }
4813
4814 static void execSetupGame_setGameSpeeds(boolean update_value)
4815 {
4816   if (setup.game_speed_extended)
4817   {
4818     game_speeds_list = game_speeds_list_extended;
4819     game_speeds      = game_speeds_extended;
4820   }
4821   else
4822   {
4823     game_speeds_list = game_speeds_list_normal;
4824     game_speeds      = game_speeds_normal;
4825   }
4826
4827   if (game_speeds == NULL)
4828   {
4829     int i;
4830
4831     for (i = 0; game_speeds_list[i].value != -1; i++)
4832     {
4833       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4834       char identifier[32], name[32];
4835       int value = game_speeds_list[i].value;
4836       char *text = game_speeds_list[i].text;
4837
4838       ti->node_top = &game_speeds;
4839       ti->sort_priority = 10000 - value;
4840
4841       sprintf(identifier, "%d", value);
4842       sprintf(name, "%s", text);
4843
4844       setString(&ti->identifier, identifier);
4845       setString(&ti->name, name);
4846       setString(&ti->name_sorting, name);
4847       setString(&ti->infotext, STR_SETUP_CHOOSE_GAME_SPEED);
4848
4849       pushTreeInfo(&game_speeds, ti);
4850     }
4851
4852     // sort game speed values to start with slowest game speed
4853     sortTreeInfo(&game_speeds);
4854
4855     update_value = TRUE;
4856   }
4857
4858   if (update_value)
4859   {
4860     // set current game speed to configured game speed value
4861     game_speed_current =
4862       getTreeInfoFromIdentifier(game_speeds, i_to_a(setup.game_frame_delay));
4863
4864     // if that fails, set current game speed to reliable default value
4865     if (game_speed_current == NULL)
4866       game_speed_current =
4867         getTreeInfoFromIdentifier(game_speeds, i_to_a(GAME_FRAME_DELAY));
4868
4869     // if that also fails, set current game speed to first available speed
4870     if (game_speed_current == NULL)
4871       game_speed_current = game_speeds;
4872
4873     if (setup.game_speed_extended)
4874       game_speeds_extended = game_speeds;
4875     else
4876       game_speeds_normal = game_speeds;
4877   }
4878
4879   setup.game_frame_delay = atoi(game_speed_current->identifier);
4880
4881   // needed for displaying game speed text instead of identifier
4882   game_speed_text = game_speed_current->name;
4883 }
4884
4885 static void execSetupGame_setScrollDelays(void)
4886 {
4887   if (scroll_delays == NULL)
4888   {
4889     int i;
4890
4891     for (i = 0; scroll_delays_list[i].value != -1; i++)
4892     {
4893       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4894       char identifier[32], name[32];
4895       int value = scroll_delays_list[i].value;
4896       char *text = scroll_delays_list[i].text;
4897
4898       ti->node_top = &scroll_delays;
4899       ti->sort_priority = value;
4900
4901       sprintf(identifier, "%d", value);
4902       sprintf(name, "%s", text);
4903
4904       setString(&ti->identifier, identifier);
4905       setString(&ti->name, name);
4906       setString(&ti->name_sorting, name);
4907       setString(&ti->infotext, STR_SETUP_CHOOSE_SCROLL_DELAY);
4908
4909       pushTreeInfo(&scroll_delays, ti);
4910     }
4911
4912     // sort scroll delay values to start with lowest scroll delay value
4913     sortTreeInfo(&scroll_delays);
4914
4915     // set current scroll delay value to configured scroll delay value
4916     scroll_delay_current =
4917       getTreeInfoFromIdentifier(scroll_delays,i_to_a(setup.scroll_delay_value));
4918
4919     // if that fails, set current scroll delay to reliable default value
4920     if (scroll_delay_current == NULL)
4921       scroll_delay_current =
4922         getTreeInfoFromIdentifier(scroll_delays, i_to_a(STD_SCROLL_DELAY));
4923
4924     // if that also fails, set current scroll delay to first available value
4925     if (scroll_delay_current == NULL)
4926       scroll_delay_current = scroll_delays;
4927   }
4928
4929   setup.scroll_delay_value = atoi(scroll_delay_current->identifier);
4930
4931   // needed for displaying scroll delay text instead of identifier
4932   scroll_delay_text = scroll_delay_current->name;
4933 }
4934
4935 static void execSetupGame_setSnapshotModes(void)
4936 {
4937   if (snapshot_modes == NULL)
4938   {
4939     int i;
4940
4941     for (i = 0; snapshot_modes_list[i].value != NULL; i++)
4942     {
4943       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
4944       char identifier[32], name[32];
4945       char *value = snapshot_modes_list[i].value;
4946       char *text = snapshot_modes_list[i].text;
4947
4948       ti->node_top = &snapshot_modes;
4949       ti->sort_priority = i;
4950
4951       sprintf(identifier, "%s", value);
4952       sprintf(name, "%s", text);
4953
4954       setString(&ti->identifier, identifier);
4955       setString(&ti->name, name);
4956       setString(&ti->name_sorting, name);
4957       setString(&ti->infotext, STR_SETUP_CHOOSE_SNAPSHOT_MODE);
4958
4959       pushTreeInfo(&snapshot_modes, ti);
4960     }
4961
4962     // sort snapshot mode values to start with lowest snapshot mode value
4963     sortTreeInfo(&snapshot_modes);
4964
4965     // set current snapshot mode value to configured snapshot mode value
4966     snapshot_mode_current =
4967       getTreeInfoFromIdentifier(snapshot_modes, setup.engine_snapshot_mode);
4968
4969     // if that fails, set current snapshot mode to reliable default value
4970     if (snapshot_mode_current == NULL)
4971       snapshot_mode_current =
4972         getTreeInfoFromIdentifier(snapshot_modes, STR_SNAPSHOT_MODE_DEFAULT);
4973
4974     // if that also fails, set current snapshot mode to first available value
4975     if (snapshot_mode_current == NULL)
4976       snapshot_mode_current = snapshot_modes;
4977   }
4978
4979   setup.engine_snapshot_mode = snapshot_mode_current->identifier;
4980
4981   // needed for displaying snapshot mode text instead of identifier
4982   snapshot_mode_text = snapshot_mode_current->name;
4983 }
4984
4985 static void execSetupGame_setNetworkServerText(void)
4986 {
4987   if (strEqual(setup.network_server_hostname, STR_NETWORK_AUTO_DETECT))
4988   {
4989     strcpy(network_server_hostname, STR_NETWORK_AUTO_DETECT_SETUP);
4990   }
4991   else
4992   {
4993     strncpy(network_server_hostname, setup.network_server_hostname,
4994             MAX_SETUP_TEXT_INPUT_LEN);
4995     network_server_hostname[MAX_SETUP_TEXT_INPUT_LEN] = '\0';
4996   }
4997
4998   // needed for displaying network server text instead of identifier
4999   network_server_text = network_server_hostname;
5000 }
5001
5002 static void execSetupGame(void)
5003 {
5004   boolean check_vsync_mode = (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED);
5005
5006   execSetupGame_setGameSpeeds(FALSE);
5007   execSetupGame_setScrollDelays();
5008   execSetupGame_setSnapshotModes();
5009
5010   execSetupGame_setNetworkServerText();
5011
5012   setup_mode = SETUP_MODE_GAME;
5013
5014   DrawSetupScreen();
5015
5016   // check if vsync needs to be disabled for this game speed to work
5017   if (check_vsync_mode)
5018     DisableVsyncIfNeeded();
5019 }
5020
5021 static void execSetupChooseGameSpeed(void)
5022 {
5023   setup_mode = SETUP_MODE_CHOOSE_GAME_SPEED;
5024
5025   DrawSetupScreen();
5026 }
5027
5028 static void execSetupChooseScrollDelay(void)
5029 {
5030   setup_mode = SETUP_MODE_CHOOSE_SCROLL_DELAY;
5031
5032   DrawSetupScreen();
5033 }
5034
5035 static void execSetupChooseSnapshotMode(void)
5036 {
5037   setup_mode = SETUP_MODE_CHOOSE_SNAPSHOT_MODE;
5038
5039   DrawSetupScreen();
5040 }
5041
5042 static void execSetupEngines(void)
5043 {
5044   setup_mode = SETUP_MODE_ENGINES;
5045
5046   DrawSetupScreen();
5047 }
5048
5049 static void execSetupEditor(void)
5050 {
5051   setup_mode = SETUP_MODE_EDITOR;
5052
5053   DrawSetupScreen();
5054 }
5055
5056 static void execSetupGraphics_setWindowSizes(boolean update_list)
5057 {
5058   if (window_sizes != NULL && update_list)
5059   {
5060     freeTreeInfo(window_sizes);
5061
5062     window_sizes = NULL;
5063   }
5064
5065   if (window_sizes == NULL)
5066   {
5067     boolean current_window_size_found = FALSE;
5068     int i;
5069
5070     for (i = 0; window_sizes_list[i].value != -1; i++)
5071     {
5072       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5073       char identifier[32], name[32];
5074       int value = window_sizes_list[i].value;
5075       char *text = window_sizes_list[i].text;
5076
5077       ti->node_top = &window_sizes;
5078       ti->sort_priority = value;
5079
5080       sprintf(identifier, "%d", value);
5081       sprintf(name, "%s", text);
5082
5083       setString(&ti->identifier, identifier);
5084       setString(&ti->name, name);
5085       setString(&ti->name_sorting, name);
5086       setString(&ti->infotext, STR_SETUP_CHOOSE_WINDOW_SIZE);
5087
5088       pushTreeInfo(&window_sizes, ti);
5089
5090       if (value == setup.window_scaling_percent)
5091         current_window_size_found = TRUE;
5092     }
5093
5094     if (!current_window_size_found)
5095     {
5096       // add entry for non-preset window scaling value
5097
5098       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5099       char identifier[32], name[32];
5100       int value = setup.window_scaling_percent;
5101
5102       ti->node_top = &window_sizes;
5103       ti->sort_priority = value;
5104
5105       sprintf(identifier, "%d", value);
5106       sprintf(name, "%d %% (Current)", value);
5107
5108       setString(&ti->identifier, identifier);
5109       setString(&ti->name, name);
5110       setString(&ti->name_sorting, name);
5111       setString(&ti->infotext, STR_SETUP_CHOOSE_WINDOW_SIZE);
5112
5113       pushTreeInfo(&window_sizes, ti);
5114     }
5115
5116     // sort window size values to start with lowest window size value
5117     sortTreeInfo(&window_sizes);
5118
5119     // set current window size value to configured window size value
5120     window_size_current =
5121       getTreeInfoFromIdentifier(window_sizes,
5122                                 i_to_a(setup.window_scaling_percent));
5123
5124     // if that fails, set current window size to reliable default value
5125     if (window_size_current == NULL)
5126       window_size_current =
5127         getTreeInfoFromIdentifier(window_sizes,
5128                                   i_to_a(STD_WINDOW_SCALING_PERCENT));
5129
5130     // if that also fails, set current window size to first available value
5131     if (window_size_current == NULL)
5132       window_size_current = window_sizes;
5133   }
5134
5135   setup.window_scaling_percent = atoi(window_size_current->identifier);
5136
5137   // needed for displaying window size text instead of identifier
5138   window_size_text = window_size_current->name;
5139 }
5140
5141 static void execSetupGraphics_setScalingTypes(void)
5142 {
5143   if (scaling_types == NULL)
5144   {
5145     int i;
5146
5147     for (i = 0; scaling_types_list[i].value != NULL; i++)
5148     {
5149       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5150       char identifier[32], name[32];
5151       char *value = scaling_types_list[i].value;
5152       char *text = scaling_types_list[i].text;
5153
5154       ti->node_top = &scaling_types;
5155       ti->sort_priority = i;
5156
5157       sprintf(identifier, "%s", value);
5158       sprintf(name, "%s", text);
5159
5160       setString(&ti->identifier, identifier);
5161       setString(&ti->name, name);
5162       setString(&ti->name_sorting, name);
5163       setString(&ti->infotext, STR_SETUP_CHOOSE_SCALING_TYPE);
5164
5165       pushTreeInfo(&scaling_types, ti);
5166     }
5167
5168     // sort scaling type values to start with lowest scaling type value
5169     sortTreeInfo(&scaling_types);
5170
5171     // set current scaling type value to configured scaling type value
5172     scaling_type_current =
5173       getTreeInfoFromIdentifier(scaling_types, setup.window_scaling_quality);
5174
5175     // if that fails, set current scaling type to reliable default value
5176     if (scaling_type_current == NULL)
5177       scaling_type_current =
5178         getTreeInfoFromIdentifier(scaling_types, SCALING_QUALITY_DEFAULT);
5179
5180     // if that also fails, set current scaling type to first available value
5181     if (scaling_type_current == NULL)
5182       scaling_type_current = scaling_types;
5183   }
5184
5185   setup.window_scaling_quality = scaling_type_current->identifier;
5186
5187   // needed for displaying scaling type text instead of identifier
5188   scaling_type_text = scaling_type_current->name;
5189 }
5190
5191 static void execSetupGraphics_setRenderingModes(void)
5192 {
5193   if (rendering_modes == NULL)
5194   {
5195     int i;
5196
5197     for (i = 0; rendering_modes_list[i].value != NULL; i++)
5198     {
5199       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5200       char identifier[32], name[32];
5201       char *value = rendering_modes_list[i].value;
5202       char *text = rendering_modes_list[i].text;
5203
5204       ti->node_top = &rendering_modes;
5205       ti->sort_priority = i;
5206
5207       sprintf(identifier, "%s", value);
5208       sprintf(name, "%s", text);
5209
5210       setString(&ti->identifier, identifier);
5211       setString(&ti->name, name);
5212       setString(&ti->name_sorting, name);
5213       setString(&ti->infotext, STR_SETUP_CHOOSE_RENDERING);
5214
5215       pushTreeInfo(&rendering_modes, ti);
5216     }
5217
5218     // sort rendering mode values to start with lowest rendering mode value
5219     sortTreeInfo(&rendering_modes);
5220
5221     // set current rendering mode value to configured rendering mode value
5222     rendering_mode_current =
5223       getTreeInfoFromIdentifier(rendering_modes, setup.screen_rendering_mode);
5224
5225     // if that fails, set current rendering mode to reliable default value
5226     if (rendering_mode_current == NULL)
5227       rendering_mode_current =
5228         getTreeInfoFromIdentifier(rendering_modes,
5229                                   STR_SPECIAL_RENDERING_DEFAULT);
5230
5231     // if that also fails, set current rendering mode to first available one
5232     if (rendering_mode_current == NULL)
5233       rendering_mode_current = rendering_modes;
5234   }
5235
5236   setup.screen_rendering_mode = rendering_mode_current->identifier;
5237
5238   // needed for displaying rendering mode text instead of identifier
5239   rendering_mode_text = rendering_mode_current->name;
5240 }
5241
5242 static void execSetupGraphics_setVsyncModes(boolean update_value)
5243 {
5244   if (vsync_modes == NULL)
5245   {
5246     int i;
5247
5248     for (i = 0; vsync_modes_list[i].value != NULL; i++)
5249     {
5250       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5251       char identifier[32], name[32];
5252       char *value = vsync_modes_list[i].value;
5253       char *text = vsync_modes_list[i].text;
5254
5255       ti->node_top = &vsync_modes;
5256       ti->sort_priority = i;
5257
5258       sprintf(identifier, "%s", value);
5259       sprintf(name, "%s", text);
5260
5261       setString(&ti->identifier, identifier);
5262       setString(&ti->name, name);
5263       setString(&ti->name_sorting, name);
5264       setString(&ti->infotext, STR_SETUP_CHOOSE_VSYNC);
5265
5266       pushTreeInfo(&vsync_modes, ti);
5267     }
5268
5269     // sort vsync mode values to start with lowest vsync mode value
5270     sortTreeInfo(&vsync_modes);
5271
5272     update_value = TRUE;
5273   }
5274
5275   if (update_value)
5276   {
5277     // set current vsync mode value to configured vsync mode value
5278     vsync_mode_current =
5279       getTreeInfoFromIdentifier(vsync_modes, setup.vsync_mode);
5280
5281     // if that fails, set current vsync mode to reliable default value
5282     if (vsync_mode_current == NULL)
5283       vsync_mode_current =
5284         getTreeInfoFromIdentifier(vsync_modes, STR_VSYNC_MODE_DEFAULT);
5285
5286     // if that also fails, set current vsync mode to first available one
5287     if (vsync_mode_current == NULL)
5288       vsync_mode_current = vsync_modes;
5289   }
5290
5291   setup.vsync_mode = vsync_mode_current->identifier;
5292
5293   // needed for displaying vsync mode text instead of identifier
5294   vsync_mode_text = vsync_mode_current->name;
5295 }
5296
5297 static void execSetupGraphics(void)
5298 {
5299   boolean check_game_speed = (setup_mode == SETUP_MODE_CHOOSE_VSYNC);
5300
5301   // update "setup.window_scaling_percent" from list selection
5302   // (in this case, window scaling was changed on setup screen)
5303   if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
5304     execSetupGraphics_setWindowSizes(FALSE);
5305
5306   // update "setup.vsync_mode" from list selection
5307   // (in this case, vsync mode was changed on setup screen)
5308   if (setup_mode == SETUP_MODE_CHOOSE_VSYNC)
5309     execSetupGraphics_setVsyncModes(FALSE);
5310
5311   // update list selection from "setup.window_scaling_percent"
5312   // (window scaling may have changed by resizing the window)
5313   execSetupGraphics_setWindowSizes(TRUE);
5314
5315   // update list selection from "setup.vsync_mode"
5316   // (vsync_mode may have changed by re-creating the renderer)
5317   execSetupGraphics_setVsyncModes(TRUE);
5318
5319   execSetupGraphics_setScalingTypes();
5320   execSetupGraphics_setRenderingModes();
5321
5322   setup_mode = SETUP_MODE_GRAPHICS;
5323
5324   DrawSetupScreen();
5325
5326   // check if game speed is high enough for 60 Hz vsync to work
5327   if (check_game_speed)
5328     ModifyGameSpeedIfNeeded();
5329
5330   // window scaling may have changed at this point
5331   ChangeWindowScalingIfNeeded();
5332
5333   // window scaling quality may have changed at this point
5334   if (!strEqual(setup.window_scaling_quality, video.window_scaling_quality))
5335     SDLSetWindowScalingQuality(setup.window_scaling_quality);
5336
5337   // screen rendering mode may have changed at this point
5338   SDLSetScreenRenderingMode(setup.screen_rendering_mode);
5339
5340   int setup_vsync_mode = VSYNC_MODE_STR_TO_INT(setup.vsync_mode);
5341   int video_vsync_mode = video.vsync_mode;
5342
5343   // screen vsync mode may have changed at this point
5344   ChangeVsyncModeIfNeeded();
5345
5346   // check if setting vsync mode to selected value failed
5347   if (setup_vsync_mode != video_vsync_mode &&
5348       setup_vsync_mode != video.vsync_mode)
5349   {
5350     // changing vsync mode to selected value failed -- reset displayed value
5351     execSetupGraphics_setVsyncModes(TRUE);
5352
5353     Request("Setting VSync failed!", REQ_CONFIRM);
5354
5355     DrawSetupScreen();
5356   }
5357 }
5358
5359 static void execSetupChooseWindowSize(void)
5360 {
5361   setup_mode = SETUP_MODE_CHOOSE_WINDOW_SIZE;
5362
5363   DrawSetupScreen();
5364 }
5365
5366 static void execSetupChooseScalingType(void)
5367 {
5368   setup_mode = SETUP_MODE_CHOOSE_SCALING_TYPE;
5369
5370   DrawSetupScreen();
5371 }
5372
5373 static void execSetupChooseRenderingMode(void)
5374 {
5375   setup_mode = SETUP_MODE_CHOOSE_RENDERING;
5376
5377   DrawSetupScreen();
5378 }
5379
5380 static void execSetupChooseVsyncMode(void)
5381 {
5382   setup_mode = SETUP_MODE_CHOOSE_VSYNC;
5383
5384   DrawSetupScreen();
5385 }
5386
5387 static void execSetupChooseVolumeSimple(void)
5388 {
5389   setup_mode = SETUP_MODE_CHOOSE_VOLUME_SIMPLE;
5390
5391   DrawSetupScreen();
5392 }
5393
5394 static void execSetupChooseVolumeLoops(void)
5395 {
5396   setup_mode = SETUP_MODE_CHOOSE_VOLUME_LOOPS;
5397
5398   DrawSetupScreen();
5399 }
5400
5401 static void execSetupChooseVolumeMusic(void)
5402 {
5403   setup_mode = SETUP_MODE_CHOOSE_VOLUME_MUSIC;
5404
5405   DrawSetupScreen();
5406 }
5407
5408 static void execSetupSound(void)
5409 {
5410   if (volumes_simple == NULL)
5411   {
5412     boolean current_volume_simple_found = FALSE;
5413     int i;
5414
5415     for (i = 0; volumes_list[i].value != -1; i++)
5416     {
5417       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5418       char identifier[32], name[32];
5419       int value = volumes_list[i].value;
5420       char *text = volumes_list[i].text;
5421
5422       ti->node_top = &volumes_simple;
5423       ti->sort_priority = value;
5424
5425       sprintf(identifier, "%d", value);
5426       sprintf(name, "%s", text);
5427
5428       setString(&ti->identifier, identifier);
5429       setString(&ti->name, name);
5430       setString(&ti->name_sorting, name);
5431       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_SIMPLE);
5432
5433       pushTreeInfo(&volumes_simple, ti);
5434
5435       if (value == setup.volume_simple)
5436         current_volume_simple_found = TRUE;
5437     }
5438
5439     if (!current_volume_simple_found)
5440     {
5441       // add entry for non-preset volume value
5442
5443       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5444       char identifier[32], name[32];
5445       int value = setup.volume_simple;
5446
5447       ti->node_top = &volumes_simple;
5448       ti->sort_priority = value;
5449
5450       sprintf(identifier, "%d", value);
5451       sprintf(name, "%d %% (Current)", value);
5452
5453       setString(&ti->identifier, identifier);
5454       setString(&ti->name, name);
5455       setString(&ti->name_sorting, name);
5456       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_SIMPLE);
5457
5458       pushTreeInfo(&volumes_simple, ti);
5459     }
5460
5461     // sort volume values to start with lowest volume value
5462     sortTreeInfo(&volumes_simple);
5463
5464     // set current volume value to configured volume value
5465     volume_simple_current =
5466       getTreeInfoFromIdentifier(volumes_simple,i_to_a(setup.volume_simple));
5467
5468     // if that fails, set current volume to reliable default value
5469     if (volume_simple_current == NULL)
5470       volume_simple_current =
5471         getTreeInfoFromIdentifier(volumes_simple, i_to_a(100));
5472
5473     // if that also fails, set current volume to first available value
5474     if (volume_simple_current == NULL)
5475       volume_simple_current = volumes_simple;
5476   }
5477
5478   if (volumes_loops == NULL)
5479   {
5480     boolean current_volume_loops_found = FALSE;
5481     int i;
5482
5483     for (i = 0; volumes_list[i].value != -1; i++)
5484     {
5485       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5486       char identifier[32], name[32];
5487       int value = volumes_list[i].value;
5488       char *text = volumes_list[i].text;
5489
5490       ti->node_top = &volumes_loops;
5491       ti->sort_priority = value;
5492
5493       sprintf(identifier, "%d", value);
5494       sprintf(name, "%s", text);
5495
5496       setString(&ti->identifier, identifier);
5497       setString(&ti->name, name);
5498       setString(&ti->name_sorting, name);
5499       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_LOOPS);
5500
5501       pushTreeInfo(&volumes_loops, ti);
5502
5503       if (value == setup.volume_loops)
5504         current_volume_loops_found = TRUE;
5505     }
5506
5507     if (!current_volume_loops_found)
5508     {
5509       // add entry for non-preset volume value
5510
5511       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5512       char identifier[32], name[32];
5513       int value = setup.volume_loops;
5514
5515       ti->node_top = &volumes_loops;
5516       ti->sort_priority = value;
5517
5518       sprintf(identifier, "%d", value);
5519       sprintf(name, "%d %% (Current)", value);
5520
5521       setString(&ti->identifier, identifier);
5522       setString(&ti->name, name);
5523       setString(&ti->name_sorting, name);
5524       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_LOOPS);
5525
5526       pushTreeInfo(&volumes_loops, ti);
5527     }
5528
5529     // sort volume values to start with lowest volume value
5530     sortTreeInfo(&volumes_loops);
5531
5532     // set current volume value to configured volume value
5533     volume_loops_current =
5534       getTreeInfoFromIdentifier(volumes_loops,i_to_a(setup.volume_loops));
5535
5536     // if that fails, set current volume to reliable default value
5537     if (volume_loops_current == NULL)
5538       volume_loops_current =
5539         getTreeInfoFromIdentifier(volumes_loops, i_to_a(100));
5540
5541     // if that also fails, set current volume to first available value
5542     if (volume_loops_current == NULL)
5543       volume_loops_current = volumes_loops;
5544   }
5545
5546   if (volumes_music == NULL)
5547   {
5548     boolean current_volume_music_found = FALSE;
5549     int i;
5550
5551     for (i = 0; volumes_list[i].value != -1; i++)
5552     {
5553       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5554       char identifier[32], name[32];
5555       int value = volumes_list[i].value;
5556       char *text = volumes_list[i].text;
5557
5558       ti->node_top = &volumes_music;
5559       ti->sort_priority = value;
5560
5561       sprintf(identifier, "%d", value);
5562       sprintf(name, "%s", text);
5563
5564       setString(&ti->identifier, identifier);
5565       setString(&ti->name, name);
5566       setString(&ti->name_sorting, name);
5567       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_MUSIC);
5568
5569       pushTreeInfo(&volumes_music, ti);
5570
5571       if (value == setup.volume_music)
5572         current_volume_music_found = TRUE;
5573     }
5574
5575     if (!current_volume_music_found)
5576     {
5577       // add entry for non-preset volume value
5578
5579       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5580       char identifier[32], name[32];
5581       int value = setup.volume_music;
5582
5583       ti->node_top = &volumes_music;
5584       ti->sort_priority = value;
5585
5586       sprintf(identifier, "%d", value);
5587       sprintf(name, "%d %% (Current)", value);
5588
5589       setString(&ti->identifier, identifier);
5590       setString(&ti->name, name);
5591       setString(&ti->name_sorting, name);
5592       setString(&ti->infotext, STR_SETUP_CHOOSE_VOLUME_MUSIC);
5593
5594       pushTreeInfo(&volumes_music, ti);
5595     }
5596
5597     // sort volume values to start with lowest volume value
5598     sortTreeInfo(&volumes_music);
5599
5600     // set current volume value to configured volume value
5601     volume_music_current =
5602       getTreeInfoFromIdentifier(volumes_music,i_to_a(setup.volume_music));
5603
5604     // if that fails, set current volume to reliable default value
5605     if (volume_music_current == NULL)
5606       volume_music_current =
5607         getTreeInfoFromIdentifier(volumes_music, i_to_a(100));
5608
5609     // if that also fails, set current volume to first available value
5610     if (volume_music_current == NULL)
5611       volume_music_current = volumes_music;
5612   }
5613
5614   setup.volume_simple = atoi(volume_simple_current->identifier);
5615   setup.volume_loops  = atoi(volume_loops_current->identifier);
5616   setup.volume_music  = atoi(volume_music_current->identifier);
5617
5618   // needed for displaying volume text instead of identifier
5619   volume_simple_text = volume_simple_current->name;
5620   volume_loops_text = volume_loops_current->name;
5621   volume_music_text = volume_music_current->name;
5622
5623   setup_mode = SETUP_MODE_SOUND;
5624
5625   DrawSetupScreen();
5626 }
5627
5628 static void execSetupChooseTouchControls(void)
5629 {
5630   setup_mode = SETUP_MODE_CHOOSE_TOUCH_CONTROL;
5631
5632   DrawSetupScreen();
5633 }
5634
5635 static void execSetupChooseMoveDistance(void)
5636 {
5637   setup_mode = SETUP_MODE_CHOOSE_MOVE_DISTANCE;
5638
5639   DrawSetupScreen();
5640 }
5641
5642 static void execSetupChooseDropDistance(void)
5643 {
5644   setup_mode = SETUP_MODE_CHOOSE_DROP_DISTANCE;
5645
5646   DrawSetupScreen();
5647 }
5648
5649 static void execSetupChooseTransparency(void)
5650 {
5651   setup_mode = SETUP_MODE_CHOOSE_TRANSPARENCY;
5652
5653   DrawSetupScreen();
5654 }
5655
5656 static void execSetupChooseGridXSize_0(void)
5657 {
5658   setup_mode = SETUP_MODE_CHOOSE_GRID_XSIZE_0;
5659
5660   DrawSetupScreen();
5661 }
5662
5663 static void execSetupChooseGridYSize_0(void)
5664 {
5665   setup_mode = SETUP_MODE_CHOOSE_GRID_YSIZE_0;
5666
5667   DrawSetupScreen();
5668 }
5669
5670 static void execSetupChooseGridXSize_1(void)
5671 {
5672   setup_mode = SETUP_MODE_CHOOSE_GRID_XSIZE_1;
5673
5674   DrawSetupScreen();
5675 }
5676
5677 static void execSetupChooseGridYSize_1(void)
5678 {
5679   setup_mode = SETUP_MODE_CHOOSE_GRID_YSIZE_1;
5680
5681   DrawSetupScreen();
5682 }
5683
5684 static void execSetupConfigureVirtualButtons(void)
5685 {
5686   setup_mode = SETUP_MODE_CONFIG_VIRT_BUTTONS;
5687
5688   ConfigureVirtualButtons();
5689
5690   setup_mode = SETUP_MODE_TOUCH;
5691
5692   DrawSetupScreen();
5693 }
5694
5695 static void execSetupTouch(void)
5696 {
5697   int i, j, k;
5698
5699   if (touch_controls == NULL)
5700   {
5701     for (i = 0; touch_controls_list[i].value != NULL; i++)
5702     {
5703       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5704       char identifier[32], name[32];
5705       char *value = touch_controls_list[i].value;
5706       char *text = touch_controls_list[i].text;
5707
5708       ti->node_top = &touch_controls;
5709       ti->sort_priority = i;
5710
5711       sprintf(identifier, "%s", value);
5712       sprintf(name, "%s", text);
5713
5714       setString(&ti->identifier, identifier);
5715       setString(&ti->name, name);
5716       setString(&ti->name_sorting, name);
5717       setString(&ti->infotext, STR_SETUP_CHOOSE_TOUCH_CONTROL);
5718
5719       pushTreeInfo(&touch_controls, ti);
5720     }
5721
5722     // sort touch control values to start with lowest touch control value
5723     sortTreeInfo(&touch_controls);
5724
5725     // set current touch control value to configured touch control value
5726     touch_control_current =
5727       getTreeInfoFromIdentifier(touch_controls, setup.touch.control_type);
5728
5729     // if that fails, set current touch control to reliable default value
5730     if (touch_control_current == NULL)
5731       touch_control_current =
5732         getTreeInfoFromIdentifier(touch_controls, TOUCH_CONTROL_DEFAULT);
5733
5734     // if that also fails, set current touch control to first available value
5735     if (touch_control_current == NULL)
5736       touch_control_current = touch_controls;
5737   }
5738
5739   if (move_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 = &move_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_MOVE_DISTANCE);
5758
5759       pushTreeInfo(&move_distances, ti);
5760     }
5761
5762     // sort distance values to start with lowest distance value
5763     sortTreeInfo(&move_distances);
5764
5765     // set current distance value to configured distance value
5766     move_distance_current =
5767       getTreeInfoFromIdentifier(move_distances,
5768                                 i_to_a(setup.touch.move_distance));
5769
5770     // if that fails, set current distance to reliable default value
5771     if (move_distance_current == NULL)
5772       move_distance_current =
5773         getTreeInfoFromIdentifier(move_distances,
5774                                   i_to_a(TOUCH_MOVE_DISTANCE_DEFAULT));
5775
5776     // if that also fails, set current distance to first available value
5777     if (move_distance_current == NULL)
5778       move_distance_current = move_distances;
5779   }
5780
5781   if (drop_distances == NULL)
5782   {
5783     for (i = 0; distances_list[i].value != -1; i++)
5784     {
5785       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5786       char identifier[32], name[32];
5787       int value = distances_list[i].value;
5788       char *text = distances_list[i].text;
5789
5790       ti->node_top = &drop_distances;
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_DROP_DISTANCE);
5800
5801       pushTreeInfo(&drop_distances, ti);
5802     }
5803
5804     // sort distance values to start with lowest distance value
5805     sortTreeInfo(&drop_distances);
5806
5807     // set current distance value to configured distance value
5808     drop_distance_current =
5809       getTreeInfoFromIdentifier(drop_distances,
5810                                 i_to_a(setup.touch.drop_distance));
5811
5812     // if that fails, set current distance to reliable default value
5813     if (drop_distance_current == NULL)
5814       drop_distance_current =
5815         getTreeInfoFromIdentifier(drop_distances,
5816                                   i_to_a(TOUCH_DROP_DISTANCE_DEFAULT));
5817
5818     // if that also fails, set current distance to first available value
5819     if (drop_distance_current == NULL)
5820       drop_distance_current = drop_distances;
5821   }
5822
5823   if (transparencies == NULL)
5824   {
5825     for (i = 0; transparencies_list[i].value != -1; i++)
5826     {
5827       TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5828       char identifier[32], name[32];
5829       int value = transparencies_list[i].value;
5830       char *text = transparencies_list[i].text;
5831
5832       ti->node_top = &transparencies;
5833       ti->sort_priority = value;
5834
5835       sprintf(identifier, "%d", value);
5836       sprintf(name, "%s", text);
5837
5838       setString(&ti->identifier, identifier);
5839       setString(&ti->name, name);
5840       setString(&ti->name_sorting, name);
5841       setString(&ti->infotext, STR_SETUP_CHOOSE_TRANSPARENCY);
5842
5843       pushTreeInfo(&transparencies, ti);
5844     }
5845
5846     // sort transparency values to start with lowest transparency value
5847     sortTreeInfo(&transparencies);
5848
5849     // set current transparency value to configured transparency value
5850     transparency_current =
5851       getTreeInfoFromIdentifier(transparencies,
5852                                 i_to_a(setup.touch.transparency));
5853
5854     // if that fails, set current transparency to reliable default value
5855     if (transparency_current == NULL)
5856       transparency_current =
5857         getTreeInfoFromIdentifier(transparencies,
5858                                   i_to_a(TOUCH_TRANSPARENCY_DEFAULT));
5859
5860     // if that also fails, set current transparency to first available value
5861     if (transparency_current == NULL)
5862       transparency_current = transparencies;
5863   }
5864
5865   for (i = 0; i < 2; i++)
5866   {
5867     for (j = 0; j < 2; j++)
5868     {
5869       if (grid_sizes[i][j] == NULL)
5870       {
5871         for (k = 0; grid_sizes_list[k].value != -1; k++)
5872         {
5873           TreeInfo *ti = newTreeInfo_setDefaults(TREE_TYPE_UNDEFINED);
5874           char identifier[32], name[32];
5875           int value = grid_sizes_list[k].value;
5876           char *text = grid_sizes_list[k].text;
5877
5878           ti->node_top = &grid_sizes[i][j];
5879           ti->sort_priority = value;
5880
5881           sprintf(identifier, "%d", value);
5882           sprintf(name, "%s", text);
5883
5884           setString(&ti->identifier, identifier);
5885           setString(&ti->name, name);
5886           setString(&ti->name_sorting, name);
5887           setString(&ti->infotext,
5888                     (i == 0 ?
5889                      (j == 0 ?
5890                       STR_SETUP_CHOOSE_GRID_XSIZE_0 :
5891                       STR_SETUP_CHOOSE_GRID_YSIZE_0) :
5892                      (j == 0 ?
5893                       STR_SETUP_CHOOSE_GRID_XSIZE_1 :
5894                       STR_SETUP_CHOOSE_GRID_YSIZE_1)));
5895
5896           pushTreeInfo(&grid_sizes[i][j], ti);
5897         }
5898
5899         // sort grid size values to start with lowest grid size value
5900         sortTreeInfo(&grid_sizes[i][j]);
5901
5902         // set current grid size value to configured grid size value
5903         grid_size_current[i][j] =
5904           getTreeInfoFromIdentifier(grid_sizes[i][j],
5905                                     i_to_a(j == 0 ?
5906                                            setup.touch.grid_xsize[i] :
5907                                            setup.touch.grid_ysize[i]));
5908
5909         // if that fails, set current grid size to reliable default value
5910         if (grid_size_current[i][j] == NULL)
5911           grid_size_current[i][j] =
5912             getTreeInfoFromIdentifier(grid_sizes[i][j],
5913                                       i_to_a(j == 0 ?
5914                                              DEFAULT_GRID_XSIZE(i) :
5915                                              DEFAULT_GRID_YSIZE(i)));
5916
5917         // if that also fails, set current grid size to first available value
5918         if (grid_size_current[i][j] == NULL)
5919           grid_size_current[i][j] = grid_sizes[i][j];
5920       }
5921     }
5922   }
5923
5924   setup.touch.control_type = touch_control_current->identifier;
5925   setup.touch.move_distance = atoi(move_distance_current->identifier);
5926   setup.touch.drop_distance = atoi(drop_distance_current->identifier);
5927   setup.touch.transparency = atoi(transparency_current->identifier);
5928
5929   for (i = 0; i < 2; i++)
5930   {
5931     setup.touch.grid_xsize[i] = atoi(grid_size_current[i][0]->identifier);
5932     setup.touch.grid_ysize[i] = atoi(grid_size_current[i][1]->identifier);
5933
5934     if (i == GRID_ACTIVE_NR())
5935     {
5936       overlay.grid_xsize = setup.touch.grid_xsize[i];
5937       overlay.grid_ysize = setup.touch.grid_ysize[i];
5938     }
5939   }
5940
5941   // needed for displaying value text instead of identifier
5942   touch_controls_text = touch_control_current->name;
5943   move_distance_text = move_distance_current->name;
5944   drop_distance_text = drop_distance_current->name;
5945   transparency_text = transparency_current->name;
5946
5947   for (i = 0; i < 2; i++)
5948     for (j = 0; j < 2; j++)
5949       grid_size_text[i][j] = grid_size_current[i][j]->name;
5950
5951   setup_mode = SETUP_MODE_TOUCH;
5952
5953   DrawSetupScreen();
5954 }
5955
5956 static void execSetupArtwork(void)
5957 {
5958 #if 0
5959   Debug("screens:execSetupArtwork", "'%s', '%s', '%s'",
5960         artwork.gfx_current->subdir,
5961         artwork.gfx_current->fullpath,
5962         artwork.gfx_current->basepath);
5963 #endif
5964
5965   setup.graphics_set = artwork.gfx_current->identifier;
5966   setup.sounds_set = artwork.snd_current->identifier;
5967   setup.music_set = artwork.mus_current->identifier;
5968
5969   // needed if last screen (setup choice) changed graphics, sounds or music
5970   ReloadCustomArtwork(0);
5971
5972   // needed for displaying artwork name instead of artwork identifier
5973   graphics_set_name = artwork.gfx_current->name;
5974   sounds_set_name = artwork.snd_current->name;
5975   music_set_name = artwork.mus_current->name;
5976
5977   setup_mode = SETUP_MODE_ARTWORK;
5978
5979   DrawSetupScreen();
5980 }
5981
5982 static void execSetupChooseGraphics(void)
5983 {
5984   setup_mode = SETUP_MODE_CHOOSE_GRAPHICS;
5985
5986   DrawSetupScreen();
5987 }
5988
5989 static void execSetupChooseSounds(void)
5990 {
5991   setup_mode = SETUP_MODE_CHOOSE_SOUNDS;
5992
5993   DrawSetupScreen();
5994 }
5995
5996 static void execSetupChooseMusic(void)
5997 {
5998   setup_mode = SETUP_MODE_CHOOSE_MUSIC;
5999
6000   DrawSetupScreen();
6001 }
6002
6003 static void execSetupInput(void)
6004 {
6005   setup_mode = SETUP_MODE_INPUT;
6006
6007   DrawSetupScreen();
6008 }
6009
6010 static void execSetupShortcuts(void)
6011 {
6012   setup_mode = SETUP_MODE_SHORTCUTS;
6013
6014   DrawSetupScreen();
6015 }
6016
6017 static void execSetupShortcuts1(void)
6018 {
6019   setup_mode = SETUP_MODE_SHORTCUTS_1;
6020
6021   DrawSetupScreen();
6022 }
6023
6024 static void execSetupShortcuts2(void)
6025 {
6026   setup_mode = SETUP_MODE_SHORTCUTS_2;
6027
6028   DrawSetupScreen();
6029 }
6030
6031 static void execSetupShortcuts3(void)
6032 {
6033   setup_mode = SETUP_MODE_SHORTCUTS_3;
6034
6035   DrawSetupScreen();
6036 }
6037
6038 static void execSetupShortcuts4(void)
6039 {
6040   setup_mode = SETUP_MODE_SHORTCUTS_4;
6041
6042   DrawSetupScreen();
6043 }
6044
6045 static void execSetupShortcuts5(void)
6046 {
6047   setup_mode = SETUP_MODE_SHORTCUTS_5;
6048
6049   DrawSetupScreen();
6050 }
6051
6052 static void execExitSetup(void)
6053 {
6054   SetGameStatus(GAME_MODE_MAIN);
6055
6056   DrawMainMenu();
6057 }
6058
6059 static void execSaveAndExitSetup(void)
6060 {
6061   SaveSetup();
6062   execExitSetup();
6063 }
6064
6065 static void execGadgetNetworkServer(void)
6066 {
6067   int gadget_id = SCREEN_CTRL_ID_NETWORK_SERVER;
6068   struct GadgetInfo *gi = screen_gadget[gadget_id];
6069
6070   if (strEqual(setup.network_server_hostname, STR_NETWORK_AUTO_DETECT))
6071     network_server_hostname[0] = '\0';
6072
6073   ModifyGadget(gi, GDI_TEXT_VALUE, network_server_hostname, GDI_END);
6074
6075   MapGadget(gi);
6076
6077   ClickOnGadget(gi, MB_LEFTBUTTON);
6078 }
6079
6080 static void ToggleNetworkModeIfNeeded(void)
6081 {
6082   int font_title = FONT_TITLE_1;
6083   int font_foot = FC_BLUE;
6084   int ystart  = mSY - SY + 16;
6085   int ybottom = mSY - SY + SYSIZE - 20;
6086   char *text = (setup.network_mode ? "Start Network" : "Stop Network");
6087
6088   if (setup.network_mode == network.enabled)
6089     return;
6090
6091   network.enabled = setup.network_mode;
6092
6093   FadeOut(REDRAW_ALL);
6094
6095   ClearField();
6096
6097   DrawTextSCentered(ystart, font_title, text);
6098
6099   FadeIn(REDRAW_ALL);
6100
6101   if (network.enabled)
6102     InitNetworkServer();
6103   else
6104     DisconnectFromNetworkServer();
6105
6106   DrawTextSCentered(ybottom, font_foot,
6107                     "Press any key or button for setup menu");
6108
6109   WaitForEventToContinue();
6110
6111   DrawSetupScreen();
6112 }
6113
6114 static void ToggleGameSpeedsListIfNeeded(void)
6115 {
6116   boolean using_game_speeds_extended = (game_speeds == game_speeds_extended);
6117
6118   if (setup.game_speed_extended == using_game_speeds_extended)
6119     return;
6120
6121   // try to match similar values when changing game speeds list
6122   if (setup.game_speed_extended)
6123     setup.game_frame_delay = (setup.game_frame_delay == 15 ? 16 :
6124                               setup.game_frame_delay == 30 ? 29 :
6125                               setup.game_frame_delay);
6126   else
6127     setup.game_frame_delay = (setup.game_frame_delay == 14 ? 15 :
6128                               setup.game_frame_delay == 16 ? 15 :
6129                               setup.game_frame_delay >= 29 ? 30 :
6130                               setup.game_frame_delay <= 10 ? 10 :
6131                               setup.game_frame_delay);
6132
6133   execSetupGame_setGameSpeeds(TRUE);
6134
6135   DrawSetupScreen();
6136 }
6137
6138 static void ModifyGameSpeedIfNeeded(void)
6139 {
6140   if (strEqual(setup.vsync_mode, STR_VSYNC_MODE_OFF) ||
6141       setup.game_frame_delay <= MAX_VSYNC_FRAME_DELAY)
6142     return;
6143
6144   char message[100];
6145   char *game_speed_text = "Fast";
6146   int game_speed_value = 15;
6147
6148   if (setup.game_speed_extended)
6149   {
6150     game_speed_text = "60 fps";
6151     game_speed_value = 16;
6152   }
6153
6154   sprintf(message, "Game speed set to %s for VSync to work!", game_speed_text);
6155
6156   // set game speed to existing list value that is fast enough for vsync
6157   setup.game_frame_delay = game_speed_value;
6158
6159   execSetupGame_setGameSpeeds(TRUE);
6160
6161   Request(message, REQ_CONFIRM);
6162 }
6163
6164 static void DisableVsyncIfNeeded(void)
6165 {
6166   if (strEqual(setup.vsync_mode, STR_VSYNC_MODE_OFF) ||
6167       (setup.game_frame_delay >= MIN_VSYNC_FRAME_DELAY &&
6168        setup.game_frame_delay <= MAX_VSYNC_FRAME_DELAY))
6169     return;
6170
6171   // disable vsync for the selected game speed to work
6172   setup.vsync_mode = STR_VSYNC_MODE_OFF;
6173
6174   execSetupGraphics_setVsyncModes(TRUE);
6175
6176   Request("VSync disabled for this game speed to work!", REQ_CONFIRM);
6177 }
6178
6179 static struct
6180 {
6181   void *value;
6182   void *related_value;
6183 } hide_related_entry_list[] =
6184 {
6185   { &setup.game_frame_delay,            execSetupChooseGameSpeed        },
6186   { &setup.game_frame_delay,            &game_speed_text                },
6187
6188   { &setup.scroll_delay_value,          execSetupChooseScrollDelay      },
6189   { &setup.scroll_delay_value,          &scroll_delay_text              },
6190
6191   { &setup.engine_snapshot_mode,        execSetupChooseSnapshotMode     },
6192   { &setup.engine_snapshot_mode,        &snapshot_mode_text             },
6193
6194   { &setup.window_scaling_percent,      execSetupChooseWindowSize       },
6195   { &setup.window_scaling_percent,      &window_size_text               },
6196
6197   { &setup.window_scaling_quality,      execSetupChooseScalingType      },
6198   { &setup.window_scaling_quality,      &scaling_type_text              },
6199
6200   { &setup.screen_rendering_mode,       execSetupChooseRenderingMode    },
6201   { &setup.screen_rendering_mode,       &rendering_mode_text            },
6202
6203   { &setup.vsync_mode,                  execSetupChooseVsyncMode        },
6204   { &setup.vsync_mode,                  &vsync_mode_text                },
6205
6206   { &setup.graphics_set,                execSetupChooseGraphics         },
6207   { &setup.graphics_set,                &graphics_set_name              },
6208
6209   { &setup.sounds_set,                  execSetupChooseSounds           },
6210   { &setup.sounds_set,                  &sounds_set_name                },
6211
6212   { &setup.music_set,                   execSetupChooseMusic            },
6213   { &setup.music_set,                   &music_set_name                 },
6214
6215   { &setup.volume_simple,               execSetupChooseVolumeSimple     },
6216   { &setup.volume_simple,               &volume_simple_text             },
6217
6218   { &setup.volume_loops,                execSetupChooseVolumeLoops      },
6219   { &setup.volume_loops,                &volume_loops_text              },
6220
6221   { &setup.volume_music,                execSetupChooseVolumeMusic      },
6222   { &setup.volume_music,                &volume_music_text              },
6223
6224   { &setup.touch.control_type,          execSetupChooseTouchControls    },
6225   { &setup.touch.control_type,          &touch_controls_text            },
6226
6227   { &setup.touch.move_distance,         execSetupChooseMoveDistance     },
6228   { &setup.touch.move_distance,         &move_distance_text             },
6229
6230   { &setup.touch.drop_distance,         execSetupChooseDropDistance     },
6231   { &setup.touch.drop_distance,         &drop_distance_text             },
6232
6233   { &setup.touch.transparency,          execSetupChooseTransparency     },
6234   { &setup.touch.transparency,          &transparency_text              },
6235
6236   { &setup.touch.grid_xsize[0],         execSetupChooseGridXSize_0      },
6237   { &setup.touch.grid_xsize[0],         &grid_size_text[0][0]           },
6238
6239   { &setup.touch.grid_ysize[0],         execSetupChooseGridYSize_0      },
6240   { &setup.touch.grid_ysize[0],         &grid_size_text[0][1]           },
6241
6242   { &setup.touch.grid_xsize[1],         execSetupChooseGridXSize_1      },
6243   { &setup.touch.grid_xsize[1],         &grid_size_text[1][0]           },
6244
6245   { &setup.touch.grid_ysize[1],         execSetupChooseGridYSize_1      },
6246   { &setup.touch.grid_ysize[1],         &grid_size_text[1][1]           },
6247
6248   { &setup.internal.menu_game,          execSetupGame                   },
6249   { &setup.internal.menu_engines,       execSetupEngines                },
6250   { &setup.internal.menu_editor,        execSetupEditor                 },
6251   { &setup.internal.menu_graphics,      execSetupGraphics               },
6252   { &setup.internal.menu_sound,         execSetupSound                  },
6253   { &setup.internal.menu_artwork,       execSetupArtwork                },
6254   { &setup.internal.menu_input,         execSetupInput                  },
6255   { &setup.internal.menu_touch,         execSetupTouch                  },
6256   { &setup.internal.menu_shortcuts,     execSetupShortcuts              },
6257   { &setup.internal.menu_exit,          execExitSetup                   },
6258   { &setup.internal.menu_save_and_exit, execSaveAndExitSetup            },
6259
6260   { NULL,                               NULL                            }
6261 };
6262
6263 void setHideRelatedSetupEntries(void)
6264 {
6265   int i;
6266
6267   for (i = 0; hide_related_entry_list[i].value != NULL; i++)
6268     if (hideSetupEntry(hide_related_entry_list[i].value))
6269       setHideSetupEntry(hide_related_entry_list[i].related_value);
6270 }
6271
6272 static struct TokenInfo setup_info_main[] =
6273 {
6274   { TYPE_ENTER_MENU,    execSetupGame,          STR_SETUP_GAME          },
6275   { TYPE_ENTER_MENU,    execSetupEngines,       STR_SETUP_ENGINES       },
6276   { TYPE_ENTER_MENU,    execSetupEditor,        STR_SETUP_EDITOR        },
6277   { TYPE_ENTER_MENU,    execSetupGraphics,      STR_SETUP_GRAPHICS      },
6278   { TYPE_ENTER_MENU,    execSetupSound,         STR_SETUP_SOUND         },
6279   { TYPE_ENTER_MENU,    execSetupArtwork,       STR_SETUP_ARTWORK       },
6280   { TYPE_ENTER_MENU,    execSetupInput,         STR_SETUP_INPUT         },
6281   { TYPE_ENTER_MENU,    execSetupTouch,         STR_SETUP_TOUCH         },
6282   { TYPE_ENTER_MENU,    execSetupShortcuts,     STR_SETUP_SHORTCUTS     },
6283   { TYPE_EMPTY,         NULL,                   ""                      },
6284   { TYPE_LEAVE_MENU,    execExitSetup,          STR_SETUP_EXIT          },
6285   { TYPE_LEAVE_MENU,    execSaveAndExitSetup,   STR_SETUP_SAVE_AND_EXIT },
6286
6287   { 0,                  NULL,                   NULL                    }
6288 };
6289
6290 static struct TokenInfo setup_info_game[] =
6291 {
6292   { TYPE_SWITCH,        &setup.team_mode,       "Team-Mode (Multi-Player):" },
6293   { TYPE_SWITCH,        &setup.network_mode,    "Network Multi-Player Mode:" },
6294   { TYPE_PLAYER,        &setup.network_player_nr,"Preferred Network Player:" },
6295   { TYPE_TEXT_INPUT,    execGadgetNetworkServer, "Network Server Hostname:" },
6296   { TYPE_STRING,        &network_server_text,   ""                      },
6297   { TYPE_YES_NO,        &setup.input_on_focus,  "Only Move Focussed Player:" },
6298   { TYPE_SWITCH,        &setup.time_limit,      "Time Limit:"           },
6299   { TYPE_SWITCH,        &setup.handicap,        "Handicap:"             },
6300   { TYPE_SWITCH,        &setup.skip_levels,     "Skip Unsolved Levels:" },
6301   { TYPE_SWITCH,        &setup.increment_levels,"Increment Solved Levels:" },
6302   { TYPE_SWITCH,        &setup.auto_play_next_level,"Auto-play Next Level:" },
6303   { TYPE_SWITCH,        &setup.skip_scores_after_game,"Skip Scores After Game:" },
6304   { TYPE_YES_NO,        &setup.ask_on_game_over, "Ask on Game Over:"    },
6305   { TYPE_SWITCH,        &setup.autorecord,      "Auto-Record Tapes:"    },
6306   { TYPE_ENTER_LIST,    execSetupChooseGameSpeed, "Game Speed:"         },
6307   { TYPE_STRING,        &game_speed_text,       ""                      },
6308   { TYPE_SWITCH,        &setup.game_speed_extended, "Game Speed Extended List:" },
6309 #if 1
6310   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay:"     },
6311   { TYPE_STRING,        &scroll_delay_text,     ""                      },
6312 #endif
6313   { TYPE_ENTER_LIST, execSetupChooseSnapshotMode,"Game Engine Snapshot Mode:" },
6314   { TYPE_STRING,        &snapshot_mode_text,    ""                      },
6315   { TYPE_SWITCH,        &setup.show_snapshot_buttons,"Show Snapshot Buttons:" },
6316   { TYPE_EMPTY,         NULL,                   ""                      },
6317   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6318
6319   { 0,                  NULL,                   NULL                    }
6320 };
6321
6322 static struct TokenInfo setup_info_engines[] =
6323 {
6324   { TYPE_HEADLINE,      NULL,                   "Emerald Mine"          },
6325   { TYPE_SWITCH,        &setup.forced_scroll_delay, "Scroll Delay:"     },
6326   { TYPE_ECS_AGA,       &setup.prefer_aga_graphics, "Amiga Graphics Chipset:" },
6327   { TYPE_SWITCH,        &setup.prefer_lowpass_sounds,"Low-Pass Filter Sounds:" },
6328   { TYPE_EMPTY,         NULL,                   ""                      },
6329   { TYPE_HEADLINE,      NULL,                   "Supaplex"              },
6330   { TYPE_SWITCH,        &setup.sp_show_border_elements, "Border Elements:" },
6331   { TYPE_EMPTY,         NULL,                   ""                      },
6332   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6333
6334   { 0,                  NULL,                   NULL                    }
6335 };
6336
6337 static struct TokenInfo setup_info_editor[] =
6338 {
6339 #if 0
6340   { TYPE_SWITCH,        &setup.editor.el_boulderdash,   "Boulder Dash:" },
6341   { TYPE_SWITCH,        &setup.editor.el_emerald_mine,  "Emerald Mine:" },
6342   { TYPE_SWITCH, &setup.editor.el_emerald_mine_club,    "Emerald Mine Club:" },
6343   { TYPE_SWITCH,        &setup.editor.el_more,          "Rocks'n'Diamonds:" },
6344   { TYPE_SWITCH,        &setup.editor.el_sokoban,       "Sokoban:"      },
6345   { TYPE_SWITCH,        &setup.editor.el_supaplex,      "Supaplex:"     },
6346   { TYPE_SWITCH,        &setup.editor.el_diamond_caves, "Diamond Caves II:" },
6347   { TYPE_SWITCH,        &setup.editor.el_dx_boulderdash,"DX-Boulderdash:" },
6348   { TYPE_SWITCH,        &setup.editor.el_chars,         "Text Characters:" },
6349   { TYPE_SWITCH, &setup.editor.el_steel_chars, "Text Characters (Steel):" },
6350 #endif
6351   { TYPE_SWITCH,        &setup.editor.el_classic,  "Classic Elements:" },
6352   { TYPE_SWITCH,        &setup.editor.el_custom,  "Custom & Group Elements:" },
6353 #if 0
6354   { TYPE_SWITCH,        &setup.editor.el_headlines,     "Headlines:"    },
6355 #endif
6356   { TYPE_SWITCH, &setup.editor.el_user_defined, "User defined element list:" },
6357   { TYPE_SWITCH,        &setup.editor.el_dynamic,  "Dynamic level elements:" },
6358   { TYPE_EMPTY,         NULL,                   ""                      },
6359 #if 0
6360   { TYPE_SWITCH,        &setup.editor.el_by_game,   "Show elements by game:" },
6361   { TYPE_SWITCH,        &setup.editor.el_by_type,   "Show elements by type:" },
6362   { TYPE_EMPTY,         NULL,                   ""                      },
6363 #endif
6364   { TYPE_SWITCH, &setup.editor.show_element_token,      "Show element token:" },
6365   { TYPE_EMPTY,         NULL,                   ""                      },
6366   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6367
6368   { 0,                  NULL,                   NULL                    }
6369 };
6370
6371 static struct TokenInfo setup_info_graphics[] =
6372 {
6373 #if !defined(PLATFORM_ANDROID)
6374   { TYPE_SWITCH,        &setup.fullscreen,      "Fullscreen:"           },
6375   { TYPE_ENTER_LIST,    execSetupChooseWindowSize, "Window Scaling:"    },
6376   { TYPE_STRING,        &window_size_text,      ""                      },
6377   { TYPE_ENTER_LIST,    execSetupChooseScalingType, "Anti-Aliasing:"    },
6378   { TYPE_STRING,        &scaling_type_text,     ""                      },
6379   { TYPE_ENTER_LIST,    execSetupChooseRenderingMode, "Special Rendering:" },
6380   { TYPE_STRING,        &rendering_mode_text,   ""                      },
6381 #endif
6382 #if 0
6383   { TYPE_ENTER_LIST,    execSetupChooseScrollDelay, "Scroll Delay:"     },
6384   { TYPE_STRING,        &scroll_delay_text,     ""                      },
6385 #endif
6386   { TYPE_ENTER_LIST,    execSetupChooseVsyncMode, "Vertical Sync (VSync):" },
6387   { TYPE_STRING,        &vsync_mode_text,       ""                      },
6388   { TYPE_SWITCH,        &setup.fade_screens,    "Fade Screens:"         },
6389   { TYPE_SWITCH,        &setup.quick_switch,    "Quick Player Focus Switch:" },
6390   { TYPE_SWITCH,        &setup.quick_doors,     "Quick Menu Doors:"     },
6391   { TYPE_SWITCH,        &setup.show_titlescreen,"Show Title Screens:"   },
6392   { TYPE_SWITCH,        &setup.toons,           "Show Menu Animations:" },
6393   { TYPE_SWITCH,        &setup.small_game_graphics, "Small Game Graphics:" },
6394   { TYPE_EMPTY,         NULL,                   ""                      },
6395   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6396
6397   { 0,                  NULL,                   NULL                    }
6398 };
6399
6400 static struct TokenInfo setup_info_sound[] =
6401 {
6402   { TYPE_SWITCH,        &setup.sound_simple,    "Sound Effects (Normal):"  },
6403   { TYPE_SWITCH,        &setup.sound_loops,     "Sound Effects (Looping):" },
6404   { TYPE_SWITCH,        &setup.sound_music,     "Music:"                },
6405   { TYPE_EMPTY,         NULL,                   ""                      },
6406   { TYPE_ENTER_LIST,    execSetupChooseVolumeSimple, "Sound Volume (Normal):" },
6407   { TYPE_STRING,        &volume_simple_text,    ""                      },
6408   { TYPE_ENTER_LIST,    execSetupChooseVolumeLoops, "Sound Volume (Looping):" },
6409   { TYPE_STRING,        &volume_loops_text,     ""                      },
6410   { TYPE_ENTER_LIST,    execSetupChooseVolumeMusic, "Music Volume:"     },
6411   { TYPE_STRING,        &volume_music_text,     ""                      },
6412   { TYPE_EMPTY,         NULL,                   ""                      },
6413   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6414
6415   { 0,                  NULL,                   NULL                    }
6416 };
6417
6418 static struct TokenInfo setup_info_artwork[] =
6419 {
6420   { TYPE_ENTER_LIST,    execSetupChooseGraphics,"Custom Graphics:"      },
6421   { TYPE_STRING,        &graphics_set_name,     ""                      },
6422   { TYPE_ENTER_LIST,    execSetupChooseSounds,  "Custom Sounds:"        },
6423   { TYPE_STRING,        &sounds_set_name,       ""                      },
6424   { TYPE_ENTER_LIST,    execSetupChooseMusic,   "Custom Music:"         },
6425   { TYPE_STRING,        &music_set_name,        ""                      },
6426   { TYPE_EMPTY,         NULL,                   ""                      },
6427   { TYPE_YES_NO_AUTO,&setup.override_level_graphics,"Override Level Graphics:"},
6428   { TYPE_YES_NO_AUTO,&setup.override_level_sounds,  "Override Level Sounds:"  },
6429   { TYPE_YES_NO_AUTO,&setup.override_level_music,   "Override Level Music:"   },
6430   { TYPE_EMPTY,         NULL,                   ""                      },
6431   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6432
6433   { 0,                  NULL,                   NULL                    }
6434 };
6435
6436 static struct TokenInfo setup_info_input[] =
6437 {
6438   { TYPE_SWITCH,        NULL,                   "Player:"               },
6439   { TYPE_SWITCH,        NULL,                   "Device:"               },
6440   { TYPE_SWITCH,        NULL,                   ""                      },
6441   { TYPE_SKIPPABLE,     NULL,                   ""                      },
6442   { TYPE_EMPTY,         NULL,                   ""                      },
6443   { TYPE_EMPTY,         NULL,                   ""                      },
6444   { TYPE_EMPTY,         NULL,                   ""                      },
6445   { TYPE_EMPTY,         NULL,                   ""                      },
6446   { TYPE_EMPTY,         NULL,                   ""                      },
6447   { TYPE_EMPTY,         NULL,                   ""                      },
6448   { TYPE_EMPTY,         NULL,                   ""                      },
6449   { TYPE_EMPTY,         NULL,                   ""                      },
6450   { TYPE_SKIPPABLE,     NULL,                   ""                      },
6451   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6452
6453   { 0,                  NULL,                   NULL                    }
6454 };
6455
6456 static struct TokenInfo setup_info_touch[] =
6457 {
6458   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
6459   { TYPE_STRING,        &touch_controls_text,   ""                      },
6460   { TYPE_EMPTY,         NULL,                   ""                      },
6461   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6462
6463   { 0,                  NULL,                   NULL                    }
6464 };
6465
6466 static struct TokenInfo setup_info_touch_virtual_buttons_0[] =
6467 {
6468   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
6469   { TYPE_STRING,        &touch_controls_text,   ""                      },
6470   { TYPE_EMPTY,         NULL,                   ""                      },
6471   { TYPE_ENTER_LIST,    execSetupChooseGridXSize_0, "Horizontal Buttons (Landscape):"   },
6472   { TYPE_STRING,        &grid_size_text[0][0],  ""                      },
6473   { TYPE_ENTER_LIST,    execSetupChooseGridYSize_0, "Vertical Buttons (Landscape):"     },
6474   { TYPE_STRING,        &grid_size_text[0][1],  ""                      },
6475   { TYPE_ENTER_LIST,    execSetupChooseTransparency, "Button Transparency:" },
6476   { TYPE_STRING,        &transparency_text,     ""                      },
6477   { TYPE_SWITCH,        &setup.touch.draw_outlined, "Draw Buttons Outlined:" },
6478   { TYPE_SWITCH,        &setup.touch.draw_pressed, "Highlight Pressed Buttons:" },
6479   { TYPE_EMPTY,         NULL,                   ""                      },
6480   { TYPE_ENTER_LIST,    execSetupConfigureVirtualButtons, "Configure Virtual Buttons" },
6481   { TYPE_EMPTY,         NULL,                   ""                      },
6482   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6483
6484   { 0,                  NULL,                   NULL                    }
6485 };
6486
6487 static struct TokenInfo setup_info_touch_virtual_buttons_1[] =
6488 {
6489   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
6490   { TYPE_STRING,        &touch_controls_text,   ""                      },
6491   { TYPE_EMPTY,         NULL,                   ""                      },
6492   { TYPE_ENTER_LIST,    execSetupChooseGridXSize_1, "Horizontal Buttons (Portrait):"    },
6493   { TYPE_STRING,        &grid_size_text[1][0],  ""                      },
6494   { TYPE_ENTER_LIST,    execSetupChooseGridYSize_1, "Vertical Buttons (Portrait):"      },
6495   { TYPE_STRING,        &grid_size_text[1][1],  ""                      },
6496   { TYPE_ENTER_LIST,    execSetupChooseTransparency, "Button Transparency:" },
6497   { TYPE_STRING,        &transparency_text,     ""                      },
6498   { TYPE_SWITCH,        &setup.touch.draw_outlined, "Draw Buttons Outlined:" },
6499   { TYPE_SWITCH,        &setup.touch.draw_pressed, "Highlight Pressed Buttons:" },
6500   { TYPE_EMPTY,         NULL,                   ""                      },
6501   { TYPE_ENTER_LIST,    execSetupConfigureVirtualButtons, "Configure Virtual Buttons" },
6502   { TYPE_EMPTY,         NULL,                   ""                      },
6503   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6504
6505   { 0,                  NULL,                   NULL                    }
6506 };
6507
6508 static struct TokenInfo *setup_info_touch_virtual_buttons[] =
6509 {
6510   setup_info_touch_virtual_buttons_0,
6511   setup_info_touch_virtual_buttons_1
6512 };
6513
6514 static struct TokenInfo setup_info_touch_wipe_gestures[] =
6515 {
6516   { TYPE_ENTER_LIST,    execSetupChooseTouchControls, "Touch Control Type:" },
6517   { TYPE_STRING,        &touch_controls_text,   ""                      },
6518   { TYPE_EMPTY,         NULL,                   ""                      },
6519   { TYPE_ENTER_LIST,    execSetupChooseMoveDistance, "Move Trigger Distance:" },
6520   { TYPE_STRING,        &move_distance_text,    ""                      },
6521   { TYPE_ENTER_LIST,    execSetupChooseDropDistance, "Drop Trigger Distance:" },
6522   { TYPE_STRING,        &drop_distance_text,    ""                      },
6523   { TYPE_EMPTY,         NULL,                   ""                      },
6524   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6525
6526   { 0,                  NULL,                   NULL                    }
6527 };
6528
6529 static struct TokenInfo setup_info_shortcuts[] =
6530 {
6531   { TYPE_ENTER_MENU,    execSetupShortcuts1,    "Various Keys"          },
6532   { TYPE_ENTER_MENU,    execSetupShortcuts2,    "Player Focus"          },
6533   { TYPE_ENTER_MENU,    execSetupShortcuts3,    "Tape Buttons"          },
6534   { TYPE_ENTER_MENU,    execSetupShortcuts4,    "Sound & Music"         },
6535   { TYPE_ENTER_MENU,    execSetupShortcuts5,    "TAS Snap Keys"         },
6536   { TYPE_EMPTY,         NULL,                   ""                      },
6537   { TYPE_LEAVE_MENU,    execSetupMain,          "Back"                  },
6538
6539   { 0,                  NULL,                   NULL                    }
6540 };
6541
6542 static struct TokenInfo setup_info_shortcuts_1[] =
6543 {
6544   { TYPE_KEYTEXT,       NULL,           "Quick Save Game to Tape:",     },
6545   { TYPE_KEY,           &setup.shortcut.save_game, ""                   },
6546   { TYPE_KEYTEXT,       NULL,           "Quick Load Game from Tape:",   },
6547   { TYPE_KEY,           &setup.shortcut.load_game, ""                   },
6548   { TYPE_KEYTEXT,       NULL,           "Start Game & Toggle Pause:",   },
6549   { TYPE_KEY,           &setup.shortcut.toggle_pause, ""                },
6550   { TYPE_EMPTY,         NULL,                   ""                      },
6551   { TYPE_YES_NO,        &setup.ask_on_escape,   "Ask on 'Esc' Key:"     },
6552   { TYPE_YES_NO, &setup.ask_on_escape_editor,   "Ask on 'Esc' Key (Editor):" },
6553   { TYPE_EMPTY,         NULL,                   ""                      },
6554   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
6555
6556   { 0,                  NULL,                   NULL                    }
6557 };
6558
6559 static struct TokenInfo setup_info_shortcuts_2[] =
6560 {
6561   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 1:",       },
6562   { TYPE_KEY,           &setup.shortcut.focus_player[0], ""             },
6563   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 2:",       },
6564   { TYPE_KEY,           &setup.shortcut.focus_player[1], ""             },
6565   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 3:",       },
6566   { TYPE_KEY,           &setup.shortcut.focus_player[2], ""             },
6567   { TYPE_KEYTEXT,       NULL,           "Set Focus to Player 4:",       },
6568   { TYPE_KEY,           &setup.shortcut.focus_player[3], ""             },
6569   { TYPE_KEYTEXT,       NULL,           "Set Focus to All Players:",    },
6570   { TYPE_KEY,           &setup.shortcut.focus_player_all, ""            },
6571   { TYPE_EMPTY,         NULL,                   ""                      },
6572   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
6573
6574   { 0,                  NULL,                   NULL                    }
6575 };
6576
6577 static struct TokenInfo setup_info_shortcuts_3[] =
6578 {
6579   { TYPE_KEYTEXT,       NULL,                   "Eject Tape:",          },
6580   { TYPE_KEY,           &setup.shortcut.tape_eject, ""                  },
6581   { TYPE_KEYTEXT,       NULL,                   "Warp / Single Step:",  },
6582   { TYPE_KEY,           &setup.shortcut.tape_extra, ""                  },
6583   { TYPE_KEYTEXT,       NULL,                   "Stop Tape:",           },
6584   { TYPE_KEY,           &setup.shortcut.tape_stop, ""                   },
6585   { TYPE_KEYTEXT,       NULL,                   "Pause / Unpause Tape:",},
6586   { TYPE_KEY,           &setup.shortcut.tape_pause, ""                  },
6587   { TYPE_KEYTEXT,       NULL,                   "Record Tape:",         },
6588   { TYPE_KEY,           &setup.shortcut.tape_record, ""                 },
6589   { TYPE_KEYTEXT,       NULL,                   "Play Tape:",           },
6590   { TYPE_KEY,           &setup.shortcut.tape_play, ""                   },
6591   { TYPE_EMPTY,         NULL,                   ""                      },
6592   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
6593
6594   { 0,                  NULL,                   NULL                    }
6595 };
6596
6597 static struct TokenInfo setup_info_shortcuts_4[] =
6598 {
6599   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Normal):", },
6600   { TYPE_KEY,           &setup.shortcut.sound_simple, ""                },
6601   { TYPE_KEYTEXT,       NULL,           "Toggle Sound Effects (Looping):", },
6602   { TYPE_KEY,           &setup.shortcut.sound_loops, ""                 },
6603   { TYPE_KEYTEXT,       NULL,           "Toggle Music:",                },
6604   { TYPE_KEY,           &setup.shortcut.sound_music, ""                 },
6605   { TYPE_EMPTY,         NULL,                   ""                      },
6606   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
6607
6608   { 0,                  NULL,                   NULL                    }
6609 };
6610
6611 static struct TokenInfo setup_info_shortcuts_5[] =
6612 {
6613   { TYPE_KEYTEXT,       NULL,                   "Snap Left:",           },
6614   { TYPE_KEY,           &setup.shortcut.snap_left, ""                   },
6615   { TYPE_KEYTEXT,       NULL,                   "Snap Right:",          },
6616   { TYPE_KEY,           &setup.shortcut.snap_right, ""                  },
6617   { TYPE_KEYTEXT,       NULL,                   "Snap Up:",             },
6618   { TYPE_KEY,           &setup.shortcut.snap_up, ""                     },
6619   { TYPE_KEYTEXT,       NULL,                   "Snap Down:",           },
6620   { TYPE_KEY,           &setup.shortcut.snap_down, ""                   },
6621   { TYPE_EMPTY,         NULL,                   ""                      },
6622   { TYPE_LEAVE_MENU,    execSetupShortcuts,     "Back"                  },
6623
6624   { 0,                  NULL,                   NULL                    }
6625 };
6626
6627 static Key getSetupKey(void)
6628 {
6629   Key key = KSYM_UNDEFINED;
6630   boolean got_key_event = FALSE;
6631
6632   while (!got_key_event)
6633   {
6634     Event event;
6635
6636     if (NextValidEvent(&event))
6637     {
6638       switch (event.type)
6639       {
6640         case EVENT_KEYPRESS:
6641           {
6642             key = GetEventKey((KeyEvent *)&event, TRUE);
6643
6644             // press 'Escape' or 'Enter' to keep the existing key binding
6645             if (key == KSYM_Escape || key == KSYM_Return)
6646               key = KSYM_UNDEFINED;     // keep old value
6647
6648             got_key_event = TRUE;
6649           }
6650           break;
6651
6652         case EVENT_KEYRELEASE:
6653           key_joystick_mapping = 0;
6654           break;
6655
6656         default:
6657           HandleOtherEvents(&event);
6658           break;
6659       }
6660     }
6661
6662     BackToFront();
6663   }
6664
6665   return key;
6666 }
6667
6668 static int getSetupValueFont(int type, void *value)
6669 {
6670   if (type & TYPE_GHOSTED)
6671     return FONT_OPTION_OFF;
6672   else if (type & TYPE_KEY)
6673     return (type & TYPE_QUERY ? FONT_INPUT_1_ACTIVE : FONT_VALUE_1);
6674   else if (type & TYPE_STRING)
6675     return FONT_VALUE_2;
6676   else if (type & TYPE_ECS_AGA)
6677     return FONT_VALUE_1;
6678   else if (type & TYPE_BOOLEAN_STYLE)
6679     return (*(boolean *)value ? FONT_OPTION_ON : FONT_OPTION_OFF);
6680   else if (type & TYPE_YES_NO_AUTO)
6681     return (*(int *)value == AUTO  ? FONT_OPTION_ON :
6682             *(int *)value == FALSE ? FONT_OPTION_OFF : FONT_OPTION_ON);
6683   else if (type & TYPE_PLAYER)
6684     return FONT_VALUE_1;
6685   else
6686     return FONT_VALUE_1;
6687 }
6688
6689 static int getSetupValueFontNarrow(int type, int font_nr)
6690 {
6691   return (font_nr == FONT_VALUE_1    ? FONT_VALUE_NARROW :
6692           font_nr == FONT_OPTION_ON  ? FONT_OPTION_ON_NARROW :
6693           font_nr == FONT_OPTION_OFF ? FONT_OPTION_OFF_NARROW :
6694           font_nr);
6695 }
6696
6697 static void drawSetupValue(int screen_pos, int setup_info_pos_raw)
6698 {
6699   int si_pos = (setup_info_pos_raw < 0 ? screen_pos : setup_info_pos_raw);
6700   struct TokenInfo *si = &setup_info[si_pos];
6701   boolean font_draw_xoffset_modified = FALSE;
6702   boolean scrollbar_needed = (num_setup_info < max_setup_info);
6703   int font_draw_xoffset_old = -1;
6704   int xoffset = (scrollbar_needed ? -1 : 0);
6705   int menu_screen_value_xpos = MENU_SCREEN_VALUE_XPOS + xoffset;
6706   int menu_screen_max_xpos = MENU_SCREEN_MAX_XPOS + xoffset;
6707   int xpos = menu_screen_value_xpos;
6708   int ypos = MENU_SCREEN_START_YPOS + screen_pos;
6709   int startx = mSX + xpos * 32;
6710   int starty = mSY + ypos * 32;
6711   int type = si->type;
6712   void *value = si->value;
6713   char *value_string = getSetupValue(type, value);
6714   int font_nr_default = getSetupValueFont(type, value);
6715   int font_width_default = getFontWidth(font_nr_default);
6716   int font_nr = font_nr_default;
6717   int i;
6718
6719   if (value_string == NULL)
6720     return;
6721
6722   if (type & TYPE_KEY)
6723   {
6724     xpos = MENU_SCREEN_START_XPOS;
6725
6726     if (type & TYPE_QUERY)
6727       value_string = "<press key>";
6728   }
6729   else if (type & TYPE_STRING)
6730   {
6731     int max_value_len = (SXSIZE - 2 * TILEX) / font_width_default;
6732
6733     xpos = MENU_SCREEN_START_XPOS;
6734
6735     if (strlen(value_string) > max_value_len)
6736       value_string[max_value_len] = '\0';
6737   }
6738   else if (type & TYPE_YES_NO_AUTO)
6739   {
6740     xpos = menu_screen_value_xpos - 1;
6741   }
6742   else if (type & TYPE_PLAYER)
6743   {
6744     int displayed_player_nr = *(int *)value + 1;
6745
6746     value_string = getSetupValue(TYPE_INTEGER, (void *)&displayed_player_nr);
6747   }
6748
6749   startx = mSX + xpos * 32;
6750   starty = mSY + ypos * 32;
6751
6752   // special check if right-side setup values moved left due to scrollbar
6753   if (scrollbar_needed && xpos > MENU_SCREEN_START_XPOS)
6754   {
6755     int max_menu_text_length = 26;      // maximum text length for classic menu
6756     int font_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
6757     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
6758     int text_font_nr = getMenuTextFont(FONT_MENU_2);
6759     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
6760     int text_width = max_menu_text_length * getFontWidth(text_font_nr);
6761
6762     if (startx + font_xoffset < text_startx + text_width + text_font_xoffset)
6763     {
6764       // when using narrow font, left-shifting text "auto" not needed
6765       if (type & TYPE_YES_NO_AUTO)
6766         xpos += 1;
6767
6768       xpos += 1;
6769       startx = mSX + xpos * 32;
6770
6771       font_nr = getSetupValueFontNarrow(type, font_nr);
6772     }
6773   }
6774
6775   // downward compatibility correction for Juergen Bonhagen's menu settings
6776   if (setup_mode != SETUP_MODE_INPUT)
6777   {
6778     int max_menu_text_length_big = (menu_screen_value_xpos -
6779                                     MENU_SCREEN_START_XPOS);
6780     int max_menu_text_length_medium = max_menu_text_length_big * 2;
6781     int check_font_nr = FONT_OPTION_ON; // known font that needs correction
6782     int font1_xoffset = getFontBitmapInfo(font_nr)->draw_xoffset;
6783     int font2_xoffset = getFontBitmapInfo(check_font_nr)->draw_xoffset;
6784     int text_startx = mSX + MENU_SCREEN_START_XPOS * 32;
6785     int text_font_nr = getMenuTextFont(FONT_MENU_2);
6786     int text_font_xoffset = getFontBitmapInfo(text_font_nr)->draw_xoffset;
6787     int text_width = max_menu_text_length_medium * getFontWidth(text_font_nr);
6788     boolean correct_font_draw_xoffset = FALSE;
6789
6790     if (xpos == MENU_SCREEN_START_XPOS &&
6791         startx + font1_xoffset < text_startx + text_font_xoffset)
6792       correct_font_draw_xoffset = TRUE;
6793
6794     if (xpos == menu_screen_value_xpos &&
6795         startx + font2_xoffset < text_startx + text_width + text_font_xoffset)
6796       correct_font_draw_xoffset = TRUE;
6797
6798     // check if setup value would overlap with setup text when printed
6799     // (this can happen for extreme/wrong values for font draw offset)
6800     if (correct_font_draw_xoffset)
6801     {
6802       font_draw_xoffset_old = getFontBitmapInfo(font_nr)->draw_xoffset;
6803       font_draw_xoffset_modified = TRUE;
6804
6805       if (type & TYPE_KEY)
6806         getFontBitmapInfo(font_nr)->draw_xoffset += 2 * getFontWidth(font_nr);
6807       else if (!(type & TYPE_STRING))
6808         getFontBitmapInfo(font_nr)->draw_xoffset = text_font_xoffset + 20 -
6809           max_menu_text_length_medium * (16 - getFontWidth(text_font_nr));
6810     }
6811   }
6812
6813   for (i = 0; i <= menu_screen_max_xpos - xpos; i++)
6814     DrawText(startx + i * font_width_default, starty, " ", font_nr_default);
6815
6816   DrawText(startx, starty, value_string, font_nr);
6817
6818   if (type & TYPE_PLAYER)
6819   {
6820     struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
6821     int player_nr = *(int *)value;
6822     int xoff = font->draw_xoffset + getFontWidth(font_nr);
6823     int yoff = font->draw_yoffset + (getFontHeight(font_nr) - TILEY) / 2;
6824     int startx2 = startx + xoff;
6825     int starty2 = starty + yoff;
6826
6827     if (DrawingOnBackground(startx2, starty2))
6828       ClearRectangleOnBackground(drawto, startx2, starty2, TILEX, TILEY);
6829
6830     DrawFixedGraphicThruMaskExt(drawto, startx2, starty2,
6831                                 PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
6832   }
6833
6834   if (font_draw_xoffset_modified)
6835     getFontBitmapInfo(font_nr)->draw_xoffset = font_draw_xoffset_old;
6836 }
6837
6838 static void changeSetupValue(int screen_pos, int setup_info_pos_raw, int dx)
6839 {
6840   int si_pos = (setup_info_pos_raw < 0 ? screen_pos : setup_info_pos_raw);
6841   struct TokenInfo *si = &setup_info[si_pos];
6842
6843   if (si->type & TYPE_BOOLEAN_STYLE)
6844   {
6845     *(boolean *)si->value ^= TRUE;
6846   }
6847   else if (si->type & TYPE_YES_NO_AUTO)
6848   {
6849     *(int *)si->value =
6850       (dx == -1 ?
6851        (*(int *)si->value == AUTO ? TRUE :
6852         *(int *)si->value == TRUE ? FALSE : AUTO) :
6853        (*(int *)si->value == TRUE ? AUTO :
6854         *(int *)si->value == AUTO ? FALSE : TRUE));
6855   }
6856   else if (si->type & TYPE_KEY)
6857   {
6858     Key key;
6859
6860     si->type |= TYPE_QUERY;
6861     drawSetupValue(screen_pos, setup_info_pos_raw);
6862     si->type &= ~TYPE_QUERY;
6863
6864     key = getSetupKey();
6865     if (key != KSYM_UNDEFINED)
6866       *(Key *)si->value = key;
6867   }
6868   else if (si->type & TYPE_PLAYER)
6869   {
6870     int player_nr = *(int *)si->value;
6871
6872     if (dx)
6873       player_nr += dx;
6874     else
6875       player_nr = Request("Choose player", REQ_PLAYER) - 1;
6876
6877     *(int *)si->value = MIN(MAX(0, player_nr), MAX_PLAYERS - 1);
6878   }
6879
6880   drawSetupValue(screen_pos, setup_info_pos_raw);
6881
6882   // fullscreen state may have changed at this point
6883   if (si->value == &setup.fullscreen)
6884     ToggleFullscreenIfNeeded();
6885
6886   // network mode may have changed at this point
6887   if (si->value == &setup.network_mode)
6888     ToggleNetworkModeIfNeeded();
6889
6890   // game speed list may have changed at this point
6891   if (si->value == &setup.game_speed_extended)
6892     ToggleGameSpeedsListIfNeeded();
6893 }
6894
6895 static struct TokenInfo *getSetupInfoFinal(struct TokenInfo *setup_info_orig)
6896 {
6897   static struct TokenInfo *setup_info_final = NULL;
6898   int list_size = 0;
6899   int list_pos = 0;
6900   int i;
6901
6902   // determine maximum list size of target list
6903   while (setup_info_orig[list_size++].type != 0);
6904
6905   // free, allocate and clear memory for target list
6906   checked_free(setup_info_final);
6907   setup_info_final = checked_calloc(list_size * sizeof(struct TokenInfo));
6908
6909   // copy setup info list without setup entries marked as hidden
6910   for (i = 0; setup_info_orig[i].type != 0; i++)
6911   {
6912     // skip setup entries configured to be hidden
6913     if (hideSetupEntry(setup_info_orig[i].value))
6914       continue;
6915
6916     // skip skippable setup entries if screen is lower than usual
6917     if (SCR_FIELDY < SCR_FIELDY_DEFAULT &&
6918         setup_info_orig[i].type == TYPE_SKIPPABLE)
6919       continue;
6920
6921     setup_info_final[list_pos++] = setup_info_orig[i];
6922   }
6923
6924   return setup_info_final;
6925 }
6926
6927 static void DrawSetupScreen_Generic(void)
6928 {
6929   int fade_mask = REDRAW_FIELD;
6930   boolean redraw_all = FALSE;
6931   char *title_string = NULL;
6932   int i;
6933
6934   if (CheckFadeAll())
6935     fade_mask = REDRAW_ALL;
6936
6937   UnmapAllGadgets();
6938   FadeMenuSoundsAndMusic();
6939
6940   FreeScreenGadgets();
6941   CreateScreenGadgets();
6942
6943   if (redraw_mask & REDRAW_ALL)
6944     redraw_all = TRUE;
6945
6946   FadeOut(fade_mask);
6947
6948   // needed if different viewport properties defined for setup screen
6949   ChangeViewportPropertiesIfNeeded();
6950
6951   SetMainBackgroundImage(IMG_BACKGROUND_SETUP);
6952
6953   ClearField();
6954
6955   OpenDoor(GetDoorState() | DOOR_NO_DELAY | DOOR_FORCE_REDRAW);
6956
6957   if (setup_mode == SETUP_MODE_MAIN)
6958   {
6959     setup_info = setup_info_main;
6960     title_string = STR_SETUP_MAIN;
6961   }
6962   else if (setup_mode == SETUP_MODE_GAME)
6963   {
6964     setup_info = setup_info_game;
6965     title_string = STR_SETUP_GAME;
6966   }
6967   else if (setup_mode == SETUP_MODE_ENGINES)
6968   {
6969     setup_info = setup_info_engines;
6970     title_string = STR_SETUP_ENGINES;
6971   }
6972   else if (setup_mode == SETUP_MODE_EDITOR)
6973   {
6974     setup_info = setup_info_editor;
6975     title_string = STR_SETUP_EDITOR;
6976   }
6977   else if (setup_mode == SETUP_MODE_GRAPHICS)
6978   {
6979     setup_info = setup_info_graphics;
6980     title_string = STR_SETUP_GRAPHICS;
6981   }
6982   else if (setup_mode == SETUP_MODE_SOUND)
6983   {
6984     setup_info = setup_info_sound;
6985     title_string = STR_SETUP_SOUND;
6986   }
6987   else if (setup_mode == SETUP_MODE_ARTWORK)
6988   {
6989     setup_info = setup_info_artwork;
6990     title_string = STR_SETUP_ARTWORK;
6991   }
6992   else if (setup_mode == SETUP_MODE_TOUCH)
6993   {
6994     setup_info = setup_info_touch;
6995     title_string = STR_SETUP_TOUCH;
6996
6997     if (strEqual(setup.touch.control_type, TOUCH_CONTROL_VIRTUAL_BUTTONS))
6998       setup_info = setup_info_touch_virtual_buttons[GRID_ACTIVE_NR()];
6999     else if (strEqual(setup.touch.control_type, TOUCH_CONTROL_WIPE_GESTURES))
7000       setup_info = setup_info_touch_wipe_gestures;
7001   }
7002   else if (setup_mode == SETUP_MODE_SHORTCUTS)
7003   {
7004     setup_info = setup_info_shortcuts;
7005     title_string = STR_SETUP_SHORTCUTS;
7006   }
7007   else if (setup_mode == SETUP_MODE_SHORTCUTS_1)
7008   {
7009     setup_info = setup_info_shortcuts_1;
7010     title_string = STR_SETUP_SHORTCUTS;
7011   }
7012   else if (setup_mode == SETUP_MODE_SHORTCUTS_2)
7013   {
7014     setup_info = setup_info_shortcuts_2;
7015     title_string = STR_SETUP_SHORTCUTS;
7016   }
7017   else if (setup_mode == SETUP_MODE_SHORTCUTS_3)
7018   {
7019     setup_info = setup_info_shortcuts_3;
7020     title_string = STR_SETUP_SHORTCUTS;
7021   }
7022   else if (setup_mode == SETUP_MODE_SHORTCUTS_4)
7023   {
7024     setup_info = setup_info_shortcuts_4;
7025     title_string = STR_SETUP_SHORTCUTS;
7026   }
7027   else if (setup_mode == SETUP_MODE_SHORTCUTS_5)
7028   {
7029     setup_info = setup_info_shortcuts_5;
7030     title_string = STR_SETUP_SHORTCUTS;
7031   }
7032
7033   // use modified setup info without setup entries marked as hidden
7034   setup_info = getSetupInfoFinal(setup_info);
7035
7036   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, title_string);
7037
7038   // determine maximal number of setup entries that can be displayed on screen
7039   num_setup_info = 0;
7040   for (i = 0; setup_info[i].type != 0 && i < NUM_MENU_ENTRIES_ON_SCREEN; i++)
7041     num_setup_info++;
7042
7043   // determine maximal number of setup entries available for this setup screen
7044   max_setup_info = 0;
7045   for (i = 0; setup_info[i].type != 0; i++)
7046     max_setup_info++;
7047
7048   HandleSetupScreen_Generic(0, 0, 0, 0, MB_MENU_INITIALIZE);
7049
7050   MapScreenGadgets(max_setup_info);
7051
7052   if (redraw_all)
7053     redraw_mask = fade_mask = REDRAW_ALL;
7054
7055   DrawMaskedBorder(fade_mask);
7056
7057   FadeIn(fade_mask);
7058 }
7059
7060 void HandleSetupScreen_Generic(int mx, int my, int dx, int dy, int button)
7061 {
7062   menu_info = setup_info;
7063
7064   HandleMenuScreen(mx, my, dx, dy, button,
7065                    setup_mode, num_setup_info, max_setup_info);
7066 }
7067
7068 static void DrawSetupScreen_Input(void)
7069 {
7070   int i;
7071
7072   FadeOut(REDRAW_FIELD);
7073
7074   ClearField();
7075
7076   setup_info = getSetupInfoFinal(setup_info_input);
7077
7078   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, STR_SETUP_INPUT);
7079
7080   for (i = 0; setup_info[i].type != 0; i++)
7081   {
7082     if (setup_info[i].type & (TYPE_ENTER_MENU|TYPE_ENTER_LIST))
7083       initCursor(i, IMG_MENU_BUTTON_ENTER_MENU);
7084     else if (setup_info[i].type & (TYPE_LEAVE_MENU|TYPE_LEAVE_LIST))
7085       initCursor(i, IMG_MENU_BUTTON_LEAVE_MENU);
7086     else if (setup_info[i].type & ~TYPE_SKIP_ENTRY)
7087       initCursor(i, IMG_MENU_BUTTON);
7088
7089     DrawCursorAndText_Setup(i, -1, FALSE);
7090   }
7091
7092   // create gadgets for setup input menu screen
7093   FreeScreenGadgets();
7094   CreateScreenGadgets();
7095
7096   // map gadgets for setup input menu screen
7097   MapScreenMenuGadgets(SCREEN_MASK_INPUT);
7098
7099   HandleSetupScreen_Input(0, 0, 0, 0, MB_MENU_INITIALIZE);
7100
7101   FadeIn(REDRAW_FIELD);
7102 }
7103
7104 static void setJoystickDeviceToNr(char *device_name, int device_nr)
7105 {
7106   if (device_name == NULL)
7107     return;
7108
7109   if (device_nr < 0 || device_nr >= MAX_PLAYERS)
7110     device_nr = 0;
7111
7112   if (strlen(device_name) > 1)
7113   {
7114     char c1 = device_name[strlen(device_name) - 1];
7115     char c2 = device_name[strlen(device_name) - 2];
7116
7117     if (c1 >= '0' && c1 <= '9' && !(c2 >= '0' && c2 <= '9'))
7118       device_name[strlen(device_name) - 1] = '0' + (char)(device_nr % 10);
7119   }
7120   else
7121     strncpy(device_name, getDeviceNameFromJoystickNr(device_nr),
7122             strlen(device_name));
7123 }
7124
7125 static void drawPlayerSetupInputInfo(int player_nr, boolean active)
7126 {
7127   int i;
7128   static struct SetupKeyboardInfo custom_key;
7129   static struct
7130   {
7131     Key *key;
7132     char *text;
7133   } custom[] =
7134   {
7135     { &custom_key.left,  "Axis/Pad Left"  },
7136     { &custom_key.right, "Axis/Pad Right" },
7137     { &custom_key.up,    "Axis/Pad Up"    },
7138     { &custom_key.down,  "Axis/Pad Down"  },
7139     { &custom_key.snap,  "Button 1/A/X"   },
7140     { &custom_key.drop,  "Button 2/B/Y"   }
7141   };
7142   static char *joystick_name[MAX_PLAYERS] =
7143   {
7144     "Joystick1",
7145     "Joystick2",
7146     "Joystick3",
7147     "Joystick4"
7148   };
7149   int font_nr_menu = (active ? FONT_MENU_1_ACTIVE : FONT_MENU_1);
7150   int font_nr_info = FONT_MENU_1;
7151   int font_nr_name = FONT_VALUE_OLD;
7152   int font_nr_on   = FONT_VALUE_1;
7153   int font_nr_off  = FONT_VALUE_OLD;
7154   int pos = 4;
7155
7156   if (SCR_FIELDX < SCR_FIELDX_DEFAULT)
7157   {
7158     font_nr_info = FONT_MENU_2;
7159     font_nr_on   = FONT_VALUE_NARROW;
7160     font_nr_off  = FONT_VALUE_OLD_NARROW;
7161   }
7162
7163   custom_key = setup.input[player_nr].key;
7164
7165   DrawText(mSX + 11 * 32, mSY + 2 * 32, int2str(player_nr + 1, 1),
7166            FONT_INPUT_1_ACTIVE);
7167
7168   ClearRectangleOnBackground(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
7169                              TILEX, TILEY);
7170   DrawFixedGraphicThruMaskExt(drawto, mSX + 8 * TILEX, mSY + 2 * TILEY,
7171                               PLAYER_NR_GFX(IMG_PLAYER_1, player_nr), 0);
7172
7173   if (setup.input[player_nr].use_joystick)
7174   {
7175     char *device_name = setup.input[player_nr].joy.device_name;
7176     int joystick_nr = getJoystickNrFromDeviceName(device_name);
7177     boolean joystick_active = CheckJoystickOpened(joystick_nr);
7178     char *text = joystick_name[joystick_nr];
7179     int font_nr = (joystick_active ? font_nr_on : font_nr_off);
7180
7181     DrawText(mSX + 8 * 32, mSY + 3 * 32, text, font_nr);
7182     DrawText(mSX + 32, mSY + 4 * 32, "Configure", font_nr_menu);
7183   }
7184   else
7185   {
7186     DrawText(mSX + 8 * 32, mSY + 3 * 32, "Keyboard ", font_nr_on);
7187     DrawText(mSX + 1 * 32, mSY + 4 * 32, "Customize", font_nr_menu);
7188   }
7189
7190   if (SCR_FIELDY >= SCR_FIELDY_DEFAULT)
7191     DrawText(mSX + 32, mSY + 5 * 32, "Actual Settings:", font_nr_info);
7192   else
7193     pos = 3;
7194
7195   drawCursorXY(1, pos + 0, IMG_MENU_BUTTON_LEFT);
7196   drawCursorXY(1, pos + 1, IMG_MENU_BUTTON_RIGHT);
7197   drawCursorXY(1, pos + 2, IMG_MENU_BUTTON_UP);
7198   drawCursorXY(1, pos + 3, IMG_MENU_BUTTON_DOWN);
7199
7200   DrawText(mSX + 2 * 32, mSY + (pos + 2) * 32, ":", font_nr_name);
7201   DrawText(mSX + 2 * 32, mSY + (pos + 3) * 32, ":", font_nr_name);
7202   DrawText(mSX + 2 * 32, mSY + (pos + 4) * 32, ":", font_nr_name);
7203   DrawText(mSX + 2 * 32, mSY + (pos + 5) * 32, ":", font_nr_name);
7204   DrawText(mSX + 1 * 32, mSY + (pos + 6) * 32, "Snap Field:", font_nr_name);
7205   DrawText(mSX + 1 * 32, mSY + (pos + 8) * 32, "Drop Element:", font_nr_name);
7206
7207   for (i = 0; i < 6; i++)
7208   {
7209     int ypos = (pos + 2) + i + (i > 3 ? i - 3 : 0);
7210
7211     DrawText(mSX + 3 * 32, mSY + ypos * 32,
7212              "              ", font_nr_on);
7213     DrawText(mSX + 3 * 32, mSY + ypos * 32,
7214              (setup.input[player_nr].use_joystick ?
7215               custom[i].text :
7216               getKeyNameFromKey(*custom[i].key)), font_nr_on);
7217   }
7218 }
7219
7220 static int input_player_nr = 0;
7221
7222 static void HandleSetupScreen_Input_Player(int step, int direction)
7223 {
7224   int old_player_nr = input_player_nr;
7225   int new_player_nr;
7226
7227   new_player_nr = old_player_nr + step * direction;
7228   if (new_player_nr < 0)
7229     new_player_nr = 0;
7230   if (new_player_nr > MAX_PLAYERS - 1)
7231     new_player_nr = MAX_PLAYERS - 1;
7232
7233   if (new_player_nr != old_player_nr)
7234   {
7235     input_player_nr = new_player_nr;
7236
7237     drawPlayerSetupInputInfo(input_player_nr, FALSE);
7238   }
7239 }
7240
7241 void HandleSetupScreen_Input(int mx, int my, int dx, int dy, int button)
7242 {
7243   static int choice = 0;
7244   int x = 0;
7245   int y = choice;
7246   int pos_start  = SETUPINPUT_SCREEN_POS_START;
7247   int pos_empty1 = SETUPINPUT_SCREEN_POS_EMPTY1;
7248   int pos_empty2 = SETUPINPUT_SCREEN_POS_EMPTY2;
7249   int pos_end    = SETUPINPUT_SCREEN_POS_END;
7250
7251   if (SCR_FIELDY < SCR_FIELDY_DEFAULT)
7252   {
7253     int i;
7254
7255     for (i = 0; setup_info_input[i].type != 0; i++)
7256     {
7257       // adjust menu structure according to skipped setup entries
7258       if (setup_info_input[i].type == TYPE_SKIPPABLE)
7259       {
7260         pos_empty2--;
7261         pos_end--;
7262       }
7263     }
7264   }
7265
7266   if (button == MB_MENU_INITIALIZE)
7267   {
7268     // input setup menu may have changed size due to graphics configuration
7269     if (choice >= pos_empty1)
7270       choice = pos_end;
7271
7272     drawPlayerSetupInputInfo(input_player_nr, (choice == 2));
7273
7274     DrawCursorAndText_Setup(choice, -1, TRUE);
7275
7276     return;
7277   }
7278   else if (button == MB_MENU_LEAVE)
7279   {
7280     setup_mode = SETUP_MODE_MAIN;
7281     DrawSetupScreen();
7282     InitJoysticks();
7283
7284     return;
7285   }
7286
7287   if (mx || my)         // mouse input
7288   {
7289     x = (mx - mSX) / 32;
7290     y = (my - mSY) / 32 - MENU_SCREEN_START_YPOS;
7291   }
7292   else if (dx || dy)    // keyboard input
7293   {
7294     if (dx && choice == 0)
7295       x = (dx < 0 ? 10 : 12);
7296     else if ((dx && choice == 1) ||
7297              (dx == -1 && choice == pos_end))
7298       button = MB_MENU_CHOICE;
7299     else if (dy)
7300       y = choice + dy;
7301
7302     if (y >= pos_empty1 && y <= pos_empty2)
7303       y = (dy > 0 ? pos_empty2 + 1 : pos_empty1 - 1);
7304   }
7305
7306   if (y == 0 && dx != 0 && button)
7307   {
7308     HandleSetupScreen_Input_Player(1, dx < 0 ? -1 : +1);
7309   }
7310   else if (IN_VIS_FIELD(x, y) &&        // (does not use "IN_VIS_MENU()" yet)
7311            y >= pos_start && y <= pos_end &&
7312            !(y >= pos_empty1 && y <= pos_empty2))
7313   {
7314     if (button)
7315     {
7316       if (y != choice)
7317       {
7318         DrawCursorAndText_Setup(choice, -1, FALSE);
7319         DrawCursorAndText_Setup(y, -1, TRUE);
7320
7321         drawPlayerSetupInputInfo(input_player_nr, (y == 2));
7322
7323         choice = y;
7324       }
7325     }
7326     else
7327     {
7328       if (y == 1)
7329       {
7330         char *device_name = setup.input[input_player_nr].joy.device_name;
7331
7332         if (!setup.input[input_player_nr].use_joystick)
7333         {
7334           int new_device_nr = (dx >= 0 ? 0 : MAX_PLAYERS - 1);
7335
7336           setJoystickDeviceToNr(device_name, new_device_nr);
7337           setup.input[input_player_nr].use_joystick = TRUE;
7338         }
7339         else
7340         {
7341           int device_nr = getJoystickNrFromDeviceName(device_name);
7342           int new_device_nr = device_nr + (dx >= 0 ? +1 : -1);
7343
7344           if (new_device_nr < 0 || new_device_nr >= MAX_PLAYERS)
7345             setup.input[input_player_nr].use_joystick = FALSE;
7346           else
7347             setJoystickDeviceToNr(device_name, new_device_nr);
7348         }
7349
7350         drawPlayerSetupInputInfo(input_player_nr, FALSE);
7351       }
7352       else if (y == 2)
7353       {
7354         if (setup.input[input_player_nr].use_joystick)
7355           ConfigureJoystick(input_player_nr);
7356         else
7357           CustomizeKeyboard(input_player_nr);
7358       }
7359       else if (y == pos_end)
7360       {
7361         InitJoysticks();
7362
7363         FadeSetLeaveMenu();
7364
7365         setup_mode = SETUP_MODE_MAIN;
7366         DrawSetupScreen();
7367       }
7368     }
7369   }
7370 }
7371
7372 static boolean CustomizeKeyboardMain(int player_nr)
7373 {
7374   int i;
7375   int step_nr;
7376   boolean finished = FALSE;
7377   static struct SetupKeyboardInfo custom_key;
7378   static struct
7379   {
7380     Key *key;
7381     char *text;
7382   } customize_step[] =
7383   {
7384     { &custom_key.left,  "Move Left"    },
7385     { &custom_key.right, "Move Right"   },
7386     { &custom_key.up,    "Move Up"      },
7387     { &custom_key.down,  "Move Down"    },
7388     { &custom_key.snap,  "Snap Field"   },
7389     { &custom_key.drop,  "Drop Element" }
7390   };
7391   int font_nr_old = FONT_VALUE_OLD;
7392   int font_nr_new = FONT_VALUE_1;
7393   boolean success = FALSE;
7394
7395   if (SCR_FIELDX < SCR_FIELDX_DEFAULT)
7396   {
7397     font_nr_old = FONT_VALUE_OLD_NARROW;
7398     font_nr_new = FONT_VALUE_NARROW;
7399   }
7400
7401   // read existing key bindings from player setup
7402   custom_key = setup.input[player_nr].key;
7403
7404   FadeSetEnterMenu();
7405   FadeOut(REDRAW_FIELD);
7406
7407   ClearField();
7408
7409   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Keyboard Input");
7410
7411   step_nr = 0;
7412   DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
7413            customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
7414   DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
7415            "Key:", FONT_INPUT_1_ACTIVE);
7416   DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
7417            getKeyNameFromKey(*customize_step[step_nr].key), font_nr_old);
7418
7419   FadeIn(REDRAW_FIELD);
7420
7421   while (!finished)
7422   {
7423     Event event;
7424
7425     if (NextValidEvent(&event))
7426     {
7427       switch (event.type)
7428       {
7429         case EVENT_KEYPRESS:
7430           {
7431             Key key = GetEventKey((KeyEvent *)&event, FALSE);
7432
7433             // press 'Escape' to abort and keep the old key bindings
7434             if (key == KSYM_Escape)
7435             {
7436               FadeSkipNextFadeIn();
7437
7438               finished = TRUE;
7439
7440               break;
7441             }
7442
7443             // press 'Enter' to keep the existing key binding
7444             if (key == KSYM_Return)
7445               key = *customize_step[step_nr].key;
7446
7447             // check if key already used
7448             for (i = 0; i < step_nr; i++)
7449               if (*customize_step[i].key == key)
7450                 break;
7451             if (i < step_nr)
7452               break;
7453
7454             // got new key binding
7455             *customize_step[step_nr].key = key;
7456             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
7457                      "             ", font_nr_new);
7458             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
7459                      getKeyNameFromKey(key), font_nr_new);
7460             step_nr++;
7461
7462             // un-highlight last query
7463             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1)) * 32,
7464                      customize_step[step_nr - 1].text, FONT_MENU_1);
7465             DrawText(mSX, mSY + (2 + 2 * (step_nr - 1) + 1) * 32,
7466                      "Key:", FONT_MENU_1);
7467
7468             // all keys configured
7469             if (step_nr == 6)
7470             {
7471               finished = TRUE;
7472               success = TRUE;
7473
7474               break;
7475             }
7476
7477             // query next key binding
7478             DrawText(mSX, mSY + (2 + 2 * step_nr) * 32,
7479                      customize_step[step_nr].text, FONT_INPUT_1_ACTIVE);
7480             DrawText(mSX, mSY + (2 + 2 * step_nr + 1) * 32,
7481                      "Key:", FONT_INPUT_1_ACTIVE);
7482             DrawText(mSX + 4 * 32, mSY + (2 + 2 * step_nr + 1) * 32,
7483                      getKeyNameFromKey(*customize_step[step_nr].key),
7484                      font_nr_old);
7485           }
7486           break;
7487
7488         case EVENT_KEYRELEASE:
7489           key_joystick_mapping = 0;
7490           break;
7491
7492         default:
7493           HandleOtherEvents(&event);
7494           break;
7495       }
7496     }
7497
7498     BackToFront();
7499   }
7500
7501   // write new key bindings back to player setup, if successfully finished
7502   if (success)
7503     setup.input[player_nr].key = custom_key;
7504
7505   return success;
7506 }
7507
7508 void CustomizeKeyboard(int player_nr)
7509 {
7510   boolean success = CustomizeKeyboardMain(player_nr);
7511
7512   if (success)
7513   {
7514     int font_nr = FONT_TITLE_1;
7515     int font_height = getFontHeight(font_nr);
7516     int ypos1 = SYSIZE / 2 - font_height * 2;
7517     int ypos2 = SYSIZE / 2 - font_height * 1;
7518     unsigned int wait_frame_delay = 0;
7519     unsigned int wait_frame_delay_value = 2000;
7520
7521     ResetDelayCounter(&wait_frame_delay);
7522
7523     ClearField();
7524
7525     DrawTextSCentered(ypos1, font_nr, "Keyboard");
7526     DrawTextSCentered(ypos2, font_nr, "configured!");
7527
7528     while (!DelayReached(&wait_frame_delay, wait_frame_delay_value))
7529       BackToFront();
7530
7531     ClearEventQueue();
7532   }
7533
7534   DrawSetupScreen_Input();
7535 }
7536
7537 // game controller mapping generator by Gabriel Jacobo <gabomdq@gmail.com>
7538
7539 #define MARKER_BUTTON           1
7540 #define MARKER_AXIS_X           2
7541 #define MARKER_AXIS_Y           3
7542
7543 static boolean ConfigureJoystickMapButtonsAndAxes(SDL_Joystick *joystick)
7544 {
7545   static boolean bitmaps_initialized = FALSE;
7546   boolean screen_initialized = FALSE;
7547   static Bitmap *controller, *button, *axis_x, *axis_y;
7548   char *name;
7549   boolean success = TRUE;
7550   boolean done = FALSE, next = FALSE;
7551   Event event;
7552   int alpha = 200, alpha_step = -1;
7553   int alpha_ticks = 0;
7554   char mapping[4096], temp[256];
7555   int font_name = MENU_SETUP_FONT_TITLE;
7556   int font_info = MENU_SETUP_FONT_TEXT;
7557   int spacing_name = menu.line_spacing_setup[SETUP_MODE_INPUT];
7558   int spacing_line = menu.line_spacing_setup[SETUP_MODE_INPUT];
7559   int spacing_para = menu.paragraph_spacing_setup[SETUP_MODE_INPUT];
7560   int ystep_name = getMenuTextStep(spacing_name, font_name);
7561   int ystep_line = getMenuTextStep(spacing_line, font_info);
7562   int ystep_para = getMenuTextStep(spacing_para, font_info);
7563   int i, j;
7564
7565   struct
7566   {
7567     int x, y;
7568     int marker;
7569     char *field;
7570     int axis, button, hat, hat_value;
7571     char mapping[4096];
7572   }
7573   *step, *prev_step, steps[] =
7574   {
7575     { 356, 155, MARKER_BUTTON, "a",             },
7576     { 396, 122, MARKER_BUTTON, "b",             },
7577     { 320, 125, MARKER_BUTTON, "x",             },
7578     { 358,  95, MARKER_BUTTON, "y",             },
7579     { 162, 125, MARKER_BUTTON, "back",          },
7580     { 216, 125, MARKER_BUTTON, "guide",         },
7581     { 271, 125, MARKER_BUTTON, "start",         },
7582     { 110, 200, MARKER_BUTTON, "dpleft",        },
7583     { 146, 228, MARKER_BUTTON, "dpdown",        },
7584     { 178, 200, MARKER_BUTTON, "dpright",       },
7585     { 146, 172, MARKER_BUTTON, "dpup",          },
7586     {  50,  40, MARKER_BUTTON, "leftshoulder",  },
7587     {  88, -10, MARKER_AXIS_Y, "lefttrigger",   },
7588     { 382,  40, MARKER_BUTTON, "rightshoulder", },
7589     { 346, -10, MARKER_AXIS_Y, "righttrigger",  },
7590     {  73, 141, MARKER_BUTTON, "leftstick",     },
7591     { 282, 210, MARKER_BUTTON, "rightstick",    },
7592     {  73, 141, MARKER_AXIS_X, "leftx",         },
7593     {  73, 141, MARKER_AXIS_Y, "lefty",         },
7594     { 282, 210, MARKER_AXIS_X, "rightx",        },
7595     { 282, 210, MARKER_AXIS_Y, "righty",        },
7596   };
7597
7598   unsigned int event_frame_delay = 0;
7599   unsigned int event_frame_delay_value = GAME_FRAME_DELAY;
7600
7601   ResetDelayCounter(&event_frame_delay);
7602
7603   if (!bitmaps_initialized)
7604   {
7605     controller = LoadCustomImage("joystick/controller.png");
7606     button     = LoadCustomImage("joystick/button.png");
7607     axis_x     = LoadCustomImage("joystick/axis_x.png");
7608     axis_y     = LoadCustomImage("joystick/axis_y.png");
7609
7610     bitmaps_initialized = TRUE;
7611   }
7612
7613   name = getFormattedJoystickName(SDL_JoystickName(joystick));
7614
7615 #if DEBUG_JOYSTICKS
7616   // print info about the joystick we are watching
7617   Debug("joystick", "watching joystick %d: (%s)",
7618         SDL_JoystickInstanceID(joystick), name);
7619   Debug("joystick", "joystick has %d axes, %d hats, %d balls, and %d buttons",
7620         SDL_JoystickNumAxes(joystick), SDL_JoystickNumHats(joystick),
7621         SDL_JoystickNumBalls(joystick), SDL_JoystickNumButtons(joystick));
7622 #endif
7623
7624   // initialize mapping with GUID and name
7625   SDL_JoystickGetGUIDString(SDL_JoystickGetGUID(joystick), temp, sizeof(temp));
7626
7627   snprintf(mapping, sizeof(mapping), "%s,%s,platform:%s,",
7628            temp, name, SDL_GetPlatform());
7629
7630   // loop through all steps (buttons and axes), getting joystick events
7631   for (i = 0; i < SDL_arraysize(steps) && !done;)
7632   {
7633     Bitmap *marker = button;    // initialize with reliable default value
7634
7635     step = &steps[i];
7636     strcpy(step->mapping, mapping);
7637     step->axis = -1;
7638     step->button = -1;
7639     step->hat = -1;
7640     step->hat_value = -1;
7641
7642     marker = (step->marker == MARKER_BUTTON ? button :
7643               step->marker == MARKER_AXIS_X ? axis_x :
7644               step->marker == MARKER_AXIS_Y ? axis_y : marker);
7645
7646     next = FALSE;
7647
7648     while (!done && !next)
7649     {
7650       alpha += alpha_step * (int)(SDL_GetTicks() - alpha_ticks) / 5;
7651       alpha_ticks = SDL_GetTicks();
7652
7653       if (alpha >= 255)
7654       {
7655         alpha = 255;
7656         alpha_step = -1;
7657       }
7658       else if (alpha < 128)
7659       {
7660         alpha = 127;
7661         alpha_step = 1;
7662       }
7663
7664       int controller_x = SX + (SXSIZE - controller->width) / 2;
7665       int controller_y = SY + ystep_line;
7666
7667       int marker_x = controller_x + step->x;
7668       int marker_y = controller_y + step->y;
7669
7670       int ystart1 = mSY - 2 * SY + controller_y + controller->height;
7671       int ystart2 = ystart1 + ystep_name + ystep_line;
7672
7673       ClearField();
7674
7675       DrawTextSCentered(ystart1, font_name, name);
7676
7677       DrawTextSCentered(ystart2, font_info,
7678                         "Press buttons and move axes on");
7679       ystart2 += ystep_line;
7680       DrawTextSCentered(ystart2, font_info,
7681                         "your controller when indicated.");
7682       ystart2 += ystep_line;
7683       DrawTextSCentered(ystart2, font_info,
7684                         "(Your controller may look different.)");
7685       ystart2 += ystep_para;
7686
7687 #if defined(PLATFORM_ANDROID)
7688       DrawTextSCentered(ystart2, font_info,
7689                         "To correct a mistake,");
7690       ystart2 += ystep_line;
7691       DrawTextSCentered(ystart2, font_info,
7692                         "press the 'back' button.");
7693       ystart2 += ystep_line;
7694       DrawTextSCentered(ystart2, font_info,
7695                         "To skip a button or axis,");
7696       ystart2 += ystep_line;
7697       DrawTextSCentered(ystart2, font_info,
7698                         "press the 'menu' button.");
7699 #else
7700       DrawTextSCentered(ystart2, font_info,
7701                         "To correct a mistake,");
7702       ystart2 += ystep_line;
7703       DrawTextSCentered(ystart2, font_info,
7704                         "press the 'backspace' key.");
7705       ystart2 += ystep_line;
7706       DrawTextSCentered(ystart2, font_info,
7707                         "To skip a button or axis,");
7708       ystart2 += ystep_line;
7709       DrawTextSCentered(ystart2, font_info,
7710                         "press the 'return' key.");
7711       ystart2 += ystep_line;
7712       DrawTextSCentered(ystart2, font_info,
7713                         "To exit, press the 'escape' key.");
7714 #endif
7715
7716       BlitBitmapMasked(controller, drawto, 0, 0,
7717                        controller->width, controller->height,
7718                        controller_x, controller_y);
7719
7720       SDL_SetSurfaceBlendMode(marker->surface_masked, SDL_BLENDMODE_BLEND);
7721       SDL_SetSurfaceAlphaMod(marker->surface_masked, alpha);
7722
7723       BlitBitmapMasked(marker, drawto, 0, 0,
7724                        marker->width, marker->height,
7725                        marker_x, marker_y);
7726
7727       if (!screen_initialized)
7728         FadeIn(REDRAW_FIELD);
7729       else
7730         BackToFront();
7731
7732       screen_initialized = TRUE;
7733
7734       while (NextValidEvent(&event))
7735       {
7736         switch (event.type)
7737         {
7738           case SDL_JOYAXISMOTION:
7739             if (event.jaxis.value > 20000 ||
7740                 event.jaxis.value < -20000)
7741             {
7742               for (j = 0; j < i; j++)
7743                 if (steps[j].axis == event.jaxis.axis)
7744                   break;
7745
7746               if (j == i)
7747               {
7748                 if (step->marker != MARKER_AXIS_X &&
7749                     step->marker != MARKER_AXIS_Y)
7750                   break;
7751
7752                 step->axis = event.jaxis.axis;
7753                 strcat(mapping, step->field);
7754                 snprintf(temp, sizeof(temp), ":a%u,", event.jaxis.axis);
7755                 strcat(mapping, temp);
7756                 i++;
7757                 next = TRUE;
7758               }
7759             }
7760
7761             break;
7762
7763           case SDL_JOYHATMOTION:
7764             // ignore centering; we're probably just coming back
7765             // to the center from the previous item we set
7766             if (event.jhat.value == SDL_HAT_CENTERED)
7767               break;
7768
7769             for (j = 0; j < i; j++)
7770               if (steps[j].hat == event.jhat.hat &&
7771                   steps[j].hat_value == event.jhat.value)
7772                 break;
7773
7774             if (j == i)
7775             {
7776               step->hat = event.jhat.hat;
7777               step->hat_value = event.jhat.value;
7778               strcat(mapping, step->field);
7779               snprintf(temp, sizeof(temp), ":h%u.%u,",
7780                        event.jhat.hat, event.jhat.value );
7781               strcat(mapping, temp);
7782               i++;
7783               next = TRUE;
7784             }
7785
7786             break;
7787
7788           case SDL_JOYBALLMOTION:
7789             break;
7790
7791           case SDL_JOYBUTTONUP:
7792             for (j = 0; j < i; j++)
7793               if (steps[j].button == event.jbutton.button)
7794                 break;
7795
7796             if (j == i)
7797             {
7798               step->button = event.jbutton.button;
7799               strcat(mapping, step->field);
7800               snprintf(temp, sizeof(temp), ":b%u,", event.jbutton.button);
7801               strcat(mapping, temp);
7802               i++;
7803               next = TRUE;
7804             }
7805
7806             break;
7807
7808           case SDL_FINGERDOWN:
7809           case SDL_MOUSEBUTTONDOWN:
7810             // skip this step
7811             i++;
7812             next = TRUE;
7813
7814             break;
7815
7816           case SDL_KEYDOWN:
7817             if (event.key.keysym.sym == KSYM_BackSpace ||
7818                 event.key.keysym.sym == KSYM_Back)
7819             {
7820               if (i == 0)
7821               {
7822                 // leave screen
7823                 success = FALSE;
7824                 done = TRUE;
7825
7826                 break;
7827               }
7828
7829               // undo this step
7830               prev_step = &steps[i - 1];
7831               strcpy(mapping, prev_step->mapping);
7832               i--;
7833               next = TRUE;
7834
7835               break;
7836             }
7837
7838             if (event.key.keysym.sym == KSYM_space ||
7839                 event.key.keysym.sym == KSYM_Return ||
7840                 event.key.keysym.sym == KSYM_Menu)
7841             {
7842               // skip this step
7843               i++;
7844               next = TRUE;
7845
7846               break;
7847             }
7848
7849             if (event.key.keysym.sym == KSYM_Escape)
7850             {
7851               // leave screen
7852               success = FALSE;
7853               done = TRUE;
7854             }
7855
7856             break;
7857
7858           case SDL_QUIT:
7859             program.exit_function(0);
7860             break;
7861
7862           default:
7863             break;
7864         }
7865
7866         // do not handle events for longer than standard frame delay period
7867         if (DelayReached(&event_frame_delay, event_frame_delay_value))
7868           break;
7869       }
7870     }
7871   }
7872
7873   if (success)
7874   {
7875 #if DEBUG_JOYSTICKS
7876     Debug("joystick", "New game controller mapping:\n\n%s\n\n", mapping);
7877 #endif
7878
7879     // activate mapping for this game
7880     SDL_GameControllerAddMapping(mapping);
7881
7882     // save mapping to personal mappings
7883     SaveSetup_AddGameControllerMapping(mapping);
7884   }
7885
7886   // wait until the last pending event was removed from event queue
7887   while (NextValidEvent(&event));
7888
7889   return success;
7890 }
7891
7892 static int ConfigureJoystickMain(int player_nr)
7893 {
7894   char *device_name = setup.input[player_nr].joy.device_name;
7895   int joystick_nr = getJoystickNrFromDeviceName(device_name);
7896   boolean joystick_active = CheckJoystickOpened(joystick_nr);
7897   int success = FALSE;
7898   int i;
7899
7900   if (joystick.status == JOYSTICK_NOT_AVAILABLE)
7901     return JOYSTICK_NOT_AVAILABLE;
7902
7903   if (!joystick_active || !setup.input[player_nr].use_joystick)
7904     return JOYSTICK_NOT_AVAILABLE;
7905
7906   FadeSetEnterMenu();
7907   FadeOut(REDRAW_FIELD);
7908
7909   // close all joystick devices (potentially opened as game controllers)
7910   for (i = 0; i < SDL_NumJoysticks(); i++)
7911     SDLCloseJoystick(i);
7912
7913   // open joystick device as plain joystick to configure as game controller
7914   SDL_Joystick *joystick = SDL_JoystickOpen(joystick_nr);
7915
7916   // as the joystick was successfully opened before, this should not happen
7917   if (joystick == NULL)
7918     return FALSE;
7919
7920   // create new game controller mapping (buttons and axes) for joystick device
7921   success = ConfigureJoystickMapButtonsAndAxes(joystick);
7922
7923   // close joystick (and maybe re-open as configured game controller later)
7924   SDL_JoystickClose(joystick);
7925
7926   // re-open all joystick devices (potentially as game controllers)
7927   for (i = 0; i < SDL_NumJoysticks(); i++)
7928     SDLOpenJoystick(i);
7929
7930   // clear all joystick input actions for all joystick devices
7931   SDLClearJoystickState();
7932
7933   return (success ? JOYSTICK_CONFIGURED : JOYSTICK_NOT_CONFIGURED);
7934 }
7935
7936 void ConfigureJoystick(int player_nr)
7937 {
7938   boolean state = ConfigureJoystickMain(player_nr);
7939
7940   if (state != JOYSTICK_NOT_CONFIGURED)
7941   {
7942     boolean success = (state == JOYSTICK_CONFIGURED);
7943     char message1[MAX_OUTPUT_LINESIZE + 1];
7944     char *message2 = (success ? "configured!" : "not available!");
7945     char *device_name = setup.input[player_nr].joy.device_name;
7946     int nr = getJoystickNrFromDeviceName(device_name) + 1;
7947     int font_nr = FONT_TITLE_1;
7948     int font_height = getFontHeight(font_nr);
7949     int ypos1 = SYSIZE / 2 - font_height * 2;
7950     int ypos2 = SYSIZE / 2 - font_height * 1;
7951     unsigned int wait_frame_delay = 0;
7952     unsigned int wait_frame_delay_value = 2000;
7953
7954     ResetDelayCounter(&wait_frame_delay);
7955
7956     ClearField();
7957
7958     sprintf(message1, "Joystick %d", nr);
7959
7960     DrawTextSCentered(ypos1, font_nr, message1);
7961     DrawTextSCentered(ypos2, font_nr, message2);
7962
7963     while (!DelayReached(&wait_frame_delay, wait_frame_delay_value))
7964       BackToFront();
7965
7966     ClearEventQueue();
7967   }
7968
7969   DrawSetupScreen_Input();
7970 }
7971
7972 static void MapScreenMenuGadgets_OverlayTouchButtons(int y)
7973 {
7974   if (y < video.screen_height / 3)
7975   {
7976     // remap touch gadgets to access upper part of the screen
7977     UnmapScreenMenuGadgets(SCREEN_MASK_TOUCH);
7978     MapScreenMenuGadgets(SCREEN_MASK_TOUCH2);
7979   }
7980   else if (y > 2 * video.screen_height / 3)
7981   {
7982     // remap touch gadgets to access lower part of the screen
7983     MapScreenMenuGadgets(SCREEN_MASK_TOUCH);
7984     UnmapScreenMenuGadgets(SCREEN_MASK_TOUCH2);
7985   }
7986 }
7987
7988 static boolean ConfigureVirtualButtonsMain(void)
7989 {
7990   static char *customize_step_text[] =
7991   {
7992     "Move Left",
7993     "Move Right",
7994     "Move Up",
7995     "Move Down",
7996     "Snap Field",
7997     "Drop Element"
7998   };
7999   char grid_button[] =
8000   {
8001     CHAR_GRID_BUTTON_LEFT,
8002     CHAR_GRID_BUTTON_RIGHT,
8003     CHAR_GRID_BUTTON_UP,
8004     CHAR_GRID_BUTTON_DOWN,
8005     CHAR_GRID_BUTTON_SNAP,
8006     CHAR_GRID_BUTTON_DROP
8007   };
8008   enum
8009   {
8010     ACTION_NONE,
8011     ACTION_ESCAPE,
8012     ACTION_BACK,
8013     ACTION_NEXT
8014   };
8015   int font_nr = FONT_INPUT_1_ACTIVE;
8016   int font_height = getFontHeight(font_nr);
8017   int ypos1 = SYSIZE / 2 - font_height * 2;
8018   int ypos2 = SYSIZE / 2 - font_height * 1;
8019   boolean success = FALSE;
8020   boolean finished = FALSE;
8021   int step_nr = 0;
8022   char grid_button_draw = CHAR_GRID_BUTTON_NONE;
8023   char grid_button_old[MAX_GRID_XSIZE][MAX_GRID_YSIZE];
8024   char grid_button_tmp[MAX_GRID_XSIZE][MAX_GRID_YSIZE];
8025   boolean set_grid_button = FALSE;
8026   int nr = GRID_ACTIVE_NR();
8027   int x, y;
8028
8029   for (x = 0; x < MAX_GRID_XSIZE; x++)
8030     for (y = 0; y < MAX_GRID_YSIZE; y++)
8031       grid_button_old[x][y] = grid_button_tmp[x][y] = overlay.grid_button[x][y];
8032
8033   overlay.grid_button_highlight = grid_button[step_nr];
8034
8035   UnmapAllGadgets();
8036
8037   FadeSetEnterMenu();
8038   FadeOut(REDRAW_FIELD);
8039
8040   ClearField();
8041
8042   DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Virtual Buttons");
8043   DrawTextSCentered(ypos1, font_nr, "Select tiles to");
8044   DrawTextSCentered(ypos2, font_nr, customize_step_text[step_nr]);
8045
8046   FadeIn(REDRAW_FIELD);
8047
8048   SetOverlayShowGrid(TRUE);
8049
8050   // map gadgets for setup touch buttons menu screen
8051   MapScreenMenuGadgets(SCREEN_MASK_TOUCH);
8052
8053   while (!finished)
8054   {
8055     Event event;
8056
8057     while (NextValidEvent(&event))
8058     {
8059       int action = ACTION_NONE;
8060
8061       // ---------- handle events and set the resulting action ----------
8062
8063       switch (event.type)
8064       {
8065         case EVENT_USER:
8066           {
8067             UserEvent *user = (UserEvent *)&event;
8068             int id = user->value1;
8069
8070             action = (id == SCREEN_CTRL_ID_TOUCH_PREV_PAGE ||
8071                       id == SCREEN_CTRL_ID_TOUCH_PREV_PAGE2 ? ACTION_BACK :
8072                       id == SCREEN_CTRL_ID_TOUCH_NEXT_PAGE ||
8073                       id == SCREEN_CTRL_ID_TOUCH_NEXT_PAGE2 ? ACTION_NEXT :
8074                       ACTION_NONE);
8075           }
8076           break;
8077
8078         case EVENT_KEYPRESS:
8079           {
8080             Key key = GetEventKey((KeyEvent *)&event, FALSE);
8081
8082             action = (key == KSYM_Escape ?      ACTION_ESCAPE :
8083                       key == KSYM_BackSpace ||
8084                       key == KSYM_Back ?        ACTION_BACK :
8085                       key == KSYM_Return ||
8086                       key == KSYM_Menu ||
8087                       key == KSYM_space ?       ACTION_NEXT :
8088                       ACTION_NONE);
8089           }
8090           break;
8091
8092         case EVENT_KEYRELEASE:
8093           key_joystick_mapping = 0;
8094           break;
8095
8096         case EVENT_BUTTONPRESS:
8097         case EVENT_BUTTONRELEASE:
8098           {
8099             ButtonEvent *button = (ButtonEvent *)&event;
8100
8101             motion_status = FALSE;
8102
8103             if (button->type == EVENT_BUTTONPRESS)
8104               button_status = button->button;
8105             else
8106               button_status = MB_RELEASED;
8107
8108             if (HandleGadgets(button->x, button->y, button_status))
8109             {
8110               // do not handle this button event anymore
8111               break;
8112             }
8113
8114             button->x += video.screen_xoffset;
8115             button->y += video.screen_yoffset;
8116
8117             x = button->x * overlay.grid_xsize / video.screen_width;
8118             y = button->y * overlay.grid_ysize / video.screen_height;
8119
8120             if (button->type == EVENT_BUTTONPRESS)
8121             {
8122               grid_button_draw =
8123                 (overlay.grid_button[x][y] != grid_button[step_nr] ?
8124                  grid_button[step_nr] : CHAR_GRID_BUTTON_NONE);
8125
8126               set_grid_button = TRUE;
8127             }
8128
8129             MapScreenMenuGadgets_OverlayTouchButtons(button->y);
8130           }
8131           break;
8132
8133         case EVENT_MOTIONNOTIFY:
8134           {
8135             MotionEvent *motion = (MotionEvent *)&event;
8136
8137             motion_status = TRUE;
8138
8139             if (HandleGadgets(motion->x, motion->y, button_status))
8140             {
8141               // do not handle this button event anymore
8142               break;
8143             }
8144
8145             motion->x += video.screen_xoffset;
8146             motion->y += video.screen_yoffset;
8147
8148             x = motion->x * overlay.grid_xsize / video.screen_width;
8149             y = motion->y * overlay.grid_ysize / video.screen_height;
8150
8151             set_grid_button = TRUE;
8152
8153             MapScreenMenuGadgets_OverlayTouchButtons(motion->y);
8154           }
8155           break;
8156
8157         case SDL_WINDOWEVENT:
8158           HandleWindowEvent((WindowEvent *) &event);
8159
8160           // check if device has been rotated
8161           if (nr != GRID_ACTIVE_NR())
8162           {
8163             nr = GRID_ACTIVE_NR();
8164
8165             for (x = 0; x < MAX_GRID_XSIZE; x++)
8166               for (y = 0; y < MAX_GRID_YSIZE; y++)
8167                 grid_button_old[x][y] = grid_button_tmp[x][y] =
8168                   overlay.grid_button[x][y];
8169           }
8170
8171           break;
8172
8173         case SDL_APP_WILLENTERBACKGROUND:
8174         case SDL_APP_DIDENTERBACKGROUND:
8175         case SDL_APP_WILLENTERFOREGROUND:
8176         case SDL_APP_DIDENTERFOREGROUND:
8177           HandlePauseResumeEvent((PauseResumeEvent *) &event);
8178           break;
8179
8180         default:
8181           HandleOtherEvents(&event);
8182           break;
8183       }
8184
8185       // ---------- perform action set by handling events ----------
8186
8187       if (action == ACTION_ESCAPE)
8188       {
8189         // abort and restore the old key bindings
8190
8191         for (x = 0; x < MAX_GRID_XSIZE; x++)
8192           for (y = 0; y < MAX_GRID_YSIZE; y++)
8193             overlay.grid_button[x][y] = grid_button_old[x][y];
8194
8195         FadeSkipNextFadeIn();
8196
8197         finished = TRUE;
8198       }
8199       else if (action == ACTION_BACK)
8200       {
8201         // keep the configured key bindings and go to previous page
8202
8203         step_nr--;
8204
8205         if (step_nr < 0)
8206         {
8207           FadeSkipNextFadeIn();
8208
8209           finished = TRUE;
8210         }
8211       }
8212       else if (action == ACTION_NEXT)
8213       {
8214         // keep the configured key bindings and go to next page
8215
8216         step_nr++;
8217
8218         // all virtual buttons configured
8219         if (step_nr == 6)
8220         {
8221           finished = TRUE;
8222           success = TRUE;
8223         }
8224       }
8225
8226       if (action != ACTION_NONE && !finished)
8227       {
8228         for (x = 0; x < MAX_GRID_XSIZE; x++)
8229           for (y = 0; y < MAX_GRID_YSIZE; y++)
8230             grid_button_tmp[x][y] = overlay.grid_button[x][y];
8231
8232         overlay.grid_button_highlight = grid_button[step_nr];
8233
8234         // configure next virtual button
8235
8236         ClearField();
8237
8238         DrawTextSCentered(mSY - SY + 16, FONT_TITLE_1, "Virtual Buttons");
8239         DrawTextSCentered(ypos1, font_nr, "Select tiles to");
8240         DrawTextSCentered(ypos2, font_nr, customize_step_text[step_nr]);
8241       }
8242
8243       if (set_grid_button)
8244       {
8245         overlay.grid_button[x][y] =
8246           (grid_button_draw != CHAR_GRID_BUTTON_NONE ? grid_button_draw :
8247            grid_button_tmp[x][y] == grid_button[step_nr] ? CHAR_GRID_BUTTON_NONE :
8248            grid_button_tmp[x][y]);
8249
8250         set_grid_button = FALSE;
8251       }
8252     }
8253
8254     BackToFront();
8255   }
8256
8257   for (x = 0; x < MAX_GRID_XSIZE; x++)
8258     for (y = 0; y < MAX_GRID_YSIZE; y++)
8259       setup.touch.grid_button[nr][x][y] = overlay.grid_button[x][y];
8260
8261   overlay.grid_button_highlight = CHAR_GRID_BUTTON_NONE;
8262
8263   SetOverlayShowGrid(FALSE);
8264
8265   return success;
8266 }
8267
8268 void ConfigureVirtualButtons(void)
8269 {
8270   boolean success = ConfigureVirtualButtonsMain();
8271
8272   UnmapScreenMenuGadgets(SCREEN_MASK_TOUCH |
8273                          SCREEN_MASK_TOUCH2);
8274
8275   if (success)
8276   {
8277     int font_nr = FONT_TITLE_1;
8278     int font_height = getFontHeight(font_nr);
8279     int ypos1 = SYSIZE / 2 - font_height * 2;
8280     int ypos2 = SYSIZE / 2 - font_height * 1;
8281     unsigned int wait_frame_delay = 0;
8282     unsigned int wait_frame_delay_value = 2000;
8283
8284     ResetDelayCounter(&wait_frame_delay);
8285
8286     ClearField();
8287
8288     DrawTextSCentered(ypos1, font_nr, "Virtual buttons");
8289     DrawTextSCentered(ypos2, font_nr, "configured!");
8290
8291     while (!DelayReached(&wait_frame_delay, wait_frame_delay_value))
8292       BackToFront();
8293
8294     ClearEventQueue();
8295   }
8296 }
8297
8298 void DrawSetupScreen(void)
8299 {
8300   align_xoffset = 0;
8301   align_yoffset = 0;
8302
8303   if (setup_mode == SETUP_MODE_INPUT)
8304     DrawSetupScreen_Input();
8305   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
8306     DrawChooseTree(&game_speed_current);
8307   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
8308     DrawChooseTree(&scroll_delay_current);
8309   else if (setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
8310     DrawChooseTree(&snapshot_mode_current);
8311   else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
8312     DrawChooseTree(&window_size_current);
8313   else if (setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
8314     DrawChooseTree(&scaling_type_current);
8315   else if (setup_mode == SETUP_MODE_CHOOSE_RENDERING)
8316     DrawChooseTree(&rendering_mode_current);
8317   else if (setup_mode == SETUP_MODE_CHOOSE_VSYNC)
8318     DrawChooseTree(&vsync_mode_current);
8319   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
8320     DrawChooseTree(&artwork.gfx_current);
8321   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
8322     DrawChooseTree(&artwork.snd_current);
8323   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
8324     DrawChooseTree(&artwork.mus_current);
8325   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE)
8326     DrawChooseTree(&volume_simple_current);
8327   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS)
8328     DrawChooseTree(&volume_loops_current);
8329   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
8330     DrawChooseTree(&volume_music_current);
8331   else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL)
8332     DrawChooseTree(&touch_control_current);
8333   else if (setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE)
8334     DrawChooseTree(&move_distance_current);
8335   else if (setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
8336     DrawChooseTree(&drop_distance_current);
8337   else if (setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY)
8338     DrawChooseTree(&transparency_current);
8339   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_0)
8340     DrawChooseTree(&grid_size_current[0][0]);
8341   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_0)
8342     DrawChooseTree(&grid_size_current[0][1]);
8343   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_1)
8344     DrawChooseTree(&grid_size_current[1][0]);
8345   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_1)
8346     DrawChooseTree(&grid_size_current[1][1]);
8347   else
8348     DrawSetupScreen_Generic();
8349
8350   PlayMenuSoundsAndMusic();
8351 }
8352
8353 void RedrawSetupScreenAfterFullscreenToggle(void)
8354 {
8355   if (setup_mode == SETUP_MODE_GRAPHICS ||
8356       setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
8357   {
8358     // update list selection from "setup.window_scaling_percent"
8359     execSetupGraphics_setWindowSizes(TRUE);
8360
8361     DrawSetupScreen();
8362   }
8363 }
8364
8365 void RedrawSetupScreenAfterScreenRotation(int nr)
8366 {
8367   int x, y;
8368
8369   if (setup_mode == SETUP_MODE_TOUCH)
8370   {
8371     // update virtual button settings (depending on screen orientation)
8372     DrawSetupScreen();
8373   }
8374   else if (setup_mode == SETUP_MODE_CONFIG_VIRT_BUTTONS)
8375   {
8376     // save already configured virtual buttons
8377     for (x = 0; x < MAX_GRID_XSIZE; x++)
8378       for (y = 0; y < MAX_GRID_YSIZE; y++)
8379         setup.touch.grid_button[nr][x][y] = overlay.grid_button[x][y];
8380   }
8381 }
8382
8383 void HandleSetupScreen(int mx, int my, int dx, int dy, int button)
8384 {
8385   if (setup_mode == SETUP_MODE_INPUT)
8386     HandleSetupScreen_Input(mx, my, dx, dy, button);
8387   else if (setup_mode == SETUP_MODE_CHOOSE_GAME_SPEED)
8388     HandleChooseTree(mx, my, dx, dy, button, &game_speed_current);
8389   else if (setup_mode == SETUP_MODE_CHOOSE_SCROLL_DELAY)
8390     HandleChooseTree(mx, my, dx, dy, button, &scroll_delay_current);
8391   else if (setup_mode == SETUP_MODE_CHOOSE_SNAPSHOT_MODE)
8392     HandleChooseTree(mx, my, dx, dy, button, &snapshot_mode_current);
8393   else if (setup_mode == SETUP_MODE_CHOOSE_WINDOW_SIZE)
8394     HandleChooseTree(mx, my, dx, dy, button, &window_size_current);
8395   else if (setup_mode == SETUP_MODE_CHOOSE_SCALING_TYPE)
8396     HandleChooseTree(mx, my, dx, dy, button, &scaling_type_current);
8397   else if (setup_mode == SETUP_MODE_CHOOSE_RENDERING)
8398     HandleChooseTree(mx, my, dx, dy, button, &rendering_mode_current);
8399   else if (setup_mode == SETUP_MODE_CHOOSE_VSYNC)
8400     HandleChooseTree(mx, my, dx, dy, button, &vsync_mode_current);
8401   else if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS)
8402     HandleChooseTree(mx, my, dx, dy, button, &artwork.gfx_current);
8403   else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS)
8404     HandleChooseTree(mx, my, dx, dy, button, &artwork.snd_current);
8405   else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC)
8406     HandleChooseTree(mx, my, dx, dy, button, &artwork.mus_current);
8407   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_SIMPLE)
8408     HandleChooseTree(mx, my, dx, dy, button, &volume_simple_current);
8409   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_LOOPS)
8410     HandleChooseTree(mx, my, dx, dy, button, &volume_loops_current);
8411   else if (setup_mode == SETUP_MODE_CHOOSE_VOLUME_MUSIC)
8412     HandleChooseTree(mx, my, dx, dy, button, &volume_music_current);
8413   else if (setup_mode == SETUP_MODE_CHOOSE_TOUCH_CONTROL)
8414     HandleChooseTree(mx, my, dx, dy, button, &touch_control_current);
8415   else if (setup_mode == SETUP_MODE_CHOOSE_MOVE_DISTANCE)
8416     HandleChooseTree(mx, my, dx, dy, button, &move_distance_current);
8417   else if (setup_mode == SETUP_MODE_CHOOSE_DROP_DISTANCE)
8418     HandleChooseTree(mx, my, dx, dy, button, &drop_distance_current);
8419   else if (setup_mode == SETUP_MODE_CHOOSE_TRANSPARENCY)
8420     HandleChooseTree(mx, my, dx, dy, button, &transparency_current);
8421   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_0)
8422     HandleChooseTree(mx, my, dx, dy, button, &grid_size_current[0][0]);
8423   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_0)
8424     HandleChooseTree(mx, my, dx, dy, button, &grid_size_current[0][1]);
8425   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_XSIZE_1)
8426     HandleChooseTree(mx, my, dx, dy, button, &grid_size_current[1][0]);
8427   else if (setup_mode == SETUP_MODE_CHOOSE_GRID_YSIZE_1)
8428     HandleChooseTree(mx, my, dx, dy, button, &grid_size_current[1][1]);
8429   else
8430     HandleSetupScreen_Generic(mx, my, dx, dy, button);
8431 }
8432
8433 void HandleGameActions(void)
8434 {
8435   if (setup.ask_on_game_over)
8436     CheckGameOver();
8437
8438   if (game.restart_game_message != NULL)
8439   {
8440     RequestRestartGame(game.restart_game_message);
8441
8442     return;
8443   }
8444
8445   if (game_status != GAME_MODE_PLAYING)
8446     return;
8447
8448   GameActions();        // main game loop
8449
8450   if (tape.auto_play && !tape.playing)
8451     AutoPlayTapes();    // continue automatically playing next tape
8452 }
8453
8454
8455 // ---------- new screen button stuff --------------------------------------
8456
8457 static struct
8458 {
8459   int gfx_unpressed, gfx_pressed;
8460   struct MenuPosInfo *pos;
8461   boolean *check_value;
8462   int gadget_id;
8463   int screen_mask;
8464   unsigned int event_mask;
8465   boolean is_touch_button;
8466   char *infotext;
8467 } menubutton_info[NUM_SCREEN_MENUBUTTONS] =
8468 {
8469   {
8470     IMG_MENU_BUTTON_PREV_LEVEL, IMG_MENU_BUTTON_PREV_LEVEL_ACTIVE,
8471     &menu.main.button.prev_level, NULL,
8472     SCREEN_CTRL_ID_PREV_LEVEL,
8473     SCREEN_MASK_MAIN,
8474     GD_EVENT_PRESSED | GD_EVENT_REPEATED,
8475     FALSE, "previous level"
8476   },
8477   {
8478     IMG_MENU_BUTTON_NEXT_LEVEL, IMG_MENU_BUTTON_NEXT_LEVEL_ACTIVE,
8479     &menu.main.button.next_level, NULL,
8480     SCREEN_CTRL_ID_NEXT_LEVEL,
8481     SCREEN_MASK_MAIN,
8482     GD_EVENT_PRESSED | GD_EVENT_REPEATED,
8483     FALSE, "next level"
8484   },
8485   {
8486     IMG_MENU_BUTTON_FIRST_LEVEL, IMG_MENU_BUTTON_FIRST_LEVEL_ACTIVE,
8487     &menu.main.button.first_level, NULL,
8488     SCREEN_CTRL_ID_FIRST_LEVEL,
8489     SCREEN_MASK_MAIN,
8490     GD_EVENT_RELEASED,
8491     FALSE, "first level"
8492   },
8493   {
8494     IMG_MENU_BUTTON_LAST_LEVEL, IMG_MENU_BUTTON_LAST_LEVEL_ACTIVE,
8495     &menu.main.button.last_level, NULL,
8496     SCREEN_CTRL_ID_LAST_LEVEL,
8497     SCREEN_MASK_MAIN,
8498     GD_EVENT_RELEASED,
8499     FALSE, "last level"
8500   },
8501   {
8502     IMG_MENU_BUTTON_LEVEL_NUMBER, IMG_MENU_BUTTON_LEVEL_NUMBER_ACTIVE,
8503     &menu.main.button.level_number, NULL,
8504     SCREEN_CTRL_ID_LEVEL_NUMBER,
8505     SCREEN_MASK_MAIN,
8506     GD_EVENT_RELEASED,
8507     FALSE, "level number"
8508   },
8509   {
8510     IMG_MENU_BUTTON_LEFT, IMG_MENU_BUTTON_LEFT_ACTIVE,
8511     &menu.setup.button.prev_player, NULL,
8512     SCREEN_CTRL_ID_PREV_PLAYER,
8513     SCREEN_MASK_INPUT,
8514     GD_EVENT_PRESSED | GD_EVENT_REPEATED,
8515     FALSE, "previous player"
8516   },
8517   {
8518     IMG_MENU_BUTTON_RIGHT, IMG_MENU_BUTTON_RIGHT_ACTIVE,
8519     &menu.setup.button.next_player, NULL,
8520     SCREEN_CTRL_ID_NEXT_PLAYER,
8521     SCREEN_MASK_INPUT,
8522     GD_EVENT_PRESSED | GD_EVENT_REPEATED,
8523     FALSE, "next player"
8524   },
8525   {
8526     IMG_MENU_BUTTON_INSERT_SOLUTION, IMG_MENU_BUTTON_INSERT_SOLUTION_ACTIVE,
8527     &menu.main.button.insert_solution, NULL,
8528     SCREEN_CTRL_ID_INSERT_SOLUTION,
8529     SCREEN_MASK_MAIN_HAS_SOLUTION,
8530     GD_EVENT_RELEASED,
8531     FALSE, "insert solution tape"
8532   },
8533   {
8534     IMG_MENU_BUTTON_PLAY_SOLUTION, IMG_MENU_BUTTON_PLAY_SOLUTION_ACTIVE,
8535     &menu.main.button.play_solution, NULL,
8536     SCREEN_CTRL_ID_PLAY_SOLUTION,
8537     SCREEN_MASK_MAIN_HAS_SOLUTION,
8538     GD_EVENT_RELEASED,
8539     FALSE, "play solution tape"
8540   },
8541   {
8542     IMG_MENU_BUTTON_SWITCH_ECS_AGA, IMG_MENU_BUTTON_SWITCH_ECS_AGA_ACTIVE,
8543     &menu.main.button.switch_ecs_aga, &setup.prefer_aga_graphics,
8544     SCREEN_CTRL_ID_SWITCH_ECS_AGA,
8545     SCREEN_MASK_MAIN,
8546     GD_EVENT_RELEASED | GD_EVENT_OFF_BORDERS,
8547     FALSE, "switch ECS/AGA chipset"
8548   },
8549   {
8550     IMG_MENU_BUTTON_TOUCH_BACK, IMG_MENU_BUTTON_TOUCH_BACK,
8551     &menu.setup.button.touch_back, NULL,
8552     SCREEN_CTRL_ID_TOUCH_PREV_PAGE,
8553     SCREEN_MASK_TOUCH,
8554     GD_EVENT_RELEASED,
8555     TRUE, "previous page"
8556   },
8557   {
8558     IMG_MENU_BUTTON_TOUCH_NEXT, IMG_MENU_BUTTON_TOUCH_NEXT,
8559     &menu.setup.button.touch_next, NULL,
8560     SCREEN_CTRL_ID_TOUCH_NEXT_PAGE,
8561     SCREEN_MASK_TOUCH,
8562     GD_EVENT_RELEASED,
8563     TRUE, "next page"
8564   },
8565   {
8566     IMG_MENU_BUTTON_TOUCH_BACK2, IMG_MENU_BUTTON_TOUCH_BACK2,
8567     &menu.setup.button.touch_back2, NULL,
8568     SCREEN_CTRL_ID_TOUCH_PREV_PAGE2,
8569     SCREEN_MASK_TOUCH2,
8570     GD_EVENT_RELEASED,
8571     TRUE, "previous page"
8572   },
8573   {
8574     IMG_MENU_BUTTON_TOUCH_NEXT2, IMG_MENU_BUTTON_TOUCH_NEXT2,
8575     &menu.setup.button.touch_next2, NULL,
8576     SCREEN_CTRL_ID_TOUCH_NEXT_PAGE2,
8577     SCREEN_MASK_TOUCH2,
8578     GD_EVENT_RELEASED,
8579     TRUE, "next page"
8580   },
8581 };
8582
8583 static struct
8584 {
8585   int gfx_unpressed, gfx_pressed;
8586   int x, y;
8587   int gadget_id;
8588   char *infotext;
8589 } scrollbutton_info[NUM_SCREEN_SCROLLBUTTONS] =
8590 {
8591   {
8592     IMG_MENU_BUTTON_UP, IMG_MENU_BUTTON_UP_ACTIVE,
8593     -1, -1,     // these values are not constant, but can change at runtime
8594     SCREEN_CTRL_ID_SCROLL_UP,
8595     "scroll up"
8596   },
8597   {
8598     IMG_MENU_BUTTON_DOWN, IMG_MENU_BUTTON_DOWN_ACTIVE,
8599     -1, -1,     // these values are not constant, but can change at runtime
8600     SCREEN_CTRL_ID_SCROLL_DOWN,
8601     "scroll down"
8602   }
8603 };
8604
8605 static struct
8606 {
8607   int gfx_unpressed, gfx_pressed;
8608   int x, y;
8609   int width, height;
8610   int type;
8611   int gadget_id;
8612   char *infotext;
8613 } scrollbar_info[NUM_SCREEN_SCROLLBARS] =
8614 {
8615   {
8616     IMG_MENU_SCROLLBAR, IMG_MENU_SCROLLBAR_ACTIVE,
8617     -1, -1,     // these values are not constant, but can change at runtime
8618     -1, -1,     // these values are not constant, but can change at runtime
8619     GD_TYPE_SCROLLBAR_VERTICAL,
8620     SCREEN_CTRL_ID_SCROLL_VERTICAL,
8621     "scroll level series vertically"
8622   }
8623 };
8624
8625 static struct
8626 {
8627   int graphic;
8628   int gadget_id;
8629   int x, y;
8630   int size;
8631   char *value;
8632   char *infotext;
8633 } textinput_info[NUM_SCREEN_TEXTINPUT] =
8634 {
8635   {
8636     IMG_SETUP_INPUT_TEXT,
8637     SCREEN_CTRL_ID_NETWORK_SERVER,
8638     -1, -1,     // these values are not constant, but can change at runtime
8639     MAX_SETUP_TEXT_INPUT_LEN,
8640     network_server_hostname,
8641     "Network Server Hostname / IP"
8642   },
8643 };
8644
8645 static void CreateScreenMenubuttons(void)
8646 {
8647   struct GadgetInfo *gi;
8648   unsigned int event_mask;
8649   int i;
8650
8651   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
8652   {
8653     struct MenuPosInfo *pos = menubutton_info[i].pos;
8654     boolean is_touch_button = menubutton_info[i].is_touch_button;
8655     boolean is_check_button = menubutton_info[i].check_value != NULL;
8656     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
8657     int gfx_unpressed, gfx_pressed;
8658     int x, y, width, height;
8659     int gd_x1, gd_x2, gd_y1, gd_y2;
8660     int gd_x1a, gd_x2a, gd_y1a, gd_y2a;
8661     int id = menubutton_info[i].gadget_id;
8662     int type = GD_TYPE_NORMAL_BUTTON;
8663     boolean checked = FALSE;
8664
8665     event_mask = menubutton_info[i].event_mask;
8666
8667     x = (is_touch_button ? pos->x : mSX + GDI_ACTIVE_POS(pos->x));
8668     y = (is_touch_button ? pos->y : mSY + GDI_ACTIVE_POS(pos->y));
8669
8670     width  = graphic_info[menubutton_info[i].gfx_pressed].width;
8671     height = graphic_info[menubutton_info[i].gfx_pressed].height;
8672
8673     gfx_unpressed = menubutton_info[i].gfx_unpressed;
8674     gfx_pressed   = menubutton_info[i].gfx_pressed;
8675     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
8676     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
8677     gd_x1 = graphic_info[gfx_unpressed].src_x;
8678     gd_y1 = graphic_info[gfx_unpressed].src_y;
8679     gd_x2 = graphic_info[gfx_pressed].src_x;
8680     gd_y2 = graphic_info[gfx_pressed].src_y;
8681     gd_x1a = gd_x1;
8682     gd_y1a = gd_y1;
8683     gd_x2a = gd_x2;
8684     gd_y2a = gd_y2;
8685
8686     if (is_touch_button)
8687     {
8688       gd_x2 += graphic_info[gfx_pressed].pressed_xoffset;
8689       gd_y2 += graphic_info[gfx_pressed].pressed_yoffset;
8690     }
8691
8692     if (is_check_button)
8693     {
8694       gd_x1a += graphic_info[gfx_unpressed].active_xoffset;
8695       gd_y1a += graphic_info[gfx_unpressed].active_yoffset;
8696       gd_x2a += graphic_info[gfx_pressed].active_xoffset;
8697       gd_y2a += graphic_info[gfx_pressed].active_yoffset;
8698
8699       type = GD_TYPE_CHECK_BUTTON;
8700       checked = *menubutton_info[i].check_value;
8701     }
8702
8703     gi = CreateGadget(GDI_CUSTOM_ID, id,
8704                       GDI_CUSTOM_TYPE_ID, i,
8705                       GDI_IMAGE_ID, gfx_unpressed,
8706                       GDI_INFO_TEXT, menubutton_info[i].infotext,
8707                       GDI_X, x,
8708                       GDI_Y, y,
8709                       GDI_WIDTH, width,
8710                       GDI_HEIGHT, height,
8711                       GDI_TYPE, type,
8712                       GDI_STATE, GD_BUTTON_UNPRESSED,
8713                       GDI_CHECKED, checked,
8714                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
8715                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
8716                       GDI_ALT_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1a, gd_y1a,
8717                       GDI_ALT_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2a, gd_y2a,
8718                       GDI_DIRECT_DRAW, FALSE,
8719                       GDI_OVERLAY_TOUCH_BUTTON, is_touch_button,
8720                       GDI_EVENT_MASK, event_mask,
8721                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
8722                       GDI_END);
8723
8724     if (gi == NULL)
8725       Fail("cannot create gadget");
8726
8727     screen_gadget[id] = gi;
8728   }
8729 }
8730
8731 static void CreateScreenScrollbuttons(void)
8732 {
8733   struct GadgetInfo *gi;
8734   unsigned int event_mask;
8735   int i;
8736
8737   // these values are not constant, but can change at runtime
8738   scrollbutton_info[0].x = SC_SCROLL_UP_XPOS;
8739   scrollbutton_info[0].y = SC_SCROLL_UP_YPOS;
8740   scrollbutton_info[1].x = SC_SCROLL_DOWN_XPOS;
8741   scrollbutton_info[1].y = SC_SCROLL_DOWN_YPOS;
8742
8743   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
8744   {
8745     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
8746     int gfx_unpressed, gfx_pressed;
8747     int x, y, width, height;
8748     int gd_x1, gd_x2, gd_y1, gd_y2;
8749     int id = scrollbutton_info[i].gadget_id;
8750
8751     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
8752
8753     x = mSX + scrollbutton_info[i].x + menu.scrollbar_xoffset;
8754     y = mSY + scrollbutton_info[i].y;
8755     width = SC_SCROLLBUTTON_XSIZE;
8756     height = SC_SCROLLBUTTON_YSIZE;
8757
8758     // correct scrollbar position if placed outside menu (playfield) area
8759     if (x > SX + SC_SCROLL_UP_XPOS)
8760       x = SX + SC_SCROLL_UP_XPOS;
8761
8762     if (id == SCREEN_CTRL_ID_SCROLL_DOWN)
8763       y = mSY + (SC_SCROLL_VERTICAL_YPOS +
8764                  (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE);
8765
8766     gfx_unpressed = scrollbutton_info[i].gfx_unpressed;
8767     gfx_pressed   = scrollbutton_info[i].gfx_pressed;
8768     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
8769     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
8770     gd_x1 = graphic_info[gfx_unpressed].src_x;
8771     gd_y1 = graphic_info[gfx_unpressed].src_y;
8772     gd_x2 = graphic_info[gfx_pressed].src_x;
8773     gd_y2 = graphic_info[gfx_pressed].src_y;
8774
8775     gi = CreateGadget(GDI_CUSTOM_ID, id,
8776                       GDI_CUSTOM_TYPE_ID, i,
8777                       GDI_IMAGE_ID, gfx_unpressed,
8778                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
8779                       GDI_X, x,
8780                       GDI_Y, y,
8781                       GDI_WIDTH, width,
8782                       GDI_HEIGHT, height,
8783                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
8784                       GDI_STATE, GD_BUTTON_UNPRESSED,
8785                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
8786                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
8787                       GDI_DIRECT_DRAW, FALSE,
8788                       GDI_EVENT_MASK, event_mask,
8789                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
8790                       GDI_END);
8791
8792     if (gi == NULL)
8793       Fail("cannot create gadget");
8794
8795     screen_gadget[id] = gi;
8796   }
8797 }
8798
8799 static void CreateScreenScrollbars(void)
8800 {
8801   int i;
8802
8803   // these values are not constant, but can change at runtime
8804   scrollbar_info[0].x = SC_SCROLL_VERTICAL_XPOS;
8805   scrollbar_info[0].y = SC_SCROLL_VERTICAL_YPOS;
8806   scrollbar_info[0].width  = SC_SCROLL_VERTICAL_XSIZE;
8807   scrollbar_info[0].height = SC_SCROLL_VERTICAL_YSIZE;
8808
8809   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
8810   {
8811     Bitmap *gd_bitmap_unpressed, *gd_bitmap_pressed;
8812     int gfx_unpressed, gfx_pressed;
8813     int x, y, width, height;
8814     int gd_x1, gd_x2, gd_y1, gd_y2;
8815     struct GadgetInfo *gi;
8816     int items_max, items_visible, item_position;
8817     unsigned int event_mask;
8818     int num_page_entries = NUM_MENU_ENTRIES_ON_SCREEN;
8819     int id = scrollbar_info[i].gadget_id;
8820
8821     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
8822
8823     x = mSX + scrollbar_info[i].x + menu.scrollbar_xoffset;
8824     y = mSY + scrollbar_info[i].y;
8825     width  = scrollbar_info[i].width;
8826     height = scrollbar_info[i].height;
8827
8828     // correct scrollbar position if placed outside menu (playfield) area
8829     if (x > SX + SC_SCROLL_VERTICAL_XPOS)
8830       x = SX + SC_SCROLL_VERTICAL_XPOS;
8831
8832     if (id == SCREEN_CTRL_ID_SCROLL_VERTICAL)
8833       height = (NUM_MENU_ENTRIES_ON_SCREEN - 2) * SC_SCROLLBUTTON_YSIZE;
8834
8835     items_max = num_page_entries;
8836     items_visible = num_page_entries;
8837     item_position = 0;
8838
8839     gfx_unpressed = scrollbar_info[i].gfx_unpressed;
8840     gfx_pressed   = scrollbar_info[i].gfx_pressed;
8841     gd_bitmap_unpressed = graphic_info[gfx_unpressed].bitmap;
8842     gd_bitmap_pressed   = graphic_info[gfx_pressed].bitmap;
8843     gd_x1 = graphic_info[gfx_unpressed].src_x;
8844     gd_y1 = graphic_info[gfx_unpressed].src_y;
8845     gd_x2 = graphic_info[gfx_pressed].src_x;
8846     gd_y2 = graphic_info[gfx_pressed].src_y;
8847
8848     gi = CreateGadget(GDI_CUSTOM_ID, id,
8849                       GDI_CUSTOM_TYPE_ID, i,
8850                       GDI_IMAGE_ID, gfx_unpressed,
8851                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
8852                       GDI_X, x,
8853                       GDI_Y, y,
8854                       GDI_WIDTH, width,
8855                       GDI_HEIGHT, height,
8856                       GDI_TYPE, scrollbar_info[i].type,
8857                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
8858                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
8859                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
8860                       GDI_WHEEL_AREA_X, SX,
8861                       GDI_WHEEL_AREA_Y, SY,
8862                       GDI_WHEEL_AREA_WIDTH, SXSIZE,
8863                       GDI_WHEEL_AREA_HEIGHT, SYSIZE,
8864                       GDI_STATE, GD_BUTTON_UNPRESSED,
8865                       GDI_DESIGN_UNPRESSED, gd_bitmap_unpressed, gd_x1, gd_y1,
8866                       GDI_DESIGN_PRESSED, gd_bitmap_pressed, gd_x2, gd_y2,
8867                       GDI_BORDER_SIZE, SC_BORDER_SIZE, SC_BORDER_SIZE,
8868                       GDI_DIRECT_DRAW, FALSE,
8869                       GDI_EVENT_MASK, event_mask,
8870                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
8871                       GDI_END);
8872
8873     if (gi == NULL)
8874       Fail("cannot create gadget");
8875
8876     screen_gadget[id] = gi;
8877   }
8878 }
8879
8880 static void CreateScreenTextInputGadgets(void)
8881 {
8882   int i;
8883
8884   for (i = 0; i < NUM_SCREEN_TEXTINPUT; i++)
8885   {
8886     int graphic = textinput_info[i].graphic;
8887     struct GraphicInfo *gd = &graphic_info[graphic];
8888     int gd_x1 = gd->src_x;
8889     int gd_y1 = gd->src_y;
8890     int gd_x2 = gd->src_x + gd->active_xoffset;
8891     int gd_y2 = gd->src_y + gd->active_yoffset;
8892     struct GadgetInfo *gi;
8893     unsigned int event_mask;
8894     int id = textinput_info[i].gadget_id;
8895     int x = textinput_info[i].x;
8896     int y = textinput_info[i].y;
8897
8898     event_mask = GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
8899
8900     gi = CreateGadget(GDI_CUSTOM_ID, id,
8901                       GDI_CUSTOM_TYPE_ID, i,
8902                       GDI_INFO_TEXT, textinput_info[i].infotext,
8903                       GDI_X, SX + x,
8904                       GDI_Y, SY + y,
8905                       GDI_TYPE, GD_TYPE_TEXT_INPUT_ALPHANUMERIC,
8906                       GDI_TEXT_VALUE, textinput_info[i].value,
8907                       GDI_TEXT_SIZE, textinput_info[i].size,
8908                       GDI_TEXT_FONT, getSetupValueFont(TYPE_STRING, NULL),
8909                       GDI_TEXT_FONT_ACTIVE, FONT_TEXT_1,
8910                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
8911                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
8912                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
8913                       GDI_DESIGN_WIDTH, gd->width,
8914                       GDI_EVENT_MASK, event_mask,
8915                       GDI_CALLBACK_ACTION, HandleScreenGadgets,
8916                       GDI_CALLBACK_ACTION_ALWAYS, TRUE,
8917                       GDI_END);
8918
8919     if (gi == NULL)
8920       Fail("cannot create gadget");
8921
8922     screen_gadget[id] = gi;
8923   }
8924 }
8925
8926 void CreateScreenGadgets(void)
8927 {
8928   CreateScreenMenubuttons();
8929
8930   CreateScreenScrollbuttons();
8931   CreateScreenScrollbars();
8932
8933   CreateScreenTextInputGadgets();
8934 }
8935
8936 void FreeScreenGadgets(void)
8937 {
8938   int i;
8939
8940   for (i = 0; i < NUM_SCREEN_GADGETS; i++)
8941     FreeGadget(screen_gadget[i]);
8942 }
8943
8944 static void MapScreenMenuGadgets(int screen_mask)
8945 {
8946   int i;
8947
8948   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
8949     if (screen_mask & menubutton_info[i].screen_mask)
8950       MapGadget(screen_gadget[menubutton_info[i].gadget_id]);
8951 }
8952
8953 static void UnmapScreenMenuGadgets(int screen_mask)
8954 {
8955   int i;
8956
8957   for (i = 0; i < NUM_SCREEN_MENUBUTTONS; i++)
8958   {
8959     if (screen_mask & menubutton_info[i].screen_mask)
8960     {
8961       UnmapGadget(screen_gadget[menubutton_info[i].gadget_id]);
8962
8963       if (screen_mask & SCREEN_MASK_MAIN_HAS_SOLUTION)
8964         DrawBackground(screen_gadget[menubutton_info[i].gadget_id]->x,
8965                        screen_gadget[menubutton_info[i].gadget_id]->y,
8966                        screen_gadget[menubutton_info[i].gadget_id]->width,
8967                        screen_gadget[menubutton_info[i].gadget_id]->height);
8968     }
8969   }
8970 }
8971
8972 static void UpdateScreenMenuGadgets(int screen_mask, boolean map_gadgets)
8973 {
8974   if (map_gadgets)
8975     MapScreenMenuGadgets(screen_mask);
8976   else
8977     UnmapScreenMenuGadgets(screen_mask);
8978 }
8979
8980 static void MapScreenGadgets(int num_entries)
8981 {
8982   int i;
8983
8984   if (num_entries <= NUM_MENU_ENTRIES_ON_SCREEN)
8985     return;
8986
8987   for (i = 0; i < NUM_SCREEN_SCROLLBUTTONS; i++)
8988     MapGadget(screen_gadget[scrollbutton_info[i].gadget_id]);
8989
8990   for (i = 0; i < NUM_SCREEN_SCROLLBARS; i++)
8991     MapGadget(screen_gadget[scrollbar_info[i].gadget_id]);
8992 }
8993
8994 static void MapScreenTreeGadgets(TreeInfo *ti)
8995 {
8996   MapScreenGadgets(numTreeInfoInGroup(ti));
8997 }
8998
8999 static void HandleScreenGadgets(struct GadgetInfo *gi)
9000 {
9001   int id = gi->custom_id;
9002   int button = gi->event.button;
9003   int step = (button == MB_LEFTBUTTON   ? 1 :
9004               button == MB_MIDDLEBUTTON ? 5 :
9005               button == MB_RIGHTBUTTON  ? 10 : 1);
9006
9007   switch (id)
9008   {
9009     case SCREEN_CTRL_ID_PREV_LEVEL:
9010       HandleMainMenu_SelectLevel(step, -1, NO_DIRECT_LEVEL_SELECT);
9011       break;
9012
9013     case SCREEN_CTRL_ID_NEXT_LEVEL:
9014       HandleMainMenu_SelectLevel(step, +1, NO_DIRECT_LEVEL_SELECT);
9015       break;
9016
9017     case SCREEN_CTRL_ID_FIRST_LEVEL:
9018       HandleMainMenu_SelectLevel(MAX_LEVELS, -1, NO_DIRECT_LEVEL_SELECT);
9019       break;
9020
9021     case SCREEN_CTRL_ID_LAST_LEVEL:
9022       HandleMainMenu_SelectLevel(MAX_LEVELS, +1, NO_DIRECT_LEVEL_SELECT);
9023       break;
9024
9025     case SCREEN_CTRL_ID_LEVEL_NUMBER:
9026       CloseDoor(DOOR_CLOSE_2);
9027       SetGameStatus(GAME_MODE_LEVELNR);
9028       DrawChooseLevelNr();
9029       break;
9030
9031     case SCREEN_CTRL_ID_PREV_PLAYER:
9032       HandleSetupScreen_Input_Player(step, -1);
9033       break;
9034
9035     case SCREEN_CTRL_ID_NEXT_PLAYER:
9036       HandleSetupScreen_Input_Player(step, +1);
9037       break;
9038
9039     case SCREEN_CTRL_ID_INSERT_SOLUTION:
9040       InsertSolutionTape();
9041       break;
9042
9043     case SCREEN_CTRL_ID_PLAY_SOLUTION:
9044       PlaySolutionTape();
9045       break;
9046
9047     case SCREEN_CTRL_ID_SWITCH_ECS_AGA:
9048       setup.prefer_aga_graphics = !setup.prefer_aga_graphics;
9049       DrawMainMenu();
9050       break;
9051
9052     case SCREEN_CTRL_ID_TOUCH_PREV_PAGE:
9053     case SCREEN_CTRL_ID_TOUCH_NEXT_PAGE:
9054     case SCREEN_CTRL_ID_TOUCH_PREV_PAGE2:
9055     case SCREEN_CTRL_ID_TOUCH_NEXT_PAGE2:
9056       PushUserEvent(USEREVENT_GADGET_PRESSED, id, 0);
9057       break;
9058
9059     case SCREEN_CTRL_ID_SCROLL_UP:
9060       if (game_status == GAME_MODE_LEVELS)
9061         HandleChooseLevelSet(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
9062       else if (game_status == GAME_MODE_LEVELNR)
9063         HandleChooseLevelNr(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
9064       else if (game_status == GAME_MODE_SETUP)
9065         HandleSetupScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
9066       else if (game_status == GAME_MODE_INFO)
9067         HandleInfoScreen(0,0, 0, -1 * SCROLL_LINE, MB_MENU_MARK);
9068       break;
9069
9070     case SCREEN_CTRL_ID_SCROLL_DOWN:
9071       if (game_status == GAME_MODE_LEVELS)
9072         HandleChooseLevelSet(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
9073       else if (game_status == GAME_MODE_LEVELNR)
9074         HandleChooseLevelNr(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
9075       else if (game_status == GAME_MODE_SETUP)
9076         HandleSetupScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
9077       else if (game_status == GAME_MODE_INFO)
9078         HandleInfoScreen(0,0, 0, +1 * SCROLL_LINE, MB_MENU_MARK);
9079       break;
9080
9081     case SCREEN_CTRL_ID_SCROLL_VERTICAL:
9082       if (game_status == GAME_MODE_LEVELS)
9083         HandleChooseLevelSet(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
9084       else if (game_status == GAME_MODE_LEVELNR)
9085         HandleChooseLevelNr(0,0,999,gi->event.item_position,MB_MENU_INITIALIZE);
9086       else if (game_status == GAME_MODE_SETUP)
9087         HandleSetupScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
9088       else if (game_status == GAME_MODE_INFO)
9089         HandleInfoScreen(0,0, 999,gi->event.item_position,MB_MENU_INITIALIZE);
9090       break;
9091
9092     case SCREEN_CTRL_ID_NETWORK_SERVER:
9093     {
9094       if (!strEqual(gi->textinput.value, ""))
9095       {
9096         setString(&setup.network_server_hostname, gi->textinput.value);
9097
9098         network.server_host = setup.network_server_hostname;
9099       }
9100       else
9101       {
9102         setString(&setup.network_server_hostname, STR_NETWORK_AUTO_DETECT);
9103
9104         network.server_host = NULL;
9105       }
9106
9107       if (strEqual(network.server_host, STR_NETWORK_AUTO_DETECT))
9108         network.server_host = NULL;
9109
9110       execSetupGame_setNetworkServerText();
9111
9112       DrawSetupScreen();
9113
9114       break;
9115     }
9116
9117     default:
9118       break;
9119   }
9120 }
9121
9122 void DumpScreenIdentifiers(void)
9123 {
9124   int i;
9125
9126   Print("Active screen elements on current screen:\n");
9127
9128   for (i = 0; main_controls[i].nr != -1; i++)
9129   {
9130     struct MainControlInfo *mci = &main_controls[i];
9131
9132     if (mci->button_graphic != -1)
9133     {
9134       char *token = getTokenFromImageID(mci->button_graphic);
9135
9136       Print("- '%s'\n", token);
9137     }
9138   }
9139
9140   Print("Done.\n");
9141 }
9142
9143 boolean DoScreenAction(int image_id)
9144 {
9145   int i;
9146
9147   if (game_status != GAME_MODE_MAIN)
9148     return FALSE;
9149
9150   for (i = 0; main_controls[i].nr != -1; i++)
9151   {
9152     struct MainControlInfo *mci = &main_controls[i];
9153     struct MenuPosInfo *pos = mci->pos_button;
9154
9155     if (mci->button_graphic == image_id)
9156     {
9157       int x = mSX + pos->x;
9158       int y = mSY + pos->y;
9159
9160       HandleMainMenu(x, y, 0, 0, MB_MENU_CHOICE);
9161
9162       return TRUE;
9163     }
9164   }
9165
9166   return FALSE;
9167 }
9168
9169 void DrawScreenAfterAddingSet(char *tree_subdir_new, int tree_type)
9170 {
9171   // get tree info node of newly added level or artwork set
9172   TreeInfo *tree_node_first = TREE_FIRST_NODE(tree_type);
9173   TreeInfo *tree_node_new = getTreeInfoFromIdentifier(tree_node_first,
9174                                                       tree_subdir_new);
9175   if (tree_node_new == NULL)    // should not happen
9176     return;
9177
9178   // if request dialog is active, do nothing
9179   if (game.request_active)
9180     return;
9181
9182   if (game_status == GAME_MODE_MAIN &&
9183       tree_type == TREE_TYPE_LEVEL_DIR)
9184   {
9185     // when adding new level set in main menu, select it as current level set
9186
9187     // change current level set to newly added level set from zip file
9188     leveldir_current = tree_node_new;
9189
9190     // change current level number to first level of newly added level set
9191     level_nr = leveldir_current->first_level;
9192
9193     // redraw screen to reflect changed level set
9194     DrawMainMenu();
9195
9196     // save this level set and level number as last selected level set
9197     SaveLevelSetup_LastSeries();
9198     SaveLevelSetup_SeriesInfo();
9199   }
9200   else if (game_status == GAME_MODE_LEVELS &&
9201            tree_type == TREE_TYPE_LEVEL_DIR)
9202   {
9203     // when adding new level set in level set menu, set cursor and update screen
9204
9205     leveldir_current = tree_node_new;
9206
9207     DrawChooseTree(&leveldir_current);
9208   }
9209   else if (game_status == GAME_MODE_SETUP)
9210   {
9211     // when adding new artwork set in setup menu, set cursor and update screen
9212
9213     if (setup_mode == SETUP_MODE_CHOOSE_GRAPHICS &&
9214         tree_type == TREE_TYPE_GRAPHICS_DIR)
9215     {
9216       artwork.gfx_current = tree_node_new;
9217
9218       DrawChooseTree(&artwork.gfx_current);
9219     }
9220     else if (setup_mode == SETUP_MODE_CHOOSE_SOUNDS &&
9221              tree_type == TREE_TYPE_SOUNDS_DIR)
9222     {
9223       artwork.snd_current = tree_node_new;
9224
9225       DrawChooseTree(&artwork.snd_current);
9226     }
9227     else if (setup_mode == SETUP_MODE_CHOOSE_MUSIC &&
9228              tree_type == TREE_TYPE_MUSIC_DIR)
9229     {
9230       artwork.mus_current = tree_node_new;
9231
9232       DrawChooseTree(&artwork.mus_current);
9233     }
9234   }
9235 }