added support for game cycle delay in BD engine to level editor
[rocksndiamonds.git] / src / editor.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 // editor.c
10 // ============================================================================
11
12 #include <math.h>
13
14 #include "libgame/libgame.h"
15
16 #include "editor.h"
17 #include "screens.h"
18 #include "tools.h"
19 #include "files.h"
20 #include "game.h"
21 #include "init.h"
22 #include "tape.h"
23
24
25 #define INFOTEXT_UNKNOWN_ELEMENT        "unknown"
26
27
28 // ----------------------------------------------------------------------------
29 // screen and artwork graphic pixel position definitions
30 // ----------------------------------------------------------------------------
31
32 // values for the control window
33 #define ED_CTRL1_BUTTONS_HORIZ          4       // toolbox
34 #define ED_CTRL1_BUTTONS_VERT           4
35 #define ED_CTRL2_BUTTONS_HORIZ          3       // level
36 #define ED_CTRL2_BUTTONS_VERT           2
37 #define ED_CTRL3_BUTTONS_HORIZ          3       // CE and GE
38 #define ED_CTRL3_BUTTONS_VERT           1
39 #define ED_CTRL4_BUTTONS_HORIZ          2       // CE and GE
40 #define ED_CTRL4_BUTTONS_VERT           1
41 #define ED_CTRL5_BUTTONS_HORIZ          1       // properties
42 #define ED_CTRL5_BUTTONS_VERT           1
43 #define ED_CTRL6_BUTTONS_HORIZ          3       // properties
44 #define ED_CTRL6_BUTTONS_VERT           1
45 #define ED_CTRL7_BUTTONS_HORIZ          1       // palette
46 #define ED_CTRL7_BUTTONS_VERT           1
47
48 #define ED_NUM_CTRL1_BUTTONS   (ED_CTRL1_BUTTONS_HORIZ * ED_CTRL1_BUTTONS_VERT)
49 #define ED_NUM_CTRL2_BUTTONS   (ED_CTRL2_BUTTONS_HORIZ * ED_CTRL2_BUTTONS_VERT)
50 #define ED_NUM_CTRL3_BUTTONS   (ED_CTRL3_BUTTONS_HORIZ * ED_CTRL3_BUTTONS_VERT)
51 #define ED_NUM_CTRL4_BUTTONS   (ED_CTRL4_BUTTONS_HORIZ * ED_CTRL4_BUTTONS_VERT)
52 #define ED_NUM_CTRL5_BUTTONS   (ED_CTRL5_BUTTONS_HORIZ * ED_CTRL5_BUTTONS_VERT)
53 #define ED_NUM_CTRL6_BUTTONS   (ED_CTRL6_BUTTONS_HORIZ * ED_CTRL6_BUTTONS_VERT)
54 #define ED_NUM_CTRL7_BUTTONS   (ED_CTRL7_BUTTONS_HORIZ * ED_CTRL7_BUTTONS_VERT)
55 #define ED_NUM_CTRL1_2_BUTTONS (ED_NUM_CTRL1_BUTTONS   + ED_NUM_CTRL2_BUTTONS)
56 #define ED_NUM_CTRL1_3_BUTTONS (ED_NUM_CTRL1_2_BUTTONS + ED_NUM_CTRL3_BUTTONS)
57 #define ED_NUM_CTRL1_4_BUTTONS (ED_NUM_CTRL1_3_BUTTONS + ED_NUM_CTRL4_BUTTONS)
58 #define ED_NUM_CTRL1_5_BUTTONS (ED_NUM_CTRL1_4_BUTTONS + ED_NUM_CTRL5_BUTTONS)
59 #define ED_NUM_CTRL1_6_BUTTONS (ED_NUM_CTRL1_5_BUTTONS + ED_NUM_CTRL6_BUTTONS)
60 #define ED_NUM_CTRL1_7_BUTTONS (ED_NUM_CTRL1_6_BUTTONS + ED_NUM_CTRL7_BUTTONS)
61 #define ED_NUM_CTRL_BUTTONS    ED_NUM_CTRL1_7_BUTTONS
62
63 // values for the element list
64 #define ED_ELEMENTLIST_XPOS             (editor.palette.x)
65 #define ED_ELEMENTLIST_YPOS             (editor.palette.y)
66 #define ED_ELEMENTLIST_XSIZE            (graphic_info[IMG_EDITOR_PALETTE_BUTTON].width)
67 #define ED_ELEMENTLIST_YSIZE            (graphic_info[IMG_EDITOR_PALETTE_BUTTON].height)
68 #define ED_ELEMENTLIST_COLS             MAX(1, editor.palette.cols)
69 #define ED_ELEMENTLIST_ROWS             MAX(1, editor.palette.rows)
70 #define ED_ELEMENTLIST_BUTTONS_HORIZ    (ED_ELEMENTLIST_COLS)
71 #define ED_ELEMENTLIST_BUTTONS_VERT     (ED_ELEMENTLIST_ROWS)
72 #define ED_NUM_ELEMENTLIST_BUTTONS      (ED_ELEMENTLIST_BUTTONS_HORIZ * \
73                                          ED_ELEMENTLIST_BUTTONS_VERT)
74
75 // standard distances
76 #define ED_GADGET_NORMAL_DISTANCE       (editor.gadget.normal_spacing)
77 #define ED_GADGET_SMALL_DISTANCE        (editor.gadget.small_spacing)
78 #define ED_GADGET_TINY_DISTANCE         (editor.gadget.tiny_spacing)
79 #define ED_GADGET_LINE_DISTANCE         (editor.gadget.line_spacing)
80 #define ED_GADGET_TEXT_DISTANCE         (editor.gadget.text_spacing)
81 #define ED_TAB_BAR_HEIGHT               (editor.gadget.separator_line.height)
82 #define ED_DRAWINGAREA_TEXT_DISTANCE    (ED_GADGET_TEXT_DISTANCE +      \
83                                          ED_DRAWINGAREA_BORDER_SIZE)
84 #define ED_GADGET_SPACE_DISTANCE        (getFontWidth(FONT_TEXT_1))
85
86 // values for drawingarea gadgets
87 #define IMG_BORDER_1                    IMG_EDITOR_ELEMENT_BORDER
88 #define IMG_BORDER_2                    IMG_EDITOR_ELEMENT_BORDER_INPUT
89 #define ED_ELEMENT_BORDER               (graphic_info[IMG_BORDER_1].border_size)
90 #define ED_DRAWINGAREA_BORDER_SIZE      (graphic_info[IMG_BORDER_2].border_size)
91 #define ED_DRAWINGAREA_TILE_SIZE        (editor.drawingarea.tile_size)
92
93 // values for checkbutton gadgets
94 #define ED_CHECKBUTTON_XSIZE         (graphic_info[IMG_EDITOR_CHECKBOX].width)
95 #define ED_CHECKBUTTON_YSIZE         (graphic_info[IMG_EDITOR_CHECKBOX].height)
96
97 #define ED_TABBUTTON_XSIZE           (graphic_info[IMG_EDITOR_TABBUTTON].width)
98 #define ED_TABBUTTON_YSIZE           (graphic_info[IMG_EDITOR_TABBUTTON].height)
99
100 #define ED_SETTINGS_LEVEL_TABS_X        (editor.settings.tabs.x)
101 #define ED_SETTINGS_LEVEL_TABS_Y        (editor.settings.tabs.y)
102 #define ED_SETTINGS_ELEMENT_TABS_X      (editor.settings.tabs.x)
103 #define ED_SETTINGS_ELEMENT_TABS_Y      (editor.settings.tabs.y +       \
104                                          editor.settings.tabs.yoffset2)
105
106 #define ED_SETTINGS_TABS_XOFFSET        (editor.settings.tabs.draw_xoffset)
107 #define ED_SETTINGS_TABS_YOFFSET        (editor.settings.tabs.draw_yoffset)
108
109 #define ED_LEVEL_TABS_XSTART            (ED_SETTINGS_LEVEL_TABS_X)
110 #define ED_LEVEL_TABS_YSTART            (ED_SETTINGS_LEVEL_TABS_Y)
111 #define ED_LEVEL_SETTINGS_XSTART        (ED_SETTINGS_LEVEL_TABS_X +     \
112                                          ED_SETTINGS_TABS_XOFFSET)
113 #define ED_LEVEL_SETTINGS_YSTART        (ED_SETTINGS_LEVEL_TABS_Y +     \
114                                          ED_TABBUTTON_YSIZE +           \
115                                          ED_GADGET_TINY_DISTANCE +      \
116                                          ED_TAB_BAR_HEIGHT +            \
117                                          ED_SETTINGS_TABS_YOFFSET +     \
118                                          getFontHeight(FONT_TEXT_1) +   \
119                                          ED_GADGET_TEXT_DISTANCE)
120 #define ED_ELEMENT_TABS_XSTART          (ED_SETTINGS_ELEMENT_TABS_X)
121 #define ED_ELEMENT_TABS_YSTART          (ED_SETTINGS_ELEMENT_TABS_Y)
122 #define ED_ELEMENT_SETTINGS_XSTART      (ED_SETTINGS_ELEMENT_TABS_X +   \
123                                          ED_SETTINGS_TABS_XOFFSET)
124 #define ED_ELEMENT_SETTINGS_YSTART      (ED_SETTINGS_ELEMENT_TABS_Y +   \
125                                          ED_TABBUTTON_YSIZE +           \
126                                          ED_GADGET_TINY_DISTANCE +      \
127                                          ED_TAB_BAR_HEIGHT +            \
128                                          ED_SETTINGS_TABS_YOFFSET)
129
130 #define ED_SETTINGS_XOFFSET             (ED_CHECKBUTTON_XSIZE +         \
131                                          ED_GADGET_TEXT_DISTANCE)
132 #define ED_SETTINGS_YOFFSET             (ED_CHECKBUTTON_YSIZE +         \
133                                          ED_GADGET_LINE_DISTANCE)
134
135 #define ED_POS_RANGE                    (10000)
136 #define ED_POS_LEVEL_TABS_FIRST         (1 * ED_POS_RANGE)
137 #define ED_POS_LEVEL_TABS_LAST          (2 * ED_POS_RANGE - 1)
138 #define ED_POS_LEVEL_SETTINGS_FIRST     (2 * ED_POS_RANGE)
139 #define ED_POS_LEVEL_SETTINGS_LAST      (3 * ED_POS_RANGE - 1)
140 #define ED_POS_ELEMENT_TABS_FIRST       (3 * ED_POS_RANGE)
141 #define ED_POS_ELEMENT_TABS_LAST        (4 * ED_POS_RANGE - 1)
142 #define ED_POS_ELEMENT_SETTINGS_FIRST   (4 * ED_POS_RANGE)
143 #define ED_POS_ELEMENT_SETTINGS_LAST    (5 * ED_POS_RANGE - 1)
144
145 #define ED_LEVEL_TABS_XPOS(n)           (ED_POS_LEVEL_TABS_FIRST + (n))
146 #define ED_LEVEL_TABS_YPOS(n)           (ED_POS_LEVEL_TABS_FIRST + (n))
147
148 #define ED_LEVEL_SETTINGS_XPOS(n)       (ED_POS_LEVEL_SETTINGS_FIRST + (n))
149 #define ED_LEVEL_SETTINGS_YPOS(n)       (ED_POS_LEVEL_SETTINGS_FIRST + (n))
150
151 #define ED_ELEMENT_TABS_XPOS(n)         (ED_POS_ELEMENT_TABS_FIRST + (n))
152 #define ED_ELEMENT_TABS_YPOS(n)         (ED_POS_ELEMENT_TABS_FIRST + (n))
153
154 #define ED_ELEMENT_SETTINGS_XPOS(n)     (ED_POS_ELEMENT_SETTINGS_FIRST + (n))
155 #define ED_ELEMENT_SETTINGS_YPOS(n)     (ED_POS_ELEMENT_SETTINGS_FIRST + (n))
156
157 #define IS_POS_LEVEL_TABS(n)          ((n) >= ED_POS_LEVEL_TABS_FIRST && \
158                                        (n) <= ED_POS_LEVEL_TABS_LAST)
159 #define IS_POS_LEVEL_SETTINGS(n)      ((n) >= ED_POS_LEVEL_SETTINGS_FIRST && \
160                                        (n) <= ED_POS_LEVEL_SETTINGS_LAST)
161 #define IS_POS_ELEMENT_TABS(n)        ((n) >= ED_POS_ELEMENT_TABS_FIRST && \
162                                        (n) <= ED_POS_ELEMENT_TABS_LAST)
163 #define IS_POS_ELEMENT_SETTINGS(n)    ((n) >= ED_POS_ELEMENT_SETTINGS_FIRST && \
164                                        (n) <= ED_POS_ELEMENT_SETTINGS_LAST)
165
166 #define ED_LEVEL_TABS_LINE(n)           ((n) - ED_POS_LEVEL_TABS_FIRST)
167 #define ED_LEVEL_SETTINGS_LINE(n)       ((n) - ED_POS_LEVEL_SETTINGS_FIRST)
168 #define ED_ELEMENT_TABS_LINE(n)         ((n) - ED_POS_ELEMENT_TABS_FIRST)
169 #define ED_ELEMENT_SETTINGS_LINE(n)     ((n) - ED_POS_ELEMENT_SETTINGS_FIRST)
170
171 #define ED_LEVEL_TABS_X(n)              (ED_LEVEL_TABS_XSTART + \
172                                          (n) * ED_SETTINGS_TABS_XOFFSET)
173 #define ED_LEVEL_TABS_Y(n)              (ED_LEVEL_TABS_YSTART + \
174                                          (n) * ED_SETTINGS_TABS_YOFFSET)
175
176 #define ED_LEVEL_SETTINGS_X(n)          (ED_LEVEL_SETTINGS_XSTART +     \
177                                          (n) * ED_SETTINGS_XOFFSET)
178 #define ED_LEVEL_SETTINGS_Y(n)          (ED_LEVEL_SETTINGS_YSTART +     \
179                                          (n) * ED_SETTINGS_YOFFSET)
180
181 #define ED_ELEMENT_TABS_X(n)            (ED_ELEMENT_TABS_XSTART +       \
182                                          (n) * ED_SETTINGS_TABS_XOFFSET)
183 #define ED_ELEMENT_TABS_Y(n)            (ED_ELEMENT_TABS_YSTART +       \
184                                          (n) * ED_SETTINGS_TABS_YOFFSET)
185
186 #define ED_ELEMENT_SETTINGS_X(n)        (ED_ELEMENT_SETTINGS_XSTART +   \
187                                          (n) * ED_SETTINGS_XOFFSET)
188 #define ED_ELEMENT_SETTINGS_Y(n)        (ED_ELEMENT_SETTINGS_YSTART +   \
189                                          (n) * ED_SETTINGS_YOFFSET)
190
191 #define ED_POS_TO_LEVEL_TABS_X(n)       \
192   (ED_LEVEL_TABS_X(ED_LEVEL_TABS_LINE(n)))
193 #define ED_POS_TO_LEVEL_TABS_Y(n)       \
194   (ED_LEVEL_TABS_Y(ED_LEVEL_TABS_LINE(n)))
195
196 #define ED_POS_TO_LEVEL_SETTINGS_X(n)   \
197   (ED_LEVEL_SETTINGS_X(ED_LEVEL_SETTINGS_LINE(n)))
198 #define ED_POS_TO_LEVEL_SETTINGS_Y(n)   \
199   (ED_LEVEL_SETTINGS_Y(ED_LEVEL_SETTINGS_LINE(n)))
200
201 #define ED_POS_TO_ELEMENT_TABS_X(n)     \
202   (ED_ELEMENT_TABS_X(ED_ELEMENT_TABS_LINE(n)))
203 #define ED_POS_TO_ELEMENT_TABS_Y(n)     \
204   (ED_ELEMENT_TABS_Y(ED_ELEMENT_TABS_LINE(n)))
205
206 #define ED_POS_TO_ELEMENT_SETTINGS_X(n) \
207   (ED_ELEMENT_SETTINGS_X(ED_ELEMENT_SETTINGS_LINE(n)))
208 #define ED_POS_TO_ELEMENT_SETTINGS_Y(n) \
209   (ED_ELEMENT_SETTINGS_Y(ED_ELEMENT_SETTINGS_LINE(n)))
210
211 #define ED_SETTINGS_X(n)                (IS_POS_LEVEL_TABS(n) ? \
212                                          ED_POS_TO_LEVEL_TABS_X(n) : \
213                                          IS_POS_LEVEL_SETTINGS(n) ?     \
214                                          ED_POS_TO_LEVEL_SETTINGS_X(n) : \
215                                          IS_POS_ELEMENT_TABS(n) ?       \
216                                          ED_POS_TO_ELEMENT_TABS_X(n) : \
217                                          IS_POS_ELEMENT_SETTINGS(n) ?   \
218                                          ED_POS_TO_ELEMENT_SETTINGS_X(n) : (n))
219 #define ED_SETTINGS_Y(n)                (IS_POS_LEVEL_TABS(n) ? \
220                                          ED_POS_TO_LEVEL_TABS_Y(n) : \
221                                          IS_POS_LEVEL_SETTINGS(n) ?     \
222                                          ED_POS_TO_LEVEL_SETTINGS_Y(n) : \
223                                          IS_POS_ELEMENT_TABS(n) ?       \
224                                          ED_POS_TO_ELEMENT_TABS_Y(n) : \
225                                          IS_POS_ELEMENT_SETTINGS(n) ?   \
226                                          ED_POS_TO_ELEMENT_SETTINGS_Y(n) : (n))
227
228 #define ED_SETTINGS_XOFF(n)             (5 * ((n) % 4) *                \
229                                          ED_DRAWINGAREA_TILE_SIZE)
230 #define ED_SETTINGS_YOFF(n)             (5 * ((n) / 4) *                \
231                                          ED_DRAWINGAREA_TILE_SIZE)
232
233 #define ED_AREA_XOFFSET_1(n)            ((n) != 0 ?                     \
234                                          ED_DRAWINGAREA_BORDER_SIZE : 0)
235 #define ED_AREA_YOFFSET_1(n)            ((n) != 0 ?                     \
236                                          (ED_CHECKBUTTON_YSIZE -        \
237                                           ED_DRAWINGAREA_TILE_SIZE) / 2 : 0)
238
239 #define ED_AREA_XOFFSET_2(n)      (0)
240 #define ED_AREA_YOFFSET_2(n)      ((n) == 3 ?                   \
241                                    ((n) - 1) * ED_DRAWINGAREA_TILE_SIZE / 2 : 0)
242
243 #define ED_AREA_SETTINGS_X(i)      (ED_SETTINGS_X((i).x) +              \
244                                     ED_SETTINGS_XOFF((i).xoffset) +     \
245                                     ED_AREA_XOFFSET_1((i).x) -          \
246                                     ED_AREA_XOFFSET_2((i).area_xsize))
247 #define ED_AREA_SETTINGS_Y(i)      (ED_SETTINGS_Y((i).y) +              \
248                                     ED_SETTINGS_YOFF((i).yoffset) +     \
249                                     ED_AREA_YOFFSET_1((i).y) -          \
250                                     ED_AREA_YOFFSET_2((i).area_ysize))
251
252 // values for element content drawing areas
253 #define ED_AREA_1X1_LSETTINGS_XPOS(n)   ED_LEVEL_SETTINGS_XPOS(n)
254 #define ED_AREA_1X1_LSETTINGS_YPOS(n)   ED_LEVEL_SETTINGS_YPOS(n)
255 #define ED_AREA_1X1_LSETTINGS_XOFF      (0)
256 #define ED_AREA_1X1_LSETTINGS_YOFF      (0)
257
258 #define ED_AREA_1X1_SETTINGS_XPOS(n)    ED_ELEMENT_SETTINGS_XPOS(n)
259 #define ED_AREA_1X1_SETTINGS_YPOS(n)    ED_ELEMENT_SETTINGS_YPOS(n)
260 #define ED_AREA_1X1_SETTINGS_XOFF       (0)
261 #define ED_AREA_1X1_SETTINGS_YOFF       (0)
262
263 #define ED_AREA_3X3_SETTINGS_XPOS(n)    ED_ELEMENT_SETTINGS_XPOS(n)
264 #define ED_AREA_3X3_SETTINGS_YPOS(n)    ED_ELEMENT_SETTINGS_YPOS(n)
265 #define ED_AREA_3X3_SETTINGS_XOFF       (0)
266 #define ED_AREA_3X3_SETTINGS_YOFF       (0)
267
268 // element content
269 #define ED_AREA_ELEMENT_CONTENT_XOFF(n) (n)
270 #define ED_AREA_ELEMENT_CONTENT_YOFF(n) (n)
271
272 // yamyam content
273 #define ED_XPOS_YAM                     0
274 #define ED_YPOS_YAM                     5
275 #define ED_AREA_YAMYAM_CONTENT_XPOS     ED_ELEMENT_SETTINGS_XPOS(ED_XPOS_YAM)
276 #define ED_AREA_YAMYAM_CONTENT_YPOS     ED_ELEMENT_SETTINGS_YPOS(ED_YPOS_YAM)
277 #define ED_AREA_YAMYAM_CONTENT_XOFF(n)  ED_AREA_ELEMENT_CONTENT_XOFF(n)
278 #define ED_AREA_YAMYAM_CONTENT_YOFF(n)  ED_AREA_ELEMENT_CONTENT_YOFF(n)
279 #define ED_AREA_YAMYAM_CONTENT_X(n)     (ED_ELEMENT_SETTINGS_X(ED_XPOS_YAM) + \
280                                          ED_SETTINGS_XOFF(n))
281 #define ED_AREA_YAMYAM_CONTENT_Y(n)     (ED_ELEMENT_SETTINGS_Y(ED_YPOS_YAM) + \
282                                          ED_SETTINGS_YOFF(n) +          \
283                                          ED_AREA_YOFFSET_1(ED_YPOS_YAM) - \
284                                          ED_AREA_YOFFSET_2(3))
285
286 // magic ball content
287 #define ED_XPOS_BALL                    0
288 #define ED_YPOS_BALL                    6
289 #define ED_AREA_MAGIC_BALL_CONTENT_XPOS ED_ELEMENT_SETTINGS_XPOS(ED_XPOS_BALL)
290 #define ED_AREA_MAGIC_BALL_CONTENT_YPOS ED_ELEMENT_SETTINGS_YPOS(ED_YPOS_BALL)
291 #define ED_AREA_MAGIC_BALL_CONTENT_XOFF(n) ED_AREA_ELEMENT_CONTENT_XOFF(n)
292 #define ED_AREA_MAGIC_BALL_CONTENT_YOFF(n) ED_AREA_ELEMENT_CONTENT_YOFF(n)
293 #define ED_AREA_MAGIC_BALL_CONTENT_X(n) (ED_ELEMENT_SETTINGS_X(ED_XPOS_BALL) + \
294                                          ED_SETTINGS_XOFF(n))
295 #define ED_AREA_MAGIC_BALL_CONTENT_Y(n) (ED_ELEMENT_SETTINGS_Y(ED_YPOS_BALL) + \
296                                          ED_SETTINGS_YOFF(n) +          \
297                                          ED_AREA_YOFFSET_1(ED_YPOS_BALL) - \
298                                          ED_AREA_YOFFSET_2(3))
299
300 // values for scrolling gadgets for drawing area
301 #define ED_SCROLLBUTTON_XSIZE           (graphic_info[IMG_EDITOR_PLAYFIELD_SCROLLBAR].width)
302 #define ED_SCROLLBUTTON_YSIZE           (graphic_info[IMG_EDITOR_PLAYFIELD_SCROLLBAR].height)
303
304 #define ED_SCROLL_UP_XPOS               (SXSIZE - ED_SCROLLBUTTON_XSIZE)
305 #define ED_SCROLL_UP_YPOS               (0)
306 #define ED_SCROLL_DOWN_XPOS             ED_SCROLL_UP_XPOS
307 #define ED_SCROLL_DOWN_YPOS             (SYSIZE - 3 * ED_SCROLLBUTTON_YSIZE)
308 #define ED_SCROLL_LEFT_XPOS             (0)
309 #define ED_SCROLL_LEFT_YPOS             (SYSIZE - 2 * ED_SCROLLBUTTON_YSIZE)
310 #define ED_SCROLL_RIGHT_XPOS            (SXSIZE - 2 * ED_SCROLLBUTTON_XSIZE)
311 #define ED_SCROLL_RIGHT_YPOS            ED_SCROLL_LEFT_YPOS
312 #define ED_SCROLL_HORIZONTAL_XPOS       (ED_SCROLL_LEFT_XPOS +          \
313                                          ED_SCROLLBUTTON_XSIZE)
314 #define ED_SCROLL_HORIZONTAL_YPOS       ED_SCROLL_LEFT_YPOS
315 #define ED_SCROLL_HORIZONTAL_XSIZE      (SXSIZE - 3 * ED_SCROLLBUTTON_XSIZE)
316 #define ED_SCROLL_HORIZONTAL_YSIZE      ED_SCROLLBUTTON_YSIZE
317 #define ED_SCROLL_VERTICAL_XPOS         ED_SCROLL_UP_XPOS
318 #define ED_SCROLL_VERTICAL_YPOS         (ED_SCROLL_UP_YPOS +            \
319                                          ED_SCROLLBUTTON_YSIZE)
320 #define ED_SCROLL_VERTICAL_XSIZE        ED_SCROLLBUTTON_XSIZE
321 #define ED_SCROLL_VERTICAL_YSIZE        (SYSIZE - 4 * ED_SCROLLBUTTON_YSIZE)
322
323 // values for scrolling gadgets for element list
324 #define ED_SCROLLBUTTON2_XSIZE          (graphic_info[IMG_EDITOR_PALETTE_SCROLL_UP].width)
325 #define ED_SCROLLBUTTON2_YSIZE          (graphic_info[IMG_EDITOR_PALETTE_SCROLL_UP].height)
326
327 #define ED_SCROLL2_UP_XPOS              (ED_ELEMENTLIST_XPOS +          \
328                                          ED_ELEMENTLIST_BUTTONS_HORIZ * \
329                                          ED_ELEMENTLIST_XSIZE)
330 #define ED_SCROLL2_UP_YPOS              ED_ELEMENTLIST_YPOS
331 #define ED_SCROLL2_DOWN_XPOS            ED_SCROLL2_UP_XPOS
332 #define ED_SCROLL2_DOWN_YPOS            (ED_SCROLL2_UP_YPOS +           \
333                                          ED_ELEMENTLIST_BUTTONS_VERT *  \
334                                          ED_ELEMENTLIST_YSIZE -         \
335                                          ED_SCROLLBUTTON2_YSIZE)
336 #define ED_SCROLL2_VERTICAL_XPOS        ED_SCROLL2_UP_XPOS
337 #define ED_SCROLL2_VERTICAL_YPOS        (ED_SCROLL2_UP_YPOS +           \
338                                          ED_SCROLLBUTTON2_YSIZE)
339 #define ED_SCROLL2_VERTICAL_XSIZE       ED_SCROLLBUTTON2_XSIZE
340 #define ED_SCROLL2_VERTICAL_YSIZE       (ED_ELEMENTLIST_BUTTONS_VERT *  \
341                                          ED_ELEMENTLIST_YSIZE -         \
342                                          2 * ED_SCROLLBUTTON2_YSIZE)
343
344 // values for ClearEditorGadgetInfoText() and HandleEditorGadgetInfoText()
345 #define INFOTEXT_FONT           FONT_TEXT_2
346 #define INFOTEXT_XSIZE          SXSIZE
347 #define INFOTEXT_YSIZE          getFontHeight(INFOTEXT_FONT)
348 #define INFOTEXT_YSIZE_FULL     (INFOTEXT_YSIZE + ED_GADGET_SMALL_DISTANCE)
349 #define INFOTEXT_X              (editor.settings.tooltip.x)
350 #define INFOTEXT_Y              (editor.settings.tooltip.y)
351 #define INFOTEXT_XY_REDEFINED   (INFOTEXT_X != -1 || INFOTEXT_Y != -1)
352 #define INFOTEXT_XPOS           SX + (INFOTEXT_XY_REDEFINED ? INFOTEXT_X : 0)
353 #define INFOTEXT_YPOS           SY + (INFOTEXT_XY_REDEFINED ? INFOTEXT_Y : \
354                                       SYSIZE - INFOTEXT_YSIZE)
355
356
357 // ----------------------------------------------------------------------------
358 // editor gadget definitions
359 // ----------------------------------------------------------------------------
360
361 enum
362 {
363   GADGET_ID_NONE = -1,
364
365   // drawing toolbox buttons
366
367   GADGET_ID_SINGLE_ITEMS,
368   GADGET_ID_CONNECTED_ITEMS,
369   GADGET_ID_LINE,
370   GADGET_ID_ARC,
371   GADGET_ID_RECTANGLE,
372   GADGET_ID_FILLED_BOX,
373   GADGET_ID_WRAP_UP,
374   GADGET_ID_TEXT,
375   GADGET_ID_FLOOD_FILL,
376   GADGET_ID_WRAP_LEFT,
377   GADGET_ID_ZOOM,
378   GADGET_ID_WRAP_RIGHT,
379   GADGET_ID_RANDOM_PLACEMENT,
380   GADGET_ID_GRAB_BRUSH,
381   GADGET_ID_WRAP_DOWN,
382   GADGET_ID_PICK_ELEMENT,
383
384   GADGET_ID_UNDO,
385   GADGET_ID_CONF,
386   GADGET_ID_SAVE,
387   GADGET_ID_CLEAR,
388   GADGET_ID_TEST,
389   GADGET_ID_EXIT,
390
391   GADGET_ID_CUSTOM_COPY_FROM,
392   GADGET_ID_CUSTOM_COPY_TO,
393   GADGET_ID_CUSTOM_EXCHANGE,
394   GADGET_ID_CUSTOM_COPY,
395   GADGET_ID_CUSTOM_PASTE,
396
397   GADGET_ID_PROPERTIES,
398   GADGET_ID_ELEMENT_LEFT,
399   GADGET_ID_ELEMENT_MIDDLE,
400   GADGET_ID_ELEMENT_RIGHT,
401   GADGET_ID_PALETTE,
402
403   // counter gadget identifiers
404
405   GADGET_ID_SELECT_LEVEL_DOWN,
406   GADGET_ID_SELECT_LEVEL_TEXT,
407   GADGET_ID_SELECT_LEVEL_UP,
408   GADGET_ID_LEVEL_XSIZE_DOWN,
409   GADGET_ID_LEVEL_XSIZE_TEXT,
410   GADGET_ID_LEVEL_XSIZE_UP,
411   GADGET_ID_LEVEL_YSIZE_DOWN,
412   GADGET_ID_LEVEL_YSIZE_TEXT,
413   GADGET_ID_LEVEL_YSIZE_UP,
414   GADGET_ID_LEVEL_RANDOM_DOWN,
415   GADGET_ID_LEVEL_RANDOM_TEXT,
416   GADGET_ID_LEVEL_RANDOM_UP,
417   GADGET_ID_LEVEL_GEMSLIMIT_DOWN,
418   GADGET_ID_LEVEL_GEMSLIMIT_TEXT,
419   GADGET_ID_LEVEL_GEMSLIMIT_UP,
420   GADGET_ID_LEVEL_TIMELIMIT_DOWN,
421   GADGET_ID_LEVEL_TIMELIMIT_TEXT,
422   GADGET_ID_LEVEL_TIMELIMIT_UP,
423   GADGET_ID_LEVEL_TIMESCORE_DOWN,
424   GADGET_ID_LEVEL_TIMESCORE_TEXT,
425   GADGET_ID_LEVEL_TIMESCORE_UP,
426   GADGET_ID_LEVEL_RANDOM_SEED_DOWN,
427   GADGET_ID_LEVEL_RANDOM_SEED_TEXT,
428   GADGET_ID_LEVEL_RANDOM_SEED_UP,
429   GADGET_ID_LEVELSET_NUM_LEVELS_DOWN,
430   GADGET_ID_LEVELSET_NUM_LEVELS_TEXT,
431   GADGET_ID_LEVELSET_NUM_LEVELS_UP,
432   GADGET_ID_BD_CYCLE_DELAY_MS_DOWN,
433   GADGET_ID_BD_CYCLE_DELAY_MS_TEXT,
434   GADGET_ID_BD_CYCLE_DELAY_MS_UP,
435   GADGET_ID_BD_CYCLE_DELAY_C64_DOWN,
436   GADGET_ID_BD_CYCLE_DELAY_C64_TEXT,
437   GADGET_ID_BD_CYCLE_DELAY_C64_UP,
438   GADGET_ID_ELEMENT_VALUE1_DOWN,
439   GADGET_ID_ELEMENT_VALUE1_TEXT,
440   GADGET_ID_ELEMENT_VALUE1_UP,
441   GADGET_ID_ELEMENT_VALUE2_DOWN,
442   GADGET_ID_ELEMENT_VALUE2_TEXT,
443   GADGET_ID_ELEMENT_VALUE2_UP,
444   GADGET_ID_ELEMENT_VALUE3_DOWN,
445   GADGET_ID_ELEMENT_VALUE3_TEXT,
446   GADGET_ID_ELEMENT_VALUE3_UP,
447   GADGET_ID_ELEMENT_VALUE4_DOWN,
448   GADGET_ID_ELEMENT_VALUE4_TEXT,
449   GADGET_ID_ELEMENT_VALUE4_UP,
450   GADGET_ID_YAMYAM_CONTENT_DOWN,
451   GADGET_ID_YAMYAM_CONTENT_TEXT,
452   GADGET_ID_YAMYAM_CONTENT_UP,
453   GADGET_ID_BALL_CONTENT_DOWN,
454   GADGET_ID_BALL_CONTENT_TEXT,
455   GADGET_ID_BALL_CONTENT_UP,
456   GADGET_ID_ANDROID_CONTENT_DOWN,
457   GADGET_ID_ANDROID_CONTENT_TEXT,
458   GADGET_ID_ANDROID_CONTENT_UP,
459   GADGET_ID_ENVELOPE_XSIZE_DOWN,
460   GADGET_ID_ENVELOPE_XSIZE_TEXT,
461   GADGET_ID_ENVELOPE_XSIZE_UP,
462   GADGET_ID_ENVELOPE_YSIZE_DOWN,
463   GADGET_ID_ENVELOPE_YSIZE_TEXT,
464   GADGET_ID_ENVELOPE_YSIZE_UP,
465   GADGET_ID_INVENTORY_SIZE_DOWN,
466   GADGET_ID_INVENTORY_SIZE_TEXT,
467   GADGET_ID_INVENTORY_SIZE_UP,
468   GADGET_ID_MM_BALL_CONTENT_DOWN,
469   GADGET_ID_MM_BALL_CONTENT_TEXT,
470   GADGET_ID_MM_BALL_CONTENT_UP,
471   GADGET_ID_CUSTOM_SCORE_DOWN,
472   GADGET_ID_CUSTOM_SCORE_TEXT,
473   GADGET_ID_CUSTOM_SCORE_UP,
474   GADGET_ID_CUSTOM_GEMCOUNT_DOWN,
475   GADGET_ID_CUSTOM_GEMCOUNT_TEXT,
476   GADGET_ID_CUSTOM_GEMCOUNT_UP,
477   GADGET_ID_CUSTOM_VALUE_FIX_DOWN,
478   GADGET_ID_CUSTOM_VALUE_FIX_TEXT,
479   GADGET_ID_CUSTOM_VALUE_FIX_UP,
480   GADGET_ID_CUSTOM_VALUE_RND_DOWN,
481   GADGET_ID_CUSTOM_VALUE_RND_TEXT,
482   GADGET_ID_CUSTOM_VALUE_RND_UP,
483   GADGET_ID_PUSH_DELAY_FIX_DOWN,
484   GADGET_ID_PUSH_DELAY_FIX_TEXT,
485   GADGET_ID_PUSH_DELAY_FIX_UP,
486   GADGET_ID_PUSH_DELAY_RND_DOWN,
487   GADGET_ID_PUSH_DELAY_RND_TEXT,
488   GADGET_ID_PUSH_DELAY_RND_UP,
489   GADGET_ID_DROP_DELAY_FIX_DOWN,
490   GADGET_ID_DROP_DELAY_FIX_TEXT,
491   GADGET_ID_DROP_DELAY_FIX_UP,
492   GADGET_ID_DROP_DELAY_RND_DOWN,
493   GADGET_ID_DROP_DELAY_RND_TEXT,
494   GADGET_ID_DROP_DELAY_RND_UP,
495   GADGET_ID_MOVE_DELAY_FIX_DOWN,
496   GADGET_ID_MOVE_DELAY_FIX_TEXT,
497   GADGET_ID_MOVE_DELAY_FIX_UP,
498   GADGET_ID_MOVE_DELAY_RND_DOWN,
499   GADGET_ID_MOVE_DELAY_RND_TEXT,
500   GADGET_ID_MOVE_DELAY_RND_UP,
501   GADGET_ID_STEP_DELAY_FIX_DOWN,
502   GADGET_ID_STEP_DELAY_FIX_TEXT,
503   GADGET_ID_STEP_DELAY_FIX_UP,
504   GADGET_ID_STEP_DELAY_RND_DOWN,
505   GADGET_ID_STEP_DELAY_RND_TEXT,
506   GADGET_ID_STEP_DELAY_RND_UP,
507   GADGET_ID_EXPLOSION_DELAY_DOWN,
508   GADGET_ID_EXPLOSION_DELAY_TEXT,
509   GADGET_ID_EXPLOSION_DELAY_UP,
510   GADGET_ID_IGNITION_DELAY_DOWN,
511   GADGET_ID_IGNITION_DELAY_TEXT,
512   GADGET_ID_IGNITION_DELAY_UP,
513   GADGET_ID_CHANGE_DELAY_FIX_DOWN,
514   GADGET_ID_CHANGE_DELAY_FIX_TEXT,
515   GADGET_ID_CHANGE_DELAY_FIX_UP,
516   GADGET_ID_CHANGE_DELAY_RND_DOWN,
517   GADGET_ID_CHANGE_DELAY_RND_TEXT,
518   GADGET_ID_CHANGE_DELAY_RND_UP,
519   GADGET_ID_CHANGE_CONT_RND_DOWN,
520   GADGET_ID_CHANGE_CONT_RND_TEXT,
521   GADGET_ID_CHANGE_CONT_RND_UP,
522   GADGET_ID_GROUP_CONTENT_DOWN,
523   GADGET_ID_GROUP_CONTENT_TEXT,
524   GADGET_ID_GROUP_CONTENT_UP,
525
526   // drawing area identifiers
527
528   GADGET_ID_DRAWING_LEVEL,
529   GADGET_ID_YAMYAM_CONTENT_0,
530   GADGET_ID_YAMYAM_CONTENT_1,
531   GADGET_ID_YAMYAM_CONTENT_2,
532   GADGET_ID_YAMYAM_CONTENT_3,
533   GADGET_ID_YAMYAM_CONTENT_4,
534   GADGET_ID_YAMYAM_CONTENT_5,
535   GADGET_ID_YAMYAM_CONTENT_6,
536   GADGET_ID_YAMYAM_CONTENT_7,
537   GADGET_ID_MAGIC_BALL_CONTENT_0,
538   GADGET_ID_MAGIC_BALL_CONTENT_1,
539   GADGET_ID_MAGIC_BALL_CONTENT_2,
540   GADGET_ID_MAGIC_BALL_CONTENT_3,
541   GADGET_ID_MAGIC_BALL_CONTENT_4,
542   GADGET_ID_MAGIC_BALL_CONTENT_5,
543   GADGET_ID_MAGIC_BALL_CONTENT_6,
544   GADGET_ID_MAGIC_BALL_CONTENT_7,
545   GADGET_ID_ANDROID_CONTENT,
546   GADGET_ID_AMOEBA_CONTENT,
547   GADGET_ID_START_ELEMENT,
548   GADGET_ID_ARTWORK_ELEMENT,
549   GADGET_ID_EXPLOSION_ELEMENT,
550   GADGET_ID_INVENTORY_CONTENT,
551   GADGET_ID_MM_BALL_CONTENT,
552   GADGET_ID_CUSTOM_GRAPHIC,
553   GADGET_ID_CUSTOM_CONTENT,
554   GADGET_ID_CUSTOM_MOVE_ENTER,
555   GADGET_ID_CUSTOM_MOVE_LEAVE,
556   GADGET_ID_CUSTOM_CHANGE_TARGET,
557   GADGET_ID_CUSTOM_CHANGE_CONTENT,
558   GADGET_ID_CUSTOM_CHANGE_TRIGGER,
559   GADGET_ID_CUSTOM_CHANGE_ACTION,
560   GADGET_ID_GROUP_CONTENT,
561   GADGET_ID_RANDOM_BACKGROUND,
562
563   // text input identifiers
564
565   GADGET_ID_LEVEL_NAME,
566   GADGET_ID_LEVEL_AUTHOR,
567   GADGET_ID_LEVELSET_NAME,
568   GADGET_ID_LEVELSET_AUTHOR,
569   GADGET_ID_ELEMENT_NAME,
570
571   // text area identifiers
572
573   GADGET_ID_ENVELOPE_INFO,
574
575   // selectbox identifiers
576
577   GADGET_ID_TIME_OR_STEPS,
578   GADGET_ID_TIME_SCORE_BASE,
579   GADGET_ID_GAME_ENGINE_TYPE,
580   GADGET_ID_BD_SCHEDULING_TYPE,
581   GADGET_ID_LEVELSET_SAVE_MODE,
582   GADGET_ID_WIND_DIRECTION,
583   GADGET_ID_PLAYER_SPEED,
584   GADGET_ID_MM_BALL_CHOICE_MODE,
585   GADGET_ID_CUSTOM_WALK_TO_ACTION,
586   GADGET_ID_CUSTOM_EXPLOSION_TYPE,
587   GADGET_ID_CUSTOM_DEADLINESS,
588   GADGET_ID_CUSTOM_MOVE_PATTERN,
589   GADGET_ID_CUSTOM_MOVE_DIRECTION,
590   GADGET_ID_CUSTOM_MOVE_STEPSIZE,
591   GADGET_ID_CUSTOM_MOVE_LEAVE_TYPE,
592   GADGET_ID_CUSTOM_SMASH_TARGETS,
593   GADGET_ID_CUSTOM_SLIPPERY_TYPE,
594   GADGET_ID_CUSTOM_ACCESS_TYPE,
595   GADGET_ID_CUSTOM_ACCESS_LAYER,
596   GADGET_ID_CUSTOM_ACCESS_PROTECTED,
597   GADGET_ID_CUSTOM_ACCESS_DIRECTION,
598   GADGET_ID_CHANGE_TIME_UNITS,
599   GADGET_ID_CHANGE_DIRECT_ACTION,
600   GADGET_ID_CHANGE_OTHER_ACTION,
601   GADGET_ID_CHANGE_SIDE,
602   GADGET_ID_CHANGE_PLAYER,
603   GADGET_ID_CHANGE_PAGE,
604   GADGET_ID_CHANGE_REPLACE_WHEN,
605   GADGET_ID_ACTION_TYPE,
606   GADGET_ID_ACTION_MODE,
607   GADGET_ID_ACTION_ARG,
608   GADGET_ID_SELECT_CHANGE_PAGE,
609   GADGET_ID_GROUP_CHOICE_MODE,
610
611   // textbutton identifiers
612
613   GADGET_ID_LEVELCONFIG_LEVEL,
614   GADGET_ID_LEVELCONFIG_LEVELSET,
615   GADGET_ID_LEVELCONFIG_EDITOR,
616   GADGET_ID_LEVELCONFIG_ENGINE,
617   GADGET_ID_PROPERTIES_INFO,
618   GADGET_ID_PROPERTIES_CONFIG,
619   GADGET_ID_PROPERTIES_CONFIG_1,
620   GADGET_ID_PROPERTIES_CONFIG_2,
621   GADGET_ID_PROPERTIES_CHANGE,
622   GADGET_ID_SAVE_AS_TEMPLATE_1,
623   GADGET_ID_SAVE_AS_TEMPLATE_2,
624   GADGET_ID_SAVE_LEVELSET,
625   GADGET_ID_ADD_CHANGE_PAGE,
626   GADGET_ID_DEL_CHANGE_PAGE,
627
628   // graphicbutton identifiers
629
630   GADGET_ID_PREV_CHANGE_PAGE,
631   GADGET_ID_NEXT_CHANGE_PAGE,
632   GADGET_ID_COPY_CHANGE_PAGE,
633   GADGET_ID_PASTE_CHANGE_PAGE,
634
635   // gadgets for scrolling of drawing area
636
637   GADGET_ID_SCROLL_UP,
638   GADGET_ID_SCROLL_DOWN,
639   GADGET_ID_SCROLL_LEFT,
640   GADGET_ID_SCROLL_RIGHT,
641   GADGET_ID_SCROLL_HORIZONTAL,
642   GADGET_ID_SCROLL_VERTICAL,
643
644   // gadgets for scrolling element list
645
646   GADGET_ID_SCROLL_LIST_UP,
647   GADGET_ID_SCROLL_LIST_DOWN,
648   GADGET_ID_SCROLL_LIST_VERTICAL,
649
650   // checkbuttons/radiobuttons for level/element properties
651
652   GADGET_ID_AUTO_COUNT_GEMS,
653   GADGET_ID_RATE_TIME_OVER_SCORE,
654   GADGET_ID_USE_LEVELSET_ARTWORK,
655   GADGET_ID_COPY_LEVEL_TEMPLATE,
656   GADGET_ID_RANDOM_PERCENTAGE,
657   GADGET_ID_RANDOM_QUANTITY,
658   GADGET_ID_RANDOM_RESTRICTED,
659   GADGET_ID_BD_INTERMISSION,
660   GADGET_ID_BD_PAL_TIMING,
661   GADGET_ID_STICK_ELEMENT,
662   GADGET_ID_EM_SLIPPERY_GEMS,
663   GADGET_ID_EM_EXPLODES_BY_FIRE,
664   GADGET_ID_USE_SPRING_BUG,
665   GADGET_ID_USE_TIME_ORB_BUG,
666   GADGET_ID_USE_LIFE_BUGS,
667   GADGET_ID_RANDOM_BALL_CONTENT,
668   GADGET_ID_INITIAL_BALL_ACTIVE,
669   GADGET_ID_GROW_INTO_DIGGABLE,
670   GADGET_ID_SB_FIELDS_NEEDED,
671   GADGET_ID_SB_OBJECTS_NEEDED,
672   GADGET_ID_AUTO_EXIT_SOKOBAN,
673   GADGET_ID_SOLVED_BY_ONE_PLAYER,
674   GADGET_ID_FINISH_DIG_COLLECT,
675   GADGET_ID_KEEP_WALKABLE_CE,
676   GADGET_ID_CONTINUOUS_SNAPPING,
677   GADGET_ID_BLOCK_SNAP_FIELD,
678   GADGET_ID_BLOCK_LAST_FIELD,
679   GADGET_ID_SP_BLOCK_LAST_FIELD,
680   GADGET_ID_INSTANT_RELOCATION,
681   GADGET_ID_SHIFTED_RELOCATION,
682   GADGET_ID_LAZY_RELOCATION,
683   GADGET_ID_USE_START_ELEMENT,
684   GADGET_ID_USE_ARTWORK_ELEMENT,
685   GADGET_ID_USE_EXPLOSION_ELEMENT,
686   GADGET_ID_INITIAL_GRAVITY,
687   GADGET_ID_USE_INITIAL_INVENTORY,
688   GADGET_ID_CAN_PASS_TO_WALKABLE,
689   GADGET_ID_CAN_FALL_INTO_ACID,
690   GADGET_ID_CAN_MOVE_INTO_ACID,
691   GADGET_ID_DONT_COLLIDE_WITH,
692   GADGET_ID_BD_DIAGONAL_MOVEMENTS,
693   GADGET_ID_ENVELOPE_AUTOWRAP,
694   GADGET_ID_ENVELOPE_CENTERED,
695   GADGET_ID_MM_LASER_RED,
696   GADGET_ID_MM_LASER_GREEN,
697   GADGET_ID_MM_LASER_BLUE,
698   GADGET_ID_DF_LASER_RED,
699   GADGET_ID_DF_LASER_GREEN,
700   GADGET_ID_DF_LASER_BLUE,
701   GADGET_ID_ROTATE_MM_BALL_CONTENT,
702   GADGET_ID_EXPLODE_MM_BALL,
703   GADGET_ID_CUSTOM_INDESTRUCTIBLE,
704   GADGET_ID_CUSTOM_CAN_EXPLODE,
705   GADGET_ID_CUSTOM_EXPLODE_FIRE,
706   GADGET_ID_CUSTOM_EXPLODE_SMASH,
707   GADGET_ID_CUSTOM_EXPLODE_IMPACT,
708   GADGET_ID_CUSTOM_WALK_TO_OBJECT,
709   GADGET_ID_CUSTOM_DEADLY,
710   GADGET_ID_CUSTOM_CAN_MOVE,
711   GADGET_ID_CUSTOM_CAN_FALL,
712   GADGET_ID_CUSTOM_CAN_SMASH,
713   GADGET_ID_CUSTOM_SLIPPERY,
714   GADGET_ID_CUSTOM_ACCESSIBLE,
715   GADGET_ID_CUSTOM_GRAV_REACHABLE,
716   GADGET_ID_CUSTOM_USE_LAST_VALUE,
717   GADGET_ID_CUSTOM_USE_GRAPHIC,
718   GADGET_ID_CUSTOM_USE_TEMPLATE_1,
719   GADGET_ID_CUSTOM_USE_TEMPLATE_2,
720   GADGET_ID_CUSTOM_USE_TEMPLATE_3,
721   GADGET_ID_CUSTOM_CAN_CHANGE,
722   GADGET_ID_CHANGE_USE_CONTENT,
723   GADGET_ID_CHANGE_USE_EXPLOSION,
724   GADGET_ID_CHANGE_ONLY_COMPLETE,
725   GADGET_ID_CHANGE_USE_RANDOM,
726   GADGET_ID_CHANGE_HAS_ACTION,
727   GADGET_ID_CHANGE_DELAY,
728   GADGET_ID_CHANGE_BY_DIRECT_ACT,
729   GADGET_ID_CHANGE_BY_OTHER_ACT,
730
731   NUM_STATIC_GADGET_IDS
732 };
733
734 // gadgets for buttons in element list (dynamic)
735 #define GADGET_ID_ELEMENTLIST_FIRST     (NUM_STATIC_GADGET_IDS)
736 #define GADGET_ID_ELEMENTLIST_LAST      (GADGET_ID_ELEMENTLIST_FIRST +  \
737                                         ED_NUM_ELEMENTLIST_BUTTONS - 1)
738
739 #define NUM_EDITOR_GADGETS              (GADGET_ID_ELEMENTLIST_LAST + 1)
740
741 // radio button numbers
742 enum
743 {
744   RADIO_NR_NONE,
745   RADIO_NR_DRAWING_TOOLBOX,
746   RADIO_NR_RANDOM_ELEMENTS
747 };
748
749 // values for counter gadgets
750 enum
751 {
752   ED_COUNTER_ID_SELECT_LEVEL,
753   ED_COUNTER_ID_LEVEL_XSIZE,
754   ED_COUNTER_ID_LEVEL_YSIZE,
755   ED_COUNTER_ID_LEVEL_GEMSLIMIT,
756   ED_COUNTER_ID_LEVEL_TIMELIMIT,
757   ED_COUNTER_ID_LEVEL_TIMESCORE,
758   ED_COUNTER_ID_LEVEL_RANDOM_SEED,
759   ED_COUNTER_ID_LEVELSET_NUM_LEVELS,
760   ED_COUNTER_ID_LEVEL_RANDOM,
761   ED_COUNTER_ID_BD_CYCLE_DELAY_MS,
762   ED_COUNTER_ID_BD_CYCLE_DELAY_C64,
763   ED_COUNTER_ID_ELEMENT_VALUE1,
764   ED_COUNTER_ID_ELEMENT_VALUE2,
765   ED_COUNTER_ID_ELEMENT_VALUE3,
766   ED_COUNTER_ID_ELEMENT_VALUE4,
767   ED_COUNTER_ID_YAMYAM_CONTENT,
768   ED_COUNTER_ID_BALL_CONTENT,
769   ED_COUNTER_ID_ANDROID_CONTENT,
770   ED_COUNTER_ID_ENVELOPE_XSIZE,
771   ED_COUNTER_ID_ENVELOPE_YSIZE,
772   ED_COUNTER_ID_INVENTORY_SIZE,
773   ED_COUNTER_ID_MM_BALL_CONTENT,
774   ED_COUNTER_ID_CUSTOM_SCORE,
775   ED_COUNTER_ID_CUSTOM_GEMCOUNT,
776   ED_COUNTER_ID_CUSTOM_VALUE_FIX,
777   ED_COUNTER_ID_CUSTOM_VALUE_RND,
778   ED_COUNTER_ID_PUSH_DELAY_FIX,
779   ED_COUNTER_ID_PUSH_DELAY_RND,
780   ED_COUNTER_ID_DROP_DELAY_FIX,
781   ED_COUNTER_ID_DROP_DELAY_RND,
782   ED_COUNTER_ID_MOVE_DELAY_FIX,
783   ED_COUNTER_ID_MOVE_DELAY_RND,
784   ED_COUNTER_ID_STEP_DELAY_FIX,
785   ED_COUNTER_ID_STEP_DELAY_RND,
786   ED_COUNTER_ID_EXPLOSION_DELAY,
787   ED_COUNTER_ID_IGNITION_DELAY,
788   ED_COUNTER_ID_GROUP_CONTENT,
789   ED_COUNTER_ID_CHANGE_DELAY_FIX,
790   ED_COUNTER_ID_CHANGE_DELAY_RND,
791   ED_COUNTER_ID_CHANGE_CONT_RND,
792
793   ED_NUM_COUNTERBUTTONS
794 };
795
796 #define ED_COUNTER_ID_LEVEL_FIRST       ED_COUNTER_ID_LEVEL_XSIZE
797 #define ED_COUNTER_ID_LEVEL_LAST        ED_COUNTER_ID_LEVEL_RANDOM_SEED
798 #define ED_COUNTER_ID_LEVELSET_FIRST    ED_COUNTER_ID_LEVELSET_NUM_LEVELS
799 #define ED_COUNTER_ID_LEVELSET_LAST     ED_COUNTER_ID_LEVELSET_NUM_LEVELS
800 #define ED_COUNTER_ID_EDITOR_FIRST      ED_COUNTER_ID_LEVEL_RANDOM
801 #define ED_COUNTER_ID_EDITOR_LAST       ED_COUNTER_ID_LEVEL_RANDOM
802
803 #define ED_COUNTER_ID_CUSTOM1_FIRST     ED_COUNTER_ID_CUSTOM_SCORE
804 #define ED_COUNTER_ID_CUSTOM1_LAST      ED_COUNTER_ID_DROP_DELAY_RND
805 #define ED_COUNTER_ID_CUSTOM2_FIRST     ED_COUNTER_ID_MOVE_DELAY_FIX
806 #define ED_COUNTER_ID_CUSTOM2_LAST      ED_COUNTER_ID_IGNITION_DELAY
807 #define ED_COUNTER_ID_CUSTOM_FIRST      ED_COUNTER_ID_CUSTOM1_FIRST
808 #define ED_COUNTER_ID_CUSTOM_LAST       ED_COUNTER_ID_CUSTOM2_LAST
809
810 #define ED_COUNTER_ID_CHANGE_FIRST      ED_COUNTER_ID_CHANGE_DELAY_FIX
811 #define ED_COUNTER_ID_CHANGE_LAST       ED_COUNTER_ID_CHANGE_CONT_RND
812
813 // values for scrollbutton gadgets
814 enum
815 {
816   ED_SCROLLBUTTON_ID_AREA_UP,
817   ED_SCROLLBUTTON_ID_AREA_DOWN,
818   ED_SCROLLBUTTON_ID_AREA_LEFT,
819   ED_SCROLLBUTTON_ID_AREA_RIGHT,
820   ED_SCROLLBUTTON_ID_LIST_UP,
821   ED_SCROLLBUTTON_ID_LIST_DOWN,
822
823   ED_NUM_SCROLLBUTTONS
824 };
825
826 #define ED_SCROLLBUTTON_ID_AREA_FIRST   ED_SCROLLBUTTON_ID_AREA_UP
827 #define ED_SCROLLBUTTON_ID_AREA_LAST    ED_SCROLLBUTTON_ID_AREA_RIGHT
828
829 // values for scrollbar gadgets
830 enum
831 {
832   ED_SCROLLBAR_ID_AREA_HORIZONTAL,
833   ED_SCROLLBAR_ID_AREA_VERTICAL,
834   ED_SCROLLBAR_ID_LIST_VERTICAL,
835
836   ED_NUM_SCROLLBARS
837 };
838
839 #define ED_SCROLLBAR_ID_AREA_FIRST      ED_SCROLLBAR_ID_AREA_HORIZONTAL
840 #define ED_SCROLLBAR_ID_AREA_LAST       ED_SCROLLBAR_ID_AREA_VERTICAL
841
842 // values for text input gadgets
843 enum
844 {
845   ED_TEXTINPUT_ID_LEVEL_NAME,
846   ED_TEXTINPUT_ID_LEVEL_AUTHOR,
847   ED_TEXTINPUT_ID_LEVELSET_NAME,
848   ED_TEXTINPUT_ID_LEVELSET_AUTHOR,
849   ED_TEXTINPUT_ID_ELEMENT_NAME,
850
851   ED_NUM_TEXTINPUT
852 };
853
854 #define ED_TEXTINPUT_ID_LEVEL_FIRST     ED_TEXTINPUT_ID_LEVEL_NAME
855 #define ED_TEXTINPUT_ID_LEVEL_LAST      ED_TEXTINPUT_ID_LEVEL_AUTHOR
856
857 #define ED_TEXTINPUT_ID_LEVELSET_FIRST  ED_TEXTINPUT_ID_LEVELSET_NAME
858 #define ED_TEXTINPUT_ID_LEVELSET_LAST   ED_TEXTINPUT_ID_LEVELSET_AUTHOR
859
860 // values for text area gadgets
861 enum
862 {
863   ED_TEXTAREA_ID_ENVELOPE_INFO,
864
865   ED_NUM_TEXTAREAS
866 };
867
868 #define ED_TEXTAREA_ID_LEVEL_FIRST      ED_TEXTAREA_ID_ENVELOPE
869 #define ED_TEXTAREA_ID_LEVEL_LAST       ED_TEXTAREA_ID_ENVELOPE
870
871 // values for selectbox gadgets
872 enum
873 {
874   ED_SELECTBOX_ID_TIME_OR_STEPS,
875   ED_SELECTBOX_ID_TIME_SCORE_BASE,
876   ED_SELECTBOX_ID_GAME_ENGINE_TYPE,
877   ED_SELECTBOX_ID_BD_SCHEDULING_TYPE,
878   ED_SELECTBOX_ID_LEVELSET_SAVE_MODE,
879   ED_SELECTBOX_ID_WIND_DIRECTION,
880   ED_SELECTBOX_ID_PLAYER_SPEED,
881   ED_SELECTBOX_ID_MM_BALL_CHOICE_MODE,
882   ED_SELECTBOX_ID_CUSTOM_ACCESS_TYPE,
883   ED_SELECTBOX_ID_CUSTOM_ACCESS_LAYER,
884   ED_SELECTBOX_ID_CUSTOM_ACCESS_PROTECTED,
885   ED_SELECTBOX_ID_CUSTOM_ACCESS_DIRECTION,
886   ED_SELECTBOX_ID_CUSTOM_WALK_TO_ACTION,
887   ED_SELECTBOX_ID_CUSTOM_MOVE_PATTERN,
888   ED_SELECTBOX_ID_CUSTOM_MOVE_DIRECTION,
889   ED_SELECTBOX_ID_CUSTOM_MOVE_STEPSIZE,
890   ED_SELECTBOX_ID_CUSTOM_MOVE_LEAVE_TYPE,
891   ED_SELECTBOX_ID_CUSTOM_SMASH_TARGETS,
892   ED_SELECTBOX_ID_CUSTOM_SLIPPERY_TYPE,
893   ED_SELECTBOX_ID_CUSTOM_DEADLINESS,
894   ED_SELECTBOX_ID_CUSTOM_EXPLOSION_TYPE,
895   ED_SELECTBOX_ID_CHANGE_TIME_UNITS,
896   ED_SELECTBOX_ID_CHANGE_DIRECT_ACTION,
897   ED_SELECTBOX_ID_CHANGE_OTHER_ACTION,
898   ED_SELECTBOX_ID_CHANGE_SIDE,
899   ED_SELECTBOX_ID_CHANGE_PLAYER,
900   ED_SELECTBOX_ID_CHANGE_PAGE,
901   ED_SELECTBOX_ID_CHANGE_REPLACE_WHEN,
902   ED_SELECTBOX_ID_ACTION_TYPE,
903   ED_SELECTBOX_ID_ACTION_MODE,
904   ED_SELECTBOX_ID_ACTION_ARG,
905   ED_SELECTBOX_ID_SELECT_CHANGE_PAGE,
906   ED_SELECTBOX_ID_GROUP_CHOICE_MODE,
907
908   ED_NUM_SELECTBOX
909 };
910
911 #define ED_SELECTBOX_ID_LEVEL_FIRST     ED_SELECTBOX_ID_TIME_OR_STEPS
912 #define ED_SELECTBOX_ID_LEVEL_LAST      ED_SELECTBOX_ID_GAME_ENGINE_TYPE
913
914 #define ED_SELECTBOX_ID_LEVELSET_FIRST  ED_SELECTBOX_ID_LEVELSET_SAVE_MODE
915 #define ED_SELECTBOX_ID_LEVELSET_LAST   ED_SELECTBOX_ID_LEVELSET_SAVE_MODE
916
917 #define ED_SELECTBOX_ID_ENGINE_FIRST    ED_SELECTBOX_ID_BD_SCHEDULING_TYPE
918 #define ED_SELECTBOX_ID_ENGINE_LAST     ED_SELECTBOX_ID_BD_SCHEDULING_TYPE
919
920 #define ED_SELECTBOX_ID_CUSTOM1_FIRST   ED_SELECTBOX_ID_CUSTOM_ACCESS_TYPE
921 #define ED_SELECTBOX_ID_CUSTOM1_LAST    ED_SELECTBOX_ID_CUSTOM_WALK_TO_ACTION
922 #define ED_SELECTBOX_ID_CUSTOM2_FIRST   ED_SELECTBOX_ID_CUSTOM_MOVE_PATTERN
923 #define ED_SELECTBOX_ID_CUSTOM2_LAST    ED_SELECTBOX_ID_CUSTOM_EXPLOSION_TYPE
924 #define ED_SELECTBOX_ID_CUSTOM_FIRST    ED_SELECTBOX_ID_CUSTOM1_FIRST
925 #define ED_SELECTBOX_ID_CUSTOM_LAST     ED_SELECTBOX_ID_CUSTOM2_LAST
926
927 #define ED_SELECTBOX_ID_CHANGE_FIRST    ED_SELECTBOX_ID_CHANGE_TIME_UNITS
928 #define ED_SELECTBOX_ID_CHANGE_LAST     ED_SELECTBOX_ID_SELECT_CHANGE_PAGE
929
930 // values for textbutton gadgets
931 enum
932 {
933   ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL,
934   ED_TEXTBUTTON_ID_LEVELCONFIG_LEVELSET,
935   ED_TEXTBUTTON_ID_LEVELCONFIG_EDITOR,
936   ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE,
937   ED_TEXTBUTTON_ID_PROPERTIES_INFO,
938   ED_TEXTBUTTON_ID_PROPERTIES_CONFIG,
939   ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_1,
940   ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2,
941   ED_TEXTBUTTON_ID_PROPERTIES_CHANGE,
942   ED_TEXTBUTTON_ID_SAVE_LEVELSET,
943   ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2,
944   ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1,
945   ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE,
946   ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE,
947
948   ED_NUM_TEXTBUTTONS
949 };
950
951 #define ED_TAB_BUTTON_ID_LEVELCONFIG_FIRST ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL
952 #define ED_TAB_BUTTON_ID_LEVELCONFIG_LAST  ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE
953
954 #define ED_TAB_BUTTON_ID_PROPERTIES_FIRST ED_TEXTBUTTON_ID_PROPERTIES_INFO
955 #define ED_TAB_BUTTON_ID_PROPERTIES_LAST  ED_TEXTBUTTON_ID_PROPERTIES_CHANGE
956
957 #define ED_TEXTBUTTON_ID_CHANGE_FIRST   ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE
958 #define ED_TEXTBUTTON_ID_CHANGE_LAST    ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE
959
960 // values for graphicbutton gadgets
961 enum
962 {
963   ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE,
964   ED_GRAPHICBUTTON_ID_NEXT_CHANGE_PAGE,
965   ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE,
966   ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE,
967
968   ED_NUM_GRAPHICBUTTONS
969 };
970
971 #define ED_GRAPHICBUTTON_ID_CHANGE_FIRST  ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE
972 #define ED_GRAPHICBUTTON_ID_CHANGE_LAST   ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE
973
974 // values for checkbutton gadgets
975 enum
976 {
977   ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS,
978   ED_CHECKBUTTON_ID_RATE_TIME_OVER_SCORE,
979   ED_CHECKBUTTON_ID_USE_LEVELSET_ARTWORK,
980   ED_CHECKBUTTON_ID_COPY_LEVEL_TEMPLATE,
981   ED_CHECKBUTTON_ID_RANDOM_RESTRICTED,
982   ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_3,
983   ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2,
984   ED_CHECKBUTTON_ID_BD_INTERMISSION,
985   ED_CHECKBUTTON_ID_BD_PAL_TIMING,
986   ED_CHECKBUTTON_ID_STICK_ELEMENT,
987   ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS,
988   ED_CHECKBUTTON_ID_EM_EXPLODES_BY_FIRE,
989   ED_CHECKBUTTON_ID_USE_SPRING_BUG,
990   ED_CHECKBUTTON_ID_USE_TIME_ORB_BUG,
991   ED_CHECKBUTTON_ID_USE_LIFE_BUGS,
992   ED_CHECKBUTTON_ID_RANDOM_BALL_CONTENT,
993   ED_CHECKBUTTON_ID_INITIAL_BALL_ACTIVE,
994   ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE,
995   ED_CHECKBUTTON_ID_SB_FIELDS_NEEDED,
996   ED_CHECKBUTTON_ID_SB_OBJECTS_NEEDED,
997   ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN,
998   ED_CHECKBUTTON_ID_SOLVED_BY_ONE_PLAYER,
999   ED_CHECKBUTTON_ID_FINISH_DIG_COLLECT,
1000   ED_CHECKBUTTON_ID_KEEP_WALKABLE_CE,
1001   ED_CHECKBUTTON_ID_CONTINUOUS_SNAPPING,
1002   ED_CHECKBUTTON_ID_BLOCK_SNAP_FIELD,
1003   ED_CHECKBUTTON_ID_BLOCK_LAST_FIELD,
1004   ED_CHECKBUTTON_ID_SP_BLOCK_LAST_FIELD,
1005   ED_CHECKBUTTON_ID_INSTANT_RELOCATION,
1006   ED_CHECKBUTTON_ID_SHIFTED_RELOCATION,
1007   ED_CHECKBUTTON_ID_LAZY_RELOCATION,
1008   ED_CHECKBUTTON_ID_USE_START_ELEMENT,
1009   ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT,
1010   ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT,
1011   ED_CHECKBUTTON_ID_INITIAL_GRAVITY,
1012   ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY,
1013   ED_CHECKBUTTON_ID_CAN_PASS_TO_WALKABLE,
1014   ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID,
1015   ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID,
1016   ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH,
1017   ED_CHECKBUTTON_ID_BD_DIAGONAL_MOVEMENTS,
1018   ED_CHECKBUTTON_ID_ENVELOPE_AUTOWRAP,
1019   ED_CHECKBUTTON_ID_ENVELOPE_CENTERED,
1020   ED_CHECKBUTTON_ID_MM_LASER_RED,
1021   ED_CHECKBUTTON_ID_MM_LASER_GREEN,
1022   ED_CHECKBUTTON_ID_MM_LASER_BLUE,
1023   ED_CHECKBUTTON_ID_DF_LASER_RED,
1024   ED_CHECKBUTTON_ID_DF_LASER_GREEN,
1025   ED_CHECKBUTTON_ID_DF_LASER_BLUE,
1026   ED_CHECKBUTTON_ID_ROTATE_MM_BALL_CONTENT,
1027   ED_CHECKBUTTON_ID_EXPLODE_MM_BALL,
1028   ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC,
1029   ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1,
1030   ED_CHECKBUTTON_ID_CUSTOM_ACCESSIBLE,
1031   ED_CHECKBUTTON_ID_CUSTOM_GRAV_REACHABLE,
1032   ED_CHECKBUTTON_ID_CUSTOM_USE_LAST_VALUE,
1033   ED_CHECKBUTTON_ID_CUSTOM_WALK_TO_OBJECT,
1034   ED_CHECKBUTTON_ID_CUSTOM_INDESTRUCTIBLE,
1035   ED_CHECKBUTTON_ID_CUSTOM_CAN_MOVE,
1036   ED_CHECKBUTTON_ID_CUSTOM_CAN_FALL,
1037   ED_CHECKBUTTON_ID_CUSTOM_CAN_SMASH,
1038   ED_CHECKBUTTON_ID_CUSTOM_SLIPPERY,
1039   ED_CHECKBUTTON_ID_CUSTOM_DEADLY,
1040   ED_CHECKBUTTON_ID_CUSTOM_CAN_EXPLODE,
1041   ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_FIRE,
1042   ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_SMASH,
1043   ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_IMPACT,
1044   ED_CHECKBUTTON_ID_CUSTOM_CAN_CHANGE,
1045   ED_CHECKBUTTON_ID_CHANGE_DELAY,
1046   ED_CHECKBUTTON_ID_CHANGE_BY_DIRECT_ACT,
1047   ED_CHECKBUTTON_ID_CHANGE_BY_OTHER_ACT,
1048   ED_CHECKBUTTON_ID_CHANGE_USE_EXPLOSION,
1049   ED_CHECKBUTTON_ID_CHANGE_USE_CONTENT,
1050   ED_CHECKBUTTON_ID_CHANGE_ONLY_COMPLETE,
1051   ED_CHECKBUTTON_ID_CHANGE_USE_RANDOM,
1052   ED_CHECKBUTTON_ID_CHANGE_HAS_ACTION,
1053
1054   ED_NUM_CHECKBUTTONS
1055 };
1056
1057 #define ED_CHECKBUTTON_ID_LEVEL_FIRST   ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS
1058 #define ED_CHECKBUTTON_ID_LEVEL_LAST    ED_CHECKBUTTON_ID_RATE_TIME_OVER_SCORE
1059
1060 #define ED_CHECKBUTTON_ID_LEVELSET_FIRST ED_CHECKBUTTON_ID_USE_LEVELSET_ARTWORK
1061 #define ED_CHECKBUTTON_ID_LEVELSET_LAST  ED_CHECKBUTTON_ID_COPY_LEVEL_TEMPLATE
1062
1063 #define ED_CHECKBUTTON_ID_EDITOR_FIRST  ED_CHECKBUTTON_ID_RANDOM_RESTRICTED
1064 #define ED_CHECKBUTTON_ID_EDITOR_LAST   ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2
1065
1066 #define ED_CHECKBUTTON_ID_ENGINE_FIRST  ED_CHECKBUTTON_ID_BD_INTERMISSION
1067 #define ED_CHECKBUTTON_ID_ENGINE_LAST   ED_CHECKBUTTON_ID_BD_PAL_TIMING
1068
1069 #define ED_CHECKBUTTON_ID_CUSTOM1_FIRST ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC
1070 #define ED_CHECKBUTTON_ID_CUSTOM1_LAST  ED_CHECKBUTTON_ID_CUSTOM_INDESTRUCTIBLE
1071 #define ED_CHECKBUTTON_ID_CUSTOM2_FIRST ED_CHECKBUTTON_ID_CUSTOM_CAN_MOVE
1072 #define ED_CHECKBUTTON_ID_CUSTOM2_LAST  ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_IMPACT
1073 #define ED_CHECKBUTTON_ID_CUSTOM_FIRST  ED_CHECKBUTTON_ID_CUSTOM1_FIRST
1074 #define ED_CHECKBUTTON_ID_CUSTOM_LAST   ED_CHECKBUTTON_ID_CUSTOM2_LAST
1075
1076 #define ED_CHECKBUTTON_ID_CHANGE_FIRST  ED_CHECKBUTTON_ID_CUSTOM_CAN_CHANGE
1077 #define ED_CHECKBUTTON_ID_CHANGE_LAST   ED_CHECKBUTTON_ID_CHANGE_HAS_ACTION
1078
1079 // values for radiobutton gadgets
1080 enum
1081 {
1082   ED_RADIOBUTTON_ID_PERCENTAGE,
1083   ED_RADIOBUTTON_ID_QUANTITY,
1084
1085   ED_NUM_RADIOBUTTONS
1086 };
1087
1088 #define ED_RADIOBUTTON_ID_EDITOR_FIRST  ED_RADIOBUTTON_ID_PERCENTAGE
1089 #define ED_RADIOBUTTON_ID_EDITOR_LAST   ED_RADIOBUTTON_ID_QUANTITY
1090
1091 // values for drawing area gadgets
1092 enum
1093 {
1094   ED_DRAWING_ID_DRAWING_LEVEL,
1095   ED_DRAWING_ID_YAMYAM_CONTENT_0,
1096   ED_DRAWING_ID_YAMYAM_CONTENT_1,
1097   ED_DRAWING_ID_YAMYAM_CONTENT_2,
1098   ED_DRAWING_ID_YAMYAM_CONTENT_3,
1099   ED_DRAWING_ID_YAMYAM_CONTENT_4,
1100   ED_DRAWING_ID_YAMYAM_CONTENT_5,
1101   ED_DRAWING_ID_YAMYAM_CONTENT_6,
1102   ED_DRAWING_ID_YAMYAM_CONTENT_7,
1103   ED_DRAWING_ID_MAGIC_BALL_CONTENT_0,
1104   ED_DRAWING_ID_MAGIC_BALL_CONTENT_1,
1105   ED_DRAWING_ID_MAGIC_BALL_CONTENT_2,
1106   ED_DRAWING_ID_MAGIC_BALL_CONTENT_3,
1107   ED_DRAWING_ID_MAGIC_BALL_CONTENT_4,
1108   ED_DRAWING_ID_MAGIC_BALL_CONTENT_5,
1109   ED_DRAWING_ID_MAGIC_BALL_CONTENT_6,
1110   ED_DRAWING_ID_MAGIC_BALL_CONTENT_7,
1111   ED_DRAWING_ID_ANDROID_CONTENT,
1112   ED_DRAWING_ID_AMOEBA_CONTENT,
1113   ED_DRAWING_ID_START_ELEMENT,
1114   ED_DRAWING_ID_ARTWORK_ELEMENT,
1115   ED_DRAWING_ID_EXPLOSION_ELEMENT,
1116   ED_DRAWING_ID_INVENTORY_CONTENT,
1117   ED_DRAWING_ID_MM_BALL_CONTENT,
1118   ED_DRAWING_ID_CUSTOM_GRAPHIC,
1119   ED_DRAWING_ID_CUSTOM_CONTENT,
1120   ED_DRAWING_ID_CUSTOM_MOVE_ENTER,
1121   ED_DRAWING_ID_CUSTOM_MOVE_LEAVE,
1122   ED_DRAWING_ID_CUSTOM_CHANGE_TARGET,
1123   ED_DRAWING_ID_CUSTOM_CHANGE_CONTENT,
1124   ED_DRAWING_ID_CUSTOM_CHANGE_TRIGGER,
1125   ED_DRAWING_ID_CUSTOM_CHANGE_ACTION,
1126   ED_DRAWING_ID_GROUP_CONTENT,
1127   ED_DRAWING_ID_RANDOM_BACKGROUND,
1128
1129   ED_NUM_DRAWING_AREAS
1130 };
1131
1132 #define ED_DRAWING_ID_EDITOR_FIRST      ED_DRAWING_ID_RANDOM_BACKGROUND
1133 #define ED_DRAWING_ID_EDITOR_LAST       ED_DRAWING_ID_RANDOM_BACKGROUND
1134
1135
1136 // ----------------------------------------------------------------------------
1137 // some internally used definitions
1138 // ----------------------------------------------------------------------------
1139
1140 // values for CopyLevelToUndoBuffer()
1141 #define UNDO_IMMEDIATE                  0
1142 #define UNDO_ACCUMULATE                 1
1143
1144 // values for scrollbars
1145 #define ED_SCROLL_NO                    0
1146 #define ED_SCROLL_LEFT                  1
1147 #define ED_SCROLL_RIGHT                 2
1148 #define ED_SCROLL_UP                    4
1149 #define ED_SCROLL_DOWN                  8
1150
1151 // screens in the level editor
1152 #define ED_MODE_DRAWING                 0
1153 #define ED_MODE_LEVELCONFIG             1
1154 #define ED_MODE_PROPERTIES              2
1155 #define ED_MODE_PALETTE                 3
1156
1157 // sub-screens in the global settings section
1158 #define ED_MODE_LEVELCONFIG_LEVEL       ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL
1159 #define ED_MODE_LEVELCONFIG_LEVELSET    ED_TEXTBUTTON_ID_LEVELCONFIG_LEVELSET
1160 #define ED_MODE_LEVELCONFIG_EDITOR      ED_TEXTBUTTON_ID_LEVELCONFIG_EDITOR
1161 #define ED_MODE_LEVELCONFIG_ENGINE      ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE
1162
1163 // sub-screens in the element properties section
1164 #define ED_MODE_PROPERTIES_INFO         ED_TEXTBUTTON_ID_PROPERTIES_INFO
1165 #define ED_MODE_PROPERTIES_CONFIG       ED_TEXTBUTTON_ID_PROPERTIES_CONFIG
1166 #define ED_MODE_PROPERTIES_CONFIG_1     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_1
1167 #define ED_MODE_PROPERTIES_CONFIG_2     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2
1168 #define ED_MODE_PROPERTIES_CHANGE       ED_TEXTBUTTON_ID_PROPERTIES_CHANGE
1169
1170 // how many steps can be cancelled
1171 #define NUM_UNDO_STEPS                  (64 + 1)
1172
1173 // values for elements with score for certain actions
1174 #define MIN_SCORE                       0
1175 #define MAX_SCORE                       999
1176
1177 // values for elements with count for collecting
1178 #define MIN_COLLECT_COUNT               0
1179 #define MAX_COLLECT_COUNT               999
1180
1181 // values for random placement
1182 #define RANDOM_USE_PERCENTAGE           0
1183 #define RANDOM_USE_QUANTITY             1
1184
1185 // values for level set save mode
1186 #define LEVELSET_SAVE_MODE_UPDATE       0
1187 #define LEVELSET_SAVE_MODE_CREATE       1
1188
1189 // default value for element tile size in drawing area
1190 #define DEFAULT_EDITOR_TILESIZE         MINI_TILESIZE
1191 #define DEFAULT_EDITOR_TILESIZE_MM      TILESIZE
1192
1193
1194 // ----------------------------------------------------------------------------
1195 // some internally used data structure definitions
1196 // ----------------------------------------------------------------------------
1197
1198 static struct
1199 {
1200   int graphic;
1201   int gadget_id;
1202   struct XYTileSize *pos;
1203   int gadget_type;
1204   char *infotext;
1205   char shortcut;
1206 } controlbutton_info[ED_NUM_CTRL_BUTTONS] =
1207 {
1208   // note: some additional characters are already reserved for "cheat mode"
1209   // shortcuts (":XYZ" style) -- for details, see "events.c"
1210
1211   // ---------- toolbox control buttons ---------------------------------------
1212
1213   {
1214     IMG_GFX_EDITOR_BUTTON_DRAW_SINGLE,          GADGET_ID_SINGLE_ITEMS,
1215     &editor.button.draw_single,                 GD_TYPE_RADIO_BUTTON,
1216     "draw single items",                        's'
1217   },
1218   {
1219     IMG_GFX_EDITOR_BUTTON_DRAW_CONNECTED,       GADGET_ID_CONNECTED_ITEMS,
1220     &editor.button.draw_connected,              GD_TYPE_RADIO_BUTTON,
1221     "draw connected items",                     'd'
1222   },
1223   {
1224     IMG_GFX_EDITOR_BUTTON_DRAW_LINE,            GADGET_ID_LINE,
1225     &editor.button.draw_line,                   GD_TYPE_RADIO_BUTTON,
1226     "draw lines",                               'l'
1227   },
1228   {
1229     IMG_GFX_EDITOR_BUTTON_DRAW_ARC,             GADGET_ID_ARC,
1230     &editor.button.draw_arc,                    GD_TYPE_RADIO_BUTTON,
1231     "draw arcs",                                'a'
1232   },
1233   {
1234     IMG_GFX_EDITOR_BUTTON_DRAW_RECTANGLE,       GADGET_ID_RECTANGLE,
1235     &editor.button.draw_rectangle,              GD_TYPE_RADIO_BUTTON,
1236     "draw outline rectangles",                  'r'
1237   },
1238   {
1239     IMG_GFX_EDITOR_BUTTON_DRAW_FILLED_BOX,      GADGET_ID_FILLED_BOX,
1240     &editor.button.draw_filled_box,             GD_TYPE_RADIO_BUTTON,
1241     "draw filled rectangles",                   'R'
1242   },
1243   {
1244     IMG_GFX_EDITOR_BUTTON_ROTATE_UP,            GADGET_ID_WRAP_UP,
1245     &editor.button.rotate_up,                   GD_TYPE_NORMAL_BUTTON,
1246     "wrap (rotate) level up",                   0
1247   },
1248   {
1249     IMG_GFX_EDITOR_BUTTON_DRAW_TEXT,            GADGET_ID_TEXT,
1250     &editor.button.draw_text,                   GD_TYPE_RADIO_BUTTON,
1251     "enter text elements",                      't'
1252   },
1253   {
1254     IMG_GFX_EDITOR_BUTTON_FLOOD_FILL,           GADGET_ID_FLOOD_FILL,
1255     &editor.button.flood_fill,                  GD_TYPE_RADIO_BUTTON,
1256     "flood fill",                               'f'
1257   },
1258   {
1259     IMG_GFX_EDITOR_BUTTON_ROTATE_LEFT,          GADGET_ID_WRAP_LEFT,
1260     &editor.button.rotate_left,                 GD_TYPE_NORMAL_BUTTON,
1261     "wrap (rotate) level left",                 0
1262   },
1263   {
1264     IMG_GFX_EDITOR_BUTTON_ZOOM_LEVEL,           GADGET_ID_ZOOM,
1265     &editor.button.zoom_level,                  GD_TYPE_NORMAL_BUTTON,
1266     "zoom level tile size",                     '+'
1267   },
1268   {
1269     IMG_GFX_EDITOR_BUTTON_ROTATE_RIGHT,         GADGET_ID_WRAP_RIGHT,
1270     &editor.button.rotate_right,                GD_TYPE_NORMAL_BUTTON,
1271     "wrap (rotate) level right",                0
1272   },
1273   {
1274     IMG_GFX_EDITOR_BUTTON_DRAW_RANDOM,          GADGET_ID_RANDOM_PLACEMENT,
1275     &editor.button.draw_random,                 GD_TYPE_NORMAL_BUTTON,
1276     "random element placement",                 0
1277   },
1278   {
1279     IMG_GFX_EDITOR_BUTTON_GRAB_BRUSH,           GADGET_ID_GRAB_BRUSH,
1280     &editor.button.grab_brush,                  GD_TYPE_RADIO_BUTTON,
1281     "grab brush",                               'b'
1282   },
1283   {
1284     IMG_GFX_EDITOR_BUTTON_ROTATE_DOWN,          GADGET_ID_WRAP_DOWN,
1285     &editor.button.rotate_down,                 GD_TYPE_NORMAL_BUTTON,
1286     "wrap (rotate) level down",                 0
1287   },
1288   {
1289     IMG_GFX_EDITOR_BUTTON_PICK_ELEMENT,         GADGET_ID_PICK_ELEMENT,
1290     &editor.button.pick_element,                GD_TYPE_RADIO_BUTTON,
1291     "pick drawing element",                     ','
1292   },
1293
1294   // ---------- level control buttons -----------------------------------------
1295
1296   {
1297     IMG_GFX_EDITOR_BUTTON_UNDO,                 GADGET_ID_UNDO,
1298     &editor.button.undo,                        GD_TYPE_NORMAL_BUTTON,
1299     "undo/redo last operation",                 'u'
1300   },
1301   {
1302     IMG_GFX_EDITOR_BUTTON_CONF,                 GADGET_ID_CONF,
1303     &editor.button.conf,                        GD_TYPE_NORMAL_BUTTON,
1304     "level and editor settings",                'I'
1305   },
1306   {
1307     IMG_GFX_EDITOR_BUTTON_SAVE,                 GADGET_ID_SAVE,
1308     &editor.button.save,                        GD_TYPE_NORMAL_BUTTON,
1309     "save level",                               'S'
1310   },
1311   {
1312     IMG_GFX_EDITOR_BUTTON_CLEAR,                GADGET_ID_CLEAR,
1313     &editor.button.clear,                       GD_TYPE_NORMAL_BUTTON,
1314     "clear level",                              'C'
1315   },
1316   {
1317     IMG_GFX_EDITOR_BUTTON_TEST,                 GADGET_ID_TEST,
1318     &editor.button.test,                        GD_TYPE_NORMAL_BUTTON,
1319     "test level",                               'T'
1320   },
1321   {
1322     IMG_GFX_EDITOR_BUTTON_EXIT,                 GADGET_ID_EXIT,
1323     &editor.button.exit,                        GD_TYPE_NORMAL_BUTTON,
1324     "exit level editor",                        'E'
1325   },
1326
1327   // ---------- CE and GE control buttons -------------------------------------
1328
1329   {
1330     IMG_GFX_EDITOR_BUTTON_CE_COPY_FROM,         GADGET_ID_CUSTOM_COPY_FROM,
1331     &editor.button.ce_copy_from,                GD_TYPE_RADIO_BUTTON,
1332     "copy settings from other element",         0
1333   },
1334   {
1335     IMG_GFX_EDITOR_BUTTON_CE_COPY_TO,           GADGET_ID_CUSTOM_COPY_TO,
1336     &editor.button.ce_copy_to,                  GD_TYPE_RADIO_BUTTON,
1337     "copy settings to other element",           0
1338   },
1339   {
1340     IMG_GFX_EDITOR_BUTTON_CE_SWAP,              GADGET_ID_CUSTOM_EXCHANGE,
1341     &editor.button.ce_swap,                     GD_TYPE_RADIO_BUTTON,
1342     "exchange element with other element",      0
1343   },
1344   {
1345     IMG_GFX_EDITOR_BUTTON_CE_COPY,              GADGET_ID_CUSTOM_COPY,
1346     &editor.button.ce_copy,                     GD_TYPE_NORMAL_BUTTON,
1347     "copy settings from this element",          0
1348   },
1349   {
1350     IMG_GFX_EDITOR_BUTTON_CE_PASTE,             GADGET_ID_CUSTOM_PASTE,
1351     &editor.button.ce_paste,                    GD_TYPE_NORMAL_BUTTON,
1352     "paste settings to this element",           0
1353   },
1354
1355   // ---------- palette control buttons ---------------------------------------
1356
1357   {
1358     IMG_GFX_EDITOR_BUTTON_PROPERTIES,           GADGET_ID_PROPERTIES,
1359     &editor.button.properties,                  GD_TYPE_NORMAL_BUTTON,
1360     "properties of drawing element",            'p'
1361   },
1362   {
1363     IMG_GFX_EDITOR_BUTTON_ELEMENT_LEFT,         GADGET_ID_ELEMENT_LEFT,
1364     &editor.button.element_left,                GD_TYPE_NORMAL_BUTTON,
1365     "properties of drawing element 1",          '1'
1366   },
1367   {
1368     IMG_GFX_EDITOR_BUTTON_ELEMENT_MIDDLE,       GADGET_ID_ELEMENT_MIDDLE,
1369     &editor.button.element_middle,              GD_TYPE_NORMAL_BUTTON,
1370     "properties of drawing element 2",          '2'
1371   },
1372   {
1373     IMG_GFX_EDITOR_BUTTON_ELEMENT_RIGHT,        GADGET_ID_ELEMENT_RIGHT,
1374     &editor.button.element_right,               GD_TYPE_NORMAL_BUTTON,
1375     "properties of drawing element 3",          '3'
1376   },
1377   {
1378     IMG_GFX_EDITOR_BUTTON_PALETTE,              GADGET_ID_PALETTE,
1379     &editor.button.palette,                     GD_TYPE_NORMAL_BUTTON,
1380     "show list of elements",                    'e'
1381   }
1382 };
1383
1384 static int random_placement_value = 10;
1385 static int random_placement_method = RANDOM_USE_QUANTITY;
1386 static int random_placement_background_element = EL_SAND;
1387 static boolean random_placement_background_restricted = FALSE;
1388 static boolean stick_element_properties_window = FALSE;
1389 static boolean custom_element_properties[NUM_ELEMENT_PROPERTIES];
1390 static boolean custom_element_change_events[NUM_CHANGE_EVENTS];
1391 static struct ElementChangeInfo custom_element_change;
1392 static struct ElementGroupInfo group_element_info;
1393 static struct ElementInfo custom_element;
1394
1395 static char levelset_name[MAX_LEVEL_NAME_LEN + 1];
1396 static char levelset_author[MAX_LEVEL_AUTHOR_LEN + 1];
1397 static int levelset_num_levels = 100;
1398 static boolean levelset_use_levelset_artwork = FALSE;
1399 static boolean levelset_copy_level_template = FALSE;
1400 static int levelset_save_mode = LEVELSET_SAVE_MODE_UPDATE;
1401
1402 static struct
1403 {
1404   int gadget_type_id;
1405   int x, y;
1406   int min_value, max_value;
1407   int gadget_id_down, gadget_id_up;
1408   int gadget_id_text;
1409   int gadget_id_align;
1410   int *value;
1411   char *text_above, *text_left, *text_right;
1412 } counterbutton_info[ED_NUM_COUNTERBUTTONS] =
1413 {
1414   // ---------- current level number ------------------------------------------
1415
1416   {
1417     ED_COUNTER_ID_SELECT_LEVEL,
1418     -1, -1,     // these values are not constant, but can change at runtime
1419     1,                                  100,
1420     GADGET_ID_SELECT_LEVEL_DOWN,        GADGET_ID_SELECT_LEVEL_UP,
1421     GADGET_ID_SELECT_LEVEL_TEXT,        GADGET_ID_NONE,
1422     &level_nr,
1423     NULL,                               NULL, NULL
1424   },
1425
1426   // ---------- level and editor settings -------------------------------------
1427
1428   {
1429     ED_COUNTER_ID_LEVEL_XSIZE,
1430     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
1431     MIN_LEV_FIELDX,                     MAX_LEV_FIELDX,
1432     GADGET_ID_LEVEL_XSIZE_DOWN,         GADGET_ID_LEVEL_XSIZE_UP,
1433     GADGET_ID_LEVEL_XSIZE_TEXT,         GADGET_ID_NONE,
1434     &level.fieldx,
1435     "playfield size:",                  NULL, "width",
1436   },
1437   {
1438     ED_COUNTER_ID_LEVEL_YSIZE,
1439     -1,                                 ED_LEVEL_SETTINGS_YPOS(4),
1440     MIN_LEV_FIELDY,                     MAX_LEV_FIELDY,
1441     GADGET_ID_LEVEL_YSIZE_DOWN,         GADGET_ID_LEVEL_YSIZE_UP,
1442     GADGET_ID_LEVEL_YSIZE_TEXT,         GADGET_ID_LEVEL_XSIZE_UP,
1443     &level.fieldy,
1444     NULL,                               " ", "height",
1445   },
1446   {
1447     ED_COUNTER_ID_LEVEL_GEMSLIMIT,
1448     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(5),
1449     0,                                  999,
1450     GADGET_ID_LEVEL_GEMSLIMIT_DOWN,     GADGET_ID_LEVEL_GEMSLIMIT_UP,
1451     GADGET_ID_LEVEL_GEMSLIMIT_TEXT,     GADGET_ID_NONE,
1452     &level.gems_needed,
1453     NULL,                               "number of gems to collect:", NULL
1454   },
1455   {
1456     ED_COUNTER_ID_LEVEL_TIMELIMIT,
1457     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(8),
1458     0,                                  9999,
1459     GADGET_ID_LEVEL_TIMELIMIT_DOWN,     GADGET_ID_LEVEL_TIMELIMIT_UP,
1460     GADGET_ID_LEVEL_TIMELIMIT_TEXT,     GADGET_ID_NONE,
1461     &level.time,
1462     "time or step limit to solve level:", NULL, NULL
1463   },
1464   {
1465     ED_COUNTER_ID_LEVEL_TIMESCORE,
1466     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(10),
1467     0,                                  999,
1468     GADGET_ID_LEVEL_TIMESCORE_DOWN,     GADGET_ID_LEVEL_TIMESCORE_UP,
1469     GADGET_ID_LEVEL_TIMESCORE_TEXT,     GADGET_ID_NONE,
1470     &level.score[SC_TIME_BONUS],
1471     "score for time or steps left:",    NULL, NULL
1472   },
1473   {
1474     ED_COUNTER_ID_LEVEL_RANDOM_SEED,
1475     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(13),
1476     0,                                  9999,
1477     GADGET_ID_LEVEL_RANDOM_SEED_DOWN,   GADGET_ID_LEVEL_RANDOM_SEED_UP,
1478     GADGET_ID_LEVEL_RANDOM_SEED_TEXT,   GADGET_ID_NONE,
1479     &level.random_seed,
1480     NULL,                               "random seed:", "(0 => random)"
1481   },
1482   {
1483     ED_COUNTER_ID_LEVELSET_NUM_LEVELS,
1484     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
1485     1,                                  MAX_LEVELS,
1486     GADGET_ID_LEVELSET_NUM_LEVELS_DOWN, GADGET_ID_LEVELSET_NUM_LEVELS_UP,
1487     GADGET_ID_LEVELSET_NUM_LEVELS_TEXT, GADGET_ID_NONE,
1488     &levelset_num_levels,
1489     "number of levels:",                NULL, NULL,
1490   },
1491   {
1492     ED_COUNTER_ID_LEVEL_RANDOM,
1493     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(0),
1494     1,                                  100,
1495     GADGET_ID_LEVEL_RANDOM_DOWN,        GADGET_ID_LEVEL_RANDOM_UP,
1496     GADGET_ID_LEVEL_RANDOM_TEXT,        GADGET_ID_NONE,
1497     &random_placement_value,
1498     "random element placement:",        NULL, "in"
1499   },
1500   {
1501     ED_COUNTER_ID_BD_CYCLE_DELAY_MS,
1502     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(3),
1503     50,                                 500,
1504     GADGET_ID_BD_CYCLE_DELAY_MS_DOWN,   GADGET_ID_BD_CYCLE_DELAY_MS_UP,
1505     GADGET_ID_BD_CYCLE_DELAY_MS_TEXT,   GADGET_ID_NONE,
1506     &level.bd_cycle_delay_ms,
1507     NULL,                               NULL, "game cycle delay (ms)"
1508   },
1509   {
1510     ED_COUNTER_ID_BD_CYCLE_DELAY_C64,
1511     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(3),
1512     0,                                  32,
1513     GADGET_ID_BD_CYCLE_DELAY_C64_DOWN,  GADGET_ID_BD_CYCLE_DELAY_C64_UP,
1514     GADGET_ID_BD_CYCLE_DELAY_C64_TEXT,  GADGET_ID_NONE,
1515     &level.bd_cycle_delay_c64,
1516     NULL,                               NULL, "game cycle delay (C64-style)"
1517   },
1518
1519   // ---------- element settings: configure (various elements) ----------------
1520
1521   {
1522     ED_COUNTER_ID_ELEMENT_VALUE1,
1523     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
1524     MIN_SCORE,                          MAX_SCORE,
1525     GADGET_ID_ELEMENT_VALUE1_DOWN,      GADGET_ID_ELEMENT_VALUE1_UP,
1526     GADGET_ID_ELEMENT_VALUE1_TEXT,      GADGET_ID_NONE,
1527     NULL,                               // will be set when used
1528     NULL,                               NULL, NULL
1529   },
1530   {
1531     ED_COUNTER_ID_ELEMENT_VALUE2,
1532     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
1533     MIN_SCORE,                          MAX_SCORE,
1534     GADGET_ID_ELEMENT_VALUE2_DOWN,      GADGET_ID_ELEMENT_VALUE2_UP,
1535     GADGET_ID_ELEMENT_VALUE2_TEXT,      GADGET_ID_NONE,
1536     NULL,                               // will be set when used
1537     NULL,                               NULL, NULL
1538   },
1539   {
1540     ED_COUNTER_ID_ELEMENT_VALUE3,
1541     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
1542     MIN_SCORE,                          MAX_SCORE,
1543     GADGET_ID_ELEMENT_VALUE3_DOWN,      GADGET_ID_ELEMENT_VALUE3_UP,
1544     GADGET_ID_ELEMENT_VALUE3_TEXT,      GADGET_ID_NONE,
1545     NULL,                               // will be set when used
1546     NULL,                               NULL, NULL
1547   },
1548   {
1549     ED_COUNTER_ID_ELEMENT_VALUE4,
1550     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1551     MIN_SCORE,                          MAX_SCORE,
1552     GADGET_ID_ELEMENT_VALUE4_DOWN,      GADGET_ID_ELEMENT_VALUE4_UP,
1553     GADGET_ID_ELEMENT_VALUE4_TEXT,      GADGET_ID_NONE,
1554     NULL,                               // will be set when used
1555     NULL,                               NULL, NULL
1556   },
1557   {
1558     ED_COUNTER_ID_YAMYAM_CONTENT,
1559     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1560     MIN_ELEMENT_CONTENTS,               MAX_ELEMENT_CONTENTS,
1561     GADGET_ID_YAMYAM_CONTENT_DOWN,      GADGET_ID_YAMYAM_CONTENT_UP,
1562     GADGET_ID_YAMYAM_CONTENT_TEXT,      GADGET_ID_NONE,
1563     &level.num_yamyam_contents,
1564     NULL,                               NULL, "number of content areas"
1565   },
1566   {
1567     ED_COUNTER_ID_BALL_CONTENT,
1568     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
1569     MIN_ELEMENT_CONTENTS,               MAX_ELEMENT_CONTENTS,
1570     GADGET_ID_BALL_CONTENT_DOWN,        GADGET_ID_BALL_CONTENT_UP,
1571     GADGET_ID_BALL_CONTENT_TEXT,        GADGET_ID_NONE,
1572     &level.num_ball_contents,
1573     NULL,                               NULL, "number of content areas"
1574   },
1575   {
1576     ED_COUNTER_ID_ANDROID_CONTENT,
1577     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
1578     MIN_ANDROID_ELEMENTS,               MAX_ANDROID_ELEMENTS,
1579     GADGET_ID_ANDROID_CONTENT_DOWN,     GADGET_ID_ANDROID_CONTENT_UP,
1580     GADGET_ID_ANDROID_CONTENT_TEXT,     GADGET_ID_NONE,
1581     &level.num_android_clone_elements,
1582     NULL,                               NULL, "number of clonable elements"
1583   },
1584   {
1585     ED_COUNTER_ID_ENVELOPE_XSIZE,
1586     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
1587     MIN_ENVELOPE_XSIZE,                 MAX_ENVELOPE_XSIZE,
1588     GADGET_ID_ENVELOPE_XSIZE_DOWN,      GADGET_ID_ENVELOPE_XSIZE_UP,
1589     GADGET_ID_ENVELOPE_XSIZE_TEXT,      GADGET_ID_NONE,
1590     NULL,                               // will be set when used
1591     NULL,                               NULL, "width",
1592   },
1593   {
1594     ED_COUNTER_ID_ENVELOPE_YSIZE,
1595     -1,                                 ED_ELEMENT_SETTINGS_YPOS(0),
1596     MIN_ENVELOPE_YSIZE,                 MAX_ENVELOPE_YSIZE,
1597     GADGET_ID_ENVELOPE_YSIZE_DOWN,      GADGET_ID_ENVELOPE_YSIZE_UP,
1598     GADGET_ID_ENVELOPE_YSIZE_TEXT,      GADGET_ID_ENVELOPE_XSIZE_UP,
1599     NULL,                               // will be set when used
1600     NULL,                               " ", "height",
1601   },
1602   {
1603     ED_COUNTER_ID_INVENTORY_SIZE,
1604     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
1605     MIN_INITIAL_INVENTORY_SIZE,         MAX_INITIAL_INVENTORY_SIZE,
1606     GADGET_ID_INVENTORY_SIZE_DOWN,      GADGET_ID_INVENTORY_SIZE_UP,
1607     GADGET_ID_INVENTORY_SIZE_TEXT,      GADGET_ID_NONE,
1608     &level.initial_inventory_size[0],
1609     NULL,                               NULL, "number of inventory elements"
1610   },
1611   {
1612     ED_COUNTER_ID_MM_BALL_CONTENT,
1613     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1614     MIN_ELEMENTS_IN_GROUP,              MAX_MM_BALL_CONTENTS,
1615     GADGET_ID_MM_BALL_CONTENT_DOWN,     GADGET_ID_MM_BALL_CONTENT_UP,
1616     GADGET_ID_MM_BALL_CONTENT_TEXT,     GADGET_ID_NONE,
1617     &level.num_mm_ball_contents,
1618     NULL,                               NULL, "number of content elements"
1619   },
1620
1621   // ---------- element settings: configure 1 (custom elements) ---------------
1622
1623   {
1624     ED_COUNTER_ID_CUSTOM_SCORE,
1625     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(5),
1626     MIN_SCORE,                          MAX_SCORE,
1627     GADGET_ID_CUSTOM_SCORE_DOWN,        GADGET_ID_CUSTOM_SCORE_UP,
1628     GADGET_ID_CUSTOM_SCORE_TEXT,        GADGET_ID_NONE,
1629     &custom_element.collect_score_initial,
1630     NULL,                               "CE score", " "
1631   },
1632   {
1633     ED_COUNTER_ID_CUSTOM_GEMCOUNT,
1634     -1,                                 ED_ELEMENT_SETTINGS_YPOS(5),
1635     MIN_COLLECT_COUNT,                  MAX_COLLECT_COUNT,
1636     GADGET_ID_CUSTOM_GEMCOUNT_DOWN,     GADGET_ID_CUSTOM_GEMCOUNT_UP,
1637     GADGET_ID_CUSTOM_GEMCOUNT_TEXT,     GADGET_ID_CUSTOM_SCORE_UP,
1638     &custom_element.collect_count_initial,
1639     NULL,                               "CE count", NULL
1640   },
1641   {
1642     ED_COUNTER_ID_CUSTOM_VALUE_FIX,
1643     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(10),
1644     0,                                  9999,
1645     GADGET_ID_CUSTOM_VALUE_FIX_DOWN,    GADGET_ID_CUSTOM_VALUE_FIX_UP,
1646     GADGET_ID_CUSTOM_VALUE_FIX_TEXT,    GADGET_ID_NONE,
1647     &custom_element.ce_value_fixed_initial,
1648     NULL,                               "CE value", NULL
1649   },
1650   {
1651     ED_COUNTER_ID_CUSTOM_VALUE_RND,
1652     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(10),
1653     0,                                  9999,
1654     GADGET_ID_CUSTOM_VALUE_RND_DOWN,    GADGET_ID_CUSTOM_VALUE_RND_UP,
1655     GADGET_ID_CUSTOM_VALUE_RND_TEXT,    GADGET_ID_CUSTOM_VALUE_FIX_UP,
1656     &custom_element.ce_value_random_initial,
1657     NULL,                               "+random", NULL
1658   },
1659   {
1660     ED_COUNTER_ID_PUSH_DELAY_FIX,
1661     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(6),
1662     0,                                  999,
1663     GADGET_ID_PUSH_DELAY_FIX_DOWN,      GADGET_ID_PUSH_DELAY_FIX_UP,
1664     GADGET_ID_PUSH_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1665     &custom_element.push_delay_fixed,
1666     NULL,                               "push delay", NULL
1667   },
1668   {
1669     ED_COUNTER_ID_PUSH_DELAY_RND,
1670     -1,                                 ED_ELEMENT_SETTINGS_YPOS(6),
1671     0,                                  999,
1672     GADGET_ID_PUSH_DELAY_RND_DOWN,      GADGET_ID_PUSH_DELAY_RND_UP,
1673     GADGET_ID_PUSH_DELAY_RND_TEXT,      GADGET_ID_PUSH_DELAY_FIX_UP,
1674     &custom_element.push_delay_random,
1675     NULL,                               "+random", NULL
1676   },
1677   {
1678     ED_COUNTER_ID_DROP_DELAY_FIX,
1679     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(7),
1680     0,                                  999,
1681     GADGET_ID_DROP_DELAY_FIX_DOWN,      GADGET_ID_DROP_DELAY_FIX_UP,
1682     GADGET_ID_DROP_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1683     &custom_element.drop_delay_fixed,
1684     NULL,                               "drop delay", NULL
1685   },
1686   {
1687     ED_COUNTER_ID_DROP_DELAY_RND,
1688     -1,                                 ED_ELEMENT_SETTINGS_YPOS(7),
1689     0,                                  999,
1690     GADGET_ID_DROP_DELAY_RND_DOWN,      GADGET_ID_DROP_DELAY_RND_UP,
1691     GADGET_ID_DROP_DELAY_RND_TEXT,      GADGET_ID_DROP_DELAY_FIX_UP,
1692     &custom_element.drop_delay_random,
1693     NULL,                               "+random", NULL
1694   },
1695
1696   // ---------- element settings: configure 2 (custom elements) ---------------
1697
1698   {
1699     ED_COUNTER_ID_MOVE_DELAY_FIX,
1700     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(5),
1701     0,                                  999,
1702     GADGET_ID_MOVE_DELAY_FIX_DOWN,      GADGET_ID_MOVE_DELAY_FIX_UP,
1703     GADGET_ID_MOVE_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1704     &custom_element.move_delay_fixed,
1705     NULL,                               "move delay", NULL
1706   },
1707   {
1708     ED_COUNTER_ID_MOVE_DELAY_RND,
1709     -1,                                 ED_ELEMENT_SETTINGS_YPOS(5),
1710     0,                                  999,
1711     GADGET_ID_MOVE_DELAY_RND_DOWN,      GADGET_ID_MOVE_DELAY_RND_UP,
1712     GADGET_ID_MOVE_DELAY_RND_TEXT,      GADGET_ID_MOVE_DELAY_FIX_UP,
1713     &custom_element.move_delay_random,
1714     NULL,                               "+random", NULL
1715   },
1716   {
1717     ED_COUNTER_ID_STEP_DELAY_FIX,
1718     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(6),
1719     0,                                  999,
1720     GADGET_ID_STEP_DELAY_FIX_DOWN,      GADGET_ID_STEP_DELAY_FIX_UP,
1721     GADGET_ID_STEP_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1722     &custom_element.step_delay_fixed,
1723     NULL,                               "step delay", NULL
1724   },
1725   {
1726     ED_COUNTER_ID_STEP_DELAY_RND,
1727     -1,                                 ED_ELEMENT_SETTINGS_YPOS(6),
1728     0,                                  999,
1729     GADGET_ID_STEP_DELAY_RND_DOWN,      GADGET_ID_STEP_DELAY_RND_UP,
1730     GADGET_ID_STEP_DELAY_RND_TEXT,      GADGET_ID_STEP_DELAY_FIX_UP,
1731     &custom_element.step_delay_random,
1732     NULL,                               "+random", NULL
1733   },
1734   {
1735     ED_COUNTER_ID_EXPLOSION_DELAY,
1736     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(13),
1737     0,                                  999,
1738     GADGET_ID_EXPLOSION_DELAY_DOWN,     GADGET_ID_EXPLOSION_DELAY_UP,
1739     GADGET_ID_EXPLOSION_DELAY_TEXT,     GADGET_ID_NONE,
1740     &custom_element.explosion_delay,
1741     NULL,                               "explosion delay", NULL
1742   },
1743   {
1744     ED_COUNTER_ID_IGNITION_DELAY,
1745     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(14),
1746     0,                                  999,
1747     GADGET_ID_IGNITION_DELAY_DOWN,      GADGET_ID_IGNITION_DELAY_UP,
1748     GADGET_ID_IGNITION_DELAY_TEXT,      GADGET_ID_NONE,
1749     &custom_element.ignition_delay,
1750     NULL,                               "ignition delay", "(by fire)"
1751   },
1752
1753   // ---------- element settings: configure (group elements) ------------------
1754
1755   {
1756     ED_COUNTER_ID_GROUP_CONTENT,
1757     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1758     MIN_ELEMENTS_IN_GROUP,              MAX_ELEMENTS_IN_GROUP,
1759     GADGET_ID_GROUP_CONTENT_DOWN,       GADGET_ID_GROUP_CONTENT_UP,
1760     GADGET_ID_GROUP_CONTENT_TEXT,       GADGET_ID_NONE,
1761     &group_element_info.num_elements,
1762     NULL,                               NULL, "number of elements in group"
1763   },
1764
1765   // ---------- element settings: advanced (custom elements) ------------------
1766
1767   {
1768     ED_COUNTER_ID_CHANGE_DELAY_FIX,
1769     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(2),
1770     0,                                  999,
1771     GADGET_ID_CHANGE_DELAY_FIX_DOWN,    GADGET_ID_CHANGE_DELAY_FIX_UP,
1772     GADGET_ID_CHANGE_DELAY_FIX_TEXT,    GADGET_ID_NONE,
1773     &custom_element_change.delay_fixed,
1774     NULL,                               "CE delay", NULL,
1775   },
1776   {
1777     ED_COUNTER_ID_CHANGE_DELAY_RND,
1778     -1,                                 ED_ELEMENT_SETTINGS_YPOS(2),
1779     0,                                  999,
1780     GADGET_ID_CHANGE_DELAY_RND_DOWN,    GADGET_ID_CHANGE_DELAY_RND_UP,
1781     GADGET_ID_CHANGE_DELAY_RND_TEXT,    GADGET_ID_CHANGE_DELAY_FIX_UP,
1782     &custom_element_change.delay_random,
1783     NULL,                               "+random", NULL
1784   },
1785   {
1786     ED_COUNTER_ID_CHANGE_CONT_RND,
1787     ED_ELEMENT_SETTINGS_XPOS(3),        ED_ELEMENT_SETTINGS_YPOS(12),
1788     0,                                  100,
1789     GADGET_ID_CHANGE_CONT_RND_DOWN,     GADGET_ID_CHANGE_CONT_RND_UP,
1790     GADGET_ID_CHANGE_CONT_RND_TEXT,     GADGET_ID_NONE,
1791     &custom_element_change.random_percentage,
1792     NULL,                               "use random replace:", "%"
1793   },
1794 };
1795
1796 static struct
1797 {
1798   int gadget_type_id;
1799   int x, y;
1800   int gadget_id;
1801   int size;
1802   char *value;
1803   char *text_above, *infotext;
1804 } textinput_info[ED_NUM_TEXTINPUT] =
1805 {
1806   {
1807     ED_TEXTINPUT_ID_LEVEL_NAME,
1808     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(0),
1809     GADGET_ID_LEVEL_NAME,
1810     MAX_LEVEL_NAME_LEN,
1811     level.name,
1812     "Title:", "Title for this level"
1813   },
1814   {
1815     ED_TEXTINPUT_ID_LEVEL_AUTHOR,
1816     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(2),
1817     GADGET_ID_LEVEL_AUTHOR,
1818     MAX_LEVEL_AUTHOR_LEN,
1819     level.author,
1820     "Author:", "Author for this level"
1821   },
1822   {
1823     ED_TEXTINPUT_ID_LEVELSET_NAME,
1824     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(0),
1825     GADGET_ID_LEVELSET_NAME,
1826     MAX_LEVEL_NAME_LEN,
1827     levelset_name,
1828     "Title:", "Title for this or new level set"
1829   },
1830   {
1831     ED_TEXTINPUT_ID_LEVELSET_AUTHOR,
1832     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(2),
1833     GADGET_ID_LEVELSET_AUTHOR,
1834     MAX_LEVEL_AUTHOR_LEN,
1835     levelset_author,
1836     "Author:", "Author for this or new level set"
1837   },
1838   {
1839     ED_TEXTINPUT_ID_ELEMENT_NAME,
1840     -1, -1,     // these values are not constant, but can change at runtime
1841     GADGET_ID_ELEMENT_NAME,
1842     MAX_ELEMENT_NAME_LEN - 2,           // currently 2 chars less editable
1843     custom_element.description,
1844     NULL, "Element name"
1845   }
1846 };
1847
1848 static struct
1849 {
1850   int gadget_type_id;
1851   int x, y;
1852   int gadget_id;
1853   int xsize, ysize;
1854   char *value;
1855   char *text_above, *text_above_cropped, *infotext;
1856 } textarea_info[ED_NUM_TEXTAREAS] =
1857 {
1858   {
1859     ED_TEXTAREA_ID_ENVELOPE_INFO,
1860     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1861     GADGET_ID_ENVELOPE_INFO,
1862     MAX_ENVELOPE_XSIZE, MAX_ENVELOPE_YSIZE,
1863     NULL,
1864     "Envelope Content:", "Envelope Content (cropped):", "Envelope Content"
1865   }
1866 };
1867
1868 static struct ValueTextInfo options_time_or_steps[] =
1869 {
1870   { 0,                          "seconds"                       },
1871   { 1,                          "steps"                         },
1872
1873   { -1,                         NULL                            }
1874 };
1875
1876 static struct ValueTextInfo options_time_score_base[] =
1877 {
1878   { 1,                          "per second/step"               },
1879   { 10,                         "per 10 seconds/steps"          },
1880
1881   { -1,                         NULL                            }
1882 };
1883
1884 static struct ValueTextInfo options_game_engine_type[] =
1885 {
1886   { GAME_ENGINE_TYPE_RND,       "Rocks'n'Diamonds"              },
1887   { GAME_ENGINE_TYPE_BD,        "Boulder Dash"                  },
1888   { GAME_ENGINE_TYPE_EM,        "Emerald Mine"                  },
1889   { GAME_ENGINE_TYPE_SP,        "Supaplex"                      },
1890   { GAME_ENGINE_TYPE_MM,        "Mirror Magic"                  },
1891
1892   { -1,                         NULL                            }
1893 };
1894
1895 static struct ValueTextInfo options_levelset_save_mode[] =
1896 {
1897   { LEVELSET_SAVE_MODE_UPDATE,  "Update this level set"         },
1898   { LEVELSET_SAVE_MODE_CREATE,  "Create new level set"          },
1899
1900   { -1,                         NULL                            }
1901 };
1902
1903 static struct ValueTextInfo options_wind_direction[] =
1904 {
1905   { MV_START_NONE,              "none"                          },
1906   { MV_START_LEFT,              "left"                          },
1907   { MV_START_RIGHT,             "right"                         },
1908   { MV_START_UP,                "up"                            },
1909   { MV_START_DOWN,              "down"                          },
1910
1911   { -1,                         NULL                            }
1912 };
1913
1914 static struct ValueTextInfo options_player_speed[] =
1915 {
1916   { 0,                          "frozen"                        },
1917   { 1,                          "very slow"                     },
1918   { 2,                          "slow"                          },
1919   { 4,                          "normal"                        },
1920   { 8,                          "fast"                          },
1921   { 16,                         "very fast"                     },
1922   { 32,                         "ultrafast"                     },
1923
1924   { -1,                         NULL                            }
1925 };
1926
1927 static struct ValueTextInfo options_access_type[] =
1928 {
1929   { EP_WALKABLE,                "walkable"                      },
1930   { EP_PASSABLE,                "passable"                      },
1931
1932   { -1,                         NULL                            }
1933 };
1934
1935 static struct ValueTextInfo options_access_layer[] =
1936 {
1937   { EP_ACCESSIBLE_OVER,         "over"                          },
1938   { EP_ACCESSIBLE_INSIDE,       "inside"                        },
1939   { EP_ACCESSIBLE_UNDER,        "under"                         },
1940
1941   { -1,                         NULL                            }
1942 };
1943
1944 static struct ValueTextInfo options_access_protected[] =
1945 {
1946   { 0,                          "unprotected"                   },
1947   { 1,                          "protected"                     },
1948
1949   { -1,                         NULL                            }
1950 };
1951
1952 static struct ValueTextInfo options_access_direction[] =
1953 {
1954   { MV_NO_DIRECTION,            "no direction"                  },
1955   { MV_LEFT,                    "left"                          },
1956   { MV_RIGHT,                   "right"                         },
1957   { MV_UP,                      "up"                            },
1958   { MV_DOWN,                    "down"                          },
1959   { MV_LEFT  | MV_UP,           "left + up"                     },
1960   { MV_LEFT  | MV_DOWN,         "left + down"                   },
1961   { MV_RIGHT | MV_UP,           "right + up"                    },
1962   { MV_RIGHT | MV_DOWN,         "right + down"                  },
1963   { MV_HORIZONTAL,              "horizontal"                    },
1964   { MV_VERTICAL,                "vertical"                      },
1965   { MV_HORIZONTAL | MV_UP,      "horizontal + up"               },
1966   { MV_HORIZONTAL | MV_DOWN,    "horizontal + down"             },
1967   { MV_VERTICAL   | MV_LEFT,    "vertical + left"               },
1968   { MV_VERTICAL   | MV_RIGHT,   "vertical + right"              },
1969   { MV_ALL_DIRECTIONS,          "all directions"                },
1970
1971   { -1,                         NULL                            }
1972 };
1973
1974 static struct ValueTextInfo options_walk_to_action[] =
1975 {
1976   { EP_DIGGABLE,                "diggable"                      },
1977   { EP_COLLECTIBLE_ONLY,        "collectible"                   },
1978   { EP_DROPPABLE,               "collectible & droppable"       },
1979   { EP_THROWABLE,               "collectible & throwable"       },
1980   { EP_PUSHABLE,                "pushable"                      },
1981
1982   { -1,                         NULL                            }
1983 };
1984
1985 static struct ValueTextInfo options_move_pattern[] =
1986 {
1987   { MV_LEFT,                    "left"                          },
1988   { MV_RIGHT,                   "right"                         },
1989   { MV_UP,                      "up"                            },
1990   { MV_DOWN,                    "down"                          },
1991   { MV_HORIZONTAL,              "horizontal"                    },
1992   { MV_VERTICAL,                "vertical"                      },
1993   { MV_ALL_DIRECTIONS,          "all directions"                },
1994   { MV_WIND_DIRECTION,          "wind direction"                },
1995   { MV_TOWARDS_PLAYER,          "towards player"                },
1996   { MV_AWAY_FROM_PLAYER,        "away from player"              },
1997   { MV_ALONG_LEFT_SIDE,         "along left side"               },
1998   { MV_ALONG_RIGHT_SIDE,        "along right side"              },
1999   { MV_TURNING_LEFT,            "turning left"                  },
2000   { MV_TURNING_RIGHT,           "turning right"                 },
2001   { MV_TURNING_LEFT_RIGHT,      "turning left, right"           },
2002   { MV_TURNING_RIGHT_LEFT,      "turning right, left"           },
2003   { MV_TURNING_RANDOM,          "turning random"                },
2004   { MV_MAZE_RUNNER,             "maze runner style"             },
2005   { MV_MAZE_HUNTER,             "maze hunter style"             },
2006   { MV_WHEN_PUSHED,             "when pushed"                   },
2007   { MV_WHEN_DROPPED,            "when dropped/thrown"           },
2008
2009   { -1,                         NULL                            }
2010 };
2011
2012 static struct ValueTextInfo options_move_direction[] =
2013 {
2014   { MV_START_AUTOMATIC,         "automatic"                     },
2015   { MV_START_LEFT,              "left"                          },
2016   { MV_START_RIGHT,             "right"                         },
2017   { MV_START_UP,                "up"                            },
2018   { MV_START_DOWN,              "down"                          },
2019   { MV_START_RANDOM,            "random"                        },
2020   { MV_START_PREVIOUS,          "previous"                      },
2021
2022   { -1,                         NULL                            }
2023 };
2024
2025 static struct ValueTextInfo options_move_stepsize[] =
2026 {
2027   { 0,                          "not moving"                    },
2028   { 1,                          "very slow"                     },
2029   { 2,                          "slow"                          },
2030   { 4,                          "normal"                        },
2031   { 8,                          "fast"                          },
2032   { 16,                         "very fast"                     },
2033   { 32,                         "even faster"                   },
2034
2035   { -1,                         NULL                            }
2036 };
2037
2038 static struct ValueTextInfo options_move_leave_type[] =
2039 {
2040   { LEAVE_TYPE_UNLIMITED,       "leave behind"                  },
2041   { LEAVE_TYPE_LIMITED,         "change it to"                  },
2042
2043   { -1,                         NULL                            }
2044 };
2045
2046 static struct ValueTextInfo options_smash_targets[] =
2047 {
2048   { EP_CAN_SMASH_PLAYER,        "player"                        },
2049 #if 0
2050   { EP_CAN_SMASH_ENEMIES,       "enemies"                       },
2051 #endif
2052   { EP_CAN_SMASH_EVERYTHING,    "everything"                    },
2053
2054   { -1,                         NULL                            }
2055 };
2056
2057 static struct ValueTextInfo options_slippery_type[] =
2058 {
2059   { SLIPPERY_ANY_RANDOM,        "random"                        },
2060   { SLIPPERY_ANY_LEFT_RIGHT,    "left, right"                   },
2061   { SLIPPERY_ANY_RIGHT_LEFT,    "right, left"                   },
2062   { SLIPPERY_ONLY_LEFT,         "only left"                     },
2063   { SLIPPERY_ONLY_RIGHT,        "only right"                    },
2064
2065   { -1,                         NULL                            }
2066 };
2067
2068 static struct ValueTextInfo options_deadliness[] =
2069 {
2070   { EP_DONT_RUN_INTO,           "running into"                  },
2071   { EP_DONT_COLLIDE_WITH,       "colliding with"                },
2072   { EP_DONT_GET_HIT_BY,         "getting hit by"                },
2073   { EP_DONT_TOUCH,              "touching"                      },
2074
2075   { -1,                         NULL                            }
2076 };
2077
2078 static struct ValueTextInfo options_explosion_type[] =
2079 {
2080   { EXPLODES_3X3,               "3x3"                           },
2081   { EXPLODES_CROSS,             "3+3"                           },
2082   { EXPLODES_1X1,               "1x1"                           },
2083
2084   { -1,                         NULL                            }
2085 };
2086
2087 static struct ValueTextInfo options_time_units[] =
2088 {
2089   { 1,                          "frames"                        },
2090   { FRAMES_PER_SECOND,          "seconds"                       },
2091
2092   { -1,                         NULL                            }
2093 };
2094
2095 static struct ValueTextInfo options_change_direct_action[] =
2096 {
2097   { CE_TOUCHED_BY_PLAYER,       "touched by player"             },
2098   { CE_PRESSED_BY_PLAYER,       "pressed by player"             },
2099   { CE_SWITCHED_BY_PLAYER,      "switched by player"            },
2100   { CE_SNAPPED_BY_PLAYER,       "snapped by player"             },
2101   { CE_PUSHED_BY_PLAYER,        "pushed by player"              },
2102   { CE_ENTERED_BY_PLAYER,       "entered by player"             },
2103   { CE_LEFT_BY_PLAYER,          "left by player"                },
2104   { CE_DROPPED_BY_PLAYER,       "dropped/thrown by player"      },
2105   { CE_SWITCHED,                "switched"                      },
2106   { CE_HITTING_SOMETHING,       "hitting something"             },
2107   { CE_HIT_BY_SOMETHING,        "hit by something"              },
2108 #if 0
2109   { CE_BLOCKED,                 "blocked"                       },
2110 #endif
2111   { CE_IMPACT,                  "impact (on something)"         },
2112   { CE_SMASHED,                 "smashed (from above)"          },
2113 #if 0
2114   { CE_VALUE_CHANGES,           "CE value changes"              },
2115   { CE_SCORE_CHANGES,           "CE score changes"              },
2116 #endif
2117   { CE_VALUE_GETS_ZERO,         "CE value gets 0"               },
2118   { CE_SCORE_GETS_ZERO,         "CE score gets 0"               },
2119   { CE_UNDEFINED,               " "                             },
2120   { CE_HEADLINE_SPECIAL_EVENTS, "[mouse events]"                },
2121   { CE_CLICKED_BY_MOUSE,        "clicked by mouse"              },
2122   { CE_PRESSED_BY_MOUSE,        "pressed by mouse"              },
2123   { CE_UNDEFINED,               " "                             },
2124   { CE_HEADLINE_SPECIAL_EVENTS, "[static states]"               },
2125   { CE_NEXT_TO_PLAYER,          "next to player"                },
2126
2127   { -1,                         NULL                            }
2128 };
2129
2130 static struct ValueTextInfo options_change_other_action[] =
2131 {
2132   { CE_PLAYER_TOUCHES_X,        "player touches"                },
2133   { CE_PLAYER_PRESSES_X,        "player presses"                },
2134   { CE_PLAYER_SWITCHES_X,       "player switches"               },
2135   { CE_PLAYER_SNAPS_X,          "player snaps"                  },
2136   { CE_PLAYER_PUSHES_X,         "player pushes"                 },
2137   { CE_PLAYER_ENTERS_X,         "player enters"                 },
2138   { CE_PLAYER_LEAVES_X,         "player leaves"                 },
2139   { CE_PLAYER_DIGS_X,           "player digs"                   },
2140   { CE_PLAYER_COLLECTS_X,       "player collects"               },
2141   { CE_PLAYER_DROPS_X,          "player drops/throws"           },
2142   { CE_TOUCHING_X,              "touching"                      },
2143   { CE_HITTING_X,               "hitting"                       },
2144   { CE_DIGGING_X,               "digging"                       },
2145   { CE_HIT_BY_X,                "hit by"                        },
2146   { CE_SWITCH_OF_X,             "switch of"                     },
2147   { CE_CHANGE_OF_X,             "change by page of"             },
2148   { CE_EXPLOSION_OF_X,          "explosion of"                  },
2149   { CE_MOVE_OF_X,               "move of"                       },
2150   { CE_CREATION_OF_X,           "creation of"                   },
2151   { CE_VALUE_CHANGES_OF_X,      "CE value changes of"           },
2152   { CE_SCORE_CHANGES_OF_X,      "CE score changes of"           },
2153   { CE_VALUE_GETS_ZERO_OF_X,    "CE value gets 0 of"            },
2154   { CE_SCORE_GETS_ZERO_OF_X,    "CE score gets 0 of"            },
2155   { CE_UNDEFINED,               " "                             },
2156   { CE_HEADLINE_SPECIAL_EVENTS, "[mouse events]"                },
2157   { CE_MOUSE_CLICKED_ON_X,      "mouse clicked on"              },
2158   { CE_MOUSE_PRESSED_ON_X,      "mouse pressed on"              },
2159   { CE_UNDEFINED,               " "                             },
2160   { CE_HEADLINE_SPECIAL_EVENTS, "[static states]"               },
2161   { CE_PLAYER_NEXT_TO_X,        "player next to"                },
2162   { CE_NEXT_TO_X,               "next to"                       },
2163
2164   { -1,                         NULL                            }
2165 };
2166
2167 static struct ValueTextInfo options_change_trigger_side[] =
2168 {
2169   { CH_SIDE_LEFT,               "left"                          },
2170   { CH_SIDE_RIGHT,              "right"                         },
2171   { CH_SIDE_TOP,                "top"                           },
2172   { CH_SIDE_BOTTOM,             "bottom"                        },
2173   { CH_SIDE_LEFT_RIGHT,         "left/right"                    },
2174   { CH_SIDE_TOP_BOTTOM,         "top/bottom"                    },
2175   { CH_SIDE_ANY,                "any"                           },
2176
2177   { -1,                         NULL                            }
2178 };
2179
2180 static struct ValueTextInfo options_change_trigger_player[] =
2181 {
2182   { CH_PLAYER_1,                "1"                             },
2183   { CH_PLAYER_2,                "2"                             },
2184   { CH_PLAYER_3,                "3"                             },
2185   { CH_PLAYER_4,                "4"                             },
2186   { CH_PLAYER_ANY,              "any"                           },
2187
2188   { -1,                         NULL                            }
2189 };
2190
2191 static struct ValueTextInfo options_change_trigger_page[] =
2192 {
2193   { (1u << 0),                  "1"                             },
2194   { (1u << 1),                  "2"                             },
2195   { (1u << 2),                  "3"                             },
2196   { (1u << 3),                  "4"                             },
2197   { (1u << 4),                  "5"                             },
2198   { (1u << 5),                  "6"                             },
2199   { (1u << 6),                  "7"                             },
2200   { (1u << 7),                  "8"                             },
2201   { (1u << 8),                  "9"                             },
2202   { (1u << 9),                  "10"                            },
2203   { (1u << 10),                 "11"                            },
2204   { (1u << 11),                 "12"                            },
2205   { (1u << 12),                 "13"                            },
2206   { (1u << 13),                 "14"                            },
2207   { (1u << 14),                 "15"                            },
2208   { (1u << 15),                 "16"                            },
2209   { (1u << 16),                 "17"                            },
2210   { (1u << 17),                 "18"                            },
2211   { (1u << 18),                 "19"                            },
2212   { (1u << 19),                 "20"                            },
2213   { (1u << 20),                 "21"                            },
2214   { (1u << 21),                 "22"                            },
2215   { (1u << 22),                 "23"                            },
2216   { (1u << 23),                 "24"                            },
2217   { (1u << 24),                 "25"                            },
2218   { (1u << 25),                 "26"                            },
2219   { (1u << 26),                 "27"                            },
2220   { (1u << 27),                 "28"                            },
2221   { (1u << 28),                 "29"                            },
2222   { (1u << 29),                 "30"                            },
2223   { (1u << 30),                 "31"                            },
2224   { (1u << 31),                 "32"                            },
2225   { CH_PAGE_ANY,                "any"                           },
2226
2227   { -1,                         NULL                            }
2228 };
2229
2230 static struct ValueTextInfo options_change_replace_when[] =
2231 {
2232   { CP_WHEN_EMPTY,              "empty"                         },
2233   { CP_WHEN_WALKABLE,           "walkable"                      },
2234   { CP_WHEN_DIGGABLE,           "diggable"                      },
2235   { CP_WHEN_COLLECTIBLE,        "collectible"                   },
2236   { CP_WHEN_REMOVABLE,          "removable"                     },
2237   { CP_WHEN_DESTRUCTIBLE,       "destructible"                  },
2238
2239   { -1,                         NULL                            }
2240 };
2241
2242 static struct ValueTextInfo options_action_type[] =
2243 {
2244   { CA_NO_ACTION,               "no action"                     },
2245   { CA_UNDEFINED,               " "                             },
2246   { CA_HEADLINE_LEVEL_ACTIONS,  "[level]"                       },
2247   { CA_RESTART_LEVEL,           "restart level"                 },
2248   { CA_SHOW_ENVELOPE,           "show envelope"                 },
2249   { CA_SET_LEVEL_TIME,          "set time"                      },
2250   { CA_SET_LEVEL_SCORE,         "set score"                     },
2251   { CA_SET_LEVEL_GEMS,          "set gems"                      },
2252   { CA_SET_LEVEL_WIND,          "set wind dir."                 },
2253   { CA_SET_LEVEL_RANDOM_SEED,   "set random seed"               },
2254   { CA_UNDEFINED,               " "                             },
2255   { CA_HEADLINE_PLAYER_ACTIONS, "[player]"                      },
2256   { CA_MOVE_PLAYER,             "move player"                   },
2257   { CA_MOVE_PLAYER_NEW,         "move player new"               },
2258   { CA_EXIT_PLAYER,             "exit player"                   },
2259   { CA_KILL_PLAYER,             "kill player"                   },
2260   { CA_SET_PLAYER_KEYS,         "set keys"                      },
2261   { CA_SET_PLAYER_SPEED,        "set speed"                     },
2262   { CA_SET_PLAYER_SHIELD,       "set shield"                    },
2263   { CA_SET_PLAYER_GRAVITY,      "set gravity"                   },
2264   { CA_SET_PLAYER_ARTWORK,      "set artwork"                   },
2265   { CA_SET_PLAYER_INVENTORY,    "set inventory"                 },
2266   { CA_UNDEFINED,               " "                             },
2267   { CA_HEADLINE_CE_ACTIONS,     "[CE]"                          },
2268   { CA_SET_CE_VALUE,            "set CE value"                  },
2269   { CA_SET_CE_SCORE,            "set CE score"                  },
2270   { CA_SET_CE_ARTWORK,          "set CE artwork"                },
2271   { CA_UNDEFINED,               " "                             },
2272   { CA_HEADLINE_ENGINE_ACTIONS, "[engine]"                      },
2273   { CA_SET_ENGINE_SCAN_MODE,    "set scan mode"                 },
2274
2275   { -1,                         NULL                            }
2276 };
2277
2278 static struct ValueTextInfo options_action_mode_none[] =
2279 {
2280   { CA_MODE_UNDEFINED,          " "                             },
2281
2282   { -1,                         NULL                            }
2283 };
2284
2285 static struct ValueTextInfo options_action_mode_assign[] =
2286 {
2287   { CA_MODE_SET,                "="                             },
2288
2289   { -1,                         NULL                            }
2290 };
2291
2292 static struct ValueTextInfo options_action_mode_add_remove[] =
2293 {
2294   { CA_MODE_ADD,                "+"                             },
2295   { CA_MODE_SUBTRACT,           "-"                             },
2296
2297   { -1,                         NULL                            }
2298 };
2299
2300 static struct ValueTextInfo options_action_mode_calculate[] =
2301 {
2302   { CA_MODE_SET,                "="                             },
2303   { CA_MODE_ADD,                "+"                             },
2304   { CA_MODE_SUBTRACT,           "-"                             },
2305   { CA_MODE_MULTIPLY,           "*"                             },
2306   { CA_MODE_DIVIDE,             "/"                             },
2307   { CA_MODE_MODULO,             "%"                             },
2308
2309   { -1,                         NULL                            }
2310 };
2311
2312 static struct ValueTextInfo options_action_arg_none[] =
2313 {
2314   { CA_ARG_UNDEFINED,           "         "                     },
2315
2316   { -1,                         NULL                            }
2317 };
2318
2319 static struct ValueTextInfo options_action_arg_player[] =
2320 {
2321   { CA_ARG_PLAYER_HEADLINE,     "[player]"                      },
2322   { CA_ARG_PLAYER_1,            "1"                             },
2323   { CA_ARG_PLAYER_2,            "2"                             },
2324   { CA_ARG_PLAYER_3,            "3"                             },
2325   { CA_ARG_PLAYER_4,            "4"                             },
2326   { CA_ARG_PLAYER_ANY,          "any"                           },
2327   { CA_ARG_PLAYER_TRIGGER,      "trigger"                       },
2328   { CA_ARG_PLAYER_ACTION,       "action ->"                     },
2329
2330   { -1,                         NULL                            }
2331 };
2332
2333 static struct ValueTextInfo options_action_arg_number[] =
2334 {
2335   { CA_ARG_NUMBER_HEADLINE,     "[number]"                      },
2336   { CA_ARG_0,                   "0"                             },
2337   { CA_ARG_1,                   "1"                             },
2338   { CA_ARG_2,                   "2"                             },
2339   { CA_ARG_3,                   "3"                             },
2340   { CA_ARG_4,                   "4"                             },
2341   { CA_ARG_5,                   "5"                             },
2342   { CA_ARG_10,                  "10"                            },
2343   { CA_ARG_100,                 "100"                           },
2344   { CA_ARG_1000,                "1000"                          },
2345   { CA_ARG_UNDEFINED,           " "                             },
2346   { CA_ARG_NUMBER_MIN,          "min"                           },
2347   { CA_ARG_NUMBER_MAX,          "max"                           },
2348   { CA_ARG_UNDEFINED,           " "                             },
2349   { CA_ARG_NUMBER_RESET,        "reset"                         },
2350   { CA_ARG_UNDEFINED,           " "                             },
2351   { CA_ARG_NUMBER_CE_VALUE,     "CE value"                      },
2352   { CA_ARG_NUMBER_CE_SCORE,     "CE score"                      },
2353   { CA_ARG_NUMBER_CE_DELAY,     "CE delay"                      },
2354   { CA_ARG_UNDEFINED,           " "                             },
2355   { CA_ARG_NUMBER_LEVEL_TIME,   "time"                          },
2356   { CA_ARG_NUMBER_LEVEL_GEMS,   "gems"                          },
2357   { CA_ARG_NUMBER_LEVEL_SCORE,  "score"                         },
2358   { CA_ARG_UNDEFINED,           " "                             },
2359   { CA_ARG_ELEMENT_CV_HEADLINE, "[CE value]"                    },
2360   { CA_ARG_ELEMENT_CV_TARGET,   "target"                        },
2361   { CA_ARG_ELEMENT_CV_TRIGGER,  "trigger"                       },
2362   { CA_ARG_ELEMENT_CV_ACTION,   "action ->"                     },
2363   { CA_ARG_UNDEFINED,           " "                             },
2364   { CA_ARG_ELEMENT_CS_HEADLINE, "[CE score]"                    },
2365   { CA_ARG_ELEMENT_CS_TARGET,   "target"                        },
2366   { CA_ARG_ELEMENT_CS_TRIGGER,  "trigger"                       },
2367   { CA_ARG_ELEMENT_CS_ACTION,   "action ->"                     },
2368
2369   { -1,                         NULL                            }
2370 };
2371
2372 static struct ValueTextInfo options_action_arg_value[] =
2373 {
2374   { CA_ARG_NUMBER_HEADLINE,     "[number]"                      },
2375   { CA_ARG_0,                   "0"                             },
2376   { CA_ARG_1,                   "1"                             },
2377   { CA_ARG_2,                   "2"                             },
2378   { CA_ARG_3,                   "3"                             },
2379   { CA_ARG_4,                   "4"                             },
2380   { CA_ARG_5,                   "5"                             },
2381   { CA_ARG_10,                  "10"                            },
2382   { CA_ARG_100,                 "100"                           },
2383   { CA_ARG_1000,                "1000"                          },
2384   { CA_ARG_UNDEFINED,           " "                             },
2385   { CA_ARG_NUMBER_MIN,          "min"                           },
2386   { CA_ARG_NUMBER_MAX,          "max"                           },
2387   { CA_ARG_UNDEFINED,           " "                             },
2388   { CA_ARG_NUMBER_RESET,        "reset"                         },
2389   { CA_ARG_UNDEFINED,           " "                             },
2390   { CA_ARG_NUMBER_CE_VALUE,     "CE value"                      },
2391   { CA_ARG_NUMBER_CE_SCORE,     "CE score"                      },
2392   { CA_ARG_NUMBER_CE_DELAY,     "CE delay"                      },
2393   { CA_ARG_UNDEFINED,           " "                             },
2394   { CA_ARG_NUMBER_LEVEL_TIME,   "time"                          },
2395   { CA_ARG_NUMBER_LEVEL_GEMS,   "gems"                          },
2396   { CA_ARG_NUMBER_LEVEL_SCORE,  "score"                         },
2397   { CA_ARG_UNDEFINED,           " "                             },
2398   { CA_ARG_ELEMENT_CV_HEADLINE, "[CE value]"                    },
2399   { CA_ARG_ELEMENT_CV_TARGET,   "target"                        },
2400   { CA_ARG_ELEMENT_CV_TRIGGER,  "trigger"                       },
2401   { CA_ARG_ELEMENT_CV_ACTION,   "action ->"                     },
2402   { CA_ARG_UNDEFINED,           " "                             },
2403   { CA_ARG_ELEMENT_CS_HEADLINE, "[CE score]"                    },
2404   { CA_ARG_ELEMENT_CS_TARGET,   "target"                        },
2405   { CA_ARG_ELEMENT_CS_TRIGGER,  "trigger"                       },
2406   { CA_ARG_ELEMENT_CS_ACTION,   "action ->"                     },
2407   { CA_ARG_UNDEFINED,           " "                             },
2408   { CA_ARG_ELEMENT_NR_HEADLINE, "[element]"                     },
2409   { CA_ARG_ELEMENT_NR_TARGET,   "target"                        },
2410   { CA_ARG_ELEMENT_NR_TRIGGER,  "trigger"                       },
2411   { CA_ARG_ELEMENT_NR_ACTION,   "action ->"                     },
2412
2413   { -1,                         NULL                            }
2414 };
2415
2416 static struct ValueTextInfo options_action_arg_envelope[] =
2417 {
2418   { CA_ARG_NUMBER_HEADLINE,     "[number]"                      },
2419   { CA_ARG_1,                   "1"                             },
2420   { CA_ARG_2,                   "2"                             },
2421   { CA_ARG_3,                   "3"                             },
2422   { CA_ARG_4,                   "4"                             },
2423   { CA_ARG_UNDEFINED,           " "                             },
2424   { CA_ARG_ELEMENT_HEADLINE,    "[element]"                     },
2425   { CA_ARG_ELEMENT_TARGET,      "target"                        },
2426   { CA_ARG_ELEMENT_TRIGGER,     "trigger"                       },
2427   { CA_ARG_ELEMENT_ACTION,      "action ->"                     },
2428
2429   { -1,                         NULL                            }
2430 };
2431
2432 static struct ValueTextInfo options_action_arg_key[] =
2433 {
2434   { CA_ARG_NUMBER_HEADLINE,     "[number]"                      },
2435   { CA_ARG_1,                   "1"                             },
2436   { CA_ARG_2,                   "2"                             },
2437   { CA_ARG_3,                   "3"                             },
2438   { CA_ARG_4,                   "4"                             },
2439   { CA_ARG_5,                   "5"                             },
2440   { CA_ARG_6,                   "6"                             },
2441   { CA_ARG_7,                   "7"                             },
2442   { CA_ARG_8,                   "8"                             },
2443   { CA_ARG_UNDEFINED,           " "                             },
2444   { CA_ARG_ELEMENT_HEADLINE,    "[element]"                     },
2445   { CA_ARG_ELEMENT_TARGET,      "target"                        },
2446   { CA_ARG_ELEMENT_TRIGGER,     "trigger"                       },
2447   { CA_ARG_ELEMENT_ACTION,      "action ->"                     },
2448
2449   { -1,                         NULL                            }
2450 };
2451
2452 static struct ValueTextInfo options_action_arg_speed[] =
2453 {
2454   { CA_ARG_SPEED_HEADLINE,      "[speed]"                       },
2455   { CA_ARG_SPEED_NOT_MOVING,    "frozen"                        },
2456   { CA_ARG_SPEED_VERY_SLOW,     "very slow"                     },
2457   { CA_ARG_SPEED_SLOW,          "slow"                          },
2458   { CA_ARG_SPEED_NORMAL,        "normal"                        },
2459   { CA_ARG_SPEED_FAST,          "fast"                          },
2460   { CA_ARG_SPEED_VERY_FAST,     "very fast"                     },
2461   { CA_ARG_SPEED_EVEN_FASTER,   "ultrafast"                     },
2462   { CA_ARG_UNDEFINED,           " "                             },
2463   { CA_ARG_SPEED_SLOWER,        "slower"                        },
2464   { CA_ARG_SPEED_FASTER,        "faster"                        },
2465   { CA_ARG_UNDEFINED,           " "                             },
2466   { CA_ARG_SPEED_RESET,         "reset"                         },
2467
2468   { -1,                         NULL                            }
2469 };
2470
2471 static struct ValueTextInfo options_action_arg_shield[] =
2472 {
2473   { CA_ARG_SHIELD_HEADLINE,     "[shield]"                      },
2474   { CA_ARG_SHIELD_OFF,          "off"                           },
2475   { CA_ARG_SHIELD_NORMAL,       "normal"                        },
2476   { CA_ARG_SHIELD_DEADLY,       "deadly"                        },
2477
2478   { -1,                         NULL                            }
2479 };
2480
2481 static struct ValueTextInfo options_action_arg_artwork[] =
2482 {
2483   { CA_ARG_ELEMENT_HEADLINE,    "[element]"                     },
2484   { CA_ARG_ELEMENT_TARGET,      "target"                        },
2485   { CA_ARG_ELEMENT_TRIGGER,     "trigger"                       },
2486   { CA_ARG_ELEMENT_ACTION,      "action ->"                     },
2487   { CA_ARG_UNDEFINED,           " "                             },
2488   { CA_ARG_ELEMENT_RESET,       "reset"                         },
2489
2490   { -1,                         NULL                            }
2491 };
2492
2493 static struct ValueTextInfo options_action_arg_gravity[] =
2494 {
2495   { CA_ARG_GRAVITY_HEADLINE,    "[gravity]"                     },
2496   { CA_ARG_GRAVITY_ON,          "on"                            },
2497   { CA_ARG_GRAVITY_OFF,         "off"                           },
2498   { CA_ARG_GRAVITY_TOGGLE,      "toggle"                        },
2499
2500   { -1,                         NULL                            }
2501 };
2502
2503 static struct ValueTextInfo options_action_arg_direction[] =
2504 {
2505   { CA_ARG_DIRECTION_HEADLINE,  "[dir.]"                        },
2506   { CA_ARG_DIRECTION_NONE,      "none"                          },
2507   { CA_ARG_DIRECTION_LEFT,      "left"                          },
2508   { CA_ARG_DIRECTION_RIGHT,     "right"                         },
2509   { CA_ARG_DIRECTION_UP,        "up"                            },
2510   { CA_ARG_DIRECTION_DOWN,      "down"                          },
2511   { CA_ARG_DIRECTION_TRIGGER,   "trigger"                       },
2512   { CA_ARG_DIRECTION_TRIGGER_BACK, "-trigger"                   },
2513
2514   { -1,                         NULL                            }
2515 };
2516
2517 static struct ValueTextInfo options_action_arg_scan_mode[] =
2518 {
2519   { CA_ARG_SCAN_MODE_HEADLINE,  "[mode]"                        },
2520   { CA_ARG_SCAN_MODE_NORMAL,    "normal"                        },
2521   { CA_ARG_SCAN_MODE_REVERSE,   "reverse"                       },
2522
2523   { -1,                         NULL                            }
2524 };
2525
2526 static struct ValueTextInfo options_action_arg_inventory[] =
2527 {
2528   { CA_ARG_INVENTORY_HEADLINE,  "[add]"                         },
2529   { CA_ARG_ELEMENT_TARGET,      "+ target"                      },
2530   { CA_ARG_ELEMENT_TRIGGER,     "+ trigger"                     },
2531   { CA_ARG_ELEMENT_ACTION,      "+ action"                      },
2532   { CA_ARG_UNDEFINED,           " "                             },
2533   { CA_ARG_INVENTORY_RM_HEADLINE,"[remove]"                     },
2534   { CA_ARG_INVENTORY_RM_TARGET, "- target"                      },
2535   { CA_ARG_INVENTORY_RM_TRIGGER,"- trigger"                     },
2536   { CA_ARG_INVENTORY_RM_ACTION, "- action"                      },
2537   { CA_ARG_INVENTORY_RM_FIRST,  "- first"                       },
2538   { CA_ARG_INVENTORY_RM_LAST,   "- last"                        },
2539   { CA_ARG_INVENTORY_RM_ALL,    "- all"                         },
2540   { CA_ARG_UNDEFINED,           " "                             },
2541   { CA_ARG_INVENTORY_RESET,     "reset"                         },
2542
2543   { -1,                         NULL                            }
2544 };
2545
2546 static char options_change_page_strings[MAX_CHANGE_PAGES][10];
2547 static struct ValueTextInfo options_change_page[MAX_CHANGE_PAGES + 1] =
2548 {
2549   { -1,                         NULL                            }
2550 };
2551
2552 static struct ValueTextInfo options_group_choice_mode[] =
2553 {
2554   { ANIM_RANDOM,                "random"                        },
2555   { ANIM_LOOP,                  "loop"                          },
2556   { ANIM_LINEAR,                "linear"                        },
2557   { ANIM_PINGPONG,              "pingpong"                      },
2558   { ANIM_PINGPONG2,             "pingpong 2"                    },
2559   { ANIM_LEVEL_NR,              "level number"                  },
2560
2561   { -1,                         NULL                            }
2562 };
2563
2564 static struct ValueTextInfo options_bd_scheduling_type[] =
2565 {
2566   { GD_SCHEDULING_MILLISECONDS,   "Milliseconds"                },
2567   { GD_SCHEDULING_BD1,            "BD1"                         },
2568   { GD_SCHEDULING_BD2,            "BD2"                         },
2569   { GD_SCHEDULING_PLCK,           "Construction Kit"            },
2570   { GD_SCHEDULING_CRDR,           "Crazy Dream 7"               },
2571   { GD_SCHEDULING_BD1_ATARI,      "Atari BD1"                   },
2572   { GD_SCHEDULING_BD2_PLCK_ATARI, "Atari BD2 / PLCK"            },
2573
2574   { -1,                           NULL                          }
2575 };
2576
2577 static struct ValueTextInfo *action_arg_modes[] =
2578 {
2579   options_action_mode_none,
2580   options_action_mode_assign,
2581   options_action_mode_add_remove,
2582   options_action_mode_calculate,
2583 };
2584
2585 static struct
2586 {
2587   int value;
2588   int mode;
2589   struct ValueTextInfo *options;
2590 }
2591 action_arg_options[] =
2592 {
2593   { CA_NO_ACTION,               0,      options_action_arg_none,        },
2594   { CA_EXIT_PLAYER,             0,      options_action_arg_player,      },
2595   { CA_KILL_PLAYER,             0,      options_action_arg_player,      },
2596   { CA_MOVE_PLAYER,             0,      options_action_arg_direction,   },
2597   { CA_MOVE_PLAYER_NEW,         0,      options_action_arg_direction,   },
2598   { CA_RESTART_LEVEL,           0,      options_action_arg_none,        },
2599   { CA_SHOW_ENVELOPE,           0,      options_action_arg_envelope,    },
2600   { CA_SET_LEVEL_TIME,          3,      options_action_arg_number,      },
2601   { CA_SET_LEVEL_GEMS,          3,      options_action_arg_number,      },
2602   { CA_SET_LEVEL_SCORE,         3,      options_action_arg_number,      },
2603   { CA_SET_LEVEL_WIND,          1,      options_action_arg_direction,   },
2604   { CA_SET_LEVEL_RANDOM_SEED,   1,      options_action_arg_number,      },
2605   { CA_SET_PLAYER_KEYS,         2,      options_action_arg_key,         },
2606   { CA_SET_PLAYER_SPEED,        1,      options_action_arg_speed,       },
2607   { CA_SET_PLAYER_SHIELD,       1,      options_action_arg_shield,      },
2608   { CA_SET_PLAYER_GRAVITY,      1,      options_action_arg_gravity,     },
2609   { CA_SET_PLAYER_ARTWORK,      1,      options_action_arg_artwork,     },
2610   { CA_SET_PLAYER_INVENTORY,    0,      options_action_arg_inventory,   },
2611   { CA_SET_CE_VALUE,            3,      options_action_arg_value,       },
2612   { CA_SET_CE_SCORE,            3,      options_action_arg_value,       },
2613   { CA_SET_CE_ARTWORK,          1,      options_action_arg_artwork,     },
2614   { CA_SET_ENGINE_SCAN_MODE,    1,      options_action_arg_scan_mode,   },
2615
2616   { -1,                         FALSE,  NULL                            }
2617 };
2618
2619 static struct
2620 {
2621   int gadget_type_id;
2622   int x, y;
2623   int gadget_id;
2624   int gadget_id_align;
2625   int size;     // char size of selectbox or '-1' (dynamically determined)
2626   struct ValueTextInfo *options;
2627   int *value;
2628   char *text_above, *text_left, *text_right, *infotext;
2629 } selectbox_info[ED_NUM_SELECTBOX] =
2630 {
2631   // ---------- level and editor settings -------------------------------------
2632
2633   {
2634     ED_SELECTBOX_ID_TIME_OR_STEPS,
2635     -1,                                 ED_LEVEL_SETTINGS_YPOS(8),
2636     GADGET_ID_TIME_OR_STEPS,            GADGET_ID_LEVEL_TIMELIMIT_UP,
2637     -1,
2638     options_time_or_steps,
2639     &level.use_step_counter,
2640     NULL, NULL, "(0 => no limit)",      "time or step limit"
2641   },
2642   {
2643     ED_SELECTBOX_ID_TIME_SCORE_BASE,
2644     -1,                                 ED_LEVEL_SETTINGS_YPOS(10),
2645     GADGET_ID_TIME_SCORE_BASE,          GADGET_ID_LEVEL_TIMESCORE_UP,
2646     -1,
2647     options_time_score_base,
2648     &level.time_score_base,
2649     NULL, NULL, NULL,                   "time score for 1 or 10 seconds/steps"
2650   },
2651   {
2652     ED_SELECTBOX_ID_GAME_ENGINE_TYPE,
2653     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(12),
2654     GADGET_ID_GAME_ENGINE_TYPE,         GADGET_ID_NONE,
2655     -1,
2656     options_game_engine_type,
2657     &level.game_engine_type,
2658     NULL, "game engine:", NULL,         "game engine"
2659   },
2660   {
2661     ED_SELECTBOX_ID_BD_SCHEDULING_TYPE,
2662     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(1),
2663     GADGET_ID_BD_SCHEDULING_TYPE,       GADGET_ID_NONE,
2664     -1,
2665     options_bd_scheduling_type,
2666     &level.bd_scheduling_type,
2667     NULL, "scheduling type:", NULL,     "level timing"
2668   },
2669   {
2670     ED_SELECTBOX_ID_LEVELSET_SAVE_MODE,
2671     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(10),
2672     GADGET_ID_LEVELSET_SAVE_MODE,       GADGET_ID_NONE,
2673     -1,
2674     options_levelset_save_mode,
2675     &levelset_save_mode,
2676     "Action:", NULL, NULL,              "action when saving level set"
2677   },
2678
2679   // ---------- element settings: configure (several elements) ----------------
2680
2681   {
2682     ED_SELECTBOX_ID_WIND_DIRECTION,
2683     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
2684     GADGET_ID_WIND_DIRECTION,           GADGET_ID_NONE,
2685     -1,
2686     options_wind_direction,
2687     &level.wind_direction_initial,
2688     NULL, "initial wind direction:", NULL,      "initial wind direction"
2689   },
2690   {
2691     ED_SELECTBOX_ID_PLAYER_SPEED,
2692     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(7),
2693     GADGET_ID_PLAYER_SPEED,             GADGET_ID_NONE,
2694     -1,
2695     options_player_speed,
2696     &level.initial_player_stepsize[0],
2697     NULL, "initial player speed:", NULL,        "initial player speed"
2698   },
2699   {
2700     ED_SELECTBOX_ID_MM_BALL_CHOICE_MODE,
2701     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
2702     GADGET_ID_MM_BALL_CHOICE_MODE,      GADGET_ID_NONE,
2703     -1,
2704     options_group_choice_mode,
2705     &level.mm_ball_choice_mode,
2706     NULL, "choice type:", NULL,         "type of content choice"
2707   },
2708
2709   // ---------- element settings: configure 1 (custom elements) ---------------
2710
2711   {
2712     ED_SELECTBOX_ID_CUSTOM_ACCESS_TYPE,
2713     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(2),
2714     GADGET_ID_CUSTOM_ACCESS_TYPE,       GADGET_ID_NONE,
2715     -1,
2716     options_access_type,
2717     &custom_element.access_type,
2718     NULL, NULL, NULL,                   "type of access to this field"
2719   },
2720   {
2721     ED_SELECTBOX_ID_CUSTOM_ACCESS_LAYER,
2722     -1,                                 ED_ELEMENT_SETTINGS_YPOS(2),
2723     GADGET_ID_CUSTOM_ACCESS_LAYER,      GADGET_ID_CUSTOM_ACCESS_TYPE,
2724     -1,
2725     options_access_layer,
2726     &custom_element.access_layer,
2727     NULL, NULL, NULL,                   "layer of access for this field"
2728   },
2729   {
2730     ED_SELECTBOX_ID_CUSTOM_ACCESS_PROTECTED,
2731     -1,                                 ED_ELEMENT_SETTINGS_YPOS(2),
2732     GADGET_ID_CUSTOM_ACCESS_PROTECTED,  GADGET_ID_CUSTOM_ACCESS_LAYER,
2733     -1,
2734     options_access_protected,
2735     &custom_element.access_protected,
2736     NULL, NULL, NULL,                   "protected access for this field"
2737   },
2738   {
2739     ED_SELECTBOX_ID_CUSTOM_ACCESS_DIRECTION,
2740     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(3),
2741     GADGET_ID_CUSTOM_ACCESS_DIRECTION,  GADGET_ID_NONE,
2742     -1,
2743     options_access_direction,
2744     &custom_element.access_direction,
2745     NULL, "from", NULL,                 "access direction for this field"
2746   },
2747   {
2748     ED_SELECTBOX_ID_CUSTOM_WALK_TO_ACTION,
2749     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(4),
2750     GADGET_ID_CUSTOM_WALK_TO_ACTION,    GADGET_ID_NONE,
2751     -1,
2752     options_walk_to_action,
2753     &custom_element.walk_to_action,
2754     NULL, NULL, NULL,                   "diggable/collectible/pushable"
2755   },
2756
2757   // ---------- element settings: configure 2 (custom elements) ---------------
2758
2759   {
2760     ED_SELECTBOX_ID_CUSTOM_MOVE_PATTERN,
2761     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(1),
2762     GADGET_ID_CUSTOM_MOVE_PATTERN,      GADGET_ID_NONE,
2763     -1,
2764     options_move_pattern,
2765     &custom_element.move_pattern,
2766     NULL, "can move", NULL,             "element move pattern"
2767   },
2768   {
2769     ED_SELECTBOX_ID_CUSTOM_MOVE_DIRECTION,
2770     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(2),
2771     GADGET_ID_CUSTOM_MOVE_DIRECTION,    GADGET_ID_NONE,
2772     -1,
2773     options_move_direction,
2774     &custom_element.move_direction_initial,
2775     NULL, "starts moving", NULL,        "initial element move direction"
2776   },
2777   {
2778     ED_SELECTBOX_ID_CUSTOM_MOVE_STEPSIZE,
2779     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(4),
2780     GADGET_ID_CUSTOM_MOVE_STEPSIZE,     GADGET_ID_NONE,
2781     -1,
2782     options_move_stepsize,
2783     &custom_element.move_stepsize,
2784     NULL, "move/fall speed", NULL,      "speed of element movement"
2785   },
2786   {
2787     ED_SELECTBOX_ID_CUSTOM_MOVE_LEAVE_TYPE,
2788     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(3),
2789     GADGET_ID_CUSTOM_MOVE_LEAVE_TYPE,   GADGET_ID_NONE,
2790     -1,
2791     options_move_leave_type,
2792     &custom_element.move_leave_type,
2793     // left text with leading spaces to place gadget next to "can dig" gadget
2794     // (needed because drawing area gadgets created after selectbox gadgets)
2795     // NULL, "can dig:    can", ":",    "leave behind or change element"
2796     NULL, "            can", ":",       "leave behind or change element"
2797   },
2798   {
2799     ED_SELECTBOX_ID_CUSTOM_SMASH_TARGETS,
2800     -1,                                 ED_ELEMENT_SETTINGS_YPOS(8),
2801     GADGET_ID_CUSTOM_SMASH_TARGETS,     GADGET_ID_CUSTOM_CAN_SMASH,
2802     -1,
2803     options_smash_targets,
2804     &custom_element.smash_targets,
2805     NULL, "can smash", NULL,            "elements that can be smashed"
2806   },
2807   {
2808     ED_SELECTBOX_ID_CUSTOM_SLIPPERY_TYPE,
2809     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(9),
2810     GADGET_ID_CUSTOM_SLIPPERY_TYPE,     GADGET_ID_NONE,
2811     -1,
2812     options_slippery_type,
2813     &custom_element.slippery_type,
2814     NULL, "slippery", NULL,             "where other elements fall down"
2815   },
2816   {
2817     ED_SELECTBOX_ID_CUSTOM_DEADLINESS,
2818     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(10),
2819     GADGET_ID_CUSTOM_DEADLINESS,        GADGET_ID_NONE,
2820     -1,
2821     options_deadliness,
2822     &custom_element.deadliness,
2823     NULL, "deadly when", NULL,          "deadliness of element"
2824   },
2825   {
2826     ED_SELECTBOX_ID_CUSTOM_EXPLOSION_TYPE,
2827     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(11),
2828     GADGET_ID_CUSTOM_EXPLOSION_TYPE,    GADGET_ID_NONE,
2829     -1,
2830     options_explosion_type,
2831     &custom_element.explosion_type,
2832     NULL, "can explode", NULL,          "explosion type"
2833   },
2834
2835   // ---------- element settings: advanced (custom elements) ------------------
2836
2837   {
2838     ED_SELECTBOX_ID_CHANGE_TIME_UNITS,
2839     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(3),
2840     GADGET_ID_CHANGE_TIME_UNITS,        GADGET_ID_NONE,
2841     -1,
2842     options_time_units,
2843     &custom_element_change.delay_frames,
2844     NULL, "delay time given in", NULL,  "delay time units for change"
2845   },
2846   {
2847     ED_SELECTBOX_ID_CHANGE_DIRECT_ACTION,
2848     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(4),
2849     GADGET_ID_CHANGE_DIRECT_ACTION,     GADGET_ID_NONE,
2850     -1,
2851     options_change_direct_action,
2852     &custom_element_change.direct_action,
2853     NULL, NULL, NULL,                   "type of direct action"
2854   },
2855   {
2856     ED_SELECTBOX_ID_CHANGE_OTHER_ACTION,
2857     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(5),
2858     GADGET_ID_CHANGE_OTHER_ACTION,      GADGET_ID_NONE,
2859     -1,
2860     options_change_other_action,
2861     &custom_element_change.other_action,
2862     NULL, NULL, "element:",             "type of other element action"
2863   },
2864   {
2865     ED_SELECTBOX_ID_CHANGE_SIDE,
2866     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(6),
2867     GADGET_ID_CHANGE_SIDE,              GADGET_ID_NONE,
2868     -1,
2869     options_change_trigger_side,
2870     &custom_element_change.trigger_side,
2871     NULL, "at", "side",                 "element side triggering change"
2872   },
2873   {
2874     ED_SELECTBOX_ID_CHANGE_PLAYER,
2875     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(7),
2876     GADGET_ID_CHANGE_PLAYER,            GADGET_ID_NONE,
2877     -1,
2878     options_change_trigger_player,
2879     &custom_element_change.trigger_player,
2880     NULL, "player:", " ",               "player that causes change"
2881   },
2882   {
2883     ED_SELECTBOX_ID_CHANGE_PAGE,
2884     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(7),
2885     GADGET_ID_CHANGE_PAGE,              GADGET_ID_CHANGE_PLAYER,
2886     -1,
2887     options_change_trigger_page,
2888     &custom_element_change.trigger_page,
2889     NULL, "page:", NULL,                "change page that causes change"
2890   },
2891   {
2892     ED_SELECTBOX_ID_CHANGE_REPLACE_WHEN,
2893     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(10),
2894     GADGET_ID_CHANGE_REPLACE_WHEN,      GADGET_ID_NONE,
2895     -1,
2896     options_change_replace_when,
2897     &custom_element_change.replace_when,
2898     NULL, "replace when", NULL,         "which elements can be replaced"
2899   },
2900   {
2901     ED_SELECTBOX_ID_ACTION_TYPE,
2902     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(13),
2903     GADGET_ID_ACTION_TYPE,              GADGET_ID_NONE,
2904     15,
2905     options_action_type,
2906     &custom_element_change.action_type,
2907     NULL, NULL, NULL,                   "action on specified condition"
2908   },
2909   {
2910     ED_SELECTBOX_ID_ACTION_MODE,
2911     -1,                                 ED_ELEMENT_SETTINGS_YPOS(13),
2912     GADGET_ID_ACTION_MODE,              GADGET_ID_ACTION_TYPE,
2913     -1,
2914     options_action_mode_none,
2915     &custom_element_change.action_mode,
2916     NULL, NULL, NULL,                   "action operator"
2917   },
2918   {
2919     ED_SELECTBOX_ID_ACTION_ARG,
2920     -1,                                 ED_ELEMENT_SETTINGS_YPOS(13),
2921     GADGET_ID_ACTION_ARG,               GADGET_ID_ACTION_MODE,
2922     -1,
2923     options_action_arg_none,
2924     &custom_element_change.action_arg,
2925     NULL, NULL, NULL,                   "action parameter"
2926   },
2927   {
2928     ED_SELECTBOX_ID_SELECT_CHANGE_PAGE,
2929     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(14),
2930     GADGET_ID_SELECT_CHANGE_PAGE,       GADGET_ID_NONE,
2931     3,
2932     options_change_page,
2933     &custom_element.current_change_page,
2934     NULL, NULL, NULL,                   "element change page"
2935   },
2936
2937   // ---------- element settings: configure (group elements) ------------------
2938
2939   {
2940     ED_SELECTBOX_ID_GROUP_CHOICE_MODE,
2941     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
2942     GADGET_ID_GROUP_CHOICE_MODE,        GADGET_ID_NONE,
2943     -1,
2944     options_group_choice_mode,
2945     &group_element_info.choice_mode,
2946     NULL, "choice type:", NULL,         "type of group element choice"
2947   },
2948 };
2949
2950 static struct
2951 {
2952   int gadget_type_id;
2953   int x, y;
2954   int gadget_id;
2955   int gadget_id_align;
2956   int size;
2957   char *text;
2958   char *text_above, *text_left, *text_right, *infotext;
2959 } textbutton_info[ED_NUM_TEXTBUTTONS] =
2960 {
2961   // ---------- level and editor settings (tabs) ------------------------------
2962
2963   {
2964     ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL,
2965     ED_LEVEL_TABS_XPOS(0),              ED_LEVEL_TABS_YPOS(0),
2966     GADGET_ID_LEVELCONFIG_LEVEL,        GADGET_ID_NONE,
2967     8,                                  "Level",
2968     NULL, NULL, NULL,                   "Configure level settings"
2969   },
2970   {
2971     ED_TEXTBUTTON_ID_LEVELCONFIG_LEVELSET,
2972     -1,                                 -1,
2973     GADGET_ID_LEVELCONFIG_LEVELSET,     GADGET_ID_LEVELCONFIG_LEVEL,
2974     8,                                  "Levelset",
2975     NULL, NULL, NULL,                   "Update this or create new level set"
2976   },
2977   {
2978     ED_TEXTBUTTON_ID_LEVELCONFIG_EDITOR,
2979     -1,                                 -1,
2980     GADGET_ID_LEVELCONFIG_EDITOR,       GADGET_ID_LEVELCONFIG_LEVELSET,
2981     8,                                  "Editor",
2982     NULL, NULL, NULL,                   "Configure editor settings"
2983   },
2984   {
2985     ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE,
2986     -1,                                 -1,
2987     GADGET_ID_LEVELCONFIG_ENGINE,       GADGET_ID_LEVELCONFIG_EDITOR,
2988     8,                                  "Engine",
2989     NULL, NULL, NULL,                   "Configure engine settings"
2990   },
2991
2992   // ---------- element settings (tabs) ---------------------------------------
2993
2994   {
2995     ED_TEXTBUTTON_ID_PROPERTIES_INFO,
2996     ED_ELEMENT_TABS_XPOS(0),            ED_ELEMENT_TABS_YPOS(0),
2997     GADGET_ID_PROPERTIES_INFO,          GADGET_ID_NONE,
2998     8,                                  "Info",
2999     NULL, NULL, NULL,                   "Show information about element"
3000   },
3001   {
3002     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG,
3003     -1,                                 -1,
3004     GADGET_ID_PROPERTIES_CONFIG,        GADGET_ID_PROPERTIES_INFO,
3005     8,                                  "Config",
3006     NULL, NULL, NULL,                   "Configure element properties"
3007   },
3008   {
3009     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_1,
3010     -1,                                 -1,
3011     GADGET_ID_PROPERTIES_CONFIG_1,      GADGET_ID_PROPERTIES_INFO,
3012     8,                                  "Config 1",
3013     NULL, NULL, NULL,                   "Configure element properties, part 1"
3014   },
3015   {
3016     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2,
3017     -1,                                 -1,
3018     GADGET_ID_PROPERTIES_CONFIG_2,      GADGET_ID_PROPERTIES_CONFIG_1,
3019     8,                                  "Config 2",
3020     NULL, NULL, NULL,                   "Configure element properties, part 2"
3021   },
3022   {
3023     ED_TEXTBUTTON_ID_PROPERTIES_CHANGE,
3024     -1,                                 -1,
3025     GADGET_ID_PROPERTIES_CHANGE,        GADGET_ID_PROPERTIES_CONFIG_2,
3026     8,                                  "Change",
3027     NULL, NULL, NULL,                   "Configure custom element change pages"
3028   },
3029
3030   // ---------- level and editor settings (buttons) ---------------------------
3031
3032   {
3033     ED_TEXTBUTTON_ID_SAVE_LEVELSET,
3034     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(10),
3035     GADGET_ID_SAVE_LEVELSET,            GADGET_ID_LEVELSET_SAVE_MODE,
3036     -1,                                 "Save",
3037     NULL, NULL, NULL,                   "Update or create level set"
3038   },
3039   {
3040     ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2,
3041     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(6),
3042     GADGET_ID_SAVE_AS_TEMPLATE_2,       GADGET_ID_NONE,
3043     -1,                                 "Save",
3044     NULL, NULL,                         "this level as level template",
3045     "Save current settings as new template"
3046   },
3047
3048   // ---------- element settings (buttons) ------------------------------------
3049
3050   {
3051     ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1,
3052     -1,                                 -1,
3053     GADGET_ID_SAVE_AS_TEMPLATE_1,       GADGET_ID_CUSTOM_USE_TEMPLATE_1,
3054     -1,                                 "Save",
3055     NULL, " ",                          "As Template",
3056     "Save current settings as new template"
3057   },
3058   {
3059     ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE,
3060     -1,                                 -1,
3061     GADGET_ID_ADD_CHANGE_PAGE,          GADGET_ID_PASTE_CHANGE_PAGE,
3062     -1,                                 "New",
3063     NULL, NULL, NULL,                   "Add new change page"
3064   },
3065   {
3066     ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE,
3067     -1,                                 -1,
3068     GADGET_ID_DEL_CHANGE_PAGE,          GADGET_ID_ADD_CHANGE_PAGE,
3069     -1,                                 "Delete",
3070     NULL, NULL, NULL,                   "Delete current change page"
3071   },
3072 };
3073
3074 static struct
3075 {
3076   int gadget_type_id;
3077   int graphic;
3078   int x, y;
3079   int gadget_id;
3080   int gadget_id_align;
3081   char *text_left, *text_right, *infotext;
3082 } graphicbutton_info[ED_NUM_GRAPHICBUTTONS] =
3083 {
3084   {
3085     ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE,
3086     IMG_EDITOR_COUNTER_DOWN,
3087     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(14),
3088     GADGET_ID_PREV_CHANGE_PAGE,         GADGET_ID_NONE,
3089     NULL, NULL,                         "select previous change page"
3090   },
3091   {
3092     ED_GRAPHICBUTTON_ID_NEXT_CHANGE_PAGE,
3093     IMG_EDITOR_COUNTER_UP,
3094     -1,                                 ED_ELEMENT_SETTINGS_YPOS(14),
3095     GADGET_ID_NEXT_CHANGE_PAGE,         GADGET_ID_SELECT_CHANGE_PAGE,
3096     NULL, "change page",                "select next change page"
3097   },
3098   {
3099     ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE,
3100     IMG_GFX_EDITOR_BUTTON_CP_COPY,
3101     -1,                                 ED_ELEMENT_SETTINGS_YPOS(14),
3102     GADGET_ID_COPY_CHANGE_PAGE,         GADGET_ID_NEXT_CHANGE_PAGE,
3103     " ", NULL,                          "copy settings from this change page"
3104   },
3105   {
3106     ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE,
3107     IMG_GFX_EDITOR_BUTTON_CP_PASTE,
3108     -1,                                 ED_ELEMENT_SETTINGS_YPOS(14),
3109     GADGET_ID_PASTE_CHANGE_PAGE,        GADGET_ID_COPY_CHANGE_PAGE,
3110     NULL, NULL,                         "paste settings to this change page"
3111   },
3112 };
3113
3114 static struct
3115 {
3116   int x, y;
3117 } scrollbutton_pos[ED_NUM_SCROLLBUTTONS];
3118
3119 static struct
3120 {
3121   int gadget_type_id;
3122   int graphic;
3123   int gadget_id;
3124   char *infotext;
3125 } scrollbutton_info[ED_NUM_SCROLLBUTTONS] =
3126 {
3127   {
3128     ED_SCROLLBUTTON_ID_AREA_UP,
3129     IMG_EDITOR_PLAYFIELD_SCROLL_UP,
3130     GADGET_ID_SCROLL_UP,
3131     "scroll level editing area up"
3132   },
3133   {
3134     ED_SCROLLBUTTON_ID_AREA_DOWN,
3135     IMG_EDITOR_PLAYFIELD_SCROLL_DOWN,
3136     GADGET_ID_SCROLL_DOWN,
3137     "scroll level editing area down"
3138   },
3139   {
3140     ED_SCROLLBUTTON_ID_AREA_LEFT,
3141     IMG_EDITOR_PLAYFIELD_SCROLL_LEFT,
3142     GADGET_ID_SCROLL_LEFT,
3143     "scroll level editing area left"
3144   },
3145   {
3146     ED_SCROLLBUTTON_ID_AREA_RIGHT,
3147     IMG_EDITOR_PLAYFIELD_SCROLL_RIGHT,
3148     GADGET_ID_SCROLL_RIGHT,
3149     "scroll level editing area right"
3150   },
3151   {
3152     ED_SCROLLBUTTON_ID_LIST_UP,
3153     IMG_EDITOR_PALETTE_SCROLL_UP,
3154     GADGET_ID_SCROLL_LIST_UP,
3155     "scroll element list up ('Page Up')"
3156   },
3157   {
3158     ED_SCROLLBUTTON_ID_LIST_DOWN,
3159     IMG_EDITOR_PALETTE_SCROLL_DOWN,
3160     GADGET_ID_SCROLL_LIST_DOWN,
3161     "scroll element list down ('Page Down')"
3162   },
3163 };
3164
3165 static struct
3166 {
3167   int x, y;
3168   int width, height;
3169   int wheel_x, wheel_y;
3170   int wheel_width, wheel_height;
3171 } scrollbar_pos[ED_NUM_SCROLLBARS];
3172
3173 static struct
3174 {
3175   int gadget_type_id;
3176   int graphic;
3177   int type;
3178   int gadget_id;
3179   char *infotext;
3180 } scrollbar_info[ED_NUM_SCROLLBARS] =
3181 {
3182   {
3183     ED_SCROLLBAR_ID_AREA_HORIZONTAL,
3184     IMG_EDITOR_PLAYFIELD_SCROLLBAR,
3185     GD_TYPE_SCROLLBAR_HORIZONTAL,
3186     GADGET_ID_SCROLL_HORIZONTAL,
3187     "scroll level editing area horizontally"
3188   },
3189   {
3190     ED_SCROLLBAR_ID_AREA_VERTICAL,
3191     IMG_EDITOR_PLAYFIELD_SCROLLBAR,
3192     GD_TYPE_SCROLLBAR_VERTICAL,
3193     GADGET_ID_SCROLL_VERTICAL,
3194     "scroll level editing area vertically"
3195   },
3196   {
3197     ED_SCROLLBAR_ID_LIST_VERTICAL,
3198     IMG_EDITOR_PALETTE_SCROLLBAR,
3199     GD_TYPE_SCROLLBAR_VERTICAL,
3200     GADGET_ID_SCROLL_LIST_VERTICAL,
3201     "scroll element list vertically"
3202   }
3203 };
3204
3205
3206 static struct
3207 {
3208   int gadget_type_id;
3209   int x, y;
3210   int gadget_id;
3211   int gadget_id_align;
3212   int radio_button_nr;
3213   int *value;
3214   int checked_value;
3215   char *text_left, *text_right, *infotext;
3216 } radiobutton_info[ED_NUM_RADIOBUTTONS] =
3217 {
3218   {
3219     ED_RADIOBUTTON_ID_PERCENTAGE,
3220     -1,                                 ED_LEVEL_SETTINGS_YPOS(0),
3221     GADGET_ID_RANDOM_PERCENTAGE,        GADGET_ID_LEVEL_RANDOM_UP,
3222     RADIO_NR_RANDOM_ELEMENTS,
3223     &random_placement_method,           RANDOM_USE_PERCENTAGE,
3224     " ", "percentage",                  "use percentage for random elements"
3225   },
3226   {
3227     ED_RADIOBUTTON_ID_QUANTITY,
3228     -1,                                 ED_LEVEL_SETTINGS_YPOS(0),
3229     GADGET_ID_RANDOM_QUANTITY,          GADGET_ID_RANDOM_PERCENTAGE,
3230     RADIO_NR_RANDOM_ELEMENTS,
3231     &random_placement_method,           RANDOM_USE_QUANTITY,
3232     " ", "quantity",                    "use quantity for random elements"
3233   }
3234 };
3235
3236 static struct
3237 {
3238   int gadget_type_id;
3239   int x, y;
3240   int gadget_id;
3241   int gadget_id_align;
3242   boolean *value;
3243   char *text_above, *text_left, *text_right, *infotext;
3244 } checkbutton_info[ED_NUM_CHECKBUTTONS] =
3245 {
3246   // ---------- level and editor settings -------------------------------------
3247
3248   {
3249     ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS,
3250     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(6),
3251     GADGET_ID_AUTO_COUNT_GEMS,          GADGET_ID_NONE,
3252     &level.auto_count_gems,
3253     NULL, NULL,
3254     "automatically count gems needed",  "set counter to number of gems"
3255   },
3256   {
3257     ED_CHECKBUTTON_ID_RATE_TIME_OVER_SCORE,
3258     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(11),
3259     GADGET_ID_RATE_TIME_OVER_SCORE,     GADGET_ID_NONE,
3260     &level.rate_time_over_score,
3261     NULL, NULL,
3262     "rate time/steps used over score",  "sort high scores by playing time/steps"
3263   },
3264   {
3265     ED_CHECKBUTTON_ID_USE_LEVELSET_ARTWORK,
3266     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(7),
3267     GADGET_ID_USE_LEVELSET_ARTWORK,     GADGET_ID_NONE,
3268     &levelset_use_levelset_artwork,
3269     NULL, NULL,
3270     "use current custom artwork",       "use custom artwork of this level set"
3271   },
3272   {
3273     ED_CHECKBUTTON_ID_COPY_LEVEL_TEMPLATE,
3274     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(8),
3275     GADGET_ID_COPY_LEVEL_TEMPLATE,      GADGET_ID_NONE,
3276     &levelset_copy_level_template,
3277     NULL, NULL,
3278     "copy current level template",      "copy level template of this level set"
3279   },
3280   {
3281     ED_CHECKBUTTON_ID_RANDOM_RESTRICTED,
3282     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(1),
3283     GADGET_ID_RANDOM_RESTRICTED,        GADGET_ID_NONE,
3284     &random_placement_background_restricted,
3285     NULL, NULL,
3286     "restrict random placement to:",    "set random placement restriction"
3287   },
3288   {
3289     ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_3,
3290     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
3291     GADGET_ID_CUSTOM_USE_TEMPLATE_3,    GADGET_ID_NONE,
3292     &setup.editor.use_template_for_new_levels,
3293     "Template for new levels and CE/GE:", NULL,
3294     "use template for new levels",      "use template for level properties"
3295   },
3296   {
3297     ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2,
3298     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(5),
3299     GADGET_ID_CUSTOM_USE_TEMPLATE_2,    GADGET_ID_NONE,
3300     &level.use_custom_template,
3301     NULL, NULL,
3302     "use template for custom elements", "use template for custom properties"
3303   },
3304   {
3305     ED_CHECKBUTTON_ID_BD_INTERMISSION,
3306     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(0),
3307     GADGET_ID_BD_INTERMISSION,          GADGET_ID_NONE,
3308     &level.bd_intermission,
3309     "Boulder Dash game engine settings:", NULL,
3310     "intermission",                     "level is an intermission level"
3311   },
3312   {
3313     ED_CHECKBUTTON_ID_BD_PAL_TIMING,
3314     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(2),
3315     GADGET_ID_BD_PAL_TIMING,            GADGET_ID_NONE,
3316     &level.bd_pal_timing,
3317     NULL, NULL,
3318     "PAL timing",                       "use slower timer (like PAL C64)"
3319   },
3320
3321   // ---------- element settings: configure (various elements) ----------------
3322
3323   {
3324     ED_CHECKBUTTON_ID_STICK_ELEMENT,
3325     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3326     GADGET_ID_STICK_ELEMENT,            GADGET_ID_NONE,
3327     &stick_element_properties_window,
3328     NULL, NULL,
3329     "stick this screen to edit content","stick this screen to edit content"
3330   },
3331   {
3332     ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS,
3333     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3334     GADGET_ID_EM_SLIPPERY_GEMS,         GADGET_ID_NONE,
3335     &level.em_slippery_gems,
3336     NULL, NULL,
3337     "slip down from certain flat walls","use EM/DC style slipping behaviour"
3338   },
3339   {
3340     ED_CHECKBUTTON_ID_EM_EXPLODES_BY_FIRE,
3341     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3342     GADGET_ID_EM_EXPLODES_BY_FIRE,      GADGET_ID_NONE,
3343     &level.em_explodes_by_fire,
3344     NULL, NULL,
3345     "explodes with chain reaction",     "use R'n'D style explosion behaviour"
3346   },
3347   {
3348     ED_CHECKBUTTON_ID_USE_SPRING_BUG,
3349     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3350     GADGET_ID_USE_SPRING_BUG,           GADGET_ID_NONE,
3351     &level.use_spring_bug,
3352     NULL, NULL,
3353     "use spring pushing bug",           "use odd spring pushing behaviour"
3354   },
3355   {
3356     ED_CHECKBUTTON_ID_USE_TIME_ORB_BUG,
3357     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3358     GADGET_ID_USE_TIME_ORB_BUG,         GADGET_ID_NONE,
3359     &level.use_time_orb_bug,
3360     NULL, NULL,
3361     "use time orb bug",                 "use odd time orb behaviour"
3362   },
3363   {
3364     ED_CHECKBUTTON_ID_USE_LIFE_BUGS,
3365     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(5),
3366     GADGET_ID_USE_LIFE_BUGS,            GADGET_ID_NONE,
3367     &level.use_life_bugs,
3368     NULL, NULL,
3369     "use buggy element behaviour",      "use odd (historic) element behaviour"
3370   },
3371   {
3372     ED_CHECKBUTTON_ID_RANDOM_BALL_CONTENT,
3373     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3374     GADGET_ID_RANDOM_BALL_CONTENT,      GADGET_ID_NONE,
3375     &level.ball_random,
3376     NULL, NULL,
3377     "create single random element",     "only create one element from content"
3378   },
3379   {
3380     ED_CHECKBUTTON_ID_INITIAL_BALL_ACTIVE,
3381     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3382     GADGET_ID_INITIAL_BALL_ACTIVE,      GADGET_ID_NONE,
3383     &level.ball_active_initial,
3384     NULL, NULL,
3385     "magic ball initially activated",   "activate magic ball after level start"
3386   },
3387   {
3388     ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE,
3389     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3390     GADGET_ID_GROW_INTO_DIGGABLE,       GADGET_ID_NONE,
3391     &level.grow_into_diggable,
3392     NULL, NULL,
3393     "can grow into anything diggable",  "grow into more than just sand"
3394   },
3395   {
3396     ED_CHECKBUTTON_ID_SB_FIELDS_NEEDED,
3397     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3398     GADGET_ID_SB_FIELDS_NEEDED,         GADGET_ID_NONE,
3399     &level.sb_fields_needed,
3400     NULL, NULL,
3401     "all fields need to be filled",     "require all SB fields to be solved"
3402   },
3403   {
3404     ED_CHECKBUTTON_ID_SB_OBJECTS_NEEDED,
3405     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3406     GADGET_ID_SB_OBJECTS_NEEDED,        GADGET_ID_NONE,
3407     &level.sb_objects_needed,
3408     NULL, NULL,
3409     "all objects need to be placed",    "require all SB objects to be solved"
3410   },
3411   {
3412     ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN,
3413     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3414     GADGET_ID_AUTO_EXIT_SOKOBAN,        GADGET_ID_NONE,
3415     &level.auto_exit_sokoban,
3416     NULL, NULL,
3417     "exit level if all tasks solved",   "automatically finish Sokoban levels"
3418   },
3419   {
3420     ED_CHECKBUTTON_ID_SOLVED_BY_ONE_PLAYER,
3421     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(14),
3422     GADGET_ID_SOLVED_BY_ONE_PLAYER,     GADGET_ID_NONE,
3423     &level.solved_by_one_player,
3424     NULL, NULL,
3425     "only one player must enter exit",  "level solved by first player in exit"
3426   },
3427   {
3428     ED_CHECKBUTTON_ID_FINISH_DIG_COLLECT,
3429     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
3430     GADGET_ID_FINISH_DIG_COLLECT,       GADGET_ID_NONE,
3431     &level.finish_dig_collect,
3432     NULL, NULL,
3433     "CE action on finished dig/collect", "only finished dig/collect triggers CE"
3434   },
3435   {
3436     ED_CHECKBUTTON_ID_KEEP_WALKABLE_CE,
3437     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
3438     GADGET_ID_KEEP_WALKABLE_CE,         GADGET_ID_NONE,
3439     &level.keep_walkable_ce,
3440     NULL, NULL,
3441     "keep walkable CE changed to player", "keep CE changing to player if walkable"
3442   },
3443   {
3444     ED_CHECKBUTTON_ID_CONTINUOUS_SNAPPING,
3445     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(9),
3446     GADGET_ID_CONTINUOUS_SNAPPING,      GADGET_ID_NONE,
3447     &level.continuous_snapping,
3448     NULL, NULL,
3449     "continuous snapping",              "use snapping without releasing key"
3450   },
3451   {
3452     ED_CHECKBUTTON_ID_BLOCK_SNAP_FIELD,
3453     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(8),
3454     GADGET_ID_BLOCK_SNAP_FIELD,         GADGET_ID_NONE,
3455     &level.block_snap_field,
3456     NULL, NULL,
3457     "block snapped field when snapping", "use snapping delay to show animation"
3458   },
3459   {
3460     ED_CHECKBUTTON_ID_BLOCK_LAST_FIELD,
3461     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3462     GADGET_ID_BLOCK_LAST_FIELD,         GADGET_ID_NONE,
3463     &level.block_last_field,
3464     NULL, NULL,
3465     "block last field when moving",     "player blocks last field when moving"
3466   },
3467   {
3468     ED_CHECKBUTTON_ID_SP_BLOCK_LAST_FIELD,
3469     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3470     GADGET_ID_SP_BLOCK_LAST_FIELD,      GADGET_ID_NONE,
3471     &level.sp_block_last_field,
3472     NULL, NULL,
3473     "block last field when moving",     "player blocks last field when moving"
3474   },
3475   {
3476     ED_CHECKBUTTON_ID_INSTANT_RELOCATION,
3477     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
3478     GADGET_ID_INSTANT_RELOCATION,       GADGET_ID_NONE,
3479     &level.instant_relocation,
3480     NULL, NULL,
3481     "no scrolling when relocating",     "player gets relocated without delay"
3482   },
3483   {
3484     ED_CHECKBUTTON_ID_SHIFTED_RELOCATION,
3485     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
3486     GADGET_ID_SHIFTED_RELOCATION,       GADGET_ID_NONE,
3487     &level.shifted_relocation,
3488     NULL, NULL,
3489     "no centering when relocating",     "level not centered after relocation"
3490   },
3491   {
3492     ED_CHECKBUTTON_ID_LAZY_RELOCATION,
3493     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(5),
3494     GADGET_ID_LAZY_RELOCATION,          GADGET_ID_NONE,
3495     &level.lazy_relocation,
3496     NULL, NULL,
3497     "only redraw off-screen relocation","no redraw if relocation target visible"
3498   },
3499   {
3500     ED_CHECKBUTTON_ID_USE_START_ELEMENT,
3501     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(10),
3502     GADGET_ID_USE_START_ELEMENT,        GADGET_ID_NONE,
3503     &level.use_start_element[0],
3504     NULL, NULL,
3505     "use level start element:",        "start level at this element's position"
3506   },
3507   {
3508     ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT,
3509     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(11),
3510     GADGET_ID_USE_ARTWORK_ELEMENT,      GADGET_ID_NONE,
3511     &level.use_artwork_element[0],
3512     NULL, NULL,
3513     "use artwork from element:",        "use player artwork from other element"
3514   },
3515   {
3516     ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT,
3517     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(12),
3518     GADGET_ID_USE_EXPLOSION_ELEMENT,    GADGET_ID_NONE,
3519     &level.use_explosion_element[0],
3520     NULL, NULL,
3521     "use explosion from element:",      "use explosion properties from element"
3522   },
3523   {
3524     ED_CHECKBUTTON_ID_INITIAL_GRAVITY,
3525     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(13),
3526     GADGET_ID_INITIAL_GRAVITY,          GADGET_ID_NONE,
3527     &level.initial_player_gravity[0],
3528     NULL, NULL,
3529     "use initial gravity",              "set initial player gravity"
3530   },
3531   {
3532     ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY,
3533     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3534     GADGET_ID_USE_INITIAL_INVENTORY,    GADGET_ID_NONE,
3535     &level.use_initial_inventory[0],
3536     NULL, NULL,
3537     "use initial inventory:",           "use collected elements on level start"
3538   },
3539   {
3540     ED_CHECKBUTTON_ID_CAN_PASS_TO_WALKABLE,
3541     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(6),
3542     GADGET_ID_CAN_PASS_TO_WALKABLE,     GADGET_ID_NONE,
3543     &level.can_pass_to_walkable,
3544     NULL, NULL,
3545     "can pass to walkable element",     "player can pass to empty or walkable"
3546   },
3547   {
3548     ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID,
3549     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3550     GADGET_ID_CAN_FALL_INTO_ACID,       GADGET_ID_NONE,
3551     &custom_element_properties[EP_CAN_MOVE_INTO_ACID],
3552     NULL, NULL,
3553     "can fall into acid (with gravity)","player can fall into acid pool"
3554   },
3555   {
3556     ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID,
3557     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3558     GADGET_ID_CAN_MOVE_INTO_ACID,       GADGET_ID_NONE,
3559     &custom_element_properties[EP_CAN_MOVE_INTO_ACID],
3560     NULL, NULL,
3561     "can move into acid",               "element can move into acid pool"
3562   },
3563   {
3564     ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH,
3565     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3566     GADGET_ID_DONT_COLLIDE_WITH,        GADGET_ID_NONE,
3567     &custom_element_properties[EP_DONT_COLLIDE_WITH],
3568     NULL, NULL,
3569     "deadly when colliding with",       "element is deadly when hitting player"
3570   },
3571   {
3572     ED_CHECKBUTTON_ID_BD_DIAGONAL_MOVEMENTS,
3573     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3574     GADGET_ID_BD_DIAGONAL_MOVEMENTS,    GADGET_ID_NONE,
3575     &level.bd_diagonal_movements,
3576     NULL, NULL,
3577     "can move diagonally",              "player can move diagonally"
3578   },
3579   {
3580     ED_CHECKBUTTON_ID_ENVELOPE_AUTOWRAP,
3581     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3582     GADGET_ID_ENVELOPE_AUTOWRAP,        GADGET_ID_NONE,
3583     &level.envelope[0].autowrap,
3584     NULL, NULL,
3585     "auto-wrap",                        "automatically wrap envelope text"
3586   },
3587   {
3588     ED_CHECKBUTTON_ID_ENVELOPE_CENTERED,
3589     -1,                                 ED_ELEMENT_SETTINGS_YPOS(1),
3590     GADGET_ID_ENVELOPE_CENTERED,        GADGET_ID_ENVELOPE_AUTOWRAP,
3591     &level.envelope[0].centered,
3592     NULL, " ",
3593     "centered",                         "automatically center envelope text"
3594   },
3595   {
3596     ED_CHECKBUTTON_ID_MM_LASER_RED,
3597     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3598     GADGET_ID_MM_LASER_RED,             GADGET_ID_NONE,
3599     &level.mm_laser_red,
3600     "choose color components for laser:", NULL,
3601     "red",                              "use red color components in laser"
3602   },
3603   {
3604     ED_CHECKBUTTON_ID_MM_LASER_GREEN,
3605     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3606     GADGET_ID_MM_LASER_GREEN,           GADGET_ID_NONE,
3607     &level.mm_laser_green,
3608     NULL, NULL,
3609     "green",                            "use green color components in laser"
3610   },
3611   {
3612     ED_CHECKBUTTON_ID_MM_LASER_BLUE,
3613     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
3614     GADGET_ID_MM_LASER_BLUE,            GADGET_ID_NONE,
3615     &level.mm_laser_blue,
3616     NULL, NULL,
3617     "blue",                             "use blue color components in laser"
3618   },
3619   {
3620     ED_CHECKBUTTON_ID_DF_LASER_RED,
3621     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3622     GADGET_ID_DF_LASER_RED,             GADGET_ID_NONE,
3623     &level.df_laser_red,
3624     "choose color components for laser:", NULL,
3625     "red",                              "use red color components in laser"
3626   },
3627   {
3628     ED_CHECKBUTTON_ID_DF_LASER_GREEN,
3629     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3630     GADGET_ID_DF_LASER_GREEN,           GADGET_ID_NONE,
3631     &level.df_laser_green,
3632     NULL, NULL,
3633     "green",                            "use green color components in laser"
3634   },
3635   {
3636     ED_CHECKBUTTON_ID_DF_LASER_BLUE,
3637     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
3638     GADGET_ID_DF_LASER_BLUE,            GADGET_ID_NONE,
3639     &level.df_laser_blue,
3640     NULL, NULL,
3641     "blue",                             "use blue color components in laser"
3642   },
3643   {
3644     ED_CHECKBUTTON_ID_ROTATE_MM_BALL_CONTENT,
3645     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(5),
3646     GADGET_ID_ROTATE_MM_BALL_CONTENT,   GADGET_ID_NONE,
3647     &level.rotate_mm_ball_content,
3648     NULL, NULL,
3649     "randomly rotate created content",  "randomly rotate newly created content"
3650   },
3651   {
3652     ED_CHECKBUTTON_ID_EXPLODE_MM_BALL,
3653     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(6),
3654     GADGET_ID_EXPLODE_MM_BALL,          GADGET_ID_NONE,
3655     &level.explode_mm_ball,
3656     NULL, NULL,
3657     "explode ball instead of melting",  "use explosion to release ball content"
3658   },
3659
3660   // ---------- element settings: configure 1 (custom elements) ---------------
3661
3662   {
3663     ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC,
3664     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3665     GADGET_ID_CUSTOM_USE_GRAPHIC,       GADGET_ID_NONE,
3666     &custom_element.use_gfx_element,
3667     NULL, NULL,
3668     "use graphic of element:",          "use existing element graphic"
3669   },
3670   {
3671     ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1,
3672     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(14),
3673     GADGET_ID_CUSTOM_USE_TEMPLATE_1,    GADGET_ID_NONE,
3674     &level.use_custom_template,
3675     NULL, NULL,
3676     "use template",                     "use template for custom properties"
3677   },
3678   {
3679     ED_CHECKBUTTON_ID_CUSTOM_ACCESSIBLE,
3680     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3681     GADGET_ID_CUSTOM_ACCESSIBLE,        GADGET_ID_NONE,
3682     &custom_element_properties[EP_ACCESSIBLE],
3683     NULL, NULL,
3684     NULL,                               "player can walk to or pass this field"
3685   },
3686   {
3687     ED_CHECKBUTTON_ID_CUSTOM_GRAV_REACHABLE,
3688     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(9),
3689     GADGET_ID_CUSTOM_GRAV_REACHABLE,    GADGET_ID_NONE,
3690     &custom_element_properties[EP_GRAVITY_REACHABLE],
3691     NULL, NULL,
3692     "reachable despite gravity",        "player can walk/dig despite gravity"
3693   },
3694   {
3695     ED_CHECKBUTTON_ID_CUSTOM_USE_LAST_VALUE,
3696     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(11),
3697     GADGET_ID_CUSTOM_USE_LAST_VALUE,    GADGET_ID_NONE,
3698     &custom_element.use_last_ce_value,
3699     NULL, NULL,
3700     "use last CE value after change",   "use last CE value after change"
3701   },
3702   {
3703     ED_CHECKBUTTON_ID_CUSTOM_WALK_TO_OBJECT,
3704     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
3705     GADGET_ID_CUSTOM_WALK_TO_OBJECT,    GADGET_ID_NONE,
3706     &custom_element_properties[EP_WALK_TO_OBJECT],
3707     NULL, NULL,
3708     NULL,                               "player can dig/collect/push element"
3709   },
3710   {
3711     ED_CHECKBUTTON_ID_CUSTOM_INDESTRUCTIBLE,
3712     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(8),
3713     GADGET_ID_CUSTOM_INDESTRUCTIBLE,    GADGET_ID_NONE,
3714     &custom_element_properties[EP_INDESTRUCTIBLE],
3715     NULL, NULL,
3716     "indestructible",                   "element is indestructible"
3717   },
3718
3719   // ---------- element settings: configure 2 (custom elements) ---------------
3720
3721   {
3722     ED_CHECKBUTTON_ID_CUSTOM_CAN_MOVE,
3723     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3724     GADGET_ID_CUSTOM_CAN_MOVE,          GADGET_ID_NONE,
3725     &custom_element_properties[EP_CAN_MOVE],
3726     NULL, NULL,
3727     NULL,                               "element can move with some pattern"
3728   },
3729   {
3730     ED_CHECKBUTTON_ID_CUSTOM_CAN_FALL,
3731     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(8),
3732     GADGET_ID_CUSTOM_CAN_FALL,          GADGET_ID_NONE,
3733     &custom_element_properties[EP_CAN_FALL],
3734     NULL, NULL,
3735     "can fall",                         "element can fall down"
3736   },
3737   {
3738     ED_CHECKBUTTON_ID_CUSTOM_CAN_SMASH,
3739     -1,                                 ED_ELEMENT_SETTINGS_YPOS(8),
3740     GADGET_ID_CUSTOM_CAN_SMASH,         GADGET_ID_CUSTOM_CAN_FALL,
3741     &custom_element_properties[EP_CAN_SMASH],
3742     NULL, " ",
3743     NULL,                               "element can smash other elements"
3744   },
3745   {
3746     ED_CHECKBUTTON_ID_CUSTOM_SLIPPERY,
3747     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(9),
3748     GADGET_ID_CUSTOM_SLIPPERY,          GADGET_ID_NONE,
3749     &custom_element_properties[EP_SLIPPERY],
3750     NULL, NULL,
3751     NULL,                               "other elements can fall down from it"
3752   },
3753   {
3754     ED_CHECKBUTTON_ID_CUSTOM_DEADLY,
3755     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(10),
3756     GADGET_ID_CUSTOM_DEADLY,            GADGET_ID_NONE,
3757     &custom_element_properties[EP_DEADLY],
3758     NULL, NULL,
3759     NULL,                               "element can kill the player"
3760   },
3761   {
3762     ED_CHECKBUTTON_ID_CUSTOM_CAN_EXPLODE,
3763     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(11),
3764     GADGET_ID_CUSTOM_CAN_EXPLODE,       GADGET_ID_NONE,
3765     &custom_element_properties[EP_CAN_EXPLODE],
3766     NULL, NULL,
3767     NULL,                               "element can explode"
3768   },
3769   {
3770     ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_FIRE,
3771     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(12),
3772     GADGET_ID_CUSTOM_EXPLODE_FIRE,      GADGET_ID_NONE,
3773     &custom_element_properties[EP_EXPLODES_BY_FIRE],
3774     NULL, NULL,
3775     "by fire",                          "element can explode by fire/explosion"
3776   },
3777   {
3778     ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_SMASH,
3779     -1,                                 ED_ELEMENT_SETTINGS_YPOS(12),
3780     GADGET_ID_CUSTOM_EXPLODE_SMASH,     GADGET_ID_CUSTOM_EXPLODE_FIRE,
3781     &custom_element_properties[EP_EXPLODES_SMASHED],
3782     NULL, " ",
3783     "smashed",                          "element can explode when smashed"
3784   },
3785   {
3786     ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_IMPACT,
3787     -1,                                 ED_ELEMENT_SETTINGS_YPOS(12),
3788     GADGET_ID_CUSTOM_EXPLODE_IMPACT,    GADGET_ID_CUSTOM_EXPLODE_SMASH,
3789     &custom_element_properties[EP_EXPLODES_IMPACT],
3790     NULL, " ",
3791     "impact",                           "element can explode on impact"
3792   },
3793
3794   // ---------- element settings: advanced (custom elements) ------------------
3795
3796   {
3797     ED_CHECKBUTTON_ID_CUSTOM_CAN_CHANGE,
3798     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3799     GADGET_ID_CUSTOM_CAN_CHANGE,        GADGET_ID_NONE,
3800     &custom_element_change.can_change,
3801     NULL, NULL,
3802     "element changes to:",              "change element on specified condition"
3803   },
3804   {
3805     ED_CHECKBUTTON_ID_CHANGE_DELAY,
3806     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(2),
3807     GADGET_ID_CHANGE_DELAY,             GADGET_ID_NONE,
3808     &custom_element_change_events[CE_DELAY],
3809     NULL, NULL,
3810     NULL,                               "element changes after delay"
3811   },
3812   {
3813     ED_CHECKBUTTON_ID_CHANGE_BY_DIRECT_ACT,
3814     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(4),
3815     GADGET_ID_CHANGE_BY_DIRECT_ACT,     GADGET_ID_NONE,
3816     &custom_element_change_events[CE_BY_DIRECT_ACTION],
3817     NULL, NULL,
3818     NULL,                               "element changes by direct action"
3819   },
3820   {
3821     ED_CHECKBUTTON_ID_CHANGE_BY_OTHER_ACT,
3822     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(5),
3823     GADGET_ID_CHANGE_BY_OTHER_ACT,      GADGET_ID_NONE,
3824     &custom_element_change_events[CE_BY_OTHER_ACTION],
3825     NULL, NULL,
3826     NULL,                               "element changes by other element"
3827   },
3828   {
3829     ED_CHECKBUTTON_ID_CHANGE_USE_EXPLOSION,
3830     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(8),
3831     GADGET_ID_CHANGE_USE_EXPLOSION,     GADGET_ID_NONE,
3832     &custom_element_change.explode,
3833     NULL, NULL,
3834     "explode instead of change",        "element explodes instead of change"
3835   },
3836   {
3837     ED_CHECKBUTTON_ID_CHANGE_USE_CONTENT,
3838     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(9),
3839     GADGET_ID_CHANGE_USE_CONTENT,       GADGET_ID_NONE,
3840     &custom_element_change.use_target_content,
3841     NULL, NULL,
3842     "use extended change target:",      "element changes to more elements"
3843   },
3844   {
3845     ED_CHECKBUTTON_ID_CHANGE_ONLY_COMPLETE,
3846     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(11),
3847     GADGET_ID_CHANGE_ONLY_COMPLETE,     GADGET_ID_NONE,
3848     &custom_element_change.only_if_complete,
3849     NULL, NULL,
3850     "replace all or nothing",           "only replace when all can be changed"
3851   },
3852   {
3853     ED_CHECKBUTTON_ID_CHANGE_USE_RANDOM,
3854     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(12),
3855     GADGET_ID_CHANGE_USE_RANDOM,        GADGET_ID_NONE,
3856     &custom_element_change.use_random_replace,
3857     NULL, NULL,
3858     NULL,                               "use percentage for random replace"
3859   },
3860   {
3861     ED_CHECKBUTTON_ID_CHANGE_HAS_ACTION,
3862     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(13),
3863     GADGET_ID_CHANGE_HAS_ACTION,        GADGET_ID_NONE,
3864     &custom_element_change.has_action,
3865     NULL, NULL,
3866     NULL,                               "execute action on specified condition"
3867   },
3868 };
3869
3870 static struct
3871 {
3872   int gadget_type_id;
3873   int x, y;
3874   int xoffset, yoffset;
3875   int gadget_id;
3876   int gadget_id_align;
3877   int *value;
3878   int area_xsize, area_ysize;
3879   char *text_left, *text_right, *text_above, *text_below, *infotext;
3880 } drawingarea_info[ED_NUM_DRAWING_AREAS] =
3881 {
3882   // ---------- level playfield content ---------------------------------------
3883
3884   {
3885     ED_DRAWING_ID_DRAWING_LEVEL,
3886     0,                                  0,
3887     0,                                  0,
3888     GADGET_ID_DRAWING_LEVEL,            GADGET_ID_NONE,
3889     NULL,
3890     -1, -1,     // these values are not constant, but can change at runtime
3891     NULL, NULL, NULL, NULL,             NULL
3892   },
3893
3894   // ---------- yam yam content -----------------------------------------------
3895
3896   {
3897     ED_DRAWING_ID_YAMYAM_CONTENT_0,
3898     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
3899     ED_AREA_YAMYAM_CONTENT_XOFF(0),     ED_AREA_YAMYAM_CONTENT_YOFF(0),
3900     GADGET_ID_YAMYAM_CONTENT_0,         GADGET_ID_NONE,
3901     &level.yamyam_content[0].e[0][0],   3, 3,
3902     NULL, NULL, NULL, "1",              NULL
3903   },
3904   {
3905     ED_DRAWING_ID_YAMYAM_CONTENT_1,
3906     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
3907     ED_AREA_YAMYAM_CONTENT_XOFF(1),     ED_AREA_YAMYAM_CONTENT_YOFF(1),
3908     GADGET_ID_YAMYAM_CONTENT_1,         GADGET_ID_NONE,
3909     &level.yamyam_content[1].e[0][0],   3, 3,
3910     NULL, NULL, NULL, "2",              NULL
3911   },
3912   {
3913     ED_DRAWING_ID_YAMYAM_CONTENT_2,
3914     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
3915     ED_AREA_YAMYAM_CONTENT_XOFF(2),     ED_AREA_YAMYAM_CONTENT_YOFF(2),
3916     GADGET_ID_YAMYAM_CONTENT_2,         GADGET_ID_NONE,
3917     &level.yamyam_content[2].e[0][0],   3, 3,
3918     NULL, NULL, NULL, "3",              NULL
3919   },
3920   {
3921     ED_DRAWING_ID_YAMYAM_CONTENT_3,
3922     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
3923     ED_AREA_YAMYAM_CONTENT_XOFF(3),     ED_AREA_YAMYAM_CONTENT_YOFF(3),
3924     GADGET_ID_YAMYAM_CONTENT_3,         GADGET_ID_NONE,
3925     &level.yamyam_content[3].e[0][0],   3, 3,
3926     NULL, NULL, NULL, "4",              NULL
3927   },
3928   {
3929     ED_DRAWING_ID_YAMYAM_CONTENT_4,
3930     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
3931     ED_AREA_YAMYAM_CONTENT_XOFF(4),     ED_AREA_YAMYAM_CONTENT_YOFF(4),
3932     GADGET_ID_YAMYAM_CONTENT_4,         GADGET_ID_NONE,
3933     &level.yamyam_content[4].e[0][0],   3, 3,
3934     NULL, NULL, NULL, "5",              NULL
3935   },
3936   {
3937     ED_DRAWING_ID_YAMYAM_CONTENT_5,
3938     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
3939     ED_AREA_YAMYAM_CONTENT_XOFF(5),     ED_AREA_YAMYAM_CONTENT_YOFF(5),
3940     GADGET_ID_YAMYAM_CONTENT_5,         GADGET_ID_NONE,
3941     &level.yamyam_content[5].e[0][0],   3, 3,
3942     NULL, NULL, NULL, "6",              NULL
3943   },
3944   {
3945     ED_DRAWING_ID_YAMYAM_CONTENT_6,
3946     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
3947     ED_AREA_YAMYAM_CONTENT_XOFF(6),     ED_AREA_YAMYAM_CONTENT_YOFF(6),
3948     GADGET_ID_YAMYAM_CONTENT_6,         GADGET_ID_NONE,
3949     &level.yamyam_content[6].e[0][0],   3, 3,
3950     NULL, NULL, NULL, "7",              NULL
3951   },
3952   {
3953     ED_DRAWING_ID_YAMYAM_CONTENT_7,
3954     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
3955     ED_AREA_YAMYAM_CONTENT_XOFF(7),     ED_AREA_YAMYAM_CONTENT_YOFF(7),
3956     GADGET_ID_YAMYAM_CONTENT_7,         GADGET_ID_NONE,
3957     &level.yamyam_content[7].e[0][0],   3, 3,
3958     NULL, NULL, NULL, "8",              NULL
3959   },
3960
3961   // ---------- magic ball content --------------------------------------------
3962
3963   {
3964     ED_DRAWING_ID_MAGIC_BALL_CONTENT_0,
3965     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
3966     ED_AREA_MAGIC_BALL_CONTENT_XOFF(0), ED_AREA_MAGIC_BALL_CONTENT_YOFF(0),
3967     GADGET_ID_MAGIC_BALL_CONTENT_0,     GADGET_ID_NONE,
3968     &level.ball_content[0].e[0][0],     3, 3,
3969     NULL, NULL, NULL, "1",              NULL
3970   },
3971   {
3972     ED_DRAWING_ID_MAGIC_BALL_CONTENT_1,
3973     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
3974     ED_AREA_MAGIC_BALL_CONTENT_XOFF(1), ED_AREA_MAGIC_BALL_CONTENT_YOFF(1),
3975     GADGET_ID_MAGIC_BALL_CONTENT_1,     GADGET_ID_NONE,
3976     &level.ball_content[1].e[0][0],     3, 3,
3977     NULL, NULL, NULL, "2",              NULL
3978   },
3979   {
3980     ED_DRAWING_ID_MAGIC_BALL_CONTENT_2,
3981     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
3982     ED_AREA_MAGIC_BALL_CONTENT_XOFF(2), ED_AREA_MAGIC_BALL_CONTENT_YOFF(2),
3983     GADGET_ID_MAGIC_BALL_CONTENT_2,     GADGET_ID_NONE,
3984     &level.ball_content[2].e[0][0],     3, 3,
3985     NULL, NULL, NULL, "3",              NULL
3986   },
3987   {
3988     ED_DRAWING_ID_MAGIC_BALL_CONTENT_3,
3989     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
3990     ED_AREA_MAGIC_BALL_CONTENT_XOFF(3), ED_AREA_MAGIC_BALL_CONTENT_YOFF(3),
3991     GADGET_ID_MAGIC_BALL_CONTENT_3,     GADGET_ID_NONE,
3992     &level.ball_content[3].e[0][0],     3, 3,
3993     NULL, NULL, NULL, "4",              NULL
3994   },
3995   {
3996     ED_DRAWING_ID_MAGIC_BALL_CONTENT_4,
3997     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
3998     ED_AREA_MAGIC_BALL_CONTENT_XOFF(4), ED_AREA_MAGIC_BALL_CONTENT_YOFF(4),
3999     GADGET_ID_MAGIC_BALL_CONTENT_4,     GADGET_ID_NONE,
4000     &level.ball_content[4].e[0][0],     3, 3,
4001     NULL, NULL, NULL, "5",              NULL
4002   },
4003   {
4004     ED_DRAWING_ID_MAGIC_BALL_CONTENT_5,
4005     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4006     ED_AREA_MAGIC_BALL_CONTENT_XOFF(5), ED_AREA_MAGIC_BALL_CONTENT_YOFF(5),
4007     GADGET_ID_MAGIC_BALL_CONTENT_5,     GADGET_ID_NONE,
4008     &level.ball_content[5].e[0][0],     3, 3,
4009     NULL, NULL, NULL, "6",              NULL
4010   },
4011   {
4012     ED_DRAWING_ID_MAGIC_BALL_CONTENT_6,
4013     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4014     ED_AREA_MAGIC_BALL_CONTENT_XOFF(6), ED_AREA_MAGIC_BALL_CONTENT_YOFF(6),
4015     GADGET_ID_MAGIC_BALL_CONTENT_6,     GADGET_ID_NONE,
4016     &level.ball_content[6].e[0][0],     3, 3,
4017     NULL, NULL, NULL, "7",              NULL
4018   },
4019   {
4020     ED_DRAWING_ID_MAGIC_BALL_CONTENT_7,
4021     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4022     ED_AREA_MAGIC_BALL_CONTENT_XOFF(7), ED_AREA_MAGIC_BALL_CONTENT_YOFF(7),
4023     GADGET_ID_MAGIC_BALL_CONTENT_7,     GADGET_ID_NONE,
4024     &level.ball_content[7].e[0][0],     3, 3,
4025     NULL, NULL, NULL, "8",              NULL
4026   },
4027
4028   // ---------- android content -----------------------------------------------
4029
4030   {
4031     ED_DRAWING_ID_ANDROID_CONTENT,
4032     ED_AREA_1X1_SETTINGS_XPOS(0),       ED_AREA_1X1_SETTINGS_YPOS(6),
4033     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4034     GADGET_ID_ANDROID_CONTENT,          GADGET_ID_NONE,
4035     &level.android_clone_element[0],    MAX_ANDROID_ELEMENTS, 1,
4036     NULL, NULL, "elements:", NULL,      "elements android can clone"
4037   },
4038
4039   // ---------- amoeba content ------------------------------------------------
4040
4041   {
4042     ED_DRAWING_ID_AMOEBA_CONTENT,
4043     ED_AREA_1X1_SETTINGS_XPOS(0),       ED_AREA_1X1_SETTINGS_YPOS(3),
4044     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4045     GADGET_ID_AMOEBA_CONTENT,           GADGET_ID_NONE,
4046     &level.amoeba_content,              1, 1,
4047     "content:", NULL, NULL, NULL,       "amoeba content"
4048   },
4049
4050   // ---------- level start element -------------------------------------------
4051
4052   {
4053     ED_DRAWING_ID_START_ELEMENT,
4054     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(10),
4055     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4056     GADGET_ID_START_ELEMENT,            GADGET_ID_USE_START_ELEMENT,
4057     &level.start_element[0],            1, 1,
4058     NULL, NULL, NULL, NULL,             "level start element"
4059   },
4060
4061   // ---------- player artwork element ----------------------------------------
4062
4063   {
4064     ED_DRAWING_ID_ARTWORK_ELEMENT,
4065     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(11),
4066     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4067     GADGET_ID_ARTWORK_ELEMENT,          GADGET_ID_USE_ARTWORK_ELEMENT,
4068     &level.artwork_element[0],          1, 1,
4069     NULL, NULL, NULL, NULL,             "element for player artwork"
4070   },
4071
4072   // ---------- player explosion element --------------------------------------
4073
4074   {
4075     ED_DRAWING_ID_EXPLOSION_ELEMENT,
4076     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(12),
4077     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4078     GADGET_ID_EXPLOSION_ELEMENT,        GADGET_ID_USE_EXPLOSION_ELEMENT,
4079     &level.explosion_element[0],        1, 1,
4080     NULL, NULL, NULL, NULL,             "element for player explosion"
4081   },
4082
4083   // ---------- player initial inventory --------------------------------------
4084
4085   {
4086     ED_DRAWING_ID_INVENTORY_CONTENT,
4087     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(1),
4088     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4089     GADGET_ID_INVENTORY_CONTENT,        GADGET_ID_USE_INITIAL_INVENTORY,
4090     &level.initial_inventory_content[0][0], MAX_INITIAL_INVENTORY_SIZE, 1,
4091     NULL, NULL, NULL, NULL,             "content for initial inventory"
4092   },
4093
4094   // ---------- gray ball content -----------------------------------------
4095
4096   {
4097     ED_DRAWING_ID_MM_BALL_CONTENT,
4098     ED_AREA_1X1_SETTINGS_XPOS(0),       ED_AREA_1X1_SETTINGS_YPOS(2),
4099     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4100     GADGET_ID_MM_BALL_CONTENT,          GADGET_ID_NONE,
4101     &level.mm_ball_content[0],          MAX_MM_BALL_CONTENTS, 1,
4102     "content:", NULL, NULL, NULL,       "content for gray ball"
4103   },
4104
4105   // ---------- element settings: configure 1 (custom elements) ---------------
4106
4107   // ---------- custom graphic ------------------------------------------------
4108
4109   {
4110     ED_DRAWING_ID_CUSTOM_GRAPHIC,
4111     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(1),
4112     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4113     GADGET_ID_CUSTOM_GRAPHIC,           GADGET_ID_CUSTOM_USE_GRAPHIC,
4114     &custom_element.gfx_element_initial, 1, 1,
4115     NULL, NULL, NULL, NULL,             "custom graphic element"
4116   },
4117
4118   // ---------- element settings: configure 2 (custom elements) ---------------
4119
4120   // ---------- custom content (when exploding) -------------------------------
4121
4122   {
4123     ED_DRAWING_ID_CUSTOM_CONTENT,
4124     -1,                                 ED_AREA_3X3_SETTINGS_YPOS(11),
4125     0,                                  ED_AREA_3X3_SETTINGS_YOFF,
4126     GADGET_ID_CUSTOM_CONTENT,           GADGET_ID_NONE, // align three rows
4127     &custom_element.content.e[0][0],    3, 3,
4128     "content:", NULL, NULL, NULL,       NULL
4129   },
4130
4131   // ---------- custom enter and leave element (when moving) ------------------
4132
4133   {
4134     ED_DRAWING_ID_CUSTOM_MOVE_ENTER,
4135     ED_AREA_1X1_SETTINGS_XPOS(1),       ED_AREA_1X1_SETTINGS_YPOS(3),
4136     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4137     GADGET_ID_CUSTOM_MOVE_ENTER,        GADGET_ID_NONE,
4138     &custom_element.move_enter_element, 1, 1,
4139     "can dig:", " ", NULL, NULL,        "element that can be digged/collected"
4140   },
4141   {
4142     ED_DRAWING_ID_CUSTOM_MOVE_LEAVE,
4143     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(3),
4144     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4145     GADGET_ID_CUSTOM_MOVE_LEAVE,        GADGET_ID_CUSTOM_MOVE_LEAVE_TYPE,
4146     &custom_element.move_leave_element, 1, 1,
4147     NULL, NULL, NULL, NULL,             "element that will be left behind"
4148   },
4149
4150   // ---------- element settings: advanced (custom elements) ------------------
4151
4152   // ---------- custom change target ------------------------------------------
4153
4154   {
4155     ED_DRAWING_ID_CUSTOM_CHANGE_TARGET,
4156     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(1),
4157     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4158     GADGET_ID_CUSTOM_CHANGE_TARGET,     GADGET_ID_CUSTOM_CAN_CHANGE,
4159     &custom_element_change.target_element, 1, 1,
4160     NULL, "after/when:", NULL, NULL,    "new target element after change"
4161   },
4162
4163   // ---------- custom change content (extended change target) ----------------
4164
4165   {
4166     ED_DRAWING_ID_CUSTOM_CHANGE_CONTENT,
4167     -1,                                 ED_AREA_3X3_SETTINGS_YPOS(9),
4168     0,                                  ED_AREA_3X3_SETTINGS_YOFF,
4169     GADGET_ID_CUSTOM_CHANGE_CONTENT,    GADGET_ID_NONE, // align three rows
4170     &custom_element_change.target_content.e[0][0], 3, 3,
4171     NULL, NULL, NULL, NULL,             "new extended elements after change"
4172   },
4173
4174   // ---------- custom change trigger (element causing change) ----------------
4175
4176   {
4177     ED_DRAWING_ID_CUSTOM_CHANGE_TRIGGER,
4178     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(5),
4179     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4180     GADGET_ID_CUSTOM_CHANGE_TRIGGER,    GADGET_ID_CHANGE_OTHER_ACTION,
4181     &custom_element_change.initial_trigger_element, 1, 1,
4182     NULL, NULL, NULL, NULL,             "other element triggering change"
4183   },
4184
4185   // ---------- custom change action (element used for action) ----------------
4186
4187   {
4188     ED_DRAWING_ID_CUSTOM_CHANGE_ACTION,
4189     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(13),
4190     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4191     GADGET_ID_CUSTOM_CHANGE_ACTION,     GADGET_ID_ACTION_ARG,
4192     &custom_element_change.action_element, 1, 1,
4193     NULL, NULL, NULL, NULL,             "element used as action parameter"
4194   },
4195
4196   // ---------- group element content -----------------------------------------
4197
4198   {
4199     ED_DRAWING_ID_GROUP_CONTENT,
4200     ED_AREA_1X1_SETTINGS_XPOS(0),       ED_AREA_1X1_SETTINGS_YPOS(2),
4201     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4202     GADGET_ID_GROUP_CONTENT,            GADGET_ID_NONE,
4203     &group_element_info.element[0],     MAX_ELEMENTS_IN_GROUP, 1,
4204     "content:", NULL, NULL, NULL,       NULL
4205   },
4206
4207   // ---------- random background (for random painting) -----------------------
4208
4209   {
4210     ED_DRAWING_ID_RANDOM_BACKGROUND,
4211     -1,                                 ED_AREA_1X1_LSETTINGS_YPOS(1),
4212     0,                                  ED_AREA_1X1_LSETTINGS_YOFF,
4213     GADGET_ID_RANDOM_BACKGROUND,        GADGET_ID_RANDOM_RESTRICTED,
4214     &random_placement_background_element, 1, 1,
4215     NULL, NULL, NULL, NULL,             "random placement background"
4216   },
4217 };
4218
4219
4220 // ----------------------------------------------------------------------------
4221 // some internally used variables
4222 // ----------------------------------------------------------------------------
4223
4224 // maximal size of level editor drawing area
4225 static int MAX_ED_FIELDX, MAX_ED_FIELDY;
4226
4227 // actual size of level editor drawing area
4228 static int ed_fieldx, ed_fieldy;
4229
4230 // actual position of level editor drawing area in level playfield
4231 static int level_xpos = -1, level_ypos = -1;
4232
4233 // actual tile size used to display playfield drawing area
4234 static int ed_tilesize = DEFAULT_EDITOR_TILESIZE;
4235 static int ed_tilesize_default = DEFAULT_EDITOR_TILESIZE;
4236
4237 #define IN_ED_FIELD(x, y)       IN_FIELD(x, y, ed_fieldx, ed_fieldy)
4238
4239 // drawing elements on the three mouse buttons
4240 static int new_element1 = EL_WALL;
4241 static int new_element2 = EL_EMPTY;
4242 static int new_element3 = EL_SAND;
4243
4244 #define IS_VALID_BUTTON(button) (button >= 1 && button <= 3)
4245 #define BUTTON_ELEMENT(button) ((button) == 1 ? new_element1 : \
4246                                 (button) == 2 ? new_element2 : \
4247                                 (button) == 3 ? new_element3 : EL_EMPTY)
4248
4249 #define BUTTON_TILE_SIZE(x)     ((x) >= TILESIZE ? TILESIZE : MINI_TILESIZE)
4250
4251 static int use_permanent_palette = TRUE;
4252
4253 #define PX              (use_permanent_palette ? DX : SX)
4254 #define PY              (use_permanent_palette ? DY : SY)
4255 #define PXSIZE          (use_permanent_palette ? DXSIZE : SXSIZE)
4256 #define PYSIZE          (use_permanent_palette ? DYSIZE : SYSIZE)
4257
4258 // forward declaration for internal use
4259 static void CopyBrushToCursor(int, int);
4260 static void DeleteBrushFromCursor(void);
4261 static void ModifyEditorCounterValue(int, int);
4262 static void ModifyEditorCounterLimits(int, int, int);
4263 static void ModifyEditorSelectboxValue(int, int);
4264 static void ModifyEditorSelectboxOptions(int, struct ValueTextInfo *);
4265 static void ModifyEditorDrawingArea(int, int, int);
4266 static void ModifyEditorElementList(void);
4267 static void AdjustElementListScrollbar(void);
4268 static void RedrawDrawingElements(void);
4269 static void DrawDrawingWindowExt(boolean);
4270 static void DrawDrawingWindow(void);
4271 static void DrawLevelConfigWindow(void);
4272 static void DrawPropertiesWindow(void);
4273 static void DrawPaletteWindow(void);
4274 static void UpdateCustomElementGraphicGadgets(void);
4275 static boolean checkPropertiesConfig(int);
4276 static void SetAutomaticNumberOfGemsNeeded(void);
4277 static void ClearEditorGadgetInfoText(void);
4278 static void CopyLevelToUndoBuffer(int);
4279 static void HandleDrawingAreas(struct GadgetInfo *);
4280 static void HandleCounterButtons(struct GadgetInfo *);
4281 static void HandleTextInputGadgets(struct GadgetInfo *);
4282 static void HandleTextAreaGadgets(struct GadgetInfo *);
4283 static void HandleSelectboxGadgets(struct GadgetInfo *);
4284 static void HandleTextbuttonGadgets(struct GadgetInfo *);
4285 static void HandleGraphicbuttonGadgets(struct GadgetInfo *);
4286 static void HandleRadiobuttons(struct GadgetInfo *);
4287 static void HandleCheckbuttons(struct GadgetInfo *);
4288 static void HandleControlButtons(struct GadgetInfo *);
4289 static void HandleDrawingAreaInfo(struct GadgetInfo *);
4290 static void PrintEditorGadgetInfoText(struct GadgetInfo *);
4291 static boolean AskToCopyAndModifyLevelTemplate(void);
4292 static boolean getDrawModeHiRes(void);
4293 static int getTabulatorBarWidth(void);
4294 static int getTabulatorBarHeight(void);
4295 static Pixel getTabulatorBarColor(void);
4296 static int numHiresTiles(int);
4297
4298 static int num_editor_gadgets = 0;      // dynamically determined
4299
4300 static struct GadgetInfo **level_editor_gadget = NULL;
4301 static int *right_gadget_border = NULL;
4302
4303 static int drawing_function = GADGET_ID_SINGLE_ITEMS;
4304 static int last_drawing_function = GADGET_ID_SINGLE_ITEMS;
4305 static boolean draw_with_brush = FALSE;
4306 static int properties_element = 0;
4307
4308 static short TileBackup[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
4309 static short UndoBuffer[NUM_UNDO_STEPS][MAX_LEV_FIELDX][MAX_LEV_FIELDY];
4310 static short IntelliDrawBuffer[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
4311 static int undo_buffer_position = 0;
4312 static int undo_buffer_steps = 0;
4313 static int redo_buffer_steps = 0;
4314
4315 static int edit_mode;
4316 static int edit_mode_levelconfig;
4317 static int edit_mode_properties;
4318
4319 static int element_shift = 0;
4320
4321 static int editor_el_players[] =
4322 {
4323   EL_PLAYER_1,
4324   EL_PLAYER_2,
4325   EL_PLAYER_3,
4326   EL_PLAYER_4
4327 };
4328 static int *editor_el_players_ptr = editor_el_players;
4329 static int num_editor_el_players = ARRAY_SIZE(editor_el_players);
4330
4331 static int editor_hl_boulderdash[] =
4332 {
4333   EL_INTERNAL_CASCADE_BD_ACTIVE,
4334   EL_CHAR('B'),
4335   EL_CHAR('D'),
4336   EL_EMPTY,
4337 };
4338
4339 static int editor_el_boulderdash[] =
4340 {
4341   EL_EMPTY,
4342   EL_SAND,
4343   EL_BD_ROCK,
4344   EL_BD_DIAMOND,
4345
4346   EL_STEELWALL,
4347   EL_BD_WALL,
4348   EL_BD_EXPANDABLE_WALL,
4349   EL_BD_MAGIC_WALL,
4350
4351   EL_BD_AMOEBA,
4352   EL_BD_BUTTERFLY_UP,
4353   EL_BD_FIREFLY_UP,
4354   EL_EXIT_CLOSED,
4355
4356   EL_BD_BUTTERFLY_LEFT,
4357   EL_BD_FIREFLY_LEFT,
4358   EL_BD_BUTTERFLY_RIGHT,
4359   EL_BD_FIREFLY_RIGHT,
4360
4361   EL_EMPTY,
4362   EL_BD_BUTTERFLY_DOWN,
4363   EL_BD_FIREFLY_DOWN,
4364   EL_EXIT_OPEN,
4365 };
4366 static int *editor_hl_boulderdash_ptr = editor_hl_boulderdash;
4367 static int *editor_el_boulderdash_ptr = editor_el_boulderdash;
4368 static int num_editor_hl_boulderdash = ARRAY_SIZE(editor_hl_boulderdash);
4369 static int num_editor_el_boulderdash = ARRAY_SIZE(editor_el_boulderdash);
4370
4371 static int editor_hl_boulderdash_native[] =
4372 {
4373   EL_INTERNAL_CASCADE_BD_NATIVE_ACTIVE,
4374   EL_CHAR('B'),
4375   EL_CHAR('D'),
4376   EL_EMPTY,
4377 };
4378
4379 static int editor_el_boulderdash_native[] =
4380 {
4381   EL_EMPTY,
4382   EL_BD_SAND,
4383   EL_BD_ROCK,
4384   EL_BD_DIAMOND,
4385
4386   EL_BD_STEELWALL,
4387   EL_BD_WALL,
4388   EL_EMPTY,
4389   EL_BD_MAGIC_WALL,
4390
4391   EL_BD_AMOEBA,
4392   EL_BD_BUTTERFLY_UP,
4393   EL_BD_FIREFLY_UP,
4394   EL_EXIT_CLOSED,
4395
4396   EL_BD_BUTTERFLY_LEFT,
4397   EL_BD_FIREFLY_LEFT,
4398   EL_BD_BUTTERFLY_RIGHT,
4399   EL_BD_FIREFLY_RIGHT,
4400
4401   EL_BD_INBOX,
4402   EL_BD_BUTTERFLY_DOWN,
4403   EL_BD_FIREFLY_DOWN,
4404   EL_EXIT_OPEN,
4405
4406   EL_BD_AMOEBA_2,
4407   EL_BD_BUTTERFLY_2_UP,
4408   EL_BD_FIREFLY_2_UP,
4409   EL_EMPTY,
4410
4411   EL_BD_BUTTERFLY_2_LEFT,
4412   EL_BD_FIREFLY_2_LEFT,
4413   EL_BD_BUTTERFLY_2_RIGHT,
4414   EL_BD_FIREFLY_2_RIGHT,
4415
4416   EL_EMPTY,
4417   EL_BD_BUTTERFLY_2_DOWN,
4418   EL_BD_FIREFLY_2_DOWN,
4419   EL_EMPTY,
4420
4421   EL_EMPTY,
4422   EL_BD_DRAGONFLY_UP,
4423   EL_BD_STONEFLY_UP,
4424   EL_EMPTY,
4425
4426   EL_BD_DRAGONFLY_LEFT,
4427   EL_BD_STONEFLY_LEFT,
4428   EL_BD_DRAGONFLY_RIGHT,
4429   EL_BD_STONEFLY_RIGHT,
4430
4431   EL_EMPTY,
4432   EL_BD_DRAGONFLY_DOWN,
4433   EL_BD_STONEFLY_DOWN,
4434   EL_EMPTY,
4435
4436   EL_EMPTY,
4437   EL_BD_BITER_UP,
4438   EL_BD_COW_UP,
4439   EL_EMPTY,
4440
4441   EL_BD_BITER_LEFT,
4442   EL_BD_COW_LEFT,
4443   EL_BD_BITER_RIGHT,
4444   EL_BD_COW_RIGHT,
4445
4446   EL_EMPTY,
4447   EL_BD_BITER_DOWN,
4448   EL_BD_COW_DOWN,
4449   EL_EMPTY,
4450
4451   EL_EMPTY,
4452   EL_BD_SAND,
4453   EL_BD_SAND_BALL,
4454   EL_BD_SAND_LOOSE,
4455
4456   EL_BD_SAND_SLOPED_UP_LEFT,
4457   EL_BD_SAND_SLOPED_UP_RIGHT,
4458   EL_BD_WALL_SLOPED_UP_LEFT,
4459   EL_BD_WALL_SLOPED_UP_RIGHT,
4460
4461   EL_BD_SAND_SLOPED_DOWN_LEFT,
4462   EL_BD_SAND_SLOPED_DOWN_RIGHT,
4463   EL_BD_WALL_SLOPED_DOWN_LEFT,
4464   EL_BD_WALL_SLOPED_DOWN_RIGHT,
4465
4466   EL_BD_FLYING_DIAMOND,
4467   EL_BD_FLYING_ROCK,
4468   EL_BD_STEELWALL_SLOPED_UP_LEFT,
4469   EL_BD_STEELWALL_SLOPED_UP_RIGHT,
4470
4471   EL_BD_NUT,
4472   EL_BD_MEGA_ROCK,
4473   EL_BD_STEELWALL_SLOPED_DOWN_LEFT,
4474   EL_BD_STEELWALL_SLOPED_DOWN_RIGHT,
4475
4476   EL_BD_SAND_2,
4477   EL_BD_WALL_NON_SLOPED,
4478   EL_BD_WALL,
4479   EL_BD_MAGIC_WALL,
4480
4481   EL_BD_EXIT_CLOSED,
4482   EL_BD_EXIT_OPEN,
4483   EL_BD_INVISIBLE_EXIT_CLOSED,
4484   EL_BD_INVISIBLE_EXIT_OPEN,
4485
4486   EL_BD_STEELWALL,
4487   EL_BD_STEELWALL_EXPLODABLE,
4488   EL_BD_STEELWALL_DIGGABLE,
4489   EL_BD_WALL_DIGGABLE,
4490
4491   EL_BD_EXPANDABLE_WALL_HORIZONTAL,
4492   EL_BD_EXPANDABLE_WALL_VERTICAL,
4493   EL_BD_EXPANDABLE_WALL_ANY,
4494   EL_BD_CREATURE_SWITCH,
4495
4496   EL_BD_EXPANDABLE_STEELWALL_HORIZONTAL,
4497   EL_BD_EXPANDABLE_STEELWALL_VERTICAL,
4498   EL_BD_EXPANDABLE_STEELWALL_ANY,
4499   EL_BD_EXPANDABLE_WALL_SWITCH_HORIZONTAL,
4500
4501   EL_BD_BITER_SWITCH_1,
4502   EL_BD_REPLICATOR_SWITCH,
4503   EL_BD_CONVEYOR_SWITCH,
4504   EL_BD_CONVEYOR_DIR_SWITCH_RIGHT,
4505
4506   EL_BD_ACID,
4507   EL_BD_FALLING_WALL,
4508   EL_BD_BOX,
4509   EL_BD_TIME_PENALTY,
4510
4511   EL_BD_GRAVESTONE,
4512   EL_BD_ROCK_GLUED,
4513   EL_BD_DIAMOND_GLUED,
4514   EL_BD_DIAMOND_KEY,
4515
4516   EL_BD_TRAPPED_DIAMOND,
4517   EL_BD_CLOCK,
4518   EL_BD_SAND_GLUED,
4519   EL_BD_WATER,
4520
4521   EL_BD_KEY_1,
4522   EL_BD_KEY_2,
4523   EL_BD_KEY_3,
4524   EL_EMPTY,
4525
4526   EL_BD_WALL_KEY_1,
4527   EL_BD_WALL_KEY_2,
4528   EL_BD_WALL_KEY_3,
4529   EL_BD_WALL_DIAMOND,
4530
4531   EL_BD_GATE_1,
4532   EL_BD_GATE_2,
4533   EL_BD_GATE_3,
4534   EL_BD_POT,
4535
4536   EL_BD_GRAVITY_SWITCH,
4537   EL_BD_PNEUMATIC_HAMMER,
4538   EL_BD_TELEPORTER,
4539   EL_BD_SKELETON,
4540
4541   EL_BD_AMOEBA,
4542   EL_BD_AMOEBA_2,
4543   EL_BD_REPLICATOR,
4544   EL_BD_BLADDER_SPENDER,
4545
4546   EL_BD_CONVEYOR_LEFT,
4547   EL_BD_CONVEYOR_RIGHT,
4548   EL_BD_BOMB,
4549   EL_BD_NITRO_PACK,
4550
4551   EL_BD_LAVA,
4552   EL_BD_SWEET,
4553   EL_BD_VOODOO_DOLL,
4554   EL_BD_SLIME,
4555
4556   EL_BD_BLADDER,
4557   EL_BD_WAITING_ROCK,
4558   EL_BD_CHASING_ROCK,
4559   EL_BD_GHOST,
4560
4561   EL_BD_PLAYER,
4562   EL_BD_PLAYER_WITH_BOMB,
4563   EL_BD_PLAYER_GLUED,
4564   EL_BD_PLAYER_STIRRING,
4565 };
4566 static int *editor_hl_boulderdash_native_ptr = editor_hl_boulderdash_native;
4567 static int *editor_el_boulderdash_native_ptr = editor_el_boulderdash_native;
4568 static int num_editor_hl_boulderdash_native = ARRAY_SIZE(editor_hl_boulderdash_native);
4569 static int num_editor_el_boulderdash_native = ARRAY_SIZE(editor_el_boulderdash_native);
4570
4571 static int editor_hl_emerald_mine[] =
4572 {
4573   EL_INTERNAL_CASCADE_EM_ACTIVE,
4574   EL_CHAR('E'),
4575   EL_CHAR('M'),
4576   EL_EMPTY,
4577 };
4578
4579 static int editor_el_emerald_mine[] =
4580 {
4581   EL_SAND,
4582   EL_ROCK,
4583   EL_QUICKSAND_EMPTY,
4584   EL_QUICKSAND_FULL,
4585
4586   EL_STEELWALL,
4587   EL_WALL,
4588   EL_WALL_SLIPPERY,
4589   EL_MAGIC_WALL,
4590
4591   EL_EMERALD,
4592   EL_DIAMOND,
4593   EL_NUT,
4594   EL_BOMB,
4595
4596   EL_EM_DYNAMITE,
4597   EL_EM_DYNAMITE_ACTIVE,
4598   EL_EM_EXIT_CLOSED,
4599   EL_EM_EXIT_OPEN,
4600
4601   EL_YAMYAM_UP,
4602   EL_BUG_UP,
4603   EL_SPACESHIP_UP,
4604   EL_ROBOT,
4605
4606   EL_BUG_LEFT,
4607   EL_SPACESHIP_LEFT,
4608   EL_BUG_RIGHT,
4609   EL_SPACESHIP_RIGHT,
4610
4611   EL_ROBOT_WHEEL,
4612   EL_BUG_DOWN,
4613   EL_SPACESHIP_DOWN,
4614   EL_INVISIBLE_WALL,
4615
4616   EL_ACID_POOL_TOPLEFT,
4617   EL_ACID,
4618   EL_ACID_POOL_TOPRIGHT,
4619   EL_AMOEBA_DROP,
4620
4621   EL_ACID_POOL_BOTTOMLEFT,
4622   EL_ACID_POOL_BOTTOM,
4623   EL_ACID_POOL_BOTTOMRIGHT,
4624   EL_AMOEBA_WET,
4625
4626   EL_EM_KEY_1,
4627   EL_EM_KEY_2,
4628   EL_EM_KEY_3,
4629   EL_EM_KEY_4,
4630
4631   EL_EM_GATE_1,
4632   EL_EM_GATE_2,
4633   EL_EM_GATE_3,
4634   EL_EM_GATE_4,
4635
4636   EL_EM_GATE_1_GRAY,
4637   EL_EM_GATE_2_GRAY,
4638   EL_EM_GATE_3_GRAY,
4639   EL_EM_GATE_4_GRAY,
4640 };
4641 static int *editor_hl_emerald_mine_ptr = editor_hl_emerald_mine;
4642 static int *editor_el_emerald_mine_ptr = editor_el_emerald_mine;
4643 static int num_editor_hl_emerald_mine = ARRAY_SIZE(editor_hl_emerald_mine);
4644 static int num_editor_el_emerald_mine = ARRAY_SIZE(editor_el_emerald_mine);
4645
4646 static int editor_hl_emerald_mine_club[] =
4647 {
4648   EL_INTERNAL_CASCADE_EMC_ACTIVE,
4649   EL_CHAR('E'),
4650   EL_CHAR('M'),
4651   EL_CHAR('C'),
4652 };
4653
4654 static int editor_el_emerald_mine_club[] =
4655 {
4656   EL_EMC_KEY_5,
4657   EL_EMC_KEY_6,
4658   EL_EMC_KEY_7,
4659   EL_EMC_KEY_8,
4660
4661   EL_EMC_GATE_5,
4662   EL_EMC_GATE_6,
4663   EL_EMC_GATE_7,
4664   EL_EMC_GATE_8,
4665
4666   EL_EMC_GATE_5_GRAY,
4667   EL_EMC_GATE_6_GRAY,
4668   EL_EMC_GATE_7_GRAY,
4669   EL_EMC_GATE_8_GRAY,
4670
4671   EL_EMC_STEELWALL_1,
4672   EL_EMC_STEELWALL_2,
4673   EL_EMC_STEELWALL_3,
4674   EL_EMC_STEELWALL_4,
4675
4676   EL_EMC_WALL_13,
4677   EL_EMC_WALL_14,
4678   EL_EMC_WALL_15,
4679   EL_EMC_WALL_16,
4680
4681   EL_EMC_WALL_SLIPPERY_1,
4682   EL_EMC_WALL_SLIPPERY_2,
4683   EL_EMC_WALL_SLIPPERY_3,
4684   EL_EMC_WALL_SLIPPERY_4,
4685
4686   EL_EMC_WALL_1,
4687   EL_EMC_WALL_2,
4688   EL_EMC_WALL_3,
4689   EL_EMC_WALL_4,
4690
4691   EL_EMC_WALL_5,
4692   EL_EMC_WALL_6,
4693   EL_EMC_WALL_7,
4694   EL_EMC_WALL_8,
4695
4696   EL_EMC_WALL_9,
4697   EL_EMC_WALL_10,
4698   EL_EMC_WALL_11,
4699   EL_EMC_WALL_12,
4700
4701   EL_EMC_GRASS,
4702   EL_EMC_FAKE_GRASS,
4703   EL_EMC_PLANT,
4704   EL_EMC_DRIPPER,
4705
4706   EL_EMC_MAGIC_BALL,
4707   EL_EMC_MAGIC_BALL_SWITCH,
4708   EL_EMC_LENSES,
4709   EL_EMC_MAGNIFIER,
4710
4711   EL_SPRING_LEFT,
4712   EL_SPRING,
4713   EL_SPRING_RIGHT,
4714   EL_EMC_SPRING_BUMPER,
4715
4716   EL_BALLOON,
4717   EL_YAMYAM_UP,
4718   EL_BALLOON_SWITCH_UP,
4719   EL_BALLOON_SWITCH_ANY,
4720
4721   EL_YAMYAM_LEFT,
4722   EL_BALLOON_SWITCH_LEFT,
4723   EL_YAMYAM_RIGHT,
4724   EL_BALLOON_SWITCH_RIGHT,
4725
4726   EL_EMC_ANDROID,
4727   EL_YAMYAM_DOWN,
4728   EL_BALLOON_SWITCH_DOWN,
4729   EL_BALLOON_SWITCH_NONE,
4730 };
4731 static int *editor_hl_emerald_mine_club_ptr = editor_hl_emerald_mine_club;
4732 static int *editor_el_emerald_mine_club_ptr = editor_el_emerald_mine_club;
4733 static int num_editor_hl_emerald_mine_club = ARRAY_SIZE(editor_hl_emerald_mine_club);
4734 static int num_editor_el_emerald_mine_club = ARRAY_SIZE(editor_el_emerald_mine_club);
4735
4736 static int editor_hl_rnd[] =
4737 {
4738   EL_INTERNAL_CASCADE_RND_ACTIVE,
4739   EL_CHAR('R'),
4740   EL_CHAR('N'),
4741   EL_CHAR('D'),
4742 };
4743
4744 static int editor_el_rnd[] =
4745 {
4746   EL_DYNAMITE,                  // RND
4747   EL_DYNAMITE_ACTIVE,           // RND
4748   EL_EMPTY,
4749   EL_EMPTY,
4750
4751   EL_KEY_1,
4752   EL_KEY_2,
4753   EL_KEY_3,
4754   EL_KEY_4,
4755
4756   EL_GATE_1,
4757   EL_GATE_2,
4758   EL_GATE_3,
4759   EL_GATE_4,
4760
4761   EL_GATE_1_GRAY,
4762   EL_GATE_2_GRAY,
4763   EL_GATE_3_GRAY,
4764   EL_GATE_4_GRAY,
4765
4766   EL_ARROW_LEFT,
4767   EL_ARROW_RIGHT,
4768   EL_ARROW_UP,
4769   EL_ARROW_DOWN,
4770
4771   EL_AMOEBA_DEAD,
4772   EL_AMOEBA_DRY,
4773   EL_AMOEBA_FULL,
4774   EL_GAME_OF_LIFE,
4775
4776   EL_EMERALD_YELLOW,
4777   EL_EMERALD_RED,
4778   EL_EMERALD_PURPLE,
4779   EL_BIOMAZE,
4780
4781   EL_WALL_EMERALD_YELLOW,
4782   EL_WALL_EMERALD_RED,
4783   EL_WALL_EMERALD_PURPLE,
4784   EL_WALL_BD_DIAMOND,
4785
4786   EL_SPEED_PILL,
4787   EL_PACMAN_UP,
4788   EL_TIME_ORB_FULL,
4789   EL_TIME_ORB_EMPTY,
4790
4791   EL_PACMAN_LEFT,
4792   EL_DARK_YAMYAM,
4793   EL_PACMAN_RIGHT,
4794   EL_YAMYAM,                    // RND
4795
4796   EL_BLACK_ORB,
4797   EL_PACMAN_DOWN,
4798   EL_LAMP,
4799   EL_LAMP_ACTIVE,
4800
4801   EL_DYNABOMB_INCREASE_NUMBER,
4802   EL_DYNABOMB_INCREASE_SIZE,
4803   EL_DYNABOMB_INCREASE_POWER,
4804   EL_STONEBLOCK,
4805
4806   EL_MOLE,
4807   EL_PENGUIN,
4808   EL_PIG,
4809   EL_DRAGON,
4810
4811   EL_BUG,
4812   EL_MOLE_UP,
4813   EL_BD_BUTTERFLY,
4814   EL_BD_FIREFLY,
4815
4816   EL_MOLE_LEFT,
4817   EL_SATELLITE,
4818   EL_MOLE_RIGHT,
4819   EL_PACMAN,
4820
4821   EL_SPACESHIP,
4822   EL_MOLE_DOWN,
4823   EL_INVISIBLE_STEELWALL,
4824   EL_INVISIBLE_WALL,
4825
4826   EL_EXPANDABLE_WALL,
4827   EL_EXPANDABLE_WALL_HORIZONTAL,
4828   EL_EXPANDABLE_WALL_VERTICAL,
4829   EL_EXPANDABLE_WALL_ANY,
4830 };
4831 static int *editor_hl_rnd_ptr = editor_hl_rnd;
4832 static int *editor_el_rnd_ptr = editor_el_rnd;
4833 static int num_editor_hl_rnd = ARRAY_SIZE(editor_hl_rnd);
4834 static int num_editor_el_rnd = ARRAY_SIZE(editor_el_rnd);
4835
4836 static int editor_hl_sokoban[] =
4837 {
4838   EL_INTERNAL_CASCADE_SB_ACTIVE,
4839   EL_CHAR('S'),
4840   EL_CHAR('B'),
4841   EL_EMPTY,
4842 };
4843
4844 static int editor_el_sokoban[] =
4845 {
4846   EL_SOKOBAN_OBJECT,
4847   EL_SOKOBAN_FIELD_EMPTY,
4848   EL_SOKOBAN_FIELD_FULL,
4849   EL_SOKOBAN_FIELD_PLAYER,
4850 };
4851 static int *editor_hl_sokoban_ptr = editor_hl_sokoban;
4852 static int *editor_el_sokoban_ptr = editor_el_sokoban;
4853 static int num_editor_hl_sokoban = ARRAY_SIZE(editor_hl_sokoban);
4854 static int num_editor_el_sokoban = ARRAY_SIZE(editor_el_sokoban);
4855
4856 static int editor_hl_supaplex[] =
4857 {
4858   EL_INTERNAL_CASCADE_SP_ACTIVE,
4859   EL_CHAR('S'),
4860   EL_CHAR('P'),
4861   EL_EMPTY,
4862 };
4863
4864 static int editor_el_supaplex[] =
4865 {
4866   EL_SP_MURPHY,
4867   EL_EMPTY,
4868   EL_SP_BASE,
4869   EL_SP_BUGGY_BASE,
4870
4871   EL_SP_INFOTRON,
4872   EL_SP_ZONK,
4873   EL_SP_SNIKSNAK,
4874   EL_SP_ELECTRON,
4875
4876   EL_SP_DISK_RED,
4877   EL_SP_DISK_ORANGE,
4878   EL_SP_DISK_YELLOW,
4879   EL_SP_TERMINAL,
4880
4881   EL_SP_EXIT_CLOSED,
4882   EL_SP_PORT_HORIZONTAL,
4883   EL_SP_PORT_VERTICAL,
4884   EL_SP_PORT_ANY,
4885
4886   EL_SP_PORT_LEFT,
4887   EL_SP_PORT_RIGHT,
4888   EL_SP_PORT_UP,
4889   EL_SP_PORT_DOWN,
4890
4891   EL_SP_GRAVITY_PORT_LEFT,
4892   EL_SP_GRAVITY_PORT_RIGHT,
4893   EL_SP_GRAVITY_PORT_UP,
4894   EL_SP_GRAVITY_PORT_DOWN,
4895
4896   EL_SP_GRAVITY_ON_PORT_LEFT,
4897   EL_SP_GRAVITY_ON_PORT_RIGHT,
4898   EL_SP_GRAVITY_ON_PORT_UP,
4899   EL_SP_GRAVITY_ON_PORT_DOWN,
4900
4901   EL_SP_GRAVITY_OFF_PORT_LEFT,
4902   EL_SP_GRAVITY_OFF_PORT_RIGHT,
4903   EL_SP_GRAVITY_OFF_PORT_UP,
4904   EL_SP_GRAVITY_OFF_PORT_DOWN,
4905
4906   EL_SP_HARDWARE_GRAY,
4907   EL_SP_HARDWARE_GREEN,
4908   EL_SP_HARDWARE_BLUE,
4909   EL_SP_HARDWARE_RED,
4910
4911   EL_SP_HARDWARE_BASE_1,
4912   EL_SP_HARDWARE_BASE_2,
4913   EL_SP_HARDWARE_BASE_3,
4914   EL_SP_HARDWARE_BASE_4,
4915
4916   EL_SP_HARDWARE_BASE_5,
4917   EL_SP_HARDWARE_BASE_6,
4918   EL_SP_HARDWARE_YELLOW,
4919   EL_SP_CHIP_TOP,
4920
4921   EL_SP_CHIP_SINGLE,
4922   EL_SP_CHIP_LEFT,
4923   EL_SP_CHIP_RIGHT,
4924   EL_SP_CHIP_BOTTOM,
4925 };
4926 static int *editor_hl_supaplex_ptr = editor_hl_supaplex;
4927 static int *editor_el_supaplex_ptr = editor_el_supaplex;
4928 static int num_editor_hl_supaplex = ARRAY_SIZE(editor_hl_supaplex);
4929 static int num_editor_el_supaplex = ARRAY_SIZE(editor_el_supaplex);
4930
4931 static int editor_hl_diamond_caves[] =
4932 {
4933   EL_INTERNAL_CASCADE_DC_ACTIVE,
4934   EL_CHAR('D'),
4935   EL_CHAR('C'),
4936   EL_CHAR('2'),
4937 };
4938
4939 static int editor_el_diamond_caves[] =
4940 {
4941   EL_EM_STEEL_EXIT_CLOSED,      // DC2
4942   EL_EM_STEEL_EXIT_OPEN,        // DC2
4943   EL_WALL_EMERALD,              // DC2
4944   EL_WALL_DIAMOND,              // DC2
4945
4946   EL_PEARL,
4947   EL_CRYSTAL,
4948   EL_WALL_PEARL,
4949   EL_WALL_CRYSTAL,
4950
4951   EL_CONVEYOR_BELT_1_LEFT,
4952   EL_CONVEYOR_BELT_1_MIDDLE,
4953   EL_CONVEYOR_BELT_1_RIGHT,
4954   EL_CONVEYOR_BELT_1_SWITCH_MIDDLE,
4955
4956   EL_CONVEYOR_BELT_2_LEFT,
4957   EL_CONVEYOR_BELT_2_MIDDLE,
4958   EL_CONVEYOR_BELT_2_RIGHT,
4959   EL_CONVEYOR_BELT_2_SWITCH_MIDDLE,
4960
4961   EL_CONVEYOR_BELT_3_LEFT,
4962   EL_CONVEYOR_BELT_3_MIDDLE,
4963   EL_CONVEYOR_BELT_3_RIGHT,
4964   EL_CONVEYOR_BELT_3_SWITCH_MIDDLE,
4965
4966   EL_CONVEYOR_BELT_4_LEFT,
4967   EL_CONVEYOR_BELT_4_MIDDLE,
4968   EL_CONVEYOR_BELT_4_RIGHT,
4969   EL_CONVEYOR_BELT_4_SWITCH_MIDDLE,
4970
4971   EL_CONVEYOR_BELT_1_SWITCH_LEFT,
4972   EL_CONVEYOR_BELT_2_SWITCH_LEFT,
4973   EL_CONVEYOR_BELT_3_SWITCH_LEFT,
4974   EL_CONVEYOR_BELT_4_SWITCH_LEFT,
4975
4976   EL_CONVEYOR_BELT_1_SWITCH_RIGHT,
4977   EL_CONVEYOR_BELT_2_SWITCH_RIGHT,
4978   EL_CONVEYOR_BELT_3_SWITCH_RIGHT,
4979   EL_CONVEYOR_BELT_4_SWITCH_RIGHT,
4980
4981   EL_TIMEGATE_CLOSED,
4982   EL_TIMEGATE_OPEN,
4983   EL_TIMEGATE_SWITCH,
4984   EL_DC_TIMEGATE_SWITCH,
4985
4986   EL_SWITCHGATE_CLOSED,
4987   EL_SWITCHGATE_OPEN,
4988   EL_SWITCHGATE_SWITCH_UP,
4989   EL_SWITCHGATE_SWITCH_DOWN,
4990
4991   EL_LIGHT_SWITCH,
4992   EL_LIGHT_SWITCH_ACTIVE,
4993   EL_DC_SWITCHGATE_SWITCH_UP,
4994   EL_DC_SWITCHGATE_SWITCH_DOWN,
4995
4996   EL_STEEL_EXIT_CLOSED,
4997   EL_STEEL_EXIT_OPEN,
4998   EL_STEELWALL_SLIPPERY,
4999   EL_INVISIBLE_SAND,
5000
5001   EL_QUICKSAND_FAST_EMPTY,
5002   EL_QUICKSAND_FAST_FULL,
5003   EL_LANDMINE,
5004   EL_DC_LANDMINE,
5005
5006   EL_SHIELD_NORMAL,
5007   EL_SHIELD_DEADLY,
5008   EL_EXTRA_TIME,
5009   EL_DC_MAGIC_WALL,
5010
5011   EL_ENVELOPE_1,
5012   EL_ENVELOPE_2,
5013   EL_ENVELOPE_3,
5014   EL_ENVELOPE_4,
5015
5016   EL_SIGN_RADIOACTIVITY,
5017   EL_SIGN_WHEELCHAIR,
5018   EL_SIGN_PARKING,
5019   EL_SIGN_NO_ENTRY,
5020
5021   EL_SIGN_GIVE_WAY,
5022   EL_SIGN_ENTRY_FORBIDDEN,
5023   EL_SIGN_EMERGENCY_EXIT,
5024   EL_SIGN_YIN_YANG,
5025
5026 #if 0
5027   EL_SIGN_SPERMS,
5028   EL_SIGN_BULLET,
5029   EL_SIGN_HEART,
5030   EL_SIGN_CROSS,
5031
5032   EL_SIGN_FRANKIE,
5033   EL_EMPTY,
5034   EL_EMPTY,
5035   EL_EMPTY,
5036
5037   EL_SPERMS,
5038   EL_BULLET,
5039   EL_HEART,
5040   EL_CROSS,
5041
5042   EL_FRANKIE,
5043   EL_EMPTY,
5044   EL_EMPTY,
5045   EL_EMPTY,
5046 #endif
5047
5048   EL_DC_STEELWALL_2_SINGLE,
5049   EL_DC_STEELWALL_2_TOP,
5050   EL_SIGN_EXCLAMATION,
5051   EL_SIGN_STOP,
5052
5053   EL_DC_STEELWALL_2_LEFT,
5054   EL_DC_STEELWALL_2_MIDDLE,
5055   EL_DC_STEELWALL_2_HORIZONTAL,
5056   EL_DC_STEELWALL_2_RIGHT,
5057
5058   EL_DC_STEELWALL_1_TOPLEFT,
5059   EL_DC_STEELWALL_2_VERTICAL,
5060   EL_DC_STEELWALL_1_TOPRIGHT,
5061   EL_DC_GATE_WHITE,
5062
5063   EL_DC_STEELWALL_1_VERTICAL,
5064   EL_DC_STEELWALL_2_BOTTOM,
5065   EL_DC_KEY_WHITE,
5066   EL_DC_GATE_WHITE_GRAY,
5067
5068   EL_DC_STEELWALL_1_BOTTOMLEFT,
5069   EL_DC_STEELWALL_1_HORIZONTAL,
5070   EL_DC_STEELWALL_1_BOTTOMRIGHT,
5071   EL_DC_GATE_FAKE_GRAY,
5072
5073   EL_DC_STEELWALL_1_BOTTOMRIGHT_2,
5074   EL_DC_STEELWALL_1_BOTTOM,
5075   EL_DC_STEELWALL_1_BOTTOMLEFT_2,
5076   EL_EXPANDABLE_STEELWALL_HORIZONTAL,
5077
5078   EL_DC_STEELWALL_1_RIGHT,
5079   EL_EMPTY,
5080   EL_DC_STEELWALL_1_LEFT,
5081   EL_EXPANDABLE_STEELWALL_VERTICAL,
5082
5083   EL_DC_STEELWALL_1_TOPRIGHT_2,
5084   EL_DC_STEELWALL_1_TOP,
5085   EL_DC_STEELWALL_1_TOPLEFT_2,
5086   EL_EXPANDABLE_STEELWALL_ANY,
5087 };
5088 static int *editor_hl_diamond_caves_ptr = editor_hl_diamond_caves;
5089 static int *editor_el_diamond_caves_ptr = editor_el_diamond_caves;
5090 static int num_editor_hl_diamond_caves = ARRAY_SIZE(editor_hl_diamond_caves);
5091 static int num_editor_el_diamond_caves = ARRAY_SIZE(editor_el_diamond_caves);
5092
5093 static int editor_hl_dx_boulderdash[] =
5094 {
5095   EL_INTERNAL_CASCADE_DX_ACTIVE,
5096   EL_CHAR('D'),
5097   EL_CHAR('X'),
5098   EL_EMPTY,
5099 };
5100
5101 static int editor_el_dx_boulderdash[] =
5102 {
5103   EL_EMPTY,
5104   EL_TUBE_RIGHT_DOWN,
5105   EL_TUBE_HORIZONTAL_DOWN,
5106   EL_TUBE_LEFT_DOWN,
5107
5108   EL_TUBE_HORIZONTAL,
5109   EL_TUBE_VERTICAL_RIGHT,
5110   EL_TUBE_ANY,
5111   EL_TUBE_VERTICAL_LEFT,
5112
5113   EL_TUBE_VERTICAL,
5114   EL_TUBE_RIGHT_UP,
5115   EL_TUBE_HORIZONTAL_UP,
5116   EL_TUBE_LEFT_UP,
5117
5118   EL_TRAP,
5119   EL_DX_SUPABOMB,
5120   EL_EMPTY,
5121   EL_EMPTY
5122 };
5123 static int *editor_hl_dx_boulderdash_ptr = editor_hl_dx_boulderdash;
5124 static int *editor_el_dx_boulderdash_ptr = editor_el_dx_boulderdash;
5125 static int num_editor_hl_dx_boulderdash = ARRAY_SIZE(editor_hl_dx_boulderdash);
5126 static int num_editor_el_dx_boulderdash = ARRAY_SIZE(editor_el_dx_boulderdash);
5127
5128 static int editor_hl_mirror_magic[] =
5129 {
5130   EL_INTERNAL_CASCADE_MM_ACTIVE,
5131   EL_CHAR('M'),
5132   EL_CHAR('M'),
5133   EL_EMPTY,
5134 };
5135
5136 static int editor_el_mirror_magic[] =
5137 {
5138   EL_MM_MCDUFFIN_RIGHT,
5139   EL_MM_MCDUFFIN_UP,
5140   EL_MM_MCDUFFIN_LEFT,
5141   EL_MM_MCDUFFIN_DOWN,
5142
5143   EL_MM_MIRROR_START,
5144   EL_MM_MIRROR_FIXED_START,
5145   EL_MM_POLARIZER_START,
5146   EL_MM_POLARIZER_CROSS_START,
5147
5148   EL_MM_TELEPORTER_RED_START,
5149   EL_MM_TELEPORTER_YELLOW_START,
5150   EL_MM_TELEPORTER_GREEN_START,
5151   EL_MM_TELEPORTER_BLUE_START,
5152
5153   EL_MM_PRISM,
5154   EL_MM_FUSE_ACTIVE,
5155   EL_MM_PACMAN_RIGHT,
5156   EL_MM_EXIT_CLOSED,
5157
5158   EL_MM_KETTLE,
5159   EL_MM_BOMB,
5160   EL_MM_KEY,
5161   EL_MM_FUEL_FULL,
5162
5163   EL_MM_LIGHTBULB,
5164   EL_MM_LIGHTBULB_ACTIVE,
5165   EL_MM_GRAY_BALL,
5166   EL_MM_LIGHTBALL,
5167
5168   EL_MM_STEEL_WALL,
5169   EL_MM_WOODEN_WALL,
5170   EL_MM_ICE_WALL,
5171   EL_MM_AMOEBA_WALL,
5172
5173   EL_MM_STEEL_LOCK,
5174   EL_MM_WOODEN_LOCK,
5175   EL_MM_STEEL_BLOCK,
5176   EL_MM_WOODEN_BLOCK,
5177
5178   EL_MM_STEEL_GRID_FIXED_1,
5179   EL_MM_STEEL_GRID_FIXED_2,
5180   EL_MM_STEEL_GRID_FIXED_3,
5181   EL_MM_STEEL_GRID_FIXED_4,
5182
5183   EL_MM_WOODEN_GRID_FIXED_1,
5184   EL_MM_WOODEN_GRID_FIXED_2,
5185   EL_MM_WOODEN_GRID_FIXED_3,
5186   EL_MM_WOODEN_GRID_FIXED_4,
5187
5188   EL_MM_ENVELOPE_1,
5189   EL_MM_ENVELOPE_2,
5190   EL_MM_ENVELOPE_3,
5191   EL_MM_ENVELOPE_4
5192 };
5193 static int *editor_hl_mirror_magic_ptr = editor_hl_mirror_magic;
5194 static int *editor_el_mirror_magic_ptr = editor_el_mirror_magic;
5195 static int num_editor_hl_mirror_magic = ARRAY_SIZE(editor_hl_mirror_magic);
5196 static int num_editor_el_mirror_magic = ARRAY_SIZE(editor_el_mirror_magic);
5197
5198 static int editor_hl_deflektor[] =
5199 {
5200   EL_INTERNAL_CASCADE_DF_ACTIVE,
5201   EL_CHAR('D'),
5202   EL_CHAR('F'),
5203   EL_EMPTY,
5204 };
5205
5206 static int editor_el_deflektor[] =
5207 {
5208   EL_DF_LASER_RIGHT,
5209   EL_DF_LASER_UP,
5210   EL_DF_LASER_LEFT,
5211   EL_DF_LASER_DOWN,
5212
5213   EL_DF_RECEIVER_RIGHT,
5214   EL_DF_RECEIVER_UP,
5215   EL_DF_RECEIVER_LEFT,
5216   EL_DF_RECEIVER_DOWN,
5217
5218   EL_DF_MIRROR_START,
5219   EL_DF_MIRROR_ROTATING_START,
5220   EL_DF_MIRROR_FIXED_START,
5221   EL_DF_CELL,
5222
5223   EL_DF_FIBRE_OPTIC_RED_1,
5224   EL_DF_FIBRE_OPTIC_YELLOW_1,
5225   EL_DF_FIBRE_OPTIC_GREEN_1,
5226   EL_DF_FIBRE_OPTIC_BLUE_1,
5227
5228   EL_DF_STEEL_GRID_FIXED_START,
5229   EL_DF_STEEL_GRID_ROTATING_START,
5230   EL_DF_WOODEN_GRID_FIXED_START,
5231   EL_DF_WOODEN_GRID_ROTATING_START,
5232
5233   EL_DF_STEEL_WALL,
5234   EL_DF_WOODEN_WALL,
5235   EL_DF_REFRACTOR,
5236   EL_DF_MINE,
5237
5238   EL_DF_SLOPE_1,
5239   EL_DF_SLOPE_2,
5240   EL_DF_SLOPE_3,
5241   EL_DF_SLOPE_4
5242 };
5243 static int *editor_hl_deflektor_ptr = editor_hl_deflektor;
5244 static int *editor_el_deflektor_ptr = editor_el_deflektor;
5245 static int num_editor_hl_deflektor = ARRAY_SIZE(editor_hl_deflektor);
5246 static int num_editor_el_deflektor = ARRAY_SIZE(editor_el_deflektor);
5247
5248 static int editor_hl_chars[] =
5249 {
5250   EL_INTERNAL_CASCADE_CHARS_ACTIVE,
5251   EL_CHAR('T'),
5252   EL_CHAR('X'),
5253   EL_CHAR('T'),
5254 };
5255
5256 static int editor_el_chars[] =
5257 {
5258   EL_CHAR(' '),
5259   EL_CHAR('!'),
5260   EL_CHAR('"'),
5261   EL_CHAR('#'),
5262
5263   EL_CHAR('$'),
5264   EL_CHAR('%'),
5265   EL_CHAR('&'),
5266   EL_CHAR('\''),
5267
5268   EL_CHAR('('),
5269   EL_CHAR(')'),
5270   EL_CHAR('*'),
5271   EL_CHAR('+'),
5272
5273   EL_CHAR(','),
5274   EL_CHAR('-'),
5275   EL_CHAR('.'),
5276   EL_CHAR('/'),
5277
5278   EL_CHAR('0'),
5279   EL_CHAR('1'),
5280   EL_CHAR('2'),
5281   EL_CHAR('3'),
5282
5283   EL_CHAR('4'),
5284   EL_CHAR('5'),
5285   EL_CHAR('6'),
5286   EL_CHAR('7'),
5287
5288   EL_CHAR('8'),
5289   EL_CHAR('9'),
5290   EL_CHAR(':'),
5291   EL_CHAR(';'),
5292
5293   EL_CHAR('<'),
5294   EL_CHAR('='),
5295   EL_CHAR('>'),
5296   EL_CHAR('?'),
5297
5298   EL_CHAR('@'),
5299   EL_CHAR('A'),
5300   EL_CHAR('B'),
5301   EL_CHAR('C'),
5302
5303   EL_CHAR('D'),
5304   EL_CHAR('E'),
5305   EL_CHAR('F'),
5306   EL_CHAR('G'),
5307
5308   EL_CHAR('H'),
5309   EL_CHAR('I'),
5310   EL_CHAR('J'),
5311   EL_CHAR('K'),
5312
5313   EL_CHAR('L'),
5314   EL_CHAR('M'),
5315   EL_CHAR('N'),
5316   EL_CHAR('O'),
5317
5318   EL_CHAR('P'),
5319   EL_CHAR('Q'),
5320   EL_CHAR('R'),
5321   EL_CHAR('S'),
5322
5323   EL_CHAR('T'),
5324   EL_CHAR('U'),
5325   EL_CHAR('V'),
5326   EL_CHAR('W'),
5327
5328   EL_CHAR('X'),
5329   EL_CHAR('Y'),
5330   EL_CHAR('Z'),
5331   EL_CHAR('['),
5332
5333   EL_CHAR('\\'),
5334   EL_CHAR(']'),
5335   EL_CHAR('^'),
5336   EL_CHAR('_'),
5337
5338   EL_CHAR(CHAR_BYTE_COPYRIGHT),
5339   EL_CHAR(CHAR_BYTE_UMLAUT_A),
5340   EL_CHAR(CHAR_BYTE_UMLAUT_O),
5341   EL_CHAR(CHAR_BYTE_UMLAUT_U),
5342
5343   EL_CHAR(CHAR_BYTE_DEGREE),
5344   EL_CHAR(CHAR_BYTE_REGISTERED),
5345   EL_CHAR(FONT_ASCII_CURSOR),
5346   EL_CHAR(FONT_ASCII_BUTTON),
5347
5348   EL_CHAR(FONT_ASCII_UP),
5349   EL_CHAR(FONT_ASCII_DOWN),
5350   EL_CHAR(' '),
5351   EL_CHAR(' ')
5352 };
5353 static int *editor_hl_chars_ptr = editor_hl_chars;
5354 static int *editor_el_chars_ptr = editor_el_chars;
5355 static int num_editor_hl_chars = ARRAY_SIZE(editor_hl_chars);
5356 static int num_editor_el_chars = ARRAY_SIZE(editor_el_chars);
5357
5358 static int editor_hl_steel_chars[] =
5359 {
5360   EL_INTERNAL_CASCADE_STEEL_CHARS_ACTIVE,
5361   EL_STEEL_CHAR('T'),
5362   EL_STEEL_CHAR('X'),
5363   EL_STEEL_CHAR('T'),
5364 };
5365
5366 static int editor_el_steel_chars[] =
5367 {
5368   EL_STEEL_CHAR(' '),
5369   EL_STEEL_CHAR('!'),
5370   EL_STEEL_CHAR('"'),
5371   EL_STEEL_CHAR('#'),
5372
5373   EL_STEEL_CHAR('$'),
5374   EL_STEEL_CHAR('%'),
5375   EL_STEEL_CHAR('&'),
5376   EL_STEEL_CHAR('\''),
5377
5378   EL_STEEL_CHAR('('),
5379   EL_STEEL_CHAR(')'),
5380   EL_STEEL_CHAR('*'),
5381   EL_STEEL_CHAR('+'),
5382
5383   EL_STEEL_CHAR(','),
5384   EL_STEEL_CHAR('-'),
5385   EL_STEEL_CHAR('.'),
5386   EL_STEEL_CHAR('/'),
5387
5388   EL_STEEL_CHAR('0'),
5389   EL_STEEL_CHAR('1'),
5390   EL_STEEL_CHAR('2'),
5391   EL_STEEL_CHAR('3'),
5392
5393   EL_STEEL_CHAR('4'),
5394   EL_STEEL_CHAR('5'),
5395   EL_STEEL_CHAR('6'),
5396   EL_STEEL_CHAR('7'),
5397
5398   EL_STEEL_CHAR('8'),
5399   EL_STEEL_CHAR('9'),
5400   EL_STEEL_CHAR(':'),
5401   EL_STEEL_CHAR(';'),
5402
5403   EL_STEEL_CHAR('<'),
5404   EL_STEEL_CHAR('='),
5405   EL_STEEL_CHAR('>'),
5406   EL_STEEL_CHAR('?'),
5407
5408   EL_STEEL_CHAR('@'),
5409   EL_STEEL_CHAR('A'),
5410   EL_STEEL_CHAR('B'),
5411   EL_STEEL_CHAR('C'),
5412
5413   EL_STEEL_CHAR('D'),
5414   EL_STEEL_CHAR('E'),
5415   EL_STEEL_CHAR('F'),
5416   EL_STEEL_CHAR('G'),
5417
5418   EL_STEEL_CHAR('H'),
5419   EL_STEEL_CHAR('I'),
5420   EL_STEEL_CHAR('J'),
5421   EL_STEEL_CHAR('K'),
5422
5423   EL_STEEL_CHAR('L'),
5424   EL_STEEL_CHAR('M'),
5425   EL_STEEL_CHAR('N'),
5426   EL_STEEL_CHAR('O'),
5427
5428   EL_STEEL_CHAR('P'),
5429   EL_STEEL_CHAR('Q'),
5430   EL_STEEL_CHAR('R'),
5431   EL_STEEL_CHAR('S'),
5432
5433   EL_STEEL_CHAR('T'),
5434   EL_STEEL_CHAR('U'),
5435   EL_STEEL_CHAR('V'),
5436   EL_STEEL_CHAR('W'),
5437
5438   EL_STEEL_CHAR('X'),
5439   EL_STEEL_CHAR('Y'),
5440   EL_STEEL_CHAR('Z'),
5441   EL_STEEL_CHAR('['),
5442
5443   EL_STEEL_CHAR('\\'),
5444   EL_STEEL_CHAR(']'),
5445   EL_STEEL_CHAR('^'),
5446   EL_STEEL_CHAR('_'),
5447
5448   EL_STEEL_CHAR(CHAR_BYTE_COPYRIGHT),
5449   EL_STEEL_CHAR(CHAR_BYTE_UMLAUT_A),
5450   EL_STEEL_CHAR(CHAR_BYTE_UMLAUT_O),
5451   EL_STEEL_CHAR(CHAR_BYTE_UMLAUT_U),
5452
5453   EL_STEEL_CHAR(CHAR_BYTE_DEGREE),
5454   EL_STEEL_CHAR(CHAR_BYTE_REGISTERED),
5455   EL_STEEL_CHAR(FONT_ASCII_CURSOR),
5456   EL_STEEL_CHAR(FONT_ASCII_BUTTON),
5457
5458   EL_STEEL_CHAR(FONT_ASCII_UP),
5459   EL_STEEL_CHAR(FONT_ASCII_DOWN),
5460   EL_STEEL_CHAR(' '),
5461   EL_STEEL_CHAR(' ')
5462 };
5463 static int *editor_hl_steel_chars_ptr = editor_hl_steel_chars;
5464 static int *editor_el_steel_chars_ptr = editor_el_steel_chars;
5465 static int num_editor_hl_steel_chars = ARRAY_SIZE(editor_hl_steel_chars);
5466 static int num_editor_el_steel_chars = ARRAY_SIZE(editor_el_steel_chars);
5467
5468 static int editor_hl_custom[] =
5469 {
5470   EL_INTERNAL_CASCADE_CE_ACTIVE,
5471   EL_CHAR('C'),
5472   EL_CHAR('E'),
5473   EL_EMPTY,
5474 };
5475
5476 static int editor_el_custom[] =
5477 {
5478   EL_CUSTOM_START + 0,
5479   EL_CUSTOM_START + 1,
5480   EL_CUSTOM_START + 2,
5481   EL_CUSTOM_START + 3,
5482
5483   EL_CUSTOM_START + 4,
5484   EL_CUSTOM_START + 5,
5485   EL_CUSTOM_START + 6,
5486   EL_CUSTOM_START + 7,
5487
5488   EL_CUSTOM_START + 8,
5489   EL_CUSTOM_START + 9,
5490   EL_CUSTOM_START + 10,
5491   EL_CUSTOM_START + 11,
5492
5493   EL_CUSTOM_START + 12,
5494   EL_CUSTOM_START + 13,
5495   EL_CUSTOM_START + 14,
5496   EL_CUSTOM_START + 15,
5497
5498   EL_CUSTOM_START + 16,
5499   EL_CUSTOM_START + 17,
5500   EL_CUSTOM_START + 18,
5501   EL_CUSTOM_START + 19,
5502
5503   EL_CUSTOM_START + 20,
5504   EL_CUSTOM_START + 21,
5505   EL_CUSTOM_START + 22,
5506   EL_CUSTOM_START + 23,
5507
5508   EL_CUSTOM_START + 24,
5509   EL_CUSTOM_START + 25,
5510   EL_CUSTOM_START + 26,
5511   EL_CUSTOM_START + 27,
5512
5513   EL_CUSTOM_START + 28,
5514   EL_CUSTOM_START + 29,
5515   EL_CUSTOM_START + 30,
5516   EL_CUSTOM_START + 31,
5517
5518   EL_CUSTOM_START + 32,
5519   EL_CUSTOM_START + 33,
5520   EL_CUSTOM_START + 34,
5521   EL_CUSTOM_START + 35,
5522
5523   EL_CUSTOM_START + 36,
5524   EL_CUSTOM_START + 37,
5525   EL_CUSTOM_START + 38,
5526   EL_CUSTOM_START + 39,
5527
5528   EL_CUSTOM_START + 40,
5529   EL_CUSTOM_START + 41,
5530   EL_CUSTOM_START + 42,
5531   EL_CUSTOM_START + 43,
5532
5533   EL_CUSTOM_START + 44,
5534   EL_CUSTOM_START + 45,
5535   EL_CUSTOM_START + 46,
5536   EL_CUSTOM_START + 47,
5537
5538   EL_CUSTOM_START + 48,
5539   EL_CUSTOM_START + 49,
5540   EL_CUSTOM_START + 50,
5541   EL_CUSTOM_START + 51,
5542
5543   EL_CUSTOM_START + 52,
5544   EL_CUSTOM_START + 53,
5545   EL_CUSTOM_START + 54,
5546   EL_CUSTOM_START + 55,
5547
5548   EL_CUSTOM_START + 56,
5549   EL_CUSTOM_START + 57,
5550   EL_CUSTOM_START + 58,
5551   EL_CUSTOM_START + 59,
5552
5553   EL_CUSTOM_START + 60,
5554   EL_CUSTOM_START + 61,
5555   EL_CUSTOM_START + 62,
5556   EL_CUSTOM_START + 63,
5557
5558   EL_CUSTOM_START + 64,
5559   EL_CUSTOM_START + 65,
5560   EL_CUSTOM_START + 66,
5561   EL_CUSTOM_START + 67,
5562
5563   EL_CUSTOM_START + 68,
5564   EL_CUSTOM_START + 69,
5565   EL_CUSTOM_START + 70,
5566   EL_CUSTOM_START + 71,
5567
5568   EL_CUSTOM_START + 72,
5569   EL_CUSTOM_START + 73,
5570   EL_CUSTOM_START + 74,
5571   EL_CUSTOM_START + 75,
5572
5573   EL_CUSTOM_START + 76,
5574   EL_CUSTOM_START + 77,
5575   EL_CUSTOM_START + 78,
5576   EL_CUSTOM_START + 79,
5577
5578   EL_CUSTOM_START + 80,
5579   EL_CUSTOM_START + 81,
5580   EL_CUSTOM_START + 82,
5581   EL_CUSTOM_START + 83,
5582
5583   EL_CUSTOM_START + 84,
5584   EL_CUSTOM_START + 85,
5585   EL_CUSTOM_START + 86,
5586   EL_CUSTOM_START + 87,
5587
5588   EL_CUSTOM_START + 88,
5589   EL_CUSTOM_START + 89,
5590   EL_CUSTOM_START + 90,
5591   EL_CUSTOM_START + 91,
5592
5593   EL_CUSTOM_START + 92,
5594   EL_CUSTOM_START + 93,
5595   EL_CUSTOM_START + 94,
5596   EL_CUSTOM_START + 95,
5597
5598   EL_CUSTOM_START + 96,
5599   EL_CUSTOM_START + 97,
5600   EL_CUSTOM_START + 98,
5601   EL_CUSTOM_START + 99,
5602
5603   EL_CUSTOM_START + 100,
5604   EL_CUSTOM_START + 101,
5605   EL_CUSTOM_START + 102,
5606   EL_CUSTOM_START + 103,
5607
5608   EL_CUSTOM_START + 104,
5609   EL_CUSTOM_START + 105,
5610   EL_CUSTOM_START + 106,
5611   EL_CUSTOM_START + 107,
5612
5613   EL_CUSTOM_START + 108,
5614   EL_CUSTOM_START + 109,
5615   EL_CUSTOM_START + 110,
5616   EL_CUSTOM_START + 111,
5617
5618   EL_CUSTOM_START + 112,
5619   EL_CUSTOM_START + 113,
5620   EL_CUSTOM_START + 114,
5621   EL_CUSTOM_START + 115,
5622
5623   EL_CUSTOM_START + 116,
5624   EL_CUSTOM_START + 117,
5625   EL_CUSTOM_START + 118,
5626   EL_CUSTOM_START + 119,
5627
5628   EL_CUSTOM_START + 120,
5629   EL_CUSTOM_START + 121,
5630   EL_CUSTOM_START + 122,
5631   EL_CUSTOM_START + 123,
5632
5633   EL_CUSTOM_START + 124,
5634   EL_CUSTOM_START + 125,
5635   EL_CUSTOM_START + 126,
5636   EL_CUSTOM_START + 127,
5637
5638   EL_CUSTOM_START + 128,
5639   EL_CUSTOM_START + 129,
5640   EL_CUSTOM_START + 130,
5641   EL_CUSTOM_START + 131,
5642
5643   EL_CUSTOM_START + 132,
5644   EL_CUSTOM_START + 133,
5645   EL_CUSTOM_START + 134,
5646   EL_CUSTOM_START + 135,
5647
5648   EL_CUSTOM_START + 136,
5649   EL_CUSTOM_START + 137,
5650   EL_CUSTOM_START + 138,
5651   EL_CUSTOM_START + 139,
5652
5653   EL_CUSTOM_START + 140,
5654   EL_CUSTOM_START + 141,
5655   EL_CUSTOM_START + 142,
5656   EL_CUSTOM_START + 143,
5657
5658   EL_CUSTOM_START + 144,
5659   EL_CUSTOM_START + 145,
5660   EL_CUSTOM_START + 146,
5661   EL_CUSTOM_START + 147,
5662
5663   EL_CUSTOM_START + 148,
5664   EL_CUSTOM_START + 149,
5665   EL_CUSTOM_START + 150,
5666   EL_CUSTOM_START + 151,
5667
5668   EL_CUSTOM_START + 152,
5669   EL_CUSTOM_START + 153,
5670   EL_CUSTOM_START + 154,
5671   EL_CUSTOM_START + 155,
5672
5673   EL_CUSTOM_START + 156,
5674   EL_CUSTOM_START + 157,
5675   EL_CUSTOM_START + 158,
5676   EL_CUSTOM_START + 159,
5677
5678   EL_CUSTOM_START + 160,
5679   EL_CUSTOM_START + 161,
5680   EL_CUSTOM_START + 162,
5681   EL_CUSTOM_START + 163,
5682
5683   EL_CUSTOM_START + 164,
5684   EL_CUSTOM_START + 165,
5685   EL_CUSTOM_START + 166,
5686   EL_CUSTOM_START + 167,
5687
5688   EL_CUSTOM_START + 168,
5689   EL_CUSTOM_START + 169,
5690   EL_CUSTOM_START + 170,
5691   EL_CUSTOM_START + 171,
5692
5693   EL_CUSTOM_START + 172,
5694   EL_CUSTOM_START + 173,
5695   EL_CUSTOM_START + 174,
5696   EL_CUSTOM_START + 175,
5697
5698   EL_CUSTOM_START + 176,
5699   EL_CUSTOM_START + 177,
5700   EL_CUSTOM_START + 178,
5701   EL_CUSTOM_START + 179,
5702
5703   EL_CUSTOM_START + 180,
5704   EL_CUSTOM_START + 181,
5705   EL_CUSTOM_START + 182,
5706   EL_CUSTOM_START + 183,
5707
5708   EL_CUSTOM_START + 184,
5709   EL_CUSTOM_START + 185,
5710   EL_CUSTOM_START + 186,
5711   EL_CUSTOM_START + 187,
5712
5713   EL_CUSTOM_START + 188,
5714   EL_CUSTOM_START + 189,
5715   EL_CUSTOM_START + 190,
5716   EL_CUSTOM_START + 191,
5717
5718   EL_CUSTOM_START + 192,
5719   EL_CUSTOM_START + 193,
5720   EL_CUSTOM_START + 194,
5721   EL_CUSTOM_START + 195,
5722
5723   EL_CUSTOM_START + 196,
5724   EL_CUSTOM_START + 197,
5725   EL_CUSTOM_START + 198,
5726   EL_CUSTOM_START + 199,
5727
5728   EL_CUSTOM_START + 200,
5729   EL_CUSTOM_START + 201,
5730   EL_CUSTOM_START + 202,
5731   EL_CUSTOM_START + 203,
5732
5733   EL_CUSTOM_START + 204,
5734   EL_CUSTOM_START + 205,
5735   EL_CUSTOM_START + 206,
5736   EL_CUSTOM_START + 207,
5737
5738   EL_CUSTOM_START + 208,
5739   EL_CUSTOM_START + 209,
5740   EL_CUSTOM_START + 210,
5741   EL_CUSTOM_START + 211,
5742
5743   EL_CUSTOM_START + 212,
5744   EL_CUSTOM_START + 213,
5745   EL_CUSTOM_START + 214,
5746   EL_CUSTOM_START + 215,
5747
5748   EL_CUSTOM_START + 216,
5749   EL_CUSTOM_START + 217,
5750   EL_CUSTOM_START + 218,
5751   EL_CUSTOM_START + 219,
5752
5753   EL_CUSTOM_START + 220,
5754   EL_CUSTOM_START + 221,
5755   EL_CUSTOM_START + 222,
5756   EL_CUSTOM_START + 223,
5757
5758   EL_CUSTOM_START + 224,
5759   EL_CUSTOM_START + 225,
5760   EL_CUSTOM_START + 226,
5761   EL_CUSTOM_START + 227,
5762
5763   EL_CUSTOM_START + 228,
5764   EL_CUSTOM_START + 229,
5765   EL_CUSTOM_START + 230,
5766   EL_CUSTOM_START + 231,
5767
5768   EL_CUSTOM_START + 232,
5769   EL_CUSTOM_START + 233,
5770   EL_CUSTOM_START + 234,
5771   EL_CUSTOM_START + 235,
5772
5773   EL_CUSTOM_START + 236,
5774   EL_CUSTOM_START + 237,
5775   EL_CUSTOM_START + 238,
5776   EL_CUSTOM_START + 239,
5777
5778   EL_CUSTOM_START + 240,
5779   EL_CUSTOM_START + 241,
5780   EL_CUSTOM_START + 242,
5781   EL_CUSTOM_START + 243,
5782
5783   EL_CUSTOM_START + 244,
5784   EL_CUSTOM_START + 245,
5785   EL_CUSTOM_START + 246,
5786   EL_CUSTOM_START + 247,
5787
5788   EL_CUSTOM_START + 248,
5789   EL_CUSTOM_START + 249,
5790   EL_CUSTOM_START + 250,
5791   EL_CUSTOM_START + 251,
5792
5793   EL_CUSTOM_START + 252,
5794   EL_CUSTOM_START + 253,
5795   EL_CUSTOM_START + 254,
5796   EL_CUSTOM_START + 255
5797 };
5798 static int *editor_hl_custom_ptr = editor_hl_custom;
5799 static int *editor_el_custom_ptr = editor_el_custom;
5800 static int num_editor_hl_custom = ARRAY_SIZE(editor_hl_custom);
5801 static int num_editor_el_custom = ARRAY_SIZE(editor_el_custom);
5802
5803 static int editor_hl_group[] =
5804 {
5805   EL_INTERNAL_CASCADE_GE_ACTIVE,
5806   EL_CHAR('G'),
5807   EL_CHAR('E'),
5808   EL_EMPTY,
5809 };
5810
5811 static int editor_el_group[] =
5812 {
5813   EL_GROUP_START + 0,
5814   EL_GROUP_START + 1,
5815   EL_GROUP_START + 2,
5816   EL_GROUP_START + 3,
5817
5818   EL_GROUP_START + 4,
5819   EL_GROUP_START + 5,
5820   EL_GROUP_START + 6,
5821   EL_GROUP_START + 7,
5822
5823   EL_GROUP_START + 8,
5824   EL_GROUP_START + 9,
5825   EL_GROUP_START + 10,
5826   EL_GROUP_START + 11,
5827
5828   EL_GROUP_START + 12,
5829   EL_GROUP_START + 13,
5830   EL_GROUP_START + 14,
5831   EL_GROUP_START + 15,
5832
5833   EL_GROUP_START + 16,
5834   EL_GROUP_START + 17,
5835   EL_GROUP_START + 18,
5836   EL_GROUP_START + 19,
5837
5838   EL_GROUP_START + 20,
5839   EL_GROUP_START + 21,
5840   EL_GROUP_START + 22,
5841   EL_GROUP_START + 23,
5842
5843   EL_GROUP_START + 24,
5844   EL_GROUP_START + 25,
5845   EL_GROUP_START + 26,
5846   EL_GROUP_START + 27,
5847
5848   EL_GROUP_START + 28,
5849   EL_GROUP_START + 29,
5850   EL_GROUP_START + 30,
5851   EL_GROUP_START + 31
5852 };
5853 static int *editor_hl_group_ptr = editor_hl_group;
5854 static int *editor_el_group_ptr = editor_el_group;
5855 static int num_editor_hl_group = ARRAY_SIZE(editor_hl_group);
5856 static int num_editor_el_group = ARRAY_SIZE(editor_el_group);
5857
5858 static int editor_hl_empty_space[] =
5859 {
5860   EL_INTERNAL_CASCADE_ES_ACTIVE,
5861   EL_CHAR('E'),
5862   EL_CHAR('S'),
5863   EL_EMPTY,
5864 };
5865
5866 static int editor_el_empty_space[] =
5867 {
5868   EL_EMPTY_SPACE_1,
5869   EL_EMPTY_SPACE_2,
5870   EL_EMPTY_SPACE_3,
5871   EL_EMPTY_SPACE_4,
5872
5873   EL_EMPTY_SPACE_5,
5874   EL_EMPTY_SPACE_6,
5875   EL_EMPTY_SPACE_7,
5876   EL_EMPTY_SPACE_8,
5877
5878   EL_EMPTY_SPACE_9,
5879   EL_EMPTY_SPACE_10,
5880   EL_EMPTY_SPACE_11,
5881   EL_EMPTY_SPACE_12,
5882
5883   EL_EMPTY_SPACE_13,
5884   EL_EMPTY_SPACE_14,
5885   EL_EMPTY_SPACE_15,
5886   EL_EMPTY_SPACE_16
5887 };
5888 static int *editor_hl_empty_space_ptr = editor_hl_empty_space;
5889 static int *editor_el_empty_space_ptr = editor_el_empty_space;
5890 static int num_editor_hl_empty_space = ARRAY_SIZE(editor_hl_empty_space);
5891 static int num_editor_el_empty_space = ARRAY_SIZE(editor_el_empty_space);
5892
5893 static int editor_hl_reference[] =
5894 {
5895   EL_INTERNAL_CASCADE_REF_ACTIVE,
5896   EL_CHAR('R'),
5897   EL_CHAR('E'),
5898   EL_CHAR('F')
5899 };
5900
5901 static int editor_el_reference[] =
5902 {
5903   EL_TRIGGER_PLAYER,
5904   EL_TRIGGER_ELEMENT,
5905   EL_TRIGGER_CE_VALUE,
5906   EL_TRIGGER_CE_SCORE,
5907
5908   EL_SELF,
5909   EL_ANY_ELEMENT,
5910   EL_CURRENT_CE_VALUE,
5911   EL_CURRENT_CE_SCORE,
5912
5913   EL_PREV_CE_8,
5914   EL_PREV_CE_7,
5915   EL_PREV_CE_6,
5916   EL_PREV_CE_5,
5917
5918   EL_PREV_CE_4,
5919   EL_PREV_CE_3,
5920   EL_PREV_CE_2,
5921   EL_PREV_CE_1,
5922
5923   EL_NEXT_CE_1,
5924   EL_NEXT_CE_2,
5925   EL_NEXT_CE_3,
5926   EL_NEXT_CE_4,
5927
5928   EL_NEXT_CE_5,
5929   EL_NEXT_CE_6,
5930   EL_NEXT_CE_7,
5931   EL_NEXT_CE_8,
5932 };
5933 static int *editor_hl_reference_ptr = editor_hl_reference;
5934 static int *editor_el_reference_ptr = editor_el_reference;
5935 static int num_editor_hl_reference = ARRAY_SIZE(editor_hl_reference);
5936 static int num_editor_el_reference = ARRAY_SIZE(editor_el_reference);
5937
5938 static int editor_hl_user_defined[] =
5939 {
5940   EL_INTERNAL_CASCADE_USER_ACTIVE,
5941   EL_CHAR('M'),
5942   EL_CHAR('Y'),
5943   EL_EMPTY,
5944 };
5945
5946 static int *editor_hl_user_defined_ptr = editor_hl_user_defined;
5947 static int *editor_el_user_defined_ptr = NULL;
5948 static int num_editor_hl_user_defined = ARRAY_SIZE(editor_hl_user_defined);
5949 static int num_editor_el_user_defined = 0;
5950
5951 static int editor_hl_dynamic[] =
5952 {
5953   EL_INTERNAL_CASCADE_DYNAMIC_ACTIVE,
5954   EL_CHAR('U'),
5955   EL_CHAR('S'),
5956   EL_CHAR('E'),
5957 };
5958
5959 static int *editor_hl_dynamic_ptr = editor_hl_dynamic;
5960 static int *editor_el_dynamic_ptr = NULL;
5961 static int num_editor_hl_dynamic = ARRAY_SIZE(editor_hl_dynamic);
5962 static int num_editor_el_dynamic = 0;
5963
5964 static int editor_hl_empty[] = { EL_EMPTY };
5965 static int *editor_el_empty = NULL;     // dynamically allocated
5966
5967 static int *editor_hl_empty_ptr = editor_hl_empty;
5968 static int *editor_el_empty_ptr = NULL;
5969 static int num_editor_hl_empty = 0;
5970 static int num_editor_el_empty = 0;     // dynamically determined, if needed
5971
5972 static boolean use_el_empty = FALSE;
5973
5974 static int *editor_elements = NULL;     // dynamically allocated
5975 static int num_editor_elements = 0;     // dynamically determined
5976
5977 static boolean setup_editor_cascade_never = FALSE;
5978
5979 static boolean setup_editor_el_players                  = TRUE;
5980 static boolean setup_editor_el_boulderdash              = TRUE;
5981 static boolean setup_editor_el_boulderdash_native       = TRUE;
5982 static boolean setup_editor_el_emerald_mine             = TRUE;
5983 static boolean setup_editor_el_emerald_mine_club        = TRUE;
5984 static boolean setup_editor_el_more                     = TRUE;
5985 static boolean setup_editor_el_sokoban                  = TRUE;
5986 static boolean setup_editor_el_supaplex                 = TRUE;
5987 static boolean setup_editor_el_diamond_caves            = TRUE;
5988 static boolean setup_editor_el_dx_boulderdash           = TRUE;
5989 static boolean setup_editor_el_mirror_magic             = TRUE;
5990 static boolean setup_editor_el_deflektor                = TRUE;
5991 static boolean setup_editor_el_chars                    = TRUE;
5992 static boolean setup_editor_el_steel_chars              = TRUE;
5993 static boolean setup_editor_el_custom                   = TRUE;
5994 static boolean setup_editor_el_user_defined             = TRUE;
5995 static boolean setup_editor_el_dynamic                  = TRUE;
5996
5997 static int editor_hl_unused[] = { EL_EMPTY };
5998 static int *editor_hl_unused_ptr = editor_hl_unused;
5999 static int num_editor_hl_unused = 0;
6000
6001 static struct
6002 {
6003   boolean *setup_value;
6004   boolean *setup_cascade_value;
6005
6006   int **headline_list;
6007   int *headline_list_size;
6008
6009   int **element_list;
6010   int *element_list_size;
6011
6012   boolean last_setup_value;
6013 }
6014 editor_elements_info[] =
6015 {
6016   {
6017     &setup_editor_el_players,
6018     &setup_editor_cascade_never,
6019     &editor_hl_unused_ptr,              &num_editor_hl_unused,
6020     &editor_el_players_ptr,             &num_editor_el_players
6021   },
6022   {
6023     &setup_editor_el_boulderdash,
6024     &setup.editor_cascade.el_bd,
6025     &editor_hl_boulderdash_ptr,         &num_editor_hl_boulderdash,
6026     &editor_el_boulderdash_ptr,         &num_editor_el_boulderdash
6027   },
6028   {
6029     &setup_editor_el_boulderdash_native,
6030     &setup.editor_cascade.el_bd_native,
6031     &editor_hl_boulderdash_native_ptr,  &num_editor_hl_boulderdash_native,
6032     &editor_el_boulderdash_native_ptr,  &num_editor_el_boulderdash_native
6033   },
6034   {
6035     &setup_editor_el_emerald_mine,
6036     &setup.editor_cascade.el_em,
6037     &editor_hl_emerald_mine_ptr,        &num_editor_hl_emerald_mine,
6038     &editor_el_emerald_mine_ptr,        &num_editor_el_emerald_mine
6039   },
6040   {
6041     &setup_editor_el_emerald_mine_club,
6042     &setup.editor_cascade.el_emc,
6043     &editor_hl_emerald_mine_club_ptr,   &num_editor_hl_emerald_mine_club,
6044     &editor_el_emerald_mine_club_ptr,   &num_editor_el_emerald_mine_club
6045   },
6046   {
6047     &setup_editor_el_more,
6048     &setup.editor_cascade.el_rnd,
6049     &editor_hl_rnd_ptr,                 &num_editor_hl_rnd,
6050     &editor_el_rnd_ptr,                 &num_editor_el_rnd
6051   },
6052   {
6053     &setup_editor_el_sokoban,
6054     &setup.editor_cascade.el_sb,
6055     &editor_hl_sokoban_ptr,             &num_editor_hl_sokoban,
6056     &editor_el_sokoban_ptr,             &num_editor_el_sokoban
6057   },
6058   {
6059     &setup_editor_el_supaplex,
6060     &setup.editor_cascade.el_sp,
6061     &editor_hl_supaplex_ptr,            &num_editor_hl_supaplex,
6062     &editor_el_supaplex_ptr,            &num_editor_el_supaplex
6063   },
6064   {
6065     &setup_editor_el_diamond_caves,
6066     &setup.editor_cascade.el_dc,
6067     &editor_hl_diamond_caves_ptr,       &num_editor_hl_diamond_caves,
6068     &editor_el_diamond_caves_ptr,       &num_editor_el_diamond_caves
6069   },
6070   {
6071     &setup_editor_el_dx_boulderdash,
6072     &setup.editor_cascade.el_dx,
6073     &editor_hl_dx_boulderdash_ptr,      &num_editor_hl_dx_boulderdash,
6074     &editor_el_dx_boulderdash_ptr,      &num_editor_el_dx_boulderdash
6075   },
6076   {
6077     &setup_editor_el_mirror_magic,
6078     &setup.editor_cascade.el_mm,
6079     &editor_hl_mirror_magic_ptr,        &num_editor_hl_mirror_magic,
6080     &editor_el_mirror_magic_ptr,        &num_editor_el_mirror_magic
6081   },
6082   {
6083     &setup_editor_el_deflektor,
6084     &setup.editor_cascade.el_df,
6085     &editor_hl_deflektor_ptr,           &num_editor_hl_deflektor,
6086     &editor_el_deflektor_ptr,           &num_editor_el_deflektor
6087   },
6088   {
6089     &setup_editor_el_chars,
6090     &setup.editor_cascade.el_chars,
6091     &editor_hl_chars_ptr,               &num_editor_hl_chars,
6092     &editor_el_chars_ptr,               &num_editor_el_chars
6093   },
6094   {
6095     &setup_editor_el_steel_chars,
6096     &setup.editor_cascade.el_steel_chars,
6097     &editor_hl_steel_chars_ptr,         &num_editor_hl_steel_chars,
6098     &editor_el_steel_chars_ptr,         &num_editor_el_steel_chars
6099   },
6100   {
6101     &setup_editor_el_custom,
6102     &setup.editor_cascade.el_ce,
6103     &editor_hl_custom_ptr,              &num_editor_hl_custom,
6104     &editor_el_custom_ptr,              &num_editor_el_custom
6105   },
6106   {
6107     &setup_editor_el_custom,
6108     &setup.editor_cascade.el_ge,
6109     &editor_hl_group_ptr,               &num_editor_hl_group,
6110     &editor_el_group_ptr,               &num_editor_el_group
6111   },
6112   {
6113     &setup_editor_el_custom,
6114     &setup.editor_cascade.el_es,
6115     &editor_hl_empty_space_ptr,         &num_editor_hl_empty_space,
6116     &editor_el_empty_space_ptr,         &num_editor_el_empty_space
6117   },
6118   {
6119     &setup_editor_el_custom,
6120     &setup.editor_cascade.el_ref,
6121     &editor_hl_reference_ptr,           &num_editor_hl_reference,
6122     &editor_el_reference_ptr,           &num_editor_el_reference
6123   },
6124   {
6125     &setup_editor_el_user_defined,
6126     &setup.editor_cascade.el_user,
6127     &editor_hl_user_defined_ptr,        &num_editor_hl_user_defined,
6128     &editor_el_user_defined_ptr,        &num_editor_el_user_defined
6129   },
6130   {
6131     &setup_editor_el_dynamic,
6132     &setup.editor_cascade.el_dynamic,
6133     &editor_hl_dynamic_ptr,             &num_editor_hl_dynamic,
6134     &editor_el_dynamic_ptr,             &num_editor_el_dynamic,
6135   },
6136   {
6137     &use_el_empty,
6138     &use_el_empty,
6139     &editor_hl_empty_ptr,               &num_editor_hl_empty,
6140     &editor_el_empty_ptr,               &num_editor_el_empty,
6141   },
6142   {
6143     NULL,
6144     NULL,
6145     NULL,                               NULL,
6146     NULL,                               NULL
6147   }
6148 };
6149
6150 static struct XY xy_directions[] =
6151 {
6152   { -1,  0 },
6153   { +1,  0 },
6154   {  0, -1 },
6155   {  0, +1 }
6156 };
6157
6158
6159 // ----------------------------------------------------------------------------
6160 // functions
6161 // ----------------------------------------------------------------------------
6162
6163 static int getMaxInfoTextLength(void)
6164 {
6165   return (SXSIZE / getFontWidth(INFOTEXT_FONT));
6166 }
6167
6168 static int getTextWidthForGadget(char *text)
6169 {
6170   if (text == NULL)
6171     return 0;
6172
6173   return (getTextWidth(text, FONT_TEXT_1) + ED_GADGET_TEXT_DISTANCE);
6174 }
6175
6176 static int getTextWidthForDrawingArea(char *text)
6177 {
6178   if (text == NULL)
6179     return 0;
6180
6181   return (getTextWidth(text, FONT_TEXT_1) + ED_DRAWINGAREA_TEXT_DISTANCE);
6182 }
6183
6184 static int getRightGadgetBorder(struct GadgetInfo *gi, char *text)
6185 {
6186   return (gi->x + gi->width + getTextWidthForGadget(text));
6187 }
6188
6189 static char *getElementInfoText(int element)
6190 {
6191   char *info_text = NULL;
6192
6193   if (element < MAX_NUM_ELEMENTS)
6194   {
6195     if (strlen(element_info[element].description) > 0)
6196       info_text = element_info[element].description;
6197     else if (element_info[element].custom_description != NULL)
6198       info_text = element_info[element].custom_description;
6199     else if (element_info[element].editor_description != NULL)
6200       info_text = element_info[element].editor_description;
6201   }
6202
6203   if (info_text == NULL)
6204     info_text = INFOTEXT_UNKNOWN_ELEMENT;
6205
6206   return info_text;
6207 }
6208
6209 static char *getElementDescriptionFilenameExt(char *basename)
6210 {
6211   char *elements_subdir = ELEMENTS_DIRECTORY;
6212   static char *elements_subdir2 = NULL;
6213   static char *filename = NULL;
6214
6215   if (elements_subdir2 == NULL)
6216     elements_subdir2 = getPath2(DOCS_DIRECTORY, elements_subdir);
6217
6218   checked_free(filename);
6219
6220   // 1st try: look for element description in current level set directory
6221   filename = getPath3(getCurrentLevelDir(), elements_subdir2, basename);
6222   if (fileExists(filename))
6223     return filename;
6224
6225   free(filename);
6226
6227   // 2nd try: look for element description in the game's base directory
6228   filename = getPath3(options.docs_directory, elements_subdir, basename);
6229   if (fileExists(filename))
6230     return filename;
6231
6232   return NULL;
6233 }
6234
6235 static char *getElementDescriptionFilename(int element)
6236 {
6237   char basename[MAX_FILENAME_LEN];
6238   char *filename;
6239
6240   // 1st try: look for element description file for exactly this element
6241   sprintf(basename, "%s.txt", element_info[element].token_name);
6242   filename = getElementDescriptionFilenameExt(basename);
6243   if (filename != NULL)
6244     return filename;
6245
6246   // 2nd try: look for element description file for this element's class
6247   sprintf(basename, "%s.txt", element_info[element].class_name);
6248   filename = getElementDescriptionFilenameExt(basename);
6249   if (filename != NULL)
6250     return filename;
6251
6252   // 3rd try: look for generic fallback text file for any element
6253   filename = getElementDescriptionFilenameExt(FALLBACK_TEXT_FILENAME);
6254   if (filename != NULL)
6255     return filename;
6256
6257   return NULL;
6258 }
6259
6260 static boolean suppressBorderElement(void)
6261 {
6262   return (level.game_engine_type == GAME_ENGINE_TYPE_MM &&
6263           lev_fieldx <= MAX_ED_FIELDX &&
6264           lev_fieldy <= MAX_ED_FIELDY);
6265 }
6266
6267 static void InitDynamicEditorElementList(int **elements, int *num_elements)
6268 {
6269   boolean element_found[NUM_FILE_ELEMENTS];
6270   int i, x, y;
6271
6272   // initialize list of used elements to "not used"
6273   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6274     element_found[i] = FALSE;
6275
6276   // find all elements used in current level
6277   for (y = 0; y < lev_fieldy; y++)
6278   {
6279     for (x = 0; x < lev_fieldx; x++)
6280     {
6281       if (Tile[x][y] >= NUM_FILE_ELEMENTS)      // should never happen
6282         continue;
6283
6284       if (IS_MM_WALL(Tile[x][y]))
6285         element_found[map_mm_wall_element(Tile[x][y])] = TRUE;
6286       else
6287         element_found[Tile[x][y]] = TRUE;
6288     }
6289   }
6290
6291   *num_elements = 0;
6292
6293   // count number of elements used in current level
6294   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6295     if (element_found[i])
6296       (*num_elements)++;
6297
6298   // add space for up to 3 more elements for padding that may be needed
6299   *num_elements += 3;
6300
6301   // free memory for old list of elements, if needed
6302   checked_free(*elements);
6303
6304   // allocate memory for new list of elements
6305   *elements = checked_malloc(*num_elements * sizeof(int));
6306
6307   *num_elements = 0;
6308
6309   // add all elements used in current level (non-custom/group/empty elements)
6310   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6311     if (element_found[i] && !(IS_CUSTOM_ELEMENT(i) ||
6312                               IS_GROUP_ELEMENT(i) ||
6313                               IS_EMPTY_ELEMENT(i)))
6314       (*elements)[(*num_elements)++] = i;
6315
6316   // add all elements used in current level (custom/group/empty elements)
6317   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6318     if (element_found[i] && (IS_CUSTOM_ELEMENT(i) ||
6319                              IS_GROUP_ELEMENT(i) ||
6320                              IS_EMPTY_ELEMENT(i)))
6321       (*elements)[(*num_elements)++] = i;
6322
6323   while (*num_elements % 4)     // pad with empty elements, if needed
6324     (*elements)[(*num_elements)++] = EL_EMPTY;
6325 }
6326
6327 static void ReinitializeElementList_EnableSections(void)
6328 {
6329   // default: enable all element sections
6330
6331   setup_editor_el_players               = TRUE;
6332   setup_editor_el_boulderdash           = TRUE;
6333   setup_editor_el_boulderdash_native    = TRUE;
6334   setup_editor_el_emerald_mine          = TRUE;
6335   setup_editor_el_emerald_mine_club     = TRUE;
6336   setup_editor_el_more                  = TRUE;
6337   setup_editor_el_sokoban               = TRUE;
6338   setup_editor_el_supaplex              = TRUE;
6339   setup_editor_el_diamond_caves         = TRUE;
6340   setup_editor_el_dx_boulderdash        = TRUE;
6341   setup_editor_el_mirror_magic          = TRUE;
6342   setup_editor_el_deflektor             = TRUE;
6343   setup_editor_el_chars                 = TRUE;
6344   setup_editor_el_steel_chars           = TRUE;
6345
6346   setup_editor_el_custom                = TRUE;
6347   setup_editor_el_user_defined          = TRUE;
6348   setup_editor_el_dynamic               = TRUE;
6349
6350   // now disable all element sections not to be displayed
6351
6352   if (!setup.editor.el_classic)
6353   {
6354     setup_editor_el_players             = FALSE;
6355     setup_editor_el_boulderdash         = FALSE;
6356     setup_editor_el_boulderdash_native  = FALSE;
6357     setup_editor_el_emerald_mine        = FALSE;
6358     setup_editor_el_emerald_mine_club   = FALSE;
6359     setup_editor_el_more                = FALSE;
6360     setup_editor_el_sokoban             = FALSE;
6361     setup_editor_el_supaplex            = FALSE;
6362     setup_editor_el_diamond_caves       = FALSE;
6363     setup_editor_el_dx_boulderdash      = FALSE;
6364     setup_editor_el_mirror_magic        = FALSE;
6365     setup_editor_el_deflektor           = FALSE;
6366     setup_editor_el_chars               = FALSE;
6367     setup_editor_el_steel_chars         = FALSE;
6368   }
6369
6370   if (!setup.editor.el_custom)
6371   {
6372     setup_editor_el_custom              = FALSE;
6373   }
6374
6375   if (!setup.editor.el_user_defined)
6376   {
6377     setup_editor_el_user_defined        = FALSE;
6378   }
6379
6380   if (!setup.editor.el_dynamic)
6381   {
6382     setup_editor_el_dynamic             = FALSE;
6383   }
6384
6385   if (level.game_engine_type == GAME_ENGINE_TYPE_RND)
6386   {
6387     setup_editor_el_boulderdash_native  = FALSE;
6388     setup_editor_el_mirror_magic        = FALSE;
6389     setup_editor_el_deflektor           = FALSE;
6390   }
6391   else if (level.game_engine_type == GAME_ENGINE_TYPE_BD)
6392   {
6393     setup_editor_el_players             = FALSE;
6394     setup_editor_el_boulderdash         = FALSE;
6395     setup_editor_el_emerald_mine        = FALSE;
6396     setup_editor_el_emerald_mine_club   = FALSE;
6397     setup_editor_el_more                = FALSE;
6398     setup_editor_el_sokoban             = FALSE;
6399     setup_editor_el_supaplex            = FALSE;
6400     setup_editor_el_diamond_caves       = FALSE;
6401     setup_editor_el_dx_boulderdash      = FALSE;
6402     setup_editor_el_mirror_magic        = FALSE;
6403     setup_editor_el_deflektor           = FALSE;
6404     setup_editor_el_chars               = FALSE;
6405     setup_editor_el_steel_chars         = FALSE;
6406
6407     setup_editor_el_custom              = FALSE;
6408   }
6409   else if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
6410   {
6411     setup_editor_el_boulderdash         = FALSE;
6412     setup_editor_el_boulderdash_native  = FALSE;
6413     setup_editor_el_more                = FALSE;
6414     setup_editor_el_sokoban             = FALSE;
6415     setup_editor_el_supaplex            = FALSE;
6416     setup_editor_el_diamond_caves       = FALSE;
6417     setup_editor_el_dx_boulderdash      = FALSE;
6418     setup_editor_el_mirror_magic        = FALSE;
6419     setup_editor_el_deflektor           = FALSE;
6420     setup_editor_el_steel_chars         = FALSE;
6421
6422     setup_editor_el_custom              = FALSE;
6423   }
6424   else if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
6425   {
6426     setup_editor_el_players             = FALSE;
6427     setup_editor_el_boulderdash         = FALSE;
6428     setup_editor_el_boulderdash_native  = FALSE;
6429     setup_editor_el_emerald_mine        = FALSE;
6430     setup_editor_el_emerald_mine_club   = FALSE;
6431     setup_editor_el_more                = FALSE;
6432     setup_editor_el_sokoban             = FALSE;
6433     setup_editor_el_diamond_caves       = FALSE;
6434     setup_editor_el_dx_boulderdash      = FALSE;
6435     setup_editor_el_mirror_magic        = FALSE;
6436     setup_editor_el_deflektor           = FALSE;
6437     setup_editor_el_chars               = FALSE;
6438     setup_editor_el_steel_chars         = FALSE;
6439
6440     setup_editor_el_custom              = FALSE;
6441   }
6442   else if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
6443   {
6444     setup_editor_el_players             = FALSE;
6445     setup_editor_el_boulderdash         = FALSE;
6446     setup_editor_el_boulderdash_native  = FALSE;
6447     setup_editor_el_emerald_mine        = FALSE;
6448     setup_editor_el_emerald_mine_club   = FALSE;
6449     setup_editor_el_more                = FALSE;
6450     setup_editor_el_sokoban             = FALSE;
6451     setup_editor_el_supaplex            = FALSE;
6452     setup_editor_el_diamond_caves       = FALSE;
6453     setup_editor_el_dx_boulderdash      = FALSE;
6454     setup_editor_el_steel_chars         = FALSE;
6455
6456     setup_editor_el_custom              = FALSE;
6457   }
6458 }
6459
6460 static void ReinitializeElementList(void)
6461 {
6462   static boolean initialization_needed = TRUE;
6463   int pos = 0;
6464   int i, j;
6465
6466   ReinitializeElementList_EnableSections();
6467
6468   if (initialization_needed)
6469   {
6470     LoadSetup_EditorCascade();          // load last editor cascade state
6471
6472     // initialize editor cascade element from saved cascade state
6473     for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6474     {
6475       int *cascade_element = &(*editor_elements_info[i].headline_list)[0];
6476       boolean cascade_value = *editor_elements_info[i].setup_cascade_value;
6477
6478       if (IS_EDITOR_CASCADE(*cascade_element))
6479         *cascade_element =
6480           (cascade_value ? EL_CASCADE_ACTIVE(*cascade_element) :
6481            EL_CASCADE_INACTIVE(*cascade_element));
6482     }
6483
6484     initialization_needed = FALSE;
6485   }
6486
6487   checked_free(editor_elements);
6488
6489   // reload optional user defined element list for each invocation of editor
6490   LoadUserDefinedEditorElementList(&editor_el_user_defined_ptr,
6491                                    &num_editor_el_user_defined);
6492
6493   // initialize dynamic level element list for each invocation of editor
6494   InitDynamicEditorElementList(&editor_el_dynamic_ptr,
6495                                &num_editor_el_dynamic);
6496
6497   // initialize list of empty elements (used for padding, if needed)
6498   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
6499     editor_el_empty[i] = EL_EMPTY;
6500
6501   // do some sanity checks for each element from element list
6502   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6503   {
6504     for (j = 0; j < *editor_elements_info[i].element_list_size; j++)
6505     {
6506       int element = (*editor_elements_info[i].element_list)[j];
6507
6508       if (element >= NUM_FILE_ELEMENTS)
6509         Warn("editor element %d is runtime element", element);
6510
6511       if (strEqual(getElementInfoText(element), INFOTEXT_UNKNOWN_ELEMENT))
6512         Warn("no element description text for element %d", element);
6513     }
6514   }
6515
6516   num_editor_elements = 0;
6517   use_el_empty = FALSE;
6518
6519   // determine size of element list
6520   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6521   {
6522     if (*editor_elements_info[i].setup_value)
6523     {
6524       boolean found_inactive_cascade = FALSE;
6525
6526       if (setup.editor.el_headlines)
6527       {
6528         // required for correct padding of palette headline buttons
6529         if (*editor_elements_info[i].headline_list_size > 0)
6530           num_editor_elements += ED_ELEMENTLIST_COLS;
6531
6532         for (j = 0; j < *editor_elements_info[i].headline_list_size; j++)
6533         {
6534           int element = (*editor_elements_info[i].headline_list)[j];
6535
6536           if (IS_EDITOR_CASCADE_INACTIVE(element))
6537             found_inactive_cascade = TRUE;
6538         }
6539       }
6540
6541       if (found_inactive_cascade)
6542         continue;
6543
6544       // required for correct padding of palette element buttons
6545       int element_list_size = *editor_elements_info[i].element_list_size;
6546       int element_rows =
6547         (element_list_size + ED_ELEMENTLIST_COLS - 1) / ED_ELEMENTLIST_COLS;
6548       int element_buttons = ED_ELEMENTLIST_COLS * element_rows;
6549
6550       num_editor_elements += element_buttons;
6551     }
6552   }
6553
6554   if (num_editor_elements < ED_NUM_ELEMENTLIST_BUTTONS)
6555   {
6556     // offer at least as many elements as element buttons exist
6557     use_el_empty = TRUE;
6558     num_editor_el_empty = ED_NUM_ELEMENTLIST_BUTTONS - num_editor_elements;
6559
6560     num_editor_elements += num_editor_el_empty;
6561   }
6562   else
6563   {
6564     num_editor_el_empty = 0;
6565   }
6566
6567   editor_elements = checked_malloc(num_editor_elements * sizeof(int));
6568
6569   // fill element list
6570   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6571   {
6572     boolean found_inactive_cascade = FALSE;
6573
6574     if (*editor_elements_info[i].setup_value)
6575     {
6576       if (setup.editor.el_headlines)
6577       {
6578         // required for correct padding of palette headline buttons
6579         int headline_size = (*editor_elements_info[i].headline_list_size > 0 ?
6580                              ED_ELEMENTLIST_COLS : 0);
6581
6582         for (j = 0; j < headline_size; j++)
6583         {
6584           // use empty elements for padding of palette headline buttons
6585           int element = (j < *editor_elements_info[i].headline_list_size ?
6586                          (*editor_elements_info[i].headline_list)[j] :
6587                          editor_el_empty[0]);
6588
6589           editor_elements[pos++] = element;
6590
6591           if (IS_EDITOR_CASCADE_INACTIVE(element))
6592             found_inactive_cascade = TRUE;
6593         }
6594       }
6595
6596       if (found_inactive_cascade)
6597         continue;
6598
6599       // required for correct padding of palette element buttons
6600       int element_list_size = *editor_elements_info[i].element_list_size;
6601       int element_rows =
6602         (element_list_size + ED_ELEMENTLIST_COLS - 1) / ED_ELEMENTLIST_COLS;
6603       int element_buttons = ED_ELEMENTLIST_COLS * element_rows;
6604
6605       // copy all elements from element list
6606       for (j = 0; j < element_list_size; j++)
6607         editor_elements[pos++] = (*editor_elements_info[i].element_list)[j];
6608
6609       // use empty elements for padding of palette element buttons
6610       for (j = 0; j < element_buttons - element_list_size; j++)
6611         editor_elements[pos++] = editor_el_empty[0];
6612     }
6613   }
6614
6615   // (this function is also called before editor gadgets are initialized!)
6616   AdjustElementListScrollbar();
6617 }
6618
6619 void PrintEditorElementList(void)
6620 {
6621   boolean *stop = &setup_editor_el_user_defined;
6622   int i, j;
6623
6624   for (i = 0; editor_elements_info[i].setup_value != stop; i++)
6625   {
6626     int cascade_element = (*editor_elements_info[i].headline_list)[0];
6627
6628     if (IS_EDITOR_CASCADE(cascade_element))
6629     {
6630       int cascade_element_show = EL_CASCADE_INACTIVE(cascade_element);
6631       char *headline = element_info[cascade_element_show].editor_description;
6632
6633       PrintLineWithPrefix("# ", "-", 77);
6634       Print("# %s\n", headline);
6635       PrintLineWithPrefix("# ", "-", 77);
6636     }
6637
6638     for (j = 0; j < *editor_elements_info[i].headline_list_size; j++)
6639     {
6640       int element = (*editor_elements_info[i].headline_list)[j];
6641
6642       if (IS_EDITOR_CASCADE(element))
6643         element = EL_CHAR_MINUS;
6644
6645       Print("# %s\n", element_info[element].token_name);
6646     }
6647
6648     if (j > 0)
6649       Print("#\n");
6650
6651     for (j = 0; j < *editor_elements_info[i].element_list_size; j++)
6652     {
6653       int element = (*editor_elements_info[i].element_list)[j];
6654
6655       Print("# %s\n", element_info[element].token_name);
6656     }
6657
6658     if (j > 0)
6659       Print("#\n");
6660   }
6661 }
6662
6663 static void ReinitializeElementListButtons(void)
6664 {
6665   static boolean last_setup_value_headlines = FALSE;
6666   static boolean initialization_needed = TRUE;
6667   int i;
6668
6669   if (!initialization_needed)   // check if editor element setup has changed
6670   {
6671     if (last_setup_value_headlines != setup.editor.el_headlines)
6672       initialization_needed = TRUE;
6673
6674     for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6675       if (editor_elements_info[i].last_setup_value !=
6676           *editor_elements_info[i].setup_value)
6677         initialization_needed = TRUE;
6678   }
6679
6680   if (!initialization_needed)
6681     return;
6682
6683   FreeLevelEditorGadgets();
6684   CreateLevelEditorGadgets();
6685
6686   // store current setup values for next invocation of this function
6687   last_setup_value_headlines = setup.editor.el_headlines;
6688   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6689     editor_elements_info[i].last_setup_value =
6690       *editor_elements_info[i].setup_value;
6691
6692   initialization_needed = FALSE;
6693 }
6694
6695 static void DrawElementBorder(int dest_x, int dest_y, int width, int height,
6696                               boolean input)
6697 {
6698   int border_graphic =
6699     (input ? IMG_EDITOR_ELEMENT_BORDER_INPUT : IMG_EDITOR_ELEMENT_BORDER);
6700   struct GraphicInfo *g = &graphic_info[border_graphic];
6701   Bitmap *src_bitmap = g->bitmap;
6702   int src_x = g->src_x;
6703   int src_y = g->src_y;
6704   int border_size = g->border_size;
6705   int border_xpos = g->width  - border_size;
6706   int border_ypos = g->height - border_size;
6707   int tilesize = ED_DRAWINGAREA_TILE_SIZE;
6708   int i;
6709
6710   BlitBitmap(src_bitmap, drawto, src_x, src_y,
6711              border_size, border_size,
6712              dest_x - border_size, dest_y - border_size);
6713   BlitBitmap(src_bitmap, drawto, src_x + border_xpos, src_y,
6714              border_size, border_size,
6715              dest_x + width, dest_y - border_size);
6716   BlitBitmap(src_bitmap, drawto, src_x, src_y + border_ypos,
6717              border_size, border_size,
6718              dest_x - border_size, dest_y + height);
6719   BlitBitmap(src_bitmap, drawto, src_x + border_xpos, src_y + border_ypos,
6720              border_size, border_size,
6721              dest_x + width, dest_y + height);
6722
6723   for (i = 0; i < width / tilesize; i++)
6724   {
6725     BlitBitmap(src_bitmap, drawto, src_x + border_size, src_y,
6726                tilesize, border_size,
6727                dest_x + i * tilesize, dest_y - border_size);
6728     BlitBitmap(src_bitmap, drawto, src_x + border_size, src_y + border_ypos,
6729                tilesize, border_size,
6730                dest_x + i * tilesize, dest_y + height);
6731   }
6732
6733   for (i = 0; i < height / tilesize; i++)
6734   {
6735     BlitBitmap(src_bitmap, drawto, src_x, src_y + border_size,
6736                border_size, tilesize,
6737                dest_x - border_size, dest_y + i * tilesize);
6738     BlitBitmap(src_bitmap, drawto, src_x + border_xpos, src_y + border_size,
6739                border_size, tilesize,
6740                dest_x + width, dest_y + i * tilesize);
6741   }
6742
6743   ClearRectangle(drawto, dest_x - 1, dest_y - 1, width + 2, height + 2);
6744 }
6745
6746 static void DrawEditorLevelBorderLine(int x, int y, int xsize, int ysize)
6747 {
6748   int xsize_tile = MAX(ed_tilesize, xsize);
6749   int ysize_tile = MAX(ed_tilesize, ysize);
6750   int xsize_full = xsize + 1;
6751   int ysize_full = ysize + 1;
6752   int xsize_thin = (xsize < ed_tilesize ? 1 : xsize);
6753   int ysize_thin = (ysize < ed_tilesize ? 1 : ysize);
6754   Pixel line_color = getTabulatorBarColor();
6755
6756   if (line_color == BLACK_PIXEL)                // black => transparent
6757     return;
6758
6759   FillRectangle(drawto, SX + x, SY + y, xsize_tile, ysize_tile, BLACK_PIXEL);
6760   FillRectangle(drawto, SX + x, SY + y, xsize_full, ysize_full, line_color);
6761   FillRectangle(drawto, SX + x, SY + y, xsize_thin, ysize_thin, BLACK_PIXEL);
6762 }
6763
6764 static void DrawEditorLevelBorderLinesIfNeeded(void)
6765 {
6766   int xsize = lev_fieldx * ed_tilesize;
6767   int ysize = lev_fieldy * ed_tilesize;
6768   int line_size = getTabulatorBarHeight();
6769
6770   if (!suppressBorderElement())
6771     return;
6772
6773   // draw little border line around editable level playfield
6774
6775   if (xsize < SXSIZE)
6776     DrawEditorLevelBorderLine(xsize, 0, line_size, ysize);
6777
6778   if (ysize < SYSIZE)
6779     DrawEditorLevelBorderLine(0, ysize, xsize, line_size);
6780
6781   if (xsize < SXSIZE && ysize < SYSIZE)
6782     DrawEditorLevelBorderLine(xsize, ysize, line_size, line_size);
6783 }
6784
6785 static void DrawEditorElement(int x, int y, int element)
6786 {
6787   DrawSizedElement(x, y, element, ed_tilesize);
6788 }
6789
6790 static void DrawEditorElementThruMask(int x, int y, int element)
6791 {
6792   DrawSizedElementThruMask(x, y, element, ed_tilesize);
6793 }
6794
6795 static void DrawEditorElementOrWall(int x, int y, int scroll_x, int scroll_y)
6796 {
6797   DrawSizedElementOrWall(x, y, scroll_x, scroll_y, ed_tilesize);
6798 }
6799
6800 static void DrawEditorLevel(int size_x, int size_y, int scroll_x, int scroll_y)
6801 {
6802   DrawSizedLevel(size_x, size_y, scroll_x, scroll_y, ed_tilesize);
6803   DrawEditorLevelBorderLinesIfNeeded();
6804 }
6805
6806 static void DrawDrawingArea(int id)
6807 {
6808   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
6809   int x, y;
6810
6811   int *value = drawingarea_info[id].value;
6812   int area_xsize = drawingarea_info[id].area_xsize;
6813   int area_ysize = drawingarea_info[id].area_ysize;
6814   int tilesize = ED_DRAWINGAREA_TILE_SIZE;
6815
6816   for (x = 0; x < area_xsize; x++)
6817     for (y = 0; y < area_ysize; y++)
6818       DrawSizedGraphicExt(drawto,
6819                           gi->x + x * tilesize,
6820                           gi->y + y * tilesize,
6821                           el2edimg(value[x * area_ysize + y]), 0, tilesize);
6822 }
6823
6824 static void ScrollEditorLevel(int from_x, int from_y, int scroll)
6825 {
6826   int x, y;
6827   int dx = (scroll == ED_SCROLL_LEFT ? -1 : scroll == ED_SCROLL_RIGHT ? 1 : 0);
6828   int dy = (scroll == ED_SCROLL_UP   ? -1 : scroll == ED_SCROLL_DOWN  ? 1 : 0);
6829
6830   BlitBitmap(drawto, drawto,
6831              SX + (dx == -1 ? ed_tilesize : 0),
6832              SY + (dy == -1 ? ed_tilesize : 0),
6833              (ed_fieldx * ed_tilesize) - (dx != 0 ? ed_tilesize : 0),
6834              (ed_fieldy * ed_tilesize) - (dy != 0 ? ed_tilesize : 0),
6835              SX + (dx == +1 ? ed_tilesize : 0),
6836              SY + (dy == +1 ? ed_tilesize : 0));
6837
6838   if (dx)
6839   {
6840     x = (dx == 1 ? 0 : ed_fieldx - 1);
6841     for (y = 0; y < ed_fieldy; y++)
6842       DrawEditorElementOrWall(x, y, from_x, from_y);
6843   }
6844   else if (dy)
6845   {
6846     y = (dy == 1 ? 0 : ed_fieldy - 1);
6847     for (x = 0; x < ed_fieldx; x++)
6848       DrawEditorElementOrWall(x, y, from_x, from_y);
6849   }
6850
6851   redraw_mask |= REDRAW_FIELD;
6852   BackToFront();
6853 }
6854
6855 static void getEditorGraphicSource(int element, int tile_size, Bitmap **bitmap,
6856                                    int *x, int *y)
6857 {
6858   getSizedGraphicSource(el2edimg(element), 0, tile_size, bitmap, x, y);
6859 }
6860
6861 static void CreateControlButtons(void)
6862 {
6863   struct GadgetInfo *gi;
6864   int i;
6865
6866   // create toolbox buttons
6867   for (i = 0; i < ED_NUM_CTRL_BUTTONS; i++)
6868   {
6869     int type_id = controlbutton_info[i].gadget_id;      // same as gadget ID here
6870     int id = controlbutton_info[i].gadget_id;
6871     int type = controlbutton_info[i].gadget_type;
6872     int graphic = controlbutton_info[i].graphic;
6873     struct XYTileSize *pos = controlbutton_info[i].pos;
6874     struct GraphicInfo *gd = &graphic_info[graphic];
6875     Bitmap *deco_bitmap = NULL;
6876     int deco_x = 0, deco_y = 0, deco_xpos = 0, deco_ypos = 0;
6877     int tile_size = 0, deco_shift = 0;
6878     boolean deco_masked = FALSE;
6879     int gd_x1 = gd->src_x;
6880     int gd_y1 = gd->src_y;
6881     int gd_x2 = gd->src_x + gd->pressed_xoffset;
6882     int gd_y2 = gd->src_y + gd->pressed_yoffset;
6883     int gd_x1a = gd->src_x + gd->active_xoffset;
6884     int gd_y1a = gd->src_y + gd->active_yoffset;
6885     int gd_x2a = gd->src_x + gd->active_xoffset + gd->pressed_xoffset;
6886     int gd_y2a = gd->src_y + gd->active_yoffset + gd->pressed_yoffset;
6887     int x = pos->x;
6888     int y = pos->y;
6889     unsigned int event_mask;
6890     int radio_button_nr = RADIO_NR_NONE;
6891     boolean checked = FALSE;
6892
6893     if (type_id != i)
6894       Fail("'controlbutton_info' structure corrupted at index %d -- please fix", i);
6895
6896     if (type == GD_TYPE_RADIO_BUTTON)
6897     {
6898       event_mask = GD_EVENT_PRESSED;
6899       radio_button_nr = RADIO_NR_DRAWING_TOOLBOX;
6900
6901       if (id == drawing_function)
6902         checked = TRUE;
6903     }
6904     else
6905     {
6906       if (id == GADGET_ID_WRAP_LEFT ||
6907           id == GADGET_ID_WRAP_RIGHT ||
6908           id == GADGET_ID_WRAP_UP ||
6909           id == GADGET_ID_WRAP_DOWN)
6910         event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
6911       else
6912         event_mask = GD_EVENT_RELEASED;
6913     }
6914
6915     if (id == GADGET_ID_PROPERTIES ||
6916         id == GADGET_ID_PALETTE)
6917     {
6918       x += DX;
6919       y += DY;
6920     }
6921     else if (id == GADGET_ID_ELEMENT_LEFT ||
6922              id == GADGET_ID_ELEMENT_MIDDLE ||
6923              id == GADGET_ID_ELEMENT_RIGHT)
6924     {
6925       x += DX;
6926       y += DY;
6927
6928       int element = (id == GADGET_ID_ELEMENT_LEFT   ? new_element1 :
6929                      id == GADGET_ID_ELEMENT_MIDDLE ? new_element2 :
6930                      id == GADGET_ID_ELEMENT_RIGHT  ? new_element3 : EL_EMPTY);
6931
6932       tile_size = BUTTON_TILE_SIZE(id == GADGET_ID_ELEMENT_LEFT ?
6933                                    editor.button.element_left.tile_size :
6934                                    id == GADGET_ID_ELEMENT_MIDDLE ?
6935                                    editor.button.element_middle.tile_size :
6936                                    id == GADGET_ID_ELEMENT_RIGHT ?
6937                                    editor.button.element_right.tile_size : 0);
6938
6939       // make sure that decoration does not overlap gadget border
6940       tile_size = MIN(tile_size, MIN(gd->width, gd->height));
6941
6942       getEditorGraphicSource(element, tile_size, &deco_bitmap, &deco_x,&deco_y);
6943
6944       deco_xpos = (gd->width  - tile_size) / 2;
6945       deco_ypos = (gd->height - tile_size) / 2;
6946       deco_shift = 1;
6947       deco_masked = gd->draw_masked;
6948     }
6949     else
6950     {
6951       x += EX;
6952       y += EY;
6953     }
6954
6955     gi = CreateGadget(GDI_CUSTOM_ID, id,
6956                       GDI_CUSTOM_TYPE_ID, type_id,
6957                       GDI_IMAGE_ID, graphic,
6958                       GDI_INFO_TEXT, controlbutton_info[i].infotext,
6959                       GDI_X, x,
6960                       GDI_Y, y,
6961                       GDI_WIDTH, gd->width,
6962                       GDI_HEIGHT, gd->height,
6963                       GDI_TYPE, type,
6964                       GDI_STATE, GD_BUTTON_UNPRESSED,
6965                       GDI_RADIO_NR, radio_button_nr,
6966                       GDI_CHECKED, checked,
6967                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
6968                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
6969                       GDI_ALT_DESIGN_UNPRESSED, gd->bitmap, gd_x1a, gd_y1a,
6970                       GDI_ALT_DESIGN_PRESSED, gd->bitmap, gd_x2a, gd_y2a,
6971                       GDI_DECORATION_DESIGN, deco_bitmap, deco_x, deco_y,
6972                       GDI_DECORATION_POSITION, deco_xpos, deco_ypos,
6973                       GDI_DECORATION_SIZE, tile_size, tile_size,
6974                       GDI_DECORATION_SHIFTING, deco_shift, deco_shift,
6975                       GDI_DECORATION_MASKED, deco_masked,
6976                       GDI_EVENT_MASK, event_mask,
6977                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
6978                       GDI_CALLBACK_ACTION, HandleControlButtons,
6979                       GDI_END);
6980
6981     if (gi == NULL)
6982       Fail("cannot create gadget");
6983
6984     level_editor_gadget[id] = gi;
6985   }
6986
6987   // these values are not constant, but can change at runtime
6988   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_UP].x    = ED_SCROLL_UP_XPOS;
6989   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_UP].y    = ED_SCROLL_UP_YPOS;
6990   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_DOWN].x  = ED_SCROLL_DOWN_XPOS;
6991   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_DOWN].y  = ED_SCROLL_DOWN_YPOS;
6992   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_LEFT].x  = ED_SCROLL_LEFT_XPOS;
6993   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_LEFT].y  = ED_SCROLL_LEFT_YPOS;
6994   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_RIGHT].x = ED_SCROLL_RIGHT_XPOS;
6995   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_RIGHT].y = ED_SCROLL_RIGHT_YPOS;
6996   scrollbutton_pos[ED_SCROLLBUTTON_ID_LIST_UP].x    = ED_SCROLL2_UP_XPOS;
6997   scrollbutton_pos[ED_SCROLLBUTTON_ID_LIST_UP].y    = ED_SCROLL2_UP_YPOS;
6998   scrollbutton_pos[ED_SCROLLBUTTON_ID_LIST_DOWN].x  = ED_SCROLL2_DOWN_XPOS;
6999   scrollbutton_pos[ED_SCROLLBUTTON_ID_LIST_DOWN].y  = ED_SCROLL2_DOWN_YPOS;
7000
7001   // create buttons for scrolling of drawing area and element list
7002   for (i = 0; i < ED_NUM_SCROLLBUTTONS; i++)
7003   {
7004     int id = scrollbutton_info[i].gadget_id;
7005     int type_id = scrollbutton_info[i].gadget_type_id;
7006     int graphic = scrollbutton_info[i].graphic;
7007     struct GraphicInfo *gd = &graphic_info[graphic];
7008     Bitmap *gd_bitmap = gd->bitmap;
7009     int gd_x1 = gd->src_x;
7010     int gd_y1 = gd->src_y;
7011     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7012     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7013     int width  = gd->width;
7014     int height = gd->height;
7015     int x = scrollbutton_pos[i].x;
7016     int y = scrollbutton_pos[i].y;
7017     unsigned int event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
7018
7019     if (type_id != i)
7020       Fail("'scrollbutton_info' structure corrupted at index %d -- please fix", i);
7021
7022     if (id == GADGET_ID_SCROLL_LIST_UP ||
7023         id == GADGET_ID_SCROLL_LIST_DOWN)
7024     {
7025       x += PX;
7026       y += PY;
7027     }
7028     else
7029     {
7030       x += SX;
7031       y += SY;
7032     }
7033
7034     gi = CreateGadget(GDI_CUSTOM_ID, id,
7035                       GDI_CUSTOM_TYPE_ID, type_id,
7036                       GDI_IMAGE_ID, graphic,
7037                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
7038                       GDI_X, x,
7039                       GDI_Y, y,
7040                       GDI_WIDTH, width,
7041                       GDI_HEIGHT, height,
7042                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7043                       GDI_STATE, GD_BUTTON_UNPRESSED,
7044                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y1,
7045                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y2,
7046                       GDI_EVENT_MASK, event_mask,
7047                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7048                       GDI_CALLBACK_ACTION, HandleControlButtons,
7049                       GDI_END);
7050
7051     if (gi == NULL)
7052       Fail("cannot create gadget");
7053
7054     level_editor_gadget[id] = gi;
7055   }
7056
7057   // create buttons for element list
7058   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
7059   {
7060     int type_id = i;
7061     int id = GADGET_ID_ELEMENTLIST_FIRST + i;
7062     int graphic = IMG_EDITOR_PALETTE_BUTTON;
7063     struct GraphicInfo *gd = &graphic_info[graphic];
7064     Bitmap *gd_bitmap = gd->bitmap;
7065     Bitmap *deco_bitmap;
7066     int deco_x, deco_y, deco_xpos, deco_ypos;
7067     int gd_x1 = gd->src_x;
7068     int gd_y1 = gd->src_y;
7069     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7070     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7071     int xx = i % ED_ELEMENTLIST_BUTTONS_HORIZ;
7072     int yy = i / ED_ELEMENTLIST_BUTTONS_HORIZ;
7073     int x = PX + ED_ELEMENTLIST_XPOS + xx * gd->width;
7074     int y = PY + ED_ELEMENTLIST_YPOS + yy * gd->height;
7075     int element = editor_elements[i];
7076     int tile_size = BUTTON_TILE_SIZE(editor.palette.tile_size);
7077     unsigned int event_mask = GD_EVENT_RELEASED;
7078
7079     getEditorGraphicSource(element, tile_size, &deco_bitmap, &deco_x, &deco_y);
7080
7081     deco_xpos = (gd->width  - tile_size) / 2;
7082     deco_ypos = (gd->height - tile_size) / 2;
7083
7084     gi = CreateGadget(GDI_CUSTOM_ID, id,
7085                       GDI_CUSTOM_TYPE_ID, type_id,
7086                       GDI_IMAGE_ID, graphic,
7087                       GDI_INFO_TEXT, getElementInfoText(element),
7088                       GDI_X, x,
7089                       GDI_Y, y,
7090                       GDI_WIDTH, gd->width,
7091                       GDI_HEIGHT, gd->height,
7092                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7093                       GDI_STATE, GD_BUTTON_UNPRESSED,
7094                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y1,
7095                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y2,
7096                       GDI_DECORATION_DESIGN, deco_bitmap, deco_x, deco_y,
7097                       GDI_DECORATION_POSITION, deco_xpos, deco_ypos,
7098                       GDI_DECORATION_SIZE, tile_size, tile_size,
7099                       GDI_DECORATION_SHIFTING, 1, 1,
7100                       GDI_EVENT_MASK, event_mask,
7101                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7102                       GDI_CALLBACK_ACTION, HandleControlButtons,
7103                       GDI_END);
7104
7105     if (gi == NULL)
7106       Fail("cannot create gadget");
7107
7108     level_editor_gadget[id] = gi;
7109   }
7110 }
7111
7112 static void CreateCounterButtons(void)
7113 {
7114   int max_infotext_len = getMaxInfoTextLength();
7115   int i;
7116
7117   for (i = 0; i < ED_NUM_COUNTERBUTTONS; i++)
7118   {
7119     int type_id = counterbutton_info[i].gadget_type_id;
7120     int x = SX + ED_SETTINGS_X(counterbutton_info[i].x); // down count button
7121     int y = SY + ED_SETTINGS_Y(counterbutton_info[i].y);
7122     int j;
7123
7124     if (type_id != i)
7125       Fail("'counterbutton_info' structure corrupted at index %d -- please fix", i);
7126
7127     // determine horizontal position to the right of specified gadget
7128     if (counterbutton_info[i].gadget_id_align != GADGET_ID_NONE)
7129       x = (right_gadget_border[counterbutton_info[i].gadget_id_align] +
7130            ED_GADGET_TEXT_DISTANCE);
7131
7132     // determine horizontal offset for leading text
7133     if (counterbutton_info[i].text_left != NULL)
7134       x += getTextWidthForGadget(counterbutton_info[i].text_left);
7135
7136     for (j = 0; j < 2; j++)
7137     {
7138       struct GadgetInfo *gi;
7139       int id = (j == 0 ?
7140                 counterbutton_info[i].gadget_id_down :
7141                 counterbutton_info[i].gadget_id_up);
7142       int graphic;
7143       struct GraphicInfo *gd;
7144       int gd_x1, gd_x2, gd_y1, gd_y2;
7145       unsigned int event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
7146       char infotext[max_infotext_len + 1];
7147
7148       if (i == ED_COUNTER_ID_SELECT_LEVEL)
7149       {
7150         graphic = (j == 0 ?
7151                    IMG_GFX_EDITOR_BUTTON_PREV_LEVEL :
7152                    IMG_GFX_EDITOR_BUTTON_NEXT_LEVEL);
7153
7154         event_mask |= GD_EVENT_RELEASED;
7155
7156         if (j == 0)
7157         {
7158           x = DX + editor.button.prev_level.x;
7159           y = DY + editor.button.prev_level.y;
7160         }
7161         else
7162         {
7163           x = DX + editor.button.next_level.x;
7164           y = DY + editor.button.next_level.y;
7165         }
7166       }
7167       else
7168       {
7169         graphic = (j == 0 ?
7170                    IMG_EDITOR_COUNTER_DOWN :
7171                    IMG_EDITOR_COUNTER_UP);
7172       }
7173
7174       gd = &graphic_info[graphic];
7175
7176       gd_x1 = gd->src_x;
7177       gd_y1 = gd->src_y;
7178       gd_x2 = gd->src_x + gd->pressed_xoffset;
7179       gd_y2 = gd->src_y + gd->pressed_yoffset;
7180
7181       sprintf(infotext, "%s counter value by 1, 5 or 10",
7182               (j == 0 ? "decrease" : "increase"));
7183
7184       gi = CreateGadget(GDI_CUSTOM_ID, id,
7185                         GDI_CUSTOM_TYPE_ID, type_id,
7186                         GDI_IMAGE_ID, graphic,
7187                         GDI_INFO_TEXT, infotext,
7188                         GDI_X, x,
7189                         GDI_Y, y,
7190                         GDI_WIDTH, gd->width,
7191                         GDI_HEIGHT, gd->height,
7192                         GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7193                         GDI_STATE, GD_BUTTON_UNPRESSED,
7194                         GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7195                         GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7196                         GDI_EVENT_MASK, event_mask,
7197                         GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7198                         GDI_CALLBACK_ACTION, HandleCounterButtons,
7199                         GDI_END);
7200
7201       if (gi == NULL)
7202         Fail("cannot create gadget");
7203
7204       level_editor_gadget[id] = gi;
7205       right_gadget_border[id] =
7206         getRightGadgetBorder(gi, counterbutton_info[i].text_right);
7207
7208       x += gi->width + ED_GADGET_SMALL_DISTANCE;        // text count button
7209
7210       if (j == 0)
7211       {
7212         int font_type = FONT_INPUT_1;
7213         int font_type_active = FONT_INPUT_1_ACTIVE;
7214
7215         id = counterbutton_info[i].gadget_id_text;
7216
7217         event_mask = GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
7218
7219         if (i == ED_COUNTER_ID_SELECT_LEVEL)
7220         {
7221           graphic = IMG_GFX_EDITOR_INPUT_LEVEL_NUMBER;
7222
7223           font_type = FONT_LEVEL_NUMBER;
7224           font_type_active = FONT_LEVEL_NUMBER_ACTIVE;
7225
7226           x = DX + editor.input.level_number.x;
7227           y = DY + editor.input.level_number.y;
7228         }
7229         else
7230         {
7231           graphic = IMG_EDITOR_COUNTER_INPUT;
7232         }
7233
7234         gd = &graphic_info[graphic];
7235
7236         gd_x1 = gd->src_x;
7237         gd_y1 = gd->src_y;
7238         gd_x2 = gd->src_x + gd->active_xoffset;
7239         gd_y2 = gd->src_y + gd->active_yoffset;
7240
7241         gi = CreateGadget(GDI_CUSTOM_ID, id,
7242                           GDI_CUSTOM_TYPE_ID, type_id,
7243                           GDI_IMAGE_ID, graphic,
7244                           GDI_INFO_TEXT, "enter counter value",
7245                           GDI_X, x,
7246                           GDI_Y, y,
7247                           GDI_TYPE, GD_TYPE_TEXT_INPUT_NUMERIC,
7248                           GDI_NUMBER_VALUE, 0,
7249                           GDI_NUMBER_MIN, counterbutton_info[i].min_value,
7250                           GDI_NUMBER_MAX, counterbutton_info[i].max_value,
7251                           GDI_TEXT_SIZE, 3,     // minimal counter text size
7252                           GDI_TEXT_FONT, font_type,
7253                           GDI_TEXT_FONT_ACTIVE, font_type_active,
7254                           GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7255                           GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7256                           GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7257                           GDI_DESIGN_WIDTH, gd->width,
7258                           GDI_EVENT_MASK, event_mask,
7259                           GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7260                           GDI_CALLBACK_ACTION, HandleCounterButtons,
7261                           GDI_END);
7262
7263         if (gi == NULL)
7264           Fail("cannot create gadget");
7265
7266         level_editor_gadget[id] = gi;
7267         right_gadget_border[id] =
7268           getRightGadgetBorder(gi, counterbutton_info[i].text_right);
7269
7270         x += gi->width + ED_GADGET_SMALL_DISTANCE;      // up count button
7271       }
7272     }
7273   }
7274 }
7275
7276 static void CreateDrawingAreas(void)
7277 {
7278   int i;
7279
7280   // these values are not constant, but can change at runtime
7281   drawingarea_info[ED_DRAWING_ID_DRAWING_LEVEL].area_xsize = MAX_ED_FIELDX;
7282   drawingarea_info[ED_DRAWING_ID_DRAWING_LEVEL].area_ysize = MAX_ED_FIELDY;
7283
7284   for (i = 0; i < ED_NUM_DRAWING_AREAS; i++)
7285   {
7286     struct GadgetInfo *gi;
7287     int id = drawingarea_info[i].gadget_id;
7288     int type_id = drawingarea_info[i].gadget_type_id;
7289     int x = SX + ED_AREA_SETTINGS_X(drawingarea_info[i]);
7290     int y = SY + ED_AREA_SETTINGS_Y(drawingarea_info[i]);
7291     int area_xsize = drawingarea_info[i].area_xsize;
7292     int area_ysize = drawingarea_info[i].area_ysize;
7293     int item_size = (id == GADGET_ID_DRAWING_LEVEL ?
7294                      ed_tilesize : ED_DRAWINGAREA_TILE_SIZE);
7295     unsigned int event_mask =
7296       GD_EVENT_PRESSED | GD_EVENT_RELEASED | GD_EVENT_MOVING |
7297       GD_EVENT_OFF_BORDERS | GD_EVENT_PIXEL_PRECISE;
7298
7299     if (type_id != i)
7300       Fail("'drawingarea_info' structure corrupted at index %d -- please fix", i);
7301
7302     // determine horizontal position to the right of specified gadget
7303     if (drawingarea_info[i].gadget_id_align != GADGET_ID_NONE)
7304       x = (right_gadget_border[drawingarea_info[i].gadget_id_align] +
7305            ED_DRAWINGAREA_TEXT_DISTANCE);
7306
7307     // determine horizontal offset for leading text
7308     if (drawingarea_info[i].text_left != NULL)
7309       x += getTextWidthForDrawingArea(drawingarea_info[i].text_left);
7310
7311     gi = CreateGadget(GDI_CUSTOM_ID, id,
7312                       GDI_CUSTOM_TYPE_ID, type_id,
7313                       GDI_X, x,
7314                       GDI_Y, y,
7315                       GDI_TYPE, GD_TYPE_DRAWING_AREA,
7316                       GDI_AREA_SIZE, area_xsize, area_ysize,
7317                       GDI_ITEM_SIZE, item_size, item_size,
7318                       GDI_EVENT_MASK, event_mask,
7319                       GDI_CALLBACK_INFO, HandleDrawingAreaInfo,
7320                       GDI_CALLBACK_ACTION, HandleDrawingAreas,
7321                       GDI_END);
7322
7323     if (gi == NULL)
7324       Fail("cannot create gadget");
7325
7326     level_editor_gadget[id] = gi;
7327     right_gadget_border[id] =
7328       getRightGadgetBorder(gi, drawingarea_info[i].text_right);
7329   }
7330 }
7331
7332 static void CreateTextInputGadgets(void)
7333 {
7334   struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_INPUT_TEXT];
7335   int max_infotext_len = getMaxInfoTextLength();
7336   int i;
7337
7338   for (i = 0; i < ED_NUM_TEXTINPUT; i++)
7339   {
7340     int gd_x1 = gd->src_x;
7341     int gd_y1 = gd->src_y;
7342     int gd_x2 = gd->src_x + gd->active_xoffset;
7343     int gd_y2 = gd->src_y + gd->active_yoffset;
7344     struct GadgetInfo *gi;
7345     unsigned int event_mask = GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
7346     char infotext[MAX_OUTPUT_LINESIZE + 1];
7347     int id = textinput_info[i].gadget_id;
7348     int type_id = textinput_info[i].gadget_type_id;
7349     int x, y;
7350
7351     if (type_id != i)
7352       Fail("'textinput_info' structure corrupted at index %d -- please fix", i);
7353
7354     if (i == ED_TEXTINPUT_ID_ELEMENT_NAME)
7355     {
7356       int element_border = graphic_info[IMG_EDITOR_ELEMENT_BORDER].border_size;
7357       int border_size = gd->border_size;
7358       int font_nr = FONT_INPUT_1;
7359       int font_height = getFontHeight(font_nr);
7360       int xoffset = element_border + TILEX + element_border + 3 * border_size;
7361       int yoffset = element_border + (TILEY - font_height) / 2;
7362
7363       x = (editor.settings.element_name.x != -1 ?
7364            editor.settings.element_name.x :
7365            editor.settings.element_graphic.x + xoffset) - border_size;
7366       y = (editor.settings.element_name.y != -1 ?
7367            editor.settings.element_name.y :
7368            editor.settings.element_graphic.y + yoffset) - border_size;
7369     }
7370     else
7371     {
7372       x = ED_SETTINGS_X(textinput_info[i].x);
7373       y = ED_SETTINGS_Y(textinput_info[i].y);
7374     }
7375
7376     sprintf(infotext, "Enter %s", textinput_info[i].infotext);
7377     infotext[max_infotext_len] = '\0';
7378
7379     gi = CreateGadget(GDI_CUSTOM_ID, id,
7380                       GDI_CUSTOM_TYPE_ID, type_id,
7381                       GDI_INFO_TEXT, infotext,
7382                       GDI_X, SX + x,
7383                       GDI_Y, SY + y,
7384                       GDI_TYPE, GD_TYPE_TEXT_INPUT_ALPHANUMERIC,
7385                       GDI_TEXT_VALUE, textinput_info[i].value,
7386                       GDI_TEXT_SIZE, textinput_info[i].size,
7387                       GDI_TEXT_FONT, FONT_INPUT_1,
7388                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_1_ACTIVE,
7389                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7390                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7391                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7392                       GDI_DESIGN_WIDTH, gd->width,
7393                       GDI_EVENT_MASK, event_mask,
7394                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7395                       GDI_CALLBACK_ACTION, HandleTextInputGadgets,
7396                       GDI_END);
7397
7398     if (gi == NULL)
7399       Fail("cannot create gadget");
7400
7401     level_editor_gadget[id] = gi;
7402   }
7403 }
7404
7405 static void CreateTextAreaGadgets(void)
7406 {
7407   int max_infotext_len = getMaxInfoTextLength();
7408   int i;
7409
7410   for (i = 0; i < ED_NUM_TEXTAREAS; i++)
7411   {
7412     struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_INPUT_TEXTAREA];
7413     int gd_x1 = gd->src_x;
7414     int gd_y1 = gd->src_y;
7415     int gd_x2 = gd->src_x + gd->active_xoffset;
7416     int gd_y2 = gd->src_y + gd->active_yoffset;
7417     struct GadgetInfo *gi;
7418     unsigned int event_mask = GD_EVENT_TEXT_LEAVING;
7419     char infotext[MAX_OUTPUT_LINESIZE + 1];
7420     int id = textarea_info[i].gadget_id;
7421     int type_id = textarea_info[i].gadget_type_id;
7422     int area_xsize = textarea_info[i].xsize;
7423     int area_ysize = textarea_info[i].ysize;
7424
7425     if (type_id != i)
7426       Fail("'textarea_info' structure corrupted at index %d -- please fix", i);
7427
7428     sprintf(infotext, "Enter %s", textarea_info[i].infotext);
7429     infotext[max_infotext_len] = '\0';
7430
7431     gi = CreateGadget(GDI_CUSTOM_ID, id,
7432                       GDI_CUSTOM_TYPE_ID, type_id,
7433                       GDI_INFO_TEXT, infotext,
7434                       GDI_X, SX + ED_SETTINGS_X(textarea_info[i].x),
7435                       GDI_Y, SY + ED_SETTINGS_Y(textarea_info[i].y),
7436                       GDI_TYPE, GD_TYPE_TEXT_AREA,
7437                       GDI_AREA_SIZE, area_xsize, area_ysize,
7438                       GDI_TEXT_FONT, FONT_INPUT_1,
7439                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_1_ACTIVE,
7440                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7441                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7442                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7443                       GDI_DESIGN_WIDTH, gd->width,
7444                       GDI_EVENT_MASK, event_mask,
7445                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7446                       GDI_CALLBACK_ACTION, HandleTextAreaGadgets,
7447                       GDI_END);
7448
7449     if (gi == NULL)
7450       Fail("cannot create gadget");
7451
7452     level_editor_gadget[id] = gi;
7453   }
7454 }
7455
7456 static void CreateSelectboxGadgets(void)
7457 {
7458   int max_infotext_len = getMaxInfoTextLength();
7459   int i, j;
7460
7461   for (i = 0; i < ED_NUM_SELECTBOX; i++)
7462   {
7463     struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_SELECTBOX_INPUT];
7464     struct GraphicInfo *gd2 = &graphic_info[IMG_EDITOR_SELECTBOX_BUTTON];
7465     int gd_x1 = gd->src_x;
7466     int gd_y1 = gd->src_y;
7467     int gd_x2 = gd->src_x + gd->active_xoffset;
7468     int gd_y2 = gd->src_y + gd->active_yoffset;
7469     int selectbox_button_xsize = gd2->width;
7470     struct GadgetInfo *gi;
7471     char infotext[MAX_OUTPUT_LINESIZE + 1];
7472     int id = selectbox_info[i].gadget_id;
7473     int type_id = selectbox_info[i].gadget_type_id;
7474     int x = SX + ED_SETTINGS_X(selectbox_info[i].x);
7475     int y = SY + ED_SETTINGS_Y(selectbox_info[i].y);
7476     unsigned int event_mask =
7477       GD_EVENT_RELEASED | GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
7478
7479     if (type_id != i)
7480       Fail("'selectbox_info' structure corrupted at index %d -- please fix", i);
7481
7482     if (selectbox_info[i].size == -1)   // dynamically determine size
7483     {
7484       // (we cannot use -1 for uninitialized values if we directly compare
7485       // with results from strlen(), because the '<' and '>' operation will
7486       // implicitely cast -1 to an unsigned integer value!)
7487       selectbox_info[i].size = 0;
7488
7489       for (j = 0; selectbox_info[i].options[j].text != NULL; j++)
7490         if (strlen(selectbox_info[i].options[j].text) > selectbox_info[i].size)
7491           selectbox_info[i].size = strlen(selectbox_info[i].options[j].text);
7492
7493       selectbox_info[i].size++;         // add one character empty space
7494     }
7495
7496     // determine horizontal position to the right of specified gadget
7497     if (selectbox_info[i].gadget_id_align != GADGET_ID_NONE)
7498       x = (right_gadget_border[selectbox_info[i].gadget_id_align] +
7499            ED_GADGET_TEXT_DISTANCE);
7500
7501     // determine horizontal offset for leading text
7502     if (selectbox_info[i].text_left != NULL)
7503       x += getTextWidthForGadget(selectbox_info[i].text_left);
7504
7505     sprintf(infotext, "Select %s", selectbox_info[i].infotext);
7506     infotext[max_infotext_len] = '\0';
7507
7508     gi = CreateGadget(GDI_CUSTOM_ID, id,
7509                       GDI_CUSTOM_TYPE_ID, type_id,
7510                       GDI_INFO_TEXT, infotext,
7511                       GDI_X, x,
7512                       GDI_Y, y,
7513                       GDI_TYPE, GD_TYPE_SELECTBOX,
7514                       GDI_SELECTBOX_OPTIONS, selectbox_info[i].options,
7515                       GDI_SELECTBOX_CHAR_UNSELECTABLE, '[',
7516                       GDI_TEXT_SIZE, selectbox_info[i].size,
7517                       GDI_TEXT_FONT, FONT_INPUT_1,
7518                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_1_ACTIVE,
7519                       GDI_TEXT_FONT_UNSELECTABLE, FONT_TEXT_1,
7520                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7521                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7522                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7523                       GDI_BORDER_SIZE_SELECTBUTTON, selectbox_button_xsize,
7524                       GDI_DESIGN_WIDTH, gd->width,
7525                       GDI_EVENT_MASK, event_mask,
7526                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7527                       GDI_CALLBACK_ACTION, HandleSelectboxGadgets,
7528                       GDI_END);
7529
7530     if (gi == NULL)
7531       Fail("cannot create gadget");
7532
7533     level_editor_gadget[id] = gi;
7534     right_gadget_border[id] =
7535       getRightGadgetBorder(gi, selectbox_info[i].text_right);
7536   }
7537 }
7538
7539 static void CreateTextbuttonGadgets(void)
7540 {
7541   int max_infotext_len = getMaxInfoTextLength();
7542   int i;
7543
7544   for (i = 0; i < ED_NUM_TEXTBUTTONS; i++)
7545   {
7546     int id = textbutton_info[i].gadget_id;
7547     int type_id = textbutton_info[i].gadget_type_id;
7548     int is_tab_button =
7549       ((id >= GADGET_ID_LEVELCONFIG_LEVEL && id <= GADGET_ID_LEVELCONFIG_ENGINE) ||
7550        (id >= GADGET_ID_PROPERTIES_INFO && id <= GADGET_ID_PROPERTIES_CHANGE));
7551     int graphic =
7552       (is_tab_button ? IMG_EDITOR_TABBUTTON : IMG_EDITOR_TEXTBUTTON);
7553     int gadget_distance =
7554       (is_tab_button ? ED_GADGET_SMALL_DISTANCE : ED_GADGET_TEXT_DISTANCE);
7555     struct GraphicInfo *gd = &graphic_info[graphic];
7556     int gd_x1 = gd->src_x;
7557     int gd_y1 = gd->src_y;
7558     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7559     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7560     int gd_x1a = gd->src_x + gd->active_xoffset;
7561     int gd_y1a = gd->src_y + gd->active_yoffset;
7562     int border_xsize = gd->border_size + gd->draw_xoffset;
7563     int border_ysize = gd->border_size;
7564     struct GadgetInfo *gi;
7565     unsigned int event_mask = GD_EVENT_RELEASED;
7566     char infotext[MAX_OUTPUT_LINESIZE + 1];
7567     int x = SX + ED_SETTINGS_X(textbutton_info[i].x);
7568     int y = SY + ED_SETTINGS_Y(textbutton_info[i].y);
7569
7570     if (type_id != i)
7571       Fail("'textbutton_info' structure corrupted at index %d -- please fix", i);
7572
7573     if (textbutton_info[i].size == -1)  // dynamically determine size
7574       textbutton_info[i].size = strlen(textbutton_info[i].text);
7575
7576     sprintf(infotext, "%s", textbutton_info[i].infotext);
7577     infotext[max_infotext_len] = '\0';
7578
7579     // determine horizontal position to the right of specified gadget
7580     if (textbutton_info[i].gadget_id_align != GADGET_ID_NONE)
7581     {
7582       int gadget_id_align = textbutton_info[i].gadget_id_align;
7583
7584       x = right_gadget_border[gadget_id_align] + gadget_distance;
7585
7586       if (textbutton_info[i].y == -1)
7587         y = level_editor_gadget[gadget_id_align]->y;
7588     }
7589
7590     // determine horizontal offset for leading text
7591     if (textbutton_info[i].text_left != NULL)
7592       x += getTextWidthForGadget(textbutton_info[i].text_left);
7593
7594     gi = CreateGadget(GDI_CUSTOM_ID, id,
7595                       GDI_CUSTOM_TYPE_ID, type_id,
7596                       GDI_IMAGE_ID, graphic,
7597                       GDI_INFO_TEXT, infotext,
7598                       GDI_X, x,
7599                       GDI_Y, y,
7600                       GDI_TYPE, GD_TYPE_TEXT_BUTTON,
7601                       GDI_TEXT_VALUE, textbutton_info[i].text,
7602                       GDI_TEXT_SIZE, textbutton_info[i].size,
7603                       GDI_TEXT_FONT, FONT_INPUT_2,
7604                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_2_ACTIVE,
7605                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7606                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7607                       GDI_ALT_DESIGN_UNPRESSED, gd->bitmap, gd_x1a, gd_y1a,
7608                       GDI_BORDER_SIZE, border_xsize, border_ysize,
7609                       GDI_DESIGN_WIDTH, gd->width,
7610                       GDI_DECORATION_SHIFTING, 1, 1,
7611                       GDI_EVENT_MASK, event_mask,
7612                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7613                       GDI_CALLBACK_ACTION, HandleTextbuttonGadgets,
7614                       GDI_END);
7615
7616     if (gi == NULL)
7617       Fail("cannot create gadget");
7618
7619     level_editor_gadget[id] = gi;
7620     right_gadget_border[id] =
7621       getRightGadgetBorder(gi, textbutton_info[i].text_right);
7622   }
7623 }
7624
7625 static void CreateGraphicbuttonGadgets(void)
7626 {
7627   struct GadgetInfo *gi;
7628   int i;
7629
7630   // create buttons for scrolling of drawing area and element list
7631   for (i = 0; i < ED_NUM_GRAPHICBUTTONS; i++)
7632   {
7633     int id = graphicbutton_info[i].gadget_id;
7634     int type_id = graphicbutton_info[i].gadget_type_id;
7635     int x = SX + ED_SETTINGS_X(graphicbutton_info[i].x);
7636     int y = SY + ED_SETTINGS_Y(graphicbutton_info[i].y);
7637     int graphic = graphicbutton_info[i].graphic;
7638     struct GraphicInfo *gd = &graphic_info[graphic];
7639     int gd_x1 = gd->src_x;
7640     int gd_y1 = gd->src_y;
7641     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7642     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7643     unsigned int event_mask = GD_EVENT_RELEASED;
7644
7645     if (type_id != i)
7646       Fail("'graphicbutton_info' structure corrupted at index %d -- please fix", i);
7647
7648     // determine horizontal position to the right of specified gadget
7649     if (graphicbutton_info[i].gadget_id_align != GADGET_ID_NONE)
7650       x = (right_gadget_border[graphicbutton_info[i].gadget_id_align] +
7651            ED_GADGET_TEXT_DISTANCE);
7652
7653     // determine horizontal offset for leading text
7654     if (graphicbutton_info[i].text_left != NULL)
7655       x += getTextWidthForGadget(graphicbutton_info[i].text_left);
7656
7657     gi = CreateGadget(GDI_CUSTOM_ID, id,
7658                       GDI_CUSTOM_TYPE_ID, type_id,
7659                       GDI_IMAGE_ID, graphic,
7660                       GDI_INFO_TEXT, graphicbutton_info[i].infotext,
7661                       GDI_X, x,
7662                       GDI_Y, y,
7663                       GDI_WIDTH, gd->width,
7664                       GDI_HEIGHT, gd->height,
7665                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7666                       GDI_STATE, GD_BUTTON_UNPRESSED,
7667                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7668                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7669                       GDI_EVENT_MASK, event_mask,
7670                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7671                       GDI_CALLBACK_ACTION, HandleGraphicbuttonGadgets,
7672                       GDI_END);
7673
7674     if (gi == NULL)
7675       Fail("cannot create gadget");
7676
7677     level_editor_gadget[id] = gi;
7678     right_gadget_border[id] =
7679       getRightGadgetBorder(gi, graphicbutton_info[i].text_right);
7680   }
7681 }
7682
7683 static void CreateScrollbarGadgets(void)
7684 {
7685   int i;
7686
7687   // these values are not constant, but can change at runtime
7688   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].x =
7689     SX + ED_SCROLL_HORIZONTAL_XPOS;
7690   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].y =
7691     SY + ED_SCROLL_HORIZONTAL_YPOS;
7692   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].width =
7693     ED_SCROLL_HORIZONTAL_XSIZE;
7694   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].height =
7695     ED_SCROLL_HORIZONTAL_YSIZE;
7696   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].wheel_x      = SX;
7697   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].wheel_y      = SY;
7698   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].wheel_width  = SXSIZE;
7699   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].wheel_height = SYSIZE;
7700
7701   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].x =
7702     SX + ED_SCROLL_VERTICAL_XPOS;
7703   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].y =
7704     SY + ED_SCROLL_VERTICAL_YPOS;
7705   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].width =
7706     ED_SCROLL_VERTICAL_XSIZE;
7707   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].height =
7708     ED_SCROLL_VERTICAL_YSIZE;
7709   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].wheel_x      = SX;
7710   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].wheel_y      = SY;
7711   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].wheel_width  = SXSIZE;
7712   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].wheel_height = SYSIZE;
7713
7714   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].x =
7715     PX + ED_SCROLL2_VERTICAL_XPOS;
7716   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].y =
7717     PY + ED_SCROLL2_VERTICAL_YPOS;
7718   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].width =
7719     ED_SCROLL2_VERTICAL_XSIZE;
7720   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].height =
7721     ED_SCROLL2_VERTICAL_YSIZE;
7722   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].wheel_x = PX;
7723   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].wheel_y = PY;
7724   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].wheel_width  = PXSIZE;
7725   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].wheel_height = PYSIZE;
7726
7727   for (i = 0; i < ED_NUM_SCROLLBARS; i++)
7728   {
7729     int id = scrollbar_info[i].gadget_id;
7730     int type_id = scrollbar_info[i].gadget_type_id;
7731     int graphic = scrollbar_info[i].graphic;
7732     struct GraphicInfo *gd = &graphic_info[graphic];
7733     int gd_x1 = gd->src_x;
7734     int gd_y1 = gd->src_y;
7735     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7736     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7737     struct GadgetInfo *gi;
7738     int items_max, items_visible, item_position;
7739     unsigned int event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
7740
7741     if (type_id != i)
7742       Fail("'scrollbar_info' structure corrupted at index %d -- please fix", i);
7743
7744     if (i == ED_SCROLLBAR_ID_LIST_VERTICAL)
7745     {
7746       items_max = num_editor_elements / ED_ELEMENTLIST_BUTTONS_HORIZ;
7747       items_visible = ED_ELEMENTLIST_BUTTONS_VERT;
7748       item_position = element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ;
7749     }
7750     else        // drawing area scrollbars
7751     {
7752       if (scrollbar_info[i].type == GD_TYPE_SCROLLBAR_HORIZONTAL)
7753       {
7754         items_max = MAX(lev_fieldx + 2, ed_fieldx);
7755         items_visible = ed_fieldx;
7756         item_position = 0;
7757       }
7758       else
7759       {
7760         items_max = MAX(lev_fieldy + 2, ed_fieldy);
7761         items_visible = ed_fieldy;
7762         item_position = 0;
7763       }
7764     }
7765
7766     gi = CreateGadget(GDI_CUSTOM_ID, id,
7767                       GDI_CUSTOM_TYPE_ID, type_id,
7768                       GDI_IMAGE_ID, graphic,
7769                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
7770                       GDI_X, scrollbar_pos[i].x,
7771                       GDI_Y, scrollbar_pos[i].y,
7772                       GDI_WIDTH, scrollbar_pos[i].width,
7773                       GDI_HEIGHT, scrollbar_pos[i].height,
7774                       GDI_TYPE, scrollbar_info[i].type,
7775                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
7776                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
7777                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
7778                       GDI_WHEEL_AREA_X, scrollbar_pos[i].wheel_x,
7779                       GDI_WHEEL_AREA_Y, scrollbar_pos[i].wheel_y,
7780                       GDI_WHEEL_AREA_WIDTH, scrollbar_pos[i].wheel_width,
7781                       GDI_WHEEL_AREA_HEIGHT, scrollbar_pos[i].wheel_height,
7782                       GDI_STATE, GD_BUTTON_UNPRESSED,
7783                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7784                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7785                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7786                       GDI_EVENT_MASK, event_mask,
7787                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7788                       GDI_CALLBACK_ACTION, HandleControlButtons,
7789                       GDI_END);
7790
7791     if (gi == NULL)
7792       Fail("cannot create gadget");
7793
7794     level_editor_gadget[id] = gi;
7795   }
7796 }
7797
7798 static void CreateCheckbuttonGadgets(void)
7799 {
7800   struct GadgetInfo *gi;
7801   int i;
7802
7803   for (i = 0; i < ED_NUM_CHECKBUTTONS; i++)
7804   {
7805     int id = checkbutton_info[i].gadget_id;
7806     int type_id = checkbutton_info[i].gadget_type_id;
7807     int graphic = (id == GADGET_ID_STICK_ELEMENT ? IMG_EDITOR_STICKYBUTTON :
7808                    IMG_EDITOR_CHECKBOX);
7809     struct GraphicInfo *gd = &graphic_info[graphic];
7810     int gd_x1 = gd->src_x;
7811     int gd_y1 = gd->src_y;
7812     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7813     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7814     int gd_x1a = gd->src_x + gd->active_xoffset;
7815     int gd_y1a = gd->src_y + gd->active_yoffset;
7816     int gd_x2a = gd->src_x + gd->active_xoffset + gd->pressed_xoffset;
7817     int gd_y2a = gd->src_y + gd->active_yoffset + gd->pressed_yoffset;
7818     int x = SX + ED_SETTINGS_X(checkbutton_info[i].x);
7819     int y = SY + ED_SETTINGS_Y(checkbutton_info[i].y);
7820     unsigned int event_mask = GD_EVENT_PRESSED;
7821
7822     if (type_id != i)
7823       Fail("'checkbutton_info' structure corrupted at index %d -- please fix", i);
7824
7825     // determine horizontal position to the right of specified gadget
7826     if (checkbutton_info[i].gadget_id_align != GADGET_ID_NONE)
7827       x = (right_gadget_border[checkbutton_info[i].gadget_id_align] +
7828            ED_GADGET_TEXT_DISTANCE);
7829
7830     // determine horizontal offset for leading text
7831     if (checkbutton_info[i].text_left != NULL)
7832       x += getTextWidthForGadget(checkbutton_info[i].text_left);
7833
7834     gi = CreateGadget(GDI_CUSTOM_ID, id,
7835                       GDI_CUSTOM_TYPE_ID, type_id,
7836                       GDI_IMAGE_ID, graphic,
7837                       GDI_INFO_TEXT, checkbutton_info[i].infotext,
7838                       GDI_X, x,
7839                       GDI_Y, y,
7840                       GDI_WIDTH, gd->width,
7841                       GDI_HEIGHT, gd->height,
7842                       GDI_TYPE, GD_TYPE_CHECK_BUTTON,
7843                       GDI_CHECKED, *checkbutton_info[i].value,
7844                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7845                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7846                       GDI_ALT_DESIGN_UNPRESSED, gd->bitmap, gd_x1a, gd_y1a,
7847                       GDI_ALT_DESIGN_PRESSED, gd->bitmap, gd_x2a, gd_y2a,
7848                       GDI_EVENT_MASK, event_mask,
7849                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7850                       GDI_CALLBACK_ACTION, HandleCheckbuttons,
7851                       GDI_END);
7852
7853     if (gi == NULL)
7854       Fail("cannot create gadget");
7855
7856     level_editor_gadget[id] = gi;
7857     right_gadget_border[id] =
7858       getRightGadgetBorder(gi, checkbutton_info[i].text_right);
7859   }
7860 }
7861
7862 static void CreateRadiobuttonGadgets(void)
7863 {
7864   int graphic = IMG_EDITOR_RADIOBUTTON;
7865   struct GraphicInfo *gd = &graphic_info[graphic];
7866   int gd_x1 = gd->src_x;
7867   int gd_y1 = gd->src_y;
7868   int gd_x2 = gd->src_x + gd->pressed_xoffset;
7869   int gd_y2 = gd->src_y + gd->pressed_yoffset;
7870   int gd_x1a = gd->src_x + gd->active_xoffset;
7871   int gd_y1a = gd->src_y + gd->active_yoffset;
7872   int gd_x2a = gd->src_x + gd->active_xoffset + gd->pressed_xoffset;
7873   int gd_y2a = gd->src_y + gd->active_yoffset + gd->pressed_yoffset;
7874   struct GadgetInfo *gi;
7875   int i;
7876
7877   for (i = 0; i < ED_NUM_RADIOBUTTONS; i++)
7878   {
7879     int id = radiobutton_info[i].gadget_id;
7880     int type_id = radiobutton_info[i].gadget_type_id;
7881     int x = SX + ED_SETTINGS_X(radiobutton_info[i].x);
7882     int y = SY + ED_SETTINGS_Y(radiobutton_info[i].y);
7883     unsigned int event_mask = GD_EVENT_PRESSED;
7884
7885     if (type_id != i)
7886       Fail("'radiobutton_info' structure corrupted at index %d -- please fix", i);
7887
7888     int checked =
7889       (*radiobutton_info[i].value == radiobutton_info[i].checked_value);
7890
7891     // determine horizontal position to the right of specified gadget
7892     if (radiobutton_info[i].gadget_id_align != GADGET_ID_NONE)
7893       x = (right_gadget_border[radiobutton_info[i].gadget_id_align] +
7894            ED_GADGET_TEXT_DISTANCE);
7895
7896     // determine horizontal offset for leading text
7897     if (radiobutton_info[i].text_left != NULL)
7898       x += getTextWidthForGadget(radiobutton_info[i].text_left);
7899
7900     gi = CreateGadget(GDI_CUSTOM_ID, id,
7901                       GDI_CUSTOM_TYPE_ID, type_id,
7902                       GDI_IMAGE_ID, graphic,
7903                       GDI_INFO_TEXT, radiobutton_info[i].infotext,
7904                       GDI_X, x,
7905                       GDI_Y, y,
7906                       GDI_WIDTH, gd->width,
7907                       GDI_HEIGHT, gd->height,
7908                       GDI_TYPE, GD_TYPE_RADIO_BUTTON,
7909                       GDI_RADIO_NR, radiobutton_info[i].radio_button_nr,
7910                       GDI_CHECKED, checked,
7911                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7912                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7913                       GDI_ALT_DESIGN_UNPRESSED, gd->bitmap, gd_x1a, gd_y1a,
7914                       GDI_ALT_DESIGN_PRESSED, gd->bitmap, gd_x2a, gd_y2a,
7915                       GDI_EVENT_MASK, event_mask,
7916                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7917                       GDI_CALLBACK_ACTION, HandleRadiobuttons,
7918                       GDI_END);
7919
7920     if (gi == NULL)
7921       Fail("cannot create gadget");
7922
7923     level_editor_gadget[id] = gi;
7924     right_gadget_border[id] =
7925       getRightGadgetBorder(gi, radiobutton_info[i].text_right);
7926   }
7927 }
7928
7929 void CreateLevelEditorGadgets(void)
7930 {
7931   // force EDITOR font inside level editor
7932   SetFontStatus(GAME_MODE_EDITOR);
7933
7934   // these values are not constant, but can change at runtime
7935   ed_fieldx = MAX_ED_FIELDX - 1;
7936   ed_fieldy = MAX_ED_FIELDY - 1;
7937
7938   num_editor_gadgets = NUM_EDITOR_GADGETS;
7939
7940   // Debug("editor", "allocating %d gadgets ...\n", num_editor_gadgets);
7941
7942   level_editor_gadget =
7943     checked_calloc(num_editor_gadgets * sizeof(struct GadgetInfo *));
7944   right_gadget_border =
7945     checked_calloc(num_editor_gadgets * sizeof(int));
7946
7947   // set number of empty (padding) element buttons to maximum number of buttons
7948   num_editor_el_empty = ED_NUM_ELEMENTLIST_BUTTONS;
7949
7950   editor_el_empty = checked_calloc(num_editor_el_empty * sizeof(int));
7951   editor_el_empty_ptr = editor_el_empty;
7952
7953   use_permanent_palette = !editor.palette.show_as_separate_screen;
7954
7955   InitGadgetScreenBorders(-1, INFOTEXT_YPOS);
7956
7957   ReinitializeElementList();
7958
7959   CreateControlButtons();
7960   CreateScrollbarGadgets();
7961
7962   // order of function calls is important because of cross-references
7963   CreateCheckbuttonGadgets();
7964   CreateCounterButtons();
7965   CreateRadiobuttonGadgets();
7966   CreateTextInputGadgets();
7967   CreateTextAreaGadgets();
7968   CreateSelectboxGadgets();
7969   CreateGraphicbuttonGadgets();
7970   CreateTextbuttonGadgets();
7971   CreateDrawingAreas();
7972
7973   ResetFontStatus();
7974 }
7975
7976 void FreeLevelEditorGadgets(void)
7977 {
7978   int i;
7979
7980   // Debug("editor", "freeing %d gadgets ...\n", num_editor_gadgets);
7981
7982   for (i = 0; i < num_editor_gadgets; i++)
7983   {
7984     FreeGadget(level_editor_gadget[i]);
7985
7986     level_editor_gadget[i] = NULL;
7987   }
7988
7989   checked_free(level_editor_gadget);
7990   checked_free(right_gadget_border);
7991
7992   checked_free(editor_el_empty);
7993 }
7994
7995 static void MapCounterButtons(int id)
7996 {
7997   int font_nr = FONT_TEXT_1;
7998   int font_height = getFontHeight(font_nr);
7999   int gadget_id_down = counterbutton_info[id].gadget_id_down;
8000   int gadget_id_text = counterbutton_info[id].gadget_id_text;
8001   int gadget_id_up   = counterbutton_info[id].gadget_id_up;
8002   struct GadgetInfo *gi_down = level_editor_gadget[gadget_id_down];
8003   struct GadgetInfo *gi_text = level_editor_gadget[gadget_id_text];
8004   struct GadgetInfo *gi_up   = level_editor_gadget[gadget_id_up];
8005   int xoffset_left = getTextWidthForGadget(counterbutton_info[id].text_left);
8006   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8007   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8008   int yoffset = (gi_down->height - font_height) / 2;
8009   int x_left = gi_down->x - xoffset_left;
8010   int x_right;  // set after gadget position was modified
8011   int y_above = gi_down->y - yoffset_above;
8012   int x = gi_down->x;
8013   int y;        // set after gadget position was modified
8014
8015   // counter limits must be changed first to prevent value truncation
8016   ModifyEditorCounterLimits(id, counterbutton_info[id].min_value,
8017                             counterbutton_info[id].max_value);
8018
8019   // right text position might have changed after setting position above
8020   x_right = gi_up->x + gi_up->width + xoffset_right;
8021
8022   ModifyEditorCounterValue(id, *counterbutton_info[id].value);
8023
8024   // set position for "value[1,2,3,4]" counter gadgets (score in most cases)
8025   if (id >= ED_COUNTER_ID_ELEMENT_VALUE1 &&
8026       id <= ED_COUNTER_ID_ELEMENT_VALUE4)
8027   {
8028     ModifyGadget(gi_down, GDI_Y,
8029                  SY + ED_SETTINGS_Y(counterbutton_info[id].y), GDI_END);
8030     ModifyGadget(gi_text, GDI_Y,
8031                  SY + ED_SETTINGS_Y(counterbutton_info[id].y), GDI_END);
8032     ModifyGadget(gi_up,   GDI_Y,
8033                  SY + ED_SETTINGS_Y(counterbutton_info[id].y), GDI_END);
8034   }
8035
8036   // vertical position might have changed after setting position above
8037   y = gi_up->y + yoffset;
8038
8039   if (counterbutton_info[id].text_above)
8040     DrawText(x, y_above, counterbutton_info[id].text_above, font_nr);
8041
8042   if (counterbutton_info[id].text_left)
8043     DrawText(x_left, y, counterbutton_info[id].text_left, font_nr);
8044
8045   if (counterbutton_info[id].text_right)
8046     DrawText(x_right, y, counterbutton_info[id].text_right, font_nr);
8047
8048   MapGadget(gi_down);
8049   MapGadget(gi_text);
8050   MapGadget(gi_up);
8051 }
8052
8053 static void MapControlButtons(void)
8054 {
8055   int counter_id;
8056   int i;
8057
8058   // map toolbox buttons (excluding special CE toolbox buttons)
8059   for (i = 0; i < ED_NUM_CTRL1_2_BUTTONS; i++)
8060     MapGadget(level_editor_gadget[i]);
8061
8062   // map toolbox buttons (element properties buttons)
8063   for (i = ED_NUM_CTRL1_4_BUTTONS; i < ED_NUM_CTRL1_7_BUTTONS; i++)
8064     MapGadget(level_editor_gadget[i]);
8065
8066   if (use_permanent_palette)
8067   {
8068     // map buttons to select elements
8069     for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
8070       MapGadget(level_editor_gadget[GADGET_ID_ELEMENTLIST_FIRST + i]);
8071     MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL]);
8072     MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_UP]);
8073     MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_DOWN]);
8074   }
8075
8076   // map buttons to select level
8077   counter_id = ED_COUNTER_ID_SELECT_LEVEL;
8078   counterbutton_info[counter_id].min_value = leveldir_current->first_level;
8079   counterbutton_info[counter_id].max_value = leveldir_current->last_level;
8080   MapCounterButtons(counter_id);
8081 }
8082
8083 static void MapDrawingArea(int id)
8084 {
8085   int font_nr = FONT_TEXT_1;
8086   int font_height = getFontHeight(font_nr);
8087   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
8088   int area_xsize = gi->drawing.area_xsize;
8089   int area_ysize = gi->drawing.area_ysize;
8090   int xoffset_left = getTextWidthForDrawingArea(drawingarea_info[id].text_left);
8091   int xoffset_below = getTextWidth(drawingarea_info[id].text_below, font_nr);
8092   int x_left  = gi->x - xoffset_left;
8093   int x_right = gi->x + gi->width + ED_DRAWINGAREA_TEXT_DISTANCE;
8094   int x_above = gi->x - ED_DRAWINGAREA_BORDER_SIZE;
8095   int x_below = gi->x + (gi->width - xoffset_below) / 2;
8096   int y_side  = gi->y + (gi->height - font_height) / 2;
8097   int y_above = gi->y - font_height - ED_DRAWINGAREA_TEXT_DISTANCE;
8098   int y_below = gi->y + gi->height + ED_DRAWINGAREA_TEXT_DISTANCE;
8099
8100   if (drawingarea_info[id].text_left)
8101     DrawText(x_left, y_side, drawingarea_info[id].text_left, font_nr);
8102
8103   if (drawingarea_info[id].text_right)
8104     DrawText(x_right, y_side, drawingarea_info[id].text_right, font_nr);
8105
8106   if (drawingarea_info[id].text_above)
8107     DrawText(x_above, y_above, drawingarea_info[id].text_above, font_nr);
8108
8109   if (drawingarea_info[id].text_below)
8110     DrawText(x_below, y_below, drawingarea_info[id].text_below, font_nr);
8111
8112   if (id != ED_DRAWING_ID_DRAWING_LEVEL)
8113   {
8114     DrawElementBorder(gi->x, gi->y,
8115                       area_xsize * ED_DRAWINGAREA_TILE_SIZE,
8116                       area_ysize * ED_DRAWINGAREA_TILE_SIZE, TRUE);
8117
8118     DrawDrawingArea(id);
8119   }
8120
8121   MapGadget(gi);
8122 }
8123
8124 static void MapTextInputGadget(int id)
8125 {
8126   int font_nr = FONT_TEXT_1;
8127   int font_height = getFontHeight(font_nr);
8128   struct GadgetInfo *gi = level_editor_gadget[textinput_info[id].gadget_id];
8129   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8130   int x_above = ED_SETTINGS_X(textinput_info[id].x);
8131   int y_above = ED_SETTINGS_Y(textinput_info[id].y) - yoffset_above;
8132
8133   if (textinput_info[id].text_above)
8134     DrawTextS(x_above, y_above, font_nr, textinput_info[id].text_above);
8135
8136   ModifyGadget(gi, GDI_TEXT_VALUE, textinput_info[id].value, GDI_END);
8137
8138   MapGadget(gi);
8139 }
8140
8141 static void MapTextAreaGadget(int id)
8142 {
8143   int font_nr = FONT_TEXT_1;
8144   int font_height = getFontHeight(font_nr);
8145   struct GadgetInfo *gi = level_editor_gadget[textarea_info[id].gadget_id];
8146   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8147   int x_above = ED_SETTINGS_X(textarea_info[id].x);
8148   int y_above = ED_SETTINGS_Y(textarea_info[id].y) - yoffset_above;
8149   char *text_above = textarea_info[id].text_above;
8150
8151   if (gi->textarea.cropped && textarea_info[id].text_above_cropped)
8152     text_above = textarea_info[id].text_above_cropped;
8153
8154   if (text_above)
8155     DrawTextS(x_above, y_above, font_nr, text_above);
8156
8157   ModifyGadget(gi, GDI_TEXT_VALUE, textarea_info[id].value, GDI_END);
8158
8159   MapGadget(gi);
8160 }
8161
8162 static void MapSelectboxGadget(int id)
8163 {
8164   int font_nr = FONT_TEXT_1;
8165   int font_height = getFontHeight(font_nr);
8166   struct GadgetInfo *gi = level_editor_gadget[selectbox_info[id].gadget_id];
8167   int xoffset_left = getTextWidthForGadget(selectbox_info[id].text_left);
8168   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8169   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8170   int yoffset = (gi->height - font_height) / 2;
8171   int x_left = gi->x - xoffset_left;
8172   int x_right = gi->x + gi->width + xoffset_right;
8173   int y_above = gi->y - yoffset_above;
8174   int x = gi->x;
8175   int y = gi->y + yoffset;
8176
8177   if (selectbox_info[id].text_above)
8178     DrawText(x, y_above, selectbox_info[id].text_above, font_nr);
8179
8180   if (selectbox_info[id].text_left)
8181     DrawText(x_left, y, selectbox_info[id].text_left, font_nr);
8182
8183   if (selectbox_info[id].text_right)
8184     DrawText(x_right, y, selectbox_info[id].text_right, font_nr);
8185
8186   ModifyEditorSelectboxValue(id, *selectbox_info[id].value);
8187
8188   MapGadget(gi);
8189 }
8190
8191 static void MapTextbuttonGadget(int id)
8192 {
8193   int font_nr = FONT_TEXT_1;
8194   int font_height = getFontHeight(font_nr);
8195   struct GadgetInfo *gi = level_editor_gadget[textbutton_info[id].gadget_id];
8196   int xoffset_left = getTextWidthForGadget(textbutton_info[id].text_left);
8197   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8198   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8199   int yoffset = (gi->height - font_height) / 2;
8200   int x_left = gi->x - xoffset_left;
8201   int x_right = gi->x + gi->width + xoffset_right;
8202   int y_above = gi->y - yoffset_above;
8203   int x = gi->x;
8204   int y = gi->y + yoffset;
8205
8206   // only show button to delete change pages when more than minimum pages
8207   if (id == ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE &&
8208       custom_element.num_change_pages == MIN_CHANGE_PAGES)
8209     return;
8210
8211   if (textbutton_info[id].text_above)
8212     DrawText(x, y_above, textbutton_info[id].text_above, font_nr);
8213
8214   if (textbutton_info[id].text_left)
8215     DrawText(x_left, y, textbutton_info[id].text_left, font_nr);
8216
8217   if (textbutton_info[id].text_right)
8218     DrawText(x_right, y, textbutton_info[id].text_right, font_nr);
8219
8220   MapGadget(gi);
8221 }
8222
8223 static void MapGraphicbuttonGadget(int id)
8224 {
8225   int font_nr = FONT_TEXT_1;
8226   int font_height = getFontHeight(font_nr);
8227   struct GadgetInfo *gi = level_editor_gadget[graphicbutton_info[id].gadget_id];
8228   int xoffset_left = getTextWidthForGadget(graphicbutton_info[id].text_left);
8229   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8230   int yoffset = (gi->height - font_height) / 2;
8231   int x_left = gi->x - xoffset_left;
8232   int x_right = gi->x + gi->width + xoffset_right;
8233   int y = gi->y + yoffset;
8234
8235   if (graphicbutton_info[id].text_left)
8236     DrawText(x_left, y, graphicbutton_info[id].text_left, font_nr);
8237
8238   if (graphicbutton_info[id].text_right)
8239     DrawText(x_right, y, graphicbutton_info[id].text_right, font_nr);
8240
8241   MapGadget(gi);
8242 }
8243
8244 static void MapRadiobuttonGadget(int id)
8245 {
8246   int font_nr = FONT_TEXT_1;
8247   int font_height = getFontHeight(font_nr);
8248   struct GadgetInfo *gi = level_editor_gadget[radiobutton_info[id].gadget_id];
8249   int xoffset_left = getTextWidthForGadget(checkbutton_info[id].text_left);
8250   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8251   int yoffset = (gi->height - font_height) / 2;
8252   int x_left = gi->x - xoffset_left;
8253   int x_right = gi->x + gi->width + xoffset_right;
8254   int y = gi->y + yoffset;
8255   boolean checked =
8256     (*radiobutton_info[id].value == radiobutton_info[id].checked_value);
8257
8258   if (radiobutton_info[id].text_left)
8259     DrawText(x_left, y, radiobutton_info[id].text_left, font_nr);
8260
8261   if (radiobutton_info[id].text_right)
8262     DrawText(x_right, y, radiobutton_info[id].text_right, font_nr);
8263
8264   ModifyGadget(gi, GDI_CHECKED, checked, GDI_END);
8265
8266   MapGadget(gi);
8267 }
8268
8269 static void MapCheckbuttonGadget(int id)
8270 {
8271   int font_nr = FONT_TEXT_1;
8272   int font_height = getFontHeight(font_nr);
8273   struct GadgetInfo *gi = level_editor_gadget[checkbutton_info[id].gadget_id];
8274   int xoffset_left = getTextWidthForGadget(checkbutton_info[id].text_left);
8275   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8276   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8277   int yoffset = (gi->height - font_height) / 2;
8278   int y_above = gi->y - yoffset_above;
8279   int x = gi->x;
8280   int x_left, x_right, y;       // set after gadget position was modified
8281
8282   // set position for gadgets with dynamically determined position
8283   if (checkbutton_info[id].x != -1)     // do not change dynamic positions
8284     ModifyGadget(gi, GDI_X, SX + ED_SETTINGS_X(checkbutton_info[id].x), GDI_END);
8285   ModifyGadget(gi, GDI_Y, SY + ED_SETTINGS_Y(checkbutton_info[id].y), GDI_END);
8286
8287   x_left = gi->x - xoffset_left;
8288   x_right = gi->x + gi->width + xoffset_right;
8289   y = gi->y + yoffset;
8290
8291   if (checkbutton_info[id].text_above)
8292     DrawText(x, y_above, checkbutton_info[id].text_above, font_nr);
8293
8294   if (checkbutton_info[id].text_left)
8295     DrawText(x_left, y, checkbutton_info[id].text_left, font_nr);
8296
8297   if (checkbutton_info[id].text_right)
8298     DrawText(x_right, y, checkbutton_info[id].text_right, font_nr);
8299
8300   ModifyGadget(gi, GDI_CHECKED, *checkbutton_info[id].value, GDI_END);
8301
8302   MapGadget(gi);
8303 }
8304
8305 static void MapMainDrawingArea(void)
8306 {
8307   boolean no_horizontal_scrollbar = (lev_fieldx + 2 <= ed_fieldx);
8308   boolean no_vertical_scrollbar = (lev_fieldy + 2 <= ed_fieldy);
8309   int i;
8310
8311   if (suppressBorderElement())
8312   {
8313     no_horizontal_scrollbar = (lev_fieldx <= ed_fieldx);
8314     no_vertical_scrollbar   = (lev_fieldy <= ed_fieldy);
8315   }
8316
8317   for (i = ED_SCROLLBUTTON_ID_AREA_FIRST; i <= ED_SCROLLBUTTON_ID_AREA_LAST; i++)
8318   {
8319     if (((i == ED_SCROLLBUTTON_ID_AREA_LEFT ||
8320           i == ED_SCROLLBUTTON_ID_AREA_RIGHT) &&
8321          no_horizontal_scrollbar) ||
8322         ((i == ED_SCROLLBUTTON_ID_AREA_UP ||
8323           i == ED_SCROLLBUTTON_ID_AREA_DOWN) &&
8324          no_vertical_scrollbar))
8325       continue;
8326
8327     MapGadget(level_editor_gadget[scrollbutton_info[i].gadget_id]);
8328   }
8329
8330   for (i = ED_SCROLLBAR_ID_AREA_FIRST; i <= ED_SCROLLBAR_ID_AREA_LAST; i++)
8331   {
8332     if ((i == ED_SCROLLBAR_ID_AREA_HORIZONTAL && no_horizontal_scrollbar) ||
8333         (i == ED_SCROLLBAR_ID_AREA_VERTICAL && no_vertical_scrollbar))
8334       continue;
8335
8336     MapGadget(level_editor_gadget[scrollbar_info[i].gadget_id]);
8337   }
8338
8339   MapDrawingArea(ED_DRAWING_ID_DRAWING_LEVEL);
8340 }
8341
8342 static void MapOrUnmapLevelEditorToolboxCustomGadgets(boolean map)
8343 {
8344   int i;
8345
8346   for (i = 0; i < ED_NUM_CTRL_BUTTONS; i++)
8347   {
8348     if (i == GADGET_ID_CUSTOM_COPY_FROM ||
8349         i == GADGET_ID_CUSTOM_COPY_TO ||
8350         i == GADGET_ID_CUSTOM_EXCHANGE ||
8351         i == GADGET_ID_CUSTOM_COPY ||
8352         i == GADGET_ID_CUSTOM_PASTE)
8353     {
8354       if (map)
8355         MapGadget(level_editor_gadget[i]);
8356       else
8357         UnmapGadget(level_editor_gadget[i]);
8358     }
8359   }
8360 }
8361
8362 static void MapLevelEditorToolboxCustomGadgets(void)
8363 {
8364   MapOrUnmapLevelEditorToolboxCustomGadgets(TRUE);
8365 }
8366
8367 static void MapLevelEditorToolboxCustomGadgetsIfNeeded(void)
8368 {
8369   if (IS_CUSTOM_ELEMENT(properties_element) ||
8370       IS_GROUP_ELEMENT(properties_element) ||
8371       IS_EMPTY_ELEMENT(properties_element))
8372     MapLevelEditorToolboxCustomGadgets();
8373 }
8374
8375 static void UnmapLevelEditorToolboxCustomGadgets(void)
8376 {
8377   MapOrUnmapLevelEditorToolboxCustomGadgets(FALSE);
8378 }
8379
8380 static void MapOrUnmapLevelEditorToolboxDrawingGadgets(boolean map)
8381 {
8382   int i;
8383
8384   for (i = 0; i < ED_NUM_CTRL1_BUTTONS; i++)
8385   {
8386     if (i != GADGET_ID_SINGLE_ITEMS &&
8387         i != GADGET_ID_PICK_ELEMENT)
8388     {
8389       struct GadgetInfo *gi = level_editor_gadget[i];
8390
8391       if (map)
8392       {
8393         MapGadget(gi);
8394       }
8395       else
8396       {
8397         int graphic = IMG_EDITOR_NO_TOOLBOX_BUTTON;
8398         struct GraphicInfo *gd = &graphic_info[graphic];
8399
8400         UnmapGadget(gi);
8401
8402         BlitBitmap(gd->bitmap, drawto, gd->src_x, gd->src_y,
8403                    gi->width, gi->height, gi->x, gi->y);
8404
8405         redraw_mask |= REDRAW_DOOR_3;
8406       }
8407     }
8408   }
8409 }
8410
8411 static void MapLevelEditorToolboxDrawingGadgets(void)
8412 {
8413   MapOrUnmapLevelEditorToolboxDrawingGadgets(TRUE);
8414 }
8415
8416 static void UnmapLevelEditorToolboxDrawingGadgets(void)
8417 {
8418   MapOrUnmapLevelEditorToolboxDrawingGadgets(FALSE);
8419 }
8420
8421 static void UnmapDrawingArea(int id)
8422 {
8423   UnmapGadget(level_editor_gadget[drawingarea_info[id].gadget_id]);
8424 }
8425
8426 static void UnmapLevelEditorFieldGadgets(void)
8427 {
8428   int i;
8429
8430   for (i = 0; i < num_editor_gadgets; i++)
8431     if (IN_GFX_FIELD_FULL(level_editor_gadget[i]->x,
8432                           level_editor_gadget[i]->y))
8433       UnmapGadget(level_editor_gadget[i]);
8434 }
8435
8436 void UnmapLevelEditorGadgets(void)
8437 {
8438   int i;
8439
8440   for (i = 0; i < num_editor_gadgets; i++)
8441     UnmapGadget(level_editor_gadget[i]);
8442 }
8443
8444 static void ResetUndoBuffer(void)
8445 {
8446   undo_buffer_position = -1;
8447   undo_buffer_steps = -1;
8448   redo_buffer_steps = 0;
8449
8450   CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
8451
8452   level.changed = FALSE;
8453 }
8454
8455 static void DrawEditModeWindowExt(boolean remap_toolbox_gadgets)
8456 {
8457   if (remap_toolbox_gadgets)
8458   {
8459     ModifyEditorElementList();
8460     RedrawDrawingElements();
8461   }
8462
8463   if (edit_mode == ED_MODE_LEVELCONFIG)
8464     DrawLevelConfigWindow();
8465   else if (edit_mode == ED_MODE_PROPERTIES)
8466     DrawPropertiesWindow();
8467   else if (edit_mode == ED_MODE_PALETTE)
8468     DrawPaletteWindow();
8469   else  // edit_mode == ED_MODE_DRAWING
8470     DrawDrawingWindowExt(remap_toolbox_gadgets);
8471 }
8472
8473 static void DrawEditModeWindow(void)
8474 {
8475   DrawEditModeWindowExt(TRUE);
8476 }
8477
8478 static void DrawEditModeWindow_PlayfieldOnly(void)
8479 {
8480   DrawEditModeWindowExt(FALSE);
8481 }
8482
8483 static void ChangeEditModeWindow(int new_edit_mode)
8484 {
8485   edit_mode = (new_edit_mode != edit_mode ? new_edit_mode : ED_MODE_DRAWING);
8486
8487   DrawEditModeWindow();
8488 }
8489
8490 static boolean LevelChanged(void)
8491 {
8492   boolean field_changed = FALSE;
8493   int x, y;
8494
8495   for (y = 0; y < lev_fieldy; y++) 
8496     for (x = 0; x < lev_fieldx; x++)
8497       if (Tile[x][y] != level.field[x][y])
8498         field_changed = TRUE;
8499
8500   return (level.changed || field_changed);
8501 }
8502
8503 static boolean PrepareSavingIntoPersonalLevelSet(void)
8504 {
8505   static LevelDirTree *last_copied_leveldir = NULL;
8506   static LevelDirTree *last_written_leveldir = NULL;
8507   static int last_copied_level_nr = -1;
8508   static int last_written_level_nr = -1;
8509   LevelDirTree *leveldir_former = leveldir_current;
8510   int level_nr_former = level_nr;
8511   int new_level_nr;
8512
8513   // remember last mod/save so that for current session, we write
8514   // back to the same personal copy, asking only about overwrite.
8515   if (leveldir_current == last_copied_leveldir &&
8516       level_nr == last_copied_level_nr)
8517   {
8518     // "cd" to personal level set dir (as used when writing last copy)
8519     leveldir_current = last_written_leveldir;
8520     level_nr = last_written_level_nr;
8521
8522     return TRUE;
8523   }
8524
8525   if (!Request("This level is read-only! "
8526                "Save into personal level set?", REQ_ASK))
8527     return FALSE;
8528
8529   // "cd" to personal level set dir (for writing copy the first time)
8530   leveldir_current =
8531     getTreeInfoFromIdentifier(leveldir_first, getLoginName());
8532
8533   // this may happen if "setup.internal.create_user_levelset" is FALSE
8534   // or if file "levelinfo.conf" is missing in personal user level set
8535   if (leveldir_current == NULL)
8536   {
8537     Request("Cannot find personal level set?!", REQ_CONFIRM);
8538
8539     leveldir_current = leveldir_former;
8540
8541     return FALSE;
8542   }
8543
8544   // find unused level number
8545   for (new_level_nr = leveldir_current->first_level; ; new_level_nr++)
8546   {
8547     static char *level_filename = NULL;
8548
8549     setString(&level_filename, getDefaultLevelFilename(new_level_nr));
8550
8551     if (!fileExists(level_filename))
8552       break;
8553   }
8554
8555   last_copied_leveldir = leveldir_former;
8556   last_copied_level_nr = level_nr_former;
8557
8558   last_written_leveldir = leveldir_current;
8559   last_written_level_nr = level_nr = new_level_nr;
8560
8561   return TRUE;
8562 }
8563
8564 static void ModifyLevelConfigForSavingIntoPersonalLevelSet(char *former_name)
8565 {
8566   static char *filename_levelinfo = NULL, *mod_name = NULL;
8567   FILE *file;
8568
8569   // annotate this copy-and-mod in personal levelinfo.conf
8570   setString(&filename_levelinfo,
8571             getPath2(getCurrentLevelDir(), LEVELINFO_FILENAME));
8572
8573   if ((file = fopen(filename_levelinfo, MODE_APPEND)))
8574   {
8575     fprintf(file, "\n");
8576     fprintf(file, "# level %d was modified from:\n", level_nr);
8577     fprintf(file, "# - previous level set name:    %s\n",
8578             former_name);
8579     fprintf(file, "# - level within previous set:  %d \"%s\"\n",
8580             level.file_info.nr, level.name);
8581     fprintf(file, "# - previous author:            %s\n",
8582             level.author);
8583     fprintf(file, "# - previous save date:         ");
8584
8585     if (level.creation_date.src == DATE_SRC_LEVELFILE)
8586     {
8587       fprintf(file, "%04d-%02d-%02d\n",
8588               level.creation_date.year,
8589               level.creation_date.month,
8590               level.creation_date.day);
8591     }
8592     else
8593     {
8594       fprintf(file, "not recorded\n");
8595     }
8596
8597     fclose(file);
8598   }
8599
8600   if (level_nr > leveldir_current->last_level)
8601     UpdateUserLevelSet(getLoginName(), NULL, NULL, level_nr + 9);
8602
8603   // else: allow the save even if annotation failed
8604
8605   // now... spray graffiti on the old level vital statistics
8606   // user can change these; just trying to set a good baseline
8607
8608   // don't truncate names for fear of making offensive or silly:
8609   // long-named original author only recorded in levelinfo.conf.
8610   // try to fit "Joe after Bob", "Joe (ed.)", then just "Joe"
8611   if (!strEqual(level.author, leveldir_current->author))
8612   {
8613     setString(&mod_name, getStringCat3(leveldir_current->author,
8614                                        " after ", level.author));
8615
8616     if (strlen(mod_name) > MAX_LEVEL_AUTHOR_LEN)
8617       setString(&mod_name,
8618                 getStringCat2(leveldir_current->author, " (ed.)"));
8619
8620     if (strlen(mod_name) > MAX_LEVEL_AUTHOR_LEN)
8621       setString(&mod_name, leveldir_current->author);
8622
8623     strncpy(level.author, mod_name, MAX_LEVEL_AUTHOR_LEN);
8624
8625     // less worried about truncation here
8626     setString(&mod_name, getStringCat2("Mod: ", level.name));
8627     strncpy(level.name, mod_name, MAX_LEVEL_NAME_LEN);
8628   }
8629 }
8630
8631 static void CopyPlayfield(short src[MAX_LEV_FIELDX][MAX_LEV_FIELDY],
8632                           short dst[MAX_LEV_FIELDX][MAX_LEV_FIELDY])
8633 {
8634   int x, y;
8635
8636   for (x = 0; x < lev_fieldx; x++)
8637     for (y = 0; y < lev_fieldy; y++) 
8638       dst[x][y] = src[x][y];
8639 }
8640
8641 static int setSelectboxValue(int selectbox_id, int new_value)
8642 {
8643   int new_index_value = 0;
8644   int i;
8645
8646   for (i = 0; selectbox_info[selectbox_id].options[i].text != NULL; i++)
8647     if (selectbox_info[selectbox_id].options[i].value == new_value)
8648       new_index_value = i;
8649
8650   *selectbox_info[selectbox_id].value =
8651     selectbox_info[selectbox_id].options[new_index_value].value;
8652
8653   return new_index_value;
8654 }
8655
8656 static void setSelectboxSpecialActionVariablesIfNeeded(void)
8657 {
8658   int i;
8659
8660   // change action mode and arg variables according to action type variable
8661   for (i = 0; action_arg_options[i].value != -1; i++)
8662   {
8663     if (action_arg_options[i].value == custom_element_change.action_type)
8664     {
8665       int mode = action_arg_options[i].mode;
8666
8667       // only change if corresponding selectbox has changed
8668       if (selectbox_info[ED_SELECTBOX_ID_ACTION_MODE].options !=
8669           action_arg_modes[mode])
8670         custom_element_change.action_mode = -1;
8671
8672       // only change if corresponding selectbox has changed
8673       if (selectbox_info[ED_SELECTBOX_ID_ACTION_ARG].options !=
8674           action_arg_options[i].options)
8675         custom_element_change.action_arg = -1;
8676
8677       break;
8678     }
8679   }
8680 }
8681
8682 static void setSelectboxSpecialActionOptions(void)
8683 {
8684   int i;
8685
8686   // change action mode and arg selectbox according to action type selectbox
8687   for (i = 0; action_arg_options[i].value != -1; i++)
8688   {
8689     if (action_arg_options[i].value == custom_element_change.action_type)
8690     {
8691       int mode = action_arg_options[i].mode;
8692
8693       ModifyEditorSelectboxOptions(ED_SELECTBOX_ID_ACTION_MODE,
8694                                    action_arg_modes[mode]);
8695       ModifyEditorSelectboxValue(ED_SELECTBOX_ID_ACTION_MODE,
8696                                  custom_element_change.action_mode);
8697
8698       ModifyEditorSelectboxOptions(ED_SELECTBOX_ID_ACTION_ARG,
8699                                    action_arg_options[i].options);
8700       ModifyEditorSelectboxValue(ED_SELECTBOX_ID_ACTION_ARG,
8701                                  custom_element_change.action_arg);
8702       break;
8703     }
8704   }
8705 }
8706
8707 static void copy_custom_element_settings(int element_from, int element_to)
8708 {
8709   struct ElementInfo *ei_from = &element_info[element_from];
8710   struct ElementInfo *ei_to = &element_info[element_to];
8711
8712   copyElementInfo(ei_from, ei_to);
8713 }
8714
8715 static void replace_custom_element_in_settings(int element_from,
8716                                                int element_to)
8717 {
8718   int i, j, x, y;
8719
8720   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
8721   {
8722     struct ElementInfo *ei = &element_info[i];
8723
8724     for (y = 0; y < 3; y++)
8725       for (x = 0; x < 3; x++)
8726         if (ei->content.e[x][y] == element_from)
8727           ei->content.e[x][y] = element_to;
8728
8729     for (j = 0; j < ei->num_change_pages; j++)
8730     {
8731       struct ElementChangeInfo *change = &ei->change_page[j];
8732
8733       if (change->target_element == element_from)
8734         change->target_element = element_to;
8735
8736       if (change->initial_trigger_element == element_from)
8737         change->initial_trigger_element = element_to;
8738
8739       if (change->action_element == element_from)
8740         change->action_element = element_to;
8741
8742       for (y = 0; y < 3; y++)
8743         for (x = 0; x < 3; x++)
8744           if (change->target_content.e[x][y] == element_from)
8745             change->target_content.e[x][y] = element_to;
8746     }
8747
8748     if (ei->group != NULL)                              // group or internal
8749       for (j = 0; j < MAX_ELEMENTS_IN_GROUP; j++)
8750         if (ei->group->element[j] == element_from)
8751           ei->group->element[j] = element_to;
8752   }
8753 }
8754
8755 static void replace_custom_element_in_playfield(int element_from,
8756                                                 int element_to)
8757 {
8758   int x, y;
8759
8760   for (x = 0; x < lev_fieldx; x++)
8761     for (y = 0; y < lev_fieldy; y++)
8762       if (Tile[x][y] == element_from)
8763         Tile[x][y] = element_to;
8764 }
8765
8766 static boolean CopyCustomElement(int element_old, int element_new,
8767                                  int copy_mode)
8768 {
8769   int copy_mode_orig = copy_mode;
8770
8771   if (copy_mode == GADGET_ID_CUSTOM_COPY)
8772   {
8773     element_new = (IS_CUSTOM_ELEMENT(element_old) ?
8774                    EL_INTERNAL_CLIPBOARD_CUSTOM : EL_INTERNAL_CLIPBOARD_GROUP);
8775     copy_mode = GADGET_ID_CUSTOM_COPY_TO;
8776   }
8777   else if (copy_mode == GADGET_ID_CUSTOM_PASTE)
8778   {
8779     element_old = (IS_CUSTOM_ELEMENT(element_new) ?
8780                    EL_INTERNAL_CLIPBOARD_CUSTOM : EL_INTERNAL_CLIPBOARD_GROUP);
8781     copy_mode = GADGET_ID_CUSTOM_COPY_TO;
8782
8783     level.changed = TRUE;
8784   }
8785   else if (IS_CUSTOM_ELEMENT(element_old) && !IS_CUSTOM_ELEMENT(element_new))
8786   {
8787     Request("Please choose custom element!", REQ_CONFIRM);
8788
8789     return FALSE;
8790   }
8791   else if (IS_GROUP_ELEMENT(element_old) && !IS_GROUP_ELEMENT(element_new))
8792   {
8793     Request("Please choose group element!", REQ_CONFIRM);
8794
8795     return FALSE;
8796   }
8797   else if (IS_EMPTY_ELEMENT(element_old) && !IS_EMPTY_ELEMENT(element_new))
8798   {
8799     Request("Please choose empty element!", REQ_CONFIRM);
8800
8801     return FALSE;
8802   }
8803   else
8804   {
8805     level.changed = TRUE;
8806   }
8807
8808   // when modifying custom/group element, ask for copying level template
8809   if (copy_mode_orig != GADGET_ID_CUSTOM_COPY && level.use_custom_template)
8810   {
8811     if (!AskToCopyAndModifyLevelTemplate())
8812       return FALSE;
8813   }
8814
8815   if (copy_mode == GADGET_ID_CUSTOM_COPY_FROM)
8816   {
8817     copy_custom_element_settings(element_new, element_old);
8818   }
8819   else if (copy_mode == GADGET_ID_CUSTOM_COPY_TO)
8820   {
8821     copy_custom_element_settings(element_old, element_new);
8822   }
8823   else if (copy_mode == GADGET_ID_CUSTOM_EXCHANGE)
8824   {
8825     copy_custom_element_settings(element_old, EL_INTERNAL_DUMMY);
8826     copy_custom_element_settings(element_new, element_old);
8827     copy_custom_element_settings(EL_INTERNAL_DUMMY, element_new);
8828
8829     replace_custom_element_in_settings(element_old, EL_INTERNAL_DUMMY);
8830     replace_custom_element_in_settings(element_new, element_old);
8831     replace_custom_element_in_settings(EL_INTERNAL_DUMMY, element_new);
8832
8833     replace_custom_element_in_playfield(element_old, EL_INTERNAL_DUMMY);
8834     replace_custom_element_in_playfield(element_new, element_old);
8835     replace_custom_element_in_playfield(EL_INTERNAL_DUMMY, element_new);
8836   }
8837
8838   UpdateCustomElementGraphicGadgets();
8839   DrawPropertiesWindow();
8840
8841   return TRUE;
8842 }
8843
8844 static void CopyCustomElementPropertiesToEditor(int element)
8845 {
8846   int i;
8847   int current_change_page = element_info[element].current_change_page;
8848
8849   // dynamically (re)build selectbox for selecting change page
8850   for (i = 0; i < element_info[element].num_change_pages; i++)
8851   {
8852     sprintf(options_change_page_strings[i], "%d", i + 1);
8853
8854     options_change_page[i].value = i;
8855     options_change_page[i].text = options_change_page_strings[i];
8856   }
8857
8858   options_change_page[i].value = -1;
8859   options_change_page[i].text = NULL;
8860
8861   // needed here to initialize combined element properties
8862   InitElementPropertiesEngine(level.game_version);
8863
8864   element_info[element].change =
8865     &element_info[element].change_page[current_change_page];
8866
8867   custom_element = element_info[element];
8868   custom_element_change = *element_info[element].change;
8869
8870   // needed to initially set selectbox options for special action options
8871   setSelectboxSpecialActionOptions();
8872
8873   // needed to initially set selectbox value variables to reliable defaults
8874   for (i = 0; i < ED_NUM_SELECTBOX; i++)
8875     setSelectboxValue(i, *selectbox_info[i].value);
8876
8877   for (i = 0; i < NUM_ELEMENT_PROPERTIES; i++)
8878     custom_element_properties[i] = HAS_PROPERTY(element, i);
8879
8880   for (i = 0; i < NUM_CHANGE_EVENTS; i++)
8881     custom_element_change_events[i] = HAS_CHANGE_EVENT(element, i);
8882
8883   // ---------- element settings: configure (custom elements) -----------------
8884
8885   // set accessible layer selectbox help value
8886   custom_element.access_type =
8887     (IS_WALKABLE(element) ? EP_WALKABLE :
8888      IS_PASSABLE(element) ? EP_PASSABLE :
8889      custom_element.access_type);
8890   custom_element.access_layer =
8891     (IS_ACCESSIBLE_OVER(element) ? EP_ACCESSIBLE_OVER :
8892      IS_ACCESSIBLE_INSIDE(element) ? EP_ACCESSIBLE_INSIDE :
8893      IS_ACCESSIBLE_UNDER(element) ? EP_ACCESSIBLE_UNDER :
8894      custom_element.access_layer);
8895   custom_element.access_protected =
8896     (IS_PROTECTED(element) ? 1 : 0);
8897   custom_element_properties[EP_ACCESSIBLE] =
8898     (IS_ACCESSIBLE_OVER(element) ||
8899      IS_ACCESSIBLE_INSIDE(element) ||
8900      IS_ACCESSIBLE_UNDER(element));
8901
8902   // set walk-to-object action selectbox help value
8903   custom_element.walk_to_action =
8904     (IS_DIGGABLE(element) ? EP_DIGGABLE :
8905      IS_COLLECTIBLE_ONLY(element) ? EP_COLLECTIBLE_ONLY :
8906      IS_DROPPABLE(element) ? EP_DROPPABLE :
8907      IS_THROWABLE(element) ? EP_THROWABLE :
8908      IS_PUSHABLE(element) ? EP_PUSHABLE :
8909      custom_element.walk_to_action);
8910   custom_element_properties[EP_WALK_TO_OBJECT] =
8911     (IS_DIGGABLE(element) ||
8912      IS_COLLECTIBLE_ONLY(element) ||
8913      IS_DROPPABLE(element) ||
8914      IS_THROWABLE(element) ||
8915      IS_PUSHABLE(element));
8916
8917   // set smash targets selectbox help value
8918   custom_element.smash_targets =
8919     (CAN_SMASH_EVERYTHING(element) ? EP_CAN_SMASH_EVERYTHING :
8920      CAN_SMASH_ENEMIES(element) ? EP_CAN_SMASH_ENEMIES :
8921      CAN_SMASH_PLAYER(element) ? EP_CAN_SMASH_PLAYER :
8922      custom_element.smash_targets);
8923   custom_element_properties[EP_CAN_SMASH] =
8924     (CAN_SMASH_EVERYTHING(element) ||
8925      CAN_SMASH_ENEMIES(element) ||
8926      CAN_SMASH_PLAYER(element));
8927
8928   // set deadliness selectbox help value
8929   custom_element.deadliness =
8930     (DONT_TOUCH(element) ? EP_DONT_TOUCH :
8931      DONT_GET_HIT_BY(element) ? EP_DONT_GET_HIT_BY :
8932      DONT_COLLIDE_WITH(element) ? EP_DONT_COLLIDE_WITH :
8933      DONT_RUN_INTO(element) ? EP_DONT_RUN_INTO :
8934      custom_element.deadliness);
8935   custom_element_properties[EP_DEADLY] =
8936     (DONT_TOUCH(element) ||
8937      DONT_GET_HIT_BY(element) ||
8938      DONT_COLLIDE_WITH(element) ||
8939      DONT_RUN_INTO(element));
8940
8941   // ---------- element settings: advanced (custom elements) ------------------
8942
8943   // set "change by direct action" selectbox help value
8944   custom_element_change.direct_action =
8945     (HAS_CHANGE_EVENT(element, CE_NEXT_TO_PLAYER) ? CE_NEXT_TO_PLAYER :
8946      HAS_CHANGE_EVENT(element, CE_TOUCHED_BY_PLAYER) ? CE_TOUCHED_BY_PLAYER :
8947      HAS_CHANGE_EVENT(element, CE_PRESSED_BY_PLAYER) ? CE_PRESSED_BY_PLAYER :
8948      HAS_CHANGE_EVENT(element, CE_SWITCHED_BY_PLAYER) ? CE_SWITCHED_BY_PLAYER :
8949      HAS_CHANGE_EVENT(element, CE_SNAPPED_BY_PLAYER) ? CE_SNAPPED_BY_PLAYER :
8950      HAS_CHANGE_EVENT(element, CE_PUSHED_BY_PLAYER) ? CE_PUSHED_BY_PLAYER :
8951      HAS_CHANGE_EVENT(element, CE_ENTERED_BY_PLAYER) ? CE_ENTERED_BY_PLAYER :
8952      HAS_CHANGE_EVENT(element, CE_LEFT_BY_PLAYER) ? CE_LEFT_BY_PLAYER :
8953      HAS_CHANGE_EVENT(element, CE_DROPPED_BY_PLAYER) ? CE_DROPPED_BY_PLAYER :
8954      HAS_CHANGE_EVENT(element, CE_SWITCHED) ? CE_SWITCHED :
8955      HAS_CHANGE_EVENT(element, CE_HITTING_SOMETHING) ? CE_HITTING_SOMETHING :
8956      HAS_CHANGE_EVENT(element, CE_HIT_BY_SOMETHING) ? CE_HIT_BY_SOMETHING :
8957      HAS_CHANGE_EVENT(element, CE_BLOCKED) ? CE_BLOCKED :
8958      HAS_CHANGE_EVENT(element, CE_IMPACT) ? CE_IMPACT :
8959      HAS_CHANGE_EVENT(element, CE_SMASHED) ? CE_SMASHED :
8960      HAS_CHANGE_EVENT(element, CE_VALUE_CHANGES) ? CE_VALUE_CHANGES :
8961      HAS_CHANGE_EVENT(element, CE_SCORE_CHANGES) ? CE_SCORE_CHANGES :
8962      HAS_CHANGE_EVENT(element, CE_VALUE_GETS_ZERO) ? CE_VALUE_GETS_ZERO :
8963      HAS_CHANGE_EVENT(element, CE_SCORE_GETS_ZERO) ? CE_SCORE_GETS_ZERO :
8964      HAS_CHANGE_EVENT(element, CE_CLICKED_BY_MOUSE) ? CE_CLICKED_BY_MOUSE :
8965      HAS_CHANGE_EVENT(element, CE_PRESSED_BY_MOUSE) ? CE_PRESSED_BY_MOUSE :
8966      custom_element_change.direct_action);
8967
8968   // set "change by other element action" selectbox help value
8969   custom_element_change.other_action =
8970     (HAS_CHANGE_EVENT(element, CE_PLAYER_NEXT_TO_X) ? CE_PLAYER_NEXT_TO_X :
8971      HAS_CHANGE_EVENT(element, CE_PLAYER_TOUCHES_X) ? CE_PLAYER_TOUCHES_X :
8972      HAS_CHANGE_EVENT(element, CE_PLAYER_PRESSES_X) ? CE_PLAYER_PRESSES_X :
8973      HAS_CHANGE_EVENT(element, CE_PLAYER_SWITCHES_X) ? CE_PLAYER_SWITCHES_X :
8974      HAS_CHANGE_EVENT(element, CE_PLAYER_SNAPS_X) ? CE_PLAYER_SNAPS_X :
8975      HAS_CHANGE_EVENT(element, CE_PLAYER_PUSHES_X) ? CE_PLAYER_PUSHES_X :
8976      HAS_CHANGE_EVENT(element, CE_PLAYER_ENTERS_X) ? CE_PLAYER_ENTERS_X :
8977      HAS_CHANGE_EVENT(element, CE_PLAYER_LEAVES_X) ? CE_PLAYER_LEAVES_X :
8978      HAS_CHANGE_EVENT(element, CE_PLAYER_DIGS_X) ? CE_PLAYER_DIGS_X :
8979      HAS_CHANGE_EVENT(element, CE_PLAYER_COLLECTS_X) ? CE_PLAYER_COLLECTS_X :
8980      HAS_CHANGE_EVENT(element, CE_PLAYER_DROPS_X) ? CE_PLAYER_DROPS_X :
8981      HAS_CHANGE_EVENT(element, CE_NEXT_TO_X) ? CE_NEXT_TO_X :
8982      HAS_CHANGE_EVENT(element, CE_TOUCHING_X) ? CE_TOUCHING_X :
8983      HAS_CHANGE_EVENT(element, CE_HITTING_X) ? CE_HITTING_X :
8984      HAS_CHANGE_EVENT(element, CE_DIGGING_X) ? CE_DIGGING_X :
8985      HAS_CHANGE_EVENT(element, CE_HIT_BY_X) ? CE_HIT_BY_X :
8986      HAS_CHANGE_EVENT(element, CE_SWITCH_OF_X) ? CE_SWITCH_OF_X :
8987      HAS_CHANGE_EVENT(element, CE_CHANGE_OF_X) ? CE_CHANGE_OF_X :
8988      HAS_CHANGE_EVENT(element, CE_EXPLOSION_OF_X) ? CE_EXPLOSION_OF_X :
8989      HAS_CHANGE_EVENT(element, CE_MOVE_OF_X) ? CE_MOVE_OF_X :
8990      HAS_CHANGE_EVENT(element, CE_CREATION_OF_X) ? CE_CREATION_OF_X :
8991      HAS_CHANGE_EVENT(element, CE_VALUE_CHANGES_OF_X) ? CE_VALUE_CHANGES_OF_X :
8992      HAS_CHANGE_EVENT(element, CE_SCORE_CHANGES_OF_X) ? CE_SCORE_CHANGES_OF_X :
8993      HAS_CHANGE_EVENT(element, CE_VALUE_GETS_ZERO_OF_X) ? CE_VALUE_GETS_ZERO_OF_X :
8994      HAS_CHANGE_EVENT(element, CE_SCORE_GETS_ZERO_OF_X) ? CE_SCORE_GETS_ZERO_OF_X :
8995      HAS_CHANGE_EVENT(element, CE_MOUSE_CLICKED_ON_X) ? CE_MOUSE_CLICKED_ON_X :
8996      HAS_CHANGE_EVENT(element, CE_MOUSE_PRESSED_ON_X) ? CE_MOUSE_PRESSED_ON_X :
8997      custom_element_change.other_action);
8998 }
8999
9000 static void CopyGroupElementPropertiesToEditor(int element)
9001 {
9002   group_element_info = *element_info[element].group;
9003   custom_element = element_info[element];       // needed for description
9004 }
9005
9006 static void CopyEmptyElementPropertiesToEditor(int element)
9007 {
9008   custom_element = element_info[element];
9009 }
9010
9011 static void CopyClassicElementPropertiesToEditor(int element)
9012 {
9013   if (IS_PLAYER_ELEMENT(element) || COULD_MOVE_INTO_ACID(element))
9014     custom_element_properties[EP_CAN_MOVE_INTO_ACID] =
9015       getMoveIntoAcidProperty(&level, element);
9016
9017   if (MAYBE_DONT_COLLIDE_WITH(element))
9018     custom_element_properties[EP_DONT_COLLIDE_WITH] =
9019       getDontCollideWithProperty(&level, element);
9020 }
9021
9022 static void CopyElementPropertiesToEditor(int element)
9023 {
9024   if (IS_CUSTOM_ELEMENT(element))
9025     CopyCustomElementPropertiesToEditor(element);
9026   else if (IS_GROUP_ELEMENT(element))
9027     CopyGroupElementPropertiesToEditor(element);
9028   else if (IS_EMPTY_ELEMENT(element))
9029     CopyEmptyElementPropertiesToEditor(element);
9030   else
9031     CopyClassicElementPropertiesToEditor(element);
9032 }
9033
9034 static boolean AskToCopyAndModifyLevelTemplate(void)
9035 {
9036   if (Request("Copy and modify settings from level template?", REQ_ASK))
9037   {
9038     level.use_custom_template = FALSE;
9039
9040     ModifyGadget(level_editor_gadget[GADGET_ID_CUSTOM_USE_TEMPLATE_1],
9041                  GDI_CHECKED, FALSE, GDI_END);
9042     ModifyGadget(level_editor_gadget[GADGET_ID_CUSTOM_USE_TEMPLATE_2],
9043                  GDI_CHECKED, FALSE, GDI_END);
9044
9045     return TRUE;
9046   }
9047   else
9048   {
9049     LoadLevelTemplate(-1);      // this resets all element modifications ...
9050
9051     DrawEditModeWindow();       // ... and copies them to 'custom_element'
9052
9053     return FALSE;
9054   }
9055 }
9056
9057 static void CopyCustomElementPropertiesToGame(int element)
9058 {
9059   int i;
9060   int access_type_and_layer;
9061
9062   // mark that this custom element has been modified
9063   custom_element.modified_settings = TRUE;
9064   level.changed = TRUE;
9065
9066   if (level.use_custom_template)
9067     AskToCopyAndModifyLevelTemplate();
9068
9069   element_info[element] = custom_element;
9070   *element_info[element].change = custom_element_change;
9071
9072   // ---------- element settings: configure (custom elements) -----------------
9073
9074   // set accessible property from checkbox and selectbox
9075   custom_element_properties[EP_WALKABLE_OVER] = FALSE;
9076   custom_element_properties[EP_WALKABLE_INSIDE] = FALSE;
9077   custom_element_properties[EP_WALKABLE_UNDER] = FALSE;
9078   custom_element_properties[EP_PASSABLE_OVER] = FALSE;
9079   custom_element_properties[EP_PASSABLE_INSIDE] = FALSE;
9080   custom_element_properties[EP_PASSABLE_UNDER] = FALSE;
9081   access_type_and_layer = ((custom_element.access_type == EP_WALKABLE ?
9082                             EP_WALKABLE_OVER : EP_PASSABLE_OVER) +
9083                            (custom_element.access_layer - EP_ACCESSIBLE_OVER));
9084   custom_element_properties[access_type_and_layer] =
9085     custom_element_properties[EP_ACCESSIBLE];
9086   custom_element_properties[EP_PROTECTED] =
9087     (custom_element.access_protected != 0 &&
9088      custom_element_properties[EP_ACCESSIBLE]);
9089
9090   // set walk-to-object property from checkbox and selectbox
9091   custom_element_properties[EP_DIGGABLE] = FALSE;
9092   custom_element_properties[EP_COLLECTIBLE_ONLY] = FALSE;
9093   custom_element_properties[EP_DROPPABLE] = FALSE;
9094   custom_element_properties[EP_THROWABLE] = FALSE;
9095   custom_element_properties[EP_PUSHABLE] = FALSE;
9096   custom_element_properties[custom_element.walk_to_action] =
9097     custom_element_properties[EP_WALK_TO_OBJECT];
9098
9099   // set smash property from checkbox and selectbox
9100   custom_element_properties[EP_CAN_SMASH_PLAYER] = FALSE;
9101   custom_element_properties[EP_CAN_SMASH_ENEMIES] = FALSE;
9102   custom_element_properties[EP_CAN_SMASH_EVERYTHING] = FALSE;
9103   custom_element_properties[custom_element.smash_targets] =
9104     custom_element_properties[EP_CAN_SMASH];
9105
9106   // set deadliness property from checkbox and selectbox
9107   custom_element_properties[EP_DONT_RUN_INTO] = FALSE;
9108   custom_element_properties[EP_DONT_COLLIDE_WITH] = FALSE;
9109   custom_element_properties[EP_DONT_GET_HIT_BY] = FALSE;
9110   custom_element_properties[EP_DONT_TOUCH] = FALSE;
9111   custom_element_properties[custom_element.deadliness] =
9112     custom_element_properties[EP_DEADLY];
9113
9114   // ---------- element settings: advanced (custom elements) ------------------
9115
9116   // set player change event from checkbox and selectbox
9117   custom_element_change_events[CE_NEXT_TO_PLAYER] = FALSE;
9118   custom_element_change_events[CE_TOUCHED_BY_PLAYER] = FALSE;
9119   custom_element_change_events[CE_PRESSED_BY_PLAYER] = FALSE;
9120   custom_element_change_events[CE_SWITCHED_BY_PLAYER] = FALSE;
9121   custom_element_change_events[CE_SNAPPED_BY_PLAYER] = FALSE;
9122   custom_element_change_events[CE_PUSHED_BY_PLAYER] = FALSE;
9123   custom_element_change_events[CE_ENTERED_BY_PLAYER] = FALSE;
9124   custom_element_change_events[CE_LEFT_BY_PLAYER] = FALSE;
9125   custom_element_change_events[CE_DROPPED_BY_PLAYER] = FALSE;
9126   custom_element_change_events[CE_SWITCHED] = FALSE;
9127   custom_element_change_events[CE_HITTING_SOMETHING] = FALSE;
9128   custom_element_change_events[CE_HIT_BY_SOMETHING] = FALSE;
9129   custom_element_change_events[CE_BLOCKED] = FALSE;
9130   custom_element_change_events[CE_IMPACT] = FALSE;
9131   custom_element_change_events[CE_SMASHED] = FALSE;
9132   custom_element_change_events[CE_VALUE_CHANGES] = FALSE;
9133   custom_element_change_events[CE_SCORE_CHANGES] = FALSE;
9134   custom_element_change_events[CE_VALUE_GETS_ZERO] = FALSE;
9135   custom_element_change_events[CE_SCORE_GETS_ZERO] = FALSE;
9136   custom_element_change_events[CE_CLICKED_BY_MOUSE] = FALSE;
9137   custom_element_change_events[CE_PRESSED_BY_MOUSE] = FALSE;
9138   custom_element_change_events[custom_element_change.direct_action] =
9139     custom_element_change_events[CE_BY_DIRECT_ACTION];
9140
9141   // set other element action change event from checkbox and selectbox
9142   custom_element_change_events[CE_PLAYER_NEXT_TO_X] = FALSE;
9143   custom_element_change_events[CE_PLAYER_TOUCHES_X] = FALSE;
9144   custom_element_change_events[CE_PLAYER_PRESSES_X] = FALSE;
9145   custom_element_change_events[CE_PLAYER_SWITCHES_X] = FALSE;
9146   custom_element_change_events[CE_PLAYER_SNAPS_X] = FALSE;
9147   custom_element_change_events[CE_PLAYER_PUSHES_X] = FALSE;
9148   custom_element_change_events[CE_PLAYER_ENTERS_X] = FALSE;
9149   custom_element_change_events[CE_PLAYER_LEAVES_X] = FALSE;
9150   custom_element_change_events[CE_PLAYER_DIGS_X] = FALSE;
9151   custom_element_change_events[CE_PLAYER_COLLECTS_X] = FALSE;
9152   custom_element_change_events[CE_PLAYER_DROPS_X] = FALSE;
9153   custom_element_change_events[CE_NEXT_TO_X] = FALSE;
9154   custom_element_change_events[CE_TOUCHING_X] = FALSE;
9155   custom_element_change_events[CE_HITTING_X] = FALSE;
9156   custom_element_change_events[CE_DIGGING_X] = FALSE;
9157   custom_element_change_events[CE_HIT_BY_X] = FALSE;
9158   custom_element_change_events[CE_SWITCH_OF_X] = FALSE;
9159   custom_element_change_events[CE_CHANGE_OF_X] = FALSE;
9160   custom_element_change_events[CE_EXPLOSION_OF_X] = FALSE;
9161   custom_element_change_events[CE_MOVE_OF_X] = FALSE;
9162   custom_element_change_events[CE_CREATION_OF_X] = FALSE;
9163   custom_element_change_events[CE_VALUE_CHANGES_OF_X] = FALSE;
9164   custom_element_change_events[CE_SCORE_CHANGES_OF_X] = FALSE;
9165   custom_element_change_events[CE_VALUE_GETS_ZERO_OF_X] = FALSE;
9166   custom_element_change_events[CE_SCORE_GETS_ZERO_OF_X] = FALSE;
9167   custom_element_change_events[CE_MOUSE_CLICKED_ON_X] = FALSE;
9168   custom_element_change_events[CE_MOUSE_PRESSED_ON_X] = FALSE;
9169   custom_element_change_events[custom_element_change.other_action] =
9170     custom_element_change_events[CE_BY_OTHER_ACTION];
9171
9172   for (i = 0; i < NUM_ELEMENT_PROPERTIES; i++)
9173     SET_PROPERTY(element, i, custom_element_properties[i]);
9174
9175   for (i = 0; i < NUM_CHANGE_EVENTS; i++)
9176     SET_CHANGE_EVENT(element, i, custom_element_change_events[i]);
9177
9178   // copy change events also to special level editor variable
9179   custom_element = element_info[element];
9180   custom_element_change = *element_info[element].change;
9181
9182   // needed here to restore runtime value "element_info[element].gfx_element"
9183   InitElementPropertiesGfxElement();
9184 }
9185
9186 static void CopyGroupElementPropertiesToGame(int element)
9187 {
9188   // mark that this group element has been modified
9189   custom_element.modified_settings = TRUE;
9190   level.changed = TRUE;
9191
9192   if (level.use_custom_template)
9193     AskToCopyAndModifyLevelTemplate();
9194
9195   element_info[element] = custom_element;
9196   *element_info[element].group = group_element_info;
9197
9198   // needed here to restore runtime value "element_info[element].gfx_element"
9199   InitElementPropertiesGfxElement();
9200 }
9201
9202 static void CopyEmptyElementPropertiesToGame(int element)
9203 {
9204   // mark that this empty element has been modified
9205   custom_element.modified_settings = TRUE;
9206   level.changed = TRUE;
9207
9208   if (level.use_custom_template)
9209     AskToCopyAndModifyLevelTemplate();
9210
9211   element_info[element] = custom_element;
9212
9213   // needed here to restore runtime value "element_info[element].gfx_element"
9214   InitElementPropertiesGfxElement();
9215 }
9216
9217 static void CopyClassicElementPropertiesToGame(int element)
9218 {
9219   if (IS_PLAYER_ELEMENT(element) || COULD_MOVE_INTO_ACID(element))
9220     setMoveIntoAcidProperty(&level, element,
9221                             custom_element_properties[EP_CAN_MOVE_INTO_ACID]);
9222
9223   if (MAYBE_DONT_COLLIDE_WITH(element))
9224     setDontCollideWithProperty(&level, element,
9225                               custom_element_properties[EP_DONT_COLLIDE_WITH]);
9226 }
9227
9228 static void CopyElementPropertiesToGame(int element)
9229 {
9230   if (IS_CUSTOM_ELEMENT(element))
9231     CopyCustomElementPropertiesToGame(element);
9232   else if (IS_GROUP_ELEMENT(element))
9233     CopyGroupElementPropertiesToGame(element);
9234   else if (IS_EMPTY_ELEMENT(element))
9235     CopyEmptyElementPropertiesToGame(element);
9236   else
9237     CopyClassicElementPropertiesToGame(element);
9238 }
9239
9240 #if DEBUG
9241 static void CheckElementDescriptions(void)
9242 {
9243   int i;
9244
9245   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
9246     if (getElementDescriptionFilename(i) == NULL && !IS_OBSOLETE(i))
9247       Warn("no element description file for element '%s'", EL_NAME(i));
9248 }
9249 #endif
9250
9251 static int getMaxEdFieldX(boolean has_scrollbar)
9252 {
9253   int scrollbar_width = (has_scrollbar ? ED_SCROLLBUTTON_XSIZE : 0);
9254   int sxsize = SXSIZE - scrollbar_width;
9255   int max_ed_fieldx = sxsize / ed_tilesize;
9256
9257   return max_ed_fieldx;
9258 }
9259
9260 static int getMaxEdFieldY(boolean has_scrollbar)
9261 {
9262   int infotext_height = (IN_PIX_FIELD(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY) ?
9263                          INFOTEXT_YSIZE_FULL : 0);
9264   int scrollbar_height = (has_scrollbar ? ED_SCROLLBUTTON_YSIZE : 0);
9265   int sysize = SYSIZE - scrollbar_height - infotext_height;
9266   int max_ed_fieldy = sysize / ed_tilesize;
9267
9268   return max_ed_fieldy;
9269 }
9270
9271 static void InitZoomLevelSettings(int zoom_tilesize)
9272 {
9273   static int last_game_engine_type = GAME_ENGINE_TYPE_UNKNOWN;
9274
9275   if (zoom_tilesize == -1 && level.game_engine_type != last_game_engine_type)
9276   {
9277     ed_tilesize = setup.auto_setup.editor_zoom_tilesize;
9278     ed_tilesize_default = DEFAULT_EDITOR_TILESIZE;
9279
9280     // make sure that tile size is always a power of 2
9281     ed_tilesize = (1 << log_2(ed_tilesize));
9282
9283     if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
9284     {
9285       ed_tilesize = DEFAULT_EDITOR_TILESIZE_MM;
9286       ed_tilesize_default = DEFAULT_EDITOR_TILESIZE_MM;
9287     }
9288   }
9289
9290   last_game_engine_type = level.game_engine_type;
9291
9292   // limit zoom tilesize by upper and lower bound
9293   ed_tilesize = MIN(MAX(MICRO_TILESIZE, ed_tilesize), TILESIZE);
9294
9295   // store zoom tilesize in auto setup file only if it was manually changed
9296   if (zoom_tilesize != -1)
9297     setup.auto_setup.editor_zoom_tilesize = ed_tilesize;
9298
9299   MAX_ED_FIELDX = getMaxEdFieldX(FALSE);
9300   MAX_ED_FIELDY = getMaxEdFieldY(FALSE);
9301 }
9302
9303 static void InitDrawingElements(void)
9304 {
9305   static int game_engine_type_last = GAME_ENGINE_TYPE_UNKNOWN;
9306
9307   if (level.game_engine_type == game_engine_type_last)
9308     return;
9309
9310   if (level.game_engine_type == GAME_ENGINE_TYPE_BD)
9311   {
9312     new_element1 = EL_BD_WALL;
9313     new_element2 = EL_EMPTY;
9314     new_element3 = EL_BD_SAND;
9315   }
9316   else if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
9317   {
9318     new_element1 = EL_SP_CHIP_SINGLE;
9319     new_element2 = EL_EMPTY;
9320     new_element3 = EL_SP_BASE;
9321   }
9322   else if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
9323   {
9324     new_element1 = EL_MM_MIRROR_START;
9325     new_element2 = EL_EMPTY;
9326     new_element3 = EL_MM_WOODEN_WALL;
9327   }
9328   else
9329   {
9330     new_element1 = EL_WALL;
9331     new_element2 = EL_EMPTY;
9332     new_element3 = EL_SAND;
9333   }
9334
9335   game_engine_type_last = level.game_engine_type;
9336 }
9337
9338 static void InitLevelSetInfo(void)
9339 {
9340   snprintf(levelset_name,   MAX_LEVEL_NAME_LEN + 1,
9341            "%s", leveldir_current->name);
9342   snprintf(levelset_author, MAX_LEVEL_AUTHOR_LEN + 1,
9343            "%s", leveldir_current->author);
9344
9345   levelset_num_levels = leveldir_current->levels;
9346
9347   levelset_use_levelset_artwork = FALSE;
9348   levelset_copy_level_template = FALSE;
9349
9350   levelset_save_mode = LEVELSET_SAVE_MODE_UPDATE;
9351 }
9352
9353 static void ChangeEditorToLevelSet(char *levelset_subdir)
9354 {
9355   leveldir_current = getTreeInfoFromIdentifier(leveldir_first, levelset_subdir);
9356
9357   // the previous level set might have used custom artwork
9358   ReloadCustomArtwork(0);
9359
9360   LoadLevelSetup_SeriesInfo();
9361
9362   SaveLevelSetup_LastSeries();
9363   SaveLevelSetup_SeriesInfo();
9364
9365   TapeErase();
9366
9367   LoadLevel(level_nr);
9368   LoadScore(level_nr);
9369
9370   DrawLevelEd();
9371 }
9372
9373 static boolean useEditorDoorAnimation(void)
9374 {
9375   struct RectWithBorder *vp_door_1 = &viewport.door_1[GAME_MODE_MAIN];
9376   boolean door_1_viewport_unchanged =
9377     (vp_door_1->x      == DX     &&
9378      vp_door_1->y      == DY     &&
9379      vp_door_1->width  == DXSIZE &&
9380      vp_door_1->height == DYSIZE);
9381   boolean door_1_contains_toolbox =
9382     (EX >= DX &&
9383      EY >= DY &&
9384      EX + EXSIZE <= DX + DXSIZE &&
9385      EY + EYSIZE <= DY + DYSIZE);
9386
9387   return (door_1_viewport_unchanged && door_1_contains_toolbox);
9388 }
9389
9390 static void DrawEditorDoorBackground(int graphic, int x, int y,
9391                                      int width, int height)
9392 {
9393   struct GraphicInfo *g = &graphic_info[graphic];
9394
9395   if (g->bitmap != NULL)
9396     BlitBitmap(g->bitmap, drawto, g->src_x, g->src_y,
9397                MIN(width, g->width), MIN(height, g->height), x, y);
9398   else
9399     ClearRectangle(drawto, x, y, width, height);
9400 }
9401
9402 static void DrawEditorDoorContent(void)
9403 {
9404   // needed for gadgets drawn on background (like palette scrollbar)
9405   SetDoorBackgroundImage(IMG_UNDEFINED);
9406
9407   // copy default editor door content to main double buffer
9408   DrawEditorDoorBackground(IMG_BACKGROUND_PALETTE, DX, DY, DXSIZE, DYSIZE);
9409
9410   // draw bigger door
9411   DrawSpecialEditorDoor();
9412
9413   // draw new control window
9414   DrawEditorDoorBackground(IMG_BACKGROUND_TOOLBOX, EX, EY, EXSIZE, EYSIZE);
9415
9416   // draw all toolbox gadgets to editor doors
9417   MapControlButtons();
9418
9419   // when returning from test game to properties page, redraw toolbox gadgets
9420   if (edit_mode == ED_MODE_PROPERTIES)
9421   {
9422     UnmapLevelEditorToolboxDrawingGadgets();
9423     UnmapLevelEditorToolboxCustomGadgets();
9424
9425     MapLevelEditorToolboxCustomGadgetsIfNeeded();
9426   }
9427
9428   // draw all palette gadgets to editor doors
9429   ModifyEditorElementList();
9430   RedrawDrawingElements();
9431
9432   // copy actual editor door content to door double buffer for OpenDoor()
9433   BlitBitmap(drawto, bitmap_db_door_1, DX, DY, DXSIZE, DYSIZE, 0, 0);
9434 }
9435
9436 void DrawLevelEd(void)
9437 {
9438   int fade_mask = REDRAW_FIELD;
9439
9440   FadeSoundsAndMusic();
9441
9442   if (CheckFadeAll())
9443     fade_mask = REDRAW_ALL;
9444
9445   FadeOut(fade_mask);
9446
9447   // needed if different viewport properties defined for editor
9448   ChangeViewportPropertiesIfNeeded();
9449
9450   ClearField();
9451
9452   InitZoomLevelSettings(-1);
9453   InitDrawingElements();
9454   InitLevelSetInfo();
9455
9456 #if DEBUG
9457   CheckElementDescriptions();
9458 #endif
9459
9460   if (level_editor_test_game)
9461   {
9462     CopyPlayfield(level.field, Tile);
9463     CopyPlayfield(TileBackup, level.field);
9464
9465     level_editor_test_game = FALSE;
9466   }
9467   else
9468   {
9469     edit_mode = ED_MODE_DRAWING;
9470     edit_mode_levelconfig = ED_MODE_LEVELCONFIG_LEVEL;
9471     edit_mode_properties = ED_MODE_PROPERTIES_INFO;
9472
9473     ResetUndoBuffer();
9474
9475     level_xpos = -1;
9476     level_ypos = -1;
9477   }
9478
9479   // redraw_mask |= REDRAW_ALL;
9480
9481   FreeLevelEditorGadgets();
9482   CreateLevelEditorGadgets();
9483
9484   ReinitializeElementList();            // update dynamic level element list
9485   ReinitializeElementListButtons();     // custom element may look different
9486
9487   InitElementPropertiesGfxElement();
9488
9489   UnmapAllGadgets();
9490
9491   DrawEditModeWindow_PlayfieldOnly();
9492
9493   DrawMaskedBorder(fade_mask);
9494
9495   // use door animation if door 1 viewport is unchanged and contains toolbox
9496   if (useEditorDoorAnimation())
9497   {
9498     FadeIn(fade_mask);
9499
9500     DrawEditorDoorContent();
9501
9502     OpenDoor(DOOR_OPEN_1 | DOOR_FORCE_ANIM);
9503   }
9504   else
9505   {
9506     DrawEditorDoorContent();
9507
9508     FadeIn(fade_mask);
9509   }
9510
9511   SetDoorState(DOOR_OPEN_1 | DOOR_OPEN_2);
9512 }
9513
9514 static void AdjustDrawingAreaGadgets(void)
9515 {
9516   int ed_xsize = lev_fieldx + 2;
9517   int ed_ysize = lev_fieldy + 2;
9518   int max_ed_fieldx = MAX_ED_FIELDX;
9519   int max_ed_fieldy = MAX_ED_FIELDY;
9520   boolean horizontal_scrollbar_needed;
9521   boolean vertical_scrollbar_needed;
9522   int x, y, width, height;
9523
9524   if (suppressBorderElement())
9525   {
9526     ed_xsize = lev_fieldx;
9527     ed_ysize = lev_fieldy;
9528   }
9529
9530   // check if we need any scrollbars
9531   horizontal_scrollbar_needed = (ed_xsize > max_ed_fieldx);
9532   vertical_scrollbar_needed   = (ed_ysize > max_ed_fieldy);
9533
9534   // check if we have a smaller editor field because of scrollbars
9535   max_ed_fieldx = getMaxEdFieldX(vertical_scrollbar_needed);
9536   max_ed_fieldy = getMaxEdFieldY(horizontal_scrollbar_needed);
9537
9538   // check again if we now need more scrollbars because of less space
9539   horizontal_scrollbar_needed = (ed_xsize > max_ed_fieldx);
9540   vertical_scrollbar_needed   = (ed_ysize > max_ed_fieldy);
9541
9542   // check if editor field gets even smaller after adding new scrollbars
9543   max_ed_fieldx = getMaxEdFieldX(vertical_scrollbar_needed);
9544   max_ed_fieldy = getMaxEdFieldY(horizontal_scrollbar_needed);
9545
9546   ed_fieldx = (ed_xsize > max_ed_fieldx ? max_ed_fieldx : ed_xsize);
9547   ed_fieldy = (ed_ysize > max_ed_fieldy ? max_ed_fieldy : ed_ysize);
9548
9549   x = SX + ed_fieldx * ed_tilesize;
9550   y = SY + ed_fieldy * ed_tilesize;
9551
9552   width  = ed_fieldx * ed_tilesize - 2 * ED_SCROLLBUTTON_XSIZE;
9553   height = ed_fieldy * ed_tilesize - 2 * ED_SCROLLBUTTON_YSIZE;
9554
9555   // adjust drawing area gadget
9556   ModifyGadget(level_editor_gadget[GADGET_ID_DRAWING_LEVEL],
9557                GDI_AREA_SIZE, ed_fieldx, ed_fieldy,
9558                GDI_ITEM_SIZE, ed_tilesize, ed_tilesize,
9559                GDI_END);
9560
9561   // adjust horizontal scrollbar gadgets
9562   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LEFT],
9563                GDI_Y, y,
9564                GDI_END);
9565   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_RIGHT],
9566                GDI_X, x - ED_SCROLLBUTTON_XSIZE,
9567                GDI_Y, y,
9568                GDI_END);
9569   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
9570                GDI_Y, y,
9571                GDI_WIDTH, width,
9572                GDI_SCROLLBAR_ITEMS_VISIBLE, ed_fieldx,
9573                GDI_END);
9574
9575   // adjust vertical scrollbar gadgets
9576   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_UP],
9577                GDI_X, x,
9578                GDI_END);
9579   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_DOWN],
9580                GDI_X, x,
9581                GDI_Y, y - ED_SCROLLBUTTON_YSIZE,
9582                GDI_END);
9583   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
9584                GDI_X, x,
9585                GDI_HEIGHT, height,
9586                GDI_SCROLLBAR_ITEMS_VISIBLE, ed_fieldy,
9587                GDI_END);
9588 }
9589
9590 static void AdjustLevelScrollPosition(void)
9591 {
9592   if (level_xpos < -1)
9593     level_xpos = -1;
9594   if (level_xpos > lev_fieldx - ed_fieldx + 1)
9595     level_xpos = lev_fieldx - ed_fieldx + 1;
9596   if (lev_fieldx < ed_fieldx - 2)
9597     level_xpos = -1;
9598
9599   if (level_ypos < -1)
9600     level_ypos = -1;
9601   if (level_ypos > lev_fieldy - ed_fieldy + 1)
9602     level_ypos = lev_fieldy - ed_fieldy + 1;
9603   if (lev_fieldy < ed_fieldy - 2)
9604     level_ypos = -1;
9605
9606   if (suppressBorderElement())
9607   {
9608     level_xpos = 0;
9609     level_ypos = 0;
9610   }
9611 }
9612
9613 static void AdjustEditorScrollbar(int id)
9614 {
9615   struct GadgetInfo *gi = level_editor_gadget[id];
9616   int items_max, items_visible, item_position;
9617
9618   if (id == GADGET_ID_SCROLL_HORIZONTAL)
9619   {
9620     items_max = MAX(lev_fieldx + 2, ed_fieldx);
9621     items_visible = ed_fieldx;
9622     item_position = level_xpos + 1;
9623   }
9624   else
9625   {
9626     items_max = MAX(lev_fieldy + 2, ed_fieldy);
9627     items_visible = ed_fieldy;
9628     item_position = level_ypos + 1;
9629   }
9630
9631   if (item_position > items_max - items_visible)
9632     item_position = items_max - items_visible;
9633
9634   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
9635                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
9636 }
9637
9638 static void AdjustElementListScrollbar(void)
9639 {
9640   struct GadgetInfo *gi = level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL];
9641   int items_max, items_visible, item_position;
9642
9643   // correct position of element list scrollbar
9644   if (element_shift < 0)
9645     element_shift = 0;
9646   if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
9647     element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
9648
9649   items_max = num_editor_elements / ED_ELEMENTLIST_BUTTONS_HORIZ;
9650   items_visible = ED_ELEMENTLIST_BUTTONS_VERT;
9651   item_position = element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ;
9652
9653   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
9654                GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
9655                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
9656 }
9657
9658 static void ModifyEditorCounterValue(int counter_id, int new_value)
9659 {
9660   int *counter_value = counterbutton_info[counter_id].value;
9661   int gadget_id = counterbutton_info[counter_id].gadget_id_text;
9662   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9663
9664   ModifyGadget(gi, GDI_NUMBER_VALUE, new_value, GDI_END);
9665
9666   if (counter_value != NULL)
9667     *counter_value = gi->textinput.number_value;
9668 }
9669
9670 static void ModifyEditorCounterLimits(int counter_id, int min, int max)
9671 {
9672   int gadget_id = counterbutton_info[counter_id].gadget_id_text;
9673   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9674
9675   ModifyGadget(gi, GDI_NUMBER_MIN, min, GDI_NUMBER_MAX, max, GDI_END);
9676
9677   if (counter_id >= ED_COUNTER_ID_ELEMENT_VALUE1 &&
9678       counter_id <= ED_COUNTER_ID_ELEMENT_VALUE4)
9679   {
9680     int gadget_id_up = counterbutton_info[counter_id].gadget_id_up;
9681     struct GadgetInfo *gi_up = level_editor_gadget[gadget_id_up];
9682
9683     ModifyGadget(gi, GDI_TEXT_SIZE, (max < 10 ? 1 : 3), GDI_END);
9684     ModifyGadget(gi_up, GDI_X, gi->x + gi->width + ED_GADGET_SMALL_DISTANCE,
9685                  GDI_END);
9686   }
9687 }
9688
9689 static void ModifyEditorSelectboxValue(int selectbox_id, int new_value)
9690 {
9691   int gadget_id = selectbox_info[selectbox_id].gadget_id;
9692   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9693   int new_index_value = setSelectboxValue(selectbox_id, new_value);
9694
9695   ModifyGadget(gi, GDI_SELECTBOX_INDEX, new_index_value, GDI_END);
9696 }
9697
9698 static void ModifyEditorSelectboxOptions(int selectbox_id,
9699                                          struct ValueTextInfo *options)
9700 {
9701   int gadget_id = selectbox_info[selectbox_id].gadget_id;
9702   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9703
9704   selectbox_info[selectbox_id].options = options;
9705
9706   // set index to zero -- list may be shorter now (correct later, if needed)
9707   ModifyGadget(gi, GDI_SELECTBOX_INDEX, 0,
9708                GDI_SELECTBOX_OPTIONS, options, GDI_END);
9709 }
9710
9711 static void ModifyEditorDrawingArea(int drawingarea_id, int xsize, int ysize)
9712 {
9713   int gadget_id = drawingarea_info[drawingarea_id].gadget_id;
9714   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9715
9716   drawingarea_info[drawingarea_id].area_xsize = xsize;
9717   drawingarea_info[drawingarea_id].area_ysize = ysize;
9718
9719   ModifyGadget(gi, GDI_AREA_SIZE, xsize, ysize, GDI_END);
9720 }
9721
9722 static void ModifyEditorElementList(void)
9723 {
9724   int i;
9725
9726   if (!use_permanent_palette && edit_mode != ED_MODE_PALETTE)
9727     return;
9728
9729   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
9730   {
9731     int gadget_id = GADGET_ID_ELEMENTLIST_FIRST + i;
9732     struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9733     struct GadgetDesign *gd = &gi->deco.design;
9734     int element = editor_elements[element_shift + i];
9735     int tile_size = BUTTON_TILE_SIZE(editor.palette.tile_size);
9736
9737     UnmapGadget(gi);
9738
9739     getEditorGraphicSource(element, tile_size, &gd->bitmap, &gd->x, &gd->y);
9740
9741     ModifyGadget(gi, GDI_INFO_TEXT, getElementInfoText(element), GDI_END);
9742
9743     MapGadget(gi);
9744   }
9745 }
9746
9747 static void DrawDrawingElementGraphic(int element, struct XYTileSize *pos)
9748 {
9749   int graphic = el2edimg(element);
9750   int tile_size = BUTTON_TILE_SIZE(pos->tile_size);
9751
9752   if (pos->x == -1 &&
9753       pos->y == -1)
9754     return;
9755
9756   DrawSizedGraphicExt(drawto, DX + pos->x, DY + pos->y, graphic, 0, tile_size);
9757 }
9758
9759 static void ModifyDrawingElementButton(int element, int id)
9760 {
9761   struct GadgetInfo *gi = level_editor_gadget[id];
9762   Bitmap *deco_bitmap;
9763   int deco_x, deco_y;
9764   int tile_size = gi->deco.width;
9765
9766   getEditorGraphicSource(element, tile_size, &deco_bitmap, &deco_x, &deco_y);
9767
9768   ModifyGadget(gi, GDI_DECORATION_DESIGN, deco_bitmap, deco_x, deco_y, GDI_END);
9769 }
9770
9771 static void PickDrawingElement(int button, int element)
9772 {
9773   struct
9774   {
9775     int *new_element;
9776     struct XYTileSize *pos;
9777     int id;
9778   } de, drawing_elements[] =
9779   {
9780     { &new_element1, &editor.palette.element_left,   GADGET_ID_ELEMENT_LEFT   },
9781     { &new_element2, &editor.palette.element_middle, GADGET_ID_ELEMENT_MIDDLE },
9782     { &new_element3, &editor.palette.element_right,  GADGET_ID_ELEMENT_RIGHT  },
9783   };
9784
9785   if (button < 1 || button > 3)
9786     return;
9787
9788   if (IS_MM_WALL(element))
9789     element = map_mm_wall_element(element);
9790
9791   de = drawing_elements[button - 1];
9792
9793   *de.new_element = element;    // update global drawing element variable
9794
9795   DrawDrawingElementGraphic(element, de.pos);
9796   ModifyDrawingElementButton(element, de.id);
9797
9798   redraw_mask |= REDRAW_DOOR_1;
9799 }
9800
9801 static void RedrawDrawingElements(void)
9802 {
9803   PickDrawingElement(1, new_element1);
9804   PickDrawingElement(2, new_element2);
9805   PickDrawingElement(3, new_element3);
9806 }
9807
9808 static void DrawDrawingWindowExt(boolean remap_toolbox_gadgets)
9809 {
9810   stick_element_properties_window = FALSE;
9811
9812   SetMainBackgroundImage(IMG_UNDEFINED);
9813   ClearField();
9814
9815   UnmapLevelEditorFieldGadgets();
9816
9817   AdjustDrawingAreaGadgets();
9818   AdjustLevelScrollPosition();
9819   AdjustEditorScrollbar(GADGET_ID_SCROLL_HORIZONTAL);
9820   AdjustEditorScrollbar(GADGET_ID_SCROLL_VERTICAL);
9821
9822   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
9823
9824   MapMainDrawingArea();
9825
9826   if (remap_toolbox_gadgets)
9827   {
9828     UnmapLevelEditorToolboxCustomGadgets();
9829     MapLevelEditorToolboxDrawingGadgets();
9830   }
9831 }
9832
9833 static void DrawDrawingWindow(void)
9834 {
9835   DrawDrawingWindowExt(TRUE);
9836 }
9837
9838 static int getTabulatorBarWidth(void)
9839 {
9840   struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_PROPERTIES_INFO];
9841   struct GadgetInfo *gd_gi4 = level_editor_gadget[GADGET_ID_PROPERTIES_CHANGE];
9842
9843   return gd_gi4->x - gd_gi1->x + gd_gi4->width;
9844 }
9845
9846 static int getTabulatorBarHeight(void)
9847 {
9848   return ED_TAB_BAR_HEIGHT;
9849 }
9850
9851 static Pixel getTabulatorBarColor(void)
9852 {
9853   struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_LEVELCONFIG_LEVEL];
9854   struct GadgetDesign *gd = &gd_gi1->alt_design[GD_BUTTON_UNPRESSED];
9855   int gd_x = gd->x + gd_gi1->border.width / 2;
9856   int gd_y = gd->y + gd_gi1->height - 1;
9857
9858   return GetPixel(gd->bitmap, gd_x, gd_y);
9859 }
9860
9861 static void DrawLevelConfigTabulatorGadgets(void)
9862 {
9863   struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_LEVELCONFIG_LEVEL];
9864   Pixel tab_color = getTabulatorBarColor();
9865   int id_first = ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL;
9866   int id_last  = ED_TEXTBUTTON_ID_LEVELCONFIG_EDITOR;
9867   int i;
9868
9869   // draw additional "engine" tabulator when using native BD engine
9870   if (level.game_engine_type == GAME_ENGINE_TYPE_BD)
9871     id_last = ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE;
9872
9873   for (i = id_first; i <= id_last; i++)
9874   {
9875     int gadget_id = textbutton_info[i].gadget_id;
9876     struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9877     boolean active = (i != edit_mode_levelconfig);
9878
9879     // draw background line below tabulator button
9880     ClearRectangleOnBackground(drawto, gi->x, gi->y + gi->height, gi->width, 1);
9881
9882     // draw solid line below inactive tabulator buttons
9883     if (!active && tab_color != BLACK_PIXEL)    // black => transparent
9884       FillRectangle(drawto, gi->x, gi->y + gi->height, gi->width,
9885                     ED_GADGET_TINY_DISTANCE, tab_color);
9886
9887     ModifyGadget(gi, GDI_ACTIVE, active, GDI_END);
9888     MapTextbuttonGadget(i);
9889   }
9890
9891   // draw little border line below tabulator buttons
9892   if (tab_color != BLACK_PIXEL)                 // black => transparent
9893     FillRectangle(drawto, gd_gi1->x, gd_gi1->y + gd_gi1->height +
9894                   ED_GADGET_TINY_DISTANCE,
9895                   getTabulatorBarWidth(), getTabulatorBarHeight(), tab_color);
9896 }
9897
9898 static void DrawPropertiesTabulatorGadgets(void)
9899 {
9900   struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_PROPERTIES_INFO];
9901   struct GadgetDesign *gd = &gd_gi1->alt_design[GD_BUTTON_UNPRESSED];
9902   int gd_x = gd->x + gd_gi1->border.width / 2;
9903   int gd_y = gd->y + gd_gi1->height - 1;
9904   Pixel tab_color = GetPixel(gd->bitmap, gd_x, gd_y);
9905   int id_first = ED_TEXTBUTTON_ID_PROPERTIES_INFO;
9906   int id_last  = ED_TEXTBUTTON_ID_PROPERTIES_CONFIG;
9907   int i;
9908
9909   // draw two config tabulators for player elements
9910   if (IS_PLAYER_ELEMENT(properties_element))
9911     id_last = ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2;
9912
9913   // draw two config and one "change" tabulator for custom elements
9914   if (IS_CUSTOM_ELEMENT(properties_element))
9915     id_last = ED_TEXTBUTTON_ID_PROPERTIES_CHANGE;
9916
9917   for (i = id_first; i <= id_last; i++)
9918   {
9919     int gadget_id = textbutton_info[i].gadget_id;
9920     struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9921     boolean active = (i != edit_mode_properties);
9922
9923     // use "config 1" and "config 2" instead of "config" for players and CEs
9924     if (i == ED_TEXTBUTTON_ID_PROPERTIES_CONFIG &&
9925         (IS_PLAYER_ELEMENT(properties_element) ||
9926          IS_CUSTOM_ELEMENT(properties_element)))
9927       continue;
9928
9929     // draw background line below tabulator button
9930     ClearRectangleOnBackground(drawto, gi->x, gi->y + gi->height, gi->width, 1);
9931
9932     // draw solid line below inactive tabulator buttons
9933     if (!active && tab_color != BLACK_PIXEL)    // black => transparent
9934       FillRectangle(drawto, gi->x, gi->y + gi->height, gi->width,
9935                     ED_GADGET_TINY_DISTANCE, tab_color);
9936
9937     ModifyGadget(gi, GDI_ACTIVE, active, GDI_END);
9938     MapTextbuttonGadget(i);
9939   }
9940
9941   // draw little border line below tabulator buttons
9942   if (tab_color != BLACK_PIXEL)                 // black => transparent
9943     FillRectangle(drawto, gd_gi1->x, gd_gi1->y + gd_gi1->height +
9944                   ED_GADGET_TINY_DISTANCE,
9945                   getTabulatorBarWidth(), getTabulatorBarHeight(), tab_color);
9946 }
9947
9948 static void PrintInfoText(char *text, int font_nr, int xpos, int ypos)
9949 {
9950   DrawText(SX + xpos, SY + ypos, text, font_nr);
9951 }
9952
9953 static int PrintElementDescriptionFromFile(char *filename, int font_nr,
9954                                            int xpos, int ypos)
9955 {
9956   int font_width = getFontWidth(font_nr);
9957   int font_height = getFontHeight(font_nr);
9958   int max_chars_per_line = (SXSIZE - 2 * xpos) / font_width;
9959   int max_lines_drawable = (SYSIZE - ypos) / font_height - 1;
9960
9961   return DrawTextFile(SX + xpos, SY + ypos, filename, font_nr,
9962                       max_chars_per_line, -1, max_lines_drawable, 0, -1,
9963                       TRUE, FALSE, FALSE);
9964 }
9965
9966 static void DrawLevelConfigLevel(void)
9967 {
9968   int i;
9969
9970   // draw counter gadgets
9971   for (i = ED_COUNTER_ID_LEVEL_FIRST; i <= ED_COUNTER_ID_LEVEL_LAST; i++)
9972     MapCounterButtons(i);
9973
9974   // draw checkbutton gadgets
9975   for (i = ED_CHECKBUTTON_ID_LEVEL_FIRST; i<= ED_CHECKBUTTON_ID_LEVEL_LAST; i++)
9976     MapCheckbuttonGadget(i);
9977
9978   // draw selectbox gadgets
9979   for (i = ED_SELECTBOX_ID_LEVEL_FIRST; i <= ED_SELECTBOX_ID_LEVEL_LAST; i++)
9980     MapSelectboxGadget(i);
9981
9982   // draw text input gadgets
9983   for (i = ED_TEXTINPUT_ID_LEVEL_FIRST; i <= ED_TEXTINPUT_ID_LEVEL_LAST; i++)
9984     MapTextInputGadget(i);
9985 }
9986
9987 static char *getLevelSubdirFromSaveMode(int save_mode)
9988 {
9989   if (save_mode == LEVELSET_SAVE_MODE_CREATE)
9990     return getNewUserLevelSubdir();
9991
9992   return leveldir_current->subdir;
9993 }
9994
9995 static void DrawLevelConfigLevelSet_DirectoryInfo(void)
9996 {
9997   char *directory_text = "Level set directory:";
9998   char *directory_name = getLevelSubdirFromSaveMode(levelset_save_mode);
9999   int font1_nr = FONT_TEXT_1;
10000   int font2_nr = FONT_TEXT_2;
10001   int font1_height = getFontHeight(font1_nr);
10002   int yoffset_above = font1_height + ED_GADGET_LINE_DISTANCE;
10003   int x = ED_LEVEL_SETTINGS_X(0);
10004   int y = ED_LEVEL_SETTINGS_Y(6);
10005
10006   PrintInfoText(directory_text, font1_nr, x, y - yoffset_above);
10007   PrintInfoText(directory_name, font2_nr, x, y);
10008 }
10009
10010 static void DrawLevelConfigLevelSet(void)
10011 {
10012   boolean artwork_exists = checkIfCustomArtworkExistsForCurrentLevelSet();
10013   boolean template_exists = fileExists(getLocalLevelTemplateFilename());
10014   int i;
10015
10016   // draw counter gadgets
10017   for (i = ED_COUNTER_ID_LEVELSET_FIRST; i <= ED_COUNTER_ID_LEVELSET_LAST; i++)
10018     MapCounterButtons(i);
10019
10020   // draw checkbutton gadgets
10021   for (i = ED_CHECKBUTTON_ID_LEVELSET_FIRST; i <= ED_CHECKBUTTON_ID_LEVELSET_LAST; i++)
10022   {
10023     if (levelset_save_mode == LEVELSET_SAVE_MODE_UPDATE ||
10024         (i == ED_CHECKBUTTON_ID_USE_LEVELSET_ARTWORK && !artwork_exists) ||
10025         (i == ED_CHECKBUTTON_ID_COPY_LEVEL_TEMPLATE  && !template_exists))
10026       continue;
10027
10028     MapCheckbuttonGadget(i);
10029   }
10030
10031   // draw selectbox gadgets
10032   for (i = ED_SELECTBOX_ID_LEVELSET_FIRST; i <= ED_SELECTBOX_ID_LEVELSET_LAST; i++)
10033     MapSelectboxGadget(i);
10034
10035   // draw text input gadgets
10036   for (i = ED_TEXTINPUT_ID_LEVELSET_FIRST; i <= ED_TEXTINPUT_ID_LEVELSET_LAST; i++)
10037     MapTextInputGadget(i);
10038
10039   // draw textbutton gadgets
10040   MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_LEVELSET);
10041
10042   // draw info text
10043   DrawLevelConfigLevelSet_DirectoryInfo();
10044 }
10045
10046 static void DrawLevelConfigEditor(void)
10047 {
10048   int i;
10049
10050   // draw counter gadgets
10051   for (i = ED_COUNTER_ID_EDITOR_FIRST; i <= ED_COUNTER_ID_EDITOR_LAST; i++)
10052     MapCounterButtons(i);
10053
10054   // draw checkbutton gadgets
10055   for (i = ED_CHECKBUTTON_ID_EDITOR_FIRST; i <= ED_CHECKBUTTON_ID_EDITOR_LAST; i++)
10056     MapCheckbuttonGadget(i);
10057
10058   // draw radiobutton gadgets
10059   for (i = ED_RADIOBUTTON_ID_EDITOR_FIRST; i <= ED_RADIOBUTTON_ID_EDITOR_LAST; i++)
10060     MapRadiobuttonGadget(i);
10061
10062   // draw drawing area
10063   MapDrawingArea(ED_DRAWING_ID_RANDOM_BACKGROUND);
10064
10065   // draw textbutton gadgets
10066   MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2);
10067 }
10068
10069 static void DrawLevelConfigEngine(void)
10070 {
10071   int i;
10072
10073   // draw counter gadgets
10074   if (level.bd_scheduling_type == GD_SCHEDULING_MILLISECONDS)
10075     MapCounterButtons(ED_COUNTER_ID_BD_CYCLE_DELAY_MS);
10076   else
10077     MapCounterButtons(ED_COUNTER_ID_BD_CYCLE_DELAY_C64);
10078
10079   // draw checkbutton gadgets
10080   for (i = ED_CHECKBUTTON_ID_ENGINE_FIRST; i <= ED_CHECKBUTTON_ID_ENGINE_LAST; i++)
10081     MapCheckbuttonGadget(i);
10082
10083   // draw selectbox gadgets
10084   for (i = ED_SELECTBOX_ID_ENGINE_FIRST; i <= ED_SELECTBOX_ID_ENGINE_LAST; i++)
10085     MapSelectboxGadget(i);
10086 }
10087
10088 static void DrawLevelConfigWindow(void)
10089 {
10090   char *text = "Global Settings";
10091   int font_nr = FONT_TITLE_1;
10092   struct MenuPosInfo *pos = &editor.settings.headline;
10093   int sx = SX + ALIGNED_XPOS(pos->x, getTextWidth(text, font_nr), pos->align);
10094   int sy = SY + pos->y;
10095
10096   stick_element_properties_window = FALSE;
10097
10098   SetAutomaticNumberOfGemsNeeded();
10099
10100   UnmapLevelEditorFieldGadgets();
10101
10102   SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
10103   ClearField();
10104
10105   DrawText(sx, sy, text, font_nr);
10106
10107   DrawLevelConfigTabulatorGadgets();
10108
10109   if (edit_mode_levelconfig == ED_MODE_LEVELCONFIG_LEVEL)
10110     DrawLevelConfigLevel();
10111   else if (edit_mode_levelconfig == ED_MODE_LEVELCONFIG_LEVELSET)
10112     DrawLevelConfigLevelSet();
10113   else if (edit_mode_levelconfig == ED_MODE_LEVELCONFIG_EDITOR)
10114     DrawLevelConfigEditor();
10115   else if (edit_mode_levelconfig == ED_MODE_LEVELCONFIG_ENGINE)
10116     DrawLevelConfigEngine();
10117 }
10118
10119 static void DrawCustomContentArea(void)
10120 {
10121   int id = ED_DRAWING_ID_CUSTOM_CONTENT;
10122   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
10123   int x1 = right_gadget_border[GADGET_ID_CUSTOM_DEADLINESS];
10124   int x2 = right_gadget_border[GADGET_ID_CUSTOM_EXPLOSION_TYPE];
10125   int x3 = right_gadget_border[GADGET_ID_CUSTOM_EXPLODE_IMPACT];
10126   int xoffset = ED_GADGET_SPACE_DISTANCE;
10127
10128   // add distance for potential left text (without drawing area border)
10129   x2 += getTextWidthForGadget(drawingarea_info[id].text_left);
10130
10131   ModifyGadget(gi, GDI_X, MAX(x1, MAX(x2, x3)) + xoffset, GDI_END);
10132
10133   MapDrawingArea(ED_DRAWING_ID_CUSTOM_CONTENT);
10134 }
10135
10136 static void DrawCustomChangeContentArea(void)
10137 {
10138   int id = ED_DRAWING_ID_CUSTOM_CHANGE_CONTENT;
10139   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
10140   int x1 = right_gadget_border[GADGET_ID_CHANGE_USE_CONTENT];
10141   int x2 = right_gadget_border[GADGET_ID_CHANGE_REPLACE_WHEN];
10142   int x3 = right_gadget_border[GADGET_ID_CHANGE_ONLY_COMPLETE];
10143   int xoffset = ED_GADGET_SPACE_DISTANCE;
10144
10145   ModifyGadget(gi, GDI_X, MAX(x1, MAX(x2, x3)) + xoffset, GDI_END);
10146
10147   MapDrawingArea(id);
10148 }
10149
10150 static void RemoveElementContentArea(int id, int font_height)
10151 {
10152   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10153
10154   DrawBackground(SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size,
10155                  SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size,
10156                  3 * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10157                  3 * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size +
10158                  ED_GADGET_TEXT_DISTANCE + font_height);
10159 }
10160
10161 static void DrawYamYamContentAreas(void)
10162 {
10163   int font_nr = FONT_TEXT_1;
10164   int font_height = getFontHeight(font_nr);
10165   int tilesize = ED_DRAWINGAREA_TILE_SIZE;
10166   int yoffset = (tilesize - font_height) / 2;
10167   int x = SX + ED_AREA_YAMYAM_CONTENT_X(3) + 4 * tilesize;
10168   int y = SY + ED_AREA_YAMYAM_CONTENT_Y(3) + yoffset;
10169   int i;
10170
10171   // display counter to choose number of element content areas
10172   MapCounterButtons(ED_COUNTER_ID_YAMYAM_CONTENT);
10173
10174   for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
10175   {
10176     int id = ED_DRAWING_ID_YAMYAM_CONTENT_0 + i;
10177
10178     if (i < level.num_yamyam_contents)
10179     {
10180       MapDrawingArea(id);
10181     }
10182     else
10183     {
10184       UnmapDrawingArea(id);
10185
10186       // delete content areas in case of reducing number of them
10187       RemoveElementContentArea(id, font_height);
10188     }
10189   }
10190
10191   DrawText(x, y + 0 * tilesize, "content", font_nr);
10192   DrawText(x, y + 1 * tilesize, "when",    font_nr);
10193   DrawText(x, y + 2 * tilesize, "smashed", font_nr);
10194 }
10195
10196 static void DrawMagicBallContentAreas(void)
10197 {
10198   int font_nr = FONT_TEXT_1;
10199   int font_height = getFontHeight(font_nr);
10200   int tilesize = ED_DRAWINGAREA_TILE_SIZE;
10201   int yoffset = (tilesize - font_height) / 2;
10202   int x = SX + ED_AREA_MAGIC_BALL_CONTENT_X(3) + 4 * tilesize;
10203   int y = SY + ED_AREA_MAGIC_BALL_CONTENT_Y(3) + yoffset;
10204   int i;
10205
10206   // display counter to choose number of element content areas
10207   MapCounterButtons(ED_COUNTER_ID_BALL_CONTENT);
10208
10209   for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
10210   {
10211     int id = ED_DRAWING_ID_MAGIC_BALL_CONTENT_0 + i;
10212
10213     if (i < level.num_ball_contents)
10214     {
10215       MapDrawingArea(id);
10216     }
10217     else
10218     {
10219       UnmapDrawingArea(id);
10220
10221       // delete content areas in case of reducing number of them
10222       RemoveElementContentArea(id, font_height);
10223     }
10224   }
10225
10226   DrawText(x, y + 0 * tilesize, "generated", font_nr);
10227   DrawText(x, y + 1 * tilesize, "when",      font_nr);
10228   DrawText(x, y + 2 * tilesize, "active",    font_nr);
10229 }
10230
10231 static void DrawAndroidElementArea(void)
10232 {
10233   int id = ED_DRAWING_ID_ANDROID_CONTENT;
10234   int num_elements = level.num_android_clone_elements;
10235   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10236   int sx = SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size;
10237   int sy = SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size;
10238   int xsize = MAX_ANDROID_ELEMENTS;
10239   int ysize = 1;
10240
10241   // display counter to choose number of element areas
10242   MapCounterButtons(ED_COUNTER_ID_ANDROID_CONTENT);
10243
10244   if (drawingarea_info[id].text_left != NULL)
10245     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
10246
10247   UnmapDrawingArea(id);
10248
10249   ModifyEditorDrawingArea(id, num_elements, 1);
10250
10251   // delete content areas in case of reducing number of them
10252   DrawBackground(sx, sy,
10253                  xsize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10254                  ysize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size);
10255
10256   MapDrawingArea(id);
10257 }
10258
10259 static void DrawGroupElementArea(void)
10260 {
10261   int id = ED_DRAWING_ID_GROUP_CONTENT;
10262   int num_elements = group_element_info.num_elements;
10263   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10264   int sx = SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size;
10265   int sy = SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size;
10266   int xsize = MAX_ELEMENTS_IN_GROUP;
10267   int ysize = 1;
10268
10269   if (drawingarea_info[id].text_left != NULL)
10270     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
10271
10272   UnmapDrawingArea(id);
10273
10274   ModifyEditorDrawingArea(id, num_elements, 1);
10275
10276   // delete content areas in case of reducing number of them
10277   DrawBackground(sx, sy,
10278                  xsize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10279                  ysize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size);
10280
10281   MapDrawingArea(id);
10282 }
10283
10284 static void DrawPlayerInitialInventoryArea(int element)
10285 {
10286   int id = ED_DRAWING_ID_INVENTORY_CONTENT;
10287   int player_nr = GET_PLAYER_NR(element);
10288   int num_elements = level.initial_inventory_size[player_nr];
10289   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10290   int sx = SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size;
10291   int sy = SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size;
10292   int xsize = MAX_INITIAL_INVENTORY_SIZE;
10293   int ysize = 1;
10294
10295   // determine horizontal position to the right of specified gadget
10296   if (drawingarea_info[id].gadget_id_align != GADGET_ID_NONE)
10297     sx = (right_gadget_border[drawingarea_info[id].gadget_id_align] +
10298           ED_DRAWINGAREA_TEXT_DISTANCE);
10299
10300   // determine horizontal offset for leading text
10301   if (drawingarea_info[id].text_left != NULL)
10302     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
10303
10304   UnmapDrawingArea(id);
10305
10306   ModifyEditorDrawingArea(id, num_elements, 1);
10307
10308   // delete content areas in case of reducing number of them
10309   DrawBackground(sx, sy,
10310                  xsize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10311                  ysize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size);
10312
10313   MapDrawingArea(id);
10314 }
10315
10316 static void DrawMMBallContentArea(void)
10317 {
10318   int id = ED_DRAWING_ID_MM_BALL_CONTENT;
10319   int num_elements = level.num_mm_ball_contents;
10320   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10321   int sx = SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size;
10322   int sy = SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size;
10323   int xsize = MAX_MM_BALL_CONTENTS;
10324   int ysize = 1;
10325
10326   if (drawingarea_info[id].text_left != NULL)
10327     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
10328
10329   UnmapDrawingArea(id);
10330
10331   ModifyEditorDrawingArea(id, num_elements, 1);
10332
10333   // delete content areas in case of reducing number of them
10334   DrawBackground(sx, sy,
10335                  xsize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10336                  ysize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size);
10337
10338   MapDrawingArea(id);
10339 }
10340
10341 static void DrawEnvelopeTextArea(int envelope_nr)
10342 {
10343   int id = ED_TEXTAREA_ID_ENVELOPE_INFO;
10344   struct GadgetInfo *gi = level_editor_gadget[textarea_info[id].gadget_id];
10345
10346   UnmapGadget(gi);
10347
10348   DrawBackground(gi->x, gi->y,
10349                  gi->textarea.crop_width, gi->textarea.crop_height);
10350
10351   if (envelope_nr != -1)
10352     textarea_info[id].value = level.envelope[envelope_nr].text;
10353
10354   ModifyGadget(gi, GDI_AREA_SIZE,
10355                *counterbutton_info[ED_COUNTER_ID_ENVELOPE_XSIZE].value,
10356                *counterbutton_info[ED_COUNTER_ID_ENVELOPE_YSIZE].value,
10357                GDI_END);
10358
10359   MapTextAreaGadget(ED_TEXTAREA_ID_ENVELOPE_INFO);
10360 }
10361
10362 static void DrawPropertiesInfo(void)
10363 {
10364   static struct
10365   {
10366     int value;
10367     char *text;
10368   }
10369   properties[] =
10370   {
10371     // configurable properties
10372
10373     { EP_WALKABLE_OVER,         "- player can walk over it"             },
10374     { EP_WALKABLE_INSIDE,       "- player can walk inside it"           },
10375     { EP_WALKABLE_UNDER,        "- player can walk under it"            },
10376     { EP_PASSABLE_OVER,         "- player can pass over it"             },
10377     { EP_PASSABLE_INSIDE,       "- player can pass through it"          },
10378     { EP_PASSABLE_UNDER,        "- player can pass under it"            },
10379     { EP_PROTECTED,             "- player is protected by it"           },
10380
10381     { EP_DIGGABLE,              "- can be digged away"                  },
10382     { EP_COLLECTIBLE,           "- can be collected"                    },
10383     { EP_DROPPABLE,             "- can be dropped after collecting"     },
10384     { EP_THROWABLE,             "- can be thrown after collecting"      },
10385     { EP_PUSHABLE,              "- can be pushed"                       },
10386
10387     { EP_CAN_FALL,              "- can fall"                            },
10388     { EP_CAN_MOVE,              "- can move"                            },
10389
10390     { EP_CAN_SMASH_PLAYER,      "- can smash player"                    },
10391 #if 0
10392     { EP_CAN_SMASH_ENEMIES,     "- can smash good and bad guys"         },
10393 #endif
10394     { EP_CAN_SMASH_EVERYTHING,  "- can smash everything smashable"      },
10395
10396     { EP_SLIPPERY,              "- slippery for falling elements"       },
10397     { EP_EM_SLIPPERY_WALL,      "- slippery for some gems (EM style)"   },
10398
10399     { EP_DONT_RUN_INTO,         "- deadly when running into"            },
10400     { EP_DONT_COLLIDE_WITH,     "- deadly when colliding with"          },
10401     { EP_DONT_GET_HIT_BY,       "- deadly when getting hit by"          },
10402     { EP_DONT_TOUCH,            "- deadly when touching"                },
10403
10404     { EP_INDESTRUCTIBLE,        "- indestructible"                      },
10405
10406     { EP_CAN_EXPLODE_BY_FIRE,   "- can explode by fire or explosions"   },
10407     { EP_CAN_EXPLODE_SMASHED,   "- can explode when smashed"            },
10408     { EP_CAN_EXPLODE_IMPACT,    "- can explode on impact"               },
10409
10410     { EP_CAN_CHANGE,            "- can change to other element"         },
10411
10412     // pre-defined properties
10413     { EP_CAN_PASS_MAGIC_WALL,   "- can pass magic walls"                },
10414     { EP_CAN_PASS_DC_MAGIC_WALL,"- can pass magic walls (DC style)"     },
10415     { EP_SWITCHABLE,            "- can be switched"                     },
10416 #if 0
10417     { EP_HAS_EDITOR_CONTENT,    "- can contain other elements"          },
10418 #endif
10419
10420     { -1,                       NULL                                    }
10421   };
10422   char *filename = getElementDescriptionFilename(properties_element);
10423   char *num_elements_text = "In this level: ";
10424   char *num_similar_text = "Similar tiles: ";
10425   char *properties_text = "Standard properties: ";
10426   char *description_text = "Description:";
10427   char *no_description_text = "No description available.";
10428   char *none_text = "None";
10429   float percentage;
10430   int num_elements_in_level = 0;
10431   int num_similar_in_level = 0;
10432   int num_hires_tiles_in_level = 0;
10433   int num_standard_properties = 0;
10434   int font1_nr = FONT_TEXT_1;
10435   int font2_nr = FONT_TEXT_2;
10436   int font1_width = getFontWidth(font1_nr);
10437   int font1_height = getFontHeight(font1_nr);
10438   int font2_height = getFontHeight(font2_nr);
10439   int line1_height = font1_height + ED_GADGET_LINE_DISTANCE;
10440   int font2_yoffset = (font1_height - font2_height) / 2;
10441   int num_elements_text_len = strlen(num_elements_text) * font1_width;
10442   int num_similar_text_len = strlen(num_similar_text) * font1_width;
10443   int properties_text_len = strlen(properties_text) * font1_width;
10444   int xpos = ED_ELEMENT_SETTINGS_X(0);
10445   int ypos = ED_ELEMENT_SETTINGS_Y(0) + ED_GADGET_SMALL_DISTANCE;
10446   int i, x, y;
10447
10448   if (setup.editor.show_element_token)
10449   {
10450     int font3_nr = FONT_TEXT_3;
10451     int font3_height = getFontHeight(font3_nr);
10452
10453     DrawTextF(xpos, ypos, font3_nr,
10454               "[%s]", element_info[properties_element].token_name);
10455
10456     ypos += 2 * font3_height;
10457   }
10458
10459   // ----- print number of elements / percentage of this element in level
10460
10461   for (y = 0; y < lev_fieldy; y++)
10462   {
10463     for (x = 0; x < lev_fieldx; x++)
10464     {
10465       if (Tile[x][y] == properties_element)
10466       {
10467         num_elements_in_level++;
10468       }
10469       else if (IS_MM_WALL(Tile[x][y]) &&
10470                map_mm_wall_element(Tile[x][y]) == properties_element)
10471       {
10472         num_hires_tiles_in_level += numHiresTiles(Tile[x][y]);
10473       }
10474     }
10475   }
10476
10477   percentage = num_elements_in_level * 100.0 / (lev_fieldx * lev_fieldy);
10478
10479   DrawTextS(xpos, ypos, font1_nr, num_elements_text);
10480
10481   if (num_hires_tiles_in_level > 0)
10482     DrawTextF(xpos + num_elements_text_len, ypos + font2_yoffset, font2_nr,
10483               "%d wall tiles", num_hires_tiles_in_level);
10484   else if (num_elements_in_level > 0)
10485     DrawTextF(xpos + num_elements_text_len, ypos + font2_yoffset, font2_nr,
10486               "%d (%.2f %%)", num_elements_in_level, percentage);
10487   else
10488     DrawTextF(xpos + num_elements_text_len, ypos + font2_yoffset, font2_nr,
10489               none_text);
10490
10491   // ----- print number of similar elements / percentage of them in level
10492
10493   for (y = 0; y < lev_fieldy; y++)
10494   {
10495     for (x = 0; x < lev_fieldx; x++)
10496     {
10497       if (strEqual(element_info[Tile[x][y]].class_name,
10498                    element_info[properties_element].class_name))
10499       {
10500         num_similar_in_level++;
10501       }
10502     }
10503   }
10504
10505   if (num_similar_in_level != num_elements_in_level)
10506   {
10507     ypos += 1 * MAX(font1_height, font2_height);
10508
10509     percentage = num_similar_in_level * 100.0 / (lev_fieldx * lev_fieldy);
10510
10511     DrawTextS(xpos, ypos, font1_nr, num_similar_text);
10512
10513     if (num_similar_in_level > 0)
10514       DrawTextF(xpos + num_similar_text_len, ypos + font2_yoffset, font2_nr,
10515                 "%d (%.2f %%)", num_similar_in_level, percentage);
10516     else
10517       DrawTextF(xpos + num_similar_text_len, ypos + font2_yoffset, font2_nr,
10518                 none_text);
10519   }
10520
10521   ypos += 2 * MAX(font1_height, font2_height);
10522
10523   // ----- print standard properties of this element
10524
10525   DrawTextS(xpos, ypos, font1_nr, properties_text);
10526
10527   ypos += line1_height;
10528
10529   for (i = 0; properties[i].value != -1; i++)
10530   {
10531     if (!HAS_PROPERTY(properties_element, properties[i].value))
10532       continue;
10533
10534     DrawTextS(xpos, ypos, font2_nr, properties[i].text);
10535
10536     ypos += font2_height;
10537
10538     num_standard_properties++;
10539   }
10540
10541   if (num_standard_properties == 0)
10542   {
10543     DrawTextS(xpos + properties_text_len, ypos - line1_height + font2_yoffset,
10544               font2_nr, none_text);
10545
10546     ypos -= (line1_height - font1_height);
10547   }
10548
10549   ypos += MAX(font1_height, font2_height);
10550
10551   // ----- print special description of this element
10552
10553   PrintInfoText(description_text, font1_nr, xpos, ypos);
10554
10555   ypos += line1_height;
10556
10557   if (PrintElementDescriptionFromFile(filename, font2_nr, xpos, ypos) == 0)
10558     PrintInfoText(no_description_text, font1_nr, xpos, ypos - line1_height);
10559 }
10560
10561 #define TEXT_COLLECTING         "Score for collecting"
10562 #define TEXT_COLLECTING_EXTRA   "Score for extra diamonds"
10563 #define TEXT_SMASHING           "Score for smashing"
10564 #define TEXT_SLURPING           "Score for slurping robot"
10565 #define TEXT_CRACKING           "Score for cracking"
10566 #define TEXT_AMOEBA_SPEED       "Speed of amoeba growth"
10567 #define TEXT_DURATION           "Duration when activated"
10568 #define TEXT_DELAY_ON           "Delay before activating"
10569 #define TEXT_DELAY_OFF          "Delay before deactivating"
10570 #define TEXT_DELAY_CHANGING     "Delay before changing"
10571 #define TEXT_DELAY_EXPLODING    "Delay before exploding"
10572 #define TEXT_DELAY_MOVING       "Delay before moving"
10573 #define TEXT_BALL_DELAY         "Element generation delay"
10574 #define TEXT_MOVE_SPEED         "Speed of android moving"
10575 #define TEXT_CLONE_SPEED        "Speed of android cloning"
10576 #define TEXT_GAME_OF_LIFE_1     "Min neighbours to survive"
10577 #define TEXT_GAME_OF_LIFE_2     "Max neighbours to survive"
10578 #define TEXT_GAME_OF_LIFE_3     "Min neighbours to create"
10579 #define TEXT_GAME_OF_LIFE_4     "Max neighbours to create"
10580 #define TEXT_TIME_BONUS         "Extra time to solve level"
10581
10582 static struct
10583 {
10584   int element;
10585   int *value;
10586   char *text;
10587 } elements_with_counter[] =
10588 {
10589   { EL_EMERALD,                 &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10590   { EL_BD_DIAMOND,              &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10591   { EL_BD_DIAMOND,              &level.score[SC_DIAMOND_EXTRA], TEXT_COLLECTING_EXTRA   },
10592   { EL_EMERALD_YELLOW,          &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10593   { EL_EMERALD_RED,             &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10594   { EL_EMERALD_PURPLE,          &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10595   { EL_SP_INFOTRON,             &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10596   { EL_DIAMOND,                 &level.score[SC_DIAMOND],       TEXT_COLLECTING         },
10597   { EL_CRYSTAL,                 &level.score[SC_CRYSTAL],       TEXT_COLLECTING         },
10598   { EL_PEARL,                   &level.score[SC_PEARL],         TEXT_COLLECTING         },
10599   { EL_BUG,                     &level.score[SC_BUG],           TEXT_SMASHING           },
10600   { EL_BUG_RIGHT,               &level.score[SC_BUG],           TEXT_SMASHING           },
10601   { EL_BUG_UP,                  &level.score[SC_BUG],           TEXT_SMASHING           },
10602   { EL_BUG_LEFT,                &level.score[SC_BUG],           TEXT_SMASHING           },
10603   { EL_BUG_DOWN,                &level.score[SC_BUG],           TEXT_SMASHING           },
10604   { EL_BD_BUTTERFLY,            &level.score[SC_BUG],           TEXT_SMASHING           },
10605   { EL_BD_BUTTERFLY_RIGHT,      &level.score[SC_BUG],           TEXT_SMASHING           },
10606   { EL_BD_BUTTERFLY_UP,         &level.score[SC_BUG],           TEXT_SMASHING           },
10607   { EL_BD_BUTTERFLY_LEFT,       &level.score[SC_BUG],           TEXT_SMASHING           },
10608   { EL_BD_BUTTERFLY_DOWN,       &level.score[SC_BUG],           TEXT_SMASHING           },
10609   { EL_SP_ELECTRON,             &level.score[SC_BUG],           TEXT_SMASHING           },
10610   { EL_SPACESHIP,               &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10611   { EL_SPACESHIP_RIGHT,         &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10612   { EL_SPACESHIP_UP,            &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10613   { EL_SPACESHIP_LEFT,          &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10614   { EL_SPACESHIP_DOWN,          &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10615   { EL_BD_FIREFLY,              &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10616   { EL_BD_FIREFLY_RIGHT,        &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10617   { EL_BD_FIREFLY_UP,           &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10618   { EL_BD_FIREFLY_LEFT,         &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10619   { EL_BD_FIREFLY_DOWN,         &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10620   { EL_SP_SNIKSNAK,             &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10621   { EL_YAMYAM,                  &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10622   { EL_YAMYAM_LEFT,             &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10623   { EL_YAMYAM_RIGHT,            &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10624   { EL_YAMYAM_UP,               &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10625   { EL_YAMYAM_DOWN,             &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10626   { EL_DARK_YAMYAM,             &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10627   { EL_ROBOT,                   &level.score[SC_ROBOT],         TEXT_SMASHING           },
10628   { EL_PACMAN,                  &level.score[SC_PACMAN],        TEXT_SMASHING           },
10629   { EL_PACMAN_RIGHT,            &level.score[SC_PACMAN],        TEXT_SMASHING           },
10630   { EL_PACMAN_UP,               &level.score[SC_PACMAN],        TEXT_SMASHING           },
10631   { EL_PACMAN_LEFT,             &level.score[SC_PACMAN],        TEXT_SMASHING           },
10632   { EL_PACMAN_DOWN,             &level.score[SC_PACMAN],        TEXT_SMASHING           },
10633   { EL_NUT,                     &level.score[SC_NUT],           TEXT_CRACKING           },
10634   { EL_DYNAMITE,                &level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10635   { EL_EM_DYNAMITE,             &level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10636   { EL_DYNABOMB_INCREASE_NUMBER,&level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10637   { EL_DYNABOMB_INCREASE_SIZE,  &level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10638   { EL_DYNABOMB_INCREASE_POWER, &level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10639   { EL_SHIELD_NORMAL,           &level.score[SC_SHIELD],        TEXT_COLLECTING         },
10640   { EL_SHIELD_DEADLY,           &level.score[SC_SHIELD],        TEXT_COLLECTING         },
10641   { EL_EXTRA_TIME,              &level.extra_time_score,        TEXT_COLLECTING         },
10642   { EL_KEY_1,                   &level.score[SC_KEY],           TEXT_COLLECTING         },
10643   { EL_KEY_2,                   &level.score[SC_KEY],           TEXT_COLLECTING         },
10644   { EL_KEY_3,                   &level.score[SC_KEY],           TEXT_COLLECTING         },
10645   { EL_KEY_4,                   &level.score[SC_KEY],           TEXT_COLLECTING         },
10646   { EL_EM_KEY_1,                &level.score[SC_KEY],           TEXT_COLLECTING         },
10647   { EL_EM_KEY_2,                &level.score[SC_KEY],           TEXT_COLLECTING         },
10648   { EL_EM_KEY_3,                &level.score[SC_KEY],           TEXT_COLLECTING         },
10649   { EL_EM_KEY_4,                &level.score[SC_KEY],           TEXT_COLLECTING         },
10650   { EL_EMC_KEY_5,               &level.score[SC_KEY],           TEXT_COLLECTING         },
10651   { EL_EMC_KEY_6,               &level.score[SC_KEY],           TEXT_COLLECTING         },
10652   { EL_EMC_KEY_7,               &level.score[SC_KEY],           TEXT_COLLECTING         },
10653   { EL_EMC_KEY_8,               &level.score[SC_KEY],           TEXT_COLLECTING         },
10654   { EL_DC_KEY_WHITE,            &level.score[SC_KEY],           TEXT_COLLECTING         },
10655   { EL_MM_KETTLE,               &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10656   { EL_DF_CELL,                 &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10657   { EL_MM_KEY,                  &level.score[SC_KEY],           TEXT_COLLECTING         },
10658   { EL_MM_LIGHTBALL,            &level.score[SC_ELEM_BONUS],    TEXT_COLLECTING         },
10659   { EL_MM_PACMAN,               &level.score[SC_PACMAN],        TEXT_SMASHING           },
10660   { EL_MM_PACMAN_RIGHT,         &level.score[SC_PACMAN],        TEXT_SMASHING           },
10661   { EL_MM_PACMAN_UP,            &level.score[SC_PACMAN],        TEXT_SMASHING           },
10662   { EL_MM_PACMAN_LEFT,          &level.score[SC_PACMAN],        TEXT_SMASHING           },
10663   { EL_MM_PACMAN_DOWN,          &level.score[SC_PACMAN],        TEXT_SMASHING           },
10664   { EL_AMOEBA_WET,              &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10665   { EL_AMOEBA_DRY,              &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10666   { EL_AMOEBA_FULL,             &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10667   { EL_BD_AMOEBA,               &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10668   { EL_EMC_DRIPPER,             &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10669   { EL_MAGIC_WALL,              &level.time_magic_wall,         TEXT_DURATION           },
10670   { EL_BD_MAGIC_WALL,           &level.time_magic_wall,         TEXT_DURATION           },
10671   { EL_DC_MAGIC_WALL,           &level.time_magic_wall,         TEXT_DURATION           },
10672   { EL_ROBOT_WHEEL,             &level.time_wheel,              TEXT_DURATION           },
10673   { EL_TIMEGATE_SWITCH,         &level.time_timegate,           TEXT_DURATION           },
10674   { EL_DC_TIMEGATE_SWITCH,      &level.time_timegate,           TEXT_DURATION           },
10675   { EL_LIGHT_SWITCH,            &level.time_light,              TEXT_DURATION           },
10676   { EL_LIGHT_SWITCH_ACTIVE,     &level.time_light,              TEXT_DURATION           },
10677   { EL_SHIELD_NORMAL,           &level.shield_normal_time,      TEXT_DURATION           },
10678   { EL_SHIELD_DEADLY,           &level.shield_deadly_time,      TEXT_DURATION           },
10679   { EL_EXTRA_TIME,              &level.extra_time,              TEXT_TIME_BONUS         },
10680   { EL_TIME_ORB_FULL,           &level.time_orb_time,           TEXT_TIME_BONUS         },
10681   { EL_GAME_OF_LIFE,            &level.game_of_life[0],         TEXT_GAME_OF_LIFE_1     },
10682   { EL_GAME_OF_LIFE,            &level.game_of_life[1],         TEXT_GAME_OF_LIFE_2     },
10683   { EL_GAME_OF_LIFE,            &level.game_of_life[2],         TEXT_GAME_OF_LIFE_3     },
10684   { EL_GAME_OF_LIFE,            &level.game_of_life[3],         TEXT_GAME_OF_LIFE_4     },
10685   { EL_BIOMAZE,                 &level.biomaze[0],              TEXT_GAME_OF_LIFE_1     },
10686   { EL_BIOMAZE,                 &level.biomaze[1],              TEXT_GAME_OF_LIFE_2     },
10687   { EL_BIOMAZE,                 &level.biomaze[2],              TEXT_GAME_OF_LIFE_3     },
10688   { EL_BIOMAZE,                 &level.biomaze[3],              TEXT_GAME_OF_LIFE_4     },
10689   { EL_EMC_ANDROID,             &level.android_move_time,       TEXT_MOVE_SPEED         },
10690   { EL_EMC_ANDROID,             &level.android_clone_time,      TEXT_CLONE_SPEED        },
10691   { EL_EMC_MAGIC_BALL,          &level.ball_time,               TEXT_BALL_DELAY         },
10692   { EL_EMC_LENSES,              &level.lenses_score,            TEXT_COLLECTING         },
10693   { EL_EMC_MAGNIFIER,           &level.magnify_score,           TEXT_COLLECTING         },
10694   { EL_SPRING,                  &level.slurp_score,             TEXT_SLURPING           },
10695   { EL_SPRING_LEFT,             &level.slurp_score,             TEXT_SLURPING           },
10696   { EL_SPRING_RIGHT,            &level.slurp_score,             TEXT_SLURPING           },
10697   { EL_EMC_LENSES,              &level.lenses_time,             TEXT_DURATION           },
10698   { EL_EMC_MAGNIFIER,           &level.magnify_time,            TEXT_DURATION           },
10699   { EL_MM_FUSE_ACTIVE,          &level.mm_time_fuse,            TEXT_DELAY_OFF          },
10700   { EL_MM_BOMB,                 &level.mm_time_bomb,            TEXT_DELAY_EXPLODING    },
10701   { EL_MM_GRAY_BALL,            &level.mm_time_ball,            TEXT_DELAY_CHANGING     },
10702   { EL_MM_STEEL_BLOCK,          &level.mm_time_block,           TEXT_DELAY_MOVING       },
10703   { EL_MM_WOODEN_BLOCK,         &level.mm_time_block,           TEXT_DELAY_MOVING       },
10704
10705   { -1,                         NULL,                           NULL                    }
10706 };
10707
10708 static boolean checkPropertiesConfig(int element)
10709 {
10710   int i;
10711
10712   // special case: empty space customization only available in R'n'D game engine
10713   if (element == EL_EMPTY_SPACE && level.game_engine_type != GAME_ENGINE_TYPE_RND)
10714     return FALSE;
10715
10716   if (IS_GEM(element) ||
10717       IS_CUSTOM_ELEMENT(element) ||
10718       IS_GROUP_ELEMENT(element) ||
10719       IS_EMPTY_ELEMENT(element) ||
10720       IS_BALLOON_ELEMENT(element) ||
10721       IS_ENVELOPE(element) ||
10722       IS_MM_ENVELOPE(element) ||
10723       IS_MM_MCDUFFIN(element) ||
10724       IS_DF_LASER(element) ||
10725       IS_PLAYER_ELEMENT(element) ||
10726       IS_BD_PLAYER_ELEMENT(element) ||
10727       IS_SOKOBAN_OBJECT_OR_FIELD(element) ||
10728       HAS_EDITOR_CONTENT(element) ||
10729       CAN_GROW(element) ||
10730       COULD_MOVE_INTO_ACID(element) ||
10731       MAYBE_DONT_COLLIDE_WITH(element))
10732     return TRUE;
10733   else
10734     for (i = 0; elements_with_counter[i].element != -1; i++)
10735       if (elements_with_counter[i].element == element)
10736         return TRUE;
10737
10738   return FALSE;
10739 }
10740
10741 static void SetAutomaticNumberOfGemsNeeded(void)
10742 {
10743   int x, y;
10744
10745   if (!level.auto_count_gems)
10746     return;
10747
10748   level.gems_needed = 0;
10749
10750   for (x = 0; x < lev_fieldx; x++)
10751   {
10752     for (y = 0; y < lev_fieldy; y++)
10753     {
10754       int element = Tile[x][y];
10755
10756       switch (element)
10757       {
10758         case EL_EMERALD:
10759         case EL_EMERALD_YELLOW:
10760         case EL_EMERALD_RED:
10761         case EL_EMERALD_PURPLE:
10762         case EL_BD_DIAMOND:
10763         case EL_WALL_EMERALD:
10764         case EL_WALL_EMERALD_YELLOW:
10765         case EL_WALL_EMERALD_RED:
10766         case EL_WALL_EMERALD_PURPLE:
10767         case EL_WALL_BD_DIAMOND:
10768         case EL_NUT:
10769         case EL_SP_INFOTRON:
10770         case EL_MM_KETTLE:
10771         case EL_DF_CELL:
10772           level.gems_needed++;
10773           break;
10774
10775         case EL_DIAMOND:
10776         case EL_WALL_DIAMOND:
10777           level.gems_needed += 3;
10778           break;
10779
10780         case EL_PEARL:
10781         case EL_WALL_PEARL:
10782           level.gems_needed += 5;
10783           break;
10784
10785         case EL_CRYSTAL:
10786         case EL_WALL_CRYSTAL:
10787           level.gems_needed += 8;
10788           break;
10789
10790         default:
10791           break;
10792       }
10793     }
10794   }
10795
10796   ModifyEditorCounterValue(ED_COUNTER_ID_LEVEL_GEMSLIMIT, level.gems_needed);
10797 }
10798
10799 static void DrawPropertiesConfig(void)
10800 {
10801   boolean draw_footer_line = FALSE;
10802   int max_num_element_counters = 4;
10803   int num_element_counters = 0;
10804   int i;
10805
10806   if (!checkPropertiesConfig(properties_element))
10807   {
10808     int xpos = ED_ELEMENT_SETTINGS_X(0);
10809     int ypos = ED_ELEMENT_SETTINGS_Y(0) + ED_GADGET_SMALL_DISTANCE;
10810
10811     PrintInfoText("No configuration options available.",
10812                   FONT_TEXT_1, xpos, ypos);
10813
10814     return;
10815   }
10816
10817   // check if there are elements where a value can be chosen for
10818   for (i = 0; elements_with_counter[i].element != -1; i++)
10819   {
10820     if (elements_with_counter[i].element == properties_element)
10821     {
10822       int counter_id = ED_COUNTER_ID_ELEMENT_VALUE1 + num_element_counters;
10823
10824       counterbutton_info[counter_id].y =
10825         ED_ELEMENT_SETTINGS_YPOS(
10826                 (HAS_EDITOR_CONTENT(properties_element)      ? 1 : 0) +
10827                 (CAN_GROW(properties_element)                ? 1 : 0) +
10828                 (COULD_MOVE_INTO_ACID(properties_element)    ? 1 : 0) +
10829                 (MAYBE_DONT_COLLIDE_WITH(properties_element) ? 1 : 0) +
10830                 (properties_element == EL_EMC_MAGIC_BALL     ? 2 : 0) +
10831                 num_element_counters);
10832
10833       counterbutton_info[counter_id].value = elements_with_counter[i].value;
10834       counterbutton_info[counter_id].text_right = elements_with_counter[i].text;
10835
10836       if (properties_element == EL_GAME_OF_LIFE ||
10837           properties_element == EL_BIOMAZE)
10838       {
10839         counterbutton_info[counter_id].min_value = 0;   // min neighbours
10840         counterbutton_info[counter_id].max_value = 8;   // max neighbours
10841       }
10842       else
10843       {
10844         // !!! CHANGE THIS FOR CERTAIN ELEMENTS !!!
10845         counterbutton_info[counter_id].min_value = MIN_SCORE;
10846         counterbutton_info[counter_id].max_value = MAX_SCORE;
10847       }
10848
10849       MapCounterButtons(counter_id);
10850
10851       num_element_counters++;
10852       if (num_element_counters >= max_num_element_counters)
10853         break;
10854     }
10855   }
10856
10857   if (HAS_EDITOR_CONTENT(properties_element))
10858   {
10859     // draw stickybutton gadget
10860     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
10861
10862     if (IS_AMOEBOID(properties_element))
10863       MapDrawingArea(ED_DRAWING_ID_AMOEBA_CONTENT);
10864     else if (properties_element == EL_YAMYAM ||
10865              properties_element == EL_YAMYAM_LEFT ||
10866              properties_element == EL_YAMYAM_RIGHT ||
10867              properties_element == EL_YAMYAM_UP ||
10868              properties_element == EL_YAMYAM_DOWN)
10869       DrawYamYamContentAreas();
10870     else if (properties_element == EL_EMC_MAGIC_BALL)
10871     {
10872       DrawMagicBallContentAreas();
10873
10874       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_RANDOM_BALL_CONTENT);
10875       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_INITIAL_BALL_ACTIVE);
10876     }
10877     else if (properties_element == EL_EMC_ANDROID)
10878       DrawAndroidElementArea();
10879     else if (properties_element == EL_MM_GRAY_BALL)
10880     {
10881       MapCounterButtons(ED_COUNTER_ID_MM_BALL_CONTENT);
10882       MapSelectboxGadget(ED_SELECTBOX_ID_MM_BALL_CHOICE_MODE);
10883       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_ROTATE_MM_BALL_CONTENT);
10884       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_EXPLODE_MM_BALL);
10885
10886       DrawMMBallContentArea();
10887     }
10888   }
10889
10890   if (IS_PLAYER_ELEMENT(properties_element))
10891   {
10892     int player_nr = GET_PLAYER_NR(properties_element);
10893
10894     // these properties can be set for every player individually
10895
10896     if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_1)
10897     {
10898       drawingarea_info[ED_DRAWING_ID_START_ELEMENT].value =
10899         &level.start_element[player_nr];
10900       drawingarea_info[ED_DRAWING_ID_ARTWORK_ELEMENT].value =
10901         &level.artwork_element[player_nr];
10902       drawingarea_info[ED_DRAWING_ID_EXPLOSION_ELEMENT].value =
10903         &level.explosion_element[player_nr];
10904
10905       checkbutton_info[ED_CHECKBUTTON_ID_USE_START_ELEMENT].value =
10906         &level.use_start_element[player_nr];
10907       checkbutton_info[ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT].value =
10908         &level.use_artwork_element[player_nr];
10909       checkbutton_info[ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT].value =
10910         &level.use_explosion_element[player_nr];
10911       checkbutton_info[ED_CHECKBUTTON_ID_INITIAL_GRAVITY].value =
10912         &level.initial_player_gravity[player_nr];
10913
10914       selectbox_info[ED_SELECTBOX_ID_PLAYER_SPEED].value =
10915         &level.initial_player_stepsize[player_nr];
10916
10917       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID);
10918       MapCheckbuttonGadget(properties_element == EL_SP_MURPHY ?
10919                            ED_CHECKBUTTON_ID_SP_BLOCK_LAST_FIELD :
10920                            ED_CHECKBUTTON_ID_BLOCK_LAST_FIELD);
10921       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BLOCK_SNAP_FIELD);
10922       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CONTINUOUS_SNAPPING);
10923       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_INSTANT_RELOCATION);
10924       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SHIFTED_RELOCATION);
10925       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_LAZY_RELOCATION);
10926       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_START_ELEMENT);
10927       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT);
10928       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT);
10929       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_INITIAL_GRAVITY);
10930       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CAN_PASS_TO_WALKABLE);
10931       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SOLVED_BY_ONE_PLAYER);
10932
10933       MapDrawingArea(ED_DRAWING_ID_START_ELEMENT);
10934       MapDrawingArea(ED_DRAWING_ID_ARTWORK_ELEMENT);
10935       MapDrawingArea(ED_DRAWING_ID_EXPLOSION_ELEMENT);
10936
10937       MapSelectboxGadget(ED_SELECTBOX_ID_PLAYER_SPEED);
10938     }
10939     else if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_2)
10940     {
10941       drawingarea_info[ED_DRAWING_ID_INVENTORY_CONTENT].value =
10942         &level.initial_inventory_content[player_nr][0];
10943
10944       counterbutton_info[ED_COUNTER_ID_INVENTORY_SIZE].value =
10945         &level.initial_inventory_size[player_nr];
10946
10947       checkbutton_info[ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY].value =
10948         &level.use_initial_inventory[player_nr];
10949
10950       // draw checkbutton gadgets
10951       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY);
10952       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_FINISH_DIG_COLLECT);
10953       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_KEEP_WALKABLE_CE);
10954
10955       // draw counter gadgets
10956       MapCounterButtons(ED_COUNTER_ID_INVENTORY_SIZE);
10957
10958       // draw drawing area gadgets
10959       DrawPlayerInitialInventoryArea(properties_element);
10960     }
10961   }
10962
10963   if (IS_BD_PLAYER_ELEMENT(properties_element))
10964     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BD_DIAGONAL_MOVEMENTS);
10965
10966   // special case: slippery walls option for gems only available in R'n'D game engine
10967   if (IS_GEM(properties_element) && level.game_engine_type == GAME_ENGINE_TYPE_RND)
10968   {
10969     checkbutton_info[ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS].y =
10970       ED_ELEMENT_SETTINGS_XPOS(properties_element == EL_BD_DIAMOND ? 2 : 1);
10971
10972     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS);
10973   }
10974
10975   if (properties_element == EL_EM_DYNAMITE)
10976     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_EM_EXPLODES_BY_FIRE);
10977
10978   if (COULD_MOVE_INTO_ACID(properties_element) &&
10979       !IS_PLAYER_ELEMENT(properties_element) &&
10980       (!IS_CUSTOM_ELEMENT(properties_element) ||
10981        edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_2))
10982   {
10983     // set position for checkbutton for "can move into acid"
10984     checkbutton_info[ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID].x =
10985       ED_ELEMENT_SETTINGS_XPOS(IS_CUSTOM_ELEMENT(properties_element) ? 1 : 0);
10986     checkbutton_info[ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID].y =
10987       ED_ELEMENT_SETTINGS_YPOS(IS_CUSTOM_ELEMENT(properties_element) ? 7 :
10988                                IS_BALLOON_ELEMENT(properties_element) ||
10989                                HAS_EDITOR_CONTENT(properties_element) ? 1 : 0);
10990
10991     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID);
10992   }
10993
10994   if (MAYBE_DONT_COLLIDE_WITH(properties_element))
10995     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH);
10996
10997   if (properties_element == EL_SPRING ||
10998       properties_element == EL_SPRING_LEFT ||
10999       properties_element == EL_SPRING_RIGHT)
11000     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_SPRING_BUG);
11001
11002   if (properties_element == EL_TIME_ORB_FULL)
11003     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_TIME_ORB_BUG);
11004
11005   if (properties_element == EL_GAME_OF_LIFE ||
11006       properties_element == EL_BIOMAZE)
11007     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_LIFE_BUGS);
11008
11009   if (CAN_GROW(properties_element))
11010   {
11011     checkbutton_info[ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE].y =
11012       ED_ELEMENT_SETTINGS_YPOS(HAS_EDITOR_CONTENT(properties_element) ? 1 : 0);
11013
11014     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE);
11015   }
11016
11017   if (properties_element == EL_SOKOBAN_FIELD_EMPTY)
11018     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SB_FIELDS_NEEDED);
11019
11020   if (properties_element == EL_SOKOBAN_OBJECT)
11021     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SB_OBJECTS_NEEDED);
11022
11023   if (properties_element == EL_SOKOBAN_OBJECT ||
11024       properties_element == EL_SOKOBAN_FIELD_EMPTY ||
11025       properties_element == EL_SOKOBAN_FIELD_FULL)
11026   {
11027     checkbutton_info[ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN].y =
11028       ED_ELEMENT_SETTINGS_XPOS(properties_element == EL_SOKOBAN_FIELD_FULL ?
11029                                0 : 1);
11030
11031     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN);
11032   }
11033
11034   if (IS_BALLOON_ELEMENT(properties_element))
11035     MapSelectboxGadget(ED_SELECTBOX_ID_WIND_DIRECTION);
11036
11037   if (IS_ENVELOPE(properties_element) ||
11038       IS_MM_ENVELOPE(properties_element))
11039   {
11040     int counter1_id = ED_COUNTER_ID_ENVELOPE_XSIZE;
11041     int counter2_id = ED_COUNTER_ID_ENVELOPE_YSIZE;
11042     int button1_id = ED_CHECKBUTTON_ID_ENVELOPE_AUTOWRAP;
11043     int button2_id = ED_CHECKBUTTON_ID_ENVELOPE_CENTERED;
11044     int envelope_nr = ENVELOPE_NR(properties_element);
11045
11046     counterbutton_info[counter1_id].value = &level.envelope[envelope_nr].xsize;
11047     counterbutton_info[counter2_id].value = &level.envelope[envelope_nr].ysize;
11048
11049     checkbutton_info[button1_id].value = &level.envelope[envelope_nr].autowrap;
11050     checkbutton_info[button2_id].value = &level.envelope[envelope_nr].centered;
11051
11052     // display counter to choose size of envelope text area
11053     MapCounterButtons(counter1_id);
11054     MapCounterButtons(counter2_id);
11055
11056     // display checkbuttons to choose auto-wrap and alignment properties
11057     MapCheckbuttonGadget(button1_id);
11058     MapCheckbuttonGadget(button2_id);
11059
11060     DrawEnvelopeTextArea(envelope_nr);
11061   }
11062
11063   if (IS_MM_MCDUFFIN(properties_element))
11064   {
11065     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_MM_LASER_RED);
11066     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_MM_LASER_GREEN);
11067     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_MM_LASER_BLUE);
11068   }
11069
11070   if (IS_DF_LASER(properties_element))
11071   {
11072     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DF_LASER_RED);
11073     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DF_LASER_GREEN);
11074     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DF_LASER_BLUE);
11075   }
11076
11077   if (IS_CUSTOM_ELEMENT(properties_element))
11078   {
11079     // draw stickybutton gadget
11080     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11081
11082     if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_1)
11083     {
11084       // draw checkbutton gadgets
11085       for (i =  ED_CHECKBUTTON_ID_CUSTOM1_FIRST;
11086            i <= ED_CHECKBUTTON_ID_CUSTOM1_LAST; i++)
11087         MapCheckbuttonGadget(i);
11088
11089       // draw counter gadgets
11090       for (i =  ED_COUNTER_ID_CUSTOM1_FIRST;
11091            i <= ED_COUNTER_ID_CUSTOM1_LAST; i++)
11092         MapCounterButtons(i);
11093
11094       // draw selectbox gadgets
11095       for (i =  ED_SELECTBOX_ID_CUSTOM1_FIRST;
11096            i <= ED_SELECTBOX_ID_CUSTOM1_LAST; i++)
11097         MapSelectboxGadget(i);
11098
11099       // draw textbutton gadgets
11100       MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1);
11101
11102       // draw text input gadgets
11103       MapTextInputGadget(ED_TEXTINPUT_ID_ELEMENT_NAME);
11104
11105       // draw drawing area gadgets
11106       MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
11107
11108       draw_footer_line = TRUE;
11109     }
11110     else if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_2)
11111     {
11112       // draw checkbutton gadgets
11113       for (i =  ED_CHECKBUTTON_ID_CUSTOM2_FIRST;
11114            i <= ED_CHECKBUTTON_ID_CUSTOM2_LAST; i++)
11115         MapCheckbuttonGadget(i);
11116
11117       // draw counter gadgets
11118       for (i =  ED_COUNTER_ID_CUSTOM2_FIRST;
11119            i <= ED_COUNTER_ID_CUSTOM2_LAST; i++)
11120         MapCounterButtons(i);
11121
11122       // draw selectbox gadgets
11123       for (i =  ED_SELECTBOX_ID_CUSTOM2_FIRST;
11124            i <= ED_SELECTBOX_ID_CUSTOM2_LAST; i++)
11125         MapSelectboxGadget(i);
11126
11127       // draw drawing area gadgets
11128       MapDrawingArea(ED_DRAWING_ID_CUSTOM_MOVE_ENTER);
11129       MapDrawingArea(ED_DRAWING_ID_CUSTOM_MOVE_LEAVE);
11130       DrawCustomContentArea();
11131     }
11132   }
11133   else if (IS_GROUP_ELEMENT(properties_element))
11134   {
11135     // draw stickybutton gadget
11136     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11137
11138     // draw checkbutton gadgets
11139     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC);
11140     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1);
11141
11142     // draw counter gadgets
11143     MapCounterButtons(ED_COUNTER_ID_GROUP_CONTENT);
11144
11145     // draw selectbox gadgets
11146     MapSelectboxGadget(ED_SELECTBOX_ID_GROUP_CHOICE_MODE);
11147
11148     // draw textbutton gadgets
11149     MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1);
11150
11151     // draw drawing area gadgets
11152     DrawGroupElementArea();
11153
11154     // draw text input gadgets
11155     MapTextInputGadget(ED_TEXTINPUT_ID_ELEMENT_NAME);
11156
11157     // draw drawing area gadgets
11158     MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
11159
11160     draw_footer_line = TRUE;
11161   }
11162   else if (IS_EMPTY_ELEMENT(properties_element))
11163   {
11164     // draw stickybutton gadget
11165     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11166
11167     // draw checkbutton gadgets
11168     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC);
11169     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1);
11170
11171     // draw textbutton gadgets
11172     MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1);
11173
11174     // draw drawing area gadgets
11175     MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
11176
11177     draw_footer_line = TRUE;
11178   }
11179
11180   // draw little footer border line above CE/GE use/save template gadgets
11181   if (draw_footer_line)
11182   {
11183     struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_PROPERTIES_INFO];
11184     struct GadgetDesign *gd = &gd_gi1->alt_design[GD_BUTTON_UNPRESSED];
11185     int gd_x = gd->x + gd_gi1->border.width / 2;
11186     int gd_y = gd->y + gd_gi1->height - 1;
11187     Pixel tab_color = GetPixel(gd->bitmap, gd_x, gd_y);
11188
11189     if (tab_color != BLACK_PIXEL)               // black => transparent
11190       FillRectangle(drawto,
11191                     SX + ED_ELEMENT_SETTINGS_X(0),
11192                     SY + ED_ELEMENT_SETTINGS_Y(14) - ED_SETTINGS_TABS_YOFFSET -
11193                     ED_TAB_BAR_HEIGHT,
11194                     getTabulatorBarWidth(), getTabulatorBarHeight(), tab_color);
11195   }
11196 }
11197
11198 static void DrawPropertiesChangeDrawingAreas(void)
11199 {
11200   if (IS_CUSTOM_ELEMENT(properties_element))
11201   {
11202     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_TARGET);
11203     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_TRIGGER);
11204     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_ACTION);
11205
11206     DrawCustomChangeContentArea();
11207   }
11208
11209   redraw_mask |= REDRAW_FIELD;
11210 }
11211
11212 static void DrawPropertiesChange(void)
11213 {
11214   int i;
11215
11216   // needed to initially set selectbox options for special action options
11217   setSelectboxSpecialActionOptions();
11218
11219   // draw stickybutton gadget
11220   MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11221
11222   // draw checkbutton gadgets
11223   for (i =  ED_CHECKBUTTON_ID_CHANGE_FIRST;
11224        i <= ED_CHECKBUTTON_ID_CHANGE_LAST; i++)
11225     MapCheckbuttonGadget(i);
11226
11227   // draw counter gadgets
11228   for (i =  ED_COUNTER_ID_CHANGE_FIRST;
11229        i <= ED_COUNTER_ID_CHANGE_LAST; i++)
11230     MapCounterButtons(i);
11231
11232   // draw selectbox gadgets
11233   for (i =  ED_SELECTBOX_ID_CHANGE_FIRST;
11234        i <= ED_SELECTBOX_ID_CHANGE_LAST; i++)
11235     MapSelectboxGadget(i);
11236
11237   // draw textbutton gadgets
11238   for (i =  ED_TEXTBUTTON_ID_CHANGE_FIRST;
11239        i <= ED_TEXTBUTTON_ID_CHANGE_LAST; i++)
11240     MapTextbuttonGadget(i);
11241
11242   // draw graphicbutton gadgets
11243   for (i =  ED_GRAPHICBUTTON_ID_CHANGE_FIRST;
11244        i <= ED_GRAPHICBUTTON_ID_CHANGE_LAST; i++)
11245     MapGraphicbuttonGadget(i);
11246
11247   // draw drawing area gadgets
11248   DrawPropertiesChangeDrawingAreas();
11249 }
11250
11251 static void DrawEditorElementAnimation(int x, int y)
11252 {
11253   int graphic = el2img(properties_element);
11254   int frame = (ANIM_MODE(graphic) == ANIM_CE_VALUE ?
11255                custom_element.ce_value_fixed_initial :
11256                ANIM_MODE(graphic) == ANIM_CE_SCORE ?
11257                custom_element.collect_score_initial : FrameCounter);
11258
11259   DrawFixedGraphicAnimationExt(drawto, x, y, graphic, frame, NO_MASKING);
11260 }
11261
11262 static void DrawEditorElementName(int x, int y, int font_nr)
11263 {
11264   char *element_name = getElementInfoText(properties_element);
11265   int font_width = getFontWidth(font_nr);
11266   int font_height = getFontHeight(font_nr);
11267   int max_text_width = SXSIZE - x - ED_ELEMENT_SETTINGS_X(0);
11268   int max_chars_per_line = max_text_width / font_width;
11269
11270   if (strlen(element_name) <= max_chars_per_line)
11271     DrawTextS(x, y, font_nr, element_name);
11272   else
11273   {
11274     char buffer[max_chars_per_line + 1];
11275     int next_pos = max_chars_per_line;
11276
11277     strncpy(buffer, element_name, max_chars_per_line);
11278     buffer[max_chars_per_line] = '\0';
11279
11280     if (element_name[max_chars_per_line] == ' ')
11281       next_pos++;
11282     else
11283     {
11284       int i;
11285
11286       for (i = max_chars_per_line - 1; i >= 0; i--)
11287         if (buffer[i] == ' ')
11288           break;
11289
11290       if (strlen(&element_name[i + 1]) <= max_chars_per_line)
11291       {
11292         buffer[i] = '\0';
11293         next_pos = i + 1;
11294       }
11295     }
11296
11297     DrawTextS(x, y - font_height / 2, font_nr, buffer);
11298
11299     strncpy(buffer, &element_name[next_pos], max_chars_per_line);
11300     buffer[max_chars_per_line] = '\0';
11301
11302     DrawTextS(x, y + font_height / 2, font_nr, buffer);
11303   }
11304 }
11305
11306 static void DrawPropertiesWindow(void)
11307 {
11308   struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_INPUT_TEXT];
11309   int element_border = graphic_info[IMG_EDITOR_ELEMENT_BORDER].border_size;
11310   int border_size = gd->border_size;
11311   int font_nr = FONT_TEXT_1;
11312   int font_height = getFontHeight(font_nr);
11313   int xoffset = TILEX + element_border + 3 * border_size;
11314   int yoffset = (TILEY - font_height) / 2;
11315   int x1 = editor.settings.element_graphic.x + element_border;
11316   int y1 = editor.settings.element_graphic.y + element_border;
11317   int x2 = (editor.settings.element_name.x == -1 ? x1 + xoffset :
11318             editor.settings.element_name.x);
11319   int y2 = (editor.settings.element_name.y == -1 ? y1 + yoffset :
11320             editor.settings.element_name.y);
11321   char *text = "Element Settings";
11322   int font2_nr = FONT_TITLE_1;
11323   struct MenuPosInfo *pos = &editor.settings.headline;
11324   int sx = SX + ALIGNED_XPOS(pos->x, getTextWidth(text, font2_nr), pos->align);
11325   int sy = SY + pos->y;
11326
11327   stick_element_properties_window = FALSE;
11328
11329   // make sure that previous properties edit mode exists for this element
11330   if (edit_mode_properties > ED_MODE_PROPERTIES_CONFIG_2 &&
11331       !IS_CUSTOM_ELEMENT(properties_element))
11332     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG_2;
11333
11334   if (edit_mode_properties > ED_MODE_PROPERTIES_CONFIG &&
11335       !IS_PLAYER_ELEMENT(properties_element) &&
11336       !IS_CUSTOM_ELEMENT(properties_element))
11337     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG;
11338
11339   if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG &&
11340       (IS_PLAYER_ELEMENT(properties_element) ||
11341        IS_CUSTOM_ELEMENT(properties_element)))
11342     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG_1;
11343
11344   CopyElementPropertiesToEditor(properties_element);
11345
11346   UnmapLevelEditorFieldGadgets();
11347   UnmapLevelEditorToolboxDrawingGadgets();
11348   UnmapLevelEditorToolboxCustomGadgets();
11349
11350   MapLevelEditorToolboxCustomGadgetsIfNeeded();
11351
11352   SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
11353   ClearField();
11354
11355   DrawText(sx, sy, text, font2_nr);
11356
11357   FrameCounter = 0;     // restart animation frame counter
11358
11359   DrawElementBorder(SX + x1, SY + y1, TILEX, TILEY, FALSE);
11360   DrawEditorElementAnimation(SX + x1, SY + y1);
11361   DrawEditorElementName(x2, y2, font_nr);
11362
11363   DrawPropertiesTabulatorGadgets();
11364
11365   if (edit_mode_properties == ED_MODE_PROPERTIES_INFO)
11366     DrawPropertiesInfo();
11367   else if (edit_mode_properties == ED_MODE_PROPERTIES_CHANGE)
11368     DrawPropertiesChange();
11369   else  // (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG[_1|_2])
11370     DrawPropertiesConfig();
11371 }
11372
11373 static void DrawPaletteWindow(void)
11374 {
11375   int i;
11376
11377   UnmapLevelEditorFieldGadgets();
11378
11379   SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
11380   ClearField();
11381
11382   // map buttons to select elements
11383   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
11384     MapGadget(level_editor_gadget[GADGET_ID_ELEMENTLIST_FIRST + i]);
11385   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL]);
11386   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_UP]);
11387   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_DOWN]);
11388 }
11389
11390 static void UpdateCustomElementGraphicGadgets(void)
11391 {
11392   int i;
11393
11394   InitElementPropertiesGfxElement();
11395
11396   ModifyEditorElementList();
11397   RedrawDrawingElements();
11398
11399   // force redraw of all mapped drawing area gadgets
11400   for (i = 0; i < ED_NUM_DRAWING_AREAS; i++)
11401   {
11402     struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[i].gadget_id];
11403
11404     if (gi->mapped)
11405       MapDrawingArea(i);
11406   }
11407 }
11408
11409 static int getOpenDirectionFromTube(int element)
11410 {
11411   switch (element)
11412   {
11413     case EL_TUBE_LEFT_UP:               return (MV_LEFT | MV_UP);
11414     case EL_TUBE_LEFT_DOWN:             return (MV_LEFT | MV_DOWN);
11415     case EL_TUBE_RIGHT_UP:              return (MV_RIGHT | MV_UP);
11416     case EL_TUBE_RIGHT_DOWN:            return (MV_RIGHT | MV_DOWN);
11417     case EL_TUBE_HORIZONTAL:            return (MV_HORIZONTAL);
11418     case EL_TUBE_HORIZONTAL_UP:         return (MV_HORIZONTAL | MV_UP);
11419     case EL_TUBE_HORIZONTAL_DOWN:       return (MV_HORIZONTAL | MV_DOWN);
11420     case EL_TUBE_VERTICAL:              return (MV_VERTICAL);
11421     case EL_TUBE_VERTICAL_LEFT:         return (MV_VERTICAL | MV_LEFT);
11422     case EL_TUBE_VERTICAL_RIGHT:        return (MV_VERTICAL | MV_RIGHT);
11423     case EL_TUBE_ANY:                   return (MV_ANY_DIRECTION);
11424   }
11425
11426   return MV_NONE;
11427 }
11428
11429 static int getTubeFromOpenDirection(int direction)
11430 {
11431   switch (direction)
11432   {
11433     case (MV_LEFT | MV_UP):             return EL_TUBE_LEFT_UP;
11434     case (MV_LEFT | MV_DOWN):           return EL_TUBE_LEFT_DOWN;
11435     case (MV_RIGHT | MV_UP):            return EL_TUBE_RIGHT_UP;
11436     case (MV_RIGHT | MV_DOWN):          return EL_TUBE_RIGHT_DOWN;
11437     case (MV_HORIZONTAL):               return EL_TUBE_HORIZONTAL;
11438     case (MV_HORIZONTAL | MV_UP):       return EL_TUBE_HORIZONTAL_UP;
11439     case (MV_HORIZONTAL | MV_DOWN):     return EL_TUBE_HORIZONTAL_DOWN;
11440     case (MV_VERTICAL):                 return EL_TUBE_VERTICAL;
11441     case (MV_VERTICAL | MV_LEFT):       return EL_TUBE_VERTICAL_LEFT;
11442     case (MV_VERTICAL | MV_RIGHT):      return EL_TUBE_VERTICAL_RIGHT;
11443     case (MV_ANY_DIRECTION):            return EL_TUBE_ANY;
11444
11445     // if only one direction, fall back to simple tube with that direction
11446     case (MV_LEFT):                     return EL_TUBE_HORIZONTAL;
11447     case (MV_RIGHT):                    return EL_TUBE_HORIZONTAL;
11448     case (MV_UP):                       return EL_TUBE_VERTICAL;
11449     case (MV_DOWN):                     return EL_TUBE_VERTICAL;
11450   }
11451
11452   return EL_EMPTY;
11453 }
11454
11455 static int getTubeFromOpenDirectionNotEmpty(int direction, int element_old)
11456 {
11457   int element_new = getTubeFromOpenDirection(direction);
11458
11459   return (element_new != EL_EMPTY ? element_new : element_old);
11460 }
11461
11462 static int getOpenDirectionFromBelt(int element)
11463 {
11464   int belt_dir = getBeltDirFromBeltElement(element);
11465
11466   return (belt_dir == MV_LEFT ? MV_RIGHT :
11467           belt_dir == MV_RIGHT ? MV_LEFT :
11468           belt_dir == MV_NONE ? MV_HORIZONTAL : belt_dir);
11469 }
11470
11471 static int getBeltFromNrAndOpenDirection(int nr, int direction)
11472 {
11473   int belt_dir = (direction == MV_LEFT ? MV_RIGHT :
11474                   direction == MV_RIGHT ? MV_LEFT :
11475                   direction == MV_HORIZONTAL ? MV_NONE : direction);
11476
11477   if (direction == MV_NONE)
11478     return EL_EMPTY;
11479
11480   return getBeltElementFromBeltNrAndBeltDir(nr, belt_dir);
11481 }
11482
11483 static int getBeltFromNrAndOpenDirectionNotEmpty(int nr, int direction,
11484                                                  int element_old)
11485 {
11486   int element_new = getBeltFromNrAndOpenDirection(nr, direction);
11487
11488   return (element_new != EL_EMPTY ? element_new : element_old);
11489 }
11490
11491 static int getOpenDirectionFromPool(int element)
11492 {
11493   switch (element)
11494   {
11495     case EL_ACID_POOL_TOPLEFT:          return (MV_DOWN | MV_RIGHT);
11496     case EL_ACID_POOL_TOPRIGHT:         return (MV_DOWN | MV_LEFT);
11497     case EL_ACID_POOL_BOTTOMLEFT:       return (MV_UP | MV_RIGHT);
11498     case EL_ACID_POOL_BOTTOMRIGHT:      return (MV_UP | MV_LEFT);
11499     case EL_ACID_POOL_BOTTOM:           return (MV_HORIZONTAL | MV_UP);
11500     case EL_ACID:                       return (MV_HORIZONTAL | MV_DOWN);
11501   }
11502
11503   return MV_NONE;
11504 }
11505
11506 static int getPoolFromOpenDirection(int direction)
11507 {
11508   switch (direction)
11509   {
11510     case (MV_DOWN | MV_RIGHT):          return EL_ACID_POOL_TOPLEFT;
11511     case (MV_DOWN | MV_LEFT):           return EL_ACID_POOL_TOPRIGHT;
11512     case (MV_UP | MV_RIGHT):            return EL_ACID_POOL_BOTTOMLEFT;
11513     case (MV_UP | MV_LEFT):             return EL_ACID_POOL_BOTTOMRIGHT;
11514     case (MV_HORIZONTAL | MV_UP):       return EL_ACID_POOL_BOTTOM;
11515     case (MV_HORIZONTAL | MV_DOWN):     return EL_ACID;
11516   }
11517
11518   return EL_EMPTY;
11519 }
11520
11521 static int getPoolFromOpenDirectionExt(int direction, int help_element)
11522 {
11523   int element = getPoolFromOpenDirection(direction);
11524   int help_direction = getOpenDirectionFromPool(help_element);
11525
11526   if (element == EL_EMPTY)
11527   {
11528     int help_direction_vertical = help_direction & MV_VERTICAL;
11529
11530     element = getPoolFromOpenDirection(direction | help_direction_vertical);
11531   }
11532
11533   if (element == EL_EMPTY)
11534   {
11535     int help_direction_horizontal = help_direction & MV_HORIZONTAL;
11536
11537     element = getPoolFromOpenDirection(direction | help_direction_horizontal);
11538   }
11539
11540   return element;
11541 }
11542
11543 static int getPoolFromOpenDirectionNotEmpty(int direction, int element_old)
11544 {
11545   int element_new = getPoolFromOpenDirectionExt(direction, element_old);
11546
11547   return (element_new != EL_EMPTY ? element_new : element_old);
11548 }
11549
11550 static int getOpenDirectionFromPillar(int element)
11551 {
11552   switch (element)
11553   {
11554     case EL_EMC_WALL_1:                 return (MV_DOWN);
11555     case EL_EMC_WALL_2:                 return (MV_VERTICAL);
11556     case EL_EMC_WALL_3:                 return (MV_UP);
11557   }
11558
11559   return MV_NONE;
11560 }
11561
11562 static int getPillarFromOpenDirection(int direction)
11563 {
11564   switch (direction)
11565   {
11566     case (MV_DOWN):                     return EL_EMC_WALL_1;
11567     case (MV_VERTICAL):                 return EL_EMC_WALL_2;
11568     case (MV_UP):                       return EL_EMC_WALL_3;
11569   }
11570
11571   return EL_EMPTY;
11572 }
11573
11574 static int getPillarFromOpenDirectionNotEmpty(int direction, int element_old)
11575 {
11576   int element_new = getPillarFromOpenDirection(direction);
11577
11578   return (element_new != EL_EMPTY ? element_new : element_old);
11579 }
11580
11581 static int getOpenDirectionFromSteel2(int element)
11582 {
11583   switch (element)
11584   {
11585     case EL_DC_STEELWALL_2_LEFT:        return (MV_RIGHT);
11586     case EL_DC_STEELWALL_2_RIGHT:       return (MV_LEFT);
11587     case EL_DC_STEELWALL_2_TOP:         return (MV_DOWN);
11588     case EL_DC_STEELWALL_2_BOTTOM:      return (MV_UP);
11589     case EL_DC_STEELWALL_2_HORIZONTAL:  return (MV_HORIZONTAL);
11590     case EL_DC_STEELWALL_2_VERTICAL:    return (MV_VERTICAL);
11591     case EL_DC_STEELWALL_2_MIDDLE:      return (MV_ANY_DIRECTION);
11592     case EL_DC_STEELWALL_2_SINGLE:      return (MV_NONE);
11593   }
11594
11595   return MV_NONE;
11596 }
11597
11598 static int getSteel2FromOpenDirection(int direction)
11599 {
11600   switch (direction)
11601   {
11602     case (MV_RIGHT):                    return EL_DC_STEELWALL_2_LEFT;
11603     case (MV_LEFT):                     return EL_DC_STEELWALL_2_RIGHT;
11604     case (MV_DOWN):                     return EL_DC_STEELWALL_2_TOP;
11605     case (MV_UP):                       return EL_DC_STEELWALL_2_BOTTOM;
11606     case (MV_HORIZONTAL):               return EL_DC_STEELWALL_2_HORIZONTAL;
11607     case (MV_VERTICAL):                 return EL_DC_STEELWALL_2_VERTICAL;
11608     case (MV_ANY_DIRECTION):            return EL_DC_STEELWALL_2_MIDDLE;
11609     case (MV_NONE):                     return EL_DC_STEELWALL_2_SINGLE;
11610   }
11611
11612   return EL_EMPTY;
11613 }
11614
11615 static int getSteel2FromOpenDirectionNotEmpty(int direction, int element_old)
11616 {
11617   int element_new = getSteel2FromOpenDirection(direction);
11618
11619   return (element_new != EL_EMPTY ? element_new : element_old);
11620 }
11621
11622 static int getOpenDirectionFromChip(int element)
11623 {
11624   switch (element)
11625   {
11626     case EL_SP_CHIP_SINGLE:             return (MV_NONE);
11627     case EL_SP_CHIP_LEFT:               return (MV_RIGHT);
11628     case EL_SP_CHIP_RIGHT:              return (MV_LEFT);
11629     case EL_SP_CHIP_TOP:                return (MV_DOWN);
11630     case EL_SP_CHIP_BOTTOM:             return (MV_UP);
11631   }
11632
11633   return MV_NONE;
11634 }
11635
11636 static int getChipFromOpenDirection(int direction)
11637 {
11638   switch (direction)
11639   {
11640     case (MV_NONE):                     return EL_SP_CHIP_SINGLE;
11641     case (MV_LEFT):                     return EL_SP_CHIP_RIGHT;
11642     case (MV_RIGHT):                    return EL_SP_CHIP_LEFT;
11643     case (MV_UP):                       return EL_SP_CHIP_BOTTOM;
11644     case (MV_DOWN):                     return EL_SP_CHIP_TOP;
11645   }
11646
11647   return EL_EMPTY;
11648 }
11649
11650 static int getChipFromOpenDirectionNotEmpty(int direction, int element_old)
11651 {
11652   int element_new = getChipFromOpenDirection(direction);
11653
11654   return (element_new != EL_EMPTY ? element_new : element_old);
11655 }
11656
11657 static int getClosedTube(int x, int y)
11658 {
11659   struct XY *xy = xy_directions;
11660   int element_old = IntelliDrawBuffer[x][y];
11661   int direction_old = getOpenDirectionFromTube(element_old);
11662   int direction_new = MV_NONE;
11663   int i;
11664
11665   for (i = 0; i < NUM_DIRECTIONS; i++)
11666   {
11667     int xx = x + xy[i].x;
11668     int yy = y + xy[i].y;
11669     int dir = MV_DIR_FROM_BIT(i);
11670     int dir_opposite = MV_DIR_OPPOSITE(dir);
11671
11672     if (IN_LEV_FIELD(xx, yy) && IS_TUBE(IntelliDrawBuffer[xx][yy]) &&
11673         (direction_old & dir) &&
11674         (getOpenDirectionFromTube(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11675       direction_new |= dir;
11676   }
11677
11678   return getTubeFromOpenDirectionNotEmpty(direction_new, element_old);
11679 }
11680
11681 static int getClosedBelt(int x, int y)
11682 {
11683   struct XY *xy = xy_directions;
11684   int element_old = IntelliDrawBuffer[x][y];
11685   int nr = getBeltNrFromBeltElement(element_old);
11686   int direction_old = getOpenDirectionFromBelt(element_old);
11687   int direction_new = MV_NONE;
11688   int i;
11689
11690   for (i = MV_BIT_LEFT; i <= MV_BIT_RIGHT; i++)
11691   {
11692     int xx = x + xy[i].x;
11693     int yy = y + xy[i].y;
11694     int dir = MV_DIR_FROM_BIT(i);
11695     int dir_opposite = MV_DIR_OPPOSITE(dir);
11696
11697     if (IN_LEV_FIELD(xx, yy) && IS_BELT(IntelliDrawBuffer[xx][yy]) &&
11698         (direction_old & dir) &&
11699         (getOpenDirectionFromBelt(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11700       direction_new |= dir;
11701   }
11702
11703   return getBeltFromNrAndOpenDirection(nr, direction_new);
11704 }
11705
11706 static int getClosedPool(int x, int y)
11707 {
11708   struct XY *xy = xy_directions;
11709   int element_old = IntelliDrawBuffer[x][y];
11710   int direction_old = getOpenDirectionFromPool(element_old);
11711   int direction_new = MV_NONE;
11712   int i;
11713
11714   for (i = 0; i < NUM_DIRECTIONS; i++)
11715   {
11716     int xx = x + xy[i].x;
11717     int yy = y + xy[i].y;
11718     int dir = MV_DIR_FROM_BIT(i);
11719     int dir_opposite = MV_DIR_OPPOSITE(dir);
11720
11721     if (IN_LEV_FIELD(xx, yy) &&
11722         IS_ACID_POOL_OR_ACID(IntelliDrawBuffer[xx][yy]) &&
11723         (direction_old & dir) &&
11724         (getOpenDirectionFromPool(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11725       direction_new |= dir;
11726   }
11727
11728   return getPoolFromOpenDirectionNotEmpty(direction_new, element_old);
11729 }
11730
11731 static int getClosedPillar(int x, int y)
11732 {
11733   struct XY *xy = xy_directions;
11734   int element_old = IntelliDrawBuffer[x][y];
11735   int direction_old = getOpenDirectionFromPillar(element_old);
11736   int direction_new = MV_NONE;
11737   int i;
11738
11739   for (i = MV_BIT_UP; i <= MV_BIT_DOWN; i++)
11740   {
11741     int xx = x + xy[i].x;
11742     int yy = y + xy[i].y;
11743     int dir = MV_DIR_FROM_BIT(i);
11744     int dir_opposite = MV_DIR_OPPOSITE(dir);
11745
11746     if (IN_LEV_FIELD(xx, yy) && IS_EMC_PILLAR(IntelliDrawBuffer[xx][yy]) &&
11747         (direction_old & dir) &&
11748         (getOpenDirectionFromPillar(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11749       direction_new |= dir;
11750   }
11751
11752   return getPillarFromOpenDirectionNotEmpty(direction_new, element_old);
11753 }
11754
11755 static int getClosedSteel2(int x, int y)
11756 {
11757   struct XY *xy = xy_directions;
11758   int element_old = IntelliDrawBuffer[x][y];
11759   int direction_old = getOpenDirectionFromSteel2(element_old);
11760   int direction_new = MV_NONE;
11761   int i;
11762
11763   for (i = 0; i < NUM_DIRECTIONS; i++)
11764   {
11765     int xx = x + xy[i].x;
11766     int yy = y + xy[i].y;
11767     int dir = MV_DIR_FROM_BIT(i);
11768     int dir_opposite = MV_DIR_OPPOSITE(dir);
11769
11770     if (IN_LEV_FIELD(xx, yy) && IS_DC_STEELWALL_2(IntelliDrawBuffer[xx][yy]) &&
11771         (direction_old & dir) &&
11772         (getOpenDirectionFromSteel2(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11773       direction_new |= dir;
11774   }
11775
11776   return getSteel2FromOpenDirectionNotEmpty(direction_new, element_old);
11777 }
11778
11779 static int getClosedChip(int x, int y)
11780 {
11781   struct XY *xy = xy_directions;
11782   int element_old = IntelliDrawBuffer[x][y];
11783   int direction_old = getOpenDirectionFromChip(element_old);
11784   int direction_new = MV_NONE;
11785   int i;
11786
11787   for (i = 0; i < NUM_DIRECTIONS; i++)
11788   {
11789     int xx = x + xy[i].x;
11790     int yy = y + xy[i].y;
11791     int dir = MV_DIR_FROM_BIT(i);
11792     int dir_opposite = MV_DIR_OPPOSITE(dir);
11793
11794     if (IN_LEV_FIELD(xx, yy) && IS_SP_CHIP(IntelliDrawBuffer[xx][yy]) &&
11795         (direction_old & dir) &&
11796         (getOpenDirectionFromChip(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11797       direction_new |= dir;
11798   }
11799
11800   return getChipFromOpenDirectionNotEmpty(direction_new, element_old);
11801 }
11802
11803 static void SetElementSimpleExt(int x, int y, int dx, int dy, int element,
11804                                 boolean change_level)
11805 {
11806   int sx = x - level_xpos;
11807   int sy = y - level_ypos;
11808   int old_element = Tile[x][y];
11809   int new_element = element;
11810   unsigned int new_bitmask = (getDrawModeHiRes() ? (dx + 1) << (dy * 2) : 0x0f);
11811   boolean draw_masked = FALSE;
11812
11813   if (IS_MM_WALL_EDITOR(element))
11814   {
11815     element = map_mm_wall_element_editor(element) | new_bitmask;
11816
11817     if (IS_MM_WALL(old_element))
11818       element |= MM_WALL_BITS(old_element);
11819
11820     if (!change_level)
11821       draw_masked = TRUE;
11822   }
11823   else if (IS_MM_WALL(old_element) && element == EL_EMPTY)
11824   {
11825     int element_changed = old_element & ~new_bitmask;
11826
11827     if (MM_WALL_BITS(element_changed) != 0)
11828       element = element_changed;
11829   }
11830
11831   IntelliDrawBuffer[x][y] = element;
11832
11833   if (change_level)
11834     Tile[x][y] = element;
11835
11836   if (IN_ED_FIELD(sx, sy))
11837   {
11838     if (IS_MM_WALL(old_element) && new_element == EL_EMPTY)
11839       DrawSizedWallParts_MM(sx, sy, EL_EMPTY, ed_tilesize, FALSE, new_bitmask);
11840     else if (draw_masked)
11841       DrawEditorElementThruMask(sx, sy, element);
11842     else
11843       DrawEditorElement(sx, sy, element);
11844   }
11845 }
11846
11847 static void SetElementSimple(int x, int y, int element, boolean change_level)
11848 {
11849   SetElementSimpleExt(x, y, 0, 0, element, change_level);
11850 }
11851
11852 static void MergeAndCloseNeighbourElements(int x1, int y1, int *element1,
11853                                            int x2, int y2, int *element2,
11854                                            int (*close_function)(int, int),
11855                                            boolean change_level)
11856 {
11857   // set neighbour elements to newly determined connections
11858   SetElementSimple(x1, y1, *element1, change_level);
11859   SetElementSimple(x2, y2, *element2, change_level);
11860
11861   // remove all open connections of neighbour elements
11862   *element1 = close_function(x1, y1);
11863   *element2 = close_function(x2, y2);
11864
11865   // set neighbour elements to new, minimized connections
11866   SetElementSimple(x1, y1, *element1, change_level);
11867   SetElementSimple(x2, y2, *element2, change_level);
11868 }
11869
11870 static void SetElementIntelliDraw(int x, int y, int dx, int dy, int new_element,
11871                                   boolean change_level, int button)
11872 {
11873   struct XY *xy = xy_directions;
11874   static int last_x = -1;
11875   static int last_y = -1;
11876
11877   if (new_element == EL_UNDEFINED)
11878   {
11879     last_x = -1;
11880     last_y = -1;
11881
11882     return;
11883   }
11884
11885   int old_element = IntelliDrawBuffer[x][y];
11886
11887   if (IS_TUBE(new_element))
11888   {
11889     int last_element_new = EL_UNDEFINED;
11890     int direction = MV_NONE;
11891     int i;
11892
11893     // if old element is of same kind, keep all existing directions
11894     if (IS_TUBE(old_element))
11895       direction |= getOpenDirectionFromTube(old_element);
11896
11897     for (i = 0; i < NUM_DIRECTIONS; i++)
11898     {
11899       int xx = x + xy[i].x;
11900       int yy = y + xy[i].y;
11901
11902       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
11903           IS_TUBE(IntelliDrawBuffer[last_x][last_y]))
11904       {
11905         int dir = MV_DIR_FROM_BIT(i);
11906         int dir_opposite = MV_DIR_OPPOSITE(dir);
11907         int last_element_old = IntelliDrawBuffer[last_x][last_y];
11908         int last_direction_old = getOpenDirectionFromTube(last_element_old);
11909         int last_direction_new = last_direction_old | dir_opposite;
11910
11911         last_element_new = getTubeFromOpenDirection(last_direction_new);
11912
11913         direction |= dir;
11914       }
11915     }
11916
11917     new_element = getTubeFromOpenDirectionNotEmpty(direction, new_element);
11918
11919     if (last_element_new != EL_UNDEFINED)
11920       MergeAndCloseNeighbourElements(x, y, &new_element,
11921                                      last_x, last_y, &last_element_new,
11922                                      getClosedTube, change_level);
11923   }
11924   else if (IS_BELT(new_element))
11925   {
11926     int belt_nr = getBeltNrFromBeltElement(new_element);
11927     int last_element_new = EL_UNDEFINED;
11928     int direction = MV_NONE;
11929     int i;
11930
11931     // if old element is of same kind, keep all existing directions
11932     if (IS_BELT(old_element))
11933       direction |= getOpenDirectionFromBelt(old_element);
11934
11935     for (i = MV_BIT_LEFT; i <= MV_BIT_RIGHT; i++)
11936     {
11937       int xx = x + xy[i].x;
11938       int yy = y + xy[i].y;
11939
11940       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
11941           IS_BELT(IntelliDrawBuffer[last_x][last_y]))
11942       {
11943         int dir = MV_DIR_FROM_BIT(i);
11944         int dir_opposite = MV_DIR_OPPOSITE(dir);
11945         int last_element_old = IntelliDrawBuffer[last_x][last_y];
11946         int last_belt_nr = getBeltNrFromBeltElement(last_element_old);
11947         int last_direction_old = getOpenDirectionFromBelt(last_element_old);
11948         int last_direction_new = last_direction_old | dir_opposite;
11949
11950         last_element_new = getBeltFromNrAndOpenDirection(last_belt_nr,
11951                                                          last_direction_new);
11952         direction |= dir;
11953       }
11954     }
11955
11956     new_element = getBeltFromNrAndOpenDirectionNotEmpty(belt_nr, direction,
11957                                                         new_element);
11958     if (last_element_new != EL_UNDEFINED)
11959       MergeAndCloseNeighbourElements(x, y, &new_element,
11960                                      last_x, last_y, &last_element_new,
11961                                      getClosedBelt, change_level);
11962   }
11963   else if (IS_ACID_POOL_OR_ACID(new_element))
11964   {
11965     int last_element_new = EL_UNDEFINED;
11966     int direction = MV_NONE;
11967     int i;
11968
11969     // if old element is of same kind, keep all existing directions
11970     if (IS_ACID_POOL_OR_ACID(old_element))
11971       direction |= getOpenDirectionFromPool(old_element);
11972
11973     for (i = 0; i < NUM_DIRECTIONS; i++)
11974     {
11975       int xx = x + xy[i].x;
11976       int yy = y + xy[i].y;
11977
11978       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
11979           IS_ACID_POOL_OR_ACID(IntelliDrawBuffer[last_x][last_y]))
11980       {
11981         int dir = MV_DIR_FROM_BIT(i);
11982         int dir_opposite = MV_DIR_OPPOSITE(dir);
11983         int last_element_old = IntelliDrawBuffer[last_x][last_y];
11984         int last_direction_old = getOpenDirectionFromPool(last_element_old);
11985         int last_direction_new = last_direction_old | dir_opposite;
11986
11987         last_element_new = getPoolFromOpenDirection(last_direction_new);
11988
11989         direction |= dir;
11990       }
11991     }
11992
11993     // special corrections needed for intuitively correct acid pool drawing
11994     if (last_element_new == EL_EMPTY)
11995       last_element_new = new_element;
11996     else if (last_element_new != EL_UNDEFINED)
11997       new_element = last_element_new;
11998
11999     new_element = getPoolFromOpenDirectionNotEmpty(direction, new_element);
12000
12001     if (last_element_new != EL_UNDEFINED)
12002       MergeAndCloseNeighbourElements(x, y, &new_element,
12003                                      last_x, last_y, &last_element_new,
12004                                      getClosedPool, change_level);
12005   }
12006   else if (IS_EMC_PILLAR(new_element))
12007   {
12008     int last_element_new = EL_UNDEFINED;
12009     int direction = MV_NONE;
12010     int i;
12011
12012     // if old element is of same kind, keep all existing directions
12013     if (IS_EMC_PILLAR(old_element))
12014       direction |= getOpenDirectionFromPillar(old_element);
12015
12016     for (i = MV_BIT_UP; i <= MV_BIT_DOWN; i++)
12017     {
12018       int xx = x + xy[i].x;
12019       int yy = y + xy[i].y;
12020
12021       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12022           IS_EMC_PILLAR(IntelliDrawBuffer[last_x][last_y]))
12023       {
12024         int dir = MV_DIR_FROM_BIT(i);
12025         int dir_opposite = MV_DIR_OPPOSITE(dir);
12026         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12027         int last_direction_old = getOpenDirectionFromPillar(last_element_old);
12028         int last_direction_new = last_direction_old | dir_opposite;
12029
12030         last_element_new = getPillarFromOpenDirection(last_direction_new);
12031
12032         direction |= dir;
12033       }
12034     }
12035
12036     new_element = getPillarFromOpenDirectionNotEmpty(direction, new_element);
12037
12038     if (last_element_new != EL_UNDEFINED)
12039       MergeAndCloseNeighbourElements(x, y, &new_element,
12040                                      last_x, last_y, &last_element_new,
12041                                      getClosedPillar, change_level);
12042   }
12043   else if (IS_DC_STEELWALL_2(new_element))
12044   {
12045     int last_element_new = EL_UNDEFINED;
12046     int direction = MV_NONE;
12047     int i;
12048
12049     // if old element is of same kind, keep all existing directions
12050     if (IS_DC_STEELWALL_2(old_element))
12051       direction |= getOpenDirectionFromSteel2(old_element);
12052
12053     for (i = 0; i < NUM_DIRECTIONS; i++)
12054     {
12055       int xx = x + xy[i].x;
12056       int yy = y + xy[i].y;
12057
12058       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12059           IS_DC_STEELWALL_2(IntelliDrawBuffer[last_x][last_y]))
12060       {
12061         int dir = MV_DIR_FROM_BIT(i);
12062         int dir_opposite = MV_DIR_OPPOSITE(dir);
12063         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12064         int last_direction_old = getOpenDirectionFromSteel2(last_element_old);
12065         int last_direction_new = last_direction_old | dir_opposite;
12066
12067         last_element_new = getSteel2FromOpenDirection(last_direction_new);
12068
12069         direction |= dir;
12070       }
12071     }
12072
12073     new_element = getSteel2FromOpenDirectionNotEmpty(direction, new_element);
12074
12075     if (last_element_new != EL_UNDEFINED)
12076       MergeAndCloseNeighbourElements(x, y, &new_element,
12077                                      last_x, last_y, &last_element_new,
12078                                      getClosedSteel2, change_level);
12079   }
12080   else if (IS_SP_CHIP(new_element))
12081   {
12082     int last_element_new = EL_UNDEFINED;
12083     int direction = MV_NONE;
12084     int i;
12085
12086     // (do not keep existing directions, regardless of kind of old element)
12087
12088     for (i = 0; i < NUM_DIRECTIONS; i++)
12089     {
12090       int xx = x + xy[i].x;
12091       int yy = y + xy[i].y;
12092
12093       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12094           IS_SP_CHIP(IntelliDrawBuffer[last_x][last_y]))
12095       {
12096         int dir = MV_DIR_FROM_BIT(i);
12097         int dir_opposite = MV_DIR_OPPOSITE(dir);
12098         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12099         int last_direction_old = getOpenDirectionFromChip(last_element_old);
12100         int last_direction_new = last_direction_old | dir_opposite;
12101
12102         if (last_direction_old == MV_NONE)
12103         {
12104           last_element_new = getChipFromOpenDirection(last_direction_new);
12105           direction |= dir;
12106         }
12107         else if (last_direction_old & (dir | dir_opposite))
12108         {
12109           direction |= MV_DIR_OPPOSITE(last_direction_old);
12110         }
12111         else
12112         {
12113           direction |= MV_DIR_OPPOSITE(dir);
12114         }
12115       }
12116     }
12117
12118     new_element = getChipFromOpenDirectionNotEmpty(direction, new_element);
12119
12120     if (last_element_new != EL_UNDEFINED)
12121       MergeAndCloseNeighbourElements(x, y, &new_element,
12122                                      last_x, last_y, &last_element_new,
12123                                      getClosedChip, change_level);
12124   }
12125   else if (IS_SP_HARDWARE_BASE(new_element))
12126   {
12127     int nr = GetSimpleRandom(6);
12128
12129     new_element = (nr == 0 ? EL_SP_HARDWARE_BASE_1 :
12130                    nr == 1 ? EL_SP_HARDWARE_BASE_2 :
12131                    nr == 2 ? EL_SP_HARDWARE_BASE_3 :
12132                    nr == 3 ? EL_SP_HARDWARE_BASE_4 :
12133                    nr == 4 ? EL_SP_HARDWARE_BASE_5 : EL_SP_HARDWARE_BASE_6);
12134   }
12135   else if (new_element == EL_SP_HARDWARE_GREEN ||
12136            new_element == EL_SP_HARDWARE_BLUE ||
12137            new_element == EL_SP_HARDWARE_RED)
12138   {
12139     int nr = GetSimpleRandom(3);
12140
12141     new_element = (nr == 0 ? EL_SP_HARDWARE_GREEN :
12142                    nr == 1 ? EL_SP_HARDWARE_BLUE : EL_SP_HARDWARE_RED);
12143   }
12144   else if (IS_GROUP_ELEMENT(new_element))
12145   {
12146     boolean connected_drawing = FALSE;
12147     int i;
12148
12149     for (i = 0; i < NUM_DIRECTIONS; i++)
12150     {
12151       int xx = x + xy[i].x;
12152       int yy = y + xy[i].y;
12153
12154       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12155           IS_IN_GROUP_EL(IntelliDrawBuffer[last_x][last_y], new_element))
12156         connected_drawing = TRUE;
12157     }
12158
12159     if (!connected_drawing)
12160       ResolveGroupElement(new_element);
12161
12162     new_element = GetElementFromGroupElement(new_element);
12163   }
12164   else if (IS_BELT_SWITCH(old_element))
12165   {
12166     int belt_nr = getBeltNrFromBeltSwitchElement(old_element);
12167     int belt_dir = getBeltDirFromBeltSwitchElement(old_element);
12168
12169     belt_dir = (belt_dir == MV_LEFT ? MV_NONE :
12170                 belt_dir == MV_NONE ? MV_RIGHT : MV_LEFT);
12171
12172     new_element = getBeltSwitchElementFromBeltNrAndBeltDir(belt_nr, belt_dir);
12173   }
12174   else
12175   {
12176     static int swappable_elements[][2] =
12177     {
12178       { EL_EXIT_CLOSED,                 EL_EXIT_OPEN                    },
12179       { EL_DYNAMITE,                    EL_DYNAMITE_ACTIVE              },
12180       { EL_EM_DYNAMITE,                 EL_EM_DYNAMITE_ACTIVE           },
12181       { EL_QUICKSAND_EMPTY,             EL_QUICKSAND_FULL               },
12182       { EL_EMERALD,                     EL_WALL_EMERALD                 },
12183       { EL_EMERALD_YELLOW,              EL_WALL_EMERALD_YELLOW          },
12184       { EL_EMERALD_RED,                 EL_WALL_EMERALD_RED             },
12185       { EL_EMERALD_PURPLE,              EL_WALL_EMERALD_PURPLE          },
12186       { EL_DIAMOND,                     EL_WALL_DIAMOND                 },
12187       { EL_BD_DIAMOND,                  EL_WALL_BD_DIAMOND              },
12188       { EL_GATE_1,                      EL_GATE_1_GRAY                  },
12189       { EL_GATE_2,                      EL_GATE_2_GRAY                  },
12190       { EL_GATE_3,                      EL_GATE_3_GRAY                  },
12191       { EL_GATE_4,                      EL_GATE_4_GRAY                  },
12192       { EL_EM_GATE_1,                   EL_EM_GATE_1_GRAY               },
12193       { EL_EM_GATE_2,                   EL_EM_GATE_2_GRAY               },
12194       { EL_EM_GATE_3,                   EL_EM_GATE_3_GRAY               },
12195       { EL_EM_GATE_4,                   EL_EM_GATE_4_GRAY               },
12196       { EL_EMC_GATE_5,                  EL_EMC_GATE_5_GRAY              },
12197       { EL_EMC_GATE_6,                  EL_EMC_GATE_6_GRAY              },
12198       { EL_EMC_GATE_7,                  EL_EMC_GATE_7_GRAY              },
12199       { EL_EMC_GATE_8,                  EL_EMC_GATE_8_GRAY              },
12200       { EL_DC_GATE_WHITE,               EL_DC_GATE_WHITE_GRAY           },
12201       { EL_TIME_ORB_EMPTY,              EL_TIME_ORB_FULL                },
12202       { EL_LAMP,                        EL_LAMP_ACTIVE                  },
12203       { EL_SOKOBAN_FIELD_EMPTY,         EL_SOKOBAN_FIELD_FULL           },
12204       { EL_SP_BASE,                     EL_SP_BUGGY_BASE                },
12205       { EL_PEARL,                       EL_WALL_PEARL                   },
12206       { EL_CRYSTAL,                     EL_WALL_CRYSTAL                 },
12207       { EL_TIMEGATE_CLOSED,             EL_TIMEGATE_OPEN                },
12208       { EL_SWITCHGATE_CLOSED,           EL_SWITCHGATE_OPEN              },
12209       { EL_SWITCHGATE_SWITCH_UP,        EL_SWITCHGATE_SWITCH_DOWN       },
12210       { EL_DC_SWITCHGATE_SWITCH_UP,     EL_DC_SWITCHGATE_SWITCH_DOWN    },
12211       { EL_LIGHT_SWITCH,                EL_LIGHT_SWITCH_ACTIVE          },
12212       { EL_LANDMINE,                    EL_DC_LANDMINE                  },
12213       { EL_SHIELD_NORMAL,               EL_SHIELD_DEADLY                },
12214       { EL_STEEL_EXIT_CLOSED,           EL_STEEL_EXIT_OPEN              },
12215       { EL_EM_EXIT_CLOSED,              EL_EM_EXIT_OPEN                 },
12216       { EL_EM_STEEL_EXIT_CLOSED,        EL_EM_STEEL_EXIT_OPEN           },
12217       { EL_QUICKSAND_FAST_EMPTY,        EL_QUICKSAND_FAST_FULL          },
12218       { EL_MM_EXIT_CLOSED,              EL_MM_EXIT_OPEN                 },
12219       { EL_MM_FUSE,                     EL_MM_FUSE_ACTIVE               },
12220       { EL_MM_LIGHTBULB,                EL_MM_LIGHTBULB_ACTIVE          },
12221       { EL_MM_FUEL_EMPTY,               EL_MM_FUEL_FULL                 },
12222
12223       { -1,                             -1                              },
12224     };
12225     static int rotatable_elements_4[][4] =
12226     {
12227       {
12228         EL_BUG_UP,
12229         EL_BUG_RIGHT,
12230         EL_BUG_DOWN,
12231         EL_BUG_LEFT
12232       },
12233       {
12234         EL_SPACESHIP_UP,
12235         EL_SPACESHIP_RIGHT,
12236         EL_SPACESHIP_DOWN,
12237         EL_SPACESHIP_LEFT
12238       },
12239       {
12240         EL_BD_BUTTERFLY_UP,
12241         EL_BD_BUTTERFLY_RIGHT,
12242         EL_BD_BUTTERFLY_DOWN,
12243         EL_BD_BUTTERFLY_LEFT
12244       },
12245       {
12246         EL_BD_FIREFLY_UP,
12247         EL_BD_FIREFLY_RIGHT,
12248         EL_BD_FIREFLY_DOWN,
12249         EL_BD_FIREFLY_LEFT
12250       },
12251       {
12252         EL_PACMAN_UP,
12253         EL_PACMAN_RIGHT,
12254         EL_PACMAN_DOWN,
12255         EL_PACMAN_LEFT
12256       },
12257       {
12258         EL_YAMYAM_UP,
12259         EL_YAMYAM_RIGHT,
12260         EL_YAMYAM_DOWN,
12261         EL_YAMYAM_LEFT
12262       },
12263       {
12264         EL_ARROW_UP,
12265         EL_ARROW_RIGHT,
12266         EL_ARROW_DOWN,
12267         EL_ARROW_LEFT
12268       },
12269       {
12270         EL_SP_PORT_UP,
12271         EL_SP_PORT_RIGHT,
12272         EL_SP_PORT_DOWN,
12273         EL_SP_PORT_LEFT
12274       },
12275       {
12276         EL_SP_GRAVITY_PORT_UP,
12277         EL_SP_GRAVITY_PORT_RIGHT,
12278         EL_SP_GRAVITY_PORT_DOWN,
12279         EL_SP_GRAVITY_PORT_LEFT
12280       },
12281       {
12282         EL_SP_GRAVITY_ON_PORT_UP,
12283         EL_SP_GRAVITY_ON_PORT_RIGHT,
12284         EL_SP_GRAVITY_ON_PORT_DOWN,
12285         EL_SP_GRAVITY_ON_PORT_LEFT
12286       },
12287       {
12288         EL_SP_GRAVITY_OFF_PORT_UP,
12289         EL_SP_GRAVITY_OFF_PORT_RIGHT,
12290         EL_SP_GRAVITY_OFF_PORT_DOWN,
12291         EL_SP_GRAVITY_OFF_PORT_LEFT
12292       },
12293       {
12294         EL_MOLE_UP,
12295         EL_MOLE_RIGHT,
12296         EL_MOLE_DOWN,
12297         EL_MOLE_LEFT
12298       },
12299       {
12300         EL_BALLOON_SWITCH_UP,
12301         EL_BALLOON_SWITCH_RIGHT,
12302         EL_BALLOON_SWITCH_DOWN,
12303         EL_BALLOON_SWITCH_LEFT
12304       },
12305       {
12306         EL_MM_MCDUFFIN_UP,
12307         EL_MM_MCDUFFIN_RIGHT,
12308         EL_MM_MCDUFFIN_DOWN,
12309         EL_MM_MCDUFFIN_LEFT
12310       },
12311       {
12312         EL_MM_MIRROR_FIXED_1,
12313         EL_MM_MIRROR_FIXED_4,
12314         EL_MM_MIRROR_FIXED_3,
12315         EL_MM_MIRROR_FIXED_2
12316       },
12317       {
12318         EL_MM_STEEL_GRID_FIXED_1,
12319         EL_MM_STEEL_GRID_FIXED_4,
12320         EL_MM_STEEL_GRID_FIXED_2,
12321         EL_MM_STEEL_GRID_FIXED_3
12322       },
12323       {
12324         EL_MM_WOODEN_GRID_FIXED_1,
12325         EL_MM_WOODEN_GRID_FIXED_4,
12326         EL_MM_WOODEN_GRID_FIXED_2,
12327         EL_MM_WOODEN_GRID_FIXED_3
12328       },
12329       {
12330         EL_MM_POLARIZER_CROSS_1,
12331         EL_MM_POLARIZER_CROSS_4,
12332         EL_MM_POLARIZER_CROSS_3,
12333         EL_MM_POLARIZER_CROSS_2
12334       },
12335       {
12336         EL_MM_PACMAN_UP,
12337         EL_MM_PACMAN_RIGHT,
12338         EL_MM_PACMAN_DOWN,
12339         EL_MM_PACMAN_LEFT
12340       },
12341       {
12342         EL_DF_LASER_UP,
12343         EL_DF_LASER_RIGHT,
12344         EL_DF_LASER_DOWN,
12345         EL_DF_LASER_LEFT
12346       },
12347       {
12348         EL_DF_RECEIVER_UP,
12349         EL_DF_RECEIVER_RIGHT,
12350         EL_DF_RECEIVER_DOWN,
12351         EL_DF_RECEIVER_LEFT
12352       },
12353       {
12354         EL_DF_SLOPE_1,
12355         EL_DF_SLOPE_4,
12356         EL_DF_SLOPE_3,
12357         EL_DF_SLOPE_2
12358       },
12359
12360       {
12361         -1,
12362       },
12363     };
12364     static int rotatable_elements_8[][8] =
12365     {
12366       {
12367         EL_DF_STEEL_GRID_FIXED_1,
12368         EL_DF_STEEL_GRID_FIXED_8,
12369         EL_DF_STEEL_GRID_FIXED_7,
12370         EL_DF_STEEL_GRID_FIXED_6,
12371         EL_DF_STEEL_GRID_FIXED_5,
12372         EL_DF_STEEL_GRID_FIXED_4,
12373         EL_DF_STEEL_GRID_FIXED_3,
12374         EL_DF_STEEL_GRID_FIXED_2
12375       },
12376       {
12377         EL_DF_WOODEN_GRID_FIXED_1,
12378         EL_DF_WOODEN_GRID_FIXED_8,
12379         EL_DF_WOODEN_GRID_FIXED_7,
12380         EL_DF_WOODEN_GRID_FIXED_6,
12381         EL_DF_WOODEN_GRID_FIXED_5,
12382         EL_DF_WOODEN_GRID_FIXED_4,
12383         EL_DF_WOODEN_GRID_FIXED_3,
12384         EL_DF_WOODEN_GRID_FIXED_2
12385       },
12386       {
12387         EL_DF_STEEL_GRID_ROTATING_1,
12388         EL_DF_STEEL_GRID_ROTATING_8,
12389         EL_DF_STEEL_GRID_ROTATING_7,
12390         EL_DF_STEEL_GRID_ROTATING_6,
12391         EL_DF_STEEL_GRID_ROTATING_5,
12392         EL_DF_STEEL_GRID_ROTATING_4,
12393         EL_DF_STEEL_GRID_ROTATING_3,
12394         EL_DF_STEEL_GRID_ROTATING_2
12395       },
12396       {
12397         EL_DF_WOODEN_GRID_ROTATING_1,
12398         EL_DF_WOODEN_GRID_ROTATING_8,
12399         EL_DF_WOODEN_GRID_ROTATING_7,
12400         EL_DF_WOODEN_GRID_ROTATING_6,
12401         EL_DF_WOODEN_GRID_ROTATING_5,
12402         EL_DF_WOODEN_GRID_ROTATING_4,
12403         EL_DF_WOODEN_GRID_ROTATING_3,
12404         EL_DF_WOODEN_GRID_ROTATING_2
12405       },
12406
12407       {
12408         -1,
12409       },
12410     };
12411     static int rotatable_elements_16[][16] =
12412     {
12413       {
12414         EL_MM_MIRROR_1,
12415         EL_MM_MIRROR_16,
12416         EL_MM_MIRROR_15,
12417         EL_MM_MIRROR_14,
12418         EL_MM_MIRROR_13,
12419         EL_MM_MIRROR_12,
12420         EL_MM_MIRROR_11,
12421         EL_MM_MIRROR_10,
12422         EL_MM_MIRROR_9,
12423         EL_MM_MIRROR_8,
12424         EL_MM_MIRROR_7,
12425         EL_MM_MIRROR_6,
12426         EL_MM_MIRROR_5,
12427         EL_MM_MIRROR_4,
12428         EL_MM_MIRROR_3,
12429         EL_MM_MIRROR_2
12430       },
12431       {
12432         EL_MM_TELEPORTER_5,
12433         EL_MM_TELEPORTER_4,
12434         EL_MM_TELEPORTER_3,
12435         EL_MM_TELEPORTER_2,
12436         EL_MM_TELEPORTER_1,
12437         EL_MM_TELEPORTER_16,
12438         EL_MM_TELEPORTER_15,
12439         EL_MM_TELEPORTER_14,
12440         EL_MM_TELEPORTER_13,
12441         EL_MM_TELEPORTER_12,
12442         EL_MM_TELEPORTER_11,
12443         EL_MM_TELEPORTER_10,
12444         EL_MM_TELEPORTER_9,
12445         EL_MM_TELEPORTER_8,
12446         EL_MM_TELEPORTER_7,
12447         EL_MM_TELEPORTER_6
12448       },
12449       {
12450         EL_MM_TELEPORTER_RED_5,
12451         EL_MM_TELEPORTER_RED_4,
12452         EL_MM_TELEPORTER_RED_3,
12453         EL_MM_TELEPORTER_RED_2,
12454         EL_MM_TELEPORTER_RED_1,
12455         EL_MM_TELEPORTER_RED_16,
12456         EL_MM_TELEPORTER_RED_15,
12457         EL_MM_TELEPORTER_RED_14,
12458         EL_MM_TELEPORTER_RED_13,
12459         EL_MM_TELEPORTER_RED_12,
12460         EL_MM_TELEPORTER_RED_11,
12461         EL_MM_TELEPORTER_RED_10,
12462         EL_MM_TELEPORTER_RED_9,
12463         EL_MM_TELEPORTER_RED_8,
12464         EL_MM_TELEPORTER_RED_7,
12465         EL_MM_TELEPORTER_RED_6
12466       },
12467       {
12468         EL_MM_TELEPORTER_YELLOW_5,
12469         EL_MM_TELEPORTER_YELLOW_4,
12470         EL_MM_TELEPORTER_YELLOW_3,
12471         EL_MM_TELEPORTER_YELLOW_2,
12472         EL_MM_TELEPORTER_YELLOW_1,
12473         EL_MM_TELEPORTER_YELLOW_16,
12474         EL_MM_TELEPORTER_YELLOW_15,
12475         EL_MM_TELEPORTER_YELLOW_14,
12476         EL_MM_TELEPORTER_YELLOW_13,
12477         EL_MM_TELEPORTER_YELLOW_12,
12478         EL_MM_TELEPORTER_YELLOW_11,
12479         EL_MM_TELEPORTER_YELLOW_10,
12480         EL_MM_TELEPORTER_YELLOW_9,
12481         EL_MM_TELEPORTER_YELLOW_8,
12482         EL_MM_TELEPORTER_YELLOW_7,
12483         EL_MM_TELEPORTER_YELLOW_6
12484       },
12485       {
12486         EL_MM_TELEPORTER_GREEN_5,
12487         EL_MM_TELEPORTER_GREEN_4,
12488         EL_MM_TELEPORTER_GREEN_3,
12489         EL_MM_TELEPORTER_GREEN_2,
12490         EL_MM_TELEPORTER_GREEN_1,
12491         EL_MM_TELEPORTER_GREEN_16,
12492         EL_MM_TELEPORTER_GREEN_15,
12493         EL_MM_TELEPORTER_GREEN_14,
12494         EL_MM_TELEPORTER_GREEN_13,
12495         EL_MM_TELEPORTER_GREEN_12,
12496         EL_MM_TELEPORTER_GREEN_11,
12497         EL_MM_TELEPORTER_GREEN_10,
12498         EL_MM_TELEPORTER_GREEN_9,
12499         EL_MM_TELEPORTER_GREEN_8,
12500         EL_MM_TELEPORTER_GREEN_7,
12501         EL_MM_TELEPORTER_GREEN_6
12502       },
12503       {
12504         EL_MM_TELEPORTER_BLUE_5,
12505         EL_MM_TELEPORTER_BLUE_4,
12506         EL_MM_TELEPORTER_BLUE_3,
12507         EL_MM_TELEPORTER_BLUE_2,
12508         EL_MM_TELEPORTER_BLUE_1,
12509         EL_MM_TELEPORTER_BLUE_16,
12510         EL_MM_TELEPORTER_BLUE_15,
12511         EL_MM_TELEPORTER_BLUE_14,
12512         EL_MM_TELEPORTER_BLUE_13,
12513         EL_MM_TELEPORTER_BLUE_12,
12514         EL_MM_TELEPORTER_BLUE_11,
12515         EL_MM_TELEPORTER_BLUE_10,
12516         EL_MM_TELEPORTER_BLUE_9,
12517         EL_MM_TELEPORTER_BLUE_8,
12518         EL_MM_TELEPORTER_BLUE_7,
12519         EL_MM_TELEPORTER_BLUE_6
12520       },
12521       {
12522         EL_MM_POLARIZER_1,
12523         EL_MM_POLARIZER_16,
12524         EL_MM_POLARIZER_15,
12525         EL_MM_POLARIZER_14,
12526         EL_MM_POLARIZER_13,
12527         EL_MM_POLARIZER_12,
12528         EL_MM_POLARIZER_11,
12529         EL_MM_POLARIZER_10,
12530         EL_MM_POLARIZER_9,
12531         EL_MM_POLARIZER_8,
12532         EL_MM_POLARIZER_7,
12533         EL_MM_POLARIZER_6,
12534         EL_MM_POLARIZER_5,
12535         EL_MM_POLARIZER_4,
12536         EL_MM_POLARIZER_3,
12537         EL_MM_POLARIZER_2
12538       },
12539       {
12540         EL_DF_MIRROR_1,
12541         EL_DF_MIRROR_16,
12542         EL_DF_MIRROR_15,
12543         EL_DF_MIRROR_14,
12544         EL_DF_MIRROR_13,
12545         EL_DF_MIRROR_12,
12546         EL_DF_MIRROR_11,
12547         EL_DF_MIRROR_10,
12548         EL_DF_MIRROR_9,
12549         EL_DF_MIRROR_8,
12550         EL_DF_MIRROR_7,
12551         EL_DF_MIRROR_6,
12552         EL_DF_MIRROR_5,
12553         EL_DF_MIRROR_4,
12554         EL_DF_MIRROR_3,
12555         EL_DF_MIRROR_2
12556       },
12557       {
12558         EL_DF_MIRROR_ROTATING_1,
12559         EL_DF_MIRROR_ROTATING_16,
12560         EL_DF_MIRROR_ROTATING_15,
12561         EL_DF_MIRROR_ROTATING_14,
12562         EL_DF_MIRROR_ROTATING_13,
12563         EL_DF_MIRROR_ROTATING_12,
12564         EL_DF_MIRROR_ROTATING_11,
12565         EL_DF_MIRROR_ROTATING_10,
12566         EL_DF_MIRROR_ROTATING_9,
12567         EL_DF_MIRROR_ROTATING_8,
12568         EL_DF_MIRROR_ROTATING_7,
12569         EL_DF_MIRROR_ROTATING_6,
12570         EL_DF_MIRROR_ROTATING_5,
12571         EL_DF_MIRROR_ROTATING_4,
12572         EL_DF_MIRROR_ROTATING_3,
12573         EL_DF_MIRROR_ROTATING_2
12574       },
12575       {
12576         EL_DF_MIRROR_FIXED_1,
12577         EL_DF_MIRROR_FIXED_16,
12578         EL_DF_MIRROR_FIXED_15,
12579         EL_DF_MIRROR_FIXED_14,
12580         EL_DF_MIRROR_FIXED_13,
12581         EL_DF_MIRROR_FIXED_12,
12582         EL_DF_MIRROR_FIXED_11,
12583         EL_DF_MIRROR_FIXED_10,
12584         EL_DF_MIRROR_FIXED_9,
12585         EL_DF_MIRROR_FIXED_8,
12586         EL_DF_MIRROR_FIXED_7,
12587         EL_DF_MIRROR_FIXED_6,
12588         EL_DF_MIRROR_FIXED_5,
12589         EL_DF_MIRROR_FIXED_4,
12590         EL_DF_MIRROR_FIXED_3,
12591         EL_DF_MIRROR_FIXED_2
12592       },
12593
12594       {
12595         -1,
12596       },
12597     };
12598     int i, j;
12599
12600     for (i = 0; swappable_elements[i][0] != -1; i++)
12601     {
12602       int element1 = swappable_elements[i][0];
12603       int element2 = swappable_elements[i][1];
12604
12605       if (old_element == element1 || old_element == element2)
12606         new_element = (old_element == element1 ? element2 : element1);
12607     }
12608
12609     for (i = 0; rotatable_elements_4[i][0] != -1; i++)
12610     {
12611       for (j = 0; j < 4; j++)
12612       {
12613         int element = rotatable_elements_4[i][j];
12614
12615         if (old_element == element)
12616           new_element = (button == 1 ? rotatable_elements_4[i][(j + 3) % 4] :
12617                          button == 2 ? rotatable_elements_4[i][0]           :
12618                          button == 3 ? rotatable_elements_4[i][(j + 1) % 4] :
12619                          old_element);
12620       }
12621     }
12622
12623     for (i = 0; rotatable_elements_8[i][0] != -1; i++)
12624     {
12625       for (j = 0; j < 8; j++)
12626       {
12627         int element = rotatable_elements_8[i][j];
12628
12629         if (old_element == element)
12630           new_element = (button == 1 ? rotatable_elements_8[i][(j + 7) % 8] :
12631                          button == 2 ? rotatable_elements_8[i][0]           :
12632                          button == 3 ? rotatable_elements_8[i][(j + 1) % 8] :
12633                          old_element);
12634       }
12635     }
12636
12637     for (i = 0; rotatable_elements_16[i][0] != -1; i++)
12638     {
12639       for (j = 0; j < 16; j++)
12640       {
12641         int element = rotatable_elements_16[i][j];
12642
12643         if (old_element == element)
12644           new_element = (button == 1 ? rotatable_elements_16[i][(j + 15) % 16] :
12645                          button == 2 ? rotatable_elements_16[i][0]             :
12646                          button == 3 ? rotatable_elements_16[i][(j + 1)  % 16] :
12647                          old_element);
12648       }
12649     }
12650
12651     if (old_element != new_element)
12652     {
12653       int max_infotext_len = getMaxInfoTextLength();
12654       char infotext[MAX_OUTPUT_LINESIZE + 1];
12655
12656       strncpy(infotext, getElementInfoText(new_element), max_infotext_len);
12657       infotext[max_infotext_len] = '\0';
12658
12659       ClearEditorGadgetInfoText();
12660
12661       DrawTextS(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY, INFOTEXT_FONT,
12662                 infotext);
12663     }
12664   }
12665
12666   if (IS_MM_WALL_EDITOR(new_element))
12667     SetElementSimpleExt(x, y, dx, dy, new_element, change_level);
12668   else
12669     SetElementSimple(x, y, new_element, change_level);
12670
12671   last_x = x;
12672   last_y = y;
12673 }
12674
12675 static void ResetIntelliDraw(void)
12676 {
12677   int x, y;
12678
12679   for (x = 0; x < lev_fieldx; x++)
12680     for (y = 0; y < lev_fieldy; y++)
12681       IntelliDrawBuffer[x][y] = Tile[x][y];
12682
12683   SetElementIntelliDraw(-1, -1, -1, -1, EL_UNDEFINED, FALSE, -1);
12684 }
12685
12686 static boolean draw_mode_hires = FALSE;
12687
12688 static boolean isHiresTileElement(int element)
12689 {
12690   return (IS_MM_WALL(element)        || element == EL_EMPTY);
12691 }
12692
12693 static boolean isHiresDrawElement(int element)
12694 {
12695   return (IS_MM_WALL_EDITOR(element) || element == EL_EMPTY);
12696 }
12697
12698 static int numHiresTiles(int element)
12699 {
12700   if (IS_MM_WALL(element))
12701     return get_number_of_bits(MM_WALL_BITS(element));
12702
12703   return 1;
12704 }
12705
12706 static void SetDrawModeHiRes(int element)
12707 {
12708   draw_mode_hires =
12709     (level.game_engine_type == GAME_ENGINE_TYPE_MM &&
12710      isHiresDrawElement(element));
12711 }
12712
12713 static boolean getDrawModeHiRes(void)
12714 {
12715   return draw_mode_hires;
12716 }
12717
12718 static int getLoResScreenPos(int pos)
12719 {
12720   return (getDrawModeHiRes() ? pos / 2 : pos);
12721 }
12722
12723 static int getLoResScreenMod(int pos)
12724 {
12725   return (getDrawModeHiRes() ? pos % 2 : 0);
12726 }
12727
12728 static void SetElementExt(int x, int y, int dx, int dy, int element,
12729                           boolean change_level, int button)
12730 {
12731   if (element < 0)
12732     SetElementSimple(x, y, Tile[x][y], change_level);
12733   else if (GetKeyModState() & KMOD_Shift)
12734     SetElementIntelliDraw(x, y, dx, dy, element, change_level, button);
12735   else
12736     SetElementSimpleExt(x, y, dx, dy, element, change_level);
12737 }
12738
12739 static void SetElement(int x, int y, int element)
12740 {
12741   SetElementExt(x, y, 0, 0, element, TRUE, -1);
12742 }
12743
12744 static void SetElementButton(int x, int y, int dx, int dy, int element,
12745                              int button)
12746 {
12747   SetElementExt(x, y, dx, dy, element, TRUE, button);
12748 }
12749
12750 static void SetElementHiRes(int sx2, int sy2, int element, boolean change_level)
12751 {
12752   int lx = getLoResScreenPos(sx2) + level_xpos;
12753   int ly = getLoResScreenPos(sy2) + level_ypos;
12754   int dx = getLoResScreenMod(sx2);
12755   int dy = getLoResScreenMod(sy2);
12756
12757   SetElementExt(lx, ly, dx, dy, element, change_level, -1);
12758 }
12759
12760 static void SetLevelElementHiRes(int lx2, int ly2, int element)
12761 {
12762   int lx = lx2 / 2;
12763   int ly = ly2 / 2;
12764   int dx = lx2 % 2;
12765   int dy = ly2 % 2;
12766
12767   SetElementExt(lx, ly, dx, dy, element, TRUE, -1);
12768 }
12769
12770 static int getLevelElementHiRes(int lx2, int ly2)
12771 {
12772   int lx = lx2 / 2;
12773   int ly = ly2 / 2;
12774   int dx = lx2 % 2;
12775   int dy = ly2 % 2;
12776   int element = Tile[lx][ly];
12777   unsigned int bitmask = (dx + 1) << (dy * 2);
12778
12779   if (IS_MM_WALL(element))
12780   {
12781     if (element & bitmask)
12782       return map_mm_wall_element(element);
12783     else
12784       return EL_EMPTY;
12785   }
12786
12787   return element;
12788 }
12789
12790 static void DrawLineElement(int x, int y, int element, boolean change_level)
12791 {
12792   SetElementHiRes(x, y, element, change_level);
12793 }
12794
12795 static void DrawLine(int from_x, int from_y, int to_x, int to_y,
12796                      int element, boolean change_level)
12797 {
12798   int xsize = ABS(to_x - from_x);
12799   int ysize = ABS(to_y - from_y);
12800   int dx = (to_x < from_x ? -1 : +1);
12801   int dy = (to_y < from_y ? -1 : +1);
12802   int i;
12803
12804   if (from_y == to_y)                   // horizontal line
12805   {
12806     for (i = 0; i <= xsize; i++)
12807       DrawLineElement(from_x + i * dx, from_y, element, change_level);
12808   }
12809   else if (from_x == to_x)              // vertical line
12810   {
12811     for (i = 0; i <= ysize; i++)
12812       DrawLineElement(from_x, from_y + i * dy, element, change_level);
12813   }
12814   else                                  // diagonal line
12815   {
12816     if (ysize < xsize)                  // a < 1
12817     {
12818       float a = (float)ysize / (float)xsize;
12819
12820       for (i = 0; i <= xsize; i++)
12821       {
12822         int x = dx * i;
12823         int y = dy * (int)(a * i + 0.5);
12824
12825         DrawLineElement(from_x + x, from_y + y, element, change_level);
12826       }
12827     }
12828     else                                // a >= 1
12829     {
12830       float a = (float)xsize / (float)ysize;
12831
12832       for (i = 0; i <= ysize; i++)
12833       {
12834         int x = dx * (int)(a * i + 0.5);
12835         int y = dy * i;
12836
12837         DrawLineElement(from_x + x, from_y + y, element, change_level);
12838       }
12839     }
12840   }
12841 }
12842
12843 static void DrawBox(int from_x, int from_y, int to_x, int to_y,
12844                     int element, boolean change_level)
12845 {
12846   DrawLine(from_x, from_y, from_x, to_y, element, change_level);
12847   DrawLine(from_x, to_y, to_x, to_y, element, change_level);
12848   DrawLine(to_x, to_y, to_x, from_y, element, change_level);
12849   DrawLine(to_x, from_y, from_x, from_y, element, change_level);
12850 }
12851
12852 static void DrawFilledBox(int from_x, int from_y, int to_x, int to_y,
12853                           int element, boolean change_level)
12854 {
12855   int y;
12856
12857   if (from_y > to_y)
12858     swap_number_pairs(&from_x, &from_y, &to_x, &to_y);
12859
12860   for (y = from_y; y <= to_y; y++)
12861     DrawLine(from_x, y, to_x, y, element, change_level);
12862 }
12863
12864 static void DrawArcExt(int from_x, int from_y, int to_x2, int to_y2,
12865                        int element, boolean change_level)
12866 {
12867   int to_x = to_x2 - (to_x2 > from_x ? +1 : -1);
12868   int to_y = to_y2 - (to_y2 > from_y ? +1 : -1);
12869   int len_x = ABS(to_x - from_x);
12870   int len_y = ABS(to_y - from_y);
12871   int radius, x, y;
12872
12873   radius = (int)(sqrt((float)(len_x * len_x + len_y * len_y)) + 0.5);
12874
12875   // not optimal (some points get drawn twice) but simple,
12876   // and fast enough for the few points we are drawing
12877
12878   for (x = 0; x <= radius; x++)
12879   {
12880     int sx, sy, sx2, sy2, lx, ly;
12881
12882     y = (int)(sqrt((float)(radius * radius - x * x)) + 0.5);
12883
12884     sx2 = from_x + x * (from_x < to_x2 ? +1 : -1);
12885     sy2 = from_y + y * (from_y < to_y2 ? +1 : -1);
12886     sx = getLoResScreenPos(sx2);
12887     sy = getLoResScreenPos(sy2);
12888     lx = sx + level_xpos;
12889     ly = sy + level_ypos;
12890
12891     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
12892       DrawLineElement(sx2, sy2, element, change_level);
12893   }
12894
12895   for (y = 0; y <= radius; y++)
12896   {
12897     int sx, sy, sx2, sy2, lx, ly;
12898
12899     x = (int)(sqrt((float)(radius * radius - y * y)) + 0.5);
12900
12901     sx2 = from_x + x * (from_x < to_x2 ? +1 : -1);
12902     sy2 = from_y + y * (from_y < to_y2 ? +1 : -1);
12903     sx = getLoResScreenPos(sx2);
12904     sy = getLoResScreenPos(sy2);
12905     lx = sx + level_xpos;
12906     ly = sy + level_ypos;
12907
12908     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
12909       DrawLineElement(sx2, sy2, element, change_level);
12910   }
12911 }
12912
12913 static void DrawArc(int from_x, int from_y, int to_x, int to_y,
12914                     int element, boolean change_level)
12915 {
12916   int to_x2 = to_x + (to_x < from_x ? -1 : +1);
12917   int to_y2 = to_y + (to_y > from_y ? +1 : -1);
12918
12919   DrawArcExt(from_x, from_y, to_x2, to_y2, element, change_level);
12920 }
12921
12922 #define DRAW_CIRCLES_BUTTON_AVAILABLE   0
12923
12924 #if DRAW_CIRCLES_BUTTON_AVAILABLE
12925 static void DrawCircle(int from_x, int from_y, int to_x, int to_y,
12926                        int element, boolean change_level)
12927 {
12928   int to_x2 = to_x + (to_x < from_x ? -1 : +1);
12929   int to_y2 = to_y + (to_y > from_y ? +1 : -1);
12930   int mirror_to_x2 = from_x - (to_x2 - from_x);
12931   int mirror_to_y2 = from_y - (to_y2 - from_y);
12932
12933   DrawArcExt(from_x, from_y, to_x2, to_y2, element, change_level);
12934   DrawArcExt(from_x, from_y, mirror_to_x2, to_y2, element, change_level);
12935   DrawArcExt(from_x, from_y, to_x2, mirror_to_y2, element, change_level);
12936   DrawArcExt(from_x, from_y, mirror_to_x2, mirror_to_y2, element, change_level);
12937 }
12938 #endif
12939
12940 static void DrawAreaBorder(int from_x, int from_y, int to_x, int to_y)
12941 {
12942   int from_sx, from_sy;
12943   int to_sx, to_sy;
12944
12945   if (from_x > to_x)
12946     swap_numbers(&from_x, &to_x);
12947
12948   if (from_y > to_y)
12949     swap_numbers(&from_y, &to_y);
12950
12951   from_sx = SX + from_x * ed_tilesize;
12952   from_sy = SY + from_y * ed_tilesize;
12953   to_sx = SX + (to_x + 1) * ed_tilesize - 1;
12954   to_sy = SY + (to_y + 1) * ed_tilesize - 1;
12955
12956   DrawSimpleWhiteLine(drawto, from_sx, from_sy, to_sx, from_sy);
12957   DrawSimpleWhiteLine(drawto, to_sx, from_sy, to_sx, to_sy);
12958   DrawSimpleWhiteLine(drawto, to_sx, to_sy, from_sx, to_sy);
12959   DrawSimpleWhiteLine(drawto, from_sx, to_sy, from_sx, from_sy);
12960
12961   if (from_x == to_x && from_y == to_y)
12962     MarkTileDirty(from_x/2, from_y/2);
12963   else
12964     redraw_mask |= REDRAW_FIELD;
12965 }
12966
12967 static void DrawAreaBox(int from_x, int from_y, int to_x, int to_y,
12968                         int element, boolean change_level)
12969 {
12970   DrawBox(from_x, from_y, to_x, to_y, element, change_level);
12971 }
12972
12973 static void SelectArea(int from_x, int from_y, int to_x, int to_y,
12974                        int element, boolean change_level)
12975 {
12976   if (element == -1 || change_level)
12977     DrawAreaBox(from_x, from_y, to_x, to_y, -1, FALSE);
12978   else
12979     DrawAreaBorder(from_x, from_y, to_x, to_y);
12980 }
12981
12982 // values for CopyBrushExt()
12983 #define CB_AREA_TO_BRUSH                0
12984 #define CB_BRUSH_TO_CURSOR              1
12985 #define CB_BRUSH_TO_LEVEL               2
12986 #define CB_DELETE_OLD_CURSOR            3
12987 #define CB_DUMP_BRUSH                   4
12988 #define CB_DUMP_BRUSH_SMALL             5
12989 #define CB_CLIPBOARD_TO_BRUSH           6
12990 #define CB_BRUSH_TO_CLIPBOARD           7
12991 #define CB_BRUSH_TO_CLIPBOARD_SMALL     8
12992 #define CB_UPDATE_BRUSH_POSITION        9
12993 #define CB_FLIP_BRUSH_X                 10
12994 #define CB_FLIP_BRUSH_Y                 11
12995 #define CB_FLIP_BRUSH_XY                12
12996
12997 #define MAX_CB_PART_SIZE        10
12998 #define MAX_CB_LINE_SIZE        (MAX_LEV_FIELDX + 1)    // text plus newline
12999 #define MAX_CB_NUM_LINES        (MAX_LEV_FIELDY)
13000 #define MAX_CB_TEXT_SIZE        (MAX_CB_LINE_SIZE *     \
13001                                  MAX_CB_NUM_LINES *     \
13002                                  MAX_CB_PART_SIZE)
13003
13004 static int getFlippedTileExt(int map[], int element)
13005 {
13006   int i;
13007
13008   for (i = 0; map[i] != -1; i++)
13009     if (map[i] == element)
13010       return map[i ^ 1];        // get flipped element by flipping LSB of index
13011
13012   return element;
13013 }
13014
13015 static int getFlippedTileX(int element)
13016 {
13017   int map[] =
13018   {
13019     EL_BD_BUTTERFLY_LEFT,               EL_BD_BUTTERFLY_RIGHT,
13020     EL_BD_FIREFLY_LEFT,                 EL_BD_FIREFLY_RIGHT,
13021     EL_BUG_LEFT,                        EL_BUG_RIGHT,
13022     EL_SPACESHIP_LEFT,                  EL_SPACESHIP_RIGHT,
13023     EL_PACMAN_LEFT,                     EL_PACMAN_RIGHT,
13024     EL_ARROW_LEFT,                      EL_ARROW_RIGHT,
13025     EL_MOLE_LEFT,                       EL_MOLE_RIGHT,
13026     EL_BALLOON_SWITCH_LEFT,             EL_BALLOON_SWITCH_RIGHT,
13027     EL_YAMYAM_LEFT,                     EL_YAMYAM_RIGHT,
13028     EL_SP_PORT_LEFT,                    EL_SP_PORT_RIGHT,
13029     EL_SP_GRAVITY_PORT_LEFT,            EL_SP_GRAVITY_PORT_RIGHT,
13030     EL_SP_GRAVITY_ON_PORT_LEFT,         EL_SP_GRAVITY_ON_PORT_RIGHT,
13031     EL_SP_GRAVITY_OFF_PORT_LEFT,        EL_SP_GRAVITY_OFF_PORT_RIGHT,
13032     EL_CONVEYOR_BELT_1_LEFT,            EL_CONVEYOR_BELT_1_RIGHT,
13033     EL_CONVEYOR_BELT_2_LEFT,            EL_CONVEYOR_BELT_2_RIGHT,
13034     EL_CONVEYOR_BELT_3_LEFT,            EL_CONVEYOR_BELT_3_RIGHT,
13035     EL_CONVEYOR_BELT_4_LEFT,            EL_CONVEYOR_BELT_4_RIGHT,
13036     EL_SPRING_LEFT,                     EL_SPRING_RIGHT,
13037     EL_SP_CHIP_LEFT,                    EL_SP_CHIP_RIGHT,
13038     EL_TUBE_VERTICAL_LEFT,              EL_TUBE_VERTICAL_RIGHT,
13039     EL_TUBE_LEFT_UP,                    EL_TUBE_RIGHT_UP,
13040     EL_TUBE_LEFT_DOWN,                  EL_TUBE_RIGHT_DOWN,
13041     EL_DC_STEELWALL_1_LEFT,             EL_DC_STEELWALL_1_RIGHT,
13042     EL_DC_STEELWALL_1_TOPLEFT,          EL_DC_STEELWALL_1_TOPRIGHT,
13043     EL_DC_STEELWALL_1_BOTTOMLEFT,       EL_DC_STEELWALL_1_BOTTOMRIGHT,
13044     EL_DC_STEELWALL_1_TOPLEFT_2,        EL_DC_STEELWALL_1_TOPRIGHT_2,
13045     EL_DC_STEELWALL_1_BOTTOMLEFT_2,     EL_DC_STEELWALL_1_BOTTOMRIGHT_2,
13046     EL_DC_STEELWALL_2_LEFT,             EL_DC_STEELWALL_2_RIGHT,
13047     EL_ACID_POOL_TOPLEFT,               EL_ACID_POOL_TOPRIGHT,
13048     EL_ACID_POOL_BOTTOMLEFT,            EL_ACID_POOL_BOTTOMRIGHT,
13049
13050     -1
13051   };
13052
13053   return getFlippedTileExt(map, element);
13054 }
13055
13056 static int getFlippedTileY(int element)
13057 {
13058   int map[] =
13059   {
13060     EL_BD_BUTTERFLY_UP,                 EL_BD_BUTTERFLY_DOWN,
13061     EL_BD_FIREFLY_UP,                   EL_BD_FIREFLY_DOWN,
13062     EL_BUG_UP,                          EL_BUG_DOWN,
13063     EL_SPACESHIP_UP,                    EL_SPACESHIP_DOWN,
13064     EL_PACMAN_UP,                       EL_PACMAN_DOWN,
13065     EL_ARROW_UP,                        EL_ARROW_DOWN,
13066     EL_MOLE_UP,                         EL_MOLE_DOWN,
13067     EL_BALLOON_SWITCH_UP,               EL_BALLOON_SWITCH_DOWN,
13068     EL_YAMYAM_UP,                       EL_YAMYAM_DOWN,
13069     EL_SP_PORT_UP,                      EL_SP_PORT_DOWN,
13070     EL_SP_GRAVITY_PORT_UP,              EL_SP_GRAVITY_PORT_DOWN,
13071     EL_SP_GRAVITY_ON_PORT_UP,           EL_SP_GRAVITY_ON_PORT_DOWN,
13072     EL_SP_GRAVITY_OFF_PORT_UP,          EL_SP_GRAVITY_OFF_PORT_DOWN,
13073     EL_SP_CHIP_TOP,                     EL_SP_CHIP_BOTTOM,
13074     EL_TUBE_HORIZONTAL_UP,              EL_TUBE_HORIZONTAL_DOWN,
13075     EL_TUBE_LEFT_UP,                    EL_TUBE_LEFT_DOWN,
13076     EL_TUBE_RIGHT_UP,                   EL_TUBE_RIGHT_DOWN,
13077     EL_DC_STEELWALL_1_TOP,              EL_DC_STEELWALL_1_BOTTOM,
13078     EL_DC_STEELWALL_1_TOPLEFT,          EL_DC_STEELWALL_1_BOTTOMLEFT,
13079     EL_DC_STEELWALL_1_TOPRIGHT,         EL_DC_STEELWALL_1_BOTTOMRIGHT,
13080     EL_DC_STEELWALL_1_TOPLEFT_2,        EL_DC_STEELWALL_1_BOTTOMLEFT_2,
13081     EL_DC_STEELWALL_1_TOPRIGHT_2,       EL_DC_STEELWALL_1_BOTTOMRIGHT_2,
13082     EL_DC_STEELWALL_2_TOP,              EL_DC_STEELWALL_2_BOTTOM,
13083     EL_EMC_WALL_1,                      EL_EMC_WALL_3,
13084
13085     -1
13086   };
13087
13088   return getFlippedTileExt(map, element);
13089 }
13090
13091 static int getFlippedTileXY(int element)
13092 {
13093   int map[] =
13094   {
13095     EL_BD_BUTTERFLY_LEFT,               EL_BD_BUTTERFLY_UP,
13096     EL_BD_BUTTERFLY_RIGHT,              EL_BD_BUTTERFLY_DOWN,
13097     EL_BD_FIREFLY_LEFT,                 EL_BD_FIREFLY_UP,
13098     EL_BD_FIREFLY_RIGHT,                EL_BD_FIREFLY_DOWN,
13099     EL_BUG_LEFT,                        EL_BUG_UP,
13100     EL_BUG_RIGHT,                       EL_BUG_DOWN,
13101     EL_SPACESHIP_LEFT,                  EL_SPACESHIP_UP,
13102     EL_SPACESHIP_RIGHT,                 EL_SPACESHIP_DOWN,
13103     EL_PACMAN_LEFT,                     EL_PACMAN_UP,
13104     EL_PACMAN_RIGHT,                    EL_PACMAN_DOWN,
13105     EL_ARROW_LEFT,                      EL_ARROW_UP,
13106     EL_ARROW_RIGHT,                     EL_ARROW_DOWN,
13107     EL_MOLE_LEFT,                       EL_MOLE_UP,
13108     EL_MOLE_RIGHT,                      EL_MOLE_DOWN,
13109     EL_BALLOON_SWITCH_LEFT,             EL_BALLOON_SWITCH_UP,
13110     EL_BALLOON_SWITCH_RIGHT,            EL_BALLOON_SWITCH_DOWN,
13111     EL_YAMYAM_LEFT,                     EL_YAMYAM_UP,
13112     EL_YAMYAM_RIGHT,                    EL_YAMYAM_DOWN,
13113     EL_SP_PORT_LEFT,                    EL_SP_PORT_UP,
13114     EL_SP_PORT_RIGHT,                   EL_SP_PORT_DOWN,
13115     EL_SP_GRAVITY_PORT_LEFT,            EL_SP_GRAVITY_PORT_UP,
13116     EL_SP_GRAVITY_PORT_RIGHT,           EL_SP_GRAVITY_PORT_DOWN,
13117     EL_SP_GRAVITY_ON_PORT_LEFT,         EL_SP_GRAVITY_ON_PORT_UP,
13118     EL_SP_GRAVITY_ON_PORT_RIGHT,        EL_SP_GRAVITY_ON_PORT_DOWN,
13119     EL_SP_GRAVITY_OFF_PORT_LEFT,        EL_SP_GRAVITY_OFF_PORT_UP,
13120     EL_SP_GRAVITY_OFF_PORT_RIGHT,       EL_SP_GRAVITY_OFF_PORT_DOWN,
13121     EL_SP_CHIP_LEFT,                    EL_SP_CHIP_TOP,
13122     EL_SP_CHIP_RIGHT,                   EL_SP_CHIP_BOTTOM,
13123     EL_TUBE_VERTICAL,                   EL_TUBE_HORIZONTAL,
13124     EL_TUBE_VERTICAL_LEFT,              EL_TUBE_HORIZONTAL_UP,
13125     EL_TUBE_VERTICAL_RIGHT,             EL_TUBE_HORIZONTAL_DOWN,
13126     EL_TUBE_LEFT_DOWN,                  EL_TUBE_RIGHT_UP,
13127     EL_DC_STEELWALL_1_LEFT,             EL_DC_STEELWALL_1_TOP,
13128     EL_DC_STEELWALL_1_RIGHT,            EL_DC_STEELWALL_1_BOTTOM,
13129     EL_DC_STEELWALL_1_HORIZONTAL,       EL_DC_STEELWALL_1_VERTICAL,
13130     EL_DC_STEELWALL_1_TOPRIGHT,         EL_DC_STEELWALL_1_BOTTOMLEFT,
13131     EL_DC_STEELWALL_1_TOPRIGHT_2,       EL_DC_STEELWALL_1_BOTTOMLEFT_2,
13132     EL_DC_STEELWALL_2_LEFT,             EL_DC_STEELWALL_2_TOP,
13133     EL_DC_STEELWALL_2_RIGHT,            EL_DC_STEELWALL_2_BOTTOM,
13134     EL_DC_STEELWALL_2_HORIZONTAL,       EL_DC_STEELWALL_2_VERTICAL,
13135     EL_EXPANDABLE_WALL_HORIZONTAL,      EL_EXPANDABLE_WALL_VERTICAL,
13136     EL_EXPANDABLE_STEELWALL_HORIZONTAL, EL_EXPANDABLE_STEELWALL_VERTICAL,
13137
13138     -1
13139   };
13140
13141   return getFlippedTileExt(map, element);
13142 }
13143
13144 static int getFlippedTile(int element, int mode)
13145 {
13146   if (IS_MM_ELEMENT(element))
13147   {
13148     // get MM game element
13149     element = map_element_RND_to_MM(element);
13150
13151     // get flipped game element
13152     element = (mode == CB_FLIP_BRUSH_X  ? getFlippedTileX_MM(element) :
13153                mode == CB_FLIP_BRUSH_Y  ? getFlippedTileY_MM(element) :
13154                mode == CB_FLIP_BRUSH_XY ? getFlippedTileXY_MM(element) :
13155                element);
13156
13157     // get RND game element again
13158     element = map_element_MM_to_RND(element);
13159   }
13160   else
13161   {
13162     // get flipped game element
13163     element = (mode == CB_FLIP_BRUSH_X  ? getFlippedTileX(element) :
13164                mode == CB_FLIP_BRUSH_Y  ? getFlippedTileY(element) :
13165                mode == CB_FLIP_BRUSH_XY ? getFlippedTileXY(element) :
13166                element);
13167   }
13168
13169   return element;
13170 }
13171
13172 static void SwapFlippedTiles(short *tile1, short *tile2, int mode)
13173 {
13174   // flip tiles
13175   short tile1_flipped = getFlippedTile(*tile1, mode);
13176   short tile2_flipped = getFlippedTile(*tile2, mode);
13177
13178   // swap tiles
13179   *tile1 = tile2_flipped;
13180   *tile2 = tile1_flipped;
13181 }
13182
13183 static void DrawBrushElement(int sx, int sy, int element, boolean change_level)
13184 {
13185   DrawLineElement(sx, sy, element, change_level);
13186 }
13187
13188 static void CopyBrushExt(int from_x, int from_y, int to_x, int to_y,
13189                          int button, int mode)
13190 {
13191   static short brush_buffer[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
13192   static int brush_width, brush_height;
13193   static int last_cursor_x = -1, last_cursor_y = -1;
13194   static boolean delete_old_brush = FALSE;
13195   int new_element = BUTTON_ELEMENT(button);
13196   int x, y;
13197
13198   if (mode == CB_DUMP_BRUSH ||
13199       mode == CB_DUMP_BRUSH_SMALL ||
13200       mode == CB_BRUSH_TO_CLIPBOARD ||
13201       mode == CB_BRUSH_TO_CLIPBOARD_SMALL)
13202   {
13203     if (edit_mode != ED_MODE_DRAWING)
13204       return;
13205
13206     char part[MAX_CB_PART_SIZE + 1] = "";
13207     char text[MAX_CB_TEXT_SIZE + 1] = "";
13208     int width  = (draw_with_brush ? brush_width  : lev_fieldx);
13209     int height = (draw_with_brush ? brush_height : lev_fieldy);
13210     char *format = "%s%03d";
13211
13212     for (y = 0; y < height; y++)
13213       for (x = 0; x < width; x++)
13214         if ((draw_with_brush ? brush_buffer[x][y] : Tile[x][y]) > 999)
13215           format = "%s%04d";
13216
13217     for (y = 0; y < height; y++)
13218     {
13219       for (x = 0; x < width; x++)
13220       {
13221         int element = (draw_with_brush ? brush_buffer[x][y] : Tile[x][y]);
13222         char *prefix = (mode == CB_DUMP_BRUSH ||
13223                         mode == CB_BRUSH_TO_CLIPBOARD ? "`" : "¸");
13224
13225         if (element >= NUM_FILE_ELEMENTS)
13226           element = EL_UNKNOWN;
13227
13228         // copy brush to level sketch text buffer for the R'n'D forum:
13229         // - large tiles: `xxx or `xxxx (0x60 ASCII)
13230         // - small tiles: Â¸xxx or Â¸xxxx (0xb8 ISO-8859-1, 0xc2b8 UTF-8)
13231         snprintf(part, MAX_CB_PART_SIZE + 1, format, prefix, element);
13232         strcat(text, part);
13233       }
13234
13235       strcat(text, "\n");
13236     }
13237
13238     if (mode == CB_BRUSH_TO_CLIPBOARD ||
13239         mode == CB_BRUSH_TO_CLIPBOARD_SMALL)
13240       SDL_SetClipboardText(text);
13241     else
13242       Print("%s", text);        // print brush data to console and log file
13243
13244     return;
13245   }
13246
13247   if (mode == CB_CLIPBOARD_TO_BRUSH)
13248   {
13249     if (edit_mode != ED_MODE_DRAWING)
13250       return;
13251
13252     if (!SDL_HasClipboardText())
13253     {
13254       Request("Clipboard is empty!", REQ_CONFIRM);
13255
13256       return;
13257     }
13258
13259     boolean copy_to_brush = (draw_with_brush ||
13260                              drawing_function == GADGET_ID_GRAB_BRUSH);
13261
13262     // this will delete the old brush, if already drawing with a brush
13263     if (copy_to_brush)
13264       ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS], MB_LEFTBUTTON);
13265
13266     // initialization is required for "odd" (incomplete) clipboard content
13267     for (x = 0; x < MAX_LEV_FIELDX; x++)
13268       for (y = 0; y < MAX_LEV_FIELDY; y++)
13269         brush_buffer[x][y] = EL_EMPTY;
13270
13271     brush_width  = 0;
13272     brush_height = 0;
13273     x = 0;
13274     y = 0;
13275
13276     char *clipboard_text = SDL_GetClipboardText();
13277     char *ptr = clipboard_text;
13278     boolean allow_new_row = FALSE;
13279     boolean stop = FALSE;
13280
13281     while (*ptr && !stop)
13282     {
13283       boolean prefix_found = FALSE;
13284       boolean start_new_row = FALSE;
13285
13286       // level sketch element number prefixes (may be multi-byte characters)
13287       char *prefix_list[] = { "`", "¸" };
13288       int i;
13289
13290       for (i = 0; i < ARRAY_SIZE(prefix_list); i++)
13291       {
13292         char *prefix = prefix_list[i];
13293
13294         // check if string is large enough for prefix
13295         if (strlen(ptr) < strlen(prefix))
13296         {
13297           stop = TRUE;
13298
13299           break;
13300         }
13301
13302         // check if string starts with prefix
13303         if (strPrefix(ptr, prefix))
13304         {
13305           ptr += strlen(prefix);
13306
13307           prefix_found = TRUE;
13308
13309           break;
13310         }
13311       }
13312
13313       // check if prefix found and followed by (at least) three digits
13314       if (prefix_found &&
13315           strlen(ptr) >= 3 &&
13316           ptr[0] >= '0' && ptr[0] <= '9' &&
13317           ptr[1] >= '0' && ptr[1] <= '9' &&
13318           ptr[2] >= '0' && ptr[2] <= '9')
13319       {
13320         int element = ((ptr[0] - '0') * 100 +
13321                        (ptr[1] - '0') * 10 +
13322                        (ptr[2] - '0'));
13323
13324         ptr += 3;
13325
13326         // level sketch element number might consist of four digits
13327         if (ptr[0] >= '0' && ptr[0] <= '9')
13328         {
13329           element = element * 10 + (ptr[0] - '0');
13330           ptr++;
13331         }
13332
13333         // remap some (historic, now obsolete) elements
13334         element = getMappedElement(element);
13335
13336         if (element >= NUM_FILE_ELEMENTS)
13337           element = EL_UNKNOWN;
13338
13339         brush_buffer[x][y] = element;
13340
13341         brush_width  = MAX(x + 1, brush_width);
13342         brush_height = MAX(y + 1, brush_height);
13343
13344         x++;
13345
13346         if (x >= MAX_LEV_FIELDX)
13347           start_new_row = TRUE;
13348
13349         allow_new_row = TRUE;
13350       }
13351       else
13352       {
13353         if ((*ptr == '\n' || *ptr == '\r') && allow_new_row)
13354           start_new_row = TRUE;
13355
13356         ptr++;          // !!! FIX THIS for real UTF-8 handling !!!
13357       }
13358
13359       if (start_new_row)
13360       {
13361         x = 0;
13362         y++;
13363
13364         if (y >= MAX_LEV_FIELDY)
13365           stop = TRUE;
13366
13367         allow_new_row = FALSE;
13368       }
13369     }
13370
13371     SDL_free(clipboard_text);
13372
13373     if (brush_width == 0 || brush_height == 0)
13374     {
13375       Request("No level sketch found in clipboard!", REQ_CONFIRM);
13376
13377       return;
13378     }
13379
13380     if (copy_to_brush)
13381     {
13382       struct GadgetInfo *gi = level_editor_gadget[GADGET_ID_DRAWING_LEVEL];
13383       int mx, my;
13384
13385       SDL_GetMouseState(&mx, &my);
13386
13387       // if inside drawing area, activate and draw brush at last mouse position
13388       if (mx >= gi->x && mx < gi->x + gi->width &&
13389           my >= gi->y && my < gi->y + gi->height)
13390         CopyBrushToCursor(last_cursor_x, last_cursor_y);
13391
13392       draw_with_brush = TRUE;
13393     }
13394     else
13395     {
13396       char request[100];
13397
13398       sprintf(request, "Replace level with %dx%d level sketch from clipboard?",
13399               brush_width, brush_height);
13400
13401       if (!Request(request, REQ_ASK))
13402         return;
13403
13404       for (x = 0; x < MAX_LEV_FIELDX; x++)
13405         for (y = 0; y < MAX_LEV_FIELDY; y++)
13406           Tile[x][y] = brush_buffer[x][y];
13407
13408       lev_fieldx = level.fieldx = brush_width;
13409       lev_fieldy = level.fieldy = brush_height;
13410
13411       boolean use_bd_engine = TRUE;
13412       boolean use_em_engine = TRUE;
13413       boolean use_sp_engine = TRUE;
13414       boolean use_mm_engine = TRUE;
13415
13416       for (x = 0; x < MAX_LEV_FIELDX; x++)
13417       {
13418         for (y = 0; y < MAX_LEV_FIELDY; y++)
13419         {
13420           int element = Tile[x][y];
13421
13422           if (!IS_BD_ELEMENT(element) && !IS_PLAYER_ELEMENT(element))
13423             use_bd_engine = FALSE;
13424
13425           if (!IS_EM_ELEMENT(element) && !IS_PLAYER_ELEMENT(element))
13426             use_em_engine = FALSE;
13427
13428           if (!IS_SP_ELEMENT(element))
13429             use_sp_engine = FALSE;
13430
13431           if (!IS_MM_ELEMENT(element) && element != EL_EMPTY)
13432             use_mm_engine = FALSE;
13433         }
13434       }
13435
13436       level.game_engine_type = (use_bd_engine ? GAME_ENGINE_TYPE_BD :
13437                                 use_em_engine ? GAME_ENGINE_TYPE_EM :
13438                                 use_sp_engine ? GAME_ENGINE_TYPE_SP :
13439                                 use_mm_engine ? GAME_ENGINE_TYPE_MM :
13440                                 GAME_ENGINE_TYPE_RND);
13441
13442       // update element selection list
13443       ReinitializeElementList();
13444       ModifyEditorElementList();
13445
13446       SetBorderElement();
13447
13448       DrawEditModeWindow();
13449       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13450     }
13451
13452     return;
13453   }
13454
13455   if (mode == CB_DELETE_OLD_CURSOR && !delete_old_brush)
13456     return;
13457
13458   if (mode == CB_AREA_TO_BRUSH)
13459   {
13460     int from_lx, from_ly;
13461
13462     if (from_x > to_x)
13463       swap_numbers(&from_x, &to_x);
13464
13465     if (from_y > to_y)
13466       swap_numbers(&from_y, &to_y);
13467
13468     brush_width = to_x - from_x + 1;
13469     brush_height = to_y - from_y + 1;
13470
13471     from_lx = from_x + level_xpos;
13472     from_ly = from_y + level_ypos;
13473
13474     for (y = 0; y < brush_height; y++)
13475     {
13476       for (x = 0; x < brush_width; x++)
13477       {
13478         brush_buffer[x][y] = Tile[from_lx + x][from_ly + y];
13479
13480         if (button != 1)
13481           DrawBrushElement(from_x + x, from_y + y, new_element, TRUE);
13482       }
13483     }
13484
13485     if (button != 1)
13486       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13487
13488     delete_old_brush = FALSE;
13489   }
13490   else if (mode == CB_BRUSH_TO_CURSOR || mode == CB_DELETE_OLD_CURSOR ||
13491            mode == CB_BRUSH_TO_LEVEL)
13492   {
13493     int cursor_x = (mode == CB_DELETE_OLD_CURSOR ? last_cursor_x : from_x);
13494     int cursor_y = (mode == CB_DELETE_OLD_CURSOR ? last_cursor_y : from_y);
13495     int cursor_from_x = cursor_x - brush_width / 2;
13496     int cursor_from_y = cursor_y - brush_height / 2;
13497     int border_from_x = cursor_x, border_from_y = cursor_y;
13498     int border_to_x = cursor_x, border_to_y = cursor_y;
13499
13500     if (mode != CB_DELETE_OLD_CURSOR && delete_old_brush)
13501       CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
13502
13503     if (!IN_ED_FIELD(cursor_x, cursor_y) ||
13504         !IN_LEV_FIELD(cursor_x + level_xpos, cursor_y + level_ypos))
13505     {
13506       delete_old_brush = FALSE;
13507
13508       return;
13509     }
13510
13511     for (y = 0; y < brush_height; y++)
13512     {
13513       for (x = 0; x < brush_width; x++)
13514       {
13515         int sx = cursor_from_x + x;
13516         int sy = cursor_from_y + y;
13517         int lx = sx + level_xpos;
13518         int ly = sy + level_ypos;
13519         boolean change_level = (mode == CB_BRUSH_TO_LEVEL);
13520         int element = (mode == CB_DELETE_OLD_CURSOR ? -1 :
13521                        mode == CB_BRUSH_TO_CURSOR || button == 1 ?
13522                        brush_buffer[x][y] : new_element);
13523
13524         if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
13525         {
13526           if (sx < border_from_x)
13527             border_from_x = sx;
13528           else if (sx > border_to_x)
13529             border_to_x = sx;
13530           if (sy < border_from_y)
13531             border_from_y = sy;
13532           else if (sy > border_to_y)
13533             border_to_y = sy;
13534
13535           DrawBrushElement(sx, sy, element, change_level);
13536         }
13537       }
13538     }
13539
13540     if (mode != CB_DELETE_OLD_CURSOR)
13541       DrawAreaBorder(border_from_x, border_from_y, border_to_x, border_to_y);
13542
13543     last_cursor_x = cursor_x;
13544     last_cursor_y = cursor_y;
13545
13546     delete_old_brush = TRUE;
13547   }
13548   else if (mode == CB_FLIP_BRUSH_X)
13549   {
13550     for (y = 0; y < brush_height; y++)
13551       for (x = 0; x < (brush_width + 1) / 2; x++)
13552         SwapFlippedTiles(&brush_buffer[x][y],
13553                          &brush_buffer[brush_width - x - 1][y], mode);
13554
13555     CopyBrushExt(last_cursor_x, last_cursor_y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13556   }
13557   else if (mode == CB_FLIP_BRUSH_Y)
13558   {
13559     for (y = 0; y < (brush_height + 1) / 2; y++)
13560       for (x = 0; x < brush_width; x++)
13561         SwapFlippedTiles(&brush_buffer[x][y],
13562                          &brush_buffer[x][brush_height - y - 1], mode);
13563
13564     CopyBrushExt(last_cursor_x, last_cursor_y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13565   }
13566   else if (mode == CB_FLIP_BRUSH_XY)
13567   {
13568     CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
13569
13570     for (y = 0; y < MAX(brush_width, brush_height); y++)
13571       for (x = 0; x <= y; x++)
13572         SwapFlippedTiles(&brush_buffer[x][y],
13573                          &brush_buffer[y][x], mode);
13574
13575     swap_numbers(&brush_width, &brush_height);
13576
13577     CopyBrushExt(last_cursor_x, last_cursor_y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13578   }
13579
13580   if (mode == CB_UPDATE_BRUSH_POSITION)
13581   {
13582     last_cursor_x = from_x;
13583     last_cursor_y = from_y;
13584   }
13585 }
13586
13587 static void CopyAreaToBrush(int from_x, int from_y, int to_x, int to_y,
13588                             int button)
13589 {
13590   CopyBrushExt(from_x, from_y, to_x, to_y, button, CB_AREA_TO_BRUSH);
13591 }
13592
13593 static void CopyBrushToLevel(int x, int y, int button)
13594 {
13595   CopyBrushExt(x, y, 0, 0, button, CB_BRUSH_TO_LEVEL);
13596 }
13597
13598 static void CopyBrushToCursor(int x, int y)
13599 {
13600   CopyBrushExt(x, y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13601 }
13602
13603 static void UpdateBrushPosition(int x, int y)
13604 {
13605   CopyBrushExt(x, y, 0, 0, 0, CB_UPDATE_BRUSH_POSITION);
13606 }
13607
13608 static void DeleteBrushFromCursor(void)
13609 {
13610   CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
13611 }
13612
13613 static void FlipBrushX(void)
13614 {
13615   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_X);
13616 }
13617
13618 static void FlipBrushY(void)
13619 {
13620   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_Y);
13621 }
13622
13623 static void RotateBrush(void)
13624 {
13625   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_XY);
13626   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_X);
13627 }
13628
13629 void DumpBrush(void)
13630 {
13631   CopyBrushExt(0, 0, 0, 0, 0, CB_DUMP_BRUSH);
13632 }
13633
13634 void DumpBrush_Small(void)
13635 {
13636   CopyBrushExt(0, 0, 0, 0, 0, CB_DUMP_BRUSH_SMALL);
13637 }
13638
13639 void CopyClipboardToBrush(void)
13640 {
13641   CopyBrushExt(0, 0, 0, 0, 0, CB_CLIPBOARD_TO_BRUSH);
13642 }
13643
13644 void CopyBrushToClipboard(void)
13645 {
13646   CopyBrushExt(0, 0, 0, 0, 0, CB_BRUSH_TO_CLIPBOARD);
13647 }
13648
13649 void CopyBrushToClipboard_Small(void)
13650 {
13651   CopyBrushExt(0, 0, 0, 0, 0, CB_BRUSH_TO_CLIPBOARD_SMALL);
13652 }
13653
13654 void UndoLevelEditorOperation(void)
13655 {
13656   ClickOnGadget(level_editor_gadget[GADGET_ID_UNDO], -1);
13657 }
13658
13659 void RedoLevelEditorOperation(void)
13660 {
13661   ClickOnGadget(level_editor_gadget[GADGET_ID_UNDO], 3);
13662 }
13663
13664 static void FloodFill(int from_x, int from_y, int fill_element)
13665 {
13666   FloodFillLevel(from_x, from_y, fill_element, Tile, lev_fieldx, lev_fieldy);
13667 }
13668
13669 static void FloodFillWall_MM(int from_sx2, int from_sy2, int fill_element)
13670 {
13671   int from_x = from_sx2 + 2 * level_xpos;
13672   int from_y = from_sy2 + 2 * level_ypos;
13673   int max_fillx = lev_fieldx * 2;
13674   int max_filly = lev_fieldy * 2;
13675   short Fill[max_fillx][max_filly];
13676   int x, y;
13677
13678   for (x = 0; x < max_fillx; x++)
13679     for (y = 0; y < max_filly; y++)
13680       Fill[x][y] = getLevelElementHiRes(x, y);
13681
13682   FloodFillLevelExt(from_x, from_y, fill_element, max_fillx, max_filly,
13683                     Fill, max_fillx, max_filly);
13684
13685   for (x = 0; x < max_fillx; x++)
13686     for (y = 0; y < max_filly; y++)
13687       if (Fill[x][y] == fill_element)
13688         SetLevelElementHiRes(x, y, Fill[x][y]);
13689 }
13690
13691 // values for DrawLevelText() modes
13692 #define TEXT_INIT               0
13693 #define TEXT_SETCURSOR          1
13694 #define TEXT_WRITECHAR          2
13695 #define TEXT_BACKSPACE          3
13696 #define TEXT_NEWLINE            4
13697 #define TEXT_END                5
13698 #define TEXT_QUERY_TYPING       6
13699
13700 static int DrawLevelText(int sx, int sy, char letter, int mode)
13701 {
13702   static short delete_buffer[MAX_LEV_FIELDX];
13703   static int start_sx;
13704   static int last_sx, last_sy;
13705   static boolean typing = FALSE;
13706   int letter_element;
13707   int lx = 0, ly = 0;
13708
13709   // map lower case letters to upper case and convert special characters
13710   if (letter >= 'a' && letter <= 'z')
13711     letter_element = EL_CHAR_ASCII0 + letter + (int)('A' - 'a');
13712   else if (letter == CHAR_BYTE_UMLAUT_a || letter == CHAR_BYTE_UMLAUT_A)
13713     letter_element = EL_CHAR_AUMLAUT;
13714   else if (letter == CHAR_BYTE_UMLAUT_o || letter == CHAR_BYTE_UMLAUT_O)
13715     letter_element = EL_CHAR_OUMLAUT;
13716   else if (letter == CHAR_BYTE_UMLAUT_u || letter == CHAR_BYTE_UMLAUT_U)
13717     letter_element = EL_CHAR_UUMLAUT;
13718   else if (letter == '^')
13719     letter_element = EL_CHAR_COPYRIGHT;
13720   else
13721     letter_element = EL_CHAR_ASCII0 + letter;
13722
13723   if (mode != TEXT_INIT)
13724   {
13725     if (!typing)
13726       return FALSE;
13727
13728     if (mode != TEXT_SETCURSOR)
13729     {
13730       sx = last_sx;
13731       sy = last_sy;
13732     }
13733
13734     lx = last_sx + level_xpos;
13735     ly = last_sy + level_ypos;
13736   }
13737
13738   switch (mode)
13739   {
13740     case TEXT_INIT:
13741       if (typing)
13742         DrawLevelText(0, 0, 0, TEXT_END);
13743
13744       typing = TRUE;
13745       start_sx = sx;
13746       last_sx = sx;
13747       last_sy = sy;
13748       DrawLevelText(sx, sy, 0, TEXT_SETCURSOR);
13749       break;
13750
13751     case TEXT_SETCURSOR:
13752       DrawEditorElement(last_sx, last_sy, Tile[lx][ly]);
13753       DrawAreaBorder(sx, sy, sx, sy);
13754       StartTextInput(SX + sx * ed_tilesize, SY + sy * ed_tilesize,
13755                      ed_tilesize, ed_tilesize);
13756       last_sx = sx;
13757       last_sy = sy;
13758       break;
13759
13760     case TEXT_WRITECHAR:
13761       if (letter_element >= EL_CHAR_START && letter_element <= EL_CHAR_END)
13762       {
13763         if (new_element1 >= EL_STEEL_CHAR_START &&
13764             new_element1 <= EL_STEEL_CHAR_END)
13765           letter_element = letter_element - EL_CHAR_START + EL_STEEL_CHAR_START;
13766
13767         delete_buffer[sx - start_sx] = Tile[lx][ly];
13768         Tile[lx][ly] = letter_element;
13769
13770         if (sx + 1 < ed_fieldx && lx + 1 < lev_fieldx)
13771           DrawLevelText(sx + 1, sy, 0, TEXT_SETCURSOR);
13772         else if (sy + 1 < ed_fieldy && ly + 1 < lev_fieldy)
13773           DrawLevelText(start_sx, sy + 1, 0, TEXT_SETCURSOR);
13774         else
13775           DrawLevelText(0, 0, 0, TEXT_END);
13776
13777         level.changed = TRUE;
13778       }
13779       break;
13780
13781     case TEXT_BACKSPACE:
13782       if (sx > start_sx)
13783       {
13784         Tile[lx - 1][ly] = delete_buffer[sx - start_sx - 1];
13785         DrawEditorElement(sx - 1, sy, Tile[lx - 1][ly]);
13786         DrawLevelText(sx - 1, sy, 0, TEXT_SETCURSOR);
13787       }
13788       break;
13789
13790     case TEXT_NEWLINE:
13791       if (sy + 1 < ed_fieldy && ly + 1 < lev_fieldy)
13792         DrawLevelText(start_sx, sy + 1, 0, TEXT_SETCURSOR);
13793       else
13794         DrawLevelText(0, 0, 0, TEXT_END);
13795       break;
13796
13797     case TEXT_END:
13798       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13799       DrawEditorElement(sx, sy, Tile[lx][ly]);
13800       StopTextInput();
13801       typing = FALSE;
13802       break;
13803
13804     case TEXT_QUERY_TYPING:
13805       break;
13806
13807     default:
13808       break;
13809   }
13810
13811   return typing;
13812 }
13813
13814 static void SetTextCursor(int unused_sx, int unused_sy, int sx, int sy,
13815                           int element, boolean change_level)
13816 {
13817   int lx = sx + level_xpos;
13818   int ly = sy + level_ypos;
13819
13820   if (element == -1)
13821     DrawEditorElement(sx, sy, Tile[lx][ly]);
13822   else
13823     DrawAreaBorder(sx, sy, sx, sy);
13824 }
13825
13826 static void CheckLevelBorderElement(boolean redraw_playfield)
13827 {
13828   int last_border_element = BorderElement;
13829
13830   SetBorderElement();
13831
13832   if (redraw_playfield && BorderElement != last_border_element)
13833     DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
13834 }
13835
13836 static void CopyLevelToUndoBuffer(int mode)
13837 {
13838   static boolean accumulated_undo = FALSE;
13839   boolean new_undo_buffer_position = TRUE;
13840   int x, y;
13841
13842   if (undo_buffer_steps == 0)
13843     accumulated_undo = FALSE;
13844
13845   switch (mode)
13846   {
13847     case UNDO_IMMEDIATE:
13848       accumulated_undo = FALSE;
13849       break;
13850
13851     case UNDO_ACCUMULATE:
13852       if (accumulated_undo)
13853         new_undo_buffer_position = FALSE;
13854       accumulated_undo = TRUE;
13855       break;
13856
13857     default:
13858       break;
13859   }
13860
13861   if (new_undo_buffer_position)
13862   {
13863     // advance position in undo buffer ring
13864     undo_buffer_position = (undo_buffer_position + 1) % NUM_UNDO_STEPS;
13865
13866     if (undo_buffer_steps < NUM_UNDO_STEPS - 1)
13867       undo_buffer_steps++;
13868   }
13869
13870   // always reset redo buffer when storing level change into undo buffer
13871   redo_buffer_steps = 0;
13872
13873   for (x = 0; x < lev_fieldx; x++)
13874     for (y = 0; y < lev_fieldy; y++)
13875       UndoBuffer[undo_buffer_position][x][y] = Tile[x][y];
13876
13877   // check if drawing operation forces change of border style
13878   CheckLevelBorderElement(TRUE);
13879
13880   level.changed = TRUE;
13881 }
13882
13883 static void RandomPlacement(int new_element)
13884 {
13885   static boolean free_position[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
13886   int num_free_positions = 0;
13887   int num_percentage, num_elements;
13888   int x, y;
13889
13890   ResetIntelliDraw();
13891
13892   // determine number of free positions for randomly placing the new element
13893   for (x = 0; x < lev_fieldx; x++) for (y = 0; y < lev_fieldy; y++)
13894   {
13895     free_position[x][y] =
13896       (random_placement_background_restricted ?
13897        Tile[x][y] == random_placement_background_element :
13898        Tile[x][y] != new_element);
13899
13900     if (free_position[x][y])
13901       num_free_positions++;
13902   }
13903
13904   // determine number of new elements to place there
13905   num_percentage = num_free_positions * random_placement_value / 100;
13906   num_elements = (random_placement_method == RANDOM_USE_PERCENTAGE ?
13907                   num_percentage : random_placement_value);
13908
13909   // if less free positions than elements to place, fill all these positions
13910   if (num_free_positions < num_elements)
13911   {
13912     for (x = 0; x < lev_fieldx; x++)
13913       for (y = 0; y < lev_fieldy; y++)
13914         if (free_position[x][y])
13915           SetElement(x, y, new_element);
13916   }
13917   else
13918   {
13919     while (num_elements > 0)
13920     {
13921       x = GetSimpleRandom(lev_fieldx);
13922       y = GetSimpleRandom(lev_fieldy);
13923
13924       // don't place element at the same position twice
13925       if (free_position[x][y])
13926       {
13927         free_position[x][y] = FALSE;
13928         SetElement(x, y, new_element);
13929         num_elements--;
13930       }
13931     }
13932   }
13933
13934   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
13935   CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13936 }
13937
13938 static void WrapLevel(int dx, int dy)
13939 {
13940   int wrap_dx = lev_fieldx - dx;
13941   int wrap_dy = lev_fieldy - dy;
13942   int x, y;
13943
13944   for (x = 0; x < lev_fieldx; x++)
13945     for (y = 0; y < lev_fieldy; y++)
13946       TileBackup[x][y] = Tile[x][y];
13947
13948   for (x = 0; x < lev_fieldx; x++)
13949     for (y = 0; y < lev_fieldy; y++)
13950       Tile[x][y] =
13951         TileBackup[(x + wrap_dx) % lev_fieldx][(y + wrap_dy) % lev_fieldy];
13952
13953   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
13954   CopyLevelToUndoBuffer(UNDO_ACCUMULATE);
13955 }
13956
13957 static void DrawAreaElementHighlight(boolean highlighted,
13958                                      boolean highlighted_similar)
13959 {
13960   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
13961
13962   if (!highlighted)
13963     return;
13964
13965   int x, y;
13966
13967   for (x = 0; x < ed_fieldx; x++)
13968   {
13969     for (y = 0; y < ed_fieldy; y++)
13970     {
13971       boolean highlight = FALSE;
13972       int lx = x + level_xpos;
13973       int ly = y + level_ypos;
13974
13975       if (!IN_LEV_FIELD(lx, ly))
13976         continue;
13977
13978       // check if element is the same
13979       if (Tile[lx][ly] == new_element1)
13980         highlight = TRUE;
13981
13982       // check if element is similar
13983       if (highlighted_similar &&
13984           strEqual(element_info[Tile[lx][ly]].class_name,
13985                    element_info[new_element1].class_name))
13986         highlight = TRUE;
13987
13988       // check if element is matching MM style wall
13989       if (IS_MM_WALL(Tile[lx][ly]) &&
13990           map_mm_wall_element(Tile[lx][ly]) == new_element1)
13991         highlight = TRUE;
13992
13993       if (!highlight)
13994         continue;
13995
13996       if (IS_MM_WALL(Tile[lx][ly]) && !highlighted_similar)
13997       {
13998         int i;
13999
14000         for (i = 0; i < 4; i++)
14001         {
14002           if (!(MM_WALL_BITS(Tile[lx][ly]) & (1 << i)))
14003             continue;
14004
14005           int xx = x * 2 + (i % 2);
14006           int yy = y * 2 + (i / 2);
14007           int sx = SX + xx * ed_tilesize / 2;
14008           int sy = SY + yy * ed_tilesize / 2;
14009           int from_sx = sx;
14010           int from_sy = sy;
14011           int to_sx = sx + ed_tilesize / 2 - 1;
14012           int to_sy = sy + ed_tilesize / 2 - 1;
14013
14014           DrawSimpleWhiteLine(drawto, from_sx, from_sy, to_sx,   from_sy);
14015           DrawSimpleWhiteLine(drawto, to_sx,   from_sy, to_sx,   to_sy);
14016           DrawSimpleWhiteLine(drawto, to_sx,   to_sy,   from_sx, to_sy);
14017           DrawSimpleWhiteLine(drawto, from_sx, to_sy,   from_sx, from_sy);
14018         }
14019       }
14020       else
14021       {
14022         int sx = SX + x * ed_tilesize;
14023         int sy = SY + y * ed_tilesize;
14024         int from_sx = sx;
14025         int from_sy = sy;
14026         int to_sx = sx + ed_tilesize - 1;
14027         int to_sy = sy + ed_tilesize - 1;
14028
14029         DrawSimpleWhiteLine(drawto, from_sx, from_sy, to_sx,   from_sy);
14030         DrawSimpleWhiteLine(drawto, to_sx,   from_sy, to_sx,   to_sy);
14031         DrawSimpleWhiteLine(drawto, to_sx,   to_sy,   from_sx, to_sy);
14032         DrawSimpleWhiteLine(drawto, from_sx, to_sy,   from_sx, from_sy);
14033       }
14034     }
14035   }
14036 }
14037
14038 static void CopyLevelTemplateToUserLevelSet(char *levelset_subdir)
14039 {
14040   char *template_filename_old = getLocalLevelTemplateFilename();
14041   char *template_filename_new =
14042     getPath2(getUserLevelDir(levelset_subdir), LEVELTEMPLATE_FILENAME);
14043
14044   if (copyFile(template_filename_old, template_filename_new) != 0)
14045     Request("Cannot copy level template!", REQ_CONFIRM);
14046
14047   free(template_filename_new);
14048 }
14049
14050 static void HandleDrawingAreas(struct GadgetInfo *gi)
14051 {
14052   static boolean started_inside_drawing_area = FALSE;
14053   static int last_sx = -1;
14054   static int last_sy = -1;
14055   static int last_sx2 = -1;
14056   static int last_sy2 = -1;
14057   int id = gi->custom_id;
14058   int type_id = gi->custom_type_id;
14059   boolean button_press_event;
14060   boolean button_release_event;
14061   boolean inside_drawing_area = !gi->event.off_borders;
14062   boolean draw_level = (id == GADGET_ID_DRAWING_LEVEL);
14063   int actual_drawing_function;
14064   int button = gi->event.button;
14065   int new_element = BUTTON_ELEMENT(button);
14066   int sx = gi->event.x, sy = gi->event.y;
14067   int min_sx = 0, min_sy = 0;
14068   int max_sx = gi->drawing.area_xsize - 1, max_sy = gi->drawing.area_ysize - 1;
14069   int item_xsize = gi->drawing.item_xsize, item_ysize = gi->drawing.item_ysize;
14070   int mini_item_xsize = item_xsize / 2, mini_item_ysize = item_ysize / 2;
14071   int sx2 = gi->event.mx / mini_item_xsize;
14072   int sy2 = gi->event.my / mini_item_ysize;
14073   int dx = sx2 % 2;
14074   int dy = sy2 % 2;
14075   int lx = 0, ly = 0;
14076   int x, y;
14077
14078   button_press_event = (gi->event.type == GD_EVENT_PRESSED);
14079   button_release_event = (gi->event.type == GD_EVENT_RELEASED);
14080
14081   // make sure to stay inside drawing area boundaries
14082   sx = (sx < min_sx ? min_sx : sx > max_sx ? max_sx : sx);
14083   sy = (sy < min_sy ? min_sy : sy > max_sy ? max_sy : sy);
14084
14085   if (draw_level)
14086   {
14087     int min_lx = 0, min_ly = 0;
14088     int max_lx = lev_fieldx - 1, max_ly = lev_fieldy - 1;
14089
14090     // get positions inside level field
14091     lx = sx + level_xpos;
14092     ly = sy + level_ypos;
14093
14094     if (!IN_LEV_FIELD(lx, ly))
14095       inside_drawing_area = FALSE;
14096
14097     // make sure to stay inside level field boundaries
14098     lx = (lx < min_lx ? min_lx : lx > max_lx ? max_lx : lx);
14099     ly = (ly < min_ly ? min_ly : ly > max_ly ? max_ly : ly);
14100
14101     // correct drawing area positions accordingly
14102     sx = lx - level_xpos;
14103     sy = ly - level_ypos;
14104   }
14105
14106   // also correct MM wall-sized (double) drawing area positions accordingly
14107   if (sx2 / 2 < sx || sx2 / 2 > sx)
14108   {
14109     dx = (sx2 / 2 < sx ? 0 : 1);
14110     sx2 = sx * 2 + dx;
14111   }
14112   if (sy2 / 2 < sy || sy2 / 2 > sy)
14113   {
14114     dy = (sy2 / 2 < sy ? 0 : 1);
14115     sy2 = sy * 2 + dy;
14116   }
14117
14118   if (!button_press_event && !button_release_event)
14119   {
14120     int old_element = (IN_LEV_FIELD(lx, ly) ? Tile[lx][ly] : EL_UNDEFINED);
14121     boolean hires_drawing = (level.game_engine_type == GAME_ENGINE_TYPE_MM &&
14122                              isHiresTileElement(old_element) &&
14123                              isHiresDrawElement(new_element));
14124
14125     // prevent handling events for every pixel position when moving mouse
14126     if ((sx == last_sx && sy == last_sy && !hires_drawing) ||
14127         (sx2 == last_sx2 && sy2 == last_sy2))
14128       return;
14129   }
14130
14131   last_sx = sx;
14132   last_sy = sy;
14133   last_sx2 = sx2;
14134   last_sy2 = sy2;
14135
14136   if (button_press_event)
14137     started_inside_drawing_area = inside_drawing_area;
14138
14139   if (!started_inside_drawing_area)
14140     return;
14141
14142   if (!IS_VALID_BUTTON(button))
14143     return;
14144
14145   // handle info callback for each invocation of action callback
14146   gi->callback_info(gi);
14147
14148   // automatically switch to 'single item' drawing mode, if needed
14149   actual_drawing_function =
14150     (draw_level || drawing_function == GADGET_ID_PICK_ELEMENT ?
14151      drawing_function : GADGET_ID_SINGLE_ITEMS);
14152
14153   // clicking into drawing area with pressed Control key picks element
14154   if (GetKeyModState() & KMOD_Control)
14155   {
14156     last_drawing_function = drawing_function;
14157     actual_drawing_function = GADGET_ID_PICK_ELEMENT;
14158   }
14159
14160   if (GetKeyModState() & KMOD_Shift)
14161   {
14162     if (button_press_event || button_release_event)
14163       ResetIntelliDraw();
14164   }
14165
14166   SetDrawModeHiRes(-1);         // reset to normal draw mode
14167
14168   switch (actual_drawing_function)
14169   {
14170     case GADGET_ID_SINGLE_ITEMS:
14171       if (draw_level)
14172       {
14173         if (button_release_event)
14174         {
14175           CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14176
14177           if (edit_mode == ED_MODE_DRAWING && draw_with_brush &&
14178               !inside_drawing_area)
14179             DeleteBrushFromCursor();
14180
14181           break;
14182         }
14183
14184         if (draw_with_brush)
14185         {
14186           CopyBrushToLevel(sx, sy, button);
14187         }
14188         else
14189         {
14190           SetDrawModeHiRes(new_element);
14191
14192           if (IS_PLAYER_ELEMENT(new_element) || IS_MM_MCDUFFIN(new_element))
14193           {
14194             // remove player at old position
14195             for (y = 0; y < lev_fieldy; y++)
14196             {
14197               for (x = 0; x < lev_fieldx; x++)
14198               {
14199                 int old_element = Tile[x][y];
14200
14201                 if (IS_PLAYER_ELEMENT(old_element) &&
14202                     IS_PLAYER_ELEMENT(new_element))
14203                 {
14204                   int replaced_with_element =
14205                     (old_element == EL_SOKOBAN_FIELD_PLAYER &&
14206                      new_element == EL_PLAYER_1 ? EL_SOKOBAN_FIELD_EMPTY :
14207
14208                      old_element == EL_SOKOBAN_FIELD_PLAYER &&
14209                      new_element == old_element ? EL_SOKOBAN_FIELD_EMPTY :
14210
14211                      new_element == EL_SOKOBAN_FIELD_PLAYER &&
14212                      old_element == EL_PLAYER_1 ? EL_EMPTY :
14213
14214                      new_element >= EL_PLAYER_1 &&
14215                      new_element <= EL_PLAYER_4 &&
14216                      new_element == old_element ? EL_EMPTY :
14217
14218                      old_element);
14219
14220                   SetElement(x, y, replaced_with_element);
14221                 }
14222                 else if (IS_MM_MCDUFFIN(old_element) &&
14223                          IS_MM_MCDUFFIN(new_element))
14224                 {
14225                   // remove McDuffin at old position
14226                   SetElement(x, y, EL_EMPTY);
14227                 }
14228               }
14229             }
14230           }
14231
14232           SetElementButton(lx, ly, dx, dy, new_element, button);
14233         }
14234       }
14235       else if (!button_release_event)
14236       {
14237         int pos = sx * drawingarea_info[type_id].area_ysize + sy;
14238
14239         if (item_xsize == MINI_TILEX && item_ysize == MINI_TILEY)
14240           DrawMiniGraphicExt(drawto,
14241                              gi->x + sx * MINI_TILEX,
14242                              gi->y + sy * MINI_TILEY,
14243                              el2edimg(new_element));
14244         else
14245           DrawFixedGraphicExt(drawto,
14246                               gi->x + sx * TILEX,
14247                               gi->y + sy * TILEY,
14248                               el2edimg(new_element), 0);
14249
14250         if (id == GADGET_ID_CUSTOM_GRAPHIC)
14251           new_element = GFX_ELEMENT(new_element);
14252
14253         drawingarea_info[type_id].value[pos] = new_element;
14254
14255         CopyElementPropertiesToGame(properties_element);
14256
14257         if (id == GADGET_ID_CUSTOM_GRAPHIC)
14258         {
14259           UpdateCustomElementGraphicGadgets();
14260
14261           FrameCounter = 0;     // restart animation frame counter
14262         }
14263       }
14264       break;
14265
14266     case GADGET_ID_CONNECTED_ITEMS:
14267       {
14268         static int last_sx = -1;
14269         static int last_sy = -1;
14270
14271         if (button_release_event)
14272           CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14273
14274         SetDrawModeHiRes(new_element);
14275
14276         if (getDrawModeHiRes())
14277         {
14278           sx = sx2;
14279           sy = sy2;
14280         }
14281
14282         if (!button_press_event)
14283           DrawLine(last_sx, last_sy, sx, sy, new_element, TRUE);
14284
14285         last_sx = sx;
14286         last_sy = sy;
14287       }
14288       break;
14289
14290     case GADGET_ID_LINE:
14291     case GADGET_ID_ARC:
14292     case GADGET_ID_RECTANGLE:
14293     case GADGET_ID_FILLED_BOX:
14294       SetDrawModeHiRes(new_element);
14295
14296       if (getDrawModeHiRes())
14297       {
14298         sx = sx2;
14299         sy = sy2;
14300       }
14301       // FALLTHROUGH
14302     case GADGET_ID_GRAB_BRUSH:
14303     case GADGET_ID_TEXT:
14304       {
14305         static int last_sx = -1;
14306         static int last_sy = -1;
14307         static int start_sx = -1;
14308         static int start_sy = -1;
14309         void (*draw_func)(int, int, int, int, int, boolean);
14310
14311         if (drawing_function == GADGET_ID_LINE)
14312           draw_func = DrawLine;
14313         else if (drawing_function == GADGET_ID_ARC)
14314           draw_func = DrawArc;
14315         else if (drawing_function == GADGET_ID_RECTANGLE)
14316           draw_func = DrawBox;
14317         else if (drawing_function == GADGET_ID_FILLED_BOX)
14318           draw_func = DrawFilledBox;
14319         else if (drawing_function == GADGET_ID_GRAB_BRUSH)
14320           draw_func = SelectArea;
14321         else // (drawing_function == GADGET_ID_TEXT)
14322           draw_func = SetTextCursor;
14323
14324         if (button_press_event)
14325         {
14326           draw_func(sx, sy, sx, sy, new_element, FALSE);
14327           start_sx = last_sx = sx;
14328           start_sy = last_sy = sy;
14329
14330           if (drawing_function == GADGET_ID_TEXT)
14331             DrawLevelText(0, 0, 0, TEXT_END);
14332         }
14333         else if (button_release_event)
14334         {
14335           draw_func(start_sx, start_sy, sx, sy, new_element, TRUE);
14336           if (drawing_function == GADGET_ID_GRAB_BRUSH)
14337           {
14338             CopyAreaToBrush(start_sx, start_sy, sx, sy, button);
14339             CopyBrushToCursor(sx, sy);
14340             ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS],
14341                           MB_LEFTBUTTON);
14342             draw_with_brush = TRUE;
14343           }
14344           else if (drawing_function == GADGET_ID_TEXT)
14345             DrawLevelText(sx, sy, 0, TEXT_INIT);
14346           else
14347             CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14348         }
14349         else if (last_sx != sx || last_sy != sy)
14350         {
14351           draw_func(start_sx, start_sy, last_sx, last_sy, -1, FALSE);
14352           if (IS_MM_WALL_EDITOR(new_element))   // clear wall background
14353             draw_func(start_sx, start_sy, sx, sy, EL_EMPTY, FALSE);
14354           draw_func(start_sx, start_sy, sx, sy, new_element, FALSE);
14355           last_sx = sx;
14356           last_sy = sy;
14357         }
14358       }
14359       break;
14360
14361     case GADGET_ID_FLOOD_FILL:
14362       if (button_press_event && Tile[lx][ly] != new_element)
14363       {
14364         if (IS_MM_WALL_EDITOR(new_element))
14365           FloodFillWall_MM(sx2, sy2, new_element);
14366         else
14367           FloodFill(lx, ly, new_element);
14368
14369         DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14370         CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14371       }
14372       break;
14373
14374     case GADGET_ID_PICK_ELEMENT:
14375       if (button_release_event)
14376         ClickOnGadget(level_editor_gadget[last_drawing_function],
14377                       MB_LEFTBUTTON);
14378       else if (draw_level)
14379         PickDrawingElement(button, Tile[lx][ly]);
14380       else
14381       {
14382         int pos = sx * drawingarea_info[type_id].area_ysize + sy;
14383
14384         PickDrawingElement(button, drawingarea_info[type_id].value[pos]);
14385       }
14386
14387     default:
14388       break;
14389   }
14390
14391   // do not mark level as modified for certain non-level-changing gadgets
14392   if ((type_id >= ED_DRAWING_ID_EDITOR_FIRST &&
14393        type_id <= ED_DRAWING_ID_EDITOR_LAST) ||
14394       actual_drawing_function == GADGET_ID_GRAB_BRUSH ||
14395       actual_drawing_function == GADGET_ID_PICK_ELEMENT)
14396     return;
14397
14398   level.changed = TRUE;
14399 }
14400
14401 static void HandleCounterButtons(struct GadgetInfo *gi)
14402 {
14403   int gadget_id = gi->custom_id;
14404   int counter_id = gi->custom_type_id;
14405   int button = gi->event.button;
14406   int *counter_value = counterbutton_info[counter_id].value;
14407   int step = BUTTON_STEPSIZE(button) *
14408     (gadget_id == counterbutton_info[counter_id].gadget_id_down ? -1 : +1);
14409
14410   if (counter_id == ED_COUNTER_ID_SELECT_LEVEL)
14411   {
14412     boolean pressed = (gi->event.type == GD_EVENT_PRESSED);
14413     boolean released = (gi->event.type == GD_EVENT_RELEASED);
14414     boolean level_changed = LevelChanged();
14415
14416     if ((level_changed && pressed) || (!level_changed && released))
14417       return;
14418
14419     if (level_changed && !Request("Level has changed! Discard changes?",
14420                                   REQ_ASK))
14421     {
14422       if (gadget_id == counterbutton_info[counter_id].gadget_id_text)
14423         ModifyEditorCounterValue(counter_id, *counter_value);
14424
14425       return;
14426     }
14427   }
14428
14429   if (gadget_id == counterbutton_info[counter_id].gadget_id_text)
14430     *counter_value = gi->textinput.number_value;
14431   else
14432     ModifyEditorCounterValue(counter_id, *counter_value + step);
14433
14434   if (counter_id == ED_COUNTER_ID_SELECT_LEVEL)
14435   {
14436     int last_game_engine_type = level.game_engine_type;
14437
14438     LoadLevel(level_nr);
14439     LoadScore(level_nr);
14440
14441     SaveLevelSetup_SeriesInfo();
14442
14443     TapeErase();
14444
14445     ResetUndoBuffer();
14446     DrawEditModeWindow();
14447
14448     if (level.game_engine_type != last_game_engine_type)
14449     {
14450       // update element selection list
14451       ReinitializeElementList();
14452       ModifyEditorElementList();
14453     }
14454
14455     return;
14456   }
14457
14458   switch (counter_id)
14459   {
14460     case ED_COUNTER_ID_YAMYAM_CONTENT:
14461       DrawYamYamContentAreas();
14462       break;
14463
14464     case ED_COUNTER_ID_BALL_CONTENT:
14465       DrawMagicBallContentAreas();
14466       break;
14467
14468     case ED_COUNTER_ID_ANDROID_CONTENT:
14469       DrawAndroidElementArea();
14470       break;
14471
14472     case ED_COUNTER_ID_GROUP_CONTENT:
14473       DrawGroupElementArea();
14474       CopyGroupElementPropertiesToGame(properties_element);
14475       break;
14476
14477     case ED_COUNTER_ID_INVENTORY_SIZE:
14478       DrawPlayerInitialInventoryArea(properties_element);
14479       break;
14480
14481     case ED_COUNTER_ID_MM_BALL_CONTENT:
14482       DrawMMBallContentArea();
14483       break;
14484
14485     case ED_COUNTER_ID_ENVELOPE_XSIZE:
14486     case ED_COUNTER_ID_ENVELOPE_YSIZE:
14487       DrawEnvelopeTextArea(-1);
14488       break;
14489
14490     case ED_COUNTER_ID_LEVEL_XSIZE:
14491     case ED_COUNTER_ID_LEVEL_YSIZE:
14492       lev_fieldx = level.fieldx;
14493       lev_fieldy = level.fieldy;
14494
14495       // check if resizing of level results in change of border border
14496       SetBorderElement();
14497
14498       break;
14499
14500     default:
14501       break;
14502   }
14503
14504   if ((counter_id >= ED_COUNTER_ID_CUSTOM_FIRST &&
14505        counter_id <= ED_COUNTER_ID_CUSTOM_LAST) ||
14506       (counter_id >= ED_COUNTER_ID_CHANGE_FIRST &&
14507        counter_id <= ED_COUNTER_ID_CHANGE_LAST))
14508     CopyElementPropertiesToGame(properties_element);
14509
14510   // do not mark level as modified for certain non-level-changing gadgets
14511   if ((counter_id >= ED_COUNTER_ID_LEVELSET_FIRST &&
14512        counter_id <= ED_COUNTER_ID_LEVELSET_LAST) ||
14513       (counter_id >= ED_COUNTER_ID_EDITOR_FIRST &&
14514        counter_id <= ED_COUNTER_ID_EDITOR_LAST))
14515     return;
14516
14517   level.changed = TRUE;
14518 }
14519
14520 static void HandleTextInputGadgets(struct GadgetInfo *gi)
14521 {
14522   int type_id = gi->custom_type_id;
14523
14524   strcpy(textinput_info[type_id].value, gi->textinput.value);
14525
14526   if (type_id == ED_TEXTINPUT_ID_ELEMENT_NAME)
14527   {
14528     CopyElementPropertiesToGame(properties_element);
14529
14530     ModifyEditorElementList();  // update changed button info text
14531   }
14532
14533   // do not mark level as modified for certain non-level-changing gadgets
14534   if (type_id >= ED_TEXTINPUT_ID_LEVELSET_FIRST &&
14535       type_id <= ED_TEXTINPUT_ID_LEVELSET_LAST)
14536     return;
14537
14538   level.changed = TRUE;
14539 }
14540
14541 static void HandleTextAreaGadgets(struct GadgetInfo *gi)
14542 {
14543   int type_id = gi->custom_type_id;
14544
14545   strncpy(textarea_info[type_id].value, gi->textarea.value,
14546           MAX_ENVELOPE_TEXT_LEN);
14547   textarea_info[type_id].value[MAX_ENVELOPE_TEXT_LEN] = '\0';
14548
14549   level.changed = TRUE;
14550 }
14551
14552 static void HandleSelectboxGadgets(struct GadgetInfo *gi)
14553 {
14554   int type_id = gi->custom_type_id;
14555   int value_old = *selectbox_info[type_id].value;
14556   int value_new = selectbox_info[type_id].options[gi->selectbox.index].value;
14557
14558   *selectbox_info[type_id].value = value_new;
14559
14560   if (type_id == ED_SELECTBOX_ID_LEVELSET_SAVE_MODE)
14561   {
14562     DrawLevelConfigWindow();
14563   }
14564   else if (type_id == ED_SELECTBOX_ID_SELECT_CHANGE_PAGE)
14565   {
14566     element_info[properties_element].current_change_page = gi->selectbox.index;
14567
14568     DrawPropertiesWindow();
14569   }
14570   else if ((type_id >= ED_SELECTBOX_ID_CUSTOM_FIRST &&
14571             type_id <= ED_SELECTBOX_ID_CUSTOM_LAST) ||
14572            (type_id >= ED_SELECTBOX_ID_CHANGE_FIRST &&
14573             type_id <= ED_SELECTBOX_ID_CHANGE_LAST) ||
14574            (type_id == ED_SELECTBOX_ID_GROUP_CHOICE_MODE))
14575   {
14576     if (type_id == ED_SELECTBOX_ID_ACTION_TYPE)
14577     {
14578       // when changing action type, also check action mode and action arg
14579       if (value_old != value_new)
14580         setSelectboxSpecialActionVariablesIfNeeded();
14581
14582       DrawPropertiesChange();
14583     }
14584
14585     CopyElementPropertiesToGame(properties_element);
14586   }
14587   else if (type_id == ED_SELECTBOX_ID_GAME_ENGINE_TYPE)
14588   {
14589     // show or hide "engine" tabulator depending on game engine type
14590     DrawLevelConfigWindow();
14591
14592     // update element selection list depending on game engine type
14593     ReinitializeElementList();
14594     ModifyEditorElementList();
14595   }
14596   else if (type_id == ED_SELECTBOX_ID_BD_SCHEDULING_TYPE)
14597   {
14598     // update BD cycle delay counter gadgets depending on BD scheduling type
14599     DrawLevelConfigWindow();
14600   }
14601
14602   // do not mark level as modified for certain non-level-changing gadgets
14603   if (type_id == ED_SELECTBOX_ID_LEVELSET_SAVE_MODE ||
14604       type_id == ED_SELECTBOX_ID_SELECT_CHANGE_PAGE)
14605     return;
14606
14607   level.changed = TRUE;
14608 }
14609
14610 static void HandleTextbuttonGadgets(struct GadgetInfo *gi)
14611 {
14612   int type_id = gi->custom_type_id;
14613   int i;
14614
14615   if (type_id >= ED_TAB_BUTTON_ID_LEVELCONFIG_FIRST &&
14616       type_id <= ED_TAB_BUTTON_ID_LEVELCONFIG_LAST)
14617   {
14618     edit_mode_levelconfig = gi->custom_type_id;
14619
14620     DrawLevelConfigWindow();
14621   }
14622   else if (type_id >= ED_TAB_BUTTON_ID_PROPERTIES_FIRST &&
14623            type_id <= ED_TAB_BUTTON_ID_PROPERTIES_LAST)
14624   {
14625     edit_mode_properties = gi->custom_type_id;
14626
14627     DrawPropertiesWindow();
14628   }
14629   else if (type_id == ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1 ||
14630            type_id == ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2)
14631   {
14632     boolean new_template = !fileExists(getLocalLevelTemplateFilename());
14633
14634     // backup original "level.field" (needed to track playfield changes)
14635     CopyPlayfield(level.field, TileBackup);
14636
14637     // "SaveLevelTemplate()" uses "level.field", so copy editor playfield
14638     CopyPlayfield(Tile, level.field);
14639
14640     if (new_template ||
14641         Request("Save this template and kill the old?", REQ_ASK))
14642       SaveLevelTemplate();
14643
14644     if (new_template)
14645       Request("Template saved!", REQ_CONFIRM);
14646
14647     // restore original "level.field" (needed to track playfield changes)
14648     CopyPlayfield(TileBackup, level.field);
14649   }
14650   else if (type_id == ED_TEXTBUTTON_ID_SAVE_LEVELSET)
14651   {
14652     char *levelset_subdir = getLevelSubdirFromSaveMode(levelset_save_mode);
14653
14654     if (levelset_save_mode == LEVELSET_SAVE_MODE_UPDATE &&
14655         leveldir_current->readonly)
14656     {
14657       Request("This level set is read-only!", REQ_CONFIRM);
14658
14659       return;
14660     }
14661
14662     if (strEqual(levelset_name, ""))
14663     {
14664       Request("Please enter level set title!", REQ_CONFIRM);
14665
14666       return;
14667     }
14668
14669     if (strEqual(levelset_author, ""))
14670     {
14671       Request("Please enter level set author!", REQ_CONFIRM);
14672
14673       return;
14674     }
14675
14676     if (levelset_save_mode == LEVELSET_SAVE_MODE_UPDATE)
14677     {
14678       if (UpdateUserLevelSet(levelset_subdir,
14679                              levelset_name,
14680                              levelset_author,
14681                              levelset_num_levels))
14682       {
14683         Request("Level set updated!", REQ_CONFIRM);
14684       }
14685       else
14686       {
14687         Request("Updating level set failed!", REQ_CONFIRM);
14688       }
14689     }
14690     else if (levelset_save_mode == LEVELSET_SAVE_MODE_CREATE)
14691     {
14692       if (level.changed && !Request("Level has changed! Discard changes?",
14693                                      REQ_ASK))
14694         return;
14695
14696       if (CreateUserLevelSet(levelset_subdir,
14697                              levelset_name,
14698                              levelset_author,
14699                              levelset_num_levels,
14700                              levelset_use_levelset_artwork))
14701       {
14702         if (levelset_copy_level_template)
14703           CopyLevelTemplateToUserLevelSet(levelset_subdir);
14704
14705         Request("New level set created!", REQ_CONFIRM);
14706
14707         AddUserLevelSetToLevelInfo(levelset_subdir);
14708         ChangeEditorToLevelSet(levelset_subdir);
14709       }
14710       else
14711       {
14712         Request("Creating new level set failed!", REQ_CONFIRM);
14713       }
14714     }
14715   }
14716   else if (type_id == ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE &&
14717            custom_element.num_change_pages < MAX_CHANGE_PAGES)
14718   {
14719     struct ElementInfo *ei = &element_info[properties_element];
14720
14721     // when modifying custom element, ask for copying level template
14722     if (level.use_custom_template && !AskToCopyAndModifyLevelTemplate())
14723       return;
14724
14725     setElementChangePages(ei, ei->num_change_pages + 1);
14726
14727     // set new change page to be new current change page
14728     ei->current_change_page = ei->num_change_pages - 1;
14729     ei->change = &ei->change_page[ei->current_change_page];
14730
14731     setElementChangeInfoToDefaults(ei->change);
14732
14733     DrawPropertiesWindow();
14734
14735     level.changed = TRUE;
14736   }
14737   else if (type_id == ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE &&
14738            custom_element.num_change_pages > MIN_CHANGE_PAGES)
14739   {
14740     struct ElementInfo *ei = &element_info[properties_element];
14741
14742     // when modifying custom element, ask for copying level template
14743     if (level.use_custom_template && !AskToCopyAndModifyLevelTemplate())
14744       return;
14745
14746     // copy all change pages after change page to be deleted
14747     for (i = ei->current_change_page; i < ei->num_change_pages - 1; i++)
14748       ei->change_page[i] = ei->change_page[i + 1];
14749
14750     setElementChangePages(ei, ei->num_change_pages - 1);
14751
14752     DrawPropertiesWindow();
14753
14754     level.changed = TRUE;
14755   }
14756 }
14757
14758 static void HandleGraphicbuttonGadgets(struct GadgetInfo *gi)
14759 {
14760   int type_id = gi->custom_type_id;
14761
14762   if (type_id == ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE ||
14763       type_id == ED_GRAPHICBUTTON_ID_NEXT_CHANGE_PAGE)
14764   {
14765     struct ElementInfo *ei = &element_info[properties_element];
14766     int step = BUTTON_STEPSIZE(gi->event.button);
14767
14768     step *= (type_id == ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE ? -1 : +1);
14769     ei->current_change_page += step;
14770
14771     if (ei->current_change_page < 0)
14772       ei->current_change_page = 0;
14773     else if (ei->current_change_page >= ei->num_change_pages)
14774       ei->current_change_page = ei->num_change_pages - 1;
14775
14776     DrawPropertiesWindow();
14777   }
14778   else if (type_id == ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE ||
14779            type_id == ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE)
14780   {
14781     struct ElementInfo *ei = &element_info[properties_element];
14782     int current_change_page = ei->current_change_page;
14783
14784     if (type_id == ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE)
14785     {
14786       element_info[EL_INTERNAL_CLIPBOARD_CHANGE].change_page[0] =
14787         ei->change_page[current_change_page];
14788     }
14789     else if (type_id == ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE)
14790     {
14791       // when modifying custom element, ask for copying level template
14792       if (level.use_custom_template && !AskToCopyAndModifyLevelTemplate())
14793         return;
14794
14795       ei->change_page[current_change_page] =
14796         element_info[EL_INTERNAL_CLIPBOARD_CHANGE].change_page[0];
14797
14798       level.changed = TRUE;
14799     }
14800
14801     DrawPropertiesWindow();
14802   }
14803 }
14804
14805 static void HandleRadiobuttons(struct GadgetInfo *gi)
14806 {
14807   int type_id = gi->custom_type_id;
14808
14809   *radiobutton_info[type_id].value =
14810     radiobutton_info[type_id].checked_value;
14811
14812   // do not mark level as modified for certain non-level-changing gadgets
14813   if (type_id >= ED_RADIOBUTTON_ID_EDITOR_FIRST &&
14814       type_id <= ED_RADIOBUTTON_ID_EDITOR_LAST)
14815     return;
14816
14817   level.changed = TRUE;
14818 }
14819
14820 static void HandleCheckbuttons(struct GadgetInfo *gi)
14821 {
14822   int type_id = gi->custom_type_id;
14823
14824   *checkbutton_info[type_id].value ^= TRUE;
14825
14826   if (type_id == ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID ||
14827       type_id == ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID ||
14828       type_id == ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH ||
14829       (((type_id >= ED_CHECKBUTTON_ID_CUSTOM_FIRST &&
14830          type_id <= ED_CHECKBUTTON_ID_CUSTOM_LAST) ||
14831         (type_id >= ED_CHECKBUTTON_ID_CHANGE_FIRST &&
14832          type_id <= ED_CHECKBUTTON_ID_CHANGE_LAST)) &&
14833        type_id != ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1))
14834   {
14835     CopyElementPropertiesToGame(properties_element);
14836   }
14837
14838   if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC)
14839   {
14840     UpdateCustomElementGraphicGadgets();
14841   }
14842   else if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1 ||
14843            type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2)
14844   {
14845     boolean template_related_changes_found = FALSE;
14846     int i;
14847
14848     // check if any custom, group or empty elements have been changed
14849     for (i = 0; i < NUM_FILE_ELEMENTS; i++)
14850       if ((IS_CUSTOM_ELEMENT(i) ||
14851            IS_GROUP_ELEMENT(i) ||
14852            IS_EMPTY_ELEMENT(i)) &&
14853           element_info[i].modified_settings)
14854         template_related_changes_found = TRUE;
14855
14856     if (level.use_custom_template &&
14857         !fileExists(getGlobalLevelTemplateFilename()))
14858     {
14859       Request("No level template found!", REQ_CONFIRM);
14860
14861       level.use_custom_template = FALSE;
14862
14863       ModifyGadget(gi, GDI_CHECKED, FALSE, GDI_END);
14864
14865       return;
14866     }
14867
14868     if (level.use_custom_template &&
14869         template_related_changes_found &&
14870         !Request("Discard changes and use level template?", REQ_ASK))
14871     {
14872       level.use_custom_template = FALSE;
14873
14874       ModifyGadget(gi, GDI_CHECKED, FALSE, GDI_END);
14875
14876       return;
14877     }
14878
14879     if (!level.use_custom_template &&
14880         Request("Copy settings from level template?", REQ_ASK))
14881     {
14882       return;
14883     }
14884
14885     LoadLevelTemplate(level.use_custom_template ? -1 : level_nr);
14886
14887     DrawEditModeWindow();
14888   }
14889   else if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_3)
14890   {
14891     if (setup.editor.use_template_for_new_levels &&
14892         !fileExists(getGlobalLevelTemplateFilename()))
14893     {
14894       Request("No level template found!", REQ_CONFIRM);
14895
14896       return;
14897     }
14898
14899     if (setup.editor.use_template_for_new_levels &&
14900         level.changed &&
14901         !Request("Discard level and load template?", REQ_ASK))
14902     {
14903       return;
14904     }
14905
14906     if (!setup.editor.use_template_for_new_levels &&
14907         level.changed &&
14908         !Request("Discard level and use empty level?", REQ_ASK))
14909     {
14910       return;
14911     }
14912
14913     LoadLevel(level_nr);
14914     LoadScore(level_nr);
14915
14916     TapeErase();
14917
14918     ResetUndoBuffer();
14919     DrawEditModeWindow();
14920   }
14921   else if (type_id == ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS)
14922   {
14923     SetAutomaticNumberOfGemsNeeded();
14924   }
14925
14926   // do not mark level as modified for certain non-level-changing gadgets
14927   if ((type_id >= ED_CHECKBUTTON_ID_LEVELSET_FIRST &&
14928        type_id <= ED_CHECKBUTTON_ID_LEVELSET_LAST) ||
14929       (type_id >= ED_CHECKBUTTON_ID_EDITOR_FIRST &&
14930        type_id <= ED_CHECKBUTTON_ID_EDITOR_LAST &&
14931        type_id != ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2) ||
14932       type_id == ED_CHECKBUTTON_ID_STICK_ELEMENT)
14933     return;
14934
14935   level.changed = TRUE;
14936 }
14937
14938 static void HandleControlButtons(struct GadgetInfo *gi)
14939 {
14940   static int last_level_drawing_function = GADGET_ID_SINGLE_ITEMS;
14941   static int last_edit_mode = ED_MODE_DRAWING;
14942   static int last_custom_copy_mode = -1;
14943   static int last_button = 0;
14944   int id = gi->custom_id;
14945   int button = gi->event.button;
14946   int step = BUTTON_STEPSIZE(button);
14947   int new_element = BUTTON_ELEMENT(button);
14948   int last_properties_element = properties_element;
14949   int x, y;
14950
14951   if (edit_mode == ED_MODE_DRAWING && drawing_function == GADGET_ID_TEXT)
14952     DrawLevelText(0, 0, 0, TEXT_END);
14953
14954   if (id < ED_NUM_CTRL1_BUTTONS &&
14955       id != GADGET_ID_SINGLE_ITEMS &&
14956       id != GADGET_ID_PICK_ELEMENT &&
14957       edit_mode != ED_MODE_DRAWING &&
14958       drawing_function != GADGET_ID_PICK_ELEMENT &&
14959       !(GetKeyModState() & KMOD_Control))
14960     ChangeEditModeWindow(ED_MODE_DRAWING);
14961
14962   // element copy mode active, but no element button pressed => deactivate
14963   if (last_custom_copy_mode != -1 && id < ED_NUM_CTRL_BUTTONS)
14964     last_custom_copy_mode = -1;
14965
14966   // when showing palette on element buttons, change element of button used
14967   if (editor.palette.show_on_element_buttons &&
14968       id >= GADGET_ID_ELEMENT_LEFT && id <= GADGET_ID_ELEMENT_RIGHT)
14969   {
14970     last_button = id - GADGET_ID_ELEMENT_LEFT + 1;
14971
14972     id = GADGET_ID_PALETTE;
14973   }
14974
14975   switch (id)
14976   {
14977     case GADGET_ID_SCROLL_LEFT:
14978       if (level_xpos >= 0)
14979       {
14980         if (lev_fieldx < ed_fieldx - 2)
14981           break;
14982
14983         level_xpos -= step;
14984         if (level_xpos < -1)
14985           level_xpos = -1;
14986         if (button == 1)
14987           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_RIGHT);
14988         else
14989           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14990
14991         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
14992                      GDI_SCROLLBAR_ITEM_POSITION, level_xpos + 1, GDI_END);
14993       }
14994       break;
14995
14996     case GADGET_ID_SCROLL_RIGHT:
14997       if (level_xpos <= lev_fieldx - ed_fieldx)
14998       {
14999         if (lev_fieldx < ed_fieldx - 2)
15000           break;
15001
15002         level_xpos += step;
15003         if (level_xpos > lev_fieldx - ed_fieldx + 1)
15004           level_xpos = lev_fieldx - ed_fieldx + 1;
15005         if (button == 1)
15006           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_LEFT);
15007         else
15008           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15009
15010         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
15011                      GDI_SCROLLBAR_ITEM_POSITION, level_xpos + 1, GDI_END);
15012       }
15013       break;
15014
15015     case GADGET_ID_SCROLL_UP:
15016       if (level_ypos >= 0)
15017       {
15018         if (lev_fieldy < ed_fieldy - 2)
15019           break;
15020
15021         level_ypos -= step;
15022         if (level_ypos < -1)
15023           level_ypos = -1;
15024         if (button == 1)
15025           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_DOWN);
15026         else
15027           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15028
15029         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
15030                      GDI_SCROLLBAR_ITEM_POSITION, level_ypos + 1, GDI_END);
15031       }
15032       break;
15033
15034     case GADGET_ID_SCROLL_DOWN:
15035       if (level_ypos <= lev_fieldy - ed_fieldy)
15036       {
15037         if (lev_fieldy < ed_fieldy - 2)
15038           break;
15039
15040         level_ypos += step;
15041         if (level_ypos > lev_fieldy - ed_fieldy + 1)
15042           level_ypos = lev_fieldy - ed_fieldy + 1;
15043         if (button == 1)
15044           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_UP);
15045         else
15046           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15047
15048         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
15049                      GDI_SCROLLBAR_ITEM_POSITION, level_ypos + 1, GDI_END);
15050       }
15051       break;
15052
15053     case GADGET_ID_SCROLL_HORIZONTAL:
15054       level_xpos = gi->event.item_position - 1;
15055
15056       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15057       BackToFront();
15058
15059       break;
15060
15061     case GADGET_ID_SCROLL_VERTICAL:
15062       level_ypos = gi->event.item_position - 1;
15063
15064       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15065       BackToFront();
15066
15067       break;
15068
15069     case GADGET_ID_SCROLL_LIST_UP:
15070     case GADGET_ID_SCROLL_LIST_DOWN:
15071     case GADGET_ID_SCROLL_LIST_VERTICAL:
15072       if (id == GADGET_ID_SCROLL_LIST_VERTICAL)
15073         element_shift = gi->event.item_position * ED_ELEMENTLIST_BUTTONS_HORIZ;
15074       else
15075       {
15076         step *= (id == GADGET_ID_SCROLL_LIST_UP ? -1 : +1);
15077         element_shift += step * ED_ELEMENTLIST_BUTTONS_HORIZ;
15078
15079         if (element_shift < 0)
15080           element_shift = 0;
15081         if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
15082           element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
15083
15084         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15085                      GDI_SCROLLBAR_ITEM_POSITION,
15086                      element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15087       }
15088
15089       ModifyEditorElementList();
15090
15091       break;
15092
15093     case GADGET_ID_PROPERTIES:
15094       // always switch off element properties when they are already displayed
15095       last_properties_element = new_element;
15096     case GADGET_ID_ELEMENT_LEFT:
15097     case GADGET_ID_ELEMENT_MIDDLE:
15098     case GADGET_ID_ELEMENT_RIGHT:
15099       properties_element = (id == GADGET_ID_ELEMENT_LEFT   ? new_element1 :
15100                             id == GADGET_ID_ELEMENT_MIDDLE ? new_element2 :
15101                             id == GADGET_ID_ELEMENT_RIGHT  ? new_element3 :
15102                             new_element);
15103
15104       if (edit_mode != ED_MODE_PROPERTIES)
15105       {
15106         last_edit_mode = edit_mode;
15107
15108         ChangeEditModeWindow(ED_MODE_PROPERTIES);
15109
15110         last_level_drawing_function = drawing_function;
15111         ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS],
15112                       MB_LEFTBUTTON);
15113       }
15114       else if (properties_element != last_properties_element)
15115       {
15116         DrawEditModeWindow();
15117       }
15118       else
15119       {
15120         ChangeEditModeWindow(ED_MODE_DRAWING);
15121
15122         ClickOnGadget(level_editor_gadget[last_level_drawing_function],
15123                       MB_LEFTBUTTON);
15124       }
15125       break;
15126
15127     case GADGET_ID_PALETTE:
15128       if (edit_mode != ED_MODE_PALETTE)
15129       {
15130         last_edit_mode = edit_mode;
15131
15132         ChangeEditModeWindow(ED_MODE_PALETTE);
15133       }
15134       else
15135       {
15136         ChangeEditModeWindow(last_edit_mode);
15137       }
15138       break;
15139
15140     case GADGET_ID_WRAP_LEFT:
15141       WrapLevel(-step, 0);
15142       break;
15143
15144     case GADGET_ID_WRAP_RIGHT:
15145       WrapLevel(step, 0);
15146       break;
15147
15148     case GADGET_ID_WRAP_UP:
15149       WrapLevel(0, -step);
15150       break;
15151
15152     case GADGET_ID_WRAP_DOWN:
15153       WrapLevel(0, step);
15154       break;
15155
15156     case GADGET_ID_SINGLE_ITEMS:
15157     case GADGET_ID_CONNECTED_ITEMS:
15158     case GADGET_ID_LINE:
15159     case GADGET_ID_ARC:
15160     case GADGET_ID_TEXT:
15161     case GADGET_ID_RECTANGLE:
15162     case GADGET_ID_FILLED_BOX:
15163     case GADGET_ID_FLOOD_FILL:
15164     case GADGET_ID_GRAB_BRUSH:
15165     case GADGET_ID_PICK_ELEMENT:
15166       if (drawing_function != GADGET_ID_PICK_ELEMENT)
15167         last_drawing_function = drawing_function;
15168       drawing_function = id;
15169       draw_with_brush = FALSE;
15170       break;
15171
15172     case GADGET_ID_RANDOM_PLACEMENT:
15173       RandomPlacement(new_element);
15174       break;
15175
15176     case GADGET_ID_ZOOM:
15177       // zoom level editor tile size in or out (or reset to default size)
15178       ed_tilesize = (button == 1 ? ed_tilesize * 2 :
15179                      button == 2 ? ed_tilesize_default :
15180                      button == 3 ? ed_tilesize / 2 : ed_tilesize);
15181
15182       // when using touch device, cycle through all zoom tilesizes
15183       if (runtime.uses_touch_device && ed_tilesize > TILESIZE)
15184         ed_tilesize = MICRO_TILESIZE;
15185
15186       // limit zoom level by upper and lower bound
15187       ed_tilesize = MIN(MAX(MICRO_TILESIZE, ed_tilesize), TILESIZE);
15188
15189       InitZoomLevelSettings(ed_tilesize);
15190
15191       if (edit_mode == ED_MODE_DRAWING)
15192       {
15193         DrawDrawingWindow();
15194
15195         // redraw zoom gadget info text
15196         PrintEditorGadgetInfoText(level_editor_gadget[id]);
15197       }
15198
15199       // save current editor zoom tilesize
15200       SaveSetup_AutoSetup();
15201
15202       break;
15203
15204     case GADGET_ID_CUSTOM_COPY_FROM:
15205     case GADGET_ID_CUSTOM_COPY_TO:
15206     case GADGET_ID_CUSTOM_EXCHANGE:
15207       last_custom_copy_mode = id;
15208       last_drawing_function = drawing_function;
15209       break;
15210
15211     case GADGET_ID_CUSTOM_COPY:
15212       CopyCustomElement(properties_element, -1, id);
15213       break;
15214
15215     case GADGET_ID_CUSTOM_PASTE:
15216       CopyCustomElement(-1, properties_element, id);
15217       break;
15218
15219     case GADGET_ID_UNDO:
15220       if (button < 0)   // keep button value (even if modifier keys are pressed)
15221         button = -button;
15222       else if (button == 1 && GetKeyModState() & (KMOD_Shift | KMOD_Control))
15223         button = 3;
15224
15225       if (button == 1 && undo_buffer_steps == 0)
15226       {
15227         Request("Undo buffer empty!", REQ_CONFIRM);
15228
15229         break;
15230       }
15231       else if (button == 2)
15232       {
15233         break;
15234       }
15235       else if (button == 3 && redo_buffer_steps == 0)
15236       {
15237         Request("Redo buffer empty!", REQ_CONFIRM);
15238
15239         break;
15240       }
15241
15242       if (edit_mode != ED_MODE_DRAWING)
15243         ChangeEditModeWindow(ED_MODE_DRAWING);
15244
15245       if (button == 1)
15246       {
15247         // undo
15248
15249         undo_buffer_position =
15250           (undo_buffer_position - 1 + NUM_UNDO_STEPS) % NUM_UNDO_STEPS;
15251
15252         undo_buffer_steps--;
15253         redo_buffer_steps++;
15254       }
15255       else
15256       {
15257         // redo
15258
15259         undo_buffer_position = (undo_buffer_position + 1) % NUM_UNDO_STEPS;
15260
15261         undo_buffer_steps++;
15262         redo_buffer_steps--;
15263       }
15264
15265       for (x = 0; x < lev_fieldx; x++)
15266         for (y = 0; y < lev_fieldy; y++)
15267           Tile[x][y] = UndoBuffer[undo_buffer_position][x][y];
15268
15269       // check if undo operation forces change of border style
15270       CheckLevelBorderElement(FALSE);
15271
15272       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15273
15274       break;
15275
15276     case GADGET_ID_CONF:
15277       if (edit_mode != ED_MODE_LEVELCONFIG)
15278       {
15279         last_edit_mode = edit_mode;
15280
15281         ChangeEditModeWindow(ED_MODE_LEVELCONFIG);
15282       }
15283       else
15284       {
15285         ChangeEditModeWindow(ED_MODE_DRAWING);
15286       }
15287       break;
15288
15289     case GADGET_ID_CLEAR:
15290       if (edit_mode != ED_MODE_DRAWING)
15291         ChangeEditModeWindow(ED_MODE_DRAWING);
15292
15293       for (x = 0; x < MAX_LEV_FIELDX; x++)
15294         for (y = 0; y < MAX_LEV_FIELDY; y++)
15295           Tile[x][y] = (button == 1 ? EL_EMPTY : new_element);
15296
15297       CopyLevelToUndoBuffer(GADGET_ID_CLEAR);
15298
15299       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15300       break;
15301
15302     case GADGET_ID_SAVE:
15303     {
15304       // saving read-only levels into personal level set modifies global vars
15305       // "leveldir_current" and "level_nr"; restore them after saving level
15306       LevelDirTree *leveldir_former = leveldir_current;
15307       int level_nr_former = level_nr;
15308       char *level_filename;
15309       boolean new_level;
15310
15311       if (leveldir_current->readonly &&
15312           !PrepareSavingIntoPersonalLevelSet())
15313         break;
15314
15315       level_filename = getDefaultLevelFilename(level_nr);
15316       new_level = !fileExists(level_filename);
15317
15318       if (new_level ||
15319           Request("Save this level and kill the old?", REQ_ASK))
15320       {
15321         if (leveldir_former->readonly)
15322           ModifyLevelConfigForSavingIntoPersonalLevelSet(leveldir_former->name);
15323
15324         SetAutomaticNumberOfGemsNeeded();
15325
15326         CopyPlayfield(Tile, level.field);
15327         SaveLevel(level_nr);
15328
15329         level.changed = FALSE;
15330
15331         if (new_level)
15332         {
15333           char level_saved_msg[64];
15334
15335           if (leveldir_former->readonly)
15336             sprintf(level_saved_msg,
15337                     "Level saved as level %d into personal level set!",
15338                     level_nr);
15339           else
15340             strcpy(level_saved_msg, "Level saved!");
15341
15342           Request(level_saved_msg, REQ_CONFIRM);
15343         }
15344       }
15345
15346       // "cd" back to copied-from levelset (in case of saved read-only level)
15347       leveldir_current = leveldir_former;
15348       level_nr = level_nr_former;
15349
15350       break;
15351     }
15352
15353     case GADGET_ID_TEST:
15354       if (LevelChanged())
15355         level.game_version = GAME_VERSION_ACTUAL;
15356
15357       CopyPlayfield(level.field, TileBackup);
15358       CopyPlayfield(Tile, level.field);
15359
15360       CopyNativeLevel_RND_to_Native(&level);
15361
15362       UnmapLevelEditorGadgets();
15363       UndrawSpecialEditorDoor();
15364
15365       CloseDoor(DOOR_CLOSE_ALL);
15366
15367       // needed before playing if editor playfield area has different size
15368       ClearRectangle(drawto, REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
15369
15370       // redraw_mask = REDRAW_ALL;
15371
15372       level_editor_test_game = TRUE;
15373
15374       StartGameActions(FALSE, setup.autorecord, level.random_seed);
15375
15376       break;
15377
15378     case GADGET_ID_EXIT:
15379       RequestExitLevelEditor(TRUE, FALSE);  // if level has changed, ask user
15380       break;
15381
15382     default:
15383       if (id >= GADGET_ID_ELEMENTLIST_FIRST &&
15384           id <= GADGET_ID_ELEMENTLIST_LAST)
15385       {
15386         int element_position = id - GADGET_ID_ELEMENTLIST_FIRST;
15387
15388         new_element = editor_elements[element_position + element_shift];
15389
15390         if (IS_EDITOR_CASCADE(new_element))
15391         {
15392           int i;
15393
15394           for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
15395           {
15396             int *cascade_element = &(*editor_elements_info[i].headline_list)[0];
15397             boolean *cascade_value = editor_elements_info[i].setup_cascade_value;
15398
15399             if (*cascade_element == new_element)
15400             {
15401               *cascade_element = EL_CASCADE_TOGGLE(*cascade_element);
15402               *cascade_value = IS_EDITOR_CASCADE_ACTIVE(*cascade_element);
15403
15404               // update element selection list
15405               ReinitializeElementList();
15406               ModifyEditorElementList();
15407
15408               // update cascading gadget info text
15409               PrintEditorGadgetInfoText(level_editor_gadget[id]);
15410
15411               // save current editor cascading state
15412               SaveSetup_EditorCascade();
15413
15414               break;
15415             }
15416           }
15417
15418           break;
15419         }
15420
15421         if (last_custom_copy_mode != -1)
15422         {
15423           if (CopyCustomElement(properties_element, new_element,
15424                                 last_custom_copy_mode))
15425           {
15426             ClickOnGadget(level_editor_gadget[last_drawing_function],
15427                           MB_LEFTBUTTON);
15428
15429             last_custom_copy_mode = -1;
15430           }
15431
15432           break;
15433         }
15434
15435         // change element of button used to show palette
15436         if (editor.palette.show_on_element_buttons)
15437           button = last_button;
15438
15439         PickDrawingElement(button, new_element);
15440
15441         if (!stick_element_properties_window &&
15442             drawing_function != GADGET_ID_PICK_ELEMENT &&
15443             !(GetKeyModState() & KMOD_Control))
15444         {
15445           properties_element = new_element;
15446           if (edit_mode == ED_MODE_PROPERTIES)
15447             DrawPropertiesWindow();
15448         }
15449
15450         if (drawing_function == GADGET_ID_PICK_ELEMENT)
15451           ClickOnGadget(level_editor_gadget[last_drawing_function],
15452                         MB_LEFTBUTTON);
15453
15454         if (!use_permanent_palette)
15455           ChangeEditModeWindow(last_edit_mode);
15456       }
15457 #ifdef DEBUG
15458       else if (gi->event.type == GD_EVENT_PRESSED)
15459         Debug("editor", "default: HandleControlButtons: GD_EVENT_PRESSED(%d)", id);
15460       else if (gi->event.type == GD_EVENT_RELEASED)
15461         Debug("editor", "default: HandleControlButtons: GD_EVENT_RELEASED(%d)", id);
15462       else if (gi->event.type == GD_EVENT_MOVING)
15463         Debug("editor", "default: HandleControlButtons: GD_EVENT_MOVING(%d)", id);
15464       else
15465         Debug("editor", "default: HandleControlButtons: ? (id == %d)", id);
15466 #endif
15467       break;
15468   }
15469 }
15470
15471 void HandleLevelEditorKeyInput(Key key)
15472 {
15473   char letter = getCharFromKey(key);
15474
15475   if (drawing_function == GADGET_ID_TEXT &&
15476       DrawLevelText(0, 0, 0, TEXT_QUERY_TYPING) == TRUE)
15477   {
15478     if (letter)
15479       DrawLevelText(0, 0, letter, TEXT_WRITECHAR);
15480     else if (key == KSYM_Delete || key == KSYM_BackSpace)
15481       DrawLevelText(0, 0, 0, TEXT_BACKSPACE);
15482     else if (key == KSYM_Return)
15483       DrawLevelText(0, 0, 0, TEXT_NEWLINE);
15484     else if (key == KSYM_Escape)
15485       DrawLevelText(0, 0, 0, TEXT_END);
15486
15487     return;
15488   }
15489
15490   int id = GADGET_ID_NONE;
15491   int new_element_shift = element_shift;
15492   int step = ED_ELEMENTLIST_BUTTONS_VERT - 1;
15493   int button = MB_LEFTBUTTON;
15494   int i;
15495
15496   switch (key)
15497   {
15498     case KSYM_Left:
15499       id = GADGET_ID_SCROLL_LEFT;
15500       break;
15501     case KSYM_Right:
15502       id = GADGET_ID_SCROLL_RIGHT;
15503       break;
15504     case KSYM_Up:
15505       id = GADGET_ID_SCROLL_UP;
15506       break;
15507     case KSYM_Down:
15508       id = GADGET_ID_SCROLL_DOWN;
15509       break;
15510
15511     case KSYM_Page_Up:
15512     case KSYM_Page_Down:
15513       step *= (key == KSYM_Page_Up ? -1 : +1);
15514       element_shift += step * ED_ELEMENTLIST_BUTTONS_HORIZ;
15515
15516       if (element_shift < 0)
15517         element_shift = 0;
15518       if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
15519         element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
15520
15521       ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15522                    GDI_SCROLLBAR_ITEM_POSITION,
15523                    element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15524
15525       ModifyEditorElementList();
15526
15527       break;
15528
15529     case KSYM_Home:
15530     case KSYM_End:
15531       element_shift = (key == KSYM_Home ? 0 :
15532                        num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS);
15533
15534       ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15535                    GDI_SCROLLBAR_ITEM_POSITION,
15536                    element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15537
15538       ModifyEditorElementList();
15539
15540       break;
15541
15542     case KSYM_Insert:
15543     case KSYM_Delete:
15544
15545       // this is needed to prevent interference with running "True X-Mouse"
15546       if (GetKeyModStateFromEvents() & KMOD_Control)
15547         break;
15548
15549       // check for last or next editor cascade block in element list
15550       for (i = 0; i < num_editor_elements; i++)
15551       {
15552         if ((key == KSYM_Insert && i == element_shift) ||
15553             (key == KSYM_Delete && new_element_shift > element_shift))
15554           break;
15555
15556         // jump to next cascade block (or to start of element list)
15557         if (i == 0 || IS_EDITOR_CASCADE(editor_elements[i]))
15558           new_element_shift = i;
15559       }
15560
15561       if (i < num_editor_elements)
15562         element_shift = new_element_shift;
15563
15564       if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
15565         element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
15566
15567       ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15568                    GDI_SCROLLBAR_ITEM_POSITION,
15569                    element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15570
15571       ModifyEditorElementList();
15572
15573       break;
15574
15575     case KSYM_Escape:
15576       if (edit_mode == ED_MODE_DRAWING)
15577         RequestExitLevelEditor(setup.ask_on_escape_editor, TRUE);
15578       else if (edit_mode == ED_MODE_LEVELCONFIG)
15579         HandleControlButtons(level_editor_gadget[GADGET_ID_CONF]);
15580       else if (edit_mode == ED_MODE_PROPERTIES)
15581         HandleControlButtons(level_editor_gadget[GADGET_ID_PROPERTIES]);
15582       else if (edit_mode == ED_MODE_PALETTE)
15583         HandleControlButtons(level_editor_gadget[GADGET_ID_PALETTE]);
15584       else              // should never happen
15585         ChangeEditModeWindow(ED_MODE_DRAWING);
15586
15587       break;
15588
15589     default:
15590       break;
15591   }
15592
15593   if (id != GADGET_ID_NONE)
15594     ClickOnGadget(level_editor_gadget[id], button);
15595   else if (letter == '1' || letter == '?')
15596     ClickOnGadget(level_editor_gadget[GADGET_ID_ELEMENT_LEFT], button);
15597   else if (letter == '2')
15598     ClickOnGadget(level_editor_gadget[GADGET_ID_ELEMENT_MIDDLE], button);
15599   else if (letter == '3')
15600     ClickOnGadget(level_editor_gadget[GADGET_ID_ELEMENT_RIGHT], button);
15601   else if (letter == '.')
15602     ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS], button);
15603   else if (letter == 'U')
15604     ClickOnGadget(level_editor_gadget[GADGET_ID_UNDO], 3);
15605   else if (letter == '-' || key == KSYM_KP_Subtract)
15606     ClickOnGadget(level_editor_gadget[GADGET_ID_ZOOM], 3);
15607   else if (letter == '0' || key == KSYM_KP_0)
15608     ClickOnGadget(level_editor_gadget[GADGET_ID_ZOOM], 2);
15609   else if (letter == '+' || key == KSYM_KP_Add ||
15610            letter == '=')       // ("Shift-=" is "+" on US keyboards)
15611     ClickOnGadget(level_editor_gadget[GADGET_ID_ZOOM], 1);
15612   else if (key == KSYM_Return ||
15613            key == KSYM_space ||
15614            key == setup.shortcut.toggle_pause)
15615     ClickOnGadget(level_editor_gadget[GADGET_ID_TEST], button);
15616   else
15617     for (i = 0; i < ED_NUM_CTRL_BUTTONS; i++)
15618       if (letter && letter == controlbutton_info[i].shortcut)
15619         if (!anyTextGadgetActive())
15620           ClickOnGadget(level_editor_gadget[i], button);
15621
15622   if (draw_with_brush)
15623   {
15624     if (letter == 'x')
15625       FlipBrushX();
15626     else if (letter == 'y')
15627       FlipBrushY();
15628     else if (letter == 'z')
15629       RotateBrush();
15630   }
15631 }
15632
15633 static void HandleLevelEditorIdle_Properties(void)
15634 {
15635   int element_border = graphic_info[IMG_EDITOR_ELEMENT_BORDER].border_size;
15636   int x = editor.settings.element_graphic.x + element_border;
15637   int y = editor.settings.element_graphic.y + element_border;
15638   static DelayCounter action_delay = { 0 };
15639   int i;
15640
15641   action_delay.value = GameFrameDelay;
15642
15643   if (!DelayReached(&action_delay))
15644     return;
15645
15646   for (i = 0; i < ED_NUM_SELECTBOX; i++)
15647   {
15648     struct GadgetInfo *gi = level_editor_gadget[selectbox_info[i].gadget_id];
15649
15650     if (gi->mapped && gi->active && gi->selectbox.open)
15651       return;
15652   }
15653
15654   DrawEditorElementAnimation(SX + x, SY + y);
15655
15656   redraw_mask |= REDRAW_FIELD;
15657
15658   FrameCounter++;       // increase animation frame counter
15659 }
15660
15661 static void HandleLevelEditorIdle_Drawing(void)
15662 {
15663   static boolean last_highlighted = FALSE;
15664   static boolean last_highlighted_similar = FALSE;
15665   boolean highlighted = (GetKeyModState() & KMOD_Alt);
15666   boolean highlighted_similar = (GetKeyModState() & KMOD_Shift);
15667
15668   if (highlighted != last_highlighted ||
15669       (highlighted && highlighted_similar != last_highlighted_similar))
15670   {
15671     DrawAreaElementHighlight(highlighted, highlighted_similar);
15672
15673     redraw_mask |= REDRAW_FIELD;
15674   }
15675
15676   last_highlighted = highlighted;
15677   last_highlighted_similar = highlighted_similar;
15678 }
15679
15680 void HandleLevelEditorIdle(void)
15681 {
15682   if (edit_mode == ED_MODE_PROPERTIES)
15683     HandleLevelEditorIdle_Properties();
15684   else if (edit_mode == ED_MODE_DRAWING)
15685     HandleLevelEditorIdle_Drawing();
15686 }
15687
15688 static void ClearEditorGadgetInfoText(void)
15689 {
15690   DrawBackground(INFOTEXT_XPOS, INFOTEXT_YPOS, INFOTEXT_XSIZE, INFOTEXT_YSIZE);
15691 }
15692
15693 void PrintEditorGadgetInfoText(struct GadgetInfo *gi)
15694 {
15695   char infotext[MAX_OUTPUT_LINESIZE + 1];
15696   int max_infotext_len = getMaxInfoTextLength();
15697
15698   if (gi == NULL || strlen(gi->info_text) == 0)
15699     return;
15700
15701   strncpy(infotext, gi->info_text, max_infotext_len);
15702   infotext[max_infotext_len] = '\0';
15703
15704   if (gi->custom_id < ED_NUM_CTRL_BUTTONS)
15705   {
15706     int key = controlbutton_info[gi->custom_id].shortcut;
15707
15708     if (key)
15709     {
15710       char shortcut[MAX_OUTPUT_LINESIZE + 1];
15711
15712       if (gi->custom_id == GADGET_ID_SINGLE_ITEMS)
15713         sprintf(shortcut, " ('.' or '%c')", key);
15714       else if (gi->custom_id == GADGET_ID_PICK_ELEMENT)
15715         sprintf(shortcut, " ('%c' or 'Ctrl')", key);
15716       else if (gi->custom_id == GADGET_ID_TEST)
15717         sprintf(shortcut, " ('Enter' or 'Shift-%c')", key);
15718       else if (gi->custom_id == GADGET_ID_UNDO)
15719         sprintf(shortcut, " ('%c/Shift-U')", key);
15720       else if (gi->custom_id == GADGET_ID_ZOOM)
15721         sprintf(shortcut, " ('%c', '0', '-')", key);
15722       else
15723         sprintf(shortcut, " ('%s%c')",
15724                 (key >= 'A' && key <= 'Z' ? "Shift-" : ""), key);
15725
15726       if (strlen(infotext) + strlen(shortcut) <= max_infotext_len)
15727         strcat(infotext, shortcut);
15728     }
15729   }
15730
15731   DrawText(INFOTEXT_XPOS, INFOTEXT_YPOS, infotext, INFOTEXT_FONT);
15732 }
15733
15734 void HandleEditorGadgetInfoText(void *ptr)
15735 {
15736   struct GadgetInfo *gi = (struct GadgetInfo *)ptr;
15737
15738   if (game_status != GAME_MODE_EDITOR)
15739     return;
15740
15741   ClearEditorGadgetInfoText();
15742
15743   if (gi == NULL || gi->event.type == GD_EVENT_INFO_LEAVING)
15744     return;
15745
15746   // misuse this function to delete brush cursor, if needed
15747   if (edit_mode == ED_MODE_DRAWING && draw_with_brush)
15748     DeleteBrushFromCursor();
15749
15750   PrintEditorGadgetInfoText(gi);
15751 }
15752
15753 static void HandleDrawingAreaInfo(struct GadgetInfo *gi)
15754 {
15755   int id = gi->custom_id;
15756   int type_id = gi->custom_type_id;
15757   int sx = gi->event.x;
15758   int sy = gi->event.y;
15759   int lx = sx + level_xpos;
15760   int ly = sy + level_ypos;
15761   int min_sx = 0, min_sy = 0;
15762   int max_sx = gi->drawing.area_xsize - 1;
15763   int max_sy = gi->drawing.area_ysize - 1;
15764   int actual_drawing_function = drawing_function;
15765   int max_infotext_len = getMaxInfoTextLength();
15766   char infotext[MAX_OUTPUT_LINESIZE + 1];
15767
15768   infotext[0] = '\0';           // start with empty info text
15769
15770   // pressed Control key: simulate picking element
15771   if (GetKeyModState() & KMOD_Control)
15772     actual_drawing_function = GADGET_ID_PICK_ELEMENT;
15773
15774   ClearEditorGadgetInfoText();
15775
15776   if (gi->event.type == GD_EVENT_INFO_LEAVING)
15777     return;
15778
15779   // make sure to stay inside drawing area boundaries
15780   sx = (sx < min_sx ? min_sx : sx > max_sx ? max_sx : sx);
15781   sy = (sy < min_sy ? min_sy : sy > max_sy ? max_sy : sy);
15782
15783   if (id == GADGET_ID_DRAWING_LEVEL)
15784   {
15785     if (button_status)
15786     {
15787       int min_lx = 0, min_ly = 0;
15788       int max_lx = lev_fieldx - 1, max_ly = lev_fieldy - 1;
15789
15790       // get positions inside level field
15791       lx = sx + level_xpos;
15792       ly = sy + level_ypos;
15793
15794       // make sure to stay inside level field boundaries
15795       lx = (lx < min_lx ? min_lx : lx > max_lx ? max_lx : lx);
15796       ly = (ly < min_ly ? min_ly : ly > max_ly ? max_ly : ly);
15797
15798       // correct drawing area positions accordingly
15799       sx = lx - level_xpos;
15800       sy = ly - level_ypos;
15801     }
15802
15803     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
15804     {
15805       if (button_status)        // if (gi->state == GD_BUTTON_PRESSED)
15806       {
15807         static int start_lx = 0;
15808         static int start_ly = 0;
15809         char *text;
15810
15811         if (gi->event.type == GD_EVENT_PRESSED)
15812         {
15813           start_lx = lx;
15814           start_ly = ly;
15815         }
15816
15817         switch (actual_drawing_function)
15818         {
15819           case GADGET_ID_SINGLE_ITEMS:
15820             text = "Drawing single items";
15821             break;
15822           case GADGET_ID_CONNECTED_ITEMS:
15823             text = "Drawing connected items";
15824             break;
15825           case GADGET_ID_LINE:
15826             text = "Drawing line";
15827             break;
15828           case GADGET_ID_ARC:
15829             text = "Drawing arc";
15830             break;
15831           case GADGET_ID_TEXT:
15832             text = "Setting text cursor";
15833             break;
15834           case GADGET_ID_RECTANGLE:
15835             text = "Drawing rectangle";
15836             break;
15837           case GADGET_ID_FILLED_BOX:
15838             text = "Drawing filled box";
15839             break;
15840           case GADGET_ID_FLOOD_FILL:
15841             text = "Flood fill";
15842             break;
15843           case GADGET_ID_GRAB_BRUSH:
15844             text = "Grabbing brush";
15845             break;
15846           case GADGET_ID_PICK_ELEMENT:
15847             text = "Picking element";
15848             break;
15849
15850           default:
15851             text = "Drawing position";
15852             break;
15853         }
15854
15855         if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
15856           sprintf(infotext, "%s: %d, %d", text, lx, ly);
15857         else
15858           sprintf(infotext, "%s: %d, %d", text,
15859                   ABS(lx - start_lx) + 1, ABS(ly - start_ly) + 1);
15860       }
15861       else if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
15862         strncpy(infotext, getElementInfoText(Tile[lx][ly]), max_infotext_len);
15863       else
15864         sprintf(infotext, "Level position: %d, %d", lx, ly);
15865     }
15866
15867     // misuse this function to draw brush cursor, if needed
15868     if (edit_mode == ED_MODE_DRAWING && draw_with_brush && !button_status)
15869     {
15870       if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
15871         CopyBrushToCursor(sx, sy);
15872       else
15873         DeleteBrushFromCursor();
15874     }
15875
15876     if (!draw_with_brush)
15877       UpdateBrushPosition(sx, sy);
15878   }
15879   else if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
15880   {
15881     int pos = sx * drawingarea_info[type_id].area_ysize + sy;
15882     int element = drawingarea_info[type_id].value[pos];
15883
15884     strncpy(infotext, getElementInfoText(element), max_infotext_len);
15885   }
15886   else
15887   {
15888     if (id == GADGET_ID_CUSTOM_CONTENT)
15889       sprintf(infotext, "custom element content position: %d, %d", sx, sy);
15890     else if (id == GADGET_ID_GROUP_CONTENT)
15891       sprintf(infotext, "group element position: %d", sx + 1);
15892     else if (id >= GADGET_ID_YAMYAM_CONTENT_0 &&
15893              id <= GADGET_ID_YAMYAM_CONTENT_7)
15894       sprintf(infotext, "content area %d position: %d, %d",
15895               id - GADGET_ID_YAMYAM_CONTENT_0 + 1, sx, sy);
15896     else if (id >= GADGET_ID_MAGIC_BALL_CONTENT_0 &&
15897              id <= GADGET_ID_MAGIC_BALL_CONTENT_7)
15898       sprintf(infotext, "content area %d position: %d, %d",
15899               id - GADGET_ID_MAGIC_BALL_CONTENT_0 + 1, sx, sy);
15900     else if (id == GADGET_ID_ANDROID_CONTENT)
15901       sprintf(infotext, "android element position: %d", sx + 1);
15902     else if (drawingarea_info[type_id].infotext != NULL)
15903       strcpy(infotext, drawingarea_info[type_id].infotext);
15904   }
15905
15906   infotext[max_infotext_len] = '\0';
15907
15908   if (strlen(infotext) > 0)
15909     DrawTextS(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY, INFOTEXT_FONT, infotext);
15910 }
15911
15912 void RequestExitLevelEditor(boolean ask_if_level_has_changed,
15913                             boolean quick_quit)
15914 {
15915   if (!ask_if_level_has_changed ||
15916       !LevelChanged() ||
15917       Request("Level has changed! Exit without saving?",
15918               REQ_ASK | REQ_STAY_OPEN))
15919   {
15920     struct RectWithBorder *vp_door_1 = &viewport.door_1[GAME_MODE_MAIN];
15921     struct RectWithBorder *vp_door_2 = &viewport.door_2[GAME_MODE_MAIN];
15922
15923     // draw normal door
15924     UndrawSpecialEditorDoor();
15925
15926     // use door animation if door 1 viewport is unchanged and contains toolbox
15927     if (useEditorDoorAnimation())
15928       CloseDoor(DOOR_CLOSE_1 | DOOR_FORCE_ANIM);
15929
15930     // close editor doors if viewport definition is the same as in main menu
15931     if (vp_door_1->x      == DX     &&
15932         vp_door_1->y      == DY     &&
15933         vp_door_1->width  == DXSIZE &&
15934         vp_door_1->height == DYSIZE &&
15935         vp_door_2->x      == VX     &&
15936         vp_door_2->y      == VY     &&
15937         vp_door_2->width  == VXSIZE &&
15938         vp_door_2->height == VYSIZE)
15939       CloseDoor(DOOR_CLOSE_ALL | DOOR_NO_DELAY);
15940     else
15941       SetDoorState(DOOR_CLOSE_ALL);
15942
15943     BackToFront();
15944
15945     if (quick_quit)
15946       FadeSkipNextFadeIn();
15947
15948     SetGameStatus(GAME_MODE_MAIN);
15949
15950     DrawMainMenu();
15951   }
15952   else
15953   {
15954     if (!global.use_envelope_request)
15955     {
15956       CloseDoor(DOOR_CLOSE_1);
15957       OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
15958     }
15959   }
15960 }