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