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