added support for pushing settings 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_BD_HATCHING_DELAY_CYCLES_DOWN,
439   GADGET_ID_BD_HATCHING_DELAY_CYCLES_TEXT,
440   GADGET_ID_BD_HATCHING_DELAY_CYCLES_UP,
441   GADGET_ID_BD_HATCHING_DELAY_SECONDS_DOWN,
442   GADGET_ID_BD_HATCHING_DELAY_SECONDS_TEXT,
443   GADGET_ID_BD_HATCHING_DELAY_SECONDS_UP,
444   GADGET_ID_BD_PUSHING_PROB_DOWN,
445   GADGET_ID_BD_PUSHING_PROB_TEXT,
446   GADGET_ID_BD_PUSHING_PROB_UP,
447   GADGET_ID_BD_PUSHING_PROB_WITH_SWEET_DOWN,
448   GADGET_ID_BD_PUSHING_PROB_WITH_SWEET_TEXT,
449   GADGET_ID_BD_PUSHING_PROB_WITH_SWEET_UP,
450   GADGET_ID_ELEMENT_VALUE1_DOWN,
451   GADGET_ID_ELEMENT_VALUE1_TEXT,
452   GADGET_ID_ELEMENT_VALUE1_UP,
453   GADGET_ID_ELEMENT_VALUE2_DOWN,
454   GADGET_ID_ELEMENT_VALUE2_TEXT,
455   GADGET_ID_ELEMENT_VALUE2_UP,
456   GADGET_ID_ELEMENT_VALUE3_DOWN,
457   GADGET_ID_ELEMENT_VALUE3_TEXT,
458   GADGET_ID_ELEMENT_VALUE3_UP,
459   GADGET_ID_ELEMENT_VALUE4_DOWN,
460   GADGET_ID_ELEMENT_VALUE4_TEXT,
461   GADGET_ID_ELEMENT_VALUE4_UP,
462   GADGET_ID_YAMYAM_CONTENT_DOWN,
463   GADGET_ID_YAMYAM_CONTENT_TEXT,
464   GADGET_ID_YAMYAM_CONTENT_UP,
465   GADGET_ID_BALL_CONTENT_DOWN,
466   GADGET_ID_BALL_CONTENT_TEXT,
467   GADGET_ID_BALL_CONTENT_UP,
468   GADGET_ID_ANDROID_CONTENT_DOWN,
469   GADGET_ID_ANDROID_CONTENT_TEXT,
470   GADGET_ID_ANDROID_CONTENT_UP,
471   GADGET_ID_ENVELOPE_XSIZE_DOWN,
472   GADGET_ID_ENVELOPE_XSIZE_TEXT,
473   GADGET_ID_ENVELOPE_XSIZE_UP,
474   GADGET_ID_ENVELOPE_YSIZE_DOWN,
475   GADGET_ID_ENVELOPE_YSIZE_TEXT,
476   GADGET_ID_ENVELOPE_YSIZE_UP,
477   GADGET_ID_INVENTORY_SIZE_DOWN,
478   GADGET_ID_INVENTORY_SIZE_TEXT,
479   GADGET_ID_INVENTORY_SIZE_UP,
480   GADGET_ID_MM_BALL_CONTENT_DOWN,
481   GADGET_ID_MM_BALL_CONTENT_TEXT,
482   GADGET_ID_MM_BALL_CONTENT_UP,
483   GADGET_ID_CUSTOM_SCORE_DOWN,
484   GADGET_ID_CUSTOM_SCORE_TEXT,
485   GADGET_ID_CUSTOM_SCORE_UP,
486   GADGET_ID_CUSTOM_GEMCOUNT_DOWN,
487   GADGET_ID_CUSTOM_GEMCOUNT_TEXT,
488   GADGET_ID_CUSTOM_GEMCOUNT_UP,
489   GADGET_ID_CUSTOM_VALUE_FIX_DOWN,
490   GADGET_ID_CUSTOM_VALUE_FIX_TEXT,
491   GADGET_ID_CUSTOM_VALUE_FIX_UP,
492   GADGET_ID_CUSTOM_VALUE_RND_DOWN,
493   GADGET_ID_CUSTOM_VALUE_RND_TEXT,
494   GADGET_ID_CUSTOM_VALUE_RND_UP,
495   GADGET_ID_PUSH_DELAY_FIX_DOWN,
496   GADGET_ID_PUSH_DELAY_FIX_TEXT,
497   GADGET_ID_PUSH_DELAY_FIX_UP,
498   GADGET_ID_PUSH_DELAY_RND_DOWN,
499   GADGET_ID_PUSH_DELAY_RND_TEXT,
500   GADGET_ID_PUSH_DELAY_RND_UP,
501   GADGET_ID_DROP_DELAY_FIX_DOWN,
502   GADGET_ID_DROP_DELAY_FIX_TEXT,
503   GADGET_ID_DROP_DELAY_FIX_UP,
504   GADGET_ID_DROP_DELAY_RND_DOWN,
505   GADGET_ID_DROP_DELAY_RND_TEXT,
506   GADGET_ID_DROP_DELAY_RND_UP,
507   GADGET_ID_MOVE_DELAY_FIX_DOWN,
508   GADGET_ID_MOVE_DELAY_FIX_TEXT,
509   GADGET_ID_MOVE_DELAY_FIX_UP,
510   GADGET_ID_MOVE_DELAY_RND_DOWN,
511   GADGET_ID_MOVE_DELAY_RND_TEXT,
512   GADGET_ID_MOVE_DELAY_RND_UP,
513   GADGET_ID_STEP_DELAY_FIX_DOWN,
514   GADGET_ID_STEP_DELAY_FIX_TEXT,
515   GADGET_ID_STEP_DELAY_FIX_UP,
516   GADGET_ID_STEP_DELAY_RND_DOWN,
517   GADGET_ID_STEP_DELAY_RND_TEXT,
518   GADGET_ID_STEP_DELAY_RND_UP,
519   GADGET_ID_EXPLOSION_DELAY_DOWN,
520   GADGET_ID_EXPLOSION_DELAY_TEXT,
521   GADGET_ID_EXPLOSION_DELAY_UP,
522   GADGET_ID_IGNITION_DELAY_DOWN,
523   GADGET_ID_IGNITION_DELAY_TEXT,
524   GADGET_ID_IGNITION_DELAY_UP,
525   GADGET_ID_CHANGE_DELAY_FIX_DOWN,
526   GADGET_ID_CHANGE_DELAY_FIX_TEXT,
527   GADGET_ID_CHANGE_DELAY_FIX_UP,
528   GADGET_ID_CHANGE_DELAY_RND_DOWN,
529   GADGET_ID_CHANGE_DELAY_RND_TEXT,
530   GADGET_ID_CHANGE_DELAY_RND_UP,
531   GADGET_ID_CHANGE_CONT_RND_DOWN,
532   GADGET_ID_CHANGE_CONT_RND_TEXT,
533   GADGET_ID_CHANGE_CONT_RND_UP,
534   GADGET_ID_GROUP_CONTENT_DOWN,
535   GADGET_ID_GROUP_CONTENT_TEXT,
536   GADGET_ID_GROUP_CONTENT_UP,
537
538   // drawing area identifiers
539
540   GADGET_ID_DRAWING_LEVEL,
541   GADGET_ID_YAMYAM_CONTENT_0,
542   GADGET_ID_YAMYAM_CONTENT_1,
543   GADGET_ID_YAMYAM_CONTENT_2,
544   GADGET_ID_YAMYAM_CONTENT_3,
545   GADGET_ID_YAMYAM_CONTENT_4,
546   GADGET_ID_YAMYAM_CONTENT_5,
547   GADGET_ID_YAMYAM_CONTENT_6,
548   GADGET_ID_YAMYAM_CONTENT_7,
549   GADGET_ID_MAGIC_BALL_CONTENT_0,
550   GADGET_ID_MAGIC_BALL_CONTENT_1,
551   GADGET_ID_MAGIC_BALL_CONTENT_2,
552   GADGET_ID_MAGIC_BALL_CONTENT_3,
553   GADGET_ID_MAGIC_BALL_CONTENT_4,
554   GADGET_ID_MAGIC_BALL_CONTENT_5,
555   GADGET_ID_MAGIC_BALL_CONTENT_6,
556   GADGET_ID_MAGIC_BALL_CONTENT_7,
557   GADGET_ID_ANDROID_CONTENT,
558   GADGET_ID_AMOEBA_CONTENT,
559   GADGET_ID_START_ELEMENT,
560   GADGET_ID_ARTWORK_ELEMENT,
561   GADGET_ID_EXPLOSION_ELEMENT,
562   GADGET_ID_INVENTORY_CONTENT,
563   GADGET_ID_MM_BALL_CONTENT,
564   GADGET_ID_CUSTOM_GRAPHIC,
565   GADGET_ID_CUSTOM_CONTENT,
566   GADGET_ID_CUSTOM_MOVE_ENTER,
567   GADGET_ID_CUSTOM_MOVE_LEAVE,
568   GADGET_ID_CUSTOM_CHANGE_TARGET,
569   GADGET_ID_CUSTOM_CHANGE_CONTENT,
570   GADGET_ID_CUSTOM_CHANGE_TRIGGER,
571   GADGET_ID_CUSTOM_CHANGE_ACTION,
572   GADGET_ID_GROUP_CONTENT,
573   GADGET_ID_RANDOM_BACKGROUND,
574
575   // text input identifiers
576
577   GADGET_ID_LEVEL_NAME,
578   GADGET_ID_LEVEL_AUTHOR,
579   GADGET_ID_LEVELSET_NAME,
580   GADGET_ID_LEVELSET_AUTHOR,
581   GADGET_ID_ELEMENT_NAME,
582
583   // text area identifiers
584
585   GADGET_ID_ENVELOPE_INFO,
586
587   // selectbox identifiers
588
589   GADGET_ID_TIME_OR_STEPS,
590   GADGET_ID_TIME_SCORE_BASE,
591   GADGET_ID_GAME_ENGINE_TYPE,
592   GADGET_ID_BD_SCHEDULING_TYPE,
593   GADGET_ID_LEVELSET_SAVE_MODE,
594   GADGET_ID_WIND_DIRECTION,
595   GADGET_ID_PLAYER_SPEED,
596   GADGET_ID_MM_BALL_CHOICE_MODE,
597   GADGET_ID_CUSTOM_WALK_TO_ACTION,
598   GADGET_ID_CUSTOM_EXPLOSION_TYPE,
599   GADGET_ID_CUSTOM_DEADLINESS,
600   GADGET_ID_CUSTOM_MOVE_PATTERN,
601   GADGET_ID_CUSTOM_MOVE_DIRECTION,
602   GADGET_ID_CUSTOM_MOVE_STEPSIZE,
603   GADGET_ID_CUSTOM_MOVE_LEAVE_TYPE,
604   GADGET_ID_CUSTOM_SMASH_TARGETS,
605   GADGET_ID_CUSTOM_SLIPPERY_TYPE,
606   GADGET_ID_CUSTOM_ACCESS_TYPE,
607   GADGET_ID_CUSTOM_ACCESS_LAYER,
608   GADGET_ID_CUSTOM_ACCESS_PROTECTED,
609   GADGET_ID_CUSTOM_ACCESS_DIRECTION,
610   GADGET_ID_CHANGE_TIME_UNITS,
611   GADGET_ID_CHANGE_DIRECT_ACTION,
612   GADGET_ID_CHANGE_OTHER_ACTION,
613   GADGET_ID_CHANGE_SIDE,
614   GADGET_ID_CHANGE_PLAYER,
615   GADGET_ID_CHANGE_PAGE,
616   GADGET_ID_CHANGE_REPLACE_WHEN,
617   GADGET_ID_ACTION_TYPE,
618   GADGET_ID_ACTION_MODE,
619   GADGET_ID_ACTION_ARG,
620   GADGET_ID_SELECT_CHANGE_PAGE,
621   GADGET_ID_GROUP_CHOICE_MODE,
622
623   // textbutton identifiers
624
625   GADGET_ID_LEVELCONFIG_LEVEL,
626   GADGET_ID_LEVELCONFIG_LEVELSET,
627   GADGET_ID_LEVELCONFIG_EDITOR,
628   GADGET_ID_LEVELCONFIG_ENGINE,
629   GADGET_ID_PROPERTIES_INFO,
630   GADGET_ID_PROPERTIES_CONFIG,
631   GADGET_ID_PROPERTIES_CONFIG_1,
632   GADGET_ID_PROPERTIES_CONFIG_2,
633   GADGET_ID_PROPERTIES_CHANGE,
634   GADGET_ID_SAVE_AS_TEMPLATE_1,
635   GADGET_ID_SAVE_AS_TEMPLATE_2,
636   GADGET_ID_SAVE_LEVELSET,
637   GADGET_ID_ADD_CHANGE_PAGE,
638   GADGET_ID_DEL_CHANGE_PAGE,
639
640   // graphicbutton identifiers
641
642   GADGET_ID_PREV_CHANGE_PAGE,
643   GADGET_ID_NEXT_CHANGE_PAGE,
644   GADGET_ID_COPY_CHANGE_PAGE,
645   GADGET_ID_PASTE_CHANGE_PAGE,
646
647   // gadgets for scrolling of drawing area
648
649   GADGET_ID_SCROLL_UP,
650   GADGET_ID_SCROLL_DOWN,
651   GADGET_ID_SCROLL_LEFT,
652   GADGET_ID_SCROLL_RIGHT,
653   GADGET_ID_SCROLL_HORIZONTAL,
654   GADGET_ID_SCROLL_VERTICAL,
655
656   // gadgets for scrolling element list
657
658   GADGET_ID_SCROLL_LIST_UP,
659   GADGET_ID_SCROLL_LIST_DOWN,
660   GADGET_ID_SCROLL_LIST_VERTICAL,
661
662   // checkbuttons/radiobuttons for level/element properties
663
664   GADGET_ID_AUTO_COUNT_GEMS,
665   GADGET_ID_RATE_TIME_OVER_SCORE,
666   GADGET_ID_USE_LEVELSET_ARTWORK,
667   GADGET_ID_COPY_LEVEL_TEMPLATE,
668   GADGET_ID_RANDOM_PERCENTAGE,
669   GADGET_ID_RANDOM_QUANTITY,
670   GADGET_ID_RANDOM_RESTRICTED,
671   GADGET_ID_BD_INTERMISSION,
672   GADGET_ID_BD_PAL_TIMING,
673   GADGET_ID_BD_LINE_SHIFTING_BORDERS,
674   GADGET_ID_BD_SCAN_FIRST_AND_LAST_ROW,
675   GADGET_ID_BD_SHORT_EXPLOSIONS,
676   GADGET_ID_BD_GRAVITY_AFFECTS_ALL,
677   GADGET_ID_STICK_ELEMENT,
678   GADGET_ID_EM_SLIPPERY_GEMS,
679   GADGET_ID_EM_EXPLODES_BY_FIRE,
680   GADGET_ID_USE_SPRING_BUG,
681   GADGET_ID_USE_TIME_ORB_BUG,
682   GADGET_ID_USE_LIFE_BUGS,
683   GADGET_ID_RANDOM_BALL_CONTENT,
684   GADGET_ID_INITIAL_BALL_ACTIVE,
685   GADGET_ID_GROW_INTO_DIGGABLE,
686   GADGET_ID_SB_FIELDS_NEEDED,
687   GADGET_ID_SB_OBJECTS_NEEDED,
688   GADGET_ID_AUTO_EXIT_SOKOBAN,
689   GADGET_ID_SOLVED_BY_ONE_PLAYER,
690   GADGET_ID_FINISH_DIG_COLLECT,
691   GADGET_ID_KEEP_WALKABLE_CE,
692   GADGET_ID_CONTINUOUS_SNAPPING,
693   GADGET_ID_BLOCK_SNAP_FIELD,
694   GADGET_ID_BLOCK_LAST_FIELD,
695   GADGET_ID_SP_BLOCK_LAST_FIELD,
696   GADGET_ID_INSTANT_RELOCATION,
697   GADGET_ID_SHIFTED_RELOCATION,
698   GADGET_ID_LAZY_RELOCATION,
699   GADGET_ID_USE_START_ELEMENT,
700   GADGET_ID_USE_ARTWORK_ELEMENT,
701   GADGET_ID_USE_EXPLOSION_ELEMENT,
702   GADGET_ID_INITIAL_GRAVITY,
703   GADGET_ID_USE_INITIAL_INVENTORY,
704   GADGET_ID_CAN_PASS_TO_WALKABLE,
705   GADGET_ID_CAN_FALL_INTO_ACID,
706   GADGET_ID_CAN_MOVE_INTO_ACID,
707   GADGET_ID_DONT_COLLIDE_WITH,
708   GADGET_ID_BD_DIAGONAL_MOVEMENTS,
709   GADGET_ID_BD_TOPMOST_PLAYER_ACTIVE,
710   GADGET_ID_BD_PUSH_MEGA_ROCK_WITH_SWEET,
711   GADGET_ID_ENVELOPE_AUTOWRAP,
712   GADGET_ID_ENVELOPE_CENTERED,
713   GADGET_ID_MM_LASER_RED,
714   GADGET_ID_MM_LASER_GREEN,
715   GADGET_ID_MM_LASER_BLUE,
716   GADGET_ID_DF_LASER_RED,
717   GADGET_ID_DF_LASER_GREEN,
718   GADGET_ID_DF_LASER_BLUE,
719   GADGET_ID_ROTATE_MM_BALL_CONTENT,
720   GADGET_ID_EXPLODE_MM_BALL,
721   GADGET_ID_CUSTOM_INDESTRUCTIBLE,
722   GADGET_ID_CUSTOM_CAN_EXPLODE,
723   GADGET_ID_CUSTOM_EXPLODE_FIRE,
724   GADGET_ID_CUSTOM_EXPLODE_SMASH,
725   GADGET_ID_CUSTOM_EXPLODE_IMPACT,
726   GADGET_ID_CUSTOM_WALK_TO_OBJECT,
727   GADGET_ID_CUSTOM_DEADLY,
728   GADGET_ID_CUSTOM_CAN_MOVE,
729   GADGET_ID_CUSTOM_CAN_FALL,
730   GADGET_ID_CUSTOM_CAN_SMASH,
731   GADGET_ID_CUSTOM_SLIPPERY,
732   GADGET_ID_CUSTOM_ACCESSIBLE,
733   GADGET_ID_CUSTOM_GRAV_REACHABLE,
734   GADGET_ID_CUSTOM_USE_LAST_VALUE,
735   GADGET_ID_CUSTOM_USE_GRAPHIC,
736   GADGET_ID_CUSTOM_USE_TEMPLATE_1,
737   GADGET_ID_CUSTOM_USE_TEMPLATE_2,
738   GADGET_ID_CUSTOM_USE_TEMPLATE_3,
739   GADGET_ID_CUSTOM_CAN_CHANGE,
740   GADGET_ID_CHANGE_USE_CONTENT,
741   GADGET_ID_CHANGE_USE_EXPLOSION,
742   GADGET_ID_CHANGE_ONLY_COMPLETE,
743   GADGET_ID_CHANGE_USE_RANDOM,
744   GADGET_ID_CHANGE_HAS_ACTION,
745   GADGET_ID_CHANGE_DELAY,
746   GADGET_ID_CHANGE_BY_DIRECT_ACT,
747   GADGET_ID_CHANGE_BY_OTHER_ACT,
748
749   NUM_STATIC_GADGET_IDS
750 };
751
752 // gadgets for buttons in element list (dynamic)
753 #define GADGET_ID_ELEMENTLIST_FIRST     (NUM_STATIC_GADGET_IDS)
754 #define GADGET_ID_ELEMENTLIST_LAST      (GADGET_ID_ELEMENTLIST_FIRST +  \
755                                         ED_NUM_ELEMENTLIST_BUTTONS - 1)
756
757 #define NUM_EDITOR_GADGETS              (GADGET_ID_ELEMENTLIST_LAST + 1)
758
759 // radio button numbers
760 enum
761 {
762   RADIO_NR_NONE,
763   RADIO_NR_DRAWING_TOOLBOX,
764   RADIO_NR_RANDOM_ELEMENTS
765 };
766
767 // values for counter gadgets
768 enum
769 {
770   ED_COUNTER_ID_SELECT_LEVEL,
771   ED_COUNTER_ID_LEVEL_XSIZE,
772   ED_COUNTER_ID_LEVEL_YSIZE,
773   ED_COUNTER_ID_LEVEL_GEMSLIMIT,
774   ED_COUNTER_ID_LEVEL_TIMELIMIT,
775   ED_COUNTER_ID_LEVEL_TIMESCORE,
776   ED_COUNTER_ID_LEVEL_RANDOM_SEED,
777   ED_COUNTER_ID_LEVELSET_NUM_LEVELS,
778   ED_COUNTER_ID_LEVEL_RANDOM,
779   ED_COUNTER_ID_BD_CYCLE_DELAY_MS,
780   ED_COUNTER_ID_BD_CYCLE_DELAY_C64,
781   ED_COUNTER_ID_BD_HATCHING_DELAY_CYCLES,
782   ED_COUNTER_ID_BD_HATCHING_DELAY_SECONDS,
783   ED_COUNTER_ID_BD_PUSHING_PROB,
784   ED_COUNTER_ID_BD_PUSHING_PROB_WITH_SWEET,
785   ED_COUNTER_ID_ELEMENT_VALUE1,
786   ED_COUNTER_ID_ELEMENT_VALUE2,
787   ED_COUNTER_ID_ELEMENT_VALUE3,
788   ED_COUNTER_ID_ELEMENT_VALUE4,
789   ED_COUNTER_ID_YAMYAM_CONTENT,
790   ED_COUNTER_ID_BALL_CONTENT,
791   ED_COUNTER_ID_ANDROID_CONTENT,
792   ED_COUNTER_ID_ENVELOPE_XSIZE,
793   ED_COUNTER_ID_ENVELOPE_YSIZE,
794   ED_COUNTER_ID_INVENTORY_SIZE,
795   ED_COUNTER_ID_MM_BALL_CONTENT,
796   ED_COUNTER_ID_CUSTOM_SCORE,
797   ED_COUNTER_ID_CUSTOM_GEMCOUNT,
798   ED_COUNTER_ID_CUSTOM_VALUE_FIX,
799   ED_COUNTER_ID_CUSTOM_VALUE_RND,
800   ED_COUNTER_ID_PUSH_DELAY_FIX,
801   ED_COUNTER_ID_PUSH_DELAY_RND,
802   ED_COUNTER_ID_DROP_DELAY_FIX,
803   ED_COUNTER_ID_DROP_DELAY_RND,
804   ED_COUNTER_ID_MOVE_DELAY_FIX,
805   ED_COUNTER_ID_MOVE_DELAY_RND,
806   ED_COUNTER_ID_STEP_DELAY_FIX,
807   ED_COUNTER_ID_STEP_DELAY_RND,
808   ED_COUNTER_ID_EXPLOSION_DELAY,
809   ED_COUNTER_ID_IGNITION_DELAY,
810   ED_COUNTER_ID_GROUP_CONTENT,
811   ED_COUNTER_ID_CHANGE_DELAY_FIX,
812   ED_COUNTER_ID_CHANGE_DELAY_RND,
813   ED_COUNTER_ID_CHANGE_CONT_RND,
814
815   ED_NUM_COUNTERBUTTONS
816 };
817
818 #define ED_COUNTER_ID_LEVEL_FIRST       ED_COUNTER_ID_LEVEL_XSIZE
819 #define ED_COUNTER_ID_LEVEL_LAST        ED_COUNTER_ID_LEVEL_RANDOM_SEED
820 #define ED_COUNTER_ID_LEVELSET_FIRST    ED_COUNTER_ID_LEVELSET_NUM_LEVELS
821 #define ED_COUNTER_ID_LEVELSET_LAST     ED_COUNTER_ID_LEVELSET_NUM_LEVELS
822 #define ED_COUNTER_ID_EDITOR_FIRST      ED_COUNTER_ID_LEVEL_RANDOM
823 #define ED_COUNTER_ID_EDITOR_LAST       ED_COUNTER_ID_LEVEL_RANDOM
824
825 #define ED_COUNTER_ID_CUSTOM1_FIRST     ED_COUNTER_ID_CUSTOM_SCORE
826 #define ED_COUNTER_ID_CUSTOM1_LAST      ED_COUNTER_ID_DROP_DELAY_RND
827 #define ED_COUNTER_ID_CUSTOM2_FIRST     ED_COUNTER_ID_MOVE_DELAY_FIX
828 #define ED_COUNTER_ID_CUSTOM2_LAST      ED_COUNTER_ID_IGNITION_DELAY
829 #define ED_COUNTER_ID_CUSTOM_FIRST      ED_COUNTER_ID_CUSTOM1_FIRST
830 #define ED_COUNTER_ID_CUSTOM_LAST       ED_COUNTER_ID_CUSTOM2_LAST
831
832 #define ED_COUNTER_ID_CHANGE_FIRST      ED_COUNTER_ID_CHANGE_DELAY_FIX
833 #define ED_COUNTER_ID_CHANGE_LAST       ED_COUNTER_ID_CHANGE_CONT_RND
834
835 // values for scrollbutton gadgets
836 enum
837 {
838   ED_SCROLLBUTTON_ID_AREA_UP,
839   ED_SCROLLBUTTON_ID_AREA_DOWN,
840   ED_SCROLLBUTTON_ID_AREA_LEFT,
841   ED_SCROLLBUTTON_ID_AREA_RIGHT,
842   ED_SCROLLBUTTON_ID_LIST_UP,
843   ED_SCROLLBUTTON_ID_LIST_DOWN,
844
845   ED_NUM_SCROLLBUTTONS
846 };
847
848 #define ED_SCROLLBUTTON_ID_AREA_FIRST   ED_SCROLLBUTTON_ID_AREA_UP
849 #define ED_SCROLLBUTTON_ID_AREA_LAST    ED_SCROLLBUTTON_ID_AREA_RIGHT
850
851 // values for scrollbar gadgets
852 enum
853 {
854   ED_SCROLLBAR_ID_AREA_HORIZONTAL,
855   ED_SCROLLBAR_ID_AREA_VERTICAL,
856   ED_SCROLLBAR_ID_LIST_VERTICAL,
857
858   ED_NUM_SCROLLBARS
859 };
860
861 #define ED_SCROLLBAR_ID_AREA_FIRST      ED_SCROLLBAR_ID_AREA_HORIZONTAL
862 #define ED_SCROLLBAR_ID_AREA_LAST       ED_SCROLLBAR_ID_AREA_VERTICAL
863
864 // values for text input gadgets
865 enum
866 {
867   ED_TEXTINPUT_ID_LEVEL_NAME,
868   ED_TEXTINPUT_ID_LEVEL_AUTHOR,
869   ED_TEXTINPUT_ID_LEVELSET_NAME,
870   ED_TEXTINPUT_ID_LEVELSET_AUTHOR,
871   ED_TEXTINPUT_ID_ELEMENT_NAME,
872
873   ED_NUM_TEXTINPUT
874 };
875
876 #define ED_TEXTINPUT_ID_LEVEL_FIRST     ED_TEXTINPUT_ID_LEVEL_NAME
877 #define ED_TEXTINPUT_ID_LEVEL_LAST      ED_TEXTINPUT_ID_LEVEL_AUTHOR
878
879 #define ED_TEXTINPUT_ID_LEVELSET_FIRST  ED_TEXTINPUT_ID_LEVELSET_NAME
880 #define ED_TEXTINPUT_ID_LEVELSET_LAST   ED_TEXTINPUT_ID_LEVELSET_AUTHOR
881
882 // values for text area gadgets
883 enum
884 {
885   ED_TEXTAREA_ID_ENVELOPE_INFO,
886
887   ED_NUM_TEXTAREAS
888 };
889
890 #define ED_TEXTAREA_ID_LEVEL_FIRST      ED_TEXTAREA_ID_ENVELOPE
891 #define ED_TEXTAREA_ID_LEVEL_LAST       ED_TEXTAREA_ID_ENVELOPE
892
893 // values for selectbox gadgets
894 enum
895 {
896   ED_SELECTBOX_ID_TIME_OR_STEPS,
897   ED_SELECTBOX_ID_TIME_SCORE_BASE,
898   ED_SELECTBOX_ID_GAME_ENGINE_TYPE,
899   ED_SELECTBOX_ID_BD_SCHEDULING_TYPE,
900   ED_SELECTBOX_ID_LEVELSET_SAVE_MODE,
901   ED_SELECTBOX_ID_WIND_DIRECTION,
902   ED_SELECTBOX_ID_PLAYER_SPEED,
903   ED_SELECTBOX_ID_MM_BALL_CHOICE_MODE,
904   ED_SELECTBOX_ID_CUSTOM_ACCESS_TYPE,
905   ED_SELECTBOX_ID_CUSTOM_ACCESS_LAYER,
906   ED_SELECTBOX_ID_CUSTOM_ACCESS_PROTECTED,
907   ED_SELECTBOX_ID_CUSTOM_ACCESS_DIRECTION,
908   ED_SELECTBOX_ID_CUSTOM_WALK_TO_ACTION,
909   ED_SELECTBOX_ID_CUSTOM_MOVE_PATTERN,
910   ED_SELECTBOX_ID_CUSTOM_MOVE_DIRECTION,
911   ED_SELECTBOX_ID_CUSTOM_MOVE_STEPSIZE,
912   ED_SELECTBOX_ID_CUSTOM_MOVE_LEAVE_TYPE,
913   ED_SELECTBOX_ID_CUSTOM_SMASH_TARGETS,
914   ED_SELECTBOX_ID_CUSTOM_SLIPPERY_TYPE,
915   ED_SELECTBOX_ID_CUSTOM_DEADLINESS,
916   ED_SELECTBOX_ID_CUSTOM_EXPLOSION_TYPE,
917   ED_SELECTBOX_ID_CHANGE_TIME_UNITS,
918   ED_SELECTBOX_ID_CHANGE_DIRECT_ACTION,
919   ED_SELECTBOX_ID_CHANGE_OTHER_ACTION,
920   ED_SELECTBOX_ID_CHANGE_SIDE,
921   ED_SELECTBOX_ID_CHANGE_PLAYER,
922   ED_SELECTBOX_ID_CHANGE_PAGE,
923   ED_SELECTBOX_ID_CHANGE_REPLACE_WHEN,
924   ED_SELECTBOX_ID_ACTION_TYPE,
925   ED_SELECTBOX_ID_ACTION_MODE,
926   ED_SELECTBOX_ID_ACTION_ARG,
927   ED_SELECTBOX_ID_SELECT_CHANGE_PAGE,
928   ED_SELECTBOX_ID_GROUP_CHOICE_MODE,
929
930   ED_NUM_SELECTBOX
931 };
932
933 #define ED_SELECTBOX_ID_LEVEL_FIRST     ED_SELECTBOX_ID_TIME_OR_STEPS
934 #define ED_SELECTBOX_ID_LEVEL_LAST      ED_SELECTBOX_ID_GAME_ENGINE_TYPE
935
936 #define ED_SELECTBOX_ID_LEVELSET_FIRST  ED_SELECTBOX_ID_LEVELSET_SAVE_MODE
937 #define ED_SELECTBOX_ID_LEVELSET_LAST   ED_SELECTBOX_ID_LEVELSET_SAVE_MODE
938
939 #define ED_SELECTBOX_ID_ENGINE_FIRST    ED_SELECTBOX_ID_BD_SCHEDULING_TYPE
940 #define ED_SELECTBOX_ID_ENGINE_LAST     ED_SELECTBOX_ID_BD_SCHEDULING_TYPE
941
942 #define ED_SELECTBOX_ID_CUSTOM1_FIRST   ED_SELECTBOX_ID_CUSTOM_ACCESS_TYPE
943 #define ED_SELECTBOX_ID_CUSTOM1_LAST    ED_SELECTBOX_ID_CUSTOM_WALK_TO_ACTION
944 #define ED_SELECTBOX_ID_CUSTOM2_FIRST   ED_SELECTBOX_ID_CUSTOM_MOVE_PATTERN
945 #define ED_SELECTBOX_ID_CUSTOM2_LAST    ED_SELECTBOX_ID_CUSTOM_EXPLOSION_TYPE
946 #define ED_SELECTBOX_ID_CUSTOM_FIRST    ED_SELECTBOX_ID_CUSTOM1_FIRST
947 #define ED_SELECTBOX_ID_CUSTOM_LAST     ED_SELECTBOX_ID_CUSTOM2_LAST
948
949 #define ED_SELECTBOX_ID_CHANGE_FIRST    ED_SELECTBOX_ID_CHANGE_TIME_UNITS
950 #define ED_SELECTBOX_ID_CHANGE_LAST     ED_SELECTBOX_ID_SELECT_CHANGE_PAGE
951
952 // values for textbutton gadgets
953 enum
954 {
955   ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL,
956   ED_TEXTBUTTON_ID_LEVELCONFIG_LEVELSET,
957   ED_TEXTBUTTON_ID_LEVELCONFIG_EDITOR,
958   ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE,
959   ED_TEXTBUTTON_ID_PROPERTIES_INFO,
960   ED_TEXTBUTTON_ID_PROPERTIES_CONFIG,
961   ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_1,
962   ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2,
963   ED_TEXTBUTTON_ID_PROPERTIES_CHANGE,
964   ED_TEXTBUTTON_ID_SAVE_LEVELSET,
965   ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2,
966   ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1,
967   ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE,
968   ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE,
969
970   ED_NUM_TEXTBUTTONS
971 };
972
973 #define ED_TAB_BUTTON_ID_LEVELCONFIG_FIRST ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL
974 #define ED_TAB_BUTTON_ID_LEVELCONFIG_LAST  ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE
975
976 #define ED_TAB_BUTTON_ID_PROPERTIES_FIRST ED_TEXTBUTTON_ID_PROPERTIES_INFO
977 #define ED_TAB_BUTTON_ID_PROPERTIES_LAST  ED_TEXTBUTTON_ID_PROPERTIES_CHANGE
978
979 #define ED_TEXTBUTTON_ID_CHANGE_FIRST   ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE
980 #define ED_TEXTBUTTON_ID_CHANGE_LAST    ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE
981
982 // values for graphicbutton gadgets
983 enum
984 {
985   ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE,
986   ED_GRAPHICBUTTON_ID_NEXT_CHANGE_PAGE,
987   ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE,
988   ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE,
989
990   ED_NUM_GRAPHICBUTTONS
991 };
992
993 #define ED_GRAPHICBUTTON_ID_CHANGE_FIRST  ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE
994 #define ED_GRAPHICBUTTON_ID_CHANGE_LAST   ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE
995
996 // values for checkbutton gadgets
997 enum
998 {
999   ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS,
1000   ED_CHECKBUTTON_ID_RATE_TIME_OVER_SCORE,
1001   ED_CHECKBUTTON_ID_USE_LEVELSET_ARTWORK,
1002   ED_CHECKBUTTON_ID_COPY_LEVEL_TEMPLATE,
1003   ED_CHECKBUTTON_ID_RANDOM_RESTRICTED,
1004   ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_3,
1005   ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2,
1006   ED_CHECKBUTTON_ID_BD_INTERMISSION,
1007   ED_CHECKBUTTON_ID_BD_PAL_TIMING,
1008   ED_CHECKBUTTON_ID_BD_LINE_SHIFTING_BORDERS,
1009   ED_CHECKBUTTON_ID_BD_SCAN_FIRST_AND_LAST_ROW,
1010   ED_CHECKBUTTON_ID_BD_SHORT_EXPLOSIONS,
1011   ED_CHECKBUTTON_ID_BD_GRAVITY_AFFECTS_ALL,
1012   ED_CHECKBUTTON_ID_STICK_ELEMENT,
1013   ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS,
1014   ED_CHECKBUTTON_ID_EM_EXPLODES_BY_FIRE,
1015   ED_CHECKBUTTON_ID_USE_SPRING_BUG,
1016   ED_CHECKBUTTON_ID_USE_TIME_ORB_BUG,
1017   ED_CHECKBUTTON_ID_USE_LIFE_BUGS,
1018   ED_CHECKBUTTON_ID_RANDOM_BALL_CONTENT,
1019   ED_CHECKBUTTON_ID_INITIAL_BALL_ACTIVE,
1020   ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE,
1021   ED_CHECKBUTTON_ID_SB_FIELDS_NEEDED,
1022   ED_CHECKBUTTON_ID_SB_OBJECTS_NEEDED,
1023   ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN,
1024   ED_CHECKBUTTON_ID_SOLVED_BY_ONE_PLAYER,
1025   ED_CHECKBUTTON_ID_FINISH_DIG_COLLECT,
1026   ED_CHECKBUTTON_ID_KEEP_WALKABLE_CE,
1027   ED_CHECKBUTTON_ID_CONTINUOUS_SNAPPING,
1028   ED_CHECKBUTTON_ID_BLOCK_SNAP_FIELD,
1029   ED_CHECKBUTTON_ID_BLOCK_LAST_FIELD,
1030   ED_CHECKBUTTON_ID_SP_BLOCK_LAST_FIELD,
1031   ED_CHECKBUTTON_ID_INSTANT_RELOCATION,
1032   ED_CHECKBUTTON_ID_SHIFTED_RELOCATION,
1033   ED_CHECKBUTTON_ID_LAZY_RELOCATION,
1034   ED_CHECKBUTTON_ID_USE_START_ELEMENT,
1035   ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT,
1036   ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT,
1037   ED_CHECKBUTTON_ID_INITIAL_GRAVITY,
1038   ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY,
1039   ED_CHECKBUTTON_ID_CAN_PASS_TO_WALKABLE,
1040   ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID,
1041   ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID,
1042   ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH,
1043   ED_CHECKBUTTON_ID_BD_DIAGONAL_MOVEMENTS,
1044   ED_CHECKBUTTON_ID_BD_TOPMOST_PLAYER_ACTIVE,
1045   ED_CHECKBUTTON_ID_BD_PUSH_MEGA_ROCK_WITH_SWEET,
1046   ED_CHECKBUTTON_ID_ENVELOPE_AUTOWRAP,
1047   ED_CHECKBUTTON_ID_ENVELOPE_CENTERED,
1048   ED_CHECKBUTTON_ID_MM_LASER_RED,
1049   ED_CHECKBUTTON_ID_MM_LASER_GREEN,
1050   ED_CHECKBUTTON_ID_MM_LASER_BLUE,
1051   ED_CHECKBUTTON_ID_DF_LASER_RED,
1052   ED_CHECKBUTTON_ID_DF_LASER_GREEN,
1053   ED_CHECKBUTTON_ID_DF_LASER_BLUE,
1054   ED_CHECKBUTTON_ID_ROTATE_MM_BALL_CONTENT,
1055   ED_CHECKBUTTON_ID_EXPLODE_MM_BALL,
1056   ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC,
1057   ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1,
1058   ED_CHECKBUTTON_ID_CUSTOM_ACCESSIBLE,
1059   ED_CHECKBUTTON_ID_CUSTOM_GRAV_REACHABLE,
1060   ED_CHECKBUTTON_ID_CUSTOM_USE_LAST_VALUE,
1061   ED_CHECKBUTTON_ID_CUSTOM_WALK_TO_OBJECT,
1062   ED_CHECKBUTTON_ID_CUSTOM_INDESTRUCTIBLE,
1063   ED_CHECKBUTTON_ID_CUSTOM_CAN_MOVE,
1064   ED_CHECKBUTTON_ID_CUSTOM_CAN_FALL,
1065   ED_CHECKBUTTON_ID_CUSTOM_CAN_SMASH,
1066   ED_CHECKBUTTON_ID_CUSTOM_SLIPPERY,
1067   ED_CHECKBUTTON_ID_CUSTOM_DEADLY,
1068   ED_CHECKBUTTON_ID_CUSTOM_CAN_EXPLODE,
1069   ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_FIRE,
1070   ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_SMASH,
1071   ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_IMPACT,
1072   ED_CHECKBUTTON_ID_CUSTOM_CAN_CHANGE,
1073   ED_CHECKBUTTON_ID_CHANGE_DELAY,
1074   ED_CHECKBUTTON_ID_CHANGE_BY_DIRECT_ACT,
1075   ED_CHECKBUTTON_ID_CHANGE_BY_OTHER_ACT,
1076   ED_CHECKBUTTON_ID_CHANGE_USE_EXPLOSION,
1077   ED_CHECKBUTTON_ID_CHANGE_USE_CONTENT,
1078   ED_CHECKBUTTON_ID_CHANGE_ONLY_COMPLETE,
1079   ED_CHECKBUTTON_ID_CHANGE_USE_RANDOM,
1080   ED_CHECKBUTTON_ID_CHANGE_HAS_ACTION,
1081
1082   ED_NUM_CHECKBUTTONS
1083 };
1084
1085 #define ED_CHECKBUTTON_ID_LEVEL_FIRST   ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS
1086 #define ED_CHECKBUTTON_ID_LEVEL_LAST    ED_CHECKBUTTON_ID_RATE_TIME_OVER_SCORE
1087
1088 #define ED_CHECKBUTTON_ID_LEVELSET_FIRST ED_CHECKBUTTON_ID_USE_LEVELSET_ARTWORK
1089 #define ED_CHECKBUTTON_ID_LEVELSET_LAST  ED_CHECKBUTTON_ID_COPY_LEVEL_TEMPLATE
1090
1091 #define ED_CHECKBUTTON_ID_EDITOR_FIRST  ED_CHECKBUTTON_ID_RANDOM_RESTRICTED
1092 #define ED_CHECKBUTTON_ID_EDITOR_LAST   ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2
1093
1094 #define ED_CHECKBUTTON_ID_ENGINE_FIRST  ED_CHECKBUTTON_ID_BD_INTERMISSION
1095 #define ED_CHECKBUTTON_ID_ENGINE_LAST   ED_CHECKBUTTON_ID_BD_GRAVITY_AFFECTS_ALL
1096
1097 #define ED_CHECKBUTTON_ID_CUSTOM1_FIRST ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC
1098 #define ED_CHECKBUTTON_ID_CUSTOM1_LAST  ED_CHECKBUTTON_ID_CUSTOM_INDESTRUCTIBLE
1099 #define ED_CHECKBUTTON_ID_CUSTOM2_FIRST ED_CHECKBUTTON_ID_CUSTOM_CAN_MOVE
1100 #define ED_CHECKBUTTON_ID_CUSTOM2_LAST  ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_IMPACT
1101 #define ED_CHECKBUTTON_ID_CUSTOM_FIRST  ED_CHECKBUTTON_ID_CUSTOM1_FIRST
1102 #define ED_CHECKBUTTON_ID_CUSTOM_LAST   ED_CHECKBUTTON_ID_CUSTOM2_LAST
1103
1104 #define ED_CHECKBUTTON_ID_CHANGE_FIRST  ED_CHECKBUTTON_ID_CUSTOM_CAN_CHANGE
1105 #define ED_CHECKBUTTON_ID_CHANGE_LAST   ED_CHECKBUTTON_ID_CHANGE_HAS_ACTION
1106
1107 // values for radiobutton gadgets
1108 enum
1109 {
1110   ED_RADIOBUTTON_ID_PERCENTAGE,
1111   ED_RADIOBUTTON_ID_QUANTITY,
1112
1113   ED_NUM_RADIOBUTTONS
1114 };
1115
1116 #define ED_RADIOBUTTON_ID_EDITOR_FIRST  ED_RADIOBUTTON_ID_PERCENTAGE
1117 #define ED_RADIOBUTTON_ID_EDITOR_LAST   ED_RADIOBUTTON_ID_QUANTITY
1118
1119 // values for drawing area gadgets
1120 enum
1121 {
1122   ED_DRAWING_ID_DRAWING_LEVEL,
1123   ED_DRAWING_ID_YAMYAM_CONTENT_0,
1124   ED_DRAWING_ID_YAMYAM_CONTENT_1,
1125   ED_DRAWING_ID_YAMYAM_CONTENT_2,
1126   ED_DRAWING_ID_YAMYAM_CONTENT_3,
1127   ED_DRAWING_ID_YAMYAM_CONTENT_4,
1128   ED_DRAWING_ID_YAMYAM_CONTENT_5,
1129   ED_DRAWING_ID_YAMYAM_CONTENT_6,
1130   ED_DRAWING_ID_YAMYAM_CONTENT_7,
1131   ED_DRAWING_ID_MAGIC_BALL_CONTENT_0,
1132   ED_DRAWING_ID_MAGIC_BALL_CONTENT_1,
1133   ED_DRAWING_ID_MAGIC_BALL_CONTENT_2,
1134   ED_DRAWING_ID_MAGIC_BALL_CONTENT_3,
1135   ED_DRAWING_ID_MAGIC_BALL_CONTENT_4,
1136   ED_DRAWING_ID_MAGIC_BALL_CONTENT_5,
1137   ED_DRAWING_ID_MAGIC_BALL_CONTENT_6,
1138   ED_DRAWING_ID_MAGIC_BALL_CONTENT_7,
1139   ED_DRAWING_ID_ANDROID_CONTENT,
1140   ED_DRAWING_ID_AMOEBA_CONTENT,
1141   ED_DRAWING_ID_START_ELEMENT,
1142   ED_DRAWING_ID_ARTWORK_ELEMENT,
1143   ED_DRAWING_ID_EXPLOSION_ELEMENT,
1144   ED_DRAWING_ID_INVENTORY_CONTENT,
1145   ED_DRAWING_ID_MM_BALL_CONTENT,
1146   ED_DRAWING_ID_CUSTOM_GRAPHIC,
1147   ED_DRAWING_ID_CUSTOM_CONTENT,
1148   ED_DRAWING_ID_CUSTOM_MOVE_ENTER,
1149   ED_DRAWING_ID_CUSTOM_MOVE_LEAVE,
1150   ED_DRAWING_ID_CUSTOM_CHANGE_TARGET,
1151   ED_DRAWING_ID_CUSTOM_CHANGE_CONTENT,
1152   ED_DRAWING_ID_CUSTOM_CHANGE_TRIGGER,
1153   ED_DRAWING_ID_CUSTOM_CHANGE_ACTION,
1154   ED_DRAWING_ID_GROUP_CONTENT,
1155   ED_DRAWING_ID_RANDOM_BACKGROUND,
1156
1157   ED_NUM_DRAWING_AREAS
1158 };
1159
1160 #define ED_DRAWING_ID_EDITOR_FIRST      ED_DRAWING_ID_RANDOM_BACKGROUND
1161 #define ED_DRAWING_ID_EDITOR_LAST       ED_DRAWING_ID_RANDOM_BACKGROUND
1162
1163
1164 // ----------------------------------------------------------------------------
1165 // some internally used definitions
1166 // ----------------------------------------------------------------------------
1167
1168 // values for CopyLevelToUndoBuffer()
1169 #define UNDO_IMMEDIATE                  0
1170 #define UNDO_ACCUMULATE                 1
1171
1172 // values for scrollbars
1173 #define ED_SCROLL_NO                    0
1174 #define ED_SCROLL_LEFT                  1
1175 #define ED_SCROLL_RIGHT                 2
1176 #define ED_SCROLL_UP                    4
1177 #define ED_SCROLL_DOWN                  8
1178
1179 // screens in the level editor
1180 #define ED_MODE_DRAWING                 0
1181 #define ED_MODE_LEVELCONFIG             1
1182 #define ED_MODE_PROPERTIES              2
1183 #define ED_MODE_PALETTE                 3
1184
1185 // sub-screens in the global settings section
1186 #define ED_MODE_LEVELCONFIG_LEVEL       ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL
1187 #define ED_MODE_LEVELCONFIG_LEVELSET    ED_TEXTBUTTON_ID_LEVELCONFIG_LEVELSET
1188 #define ED_MODE_LEVELCONFIG_EDITOR      ED_TEXTBUTTON_ID_LEVELCONFIG_EDITOR
1189 #define ED_MODE_LEVELCONFIG_ENGINE      ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE
1190
1191 // sub-screens in the element properties section
1192 #define ED_MODE_PROPERTIES_INFO         ED_TEXTBUTTON_ID_PROPERTIES_INFO
1193 #define ED_MODE_PROPERTIES_CONFIG       ED_TEXTBUTTON_ID_PROPERTIES_CONFIG
1194 #define ED_MODE_PROPERTIES_CONFIG_1     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_1
1195 #define ED_MODE_PROPERTIES_CONFIG_2     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2
1196 #define ED_MODE_PROPERTIES_CHANGE       ED_TEXTBUTTON_ID_PROPERTIES_CHANGE
1197
1198 // how many steps can be cancelled
1199 #define NUM_UNDO_STEPS                  (64 + 1)
1200
1201 // values for elements with score for certain actions
1202 #define MIN_SCORE                       0
1203 #define MAX_SCORE                       999
1204
1205 // values for elements with count for collecting
1206 #define MIN_COLLECT_COUNT               0
1207 #define MAX_COLLECT_COUNT               999
1208
1209 // values for random placement
1210 #define RANDOM_USE_PERCENTAGE           0
1211 #define RANDOM_USE_QUANTITY             1
1212
1213 // values for level set save mode
1214 #define LEVELSET_SAVE_MODE_UPDATE       0
1215 #define LEVELSET_SAVE_MODE_CREATE       1
1216
1217 // default value for element tile size in drawing area
1218 #define DEFAULT_EDITOR_TILESIZE         MINI_TILESIZE
1219 #define DEFAULT_EDITOR_TILESIZE_MM      TILESIZE
1220
1221
1222 // ----------------------------------------------------------------------------
1223 // some internally used data structure definitions
1224 // ----------------------------------------------------------------------------
1225
1226 static struct
1227 {
1228   int graphic;
1229   int gadget_id;
1230   struct XYTileSize *pos;
1231   int gadget_type;
1232   char *infotext;
1233   char shortcut;
1234 } controlbutton_info[ED_NUM_CTRL_BUTTONS] =
1235 {
1236   // note: some additional characters are already reserved for "cheat mode"
1237   // shortcuts (":XYZ" style) -- for details, see "events.c"
1238
1239   // ---------- toolbox control buttons ---------------------------------------
1240
1241   {
1242     IMG_GFX_EDITOR_BUTTON_DRAW_SINGLE,          GADGET_ID_SINGLE_ITEMS,
1243     &editor.button.draw_single,                 GD_TYPE_RADIO_BUTTON,
1244     "draw single items",                        's'
1245   },
1246   {
1247     IMG_GFX_EDITOR_BUTTON_DRAW_CONNECTED,       GADGET_ID_CONNECTED_ITEMS,
1248     &editor.button.draw_connected,              GD_TYPE_RADIO_BUTTON,
1249     "draw connected items",                     'd'
1250   },
1251   {
1252     IMG_GFX_EDITOR_BUTTON_DRAW_LINE,            GADGET_ID_LINE,
1253     &editor.button.draw_line,                   GD_TYPE_RADIO_BUTTON,
1254     "draw lines",                               'l'
1255   },
1256   {
1257     IMG_GFX_EDITOR_BUTTON_DRAW_ARC,             GADGET_ID_ARC,
1258     &editor.button.draw_arc,                    GD_TYPE_RADIO_BUTTON,
1259     "draw arcs",                                'a'
1260   },
1261   {
1262     IMG_GFX_EDITOR_BUTTON_DRAW_RECTANGLE,       GADGET_ID_RECTANGLE,
1263     &editor.button.draw_rectangle,              GD_TYPE_RADIO_BUTTON,
1264     "draw outline rectangles",                  'r'
1265   },
1266   {
1267     IMG_GFX_EDITOR_BUTTON_DRAW_FILLED_BOX,      GADGET_ID_FILLED_BOX,
1268     &editor.button.draw_filled_box,             GD_TYPE_RADIO_BUTTON,
1269     "draw filled rectangles",                   'R'
1270   },
1271   {
1272     IMG_GFX_EDITOR_BUTTON_ROTATE_UP,            GADGET_ID_WRAP_UP,
1273     &editor.button.rotate_up,                   GD_TYPE_NORMAL_BUTTON,
1274     "wrap (rotate) level up",                   0
1275   },
1276   {
1277     IMG_GFX_EDITOR_BUTTON_DRAW_TEXT,            GADGET_ID_TEXT,
1278     &editor.button.draw_text,                   GD_TYPE_RADIO_BUTTON,
1279     "enter text elements",                      't'
1280   },
1281   {
1282     IMG_GFX_EDITOR_BUTTON_FLOOD_FILL,           GADGET_ID_FLOOD_FILL,
1283     &editor.button.flood_fill,                  GD_TYPE_RADIO_BUTTON,
1284     "flood fill",                               'f'
1285   },
1286   {
1287     IMG_GFX_EDITOR_BUTTON_ROTATE_LEFT,          GADGET_ID_WRAP_LEFT,
1288     &editor.button.rotate_left,                 GD_TYPE_NORMAL_BUTTON,
1289     "wrap (rotate) level left",                 0
1290   },
1291   {
1292     IMG_GFX_EDITOR_BUTTON_ZOOM_LEVEL,           GADGET_ID_ZOOM,
1293     &editor.button.zoom_level,                  GD_TYPE_NORMAL_BUTTON,
1294     "zoom level tile size",                     '+'
1295   },
1296   {
1297     IMG_GFX_EDITOR_BUTTON_ROTATE_RIGHT,         GADGET_ID_WRAP_RIGHT,
1298     &editor.button.rotate_right,                GD_TYPE_NORMAL_BUTTON,
1299     "wrap (rotate) level right",                0
1300   },
1301   {
1302     IMG_GFX_EDITOR_BUTTON_DRAW_RANDOM,          GADGET_ID_RANDOM_PLACEMENT,
1303     &editor.button.draw_random,                 GD_TYPE_NORMAL_BUTTON,
1304     "random element placement",                 0
1305   },
1306   {
1307     IMG_GFX_EDITOR_BUTTON_GRAB_BRUSH,           GADGET_ID_GRAB_BRUSH,
1308     &editor.button.grab_brush,                  GD_TYPE_RADIO_BUTTON,
1309     "grab brush",                               'b'
1310   },
1311   {
1312     IMG_GFX_EDITOR_BUTTON_ROTATE_DOWN,          GADGET_ID_WRAP_DOWN,
1313     &editor.button.rotate_down,                 GD_TYPE_NORMAL_BUTTON,
1314     "wrap (rotate) level down",                 0
1315   },
1316   {
1317     IMG_GFX_EDITOR_BUTTON_PICK_ELEMENT,         GADGET_ID_PICK_ELEMENT,
1318     &editor.button.pick_element,                GD_TYPE_RADIO_BUTTON,
1319     "pick drawing element",                     ','
1320   },
1321
1322   // ---------- level control buttons -----------------------------------------
1323
1324   {
1325     IMG_GFX_EDITOR_BUTTON_UNDO,                 GADGET_ID_UNDO,
1326     &editor.button.undo,                        GD_TYPE_NORMAL_BUTTON,
1327     "undo/redo last operation",                 'u'
1328   },
1329   {
1330     IMG_GFX_EDITOR_BUTTON_CONF,                 GADGET_ID_CONF,
1331     &editor.button.conf,                        GD_TYPE_NORMAL_BUTTON,
1332     "level and editor settings",                'I'
1333   },
1334   {
1335     IMG_GFX_EDITOR_BUTTON_SAVE,                 GADGET_ID_SAVE,
1336     &editor.button.save,                        GD_TYPE_NORMAL_BUTTON,
1337     "save level",                               'S'
1338   },
1339   {
1340     IMG_GFX_EDITOR_BUTTON_CLEAR,                GADGET_ID_CLEAR,
1341     &editor.button.clear,                       GD_TYPE_NORMAL_BUTTON,
1342     "clear level",                              'C'
1343   },
1344   {
1345     IMG_GFX_EDITOR_BUTTON_TEST,                 GADGET_ID_TEST,
1346     &editor.button.test,                        GD_TYPE_NORMAL_BUTTON,
1347     "test level",                               'T'
1348   },
1349   {
1350     IMG_GFX_EDITOR_BUTTON_EXIT,                 GADGET_ID_EXIT,
1351     &editor.button.exit,                        GD_TYPE_NORMAL_BUTTON,
1352     "exit level editor",                        'E'
1353   },
1354
1355   // ---------- CE and GE control buttons -------------------------------------
1356
1357   {
1358     IMG_GFX_EDITOR_BUTTON_CE_COPY_FROM,         GADGET_ID_CUSTOM_COPY_FROM,
1359     &editor.button.ce_copy_from,                GD_TYPE_RADIO_BUTTON,
1360     "copy settings from other element",         0
1361   },
1362   {
1363     IMG_GFX_EDITOR_BUTTON_CE_COPY_TO,           GADGET_ID_CUSTOM_COPY_TO,
1364     &editor.button.ce_copy_to,                  GD_TYPE_RADIO_BUTTON,
1365     "copy settings to other element",           0
1366   },
1367   {
1368     IMG_GFX_EDITOR_BUTTON_CE_SWAP,              GADGET_ID_CUSTOM_EXCHANGE,
1369     &editor.button.ce_swap,                     GD_TYPE_RADIO_BUTTON,
1370     "exchange element with other element",      0
1371   },
1372   {
1373     IMG_GFX_EDITOR_BUTTON_CE_COPY,              GADGET_ID_CUSTOM_COPY,
1374     &editor.button.ce_copy,                     GD_TYPE_NORMAL_BUTTON,
1375     "copy settings from this element",          0
1376   },
1377   {
1378     IMG_GFX_EDITOR_BUTTON_CE_PASTE,             GADGET_ID_CUSTOM_PASTE,
1379     &editor.button.ce_paste,                    GD_TYPE_NORMAL_BUTTON,
1380     "paste settings to this element",           0
1381   },
1382
1383   // ---------- palette control buttons ---------------------------------------
1384
1385   {
1386     IMG_GFX_EDITOR_BUTTON_PROPERTIES,           GADGET_ID_PROPERTIES,
1387     &editor.button.properties,                  GD_TYPE_NORMAL_BUTTON,
1388     "properties of drawing element",            'p'
1389   },
1390   {
1391     IMG_GFX_EDITOR_BUTTON_ELEMENT_LEFT,         GADGET_ID_ELEMENT_LEFT,
1392     &editor.button.element_left,                GD_TYPE_NORMAL_BUTTON,
1393     "properties of drawing element 1",          '1'
1394   },
1395   {
1396     IMG_GFX_EDITOR_BUTTON_ELEMENT_MIDDLE,       GADGET_ID_ELEMENT_MIDDLE,
1397     &editor.button.element_middle,              GD_TYPE_NORMAL_BUTTON,
1398     "properties of drawing element 2",          '2'
1399   },
1400   {
1401     IMG_GFX_EDITOR_BUTTON_ELEMENT_RIGHT,        GADGET_ID_ELEMENT_RIGHT,
1402     &editor.button.element_right,               GD_TYPE_NORMAL_BUTTON,
1403     "properties of drawing element 3",          '3'
1404   },
1405   {
1406     IMG_GFX_EDITOR_BUTTON_PALETTE,              GADGET_ID_PALETTE,
1407     &editor.button.palette,                     GD_TYPE_NORMAL_BUTTON,
1408     "show list of elements",                    'e'
1409   }
1410 };
1411
1412 static int random_placement_value = 10;
1413 static int random_placement_method = RANDOM_USE_QUANTITY;
1414 static int random_placement_background_element = EL_SAND;
1415 static boolean random_placement_background_restricted = FALSE;
1416 static boolean stick_element_properties_window = FALSE;
1417 static boolean custom_element_properties[NUM_ELEMENT_PROPERTIES];
1418 static boolean custom_element_change_events[NUM_CHANGE_EVENTS];
1419 static struct ElementChangeInfo custom_element_change;
1420 static struct ElementGroupInfo group_element_info;
1421 static struct ElementInfo custom_element;
1422
1423 static char levelset_name[MAX_LEVEL_NAME_LEN + 1];
1424 static char levelset_author[MAX_LEVEL_AUTHOR_LEN + 1];
1425 static int levelset_num_levels = 100;
1426 static boolean levelset_use_levelset_artwork = FALSE;
1427 static boolean levelset_copy_level_template = FALSE;
1428 static int levelset_save_mode = LEVELSET_SAVE_MODE_UPDATE;
1429
1430 static struct
1431 {
1432   int gadget_type_id;
1433   int x, y;
1434   int min_value, max_value;
1435   int gadget_id_down, gadget_id_up;
1436   int gadget_id_text;
1437   int gadget_id_align;
1438   int *value;
1439   char *text_above, *text_left, *text_right;
1440 } counterbutton_info[ED_NUM_COUNTERBUTTONS] =
1441 {
1442   // ---------- current level number ------------------------------------------
1443
1444   {
1445     ED_COUNTER_ID_SELECT_LEVEL,
1446     -1, -1,     // these values are not constant, but can change at runtime
1447     1,                                  100,
1448     GADGET_ID_SELECT_LEVEL_DOWN,        GADGET_ID_SELECT_LEVEL_UP,
1449     GADGET_ID_SELECT_LEVEL_TEXT,        GADGET_ID_NONE,
1450     &level_nr,
1451     NULL,                               NULL, NULL
1452   },
1453
1454   // ---------- level and editor settings -------------------------------------
1455
1456   {
1457     ED_COUNTER_ID_LEVEL_XSIZE,
1458     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
1459     MIN_LEV_FIELDX,                     MAX_LEV_FIELDX,
1460     GADGET_ID_LEVEL_XSIZE_DOWN,         GADGET_ID_LEVEL_XSIZE_UP,
1461     GADGET_ID_LEVEL_XSIZE_TEXT,         GADGET_ID_NONE,
1462     &level.fieldx,
1463     "playfield size:",                  NULL, "width",
1464   },
1465   {
1466     ED_COUNTER_ID_LEVEL_YSIZE,
1467     -1,                                 ED_LEVEL_SETTINGS_YPOS(4),
1468     MIN_LEV_FIELDY,                     MAX_LEV_FIELDY,
1469     GADGET_ID_LEVEL_YSIZE_DOWN,         GADGET_ID_LEVEL_YSIZE_UP,
1470     GADGET_ID_LEVEL_YSIZE_TEXT,         GADGET_ID_LEVEL_XSIZE_UP,
1471     &level.fieldy,
1472     NULL,                               " ", "height",
1473   },
1474   {
1475     ED_COUNTER_ID_LEVEL_GEMSLIMIT,
1476     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(5),
1477     0,                                  999,
1478     GADGET_ID_LEVEL_GEMSLIMIT_DOWN,     GADGET_ID_LEVEL_GEMSLIMIT_UP,
1479     GADGET_ID_LEVEL_GEMSLIMIT_TEXT,     GADGET_ID_NONE,
1480     &level.gems_needed,
1481     NULL,                               "number of gems to collect:", NULL
1482   },
1483   {
1484     ED_COUNTER_ID_LEVEL_TIMELIMIT,
1485     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(8),
1486     0,                                  9999,
1487     GADGET_ID_LEVEL_TIMELIMIT_DOWN,     GADGET_ID_LEVEL_TIMELIMIT_UP,
1488     GADGET_ID_LEVEL_TIMELIMIT_TEXT,     GADGET_ID_NONE,
1489     &level.time,
1490     "time or step limit to solve level:", NULL, NULL
1491   },
1492   {
1493     ED_COUNTER_ID_LEVEL_TIMESCORE,
1494     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(10),
1495     0,                                  999,
1496     GADGET_ID_LEVEL_TIMESCORE_DOWN,     GADGET_ID_LEVEL_TIMESCORE_UP,
1497     GADGET_ID_LEVEL_TIMESCORE_TEXT,     GADGET_ID_NONE,
1498     &level.score[SC_TIME_BONUS],
1499     "score for time or steps left:",    NULL, NULL
1500   },
1501   {
1502     ED_COUNTER_ID_LEVEL_RANDOM_SEED,
1503     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(13),
1504     0,                                  9999,
1505     GADGET_ID_LEVEL_RANDOM_SEED_DOWN,   GADGET_ID_LEVEL_RANDOM_SEED_UP,
1506     GADGET_ID_LEVEL_RANDOM_SEED_TEXT,   GADGET_ID_NONE,
1507     &level.random_seed,
1508     NULL,                               "random seed:", "(0 => random)"
1509   },
1510   {
1511     ED_COUNTER_ID_LEVELSET_NUM_LEVELS,
1512     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
1513     1,                                  MAX_LEVELS,
1514     GADGET_ID_LEVELSET_NUM_LEVELS_DOWN, GADGET_ID_LEVELSET_NUM_LEVELS_UP,
1515     GADGET_ID_LEVELSET_NUM_LEVELS_TEXT, GADGET_ID_NONE,
1516     &levelset_num_levels,
1517     "number of levels:",                NULL, NULL,
1518   },
1519   {
1520     ED_COUNTER_ID_LEVEL_RANDOM,
1521     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(0),
1522     1,                                  100,
1523     GADGET_ID_LEVEL_RANDOM_DOWN,        GADGET_ID_LEVEL_RANDOM_UP,
1524     GADGET_ID_LEVEL_RANDOM_TEXT,        GADGET_ID_NONE,
1525     &random_placement_value,
1526     "random element placement:",        NULL, "in"
1527   },
1528   {
1529     ED_COUNTER_ID_BD_CYCLE_DELAY_MS,
1530     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(3),
1531     50,                                 500,
1532     GADGET_ID_BD_CYCLE_DELAY_MS_DOWN,   GADGET_ID_BD_CYCLE_DELAY_MS_UP,
1533     GADGET_ID_BD_CYCLE_DELAY_MS_TEXT,   GADGET_ID_NONE,
1534     &level.bd_cycle_delay_ms,
1535     NULL,                               NULL, "game cycle delay (ms)"
1536   },
1537   {
1538     ED_COUNTER_ID_BD_CYCLE_DELAY_C64,
1539     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(3),
1540     0,                                  32,
1541     GADGET_ID_BD_CYCLE_DELAY_C64_DOWN,  GADGET_ID_BD_CYCLE_DELAY_C64_UP,
1542     GADGET_ID_BD_CYCLE_DELAY_C64_TEXT,  GADGET_ID_NONE,
1543     &level.bd_cycle_delay_c64,
1544     NULL,                               NULL, "game cycle delay (C64-style)"
1545   },
1546   {
1547     ED_COUNTER_ID_BD_HATCHING_DELAY_CYCLES,
1548     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
1549     1,                                  40,
1550     GADGET_ID_BD_HATCHING_DELAY_CYCLES_DOWN,    GADGET_ID_BD_HATCHING_DELAY_CYCLES_UP,
1551     GADGET_ID_BD_HATCHING_DELAY_CYCLES_TEXT,    GADGET_ID_NONE,
1552     &level.bd_hatching_delay_cycles,
1553     NULL,                               NULL, "hatching delay (cycles)"
1554   },
1555   {
1556     ED_COUNTER_ID_BD_HATCHING_DELAY_SECONDS,
1557     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
1558     1,                                  40,
1559     GADGET_ID_BD_HATCHING_DELAY_SECONDS_DOWN,   GADGET_ID_BD_HATCHING_DELAY_SECONDS_UP,
1560     GADGET_ID_BD_HATCHING_DELAY_SECONDS_TEXT,   GADGET_ID_NONE,
1561     &level.bd_hatching_delay_seconds,
1562     NULL,                               NULL, "hatching delay (seconds)"
1563   },
1564
1565   // ---------- element settings: configure (various elements) ----------------
1566
1567   {
1568     ED_COUNTER_ID_BD_PUSHING_PROB,
1569     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
1570     0,                                  100,
1571     GADGET_ID_BD_PUSHING_PROB_DOWN,     GADGET_ID_BD_PUSHING_PROB_UP,
1572     GADGET_ID_BD_PUSHING_PROB_TEXT,     GADGET_ID_NONE,
1573     &level.bd_pushing_prob,
1574     NULL,                               NULL, "probability to push rocks"
1575   },
1576   {
1577     ED_COUNTER_ID_BD_PUSHING_PROB_WITH_SWEET,
1578     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1579     0,                                  100,
1580     GADGET_ID_BD_PUSHING_PROB_WITH_SWEET_DOWN,  GADGET_ID_BD_PUSHING_PROB_WITH_SWEET_UP,
1581     GADGET_ID_BD_PUSHING_PROB_WITH_SWEET_TEXT,  GADGET_ID_NONE,
1582     &level.bd_pushing_prob_with_sweet,
1583     NULL,                               NULL, "as above, after eating sweet"
1584   },
1585   {
1586     ED_COUNTER_ID_ELEMENT_VALUE1,
1587     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
1588     MIN_SCORE,                          MAX_SCORE,
1589     GADGET_ID_ELEMENT_VALUE1_DOWN,      GADGET_ID_ELEMENT_VALUE1_UP,
1590     GADGET_ID_ELEMENT_VALUE1_TEXT,      GADGET_ID_NONE,
1591     NULL,                               // will be set when used
1592     NULL,                               NULL, NULL
1593   },
1594   {
1595     ED_COUNTER_ID_ELEMENT_VALUE2,
1596     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
1597     MIN_SCORE,                          MAX_SCORE,
1598     GADGET_ID_ELEMENT_VALUE2_DOWN,      GADGET_ID_ELEMENT_VALUE2_UP,
1599     GADGET_ID_ELEMENT_VALUE2_TEXT,      GADGET_ID_NONE,
1600     NULL,                               // will be set when used
1601     NULL,                               NULL, NULL
1602   },
1603   {
1604     ED_COUNTER_ID_ELEMENT_VALUE3,
1605     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
1606     MIN_SCORE,                          MAX_SCORE,
1607     GADGET_ID_ELEMENT_VALUE3_DOWN,      GADGET_ID_ELEMENT_VALUE3_UP,
1608     GADGET_ID_ELEMENT_VALUE3_TEXT,      GADGET_ID_NONE,
1609     NULL,                               // will be set when used
1610     NULL,                               NULL, NULL
1611   },
1612   {
1613     ED_COUNTER_ID_ELEMENT_VALUE4,
1614     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1615     MIN_SCORE,                          MAX_SCORE,
1616     GADGET_ID_ELEMENT_VALUE4_DOWN,      GADGET_ID_ELEMENT_VALUE4_UP,
1617     GADGET_ID_ELEMENT_VALUE4_TEXT,      GADGET_ID_NONE,
1618     NULL,                               // will be set when used
1619     NULL,                               NULL, NULL
1620   },
1621   {
1622     ED_COUNTER_ID_YAMYAM_CONTENT,
1623     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1624     MIN_ELEMENT_CONTENTS,               MAX_ELEMENT_CONTENTS,
1625     GADGET_ID_YAMYAM_CONTENT_DOWN,      GADGET_ID_YAMYAM_CONTENT_UP,
1626     GADGET_ID_YAMYAM_CONTENT_TEXT,      GADGET_ID_NONE,
1627     &level.num_yamyam_contents,
1628     NULL,                               NULL, "number of content areas"
1629   },
1630   {
1631     ED_COUNTER_ID_BALL_CONTENT,
1632     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
1633     MIN_ELEMENT_CONTENTS,               MAX_ELEMENT_CONTENTS,
1634     GADGET_ID_BALL_CONTENT_DOWN,        GADGET_ID_BALL_CONTENT_UP,
1635     GADGET_ID_BALL_CONTENT_TEXT,        GADGET_ID_NONE,
1636     &level.num_ball_contents,
1637     NULL,                               NULL, "number of content areas"
1638   },
1639   {
1640     ED_COUNTER_ID_ANDROID_CONTENT,
1641     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
1642     MIN_ANDROID_ELEMENTS,               MAX_ANDROID_ELEMENTS,
1643     GADGET_ID_ANDROID_CONTENT_DOWN,     GADGET_ID_ANDROID_CONTENT_UP,
1644     GADGET_ID_ANDROID_CONTENT_TEXT,     GADGET_ID_NONE,
1645     &level.num_android_clone_elements,
1646     NULL,                               NULL, "number of clonable elements"
1647   },
1648   {
1649     ED_COUNTER_ID_ENVELOPE_XSIZE,
1650     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
1651     MIN_ENVELOPE_XSIZE,                 MAX_ENVELOPE_XSIZE,
1652     GADGET_ID_ENVELOPE_XSIZE_DOWN,      GADGET_ID_ENVELOPE_XSIZE_UP,
1653     GADGET_ID_ENVELOPE_XSIZE_TEXT,      GADGET_ID_NONE,
1654     NULL,                               // will be set when used
1655     NULL,                               NULL, "width",
1656   },
1657   {
1658     ED_COUNTER_ID_ENVELOPE_YSIZE,
1659     -1,                                 ED_ELEMENT_SETTINGS_YPOS(0),
1660     MIN_ENVELOPE_YSIZE,                 MAX_ENVELOPE_YSIZE,
1661     GADGET_ID_ENVELOPE_YSIZE_DOWN,      GADGET_ID_ENVELOPE_YSIZE_UP,
1662     GADGET_ID_ENVELOPE_YSIZE_TEXT,      GADGET_ID_ENVELOPE_XSIZE_UP,
1663     NULL,                               // will be set when used
1664     NULL,                               " ", "height",
1665   },
1666   {
1667     ED_COUNTER_ID_INVENTORY_SIZE,
1668     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
1669     MIN_INITIAL_INVENTORY_SIZE,         MAX_INITIAL_INVENTORY_SIZE,
1670     GADGET_ID_INVENTORY_SIZE_DOWN,      GADGET_ID_INVENTORY_SIZE_UP,
1671     GADGET_ID_INVENTORY_SIZE_TEXT,      GADGET_ID_NONE,
1672     &level.initial_inventory_size[0],
1673     NULL,                               NULL, "number of inventory elements"
1674   },
1675   {
1676     ED_COUNTER_ID_MM_BALL_CONTENT,
1677     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1678     MIN_ELEMENTS_IN_GROUP,              MAX_MM_BALL_CONTENTS,
1679     GADGET_ID_MM_BALL_CONTENT_DOWN,     GADGET_ID_MM_BALL_CONTENT_UP,
1680     GADGET_ID_MM_BALL_CONTENT_TEXT,     GADGET_ID_NONE,
1681     &level.num_mm_ball_contents,
1682     NULL,                               NULL, "number of content elements"
1683   },
1684
1685   // ---------- element settings: configure 1 (custom elements) ---------------
1686
1687   {
1688     ED_COUNTER_ID_CUSTOM_SCORE,
1689     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(5),
1690     MIN_SCORE,                          MAX_SCORE,
1691     GADGET_ID_CUSTOM_SCORE_DOWN,        GADGET_ID_CUSTOM_SCORE_UP,
1692     GADGET_ID_CUSTOM_SCORE_TEXT,        GADGET_ID_NONE,
1693     &custom_element.collect_score_initial,
1694     NULL,                               "CE score", " "
1695   },
1696   {
1697     ED_COUNTER_ID_CUSTOM_GEMCOUNT,
1698     -1,                                 ED_ELEMENT_SETTINGS_YPOS(5),
1699     MIN_COLLECT_COUNT,                  MAX_COLLECT_COUNT,
1700     GADGET_ID_CUSTOM_GEMCOUNT_DOWN,     GADGET_ID_CUSTOM_GEMCOUNT_UP,
1701     GADGET_ID_CUSTOM_GEMCOUNT_TEXT,     GADGET_ID_CUSTOM_SCORE_UP,
1702     &custom_element.collect_count_initial,
1703     NULL,                               "CE count", NULL
1704   },
1705   {
1706     ED_COUNTER_ID_CUSTOM_VALUE_FIX,
1707     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(10),
1708     0,                                  9999,
1709     GADGET_ID_CUSTOM_VALUE_FIX_DOWN,    GADGET_ID_CUSTOM_VALUE_FIX_UP,
1710     GADGET_ID_CUSTOM_VALUE_FIX_TEXT,    GADGET_ID_NONE,
1711     &custom_element.ce_value_fixed_initial,
1712     NULL,                               "CE value", NULL
1713   },
1714   {
1715     ED_COUNTER_ID_CUSTOM_VALUE_RND,
1716     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(10),
1717     0,                                  9999,
1718     GADGET_ID_CUSTOM_VALUE_RND_DOWN,    GADGET_ID_CUSTOM_VALUE_RND_UP,
1719     GADGET_ID_CUSTOM_VALUE_RND_TEXT,    GADGET_ID_CUSTOM_VALUE_FIX_UP,
1720     &custom_element.ce_value_random_initial,
1721     NULL,                               "+random", NULL
1722   },
1723   {
1724     ED_COUNTER_ID_PUSH_DELAY_FIX,
1725     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(6),
1726     0,                                  999,
1727     GADGET_ID_PUSH_DELAY_FIX_DOWN,      GADGET_ID_PUSH_DELAY_FIX_UP,
1728     GADGET_ID_PUSH_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1729     &custom_element.push_delay_fixed,
1730     NULL,                               "push delay", NULL
1731   },
1732   {
1733     ED_COUNTER_ID_PUSH_DELAY_RND,
1734     -1,                                 ED_ELEMENT_SETTINGS_YPOS(6),
1735     0,                                  999,
1736     GADGET_ID_PUSH_DELAY_RND_DOWN,      GADGET_ID_PUSH_DELAY_RND_UP,
1737     GADGET_ID_PUSH_DELAY_RND_TEXT,      GADGET_ID_PUSH_DELAY_FIX_UP,
1738     &custom_element.push_delay_random,
1739     NULL,                               "+random", NULL
1740   },
1741   {
1742     ED_COUNTER_ID_DROP_DELAY_FIX,
1743     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(7),
1744     0,                                  999,
1745     GADGET_ID_DROP_DELAY_FIX_DOWN,      GADGET_ID_DROP_DELAY_FIX_UP,
1746     GADGET_ID_DROP_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1747     &custom_element.drop_delay_fixed,
1748     NULL,                               "drop delay", NULL
1749   },
1750   {
1751     ED_COUNTER_ID_DROP_DELAY_RND,
1752     -1,                                 ED_ELEMENT_SETTINGS_YPOS(7),
1753     0,                                  999,
1754     GADGET_ID_DROP_DELAY_RND_DOWN,      GADGET_ID_DROP_DELAY_RND_UP,
1755     GADGET_ID_DROP_DELAY_RND_TEXT,      GADGET_ID_DROP_DELAY_FIX_UP,
1756     &custom_element.drop_delay_random,
1757     NULL,                               "+random", NULL
1758   },
1759
1760   // ---------- element settings: configure 2 (custom elements) ---------------
1761
1762   {
1763     ED_COUNTER_ID_MOVE_DELAY_FIX,
1764     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(5),
1765     0,                                  999,
1766     GADGET_ID_MOVE_DELAY_FIX_DOWN,      GADGET_ID_MOVE_DELAY_FIX_UP,
1767     GADGET_ID_MOVE_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1768     &custom_element.move_delay_fixed,
1769     NULL,                               "move delay", NULL
1770   },
1771   {
1772     ED_COUNTER_ID_MOVE_DELAY_RND,
1773     -1,                                 ED_ELEMENT_SETTINGS_YPOS(5),
1774     0,                                  999,
1775     GADGET_ID_MOVE_DELAY_RND_DOWN,      GADGET_ID_MOVE_DELAY_RND_UP,
1776     GADGET_ID_MOVE_DELAY_RND_TEXT,      GADGET_ID_MOVE_DELAY_FIX_UP,
1777     &custom_element.move_delay_random,
1778     NULL,                               "+random", NULL
1779   },
1780   {
1781     ED_COUNTER_ID_STEP_DELAY_FIX,
1782     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(6),
1783     0,                                  999,
1784     GADGET_ID_STEP_DELAY_FIX_DOWN,      GADGET_ID_STEP_DELAY_FIX_UP,
1785     GADGET_ID_STEP_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1786     &custom_element.step_delay_fixed,
1787     NULL,                               "step delay", NULL
1788   },
1789   {
1790     ED_COUNTER_ID_STEP_DELAY_RND,
1791     -1,                                 ED_ELEMENT_SETTINGS_YPOS(6),
1792     0,                                  999,
1793     GADGET_ID_STEP_DELAY_RND_DOWN,      GADGET_ID_STEP_DELAY_RND_UP,
1794     GADGET_ID_STEP_DELAY_RND_TEXT,      GADGET_ID_STEP_DELAY_FIX_UP,
1795     &custom_element.step_delay_random,
1796     NULL,                               "+random", NULL
1797   },
1798   {
1799     ED_COUNTER_ID_EXPLOSION_DELAY,
1800     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(13),
1801     0,                                  999,
1802     GADGET_ID_EXPLOSION_DELAY_DOWN,     GADGET_ID_EXPLOSION_DELAY_UP,
1803     GADGET_ID_EXPLOSION_DELAY_TEXT,     GADGET_ID_NONE,
1804     &custom_element.explosion_delay,
1805     NULL,                               "explosion delay", NULL
1806   },
1807   {
1808     ED_COUNTER_ID_IGNITION_DELAY,
1809     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(14),
1810     0,                                  999,
1811     GADGET_ID_IGNITION_DELAY_DOWN,      GADGET_ID_IGNITION_DELAY_UP,
1812     GADGET_ID_IGNITION_DELAY_TEXT,      GADGET_ID_NONE,
1813     &custom_element.ignition_delay,
1814     NULL,                               "ignition delay", "(by fire)"
1815   },
1816
1817   // ---------- element settings: configure (group elements) ------------------
1818
1819   {
1820     ED_COUNTER_ID_GROUP_CONTENT,
1821     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1822     MIN_ELEMENTS_IN_GROUP,              MAX_ELEMENTS_IN_GROUP,
1823     GADGET_ID_GROUP_CONTENT_DOWN,       GADGET_ID_GROUP_CONTENT_UP,
1824     GADGET_ID_GROUP_CONTENT_TEXT,       GADGET_ID_NONE,
1825     &group_element_info.num_elements,
1826     NULL,                               NULL, "number of elements in group"
1827   },
1828
1829   // ---------- element settings: advanced (custom elements) ------------------
1830
1831   {
1832     ED_COUNTER_ID_CHANGE_DELAY_FIX,
1833     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(2),
1834     0,                                  999,
1835     GADGET_ID_CHANGE_DELAY_FIX_DOWN,    GADGET_ID_CHANGE_DELAY_FIX_UP,
1836     GADGET_ID_CHANGE_DELAY_FIX_TEXT,    GADGET_ID_NONE,
1837     &custom_element_change.delay_fixed,
1838     NULL,                               "CE delay", NULL,
1839   },
1840   {
1841     ED_COUNTER_ID_CHANGE_DELAY_RND,
1842     -1,                                 ED_ELEMENT_SETTINGS_YPOS(2),
1843     0,                                  999,
1844     GADGET_ID_CHANGE_DELAY_RND_DOWN,    GADGET_ID_CHANGE_DELAY_RND_UP,
1845     GADGET_ID_CHANGE_DELAY_RND_TEXT,    GADGET_ID_CHANGE_DELAY_FIX_UP,
1846     &custom_element_change.delay_random,
1847     NULL,                               "+random", NULL
1848   },
1849   {
1850     ED_COUNTER_ID_CHANGE_CONT_RND,
1851     ED_ELEMENT_SETTINGS_XPOS(3),        ED_ELEMENT_SETTINGS_YPOS(12),
1852     0,                                  100,
1853     GADGET_ID_CHANGE_CONT_RND_DOWN,     GADGET_ID_CHANGE_CONT_RND_UP,
1854     GADGET_ID_CHANGE_CONT_RND_TEXT,     GADGET_ID_NONE,
1855     &custom_element_change.random_percentage,
1856     NULL,                               "use random replace:", "%"
1857   },
1858 };
1859
1860 static struct
1861 {
1862   int gadget_type_id;
1863   int x, y;
1864   int gadget_id;
1865   int size;
1866   char *value;
1867   char *text_above, *infotext;
1868 } textinput_info[ED_NUM_TEXTINPUT] =
1869 {
1870   {
1871     ED_TEXTINPUT_ID_LEVEL_NAME,
1872     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(0),
1873     GADGET_ID_LEVEL_NAME,
1874     MAX_LEVEL_NAME_LEN,
1875     level.name,
1876     "Title:", "Title for this level"
1877   },
1878   {
1879     ED_TEXTINPUT_ID_LEVEL_AUTHOR,
1880     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(2),
1881     GADGET_ID_LEVEL_AUTHOR,
1882     MAX_LEVEL_AUTHOR_LEN,
1883     level.author,
1884     "Author:", "Author for this level"
1885   },
1886   {
1887     ED_TEXTINPUT_ID_LEVELSET_NAME,
1888     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(0),
1889     GADGET_ID_LEVELSET_NAME,
1890     MAX_LEVEL_NAME_LEN,
1891     levelset_name,
1892     "Title:", "Title for this or new level set"
1893   },
1894   {
1895     ED_TEXTINPUT_ID_LEVELSET_AUTHOR,
1896     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(2),
1897     GADGET_ID_LEVELSET_AUTHOR,
1898     MAX_LEVEL_AUTHOR_LEN,
1899     levelset_author,
1900     "Author:", "Author for this or new level set"
1901   },
1902   {
1903     ED_TEXTINPUT_ID_ELEMENT_NAME,
1904     -1, -1,     // these values are not constant, but can change at runtime
1905     GADGET_ID_ELEMENT_NAME,
1906     MAX_ELEMENT_NAME_LEN - 2,           // currently 2 chars less editable
1907     custom_element.description,
1908     NULL, "Element name"
1909   }
1910 };
1911
1912 static struct
1913 {
1914   int gadget_type_id;
1915   int x, y;
1916   int gadget_id;
1917   int xsize, ysize;
1918   char *value;
1919   char *text_above, *text_above_cropped, *infotext;
1920 } textarea_info[ED_NUM_TEXTAREAS] =
1921 {
1922   {
1923     ED_TEXTAREA_ID_ENVELOPE_INFO,
1924     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1925     GADGET_ID_ENVELOPE_INFO,
1926     MAX_ENVELOPE_XSIZE, MAX_ENVELOPE_YSIZE,
1927     NULL,
1928     "Envelope Content:", "Envelope Content (cropped):", "Envelope Content"
1929   }
1930 };
1931
1932 static struct ValueTextInfo options_time_or_steps[] =
1933 {
1934   { 0,                          "seconds"                       },
1935   { 1,                          "steps"                         },
1936
1937   { -1,                         NULL                            }
1938 };
1939
1940 static struct ValueTextInfo options_time_score_base[] =
1941 {
1942   { 1,                          "per second/step"               },
1943   { 10,                         "per 10 seconds/steps"          },
1944
1945   { -1,                         NULL                            }
1946 };
1947
1948 static struct ValueTextInfo options_game_engine_type[] =
1949 {
1950   { GAME_ENGINE_TYPE_RND,       "Rocks'n'Diamonds"              },
1951   { GAME_ENGINE_TYPE_BD,        "Boulder Dash"                  },
1952   { GAME_ENGINE_TYPE_EM,        "Emerald Mine"                  },
1953   { GAME_ENGINE_TYPE_SP,        "Supaplex"                      },
1954   { GAME_ENGINE_TYPE_MM,        "Mirror Magic"                  },
1955
1956   { -1,                         NULL                            }
1957 };
1958
1959 static struct ValueTextInfo options_levelset_save_mode[] =
1960 {
1961   { LEVELSET_SAVE_MODE_UPDATE,  "Update this level set"         },
1962   { LEVELSET_SAVE_MODE_CREATE,  "Create new level set"          },
1963
1964   { -1,                         NULL                            }
1965 };
1966
1967 static struct ValueTextInfo options_wind_direction[] =
1968 {
1969   { MV_START_NONE,              "none"                          },
1970   { MV_START_LEFT,              "left"                          },
1971   { MV_START_RIGHT,             "right"                         },
1972   { MV_START_UP,                "up"                            },
1973   { MV_START_DOWN,              "down"                          },
1974
1975   { -1,                         NULL                            }
1976 };
1977
1978 static struct ValueTextInfo options_player_speed[] =
1979 {
1980   { 0,                          "frozen"                        },
1981   { 1,                          "very slow"                     },
1982   { 2,                          "slow"                          },
1983   { 4,                          "normal"                        },
1984   { 8,                          "fast"                          },
1985   { 16,                         "very fast"                     },
1986   { 32,                         "ultrafast"                     },
1987
1988   { -1,                         NULL                            }
1989 };
1990
1991 static struct ValueTextInfo options_access_type[] =
1992 {
1993   { EP_WALKABLE,                "walkable"                      },
1994   { EP_PASSABLE,                "passable"                      },
1995
1996   { -1,                         NULL                            }
1997 };
1998
1999 static struct ValueTextInfo options_access_layer[] =
2000 {
2001   { EP_ACCESSIBLE_OVER,         "over"                          },
2002   { EP_ACCESSIBLE_INSIDE,       "inside"                        },
2003   { EP_ACCESSIBLE_UNDER,        "under"                         },
2004
2005   { -1,                         NULL                            }
2006 };
2007
2008 static struct ValueTextInfo options_access_protected[] =
2009 {
2010   { 0,                          "unprotected"                   },
2011   { 1,                          "protected"                     },
2012
2013   { -1,                         NULL                            }
2014 };
2015
2016 static struct ValueTextInfo options_access_direction[] =
2017 {
2018   { MV_NO_DIRECTION,            "no direction"                  },
2019   { MV_LEFT,                    "left"                          },
2020   { MV_RIGHT,                   "right"                         },
2021   { MV_UP,                      "up"                            },
2022   { MV_DOWN,                    "down"                          },
2023   { MV_LEFT  | MV_UP,           "left + up"                     },
2024   { MV_LEFT  | MV_DOWN,         "left + down"                   },
2025   { MV_RIGHT | MV_UP,           "right + up"                    },
2026   { MV_RIGHT | MV_DOWN,         "right + down"                  },
2027   { MV_HORIZONTAL,              "horizontal"                    },
2028   { MV_VERTICAL,                "vertical"                      },
2029   { MV_HORIZONTAL | MV_UP,      "horizontal + up"               },
2030   { MV_HORIZONTAL | MV_DOWN,    "horizontal + down"             },
2031   { MV_VERTICAL   | MV_LEFT,    "vertical + left"               },
2032   { MV_VERTICAL   | MV_RIGHT,   "vertical + right"              },
2033   { MV_ALL_DIRECTIONS,          "all directions"                },
2034
2035   { -1,                         NULL                            }
2036 };
2037
2038 static struct ValueTextInfo options_walk_to_action[] =
2039 {
2040   { EP_DIGGABLE,                "diggable"                      },
2041   { EP_COLLECTIBLE_ONLY,        "collectible"                   },
2042   { EP_DROPPABLE,               "collectible & droppable"       },
2043   { EP_THROWABLE,               "collectible & throwable"       },
2044   { EP_PUSHABLE,                "pushable"                      },
2045
2046   { -1,                         NULL                            }
2047 };
2048
2049 static struct ValueTextInfo options_move_pattern[] =
2050 {
2051   { MV_LEFT,                    "left"                          },
2052   { MV_RIGHT,                   "right"                         },
2053   { MV_UP,                      "up"                            },
2054   { MV_DOWN,                    "down"                          },
2055   { MV_HORIZONTAL,              "horizontal"                    },
2056   { MV_VERTICAL,                "vertical"                      },
2057   { MV_ALL_DIRECTIONS,          "all directions"                },
2058   { MV_WIND_DIRECTION,          "wind direction"                },
2059   { MV_TOWARDS_PLAYER,          "towards player"                },
2060   { MV_AWAY_FROM_PLAYER,        "away from player"              },
2061   { MV_ALONG_LEFT_SIDE,         "along left side"               },
2062   { MV_ALONG_RIGHT_SIDE,        "along right side"              },
2063   { MV_TURNING_LEFT,            "turning left"                  },
2064   { MV_TURNING_RIGHT,           "turning right"                 },
2065   { MV_TURNING_LEFT_RIGHT,      "turning left, right"           },
2066   { MV_TURNING_RIGHT_LEFT,      "turning right, left"           },
2067   { MV_TURNING_RANDOM,          "turning random"                },
2068   { MV_MAZE_RUNNER,             "maze runner style"             },
2069   { MV_MAZE_HUNTER,             "maze hunter style"             },
2070   { MV_WHEN_PUSHED,             "when pushed"                   },
2071   { MV_WHEN_DROPPED,            "when dropped/thrown"           },
2072
2073   { -1,                         NULL                            }
2074 };
2075
2076 static struct ValueTextInfo options_move_direction[] =
2077 {
2078   { MV_START_AUTOMATIC,         "automatic"                     },
2079   { MV_START_LEFT,              "left"                          },
2080   { MV_START_RIGHT,             "right"                         },
2081   { MV_START_UP,                "up"                            },
2082   { MV_START_DOWN,              "down"                          },
2083   { MV_START_RANDOM,            "random"                        },
2084   { MV_START_PREVIOUS,          "previous"                      },
2085
2086   { -1,                         NULL                            }
2087 };
2088
2089 static struct ValueTextInfo options_move_stepsize[] =
2090 {
2091   { 0,                          "not moving"                    },
2092   { 1,                          "very slow"                     },
2093   { 2,                          "slow"                          },
2094   { 4,                          "normal"                        },
2095   { 8,                          "fast"                          },
2096   { 16,                         "very fast"                     },
2097   { 32,                         "even faster"                   },
2098
2099   { -1,                         NULL                            }
2100 };
2101
2102 static struct ValueTextInfo options_move_leave_type[] =
2103 {
2104   { LEAVE_TYPE_UNLIMITED,       "leave behind"                  },
2105   { LEAVE_TYPE_LIMITED,         "change it to"                  },
2106
2107   { -1,                         NULL                            }
2108 };
2109
2110 static struct ValueTextInfo options_smash_targets[] =
2111 {
2112   { EP_CAN_SMASH_PLAYER,        "player"                        },
2113 #if 0
2114   { EP_CAN_SMASH_ENEMIES,       "enemies"                       },
2115 #endif
2116   { EP_CAN_SMASH_EVERYTHING,    "everything"                    },
2117
2118   { -1,                         NULL                            }
2119 };
2120
2121 static struct ValueTextInfo options_slippery_type[] =
2122 {
2123   { SLIPPERY_ANY_RANDOM,        "random"                        },
2124   { SLIPPERY_ANY_LEFT_RIGHT,    "left, right"                   },
2125   { SLIPPERY_ANY_RIGHT_LEFT,    "right, left"                   },
2126   { SLIPPERY_ONLY_LEFT,         "only left"                     },
2127   { SLIPPERY_ONLY_RIGHT,        "only right"                    },
2128
2129   { -1,                         NULL                            }
2130 };
2131
2132 static struct ValueTextInfo options_deadliness[] =
2133 {
2134   { EP_DONT_RUN_INTO,           "running into"                  },
2135   { EP_DONT_COLLIDE_WITH,       "colliding with"                },
2136   { EP_DONT_GET_HIT_BY,         "getting hit by"                },
2137   { EP_DONT_TOUCH,              "touching"                      },
2138
2139   { -1,                         NULL                            }
2140 };
2141
2142 static struct ValueTextInfo options_explosion_type[] =
2143 {
2144   { EXPLODES_3X3,               "3x3"                           },
2145   { EXPLODES_CROSS,             "3+3"                           },
2146   { EXPLODES_1X1,               "1x1"                           },
2147
2148   { -1,                         NULL                            }
2149 };
2150
2151 static struct ValueTextInfo options_time_units[] =
2152 {
2153   { 1,                          "frames"                        },
2154   { FRAMES_PER_SECOND,          "seconds"                       },
2155
2156   { -1,                         NULL                            }
2157 };
2158
2159 static struct ValueTextInfo options_change_direct_action[] =
2160 {
2161   { CE_TOUCHED_BY_PLAYER,       "touched by player"             },
2162   { CE_PRESSED_BY_PLAYER,       "pressed by player"             },
2163   { CE_SWITCHED_BY_PLAYER,      "switched by player"            },
2164   { CE_SNAPPED_BY_PLAYER,       "snapped by player"             },
2165   { CE_PUSHED_BY_PLAYER,        "pushed by player"              },
2166   { CE_ENTERED_BY_PLAYER,       "entered by player"             },
2167   { CE_LEFT_BY_PLAYER,          "left by player"                },
2168   { CE_DROPPED_BY_PLAYER,       "dropped/thrown by player"      },
2169   { CE_SWITCHED,                "switched"                      },
2170   { CE_HITTING_SOMETHING,       "hitting something"             },
2171   { CE_HIT_BY_SOMETHING,        "hit by something"              },
2172 #if 0
2173   { CE_BLOCKED,                 "blocked"                       },
2174 #endif
2175   { CE_IMPACT,                  "impact (on something)"         },
2176   { CE_SMASHED,                 "smashed (from above)"          },
2177 #if 0
2178   { CE_VALUE_CHANGES,           "CE value changes"              },
2179   { CE_SCORE_CHANGES,           "CE score changes"              },
2180 #endif
2181   { CE_VALUE_GETS_ZERO,         "CE value gets 0"               },
2182   { CE_SCORE_GETS_ZERO,         "CE score gets 0"               },
2183   { CE_UNDEFINED,               " "                             },
2184   { CE_HEADLINE_SPECIAL_EVENTS, "[mouse events]"                },
2185   { CE_CLICKED_BY_MOUSE,        "clicked by mouse"              },
2186   { CE_PRESSED_BY_MOUSE,        "pressed by mouse"              },
2187   { CE_UNDEFINED,               " "                             },
2188   { CE_HEADLINE_SPECIAL_EVENTS, "[static states]"               },
2189   { CE_NEXT_TO_PLAYER,          "next to player"                },
2190
2191   { -1,                         NULL                            }
2192 };
2193
2194 static struct ValueTextInfo options_change_other_action[] =
2195 {
2196   { CE_PLAYER_TOUCHES_X,        "player touches"                },
2197   { CE_PLAYER_PRESSES_X,        "player presses"                },
2198   { CE_PLAYER_SWITCHES_X,       "player switches"               },
2199   { CE_PLAYER_SNAPS_X,          "player snaps"                  },
2200   { CE_PLAYER_PUSHES_X,         "player pushes"                 },
2201   { CE_PLAYER_ENTERS_X,         "player enters"                 },
2202   { CE_PLAYER_LEAVES_X,         "player leaves"                 },
2203   { CE_PLAYER_DIGS_X,           "player digs"                   },
2204   { CE_PLAYER_COLLECTS_X,       "player collects"               },
2205   { CE_PLAYER_DROPS_X,          "player drops/throws"           },
2206   { CE_TOUCHING_X,              "touching"                      },
2207   { CE_HITTING_X,               "hitting"                       },
2208   { CE_DIGGING_X,               "digging"                       },
2209   { CE_HIT_BY_X,                "hit by"                        },
2210   { CE_SWITCH_OF_X,             "switch of"                     },
2211   { CE_CHANGE_OF_X,             "change by page of"             },
2212   { CE_EXPLOSION_OF_X,          "explosion of"                  },
2213   { CE_MOVE_OF_X,               "move of"                       },
2214   { CE_CREATION_OF_X,           "creation of"                   },
2215   { CE_VALUE_CHANGES_OF_X,      "CE value changes of"           },
2216   { CE_SCORE_CHANGES_OF_X,      "CE score changes of"           },
2217   { CE_VALUE_GETS_ZERO_OF_X,    "CE value gets 0 of"            },
2218   { CE_SCORE_GETS_ZERO_OF_X,    "CE score gets 0 of"            },
2219   { CE_UNDEFINED,               " "                             },
2220   { CE_HEADLINE_SPECIAL_EVENTS, "[mouse events]"                },
2221   { CE_MOUSE_CLICKED_ON_X,      "mouse clicked on"              },
2222   { CE_MOUSE_PRESSED_ON_X,      "mouse pressed on"              },
2223   { CE_UNDEFINED,               " "                             },
2224   { CE_HEADLINE_SPECIAL_EVENTS, "[static states]"               },
2225   { CE_PLAYER_NEXT_TO_X,        "player next to"                },
2226   { CE_NEXT_TO_X,               "next to"                       },
2227
2228   { -1,                         NULL                            }
2229 };
2230
2231 static struct ValueTextInfo options_change_trigger_side[] =
2232 {
2233   { CH_SIDE_LEFT,               "left"                          },
2234   { CH_SIDE_RIGHT,              "right"                         },
2235   { CH_SIDE_TOP,                "top"                           },
2236   { CH_SIDE_BOTTOM,             "bottom"                        },
2237   { CH_SIDE_LEFT_RIGHT,         "left/right"                    },
2238   { CH_SIDE_TOP_BOTTOM,         "top/bottom"                    },
2239   { CH_SIDE_ANY,                "any"                           },
2240
2241   { -1,                         NULL                            }
2242 };
2243
2244 static struct ValueTextInfo options_change_trigger_player[] =
2245 {
2246   { CH_PLAYER_1,                "1"                             },
2247   { CH_PLAYER_2,                "2"                             },
2248   { CH_PLAYER_3,                "3"                             },
2249   { CH_PLAYER_4,                "4"                             },
2250   { CH_PLAYER_ANY,              "any"                           },
2251
2252   { -1,                         NULL                            }
2253 };
2254
2255 static struct ValueTextInfo options_change_trigger_page[] =
2256 {
2257   { (1u << 0),                  "1"                             },
2258   { (1u << 1),                  "2"                             },
2259   { (1u << 2),                  "3"                             },
2260   { (1u << 3),                  "4"                             },
2261   { (1u << 4),                  "5"                             },
2262   { (1u << 5),                  "6"                             },
2263   { (1u << 6),                  "7"                             },
2264   { (1u << 7),                  "8"                             },
2265   { (1u << 8),                  "9"                             },
2266   { (1u << 9),                  "10"                            },
2267   { (1u << 10),                 "11"                            },
2268   { (1u << 11),                 "12"                            },
2269   { (1u << 12),                 "13"                            },
2270   { (1u << 13),                 "14"                            },
2271   { (1u << 14),                 "15"                            },
2272   { (1u << 15),                 "16"                            },
2273   { (1u << 16),                 "17"                            },
2274   { (1u << 17),                 "18"                            },
2275   { (1u << 18),                 "19"                            },
2276   { (1u << 19),                 "20"                            },
2277   { (1u << 20),                 "21"                            },
2278   { (1u << 21),                 "22"                            },
2279   { (1u << 22),                 "23"                            },
2280   { (1u << 23),                 "24"                            },
2281   { (1u << 24),                 "25"                            },
2282   { (1u << 25),                 "26"                            },
2283   { (1u << 26),                 "27"                            },
2284   { (1u << 27),                 "28"                            },
2285   { (1u << 28),                 "29"                            },
2286   { (1u << 29),                 "30"                            },
2287   { (1u << 30),                 "31"                            },
2288   { (1u << 31),                 "32"                            },
2289   { CH_PAGE_ANY,                "any"                           },
2290
2291   { -1,                         NULL                            }
2292 };
2293
2294 static struct ValueTextInfo options_change_replace_when[] =
2295 {
2296   { CP_WHEN_EMPTY,              "empty"                         },
2297   { CP_WHEN_WALKABLE,           "walkable"                      },
2298   { CP_WHEN_DIGGABLE,           "diggable"                      },
2299   { CP_WHEN_COLLECTIBLE,        "collectible"                   },
2300   { CP_WHEN_REMOVABLE,          "removable"                     },
2301   { CP_WHEN_DESTRUCTIBLE,       "destructible"                  },
2302
2303   { -1,                         NULL                            }
2304 };
2305
2306 static struct ValueTextInfo options_action_type[] =
2307 {
2308   { CA_NO_ACTION,               "no action"                     },
2309   { CA_UNDEFINED,               " "                             },
2310   { CA_HEADLINE_LEVEL_ACTIONS,  "[level]"                       },
2311   { CA_RESTART_LEVEL,           "restart level"                 },
2312   { CA_SHOW_ENVELOPE,           "show envelope"                 },
2313   { CA_SET_LEVEL_TIME,          "set time"                      },
2314   { CA_SET_LEVEL_SCORE,         "set score"                     },
2315   { CA_SET_LEVEL_GEMS,          "set gems"                      },
2316   { CA_SET_LEVEL_WIND,          "set wind dir."                 },
2317   { CA_SET_LEVEL_RANDOM_SEED,   "set random seed"               },
2318   { CA_UNDEFINED,               " "                             },
2319   { CA_HEADLINE_PLAYER_ACTIONS, "[player]"                      },
2320   { CA_MOVE_PLAYER,             "move player"                   },
2321   { CA_MOVE_PLAYER_NEW,         "move player new"               },
2322   { CA_EXIT_PLAYER,             "exit player"                   },
2323   { CA_KILL_PLAYER,             "kill player"                   },
2324   { CA_SET_PLAYER_KEYS,         "set keys"                      },
2325   { CA_SET_PLAYER_SPEED,        "set speed"                     },
2326   { CA_SET_PLAYER_SHIELD,       "set shield"                    },
2327   { CA_SET_PLAYER_GRAVITY,      "set gravity"                   },
2328   { CA_SET_PLAYER_ARTWORK,      "set artwork"                   },
2329   { CA_SET_PLAYER_INVENTORY,    "set inventory"                 },
2330   { CA_UNDEFINED,               " "                             },
2331   { CA_HEADLINE_CE_ACTIONS,     "[CE]"                          },
2332   { CA_SET_CE_VALUE,            "set CE value"                  },
2333   { CA_SET_CE_SCORE,            "set CE score"                  },
2334   { CA_SET_CE_ARTWORK,          "set CE artwork"                },
2335   { CA_UNDEFINED,               " "                             },
2336   { CA_HEADLINE_ENGINE_ACTIONS, "[engine]"                      },
2337   { CA_SET_ENGINE_SCAN_MODE,    "set scan mode"                 },
2338
2339   { -1,                         NULL                            }
2340 };
2341
2342 static struct ValueTextInfo options_action_mode_none[] =
2343 {
2344   { CA_MODE_UNDEFINED,          " "                             },
2345
2346   { -1,                         NULL                            }
2347 };
2348
2349 static struct ValueTextInfo options_action_mode_assign[] =
2350 {
2351   { CA_MODE_SET,                "="                             },
2352
2353   { -1,                         NULL                            }
2354 };
2355
2356 static struct ValueTextInfo options_action_mode_add_remove[] =
2357 {
2358   { CA_MODE_ADD,                "+"                             },
2359   { CA_MODE_SUBTRACT,           "-"                             },
2360
2361   { -1,                         NULL                            }
2362 };
2363
2364 static struct ValueTextInfo options_action_mode_calculate[] =
2365 {
2366   { CA_MODE_SET,                "="                             },
2367   { CA_MODE_ADD,                "+"                             },
2368   { CA_MODE_SUBTRACT,           "-"                             },
2369   { CA_MODE_MULTIPLY,           "*"                             },
2370   { CA_MODE_DIVIDE,             "/"                             },
2371   { CA_MODE_MODULO,             "%"                             },
2372
2373   { -1,                         NULL                            }
2374 };
2375
2376 static struct ValueTextInfo options_action_arg_none[] =
2377 {
2378   { CA_ARG_UNDEFINED,           "         "                     },
2379
2380   { -1,                         NULL                            }
2381 };
2382
2383 static struct ValueTextInfo options_action_arg_player[] =
2384 {
2385   { CA_ARG_PLAYER_HEADLINE,     "[player]"                      },
2386   { CA_ARG_PLAYER_1,            "1"                             },
2387   { CA_ARG_PLAYER_2,            "2"                             },
2388   { CA_ARG_PLAYER_3,            "3"                             },
2389   { CA_ARG_PLAYER_4,            "4"                             },
2390   { CA_ARG_PLAYER_ANY,          "any"                           },
2391   { CA_ARG_PLAYER_TRIGGER,      "trigger"                       },
2392   { CA_ARG_PLAYER_ACTION,       "action ->"                     },
2393
2394   { -1,                         NULL                            }
2395 };
2396
2397 static struct ValueTextInfo options_action_arg_number[] =
2398 {
2399   { CA_ARG_NUMBER_HEADLINE,     "[number]"                      },
2400   { CA_ARG_0,                   "0"                             },
2401   { CA_ARG_1,                   "1"                             },
2402   { CA_ARG_2,                   "2"                             },
2403   { CA_ARG_3,                   "3"                             },
2404   { CA_ARG_4,                   "4"                             },
2405   { CA_ARG_5,                   "5"                             },
2406   { CA_ARG_10,                  "10"                            },
2407   { CA_ARG_100,                 "100"                           },
2408   { CA_ARG_1000,                "1000"                          },
2409   { CA_ARG_UNDEFINED,           " "                             },
2410   { CA_ARG_NUMBER_MIN,          "min"                           },
2411   { CA_ARG_NUMBER_MAX,          "max"                           },
2412   { CA_ARG_UNDEFINED,           " "                             },
2413   { CA_ARG_NUMBER_RESET,        "reset"                         },
2414   { CA_ARG_UNDEFINED,           " "                             },
2415   { CA_ARG_NUMBER_CE_VALUE,     "CE value"                      },
2416   { CA_ARG_NUMBER_CE_SCORE,     "CE score"                      },
2417   { CA_ARG_NUMBER_CE_DELAY,     "CE delay"                      },
2418   { CA_ARG_UNDEFINED,           " "                             },
2419   { CA_ARG_NUMBER_LEVEL_TIME,   "time"                          },
2420   { CA_ARG_NUMBER_LEVEL_GEMS,   "gems"                          },
2421   { CA_ARG_NUMBER_LEVEL_SCORE,  "score"                         },
2422   { CA_ARG_UNDEFINED,           " "                             },
2423   { CA_ARG_ELEMENT_CV_HEADLINE, "[CE value]"                    },
2424   { CA_ARG_ELEMENT_CV_TARGET,   "target"                        },
2425   { CA_ARG_ELEMENT_CV_TRIGGER,  "trigger"                       },
2426   { CA_ARG_ELEMENT_CV_ACTION,   "action ->"                     },
2427   { CA_ARG_UNDEFINED,           " "                             },
2428   { CA_ARG_ELEMENT_CS_HEADLINE, "[CE score]"                    },
2429   { CA_ARG_ELEMENT_CS_TARGET,   "target"                        },
2430   { CA_ARG_ELEMENT_CS_TRIGGER,  "trigger"                       },
2431   { CA_ARG_ELEMENT_CS_ACTION,   "action ->"                     },
2432
2433   { -1,                         NULL                            }
2434 };
2435
2436 static struct ValueTextInfo options_action_arg_value[] =
2437 {
2438   { CA_ARG_NUMBER_HEADLINE,     "[number]"                      },
2439   { CA_ARG_0,                   "0"                             },
2440   { CA_ARG_1,                   "1"                             },
2441   { CA_ARG_2,                   "2"                             },
2442   { CA_ARG_3,                   "3"                             },
2443   { CA_ARG_4,                   "4"                             },
2444   { CA_ARG_5,                   "5"                             },
2445   { CA_ARG_10,                  "10"                            },
2446   { CA_ARG_100,                 "100"                           },
2447   { CA_ARG_1000,                "1000"                          },
2448   { CA_ARG_UNDEFINED,           " "                             },
2449   { CA_ARG_NUMBER_MIN,          "min"                           },
2450   { CA_ARG_NUMBER_MAX,          "max"                           },
2451   { CA_ARG_UNDEFINED,           " "                             },
2452   { CA_ARG_NUMBER_RESET,        "reset"                         },
2453   { CA_ARG_UNDEFINED,           " "                             },
2454   { CA_ARG_NUMBER_CE_VALUE,     "CE value"                      },
2455   { CA_ARG_NUMBER_CE_SCORE,     "CE score"                      },
2456   { CA_ARG_NUMBER_CE_DELAY,     "CE delay"                      },
2457   { CA_ARG_UNDEFINED,           " "                             },
2458   { CA_ARG_NUMBER_LEVEL_TIME,   "time"                          },
2459   { CA_ARG_NUMBER_LEVEL_GEMS,   "gems"                          },
2460   { CA_ARG_NUMBER_LEVEL_SCORE,  "score"                         },
2461   { CA_ARG_UNDEFINED,           " "                             },
2462   { CA_ARG_ELEMENT_CV_HEADLINE, "[CE value]"                    },
2463   { CA_ARG_ELEMENT_CV_TARGET,   "target"                        },
2464   { CA_ARG_ELEMENT_CV_TRIGGER,  "trigger"                       },
2465   { CA_ARG_ELEMENT_CV_ACTION,   "action ->"                     },
2466   { CA_ARG_UNDEFINED,           " "                             },
2467   { CA_ARG_ELEMENT_CS_HEADLINE, "[CE score]"                    },
2468   { CA_ARG_ELEMENT_CS_TARGET,   "target"                        },
2469   { CA_ARG_ELEMENT_CS_TRIGGER,  "trigger"                       },
2470   { CA_ARG_ELEMENT_CS_ACTION,   "action ->"                     },
2471   { CA_ARG_UNDEFINED,           " "                             },
2472   { CA_ARG_ELEMENT_NR_HEADLINE, "[element]"                     },
2473   { CA_ARG_ELEMENT_NR_TARGET,   "target"                        },
2474   { CA_ARG_ELEMENT_NR_TRIGGER,  "trigger"                       },
2475   { CA_ARG_ELEMENT_NR_ACTION,   "action ->"                     },
2476
2477   { -1,                         NULL                            }
2478 };
2479
2480 static struct ValueTextInfo options_action_arg_envelope[] =
2481 {
2482   { CA_ARG_NUMBER_HEADLINE,     "[number]"                      },
2483   { CA_ARG_1,                   "1"                             },
2484   { CA_ARG_2,                   "2"                             },
2485   { CA_ARG_3,                   "3"                             },
2486   { CA_ARG_4,                   "4"                             },
2487   { CA_ARG_UNDEFINED,           " "                             },
2488   { CA_ARG_ELEMENT_HEADLINE,    "[element]"                     },
2489   { CA_ARG_ELEMENT_TARGET,      "target"                        },
2490   { CA_ARG_ELEMENT_TRIGGER,     "trigger"                       },
2491   { CA_ARG_ELEMENT_ACTION,      "action ->"                     },
2492
2493   { -1,                         NULL                            }
2494 };
2495
2496 static struct ValueTextInfo options_action_arg_key[] =
2497 {
2498   { CA_ARG_NUMBER_HEADLINE,     "[number]"                      },
2499   { CA_ARG_1,                   "1"                             },
2500   { CA_ARG_2,                   "2"                             },
2501   { CA_ARG_3,                   "3"                             },
2502   { CA_ARG_4,                   "4"                             },
2503   { CA_ARG_5,                   "5"                             },
2504   { CA_ARG_6,                   "6"                             },
2505   { CA_ARG_7,                   "7"                             },
2506   { CA_ARG_8,                   "8"                             },
2507   { CA_ARG_UNDEFINED,           " "                             },
2508   { CA_ARG_ELEMENT_HEADLINE,    "[element]"                     },
2509   { CA_ARG_ELEMENT_TARGET,      "target"                        },
2510   { CA_ARG_ELEMENT_TRIGGER,     "trigger"                       },
2511   { CA_ARG_ELEMENT_ACTION,      "action ->"                     },
2512
2513   { -1,                         NULL                            }
2514 };
2515
2516 static struct ValueTextInfo options_action_arg_speed[] =
2517 {
2518   { CA_ARG_SPEED_HEADLINE,      "[speed]"                       },
2519   { CA_ARG_SPEED_NOT_MOVING,    "frozen"                        },
2520   { CA_ARG_SPEED_VERY_SLOW,     "very slow"                     },
2521   { CA_ARG_SPEED_SLOW,          "slow"                          },
2522   { CA_ARG_SPEED_NORMAL,        "normal"                        },
2523   { CA_ARG_SPEED_FAST,          "fast"                          },
2524   { CA_ARG_SPEED_VERY_FAST,     "very fast"                     },
2525   { CA_ARG_SPEED_EVEN_FASTER,   "ultrafast"                     },
2526   { CA_ARG_UNDEFINED,           " "                             },
2527   { CA_ARG_SPEED_SLOWER,        "slower"                        },
2528   { CA_ARG_SPEED_FASTER,        "faster"                        },
2529   { CA_ARG_UNDEFINED,           " "                             },
2530   { CA_ARG_SPEED_RESET,         "reset"                         },
2531
2532   { -1,                         NULL                            }
2533 };
2534
2535 static struct ValueTextInfo options_action_arg_shield[] =
2536 {
2537   { CA_ARG_SHIELD_HEADLINE,     "[shield]"                      },
2538   { CA_ARG_SHIELD_OFF,          "off"                           },
2539   { CA_ARG_SHIELD_NORMAL,       "normal"                        },
2540   { CA_ARG_SHIELD_DEADLY,       "deadly"                        },
2541
2542   { -1,                         NULL                            }
2543 };
2544
2545 static struct ValueTextInfo options_action_arg_artwork[] =
2546 {
2547   { CA_ARG_ELEMENT_HEADLINE,    "[element]"                     },
2548   { CA_ARG_ELEMENT_TARGET,      "target"                        },
2549   { CA_ARG_ELEMENT_TRIGGER,     "trigger"                       },
2550   { CA_ARG_ELEMENT_ACTION,      "action ->"                     },
2551   { CA_ARG_UNDEFINED,           " "                             },
2552   { CA_ARG_ELEMENT_RESET,       "reset"                         },
2553
2554   { -1,                         NULL                            }
2555 };
2556
2557 static struct ValueTextInfo options_action_arg_gravity[] =
2558 {
2559   { CA_ARG_GRAVITY_HEADLINE,    "[gravity]"                     },
2560   { CA_ARG_GRAVITY_ON,          "on"                            },
2561   { CA_ARG_GRAVITY_OFF,         "off"                           },
2562   { CA_ARG_GRAVITY_TOGGLE,      "toggle"                        },
2563
2564   { -1,                         NULL                            }
2565 };
2566
2567 static struct ValueTextInfo options_action_arg_direction[] =
2568 {
2569   { CA_ARG_DIRECTION_HEADLINE,  "[dir.]"                        },
2570   { CA_ARG_DIRECTION_NONE,      "none"                          },
2571   { CA_ARG_DIRECTION_LEFT,      "left"                          },
2572   { CA_ARG_DIRECTION_RIGHT,     "right"                         },
2573   { CA_ARG_DIRECTION_UP,        "up"                            },
2574   { CA_ARG_DIRECTION_DOWN,      "down"                          },
2575   { CA_ARG_DIRECTION_TRIGGER,   "trigger"                       },
2576   { CA_ARG_DIRECTION_TRIGGER_BACK, "-trigger"                   },
2577
2578   { -1,                         NULL                            }
2579 };
2580
2581 static struct ValueTextInfo options_action_arg_scan_mode[] =
2582 {
2583   { CA_ARG_SCAN_MODE_HEADLINE,  "[mode]"                        },
2584   { CA_ARG_SCAN_MODE_NORMAL,    "normal"                        },
2585   { CA_ARG_SCAN_MODE_REVERSE,   "reverse"                       },
2586
2587   { -1,                         NULL                            }
2588 };
2589
2590 static struct ValueTextInfo options_action_arg_inventory[] =
2591 {
2592   { CA_ARG_INVENTORY_HEADLINE,  "[add]"                         },
2593   { CA_ARG_ELEMENT_TARGET,      "+ target"                      },
2594   { CA_ARG_ELEMENT_TRIGGER,     "+ trigger"                     },
2595   { CA_ARG_ELEMENT_ACTION,      "+ action"                      },
2596   { CA_ARG_UNDEFINED,           " "                             },
2597   { CA_ARG_INVENTORY_RM_HEADLINE,"[remove]"                     },
2598   { CA_ARG_INVENTORY_RM_TARGET, "- target"                      },
2599   { CA_ARG_INVENTORY_RM_TRIGGER,"- trigger"                     },
2600   { CA_ARG_INVENTORY_RM_ACTION, "- action"                      },
2601   { CA_ARG_INVENTORY_RM_FIRST,  "- first"                       },
2602   { CA_ARG_INVENTORY_RM_LAST,   "- last"                        },
2603   { CA_ARG_INVENTORY_RM_ALL,    "- all"                         },
2604   { CA_ARG_UNDEFINED,           " "                             },
2605   { CA_ARG_INVENTORY_RESET,     "reset"                         },
2606
2607   { -1,                         NULL                            }
2608 };
2609
2610 static char options_change_page_strings[MAX_CHANGE_PAGES][10];
2611 static struct ValueTextInfo options_change_page[MAX_CHANGE_PAGES + 1] =
2612 {
2613   { -1,                         NULL                            }
2614 };
2615
2616 static struct ValueTextInfo options_group_choice_mode[] =
2617 {
2618   { ANIM_RANDOM,                "random"                        },
2619   { ANIM_LOOP,                  "loop"                          },
2620   { ANIM_LINEAR,                "linear"                        },
2621   { ANIM_PINGPONG,              "pingpong"                      },
2622   { ANIM_PINGPONG2,             "pingpong 2"                    },
2623   { ANIM_LEVEL_NR,              "level number"                  },
2624
2625   { -1,                         NULL                            }
2626 };
2627
2628 static struct ValueTextInfo options_bd_scheduling_type[] =
2629 {
2630   { GD_SCHEDULING_MILLISECONDS,   "Milliseconds"                },
2631   { GD_SCHEDULING_BD1,            "BD1"                         },
2632   { GD_SCHEDULING_BD2,            "BD2"                         },
2633   { GD_SCHEDULING_PLCK,           "Construction Kit"            },
2634   { GD_SCHEDULING_CRDR,           "Crazy Dream 7"               },
2635   { GD_SCHEDULING_BD1_ATARI,      "Atari BD1"                   },
2636   { GD_SCHEDULING_BD2_PLCK_ATARI, "Atari BD2 / PLCK"            },
2637
2638   { -1,                           NULL                          }
2639 };
2640
2641 static struct ValueTextInfo *action_arg_modes[] =
2642 {
2643   options_action_mode_none,
2644   options_action_mode_assign,
2645   options_action_mode_add_remove,
2646   options_action_mode_calculate,
2647 };
2648
2649 static struct
2650 {
2651   int value;
2652   int mode;
2653   struct ValueTextInfo *options;
2654 }
2655 action_arg_options[] =
2656 {
2657   { CA_NO_ACTION,               0,      options_action_arg_none,        },
2658   { CA_EXIT_PLAYER,             0,      options_action_arg_player,      },
2659   { CA_KILL_PLAYER,             0,      options_action_arg_player,      },
2660   { CA_MOVE_PLAYER,             0,      options_action_arg_direction,   },
2661   { CA_MOVE_PLAYER_NEW,         0,      options_action_arg_direction,   },
2662   { CA_RESTART_LEVEL,           0,      options_action_arg_none,        },
2663   { CA_SHOW_ENVELOPE,           0,      options_action_arg_envelope,    },
2664   { CA_SET_LEVEL_TIME,          3,      options_action_arg_number,      },
2665   { CA_SET_LEVEL_GEMS,          3,      options_action_arg_number,      },
2666   { CA_SET_LEVEL_SCORE,         3,      options_action_arg_number,      },
2667   { CA_SET_LEVEL_WIND,          1,      options_action_arg_direction,   },
2668   { CA_SET_LEVEL_RANDOM_SEED,   1,      options_action_arg_number,      },
2669   { CA_SET_PLAYER_KEYS,         2,      options_action_arg_key,         },
2670   { CA_SET_PLAYER_SPEED,        1,      options_action_arg_speed,       },
2671   { CA_SET_PLAYER_SHIELD,       1,      options_action_arg_shield,      },
2672   { CA_SET_PLAYER_GRAVITY,      1,      options_action_arg_gravity,     },
2673   { CA_SET_PLAYER_ARTWORK,      1,      options_action_arg_artwork,     },
2674   { CA_SET_PLAYER_INVENTORY,    0,      options_action_arg_inventory,   },
2675   { CA_SET_CE_VALUE,            3,      options_action_arg_value,       },
2676   { CA_SET_CE_SCORE,            3,      options_action_arg_value,       },
2677   { CA_SET_CE_ARTWORK,          1,      options_action_arg_artwork,     },
2678   { CA_SET_ENGINE_SCAN_MODE,    1,      options_action_arg_scan_mode,   },
2679
2680   { -1,                         FALSE,  NULL                            }
2681 };
2682
2683 static struct
2684 {
2685   int gadget_type_id;
2686   int x, y;
2687   int gadget_id;
2688   int gadget_id_align;
2689   int size;     // char size of selectbox or '-1' (dynamically determined)
2690   struct ValueTextInfo *options;
2691   int *value;
2692   char *text_above, *text_left, *text_right, *infotext;
2693 } selectbox_info[ED_NUM_SELECTBOX] =
2694 {
2695   // ---------- level and editor settings -------------------------------------
2696
2697   {
2698     ED_SELECTBOX_ID_TIME_OR_STEPS,
2699     -1,                                 ED_LEVEL_SETTINGS_YPOS(8),
2700     GADGET_ID_TIME_OR_STEPS,            GADGET_ID_LEVEL_TIMELIMIT_UP,
2701     -1,
2702     options_time_or_steps,
2703     &level.use_step_counter,
2704     NULL, NULL, "(0 => no limit)",      "time or step limit"
2705   },
2706   {
2707     ED_SELECTBOX_ID_TIME_SCORE_BASE,
2708     -1,                                 ED_LEVEL_SETTINGS_YPOS(10),
2709     GADGET_ID_TIME_SCORE_BASE,          GADGET_ID_LEVEL_TIMESCORE_UP,
2710     -1,
2711     options_time_score_base,
2712     &level.time_score_base,
2713     NULL, NULL, NULL,                   "time score for 1 or 10 seconds/steps"
2714   },
2715   {
2716     ED_SELECTBOX_ID_GAME_ENGINE_TYPE,
2717     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(12),
2718     GADGET_ID_GAME_ENGINE_TYPE,         GADGET_ID_NONE,
2719     -1,
2720     options_game_engine_type,
2721     &level.game_engine_type,
2722     NULL, "game engine:", NULL,         "game engine"
2723   },
2724   {
2725     ED_SELECTBOX_ID_BD_SCHEDULING_TYPE,
2726     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(1),
2727     GADGET_ID_BD_SCHEDULING_TYPE,       GADGET_ID_NONE,
2728     -1,
2729     options_bd_scheduling_type,
2730     &level.bd_scheduling_type,
2731     NULL, "scheduling type:", NULL,     "level timing"
2732   },
2733   {
2734     ED_SELECTBOX_ID_LEVELSET_SAVE_MODE,
2735     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(10),
2736     GADGET_ID_LEVELSET_SAVE_MODE,       GADGET_ID_NONE,
2737     -1,
2738     options_levelset_save_mode,
2739     &levelset_save_mode,
2740     "Action:", NULL, NULL,              "action when saving level set"
2741   },
2742
2743   // ---------- element settings: configure (several elements) ----------------
2744
2745   {
2746     ED_SELECTBOX_ID_WIND_DIRECTION,
2747     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
2748     GADGET_ID_WIND_DIRECTION,           GADGET_ID_NONE,
2749     -1,
2750     options_wind_direction,
2751     &level.wind_direction_initial,
2752     NULL, "initial wind direction:", NULL,      "initial wind direction"
2753   },
2754   {
2755     ED_SELECTBOX_ID_PLAYER_SPEED,
2756     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(7),
2757     GADGET_ID_PLAYER_SPEED,             GADGET_ID_NONE,
2758     -1,
2759     options_player_speed,
2760     &level.initial_player_stepsize[0],
2761     NULL, "initial player speed:", NULL,        "initial player speed"
2762   },
2763   {
2764     ED_SELECTBOX_ID_MM_BALL_CHOICE_MODE,
2765     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
2766     GADGET_ID_MM_BALL_CHOICE_MODE,      GADGET_ID_NONE,
2767     -1,
2768     options_group_choice_mode,
2769     &level.mm_ball_choice_mode,
2770     NULL, "choice type:", NULL,         "type of content choice"
2771   },
2772
2773   // ---------- element settings: configure 1 (custom elements) ---------------
2774
2775   {
2776     ED_SELECTBOX_ID_CUSTOM_ACCESS_TYPE,
2777     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(2),
2778     GADGET_ID_CUSTOM_ACCESS_TYPE,       GADGET_ID_NONE,
2779     -1,
2780     options_access_type,
2781     &custom_element.access_type,
2782     NULL, NULL, NULL,                   "type of access to this field"
2783   },
2784   {
2785     ED_SELECTBOX_ID_CUSTOM_ACCESS_LAYER,
2786     -1,                                 ED_ELEMENT_SETTINGS_YPOS(2),
2787     GADGET_ID_CUSTOM_ACCESS_LAYER,      GADGET_ID_CUSTOM_ACCESS_TYPE,
2788     -1,
2789     options_access_layer,
2790     &custom_element.access_layer,
2791     NULL, NULL, NULL,                   "layer of access for this field"
2792   },
2793   {
2794     ED_SELECTBOX_ID_CUSTOM_ACCESS_PROTECTED,
2795     -1,                                 ED_ELEMENT_SETTINGS_YPOS(2),
2796     GADGET_ID_CUSTOM_ACCESS_PROTECTED,  GADGET_ID_CUSTOM_ACCESS_LAYER,
2797     -1,
2798     options_access_protected,
2799     &custom_element.access_protected,
2800     NULL, NULL, NULL,                   "protected access for this field"
2801   },
2802   {
2803     ED_SELECTBOX_ID_CUSTOM_ACCESS_DIRECTION,
2804     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(3),
2805     GADGET_ID_CUSTOM_ACCESS_DIRECTION,  GADGET_ID_NONE,
2806     -1,
2807     options_access_direction,
2808     &custom_element.access_direction,
2809     NULL, "from", NULL,                 "access direction for this field"
2810   },
2811   {
2812     ED_SELECTBOX_ID_CUSTOM_WALK_TO_ACTION,
2813     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(4),
2814     GADGET_ID_CUSTOM_WALK_TO_ACTION,    GADGET_ID_NONE,
2815     -1,
2816     options_walk_to_action,
2817     &custom_element.walk_to_action,
2818     NULL, NULL, NULL,                   "diggable/collectible/pushable"
2819   },
2820
2821   // ---------- element settings: configure 2 (custom elements) ---------------
2822
2823   {
2824     ED_SELECTBOX_ID_CUSTOM_MOVE_PATTERN,
2825     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(1),
2826     GADGET_ID_CUSTOM_MOVE_PATTERN,      GADGET_ID_NONE,
2827     -1,
2828     options_move_pattern,
2829     &custom_element.move_pattern,
2830     NULL, "can move", NULL,             "element move pattern"
2831   },
2832   {
2833     ED_SELECTBOX_ID_CUSTOM_MOVE_DIRECTION,
2834     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(2),
2835     GADGET_ID_CUSTOM_MOVE_DIRECTION,    GADGET_ID_NONE,
2836     -1,
2837     options_move_direction,
2838     &custom_element.move_direction_initial,
2839     NULL, "starts moving", NULL,        "initial element move direction"
2840   },
2841   {
2842     ED_SELECTBOX_ID_CUSTOM_MOVE_STEPSIZE,
2843     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(4),
2844     GADGET_ID_CUSTOM_MOVE_STEPSIZE,     GADGET_ID_NONE,
2845     -1,
2846     options_move_stepsize,
2847     &custom_element.move_stepsize,
2848     NULL, "move/fall speed", NULL,      "speed of element movement"
2849   },
2850   {
2851     ED_SELECTBOX_ID_CUSTOM_MOVE_LEAVE_TYPE,
2852     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(3),
2853     GADGET_ID_CUSTOM_MOVE_LEAVE_TYPE,   GADGET_ID_NONE,
2854     -1,
2855     options_move_leave_type,
2856     &custom_element.move_leave_type,
2857     // left text with leading spaces to place gadget next to "can dig" gadget
2858     // (needed because drawing area gadgets created after selectbox gadgets)
2859     // NULL, "can dig:    can", ":",    "leave behind or change element"
2860     NULL, "            can", ":",       "leave behind or change element"
2861   },
2862   {
2863     ED_SELECTBOX_ID_CUSTOM_SMASH_TARGETS,
2864     -1,                                 ED_ELEMENT_SETTINGS_YPOS(8),
2865     GADGET_ID_CUSTOM_SMASH_TARGETS,     GADGET_ID_CUSTOM_CAN_SMASH,
2866     -1,
2867     options_smash_targets,
2868     &custom_element.smash_targets,
2869     NULL, "can smash", NULL,            "elements that can be smashed"
2870   },
2871   {
2872     ED_SELECTBOX_ID_CUSTOM_SLIPPERY_TYPE,
2873     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(9),
2874     GADGET_ID_CUSTOM_SLIPPERY_TYPE,     GADGET_ID_NONE,
2875     -1,
2876     options_slippery_type,
2877     &custom_element.slippery_type,
2878     NULL, "slippery", NULL,             "where other elements fall down"
2879   },
2880   {
2881     ED_SELECTBOX_ID_CUSTOM_DEADLINESS,
2882     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(10),
2883     GADGET_ID_CUSTOM_DEADLINESS,        GADGET_ID_NONE,
2884     -1,
2885     options_deadliness,
2886     &custom_element.deadliness,
2887     NULL, "deadly when", NULL,          "deadliness of element"
2888   },
2889   {
2890     ED_SELECTBOX_ID_CUSTOM_EXPLOSION_TYPE,
2891     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(11),
2892     GADGET_ID_CUSTOM_EXPLOSION_TYPE,    GADGET_ID_NONE,
2893     -1,
2894     options_explosion_type,
2895     &custom_element.explosion_type,
2896     NULL, "can explode", NULL,          "explosion type"
2897   },
2898
2899   // ---------- element settings: advanced (custom elements) ------------------
2900
2901   {
2902     ED_SELECTBOX_ID_CHANGE_TIME_UNITS,
2903     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(3),
2904     GADGET_ID_CHANGE_TIME_UNITS,        GADGET_ID_NONE,
2905     -1,
2906     options_time_units,
2907     &custom_element_change.delay_frames,
2908     NULL, "delay time given in", NULL,  "delay time units for change"
2909   },
2910   {
2911     ED_SELECTBOX_ID_CHANGE_DIRECT_ACTION,
2912     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(4),
2913     GADGET_ID_CHANGE_DIRECT_ACTION,     GADGET_ID_NONE,
2914     -1,
2915     options_change_direct_action,
2916     &custom_element_change.direct_action,
2917     NULL, NULL, NULL,                   "type of direct action"
2918   },
2919   {
2920     ED_SELECTBOX_ID_CHANGE_OTHER_ACTION,
2921     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(5),
2922     GADGET_ID_CHANGE_OTHER_ACTION,      GADGET_ID_NONE,
2923     -1,
2924     options_change_other_action,
2925     &custom_element_change.other_action,
2926     NULL, NULL, "element:",             "type of other element action"
2927   },
2928   {
2929     ED_SELECTBOX_ID_CHANGE_SIDE,
2930     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(6),
2931     GADGET_ID_CHANGE_SIDE,              GADGET_ID_NONE,
2932     -1,
2933     options_change_trigger_side,
2934     &custom_element_change.trigger_side,
2935     NULL, "at", "side",                 "element side triggering change"
2936   },
2937   {
2938     ED_SELECTBOX_ID_CHANGE_PLAYER,
2939     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(7),
2940     GADGET_ID_CHANGE_PLAYER,            GADGET_ID_NONE,
2941     -1,
2942     options_change_trigger_player,
2943     &custom_element_change.trigger_player,
2944     NULL, "player:", " ",               "player that causes change"
2945   },
2946   {
2947     ED_SELECTBOX_ID_CHANGE_PAGE,
2948     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(7),
2949     GADGET_ID_CHANGE_PAGE,              GADGET_ID_CHANGE_PLAYER,
2950     -1,
2951     options_change_trigger_page,
2952     &custom_element_change.trigger_page,
2953     NULL, "page:", NULL,                "change page that causes change"
2954   },
2955   {
2956     ED_SELECTBOX_ID_CHANGE_REPLACE_WHEN,
2957     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(10),
2958     GADGET_ID_CHANGE_REPLACE_WHEN,      GADGET_ID_NONE,
2959     -1,
2960     options_change_replace_when,
2961     &custom_element_change.replace_when,
2962     NULL, "replace when", NULL,         "which elements can be replaced"
2963   },
2964   {
2965     ED_SELECTBOX_ID_ACTION_TYPE,
2966     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(13),
2967     GADGET_ID_ACTION_TYPE,              GADGET_ID_NONE,
2968     15,
2969     options_action_type,
2970     &custom_element_change.action_type,
2971     NULL, NULL, NULL,                   "action on specified condition"
2972   },
2973   {
2974     ED_SELECTBOX_ID_ACTION_MODE,
2975     -1,                                 ED_ELEMENT_SETTINGS_YPOS(13),
2976     GADGET_ID_ACTION_MODE,              GADGET_ID_ACTION_TYPE,
2977     -1,
2978     options_action_mode_none,
2979     &custom_element_change.action_mode,
2980     NULL, NULL, NULL,                   "action operator"
2981   },
2982   {
2983     ED_SELECTBOX_ID_ACTION_ARG,
2984     -1,                                 ED_ELEMENT_SETTINGS_YPOS(13),
2985     GADGET_ID_ACTION_ARG,               GADGET_ID_ACTION_MODE,
2986     -1,
2987     options_action_arg_none,
2988     &custom_element_change.action_arg,
2989     NULL, NULL, NULL,                   "action parameter"
2990   },
2991   {
2992     ED_SELECTBOX_ID_SELECT_CHANGE_PAGE,
2993     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(14),
2994     GADGET_ID_SELECT_CHANGE_PAGE,       GADGET_ID_NONE,
2995     3,
2996     options_change_page,
2997     &custom_element.current_change_page,
2998     NULL, NULL, NULL,                   "element change page"
2999   },
3000
3001   // ---------- element settings: configure (group elements) ------------------
3002
3003   {
3004     ED_SELECTBOX_ID_GROUP_CHOICE_MODE,
3005     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
3006     GADGET_ID_GROUP_CHOICE_MODE,        GADGET_ID_NONE,
3007     -1,
3008     options_group_choice_mode,
3009     &group_element_info.choice_mode,
3010     NULL, "choice type:", NULL,         "type of group element choice"
3011   },
3012 };
3013
3014 static struct
3015 {
3016   int gadget_type_id;
3017   int x, y;
3018   int gadget_id;
3019   int gadget_id_align;
3020   int size;
3021   char *text;
3022   char *text_above, *text_left, *text_right, *infotext;
3023 } textbutton_info[ED_NUM_TEXTBUTTONS] =
3024 {
3025   // ---------- level and editor settings (tabs) ------------------------------
3026
3027   {
3028     ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL,
3029     ED_LEVEL_TABS_XPOS(0),              ED_LEVEL_TABS_YPOS(0),
3030     GADGET_ID_LEVELCONFIG_LEVEL,        GADGET_ID_NONE,
3031     8,                                  "Level",
3032     NULL, NULL, NULL,                   "Configure level settings"
3033   },
3034   {
3035     ED_TEXTBUTTON_ID_LEVELCONFIG_LEVELSET,
3036     -1,                                 -1,
3037     GADGET_ID_LEVELCONFIG_LEVELSET,     GADGET_ID_LEVELCONFIG_LEVEL,
3038     8,                                  "Levelset",
3039     NULL, NULL, NULL,                   "Update this or create new level set"
3040   },
3041   {
3042     ED_TEXTBUTTON_ID_LEVELCONFIG_EDITOR,
3043     -1,                                 -1,
3044     GADGET_ID_LEVELCONFIG_EDITOR,       GADGET_ID_LEVELCONFIG_LEVELSET,
3045     8,                                  "Editor",
3046     NULL, NULL, NULL,                   "Configure editor settings"
3047   },
3048   {
3049     ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE,
3050     -1,                                 -1,
3051     GADGET_ID_LEVELCONFIG_ENGINE,       GADGET_ID_LEVELCONFIG_EDITOR,
3052     8,                                  "Engine",
3053     NULL, NULL, NULL,                   "Configure engine settings"
3054   },
3055
3056   // ---------- element settings (tabs) ---------------------------------------
3057
3058   {
3059     ED_TEXTBUTTON_ID_PROPERTIES_INFO,
3060     ED_ELEMENT_TABS_XPOS(0),            ED_ELEMENT_TABS_YPOS(0),
3061     GADGET_ID_PROPERTIES_INFO,          GADGET_ID_NONE,
3062     8,                                  "Info",
3063     NULL, NULL, NULL,                   "Show information about element"
3064   },
3065   {
3066     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG,
3067     -1,                                 -1,
3068     GADGET_ID_PROPERTIES_CONFIG,        GADGET_ID_PROPERTIES_INFO,
3069     8,                                  "Config",
3070     NULL, NULL, NULL,                   "Configure element properties"
3071   },
3072   {
3073     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_1,
3074     -1,                                 -1,
3075     GADGET_ID_PROPERTIES_CONFIG_1,      GADGET_ID_PROPERTIES_INFO,
3076     8,                                  "Config 1",
3077     NULL, NULL, NULL,                   "Configure element properties, part 1"
3078   },
3079   {
3080     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2,
3081     -1,                                 -1,
3082     GADGET_ID_PROPERTIES_CONFIG_2,      GADGET_ID_PROPERTIES_CONFIG_1,
3083     8,                                  "Config 2",
3084     NULL, NULL, NULL,                   "Configure element properties, part 2"
3085   },
3086   {
3087     ED_TEXTBUTTON_ID_PROPERTIES_CHANGE,
3088     -1,                                 -1,
3089     GADGET_ID_PROPERTIES_CHANGE,        GADGET_ID_PROPERTIES_CONFIG_2,
3090     8,                                  "Change",
3091     NULL, NULL, NULL,                   "Configure custom element change pages"
3092   },
3093
3094   // ---------- level and editor settings (buttons) ---------------------------
3095
3096   {
3097     ED_TEXTBUTTON_ID_SAVE_LEVELSET,
3098     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(10),
3099     GADGET_ID_SAVE_LEVELSET,            GADGET_ID_LEVELSET_SAVE_MODE,
3100     -1,                                 "Save",
3101     NULL, NULL, NULL,                   "Update or create level set"
3102   },
3103   {
3104     ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2,
3105     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(6),
3106     GADGET_ID_SAVE_AS_TEMPLATE_2,       GADGET_ID_NONE,
3107     -1,                                 "Save",
3108     NULL, NULL,                         "this level as level template",
3109     "Save current settings as new template"
3110   },
3111
3112   // ---------- element settings (buttons) ------------------------------------
3113
3114   {
3115     ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1,
3116     -1,                                 -1,
3117     GADGET_ID_SAVE_AS_TEMPLATE_1,       GADGET_ID_CUSTOM_USE_TEMPLATE_1,
3118     -1,                                 "Save",
3119     NULL, " ",                          "As Template",
3120     "Save current settings as new template"
3121   },
3122   {
3123     ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE,
3124     -1,                                 -1,
3125     GADGET_ID_ADD_CHANGE_PAGE,          GADGET_ID_PASTE_CHANGE_PAGE,
3126     -1,                                 "New",
3127     NULL, NULL, NULL,                   "Add new change page"
3128   },
3129   {
3130     ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE,
3131     -1,                                 -1,
3132     GADGET_ID_DEL_CHANGE_PAGE,          GADGET_ID_ADD_CHANGE_PAGE,
3133     -1,                                 "Delete",
3134     NULL, NULL, NULL,                   "Delete current change page"
3135   },
3136 };
3137
3138 static struct
3139 {
3140   int gadget_type_id;
3141   int graphic;
3142   int x, y;
3143   int gadget_id;
3144   int gadget_id_align;
3145   char *text_left, *text_right, *infotext;
3146 } graphicbutton_info[ED_NUM_GRAPHICBUTTONS] =
3147 {
3148   {
3149     ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE,
3150     IMG_EDITOR_COUNTER_DOWN,
3151     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(14),
3152     GADGET_ID_PREV_CHANGE_PAGE,         GADGET_ID_NONE,
3153     NULL, NULL,                         "select previous change page"
3154   },
3155   {
3156     ED_GRAPHICBUTTON_ID_NEXT_CHANGE_PAGE,
3157     IMG_EDITOR_COUNTER_UP,
3158     -1,                                 ED_ELEMENT_SETTINGS_YPOS(14),
3159     GADGET_ID_NEXT_CHANGE_PAGE,         GADGET_ID_SELECT_CHANGE_PAGE,
3160     NULL, "change page",                "select next change page"
3161   },
3162   {
3163     ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE,
3164     IMG_GFX_EDITOR_BUTTON_CP_COPY,
3165     -1,                                 ED_ELEMENT_SETTINGS_YPOS(14),
3166     GADGET_ID_COPY_CHANGE_PAGE,         GADGET_ID_NEXT_CHANGE_PAGE,
3167     " ", NULL,                          "copy settings from this change page"
3168   },
3169   {
3170     ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE,
3171     IMG_GFX_EDITOR_BUTTON_CP_PASTE,
3172     -1,                                 ED_ELEMENT_SETTINGS_YPOS(14),
3173     GADGET_ID_PASTE_CHANGE_PAGE,        GADGET_ID_COPY_CHANGE_PAGE,
3174     NULL, NULL,                         "paste settings to this change page"
3175   },
3176 };
3177
3178 static struct
3179 {
3180   int x, y;
3181 } scrollbutton_pos[ED_NUM_SCROLLBUTTONS];
3182
3183 static struct
3184 {
3185   int gadget_type_id;
3186   int graphic;
3187   int gadget_id;
3188   char *infotext;
3189 } scrollbutton_info[ED_NUM_SCROLLBUTTONS] =
3190 {
3191   {
3192     ED_SCROLLBUTTON_ID_AREA_UP,
3193     IMG_EDITOR_PLAYFIELD_SCROLL_UP,
3194     GADGET_ID_SCROLL_UP,
3195     "scroll level editing area up"
3196   },
3197   {
3198     ED_SCROLLBUTTON_ID_AREA_DOWN,
3199     IMG_EDITOR_PLAYFIELD_SCROLL_DOWN,
3200     GADGET_ID_SCROLL_DOWN,
3201     "scroll level editing area down"
3202   },
3203   {
3204     ED_SCROLLBUTTON_ID_AREA_LEFT,
3205     IMG_EDITOR_PLAYFIELD_SCROLL_LEFT,
3206     GADGET_ID_SCROLL_LEFT,
3207     "scroll level editing area left"
3208   },
3209   {
3210     ED_SCROLLBUTTON_ID_AREA_RIGHT,
3211     IMG_EDITOR_PLAYFIELD_SCROLL_RIGHT,
3212     GADGET_ID_SCROLL_RIGHT,
3213     "scroll level editing area right"
3214   },
3215   {
3216     ED_SCROLLBUTTON_ID_LIST_UP,
3217     IMG_EDITOR_PALETTE_SCROLL_UP,
3218     GADGET_ID_SCROLL_LIST_UP,
3219     "scroll element list up ('Page Up')"
3220   },
3221   {
3222     ED_SCROLLBUTTON_ID_LIST_DOWN,
3223     IMG_EDITOR_PALETTE_SCROLL_DOWN,
3224     GADGET_ID_SCROLL_LIST_DOWN,
3225     "scroll element list down ('Page Down')"
3226   },
3227 };
3228
3229 static struct
3230 {
3231   int x, y;
3232   int width, height;
3233   int wheel_x, wheel_y;
3234   int wheel_width, wheel_height;
3235 } scrollbar_pos[ED_NUM_SCROLLBARS];
3236
3237 static struct
3238 {
3239   int gadget_type_id;
3240   int graphic;
3241   int type;
3242   int gadget_id;
3243   char *infotext;
3244 } scrollbar_info[ED_NUM_SCROLLBARS] =
3245 {
3246   {
3247     ED_SCROLLBAR_ID_AREA_HORIZONTAL,
3248     IMG_EDITOR_PLAYFIELD_SCROLLBAR,
3249     GD_TYPE_SCROLLBAR_HORIZONTAL,
3250     GADGET_ID_SCROLL_HORIZONTAL,
3251     "scroll level editing area horizontally"
3252   },
3253   {
3254     ED_SCROLLBAR_ID_AREA_VERTICAL,
3255     IMG_EDITOR_PLAYFIELD_SCROLLBAR,
3256     GD_TYPE_SCROLLBAR_VERTICAL,
3257     GADGET_ID_SCROLL_VERTICAL,
3258     "scroll level editing area vertically"
3259   },
3260   {
3261     ED_SCROLLBAR_ID_LIST_VERTICAL,
3262     IMG_EDITOR_PALETTE_SCROLLBAR,
3263     GD_TYPE_SCROLLBAR_VERTICAL,
3264     GADGET_ID_SCROLL_LIST_VERTICAL,
3265     "scroll element list vertically"
3266   }
3267 };
3268
3269
3270 static struct
3271 {
3272   int gadget_type_id;
3273   int x, y;
3274   int gadget_id;
3275   int gadget_id_align;
3276   int radio_button_nr;
3277   int *value;
3278   int checked_value;
3279   char *text_left, *text_right, *infotext;
3280 } radiobutton_info[ED_NUM_RADIOBUTTONS] =
3281 {
3282   {
3283     ED_RADIOBUTTON_ID_PERCENTAGE,
3284     -1,                                 ED_LEVEL_SETTINGS_YPOS(0),
3285     GADGET_ID_RANDOM_PERCENTAGE,        GADGET_ID_LEVEL_RANDOM_UP,
3286     RADIO_NR_RANDOM_ELEMENTS,
3287     &random_placement_method,           RANDOM_USE_PERCENTAGE,
3288     " ", "percentage",                  "use percentage for random elements"
3289   },
3290   {
3291     ED_RADIOBUTTON_ID_QUANTITY,
3292     -1,                                 ED_LEVEL_SETTINGS_YPOS(0),
3293     GADGET_ID_RANDOM_QUANTITY,          GADGET_ID_RANDOM_PERCENTAGE,
3294     RADIO_NR_RANDOM_ELEMENTS,
3295     &random_placement_method,           RANDOM_USE_QUANTITY,
3296     " ", "quantity",                    "use quantity for random elements"
3297   }
3298 };
3299
3300 static struct
3301 {
3302   int gadget_type_id;
3303   int x, y;
3304   int gadget_id;
3305   int gadget_id_align;
3306   boolean *value;
3307   char *text_above, *text_left, *text_right, *infotext;
3308 } checkbutton_info[ED_NUM_CHECKBUTTONS] =
3309 {
3310   // ---------- level and editor settings -------------------------------------
3311
3312   {
3313     ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS,
3314     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(6),
3315     GADGET_ID_AUTO_COUNT_GEMS,          GADGET_ID_NONE,
3316     &level.auto_count_gems,
3317     NULL, NULL,
3318     "automatically count gems needed",  "set counter to number of gems"
3319   },
3320   {
3321     ED_CHECKBUTTON_ID_RATE_TIME_OVER_SCORE,
3322     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(11),
3323     GADGET_ID_RATE_TIME_OVER_SCORE,     GADGET_ID_NONE,
3324     &level.rate_time_over_score,
3325     NULL, NULL,
3326     "rate time/steps used over score",  "sort high scores by playing time/steps"
3327   },
3328   {
3329     ED_CHECKBUTTON_ID_USE_LEVELSET_ARTWORK,
3330     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(7),
3331     GADGET_ID_USE_LEVELSET_ARTWORK,     GADGET_ID_NONE,
3332     &levelset_use_levelset_artwork,
3333     NULL, NULL,
3334     "use current custom artwork",       "use custom artwork of this level set"
3335   },
3336   {
3337     ED_CHECKBUTTON_ID_COPY_LEVEL_TEMPLATE,
3338     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(8),
3339     GADGET_ID_COPY_LEVEL_TEMPLATE,      GADGET_ID_NONE,
3340     &levelset_copy_level_template,
3341     NULL, NULL,
3342     "copy current level template",      "copy level template of this level set"
3343   },
3344   {
3345     ED_CHECKBUTTON_ID_RANDOM_RESTRICTED,
3346     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(1),
3347     GADGET_ID_RANDOM_RESTRICTED,        GADGET_ID_NONE,
3348     &random_placement_background_restricted,
3349     NULL, NULL,
3350     "restrict random placement to:",    "set random placement restriction"
3351   },
3352   {
3353     ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_3,
3354     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
3355     GADGET_ID_CUSTOM_USE_TEMPLATE_3,    GADGET_ID_NONE,
3356     &setup.editor.use_template_for_new_levels,
3357     "Template for new levels and CE/GE:", NULL,
3358     "use template for new levels",      "use template for level properties"
3359   },
3360   {
3361     ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2,
3362     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(5),
3363     GADGET_ID_CUSTOM_USE_TEMPLATE_2,    GADGET_ID_NONE,
3364     &level.use_custom_template,
3365     NULL, NULL,
3366     "use template for custom elements", "use template for custom properties"
3367   },
3368   {
3369     ED_CHECKBUTTON_ID_BD_INTERMISSION,
3370     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(0),
3371     GADGET_ID_BD_INTERMISSION,          GADGET_ID_NONE,
3372     &level.bd_intermission,
3373     "Boulder Dash game engine settings:", NULL,
3374     "intermission",                     "level is an intermission level"
3375   },
3376   {
3377     ED_CHECKBUTTON_ID_BD_PAL_TIMING,
3378     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(2),
3379     GADGET_ID_BD_PAL_TIMING,            GADGET_ID_NONE,
3380     &level.bd_pal_timing,
3381     NULL, NULL,
3382     "PAL timing",                       "use slower timer (like PAL C64)"
3383   },
3384   {
3385     ED_CHECKBUTTON_ID_BD_LINE_SHIFTING_BORDERS,
3386     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(6),
3387     GADGET_ID_BD_LINE_SHIFTING_BORDERS, GADGET_ID_NONE,
3388     &level.bd_line_shifting_borders,
3389     "Compatibility settings:", NULL,
3390     "line-shifting borders",            "use line-shifting wrap-around"
3391   },
3392   {
3393     ED_CHECKBUTTON_ID_BD_SCAN_FIRST_AND_LAST_ROW,
3394     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(7),
3395     GADGET_ID_BD_SCAN_FIRST_AND_LAST_ROW, GADGET_ID_NONE,
3396     &level.bd_scan_first_and_last_row,
3397     NULL, NULL,
3398     "scan first and last row",          "also process top/bottom border rows"
3399   },
3400   {
3401     ED_CHECKBUTTON_ID_BD_SHORT_EXPLOSIONS,
3402     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(8),
3403     GADGET_ID_BD_SHORT_EXPLOSIONS,      GADGET_ID_NONE,
3404     &level.bd_short_explosions,
3405     NULL, NULL,
3406     "short explosions",                 "use four game cycles for explosions"
3407   },
3408   {
3409     ED_CHECKBUTTON_ID_BD_GRAVITY_AFFECTS_ALL,
3410     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(9),
3411     GADGET_ID_BD_GRAVITY_AFFECTS_ALL,   GADGET_ID_NONE,
3412     &level.bd_gravity_affects_all,
3413     NULL, NULL,
3414     "gravity change affects everything", "gravity affects all falling objects"
3415   },
3416
3417   // ---------- element settings: configure (various elements) ----------------
3418
3419   {
3420     ED_CHECKBUTTON_ID_STICK_ELEMENT,
3421     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3422     GADGET_ID_STICK_ELEMENT,            GADGET_ID_NONE,
3423     &stick_element_properties_window,
3424     NULL, NULL,
3425     "stick this screen to edit content","stick this screen to edit content"
3426   },
3427   {
3428     ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS,
3429     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3430     GADGET_ID_EM_SLIPPERY_GEMS,         GADGET_ID_NONE,
3431     &level.em_slippery_gems,
3432     NULL, NULL,
3433     "slip down from certain flat walls","use EM/DC style slipping behaviour"
3434   },
3435   {
3436     ED_CHECKBUTTON_ID_EM_EXPLODES_BY_FIRE,
3437     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3438     GADGET_ID_EM_EXPLODES_BY_FIRE,      GADGET_ID_NONE,
3439     &level.em_explodes_by_fire,
3440     NULL, NULL,
3441     "explodes with chain reaction",     "use R'n'D style explosion behaviour"
3442   },
3443   {
3444     ED_CHECKBUTTON_ID_USE_SPRING_BUG,
3445     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3446     GADGET_ID_USE_SPRING_BUG,           GADGET_ID_NONE,
3447     &level.use_spring_bug,
3448     NULL, NULL,
3449     "use spring pushing bug",           "use odd spring pushing behaviour"
3450   },
3451   {
3452     ED_CHECKBUTTON_ID_USE_TIME_ORB_BUG,
3453     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3454     GADGET_ID_USE_TIME_ORB_BUG,         GADGET_ID_NONE,
3455     &level.use_time_orb_bug,
3456     NULL, NULL,
3457     "use time orb bug",                 "use odd time orb behaviour"
3458   },
3459   {
3460     ED_CHECKBUTTON_ID_USE_LIFE_BUGS,
3461     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(5),
3462     GADGET_ID_USE_LIFE_BUGS,            GADGET_ID_NONE,
3463     &level.use_life_bugs,
3464     NULL, NULL,
3465     "use buggy element behaviour",      "use odd (historic) element behaviour"
3466   },
3467   {
3468     ED_CHECKBUTTON_ID_RANDOM_BALL_CONTENT,
3469     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3470     GADGET_ID_RANDOM_BALL_CONTENT,      GADGET_ID_NONE,
3471     &level.ball_random,
3472     NULL, NULL,
3473     "create single random element",     "only create one element from content"
3474   },
3475   {
3476     ED_CHECKBUTTON_ID_INITIAL_BALL_ACTIVE,
3477     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3478     GADGET_ID_INITIAL_BALL_ACTIVE,      GADGET_ID_NONE,
3479     &level.ball_active_initial,
3480     NULL, NULL,
3481     "magic ball initially activated",   "activate magic ball after level start"
3482   },
3483   {
3484     ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE,
3485     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3486     GADGET_ID_GROW_INTO_DIGGABLE,       GADGET_ID_NONE,
3487     &level.grow_into_diggable,
3488     NULL, NULL,
3489     "can grow into anything diggable",  "grow into more than just sand"
3490   },
3491   {
3492     ED_CHECKBUTTON_ID_SB_FIELDS_NEEDED,
3493     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3494     GADGET_ID_SB_FIELDS_NEEDED,         GADGET_ID_NONE,
3495     &level.sb_fields_needed,
3496     NULL, NULL,
3497     "all fields need to be filled",     "require all SB fields to be solved"
3498   },
3499   {
3500     ED_CHECKBUTTON_ID_SB_OBJECTS_NEEDED,
3501     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3502     GADGET_ID_SB_OBJECTS_NEEDED,        GADGET_ID_NONE,
3503     &level.sb_objects_needed,
3504     NULL, NULL,
3505     "all objects need to be placed",    "require all SB objects to be solved"
3506   },
3507   {
3508     ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN,
3509     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3510     GADGET_ID_AUTO_EXIT_SOKOBAN,        GADGET_ID_NONE,
3511     &level.auto_exit_sokoban,
3512     NULL, NULL,
3513     "exit level if all tasks solved",   "automatically finish Sokoban levels"
3514   },
3515   {
3516     ED_CHECKBUTTON_ID_SOLVED_BY_ONE_PLAYER,
3517     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(14),
3518     GADGET_ID_SOLVED_BY_ONE_PLAYER,     GADGET_ID_NONE,
3519     &level.solved_by_one_player,
3520     NULL, NULL,
3521     "only one player must enter exit",  "level solved by first player in exit"
3522   },
3523   {
3524     ED_CHECKBUTTON_ID_FINISH_DIG_COLLECT,
3525     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
3526     GADGET_ID_FINISH_DIG_COLLECT,       GADGET_ID_NONE,
3527     &level.finish_dig_collect,
3528     NULL, NULL,
3529     "CE action on finished dig/collect", "only finished dig/collect triggers CE"
3530   },
3531   {
3532     ED_CHECKBUTTON_ID_KEEP_WALKABLE_CE,
3533     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
3534     GADGET_ID_KEEP_WALKABLE_CE,         GADGET_ID_NONE,
3535     &level.keep_walkable_ce,
3536     NULL, NULL,
3537     "keep walkable CE changed to player", "keep CE changing to player if walkable"
3538   },
3539   {
3540     ED_CHECKBUTTON_ID_CONTINUOUS_SNAPPING,
3541     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(9),
3542     GADGET_ID_CONTINUOUS_SNAPPING,      GADGET_ID_NONE,
3543     &level.continuous_snapping,
3544     NULL, NULL,
3545     "continuous snapping",              "use snapping without releasing key"
3546   },
3547   {
3548     ED_CHECKBUTTON_ID_BLOCK_SNAP_FIELD,
3549     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(8),
3550     GADGET_ID_BLOCK_SNAP_FIELD,         GADGET_ID_NONE,
3551     &level.block_snap_field,
3552     NULL, NULL,
3553     "block snapped field when snapping", "use snapping delay to show animation"
3554   },
3555   {
3556     ED_CHECKBUTTON_ID_BLOCK_LAST_FIELD,
3557     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3558     GADGET_ID_BLOCK_LAST_FIELD,         GADGET_ID_NONE,
3559     &level.block_last_field,
3560     NULL, NULL,
3561     "block last field when moving",     "player blocks last field when moving"
3562   },
3563   {
3564     ED_CHECKBUTTON_ID_SP_BLOCK_LAST_FIELD,
3565     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3566     GADGET_ID_SP_BLOCK_LAST_FIELD,      GADGET_ID_NONE,
3567     &level.sp_block_last_field,
3568     NULL, NULL,
3569     "block last field when moving",     "player blocks last field when moving"
3570   },
3571   {
3572     ED_CHECKBUTTON_ID_INSTANT_RELOCATION,
3573     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
3574     GADGET_ID_INSTANT_RELOCATION,       GADGET_ID_NONE,
3575     &level.instant_relocation,
3576     NULL, NULL,
3577     "no scrolling when relocating",     "player gets relocated without delay"
3578   },
3579   {
3580     ED_CHECKBUTTON_ID_SHIFTED_RELOCATION,
3581     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
3582     GADGET_ID_SHIFTED_RELOCATION,       GADGET_ID_NONE,
3583     &level.shifted_relocation,
3584     NULL, NULL,
3585     "no centering when relocating",     "level not centered after relocation"
3586   },
3587   {
3588     ED_CHECKBUTTON_ID_LAZY_RELOCATION,
3589     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(5),
3590     GADGET_ID_LAZY_RELOCATION,          GADGET_ID_NONE,
3591     &level.lazy_relocation,
3592     NULL, NULL,
3593     "only redraw off-screen relocation","no redraw if relocation target visible"
3594   },
3595   {
3596     ED_CHECKBUTTON_ID_USE_START_ELEMENT,
3597     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(10),
3598     GADGET_ID_USE_START_ELEMENT,        GADGET_ID_NONE,
3599     &level.use_start_element[0],
3600     NULL, NULL,
3601     "use level start element:",        "start level at this element's position"
3602   },
3603   {
3604     ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT,
3605     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(11),
3606     GADGET_ID_USE_ARTWORK_ELEMENT,      GADGET_ID_NONE,
3607     &level.use_artwork_element[0],
3608     NULL, NULL,
3609     "use artwork from element:",        "use player artwork from other element"
3610   },
3611   {
3612     ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT,
3613     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(12),
3614     GADGET_ID_USE_EXPLOSION_ELEMENT,    GADGET_ID_NONE,
3615     &level.use_explosion_element[0],
3616     NULL, NULL,
3617     "use explosion from element:",      "use explosion properties from element"
3618   },
3619   {
3620     ED_CHECKBUTTON_ID_INITIAL_GRAVITY,
3621     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(13),
3622     GADGET_ID_INITIAL_GRAVITY,          GADGET_ID_NONE,
3623     &level.initial_player_gravity[0],
3624     NULL, NULL,
3625     "use initial gravity",              "set initial player gravity"
3626   },
3627   {
3628     ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY,
3629     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3630     GADGET_ID_USE_INITIAL_INVENTORY,    GADGET_ID_NONE,
3631     &level.use_initial_inventory[0],
3632     NULL, NULL,
3633     "use initial inventory:",           "use collected elements on level start"
3634   },
3635   {
3636     ED_CHECKBUTTON_ID_CAN_PASS_TO_WALKABLE,
3637     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(6),
3638     GADGET_ID_CAN_PASS_TO_WALKABLE,     GADGET_ID_NONE,
3639     &level.can_pass_to_walkable,
3640     NULL, NULL,
3641     "can pass to walkable element",     "player can pass to empty or walkable"
3642   },
3643   {
3644     ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID,
3645     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3646     GADGET_ID_CAN_FALL_INTO_ACID,       GADGET_ID_NONE,
3647     &custom_element_properties[EP_CAN_MOVE_INTO_ACID],
3648     NULL, NULL,
3649     "can fall into acid (with gravity)","player can fall into acid pool"
3650   },
3651   {
3652     ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID,
3653     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3654     GADGET_ID_CAN_MOVE_INTO_ACID,       GADGET_ID_NONE,
3655     &custom_element_properties[EP_CAN_MOVE_INTO_ACID],
3656     NULL, NULL,
3657     "can move into acid",               "element can move into acid pool"
3658   },
3659   {
3660     ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH,
3661     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3662     GADGET_ID_DONT_COLLIDE_WITH,        GADGET_ID_NONE,
3663     &custom_element_properties[EP_DONT_COLLIDE_WITH],
3664     NULL, NULL,
3665     "deadly when colliding with",       "element is deadly when hitting player"
3666   },
3667   {
3668     ED_CHECKBUTTON_ID_BD_DIAGONAL_MOVEMENTS,
3669     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3670     GADGET_ID_BD_DIAGONAL_MOVEMENTS,    GADGET_ID_NONE,
3671     &level.bd_diagonal_movements,
3672     NULL, NULL,
3673     "can move diagonally",              "player can move diagonally"
3674   },
3675   {
3676     ED_CHECKBUTTON_ID_BD_TOPMOST_PLAYER_ACTIVE,
3677     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3678     GADGET_ID_BD_TOPMOST_PLAYER_ACTIVE, GADGET_ID_NONE,
3679     &level.bd_topmost_player_active,
3680     NULL, NULL,
3681     "topmost player is active",         "use first player found on playfield"
3682   },
3683   {
3684     ED_CHECKBUTTON_ID_BD_PUSH_MEGA_ROCK_WITH_SWEET,
3685     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
3686     GADGET_ID_BD_PUSH_MEGA_ROCK_WITH_SWEET, GADGET_ID_NONE,
3687     &level.bd_push_mega_rock_with_sweet,
3688     NULL, NULL,
3689     "mega rocks pushable with sweet",   "push mega rocks after eating sweet"
3690   },
3691   {
3692     ED_CHECKBUTTON_ID_ENVELOPE_AUTOWRAP,
3693     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3694     GADGET_ID_ENVELOPE_AUTOWRAP,        GADGET_ID_NONE,
3695     &level.envelope[0].autowrap,
3696     NULL, NULL,
3697     "auto-wrap",                        "automatically wrap envelope text"
3698   },
3699   {
3700     ED_CHECKBUTTON_ID_ENVELOPE_CENTERED,
3701     -1,                                 ED_ELEMENT_SETTINGS_YPOS(1),
3702     GADGET_ID_ENVELOPE_CENTERED,        GADGET_ID_ENVELOPE_AUTOWRAP,
3703     &level.envelope[0].centered,
3704     NULL, " ",
3705     "centered",                         "automatically center envelope text"
3706   },
3707   {
3708     ED_CHECKBUTTON_ID_MM_LASER_RED,
3709     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3710     GADGET_ID_MM_LASER_RED,             GADGET_ID_NONE,
3711     &level.mm_laser_red,
3712     "choose color components for laser:", NULL,
3713     "red",                              "use red color components in laser"
3714   },
3715   {
3716     ED_CHECKBUTTON_ID_MM_LASER_GREEN,
3717     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3718     GADGET_ID_MM_LASER_GREEN,           GADGET_ID_NONE,
3719     &level.mm_laser_green,
3720     NULL, NULL,
3721     "green",                            "use green color components in laser"
3722   },
3723   {
3724     ED_CHECKBUTTON_ID_MM_LASER_BLUE,
3725     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
3726     GADGET_ID_MM_LASER_BLUE,            GADGET_ID_NONE,
3727     &level.mm_laser_blue,
3728     NULL, NULL,
3729     "blue",                             "use blue color components in laser"
3730   },
3731   {
3732     ED_CHECKBUTTON_ID_DF_LASER_RED,
3733     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3734     GADGET_ID_DF_LASER_RED,             GADGET_ID_NONE,
3735     &level.df_laser_red,
3736     "choose color components for laser:", NULL,
3737     "red",                              "use red color components in laser"
3738   },
3739   {
3740     ED_CHECKBUTTON_ID_DF_LASER_GREEN,
3741     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3742     GADGET_ID_DF_LASER_GREEN,           GADGET_ID_NONE,
3743     &level.df_laser_green,
3744     NULL, NULL,
3745     "green",                            "use green color components in laser"
3746   },
3747   {
3748     ED_CHECKBUTTON_ID_DF_LASER_BLUE,
3749     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
3750     GADGET_ID_DF_LASER_BLUE,            GADGET_ID_NONE,
3751     &level.df_laser_blue,
3752     NULL, NULL,
3753     "blue",                             "use blue color components in laser"
3754   },
3755   {
3756     ED_CHECKBUTTON_ID_ROTATE_MM_BALL_CONTENT,
3757     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(5),
3758     GADGET_ID_ROTATE_MM_BALL_CONTENT,   GADGET_ID_NONE,
3759     &level.rotate_mm_ball_content,
3760     NULL, NULL,
3761     "randomly rotate created content",  "randomly rotate newly created content"
3762   },
3763   {
3764     ED_CHECKBUTTON_ID_EXPLODE_MM_BALL,
3765     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(6),
3766     GADGET_ID_EXPLODE_MM_BALL,          GADGET_ID_NONE,
3767     &level.explode_mm_ball,
3768     NULL, NULL,
3769     "explode ball instead of melting",  "use explosion to release ball content"
3770   },
3771
3772   // ---------- element settings: configure 1 (custom elements) ---------------
3773
3774   {
3775     ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC,
3776     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3777     GADGET_ID_CUSTOM_USE_GRAPHIC,       GADGET_ID_NONE,
3778     &custom_element.use_gfx_element,
3779     NULL, NULL,
3780     "use graphic of element:",          "use existing element graphic"
3781   },
3782   {
3783     ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1,
3784     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(14),
3785     GADGET_ID_CUSTOM_USE_TEMPLATE_1,    GADGET_ID_NONE,
3786     &level.use_custom_template,
3787     NULL, NULL,
3788     "use template",                     "use template for custom properties"
3789   },
3790   {
3791     ED_CHECKBUTTON_ID_CUSTOM_ACCESSIBLE,
3792     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3793     GADGET_ID_CUSTOM_ACCESSIBLE,        GADGET_ID_NONE,
3794     &custom_element_properties[EP_ACCESSIBLE],
3795     NULL, NULL,
3796     NULL,                               "player can walk to or pass this field"
3797   },
3798   {
3799     ED_CHECKBUTTON_ID_CUSTOM_GRAV_REACHABLE,
3800     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(9),
3801     GADGET_ID_CUSTOM_GRAV_REACHABLE,    GADGET_ID_NONE,
3802     &custom_element_properties[EP_GRAVITY_REACHABLE],
3803     NULL, NULL,
3804     "reachable despite gravity",        "player can walk/dig despite gravity"
3805   },
3806   {
3807     ED_CHECKBUTTON_ID_CUSTOM_USE_LAST_VALUE,
3808     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(11),
3809     GADGET_ID_CUSTOM_USE_LAST_VALUE,    GADGET_ID_NONE,
3810     &custom_element.use_last_ce_value,
3811     NULL, NULL,
3812     "use last CE value after change",   "use last CE value after change"
3813   },
3814   {
3815     ED_CHECKBUTTON_ID_CUSTOM_WALK_TO_OBJECT,
3816     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
3817     GADGET_ID_CUSTOM_WALK_TO_OBJECT,    GADGET_ID_NONE,
3818     &custom_element_properties[EP_WALK_TO_OBJECT],
3819     NULL, NULL,
3820     NULL,                               "player can dig/collect/push element"
3821   },
3822   {
3823     ED_CHECKBUTTON_ID_CUSTOM_INDESTRUCTIBLE,
3824     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(8),
3825     GADGET_ID_CUSTOM_INDESTRUCTIBLE,    GADGET_ID_NONE,
3826     &custom_element_properties[EP_INDESTRUCTIBLE],
3827     NULL, NULL,
3828     "indestructible",                   "element is indestructible"
3829   },
3830
3831   // ---------- element settings: configure 2 (custom elements) ---------------
3832
3833   {
3834     ED_CHECKBUTTON_ID_CUSTOM_CAN_MOVE,
3835     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3836     GADGET_ID_CUSTOM_CAN_MOVE,          GADGET_ID_NONE,
3837     &custom_element_properties[EP_CAN_MOVE],
3838     NULL, NULL,
3839     NULL,                               "element can move with some pattern"
3840   },
3841   {
3842     ED_CHECKBUTTON_ID_CUSTOM_CAN_FALL,
3843     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(8),
3844     GADGET_ID_CUSTOM_CAN_FALL,          GADGET_ID_NONE,
3845     &custom_element_properties[EP_CAN_FALL],
3846     NULL, NULL,
3847     "can fall",                         "element can fall down"
3848   },
3849   {
3850     ED_CHECKBUTTON_ID_CUSTOM_CAN_SMASH,
3851     -1,                                 ED_ELEMENT_SETTINGS_YPOS(8),
3852     GADGET_ID_CUSTOM_CAN_SMASH,         GADGET_ID_CUSTOM_CAN_FALL,
3853     &custom_element_properties[EP_CAN_SMASH],
3854     NULL, " ",
3855     NULL,                               "element can smash other elements"
3856   },
3857   {
3858     ED_CHECKBUTTON_ID_CUSTOM_SLIPPERY,
3859     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(9),
3860     GADGET_ID_CUSTOM_SLIPPERY,          GADGET_ID_NONE,
3861     &custom_element_properties[EP_SLIPPERY],
3862     NULL, NULL,
3863     NULL,                               "other elements can fall down from it"
3864   },
3865   {
3866     ED_CHECKBUTTON_ID_CUSTOM_DEADLY,
3867     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(10),
3868     GADGET_ID_CUSTOM_DEADLY,            GADGET_ID_NONE,
3869     &custom_element_properties[EP_DEADLY],
3870     NULL, NULL,
3871     NULL,                               "element can kill the player"
3872   },
3873   {
3874     ED_CHECKBUTTON_ID_CUSTOM_CAN_EXPLODE,
3875     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(11),
3876     GADGET_ID_CUSTOM_CAN_EXPLODE,       GADGET_ID_NONE,
3877     &custom_element_properties[EP_CAN_EXPLODE],
3878     NULL, NULL,
3879     NULL,                               "element can explode"
3880   },
3881   {
3882     ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_FIRE,
3883     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(12),
3884     GADGET_ID_CUSTOM_EXPLODE_FIRE,      GADGET_ID_NONE,
3885     &custom_element_properties[EP_EXPLODES_BY_FIRE],
3886     NULL, NULL,
3887     "by fire",                          "element can explode by fire/explosion"
3888   },
3889   {
3890     ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_SMASH,
3891     -1,                                 ED_ELEMENT_SETTINGS_YPOS(12),
3892     GADGET_ID_CUSTOM_EXPLODE_SMASH,     GADGET_ID_CUSTOM_EXPLODE_FIRE,
3893     &custom_element_properties[EP_EXPLODES_SMASHED],
3894     NULL, " ",
3895     "smashed",                          "element can explode when smashed"
3896   },
3897   {
3898     ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_IMPACT,
3899     -1,                                 ED_ELEMENT_SETTINGS_YPOS(12),
3900     GADGET_ID_CUSTOM_EXPLODE_IMPACT,    GADGET_ID_CUSTOM_EXPLODE_SMASH,
3901     &custom_element_properties[EP_EXPLODES_IMPACT],
3902     NULL, " ",
3903     "impact",                           "element can explode on impact"
3904   },
3905
3906   // ---------- element settings: advanced (custom elements) ------------------
3907
3908   {
3909     ED_CHECKBUTTON_ID_CUSTOM_CAN_CHANGE,
3910     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3911     GADGET_ID_CUSTOM_CAN_CHANGE,        GADGET_ID_NONE,
3912     &custom_element_change.can_change,
3913     NULL, NULL,
3914     "element changes to:",              "change element on specified condition"
3915   },
3916   {
3917     ED_CHECKBUTTON_ID_CHANGE_DELAY,
3918     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(2),
3919     GADGET_ID_CHANGE_DELAY,             GADGET_ID_NONE,
3920     &custom_element_change_events[CE_DELAY],
3921     NULL, NULL,
3922     NULL,                               "element changes after delay"
3923   },
3924   {
3925     ED_CHECKBUTTON_ID_CHANGE_BY_DIRECT_ACT,
3926     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(4),
3927     GADGET_ID_CHANGE_BY_DIRECT_ACT,     GADGET_ID_NONE,
3928     &custom_element_change_events[CE_BY_DIRECT_ACTION],
3929     NULL, NULL,
3930     NULL,                               "element changes by direct action"
3931   },
3932   {
3933     ED_CHECKBUTTON_ID_CHANGE_BY_OTHER_ACT,
3934     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(5),
3935     GADGET_ID_CHANGE_BY_OTHER_ACT,      GADGET_ID_NONE,
3936     &custom_element_change_events[CE_BY_OTHER_ACTION],
3937     NULL, NULL,
3938     NULL,                               "element changes by other element"
3939   },
3940   {
3941     ED_CHECKBUTTON_ID_CHANGE_USE_EXPLOSION,
3942     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(8),
3943     GADGET_ID_CHANGE_USE_EXPLOSION,     GADGET_ID_NONE,
3944     &custom_element_change.explode,
3945     NULL, NULL,
3946     "explode instead of change",        "element explodes instead of change"
3947   },
3948   {
3949     ED_CHECKBUTTON_ID_CHANGE_USE_CONTENT,
3950     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(9),
3951     GADGET_ID_CHANGE_USE_CONTENT,       GADGET_ID_NONE,
3952     &custom_element_change.use_target_content,
3953     NULL, NULL,
3954     "use extended change target:",      "element changes to more elements"
3955   },
3956   {
3957     ED_CHECKBUTTON_ID_CHANGE_ONLY_COMPLETE,
3958     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(11),
3959     GADGET_ID_CHANGE_ONLY_COMPLETE,     GADGET_ID_NONE,
3960     &custom_element_change.only_if_complete,
3961     NULL, NULL,
3962     "replace all or nothing",           "only replace when all can be changed"
3963   },
3964   {
3965     ED_CHECKBUTTON_ID_CHANGE_USE_RANDOM,
3966     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(12),
3967     GADGET_ID_CHANGE_USE_RANDOM,        GADGET_ID_NONE,
3968     &custom_element_change.use_random_replace,
3969     NULL, NULL,
3970     NULL,                               "use percentage for random replace"
3971   },
3972   {
3973     ED_CHECKBUTTON_ID_CHANGE_HAS_ACTION,
3974     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(13),
3975     GADGET_ID_CHANGE_HAS_ACTION,        GADGET_ID_NONE,
3976     &custom_element_change.has_action,
3977     NULL, NULL,
3978     NULL,                               "execute action on specified condition"
3979   },
3980 };
3981
3982 static struct
3983 {
3984   int gadget_type_id;
3985   int x, y;
3986   int xoffset, yoffset;
3987   int gadget_id;
3988   int gadget_id_align;
3989   int *value;
3990   int area_xsize, area_ysize;
3991   char *text_left, *text_right, *text_above, *text_below, *infotext;
3992 } drawingarea_info[ED_NUM_DRAWING_AREAS] =
3993 {
3994   // ---------- level playfield content ---------------------------------------
3995
3996   {
3997     ED_DRAWING_ID_DRAWING_LEVEL,
3998     0,                                  0,
3999     0,                                  0,
4000     GADGET_ID_DRAWING_LEVEL,            GADGET_ID_NONE,
4001     NULL,
4002     -1, -1,     // these values are not constant, but can change at runtime
4003     NULL, NULL, NULL, NULL,             NULL
4004   },
4005
4006   // ---------- yam yam content -----------------------------------------------
4007
4008   {
4009     ED_DRAWING_ID_YAMYAM_CONTENT_0,
4010     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
4011     ED_AREA_YAMYAM_CONTENT_XOFF(0),     ED_AREA_YAMYAM_CONTENT_YOFF(0),
4012     GADGET_ID_YAMYAM_CONTENT_0,         GADGET_ID_NONE,
4013     &level.yamyam_content[0].e[0][0],   3, 3,
4014     NULL, NULL, NULL, "1",              NULL
4015   },
4016   {
4017     ED_DRAWING_ID_YAMYAM_CONTENT_1,
4018     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
4019     ED_AREA_YAMYAM_CONTENT_XOFF(1),     ED_AREA_YAMYAM_CONTENT_YOFF(1),
4020     GADGET_ID_YAMYAM_CONTENT_1,         GADGET_ID_NONE,
4021     &level.yamyam_content[1].e[0][0],   3, 3,
4022     NULL, NULL, NULL, "2",              NULL
4023   },
4024   {
4025     ED_DRAWING_ID_YAMYAM_CONTENT_2,
4026     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
4027     ED_AREA_YAMYAM_CONTENT_XOFF(2),     ED_AREA_YAMYAM_CONTENT_YOFF(2),
4028     GADGET_ID_YAMYAM_CONTENT_2,         GADGET_ID_NONE,
4029     &level.yamyam_content[2].e[0][0],   3, 3,
4030     NULL, NULL, NULL, "3",              NULL
4031   },
4032   {
4033     ED_DRAWING_ID_YAMYAM_CONTENT_3,
4034     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
4035     ED_AREA_YAMYAM_CONTENT_XOFF(3),     ED_AREA_YAMYAM_CONTENT_YOFF(3),
4036     GADGET_ID_YAMYAM_CONTENT_3,         GADGET_ID_NONE,
4037     &level.yamyam_content[3].e[0][0],   3, 3,
4038     NULL, NULL, NULL, "4",              NULL
4039   },
4040   {
4041     ED_DRAWING_ID_YAMYAM_CONTENT_4,
4042     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
4043     ED_AREA_YAMYAM_CONTENT_XOFF(4),     ED_AREA_YAMYAM_CONTENT_YOFF(4),
4044     GADGET_ID_YAMYAM_CONTENT_4,         GADGET_ID_NONE,
4045     &level.yamyam_content[4].e[0][0],   3, 3,
4046     NULL, NULL, NULL, "5",              NULL
4047   },
4048   {
4049     ED_DRAWING_ID_YAMYAM_CONTENT_5,
4050     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
4051     ED_AREA_YAMYAM_CONTENT_XOFF(5),     ED_AREA_YAMYAM_CONTENT_YOFF(5),
4052     GADGET_ID_YAMYAM_CONTENT_5,         GADGET_ID_NONE,
4053     &level.yamyam_content[5].e[0][0],   3, 3,
4054     NULL, NULL, NULL, "6",              NULL
4055   },
4056   {
4057     ED_DRAWING_ID_YAMYAM_CONTENT_6,
4058     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
4059     ED_AREA_YAMYAM_CONTENT_XOFF(6),     ED_AREA_YAMYAM_CONTENT_YOFF(6),
4060     GADGET_ID_YAMYAM_CONTENT_6,         GADGET_ID_NONE,
4061     &level.yamyam_content[6].e[0][0],   3, 3,
4062     NULL, NULL, NULL, "7",              NULL
4063   },
4064   {
4065     ED_DRAWING_ID_YAMYAM_CONTENT_7,
4066     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
4067     ED_AREA_YAMYAM_CONTENT_XOFF(7),     ED_AREA_YAMYAM_CONTENT_YOFF(7),
4068     GADGET_ID_YAMYAM_CONTENT_7,         GADGET_ID_NONE,
4069     &level.yamyam_content[7].e[0][0],   3, 3,
4070     NULL, NULL, NULL, "8",              NULL
4071   },
4072
4073   // ---------- magic ball content --------------------------------------------
4074
4075   {
4076     ED_DRAWING_ID_MAGIC_BALL_CONTENT_0,
4077     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4078     ED_AREA_MAGIC_BALL_CONTENT_XOFF(0), ED_AREA_MAGIC_BALL_CONTENT_YOFF(0),
4079     GADGET_ID_MAGIC_BALL_CONTENT_0,     GADGET_ID_NONE,
4080     &level.ball_content[0].e[0][0],     3, 3,
4081     NULL, NULL, NULL, "1",              NULL
4082   },
4083   {
4084     ED_DRAWING_ID_MAGIC_BALL_CONTENT_1,
4085     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4086     ED_AREA_MAGIC_BALL_CONTENT_XOFF(1), ED_AREA_MAGIC_BALL_CONTENT_YOFF(1),
4087     GADGET_ID_MAGIC_BALL_CONTENT_1,     GADGET_ID_NONE,
4088     &level.ball_content[1].e[0][0],     3, 3,
4089     NULL, NULL, NULL, "2",              NULL
4090   },
4091   {
4092     ED_DRAWING_ID_MAGIC_BALL_CONTENT_2,
4093     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4094     ED_AREA_MAGIC_BALL_CONTENT_XOFF(2), ED_AREA_MAGIC_BALL_CONTENT_YOFF(2),
4095     GADGET_ID_MAGIC_BALL_CONTENT_2,     GADGET_ID_NONE,
4096     &level.ball_content[2].e[0][0],     3, 3,
4097     NULL, NULL, NULL, "3",              NULL
4098   },
4099   {
4100     ED_DRAWING_ID_MAGIC_BALL_CONTENT_3,
4101     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4102     ED_AREA_MAGIC_BALL_CONTENT_XOFF(3), ED_AREA_MAGIC_BALL_CONTENT_YOFF(3),
4103     GADGET_ID_MAGIC_BALL_CONTENT_3,     GADGET_ID_NONE,
4104     &level.ball_content[3].e[0][0],     3, 3,
4105     NULL, NULL, NULL, "4",              NULL
4106   },
4107   {
4108     ED_DRAWING_ID_MAGIC_BALL_CONTENT_4,
4109     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4110     ED_AREA_MAGIC_BALL_CONTENT_XOFF(4), ED_AREA_MAGIC_BALL_CONTENT_YOFF(4),
4111     GADGET_ID_MAGIC_BALL_CONTENT_4,     GADGET_ID_NONE,
4112     &level.ball_content[4].e[0][0],     3, 3,
4113     NULL, NULL, NULL, "5",              NULL
4114   },
4115   {
4116     ED_DRAWING_ID_MAGIC_BALL_CONTENT_5,
4117     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4118     ED_AREA_MAGIC_BALL_CONTENT_XOFF(5), ED_AREA_MAGIC_BALL_CONTENT_YOFF(5),
4119     GADGET_ID_MAGIC_BALL_CONTENT_5,     GADGET_ID_NONE,
4120     &level.ball_content[5].e[0][0],     3, 3,
4121     NULL, NULL, NULL, "6",              NULL
4122   },
4123   {
4124     ED_DRAWING_ID_MAGIC_BALL_CONTENT_6,
4125     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4126     ED_AREA_MAGIC_BALL_CONTENT_XOFF(6), ED_AREA_MAGIC_BALL_CONTENT_YOFF(6),
4127     GADGET_ID_MAGIC_BALL_CONTENT_6,     GADGET_ID_NONE,
4128     &level.ball_content[6].e[0][0],     3, 3,
4129     NULL, NULL, NULL, "7",              NULL
4130   },
4131   {
4132     ED_DRAWING_ID_MAGIC_BALL_CONTENT_7,
4133     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4134     ED_AREA_MAGIC_BALL_CONTENT_XOFF(7), ED_AREA_MAGIC_BALL_CONTENT_YOFF(7),
4135     GADGET_ID_MAGIC_BALL_CONTENT_7,     GADGET_ID_NONE,
4136     &level.ball_content[7].e[0][0],     3, 3,
4137     NULL, NULL, NULL, "8",              NULL
4138   },
4139
4140   // ---------- android content -----------------------------------------------
4141
4142   {
4143     ED_DRAWING_ID_ANDROID_CONTENT,
4144     ED_AREA_1X1_SETTINGS_XPOS(0),       ED_AREA_1X1_SETTINGS_YPOS(6),
4145     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4146     GADGET_ID_ANDROID_CONTENT,          GADGET_ID_NONE,
4147     &level.android_clone_element[0],    MAX_ANDROID_ELEMENTS, 1,
4148     NULL, NULL, "elements:", NULL,      "elements android can clone"
4149   },
4150
4151   // ---------- amoeba content ------------------------------------------------
4152
4153   {
4154     ED_DRAWING_ID_AMOEBA_CONTENT,
4155     ED_AREA_1X1_SETTINGS_XPOS(0),       ED_AREA_1X1_SETTINGS_YPOS(3),
4156     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4157     GADGET_ID_AMOEBA_CONTENT,           GADGET_ID_NONE,
4158     &level.amoeba_content,              1, 1,
4159     "content:", NULL, NULL, NULL,       "amoeba content"
4160   },
4161
4162   // ---------- level start element -------------------------------------------
4163
4164   {
4165     ED_DRAWING_ID_START_ELEMENT,
4166     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(10),
4167     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4168     GADGET_ID_START_ELEMENT,            GADGET_ID_USE_START_ELEMENT,
4169     &level.start_element[0],            1, 1,
4170     NULL, NULL, NULL, NULL,             "level start element"
4171   },
4172
4173   // ---------- player artwork element ----------------------------------------
4174
4175   {
4176     ED_DRAWING_ID_ARTWORK_ELEMENT,
4177     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(11),
4178     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4179     GADGET_ID_ARTWORK_ELEMENT,          GADGET_ID_USE_ARTWORK_ELEMENT,
4180     &level.artwork_element[0],          1, 1,
4181     NULL, NULL, NULL, NULL,             "element for player artwork"
4182   },
4183
4184   // ---------- player explosion element --------------------------------------
4185
4186   {
4187     ED_DRAWING_ID_EXPLOSION_ELEMENT,
4188     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(12),
4189     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4190     GADGET_ID_EXPLOSION_ELEMENT,        GADGET_ID_USE_EXPLOSION_ELEMENT,
4191     &level.explosion_element[0],        1, 1,
4192     NULL, NULL, NULL, NULL,             "element for player explosion"
4193   },
4194
4195   // ---------- player initial inventory --------------------------------------
4196
4197   {
4198     ED_DRAWING_ID_INVENTORY_CONTENT,
4199     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(1),
4200     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4201     GADGET_ID_INVENTORY_CONTENT,        GADGET_ID_USE_INITIAL_INVENTORY,
4202     &level.initial_inventory_content[0][0], MAX_INITIAL_INVENTORY_SIZE, 1,
4203     NULL, NULL, NULL, NULL,             "content for initial inventory"
4204   },
4205
4206   // ---------- gray ball content -----------------------------------------
4207
4208   {
4209     ED_DRAWING_ID_MM_BALL_CONTENT,
4210     ED_AREA_1X1_SETTINGS_XPOS(0),       ED_AREA_1X1_SETTINGS_YPOS(2),
4211     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4212     GADGET_ID_MM_BALL_CONTENT,          GADGET_ID_NONE,
4213     &level.mm_ball_content[0],          MAX_MM_BALL_CONTENTS, 1,
4214     "content:", NULL, NULL, NULL,       "content for gray ball"
4215   },
4216
4217   // ---------- element settings: configure 1 (custom elements) ---------------
4218
4219   // ---------- custom graphic ------------------------------------------------
4220
4221   {
4222     ED_DRAWING_ID_CUSTOM_GRAPHIC,
4223     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(1),
4224     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4225     GADGET_ID_CUSTOM_GRAPHIC,           GADGET_ID_CUSTOM_USE_GRAPHIC,
4226     &custom_element.gfx_element_initial, 1, 1,
4227     NULL, NULL, NULL, NULL,             "custom graphic element"
4228   },
4229
4230   // ---------- element settings: configure 2 (custom elements) ---------------
4231
4232   // ---------- custom content (when exploding) -------------------------------
4233
4234   {
4235     ED_DRAWING_ID_CUSTOM_CONTENT,
4236     -1,                                 ED_AREA_3X3_SETTINGS_YPOS(11),
4237     0,                                  ED_AREA_3X3_SETTINGS_YOFF,
4238     GADGET_ID_CUSTOM_CONTENT,           GADGET_ID_NONE, // align three rows
4239     &custom_element.content.e[0][0],    3, 3,
4240     "content:", NULL, NULL, NULL,       NULL
4241   },
4242
4243   // ---------- custom enter and leave element (when moving) ------------------
4244
4245   {
4246     ED_DRAWING_ID_CUSTOM_MOVE_ENTER,
4247     ED_AREA_1X1_SETTINGS_XPOS(1),       ED_AREA_1X1_SETTINGS_YPOS(3),
4248     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4249     GADGET_ID_CUSTOM_MOVE_ENTER,        GADGET_ID_NONE,
4250     &custom_element.move_enter_element, 1, 1,
4251     "can dig:", " ", NULL, NULL,        "element that can be digged/collected"
4252   },
4253   {
4254     ED_DRAWING_ID_CUSTOM_MOVE_LEAVE,
4255     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(3),
4256     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4257     GADGET_ID_CUSTOM_MOVE_LEAVE,        GADGET_ID_CUSTOM_MOVE_LEAVE_TYPE,
4258     &custom_element.move_leave_element, 1, 1,
4259     NULL, NULL, NULL, NULL,             "element that will be left behind"
4260   },
4261
4262   // ---------- element settings: advanced (custom elements) ------------------
4263
4264   // ---------- custom change target ------------------------------------------
4265
4266   {
4267     ED_DRAWING_ID_CUSTOM_CHANGE_TARGET,
4268     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(1),
4269     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4270     GADGET_ID_CUSTOM_CHANGE_TARGET,     GADGET_ID_CUSTOM_CAN_CHANGE,
4271     &custom_element_change.target_element, 1, 1,
4272     NULL, "after/when:", NULL, NULL,    "new target element after change"
4273   },
4274
4275   // ---------- custom change content (extended change target) ----------------
4276
4277   {
4278     ED_DRAWING_ID_CUSTOM_CHANGE_CONTENT,
4279     -1,                                 ED_AREA_3X3_SETTINGS_YPOS(9),
4280     0,                                  ED_AREA_3X3_SETTINGS_YOFF,
4281     GADGET_ID_CUSTOM_CHANGE_CONTENT,    GADGET_ID_NONE, // align three rows
4282     &custom_element_change.target_content.e[0][0], 3, 3,
4283     NULL, NULL, NULL, NULL,             "new extended elements after change"
4284   },
4285
4286   // ---------- custom change trigger (element causing change) ----------------
4287
4288   {
4289     ED_DRAWING_ID_CUSTOM_CHANGE_TRIGGER,
4290     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(5),
4291     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4292     GADGET_ID_CUSTOM_CHANGE_TRIGGER,    GADGET_ID_CHANGE_OTHER_ACTION,
4293     &custom_element_change.initial_trigger_element, 1, 1,
4294     NULL, NULL, NULL, NULL,             "other element triggering change"
4295   },
4296
4297   // ---------- custom change action (element used for action) ----------------
4298
4299   {
4300     ED_DRAWING_ID_CUSTOM_CHANGE_ACTION,
4301     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(13),
4302     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4303     GADGET_ID_CUSTOM_CHANGE_ACTION,     GADGET_ID_ACTION_ARG,
4304     &custom_element_change.action_element, 1, 1,
4305     NULL, NULL, NULL, NULL,             "element used as action parameter"
4306   },
4307
4308   // ---------- group element content -----------------------------------------
4309
4310   {
4311     ED_DRAWING_ID_GROUP_CONTENT,
4312     ED_AREA_1X1_SETTINGS_XPOS(0),       ED_AREA_1X1_SETTINGS_YPOS(2),
4313     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4314     GADGET_ID_GROUP_CONTENT,            GADGET_ID_NONE,
4315     &group_element_info.element[0],     MAX_ELEMENTS_IN_GROUP, 1,
4316     "content:", NULL, NULL, NULL,       NULL
4317   },
4318
4319   // ---------- random background (for random painting) -----------------------
4320
4321   {
4322     ED_DRAWING_ID_RANDOM_BACKGROUND,
4323     -1,                                 ED_AREA_1X1_LSETTINGS_YPOS(1),
4324     0,                                  ED_AREA_1X1_LSETTINGS_YOFF,
4325     GADGET_ID_RANDOM_BACKGROUND,        GADGET_ID_RANDOM_RESTRICTED,
4326     &random_placement_background_element, 1, 1,
4327     NULL, NULL, NULL, NULL,             "random placement background"
4328   },
4329 };
4330
4331
4332 // ----------------------------------------------------------------------------
4333 // some internally used variables
4334 // ----------------------------------------------------------------------------
4335
4336 // maximal size of level editor drawing area
4337 static int MAX_ED_FIELDX, MAX_ED_FIELDY;
4338
4339 // actual size of level editor drawing area
4340 static int ed_fieldx, ed_fieldy;
4341
4342 // actual position of level editor drawing area in level playfield
4343 static int level_xpos = -1, level_ypos = -1;
4344
4345 // actual tile size used to display playfield drawing area
4346 static int ed_tilesize = DEFAULT_EDITOR_TILESIZE;
4347 static int ed_tilesize_default = DEFAULT_EDITOR_TILESIZE;
4348
4349 #define IN_ED_FIELD(x, y)       IN_FIELD(x, y, ed_fieldx, ed_fieldy)
4350
4351 // drawing elements on the three mouse buttons
4352 static int new_element1 = EL_WALL;
4353 static int new_element2 = EL_EMPTY;
4354 static int new_element3 = EL_SAND;
4355
4356 #define IS_VALID_BUTTON(button) (button >= 1 && button <= 3)
4357 #define BUTTON_ELEMENT(button) ((button) == 1 ? new_element1 : \
4358                                 (button) == 2 ? new_element2 : \
4359                                 (button) == 3 ? new_element3 : EL_EMPTY)
4360
4361 #define BUTTON_TILE_SIZE(x)     ((x) >= TILESIZE ? TILESIZE : MINI_TILESIZE)
4362
4363 static int use_permanent_palette = TRUE;
4364
4365 #define PX              (use_permanent_palette ? DX : SX)
4366 #define PY              (use_permanent_palette ? DY : SY)
4367 #define PXSIZE          (use_permanent_palette ? DXSIZE : SXSIZE)
4368 #define PYSIZE          (use_permanent_palette ? DYSIZE : SYSIZE)
4369
4370 // forward declaration for internal use
4371 static void CopyBrushToCursor(int, int);
4372 static void DeleteBrushFromCursor(void);
4373 static void ModifyEditorCounterValue(int, int);
4374 static void ModifyEditorCounterLimits(int, int, int);
4375 static void ModifyEditorSelectboxValue(int, int);
4376 static void ModifyEditorSelectboxOptions(int, struct ValueTextInfo *);
4377 static void ModifyEditorDrawingArea(int, int, int);
4378 static void ModifyEditorElementList(void);
4379 static void AdjustElementListScrollbar(void);
4380 static void RedrawDrawingElements(void);
4381 static void DrawDrawingWindowExt(boolean);
4382 static void DrawDrawingWindow(void);
4383 static void DrawLevelConfigWindow(void);
4384 static void DrawPropertiesWindow(void);
4385 static void DrawPaletteWindow(void);
4386 static void UpdateCustomElementGraphicGadgets(void);
4387 static boolean checkPropertiesConfig(int);
4388 static void SetAutomaticNumberOfGemsNeeded(void);
4389 static void ClearEditorGadgetInfoText(void);
4390 static void CopyLevelToUndoBuffer(int);
4391 static void HandleDrawingAreas(struct GadgetInfo *);
4392 static void HandleCounterButtons(struct GadgetInfo *);
4393 static void HandleTextInputGadgets(struct GadgetInfo *);
4394 static void HandleTextAreaGadgets(struct GadgetInfo *);
4395 static void HandleSelectboxGadgets(struct GadgetInfo *);
4396 static void HandleTextbuttonGadgets(struct GadgetInfo *);
4397 static void HandleGraphicbuttonGadgets(struct GadgetInfo *);
4398 static void HandleRadiobuttons(struct GadgetInfo *);
4399 static void HandleCheckbuttons(struct GadgetInfo *);
4400 static void HandleControlButtons(struct GadgetInfo *);
4401 static void HandleDrawingAreaInfo(struct GadgetInfo *);
4402 static void PrintEditorGadgetInfoText(struct GadgetInfo *);
4403 static boolean AskToCopyAndModifyLevelTemplate(void);
4404 static boolean getDrawModeHiRes(void);
4405 static int getTabulatorBarWidth(void);
4406 static int getTabulatorBarHeight(void);
4407 static Pixel getTabulatorBarColor(void);
4408 static int numHiresTiles(int);
4409
4410 static int num_editor_gadgets = 0;      // dynamically determined
4411
4412 static struct GadgetInfo **level_editor_gadget = NULL;
4413 static int *right_gadget_border = NULL;
4414
4415 static int drawing_function = GADGET_ID_SINGLE_ITEMS;
4416 static int last_drawing_function = GADGET_ID_SINGLE_ITEMS;
4417 static boolean draw_with_brush = FALSE;
4418 static int properties_element = 0;
4419
4420 static short TileBackup[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
4421 static short UndoBuffer[NUM_UNDO_STEPS][MAX_LEV_FIELDX][MAX_LEV_FIELDY];
4422 static short IntelliDrawBuffer[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
4423 static int undo_buffer_position = 0;
4424 static int undo_buffer_steps = 0;
4425 static int redo_buffer_steps = 0;
4426
4427 static int edit_mode;
4428 static int edit_mode_levelconfig;
4429 static int edit_mode_properties;
4430
4431 static int element_shift = 0;
4432
4433 static int editor_el_players[] =
4434 {
4435   EL_PLAYER_1,
4436   EL_PLAYER_2,
4437   EL_PLAYER_3,
4438   EL_PLAYER_4
4439 };
4440 static int *editor_el_players_ptr = editor_el_players;
4441 static int num_editor_el_players = ARRAY_SIZE(editor_el_players);
4442
4443 static int editor_hl_boulderdash[] =
4444 {
4445   EL_INTERNAL_CASCADE_BD_ACTIVE,
4446   EL_CHAR('B'),
4447   EL_CHAR('D'),
4448   EL_EMPTY,
4449 };
4450
4451 static int editor_el_boulderdash[] =
4452 {
4453   EL_EMPTY,
4454   EL_SAND,
4455   EL_BD_ROCK,
4456   EL_BD_DIAMOND,
4457
4458   EL_STEELWALL,
4459   EL_BD_WALL,
4460   EL_BD_EXPANDABLE_WALL,
4461   EL_BD_MAGIC_WALL,
4462
4463   EL_BD_AMOEBA,
4464   EL_BD_BUTTERFLY_UP,
4465   EL_BD_FIREFLY_UP,
4466   EL_EXIT_CLOSED,
4467
4468   EL_BD_BUTTERFLY_LEFT,
4469   EL_BD_FIREFLY_LEFT,
4470   EL_BD_BUTTERFLY_RIGHT,
4471   EL_BD_FIREFLY_RIGHT,
4472
4473   EL_EMPTY,
4474   EL_BD_BUTTERFLY_DOWN,
4475   EL_BD_FIREFLY_DOWN,
4476   EL_EXIT_OPEN,
4477 };
4478 static int *editor_hl_boulderdash_ptr = editor_hl_boulderdash;
4479 static int *editor_el_boulderdash_ptr = editor_el_boulderdash;
4480 static int num_editor_hl_boulderdash = ARRAY_SIZE(editor_hl_boulderdash);
4481 static int num_editor_el_boulderdash = ARRAY_SIZE(editor_el_boulderdash);
4482
4483 static int editor_hl_boulderdash_native[] =
4484 {
4485   EL_INTERNAL_CASCADE_BD_NATIVE_ACTIVE,
4486   EL_CHAR('B'),
4487   EL_CHAR('D'),
4488   EL_EMPTY,
4489 };
4490
4491 static int editor_el_boulderdash_native[] =
4492 {
4493   EL_EMPTY,
4494   EL_BD_SAND,
4495   EL_BD_ROCK,
4496   EL_BD_DIAMOND,
4497
4498   EL_BD_STEELWALL,
4499   EL_BD_WALL,
4500   EL_EMPTY,
4501   EL_BD_MAGIC_WALL,
4502
4503   EL_BD_AMOEBA,
4504   EL_BD_BUTTERFLY_UP,
4505   EL_BD_FIREFLY_UP,
4506   EL_BD_EXIT_CLOSED,
4507
4508   EL_BD_BUTTERFLY_LEFT,
4509   EL_BD_FIREFLY_LEFT,
4510   EL_BD_BUTTERFLY_RIGHT,
4511   EL_BD_FIREFLY_RIGHT,
4512
4513   EL_BD_INBOX,
4514   EL_BD_BUTTERFLY_DOWN,
4515   EL_BD_FIREFLY_DOWN,
4516   EL_BD_EXIT_OPEN,
4517
4518   EL_BD_AMOEBA_2,
4519   EL_BD_BUTTERFLY_2_UP,
4520   EL_BD_FIREFLY_2_UP,
4521   EL_EMPTY,
4522
4523   EL_BD_BUTTERFLY_2_LEFT,
4524   EL_BD_FIREFLY_2_LEFT,
4525   EL_BD_BUTTERFLY_2_RIGHT,
4526   EL_BD_FIREFLY_2_RIGHT,
4527
4528   EL_EMPTY,
4529   EL_BD_BUTTERFLY_2_DOWN,
4530   EL_BD_FIREFLY_2_DOWN,
4531   EL_EMPTY,
4532
4533   EL_EMPTY,
4534   EL_BD_DRAGONFLY_UP,
4535   EL_BD_STONEFLY_UP,
4536   EL_EMPTY,
4537
4538   EL_BD_DRAGONFLY_LEFT,
4539   EL_BD_STONEFLY_LEFT,
4540   EL_BD_DRAGONFLY_RIGHT,
4541   EL_BD_STONEFLY_RIGHT,
4542
4543   EL_EMPTY,
4544   EL_BD_DRAGONFLY_DOWN,
4545   EL_BD_STONEFLY_DOWN,
4546   EL_EMPTY,
4547
4548   EL_EMPTY,
4549   EL_BD_BITER_UP,
4550   EL_BD_COW_UP,
4551   EL_EMPTY,
4552
4553   EL_BD_BITER_LEFT,
4554   EL_BD_COW_LEFT,
4555   EL_BD_BITER_RIGHT,
4556   EL_BD_COW_RIGHT,
4557
4558   EL_EMPTY,
4559   EL_BD_BITER_DOWN,
4560   EL_BD_COW_DOWN,
4561   EL_EMPTY,
4562
4563   EL_EMPTY,
4564   EL_BD_SAND,
4565   EL_BD_SAND_BALL,
4566   EL_BD_SAND_LOOSE,
4567
4568   EL_BD_SAND_SLOPED_UP_LEFT,
4569   EL_BD_SAND_SLOPED_UP_RIGHT,
4570   EL_BD_WALL_SLOPED_UP_LEFT,
4571   EL_BD_WALL_SLOPED_UP_RIGHT,
4572
4573   EL_BD_SAND_SLOPED_DOWN_LEFT,
4574   EL_BD_SAND_SLOPED_DOWN_RIGHT,
4575   EL_BD_WALL_SLOPED_DOWN_LEFT,
4576   EL_BD_WALL_SLOPED_DOWN_RIGHT,
4577
4578   EL_BD_FLYING_DIAMOND,
4579   EL_BD_FLYING_ROCK,
4580   EL_BD_STEELWALL_SLOPED_UP_LEFT,
4581   EL_BD_STEELWALL_SLOPED_UP_RIGHT,
4582
4583   EL_BD_NUT,
4584   EL_BD_MEGA_ROCK,
4585   EL_BD_STEELWALL_SLOPED_DOWN_LEFT,
4586   EL_BD_STEELWALL_SLOPED_DOWN_RIGHT,
4587
4588   EL_BD_SAND_2,
4589   EL_BD_WALL_NON_SLOPED,
4590   EL_BD_WALL,
4591   EL_BD_MAGIC_WALL,
4592
4593   EL_BD_EXIT_CLOSED,
4594   EL_BD_EXIT_OPEN,
4595   EL_BD_INVISIBLE_EXIT_CLOSED,
4596   EL_BD_INVISIBLE_EXIT_OPEN,
4597
4598   EL_BD_STEELWALL,
4599   EL_BD_STEELWALL_EXPLODABLE,
4600   EL_BD_STEELWALL_DIGGABLE,
4601   EL_BD_WALL_DIGGABLE,
4602
4603   EL_BD_EXPANDABLE_WALL_HORIZONTAL,
4604   EL_BD_EXPANDABLE_WALL_VERTICAL,
4605   EL_BD_EXPANDABLE_WALL_ANY,
4606   EL_BD_CREATURE_SWITCH,
4607
4608   EL_BD_EXPANDABLE_STEELWALL_HORIZONTAL,
4609   EL_BD_EXPANDABLE_STEELWALL_VERTICAL,
4610   EL_BD_EXPANDABLE_STEELWALL_ANY,
4611   EL_BD_EXPANDABLE_WALL_SWITCH_HORIZONTAL,
4612
4613   EL_BD_BITER_SWITCH_1,
4614   EL_BD_REPLICATOR_SWITCH,
4615   EL_BD_CONVEYOR_SWITCH,
4616   EL_BD_CONVEYOR_DIR_SWITCH_RIGHT,
4617
4618   EL_BD_ACID,
4619   EL_BD_FALLING_WALL,
4620   EL_BD_BOX,
4621   EL_BD_TIME_PENALTY,
4622
4623   EL_BD_GRAVESTONE,
4624   EL_BD_ROCK_GLUED,
4625   EL_BD_DIAMOND_GLUED,
4626   EL_BD_DIAMOND_KEY,
4627
4628   EL_BD_TRAPPED_DIAMOND,
4629   EL_BD_CLOCK,
4630   EL_BD_SAND_GLUED,
4631   EL_BD_WATER,
4632
4633   EL_BD_KEY_1,
4634   EL_BD_KEY_2,
4635   EL_BD_KEY_3,
4636   EL_EMPTY,
4637
4638   EL_BD_WALL_KEY_1,
4639   EL_BD_WALL_KEY_2,
4640   EL_BD_WALL_KEY_3,
4641   EL_BD_WALL_DIAMOND,
4642
4643   EL_BD_GATE_1,
4644   EL_BD_GATE_2,
4645   EL_BD_GATE_3,
4646   EL_BD_POT,
4647
4648   EL_BD_GRAVITY_SWITCH,
4649   EL_BD_PNEUMATIC_HAMMER,
4650   EL_BD_TELEPORTER,
4651   EL_BD_SKELETON,
4652
4653   EL_BD_AMOEBA,
4654   EL_BD_AMOEBA_2,
4655   EL_BD_REPLICATOR,
4656   EL_BD_BLADDER_SPENDER,
4657
4658   EL_BD_CONVEYOR_LEFT,
4659   EL_BD_CONVEYOR_RIGHT,
4660   EL_BD_BOMB,
4661   EL_BD_NITRO_PACK,
4662
4663   EL_BD_LAVA,
4664   EL_BD_SWEET,
4665   EL_BD_VOODOO_DOLL,
4666   EL_BD_SLIME,
4667
4668   EL_BD_BLADDER,
4669   EL_BD_WAITING_ROCK,
4670   EL_BD_CHASING_ROCK,
4671   EL_BD_GHOST,
4672
4673   EL_BD_PLAYER,
4674   EL_BD_PLAYER_WITH_BOMB,
4675   EL_BD_PLAYER_GLUED,
4676   EL_BD_PLAYER_STIRRING,
4677 };
4678 static int *editor_hl_boulderdash_native_ptr = editor_hl_boulderdash_native;
4679 static int *editor_el_boulderdash_native_ptr = editor_el_boulderdash_native;
4680 static int num_editor_hl_boulderdash_native = ARRAY_SIZE(editor_hl_boulderdash_native);
4681 static int num_editor_el_boulderdash_native = ARRAY_SIZE(editor_el_boulderdash_native);
4682
4683 static int editor_hl_emerald_mine[] =
4684 {
4685   EL_INTERNAL_CASCADE_EM_ACTIVE,
4686   EL_CHAR('E'),
4687   EL_CHAR('M'),
4688   EL_EMPTY,
4689 };
4690
4691 static int editor_el_emerald_mine[] =
4692 {
4693   EL_SAND,
4694   EL_ROCK,
4695   EL_QUICKSAND_EMPTY,
4696   EL_QUICKSAND_FULL,
4697
4698   EL_STEELWALL,
4699   EL_WALL,
4700   EL_WALL_SLIPPERY,
4701   EL_MAGIC_WALL,
4702
4703   EL_EMERALD,
4704   EL_DIAMOND,
4705   EL_NUT,
4706   EL_BOMB,
4707
4708   EL_EM_DYNAMITE,
4709   EL_EM_DYNAMITE_ACTIVE,
4710   EL_EM_EXIT_CLOSED,
4711   EL_EM_EXIT_OPEN,
4712
4713   EL_YAMYAM_UP,
4714   EL_BUG_UP,
4715   EL_SPACESHIP_UP,
4716   EL_ROBOT,
4717
4718   EL_BUG_LEFT,
4719   EL_SPACESHIP_LEFT,
4720   EL_BUG_RIGHT,
4721   EL_SPACESHIP_RIGHT,
4722
4723   EL_ROBOT_WHEEL,
4724   EL_BUG_DOWN,
4725   EL_SPACESHIP_DOWN,
4726   EL_INVISIBLE_WALL,
4727
4728   EL_ACID_POOL_TOPLEFT,
4729   EL_ACID,
4730   EL_ACID_POOL_TOPRIGHT,
4731   EL_AMOEBA_DROP,
4732
4733   EL_ACID_POOL_BOTTOMLEFT,
4734   EL_ACID_POOL_BOTTOM,
4735   EL_ACID_POOL_BOTTOMRIGHT,
4736   EL_AMOEBA_WET,
4737
4738   EL_EM_KEY_1,
4739   EL_EM_KEY_2,
4740   EL_EM_KEY_3,
4741   EL_EM_KEY_4,
4742
4743   EL_EM_GATE_1,
4744   EL_EM_GATE_2,
4745   EL_EM_GATE_3,
4746   EL_EM_GATE_4,
4747
4748   EL_EM_GATE_1_GRAY,
4749   EL_EM_GATE_2_GRAY,
4750   EL_EM_GATE_3_GRAY,
4751   EL_EM_GATE_4_GRAY,
4752 };
4753 static int *editor_hl_emerald_mine_ptr = editor_hl_emerald_mine;
4754 static int *editor_el_emerald_mine_ptr = editor_el_emerald_mine;
4755 static int num_editor_hl_emerald_mine = ARRAY_SIZE(editor_hl_emerald_mine);
4756 static int num_editor_el_emerald_mine = ARRAY_SIZE(editor_el_emerald_mine);
4757
4758 static int editor_hl_emerald_mine_club[] =
4759 {
4760   EL_INTERNAL_CASCADE_EMC_ACTIVE,
4761   EL_CHAR('E'),
4762   EL_CHAR('M'),
4763   EL_CHAR('C'),
4764 };
4765
4766 static int editor_el_emerald_mine_club[] =
4767 {
4768   EL_EMC_KEY_5,
4769   EL_EMC_KEY_6,
4770   EL_EMC_KEY_7,
4771   EL_EMC_KEY_8,
4772
4773   EL_EMC_GATE_5,
4774   EL_EMC_GATE_6,
4775   EL_EMC_GATE_7,
4776   EL_EMC_GATE_8,
4777
4778   EL_EMC_GATE_5_GRAY,
4779   EL_EMC_GATE_6_GRAY,
4780   EL_EMC_GATE_7_GRAY,
4781   EL_EMC_GATE_8_GRAY,
4782
4783   EL_EMC_STEELWALL_1,
4784   EL_EMC_STEELWALL_2,
4785   EL_EMC_STEELWALL_3,
4786   EL_EMC_STEELWALL_4,
4787
4788   EL_EMC_WALL_13,
4789   EL_EMC_WALL_14,
4790   EL_EMC_WALL_15,
4791   EL_EMC_WALL_16,
4792
4793   EL_EMC_WALL_SLIPPERY_1,
4794   EL_EMC_WALL_SLIPPERY_2,
4795   EL_EMC_WALL_SLIPPERY_3,
4796   EL_EMC_WALL_SLIPPERY_4,
4797
4798   EL_EMC_WALL_1,
4799   EL_EMC_WALL_2,
4800   EL_EMC_WALL_3,
4801   EL_EMC_WALL_4,
4802
4803   EL_EMC_WALL_5,
4804   EL_EMC_WALL_6,
4805   EL_EMC_WALL_7,
4806   EL_EMC_WALL_8,
4807
4808   EL_EMC_WALL_9,
4809   EL_EMC_WALL_10,
4810   EL_EMC_WALL_11,
4811   EL_EMC_WALL_12,
4812
4813   EL_EMC_GRASS,
4814   EL_EMC_FAKE_GRASS,
4815   EL_EMC_PLANT,
4816   EL_EMC_DRIPPER,
4817
4818   EL_EMC_MAGIC_BALL,
4819   EL_EMC_MAGIC_BALL_SWITCH,
4820   EL_EMC_LENSES,
4821   EL_EMC_MAGNIFIER,
4822
4823   EL_SPRING_LEFT,
4824   EL_SPRING,
4825   EL_SPRING_RIGHT,
4826   EL_EMC_SPRING_BUMPER,
4827
4828   EL_BALLOON,
4829   EL_YAMYAM_UP,
4830   EL_BALLOON_SWITCH_UP,
4831   EL_BALLOON_SWITCH_ANY,
4832
4833   EL_YAMYAM_LEFT,
4834   EL_BALLOON_SWITCH_LEFT,
4835   EL_YAMYAM_RIGHT,
4836   EL_BALLOON_SWITCH_RIGHT,
4837
4838   EL_EMC_ANDROID,
4839   EL_YAMYAM_DOWN,
4840   EL_BALLOON_SWITCH_DOWN,
4841   EL_BALLOON_SWITCH_NONE,
4842 };
4843 static int *editor_hl_emerald_mine_club_ptr = editor_hl_emerald_mine_club;
4844 static int *editor_el_emerald_mine_club_ptr = editor_el_emerald_mine_club;
4845 static int num_editor_hl_emerald_mine_club = ARRAY_SIZE(editor_hl_emerald_mine_club);
4846 static int num_editor_el_emerald_mine_club = ARRAY_SIZE(editor_el_emerald_mine_club);
4847
4848 static int editor_hl_rnd[] =
4849 {
4850   EL_INTERNAL_CASCADE_RND_ACTIVE,
4851   EL_CHAR('R'),
4852   EL_CHAR('N'),
4853   EL_CHAR('D'),
4854 };
4855
4856 static int editor_el_rnd[] =
4857 {
4858   EL_DYNAMITE,                  // RND
4859   EL_DYNAMITE_ACTIVE,           // RND
4860   EL_EMPTY,
4861   EL_EMPTY,
4862
4863   EL_KEY_1,
4864   EL_KEY_2,
4865   EL_KEY_3,
4866   EL_KEY_4,
4867
4868   EL_GATE_1,
4869   EL_GATE_2,
4870   EL_GATE_3,
4871   EL_GATE_4,
4872
4873   EL_GATE_1_GRAY,
4874   EL_GATE_2_GRAY,
4875   EL_GATE_3_GRAY,
4876   EL_GATE_4_GRAY,
4877
4878   EL_ARROW_LEFT,
4879   EL_ARROW_RIGHT,
4880   EL_ARROW_UP,
4881   EL_ARROW_DOWN,
4882
4883   EL_AMOEBA_DEAD,
4884   EL_AMOEBA_DRY,
4885   EL_AMOEBA_FULL,
4886   EL_GAME_OF_LIFE,
4887
4888   EL_EMERALD_YELLOW,
4889   EL_EMERALD_RED,
4890   EL_EMERALD_PURPLE,
4891   EL_BIOMAZE,
4892
4893   EL_WALL_EMERALD_YELLOW,
4894   EL_WALL_EMERALD_RED,
4895   EL_WALL_EMERALD_PURPLE,
4896   EL_WALL_BD_DIAMOND,
4897
4898   EL_SPEED_PILL,
4899   EL_PACMAN_UP,
4900   EL_TIME_ORB_FULL,
4901   EL_TIME_ORB_EMPTY,
4902
4903   EL_PACMAN_LEFT,
4904   EL_DARK_YAMYAM,
4905   EL_PACMAN_RIGHT,
4906   EL_YAMYAM,                    // RND
4907
4908   EL_BLACK_ORB,
4909   EL_PACMAN_DOWN,
4910   EL_LAMP,
4911   EL_LAMP_ACTIVE,
4912
4913   EL_DYNABOMB_INCREASE_NUMBER,
4914   EL_DYNABOMB_INCREASE_SIZE,
4915   EL_DYNABOMB_INCREASE_POWER,
4916   EL_STONEBLOCK,
4917
4918   EL_MOLE,
4919   EL_PENGUIN,
4920   EL_PIG,
4921   EL_DRAGON,
4922
4923   EL_BUG,
4924   EL_MOLE_UP,
4925   EL_BD_BUTTERFLY,
4926   EL_BD_FIREFLY,
4927
4928   EL_MOLE_LEFT,
4929   EL_SATELLITE,
4930   EL_MOLE_RIGHT,
4931   EL_PACMAN,
4932
4933   EL_SPACESHIP,
4934   EL_MOLE_DOWN,
4935   EL_INVISIBLE_STEELWALL,
4936   EL_INVISIBLE_WALL,
4937
4938   EL_EXPANDABLE_WALL,
4939   EL_EXPANDABLE_WALL_HORIZONTAL,
4940   EL_EXPANDABLE_WALL_VERTICAL,
4941   EL_EXPANDABLE_WALL_ANY,
4942 };
4943 static int *editor_hl_rnd_ptr = editor_hl_rnd;
4944 static int *editor_el_rnd_ptr = editor_el_rnd;
4945 static int num_editor_hl_rnd = ARRAY_SIZE(editor_hl_rnd);
4946 static int num_editor_el_rnd = ARRAY_SIZE(editor_el_rnd);
4947
4948 static int editor_hl_sokoban[] =
4949 {
4950   EL_INTERNAL_CASCADE_SB_ACTIVE,
4951   EL_CHAR('S'),
4952   EL_CHAR('B'),
4953   EL_EMPTY,
4954 };
4955
4956 static int editor_el_sokoban[] =
4957 {
4958   EL_SOKOBAN_OBJECT,
4959   EL_SOKOBAN_FIELD_EMPTY,
4960   EL_SOKOBAN_FIELD_FULL,
4961   EL_SOKOBAN_FIELD_PLAYER,
4962 };
4963 static int *editor_hl_sokoban_ptr = editor_hl_sokoban;
4964 static int *editor_el_sokoban_ptr = editor_el_sokoban;
4965 static int num_editor_hl_sokoban = ARRAY_SIZE(editor_hl_sokoban);
4966 static int num_editor_el_sokoban = ARRAY_SIZE(editor_el_sokoban);
4967
4968 static int editor_hl_supaplex[] =
4969 {
4970   EL_INTERNAL_CASCADE_SP_ACTIVE,
4971   EL_CHAR('S'),
4972   EL_CHAR('P'),
4973   EL_EMPTY,
4974 };
4975
4976 static int editor_el_supaplex[] =
4977 {
4978   EL_SP_MURPHY,
4979   EL_EMPTY,
4980   EL_SP_BASE,
4981   EL_SP_BUGGY_BASE,
4982
4983   EL_SP_INFOTRON,
4984   EL_SP_ZONK,
4985   EL_SP_SNIKSNAK,
4986   EL_SP_ELECTRON,
4987
4988   EL_SP_DISK_RED,
4989   EL_SP_DISK_ORANGE,
4990   EL_SP_DISK_YELLOW,
4991   EL_SP_TERMINAL,
4992
4993   EL_SP_EXIT_CLOSED,
4994   EL_SP_PORT_HORIZONTAL,
4995   EL_SP_PORT_VERTICAL,
4996   EL_SP_PORT_ANY,
4997
4998   EL_SP_PORT_LEFT,
4999   EL_SP_PORT_RIGHT,
5000   EL_SP_PORT_UP,
5001   EL_SP_PORT_DOWN,
5002
5003   EL_SP_GRAVITY_PORT_LEFT,
5004   EL_SP_GRAVITY_PORT_RIGHT,
5005   EL_SP_GRAVITY_PORT_UP,
5006   EL_SP_GRAVITY_PORT_DOWN,
5007
5008   EL_SP_GRAVITY_ON_PORT_LEFT,
5009   EL_SP_GRAVITY_ON_PORT_RIGHT,
5010   EL_SP_GRAVITY_ON_PORT_UP,
5011   EL_SP_GRAVITY_ON_PORT_DOWN,
5012
5013   EL_SP_GRAVITY_OFF_PORT_LEFT,
5014   EL_SP_GRAVITY_OFF_PORT_RIGHT,
5015   EL_SP_GRAVITY_OFF_PORT_UP,
5016   EL_SP_GRAVITY_OFF_PORT_DOWN,
5017
5018   EL_SP_HARDWARE_GRAY,
5019   EL_SP_HARDWARE_GREEN,
5020   EL_SP_HARDWARE_BLUE,
5021   EL_SP_HARDWARE_RED,
5022
5023   EL_SP_HARDWARE_BASE_1,
5024   EL_SP_HARDWARE_BASE_2,
5025   EL_SP_HARDWARE_BASE_3,
5026   EL_SP_HARDWARE_BASE_4,
5027
5028   EL_SP_HARDWARE_BASE_5,
5029   EL_SP_HARDWARE_BASE_6,
5030   EL_SP_HARDWARE_YELLOW,
5031   EL_SP_CHIP_TOP,
5032
5033   EL_SP_CHIP_SINGLE,
5034   EL_SP_CHIP_LEFT,
5035   EL_SP_CHIP_RIGHT,
5036   EL_SP_CHIP_BOTTOM,
5037 };
5038 static int *editor_hl_supaplex_ptr = editor_hl_supaplex;
5039 static int *editor_el_supaplex_ptr = editor_el_supaplex;
5040 static int num_editor_hl_supaplex = ARRAY_SIZE(editor_hl_supaplex);
5041 static int num_editor_el_supaplex = ARRAY_SIZE(editor_el_supaplex);
5042
5043 static int editor_hl_diamond_caves[] =
5044 {
5045   EL_INTERNAL_CASCADE_DC_ACTIVE,
5046   EL_CHAR('D'),
5047   EL_CHAR('C'),
5048   EL_CHAR('2'),
5049 };
5050
5051 static int editor_el_diamond_caves[] =
5052 {
5053   EL_EM_STEEL_EXIT_CLOSED,      // DC2
5054   EL_EM_STEEL_EXIT_OPEN,        // DC2
5055   EL_WALL_EMERALD,              // DC2
5056   EL_WALL_DIAMOND,              // DC2
5057
5058   EL_PEARL,
5059   EL_CRYSTAL,
5060   EL_WALL_PEARL,
5061   EL_WALL_CRYSTAL,
5062
5063   EL_CONVEYOR_BELT_1_LEFT,
5064   EL_CONVEYOR_BELT_1_MIDDLE,
5065   EL_CONVEYOR_BELT_1_RIGHT,
5066   EL_CONVEYOR_BELT_1_SWITCH_MIDDLE,
5067
5068   EL_CONVEYOR_BELT_2_LEFT,
5069   EL_CONVEYOR_BELT_2_MIDDLE,
5070   EL_CONVEYOR_BELT_2_RIGHT,
5071   EL_CONVEYOR_BELT_2_SWITCH_MIDDLE,
5072
5073   EL_CONVEYOR_BELT_3_LEFT,
5074   EL_CONVEYOR_BELT_3_MIDDLE,
5075   EL_CONVEYOR_BELT_3_RIGHT,
5076   EL_CONVEYOR_BELT_3_SWITCH_MIDDLE,
5077
5078   EL_CONVEYOR_BELT_4_LEFT,
5079   EL_CONVEYOR_BELT_4_MIDDLE,
5080   EL_CONVEYOR_BELT_4_RIGHT,
5081   EL_CONVEYOR_BELT_4_SWITCH_MIDDLE,
5082
5083   EL_CONVEYOR_BELT_1_SWITCH_LEFT,
5084   EL_CONVEYOR_BELT_2_SWITCH_LEFT,
5085   EL_CONVEYOR_BELT_3_SWITCH_LEFT,
5086   EL_CONVEYOR_BELT_4_SWITCH_LEFT,
5087
5088   EL_CONVEYOR_BELT_1_SWITCH_RIGHT,
5089   EL_CONVEYOR_BELT_2_SWITCH_RIGHT,
5090   EL_CONVEYOR_BELT_3_SWITCH_RIGHT,
5091   EL_CONVEYOR_BELT_4_SWITCH_RIGHT,
5092
5093   EL_TIMEGATE_CLOSED,
5094   EL_TIMEGATE_OPEN,
5095   EL_TIMEGATE_SWITCH,
5096   EL_DC_TIMEGATE_SWITCH,
5097
5098   EL_SWITCHGATE_CLOSED,
5099   EL_SWITCHGATE_OPEN,
5100   EL_SWITCHGATE_SWITCH_UP,
5101   EL_SWITCHGATE_SWITCH_DOWN,
5102
5103   EL_LIGHT_SWITCH,
5104   EL_LIGHT_SWITCH_ACTIVE,
5105   EL_DC_SWITCHGATE_SWITCH_UP,
5106   EL_DC_SWITCHGATE_SWITCH_DOWN,
5107
5108   EL_STEEL_EXIT_CLOSED,
5109   EL_STEEL_EXIT_OPEN,
5110   EL_STEELWALL_SLIPPERY,
5111   EL_INVISIBLE_SAND,
5112
5113   EL_QUICKSAND_FAST_EMPTY,
5114   EL_QUICKSAND_FAST_FULL,
5115   EL_LANDMINE,
5116   EL_DC_LANDMINE,
5117
5118   EL_SHIELD_NORMAL,
5119   EL_SHIELD_DEADLY,
5120   EL_EXTRA_TIME,
5121   EL_DC_MAGIC_WALL,
5122
5123   EL_ENVELOPE_1,
5124   EL_ENVELOPE_2,
5125   EL_ENVELOPE_3,
5126   EL_ENVELOPE_4,
5127
5128   EL_SIGN_RADIOACTIVITY,
5129   EL_SIGN_WHEELCHAIR,
5130   EL_SIGN_PARKING,
5131   EL_SIGN_NO_ENTRY,
5132
5133   EL_SIGN_GIVE_WAY,
5134   EL_SIGN_ENTRY_FORBIDDEN,
5135   EL_SIGN_EMERGENCY_EXIT,
5136   EL_SIGN_YIN_YANG,
5137
5138 #if 0
5139   EL_SIGN_SPERMS,
5140   EL_SIGN_BULLET,
5141   EL_SIGN_HEART,
5142   EL_SIGN_CROSS,
5143
5144   EL_SIGN_FRANKIE,
5145   EL_EMPTY,
5146   EL_EMPTY,
5147   EL_EMPTY,
5148
5149   EL_SPERMS,
5150   EL_BULLET,
5151   EL_HEART,
5152   EL_CROSS,
5153
5154   EL_FRANKIE,
5155   EL_EMPTY,
5156   EL_EMPTY,
5157   EL_EMPTY,
5158 #endif
5159
5160   EL_DC_STEELWALL_2_SINGLE,
5161   EL_DC_STEELWALL_2_TOP,
5162   EL_SIGN_EXCLAMATION,
5163   EL_SIGN_STOP,
5164
5165   EL_DC_STEELWALL_2_LEFT,
5166   EL_DC_STEELWALL_2_MIDDLE,
5167   EL_DC_STEELWALL_2_HORIZONTAL,
5168   EL_DC_STEELWALL_2_RIGHT,
5169
5170   EL_DC_STEELWALL_1_TOPLEFT,
5171   EL_DC_STEELWALL_2_VERTICAL,
5172   EL_DC_STEELWALL_1_TOPRIGHT,
5173   EL_DC_GATE_WHITE,
5174
5175   EL_DC_STEELWALL_1_VERTICAL,
5176   EL_DC_STEELWALL_2_BOTTOM,
5177   EL_DC_KEY_WHITE,
5178   EL_DC_GATE_WHITE_GRAY,
5179
5180   EL_DC_STEELWALL_1_BOTTOMLEFT,
5181   EL_DC_STEELWALL_1_HORIZONTAL,
5182   EL_DC_STEELWALL_1_BOTTOMRIGHT,
5183   EL_DC_GATE_FAKE_GRAY,
5184
5185   EL_DC_STEELWALL_1_BOTTOMRIGHT_2,
5186   EL_DC_STEELWALL_1_BOTTOM,
5187   EL_DC_STEELWALL_1_BOTTOMLEFT_2,
5188   EL_EXPANDABLE_STEELWALL_HORIZONTAL,
5189
5190   EL_DC_STEELWALL_1_RIGHT,
5191   EL_EMPTY,
5192   EL_DC_STEELWALL_1_LEFT,
5193   EL_EXPANDABLE_STEELWALL_VERTICAL,
5194
5195   EL_DC_STEELWALL_1_TOPRIGHT_2,
5196   EL_DC_STEELWALL_1_TOP,
5197   EL_DC_STEELWALL_1_TOPLEFT_2,
5198   EL_EXPANDABLE_STEELWALL_ANY,
5199 };
5200 static int *editor_hl_diamond_caves_ptr = editor_hl_diamond_caves;
5201 static int *editor_el_diamond_caves_ptr = editor_el_diamond_caves;
5202 static int num_editor_hl_diamond_caves = ARRAY_SIZE(editor_hl_diamond_caves);
5203 static int num_editor_el_diamond_caves = ARRAY_SIZE(editor_el_diamond_caves);
5204
5205 static int editor_hl_dx_boulderdash[] =
5206 {
5207   EL_INTERNAL_CASCADE_DX_ACTIVE,
5208   EL_CHAR('D'),
5209   EL_CHAR('X'),
5210   EL_EMPTY,
5211 };
5212
5213 static int editor_el_dx_boulderdash[] =
5214 {
5215   EL_EMPTY,
5216   EL_TUBE_RIGHT_DOWN,
5217   EL_TUBE_HORIZONTAL_DOWN,
5218   EL_TUBE_LEFT_DOWN,
5219
5220   EL_TUBE_HORIZONTAL,
5221   EL_TUBE_VERTICAL_RIGHT,
5222   EL_TUBE_ANY,
5223   EL_TUBE_VERTICAL_LEFT,
5224
5225   EL_TUBE_VERTICAL,
5226   EL_TUBE_RIGHT_UP,
5227   EL_TUBE_HORIZONTAL_UP,
5228   EL_TUBE_LEFT_UP,
5229
5230   EL_TRAP,
5231   EL_DX_SUPABOMB,
5232   EL_EMPTY,
5233   EL_EMPTY
5234 };
5235 static int *editor_hl_dx_boulderdash_ptr = editor_hl_dx_boulderdash;
5236 static int *editor_el_dx_boulderdash_ptr = editor_el_dx_boulderdash;
5237 static int num_editor_hl_dx_boulderdash = ARRAY_SIZE(editor_hl_dx_boulderdash);
5238 static int num_editor_el_dx_boulderdash = ARRAY_SIZE(editor_el_dx_boulderdash);
5239
5240 static int editor_hl_mirror_magic[] =
5241 {
5242   EL_INTERNAL_CASCADE_MM_ACTIVE,
5243   EL_CHAR('M'),
5244   EL_CHAR('M'),
5245   EL_EMPTY,
5246 };
5247
5248 static int editor_el_mirror_magic[] =
5249 {
5250   EL_MM_MCDUFFIN_RIGHT,
5251   EL_MM_MCDUFFIN_UP,
5252   EL_MM_MCDUFFIN_LEFT,
5253   EL_MM_MCDUFFIN_DOWN,
5254
5255   EL_MM_MIRROR_START,
5256   EL_MM_MIRROR_FIXED_START,
5257   EL_MM_POLARIZER_START,
5258   EL_MM_POLARIZER_CROSS_START,
5259
5260   EL_MM_TELEPORTER_RED_START,
5261   EL_MM_TELEPORTER_YELLOW_START,
5262   EL_MM_TELEPORTER_GREEN_START,
5263   EL_MM_TELEPORTER_BLUE_START,
5264
5265   EL_MM_PRISM,
5266   EL_MM_FUSE_ACTIVE,
5267   EL_MM_PACMAN_RIGHT,
5268   EL_MM_EXIT_CLOSED,
5269
5270   EL_MM_KETTLE,
5271   EL_MM_BOMB,
5272   EL_MM_KEY,
5273   EL_MM_FUEL_FULL,
5274
5275   EL_MM_LIGHTBULB,
5276   EL_MM_LIGHTBULB_ACTIVE,
5277   EL_MM_GRAY_BALL,
5278   EL_MM_LIGHTBALL,
5279
5280   EL_MM_STEEL_WALL,
5281   EL_MM_WOODEN_WALL,
5282   EL_MM_ICE_WALL,
5283   EL_MM_AMOEBA_WALL,
5284
5285   EL_MM_STEEL_LOCK,
5286   EL_MM_WOODEN_LOCK,
5287   EL_MM_STEEL_BLOCK,
5288   EL_MM_WOODEN_BLOCK,
5289
5290   EL_MM_STEEL_GRID_FIXED_1,
5291   EL_MM_STEEL_GRID_FIXED_2,
5292   EL_MM_STEEL_GRID_FIXED_3,
5293   EL_MM_STEEL_GRID_FIXED_4,
5294
5295   EL_MM_WOODEN_GRID_FIXED_1,
5296   EL_MM_WOODEN_GRID_FIXED_2,
5297   EL_MM_WOODEN_GRID_FIXED_3,
5298   EL_MM_WOODEN_GRID_FIXED_4,
5299
5300   EL_MM_ENVELOPE_1,
5301   EL_MM_ENVELOPE_2,
5302   EL_MM_ENVELOPE_3,
5303   EL_MM_ENVELOPE_4
5304 };
5305 static int *editor_hl_mirror_magic_ptr = editor_hl_mirror_magic;
5306 static int *editor_el_mirror_magic_ptr = editor_el_mirror_magic;
5307 static int num_editor_hl_mirror_magic = ARRAY_SIZE(editor_hl_mirror_magic);
5308 static int num_editor_el_mirror_magic = ARRAY_SIZE(editor_el_mirror_magic);
5309
5310 static int editor_hl_deflektor[] =
5311 {
5312   EL_INTERNAL_CASCADE_DF_ACTIVE,
5313   EL_CHAR('D'),
5314   EL_CHAR('F'),
5315   EL_EMPTY,
5316 };
5317
5318 static int editor_el_deflektor[] =
5319 {
5320   EL_DF_LASER_RIGHT,
5321   EL_DF_LASER_UP,
5322   EL_DF_LASER_LEFT,
5323   EL_DF_LASER_DOWN,
5324
5325   EL_DF_RECEIVER_RIGHT,
5326   EL_DF_RECEIVER_UP,
5327   EL_DF_RECEIVER_LEFT,
5328   EL_DF_RECEIVER_DOWN,
5329
5330   EL_DF_MIRROR_START,
5331   EL_DF_MIRROR_ROTATING_START,
5332   EL_DF_MIRROR_FIXED_START,
5333   EL_DF_CELL,
5334
5335   EL_DF_FIBRE_OPTIC_RED_1,
5336   EL_DF_FIBRE_OPTIC_YELLOW_1,
5337   EL_DF_FIBRE_OPTIC_GREEN_1,
5338   EL_DF_FIBRE_OPTIC_BLUE_1,
5339
5340   EL_DF_STEEL_GRID_FIXED_START,
5341   EL_DF_STEEL_GRID_ROTATING_START,
5342   EL_DF_WOODEN_GRID_FIXED_START,
5343   EL_DF_WOODEN_GRID_ROTATING_START,
5344
5345   EL_DF_STEEL_WALL,
5346   EL_DF_WOODEN_WALL,
5347   EL_DF_REFRACTOR,
5348   EL_DF_MINE,
5349
5350   EL_DF_SLOPE_1,
5351   EL_DF_SLOPE_2,
5352   EL_DF_SLOPE_3,
5353   EL_DF_SLOPE_4
5354 };
5355 static int *editor_hl_deflektor_ptr = editor_hl_deflektor;
5356 static int *editor_el_deflektor_ptr = editor_el_deflektor;
5357 static int num_editor_hl_deflektor = ARRAY_SIZE(editor_hl_deflektor);
5358 static int num_editor_el_deflektor = ARRAY_SIZE(editor_el_deflektor);
5359
5360 static int editor_hl_chars[] =
5361 {
5362   EL_INTERNAL_CASCADE_CHARS_ACTIVE,
5363   EL_CHAR('T'),
5364   EL_CHAR('X'),
5365   EL_CHAR('T'),
5366 };
5367
5368 static int editor_el_chars[] =
5369 {
5370   EL_CHAR(' '),
5371   EL_CHAR('!'),
5372   EL_CHAR('"'),
5373   EL_CHAR('#'),
5374
5375   EL_CHAR('$'),
5376   EL_CHAR('%'),
5377   EL_CHAR('&'),
5378   EL_CHAR('\''),
5379
5380   EL_CHAR('('),
5381   EL_CHAR(')'),
5382   EL_CHAR('*'),
5383   EL_CHAR('+'),
5384
5385   EL_CHAR(','),
5386   EL_CHAR('-'),
5387   EL_CHAR('.'),
5388   EL_CHAR('/'),
5389
5390   EL_CHAR('0'),
5391   EL_CHAR('1'),
5392   EL_CHAR('2'),
5393   EL_CHAR('3'),
5394
5395   EL_CHAR('4'),
5396   EL_CHAR('5'),
5397   EL_CHAR('6'),
5398   EL_CHAR('7'),
5399
5400   EL_CHAR('8'),
5401   EL_CHAR('9'),
5402   EL_CHAR(':'),
5403   EL_CHAR(';'),
5404
5405   EL_CHAR('<'),
5406   EL_CHAR('='),
5407   EL_CHAR('>'),
5408   EL_CHAR('?'),
5409
5410   EL_CHAR('@'),
5411   EL_CHAR('A'),
5412   EL_CHAR('B'),
5413   EL_CHAR('C'),
5414
5415   EL_CHAR('D'),
5416   EL_CHAR('E'),
5417   EL_CHAR('F'),
5418   EL_CHAR('G'),
5419
5420   EL_CHAR('H'),
5421   EL_CHAR('I'),
5422   EL_CHAR('J'),
5423   EL_CHAR('K'),
5424
5425   EL_CHAR('L'),
5426   EL_CHAR('M'),
5427   EL_CHAR('N'),
5428   EL_CHAR('O'),
5429
5430   EL_CHAR('P'),
5431   EL_CHAR('Q'),
5432   EL_CHAR('R'),
5433   EL_CHAR('S'),
5434
5435   EL_CHAR('T'),
5436   EL_CHAR('U'),
5437   EL_CHAR('V'),
5438   EL_CHAR('W'),
5439
5440   EL_CHAR('X'),
5441   EL_CHAR('Y'),
5442   EL_CHAR('Z'),
5443   EL_CHAR('['),
5444
5445   EL_CHAR('\\'),
5446   EL_CHAR(']'),
5447   EL_CHAR('^'),
5448   EL_CHAR('_'),
5449
5450   EL_CHAR(CHAR_BYTE_COPYRIGHT),
5451   EL_CHAR(CHAR_BYTE_UMLAUT_A),
5452   EL_CHAR(CHAR_BYTE_UMLAUT_O),
5453   EL_CHAR(CHAR_BYTE_UMLAUT_U),
5454
5455   EL_CHAR(CHAR_BYTE_DEGREE),
5456   EL_CHAR(CHAR_BYTE_REGISTERED),
5457   EL_CHAR(FONT_ASCII_CURSOR),
5458   EL_CHAR(FONT_ASCII_BUTTON),
5459
5460   EL_CHAR(FONT_ASCII_UP),
5461   EL_CHAR(FONT_ASCII_DOWN),
5462   EL_CHAR(' '),
5463   EL_CHAR(' ')
5464 };
5465 static int *editor_hl_chars_ptr = editor_hl_chars;
5466 static int *editor_el_chars_ptr = editor_el_chars;
5467 static int num_editor_hl_chars = ARRAY_SIZE(editor_hl_chars);
5468 static int num_editor_el_chars = ARRAY_SIZE(editor_el_chars);
5469
5470 static int editor_hl_steel_chars[] =
5471 {
5472   EL_INTERNAL_CASCADE_STEEL_CHARS_ACTIVE,
5473   EL_STEEL_CHAR('T'),
5474   EL_STEEL_CHAR('X'),
5475   EL_STEEL_CHAR('T'),
5476 };
5477
5478 static int editor_el_steel_chars[] =
5479 {
5480   EL_STEEL_CHAR(' '),
5481   EL_STEEL_CHAR('!'),
5482   EL_STEEL_CHAR('"'),
5483   EL_STEEL_CHAR('#'),
5484
5485   EL_STEEL_CHAR('$'),
5486   EL_STEEL_CHAR('%'),
5487   EL_STEEL_CHAR('&'),
5488   EL_STEEL_CHAR('\''),
5489
5490   EL_STEEL_CHAR('('),
5491   EL_STEEL_CHAR(')'),
5492   EL_STEEL_CHAR('*'),
5493   EL_STEEL_CHAR('+'),
5494
5495   EL_STEEL_CHAR(','),
5496   EL_STEEL_CHAR('-'),
5497   EL_STEEL_CHAR('.'),
5498   EL_STEEL_CHAR('/'),
5499
5500   EL_STEEL_CHAR('0'),
5501   EL_STEEL_CHAR('1'),
5502   EL_STEEL_CHAR('2'),
5503   EL_STEEL_CHAR('3'),
5504
5505   EL_STEEL_CHAR('4'),
5506   EL_STEEL_CHAR('5'),
5507   EL_STEEL_CHAR('6'),
5508   EL_STEEL_CHAR('7'),
5509
5510   EL_STEEL_CHAR('8'),
5511   EL_STEEL_CHAR('9'),
5512   EL_STEEL_CHAR(':'),
5513   EL_STEEL_CHAR(';'),
5514
5515   EL_STEEL_CHAR('<'),
5516   EL_STEEL_CHAR('='),
5517   EL_STEEL_CHAR('>'),
5518   EL_STEEL_CHAR('?'),
5519
5520   EL_STEEL_CHAR('@'),
5521   EL_STEEL_CHAR('A'),
5522   EL_STEEL_CHAR('B'),
5523   EL_STEEL_CHAR('C'),
5524
5525   EL_STEEL_CHAR('D'),
5526   EL_STEEL_CHAR('E'),
5527   EL_STEEL_CHAR('F'),
5528   EL_STEEL_CHAR('G'),
5529
5530   EL_STEEL_CHAR('H'),
5531   EL_STEEL_CHAR('I'),
5532   EL_STEEL_CHAR('J'),
5533   EL_STEEL_CHAR('K'),
5534
5535   EL_STEEL_CHAR('L'),
5536   EL_STEEL_CHAR('M'),
5537   EL_STEEL_CHAR('N'),
5538   EL_STEEL_CHAR('O'),
5539
5540   EL_STEEL_CHAR('P'),
5541   EL_STEEL_CHAR('Q'),
5542   EL_STEEL_CHAR('R'),
5543   EL_STEEL_CHAR('S'),
5544
5545   EL_STEEL_CHAR('T'),
5546   EL_STEEL_CHAR('U'),
5547   EL_STEEL_CHAR('V'),
5548   EL_STEEL_CHAR('W'),
5549
5550   EL_STEEL_CHAR('X'),
5551   EL_STEEL_CHAR('Y'),
5552   EL_STEEL_CHAR('Z'),
5553   EL_STEEL_CHAR('['),
5554
5555   EL_STEEL_CHAR('\\'),
5556   EL_STEEL_CHAR(']'),
5557   EL_STEEL_CHAR('^'),
5558   EL_STEEL_CHAR('_'),
5559
5560   EL_STEEL_CHAR(CHAR_BYTE_COPYRIGHT),
5561   EL_STEEL_CHAR(CHAR_BYTE_UMLAUT_A),
5562   EL_STEEL_CHAR(CHAR_BYTE_UMLAUT_O),
5563   EL_STEEL_CHAR(CHAR_BYTE_UMLAUT_U),
5564
5565   EL_STEEL_CHAR(CHAR_BYTE_DEGREE),
5566   EL_STEEL_CHAR(CHAR_BYTE_REGISTERED),
5567   EL_STEEL_CHAR(FONT_ASCII_CURSOR),
5568   EL_STEEL_CHAR(FONT_ASCII_BUTTON),
5569
5570   EL_STEEL_CHAR(FONT_ASCII_UP),
5571   EL_STEEL_CHAR(FONT_ASCII_DOWN),
5572   EL_STEEL_CHAR(' '),
5573   EL_STEEL_CHAR(' ')
5574 };
5575 static int *editor_hl_steel_chars_ptr = editor_hl_steel_chars;
5576 static int *editor_el_steel_chars_ptr = editor_el_steel_chars;
5577 static int num_editor_hl_steel_chars = ARRAY_SIZE(editor_hl_steel_chars);
5578 static int num_editor_el_steel_chars = ARRAY_SIZE(editor_el_steel_chars);
5579
5580 static int editor_hl_custom[] =
5581 {
5582   EL_INTERNAL_CASCADE_CE_ACTIVE,
5583   EL_CHAR('C'),
5584   EL_CHAR('E'),
5585   EL_EMPTY,
5586 };
5587
5588 static int editor_el_custom[] =
5589 {
5590   EL_CUSTOM_START + 0,
5591   EL_CUSTOM_START + 1,
5592   EL_CUSTOM_START + 2,
5593   EL_CUSTOM_START + 3,
5594
5595   EL_CUSTOM_START + 4,
5596   EL_CUSTOM_START + 5,
5597   EL_CUSTOM_START + 6,
5598   EL_CUSTOM_START + 7,
5599
5600   EL_CUSTOM_START + 8,
5601   EL_CUSTOM_START + 9,
5602   EL_CUSTOM_START + 10,
5603   EL_CUSTOM_START + 11,
5604
5605   EL_CUSTOM_START + 12,
5606   EL_CUSTOM_START + 13,
5607   EL_CUSTOM_START + 14,
5608   EL_CUSTOM_START + 15,
5609
5610   EL_CUSTOM_START + 16,
5611   EL_CUSTOM_START + 17,
5612   EL_CUSTOM_START + 18,
5613   EL_CUSTOM_START + 19,
5614
5615   EL_CUSTOM_START + 20,
5616   EL_CUSTOM_START + 21,
5617   EL_CUSTOM_START + 22,
5618   EL_CUSTOM_START + 23,
5619
5620   EL_CUSTOM_START + 24,
5621   EL_CUSTOM_START + 25,
5622   EL_CUSTOM_START + 26,
5623   EL_CUSTOM_START + 27,
5624
5625   EL_CUSTOM_START + 28,
5626   EL_CUSTOM_START + 29,
5627   EL_CUSTOM_START + 30,
5628   EL_CUSTOM_START + 31,
5629
5630   EL_CUSTOM_START + 32,
5631   EL_CUSTOM_START + 33,
5632   EL_CUSTOM_START + 34,
5633   EL_CUSTOM_START + 35,
5634
5635   EL_CUSTOM_START + 36,
5636   EL_CUSTOM_START + 37,
5637   EL_CUSTOM_START + 38,
5638   EL_CUSTOM_START + 39,
5639
5640   EL_CUSTOM_START + 40,
5641   EL_CUSTOM_START + 41,
5642   EL_CUSTOM_START + 42,
5643   EL_CUSTOM_START + 43,
5644
5645   EL_CUSTOM_START + 44,
5646   EL_CUSTOM_START + 45,
5647   EL_CUSTOM_START + 46,
5648   EL_CUSTOM_START + 47,
5649
5650   EL_CUSTOM_START + 48,
5651   EL_CUSTOM_START + 49,
5652   EL_CUSTOM_START + 50,
5653   EL_CUSTOM_START + 51,
5654
5655   EL_CUSTOM_START + 52,
5656   EL_CUSTOM_START + 53,
5657   EL_CUSTOM_START + 54,
5658   EL_CUSTOM_START + 55,
5659
5660   EL_CUSTOM_START + 56,
5661   EL_CUSTOM_START + 57,
5662   EL_CUSTOM_START + 58,
5663   EL_CUSTOM_START + 59,
5664
5665   EL_CUSTOM_START + 60,
5666   EL_CUSTOM_START + 61,
5667   EL_CUSTOM_START + 62,
5668   EL_CUSTOM_START + 63,
5669
5670   EL_CUSTOM_START + 64,
5671   EL_CUSTOM_START + 65,
5672   EL_CUSTOM_START + 66,
5673   EL_CUSTOM_START + 67,
5674
5675   EL_CUSTOM_START + 68,
5676   EL_CUSTOM_START + 69,
5677   EL_CUSTOM_START + 70,
5678   EL_CUSTOM_START + 71,
5679
5680   EL_CUSTOM_START + 72,
5681   EL_CUSTOM_START + 73,
5682   EL_CUSTOM_START + 74,
5683   EL_CUSTOM_START + 75,
5684
5685   EL_CUSTOM_START + 76,
5686   EL_CUSTOM_START + 77,
5687   EL_CUSTOM_START + 78,
5688   EL_CUSTOM_START + 79,
5689
5690   EL_CUSTOM_START + 80,
5691   EL_CUSTOM_START + 81,
5692   EL_CUSTOM_START + 82,
5693   EL_CUSTOM_START + 83,
5694
5695   EL_CUSTOM_START + 84,
5696   EL_CUSTOM_START + 85,
5697   EL_CUSTOM_START + 86,
5698   EL_CUSTOM_START + 87,
5699
5700   EL_CUSTOM_START + 88,
5701   EL_CUSTOM_START + 89,
5702   EL_CUSTOM_START + 90,
5703   EL_CUSTOM_START + 91,
5704
5705   EL_CUSTOM_START + 92,
5706   EL_CUSTOM_START + 93,
5707   EL_CUSTOM_START + 94,
5708   EL_CUSTOM_START + 95,
5709
5710   EL_CUSTOM_START + 96,
5711   EL_CUSTOM_START + 97,
5712   EL_CUSTOM_START + 98,
5713   EL_CUSTOM_START + 99,
5714
5715   EL_CUSTOM_START + 100,
5716   EL_CUSTOM_START + 101,
5717   EL_CUSTOM_START + 102,
5718   EL_CUSTOM_START + 103,
5719
5720   EL_CUSTOM_START + 104,
5721   EL_CUSTOM_START + 105,
5722   EL_CUSTOM_START + 106,
5723   EL_CUSTOM_START + 107,
5724
5725   EL_CUSTOM_START + 108,
5726   EL_CUSTOM_START + 109,
5727   EL_CUSTOM_START + 110,
5728   EL_CUSTOM_START + 111,
5729
5730   EL_CUSTOM_START + 112,
5731   EL_CUSTOM_START + 113,
5732   EL_CUSTOM_START + 114,
5733   EL_CUSTOM_START + 115,
5734
5735   EL_CUSTOM_START + 116,
5736   EL_CUSTOM_START + 117,
5737   EL_CUSTOM_START + 118,
5738   EL_CUSTOM_START + 119,
5739
5740   EL_CUSTOM_START + 120,
5741   EL_CUSTOM_START + 121,
5742   EL_CUSTOM_START + 122,
5743   EL_CUSTOM_START + 123,
5744
5745   EL_CUSTOM_START + 124,
5746   EL_CUSTOM_START + 125,
5747   EL_CUSTOM_START + 126,
5748   EL_CUSTOM_START + 127,
5749
5750   EL_CUSTOM_START + 128,
5751   EL_CUSTOM_START + 129,
5752   EL_CUSTOM_START + 130,
5753   EL_CUSTOM_START + 131,
5754
5755   EL_CUSTOM_START + 132,
5756   EL_CUSTOM_START + 133,
5757   EL_CUSTOM_START + 134,
5758   EL_CUSTOM_START + 135,
5759
5760   EL_CUSTOM_START + 136,
5761   EL_CUSTOM_START + 137,
5762   EL_CUSTOM_START + 138,
5763   EL_CUSTOM_START + 139,
5764
5765   EL_CUSTOM_START + 140,
5766   EL_CUSTOM_START + 141,
5767   EL_CUSTOM_START + 142,
5768   EL_CUSTOM_START + 143,
5769
5770   EL_CUSTOM_START + 144,
5771   EL_CUSTOM_START + 145,
5772   EL_CUSTOM_START + 146,
5773   EL_CUSTOM_START + 147,
5774
5775   EL_CUSTOM_START + 148,
5776   EL_CUSTOM_START + 149,
5777   EL_CUSTOM_START + 150,
5778   EL_CUSTOM_START + 151,
5779
5780   EL_CUSTOM_START + 152,
5781   EL_CUSTOM_START + 153,
5782   EL_CUSTOM_START + 154,
5783   EL_CUSTOM_START + 155,
5784
5785   EL_CUSTOM_START + 156,
5786   EL_CUSTOM_START + 157,
5787   EL_CUSTOM_START + 158,
5788   EL_CUSTOM_START + 159,
5789
5790   EL_CUSTOM_START + 160,
5791   EL_CUSTOM_START + 161,
5792   EL_CUSTOM_START + 162,
5793   EL_CUSTOM_START + 163,
5794
5795   EL_CUSTOM_START + 164,
5796   EL_CUSTOM_START + 165,
5797   EL_CUSTOM_START + 166,
5798   EL_CUSTOM_START + 167,
5799
5800   EL_CUSTOM_START + 168,
5801   EL_CUSTOM_START + 169,
5802   EL_CUSTOM_START + 170,
5803   EL_CUSTOM_START + 171,
5804
5805   EL_CUSTOM_START + 172,
5806   EL_CUSTOM_START + 173,
5807   EL_CUSTOM_START + 174,
5808   EL_CUSTOM_START + 175,
5809
5810   EL_CUSTOM_START + 176,
5811   EL_CUSTOM_START + 177,
5812   EL_CUSTOM_START + 178,
5813   EL_CUSTOM_START + 179,
5814
5815   EL_CUSTOM_START + 180,
5816   EL_CUSTOM_START + 181,
5817   EL_CUSTOM_START + 182,
5818   EL_CUSTOM_START + 183,
5819
5820   EL_CUSTOM_START + 184,
5821   EL_CUSTOM_START + 185,
5822   EL_CUSTOM_START + 186,
5823   EL_CUSTOM_START + 187,
5824
5825   EL_CUSTOM_START + 188,
5826   EL_CUSTOM_START + 189,
5827   EL_CUSTOM_START + 190,
5828   EL_CUSTOM_START + 191,
5829
5830   EL_CUSTOM_START + 192,
5831   EL_CUSTOM_START + 193,
5832   EL_CUSTOM_START + 194,
5833   EL_CUSTOM_START + 195,
5834
5835   EL_CUSTOM_START + 196,
5836   EL_CUSTOM_START + 197,
5837   EL_CUSTOM_START + 198,
5838   EL_CUSTOM_START + 199,
5839
5840   EL_CUSTOM_START + 200,
5841   EL_CUSTOM_START + 201,
5842   EL_CUSTOM_START + 202,
5843   EL_CUSTOM_START + 203,
5844
5845   EL_CUSTOM_START + 204,
5846   EL_CUSTOM_START + 205,
5847   EL_CUSTOM_START + 206,
5848   EL_CUSTOM_START + 207,
5849
5850   EL_CUSTOM_START + 208,
5851   EL_CUSTOM_START + 209,
5852   EL_CUSTOM_START + 210,
5853   EL_CUSTOM_START + 211,
5854
5855   EL_CUSTOM_START + 212,
5856   EL_CUSTOM_START + 213,
5857   EL_CUSTOM_START + 214,
5858   EL_CUSTOM_START + 215,
5859
5860   EL_CUSTOM_START + 216,
5861   EL_CUSTOM_START + 217,
5862   EL_CUSTOM_START + 218,
5863   EL_CUSTOM_START + 219,
5864
5865   EL_CUSTOM_START + 220,
5866   EL_CUSTOM_START + 221,
5867   EL_CUSTOM_START + 222,
5868   EL_CUSTOM_START + 223,
5869
5870   EL_CUSTOM_START + 224,
5871   EL_CUSTOM_START + 225,
5872   EL_CUSTOM_START + 226,
5873   EL_CUSTOM_START + 227,
5874
5875   EL_CUSTOM_START + 228,
5876   EL_CUSTOM_START + 229,
5877   EL_CUSTOM_START + 230,
5878   EL_CUSTOM_START + 231,
5879
5880   EL_CUSTOM_START + 232,
5881   EL_CUSTOM_START + 233,
5882   EL_CUSTOM_START + 234,
5883   EL_CUSTOM_START + 235,
5884
5885   EL_CUSTOM_START + 236,
5886   EL_CUSTOM_START + 237,
5887   EL_CUSTOM_START + 238,
5888   EL_CUSTOM_START + 239,
5889
5890   EL_CUSTOM_START + 240,
5891   EL_CUSTOM_START + 241,
5892   EL_CUSTOM_START + 242,
5893   EL_CUSTOM_START + 243,
5894
5895   EL_CUSTOM_START + 244,
5896   EL_CUSTOM_START + 245,
5897   EL_CUSTOM_START + 246,
5898   EL_CUSTOM_START + 247,
5899
5900   EL_CUSTOM_START + 248,
5901   EL_CUSTOM_START + 249,
5902   EL_CUSTOM_START + 250,
5903   EL_CUSTOM_START + 251,
5904
5905   EL_CUSTOM_START + 252,
5906   EL_CUSTOM_START + 253,
5907   EL_CUSTOM_START + 254,
5908   EL_CUSTOM_START + 255
5909 };
5910 static int *editor_hl_custom_ptr = editor_hl_custom;
5911 static int *editor_el_custom_ptr = editor_el_custom;
5912 static int num_editor_hl_custom = ARRAY_SIZE(editor_hl_custom);
5913 static int num_editor_el_custom = ARRAY_SIZE(editor_el_custom);
5914
5915 static int editor_hl_group[] =
5916 {
5917   EL_INTERNAL_CASCADE_GE_ACTIVE,
5918   EL_CHAR('G'),
5919   EL_CHAR('E'),
5920   EL_EMPTY,
5921 };
5922
5923 static int editor_el_group[] =
5924 {
5925   EL_GROUP_START + 0,
5926   EL_GROUP_START + 1,
5927   EL_GROUP_START + 2,
5928   EL_GROUP_START + 3,
5929
5930   EL_GROUP_START + 4,
5931   EL_GROUP_START + 5,
5932   EL_GROUP_START + 6,
5933   EL_GROUP_START + 7,
5934
5935   EL_GROUP_START + 8,
5936   EL_GROUP_START + 9,
5937   EL_GROUP_START + 10,
5938   EL_GROUP_START + 11,
5939
5940   EL_GROUP_START + 12,
5941   EL_GROUP_START + 13,
5942   EL_GROUP_START + 14,
5943   EL_GROUP_START + 15,
5944
5945   EL_GROUP_START + 16,
5946   EL_GROUP_START + 17,
5947   EL_GROUP_START + 18,
5948   EL_GROUP_START + 19,
5949
5950   EL_GROUP_START + 20,
5951   EL_GROUP_START + 21,
5952   EL_GROUP_START + 22,
5953   EL_GROUP_START + 23,
5954
5955   EL_GROUP_START + 24,
5956   EL_GROUP_START + 25,
5957   EL_GROUP_START + 26,
5958   EL_GROUP_START + 27,
5959
5960   EL_GROUP_START + 28,
5961   EL_GROUP_START + 29,
5962   EL_GROUP_START + 30,
5963   EL_GROUP_START + 31
5964 };
5965 static int *editor_hl_group_ptr = editor_hl_group;
5966 static int *editor_el_group_ptr = editor_el_group;
5967 static int num_editor_hl_group = ARRAY_SIZE(editor_hl_group);
5968 static int num_editor_el_group = ARRAY_SIZE(editor_el_group);
5969
5970 static int editor_hl_empty_space[] =
5971 {
5972   EL_INTERNAL_CASCADE_ES_ACTIVE,
5973   EL_CHAR('E'),
5974   EL_CHAR('S'),
5975   EL_EMPTY,
5976 };
5977
5978 static int editor_el_empty_space[] =
5979 {
5980   EL_EMPTY_SPACE_1,
5981   EL_EMPTY_SPACE_2,
5982   EL_EMPTY_SPACE_3,
5983   EL_EMPTY_SPACE_4,
5984
5985   EL_EMPTY_SPACE_5,
5986   EL_EMPTY_SPACE_6,
5987   EL_EMPTY_SPACE_7,
5988   EL_EMPTY_SPACE_8,
5989
5990   EL_EMPTY_SPACE_9,
5991   EL_EMPTY_SPACE_10,
5992   EL_EMPTY_SPACE_11,
5993   EL_EMPTY_SPACE_12,
5994
5995   EL_EMPTY_SPACE_13,
5996   EL_EMPTY_SPACE_14,
5997   EL_EMPTY_SPACE_15,
5998   EL_EMPTY_SPACE_16
5999 };
6000 static int *editor_hl_empty_space_ptr = editor_hl_empty_space;
6001 static int *editor_el_empty_space_ptr = editor_el_empty_space;
6002 static int num_editor_hl_empty_space = ARRAY_SIZE(editor_hl_empty_space);
6003 static int num_editor_el_empty_space = ARRAY_SIZE(editor_el_empty_space);
6004
6005 static int editor_hl_reference[] =
6006 {
6007   EL_INTERNAL_CASCADE_REF_ACTIVE,
6008   EL_CHAR('R'),
6009   EL_CHAR('E'),
6010   EL_CHAR('F')
6011 };
6012
6013 static int editor_el_reference[] =
6014 {
6015   EL_TRIGGER_PLAYER,
6016   EL_TRIGGER_ELEMENT,
6017   EL_TRIGGER_CE_VALUE,
6018   EL_TRIGGER_CE_SCORE,
6019
6020   EL_SELF,
6021   EL_ANY_ELEMENT,
6022   EL_CURRENT_CE_VALUE,
6023   EL_CURRENT_CE_SCORE,
6024
6025   EL_PREV_CE_8,
6026   EL_PREV_CE_7,
6027   EL_PREV_CE_6,
6028   EL_PREV_CE_5,
6029
6030   EL_PREV_CE_4,
6031   EL_PREV_CE_3,
6032   EL_PREV_CE_2,
6033   EL_PREV_CE_1,
6034
6035   EL_NEXT_CE_1,
6036   EL_NEXT_CE_2,
6037   EL_NEXT_CE_3,
6038   EL_NEXT_CE_4,
6039
6040   EL_NEXT_CE_5,
6041   EL_NEXT_CE_6,
6042   EL_NEXT_CE_7,
6043   EL_NEXT_CE_8,
6044 };
6045 static int *editor_hl_reference_ptr = editor_hl_reference;
6046 static int *editor_el_reference_ptr = editor_el_reference;
6047 static int num_editor_hl_reference = ARRAY_SIZE(editor_hl_reference);
6048 static int num_editor_el_reference = ARRAY_SIZE(editor_el_reference);
6049
6050 static int editor_hl_user_defined[] =
6051 {
6052   EL_INTERNAL_CASCADE_USER_ACTIVE,
6053   EL_CHAR('M'),
6054   EL_CHAR('Y'),
6055   EL_EMPTY,
6056 };
6057
6058 static int *editor_hl_user_defined_ptr = editor_hl_user_defined;
6059 static int *editor_el_user_defined_ptr = NULL;
6060 static int num_editor_hl_user_defined = ARRAY_SIZE(editor_hl_user_defined);
6061 static int num_editor_el_user_defined = 0;
6062
6063 static int editor_hl_dynamic[] =
6064 {
6065   EL_INTERNAL_CASCADE_DYNAMIC_ACTIVE,
6066   EL_CHAR('U'),
6067   EL_CHAR('S'),
6068   EL_CHAR('E'),
6069 };
6070
6071 static int *editor_hl_dynamic_ptr = editor_hl_dynamic;
6072 static int *editor_el_dynamic_ptr = NULL;
6073 static int num_editor_hl_dynamic = ARRAY_SIZE(editor_hl_dynamic);
6074 static int num_editor_el_dynamic = 0;
6075
6076 static int editor_hl_empty[] = { EL_EMPTY };
6077 static int *editor_el_empty = NULL;     // dynamically allocated
6078
6079 static int *editor_hl_empty_ptr = editor_hl_empty;
6080 static int *editor_el_empty_ptr = NULL;
6081 static int num_editor_hl_empty = 0;
6082 static int num_editor_el_empty = 0;     // dynamically determined, if needed
6083
6084 static boolean use_el_empty = FALSE;
6085
6086 static int *editor_elements = NULL;     // dynamically allocated
6087 static int num_editor_elements = 0;     // dynamically determined
6088
6089 static boolean setup_editor_cascade_never = FALSE;
6090
6091 static boolean setup_editor_el_players                  = TRUE;
6092 static boolean setup_editor_el_boulderdash              = TRUE;
6093 static boolean setup_editor_el_boulderdash_native       = TRUE;
6094 static boolean setup_editor_el_emerald_mine             = TRUE;
6095 static boolean setup_editor_el_emerald_mine_club        = TRUE;
6096 static boolean setup_editor_el_more                     = TRUE;
6097 static boolean setup_editor_el_sokoban                  = TRUE;
6098 static boolean setup_editor_el_supaplex                 = TRUE;
6099 static boolean setup_editor_el_diamond_caves            = TRUE;
6100 static boolean setup_editor_el_dx_boulderdash           = TRUE;
6101 static boolean setup_editor_el_mirror_magic             = TRUE;
6102 static boolean setup_editor_el_deflektor                = TRUE;
6103 static boolean setup_editor_el_chars                    = TRUE;
6104 static boolean setup_editor_el_steel_chars              = TRUE;
6105 static boolean setup_editor_el_custom                   = TRUE;
6106 static boolean setup_editor_el_user_defined             = TRUE;
6107 static boolean setup_editor_el_dynamic                  = TRUE;
6108
6109 static int editor_hl_unused[] = { EL_EMPTY };
6110 static int *editor_hl_unused_ptr = editor_hl_unused;
6111 static int num_editor_hl_unused = 0;
6112
6113 static struct
6114 {
6115   boolean *setup_value;
6116   boolean *setup_cascade_value;
6117
6118   int **headline_list;
6119   int *headline_list_size;
6120
6121   int **element_list;
6122   int *element_list_size;
6123
6124   boolean last_setup_value;
6125 }
6126 editor_elements_info[] =
6127 {
6128   {
6129     &setup_editor_el_players,
6130     &setup_editor_cascade_never,
6131     &editor_hl_unused_ptr,              &num_editor_hl_unused,
6132     &editor_el_players_ptr,             &num_editor_el_players
6133   },
6134   {
6135     &setup_editor_el_boulderdash,
6136     &setup.editor_cascade.el_bd,
6137     &editor_hl_boulderdash_ptr,         &num_editor_hl_boulderdash,
6138     &editor_el_boulderdash_ptr,         &num_editor_el_boulderdash
6139   },
6140   {
6141     &setup_editor_el_boulderdash_native,
6142     &setup.editor_cascade.el_bd_native,
6143     &editor_hl_boulderdash_native_ptr,  &num_editor_hl_boulderdash_native,
6144     &editor_el_boulderdash_native_ptr,  &num_editor_el_boulderdash_native
6145   },
6146   {
6147     &setup_editor_el_emerald_mine,
6148     &setup.editor_cascade.el_em,
6149     &editor_hl_emerald_mine_ptr,        &num_editor_hl_emerald_mine,
6150     &editor_el_emerald_mine_ptr,        &num_editor_el_emerald_mine
6151   },
6152   {
6153     &setup_editor_el_emerald_mine_club,
6154     &setup.editor_cascade.el_emc,
6155     &editor_hl_emerald_mine_club_ptr,   &num_editor_hl_emerald_mine_club,
6156     &editor_el_emerald_mine_club_ptr,   &num_editor_el_emerald_mine_club
6157   },
6158   {
6159     &setup_editor_el_more,
6160     &setup.editor_cascade.el_rnd,
6161     &editor_hl_rnd_ptr,                 &num_editor_hl_rnd,
6162     &editor_el_rnd_ptr,                 &num_editor_el_rnd
6163   },
6164   {
6165     &setup_editor_el_sokoban,
6166     &setup.editor_cascade.el_sb,
6167     &editor_hl_sokoban_ptr,             &num_editor_hl_sokoban,
6168     &editor_el_sokoban_ptr,             &num_editor_el_sokoban
6169   },
6170   {
6171     &setup_editor_el_supaplex,
6172     &setup.editor_cascade.el_sp,
6173     &editor_hl_supaplex_ptr,            &num_editor_hl_supaplex,
6174     &editor_el_supaplex_ptr,            &num_editor_el_supaplex
6175   },
6176   {
6177     &setup_editor_el_diamond_caves,
6178     &setup.editor_cascade.el_dc,
6179     &editor_hl_diamond_caves_ptr,       &num_editor_hl_diamond_caves,
6180     &editor_el_diamond_caves_ptr,       &num_editor_el_diamond_caves
6181   },
6182   {
6183     &setup_editor_el_dx_boulderdash,
6184     &setup.editor_cascade.el_dx,
6185     &editor_hl_dx_boulderdash_ptr,      &num_editor_hl_dx_boulderdash,
6186     &editor_el_dx_boulderdash_ptr,      &num_editor_el_dx_boulderdash
6187   },
6188   {
6189     &setup_editor_el_mirror_magic,
6190     &setup.editor_cascade.el_mm,
6191     &editor_hl_mirror_magic_ptr,        &num_editor_hl_mirror_magic,
6192     &editor_el_mirror_magic_ptr,        &num_editor_el_mirror_magic
6193   },
6194   {
6195     &setup_editor_el_deflektor,
6196     &setup.editor_cascade.el_df,
6197     &editor_hl_deflektor_ptr,           &num_editor_hl_deflektor,
6198     &editor_el_deflektor_ptr,           &num_editor_el_deflektor
6199   },
6200   {
6201     &setup_editor_el_chars,
6202     &setup.editor_cascade.el_chars,
6203     &editor_hl_chars_ptr,               &num_editor_hl_chars,
6204     &editor_el_chars_ptr,               &num_editor_el_chars
6205   },
6206   {
6207     &setup_editor_el_steel_chars,
6208     &setup.editor_cascade.el_steel_chars,
6209     &editor_hl_steel_chars_ptr,         &num_editor_hl_steel_chars,
6210     &editor_el_steel_chars_ptr,         &num_editor_el_steel_chars
6211   },
6212   {
6213     &setup_editor_el_custom,
6214     &setup.editor_cascade.el_ce,
6215     &editor_hl_custom_ptr,              &num_editor_hl_custom,
6216     &editor_el_custom_ptr,              &num_editor_el_custom
6217   },
6218   {
6219     &setup_editor_el_custom,
6220     &setup.editor_cascade.el_ge,
6221     &editor_hl_group_ptr,               &num_editor_hl_group,
6222     &editor_el_group_ptr,               &num_editor_el_group
6223   },
6224   {
6225     &setup_editor_el_custom,
6226     &setup.editor_cascade.el_es,
6227     &editor_hl_empty_space_ptr,         &num_editor_hl_empty_space,
6228     &editor_el_empty_space_ptr,         &num_editor_el_empty_space
6229   },
6230   {
6231     &setup_editor_el_custom,
6232     &setup.editor_cascade.el_ref,
6233     &editor_hl_reference_ptr,           &num_editor_hl_reference,
6234     &editor_el_reference_ptr,           &num_editor_el_reference
6235   },
6236   {
6237     &setup_editor_el_user_defined,
6238     &setup.editor_cascade.el_user,
6239     &editor_hl_user_defined_ptr,        &num_editor_hl_user_defined,
6240     &editor_el_user_defined_ptr,        &num_editor_el_user_defined
6241   },
6242   {
6243     &setup_editor_el_dynamic,
6244     &setup.editor_cascade.el_dynamic,
6245     &editor_hl_dynamic_ptr,             &num_editor_hl_dynamic,
6246     &editor_el_dynamic_ptr,             &num_editor_el_dynamic,
6247   },
6248   {
6249     &use_el_empty,
6250     &use_el_empty,
6251     &editor_hl_empty_ptr,               &num_editor_hl_empty,
6252     &editor_el_empty_ptr,               &num_editor_el_empty,
6253   },
6254   {
6255     NULL,
6256     NULL,
6257     NULL,                               NULL,
6258     NULL,                               NULL
6259   }
6260 };
6261
6262 static struct XY xy_directions[] =
6263 {
6264   { -1,  0 },
6265   { +1,  0 },
6266   {  0, -1 },
6267   {  0, +1 }
6268 };
6269
6270
6271 // ----------------------------------------------------------------------------
6272 // functions
6273 // ----------------------------------------------------------------------------
6274
6275 static int getMaxInfoTextLength(void)
6276 {
6277   return (SXSIZE / getFontWidth(INFOTEXT_FONT));
6278 }
6279
6280 static int getTextWidthForGadget(char *text)
6281 {
6282   if (text == NULL)
6283     return 0;
6284
6285   return (getTextWidth(text, FONT_TEXT_1) + ED_GADGET_TEXT_DISTANCE);
6286 }
6287
6288 static int getTextWidthForDrawingArea(char *text)
6289 {
6290   if (text == NULL)
6291     return 0;
6292
6293   return (getTextWidth(text, FONT_TEXT_1) + ED_DRAWINGAREA_TEXT_DISTANCE);
6294 }
6295
6296 static int getRightGadgetBorder(struct GadgetInfo *gi, char *text)
6297 {
6298   return (gi->x + gi->width + getTextWidthForGadget(text));
6299 }
6300
6301 static char *getElementInfoText(int element)
6302 {
6303   char *info_text = NULL;
6304
6305   if (element < MAX_NUM_ELEMENTS)
6306   {
6307     if (strlen(element_info[element].description) > 0)
6308       info_text = element_info[element].description;
6309     else if (element_info[element].custom_description != NULL)
6310       info_text = element_info[element].custom_description;
6311     else if (element_info[element].editor_description != NULL)
6312       info_text = element_info[element].editor_description;
6313   }
6314
6315   if (info_text == NULL)
6316     info_text = INFOTEXT_UNKNOWN_ELEMENT;
6317
6318   return info_text;
6319 }
6320
6321 static char *getElementDescriptionFilenameExt(char *basename)
6322 {
6323   char *elements_subdir = ELEMENTS_DIRECTORY;
6324   static char *elements_subdir2 = NULL;
6325   static char *filename = NULL;
6326
6327   if (elements_subdir2 == NULL)
6328     elements_subdir2 = getPath2(DOCS_DIRECTORY, elements_subdir);
6329
6330   checked_free(filename);
6331
6332   // 1st try: look for element description in current level set directory
6333   filename = getPath3(getCurrentLevelDir(), elements_subdir2, basename);
6334   if (fileExists(filename))
6335     return filename;
6336
6337   free(filename);
6338
6339   // 2nd try: look for element description in the game's base directory
6340   filename = getPath3(options.docs_directory, elements_subdir, basename);
6341   if (fileExists(filename))
6342     return filename;
6343
6344   return NULL;
6345 }
6346
6347 static char *getElementDescriptionFilename(int element)
6348 {
6349   char basename[MAX_FILENAME_LEN];
6350   char *filename;
6351
6352   // 1st try: look for element description file for exactly this element
6353   sprintf(basename, "%s.txt", element_info[element].token_name);
6354   filename = getElementDescriptionFilenameExt(basename);
6355   if (filename != NULL)
6356     return filename;
6357
6358   // 2nd try: look for element description file for this element's class
6359   sprintf(basename, "%s.txt", element_info[element].class_name);
6360   filename = getElementDescriptionFilenameExt(basename);
6361   if (filename != NULL)
6362     return filename;
6363
6364   // 3rd try: look for generic fallback text file for any element
6365   filename = getElementDescriptionFilenameExt(FALLBACK_TEXT_FILENAME);
6366   if (filename != NULL)
6367     return filename;
6368
6369   return NULL;
6370 }
6371
6372 static boolean suppressBorderElement(void)
6373 {
6374   return (level.game_engine_type == GAME_ENGINE_TYPE_MM &&
6375           lev_fieldx <= MAX_ED_FIELDX &&
6376           lev_fieldy <= MAX_ED_FIELDY);
6377 }
6378
6379 static void InitDynamicEditorElementList(int **elements, int *num_elements)
6380 {
6381   boolean element_found[NUM_FILE_ELEMENTS];
6382   int i, x, y;
6383
6384   // initialize list of used elements to "not used"
6385   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6386     element_found[i] = FALSE;
6387
6388   // find all elements used in current level
6389   for (y = 0; y < lev_fieldy; y++)
6390   {
6391     for (x = 0; x < lev_fieldx; x++)
6392     {
6393       if (Tile[x][y] >= NUM_FILE_ELEMENTS)      // should never happen
6394         continue;
6395
6396       if (IS_MM_WALL(Tile[x][y]))
6397         element_found[map_mm_wall_element(Tile[x][y])] = TRUE;
6398       else
6399         element_found[Tile[x][y]] = TRUE;
6400     }
6401   }
6402
6403   *num_elements = 0;
6404
6405   // count number of elements used in current level
6406   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6407     if (element_found[i])
6408       (*num_elements)++;
6409
6410   // add space for up to 3 more elements for padding that may be needed
6411   *num_elements += 3;
6412
6413   // free memory for old list of elements, if needed
6414   checked_free(*elements);
6415
6416   // allocate memory for new list of elements
6417   *elements = checked_malloc(*num_elements * sizeof(int));
6418
6419   *num_elements = 0;
6420
6421   // add all elements used in current level (non-custom/group/empty elements)
6422   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6423     if (element_found[i] && !(IS_CUSTOM_ELEMENT(i) ||
6424                               IS_GROUP_ELEMENT(i) ||
6425                               IS_EMPTY_ELEMENT(i)))
6426       (*elements)[(*num_elements)++] = i;
6427
6428   // add all elements used in current level (custom/group/empty elements)
6429   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6430     if (element_found[i] && (IS_CUSTOM_ELEMENT(i) ||
6431                              IS_GROUP_ELEMENT(i) ||
6432                              IS_EMPTY_ELEMENT(i)))
6433       (*elements)[(*num_elements)++] = i;
6434
6435   while (*num_elements % 4)     // pad with empty elements, if needed
6436     (*elements)[(*num_elements)++] = EL_EMPTY;
6437 }
6438
6439 static void ReinitializeElementList_EnableSections(void)
6440 {
6441   // default: enable all element sections
6442
6443   setup_editor_el_players               = TRUE;
6444   setup_editor_el_boulderdash           = TRUE;
6445   setup_editor_el_boulderdash_native    = TRUE;
6446   setup_editor_el_emerald_mine          = TRUE;
6447   setup_editor_el_emerald_mine_club     = TRUE;
6448   setup_editor_el_more                  = TRUE;
6449   setup_editor_el_sokoban               = TRUE;
6450   setup_editor_el_supaplex              = TRUE;
6451   setup_editor_el_diamond_caves         = TRUE;
6452   setup_editor_el_dx_boulderdash        = TRUE;
6453   setup_editor_el_mirror_magic          = TRUE;
6454   setup_editor_el_deflektor             = TRUE;
6455   setup_editor_el_chars                 = TRUE;
6456   setup_editor_el_steel_chars           = TRUE;
6457
6458   setup_editor_el_custom                = TRUE;
6459   setup_editor_el_user_defined          = TRUE;
6460   setup_editor_el_dynamic               = TRUE;
6461
6462   // now disable all element sections not to be displayed
6463
6464   if (!setup.editor.el_classic)
6465   {
6466     setup_editor_el_players             = FALSE;
6467     setup_editor_el_boulderdash         = FALSE;
6468     setup_editor_el_boulderdash_native  = FALSE;
6469     setup_editor_el_emerald_mine        = FALSE;
6470     setup_editor_el_emerald_mine_club   = FALSE;
6471     setup_editor_el_more                = FALSE;
6472     setup_editor_el_sokoban             = FALSE;
6473     setup_editor_el_supaplex            = FALSE;
6474     setup_editor_el_diamond_caves       = FALSE;
6475     setup_editor_el_dx_boulderdash      = FALSE;
6476     setup_editor_el_mirror_magic        = FALSE;
6477     setup_editor_el_deflektor           = FALSE;
6478     setup_editor_el_chars               = FALSE;
6479     setup_editor_el_steel_chars         = FALSE;
6480   }
6481
6482   if (!setup.editor.el_custom)
6483   {
6484     setup_editor_el_custom              = FALSE;
6485   }
6486
6487   if (!setup.editor.el_user_defined)
6488   {
6489     setup_editor_el_user_defined        = FALSE;
6490   }
6491
6492   if (!setup.editor.el_dynamic)
6493   {
6494     setup_editor_el_dynamic             = FALSE;
6495   }
6496
6497   if (level.game_engine_type == GAME_ENGINE_TYPE_RND)
6498   {
6499     setup_editor_el_boulderdash_native  = FALSE;
6500     setup_editor_el_mirror_magic        = FALSE;
6501     setup_editor_el_deflektor           = FALSE;
6502   }
6503   else if (level.game_engine_type == GAME_ENGINE_TYPE_BD)
6504   {
6505     setup_editor_el_players             = FALSE;
6506     setup_editor_el_boulderdash         = FALSE;
6507     setup_editor_el_emerald_mine        = FALSE;
6508     setup_editor_el_emerald_mine_club   = FALSE;
6509     setup_editor_el_more                = FALSE;
6510     setup_editor_el_sokoban             = FALSE;
6511     setup_editor_el_supaplex            = FALSE;
6512     setup_editor_el_diamond_caves       = FALSE;
6513     setup_editor_el_dx_boulderdash      = FALSE;
6514     setup_editor_el_mirror_magic        = FALSE;
6515     setup_editor_el_deflektor           = FALSE;
6516     setup_editor_el_chars               = FALSE;
6517     setup_editor_el_steel_chars         = FALSE;
6518
6519     setup_editor_el_custom              = FALSE;
6520   }
6521   else if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
6522   {
6523     setup_editor_el_boulderdash         = FALSE;
6524     setup_editor_el_boulderdash_native  = FALSE;
6525     setup_editor_el_more                = FALSE;
6526     setup_editor_el_sokoban             = FALSE;
6527     setup_editor_el_supaplex            = FALSE;
6528     setup_editor_el_diamond_caves       = FALSE;
6529     setup_editor_el_dx_boulderdash      = FALSE;
6530     setup_editor_el_mirror_magic        = FALSE;
6531     setup_editor_el_deflektor           = FALSE;
6532     setup_editor_el_steel_chars         = FALSE;
6533
6534     setup_editor_el_custom              = FALSE;
6535   }
6536   else if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
6537   {
6538     setup_editor_el_players             = FALSE;
6539     setup_editor_el_boulderdash         = FALSE;
6540     setup_editor_el_boulderdash_native  = FALSE;
6541     setup_editor_el_emerald_mine        = FALSE;
6542     setup_editor_el_emerald_mine_club   = FALSE;
6543     setup_editor_el_more                = FALSE;
6544     setup_editor_el_sokoban             = FALSE;
6545     setup_editor_el_diamond_caves       = FALSE;
6546     setup_editor_el_dx_boulderdash      = FALSE;
6547     setup_editor_el_mirror_magic        = FALSE;
6548     setup_editor_el_deflektor           = FALSE;
6549     setup_editor_el_chars               = FALSE;
6550     setup_editor_el_steel_chars         = FALSE;
6551
6552     setup_editor_el_custom              = FALSE;
6553   }
6554   else if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
6555   {
6556     setup_editor_el_players             = FALSE;
6557     setup_editor_el_boulderdash         = FALSE;
6558     setup_editor_el_boulderdash_native  = FALSE;
6559     setup_editor_el_emerald_mine        = FALSE;
6560     setup_editor_el_emerald_mine_club   = FALSE;
6561     setup_editor_el_more                = FALSE;
6562     setup_editor_el_sokoban             = FALSE;
6563     setup_editor_el_supaplex            = FALSE;
6564     setup_editor_el_diamond_caves       = FALSE;
6565     setup_editor_el_dx_boulderdash      = FALSE;
6566     setup_editor_el_steel_chars         = FALSE;
6567
6568     setup_editor_el_custom              = FALSE;
6569   }
6570 }
6571
6572 static void ReinitializeElementList(void)
6573 {
6574   static boolean initialization_needed = TRUE;
6575   int pos = 0;
6576   int i, j;
6577
6578   ReinitializeElementList_EnableSections();
6579
6580   if (initialization_needed)
6581   {
6582     LoadSetup_EditorCascade();          // load last editor cascade state
6583
6584     // initialize editor cascade element from saved cascade state
6585     for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6586     {
6587       int *cascade_element = &(*editor_elements_info[i].headline_list)[0];
6588       boolean cascade_value = *editor_elements_info[i].setup_cascade_value;
6589
6590       if (IS_EDITOR_CASCADE(*cascade_element))
6591         *cascade_element =
6592           (cascade_value ? EL_CASCADE_ACTIVE(*cascade_element) :
6593            EL_CASCADE_INACTIVE(*cascade_element));
6594     }
6595
6596     initialization_needed = FALSE;
6597   }
6598
6599   checked_free(editor_elements);
6600
6601   // reload optional user defined element list for each invocation of editor
6602   LoadUserDefinedEditorElementList(&editor_el_user_defined_ptr,
6603                                    &num_editor_el_user_defined);
6604
6605   // initialize dynamic level element list for each invocation of editor
6606   InitDynamicEditorElementList(&editor_el_dynamic_ptr,
6607                                &num_editor_el_dynamic);
6608
6609   // initialize list of empty elements (used for padding, if needed)
6610   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
6611     editor_el_empty[i] = EL_EMPTY;
6612
6613   // do some sanity checks for each element from element list
6614   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6615   {
6616     for (j = 0; j < *editor_elements_info[i].element_list_size; j++)
6617     {
6618       int element = (*editor_elements_info[i].element_list)[j];
6619
6620       if (element >= NUM_FILE_ELEMENTS)
6621         Warn("editor element %d is runtime element", element);
6622
6623       if (strEqual(getElementInfoText(element), INFOTEXT_UNKNOWN_ELEMENT))
6624         Warn("no element description text for element %d", element);
6625     }
6626   }
6627
6628   num_editor_elements = 0;
6629   use_el_empty = FALSE;
6630
6631   // determine size of element list
6632   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6633   {
6634     if (*editor_elements_info[i].setup_value)
6635     {
6636       boolean found_inactive_cascade = FALSE;
6637
6638       if (setup.editor.el_headlines)
6639       {
6640         // required for correct padding of palette headline buttons
6641         if (*editor_elements_info[i].headline_list_size > 0)
6642           num_editor_elements += ED_ELEMENTLIST_COLS;
6643
6644         for (j = 0; j < *editor_elements_info[i].headline_list_size; j++)
6645         {
6646           int element = (*editor_elements_info[i].headline_list)[j];
6647
6648           if (IS_EDITOR_CASCADE_INACTIVE(element))
6649             found_inactive_cascade = TRUE;
6650         }
6651       }
6652
6653       if (found_inactive_cascade)
6654         continue;
6655
6656       // required for correct padding of palette element buttons
6657       int element_list_size = *editor_elements_info[i].element_list_size;
6658       int element_rows =
6659         (element_list_size + ED_ELEMENTLIST_COLS - 1) / ED_ELEMENTLIST_COLS;
6660       int element_buttons = ED_ELEMENTLIST_COLS * element_rows;
6661
6662       num_editor_elements += element_buttons;
6663     }
6664   }
6665
6666   if (num_editor_elements < ED_NUM_ELEMENTLIST_BUTTONS)
6667   {
6668     // offer at least as many elements as element buttons exist
6669     use_el_empty = TRUE;
6670     num_editor_el_empty = ED_NUM_ELEMENTLIST_BUTTONS - num_editor_elements;
6671
6672     num_editor_elements += num_editor_el_empty;
6673   }
6674   else
6675   {
6676     num_editor_el_empty = 0;
6677   }
6678
6679   editor_elements = checked_malloc(num_editor_elements * sizeof(int));
6680
6681   // fill element list
6682   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6683   {
6684     boolean found_inactive_cascade = FALSE;
6685
6686     if (*editor_elements_info[i].setup_value)
6687     {
6688       if (setup.editor.el_headlines)
6689       {
6690         // required for correct padding of palette headline buttons
6691         int headline_size = (*editor_elements_info[i].headline_list_size > 0 ?
6692                              ED_ELEMENTLIST_COLS : 0);
6693
6694         for (j = 0; j < headline_size; j++)
6695         {
6696           // use empty elements for padding of palette headline buttons
6697           int element = (j < *editor_elements_info[i].headline_list_size ?
6698                          (*editor_elements_info[i].headline_list)[j] :
6699                          editor_el_empty[0]);
6700
6701           editor_elements[pos++] = element;
6702
6703           if (IS_EDITOR_CASCADE_INACTIVE(element))
6704             found_inactive_cascade = TRUE;
6705         }
6706       }
6707
6708       if (found_inactive_cascade)
6709         continue;
6710
6711       // required for correct padding of palette element buttons
6712       int element_list_size = *editor_elements_info[i].element_list_size;
6713       int element_rows =
6714         (element_list_size + ED_ELEMENTLIST_COLS - 1) / ED_ELEMENTLIST_COLS;
6715       int element_buttons = ED_ELEMENTLIST_COLS * element_rows;
6716
6717       // copy all elements from element list
6718       for (j = 0; j < element_list_size; j++)
6719         editor_elements[pos++] = (*editor_elements_info[i].element_list)[j];
6720
6721       // use empty elements for padding of palette element buttons
6722       for (j = 0; j < element_buttons - element_list_size; j++)
6723         editor_elements[pos++] = editor_el_empty[0];
6724     }
6725   }
6726
6727   // (this function is also called before editor gadgets are initialized!)
6728   AdjustElementListScrollbar();
6729 }
6730
6731 void PrintEditorElementList(void)
6732 {
6733   boolean *stop = &setup_editor_el_user_defined;
6734   int i, j;
6735
6736   for (i = 0; editor_elements_info[i].setup_value != stop; i++)
6737   {
6738     int cascade_element = (*editor_elements_info[i].headline_list)[0];
6739
6740     if (IS_EDITOR_CASCADE(cascade_element))
6741     {
6742       int cascade_element_show = EL_CASCADE_INACTIVE(cascade_element);
6743       char *headline = element_info[cascade_element_show].editor_description;
6744
6745       PrintLineWithPrefix("# ", "-", 77);
6746       Print("# %s\n", headline);
6747       PrintLineWithPrefix("# ", "-", 77);
6748     }
6749
6750     for (j = 0; j < *editor_elements_info[i].headline_list_size; j++)
6751     {
6752       int element = (*editor_elements_info[i].headline_list)[j];
6753
6754       if (IS_EDITOR_CASCADE(element))
6755         element = EL_CHAR_MINUS;
6756
6757       Print("# %s\n", element_info[element].token_name);
6758     }
6759
6760     if (j > 0)
6761       Print("#\n");
6762
6763     for (j = 0; j < *editor_elements_info[i].element_list_size; j++)
6764     {
6765       int element = (*editor_elements_info[i].element_list)[j];
6766
6767       Print("# %s\n", element_info[element].token_name);
6768     }
6769
6770     if (j > 0)
6771       Print("#\n");
6772   }
6773 }
6774
6775 static void ReinitializeElementListButtons(void)
6776 {
6777   static boolean last_setup_value_headlines = FALSE;
6778   static boolean initialization_needed = TRUE;
6779   int i;
6780
6781   if (!initialization_needed)   // check if editor element setup has changed
6782   {
6783     if (last_setup_value_headlines != setup.editor.el_headlines)
6784       initialization_needed = TRUE;
6785
6786     for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6787       if (editor_elements_info[i].last_setup_value !=
6788           *editor_elements_info[i].setup_value)
6789         initialization_needed = TRUE;
6790   }
6791
6792   if (!initialization_needed)
6793     return;
6794
6795   FreeLevelEditorGadgets();
6796   CreateLevelEditorGadgets();
6797
6798   // store current setup values for next invocation of this function
6799   last_setup_value_headlines = setup.editor.el_headlines;
6800   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6801     editor_elements_info[i].last_setup_value =
6802       *editor_elements_info[i].setup_value;
6803
6804   initialization_needed = FALSE;
6805 }
6806
6807 static void DrawElementBorder(int dest_x, int dest_y, int width, int height,
6808                               boolean input)
6809 {
6810   int border_graphic =
6811     (input ? IMG_EDITOR_ELEMENT_BORDER_INPUT : IMG_EDITOR_ELEMENT_BORDER);
6812   struct GraphicInfo *g = &graphic_info[border_graphic];
6813   Bitmap *src_bitmap = g->bitmap;
6814   int src_x = g->src_x;
6815   int src_y = g->src_y;
6816   int border_size = g->border_size;
6817   int border_xpos = g->width  - border_size;
6818   int border_ypos = g->height - border_size;
6819   int tilesize = ED_DRAWINGAREA_TILE_SIZE;
6820   int i;
6821
6822   BlitBitmap(src_bitmap, drawto, src_x, src_y,
6823              border_size, border_size,
6824              dest_x - border_size, dest_y - border_size);
6825   BlitBitmap(src_bitmap, drawto, src_x + border_xpos, src_y,
6826              border_size, border_size,
6827              dest_x + width, dest_y - border_size);
6828   BlitBitmap(src_bitmap, drawto, src_x, src_y + border_ypos,
6829              border_size, border_size,
6830              dest_x - border_size, dest_y + height);
6831   BlitBitmap(src_bitmap, drawto, src_x + border_xpos, src_y + border_ypos,
6832              border_size, border_size,
6833              dest_x + width, dest_y + height);
6834
6835   for (i = 0; i < width / tilesize; i++)
6836   {
6837     BlitBitmap(src_bitmap, drawto, src_x + border_size, src_y,
6838                tilesize, border_size,
6839                dest_x + i * tilesize, dest_y - border_size);
6840     BlitBitmap(src_bitmap, drawto, src_x + border_size, src_y + border_ypos,
6841                tilesize, border_size,
6842                dest_x + i * tilesize, dest_y + height);
6843   }
6844
6845   for (i = 0; i < height / tilesize; i++)
6846   {
6847     BlitBitmap(src_bitmap, drawto, src_x, src_y + border_size,
6848                border_size, tilesize,
6849                dest_x - border_size, dest_y + i * tilesize);
6850     BlitBitmap(src_bitmap, drawto, src_x + border_xpos, src_y + border_size,
6851                border_size, tilesize,
6852                dest_x + width, dest_y + i * tilesize);
6853   }
6854
6855   ClearRectangle(drawto, dest_x - 1, dest_y - 1, width + 2, height + 2);
6856 }
6857
6858 static void DrawEditorLevelBorderLine(int x, int y, int xsize, int ysize)
6859 {
6860   int xsize_tile = MAX(ed_tilesize, xsize);
6861   int ysize_tile = MAX(ed_tilesize, ysize);
6862   int xsize_full = xsize + 1;
6863   int ysize_full = ysize + 1;
6864   int xsize_thin = (xsize < ed_tilesize ? 1 : xsize);
6865   int ysize_thin = (ysize < ed_tilesize ? 1 : ysize);
6866   Pixel line_color = getTabulatorBarColor();
6867
6868   if (line_color == BLACK_PIXEL)                // black => transparent
6869     return;
6870
6871   FillRectangle(drawto, SX + x, SY + y, xsize_tile, ysize_tile, BLACK_PIXEL);
6872   FillRectangle(drawto, SX + x, SY + y, xsize_full, ysize_full, line_color);
6873   FillRectangle(drawto, SX + x, SY + y, xsize_thin, ysize_thin, BLACK_PIXEL);
6874 }
6875
6876 static void DrawEditorLevelBorderLinesIfNeeded(void)
6877 {
6878   int xsize = lev_fieldx * ed_tilesize;
6879   int ysize = lev_fieldy * ed_tilesize;
6880   int line_size = getTabulatorBarHeight();
6881
6882   if (!suppressBorderElement())
6883     return;
6884
6885   // draw little border line around editable level playfield
6886
6887   if (xsize < SXSIZE)
6888     DrawEditorLevelBorderLine(xsize, 0, line_size, ysize);
6889
6890   if (ysize < SYSIZE)
6891     DrawEditorLevelBorderLine(0, ysize, xsize, line_size);
6892
6893   if (xsize < SXSIZE && ysize < SYSIZE)
6894     DrawEditorLevelBorderLine(xsize, ysize, line_size, line_size);
6895 }
6896
6897 static void DrawEditorElement(int x, int y, int element)
6898 {
6899   DrawSizedElement(x, y, element, ed_tilesize);
6900 }
6901
6902 static void DrawEditorElementThruMask(int x, int y, int element)
6903 {
6904   DrawSizedElementThruMask(x, y, element, ed_tilesize);
6905 }
6906
6907 static void DrawEditorElementOrWall(int x, int y, int scroll_x, int scroll_y)
6908 {
6909   DrawSizedElementOrWall(x, y, scroll_x, scroll_y, ed_tilesize);
6910 }
6911
6912 static void DrawEditorLevel(int size_x, int size_y, int scroll_x, int scroll_y)
6913 {
6914   DrawSizedLevel(size_x, size_y, scroll_x, scroll_y, ed_tilesize);
6915   DrawEditorLevelBorderLinesIfNeeded();
6916 }
6917
6918 static void DrawDrawingArea(int id)
6919 {
6920   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
6921   int x, y;
6922
6923   int *value = drawingarea_info[id].value;
6924   int area_xsize = drawingarea_info[id].area_xsize;
6925   int area_ysize = drawingarea_info[id].area_ysize;
6926   int tilesize = ED_DRAWINGAREA_TILE_SIZE;
6927
6928   for (x = 0; x < area_xsize; x++)
6929     for (y = 0; y < area_ysize; y++)
6930       DrawSizedGraphicExt(drawto,
6931                           gi->x + x * tilesize,
6932                           gi->y + y * tilesize,
6933                           el2edimg(value[x * area_ysize + y]), 0, tilesize);
6934 }
6935
6936 static void ScrollEditorLevel(int from_x, int from_y, int scroll)
6937 {
6938   int x, y;
6939   int dx = (scroll == ED_SCROLL_LEFT ? -1 : scroll == ED_SCROLL_RIGHT ? 1 : 0);
6940   int dy = (scroll == ED_SCROLL_UP   ? -1 : scroll == ED_SCROLL_DOWN  ? 1 : 0);
6941
6942   BlitBitmap(drawto, drawto,
6943              SX + (dx == -1 ? ed_tilesize : 0),
6944              SY + (dy == -1 ? ed_tilesize : 0),
6945              (ed_fieldx * ed_tilesize) - (dx != 0 ? ed_tilesize : 0),
6946              (ed_fieldy * ed_tilesize) - (dy != 0 ? ed_tilesize : 0),
6947              SX + (dx == +1 ? ed_tilesize : 0),
6948              SY + (dy == +1 ? ed_tilesize : 0));
6949
6950   if (dx)
6951   {
6952     x = (dx == 1 ? 0 : ed_fieldx - 1);
6953     for (y = 0; y < ed_fieldy; y++)
6954       DrawEditorElementOrWall(x, y, from_x, from_y);
6955   }
6956   else if (dy)
6957   {
6958     y = (dy == 1 ? 0 : ed_fieldy - 1);
6959     for (x = 0; x < ed_fieldx; x++)
6960       DrawEditorElementOrWall(x, y, from_x, from_y);
6961   }
6962
6963   redraw_mask |= REDRAW_FIELD;
6964   BackToFront();
6965 }
6966
6967 static void getEditorGraphicSource(int element, int tile_size, Bitmap **bitmap,
6968                                    int *x, int *y)
6969 {
6970   getSizedGraphicSource(el2edimg(element), 0, tile_size, bitmap, x, y);
6971 }
6972
6973 static void CreateControlButtons(void)
6974 {
6975   struct GadgetInfo *gi;
6976   int i;
6977
6978   // create toolbox buttons
6979   for (i = 0; i < ED_NUM_CTRL_BUTTONS; i++)
6980   {
6981     int type_id = controlbutton_info[i].gadget_id;      // same as gadget ID here
6982     int id = controlbutton_info[i].gadget_id;
6983     int type = controlbutton_info[i].gadget_type;
6984     int graphic = controlbutton_info[i].graphic;
6985     struct XYTileSize *pos = controlbutton_info[i].pos;
6986     struct GraphicInfo *gd = &graphic_info[graphic];
6987     Bitmap *deco_bitmap = NULL;
6988     int deco_x = 0, deco_y = 0, deco_xpos = 0, deco_ypos = 0;
6989     int tile_size = 0, deco_shift = 0;
6990     boolean deco_masked = FALSE;
6991     int gd_x1 = gd->src_x;
6992     int gd_y1 = gd->src_y;
6993     int gd_x2 = gd->src_x + gd->pressed_xoffset;
6994     int gd_y2 = gd->src_y + gd->pressed_yoffset;
6995     int gd_x1a = gd->src_x + gd->active_xoffset;
6996     int gd_y1a = gd->src_y + gd->active_yoffset;
6997     int gd_x2a = gd->src_x + gd->active_xoffset + gd->pressed_xoffset;
6998     int gd_y2a = gd->src_y + gd->active_yoffset + gd->pressed_yoffset;
6999     int x = pos->x;
7000     int y = pos->y;
7001     unsigned int event_mask;
7002     int radio_button_nr = RADIO_NR_NONE;
7003     boolean checked = FALSE;
7004
7005     if (type_id != i)
7006       Fail("'controlbutton_info' structure corrupted at index %d -- please fix", i);
7007
7008     if (type == GD_TYPE_RADIO_BUTTON)
7009     {
7010       event_mask = GD_EVENT_PRESSED;
7011       radio_button_nr = RADIO_NR_DRAWING_TOOLBOX;
7012
7013       if (id == drawing_function)
7014         checked = TRUE;
7015     }
7016     else
7017     {
7018       if (id == GADGET_ID_WRAP_LEFT ||
7019           id == GADGET_ID_WRAP_RIGHT ||
7020           id == GADGET_ID_WRAP_UP ||
7021           id == GADGET_ID_WRAP_DOWN)
7022         event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
7023       else
7024         event_mask = GD_EVENT_RELEASED;
7025     }
7026
7027     if (id == GADGET_ID_PROPERTIES ||
7028         id == GADGET_ID_PALETTE)
7029     {
7030       x += DX;
7031       y += DY;
7032     }
7033     else if (id == GADGET_ID_ELEMENT_LEFT ||
7034              id == GADGET_ID_ELEMENT_MIDDLE ||
7035              id == GADGET_ID_ELEMENT_RIGHT)
7036     {
7037       x += DX;
7038       y += DY;
7039
7040       int element = (id == GADGET_ID_ELEMENT_LEFT   ? new_element1 :
7041                      id == GADGET_ID_ELEMENT_MIDDLE ? new_element2 :
7042                      id == GADGET_ID_ELEMENT_RIGHT  ? new_element3 : EL_EMPTY);
7043
7044       tile_size = BUTTON_TILE_SIZE(id == GADGET_ID_ELEMENT_LEFT ?
7045                                    editor.button.element_left.tile_size :
7046                                    id == GADGET_ID_ELEMENT_MIDDLE ?
7047                                    editor.button.element_middle.tile_size :
7048                                    id == GADGET_ID_ELEMENT_RIGHT ?
7049                                    editor.button.element_right.tile_size : 0);
7050
7051       // make sure that decoration does not overlap gadget border
7052       tile_size = MIN(tile_size, MIN(gd->width, gd->height));
7053
7054       getEditorGraphicSource(element, tile_size, &deco_bitmap, &deco_x,&deco_y);
7055
7056       deco_xpos = (gd->width  - tile_size) / 2;
7057       deco_ypos = (gd->height - tile_size) / 2;
7058       deco_shift = 1;
7059       deco_masked = gd->draw_masked;
7060     }
7061     else
7062     {
7063       x += EX;
7064       y += EY;
7065     }
7066
7067     gi = CreateGadget(GDI_CUSTOM_ID, id,
7068                       GDI_CUSTOM_TYPE_ID, type_id,
7069                       GDI_IMAGE_ID, graphic,
7070                       GDI_INFO_TEXT, controlbutton_info[i].infotext,
7071                       GDI_X, x,
7072                       GDI_Y, y,
7073                       GDI_WIDTH, gd->width,
7074                       GDI_HEIGHT, gd->height,
7075                       GDI_TYPE, type,
7076                       GDI_STATE, GD_BUTTON_UNPRESSED,
7077                       GDI_RADIO_NR, radio_button_nr,
7078                       GDI_CHECKED, checked,
7079                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7080                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7081                       GDI_ALT_DESIGN_UNPRESSED, gd->bitmap, gd_x1a, gd_y1a,
7082                       GDI_ALT_DESIGN_PRESSED, gd->bitmap, gd_x2a, gd_y2a,
7083                       GDI_DECORATION_DESIGN, deco_bitmap, deco_x, deco_y,
7084                       GDI_DECORATION_POSITION, deco_xpos, deco_ypos,
7085                       GDI_DECORATION_SIZE, tile_size, tile_size,
7086                       GDI_DECORATION_SHIFTING, deco_shift, deco_shift,
7087                       GDI_DECORATION_MASKED, deco_masked,
7088                       GDI_EVENT_MASK, event_mask,
7089                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7090                       GDI_CALLBACK_ACTION, HandleControlButtons,
7091                       GDI_END);
7092
7093     if (gi == NULL)
7094       Fail("cannot create gadget");
7095
7096     level_editor_gadget[id] = gi;
7097   }
7098
7099   // these values are not constant, but can change at runtime
7100   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_UP].x    = ED_SCROLL_UP_XPOS;
7101   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_UP].y    = ED_SCROLL_UP_YPOS;
7102   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_DOWN].x  = ED_SCROLL_DOWN_XPOS;
7103   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_DOWN].y  = ED_SCROLL_DOWN_YPOS;
7104   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_LEFT].x  = ED_SCROLL_LEFT_XPOS;
7105   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_LEFT].y  = ED_SCROLL_LEFT_YPOS;
7106   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_RIGHT].x = ED_SCROLL_RIGHT_XPOS;
7107   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_RIGHT].y = ED_SCROLL_RIGHT_YPOS;
7108   scrollbutton_pos[ED_SCROLLBUTTON_ID_LIST_UP].x    = ED_SCROLL2_UP_XPOS;
7109   scrollbutton_pos[ED_SCROLLBUTTON_ID_LIST_UP].y    = ED_SCROLL2_UP_YPOS;
7110   scrollbutton_pos[ED_SCROLLBUTTON_ID_LIST_DOWN].x  = ED_SCROLL2_DOWN_XPOS;
7111   scrollbutton_pos[ED_SCROLLBUTTON_ID_LIST_DOWN].y  = ED_SCROLL2_DOWN_YPOS;
7112
7113   // create buttons for scrolling of drawing area and element list
7114   for (i = 0; i < ED_NUM_SCROLLBUTTONS; i++)
7115   {
7116     int id = scrollbutton_info[i].gadget_id;
7117     int type_id = scrollbutton_info[i].gadget_type_id;
7118     int graphic = scrollbutton_info[i].graphic;
7119     struct GraphicInfo *gd = &graphic_info[graphic];
7120     Bitmap *gd_bitmap = gd->bitmap;
7121     int gd_x1 = gd->src_x;
7122     int gd_y1 = gd->src_y;
7123     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7124     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7125     int width  = gd->width;
7126     int height = gd->height;
7127     int x = scrollbutton_pos[i].x;
7128     int y = scrollbutton_pos[i].y;
7129     unsigned int event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
7130
7131     if (type_id != i)
7132       Fail("'scrollbutton_info' structure corrupted at index %d -- please fix", i);
7133
7134     if (id == GADGET_ID_SCROLL_LIST_UP ||
7135         id == GADGET_ID_SCROLL_LIST_DOWN)
7136     {
7137       x += PX;
7138       y += PY;
7139     }
7140     else
7141     {
7142       x += SX;
7143       y += SY;
7144     }
7145
7146     gi = CreateGadget(GDI_CUSTOM_ID, id,
7147                       GDI_CUSTOM_TYPE_ID, type_id,
7148                       GDI_IMAGE_ID, graphic,
7149                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
7150                       GDI_X, x,
7151                       GDI_Y, y,
7152                       GDI_WIDTH, width,
7153                       GDI_HEIGHT, height,
7154                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7155                       GDI_STATE, GD_BUTTON_UNPRESSED,
7156                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y1,
7157                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y2,
7158                       GDI_EVENT_MASK, event_mask,
7159                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7160                       GDI_CALLBACK_ACTION, HandleControlButtons,
7161                       GDI_END);
7162
7163     if (gi == NULL)
7164       Fail("cannot create gadget");
7165
7166     level_editor_gadget[id] = gi;
7167   }
7168
7169   // create buttons for element list
7170   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
7171   {
7172     int type_id = i;
7173     int id = GADGET_ID_ELEMENTLIST_FIRST + i;
7174     int graphic = IMG_EDITOR_PALETTE_BUTTON;
7175     struct GraphicInfo *gd = &graphic_info[graphic];
7176     Bitmap *gd_bitmap = gd->bitmap;
7177     Bitmap *deco_bitmap;
7178     int deco_x, deco_y, deco_xpos, deco_ypos;
7179     int gd_x1 = gd->src_x;
7180     int gd_y1 = gd->src_y;
7181     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7182     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7183     int xx = i % ED_ELEMENTLIST_BUTTONS_HORIZ;
7184     int yy = i / ED_ELEMENTLIST_BUTTONS_HORIZ;
7185     int x = PX + ED_ELEMENTLIST_XPOS + xx * gd->width;
7186     int y = PY + ED_ELEMENTLIST_YPOS + yy * gd->height;
7187     int element = editor_elements[i];
7188     int tile_size = BUTTON_TILE_SIZE(editor.palette.tile_size);
7189     unsigned int event_mask = GD_EVENT_RELEASED;
7190
7191     getEditorGraphicSource(element, tile_size, &deco_bitmap, &deco_x, &deco_y);
7192
7193     deco_xpos = (gd->width  - tile_size) / 2;
7194     deco_ypos = (gd->height - tile_size) / 2;
7195
7196     gi = CreateGadget(GDI_CUSTOM_ID, id,
7197                       GDI_CUSTOM_TYPE_ID, type_id,
7198                       GDI_IMAGE_ID, graphic,
7199                       GDI_INFO_TEXT, getElementInfoText(element),
7200                       GDI_X, x,
7201                       GDI_Y, y,
7202                       GDI_WIDTH, gd->width,
7203                       GDI_HEIGHT, gd->height,
7204                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7205                       GDI_STATE, GD_BUTTON_UNPRESSED,
7206                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y1,
7207                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y2,
7208                       GDI_DECORATION_DESIGN, deco_bitmap, deco_x, deco_y,
7209                       GDI_DECORATION_POSITION, deco_xpos, deco_ypos,
7210                       GDI_DECORATION_SIZE, tile_size, tile_size,
7211                       GDI_DECORATION_SHIFTING, 1, 1,
7212                       GDI_EVENT_MASK, event_mask,
7213                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7214                       GDI_CALLBACK_ACTION, HandleControlButtons,
7215                       GDI_END);
7216
7217     if (gi == NULL)
7218       Fail("cannot create gadget");
7219
7220     level_editor_gadget[id] = gi;
7221   }
7222 }
7223
7224 static void CreateCounterButtons(void)
7225 {
7226   int max_infotext_len = getMaxInfoTextLength();
7227   int i;
7228
7229   for (i = 0; i < ED_NUM_COUNTERBUTTONS; i++)
7230   {
7231     int type_id = counterbutton_info[i].gadget_type_id;
7232     int x = SX + ED_SETTINGS_X(counterbutton_info[i].x); // down count button
7233     int y = SY + ED_SETTINGS_Y(counterbutton_info[i].y);
7234     int j;
7235
7236     if (type_id != i)
7237       Fail("'counterbutton_info' structure corrupted at index %d -- please fix", i);
7238
7239     // determine horizontal position to the right of specified gadget
7240     if (counterbutton_info[i].gadget_id_align != GADGET_ID_NONE)
7241       x = (right_gadget_border[counterbutton_info[i].gadget_id_align] +
7242            ED_GADGET_TEXT_DISTANCE);
7243
7244     // determine horizontal offset for leading text
7245     if (counterbutton_info[i].text_left != NULL)
7246       x += getTextWidthForGadget(counterbutton_info[i].text_left);
7247
7248     for (j = 0; j < 2; j++)
7249     {
7250       struct GadgetInfo *gi;
7251       int id = (j == 0 ?
7252                 counterbutton_info[i].gadget_id_down :
7253                 counterbutton_info[i].gadget_id_up);
7254       int graphic;
7255       struct GraphicInfo *gd;
7256       int gd_x1, gd_x2, gd_y1, gd_y2;
7257       unsigned int event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
7258       char infotext[max_infotext_len + 1];
7259
7260       if (i == ED_COUNTER_ID_SELECT_LEVEL)
7261       {
7262         graphic = (j == 0 ?
7263                    IMG_GFX_EDITOR_BUTTON_PREV_LEVEL :
7264                    IMG_GFX_EDITOR_BUTTON_NEXT_LEVEL);
7265
7266         event_mask |= GD_EVENT_RELEASED;
7267
7268         if (j == 0)
7269         {
7270           x = DX + editor.button.prev_level.x;
7271           y = DY + editor.button.prev_level.y;
7272         }
7273         else
7274         {
7275           x = DX + editor.button.next_level.x;
7276           y = DY + editor.button.next_level.y;
7277         }
7278       }
7279       else
7280       {
7281         graphic = (j == 0 ?
7282                    IMG_EDITOR_COUNTER_DOWN :
7283                    IMG_EDITOR_COUNTER_UP);
7284       }
7285
7286       gd = &graphic_info[graphic];
7287
7288       gd_x1 = gd->src_x;
7289       gd_y1 = gd->src_y;
7290       gd_x2 = gd->src_x + gd->pressed_xoffset;
7291       gd_y2 = gd->src_y + gd->pressed_yoffset;
7292
7293       sprintf(infotext, "%s counter value by 1, 5 or 10",
7294               (j == 0 ? "decrease" : "increase"));
7295
7296       gi = CreateGadget(GDI_CUSTOM_ID, id,
7297                         GDI_CUSTOM_TYPE_ID, type_id,
7298                         GDI_IMAGE_ID, graphic,
7299                         GDI_INFO_TEXT, infotext,
7300                         GDI_X, x,
7301                         GDI_Y, y,
7302                         GDI_WIDTH, gd->width,
7303                         GDI_HEIGHT, gd->height,
7304                         GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7305                         GDI_STATE, GD_BUTTON_UNPRESSED,
7306                         GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7307                         GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7308                         GDI_EVENT_MASK, event_mask,
7309                         GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7310                         GDI_CALLBACK_ACTION, HandleCounterButtons,
7311                         GDI_END);
7312
7313       if (gi == NULL)
7314         Fail("cannot create gadget");
7315
7316       level_editor_gadget[id] = gi;
7317       right_gadget_border[id] =
7318         getRightGadgetBorder(gi, counterbutton_info[i].text_right);
7319
7320       x += gi->width + ED_GADGET_SMALL_DISTANCE;        // text count button
7321
7322       if (j == 0)
7323       {
7324         int font_type = FONT_INPUT_1;
7325         int font_type_active = FONT_INPUT_1_ACTIVE;
7326
7327         id = counterbutton_info[i].gadget_id_text;
7328
7329         event_mask = GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
7330
7331         if (i == ED_COUNTER_ID_SELECT_LEVEL)
7332         {
7333           graphic = IMG_GFX_EDITOR_INPUT_LEVEL_NUMBER;
7334
7335           font_type = FONT_LEVEL_NUMBER;
7336           font_type_active = FONT_LEVEL_NUMBER_ACTIVE;
7337
7338           x = DX + editor.input.level_number.x;
7339           y = DY + editor.input.level_number.y;
7340         }
7341         else
7342         {
7343           graphic = IMG_EDITOR_COUNTER_INPUT;
7344         }
7345
7346         gd = &graphic_info[graphic];
7347
7348         gd_x1 = gd->src_x;
7349         gd_y1 = gd->src_y;
7350         gd_x2 = gd->src_x + gd->active_xoffset;
7351         gd_y2 = gd->src_y + gd->active_yoffset;
7352
7353         gi = CreateGadget(GDI_CUSTOM_ID, id,
7354                           GDI_CUSTOM_TYPE_ID, type_id,
7355                           GDI_IMAGE_ID, graphic,
7356                           GDI_INFO_TEXT, "enter counter value",
7357                           GDI_X, x,
7358                           GDI_Y, y,
7359                           GDI_TYPE, GD_TYPE_TEXT_INPUT_NUMERIC,
7360                           GDI_NUMBER_VALUE, 0,
7361                           GDI_NUMBER_MIN, counterbutton_info[i].min_value,
7362                           GDI_NUMBER_MAX, counterbutton_info[i].max_value,
7363                           GDI_TEXT_SIZE, 3,     // minimal counter text size
7364                           GDI_TEXT_FONT, font_type,
7365                           GDI_TEXT_FONT_ACTIVE, font_type_active,
7366                           GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7367                           GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7368                           GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7369                           GDI_DESIGN_WIDTH, gd->width,
7370                           GDI_EVENT_MASK, event_mask,
7371                           GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7372                           GDI_CALLBACK_ACTION, HandleCounterButtons,
7373                           GDI_END);
7374
7375         if (gi == NULL)
7376           Fail("cannot create gadget");
7377
7378         level_editor_gadget[id] = gi;
7379         right_gadget_border[id] =
7380           getRightGadgetBorder(gi, counterbutton_info[i].text_right);
7381
7382         x += gi->width + ED_GADGET_SMALL_DISTANCE;      // up count button
7383       }
7384     }
7385   }
7386 }
7387
7388 static void CreateDrawingAreas(void)
7389 {
7390   int i;
7391
7392   // these values are not constant, but can change at runtime
7393   drawingarea_info[ED_DRAWING_ID_DRAWING_LEVEL].area_xsize = MAX_ED_FIELDX;
7394   drawingarea_info[ED_DRAWING_ID_DRAWING_LEVEL].area_ysize = MAX_ED_FIELDY;
7395
7396   for (i = 0; i < ED_NUM_DRAWING_AREAS; i++)
7397   {
7398     struct GadgetInfo *gi;
7399     int id = drawingarea_info[i].gadget_id;
7400     int type_id = drawingarea_info[i].gadget_type_id;
7401     int x = SX + ED_AREA_SETTINGS_X(drawingarea_info[i]);
7402     int y = SY + ED_AREA_SETTINGS_Y(drawingarea_info[i]);
7403     int area_xsize = drawingarea_info[i].area_xsize;
7404     int area_ysize = drawingarea_info[i].area_ysize;
7405     int item_size = (id == GADGET_ID_DRAWING_LEVEL ?
7406                      ed_tilesize : ED_DRAWINGAREA_TILE_SIZE);
7407     unsigned int event_mask =
7408       GD_EVENT_PRESSED | GD_EVENT_RELEASED | GD_EVENT_MOVING |
7409       GD_EVENT_OFF_BORDERS | GD_EVENT_PIXEL_PRECISE;
7410
7411     if (type_id != i)
7412       Fail("'drawingarea_info' structure corrupted at index %d -- please fix", i);
7413
7414     // determine horizontal position to the right of specified gadget
7415     if (drawingarea_info[i].gadget_id_align != GADGET_ID_NONE)
7416       x = (right_gadget_border[drawingarea_info[i].gadget_id_align] +
7417            ED_DRAWINGAREA_TEXT_DISTANCE);
7418
7419     // determine horizontal offset for leading text
7420     if (drawingarea_info[i].text_left != NULL)
7421       x += getTextWidthForDrawingArea(drawingarea_info[i].text_left);
7422
7423     gi = CreateGadget(GDI_CUSTOM_ID, id,
7424                       GDI_CUSTOM_TYPE_ID, type_id,
7425                       GDI_X, x,
7426                       GDI_Y, y,
7427                       GDI_TYPE, GD_TYPE_DRAWING_AREA,
7428                       GDI_AREA_SIZE, area_xsize, area_ysize,
7429                       GDI_ITEM_SIZE, item_size, item_size,
7430                       GDI_EVENT_MASK, event_mask,
7431                       GDI_CALLBACK_INFO, HandleDrawingAreaInfo,
7432                       GDI_CALLBACK_ACTION, HandleDrawingAreas,
7433                       GDI_END);
7434
7435     if (gi == NULL)
7436       Fail("cannot create gadget");
7437
7438     level_editor_gadget[id] = gi;
7439     right_gadget_border[id] =
7440       getRightGadgetBorder(gi, drawingarea_info[i].text_right);
7441   }
7442 }
7443
7444 static void CreateTextInputGadgets(void)
7445 {
7446   struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_INPUT_TEXT];
7447   int max_infotext_len = getMaxInfoTextLength();
7448   int i;
7449
7450   for (i = 0; i < ED_NUM_TEXTINPUT; i++)
7451   {
7452     int gd_x1 = gd->src_x;
7453     int gd_y1 = gd->src_y;
7454     int gd_x2 = gd->src_x + gd->active_xoffset;
7455     int gd_y2 = gd->src_y + gd->active_yoffset;
7456     struct GadgetInfo *gi;
7457     unsigned int event_mask = GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
7458     char infotext[MAX_OUTPUT_LINESIZE + 1];
7459     int id = textinput_info[i].gadget_id;
7460     int type_id = textinput_info[i].gadget_type_id;
7461     int x, y;
7462
7463     if (type_id != i)
7464       Fail("'textinput_info' structure corrupted at index %d -- please fix", i);
7465
7466     if (i == ED_TEXTINPUT_ID_ELEMENT_NAME)
7467     {
7468       int element_border = graphic_info[IMG_EDITOR_ELEMENT_BORDER].border_size;
7469       int border_size = gd->border_size;
7470       int font_nr = FONT_INPUT_1;
7471       int font_height = getFontHeight(font_nr);
7472       int xoffset = element_border + TILEX + element_border + 3 * border_size;
7473       int yoffset = element_border + (TILEY - font_height) / 2;
7474
7475       x = (editor.settings.element_name.x != -1 ?
7476            editor.settings.element_name.x :
7477            editor.settings.element_graphic.x + xoffset) - border_size;
7478       y = (editor.settings.element_name.y != -1 ?
7479            editor.settings.element_name.y :
7480            editor.settings.element_graphic.y + yoffset) - border_size;
7481     }
7482     else
7483     {
7484       x = ED_SETTINGS_X(textinput_info[i].x);
7485       y = ED_SETTINGS_Y(textinput_info[i].y);
7486     }
7487
7488     sprintf(infotext, "Enter %s", textinput_info[i].infotext);
7489     infotext[max_infotext_len] = '\0';
7490
7491     gi = CreateGadget(GDI_CUSTOM_ID, id,
7492                       GDI_CUSTOM_TYPE_ID, type_id,
7493                       GDI_INFO_TEXT, infotext,
7494                       GDI_X, SX + x,
7495                       GDI_Y, SY + y,
7496                       GDI_TYPE, GD_TYPE_TEXT_INPUT_ALPHANUMERIC,
7497                       GDI_TEXT_VALUE, textinput_info[i].value,
7498                       GDI_TEXT_SIZE, textinput_info[i].size,
7499                       GDI_TEXT_FONT, FONT_INPUT_1,
7500                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_1_ACTIVE,
7501                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7502                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7503                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7504                       GDI_DESIGN_WIDTH, gd->width,
7505                       GDI_EVENT_MASK, event_mask,
7506                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7507                       GDI_CALLBACK_ACTION, HandleTextInputGadgets,
7508                       GDI_END);
7509
7510     if (gi == NULL)
7511       Fail("cannot create gadget");
7512
7513     level_editor_gadget[id] = gi;
7514   }
7515 }
7516
7517 static void CreateTextAreaGadgets(void)
7518 {
7519   int max_infotext_len = getMaxInfoTextLength();
7520   int i;
7521
7522   for (i = 0; i < ED_NUM_TEXTAREAS; i++)
7523   {
7524     struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_INPUT_TEXTAREA];
7525     int gd_x1 = gd->src_x;
7526     int gd_y1 = gd->src_y;
7527     int gd_x2 = gd->src_x + gd->active_xoffset;
7528     int gd_y2 = gd->src_y + gd->active_yoffset;
7529     struct GadgetInfo *gi;
7530     unsigned int event_mask = GD_EVENT_TEXT_LEAVING;
7531     char infotext[MAX_OUTPUT_LINESIZE + 1];
7532     int id = textarea_info[i].gadget_id;
7533     int type_id = textarea_info[i].gadget_type_id;
7534     int area_xsize = textarea_info[i].xsize;
7535     int area_ysize = textarea_info[i].ysize;
7536
7537     if (type_id != i)
7538       Fail("'textarea_info' structure corrupted at index %d -- please fix", i);
7539
7540     sprintf(infotext, "Enter %s", textarea_info[i].infotext);
7541     infotext[max_infotext_len] = '\0';
7542
7543     gi = CreateGadget(GDI_CUSTOM_ID, id,
7544                       GDI_CUSTOM_TYPE_ID, type_id,
7545                       GDI_INFO_TEXT, infotext,
7546                       GDI_X, SX + ED_SETTINGS_X(textarea_info[i].x),
7547                       GDI_Y, SY + ED_SETTINGS_Y(textarea_info[i].y),
7548                       GDI_TYPE, GD_TYPE_TEXT_AREA,
7549                       GDI_AREA_SIZE, area_xsize, area_ysize,
7550                       GDI_TEXT_FONT, FONT_INPUT_1,
7551                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_1_ACTIVE,
7552                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7553                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7554                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7555                       GDI_DESIGN_WIDTH, gd->width,
7556                       GDI_EVENT_MASK, event_mask,
7557                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7558                       GDI_CALLBACK_ACTION, HandleTextAreaGadgets,
7559                       GDI_END);
7560
7561     if (gi == NULL)
7562       Fail("cannot create gadget");
7563
7564     level_editor_gadget[id] = gi;
7565   }
7566 }
7567
7568 static void CreateSelectboxGadgets(void)
7569 {
7570   int max_infotext_len = getMaxInfoTextLength();
7571   int i, j;
7572
7573   for (i = 0; i < ED_NUM_SELECTBOX; i++)
7574   {
7575     struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_SELECTBOX_INPUT];
7576     struct GraphicInfo *gd2 = &graphic_info[IMG_EDITOR_SELECTBOX_BUTTON];
7577     int gd_x1 = gd->src_x;
7578     int gd_y1 = gd->src_y;
7579     int gd_x2 = gd->src_x + gd->active_xoffset;
7580     int gd_y2 = gd->src_y + gd->active_yoffset;
7581     int selectbox_button_xsize = gd2->width;
7582     struct GadgetInfo *gi;
7583     char infotext[MAX_OUTPUT_LINESIZE + 1];
7584     int id = selectbox_info[i].gadget_id;
7585     int type_id = selectbox_info[i].gadget_type_id;
7586     int x = SX + ED_SETTINGS_X(selectbox_info[i].x);
7587     int y = SY + ED_SETTINGS_Y(selectbox_info[i].y);
7588     unsigned int event_mask =
7589       GD_EVENT_RELEASED | GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
7590
7591     if (type_id != i)
7592       Fail("'selectbox_info' structure corrupted at index %d -- please fix", i);
7593
7594     if (selectbox_info[i].size == -1)   // dynamically determine size
7595     {
7596       // (we cannot use -1 for uninitialized values if we directly compare
7597       // with results from strlen(), because the '<' and '>' operation will
7598       // implicitely cast -1 to an unsigned integer value!)
7599       selectbox_info[i].size = 0;
7600
7601       for (j = 0; selectbox_info[i].options[j].text != NULL; j++)
7602         if (strlen(selectbox_info[i].options[j].text) > selectbox_info[i].size)
7603           selectbox_info[i].size = strlen(selectbox_info[i].options[j].text);
7604
7605       selectbox_info[i].size++;         // add one character empty space
7606     }
7607
7608     // determine horizontal position to the right of specified gadget
7609     if (selectbox_info[i].gadget_id_align != GADGET_ID_NONE)
7610       x = (right_gadget_border[selectbox_info[i].gadget_id_align] +
7611            ED_GADGET_TEXT_DISTANCE);
7612
7613     // determine horizontal offset for leading text
7614     if (selectbox_info[i].text_left != NULL)
7615       x += getTextWidthForGadget(selectbox_info[i].text_left);
7616
7617     sprintf(infotext, "Select %s", selectbox_info[i].infotext);
7618     infotext[max_infotext_len] = '\0';
7619
7620     gi = CreateGadget(GDI_CUSTOM_ID, id,
7621                       GDI_CUSTOM_TYPE_ID, type_id,
7622                       GDI_INFO_TEXT, infotext,
7623                       GDI_X, x,
7624                       GDI_Y, y,
7625                       GDI_TYPE, GD_TYPE_SELECTBOX,
7626                       GDI_SELECTBOX_OPTIONS, selectbox_info[i].options,
7627                       GDI_SELECTBOX_CHAR_UNSELECTABLE, '[',
7628                       GDI_TEXT_SIZE, selectbox_info[i].size,
7629                       GDI_TEXT_FONT, FONT_INPUT_1,
7630                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_1_ACTIVE,
7631                       GDI_TEXT_FONT_UNSELECTABLE, FONT_TEXT_1,
7632                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7633                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7634                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7635                       GDI_BORDER_SIZE_SELECTBUTTON, selectbox_button_xsize,
7636                       GDI_DESIGN_WIDTH, gd->width,
7637                       GDI_EVENT_MASK, event_mask,
7638                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7639                       GDI_CALLBACK_ACTION, HandleSelectboxGadgets,
7640                       GDI_END);
7641
7642     if (gi == NULL)
7643       Fail("cannot create gadget");
7644
7645     level_editor_gadget[id] = gi;
7646     right_gadget_border[id] =
7647       getRightGadgetBorder(gi, selectbox_info[i].text_right);
7648   }
7649 }
7650
7651 static void CreateTextbuttonGadgets(void)
7652 {
7653   int max_infotext_len = getMaxInfoTextLength();
7654   int i;
7655
7656   for (i = 0; i < ED_NUM_TEXTBUTTONS; i++)
7657   {
7658     int id = textbutton_info[i].gadget_id;
7659     int type_id = textbutton_info[i].gadget_type_id;
7660     int is_tab_button =
7661       ((id >= GADGET_ID_LEVELCONFIG_LEVEL && id <= GADGET_ID_LEVELCONFIG_ENGINE) ||
7662        (id >= GADGET_ID_PROPERTIES_INFO && id <= GADGET_ID_PROPERTIES_CHANGE));
7663     int graphic =
7664       (is_tab_button ? IMG_EDITOR_TABBUTTON : IMG_EDITOR_TEXTBUTTON);
7665     int gadget_distance =
7666       (is_tab_button ? ED_GADGET_SMALL_DISTANCE : ED_GADGET_TEXT_DISTANCE);
7667     struct GraphicInfo *gd = &graphic_info[graphic];
7668     int gd_x1 = gd->src_x;
7669     int gd_y1 = gd->src_y;
7670     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7671     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7672     int gd_x1a = gd->src_x + gd->active_xoffset;
7673     int gd_y1a = gd->src_y + gd->active_yoffset;
7674     int border_xsize = gd->border_size + gd->draw_xoffset;
7675     int border_ysize = gd->border_size;
7676     struct GadgetInfo *gi;
7677     unsigned int event_mask = GD_EVENT_RELEASED;
7678     char infotext[MAX_OUTPUT_LINESIZE + 1];
7679     int x = SX + ED_SETTINGS_X(textbutton_info[i].x);
7680     int y = SY + ED_SETTINGS_Y(textbutton_info[i].y);
7681
7682     if (type_id != i)
7683       Fail("'textbutton_info' structure corrupted at index %d -- please fix", i);
7684
7685     if (textbutton_info[i].size == -1)  // dynamically determine size
7686       textbutton_info[i].size = strlen(textbutton_info[i].text);
7687
7688     sprintf(infotext, "%s", textbutton_info[i].infotext);
7689     infotext[max_infotext_len] = '\0';
7690
7691     // determine horizontal position to the right of specified gadget
7692     if (textbutton_info[i].gadget_id_align != GADGET_ID_NONE)
7693     {
7694       int gadget_id_align = textbutton_info[i].gadget_id_align;
7695
7696       x = right_gadget_border[gadget_id_align] + gadget_distance;
7697
7698       if (textbutton_info[i].y == -1)
7699         y = level_editor_gadget[gadget_id_align]->y;
7700     }
7701
7702     // determine horizontal offset for leading text
7703     if (textbutton_info[i].text_left != NULL)
7704       x += getTextWidthForGadget(textbutton_info[i].text_left);
7705
7706     gi = CreateGadget(GDI_CUSTOM_ID, id,
7707                       GDI_CUSTOM_TYPE_ID, type_id,
7708                       GDI_IMAGE_ID, graphic,
7709                       GDI_INFO_TEXT, infotext,
7710                       GDI_X, x,
7711                       GDI_Y, y,
7712                       GDI_TYPE, GD_TYPE_TEXT_BUTTON,
7713                       GDI_TEXT_VALUE, textbutton_info[i].text,
7714                       GDI_TEXT_SIZE, textbutton_info[i].size,
7715                       GDI_TEXT_FONT, FONT_INPUT_2,
7716                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_2_ACTIVE,
7717                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7718                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7719                       GDI_ALT_DESIGN_UNPRESSED, gd->bitmap, gd_x1a, gd_y1a,
7720                       GDI_BORDER_SIZE, border_xsize, border_ysize,
7721                       GDI_DESIGN_WIDTH, gd->width,
7722                       GDI_DECORATION_SHIFTING, 1, 1,
7723                       GDI_EVENT_MASK, event_mask,
7724                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7725                       GDI_CALLBACK_ACTION, HandleTextbuttonGadgets,
7726                       GDI_END);
7727
7728     if (gi == NULL)
7729       Fail("cannot create gadget");
7730
7731     level_editor_gadget[id] = gi;
7732     right_gadget_border[id] =
7733       getRightGadgetBorder(gi, textbutton_info[i].text_right);
7734   }
7735 }
7736
7737 static void CreateGraphicbuttonGadgets(void)
7738 {
7739   struct GadgetInfo *gi;
7740   int i;
7741
7742   // create buttons for scrolling of drawing area and element list
7743   for (i = 0; i < ED_NUM_GRAPHICBUTTONS; i++)
7744   {
7745     int id = graphicbutton_info[i].gadget_id;
7746     int type_id = graphicbutton_info[i].gadget_type_id;
7747     int x = SX + ED_SETTINGS_X(graphicbutton_info[i].x);
7748     int y = SY + ED_SETTINGS_Y(graphicbutton_info[i].y);
7749     int graphic = graphicbutton_info[i].graphic;
7750     struct GraphicInfo *gd = &graphic_info[graphic];
7751     int gd_x1 = gd->src_x;
7752     int gd_y1 = gd->src_y;
7753     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7754     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7755     unsigned int event_mask = GD_EVENT_RELEASED;
7756
7757     if (type_id != i)
7758       Fail("'graphicbutton_info' structure corrupted at index %d -- please fix", i);
7759
7760     // determine horizontal position to the right of specified gadget
7761     if (graphicbutton_info[i].gadget_id_align != GADGET_ID_NONE)
7762       x = (right_gadget_border[graphicbutton_info[i].gadget_id_align] +
7763            ED_GADGET_TEXT_DISTANCE);
7764
7765     // determine horizontal offset for leading text
7766     if (graphicbutton_info[i].text_left != NULL)
7767       x += getTextWidthForGadget(graphicbutton_info[i].text_left);
7768
7769     gi = CreateGadget(GDI_CUSTOM_ID, id,
7770                       GDI_CUSTOM_TYPE_ID, type_id,
7771                       GDI_IMAGE_ID, graphic,
7772                       GDI_INFO_TEXT, graphicbutton_info[i].infotext,
7773                       GDI_X, x,
7774                       GDI_Y, y,
7775                       GDI_WIDTH, gd->width,
7776                       GDI_HEIGHT, gd->height,
7777                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7778                       GDI_STATE, GD_BUTTON_UNPRESSED,
7779                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7780                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7781                       GDI_EVENT_MASK, event_mask,
7782                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7783                       GDI_CALLBACK_ACTION, HandleGraphicbuttonGadgets,
7784                       GDI_END);
7785
7786     if (gi == NULL)
7787       Fail("cannot create gadget");
7788
7789     level_editor_gadget[id] = gi;
7790     right_gadget_border[id] =
7791       getRightGadgetBorder(gi, graphicbutton_info[i].text_right);
7792   }
7793 }
7794
7795 static void CreateScrollbarGadgets(void)
7796 {
7797   int i;
7798
7799   // these values are not constant, but can change at runtime
7800   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].x =
7801     SX + ED_SCROLL_HORIZONTAL_XPOS;
7802   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].y =
7803     SY + ED_SCROLL_HORIZONTAL_YPOS;
7804   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].width =
7805     ED_SCROLL_HORIZONTAL_XSIZE;
7806   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].height =
7807     ED_SCROLL_HORIZONTAL_YSIZE;
7808   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].wheel_x      = SX;
7809   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].wheel_y      = SY;
7810   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].wheel_width  = SXSIZE;
7811   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].wheel_height = SYSIZE;
7812
7813   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].x =
7814     SX + ED_SCROLL_VERTICAL_XPOS;
7815   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].y =
7816     SY + ED_SCROLL_VERTICAL_YPOS;
7817   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].width =
7818     ED_SCROLL_VERTICAL_XSIZE;
7819   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].height =
7820     ED_SCROLL_VERTICAL_YSIZE;
7821   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].wheel_x      = SX;
7822   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].wheel_y      = SY;
7823   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].wheel_width  = SXSIZE;
7824   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].wheel_height = SYSIZE;
7825
7826   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].x =
7827     PX + ED_SCROLL2_VERTICAL_XPOS;
7828   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].y =
7829     PY + ED_SCROLL2_VERTICAL_YPOS;
7830   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].width =
7831     ED_SCROLL2_VERTICAL_XSIZE;
7832   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].height =
7833     ED_SCROLL2_VERTICAL_YSIZE;
7834   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].wheel_x = PX;
7835   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].wheel_y = PY;
7836   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].wheel_width  = PXSIZE;
7837   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].wheel_height = PYSIZE;
7838
7839   for (i = 0; i < ED_NUM_SCROLLBARS; i++)
7840   {
7841     int id = scrollbar_info[i].gadget_id;
7842     int type_id = scrollbar_info[i].gadget_type_id;
7843     int graphic = scrollbar_info[i].graphic;
7844     struct GraphicInfo *gd = &graphic_info[graphic];
7845     int gd_x1 = gd->src_x;
7846     int gd_y1 = gd->src_y;
7847     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7848     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7849     struct GadgetInfo *gi;
7850     int items_max, items_visible, item_position;
7851     unsigned int event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
7852
7853     if (type_id != i)
7854       Fail("'scrollbar_info' structure corrupted at index %d -- please fix", i);
7855
7856     if (i == ED_SCROLLBAR_ID_LIST_VERTICAL)
7857     {
7858       items_max = num_editor_elements / ED_ELEMENTLIST_BUTTONS_HORIZ;
7859       items_visible = ED_ELEMENTLIST_BUTTONS_VERT;
7860       item_position = element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ;
7861     }
7862     else        // drawing area scrollbars
7863     {
7864       if (scrollbar_info[i].type == GD_TYPE_SCROLLBAR_HORIZONTAL)
7865       {
7866         items_max = MAX(lev_fieldx + 2, ed_fieldx);
7867         items_visible = ed_fieldx;
7868         item_position = 0;
7869       }
7870       else
7871       {
7872         items_max = MAX(lev_fieldy + 2, ed_fieldy);
7873         items_visible = ed_fieldy;
7874         item_position = 0;
7875       }
7876     }
7877
7878     gi = CreateGadget(GDI_CUSTOM_ID, id,
7879                       GDI_CUSTOM_TYPE_ID, type_id,
7880                       GDI_IMAGE_ID, graphic,
7881                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
7882                       GDI_X, scrollbar_pos[i].x,
7883                       GDI_Y, scrollbar_pos[i].y,
7884                       GDI_WIDTH, scrollbar_pos[i].width,
7885                       GDI_HEIGHT, scrollbar_pos[i].height,
7886                       GDI_TYPE, scrollbar_info[i].type,
7887                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
7888                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
7889                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
7890                       GDI_WHEEL_AREA_X, scrollbar_pos[i].wheel_x,
7891                       GDI_WHEEL_AREA_Y, scrollbar_pos[i].wheel_y,
7892                       GDI_WHEEL_AREA_WIDTH, scrollbar_pos[i].wheel_width,
7893                       GDI_WHEEL_AREA_HEIGHT, scrollbar_pos[i].wheel_height,
7894                       GDI_STATE, GD_BUTTON_UNPRESSED,
7895                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7896                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7897                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7898                       GDI_EVENT_MASK, event_mask,
7899                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7900                       GDI_CALLBACK_ACTION, HandleControlButtons,
7901                       GDI_END);
7902
7903     if (gi == NULL)
7904       Fail("cannot create gadget");
7905
7906     level_editor_gadget[id] = gi;
7907   }
7908 }
7909
7910 static void CreateCheckbuttonGadgets(void)
7911 {
7912   struct GadgetInfo *gi;
7913   int i;
7914
7915   for (i = 0; i < ED_NUM_CHECKBUTTONS; i++)
7916   {
7917     int id = checkbutton_info[i].gadget_id;
7918     int type_id = checkbutton_info[i].gadget_type_id;
7919     int graphic = (id == GADGET_ID_STICK_ELEMENT ? IMG_EDITOR_STICKYBUTTON :
7920                    IMG_EDITOR_CHECKBOX);
7921     struct GraphicInfo *gd = &graphic_info[graphic];
7922     int gd_x1 = gd->src_x;
7923     int gd_y1 = gd->src_y;
7924     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7925     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7926     int gd_x1a = gd->src_x + gd->active_xoffset;
7927     int gd_y1a = gd->src_y + gd->active_yoffset;
7928     int gd_x2a = gd->src_x + gd->active_xoffset + gd->pressed_xoffset;
7929     int gd_y2a = gd->src_y + gd->active_yoffset + gd->pressed_yoffset;
7930     int x = SX + ED_SETTINGS_X(checkbutton_info[i].x);
7931     int y = SY + ED_SETTINGS_Y(checkbutton_info[i].y);
7932     unsigned int event_mask = GD_EVENT_PRESSED;
7933
7934     if (type_id != i)
7935       Fail("'checkbutton_info' structure corrupted at index %d -- please fix", i);
7936
7937     // determine horizontal position to the right of specified gadget
7938     if (checkbutton_info[i].gadget_id_align != GADGET_ID_NONE)
7939       x = (right_gadget_border[checkbutton_info[i].gadget_id_align] +
7940            ED_GADGET_TEXT_DISTANCE);
7941
7942     // determine horizontal offset for leading text
7943     if (checkbutton_info[i].text_left != NULL)
7944       x += getTextWidthForGadget(checkbutton_info[i].text_left);
7945
7946     gi = CreateGadget(GDI_CUSTOM_ID, id,
7947                       GDI_CUSTOM_TYPE_ID, type_id,
7948                       GDI_IMAGE_ID, graphic,
7949                       GDI_INFO_TEXT, checkbutton_info[i].infotext,
7950                       GDI_X, x,
7951                       GDI_Y, y,
7952                       GDI_WIDTH, gd->width,
7953                       GDI_HEIGHT, gd->height,
7954                       GDI_TYPE, GD_TYPE_CHECK_BUTTON,
7955                       GDI_CHECKED, *checkbutton_info[i].value,
7956                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7957                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7958                       GDI_ALT_DESIGN_UNPRESSED, gd->bitmap, gd_x1a, gd_y1a,
7959                       GDI_ALT_DESIGN_PRESSED, gd->bitmap, gd_x2a, gd_y2a,
7960                       GDI_EVENT_MASK, event_mask,
7961                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7962                       GDI_CALLBACK_ACTION, HandleCheckbuttons,
7963                       GDI_END);
7964
7965     if (gi == NULL)
7966       Fail("cannot create gadget");
7967
7968     level_editor_gadget[id] = gi;
7969     right_gadget_border[id] =
7970       getRightGadgetBorder(gi, checkbutton_info[i].text_right);
7971   }
7972 }
7973
7974 static void CreateRadiobuttonGadgets(void)
7975 {
7976   int graphic = IMG_EDITOR_RADIOBUTTON;
7977   struct GraphicInfo *gd = &graphic_info[graphic];
7978   int gd_x1 = gd->src_x;
7979   int gd_y1 = gd->src_y;
7980   int gd_x2 = gd->src_x + gd->pressed_xoffset;
7981   int gd_y2 = gd->src_y + gd->pressed_yoffset;
7982   int gd_x1a = gd->src_x + gd->active_xoffset;
7983   int gd_y1a = gd->src_y + gd->active_yoffset;
7984   int gd_x2a = gd->src_x + gd->active_xoffset + gd->pressed_xoffset;
7985   int gd_y2a = gd->src_y + gd->active_yoffset + gd->pressed_yoffset;
7986   struct GadgetInfo *gi;
7987   int i;
7988
7989   for (i = 0; i < ED_NUM_RADIOBUTTONS; i++)
7990   {
7991     int id = radiobutton_info[i].gadget_id;
7992     int type_id = radiobutton_info[i].gadget_type_id;
7993     int x = SX + ED_SETTINGS_X(radiobutton_info[i].x);
7994     int y = SY + ED_SETTINGS_Y(radiobutton_info[i].y);
7995     unsigned int event_mask = GD_EVENT_PRESSED;
7996
7997     if (type_id != i)
7998       Fail("'radiobutton_info' structure corrupted at index %d -- please fix", i);
7999
8000     int checked =
8001       (*radiobutton_info[i].value == radiobutton_info[i].checked_value);
8002
8003     // determine horizontal position to the right of specified gadget
8004     if (radiobutton_info[i].gadget_id_align != GADGET_ID_NONE)
8005       x = (right_gadget_border[radiobutton_info[i].gadget_id_align] +
8006            ED_GADGET_TEXT_DISTANCE);
8007
8008     // determine horizontal offset for leading text
8009     if (radiobutton_info[i].text_left != NULL)
8010       x += getTextWidthForGadget(radiobutton_info[i].text_left);
8011
8012     gi = CreateGadget(GDI_CUSTOM_ID, id,
8013                       GDI_CUSTOM_TYPE_ID, type_id,
8014                       GDI_IMAGE_ID, graphic,
8015                       GDI_INFO_TEXT, radiobutton_info[i].infotext,
8016                       GDI_X, x,
8017                       GDI_Y, y,
8018                       GDI_WIDTH, gd->width,
8019                       GDI_HEIGHT, gd->height,
8020                       GDI_TYPE, GD_TYPE_RADIO_BUTTON,
8021                       GDI_RADIO_NR, radiobutton_info[i].radio_button_nr,
8022                       GDI_CHECKED, checked,
8023                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
8024                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
8025                       GDI_ALT_DESIGN_UNPRESSED, gd->bitmap, gd_x1a, gd_y1a,
8026                       GDI_ALT_DESIGN_PRESSED, gd->bitmap, gd_x2a, gd_y2a,
8027                       GDI_EVENT_MASK, event_mask,
8028                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
8029                       GDI_CALLBACK_ACTION, HandleRadiobuttons,
8030                       GDI_END);
8031
8032     if (gi == NULL)
8033       Fail("cannot create gadget");
8034
8035     level_editor_gadget[id] = gi;
8036     right_gadget_border[id] =
8037       getRightGadgetBorder(gi, radiobutton_info[i].text_right);
8038   }
8039 }
8040
8041 void CreateLevelEditorGadgets(void)
8042 {
8043   // force EDITOR font inside level editor
8044   SetFontStatus(GAME_MODE_EDITOR);
8045
8046   // these values are not constant, but can change at runtime
8047   ed_fieldx = MAX_ED_FIELDX - 1;
8048   ed_fieldy = MAX_ED_FIELDY - 1;
8049
8050   num_editor_gadgets = NUM_EDITOR_GADGETS;
8051
8052   // Debug("editor", "allocating %d gadgets ...\n", num_editor_gadgets);
8053
8054   level_editor_gadget =
8055     checked_calloc(num_editor_gadgets * sizeof(struct GadgetInfo *));
8056   right_gadget_border =
8057     checked_calloc(num_editor_gadgets * sizeof(int));
8058
8059   // set number of empty (padding) element buttons to maximum number of buttons
8060   num_editor_el_empty = ED_NUM_ELEMENTLIST_BUTTONS;
8061
8062   editor_el_empty = checked_calloc(num_editor_el_empty * sizeof(int));
8063   editor_el_empty_ptr = editor_el_empty;
8064
8065   use_permanent_palette = !editor.palette.show_as_separate_screen;
8066
8067   InitGadgetScreenBorders(-1, INFOTEXT_YPOS);
8068
8069   ReinitializeElementList();
8070
8071   CreateControlButtons();
8072   CreateScrollbarGadgets();
8073
8074   // order of function calls is important because of cross-references
8075   CreateCheckbuttonGadgets();
8076   CreateCounterButtons();
8077   CreateRadiobuttonGadgets();
8078   CreateTextInputGadgets();
8079   CreateTextAreaGadgets();
8080   CreateSelectboxGadgets();
8081   CreateGraphicbuttonGadgets();
8082   CreateTextbuttonGadgets();
8083   CreateDrawingAreas();
8084
8085   ResetFontStatus();
8086 }
8087
8088 void FreeLevelEditorGadgets(void)
8089 {
8090   int i;
8091
8092   // Debug("editor", "freeing %d gadgets ...\n", num_editor_gadgets);
8093
8094   for (i = 0; i < num_editor_gadgets; i++)
8095   {
8096     FreeGadget(level_editor_gadget[i]);
8097
8098     level_editor_gadget[i] = NULL;
8099   }
8100
8101   checked_free(level_editor_gadget);
8102   checked_free(right_gadget_border);
8103
8104   checked_free(editor_el_empty);
8105 }
8106
8107 static void MapCounterButtons(int id)
8108 {
8109   int font_nr = FONT_TEXT_1;
8110   int font_height = getFontHeight(font_nr);
8111   int gadget_id_down = counterbutton_info[id].gadget_id_down;
8112   int gadget_id_text = counterbutton_info[id].gadget_id_text;
8113   int gadget_id_up   = counterbutton_info[id].gadget_id_up;
8114   struct GadgetInfo *gi_down = level_editor_gadget[gadget_id_down];
8115   struct GadgetInfo *gi_text = level_editor_gadget[gadget_id_text];
8116   struct GadgetInfo *gi_up   = level_editor_gadget[gadget_id_up];
8117   int xoffset_left = getTextWidthForGadget(counterbutton_info[id].text_left);
8118   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8119   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8120   int yoffset = (gi_down->height - font_height) / 2;
8121   int x_left = gi_down->x - xoffset_left;
8122   int x_right;  // set after gadget position was modified
8123   int y_above = gi_down->y - yoffset_above;
8124   int x = gi_down->x;
8125   int y;        // set after gadget position was modified
8126
8127   // counter limits must be changed first to prevent value truncation
8128   ModifyEditorCounterLimits(id, counterbutton_info[id].min_value,
8129                             counterbutton_info[id].max_value);
8130
8131   // right text position might have changed after setting position above
8132   x_right = gi_up->x + gi_up->width + xoffset_right;
8133
8134   ModifyEditorCounterValue(id, *counterbutton_info[id].value);
8135
8136   // set position for "value[1,2,3,4]" counter gadgets (score in most cases)
8137   if (id >= ED_COUNTER_ID_ELEMENT_VALUE1 &&
8138       id <= ED_COUNTER_ID_ELEMENT_VALUE4)
8139   {
8140     ModifyGadget(gi_down, GDI_Y,
8141                  SY + ED_SETTINGS_Y(counterbutton_info[id].y), GDI_END);
8142     ModifyGadget(gi_text, GDI_Y,
8143                  SY + ED_SETTINGS_Y(counterbutton_info[id].y), GDI_END);
8144     ModifyGadget(gi_up,   GDI_Y,
8145                  SY + ED_SETTINGS_Y(counterbutton_info[id].y), GDI_END);
8146   }
8147
8148   // vertical position might have changed after setting position above
8149   y = gi_up->y + yoffset;
8150
8151   if (counterbutton_info[id].text_above)
8152     DrawText(x, y_above, counterbutton_info[id].text_above, font_nr);
8153
8154   if (counterbutton_info[id].text_left)
8155     DrawText(x_left, y, counterbutton_info[id].text_left, font_nr);
8156
8157   if (counterbutton_info[id].text_right)
8158     DrawText(x_right, y, counterbutton_info[id].text_right, font_nr);
8159
8160   MapGadget(gi_down);
8161   MapGadget(gi_text);
8162   MapGadget(gi_up);
8163 }
8164
8165 static void MapControlButtons(void)
8166 {
8167   int counter_id;
8168   int i;
8169
8170   // map toolbox buttons (excluding special CE toolbox buttons)
8171   for (i = 0; i < ED_NUM_CTRL1_2_BUTTONS; i++)
8172     MapGadget(level_editor_gadget[i]);
8173
8174   // map toolbox buttons (element properties buttons)
8175   for (i = ED_NUM_CTRL1_4_BUTTONS; i < ED_NUM_CTRL1_7_BUTTONS; i++)
8176     MapGadget(level_editor_gadget[i]);
8177
8178   if (use_permanent_palette)
8179   {
8180     // map buttons to select elements
8181     for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
8182       MapGadget(level_editor_gadget[GADGET_ID_ELEMENTLIST_FIRST + i]);
8183     MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL]);
8184     MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_UP]);
8185     MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_DOWN]);
8186   }
8187
8188   // map buttons to select level
8189   counter_id = ED_COUNTER_ID_SELECT_LEVEL;
8190   counterbutton_info[counter_id].min_value = leveldir_current->first_level;
8191   counterbutton_info[counter_id].max_value = leveldir_current->last_level;
8192   MapCounterButtons(counter_id);
8193 }
8194
8195 static void MapDrawingArea(int id)
8196 {
8197   int font_nr = FONT_TEXT_1;
8198   int font_height = getFontHeight(font_nr);
8199   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
8200   int area_xsize = gi->drawing.area_xsize;
8201   int area_ysize = gi->drawing.area_ysize;
8202   int xoffset_left = getTextWidthForDrawingArea(drawingarea_info[id].text_left);
8203   int xoffset_below = getTextWidth(drawingarea_info[id].text_below, font_nr);
8204   int x_left  = gi->x - xoffset_left;
8205   int x_right = gi->x + gi->width + ED_DRAWINGAREA_TEXT_DISTANCE;
8206   int x_above = gi->x - ED_DRAWINGAREA_BORDER_SIZE;
8207   int x_below = gi->x + (gi->width - xoffset_below) / 2;
8208   int y_side  = gi->y + (gi->height - font_height) / 2;
8209   int y_above = gi->y - font_height - ED_DRAWINGAREA_TEXT_DISTANCE;
8210   int y_below = gi->y + gi->height + ED_DRAWINGAREA_TEXT_DISTANCE;
8211
8212   if (drawingarea_info[id].text_left)
8213     DrawText(x_left, y_side, drawingarea_info[id].text_left, font_nr);
8214
8215   if (drawingarea_info[id].text_right)
8216     DrawText(x_right, y_side, drawingarea_info[id].text_right, font_nr);
8217
8218   if (drawingarea_info[id].text_above)
8219     DrawText(x_above, y_above, drawingarea_info[id].text_above, font_nr);
8220
8221   if (drawingarea_info[id].text_below)
8222     DrawText(x_below, y_below, drawingarea_info[id].text_below, font_nr);
8223
8224   if (id != ED_DRAWING_ID_DRAWING_LEVEL)
8225   {
8226     DrawElementBorder(gi->x, gi->y,
8227                       area_xsize * ED_DRAWINGAREA_TILE_SIZE,
8228                       area_ysize * ED_DRAWINGAREA_TILE_SIZE, TRUE);
8229
8230     DrawDrawingArea(id);
8231   }
8232
8233   MapGadget(gi);
8234 }
8235
8236 static void MapTextInputGadget(int id)
8237 {
8238   int font_nr = FONT_TEXT_1;
8239   int font_height = getFontHeight(font_nr);
8240   struct GadgetInfo *gi = level_editor_gadget[textinput_info[id].gadget_id];
8241   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8242   int x_above = ED_SETTINGS_X(textinput_info[id].x);
8243   int y_above = ED_SETTINGS_Y(textinput_info[id].y) - yoffset_above;
8244
8245   if (textinput_info[id].text_above)
8246     DrawTextS(x_above, y_above, font_nr, textinput_info[id].text_above);
8247
8248   ModifyGadget(gi, GDI_TEXT_VALUE, textinput_info[id].value, GDI_END);
8249
8250   MapGadget(gi);
8251 }
8252
8253 static void MapTextAreaGadget(int id)
8254 {
8255   int font_nr = FONT_TEXT_1;
8256   int font_height = getFontHeight(font_nr);
8257   struct GadgetInfo *gi = level_editor_gadget[textarea_info[id].gadget_id];
8258   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8259   int x_above = ED_SETTINGS_X(textarea_info[id].x);
8260   int y_above = ED_SETTINGS_Y(textarea_info[id].y) - yoffset_above;
8261   char *text_above = textarea_info[id].text_above;
8262
8263   if (gi->textarea.cropped && textarea_info[id].text_above_cropped)
8264     text_above = textarea_info[id].text_above_cropped;
8265
8266   if (text_above)
8267     DrawTextS(x_above, y_above, font_nr, text_above);
8268
8269   ModifyGadget(gi, GDI_TEXT_VALUE, textarea_info[id].value, GDI_END);
8270
8271   MapGadget(gi);
8272 }
8273
8274 static void MapSelectboxGadget(int id)
8275 {
8276   int font_nr = FONT_TEXT_1;
8277   int font_height = getFontHeight(font_nr);
8278   struct GadgetInfo *gi = level_editor_gadget[selectbox_info[id].gadget_id];
8279   int xoffset_left = getTextWidthForGadget(selectbox_info[id].text_left);
8280   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8281   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8282   int yoffset = (gi->height - font_height) / 2;
8283   int x_left = gi->x - xoffset_left;
8284   int x_right = gi->x + gi->width + xoffset_right;
8285   int y_above = gi->y - yoffset_above;
8286   int x = gi->x;
8287   int y = gi->y + yoffset;
8288
8289   if (selectbox_info[id].text_above)
8290     DrawText(x, y_above, selectbox_info[id].text_above, font_nr);
8291
8292   if (selectbox_info[id].text_left)
8293     DrawText(x_left, y, selectbox_info[id].text_left, font_nr);
8294
8295   if (selectbox_info[id].text_right)
8296     DrawText(x_right, y, selectbox_info[id].text_right, font_nr);
8297
8298   ModifyEditorSelectboxValue(id, *selectbox_info[id].value);
8299
8300   MapGadget(gi);
8301 }
8302
8303 static void MapTextbuttonGadget(int id)
8304 {
8305   int font_nr = FONT_TEXT_1;
8306   int font_height = getFontHeight(font_nr);
8307   struct GadgetInfo *gi = level_editor_gadget[textbutton_info[id].gadget_id];
8308   int xoffset_left = getTextWidthForGadget(textbutton_info[id].text_left);
8309   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8310   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8311   int yoffset = (gi->height - font_height) / 2;
8312   int x_left = gi->x - xoffset_left;
8313   int x_right = gi->x + gi->width + xoffset_right;
8314   int y_above = gi->y - yoffset_above;
8315   int x = gi->x;
8316   int y = gi->y + yoffset;
8317
8318   // only show button to delete change pages when more than minimum pages
8319   if (id == ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE &&
8320       custom_element.num_change_pages == MIN_CHANGE_PAGES)
8321     return;
8322
8323   if (textbutton_info[id].text_above)
8324     DrawText(x, y_above, textbutton_info[id].text_above, font_nr);
8325
8326   if (textbutton_info[id].text_left)
8327     DrawText(x_left, y, textbutton_info[id].text_left, font_nr);
8328
8329   if (textbutton_info[id].text_right)
8330     DrawText(x_right, y, textbutton_info[id].text_right, font_nr);
8331
8332   MapGadget(gi);
8333 }
8334
8335 static void MapGraphicbuttonGadget(int id)
8336 {
8337   int font_nr = FONT_TEXT_1;
8338   int font_height = getFontHeight(font_nr);
8339   struct GadgetInfo *gi = level_editor_gadget[graphicbutton_info[id].gadget_id];
8340   int xoffset_left = getTextWidthForGadget(graphicbutton_info[id].text_left);
8341   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8342   int yoffset = (gi->height - font_height) / 2;
8343   int x_left = gi->x - xoffset_left;
8344   int x_right = gi->x + gi->width + xoffset_right;
8345   int y = gi->y + yoffset;
8346
8347   if (graphicbutton_info[id].text_left)
8348     DrawText(x_left, y, graphicbutton_info[id].text_left, font_nr);
8349
8350   if (graphicbutton_info[id].text_right)
8351     DrawText(x_right, y, graphicbutton_info[id].text_right, font_nr);
8352
8353   MapGadget(gi);
8354 }
8355
8356 static void MapRadiobuttonGadget(int id)
8357 {
8358   int font_nr = FONT_TEXT_1;
8359   int font_height = getFontHeight(font_nr);
8360   struct GadgetInfo *gi = level_editor_gadget[radiobutton_info[id].gadget_id];
8361   int xoffset_left = getTextWidthForGadget(checkbutton_info[id].text_left);
8362   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8363   int yoffset = (gi->height - font_height) / 2;
8364   int x_left = gi->x - xoffset_left;
8365   int x_right = gi->x + gi->width + xoffset_right;
8366   int y = gi->y + yoffset;
8367   boolean checked =
8368     (*radiobutton_info[id].value == radiobutton_info[id].checked_value);
8369
8370   if (radiobutton_info[id].text_left)
8371     DrawText(x_left, y, radiobutton_info[id].text_left, font_nr);
8372
8373   if (radiobutton_info[id].text_right)
8374     DrawText(x_right, y, radiobutton_info[id].text_right, font_nr);
8375
8376   ModifyGadget(gi, GDI_CHECKED, checked, GDI_END);
8377
8378   MapGadget(gi);
8379 }
8380
8381 static void MapCheckbuttonGadget(int id)
8382 {
8383   int font_nr = FONT_TEXT_1;
8384   int font_height = getFontHeight(font_nr);
8385   struct GadgetInfo *gi = level_editor_gadget[checkbutton_info[id].gadget_id];
8386   int xoffset_left = getTextWidthForGadget(checkbutton_info[id].text_left);
8387   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8388   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8389   int yoffset = (gi->height - font_height) / 2;
8390   int y_above = gi->y - yoffset_above;
8391   int x = gi->x;
8392   int x_left, x_right, y;       // set after gadget position was modified
8393
8394   // set position for gadgets with dynamically determined position
8395   if (checkbutton_info[id].x != -1)     // do not change dynamic positions
8396     ModifyGadget(gi, GDI_X, SX + ED_SETTINGS_X(checkbutton_info[id].x), GDI_END);
8397   ModifyGadget(gi, GDI_Y, SY + ED_SETTINGS_Y(checkbutton_info[id].y), GDI_END);
8398
8399   x_left = gi->x - xoffset_left;
8400   x_right = gi->x + gi->width + xoffset_right;
8401   y = gi->y + yoffset;
8402
8403   if (checkbutton_info[id].text_above)
8404     DrawText(x, y_above, checkbutton_info[id].text_above, font_nr);
8405
8406   if (checkbutton_info[id].text_left)
8407     DrawText(x_left, y, checkbutton_info[id].text_left, font_nr);
8408
8409   if (checkbutton_info[id].text_right)
8410     DrawText(x_right, y, checkbutton_info[id].text_right, font_nr);
8411
8412   ModifyGadget(gi, GDI_CHECKED, *checkbutton_info[id].value, GDI_END);
8413
8414   MapGadget(gi);
8415 }
8416
8417 static void MapMainDrawingArea(void)
8418 {
8419   boolean no_horizontal_scrollbar = (lev_fieldx + 2 <= ed_fieldx);
8420   boolean no_vertical_scrollbar = (lev_fieldy + 2 <= ed_fieldy);
8421   int i;
8422
8423   if (suppressBorderElement())
8424   {
8425     no_horizontal_scrollbar = (lev_fieldx <= ed_fieldx);
8426     no_vertical_scrollbar   = (lev_fieldy <= ed_fieldy);
8427   }
8428
8429   for (i = ED_SCROLLBUTTON_ID_AREA_FIRST; i <= ED_SCROLLBUTTON_ID_AREA_LAST; i++)
8430   {
8431     if (((i == ED_SCROLLBUTTON_ID_AREA_LEFT ||
8432           i == ED_SCROLLBUTTON_ID_AREA_RIGHT) &&
8433          no_horizontal_scrollbar) ||
8434         ((i == ED_SCROLLBUTTON_ID_AREA_UP ||
8435           i == ED_SCROLLBUTTON_ID_AREA_DOWN) &&
8436          no_vertical_scrollbar))
8437       continue;
8438
8439     MapGadget(level_editor_gadget[scrollbutton_info[i].gadget_id]);
8440   }
8441
8442   for (i = ED_SCROLLBAR_ID_AREA_FIRST; i <= ED_SCROLLBAR_ID_AREA_LAST; i++)
8443   {
8444     if ((i == ED_SCROLLBAR_ID_AREA_HORIZONTAL && no_horizontal_scrollbar) ||
8445         (i == ED_SCROLLBAR_ID_AREA_VERTICAL && no_vertical_scrollbar))
8446       continue;
8447
8448     MapGadget(level_editor_gadget[scrollbar_info[i].gadget_id]);
8449   }
8450
8451   MapDrawingArea(ED_DRAWING_ID_DRAWING_LEVEL);
8452 }
8453
8454 static void MapOrUnmapLevelEditorToolboxCustomGadgets(boolean map)
8455 {
8456   int i;
8457
8458   for (i = 0; i < ED_NUM_CTRL_BUTTONS; i++)
8459   {
8460     if (i == GADGET_ID_CUSTOM_COPY_FROM ||
8461         i == GADGET_ID_CUSTOM_COPY_TO ||
8462         i == GADGET_ID_CUSTOM_EXCHANGE ||
8463         i == GADGET_ID_CUSTOM_COPY ||
8464         i == GADGET_ID_CUSTOM_PASTE)
8465     {
8466       if (map)
8467         MapGadget(level_editor_gadget[i]);
8468       else
8469         UnmapGadget(level_editor_gadget[i]);
8470     }
8471   }
8472 }
8473
8474 static void MapLevelEditorToolboxCustomGadgets(void)
8475 {
8476   MapOrUnmapLevelEditorToolboxCustomGadgets(TRUE);
8477 }
8478
8479 static void MapLevelEditorToolboxCustomGadgetsIfNeeded(void)
8480 {
8481   if (IS_CUSTOM_ELEMENT(properties_element) ||
8482       IS_GROUP_ELEMENT(properties_element) ||
8483       IS_EMPTY_ELEMENT(properties_element))
8484     MapLevelEditorToolboxCustomGadgets();
8485 }
8486
8487 static void UnmapLevelEditorToolboxCustomGadgets(void)
8488 {
8489   MapOrUnmapLevelEditorToolboxCustomGadgets(FALSE);
8490 }
8491
8492 static void MapOrUnmapLevelEditorToolboxDrawingGadgets(boolean map)
8493 {
8494   int i;
8495
8496   for (i = 0; i < ED_NUM_CTRL1_BUTTONS; i++)
8497   {
8498     if (i != GADGET_ID_SINGLE_ITEMS &&
8499         i != GADGET_ID_PICK_ELEMENT)
8500     {
8501       struct GadgetInfo *gi = level_editor_gadget[i];
8502
8503       if (map)
8504       {
8505         MapGadget(gi);
8506       }
8507       else
8508       {
8509         int graphic = IMG_EDITOR_NO_TOOLBOX_BUTTON;
8510         struct GraphicInfo *gd = &graphic_info[graphic];
8511
8512         UnmapGadget(gi);
8513
8514         BlitBitmap(gd->bitmap, drawto, gd->src_x, gd->src_y,
8515                    gi->width, gi->height, gi->x, gi->y);
8516
8517         redraw_mask |= REDRAW_DOOR_3;
8518       }
8519     }
8520   }
8521 }
8522
8523 static void MapLevelEditorToolboxDrawingGadgets(void)
8524 {
8525   MapOrUnmapLevelEditorToolboxDrawingGadgets(TRUE);
8526 }
8527
8528 static void UnmapLevelEditorToolboxDrawingGadgets(void)
8529 {
8530   MapOrUnmapLevelEditorToolboxDrawingGadgets(FALSE);
8531 }
8532
8533 static void UnmapDrawingArea(int id)
8534 {
8535   UnmapGadget(level_editor_gadget[drawingarea_info[id].gadget_id]);
8536 }
8537
8538 static void UnmapLevelEditorFieldGadgets(void)
8539 {
8540   int i;
8541
8542   for (i = 0; i < num_editor_gadgets; i++)
8543     if (IN_GFX_FIELD_FULL(level_editor_gadget[i]->x,
8544                           level_editor_gadget[i]->y))
8545       UnmapGadget(level_editor_gadget[i]);
8546 }
8547
8548 void UnmapLevelEditorGadgets(void)
8549 {
8550   int i;
8551
8552   for (i = 0; i < num_editor_gadgets; i++)
8553     UnmapGadget(level_editor_gadget[i]);
8554 }
8555
8556 static void ResetUndoBuffer(void)
8557 {
8558   undo_buffer_position = -1;
8559   undo_buffer_steps = -1;
8560   redo_buffer_steps = 0;
8561
8562   CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
8563
8564   level.changed = FALSE;
8565 }
8566
8567 static void DrawEditModeWindowExt(boolean remap_toolbox_gadgets)
8568 {
8569   if (remap_toolbox_gadgets)
8570   {
8571     ModifyEditorElementList();
8572     RedrawDrawingElements();
8573   }
8574
8575   if (edit_mode == ED_MODE_LEVELCONFIG)
8576     DrawLevelConfigWindow();
8577   else if (edit_mode == ED_MODE_PROPERTIES)
8578     DrawPropertiesWindow();
8579   else if (edit_mode == ED_MODE_PALETTE)
8580     DrawPaletteWindow();
8581   else  // edit_mode == ED_MODE_DRAWING
8582     DrawDrawingWindowExt(remap_toolbox_gadgets);
8583 }
8584
8585 static void DrawEditModeWindow(void)
8586 {
8587   DrawEditModeWindowExt(TRUE);
8588 }
8589
8590 static void DrawEditModeWindow_PlayfieldOnly(void)
8591 {
8592   DrawEditModeWindowExt(FALSE);
8593 }
8594
8595 static void ChangeEditModeWindow(int new_edit_mode)
8596 {
8597   edit_mode = (new_edit_mode != edit_mode ? new_edit_mode : ED_MODE_DRAWING);
8598
8599   DrawEditModeWindow();
8600 }
8601
8602 static boolean LevelChanged(void)
8603 {
8604   boolean field_changed = FALSE;
8605   int x, y;
8606
8607   for (y = 0; y < lev_fieldy; y++) 
8608     for (x = 0; x < lev_fieldx; x++)
8609       if (Tile[x][y] != level.field[x][y])
8610         field_changed = TRUE;
8611
8612   return (level.changed || field_changed);
8613 }
8614
8615 static boolean PrepareSavingIntoPersonalLevelSet(void)
8616 {
8617   static LevelDirTree *last_copied_leveldir = NULL;
8618   static LevelDirTree *last_written_leveldir = NULL;
8619   static int last_copied_level_nr = -1;
8620   static int last_written_level_nr = -1;
8621   LevelDirTree *leveldir_former = leveldir_current;
8622   int level_nr_former = level_nr;
8623   int new_level_nr;
8624
8625   // remember last mod/save so that for current session, we write
8626   // back to the same personal copy, asking only about overwrite.
8627   if (leveldir_current == last_copied_leveldir &&
8628       level_nr == last_copied_level_nr)
8629   {
8630     // "cd" to personal level set dir (as used when writing last copy)
8631     leveldir_current = last_written_leveldir;
8632     level_nr = last_written_level_nr;
8633
8634     return TRUE;
8635   }
8636
8637   if (!Request("This level is read-only! "
8638                "Save into personal level set?", REQ_ASK))
8639     return FALSE;
8640
8641   // "cd" to personal level set dir (for writing copy the first time)
8642   leveldir_current =
8643     getTreeInfoFromIdentifier(leveldir_first, getLoginName());
8644
8645   // this may happen if "setup.internal.create_user_levelset" is FALSE
8646   // or if file "levelinfo.conf" is missing in personal user level set
8647   if (leveldir_current == NULL)
8648   {
8649     Request("Cannot find personal level set?!", REQ_CONFIRM);
8650
8651     leveldir_current = leveldir_former;
8652
8653     return FALSE;
8654   }
8655
8656   // find unused level number
8657   for (new_level_nr = leveldir_current->first_level; ; new_level_nr++)
8658   {
8659     static char *level_filename = NULL;
8660
8661     setString(&level_filename, getDefaultLevelFilename(new_level_nr));
8662
8663     if (!fileExists(level_filename))
8664       break;
8665   }
8666
8667   last_copied_leveldir = leveldir_former;
8668   last_copied_level_nr = level_nr_former;
8669
8670   last_written_leveldir = leveldir_current;
8671   last_written_level_nr = level_nr = new_level_nr;
8672
8673   return TRUE;
8674 }
8675
8676 static void ModifyLevelConfigForSavingIntoPersonalLevelSet(char *former_name)
8677 {
8678   static char *filename_levelinfo = NULL, *mod_name = NULL;
8679   FILE *file;
8680
8681   // annotate this copy-and-mod in personal levelinfo.conf
8682   setString(&filename_levelinfo,
8683             getPath2(getCurrentLevelDir(), LEVELINFO_FILENAME));
8684
8685   if ((file = fopen(filename_levelinfo, MODE_APPEND)))
8686   {
8687     fprintf(file, "\n");
8688     fprintf(file, "# level %d was modified from:\n", level_nr);
8689     fprintf(file, "# - previous level set name:    %s\n",
8690             former_name);
8691     fprintf(file, "# - level within previous set:  %d \"%s\"\n",
8692             level.file_info.nr, level.name);
8693     fprintf(file, "# - previous author:            %s\n",
8694             level.author);
8695     fprintf(file, "# - previous save date:         ");
8696
8697     if (level.creation_date.src == DATE_SRC_LEVELFILE)
8698     {
8699       fprintf(file, "%04d-%02d-%02d\n",
8700               level.creation_date.year,
8701               level.creation_date.month,
8702               level.creation_date.day);
8703     }
8704     else
8705     {
8706       fprintf(file, "not recorded\n");
8707     }
8708
8709     fclose(file);
8710   }
8711
8712   if (level_nr > leveldir_current->last_level)
8713     UpdateUserLevelSet(getLoginName(), NULL, NULL, level_nr + 9);
8714
8715   // else: allow the save even if annotation failed
8716
8717   // now... spray graffiti on the old level vital statistics
8718   // user can change these; just trying to set a good baseline
8719
8720   // don't truncate names for fear of making offensive or silly:
8721   // long-named original author only recorded in levelinfo.conf.
8722   // try to fit "Joe after Bob", "Joe (ed.)", then just "Joe"
8723   if (!strEqual(level.author, leveldir_current->author))
8724   {
8725     setString(&mod_name, getStringCat3(leveldir_current->author,
8726                                        " after ", level.author));
8727
8728     if (strlen(mod_name) > MAX_LEVEL_AUTHOR_LEN)
8729       setString(&mod_name,
8730                 getStringCat2(leveldir_current->author, " (ed.)"));
8731
8732     if (strlen(mod_name) > MAX_LEVEL_AUTHOR_LEN)
8733       setString(&mod_name, leveldir_current->author);
8734
8735     strncpy(level.author, mod_name, MAX_LEVEL_AUTHOR_LEN);
8736
8737     // less worried about truncation here
8738     setString(&mod_name, getStringCat2("Mod: ", level.name));
8739     strncpy(level.name, mod_name, MAX_LEVEL_NAME_LEN);
8740   }
8741 }
8742
8743 static void CopyPlayfield(short src[MAX_LEV_FIELDX][MAX_LEV_FIELDY],
8744                           short dst[MAX_LEV_FIELDX][MAX_LEV_FIELDY])
8745 {
8746   int x, y;
8747
8748   for (x = 0; x < lev_fieldx; x++)
8749     for (y = 0; y < lev_fieldy; y++) 
8750       dst[x][y] = src[x][y];
8751 }
8752
8753 static int setSelectboxValue(int selectbox_id, int new_value)
8754 {
8755   int new_index_value = 0;
8756   int i;
8757
8758   for (i = 0; selectbox_info[selectbox_id].options[i].text != NULL; i++)
8759     if (selectbox_info[selectbox_id].options[i].value == new_value)
8760       new_index_value = i;
8761
8762   *selectbox_info[selectbox_id].value =
8763     selectbox_info[selectbox_id].options[new_index_value].value;
8764
8765   return new_index_value;
8766 }
8767
8768 static void setSelectboxSpecialActionVariablesIfNeeded(void)
8769 {
8770   int i;
8771
8772   // change action mode and arg variables according to action type variable
8773   for (i = 0; action_arg_options[i].value != -1; i++)
8774   {
8775     if (action_arg_options[i].value == custom_element_change.action_type)
8776     {
8777       int mode = action_arg_options[i].mode;
8778
8779       // only change if corresponding selectbox has changed
8780       if (selectbox_info[ED_SELECTBOX_ID_ACTION_MODE].options !=
8781           action_arg_modes[mode])
8782         custom_element_change.action_mode = -1;
8783
8784       // only change if corresponding selectbox has changed
8785       if (selectbox_info[ED_SELECTBOX_ID_ACTION_ARG].options !=
8786           action_arg_options[i].options)
8787         custom_element_change.action_arg = -1;
8788
8789       break;
8790     }
8791   }
8792 }
8793
8794 static void setSelectboxSpecialActionOptions(void)
8795 {
8796   int i;
8797
8798   // change action mode and arg selectbox according to action type selectbox
8799   for (i = 0; action_arg_options[i].value != -1; i++)
8800   {
8801     if (action_arg_options[i].value == custom_element_change.action_type)
8802     {
8803       int mode = action_arg_options[i].mode;
8804
8805       ModifyEditorSelectboxOptions(ED_SELECTBOX_ID_ACTION_MODE,
8806                                    action_arg_modes[mode]);
8807       ModifyEditorSelectboxValue(ED_SELECTBOX_ID_ACTION_MODE,
8808                                  custom_element_change.action_mode);
8809
8810       ModifyEditorSelectboxOptions(ED_SELECTBOX_ID_ACTION_ARG,
8811                                    action_arg_options[i].options);
8812       ModifyEditorSelectboxValue(ED_SELECTBOX_ID_ACTION_ARG,
8813                                  custom_element_change.action_arg);
8814       break;
8815     }
8816   }
8817 }
8818
8819 static void copy_custom_element_settings(int element_from, int element_to)
8820 {
8821   struct ElementInfo *ei_from = &element_info[element_from];
8822   struct ElementInfo *ei_to = &element_info[element_to];
8823
8824   copyElementInfo(ei_from, ei_to);
8825 }
8826
8827 static void replace_custom_element_in_settings(int element_from,
8828                                                int element_to)
8829 {
8830   int i, j, x, y;
8831
8832   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
8833   {
8834     struct ElementInfo *ei = &element_info[i];
8835
8836     for (y = 0; y < 3; y++)
8837       for (x = 0; x < 3; x++)
8838         if (ei->content.e[x][y] == element_from)
8839           ei->content.e[x][y] = element_to;
8840
8841     for (j = 0; j < ei->num_change_pages; j++)
8842     {
8843       struct ElementChangeInfo *change = &ei->change_page[j];
8844
8845       if (change->target_element == element_from)
8846         change->target_element = element_to;
8847
8848       if (change->initial_trigger_element == element_from)
8849         change->initial_trigger_element = element_to;
8850
8851       if (change->action_element == element_from)
8852         change->action_element = element_to;
8853
8854       for (y = 0; y < 3; y++)
8855         for (x = 0; x < 3; x++)
8856           if (change->target_content.e[x][y] == element_from)
8857             change->target_content.e[x][y] = element_to;
8858     }
8859
8860     if (ei->group != NULL)                              // group or internal
8861       for (j = 0; j < MAX_ELEMENTS_IN_GROUP; j++)
8862         if (ei->group->element[j] == element_from)
8863           ei->group->element[j] = element_to;
8864   }
8865 }
8866
8867 static void replace_custom_element_in_playfield(int element_from,
8868                                                 int element_to)
8869 {
8870   int x, y;
8871
8872   for (x = 0; x < lev_fieldx; x++)
8873     for (y = 0; y < lev_fieldy; y++)
8874       if (Tile[x][y] == element_from)
8875         Tile[x][y] = element_to;
8876 }
8877
8878 static boolean CopyCustomElement(int element_old, int element_new,
8879                                  int copy_mode)
8880 {
8881   int copy_mode_orig = copy_mode;
8882
8883   if (copy_mode == GADGET_ID_CUSTOM_COPY)
8884   {
8885     element_new = (IS_CUSTOM_ELEMENT(element_old) ?
8886                    EL_INTERNAL_CLIPBOARD_CUSTOM : EL_INTERNAL_CLIPBOARD_GROUP);
8887     copy_mode = GADGET_ID_CUSTOM_COPY_TO;
8888   }
8889   else if (copy_mode == GADGET_ID_CUSTOM_PASTE)
8890   {
8891     element_old = (IS_CUSTOM_ELEMENT(element_new) ?
8892                    EL_INTERNAL_CLIPBOARD_CUSTOM : EL_INTERNAL_CLIPBOARD_GROUP);
8893     copy_mode = GADGET_ID_CUSTOM_COPY_TO;
8894
8895     level.changed = TRUE;
8896   }
8897   else if (IS_CUSTOM_ELEMENT(element_old) && !IS_CUSTOM_ELEMENT(element_new))
8898   {
8899     Request("Please choose custom element!", REQ_CONFIRM);
8900
8901     return FALSE;
8902   }
8903   else if (IS_GROUP_ELEMENT(element_old) && !IS_GROUP_ELEMENT(element_new))
8904   {
8905     Request("Please choose group element!", REQ_CONFIRM);
8906
8907     return FALSE;
8908   }
8909   else if (IS_EMPTY_ELEMENT(element_old) && !IS_EMPTY_ELEMENT(element_new))
8910   {
8911     Request("Please choose empty element!", REQ_CONFIRM);
8912
8913     return FALSE;
8914   }
8915   else
8916   {
8917     level.changed = TRUE;
8918   }
8919
8920   // when modifying custom/group element, ask for copying level template
8921   if (copy_mode_orig != GADGET_ID_CUSTOM_COPY && level.use_custom_template)
8922   {
8923     if (!AskToCopyAndModifyLevelTemplate())
8924       return FALSE;
8925   }
8926
8927   if (copy_mode == GADGET_ID_CUSTOM_COPY_FROM)
8928   {
8929     copy_custom_element_settings(element_new, element_old);
8930   }
8931   else if (copy_mode == GADGET_ID_CUSTOM_COPY_TO)
8932   {
8933     copy_custom_element_settings(element_old, element_new);
8934   }
8935   else if (copy_mode == GADGET_ID_CUSTOM_EXCHANGE)
8936   {
8937     copy_custom_element_settings(element_old, EL_INTERNAL_DUMMY);
8938     copy_custom_element_settings(element_new, element_old);
8939     copy_custom_element_settings(EL_INTERNAL_DUMMY, element_new);
8940
8941     replace_custom_element_in_settings(element_old, EL_INTERNAL_DUMMY);
8942     replace_custom_element_in_settings(element_new, element_old);
8943     replace_custom_element_in_settings(EL_INTERNAL_DUMMY, element_new);
8944
8945     replace_custom_element_in_playfield(element_old, EL_INTERNAL_DUMMY);
8946     replace_custom_element_in_playfield(element_new, element_old);
8947     replace_custom_element_in_playfield(EL_INTERNAL_DUMMY, element_new);
8948   }
8949
8950   UpdateCustomElementGraphicGadgets();
8951   DrawPropertiesWindow();
8952
8953   return TRUE;
8954 }
8955
8956 static void CopyCustomElementPropertiesToEditor(int element)
8957 {
8958   int i;
8959   int current_change_page = element_info[element].current_change_page;
8960
8961   // dynamically (re)build selectbox for selecting change page
8962   for (i = 0; i < element_info[element].num_change_pages; i++)
8963   {
8964     sprintf(options_change_page_strings[i], "%d", i + 1);
8965
8966     options_change_page[i].value = i;
8967     options_change_page[i].text = options_change_page_strings[i];
8968   }
8969
8970   options_change_page[i].value = -1;
8971   options_change_page[i].text = NULL;
8972
8973   // needed here to initialize combined element properties
8974   InitElementPropertiesEngine(level.game_version);
8975
8976   element_info[element].change =
8977     &element_info[element].change_page[current_change_page];
8978
8979   custom_element = element_info[element];
8980   custom_element_change = *element_info[element].change;
8981
8982   // needed to initially set selectbox options for special action options
8983   setSelectboxSpecialActionOptions();
8984
8985   // needed to initially set selectbox value variables to reliable defaults
8986   for (i = 0; i < ED_NUM_SELECTBOX; i++)
8987     setSelectboxValue(i, *selectbox_info[i].value);
8988
8989   for (i = 0; i < NUM_ELEMENT_PROPERTIES; i++)
8990     custom_element_properties[i] = HAS_PROPERTY(element, i);
8991
8992   for (i = 0; i < NUM_CHANGE_EVENTS; i++)
8993     custom_element_change_events[i] = HAS_CHANGE_EVENT(element, i);
8994
8995   // ---------- element settings: configure (custom elements) -----------------
8996
8997   // set accessible layer selectbox help value
8998   custom_element.access_type =
8999     (IS_WALKABLE(element) ? EP_WALKABLE :
9000      IS_PASSABLE(element) ? EP_PASSABLE :
9001      custom_element.access_type);
9002   custom_element.access_layer =
9003     (IS_ACCESSIBLE_OVER(element) ? EP_ACCESSIBLE_OVER :
9004      IS_ACCESSIBLE_INSIDE(element) ? EP_ACCESSIBLE_INSIDE :
9005      IS_ACCESSIBLE_UNDER(element) ? EP_ACCESSIBLE_UNDER :
9006      custom_element.access_layer);
9007   custom_element.access_protected =
9008     (IS_PROTECTED(element) ? 1 : 0);
9009   custom_element_properties[EP_ACCESSIBLE] =
9010     (IS_ACCESSIBLE_OVER(element) ||
9011      IS_ACCESSIBLE_INSIDE(element) ||
9012      IS_ACCESSIBLE_UNDER(element));
9013
9014   // set walk-to-object action selectbox help value
9015   custom_element.walk_to_action =
9016     (IS_DIGGABLE(element) ? EP_DIGGABLE :
9017      IS_COLLECTIBLE_ONLY(element) ? EP_COLLECTIBLE_ONLY :
9018      IS_DROPPABLE(element) ? EP_DROPPABLE :
9019      IS_THROWABLE(element) ? EP_THROWABLE :
9020      IS_PUSHABLE(element) ? EP_PUSHABLE :
9021      custom_element.walk_to_action);
9022   custom_element_properties[EP_WALK_TO_OBJECT] =
9023     (IS_DIGGABLE(element) ||
9024      IS_COLLECTIBLE_ONLY(element) ||
9025      IS_DROPPABLE(element) ||
9026      IS_THROWABLE(element) ||
9027      IS_PUSHABLE(element));
9028
9029   // set smash targets selectbox help value
9030   custom_element.smash_targets =
9031     (CAN_SMASH_EVERYTHING(element) ? EP_CAN_SMASH_EVERYTHING :
9032      CAN_SMASH_ENEMIES(element) ? EP_CAN_SMASH_ENEMIES :
9033      CAN_SMASH_PLAYER(element) ? EP_CAN_SMASH_PLAYER :
9034      custom_element.smash_targets);
9035   custom_element_properties[EP_CAN_SMASH] =
9036     (CAN_SMASH_EVERYTHING(element) ||
9037      CAN_SMASH_ENEMIES(element) ||
9038      CAN_SMASH_PLAYER(element));
9039
9040   // set deadliness selectbox help value
9041   custom_element.deadliness =
9042     (DONT_TOUCH(element) ? EP_DONT_TOUCH :
9043      DONT_GET_HIT_BY(element) ? EP_DONT_GET_HIT_BY :
9044      DONT_COLLIDE_WITH(element) ? EP_DONT_COLLIDE_WITH :
9045      DONT_RUN_INTO(element) ? EP_DONT_RUN_INTO :
9046      custom_element.deadliness);
9047   custom_element_properties[EP_DEADLY] =
9048     (DONT_TOUCH(element) ||
9049      DONT_GET_HIT_BY(element) ||
9050      DONT_COLLIDE_WITH(element) ||
9051      DONT_RUN_INTO(element));
9052
9053   // ---------- element settings: advanced (custom elements) ------------------
9054
9055   // set "change by direct action" selectbox help value
9056   custom_element_change.direct_action =
9057     (HAS_CHANGE_EVENT(element, CE_NEXT_TO_PLAYER) ? CE_NEXT_TO_PLAYER :
9058      HAS_CHANGE_EVENT(element, CE_TOUCHED_BY_PLAYER) ? CE_TOUCHED_BY_PLAYER :
9059      HAS_CHANGE_EVENT(element, CE_PRESSED_BY_PLAYER) ? CE_PRESSED_BY_PLAYER :
9060      HAS_CHANGE_EVENT(element, CE_SWITCHED_BY_PLAYER) ? CE_SWITCHED_BY_PLAYER :
9061      HAS_CHANGE_EVENT(element, CE_SNAPPED_BY_PLAYER) ? CE_SNAPPED_BY_PLAYER :
9062      HAS_CHANGE_EVENT(element, CE_PUSHED_BY_PLAYER) ? CE_PUSHED_BY_PLAYER :
9063      HAS_CHANGE_EVENT(element, CE_ENTERED_BY_PLAYER) ? CE_ENTERED_BY_PLAYER :
9064      HAS_CHANGE_EVENT(element, CE_LEFT_BY_PLAYER) ? CE_LEFT_BY_PLAYER :
9065      HAS_CHANGE_EVENT(element, CE_DROPPED_BY_PLAYER) ? CE_DROPPED_BY_PLAYER :
9066      HAS_CHANGE_EVENT(element, CE_SWITCHED) ? CE_SWITCHED :
9067      HAS_CHANGE_EVENT(element, CE_HITTING_SOMETHING) ? CE_HITTING_SOMETHING :
9068      HAS_CHANGE_EVENT(element, CE_HIT_BY_SOMETHING) ? CE_HIT_BY_SOMETHING :
9069      HAS_CHANGE_EVENT(element, CE_BLOCKED) ? CE_BLOCKED :
9070      HAS_CHANGE_EVENT(element, CE_IMPACT) ? CE_IMPACT :
9071      HAS_CHANGE_EVENT(element, CE_SMASHED) ? CE_SMASHED :
9072      HAS_CHANGE_EVENT(element, CE_VALUE_CHANGES) ? CE_VALUE_CHANGES :
9073      HAS_CHANGE_EVENT(element, CE_SCORE_CHANGES) ? CE_SCORE_CHANGES :
9074      HAS_CHANGE_EVENT(element, CE_VALUE_GETS_ZERO) ? CE_VALUE_GETS_ZERO :
9075      HAS_CHANGE_EVENT(element, CE_SCORE_GETS_ZERO) ? CE_SCORE_GETS_ZERO :
9076      HAS_CHANGE_EVENT(element, CE_CLICKED_BY_MOUSE) ? CE_CLICKED_BY_MOUSE :
9077      HAS_CHANGE_EVENT(element, CE_PRESSED_BY_MOUSE) ? CE_PRESSED_BY_MOUSE :
9078      custom_element_change.direct_action);
9079
9080   // set "change by other element action" selectbox help value
9081   custom_element_change.other_action =
9082     (HAS_CHANGE_EVENT(element, CE_PLAYER_NEXT_TO_X) ? CE_PLAYER_NEXT_TO_X :
9083      HAS_CHANGE_EVENT(element, CE_PLAYER_TOUCHES_X) ? CE_PLAYER_TOUCHES_X :
9084      HAS_CHANGE_EVENT(element, CE_PLAYER_PRESSES_X) ? CE_PLAYER_PRESSES_X :
9085      HAS_CHANGE_EVENT(element, CE_PLAYER_SWITCHES_X) ? CE_PLAYER_SWITCHES_X :
9086      HAS_CHANGE_EVENT(element, CE_PLAYER_SNAPS_X) ? CE_PLAYER_SNAPS_X :
9087      HAS_CHANGE_EVENT(element, CE_PLAYER_PUSHES_X) ? CE_PLAYER_PUSHES_X :
9088      HAS_CHANGE_EVENT(element, CE_PLAYER_ENTERS_X) ? CE_PLAYER_ENTERS_X :
9089      HAS_CHANGE_EVENT(element, CE_PLAYER_LEAVES_X) ? CE_PLAYER_LEAVES_X :
9090      HAS_CHANGE_EVENT(element, CE_PLAYER_DIGS_X) ? CE_PLAYER_DIGS_X :
9091      HAS_CHANGE_EVENT(element, CE_PLAYER_COLLECTS_X) ? CE_PLAYER_COLLECTS_X :
9092      HAS_CHANGE_EVENT(element, CE_PLAYER_DROPS_X) ? CE_PLAYER_DROPS_X :
9093      HAS_CHANGE_EVENT(element, CE_NEXT_TO_X) ? CE_NEXT_TO_X :
9094      HAS_CHANGE_EVENT(element, CE_TOUCHING_X) ? CE_TOUCHING_X :
9095      HAS_CHANGE_EVENT(element, CE_HITTING_X) ? CE_HITTING_X :
9096      HAS_CHANGE_EVENT(element, CE_DIGGING_X) ? CE_DIGGING_X :
9097      HAS_CHANGE_EVENT(element, CE_HIT_BY_X) ? CE_HIT_BY_X :
9098      HAS_CHANGE_EVENT(element, CE_SWITCH_OF_X) ? CE_SWITCH_OF_X :
9099      HAS_CHANGE_EVENT(element, CE_CHANGE_OF_X) ? CE_CHANGE_OF_X :
9100      HAS_CHANGE_EVENT(element, CE_EXPLOSION_OF_X) ? CE_EXPLOSION_OF_X :
9101      HAS_CHANGE_EVENT(element, CE_MOVE_OF_X) ? CE_MOVE_OF_X :
9102      HAS_CHANGE_EVENT(element, CE_CREATION_OF_X) ? CE_CREATION_OF_X :
9103      HAS_CHANGE_EVENT(element, CE_VALUE_CHANGES_OF_X) ? CE_VALUE_CHANGES_OF_X :
9104      HAS_CHANGE_EVENT(element, CE_SCORE_CHANGES_OF_X) ? CE_SCORE_CHANGES_OF_X :
9105      HAS_CHANGE_EVENT(element, CE_VALUE_GETS_ZERO_OF_X) ? CE_VALUE_GETS_ZERO_OF_X :
9106      HAS_CHANGE_EVENT(element, CE_SCORE_GETS_ZERO_OF_X) ? CE_SCORE_GETS_ZERO_OF_X :
9107      HAS_CHANGE_EVENT(element, CE_MOUSE_CLICKED_ON_X) ? CE_MOUSE_CLICKED_ON_X :
9108      HAS_CHANGE_EVENT(element, CE_MOUSE_PRESSED_ON_X) ? CE_MOUSE_PRESSED_ON_X :
9109      custom_element_change.other_action);
9110 }
9111
9112 static void CopyGroupElementPropertiesToEditor(int element)
9113 {
9114   group_element_info = *element_info[element].group;
9115   custom_element = element_info[element];       // needed for description
9116 }
9117
9118 static void CopyEmptyElementPropertiesToEditor(int element)
9119 {
9120   custom_element = element_info[element];
9121 }
9122
9123 static void CopyClassicElementPropertiesToEditor(int element)
9124 {
9125   if (IS_PLAYER_ELEMENT(element) || COULD_MOVE_INTO_ACID(element))
9126     custom_element_properties[EP_CAN_MOVE_INTO_ACID] =
9127       getMoveIntoAcidProperty(&level, element);
9128
9129   if (MAYBE_DONT_COLLIDE_WITH(element))
9130     custom_element_properties[EP_DONT_COLLIDE_WITH] =
9131       getDontCollideWithProperty(&level, element);
9132 }
9133
9134 static void CopyElementPropertiesToEditor(int element)
9135 {
9136   if (IS_CUSTOM_ELEMENT(element))
9137     CopyCustomElementPropertiesToEditor(element);
9138   else if (IS_GROUP_ELEMENT(element))
9139     CopyGroupElementPropertiesToEditor(element);
9140   else if (IS_EMPTY_ELEMENT(element))
9141     CopyEmptyElementPropertiesToEditor(element);
9142   else
9143     CopyClassicElementPropertiesToEditor(element);
9144 }
9145
9146 static boolean AskToCopyAndModifyLevelTemplate(void)
9147 {
9148   if (Request("Copy and modify settings from level template?", REQ_ASK))
9149   {
9150     level.use_custom_template = FALSE;
9151
9152     ModifyGadget(level_editor_gadget[GADGET_ID_CUSTOM_USE_TEMPLATE_1],
9153                  GDI_CHECKED, FALSE, GDI_END);
9154     ModifyGadget(level_editor_gadget[GADGET_ID_CUSTOM_USE_TEMPLATE_2],
9155                  GDI_CHECKED, FALSE, GDI_END);
9156
9157     return TRUE;
9158   }
9159   else
9160   {
9161     LoadLevelTemplate(-1);      // this resets all element modifications ...
9162
9163     DrawEditModeWindow();       // ... and copies them to 'custom_element'
9164
9165     return FALSE;
9166   }
9167 }
9168
9169 static void CopyCustomElementPropertiesToGame(int element)
9170 {
9171   int i;
9172   int access_type_and_layer;
9173
9174   // mark that this custom element has been modified
9175   custom_element.modified_settings = TRUE;
9176   level.changed = TRUE;
9177
9178   if (level.use_custom_template)
9179     AskToCopyAndModifyLevelTemplate();
9180
9181   element_info[element] = custom_element;
9182   *element_info[element].change = custom_element_change;
9183
9184   // ---------- element settings: configure (custom elements) -----------------
9185
9186   // set accessible property from checkbox and selectbox
9187   custom_element_properties[EP_WALKABLE_OVER] = FALSE;
9188   custom_element_properties[EP_WALKABLE_INSIDE] = FALSE;
9189   custom_element_properties[EP_WALKABLE_UNDER] = FALSE;
9190   custom_element_properties[EP_PASSABLE_OVER] = FALSE;
9191   custom_element_properties[EP_PASSABLE_INSIDE] = FALSE;
9192   custom_element_properties[EP_PASSABLE_UNDER] = FALSE;
9193   access_type_and_layer = ((custom_element.access_type == EP_WALKABLE ?
9194                             EP_WALKABLE_OVER : EP_PASSABLE_OVER) +
9195                            (custom_element.access_layer - EP_ACCESSIBLE_OVER));
9196   custom_element_properties[access_type_and_layer] =
9197     custom_element_properties[EP_ACCESSIBLE];
9198   custom_element_properties[EP_PROTECTED] =
9199     (custom_element.access_protected != 0 &&
9200      custom_element_properties[EP_ACCESSIBLE]);
9201
9202   // set walk-to-object property from checkbox and selectbox
9203   custom_element_properties[EP_DIGGABLE] = FALSE;
9204   custom_element_properties[EP_COLLECTIBLE_ONLY] = FALSE;
9205   custom_element_properties[EP_DROPPABLE] = FALSE;
9206   custom_element_properties[EP_THROWABLE] = FALSE;
9207   custom_element_properties[EP_PUSHABLE] = FALSE;
9208   custom_element_properties[custom_element.walk_to_action] =
9209     custom_element_properties[EP_WALK_TO_OBJECT];
9210
9211   // set smash property from checkbox and selectbox
9212   custom_element_properties[EP_CAN_SMASH_PLAYER] = FALSE;
9213   custom_element_properties[EP_CAN_SMASH_ENEMIES] = FALSE;
9214   custom_element_properties[EP_CAN_SMASH_EVERYTHING] = FALSE;
9215   custom_element_properties[custom_element.smash_targets] =
9216     custom_element_properties[EP_CAN_SMASH];
9217
9218   // set deadliness property from checkbox and selectbox
9219   custom_element_properties[EP_DONT_RUN_INTO] = FALSE;
9220   custom_element_properties[EP_DONT_COLLIDE_WITH] = FALSE;
9221   custom_element_properties[EP_DONT_GET_HIT_BY] = FALSE;
9222   custom_element_properties[EP_DONT_TOUCH] = FALSE;
9223   custom_element_properties[custom_element.deadliness] =
9224     custom_element_properties[EP_DEADLY];
9225
9226   // ---------- element settings: advanced (custom elements) ------------------
9227
9228   // set player change event from checkbox and selectbox
9229   custom_element_change_events[CE_NEXT_TO_PLAYER] = FALSE;
9230   custom_element_change_events[CE_TOUCHED_BY_PLAYER] = FALSE;
9231   custom_element_change_events[CE_PRESSED_BY_PLAYER] = FALSE;
9232   custom_element_change_events[CE_SWITCHED_BY_PLAYER] = FALSE;
9233   custom_element_change_events[CE_SNAPPED_BY_PLAYER] = FALSE;
9234   custom_element_change_events[CE_PUSHED_BY_PLAYER] = FALSE;
9235   custom_element_change_events[CE_ENTERED_BY_PLAYER] = FALSE;
9236   custom_element_change_events[CE_LEFT_BY_PLAYER] = FALSE;
9237   custom_element_change_events[CE_DROPPED_BY_PLAYER] = FALSE;
9238   custom_element_change_events[CE_SWITCHED] = FALSE;
9239   custom_element_change_events[CE_HITTING_SOMETHING] = FALSE;
9240   custom_element_change_events[CE_HIT_BY_SOMETHING] = FALSE;
9241   custom_element_change_events[CE_BLOCKED] = FALSE;
9242   custom_element_change_events[CE_IMPACT] = FALSE;
9243   custom_element_change_events[CE_SMASHED] = FALSE;
9244   custom_element_change_events[CE_VALUE_CHANGES] = FALSE;
9245   custom_element_change_events[CE_SCORE_CHANGES] = FALSE;
9246   custom_element_change_events[CE_VALUE_GETS_ZERO] = FALSE;
9247   custom_element_change_events[CE_SCORE_GETS_ZERO] = FALSE;
9248   custom_element_change_events[CE_CLICKED_BY_MOUSE] = FALSE;
9249   custom_element_change_events[CE_PRESSED_BY_MOUSE] = FALSE;
9250   custom_element_change_events[custom_element_change.direct_action] =
9251     custom_element_change_events[CE_BY_DIRECT_ACTION];
9252
9253   // set other element action change event from checkbox and selectbox
9254   custom_element_change_events[CE_PLAYER_NEXT_TO_X] = FALSE;
9255   custom_element_change_events[CE_PLAYER_TOUCHES_X] = FALSE;
9256   custom_element_change_events[CE_PLAYER_PRESSES_X] = FALSE;
9257   custom_element_change_events[CE_PLAYER_SWITCHES_X] = FALSE;
9258   custom_element_change_events[CE_PLAYER_SNAPS_X] = FALSE;
9259   custom_element_change_events[CE_PLAYER_PUSHES_X] = FALSE;
9260   custom_element_change_events[CE_PLAYER_ENTERS_X] = FALSE;
9261   custom_element_change_events[CE_PLAYER_LEAVES_X] = FALSE;
9262   custom_element_change_events[CE_PLAYER_DIGS_X] = FALSE;
9263   custom_element_change_events[CE_PLAYER_COLLECTS_X] = FALSE;
9264   custom_element_change_events[CE_PLAYER_DROPS_X] = FALSE;
9265   custom_element_change_events[CE_NEXT_TO_X] = FALSE;
9266   custom_element_change_events[CE_TOUCHING_X] = FALSE;
9267   custom_element_change_events[CE_HITTING_X] = FALSE;
9268   custom_element_change_events[CE_DIGGING_X] = FALSE;
9269   custom_element_change_events[CE_HIT_BY_X] = FALSE;
9270   custom_element_change_events[CE_SWITCH_OF_X] = FALSE;
9271   custom_element_change_events[CE_CHANGE_OF_X] = FALSE;
9272   custom_element_change_events[CE_EXPLOSION_OF_X] = FALSE;
9273   custom_element_change_events[CE_MOVE_OF_X] = FALSE;
9274   custom_element_change_events[CE_CREATION_OF_X] = FALSE;
9275   custom_element_change_events[CE_VALUE_CHANGES_OF_X] = FALSE;
9276   custom_element_change_events[CE_SCORE_CHANGES_OF_X] = FALSE;
9277   custom_element_change_events[CE_VALUE_GETS_ZERO_OF_X] = FALSE;
9278   custom_element_change_events[CE_SCORE_GETS_ZERO_OF_X] = FALSE;
9279   custom_element_change_events[CE_MOUSE_CLICKED_ON_X] = FALSE;
9280   custom_element_change_events[CE_MOUSE_PRESSED_ON_X] = FALSE;
9281   custom_element_change_events[custom_element_change.other_action] =
9282     custom_element_change_events[CE_BY_OTHER_ACTION];
9283
9284   for (i = 0; i < NUM_ELEMENT_PROPERTIES; i++)
9285     SET_PROPERTY(element, i, custom_element_properties[i]);
9286
9287   for (i = 0; i < NUM_CHANGE_EVENTS; i++)
9288     SET_CHANGE_EVENT(element, i, custom_element_change_events[i]);
9289
9290   // copy change events also to special level editor variable
9291   custom_element = element_info[element];
9292   custom_element_change = *element_info[element].change;
9293
9294   // needed here to restore runtime value "element_info[element].gfx_element"
9295   InitElementPropertiesGfxElement();
9296 }
9297
9298 static void CopyGroupElementPropertiesToGame(int element)
9299 {
9300   // mark that this group element has been modified
9301   custom_element.modified_settings = TRUE;
9302   level.changed = TRUE;
9303
9304   if (level.use_custom_template)
9305     AskToCopyAndModifyLevelTemplate();
9306
9307   element_info[element] = custom_element;
9308   *element_info[element].group = group_element_info;
9309
9310   // needed here to restore runtime value "element_info[element].gfx_element"
9311   InitElementPropertiesGfxElement();
9312 }
9313
9314 static void CopyEmptyElementPropertiesToGame(int element)
9315 {
9316   // mark that this empty element has been modified
9317   custom_element.modified_settings = TRUE;
9318   level.changed = TRUE;
9319
9320   if (level.use_custom_template)
9321     AskToCopyAndModifyLevelTemplate();
9322
9323   element_info[element] = custom_element;
9324
9325   // needed here to restore runtime value "element_info[element].gfx_element"
9326   InitElementPropertiesGfxElement();
9327 }
9328
9329 static void CopyClassicElementPropertiesToGame(int element)
9330 {
9331   if (IS_PLAYER_ELEMENT(element) || COULD_MOVE_INTO_ACID(element))
9332     setMoveIntoAcidProperty(&level, element,
9333                             custom_element_properties[EP_CAN_MOVE_INTO_ACID]);
9334
9335   if (MAYBE_DONT_COLLIDE_WITH(element))
9336     setDontCollideWithProperty(&level, element,
9337                               custom_element_properties[EP_DONT_COLLIDE_WITH]);
9338 }
9339
9340 static void CopyElementPropertiesToGame(int element)
9341 {
9342   if (IS_CUSTOM_ELEMENT(element))
9343     CopyCustomElementPropertiesToGame(element);
9344   else if (IS_GROUP_ELEMENT(element))
9345     CopyGroupElementPropertiesToGame(element);
9346   else if (IS_EMPTY_ELEMENT(element))
9347     CopyEmptyElementPropertiesToGame(element);
9348   else
9349     CopyClassicElementPropertiesToGame(element);
9350 }
9351
9352 #if DEBUG
9353 static void CheckElementDescriptions(void)
9354 {
9355   int i;
9356
9357   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
9358     if (getElementDescriptionFilename(i) == NULL && !IS_OBSOLETE(i))
9359       Warn("no element description file for element '%s'", EL_NAME(i));
9360 }
9361 #endif
9362
9363 static int getMaxEdFieldX(boolean has_scrollbar)
9364 {
9365   int scrollbar_width = (has_scrollbar ? ED_SCROLLBUTTON_XSIZE : 0);
9366   int sxsize = SXSIZE - scrollbar_width;
9367   int max_ed_fieldx = sxsize / ed_tilesize;
9368
9369   return max_ed_fieldx;
9370 }
9371
9372 static int getMaxEdFieldY(boolean has_scrollbar)
9373 {
9374   int infotext_height = (IN_PIX_FIELD(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY) ?
9375                          INFOTEXT_YSIZE_FULL : 0);
9376   int scrollbar_height = (has_scrollbar ? ED_SCROLLBUTTON_YSIZE : 0);
9377   int sysize = SYSIZE - scrollbar_height - infotext_height;
9378   int max_ed_fieldy = sysize / ed_tilesize;
9379
9380   return max_ed_fieldy;
9381 }
9382
9383 static void InitZoomLevelSettings(int zoom_tilesize)
9384 {
9385   static int last_game_engine_type = GAME_ENGINE_TYPE_UNKNOWN;
9386
9387   if (zoom_tilesize == -1 && level.game_engine_type != last_game_engine_type)
9388   {
9389     ed_tilesize = setup.auto_setup.editor_zoom_tilesize;
9390     ed_tilesize_default = DEFAULT_EDITOR_TILESIZE;
9391
9392     // make sure that tile size is always a power of 2
9393     ed_tilesize = (1 << log_2(ed_tilesize));
9394
9395     if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
9396     {
9397       ed_tilesize = DEFAULT_EDITOR_TILESIZE_MM;
9398       ed_tilesize_default = DEFAULT_EDITOR_TILESIZE_MM;
9399     }
9400   }
9401
9402   last_game_engine_type = level.game_engine_type;
9403
9404   // limit zoom tilesize by upper and lower bound
9405   ed_tilesize = MIN(MAX(MICRO_TILESIZE, ed_tilesize), TILESIZE);
9406
9407   // store zoom tilesize in auto setup file only if it was manually changed
9408   if (zoom_tilesize != -1)
9409     setup.auto_setup.editor_zoom_tilesize = ed_tilesize;
9410
9411   MAX_ED_FIELDX = getMaxEdFieldX(FALSE);
9412   MAX_ED_FIELDY = getMaxEdFieldY(FALSE);
9413 }
9414
9415 static void InitDrawingElements(void)
9416 {
9417   static int game_engine_type_last = GAME_ENGINE_TYPE_UNKNOWN;
9418
9419   if (level.game_engine_type == game_engine_type_last)
9420     return;
9421
9422   if (level.game_engine_type == GAME_ENGINE_TYPE_BD)
9423   {
9424     new_element1 = EL_BD_WALL;
9425     new_element2 = EL_EMPTY;
9426     new_element3 = EL_BD_SAND;
9427   }
9428   else if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
9429   {
9430     new_element1 = EL_SP_CHIP_SINGLE;
9431     new_element2 = EL_EMPTY;
9432     new_element3 = EL_SP_BASE;
9433   }
9434   else if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
9435   {
9436     new_element1 = EL_MM_MIRROR_START;
9437     new_element2 = EL_EMPTY;
9438     new_element3 = EL_MM_WOODEN_WALL;
9439   }
9440   else
9441   {
9442     new_element1 = EL_WALL;
9443     new_element2 = EL_EMPTY;
9444     new_element3 = EL_SAND;
9445   }
9446
9447   game_engine_type_last = level.game_engine_type;
9448 }
9449
9450 static void InitLevelSetInfo(void)
9451 {
9452   snprintf(levelset_name,   MAX_LEVEL_NAME_LEN + 1,
9453            "%s", leveldir_current->name);
9454   snprintf(levelset_author, MAX_LEVEL_AUTHOR_LEN + 1,
9455            "%s", leveldir_current->author);
9456
9457   levelset_num_levels = leveldir_current->levels;
9458
9459   levelset_use_levelset_artwork = FALSE;
9460   levelset_copy_level_template = FALSE;
9461
9462   levelset_save_mode = LEVELSET_SAVE_MODE_UPDATE;
9463 }
9464
9465 static void ChangeEditorToLevelSet(char *levelset_subdir)
9466 {
9467   leveldir_current = getTreeInfoFromIdentifier(leveldir_first, levelset_subdir);
9468
9469   // the previous level set might have used custom artwork
9470   ReloadCustomArtwork(0);
9471
9472   LoadLevelSetup_SeriesInfo();
9473
9474   SaveLevelSetup_LastSeries();
9475   SaveLevelSetup_SeriesInfo();
9476
9477   TapeErase();
9478
9479   LoadLevel(level_nr);
9480   LoadScore(level_nr);
9481
9482   DrawLevelEd();
9483 }
9484
9485 static boolean useEditorDoorAnimation(void)
9486 {
9487   struct RectWithBorder *vp_door_1 = &viewport.door_1[GAME_MODE_MAIN];
9488   boolean door_1_viewport_unchanged =
9489     (vp_door_1->x      == DX     &&
9490      vp_door_1->y      == DY     &&
9491      vp_door_1->width  == DXSIZE &&
9492      vp_door_1->height == DYSIZE);
9493   boolean door_1_contains_toolbox =
9494     (EX >= DX &&
9495      EY >= DY &&
9496      EX + EXSIZE <= DX + DXSIZE &&
9497      EY + EYSIZE <= DY + DYSIZE);
9498
9499   return (door_1_viewport_unchanged && door_1_contains_toolbox);
9500 }
9501
9502 static void DrawEditorDoorBackground(int graphic, int x, int y,
9503                                      int width, int height)
9504 {
9505   struct GraphicInfo *g = &graphic_info[graphic];
9506
9507   if (g->bitmap != NULL)
9508     BlitBitmap(g->bitmap, drawto, g->src_x, g->src_y,
9509                MIN(width, g->width), MIN(height, g->height), x, y);
9510   else
9511     ClearRectangle(drawto, x, y, width, height);
9512 }
9513
9514 static void DrawEditorDoorContent(void)
9515 {
9516   // needed for gadgets drawn on background (like palette scrollbar)
9517   SetDoorBackgroundImage(IMG_UNDEFINED);
9518
9519   // copy default editor door content to main double buffer
9520   DrawEditorDoorBackground(IMG_BACKGROUND_PALETTE, DX, DY, DXSIZE, DYSIZE);
9521
9522   // draw bigger door
9523   DrawSpecialEditorDoor();
9524
9525   // draw new control window
9526   DrawEditorDoorBackground(IMG_BACKGROUND_TOOLBOX, EX, EY, EXSIZE, EYSIZE);
9527
9528   // draw all toolbox gadgets to editor doors
9529   MapControlButtons();
9530
9531   // when returning from test game to properties page, redraw toolbox gadgets
9532   if (edit_mode == ED_MODE_PROPERTIES)
9533   {
9534     UnmapLevelEditorToolboxDrawingGadgets();
9535     UnmapLevelEditorToolboxCustomGadgets();
9536
9537     MapLevelEditorToolboxCustomGadgetsIfNeeded();
9538   }
9539
9540   // draw all palette gadgets to editor doors
9541   ModifyEditorElementList();
9542   RedrawDrawingElements();
9543
9544   // copy actual editor door content to door double buffer for OpenDoor()
9545   BlitBitmap(drawto, bitmap_db_door_1, DX, DY, DXSIZE, DYSIZE, 0, 0);
9546 }
9547
9548 void DrawLevelEd(void)
9549 {
9550   int fade_mask = REDRAW_FIELD;
9551
9552   FadeSoundsAndMusic();
9553
9554   if (CheckFadeAll())
9555     fade_mask = REDRAW_ALL;
9556
9557   FadeOut(fade_mask);
9558
9559   // needed if different viewport properties defined for editor
9560   ChangeViewportPropertiesIfNeeded();
9561
9562   ClearField();
9563
9564   InitZoomLevelSettings(-1);
9565   InitDrawingElements();
9566   InitLevelSetInfo();
9567
9568 #if DEBUG
9569   CheckElementDescriptions();
9570 #endif
9571
9572   if (level_editor_test_game)
9573   {
9574     CopyPlayfield(level.field, Tile);
9575     CopyPlayfield(TileBackup, level.field);
9576
9577     level_editor_test_game = FALSE;
9578   }
9579   else
9580   {
9581     edit_mode = ED_MODE_DRAWING;
9582     edit_mode_levelconfig = ED_MODE_LEVELCONFIG_LEVEL;
9583     edit_mode_properties = ED_MODE_PROPERTIES_INFO;
9584
9585     ResetUndoBuffer();
9586
9587     level_xpos = -1;
9588     level_ypos = -1;
9589   }
9590
9591   // redraw_mask |= REDRAW_ALL;
9592
9593   FreeLevelEditorGadgets();
9594   CreateLevelEditorGadgets();
9595
9596   ReinitializeElementList();            // update dynamic level element list
9597   ReinitializeElementListButtons();     // custom element may look different
9598
9599   InitElementPropertiesGfxElement();
9600
9601   UnmapAllGadgets();
9602
9603   DrawEditModeWindow_PlayfieldOnly();
9604
9605   DrawMaskedBorder(fade_mask);
9606
9607   // use door animation if door 1 viewport is unchanged and contains toolbox
9608   if (useEditorDoorAnimation())
9609   {
9610     FadeIn(fade_mask);
9611
9612     DrawEditorDoorContent();
9613
9614     OpenDoor(DOOR_OPEN_1 | DOOR_FORCE_ANIM);
9615   }
9616   else
9617   {
9618     DrawEditorDoorContent();
9619
9620     FadeIn(fade_mask);
9621   }
9622
9623   SetDoorState(DOOR_OPEN_1 | DOOR_OPEN_2);
9624 }
9625
9626 static void AdjustDrawingAreaGadgets(void)
9627 {
9628   int ed_xsize = lev_fieldx + 2;
9629   int ed_ysize = lev_fieldy + 2;
9630   int max_ed_fieldx = MAX_ED_FIELDX;
9631   int max_ed_fieldy = MAX_ED_FIELDY;
9632   boolean horizontal_scrollbar_needed;
9633   boolean vertical_scrollbar_needed;
9634   int x, y, width, height;
9635
9636   if (suppressBorderElement())
9637   {
9638     ed_xsize = lev_fieldx;
9639     ed_ysize = lev_fieldy;
9640   }
9641
9642   // check if we need any scrollbars
9643   horizontal_scrollbar_needed = (ed_xsize > max_ed_fieldx);
9644   vertical_scrollbar_needed   = (ed_ysize > max_ed_fieldy);
9645
9646   // check if we have a smaller editor field because of scrollbars
9647   max_ed_fieldx = getMaxEdFieldX(vertical_scrollbar_needed);
9648   max_ed_fieldy = getMaxEdFieldY(horizontal_scrollbar_needed);
9649
9650   // check again if we now need more scrollbars because of less space
9651   horizontal_scrollbar_needed = (ed_xsize > max_ed_fieldx);
9652   vertical_scrollbar_needed   = (ed_ysize > max_ed_fieldy);
9653
9654   // check if editor field gets even smaller after adding new scrollbars
9655   max_ed_fieldx = getMaxEdFieldX(vertical_scrollbar_needed);
9656   max_ed_fieldy = getMaxEdFieldY(horizontal_scrollbar_needed);
9657
9658   ed_fieldx = (ed_xsize > max_ed_fieldx ? max_ed_fieldx : ed_xsize);
9659   ed_fieldy = (ed_ysize > max_ed_fieldy ? max_ed_fieldy : ed_ysize);
9660
9661   x = SX + ed_fieldx * ed_tilesize;
9662   y = SY + ed_fieldy * ed_tilesize;
9663
9664   width  = ed_fieldx * ed_tilesize - 2 * ED_SCROLLBUTTON_XSIZE;
9665   height = ed_fieldy * ed_tilesize - 2 * ED_SCROLLBUTTON_YSIZE;
9666
9667   // adjust drawing area gadget
9668   ModifyGadget(level_editor_gadget[GADGET_ID_DRAWING_LEVEL],
9669                GDI_AREA_SIZE, ed_fieldx, ed_fieldy,
9670                GDI_ITEM_SIZE, ed_tilesize, ed_tilesize,
9671                GDI_END);
9672
9673   // adjust horizontal scrollbar gadgets
9674   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LEFT],
9675                GDI_Y, y,
9676                GDI_END);
9677   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_RIGHT],
9678                GDI_X, x - ED_SCROLLBUTTON_XSIZE,
9679                GDI_Y, y,
9680                GDI_END);
9681   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
9682                GDI_Y, y,
9683                GDI_WIDTH, width,
9684                GDI_SCROLLBAR_ITEMS_VISIBLE, ed_fieldx,
9685                GDI_END);
9686
9687   // adjust vertical scrollbar gadgets
9688   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_UP],
9689                GDI_X, x,
9690                GDI_END);
9691   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_DOWN],
9692                GDI_X, x,
9693                GDI_Y, y - ED_SCROLLBUTTON_YSIZE,
9694                GDI_END);
9695   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
9696                GDI_X, x,
9697                GDI_HEIGHT, height,
9698                GDI_SCROLLBAR_ITEMS_VISIBLE, ed_fieldy,
9699                GDI_END);
9700 }
9701
9702 static void AdjustLevelScrollPosition(void)
9703 {
9704   if (level_xpos < -1)
9705     level_xpos = -1;
9706   if (level_xpos > lev_fieldx - ed_fieldx + 1)
9707     level_xpos = lev_fieldx - ed_fieldx + 1;
9708   if (lev_fieldx < ed_fieldx - 2)
9709     level_xpos = -1;
9710
9711   if (level_ypos < -1)
9712     level_ypos = -1;
9713   if (level_ypos > lev_fieldy - ed_fieldy + 1)
9714     level_ypos = lev_fieldy - ed_fieldy + 1;
9715   if (lev_fieldy < ed_fieldy - 2)
9716     level_ypos = -1;
9717
9718   if (suppressBorderElement())
9719   {
9720     level_xpos = 0;
9721     level_ypos = 0;
9722   }
9723 }
9724
9725 static void AdjustEditorScrollbar(int id)
9726 {
9727   struct GadgetInfo *gi = level_editor_gadget[id];
9728   int items_max, items_visible, item_position;
9729
9730   if (id == GADGET_ID_SCROLL_HORIZONTAL)
9731   {
9732     items_max = MAX(lev_fieldx + 2, ed_fieldx);
9733     items_visible = ed_fieldx;
9734     item_position = level_xpos + 1;
9735   }
9736   else
9737   {
9738     items_max = MAX(lev_fieldy + 2, ed_fieldy);
9739     items_visible = ed_fieldy;
9740     item_position = level_ypos + 1;
9741   }
9742
9743   if (item_position > items_max - items_visible)
9744     item_position = items_max - items_visible;
9745
9746   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
9747                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
9748 }
9749
9750 static void AdjustElementListScrollbar(void)
9751 {
9752   struct GadgetInfo *gi = level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL];
9753   int items_max, items_visible, item_position;
9754
9755   // correct position of element list scrollbar
9756   if (element_shift < 0)
9757     element_shift = 0;
9758   if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
9759     element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
9760
9761   items_max = num_editor_elements / ED_ELEMENTLIST_BUTTONS_HORIZ;
9762   items_visible = ED_ELEMENTLIST_BUTTONS_VERT;
9763   item_position = element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ;
9764
9765   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
9766                GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
9767                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
9768 }
9769
9770 static void ModifyEditorCounterValue(int counter_id, int new_value)
9771 {
9772   int *counter_value = counterbutton_info[counter_id].value;
9773   int gadget_id = counterbutton_info[counter_id].gadget_id_text;
9774   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9775
9776   ModifyGadget(gi, GDI_NUMBER_VALUE, new_value, GDI_END);
9777
9778   if (counter_value != NULL)
9779     *counter_value = gi->textinput.number_value;
9780 }
9781
9782 static void ModifyEditorCounterLimits(int counter_id, int min, int max)
9783 {
9784   int gadget_id = counterbutton_info[counter_id].gadget_id_text;
9785   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9786
9787   ModifyGadget(gi, GDI_NUMBER_MIN, min, GDI_NUMBER_MAX, max, GDI_END);
9788
9789   if (counter_id >= ED_COUNTER_ID_ELEMENT_VALUE1 &&
9790       counter_id <= ED_COUNTER_ID_ELEMENT_VALUE4)
9791   {
9792     int gadget_id_up = counterbutton_info[counter_id].gadget_id_up;
9793     struct GadgetInfo *gi_up = level_editor_gadget[gadget_id_up];
9794
9795     ModifyGadget(gi, GDI_TEXT_SIZE, (max < 10 ? 1 : 3), GDI_END);
9796     ModifyGadget(gi_up, GDI_X, gi->x + gi->width + ED_GADGET_SMALL_DISTANCE,
9797                  GDI_END);
9798   }
9799 }
9800
9801 static void ModifyEditorSelectboxValue(int selectbox_id, int new_value)
9802 {
9803   int gadget_id = selectbox_info[selectbox_id].gadget_id;
9804   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9805   int new_index_value = setSelectboxValue(selectbox_id, new_value);
9806
9807   ModifyGadget(gi, GDI_SELECTBOX_INDEX, new_index_value, GDI_END);
9808 }
9809
9810 static void ModifyEditorSelectboxOptions(int selectbox_id,
9811                                          struct ValueTextInfo *options)
9812 {
9813   int gadget_id = selectbox_info[selectbox_id].gadget_id;
9814   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9815
9816   selectbox_info[selectbox_id].options = options;
9817
9818   // set index to zero -- list may be shorter now (correct later, if needed)
9819   ModifyGadget(gi, GDI_SELECTBOX_INDEX, 0,
9820                GDI_SELECTBOX_OPTIONS, options, GDI_END);
9821 }
9822
9823 static void ModifyEditorDrawingArea(int drawingarea_id, int xsize, int ysize)
9824 {
9825   int gadget_id = drawingarea_info[drawingarea_id].gadget_id;
9826   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9827
9828   drawingarea_info[drawingarea_id].area_xsize = xsize;
9829   drawingarea_info[drawingarea_id].area_ysize = ysize;
9830
9831   ModifyGadget(gi, GDI_AREA_SIZE, xsize, ysize, GDI_END);
9832 }
9833
9834 static void ModifyEditorElementList(void)
9835 {
9836   int i;
9837
9838   if (!use_permanent_palette && edit_mode != ED_MODE_PALETTE)
9839     return;
9840
9841   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
9842   {
9843     int gadget_id = GADGET_ID_ELEMENTLIST_FIRST + i;
9844     struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9845     struct GadgetDesign *gd = &gi->deco.design;
9846     int element = editor_elements[element_shift + i];
9847     int tile_size = BUTTON_TILE_SIZE(editor.palette.tile_size);
9848
9849     UnmapGadget(gi);
9850
9851     getEditorGraphicSource(element, tile_size, &gd->bitmap, &gd->x, &gd->y);
9852
9853     ModifyGadget(gi, GDI_INFO_TEXT, getElementInfoText(element), GDI_END);
9854
9855     MapGadget(gi);
9856   }
9857 }
9858
9859 static void DrawDrawingElementGraphic(int element, struct XYTileSize *pos)
9860 {
9861   int graphic = el2edimg(element);
9862   int tile_size = BUTTON_TILE_SIZE(pos->tile_size);
9863
9864   if (pos->x == -1 &&
9865       pos->y == -1)
9866     return;
9867
9868   DrawSizedGraphicExt(drawto, DX + pos->x, DY + pos->y, graphic, 0, tile_size);
9869 }
9870
9871 static void ModifyDrawingElementButton(int element, int id)
9872 {
9873   struct GadgetInfo *gi = level_editor_gadget[id];
9874   Bitmap *deco_bitmap;
9875   int deco_x, deco_y;
9876   int tile_size = gi->deco.width;
9877
9878   getEditorGraphicSource(element, tile_size, &deco_bitmap, &deco_x, &deco_y);
9879
9880   ModifyGadget(gi, GDI_DECORATION_DESIGN, deco_bitmap, deco_x, deco_y, GDI_END);
9881 }
9882
9883 static void PickDrawingElement(int button, int element)
9884 {
9885   struct
9886   {
9887     int *new_element;
9888     struct XYTileSize *pos;
9889     int id;
9890   } de, drawing_elements[] =
9891   {
9892     { &new_element1, &editor.palette.element_left,   GADGET_ID_ELEMENT_LEFT   },
9893     { &new_element2, &editor.palette.element_middle, GADGET_ID_ELEMENT_MIDDLE },
9894     { &new_element3, &editor.palette.element_right,  GADGET_ID_ELEMENT_RIGHT  },
9895   };
9896
9897   if (button < 1 || button > 3)
9898     return;
9899
9900   if (IS_MM_WALL(element))
9901     element = map_mm_wall_element(element);
9902
9903   de = drawing_elements[button - 1];
9904
9905   *de.new_element = element;    // update global drawing element variable
9906
9907   DrawDrawingElementGraphic(element, de.pos);
9908   ModifyDrawingElementButton(element, de.id);
9909
9910   redraw_mask |= REDRAW_DOOR_1;
9911 }
9912
9913 static void RedrawDrawingElements(void)
9914 {
9915   PickDrawingElement(1, new_element1);
9916   PickDrawingElement(2, new_element2);
9917   PickDrawingElement(3, new_element3);
9918 }
9919
9920 static void DrawDrawingWindowExt(boolean remap_toolbox_gadgets)
9921 {
9922   stick_element_properties_window = FALSE;
9923
9924   SetMainBackgroundImage(IMG_UNDEFINED);
9925   ClearField();
9926
9927   UnmapLevelEditorFieldGadgets();
9928
9929   AdjustDrawingAreaGadgets();
9930   AdjustLevelScrollPosition();
9931   AdjustEditorScrollbar(GADGET_ID_SCROLL_HORIZONTAL);
9932   AdjustEditorScrollbar(GADGET_ID_SCROLL_VERTICAL);
9933
9934   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
9935
9936   MapMainDrawingArea();
9937
9938   if (remap_toolbox_gadgets)
9939   {
9940     UnmapLevelEditorToolboxCustomGadgets();
9941     MapLevelEditorToolboxDrawingGadgets();
9942   }
9943 }
9944
9945 static void DrawDrawingWindow(void)
9946 {
9947   DrawDrawingWindowExt(TRUE);
9948 }
9949
9950 static int getTabulatorBarWidth(void)
9951 {
9952   struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_PROPERTIES_INFO];
9953   struct GadgetInfo *gd_gi4 = level_editor_gadget[GADGET_ID_PROPERTIES_CHANGE];
9954
9955   return gd_gi4->x - gd_gi1->x + gd_gi4->width;
9956 }
9957
9958 static int getTabulatorBarHeight(void)
9959 {
9960   return ED_TAB_BAR_HEIGHT;
9961 }
9962
9963 static Pixel getTabulatorBarColor(void)
9964 {
9965   struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_LEVELCONFIG_LEVEL];
9966   struct GadgetDesign *gd = &gd_gi1->alt_design[GD_BUTTON_UNPRESSED];
9967   int gd_x = gd->x + gd_gi1->border.width / 2;
9968   int gd_y = gd->y + gd_gi1->height - 1;
9969
9970   return GetPixel(gd->bitmap, gd_x, gd_y);
9971 }
9972
9973 static void DrawLevelConfigTabulatorGadgets(void)
9974 {
9975   struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_LEVELCONFIG_LEVEL];
9976   Pixel tab_color = getTabulatorBarColor();
9977   int id_first = ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL;
9978   int id_last  = ED_TEXTBUTTON_ID_LEVELCONFIG_EDITOR;
9979   int i;
9980
9981   // draw additional "engine" tabulator when using native BD engine
9982   if (level.game_engine_type == GAME_ENGINE_TYPE_BD)
9983     id_last = ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE;
9984
9985   for (i = id_first; i <= id_last; i++)
9986   {
9987     int gadget_id = textbutton_info[i].gadget_id;
9988     struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9989     boolean active = (i != edit_mode_levelconfig);
9990
9991     // draw background line below tabulator button
9992     ClearRectangleOnBackground(drawto, gi->x, gi->y + gi->height, gi->width, 1);
9993
9994     // draw solid line below inactive tabulator buttons
9995     if (!active && tab_color != BLACK_PIXEL)    // black => transparent
9996       FillRectangle(drawto, gi->x, gi->y + gi->height, gi->width,
9997                     ED_GADGET_TINY_DISTANCE, tab_color);
9998
9999     ModifyGadget(gi, GDI_ACTIVE, active, GDI_END);
10000     MapTextbuttonGadget(i);
10001   }
10002
10003   // draw little border line below tabulator buttons
10004   if (tab_color != BLACK_PIXEL)                 // black => transparent
10005     FillRectangle(drawto, gd_gi1->x, gd_gi1->y + gd_gi1->height +
10006                   ED_GADGET_TINY_DISTANCE,
10007                   getTabulatorBarWidth(), getTabulatorBarHeight(), tab_color);
10008 }
10009
10010 static void DrawPropertiesTabulatorGadgets(void)
10011 {
10012   struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_PROPERTIES_INFO];
10013   struct GadgetDesign *gd = &gd_gi1->alt_design[GD_BUTTON_UNPRESSED];
10014   int gd_x = gd->x + gd_gi1->border.width / 2;
10015   int gd_y = gd->y + gd_gi1->height - 1;
10016   Pixel tab_color = GetPixel(gd->bitmap, gd_x, gd_y);
10017   int id_first = ED_TEXTBUTTON_ID_PROPERTIES_INFO;
10018   int id_last  = ED_TEXTBUTTON_ID_PROPERTIES_CONFIG;
10019   int i;
10020
10021   // draw two config tabulators for player elements
10022   if (IS_PLAYER_ELEMENT(properties_element))
10023     id_last = ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2;
10024
10025   // draw two config and one "change" tabulator for custom elements
10026   if (IS_CUSTOM_ELEMENT(properties_element))
10027     id_last = ED_TEXTBUTTON_ID_PROPERTIES_CHANGE;
10028
10029   for (i = id_first; i <= id_last; i++)
10030   {
10031     int gadget_id = textbutton_info[i].gadget_id;
10032     struct GadgetInfo *gi = level_editor_gadget[gadget_id];
10033     boolean active = (i != edit_mode_properties);
10034
10035     // use "config 1" and "config 2" instead of "config" for players and CEs
10036     if (i == ED_TEXTBUTTON_ID_PROPERTIES_CONFIG &&
10037         (IS_PLAYER_ELEMENT(properties_element) ||
10038          IS_CUSTOM_ELEMENT(properties_element)))
10039       continue;
10040
10041     // draw background line below tabulator button
10042     ClearRectangleOnBackground(drawto, gi->x, gi->y + gi->height, gi->width, 1);
10043
10044     // draw solid line below inactive tabulator buttons
10045     if (!active && tab_color != BLACK_PIXEL)    // black => transparent
10046       FillRectangle(drawto, gi->x, gi->y + gi->height, gi->width,
10047                     ED_GADGET_TINY_DISTANCE, tab_color);
10048
10049     ModifyGadget(gi, GDI_ACTIVE, active, GDI_END);
10050     MapTextbuttonGadget(i);
10051   }
10052
10053   // draw little border line below tabulator buttons
10054   if (tab_color != BLACK_PIXEL)                 // black => transparent
10055     FillRectangle(drawto, gd_gi1->x, gd_gi1->y + gd_gi1->height +
10056                   ED_GADGET_TINY_DISTANCE,
10057                   getTabulatorBarWidth(), getTabulatorBarHeight(), tab_color);
10058 }
10059
10060 static void PrintInfoText(char *text, int font_nr, int xpos, int ypos)
10061 {
10062   DrawText(SX + xpos, SY + ypos, text, font_nr);
10063 }
10064
10065 static int PrintElementDescriptionFromFile(char *filename, int font_nr,
10066                                            int xpos, int ypos)
10067 {
10068   int font_width = getFontWidth(font_nr);
10069   int font_height = getFontHeight(font_nr);
10070   int max_chars_per_line = (SXSIZE - 2 * xpos) / font_width;
10071   int max_lines_drawable = (SYSIZE - ypos) / font_height - 1;
10072
10073   return DrawTextFile(SX + xpos, SY + ypos, filename, font_nr,
10074                       max_chars_per_line, -1, max_lines_drawable, 0, -1,
10075                       TRUE, FALSE, FALSE);
10076 }
10077
10078 static void DrawLevelConfigLevel(void)
10079 {
10080   int i;
10081
10082   // draw counter gadgets
10083   for (i = ED_COUNTER_ID_LEVEL_FIRST; i <= ED_COUNTER_ID_LEVEL_LAST; i++)
10084     MapCounterButtons(i);
10085
10086   // draw checkbutton gadgets
10087   for (i = ED_CHECKBUTTON_ID_LEVEL_FIRST; i<= ED_CHECKBUTTON_ID_LEVEL_LAST; i++)
10088     MapCheckbuttonGadget(i);
10089
10090   // draw selectbox gadgets
10091   for (i = ED_SELECTBOX_ID_LEVEL_FIRST; i <= ED_SELECTBOX_ID_LEVEL_LAST; i++)
10092     MapSelectboxGadget(i);
10093
10094   // draw text input gadgets
10095   for (i = ED_TEXTINPUT_ID_LEVEL_FIRST; i <= ED_TEXTINPUT_ID_LEVEL_LAST; i++)
10096     MapTextInputGadget(i);
10097 }
10098
10099 static char *getLevelSubdirFromSaveMode(int save_mode)
10100 {
10101   if (save_mode == LEVELSET_SAVE_MODE_CREATE)
10102     return getNewUserLevelSubdir();
10103
10104   return leveldir_current->subdir;
10105 }
10106
10107 static void DrawLevelConfigLevelSet_DirectoryInfo(void)
10108 {
10109   char *directory_text = "Level set directory:";
10110   char *directory_name = getLevelSubdirFromSaveMode(levelset_save_mode);
10111   int font1_nr = FONT_TEXT_1;
10112   int font2_nr = FONT_TEXT_2;
10113   int font1_height = getFontHeight(font1_nr);
10114   int yoffset_above = font1_height + ED_GADGET_LINE_DISTANCE;
10115   int x = ED_LEVEL_SETTINGS_X(0);
10116   int y = ED_LEVEL_SETTINGS_Y(6);
10117
10118   PrintInfoText(directory_text, font1_nr, x, y - yoffset_above);
10119   PrintInfoText(directory_name, font2_nr, x, y);
10120 }
10121
10122 static void DrawLevelConfigLevelSet(void)
10123 {
10124   boolean artwork_exists = checkIfCustomArtworkExistsForCurrentLevelSet();
10125   boolean template_exists = fileExists(getLocalLevelTemplateFilename());
10126   int i;
10127
10128   // draw counter gadgets
10129   for (i = ED_COUNTER_ID_LEVELSET_FIRST; i <= ED_COUNTER_ID_LEVELSET_LAST; i++)
10130     MapCounterButtons(i);
10131
10132   // draw checkbutton gadgets
10133   for (i = ED_CHECKBUTTON_ID_LEVELSET_FIRST; i <= ED_CHECKBUTTON_ID_LEVELSET_LAST; i++)
10134   {
10135     if (levelset_save_mode == LEVELSET_SAVE_MODE_UPDATE ||
10136         (i == ED_CHECKBUTTON_ID_USE_LEVELSET_ARTWORK && !artwork_exists) ||
10137         (i == ED_CHECKBUTTON_ID_COPY_LEVEL_TEMPLATE  && !template_exists))
10138       continue;
10139
10140     MapCheckbuttonGadget(i);
10141   }
10142
10143   // draw selectbox gadgets
10144   for (i = ED_SELECTBOX_ID_LEVELSET_FIRST; i <= ED_SELECTBOX_ID_LEVELSET_LAST; i++)
10145     MapSelectboxGadget(i);
10146
10147   // draw text input gadgets
10148   for (i = ED_TEXTINPUT_ID_LEVELSET_FIRST; i <= ED_TEXTINPUT_ID_LEVELSET_LAST; i++)
10149     MapTextInputGadget(i);
10150
10151   // draw textbutton gadgets
10152   MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_LEVELSET);
10153
10154   // draw info text
10155   DrawLevelConfigLevelSet_DirectoryInfo();
10156 }
10157
10158 static void DrawLevelConfigEditor(void)
10159 {
10160   int i;
10161
10162   // draw counter gadgets
10163   for (i = ED_COUNTER_ID_EDITOR_FIRST; i <= ED_COUNTER_ID_EDITOR_LAST; i++)
10164     MapCounterButtons(i);
10165
10166   // draw checkbutton gadgets
10167   for (i = ED_CHECKBUTTON_ID_EDITOR_FIRST; i <= ED_CHECKBUTTON_ID_EDITOR_LAST; i++)
10168     MapCheckbuttonGadget(i);
10169
10170   // draw radiobutton gadgets
10171   for (i = ED_RADIOBUTTON_ID_EDITOR_FIRST; i <= ED_RADIOBUTTON_ID_EDITOR_LAST; i++)
10172     MapRadiobuttonGadget(i);
10173
10174   // draw drawing area
10175   MapDrawingArea(ED_DRAWING_ID_RANDOM_BACKGROUND);
10176
10177   // draw textbutton gadgets
10178   MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2);
10179 }
10180
10181 static void DrawLevelConfigEngine(void)
10182 {
10183   int i;
10184
10185   // draw counter gadgets
10186   if (level.bd_scheduling_type == GD_SCHEDULING_MILLISECONDS)
10187   {
10188     MapCounterButtons(ED_COUNTER_ID_BD_CYCLE_DELAY_MS);
10189     MapCounterButtons(ED_COUNTER_ID_BD_HATCHING_DELAY_CYCLES);
10190   }
10191   else
10192   {
10193     MapCounterButtons(ED_COUNTER_ID_BD_CYCLE_DELAY_C64);
10194     MapCounterButtons(ED_COUNTER_ID_BD_HATCHING_DELAY_SECONDS);
10195   }
10196
10197   // draw checkbutton gadgets
10198   for (i = ED_CHECKBUTTON_ID_ENGINE_FIRST; i <= ED_CHECKBUTTON_ID_ENGINE_LAST; i++)
10199     MapCheckbuttonGadget(i);
10200
10201   // draw selectbox gadgets
10202   for (i = ED_SELECTBOX_ID_ENGINE_FIRST; i <= ED_SELECTBOX_ID_ENGINE_LAST; i++)
10203     MapSelectboxGadget(i);
10204 }
10205
10206 static void DrawLevelConfigWindow(void)
10207 {
10208   char *text = "Global Settings";
10209   int font_nr = FONT_TITLE_1;
10210   struct MenuPosInfo *pos = &editor.settings.headline;
10211   int sx = SX + ALIGNED_XPOS(pos->x, getTextWidth(text, font_nr), pos->align);
10212   int sy = SY + pos->y;
10213
10214   stick_element_properties_window = FALSE;
10215
10216   SetAutomaticNumberOfGemsNeeded();
10217
10218   UnmapLevelEditorFieldGadgets();
10219
10220   SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
10221   ClearField();
10222
10223   DrawText(sx, sy, text, font_nr);
10224
10225   DrawLevelConfigTabulatorGadgets();
10226
10227   if (edit_mode_levelconfig == ED_MODE_LEVELCONFIG_LEVEL)
10228     DrawLevelConfigLevel();
10229   else if (edit_mode_levelconfig == ED_MODE_LEVELCONFIG_LEVELSET)
10230     DrawLevelConfigLevelSet();
10231   else if (edit_mode_levelconfig == ED_MODE_LEVELCONFIG_EDITOR)
10232     DrawLevelConfigEditor();
10233   else if (edit_mode_levelconfig == ED_MODE_LEVELCONFIG_ENGINE)
10234     DrawLevelConfigEngine();
10235 }
10236
10237 static void DrawCustomContentArea(void)
10238 {
10239   int id = ED_DRAWING_ID_CUSTOM_CONTENT;
10240   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
10241   int x1 = right_gadget_border[GADGET_ID_CUSTOM_DEADLINESS];
10242   int x2 = right_gadget_border[GADGET_ID_CUSTOM_EXPLOSION_TYPE];
10243   int x3 = right_gadget_border[GADGET_ID_CUSTOM_EXPLODE_IMPACT];
10244   int xoffset = ED_GADGET_SPACE_DISTANCE;
10245
10246   // add distance for potential left text (without drawing area border)
10247   x2 += getTextWidthForGadget(drawingarea_info[id].text_left);
10248
10249   ModifyGadget(gi, GDI_X, MAX(x1, MAX(x2, x3)) + xoffset, GDI_END);
10250
10251   MapDrawingArea(ED_DRAWING_ID_CUSTOM_CONTENT);
10252 }
10253
10254 static void DrawCustomChangeContentArea(void)
10255 {
10256   int id = ED_DRAWING_ID_CUSTOM_CHANGE_CONTENT;
10257   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
10258   int x1 = right_gadget_border[GADGET_ID_CHANGE_USE_CONTENT];
10259   int x2 = right_gadget_border[GADGET_ID_CHANGE_REPLACE_WHEN];
10260   int x3 = right_gadget_border[GADGET_ID_CHANGE_ONLY_COMPLETE];
10261   int xoffset = ED_GADGET_SPACE_DISTANCE;
10262
10263   ModifyGadget(gi, GDI_X, MAX(x1, MAX(x2, x3)) + xoffset, GDI_END);
10264
10265   MapDrawingArea(id);
10266 }
10267
10268 static void RemoveElementContentArea(int id, int font_height)
10269 {
10270   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10271
10272   DrawBackground(SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size,
10273                  SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size,
10274                  3 * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10275                  3 * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size +
10276                  ED_GADGET_TEXT_DISTANCE + font_height);
10277 }
10278
10279 static void DrawYamYamContentAreas(void)
10280 {
10281   int font_nr = FONT_TEXT_1;
10282   int font_height = getFontHeight(font_nr);
10283   int tilesize = ED_DRAWINGAREA_TILE_SIZE;
10284   int yoffset = (tilesize - font_height) / 2;
10285   int x = SX + ED_AREA_YAMYAM_CONTENT_X(3) + 4 * tilesize;
10286   int y = SY + ED_AREA_YAMYAM_CONTENT_Y(3) + yoffset;
10287   int i;
10288
10289   // display counter to choose number of element content areas
10290   MapCounterButtons(ED_COUNTER_ID_YAMYAM_CONTENT);
10291
10292   for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
10293   {
10294     int id = ED_DRAWING_ID_YAMYAM_CONTENT_0 + i;
10295
10296     if (i < level.num_yamyam_contents)
10297     {
10298       MapDrawingArea(id);
10299     }
10300     else
10301     {
10302       UnmapDrawingArea(id);
10303
10304       // delete content areas in case of reducing number of them
10305       RemoveElementContentArea(id, font_height);
10306     }
10307   }
10308
10309   DrawText(x, y + 0 * tilesize, "content", font_nr);
10310   DrawText(x, y + 1 * tilesize, "when",    font_nr);
10311   DrawText(x, y + 2 * tilesize, "smashed", font_nr);
10312 }
10313
10314 static void DrawMagicBallContentAreas(void)
10315 {
10316   int font_nr = FONT_TEXT_1;
10317   int font_height = getFontHeight(font_nr);
10318   int tilesize = ED_DRAWINGAREA_TILE_SIZE;
10319   int yoffset = (tilesize - font_height) / 2;
10320   int x = SX + ED_AREA_MAGIC_BALL_CONTENT_X(3) + 4 * tilesize;
10321   int y = SY + ED_AREA_MAGIC_BALL_CONTENT_Y(3) + yoffset;
10322   int i;
10323
10324   // display counter to choose number of element content areas
10325   MapCounterButtons(ED_COUNTER_ID_BALL_CONTENT);
10326
10327   for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
10328   {
10329     int id = ED_DRAWING_ID_MAGIC_BALL_CONTENT_0 + i;
10330
10331     if (i < level.num_ball_contents)
10332     {
10333       MapDrawingArea(id);
10334     }
10335     else
10336     {
10337       UnmapDrawingArea(id);
10338
10339       // delete content areas in case of reducing number of them
10340       RemoveElementContentArea(id, font_height);
10341     }
10342   }
10343
10344   DrawText(x, y + 0 * tilesize, "generated", font_nr);
10345   DrawText(x, y + 1 * tilesize, "when",      font_nr);
10346   DrawText(x, y + 2 * tilesize, "active",    font_nr);
10347 }
10348
10349 static void DrawAndroidElementArea(void)
10350 {
10351   int id = ED_DRAWING_ID_ANDROID_CONTENT;
10352   int num_elements = level.num_android_clone_elements;
10353   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10354   int sx = SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size;
10355   int sy = SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size;
10356   int xsize = MAX_ANDROID_ELEMENTS;
10357   int ysize = 1;
10358
10359   // display counter to choose number of element areas
10360   MapCounterButtons(ED_COUNTER_ID_ANDROID_CONTENT);
10361
10362   if (drawingarea_info[id].text_left != NULL)
10363     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
10364
10365   UnmapDrawingArea(id);
10366
10367   ModifyEditorDrawingArea(id, num_elements, 1);
10368
10369   // delete content areas in case of reducing number of them
10370   DrawBackground(sx, sy,
10371                  xsize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10372                  ysize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size);
10373
10374   MapDrawingArea(id);
10375 }
10376
10377 static void DrawGroupElementArea(void)
10378 {
10379   int id = ED_DRAWING_ID_GROUP_CONTENT;
10380   int num_elements = group_element_info.num_elements;
10381   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10382   int sx = SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size;
10383   int sy = SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size;
10384   int xsize = MAX_ELEMENTS_IN_GROUP;
10385   int ysize = 1;
10386
10387   if (drawingarea_info[id].text_left != NULL)
10388     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
10389
10390   UnmapDrawingArea(id);
10391
10392   ModifyEditorDrawingArea(id, num_elements, 1);
10393
10394   // delete content areas in case of reducing number of them
10395   DrawBackground(sx, sy,
10396                  xsize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10397                  ysize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size);
10398
10399   MapDrawingArea(id);
10400 }
10401
10402 static void DrawPlayerInitialInventoryArea(int element)
10403 {
10404   int id = ED_DRAWING_ID_INVENTORY_CONTENT;
10405   int player_nr = GET_PLAYER_NR(element);
10406   int num_elements = level.initial_inventory_size[player_nr];
10407   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10408   int sx = SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size;
10409   int sy = SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size;
10410   int xsize = MAX_INITIAL_INVENTORY_SIZE;
10411   int ysize = 1;
10412
10413   // determine horizontal position to the right of specified gadget
10414   if (drawingarea_info[id].gadget_id_align != GADGET_ID_NONE)
10415     sx = (right_gadget_border[drawingarea_info[id].gadget_id_align] +
10416           ED_DRAWINGAREA_TEXT_DISTANCE);
10417
10418   // determine horizontal offset for leading text
10419   if (drawingarea_info[id].text_left != NULL)
10420     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
10421
10422   UnmapDrawingArea(id);
10423
10424   ModifyEditorDrawingArea(id, num_elements, 1);
10425
10426   // delete content areas in case of reducing number of them
10427   DrawBackground(sx, sy,
10428                  xsize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10429                  ysize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size);
10430
10431   MapDrawingArea(id);
10432 }
10433
10434 static void DrawMMBallContentArea(void)
10435 {
10436   int id = ED_DRAWING_ID_MM_BALL_CONTENT;
10437   int num_elements = level.num_mm_ball_contents;
10438   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10439   int sx = SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size;
10440   int sy = SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size;
10441   int xsize = MAX_MM_BALL_CONTENTS;
10442   int ysize = 1;
10443
10444   if (drawingarea_info[id].text_left != NULL)
10445     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
10446
10447   UnmapDrawingArea(id);
10448
10449   ModifyEditorDrawingArea(id, num_elements, 1);
10450
10451   // delete content areas in case of reducing number of them
10452   DrawBackground(sx, sy,
10453                  xsize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10454                  ysize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size);
10455
10456   MapDrawingArea(id);
10457 }
10458
10459 static void DrawEnvelopeTextArea(int envelope_nr)
10460 {
10461   int id = ED_TEXTAREA_ID_ENVELOPE_INFO;
10462   struct GadgetInfo *gi = level_editor_gadget[textarea_info[id].gadget_id];
10463
10464   UnmapGadget(gi);
10465
10466   DrawBackground(gi->x, gi->y,
10467                  gi->textarea.crop_width, gi->textarea.crop_height);
10468
10469   if (envelope_nr != -1)
10470     textarea_info[id].value = level.envelope[envelope_nr].text;
10471
10472   ModifyGadget(gi, GDI_AREA_SIZE,
10473                *counterbutton_info[ED_COUNTER_ID_ENVELOPE_XSIZE].value,
10474                *counterbutton_info[ED_COUNTER_ID_ENVELOPE_YSIZE].value,
10475                GDI_END);
10476
10477   MapTextAreaGadget(ED_TEXTAREA_ID_ENVELOPE_INFO);
10478 }
10479
10480 static void DrawPropertiesInfo(void)
10481 {
10482   static struct
10483   {
10484     int value;
10485     char *text;
10486   }
10487   properties[] =
10488   {
10489     // configurable properties
10490
10491     { EP_WALKABLE_OVER,         "- player can walk over it"             },
10492     { EP_WALKABLE_INSIDE,       "- player can walk inside it"           },
10493     { EP_WALKABLE_UNDER,        "- player can walk under it"            },
10494     { EP_PASSABLE_OVER,         "- player can pass over it"             },
10495     { EP_PASSABLE_INSIDE,       "- player can pass through it"          },
10496     { EP_PASSABLE_UNDER,        "- player can pass under it"            },
10497     { EP_PROTECTED,             "- player is protected by it"           },
10498
10499     { EP_DIGGABLE,              "- can be digged away"                  },
10500     { EP_COLLECTIBLE,           "- can be collected"                    },
10501     { EP_DROPPABLE,             "- can be dropped after collecting"     },
10502     { EP_THROWABLE,             "- can be thrown after collecting"      },
10503     { EP_PUSHABLE,              "- can be pushed"                       },
10504
10505     { EP_CAN_FALL,              "- can fall"                            },
10506     { EP_CAN_MOVE,              "- can move"                            },
10507
10508     { EP_CAN_SMASH_PLAYER,      "- can smash player"                    },
10509 #if 0
10510     { EP_CAN_SMASH_ENEMIES,     "- can smash good and bad guys"         },
10511 #endif
10512     { EP_CAN_SMASH_EVERYTHING,  "- can smash everything smashable"      },
10513
10514     { EP_SLIPPERY,              "- slippery for falling elements"       },
10515     { EP_EM_SLIPPERY_WALL,      "- slippery for some gems (EM style)"   },
10516
10517     { EP_DONT_RUN_INTO,         "- deadly when running into"            },
10518     { EP_DONT_COLLIDE_WITH,     "- deadly when colliding with"          },
10519     { EP_DONT_GET_HIT_BY,       "- deadly when getting hit by"          },
10520     { EP_DONT_TOUCH,            "- deadly when touching"                },
10521
10522     { EP_INDESTRUCTIBLE,        "- indestructible"                      },
10523
10524     { EP_CAN_EXPLODE_BY_FIRE,   "- can explode by fire or explosions"   },
10525     { EP_CAN_EXPLODE_SMASHED,   "- can explode when smashed"            },
10526     { EP_CAN_EXPLODE_IMPACT,    "- can explode on impact"               },
10527
10528     { EP_CAN_CHANGE,            "- can change to other element"         },
10529
10530     // pre-defined properties
10531     { EP_CAN_PASS_MAGIC_WALL,   "- can pass magic walls"                },
10532     { EP_CAN_PASS_DC_MAGIC_WALL,"- can pass magic walls (DC style)"     },
10533     { EP_SWITCHABLE,            "- can be switched"                     },
10534 #if 0
10535     { EP_HAS_EDITOR_CONTENT,    "- can contain other elements"          },
10536 #endif
10537
10538     { -1,                       NULL                                    }
10539   };
10540   char *filename = getElementDescriptionFilename(properties_element);
10541   char *num_elements_text = "In this level: ";
10542   char *num_similar_text = "Similar tiles: ";
10543   char *properties_text = "Standard properties: ";
10544   char *description_text = "Description:";
10545   char *no_description_text = "No description available.";
10546   char *none_text = "None";
10547   float percentage;
10548   int num_elements_in_level = 0;
10549   int num_similar_in_level = 0;
10550   int num_hires_tiles_in_level = 0;
10551   int num_standard_properties = 0;
10552   int font1_nr = FONT_TEXT_1;
10553   int font2_nr = FONT_TEXT_2;
10554   int font1_width = getFontWidth(font1_nr);
10555   int font1_height = getFontHeight(font1_nr);
10556   int font2_height = getFontHeight(font2_nr);
10557   int line1_height = font1_height + ED_GADGET_LINE_DISTANCE;
10558   int font2_yoffset = (font1_height - font2_height) / 2;
10559   int num_elements_text_len = strlen(num_elements_text) * font1_width;
10560   int num_similar_text_len = strlen(num_similar_text) * font1_width;
10561   int properties_text_len = strlen(properties_text) * font1_width;
10562   int xpos = ED_ELEMENT_SETTINGS_X(0);
10563   int ypos = ED_ELEMENT_SETTINGS_Y(0) + ED_GADGET_SMALL_DISTANCE;
10564   int i, x, y;
10565
10566   if (setup.editor.show_element_token)
10567   {
10568     int font3_nr = FONT_TEXT_3;
10569     int font3_height = getFontHeight(font3_nr);
10570
10571     DrawTextF(xpos, ypos, font3_nr,
10572               "[%s]", element_info[properties_element].token_name);
10573
10574     ypos += 2 * font3_height;
10575   }
10576
10577   // ----- print number of elements / percentage of this element in level
10578
10579   for (y = 0; y < lev_fieldy; y++)
10580   {
10581     for (x = 0; x < lev_fieldx; x++)
10582     {
10583       if (Tile[x][y] == properties_element)
10584       {
10585         num_elements_in_level++;
10586       }
10587       else if (IS_MM_WALL(Tile[x][y]) &&
10588                map_mm_wall_element(Tile[x][y]) == properties_element)
10589       {
10590         num_hires_tiles_in_level += numHiresTiles(Tile[x][y]);
10591       }
10592     }
10593   }
10594
10595   percentage = num_elements_in_level * 100.0 / (lev_fieldx * lev_fieldy);
10596
10597   DrawTextS(xpos, ypos, font1_nr, num_elements_text);
10598
10599   if (num_hires_tiles_in_level > 0)
10600     DrawTextF(xpos + num_elements_text_len, ypos + font2_yoffset, font2_nr,
10601               "%d wall tiles", num_hires_tiles_in_level);
10602   else if (num_elements_in_level > 0)
10603     DrawTextF(xpos + num_elements_text_len, ypos + font2_yoffset, font2_nr,
10604               "%d (%.2f %%)", num_elements_in_level, percentage);
10605   else
10606     DrawTextF(xpos + num_elements_text_len, ypos + font2_yoffset, font2_nr,
10607               none_text);
10608
10609   // ----- print number of similar elements / percentage of them in level
10610
10611   for (y = 0; y < lev_fieldy; y++)
10612   {
10613     for (x = 0; x < lev_fieldx; x++)
10614     {
10615       if (strEqual(element_info[Tile[x][y]].class_name,
10616                    element_info[properties_element].class_name))
10617       {
10618         num_similar_in_level++;
10619       }
10620     }
10621   }
10622
10623   if (num_similar_in_level != num_elements_in_level)
10624   {
10625     ypos += 1 * MAX(font1_height, font2_height);
10626
10627     percentage = num_similar_in_level * 100.0 / (lev_fieldx * lev_fieldy);
10628
10629     DrawTextS(xpos, ypos, font1_nr, num_similar_text);
10630
10631     if (num_similar_in_level > 0)
10632       DrawTextF(xpos + num_similar_text_len, ypos + font2_yoffset, font2_nr,
10633                 "%d (%.2f %%)", num_similar_in_level, percentage);
10634     else
10635       DrawTextF(xpos + num_similar_text_len, ypos + font2_yoffset, font2_nr,
10636                 none_text);
10637   }
10638
10639   ypos += 2 * MAX(font1_height, font2_height);
10640
10641   // ----- print standard properties of this element
10642
10643   DrawTextS(xpos, ypos, font1_nr, properties_text);
10644
10645   ypos += line1_height;
10646
10647   for (i = 0; properties[i].value != -1; i++)
10648   {
10649     if (!HAS_PROPERTY(properties_element, properties[i].value))
10650       continue;
10651
10652     DrawTextS(xpos, ypos, font2_nr, properties[i].text);
10653
10654     ypos += font2_height;
10655
10656     num_standard_properties++;
10657   }
10658
10659   if (num_standard_properties == 0)
10660   {
10661     DrawTextS(xpos + properties_text_len, ypos - line1_height + font2_yoffset,
10662               font2_nr, none_text);
10663
10664     ypos -= (line1_height - font1_height);
10665   }
10666
10667   ypos += MAX(font1_height, font2_height);
10668
10669   // ----- print special description of this element
10670
10671   PrintInfoText(description_text, font1_nr, xpos, ypos);
10672
10673   ypos += line1_height;
10674
10675   if (PrintElementDescriptionFromFile(filename, font2_nr, xpos, ypos) == 0)
10676     PrintInfoText(no_description_text, font1_nr, xpos, ypos - line1_height);
10677 }
10678
10679 #define TEXT_COLLECTING         "Score for collecting"
10680 #define TEXT_COLLECTING_EXTRA   "Score for extra diamonds"
10681 #define TEXT_SMASHING           "Score for smashing"
10682 #define TEXT_SLURPING           "Score for slurping robot"
10683 #define TEXT_CRACKING           "Score for cracking"
10684 #define TEXT_AMOEBA_SPEED       "Speed of amoeba growth"
10685 #define TEXT_DURATION           "Duration when activated"
10686 #define TEXT_DELAY_ON           "Delay before activating"
10687 #define TEXT_DELAY_OFF          "Delay before deactivating"
10688 #define TEXT_DELAY_CHANGING     "Delay before changing"
10689 #define TEXT_DELAY_EXPLODING    "Delay before exploding"
10690 #define TEXT_DELAY_MOVING       "Delay before moving"
10691 #define TEXT_BALL_DELAY         "Element generation delay"
10692 #define TEXT_MOVE_SPEED         "Speed of android moving"
10693 #define TEXT_CLONE_SPEED        "Speed of android cloning"
10694 #define TEXT_GAME_OF_LIFE_1     "Min neighbours to survive"
10695 #define TEXT_GAME_OF_LIFE_2     "Max neighbours to survive"
10696 #define TEXT_GAME_OF_LIFE_3     "Min neighbours to create"
10697 #define TEXT_GAME_OF_LIFE_4     "Max neighbours to create"
10698 #define TEXT_TIME_BONUS         "Extra time to solve level"
10699
10700 static struct
10701 {
10702   int element;
10703   int *value;
10704   char *text;
10705 } elements_with_counter[] =
10706 {
10707   { EL_EMERALD,                 &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10708   { EL_BD_DIAMOND,              &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10709   { EL_BD_DIAMOND,              &level.score[SC_DIAMOND_EXTRA], TEXT_COLLECTING_EXTRA   },
10710   { EL_EMERALD_YELLOW,          &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10711   { EL_EMERALD_RED,             &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10712   { EL_EMERALD_PURPLE,          &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10713   { EL_SP_INFOTRON,             &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10714   { EL_DIAMOND,                 &level.score[SC_DIAMOND],       TEXT_COLLECTING         },
10715   { EL_CRYSTAL,                 &level.score[SC_CRYSTAL],       TEXT_COLLECTING         },
10716   { EL_PEARL,                   &level.score[SC_PEARL],         TEXT_COLLECTING         },
10717   { EL_BUG,                     &level.score[SC_BUG],           TEXT_SMASHING           },
10718   { EL_BUG_RIGHT,               &level.score[SC_BUG],           TEXT_SMASHING           },
10719   { EL_BUG_UP,                  &level.score[SC_BUG],           TEXT_SMASHING           },
10720   { EL_BUG_LEFT,                &level.score[SC_BUG],           TEXT_SMASHING           },
10721   { EL_BUG_DOWN,                &level.score[SC_BUG],           TEXT_SMASHING           },
10722   { EL_BD_BUTTERFLY,            &level.score[SC_BUG],           TEXT_SMASHING           },
10723   { EL_BD_BUTTERFLY_RIGHT,      &level.score[SC_BUG],           TEXT_SMASHING           },
10724   { EL_BD_BUTTERFLY_UP,         &level.score[SC_BUG],           TEXT_SMASHING           },
10725   { EL_BD_BUTTERFLY_LEFT,       &level.score[SC_BUG],           TEXT_SMASHING           },
10726   { EL_BD_BUTTERFLY_DOWN,       &level.score[SC_BUG],           TEXT_SMASHING           },
10727   { EL_SP_ELECTRON,             &level.score[SC_BUG],           TEXT_SMASHING           },
10728   { EL_SPACESHIP,               &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10729   { EL_SPACESHIP_RIGHT,         &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10730   { EL_SPACESHIP_UP,            &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10731   { EL_SPACESHIP_LEFT,          &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10732   { EL_SPACESHIP_DOWN,          &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10733   { EL_BD_FIREFLY,              &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10734   { EL_BD_FIREFLY_RIGHT,        &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10735   { EL_BD_FIREFLY_UP,           &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10736   { EL_BD_FIREFLY_LEFT,         &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10737   { EL_BD_FIREFLY_DOWN,         &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10738   { EL_SP_SNIKSNAK,             &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10739   { EL_YAMYAM,                  &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10740   { EL_YAMYAM_LEFT,             &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10741   { EL_YAMYAM_RIGHT,            &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10742   { EL_YAMYAM_UP,               &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10743   { EL_YAMYAM_DOWN,             &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10744   { EL_DARK_YAMYAM,             &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10745   { EL_ROBOT,                   &level.score[SC_ROBOT],         TEXT_SMASHING           },
10746   { EL_PACMAN,                  &level.score[SC_PACMAN],        TEXT_SMASHING           },
10747   { EL_PACMAN_RIGHT,            &level.score[SC_PACMAN],        TEXT_SMASHING           },
10748   { EL_PACMAN_UP,               &level.score[SC_PACMAN],        TEXT_SMASHING           },
10749   { EL_PACMAN_LEFT,             &level.score[SC_PACMAN],        TEXT_SMASHING           },
10750   { EL_PACMAN_DOWN,             &level.score[SC_PACMAN],        TEXT_SMASHING           },
10751   { EL_NUT,                     &level.score[SC_NUT],           TEXT_CRACKING           },
10752   { EL_DYNAMITE,                &level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10753   { EL_EM_DYNAMITE,             &level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10754   { EL_DYNABOMB_INCREASE_NUMBER,&level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10755   { EL_DYNABOMB_INCREASE_SIZE,  &level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10756   { EL_DYNABOMB_INCREASE_POWER, &level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10757   { EL_SHIELD_NORMAL,           &level.score[SC_SHIELD],        TEXT_COLLECTING         },
10758   { EL_SHIELD_DEADLY,           &level.score[SC_SHIELD],        TEXT_COLLECTING         },
10759   { EL_EXTRA_TIME,              &level.extra_time_score,        TEXT_COLLECTING         },
10760   { EL_KEY_1,                   &level.score[SC_KEY],           TEXT_COLLECTING         },
10761   { EL_KEY_2,                   &level.score[SC_KEY],           TEXT_COLLECTING         },
10762   { EL_KEY_3,                   &level.score[SC_KEY],           TEXT_COLLECTING         },
10763   { EL_KEY_4,                   &level.score[SC_KEY],           TEXT_COLLECTING         },
10764   { EL_EM_KEY_1,                &level.score[SC_KEY],           TEXT_COLLECTING         },
10765   { EL_EM_KEY_2,                &level.score[SC_KEY],           TEXT_COLLECTING         },
10766   { EL_EM_KEY_3,                &level.score[SC_KEY],           TEXT_COLLECTING         },
10767   { EL_EM_KEY_4,                &level.score[SC_KEY],           TEXT_COLLECTING         },
10768   { EL_EMC_KEY_5,               &level.score[SC_KEY],           TEXT_COLLECTING         },
10769   { EL_EMC_KEY_6,               &level.score[SC_KEY],           TEXT_COLLECTING         },
10770   { EL_EMC_KEY_7,               &level.score[SC_KEY],           TEXT_COLLECTING         },
10771   { EL_EMC_KEY_8,               &level.score[SC_KEY],           TEXT_COLLECTING         },
10772   { EL_DC_KEY_WHITE,            &level.score[SC_KEY],           TEXT_COLLECTING         },
10773   { EL_MM_KETTLE,               &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10774   { EL_DF_CELL,                 &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10775   { EL_MM_KEY,                  &level.score[SC_KEY],           TEXT_COLLECTING         },
10776   { EL_MM_LIGHTBALL,            &level.score[SC_ELEM_BONUS],    TEXT_COLLECTING         },
10777   { EL_MM_PACMAN,               &level.score[SC_PACMAN],        TEXT_SMASHING           },
10778   { EL_MM_PACMAN_RIGHT,         &level.score[SC_PACMAN],        TEXT_SMASHING           },
10779   { EL_MM_PACMAN_UP,            &level.score[SC_PACMAN],        TEXT_SMASHING           },
10780   { EL_MM_PACMAN_LEFT,          &level.score[SC_PACMAN],        TEXT_SMASHING           },
10781   { EL_MM_PACMAN_DOWN,          &level.score[SC_PACMAN],        TEXT_SMASHING           },
10782   { EL_AMOEBA_WET,              &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10783   { EL_AMOEBA_DRY,              &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10784   { EL_AMOEBA_FULL,             &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10785   { EL_BD_AMOEBA,               &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10786   { EL_EMC_DRIPPER,             &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10787   { EL_MAGIC_WALL,              &level.time_magic_wall,         TEXT_DURATION           },
10788   { EL_BD_MAGIC_WALL,           &level.time_magic_wall,         TEXT_DURATION           },
10789   { EL_DC_MAGIC_WALL,           &level.time_magic_wall,         TEXT_DURATION           },
10790   { EL_ROBOT_WHEEL,             &level.time_wheel,              TEXT_DURATION           },
10791   { EL_TIMEGATE_SWITCH,         &level.time_timegate,           TEXT_DURATION           },
10792   { EL_DC_TIMEGATE_SWITCH,      &level.time_timegate,           TEXT_DURATION           },
10793   { EL_LIGHT_SWITCH,            &level.time_light,              TEXT_DURATION           },
10794   { EL_LIGHT_SWITCH_ACTIVE,     &level.time_light,              TEXT_DURATION           },
10795   { EL_SHIELD_NORMAL,           &level.shield_normal_time,      TEXT_DURATION           },
10796   { EL_SHIELD_DEADLY,           &level.shield_deadly_time,      TEXT_DURATION           },
10797   { EL_EXTRA_TIME,              &level.extra_time,              TEXT_TIME_BONUS         },
10798   { EL_TIME_ORB_FULL,           &level.time_orb_time,           TEXT_TIME_BONUS         },
10799   { EL_GAME_OF_LIFE,            &level.game_of_life[0],         TEXT_GAME_OF_LIFE_1     },
10800   { EL_GAME_OF_LIFE,            &level.game_of_life[1],         TEXT_GAME_OF_LIFE_2     },
10801   { EL_GAME_OF_LIFE,            &level.game_of_life[2],         TEXT_GAME_OF_LIFE_3     },
10802   { EL_GAME_OF_LIFE,            &level.game_of_life[3],         TEXT_GAME_OF_LIFE_4     },
10803   { EL_BIOMAZE,                 &level.biomaze[0],              TEXT_GAME_OF_LIFE_1     },
10804   { EL_BIOMAZE,                 &level.biomaze[1],              TEXT_GAME_OF_LIFE_2     },
10805   { EL_BIOMAZE,                 &level.biomaze[2],              TEXT_GAME_OF_LIFE_3     },
10806   { EL_BIOMAZE,                 &level.biomaze[3],              TEXT_GAME_OF_LIFE_4     },
10807   { EL_EMC_ANDROID,             &level.android_move_time,       TEXT_MOVE_SPEED         },
10808   { EL_EMC_ANDROID,             &level.android_clone_time,      TEXT_CLONE_SPEED        },
10809   { EL_EMC_MAGIC_BALL,          &level.ball_time,               TEXT_BALL_DELAY         },
10810   { EL_EMC_LENSES,              &level.lenses_score,            TEXT_COLLECTING         },
10811   { EL_EMC_MAGNIFIER,           &level.magnify_score,           TEXT_COLLECTING         },
10812   { EL_SPRING,                  &level.slurp_score,             TEXT_SLURPING           },
10813   { EL_SPRING_LEFT,             &level.slurp_score,             TEXT_SLURPING           },
10814   { EL_SPRING_RIGHT,            &level.slurp_score,             TEXT_SLURPING           },
10815   { EL_EMC_LENSES,              &level.lenses_time,             TEXT_DURATION           },
10816   { EL_EMC_MAGNIFIER,           &level.magnify_time,            TEXT_DURATION           },
10817   { EL_MM_FUSE_ACTIVE,          &level.mm_time_fuse,            TEXT_DELAY_OFF          },
10818   { EL_MM_BOMB,                 &level.mm_time_bomb,            TEXT_DELAY_EXPLODING    },
10819   { EL_MM_GRAY_BALL,            &level.mm_time_ball,            TEXT_DELAY_CHANGING     },
10820   { EL_MM_STEEL_BLOCK,          &level.mm_time_block,           TEXT_DELAY_MOVING       },
10821   { EL_MM_WOODEN_BLOCK,         &level.mm_time_block,           TEXT_DELAY_MOVING       },
10822
10823   { -1,                         NULL,                           NULL                    }
10824 };
10825
10826 static boolean checkPropertiesConfig(int element)
10827 {
10828   int i;
10829
10830   // special case: empty space customization only available in R'n'D game engine
10831   if (element == EL_EMPTY_SPACE && level.game_engine_type != GAME_ENGINE_TYPE_RND)
10832     return FALSE;
10833
10834   if (IS_GEM(element) ||
10835       IS_CUSTOM_ELEMENT(element) ||
10836       IS_GROUP_ELEMENT(element) ||
10837       IS_EMPTY_ELEMENT(element) ||
10838       IS_BALLOON_ELEMENT(element) ||
10839       IS_ENVELOPE(element) ||
10840       IS_MM_ENVELOPE(element) ||
10841       IS_MM_MCDUFFIN(element) ||
10842       IS_DF_LASER(element) ||
10843       IS_PLAYER_ELEMENT(element) ||
10844       IS_BD_PLAYER_ELEMENT(element) ||
10845       IS_SOKOBAN_OBJECT_OR_FIELD(element) ||
10846       HAS_EDITOR_CONTENT(element) ||
10847       CAN_GROW(element) ||
10848       COULD_MOVE_INTO_ACID(element) ||
10849       MAYBE_DONT_COLLIDE_WITH(element))
10850     return TRUE;
10851   else
10852     for (i = 0; elements_with_counter[i].element != -1; i++)
10853       if (elements_with_counter[i].element == element)
10854         return TRUE;
10855
10856   return FALSE;
10857 }
10858
10859 static void SetAutomaticNumberOfGemsNeeded(void)
10860 {
10861   int x, y;
10862
10863   if (!level.auto_count_gems)
10864     return;
10865
10866   level.gems_needed = 0;
10867
10868   for (x = 0; x < lev_fieldx; x++)
10869   {
10870     for (y = 0; y < lev_fieldy; y++)
10871     {
10872       int element = Tile[x][y];
10873
10874       switch (element)
10875       {
10876         case EL_EMERALD:
10877         case EL_EMERALD_YELLOW:
10878         case EL_EMERALD_RED:
10879         case EL_EMERALD_PURPLE:
10880         case EL_BD_DIAMOND:
10881         case EL_WALL_EMERALD:
10882         case EL_WALL_EMERALD_YELLOW:
10883         case EL_WALL_EMERALD_RED:
10884         case EL_WALL_EMERALD_PURPLE:
10885         case EL_WALL_BD_DIAMOND:
10886         case EL_NUT:
10887         case EL_SP_INFOTRON:
10888         case EL_MM_KETTLE:
10889         case EL_DF_CELL:
10890           level.gems_needed++;
10891           break;
10892
10893         case EL_DIAMOND:
10894         case EL_WALL_DIAMOND:
10895           level.gems_needed += 3;
10896           break;
10897
10898         case EL_PEARL:
10899         case EL_WALL_PEARL:
10900           level.gems_needed += 5;
10901           break;
10902
10903         case EL_CRYSTAL:
10904         case EL_WALL_CRYSTAL:
10905           level.gems_needed += 8;
10906           break;
10907
10908         default:
10909           break;
10910       }
10911     }
10912   }
10913
10914   ModifyEditorCounterValue(ED_COUNTER_ID_LEVEL_GEMSLIMIT, level.gems_needed);
10915 }
10916
10917 static void DrawPropertiesConfig(void)
10918 {
10919   boolean draw_footer_line = FALSE;
10920   int max_num_element_counters = 4;
10921   int num_element_counters = 0;
10922   int i;
10923
10924   if (!checkPropertiesConfig(properties_element))
10925   {
10926     int xpos = ED_ELEMENT_SETTINGS_X(0);
10927     int ypos = ED_ELEMENT_SETTINGS_Y(0) + ED_GADGET_SMALL_DISTANCE;
10928
10929     PrintInfoText("No configuration options available.",
10930                   FONT_TEXT_1, xpos, ypos);
10931
10932     return;
10933   }
10934
10935   // check if there are elements where a value can be chosen for
10936   for (i = 0; elements_with_counter[i].element != -1; i++)
10937   {
10938     if (elements_with_counter[i].element == properties_element)
10939     {
10940       int counter_id = ED_COUNTER_ID_ELEMENT_VALUE1 + num_element_counters;
10941
10942       counterbutton_info[counter_id].y =
10943         ED_ELEMENT_SETTINGS_YPOS(
10944                 (HAS_EDITOR_CONTENT(properties_element)      ? 1 : 0) +
10945                 (CAN_GROW(properties_element)                ? 1 : 0) +
10946                 (COULD_MOVE_INTO_ACID(properties_element)    ? 1 : 0) +
10947                 (MAYBE_DONT_COLLIDE_WITH(properties_element) ? 1 : 0) +
10948                 (properties_element == EL_EMC_MAGIC_BALL     ? 2 : 0) +
10949                 num_element_counters);
10950
10951       counterbutton_info[counter_id].value = elements_with_counter[i].value;
10952       counterbutton_info[counter_id].text_right = elements_with_counter[i].text;
10953
10954       if (properties_element == EL_GAME_OF_LIFE ||
10955           properties_element == EL_BIOMAZE)
10956       {
10957         counterbutton_info[counter_id].min_value = 0;   // min neighbours
10958         counterbutton_info[counter_id].max_value = 8;   // max neighbours
10959       }
10960       else
10961       {
10962         // !!! CHANGE THIS FOR CERTAIN ELEMENTS !!!
10963         counterbutton_info[counter_id].min_value = MIN_SCORE;
10964         counterbutton_info[counter_id].max_value = MAX_SCORE;
10965       }
10966
10967       MapCounterButtons(counter_id);
10968
10969       num_element_counters++;
10970       if (num_element_counters >= max_num_element_counters)
10971         break;
10972     }
10973   }
10974
10975   if (HAS_EDITOR_CONTENT(properties_element))
10976   {
10977     // draw stickybutton gadget
10978     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
10979
10980     if (IS_AMOEBOID(properties_element))
10981       MapDrawingArea(ED_DRAWING_ID_AMOEBA_CONTENT);
10982     else if (properties_element == EL_YAMYAM ||
10983              properties_element == EL_YAMYAM_LEFT ||
10984              properties_element == EL_YAMYAM_RIGHT ||
10985              properties_element == EL_YAMYAM_UP ||
10986              properties_element == EL_YAMYAM_DOWN)
10987       DrawYamYamContentAreas();
10988     else if (properties_element == EL_EMC_MAGIC_BALL)
10989     {
10990       DrawMagicBallContentAreas();
10991
10992       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_RANDOM_BALL_CONTENT);
10993       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_INITIAL_BALL_ACTIVE);
10994     }
10995     else if (properties_element == EL_EMC_ANDROID)
10996       DrawAndroidElementArea();
10997     else if (properties_element == EL_MM_GRAY_BALL)
10998     {
10999       MapCounterButtons(ED_COUNTER_ID_MM_BALL_CONTENT);
11000       MapSelectboxGadget(ED_SELECTBOX_ID_MM_BALL_CHOICE_MODE);
11001       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_ROTATE_MM_BALL_CONTENT);
11002       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_EXPLODE_MM_BALL);
11003
11004       DrawMMBallContentArea();
11005     }
11006   }
11007
11008   if (IS_PLAYER_ELEMENT(properties_element))
11009   {
11010     int player_nr = GET_PLAYER_NR(properties_element);
11011
11012     // these properties can be set for every player individually
11013
11014     if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_1)
11015     {
11016       drawingarea_info[ED_DRAWING_ID_START_ELEMENT].value =
11017         &level.start_element[player_nr];
11018       drawingarea_info[ED_DRAWING_ID_ARTWORK_ELEMENT].value =
11019         &level.artwork_element[player_nr];
11020       drawingarea_info[ED_DRAWING_ID_EXPLOSION_ELEMENT].value =
11021         &level.explosion_element[player_nr];
11022
11023       checkbutton_info[ED_CHECKBUTTON_ID_USE_START_ELEMENT].value =
11024         &level.use_start_element[player_nr];
11025       checkbutton_info[ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT].value =
11026         &level.use_artwork_element[player_nr];
11027       checkbutton_info[ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT].value =
11028         &level.use_explosion_element[player_nr];
11029       checkbutton_info[ED_CHECKBUTTON_ID_INITIAL_GRAVITY].value =
11030         &level.initial_player_gravity[player_nr];
11031
11032       selectbox_info[ED_SELECTBOX_ID_PLAYER_SPEED].value =
11033         &level.initial_player_stepsize[player_nr];
11034
11035       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID);
11036       MapCheckbuttonGadget(properties_element == EL_SP_MURPHY ?
11037                            ED_CHECKBUTTON_ID_SP_BLOCK_LAST_FIELD :
11038                            ED_CHECKBUTTON_ID_BLOCK_LAST_FIELD);
11039       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BLOCK_SNAP_FIELD);
11040       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CONTINUOUS_SNAPPING);
11041       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_INSTANT_RELOCATION);
11042       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SHIFTED_RELOCATION);
11043       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_LAZY_RELOCATION);
11044       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_START_ELEMENT);
11045       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT);
11046       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT);
11047       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_INITIAL_GRAVITY);
11048       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CAN_PASS_TO_WALKABLE);
11049       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SOLVED_BY_ONE_PLAYER);
11050
11051       MapDrawingArea(ED_DRAWING_ID_START_ELEMENT);
11052       MapDrawingArea(ED_DRAWING_ID_ARTWORK_ELEMENT);
11053       MapDrawingArea(ED_DRAWING_ID_EXPLOSION_ELEMENT);
11054
11055       MapSelectboxGadget(ED_SELECTBOX_ID_PLAYER_SPEED);
11056     }
11057     else if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_2)
11058     {
11059       drawingarea_info[ED_DRAWING_ID_INVENTORY_CONTENT].value =
11060         &level.initial_inventory_content[player_nr][0];
11061
11062       counterbutton_info[ED_COUNTER_ID_INVENTORY_SIZE].value =
11063         &level.initial_inventory_size[player_nr];
11064
11065       checkbutton_info[ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY].value =
11066         &level.use_initial_inventory[player_nr];
11067
11068       // draw checkbutton gadgets
11069       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY);
11070       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_FINISH_DIG_COLLECT);
11071       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_KEEP_WALKABLE_CE);
11072
11073       // draw counter gadgets
11074       MapCounterButtons(ED_COUNTER_ID_INVENTORY_SIZE);
11075
11076       // draw drawing area gadgets
11077       DrawPlayerInitialInventoryArea(properties_element);
11078     }
11079   }
11080
11081   if (IS_BD_PLAYER_ELEMENT(properties_element))
11082   {
11083     // draw checkbutton gadgets
11084     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BD_DIAGONAL_MOVEMENTS);
11085     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BD_TOPMOST_PLAYER_ACTIVE);
11086     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BD_PUSH_MEGA_ROCK_WITH_SWEET);
11087
11088     // draw counter gadgets
11089     MapCounterButtons(ED_COUNTER_ID_BD_PUSHING_PROB);
11090     MapCounterButtons(ED_COUNTER_ID_BD_PUSHING_PROB_WITH_SWEET);
11091   }
11092
11093   // special case: slippery walls option for gems only available in R'n'D game engine
11094   if (IS_GEM(properties_element) && level.game_engine_type == GAME_ENGINE_TYPE_RND)
11095   {
11096     checkbutton_info[ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS].y =
11097       ED_ELEMENT_SETTINGS_XPOS(properties_element == EL_BD_DIAMOND ? 2 : 1);
11098
11099     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS);
11100   }
11101
11102   if (properties_element == EL_EM_DYNAMITE)
11103     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_EM_EXPLODES_BY_FIRE);
11104
11105   if (COULD_MOVE_INTO_ACID(properties_element) &&
11106       !IS_PLAYER_ELEMENT(properties_element) &&
11107       (!IS_CUSTOM_ELEMENT(properties_element) ||
11108        edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_2))
11109   {
11110     // set position for checkbutton for "can move into acid"
11111     checkbutton_info[ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID].x =
11112       ED_ELEMENT_SETTINGS_XPOS(IS_CUSTOM_ELEMENT(properties_element) ? 1 : 0);
11113     checkbutton_info[ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID].y =
11114       ED_ELEMENT_SETTINGS_YPOS(IS_CUSTOM_ELEMENT(properties_element) ? 7 :
11115                                IS_BALLOON_ELEMENT(properties_element) ||
11116                                HAS_EDITOR_CONTENT(properties_element) ? 1 : 0);
11117
11118     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID);
11119   }
11120
11121   if (MAYBE_DONT_COLLIDE_WITH(properties_element))
11122     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH);
11123
11124   if (properties_element == EL_SPRING ||
11125       properties_element == EL_SPRING_LEFT ||
11126       properties_element == EL_SPRING_RIGHT)
11127     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_SPRING_BUG);
11128
11129   if (properties_element == EL_TIME_ORB_FULL)
11130     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_TIME_ORB_BUG);
11131
11132   if (properties_element == EL_GAME_OF_LIFE ||
11133       properties_element == EL_BIOMAZE)
11134     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_LIFE_BUGS);
11135
11136   if (CAN_GROW(properties_element))
11137   {
11138     checkbutton_info[ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE].y =
11139       ED_ELEMENT_SETTINGS_YPOS(HAS_EDITOR_CONTENT(properties_element) ? 1 : 0);
11140
11141     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE);
11142   }
11143
11144   if (properties_element == EL_SOKOBAN_FIELD_EMPTY)
11145     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SB_FIELDS_NEEDED);
11146
11147   if (properties_element == EL_SOKOBAN_OBJECT)
11148     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SB_OBJECTS_NEEDED);
11149
11150   if (properties_element == EL_SOKOBAN_OBJECT ||
11151       properties_element == EL_SOKOBAN_FIELD_EMPTY ||
11152       properties_element == EL_SOKOBAN_FIELD_FULL)
11153   {
11154     checkbutton_info[ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN].y =
11155       ED_ELEMENT_SETTINGS_XPOS(properties_element == EL_SOKOBAN_FIELD_FULL ?
11156                                0 : 1);
11157
11158     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN);
11159   }
11160
11161   if (IS_BALLOON_ELEMENT(properties_element))
11162     MapSelectboxGadget(ED_SELECTBOX_ID_WIND_DIRECTION);
11163
11164   if (IS_ENVELOPE(properties_element) ||
11165       IS_MM_ENVELOPE(properties_element))
11166   {
11167     int counter1_id = ED_COUNTER_ID_ENVELOPE_XSIZE;
11168     int counter2_id = ED_COUNTER_ID_ENVELOPE_YSIZE;
11169     int button1_id = ED_CHECKBUTTON_ID_ENVELOPE_AUTOWRAP;
11170     int button2_id = ED_CHECKBUTTON_ID_ENVELOPE_CENTERED;
11171     int envelope_nr = ENVELOPE_NR(properties_element);
11172
11173     counterbutton_info[counter1_id].value = &level.envelope[envelope_nr].xsize;
11174     counterbutton_info[counter2_id].value = &level.envelope[envelope_nr].ysize;
11175
11176     checkbutton_info[button1_id].value = &level.envelope[envelope_nr].autowrap;
11177     checkbutton_info[button2_id].value = &level.envelope[envelope_nr].centered;
11178
11179     // display counter to choose size of envelope text area
11180     MapCounterButtons(counter1_id);
11181     MapCounterButtons(counter2_id);
11182
11183     // display checkbuttons to choose auto-wrap and alignment properties
11184     MapCheckbuttonGadget(button1_id);
11185     MapCheckbuttonGadget(button2_id);
11186
11187     DrawEnvelopeTextArea(envelope_nr);
11188   }
11189
11190   if (IS_MM_MCDUFFIN(properties_element))
11191   {
11192     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_MM_LASER_RED);
11193     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_MM_LASER_GREEN);
11194     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_MM_LASER_BLUE);
11195   }
11196
11197   if (IS_DF_LASER(properties_element))
11198   {
11199     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DF_LASER_RED);
11200     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DF_LASER_GREEN);
11201     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DF_LASER_BLUE);
11202   }
11203
11204   if (IS_CUSTOM_ELEMENT(properties_element))
11205   {
11206     // draw stickybutton gadget
11207     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11208
11209     if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_1)
11210     {
11211       // draw checkbutton gadgets
11212       for (i =  ED_CHECKBUTTON_ID_CUSTOM1_FIRST;
11213            i <= ED_CHECKBUTTON_ID_CUSTOM1_LAST; i++)
11214         MapCheckbuttonGadget(i);
11215
11216       // draw counter gadgets
11217       for (i =  ED_COUNTER_ID_CUSTOM1_FIRST;
11218            i <= ED_COUNTER_ID_CUSTOM1_LAST; i++)
11219         MapCounterButtons(i);
11220
11221       // draw selectbox gadgets
11222       for (i =  ED_SELECTBOX_ID_CUSTOM1_FIRST;
11223            i <= ED_SELECTBOX_ID_CUSTOM1_LAST; i++)
11224         MapSelectboxGadget(i);
11225
11226       // draw textbutton gadgets
11227       MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1);
11228
11229       // draw text input gadgets
11230       MapTextInputGadget(ED_TEXTINPUT_ID_ELEMENT_NAME);
11231
11232       // draw drawing area gadgets
11233       MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
11234
11235       draw_footer_line = TRUE;
11236     }
11237     else if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_2)
11238     {
11239       // draw checkbutton gadgets
11240       for (i =  ED_CHECKBUTTON_ID_CUSTOM2_FIRST;
11241            i <= ED_CHECKBUTTON_ID_CUSTOM2_LAST; i++)
11242         MapCheckbuttonGadget(i);
11243
11244       // draw counter gadgets
11245       for (i =  ED_COUNTER_ID_CUSTOM2_FIRST;
11246            i <= ED_COUNTER_ID_CUSTOM2_LAST; i++)
11247         MapCounterButtons(i);
11248
11249       // draw selectbox gadgets
11250       for (i =  ED_SELECTBOX_ID_CUSTOM2_FIRST;
11251            i <= ED_SELECTBOX_ID_CUSTOM2_LAST; i++)
11252         MapSelectboxGadget(i);
11253
11254       // draw drawing area gadgets
11255       MapDrawingArea(ED_DRAWING_ID_CUSTOM_MOVE_ENTER);
11256       MapDrawingArea(ED_DRAWING_ID_CUSTOM_MOVE_LEAVE);
11257       DrawCustomContentArea();
11258     }
11259   }
11260   else if (IS_GROUP_ELEMENT(properties_element))
11261   {
11262     // draw stickybutton gadget
11263     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11264
11265     // draw checkbutton gadgets
11266     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC);
11267     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1);
11268
11269     // draw counter gadgets
11270     MapCounterButtons(ED_COUNTER_ID_GROUP_CONTENT);
11271
11272     // draw selectbox gadgets
11273     MapSelectboxGadget(ED_SELECTBOX_ID_GROUP_CHOICE_MODE);
11274
11275     // draw textbutton gadgets
11276     MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1);
11277
11278     // draw drawing area gadgets
11279     DrawGroupElementArea();
11280
11281     // draw text input gadgets
11282     MapTextInputGadget(ED_TEXTINPUT_ID_ELEMENT_NAME);
11283
11284     // draw drawing area gadgets
11285     MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
11286
11287     draw_footer_line = TRUE;
11288   }
11289   else if (IS_EMPTY_ELEMENT(properties_element))
11290   {
11291     // draw stickybutton gadget
11292     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11293
11294     // draw checkbutton gadgets
11295     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC);
11296     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1);
11297
11298     // draw textbutton gadgets
11299     MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1);
11300
11301     // draw drawing area gadgets
11302     MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
11303
11304     draw_footer_line = TRUE;
11305   }
11306
11307   // draw little footer border line above CE/GE use/save template gadgets
11308   if (draw_footer_line)
11309   {
11310     struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_PROPERTIES_INFO];
11311     struct GadgetDesign *gd = &gd_gi1->alt_design[GD_BUTTON_UNPRESSED];
11312     int gd_x = gd->x + gd_gi1->border.width / 2;
11313     int gd_y = gd->y + gd_gi1->height - 1;
11314     Pixel tab_color = GetPixel(gd->bitmap, gd_x, gd_y);
11315
11316     if (tab_color != BLACK_PIXEL)               // black => transparent
11317       FillRectangle(drawto,
11318                     SX + ED_ELEMENT_SETTINGS_X(0),
11319                     SY + ED_ELEMENT_SETTINGS_Y(14) - ED_SETTINGS_TABS_YOFFSET -
11320                     ED_TAB_BAR_HEIGHT,
11321                     getTabulatorBarWidth(), getTabulatorBarHeight(), tab_color);
11322   }
11323 }
11324
11325 static void DrawPropertiesChangeDrawingAreas(void)
11326 {
11327   if (IS_CUSTOM_ELEMENT(properties_element))
11328   {
11329     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_TARGET);
11330     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_TRIGGER);
11331     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_ACTION);
11332
11333     DrawCustomChangeContentArea();
11334   }
11335
11336   redraw_mask |= REDRAW_FIELD;
11337 }
11338
11339 static void DrawPropertiesChange(void)
11340 {
11341   int i;
11342
11343   // needed to initially set selectbox options for special action options
11344   setSelectboxSpecialActionOptions();
11345
11346   // draw stickybutton gadget
11347   MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11348
11349   // draw checkbutton gadgets
11350   for (i =  ED_CHECKBUTTON_ID_CHANGE_FIRST;
11351        i <= ED_CHECKBUTTON_ID_CHANGE_LAST; i++)
11352     MapCheckbuttonGadget(i);
11353
11354   // draw counter gadgets
11355   for (i =  ED_COUNTER_ID_CHANGE_FIRST;
11356        i <= ED_COUNTER_ID_CHANGE_LAST; i++)
11357     MapCounterButtons(i);
11358
11359   // draw selectbox gadgets
11360   for (i =  ED_SELECTBOX_ID_CHANGE_FIRST;
11361        i <= ED_SELECTBOX_ID_CHANGE_LAST; i++)
11362     MapSelectboxGadget(i);
11363
11364   // draw textbutton gadgets
11365   for (i =  ED_TEXTBUTTON_ID_CHANGE_FIRST;
11366        i <= ED_TEXTBUTTON_ID_CHANGE_LAST; i++)
11367     MapTextbuttonGadget(i);
11368
11369   // draw graphicbutton gadgets
11370   for (i =  ED_GRAPHICBUTTON_ID_CHANGE_FIRST;
11371        i <= ED_GRAPHICBUTTON_ID_CHANGE_LAST; i++)
11372     MapGraphicbuttonGadget(i);
11373
11374   // draw drawing area gadgets
11375   DrawPropertiesChangeDrawingAreas();
11376 }
11377
11378 static void DrawEditorElementAnimation(int x, int y)
11379 {
11380   int graphic = el2img(properties_element);
11381   int frame = (ANIM_MODE(graphic) == ANIM_CE_VALUE ?
11382                custom_element.ce_value_fixed_initial :
11383                ANIM_MODE(graphic) == ANIM_CE_SCORE ?
11384                custom_element.collect_score_initial : FrameCounter);
11385
11386   DrawFixedGraphicAnimationExt(drawto, x, y, graphic, frame, NO_MASKING);
11387 }
11388
11389 static void DrawEditorElementName(int x, int y, int font_nr)
11390 {
11391   char *element_name = getElementInfoText(properties_element);
11392   int font_width = getFontWidth(font_nr);
11393   int font_height = getFontHeight(font_nr);
11394   int max_text_width = SXSIZE - x - ED_ELEMENT_SETTINGS_X(0);
11395   int max_chars_per_line = max_text_width / font_width;
11396
11397   if (strlen(element_name) <= max_chars_per_line)
11398     DrawTextS(x, y, font_nr, element_name);
11399   else
11400   {
11401     char buffer[max_chars_per_line + 1];
11402     int next_pos = max_chars_per_line;
11403
11404     strncpy(buffer, element_name, max_chars_per_line);
11405     buffer[max_chars_per_line] = '\0';
11406
11407     if (element_name[max_chars_per_line] == ' ')
11408       next_pos++;
11409     else
11410     {
11411       int i;
11412
11413       for (i = max_chars_per_line - 1; i >= 0; i--)
11414         if (buffer[i] == ' ')
11415           break;
11416
11417       if (strlen(&element_name[i + 1]) <= max_chars_per_line)
11418       {
11419         buffer[i] = '\0';
11420         next_pos = i + 1;
11421       }
11422     }
11423
11424     DrawTextS(x, y - font_height / 2, font_nr, buffer);
11425
11426     strncpy(buffer, &element_name[next_pos], max_chars_per_line);
11427     buffer[max_chars_per_line] = '\0';
11428
11429     DrawTextS(x, y + font_height / 2, font_nr, buffer);
11430   }
11431 }
11432
11433 static void DrawPropertiesWindow(void)
11434 {
11435   struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_INPUT_TEXT];
11436   int element_border = graphic_info[IMG_EDITOR_ELEMENT_BORDER].border_size;
11437   int border_size = gd->border_size;
11438   int font_nr = FONT_TEXT_1;
11439   int font_height = getFontHeight(font_nr);
11440   int xoffset = TILEX + element_border + 3 * border_size;
11441   int yoffset = (TILEY - font_height) / 2;
11442   int x1 = editor.settings.element_graphic.x + element_border;
11443   int y1 = editor.settings.element_graphic.y + element_border;
11444   int x2 = (editor.settings.element_name.x == -1 ? x1 + xoffset :
11445             editor.settings.element_name.x);
11446   int y2 = (editor.settings.element_name.y == -1 ? y1 + yoffset :
11447             editor.settings.element_name.y);
11448   char *text = "Element Settings";
11449   int font2_nr = FONT_TITLE_1;
11450   struct MenuPosInfo *pos = &editor.settings.headline;
11451   int sx = SX + ALIGNED_XPOS(pos->x, getTextWidth(text, font2_nr), pos->align);
11452   int sy = SY + pos->y;
11453
11454   stick_element_properties_window = FALSE;
11455
11456   // make sure that previous properties edit mode exists for this element
11457   if (edit_mode_properties > ED_MODE_PROPERTIES_CONFIG_2 &&
11458       !IS_CUSTOM_ELEMENT(properties_element))
11459     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG_2;
11460
11461   if (edit_mode_properties > ED_MODE_PROPERTIES_CONFIG &&
11462       !IS_PLAYER_ELEMENT(properties_element) &&
11463       !IS_CUSTOM_ELEMENT(properties_element))
11464     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG;
11465
11466   if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG &&
11467       (IS_PLAYER_ELEMENT(properties_element) ||
11468        IS_CUSTOM_ELEMENT(properties_element)))
11469     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG_1;
11470
11471   CopyElementPropertiesToEditor(properties_element);
11472
11473   UnmapLevelEditorFieldGadgets();
11474   UnmapLevelEditorToolboxDrawingGadgets();
11475   UnmapLevelEditorToolboxCustomGadgets();
11476
11477   MapLevelEditorToolboxCustomGadgetsIfNeeded();
11478
11479   SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
11480   ClearField();
11481
11482   DrawText(sx, sy, text, font2_nr);
11483
11484   FrameCounter = 0;     // restart animation frame counter
11485
11486   DrawElementBorder(SX + x1, SY + y1, TILEX, TILEY, FALSE);
11487   DrawEditorElementAnimation(SX + x1, SY + y1);
11488   DrawEditorElementName(x2, y2, font_nr);
11489
11490   DrawPropertiesTabulatorGadgets();
11491
11492   if (edit_mode_properties == ED_MODE_PROPERTIES_INFO)
11493     DrawPropertiesInfo();
11494   else if (edit_mode_properties == ED_MODE_PROPERTIES_CHANGE)
11495     DrawPropertiesChange();
11496   else  // (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG[_1|_2])
11497     DrawPropertiesConfig();
11498 }
11499
11500 static void DrawPaletteWindow(void)
11501 {
11502   int i;
11503
11504   UnmapLevelEditorFieldGadgets();
11505
11506   SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
11507   ClearField();
11508
11509   // map buttons to select elements
11510   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
11511     MapGadget(level_editor_gadget[GADGET_ID_ELEMENTLIST_FIRST + i]);
11512   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL]);
11513   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_UP]);
11514   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_DOWN]);
11515 }
11516
11517 static void UpdateCustomElementGraphicGadgets(void)
11518 {
11519   int i;
11520
11521   InitElementPropertiesGfxElement();
11522
11523   ModifyEditorElementList();
11524   RedrawDrawingElements();
11525
11526   // force redraw of all mapped drawing area gadgets
11527   for (i = 0; i < ED_NUM_DRAWING_AREAS; i++)
11528   {
11529     struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[i].gadget_id];
11530
11531     if (gi->mapped)
11532       MapDrawingArea(i);
11533   }
11534 }
11535
11536 static int getOpenDirectionFromTube(int element)
11537 {
11538   switch (element)
11539   {
11540     case EL_TUBE_LEFT_UP:               return (MV_LEFT | MV_UP);
11541     case EL_TUBE_LEFT_DOWN:             return (MV_LEFT | MV_DOWN);
11542     case EL_TUBE_RIGHT_UP:              return (MV_RIGHT | MV_UP);
11543     case EL_TUBE_RIGHT_DOWN:            return (MV_RIGHT | MV_DOWN);
11544     case EL_TUBE_HORIZONTAL:            return (MV_HORIZONTAL);
11545     case EL_TUBE_HORIZONTAL_UP:         return (MV_HORIZONTAL | MV_UP);
11546     case EL_TUBE_HORIZONTAL_DOWN:       return (MV_HORIZONTAL | MV_DOWN);
11547     case EL_TUBE_VERTICAL:              return (MV_VERTICAL);
11548     case EL_TUBE_VERTICAL_LEFT:         return (MV_VERTICAL | MV_LEFT);
11549     case EL_TUBE_VERTICAL_RIGHT:        return (MV_VERTICAL | MV_RIGHT);
11550     case EL_TUBE_ANY:                   return (MV_ANY_DIRECTION);
11551   }
11552
11553   return MV_NONE;
11554 }
11555
11556 static int getTubeFromOpenDirection(int direction)
11557 {
11558   switch (direction)
11559   {
11560     case (MV_LEFT | MV_UP):             return EL_TUBE_LEFT_UP;
11561     case (MV_LEFT | MV_DOWN):           return EL_TUBE_LEFT_DOWN;
11562     case (MV_RIGHT | MV_UP):            return EL_TUBE_RIGHT_UP;
11563     case (MV_RIGHT | MV_DOWN):          return EL_TUBE_RIGHT_DOWN;
11564     case (MV_HORIZONTAL):               return EL_TUBE_HORIZONTAL;
11565     case (MV_HORIZONTAL | MV_UP):       return EL_TUBE_HORIZONTAL_UP;
11566     case (MV_HORIZONTAL | MV_DOWN):     return EL_TUBE_HORIZONTAL_DOWN;
11567     case (MV_VERTICAL):                 return EL_TUBE_VERTICAL;
11568     case (MV_VERTICAL | MV_LEFT):       return EL_TUBE_VERTICAL_LEFT;
11569     case (MV_VERTICAL | MV_RIGHT):      return EL_TUBE_VERTICAL_RIGHT;
11570     case (MV_ANY_DIRECTION):            return EL_TUBE_ANY;
11571
11572     // if only one direction, fall back to simple tube with that direction
11573     case (MV_LEFT):                     return EL_TUBE_HORIZONTAL;
11574     case (MV_RIGHT):                    return EL_TUBE_HORIZONTAL;
11575     case (MV_UP):                       return EL_TUBE_VERTICAL;
11576     case (MV_DOWN):                     return EL_TUBE_VERTICAL;
11577   }
11578
11579   return EL_EMPTY;
11580 }
11581
11582 static int getTubeFromOpenDirectionNotEmpty(int direction, int element_old)
11583 {
11584   int element_new = getTubeFromOpenDirection(direction);
11585
11586   return (element_new != EL_EMPTY ? element_new : element_old);
11587 }
11588
11589 static int getOpenDirectionFromBelt(int element)
11590 {
11591   int belt_dir = getBeltDirFromBeltElement(element);
11592
11593   return (belt_dir == MV_LEFT ? MV_RIGHT :
11594           belt_dir == MV_RIGHT ? MV_LEFT :
11595           belt_dir == MV_NONE ? MV_HORIZONTAL : belt_dir);
11596 }
11597
11598 static int getBeltFromNrAndOpenDirection(int nr, int direction)
11599 {
11600   int belt_dir = (direction == MV_LEFT ? MV_RIGHT :
11601                   direction == MV_RIGHT ? MV_LEFT :
11602                   direction == MV_HORIZONTAL ? MV_NONE : direction);
11603
11604   if (direction == MV_NONE)
11605     return EL_EMPTY;
11606
11607   return getBeltElementFromBeltNrAndBeltDir(nr, belt_dir);
11608 }
11609
11610 static int getBeltFromNrAndOpenDirectionNotEmpty(int nr, int direction,
11611                                                  int element_old)
11612 {
11613   int element_new = getBeltFromNrAndOpenDirection(nr, direction);
11614
11615   return (element_new != EL_EMPTY ? element_new : element_old);
11616 }
11617
11618 static int getOpenDirectionFromPool(int element)
11619 {
11620   switch (element)
11621   {
11622     case EL_ACID_POOL_TOPLEFT:          return (MV_DOWN | MV_RIGHT);
11623     case EL_ACID_POOL_TOPRIGHT:         return (MV_DOWN | MV_LEFT);
11624     case EL_ACID_POOL_BOTTOMLEFT:       return (MV_UP | MV_RIGHT);
11625     case EL_ACID_POOL_BOTTOMRIGHT:      return (MV_UP | MV_LEFT);
11626     case EL_ACID_POOL_BOTTOM:           return (MV_HORIZONTAL | MV_UP);
11627     case EL_ACID:                       return (MV_HORIZONTAL | MV_DOWN);
11628   }
11629
11630   return MV_NONE;
11631 }
11632
11633 static int getPoolFromOpenDirection(int direction)
11634 {
11635   switch (direction)
11636   {
11637     case (MV_DOWN | MV_RIGHT):          return EL_ACID_POOL_TOPLEFT;
11638     case (MV_DOWN | MV_LEFT):           return EL_ACID_POOL_TOPRIGHT;
11639     case (MV_UP | MV_RIGHT):            return EL_ACID_POOL_BOTTOMLEFT;
11640     case (MV_UP | MV_LEFT):             return EL_ACID_POOL_BOTTOMRIGHT;
11641     case (MV_HORIZONTAL | MV_UP):       return EL_ACID_POOL_BOTTOM;
11642     case (MV_HORIZONTAL | MV_DOWN):     return EL_ACID;
11643   }
11644
11645   return EL_EMPTY;
11646 }
11647
11648 static int getPoolFromOpenDirectionExt(int direction, int help_element)
11649 {
11650   int element = getPoolFromOpenDirection(direction);
11651   int help_direction = getOpenDirectionFromPool(help_element);
11652
11653   if (element == EL_EMPTY)
11654   {
11655     int help_direction_vertical = help_direction & MV_VERTICAL;
11656
11657     element = getPoolFromOpenDirection(direction | help_direction_vertical);
11658   }
11659
11660   if (element == EL_EMPTY)
11661   {
11662     int help_direction_horizontal = help_direction & MV_HORIZONTAL;
11663
11664     element = getPoolFromOpenDirection(direction | help_direction_horizontal);
11665   }
11666
11667   return element;
11668 }
11669
11670 static int getPoolFromOpenDirectionNotEmpty(int direction, int element_old)
11671 {
11672   int element_new = getPoolFromOpenDirectionExt(direction, element_old);
11673
11674   return (element_new != EL_EMPTY ? element_new : element_old);
11675 }
11676
11677 static int getOpenDirectionFromPillar(int element)
11678 {
11679   switch (element)
11680   {
11681     case EL_EMC_WALL_1:                 return (MV_DOWN);
11682     case EL_EMC_WALL_2:                 return (MV_VERTICAL);
11683     case EL_EMC_WALL_3:                 return (MV_UP);
11684   }
11685
11686   return MV_NONE;
11687 }
11688
11689 static int getPillarFromOpenDirection(int direction)
11690 {
11691   switch (direction)
11692   {
11693     case (MV_DOWN):                     return EL_EMC_WALL_1;
11694     case (MV_VERTICAL):                 return EL_EMC_WALL_2;
11695     case (MV_UP):                       return EL_EMC_WALL_3;
11696   }
11697
11698   return EL_EMPTY;
11699 }
11700
11701 static int getPillarFromOpenDirectionNotEmpty(int direction, int element_old)
11702 {
11703   int element_new = getPillarFromOpenDirection(direction);
11704
11705   return (element_new != EL_EMPTY ? element_new : element_old);
11706 }
11707
11708 static int getOpenDirectionFromSteel2(int element)
11709 {
11710   switch (element)
11711   {
11712     case EL_DC_STEELWALL_2_LEFT:        return (MV_RIGHT);
11713     case EL_DC_STEELWALL_2_RIGHT:       return (MV_LEFT);
11714     case EL_DC_STEELWALL_2_TOP:         return (MV_DOWN);
11715     case EL_DC_STEELWALL_2_BOTTOM:      return (MV_UP);
11716     case EL_DC_STEELWALL_2_HORIZONTAL:  return (MV_HORIZONTAL);
11717     case EL_DC_STEELWALL_2_VERTICAL:    return (MV_VERTICAL);
11718     case EL_DC_STEELWALL_2_MIDDLE:      return (MV_ANY_DIRECTION);
11719     case EL_DC_STEELWALL_2_SINGLE:      return (MV_NONE);
11720   }
11721
11722   return MV_NONE;
11723 }
11724
11725 static int getSteel2FromOpenDirection(int direction)
11726 {
11727   switch (direction)
11728   {
11729     case (MV_RIGHT):                    return EL_DC_STEELWALL_2_LEFT;
11730     case (MV_LEFT):                     return EL_DC_STEELWALL_2_RIGHT;
11731     case (MV_DOWN):                     return EL_DC_STEELWALL_2_TOP;
11732     case (MV_UP):                       return EL_DC_STEELWALL_2_BOTTOM;
11733     case (MV_HORIZONTAL):               return EL_DC_STEELWALL_2_HORIZONTAL;
11734     case (MV_VERTICAL):                 return EL_DC_STEELWALL_2_VERTICAL;
11735     case (MV_ANY_DIRECTION):            return EL_DC_STEELWALL_2_MIDDLE;
11736     case (MV_NONE):                     return EL_DC_STEELWALL_2_SINGLE;
11737   }
11738
11739   return EL_EMPTY;
11740 }
11741
11742 static int getSteel2FromOpenDirectionNotEmpty(int direction, int element_old)
11743 {
11744   int element_new = getSteel2FromOpenDirection(direction);
11745
11746   return (element_new != EL_EMPTY ? element_new : element_old);
11747 }
11748
11749 static int getOpenDirectionFromChip(int element)
11750 {
11751   switch (element)
11752   {
11753     case EL_SP_CHIP_SINGLE:             return (MV_NONE);
11754     case EL_SP_CHIP_LEFT:               return (MV_RIGHT);
11755     case EL_SP_CHIP_RIGHT:              return (MV_LEFT);
11756     case EL_SP_CHIP_TOP:                return (MV_DOWN);
11757     case EL_SP_CHIP_BOTTOM:             return (MV_UP);
11758   }
11759
11760   return MV_NONE;
11761 }
11762
11763 static int getChipFromOpenDirection(int direction)
11764 {
11765   switch (direction)
11766   {
11767     case (MV_NONE):                     return EL_SP_CHIP_SINGLE;
11768     case (MV_LEFT):                     return EL_SP_CHIP_RIGHT;
11769     case (MV_RIGHT):                    return EL_SP_CHIP_LEFT;
11770     case (MV_UP):                       return EL_SP_CHIP_BOTTOM;
11771     case (MV_DOWN):                     return EL_SP_CHIP_TOP;
11772   }
11773
11774   return EL_EMPTY;
11775 }
11776
11777 static int getChipFromOpenDirectionNotEmpty(int direction, int element_old)
11778 {
11779   int element_new = getChipFromOpenDirection(direction);
11780
11781   return (element_new != EL_EMPTY ? element_new : element_old);
11782 }
11783
11784 static int getClosedTube(int x, int y)
11785 {
11786   struct XY *xy = xy_directions;
11787   int element_old = IntelliDrawBuffer[x][y];
11788   int direction_old = getOpenDirectionFromTube(element_old);
11789   int direction_new = MV_NONE;
11790   int i;
11791
11792   for (i = 0; i < NUM_DIRECTIONS; i++)
11793   {
11794     int xx = x + xy[i].x;
11795     int yy = y + xy[i].y;
11796     int dir = MV_DIR_FROM_BIT(i);
11797     int dir_opposite = MV_DIR_OPPOSITE(dir);
11798
11799     if (IN_LEV_FIELD(xx, yy) && IS_TUBE(IntelliDrawBuffer[xx][yy]) &&
11800         (direction_old & dir) &&
11801         (getOpenDirectionFromTube(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11802       direction_new |= dir;
11803   }
11804
11805   return getTubeFromOpenDirectionNotEmpty(direction_new, element_old);
11806 }
11807
11808 static int getClosedBelt(int x, int y)
11809 {
11810   struct XY *xy = xy_directions;
11811   int element_old = IntelliDrawBuffer[x][y];
11812   int nr = getBeltNrFromBeltElement(element_old);
11813   int direction_old = getOpenDirectionFromBelt(element_old);
11814   int direction_new = MV_NONE;
11815   int i;
11816
11817   for (i = MV_BIT_LEFT; i <= MV_BIT_RIGHT; i++)
11818   {
11819     int xx = x + xy[i].x;
11820     int yy = y + xy[i].y;
11821     int dir = MV_DIR_FROM_BIT(i);
11822     int dir_opposite = MV_DIR_OPPOSITE(dir);
11823
11824     if (IN_LEV_FIELD(xx, yy) && IS_BELT(IntelliDrawBuffer[xx][yy]) &&
11825         (direction_old & dir) &&
11826         (getOpenDirectionFromBelt(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11827       direction_new |= dir;
11828   }
11829
11830   return getBeltFromNrAndOpenDirection(nr, direction_new);
11831 }
11832
11833 static int getClosedPool(int x, int y)
11834 {
11835   struct XY *xy = xy_directions;
11836   int element_old = IntelliDrawBuffer[x][y];
11837   int direction_old = getOpenDirectionFromPool(element_old);
11838   int direction_new = MV_NONE;
11839   int i;
11840
11841   for (i = 0; i < NUM_DIRECTIONS; i++)
11842   {
11843     int xx = x + xy[i].x;
11844     int yy = y + xy[i].y;
11845     int dir = MV_DIR_FROM_BIT(i);
11846     int dir_opposite = MV_DIR_OPPOSITE(dir);
11847
11848     if (IN_LEV_FIELD(xx, yy) &&
11849         IS_ACID_POOL_OR_ACID(IntelliDrawBuffer[xx][yy]) &&
11850         (direction_old & dir) &&
11851         (getOpenDirectionFromPool(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11852       direction_new |= dir;
11853   }
11854
11855   return getPoolFromOpenDirectionNotEmpty(direction_new, element_old);
11856 }
11857
11858 static int getClosedPillar(int x, int y)
11859 {
11860   struct XY *xy = xy_directions;
11861   int element_old = IntelliDrawBuffer[x][y];
11862   int direction_old = getOpenDirectionFromPillar(element_old);
11863   int direction_new = MV_NONE;
11864   int i;
11865
11866   for (i = MV_BIT_UP; i <= MV_BIT_DOWN; i++)
11867   {
11868     int xx = x + xy[i].x;
11869     int yy = y + xy[i].y;
11870     int dir = MV_DIR_FROM_BIT(i);
11871     int dir_opposite = MV_DIR_OPPOSITE(dir);
11872
11873     if (IN_LEV_FIELD(xx, yy) && IS_EMC_PILLAR(IntelliDrawBuffer[xx][yy]) &&
11874         (direction_old & dir) &&
11875         (getOpenDirectionFromPillar(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11876       direction_new |= dir;
11877   }
11878
11879   return getPillarFromOpenDirectionNotEmpty(direction_new, element_old);
11880 }
11881
11882 static int getClosedSteel2(int x, int y)
11883 {
11884   struct XY *xy = xy_directions;
11885   int element_old = IntelliDrawBuffer[x][y];
11886   int direction_old = getOpenDirectionFromSteel2(element_old);
11887   int direction_new = MV_NONE;
11888   int i;
11889
11890   for (i = 0; i < NUM_DIRECTIONS; i++)
11891   {
11892     int xx = x + xy[i].x;
11893     int yy = y + xy[i].y;
11894     int dir = MV_DIR_FROM_BIT(i);
11895     int dir_opposite = MV_DIR_OPPOSITE(dir);
11896
11897     if (IN_LEV_FIELD(xx, yy) && IS_DC_STEELWALL_2(IntelliDrawBuffer[xx][yy]) &&
11898         (direction_old & dir) &&
11899         (getOpenDirectionFromSteel2(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11900       direction_new |= dir;
11901   }
11902
11903   return getSteel2FromOpenDirectionNotEmpty(direction_new, element_old);
11904 }
11905
11906 static int getClosedChip(int x, int y)
11907 {
11908   struct XY *xy = xy_directions;
11909   int element_old = IntelliDrawBuffer[x][y];
11910   int direction_old = getOpenDirectionFromChip(element_old);
11911   int direction_new = MV_NONE;
11912   int i;
11913
11914   for (i = 0; i < NUM_DIRECTIONS; i++)
11915   {
11916     int xx = x + xy[i].x;
11917     int yy = y + xy[i].y;
11918     int dir = MV_DIR_FROM_BIT(i);
11919     int dir_opposite = MV_DIR_OPPOSITE(dir);
11920
11921     if (IN_LEV_FIELD(xx, yy) && IS_SP_CHIP(IntelliDrawBuffer[xx][yy]) &&
11922         (direction_old & dir) &&
11923         (getOpenDirectionFromChip(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11924       direction_new |= dir;
11925   }
11926
11927   return getChipFromOpenDirectionNotEmpty(direction_new, element_old);
11928 }
11929
11930 static void SetElementSimpleExt(int x, int y, int dx, int dy, int element,
11931                                 boolean change_level)
11932 {
11933   int sx = x - level_xpos;
11934   int sy = y - level_ypos;
11935   int old_element = Tile[x][y];
11936   int new_element = element;
11937   unsigned int new_bitmask = (getDrawModeHiRes() ? (dx + 1) << (dy * 2) : 0x0f);
11938   boolean draw_masked = FALSE;
11939
11940   if (IS_MM_WALL_EDITOR(element))
11941   {
11942     element = map_mm_wall_element_editor(element) | new_bitmask;
11943
11944     if (IS_MM_WALL(old_element))
11945       element |= MM_WALL_BITS(old_element);
11946
11947     if (!change_level)
11948       draw_masked = TRUE;
11949   }
11950   else if (IS_MM_WALL(old_element) && element == EL_EMPTY)
11951   {
11952     int element_changed = old_element & ~new_bitmask;
11953
11954     if (MM_WALL_BITS(element_changed) != 0)
11955       element = element_changed;
11956   }
11957
11958   IntelliDrawBuffer[x][y] = element;
11959
11960   if (change_level)
11961     Tile[x][y] = element;
11962
11963   if (IN_ED_FIELD(sx, sy))
11964   {
11965     if (IS_MM_WALL(old_element) && new_element == EL_EMPTY)
11966       DrawSizedWallParts_MM(sx, sy, EL_EMPTY, ed_tilesize, FALSE, new_bitmask);
11967     else if (draw_masked)
11968       DrawEditorElementThruMask(sx, sy, element);
11969     else
11970       DrawEditorElement(sx, sy, element);
11971   }
11972 }
11973
11974 static void SetElementSimple(int x, int y, int element, boolean change_level)
11975 {
11976   SetElementSimpleExt(x, y, 0, 0, element, change_level);
11977 }
11978
11979 static void MergeAndCloseNeighbourElements(int x1, int y1, int *element1,
11980                                            int x2, int y2, int *element2,
11981                                            int (*close_function)(int, int),
11982                                            boolean change_level)
11983 {
11984   // set neighbour elements to newly determined connections
11985   SetElementSimple(x1, y1, *element1, change_level);
11986   SetElementSimple(x2, y2, *element2, change_level);
11987
11988   // remove all open connections of neighbour elements
11989   *element1 = close_function(x1, y1);
11990   *element2 = close_function(x2, y2);
11991
11992   // set neighbour elements to new, minimized connections
11993   SetElementSimple(x1, y1, *element1, change_level);
11994   SetElementSimple(x2, y2, *element2, change_level);
11995 }
11996
11997 static void SetElementIntelliDraw(int x, int y, int dx, int dy, int new_element,
11998                                   boolean change_level, int button)
11999 {
12000   struct XY *xy = xy_directions;
12001   static int last_x = -1;
12002   static int last_y = -1;
12003
12004   if (new_element == EL_UNDEFINED)
12005   {
12006     last_x = -1;
12007     last_y = -1;
12008
12009     return;
12010   }
12011
12012   int old_element = IntelliDrawBuffer[x][y];
12013
12014   if (IS_TUBE(new_element))
12015   {
12016     int last_element_new = EL_UNDEFINED;
12017     int direction = MV_NONE;
12018     int i;
12019
12020     // if old element is of same kind, keep all existing directions
12021     if (IS_TUBE(old_element))
12022       direction |= getOpenDirectionFromTube(old_element);
12023
12024     for (i = 0; i < NUM_DIRECTIONS; i++)
12025     {
12026       int xx = x + xy[i].x;
12027       int yy = y + xy[i].y;
12028
12029       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12030           IS_TUBE(IntelliDrawBuffer[last_x][last_y]))
12031       {
12032         int dir = MV_DIR_FROM_BIT(i);
12033         int dir_opposite = MV_DIR_OPPOSITE(dir);
12034         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12035         int last_direction_old = getOpenDirectionFromTube(last_element_old);
12036         int last_direction_new = last_direction_old | dir_opposite;
12037
12038         last_element_new = getTubeFromOpenDirection(last_direction_new);
12039
12040         direction |= dir;
12041       }
12042     }
12043
12044     new_element = getTubeFromOpenDirectionNotEmpty(direction, new_element);
12045
12046     if (last_element_new != EL_UNDEFINED)
12047       MergeAndCloseNeighbourElements(x, y, &new_element,
12048                                      last_x, last_y, &last_element_new,
12049                                      getClosedTube, change_level);
12050   }
12051   else if (IS_BELT(new_element))
12052   {
12053     int belt_nr = getBeltNrFromBeltElement(new_element);
12054     int last_element_new = EL_UNDEFINED;
12055     int direction = MV_NONE;
12056     int i;
12057
12058     // if old element is of same kind, keep all existing directions
12059     if (IS_BELT(old_element))
12060       direction |= getOpenDirectionFromBelt(old_element);
12061
12062     for (i = MV_BIT_LEFT; i <= MV_BIT_RIGHT; i++)
12063     {
12064       int xx = x + xy[i].x;
12065       int yy = y + xy[i].y;
12066
12067       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12068           IS_BELT(IntelliDrawBuffer[last_x][last_y]))
12069       {
12070         int dir = MV_DIR_FROM_BIT(i);
12071         int dir_opposite = MV_DIR_OPPOSITE(dir);
12072         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12073         int last_belt_nr = getBeltNrFromBeltElement(last_element_old);
12074         int last_direction_old = getOpenDirectionFromBelt(last_element_old);
12075         int last_direction_new = last_direction_old | dir_opposite;
12076
12077         last_element_new = getBeltFromNrAndOpenDirection(last_belt_nr,
12078                                                          last_direction_new);
12079         direction |= dir;
12080       }
12081     }
12082
12083     new_element = getBeltFromNrAndOpenDirectionNotEmpty(belt_nr, direction,
12084                                                         new_element);
12085     if (last_element_new != EL_UNDEFINED)
12086       MergeAndCloseNeighbourElements(x, y, &new_element,
12087                                      last_x, last_y, &last_element_new,
12088                                      getClosedBelt, change_level);
12089   }
12090   else if (IS_ACID_POOL_OR_ACID(new_element))
12091   {
12092     int last_element_new = EL_UNDEFINED;
12093     int direction = MV_NONE;
12094     int i;
12095
12096     // if old element is of same kind, keep all existing directions
12097     if (IS_ACID_POOL_OR_ACID(old_element))
12098       direction |= getOpenDirectionFromPool(old_element);
12099
12100     for (i = 0; i < NUM_DIRECTIONS; i++)
12101     {
12102       int xx = x + xy[i].x;
12103       int yy = y + xy[i].y;
12104
12105       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12106           IS_ACID_POOL_OR_ACID(IntelliDrawBuffer[last_x][last_y]))
12107       {
12108         int dir = MV_DIR_FROM_BIT(i);
12109         int dir_opposite = MV_DIR_OPPOSITE(dir);
12110         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12111         int last_direction_old = getOpenDirectionFromPool(last_element_old);
12112         int last_direction_new = last_direction_old | dir_opposite;
12113
12114         last_element_new = getPoolFromOpenDirection(last_direction_new);
12115
12116         direction |= dir;
12117       }
12118     }
12119
12120     // special corrections needed for intuitively correct acid pool drawing
12121     if (last_element_new == EL_EMPTY)
12122       last_element_new = new_element;
12123     else if (last_element_new != EL_UNDEFINED)
12124       new_element = last_element_new;
12125
12126     new_element = getPoolFromOpenDirectionNotEmpty(direction, new_element);
12127
12128     if (last_element_new != EL_UNDEFINED)
12129       MergeAndCloseNeighbourElements(x, y, &new_element,
12130                                      last_x, last_y, &last_element_new,
12131                                      getClosedPool, change_level);
12132   }
12133   else if (IS_EMC_PILLAR(new_element))
12134   {
12135     int last_element_new = EL_UNDEFINED;
12136     int direction = MV_NONE;
12137     int i;
12138
12139     // if old element is of same kind, keep all existing directions
12140     if (IS_EMC_PILLAR(old_element))
12141       direction |= getOpenDirectionFromPillar(old_element);
12142
12143     for (i = MV_BIT_UP; i <= MV_BIT_DOWN; i++)
12144     {
12145       int xx = x + xy[i].x;
12146       int yy = y + xy[i].y;
12147
12148       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12149           IS_EMC_PILLAR(IntelliDrawBuffer[last_x][last_y]))
12150       {
12151         int dir = MV_DIR_FROM_BIT(i);
12152         int dir_opposite = MV_DIR_OPPOSITE(dir);
12153         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12154         int last_direction_old = getOpenDirectionFromPillar(last_element_old);
12155         int last_direction_new = last_direction_old | dir_opposite;
12156
12157         last_element_new = getPillarFromOpenDirection(last_direction_new);
12158
12159         direction |= dir;
12160       }
12161     }
12162
12163     new_element = getPillarFromOpenDirectionNotEmpty(direction, new_element);
12164
12165     if (last_element_new != EL_UNDEFINED)
12166       MergeAndCloseNeighbourElements(x, y, &new_element,
12167                                      last_x, last_y, &last_element_new,
12168                                      getClosedPillar, change_level);
12169   }
12170   else if (IS_DC_STEELWALL_2(new_element))
12171   {
12172     int last_element_new = EL_UNDEFINED;
12173     int direction = MV_NONE;
12174     int i;
12175
12176     // if old element is of same kind, keep all existing directions
12177     if (IS_DC_STEELWALL_2(old_element))
12178       direction |= getOpenDirectionFromSteel2(old_element);
12179
12180     for (i = 0; i < NUM_DIRECTIONS; i++)
12181     {
12182       int xx = x + xy[i].x;
12183       int yy = y + xy[i].y;
12184
12185       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12186           IS_DC_STEELWALL_2(IntelliDrawBuffer[last_x][last_y]))
12187       {
12188         int dir = MV_DIR_FROM_BIT(i);
12189         int dir_opposite = MV_DIR_OPPOSITE(dir);
12190         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12191         int last_direction_old = getOpenDirectionFromSteel2(last_element_old);
12192         int last_direction_new = last_direction_old | dir_opposite;
12193
12194         last_element_new = getSteel2FromOpenDirection(last_direction_new);
12195
12196         direction |= dir;
12197       }
12198     }
12199
12200     new_element = getSteel2FromOpenDirectionNotEmpty(direction, new_element);
12201
12202     if (last_element_new != EL_UNDEFINED)
12203       MergeAndCloseNeighbourElements(x, y, &new_element,
12204                                      last_x, last_y, &last_element_new,
12205                                      getClosedSteel2, change_level);
12206   }
12207   else if (IS_SP_CHIP(new_element))
12208   {
12209     int last_element_new = EL_UNDEFINED;
12210     int direction = MV_NONE;
12211     int i;
12212
12213     // (do not keep existing directions, regardless of kind of old element)
12214
12215     for (i = 0; i < NUM_DIRECTIONS; i++)
12216     {
12217       int xx = x + xy[i].x;
12218       int yy = y + xy[i].y;
12219
12220       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12221           IS_SP_CHIP(IntelliDrawBuffer[last_x][last_y]))
12222       {
12223         int dir = MV_DIR_FROM_BIT(i);
12224         int dir_opposite = MV_DIR_OPPOSITE(dir);
12225         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12226         int last_direction_old = getOpenDirectionFromChip(last_element_old);
12227         int last_direction_new = last_direction_old | dir_opposite;
12228
12229         if (last_direction_old == MV_NONE)
12230         {
12231           last_element_new = getChipFromOpenDirection(last_direction_new);
12232           direction |= dir;
12233         }
12234         else if (last_direction_old & (dir | dir_opposite))
12235         {
12236           direction |= MV_DIR_OPPOSITE(last_direction_old);
12237         }
12238         else
12239         {
12240           direction |= MV_DIR_OPPOSITE(dir);
12241         }
12242       }
12243     }
12244
12245     new_element = getChipFromOpenDirectionNotEmpty(direction, new_element);
12246
12247     if (last_element_new != EL_UNDEFINED)
12248       MergeAndCloseNeighbourElements(x, y, &new_element,
12249                                      last_x, last_y, &last_element_new,
12250                                      getClosedChip, change_level);
12251   }
12252   else if (IS_SP_HARDWARE_BASE(new_element))
12253   {
12254     int nr = GetSimpleRandom(6);
12255
12256     new_element = (nr == 0 ? EL_SP_HARDWARE_BASE_1 :
12257                    nr == 1 ? EL_SP_HARDWARE_BASE_2 :
12258                    nr == 2 ? EL_SP_HARDWARE_BASE_3 :
12259                    nr == 3 ? EL_SP_HARDWARE_BASE_4 :
12260                    nr == 4 ? EL_SP_HARDWARE_BASE_5 : EL_SP_HARDWARE_BASE_6);
12261   }
12262   else if (new_element == EL_SP_HARDWARE_GREEN ||
12263            new_element == EL_SP_HARDWARE_BLUE ||
12264            new_element == EL_SP_HARDWARE_RED)
12265   {
12266     int nr = GetSimpleRandom(3);
12267
12268     new_element = (nr == 0 ? EL_SP_HARDWARE_GREEN :
12269                    nr == 1 ? EL_SP_HARDWARE_BLUE : EL_SP_HARDWARE_RED);
12270   }
12271   else if (IS_GROUP_ELEMENT(new_element))
12272   {
12273     boolean connected_drawing = FALSE;
12274     int i;
12275
12276     for (i = 0; i < NUM_DIRECTIONS; i++)
12277     {
12278       int xx = x + xy[i].x;
12279       int yy = y + xy[i].y;
12280
12281       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12282           IS_IN_GROUP_EL(IntelliDrawBuffer[last_x][last_y], new_element))
12283         connected_drawing = TRUE;
12284     }
12285
12286     if (!connected_drawing)
12287       ResolveGroupElement(new_element);
12288
12289     new_element = GetElementFromGroupElement(new_element);
12290   }
12291   else if (IS_BELT_SWITCH(old_element))
12292   {
12293     int belt_nr = getBeltNrFromBeltSwitchElement(old_element);
12294     int belt_dir = getBeltDirFromBeltSwitchElement(old_element);
12295
12296     belt_dir = (belt_dir == MV_LEFT ? MV_NONE :
12297                 belt_dir == MV_NONE ? MV_RIGHT : MV_LEFT);
12298
12299     new_element = getBeltSwitchElementFromBeltNrAndBeltDir(belt_nr, belt_dir);
12300   }
12301   else
12302   {
12303     static int swappable_elements[][2] =
12304     {
12305       { EL_EXIT_CLOSED,                 EL_EXIT_OPEN                    },
12306       { EL_DYNAMITE,                    EL_DYNAMITE_ACTIVE              },
12307       { EL_EM_DYNAMITE,                 EL_EM_DYNAMITE_ACTIVE           },
12308       { EL_QUICKSAND_EMPTY,             EL_QUICKSAND_FULL               },
12309       { EL_EMERALD,                     EL_WALL_EMERALD                 },
12310       { EL_EMERALD_YELLOW,              EL_WALL_EMERALD_YELLOW          },
12311       { EL_EMERALD_RED,                 EL_WALL_EMERALD_RED             },
12312       { EL_EMERALD_PURPLE,              EL_WALL_EMERALD_PURPLE          },
12313       { EL_DIAMOND,                     EL_WALL_DIAMOND                 },
12314       { EL_BD_DIAMOND,                  EL_WALL_BD_DIAMOND              },
12315       { EL_GATE_1,                      EL_GATE_1_GRAY                  },
12316       { EL_GATE_2,                      EL_GATE_2_GRAY                  },
12317       { EL_GATE_3,                      EL_GATE_3_GRAY                  },
12318       { EL_GATE_4,                      EL_GATE_4_GRAY                  },
12319       { EL_EM_GATE_1,                   EL_EM_GATE_1_GRAY               },
12320       { EL_EM_GATE_2,                   EL_EM_GATE_2_GRAY               },
12321       { EL_EM_GATE_3,                   EL_EM_GATE_3_GRAY               },
12322       { EL_EM_GATE_4,                   EL_EM_GATE_4_GRAY               },
12323       { EL_EMC_GATE_5,                  EL_EMC_GATE_5_GRAY              },
12324       { EL_EMC_GATE_6,                  EL_EMC_GATE_6_GRAY              },
12325       { EL_EMC_GATE_7,                  EL_EMC_GATE_7_GRAY              },
12326       { EL_EMC_GATE_8,                  EL_EMC_GATE_8_GRAY              },
12327       { EL_DC_GATE_WHITE,               EL_DC_GATE_WHITE_GRAY           },
12328       { EL_TIME_ORB_EMPTY,              EL_TIME_ORB_FULL                },
12329       { EL_LAMP,                        EL_LAMP_ACTIVE                  },
12330       { EL_SOKOBAN_FIELD_EMPTY,         EL_SOKOBAN_FIELD_FULL           },
12331       { EL_SP_BASE,                     EL_SP_BUGGY_BASE                },
12332       { EL_PEARL,                       EL_WALL_PEARL                   },
12333       { EL_CRYSTAL,                     EL_WALL_CRYSTAL                 },
12334       { EL_TIMEGATE_CLOSED,             EL_TIMEGATE_OPEN                },
12335       { EL_SWITCHGATE_CLOSED,           EL_SWITCHGATE_OPEN              },
12336       { EL_SWITCHGATE_SWITCH_UP,        EL_SWITCHGATE_SWITCH_DOWN       },
12337       { EL_DC_SWITCHGATE_SWITCH_UP,     EL_DC_SWITCHGATE_SWITCH_DOWN    },
12338       { EL_LIGHT_SWITCH,                EL_LIGHT_SWITCH_ACTIVE          },
12339       { EL_LANDMINE,                    EL_DC_LANDMINE                  },
12340       { EL_SHIELD_NORMAL,               EL_SHIELD_DEADLY                },
12341       { EL_STEEL_EXIT_CLOSED,           EL_STEEL_EXIT_OPEN              },
12342       { EL_EM_EXIT_CLOSED,              EL_EM_EXIT_OPEN                 },
12343       { EL_EM_STEEL_EXIT_CLOSED,        EL_EM_STEEL_EXIT_OPEN           },
12344       { EL_QUICKSAND_FAST_EMPTY,        EL_QUICKSAND_FAST_FULL          },
12345       { EL_MM_EXIT_CLOSED,              EL_MM_EXIT_OPEN                 },
12346       { EL_MM_FUSE,                     EL_MM_FUSE_ACTIVE               },
12347       { EL_MM_LIGHTBULB,                EL_MM_LIGHTBULB_ACTIVE          },
12348       { EL_MM_FUEL_EMPTY,               EL_MM_FUEL_FULL                 },
12349
12350       { -1,                             -1                              },
12351     };
12352     static int rotatable_elements_4[][4] =
12353     {
12354       {
12355         EL_BUG_UP,
12356         EL_BUG_RIGHT,
12357         EL_BUG_DOWN,
12358         EL_BUG_LEFT
12359       },
12360       {
12361         EL_SPACESHIP_UP,
12362         EL_SPACESHIP_RIGHT,
12363         EL_SPACESHIP_DOWN,
12364         EL_SPACESHIP_LEFT
12365       },
12366       {
12367         EL_BD_BUTTERFLY_UP,
12368         EL_BD_BUTTERFLY_RIGHT,
12369         EL_BD_BUTTERFLY_DOWN,
12370         EL_BD_BUTTERFLY_LEFT
12371       },
12372       {
12373         EL_BD_FIREFLY_UP,
12374         EL_BD_FIREFLY_RIGHT,
12375         EL_BD_FIREFLY_DOWN,
12376         EL_BD_FIREFLY_LEFT
12377       },
12378       {
12379         EL_PACMAN_UP,
12380         EL_PACMAN_RIGHT,
12381         EL_PACMAN_DOWN,
12382         EL_PACMAN_LEFT
12383       },
12384       {
12385         EL_YAMYAM_UP,
12386         EL_YAMYAM_RIGHT,
12387         EL_YAMYAM_DOWN,
12388         EL_YAMYAM_LEFT
12389       },
12390       {
12391         EL_ARROW_UP,
12392         EL_ARROW_RIGHT,
12393         EL_ARROW_DOWN,
12394         EL_ARROW_LEFT
12395       },
12396       {
12397         EL_SP_PORT_UP,
12398         EL_SP_PORT_RIGHT,
12399         EL_SP_PORT_DOWN,
12400         EL_SP_PORT_LEFT
12401       },
12402       {
12403         EL_SP_GRAVITY_PORT_UP,
12404         EL_SP_GRAVITY_PORT_RIGHT,
12405         EL_SP_GRAVITY_PORT_DOWN,
12406         EL_SP_GRAVITY_PORT_LEFT
12407       },
12408       {
12409         EL_SP_GRAVITY_ON_PORT_UP,
12410         EL_SP_GRAVITY_ON_PORT_RIGHT,
12411         EL_SP_GRAVITY_ON_PORT_DOWN,
12412         EL_SP_GRAVITY_ON_PORT_LEFT
12413       },
12414       {
12415         EL_SP_GRAVITY_OFF_PORT_UP,
12416         EL_SP_GRAVITY_OFF_PORT_RIGHT,
12417         EL_SP_GRAVITY_OFF_PORT_DOWN,
12418         EL_SP_GRAVITY_OFF_PORT_LEFT
12419       },
12420       {
12421         EL_MOLE_UP,
12422         EL_MOLE_RIGHT,
12423         EL_MOLE_DOWN,
12424         EL_MOLE_LEFT
12425       },
12426       {
12427         EL_BALLOON_SWITCH_UP,
12428         EL_BALLOON_SWITCH_RIGHT,
12429         EL_BALLOON_SWITCH_DOWN,
12430         EL_BALLOON_SWITCH_LEFT
12431       },
12432       {
12433         EL_MM_MCDUFFIN_UP,
12434         EL_MM_MCDUFFIN_RIGHT,
12435         EL_MM_MCDUFFIN_DOWN,
12436         EL_MM_MCDUFFIN_LEFT
12437       },
12438       {
12439         EL_MM_MIRROR_FIXED_1,
12440         EL_MM_MIRROR_FIXED_4,
12441         EL_MM_MIRROR_FIXED_3,
12442         EL_MM_MIRROR_FIXED_2
12443       },
12444       {
12445         EL_MM_STEEL_GRID_FIXED_1,
12446         EL_MM_STEEL_GRID_FIXED_4,
12447         EL_MM_STEEL_GRID_FIXED_2,
12448         EL_MM_STEEL_GRID_FIXED_3
12449       },
12450       {
12451         EL_MM_WOODEN_GRID_FIXED_1,
12452         EL_MM_WOODEN_GRID_FIXED_4,
12453         EL_MM_WOODEN_GRID_FIXED_2,
12454         EL_MM_WOODEN_GRID_FIXED_3
12455       },
12456       {
12457         EL_MM_POLARIZER_CROSS_1,
12458         EL_MM_POLARIZER_CROSS_4,
12459         EL_MM_POLARIZER_CROSS_3,
12460         EL_MM_POLARIZER_CROSS_2
12461       },
12462       {
12463         EL_MM_PACMAN_UP,
12464         EL_MM_PACMAN_RIGHT,
12465         EL_MM_PACMAN_DOWN,
12466         EL_MM_PACMAN_LEFT
12467       },
12468       {
12469         EL_DF_LASER_UP,
12470         EL_DF_LASER_RIGHT,
12471         EL_DF_LASER_DOWN,
12472         EL_DF_LASER_LEFT
12473       },
12474       {
12475         EL_DF_RECEIVER_UP,
12476         EL_DF_RECEIVER_RIGHT,
12477         EL_DF_RECEIVER_DOWN,
12478         EL_DF_RECEIVER_LEFT
12479       },
12480       {
12481         EL_DF_SLOPE_1,
12482         EL_DF_SLOPE_4,
12483         EL_DF_SLOPE_3,
12484         EL_DF_SLOPE_2
12485       },
12486
12487       {
12488         -1,
12489       },
12490     };
12491     static int rotatable_elements_8[][8] =
12492     {
12493       {
12494         EL_DF_STEEL_GRID_FIXED_1,
12495         EL_DF_STEEL_GRID_FIXED_8,
12496         EL_DF_STEEL_GRID_FIXED_7,
12497         EL_DF_STEEL_GRID_FIXED_6,
12498         EL_DF_STEEL_GRID_FIXED_5,
12499         EL_DF_STEEL_GRID_FIXED_4,
12500         EL_DF_STEEL_GRID_FIXED_3,
12501         EL_DF_STEEL_GRID_FIXED_2
12502       },
12503       {
12504         EL_DF_WOODEN_GRID_FIXED_1,
12505         EL_DF_WOODEN_GRID_FIXED_8,
12506         EL_DF_WOODEN_GRID_FIXED_7,
12507         EL_DF_WOODEN_GRID_FIXED_6,
12508         EL_DF_WOODEN_GRID_FIXED_5,
12509         EL_DF_WOODEN_GRID_FIXED_4,
12510         EL_DF_WOODEN_GRID_FIXED_3,
12511         EL_DF_WOODEN_GRID_FIXED_2
12512       },
12513       {
12514         EL_DF_STEEL_GRID_ROTATING_1,
12515         EL_DF_STEEL_GRID_ROTATING_8,
12516         EL_DF_STEEL_GRID_ROTATING_7,
12517         EL_DF_STEEL_GRID_ROTATING_6,
12518         EL_DF_STEEL_GRID_ROTATING_5,
12519         EL_DF_STEEL_GRID_ROTATING_4,
12520         EL_DF_STEEL_GRID_ROTATING_3,
12521         EL_DF_STEEL_GRID_ROTATING_2
12522       },
12523       {
12524         EL_DF_WOODEN_GRID_ROTATING_1,
12525         EL_DF_WOODEN_GRID_ROTATING_8,
12526         EL_DF_WOODEN_GRID_ROTATING_7,
12527         EL_DF_WOODEN_GRID_ROTATING_6,
12528         EL_DF_WOODEN_GRID_ROTATING_5,
12529         EL_DF_WOODEN_GRID_ROTATING_4,
12530         EL_DF_WOODEN_GRID_ROTATING_3,
12531         EL_DF_WOODEN_GRID_ROTATING_2
12532       },
12533
12534       {
12535         -1,
12536       },
12537     };
12538     static int rotatable_elements_16[][16] =
12539     {
12540       {
12541         EL_MM_MIRROR_1,
12542         EL_MM_MIRROR_16,
12543         EL_MM_MIRROR_15,
12544         EL_MM_MIRROR_14,
12545         EL_MM_MIRROR_13,
12546         EL_MM_MIRROR_12,
12547         EL_MM_MIRROR_11,
12548         EL_MM_MIRROR_10,
12549         EL_MM_MIRROR_9,
12550         EL_MM_MIRROR_8,
12551         EL_MM_MIRROR_7,
12552         EL_MM_MIRROR_6,
12553         EL_MM_MIRROR_5,
12554         EL_MM_MIRROR_4,
12555         EL_MM_MIRROR_3,
12556         EL_MM_MIRROR_2
12557       },
12558       {
12559         EL_MM_TELEPORTER_5,
12560         EL_MM_TELEPORTER_4,
12561         EL_MM_TELEPORTER_3,
12562         EL_MM_TELEPORTER_2,
12563         EL_MM_TELEPORTER_1,
12564         EL_MM_TELEPORTER_16,
12565         EL_MM_TELEPORTER_15,
12566         EL_MM_TELEPORTER_14,
12567         EL_MM_TELEPORTER_13,
12568         EL_MM_TELEPORTER_12,
12569         EL_MM_TELEPORTER_11,
12570         EL_MM_TELEPORTER_10,
12571         EL_MM_TELEPORTER_9,
12572         EL_MM_TELEPORTER_8,
12573         EL_MM_TELEPORTER_7,
12574         EL_MM_TELEPORTER_6
12575       },
12576       {
12577         EL_MM_TELEPORTER_RED_5,
12578         EL_MM_TELEPORTER_RED_4,
12579         EL_MM_TELEPORTER_RED_3,
12580         EL_MM_TELEPORTER_RED_2,
12581         EL_MM_TELEPORTER_RED_1,
12582         EL_MM_TELEPORTER_RED_16,
12583         EL_MM_TELEPORTER_RED_15,
12584         EL_MM_TELEPORTER_RED_14,
12585         EL_MM_TELEPORTER_RED_13,
12586         EL_MM_TELEPORTER_RED_12,
12587         EL_MM_TELEPORTER_RED_11,
12588         EL_MM_TELEPORTER_RED_10,
12589         EL_MM_TELEPORTER_RED_9,
12590         EL_MM_TELEPORTER_RED_8,
12591         EL_MM_TELEPORTER_RED_7,
12592         EL_MM_TELEPORTER_RED_6
12593       },
12594       {
12595         EL_MM_TELEPORTER_YELLOW_5,
12596         EL_MM_TELEPORTER_YELLOW_4,
12597         EL_MM_TELEPORTER_YELLOW_3,
12598         EL_MM_TELEPORTER_YELLOW_2,
12599         EL_MM_TELEPORTER_YELLOW_1,
12600         EL_MM_TELEPORTER_YELLOW_16,
12601         EL_MM_TELEPORTER_YELLOW_15,
12602         EL_MM_TELEPORTER_YELLOW_14,
12603         EL_MM_TELEPORTER_YELLOW_13,
12604         EL_MM_TELEPORTER_YELLOW_12,
12605         EL_MM_TELEPORTER_YELLOW_11,
12606         EL_MM_TELEPORTER_YELLOW_10,
12607         EL_MM_TELEPORTER_YELLOW_9,
12608         EL_MM_TELEPORTER_YELLOW_8,
12609         EL_MM_TELEPORTER_YELLOW_7,
12610         EL_MM_TELEPORTER_YELLOW_6
12611       },
12612       {
12613         EL_MM_TELEPORTER_GREEN_5,
12614         EL_MM_TELEPORTER_GREEN_4,
12615         EL_MM_TELEPORTER_GREEN_3,
12616         EL_MM_TELEPORTER_GREEN_2,
12617         EL_MM_TELEPORTER_GREEN_1,
12618         EL_MM_TELEPORTER_GREEN_16,
12619         EL_MM_TELEPORTER_GREEN_15,
12620         EL_MM_TELEPORTER_GREEN_14,
12621         EL_MM_TELEPORTER_GREEN_13,
12622         EL_MM_TELEPORTER_GREEN_12,
12623         EL_MM_TELEPORTER_GREEN_11,
12624         EL_MM_TELEPORTER_GREEN_10,
12625         EL_MM_TELEPORTER_GREEN_9,
12626         EL_MM_TELEPORTER_GREEN_8,
12627         EL_MM_TELEPORTER_GREEN_7,
12628         EL_MM_TELEPORTER_GREEN_6
12629       },
12630       {
12631         EL_MM_TELEPORTER_BLUE_5,
12632         EL_MM_TELEPORTER_BLUE_4,
12633         EL_MM_TELEPORTER_BLUE_3,
12634         EL_MM_TELEPORTER_BLUE_2,
12635         EL_MM_TELEPORTER_BLUE_1,
12636         EL_MM_TELEPORTER_BLUE_16,
12637         EL_MM_TELEPORTER_BLUE_15,
12638         EL_MM_TELEPORTER_BLUE_14,
12639         EL_MM_TELEPORTER_BLUE_13,
12640         EL_MM_TELEPORTER_BLUE_12,
12641         EL_MM_TELEPORTER_BLUE_11,
12642         EL_MM_TELEPORTER_BLUE_10,
12643         EL_MM_TELEPORTER_BLUE_9,
12644         EL_MM_TELEPORTER_BLUE_8,
12645         EL_MM_TELEPORTER_BLUE_7,
12646         EL_MM_TELEPORTER_BLUE_6
12647       },
12648       {
12649         EL_MM_POLARIZER_1,
12650         EL_MM_POLARIZER_16,
12651         EL_MM_POLARIZER_15,
12652         EL_MM_POLARIZER_14,
12653         EL_MM_POLARIZER_13,
12654         EL_MM_POLARIZER_12,
12655         EL_MM_POLARIZER_11,
12656         EL_MM_POLARIZER_10,
12657         EL_MM_POLARIZER_9,
12658         EL_MM_POLARIZER_8,
12659         EL_MM_POLARIZER_7,
12660         EL_MM_POLARIZER_6,
12661         EL_MM_POLARIZER_5,
12662         EL_MM_POLARIZER_4,
12663         EL_MM_POLARIZER_3,
12664         EL_MM_POLARIZER_2
12665       },
12666       {
12667         EL_DF_MIRROR_1,
12668         EL_DF_MIRROR_16,
12669         EL_DF_MIRROR_15,
12670         EL_DF_MIRROR_14,
12671         EL_DF_MIRROR_13,
12672         EL_DF_MIRROR_12,
12673         EL_DF_MIRROR_11,
12674         EL_DF_MIRROR_10,
12675         EL_DF_MIRROR_9,
12676         EL_DF_MIRROR_8,
12677         EL_DF_MIRROR_7,
12678         EL_DF_MIRROR_6,
12679         EL_DF_MIRROR_5,
12680         EL_DF_MIRROR_4,
12681         EL_DF_MIRROR_3,
12682         EL_DF_MIRROR_2
12683       },
12684       {
12685         EL_DF_MIRROR_ROTATING_1,
12686         EL_DF_MIRROR_ROTATING_16,
12687         EL_DF_MIRROR_ROTATING_15,
12688         EL_DF_MIRROR_ROTATING_14,
12689         EL_DF_MIRROR_ROTATING_13,
12690         EL_DF_MIRROR_ROTATING_12,
12691         EL_DF_MIRROR_ROTATING_11,
12692         EL_DF_MIRROR_ROTATING_10,
12693         EL_DF_MIRROR_ROTATING_9,
12694         EL_DF_MIRROR_ROTATING_8,
12695         EL_DF_MIRROR_ROTATING_7,
12696         EL_DF_MIRROR_ROTATING_6,
12697         EL_DF_MIRROR_ROTATING_5,
12698         EL_DF_MIRROR_ROTATING_4,
12699         EL_DF_MIRROR_ROTATING_3,
12700         EL_DF_MIRROR_ROTATING_2
12701       },
12702       {
12703         EL_DF_MIRROR_FIXED_1,
12704         EL_DF_MIRROR_FIXED_16,
12705         EL_DF_MIRROR_FIXED_15,
12706         EL_DF_MIRROR_FIXED_14,
12707         EL_DF_MIRROR_FIXED_13,
12708         EL_DF_MIRROR_FIXED_12,
12709         EL_DF_MIRROR_FIXED_11,
12710         EL_DF_MIRROR_FIXED_10,
12711         EL_DF_MIRROR_FIXED_9,
12712         EL_DF_MIRROR_FIXED_8,
12713         EL_DF_MIRROR_FIXED_7,
12714         EL_DF_MIRROR_FIXED_6,
12715         EL_DF_MIRROR_FIXED_5,
12716         EL_DF_MIRROR_FIXED_4,
12717         EL_DF_MIRROR_FIXED_3,
12718         EL_DF_MIRROR_FIXED_2
12719       },
12720
12721       {
12722         -1,
12723       },
12724     };
12725     int i, j;
12726
12727     for (i = 0; swappable_elements[i][0] != -1; i++)
12728     {
12729       int element1 = swappable_elements[i][0];
12730       int element2 = swappable_elements[i][1];
12731
12732       if (old_element == element1 || old_element == element2)
12733         new_element = (old_element == element1 ? element2 : element1);
12734     }
12735
12736     for (i = 0; rotatable_elements_4[i][0] != -1; i++)
12737     {
12738       for (j = 0; j < 4; j++)
12739       {
12740         int element = rotatable_elements_4[i][j];
12741
12742         if (old_element == element)
12743           new_element = (button == 1 ? rotatable_elements_4[i][(j + 3) % 4] :
12744                          button == 2 ? rotatable_elements_4[i][0]           :
12745                          button == 3 ? rotatable_elements_4[i][(j + 1) % 4] :
12746                          old_element);
12747       }
12748     }
12749
12750     for (i = 0; rotatable_elements_8[i][0] != -1; i++)
12751     {
12752       for (j = 0; j < 8; j++)
12753       {
12754         int element = rotatable_elements_8[i][j];
12755
12756         if (old_element == element)
12757           new_element = (button == 1 ? rotatable_elements_8[i][(j + 7) % 8] :
12758                          button == 2 ? rotatable_elements_8[i][0]           :
12759                          button == 3 ? rotatable_elements_8[i][(j + 1) % 8] :
12760                          old_element);
12761       }
12762     }
12763
12764     for (i = 0; rotatable_elements_16[i][0] != -1; i++)
12765     {
12766       for (j = 0; j < 16; j++)
12767       {
12768         int element = rotatable_elements_16[i][j];
12769
12770         if (old_element == element)
12771           new_element = (button == 1 ? rotatable_elements_16[i][(j + 15) % 16] :
12772                          button == 2 ? rotatable_elements_16[i][0]             :
12773                          button == 3 ? rotatable_elements_16[i][(j + 1)  % 16] :
12774                          old_element);
12775       }
12776     }
12777
12778     if (old_element != new_element)
12779     {
12780       int max_infotext_len = getMaxInfoTextLength();
12781       char infotext[MAX_OUTPUT_LINESIZE + 1];
12782
12783       strncpy(infotext, getElementInfoText(new_element), max_infotext_len);
12784       infotext[max_infotext_len] = '\0';
12785
12786       ClearEditorGadgetInfoText();
12787
12788       DrawTextS(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY, INFOTEXT_FONT,
12789                 infotext);
12790     }
12791   }
12792
12793   if (IS_MM_WALL_EDITOR(new_element))
12794     SetElementSimpleExt(x, y, dx, dy, new_element, change_level);
12795   else
12796     SetElementSimple(x, y, new_element, change_level);
12797
12798   last_x = x;
12799   last_y = y;
12800 }
12801
12802 static void ResetIntelliDraw(void)
12803 {
12804   int x, y;
12805
12806   for (x = 0; x < lev_fieldx; x++)
12807     for (y = 0; y < lev_fieldy; y++)
12808       IntelliDrawBuffer[x][y] = Tile[x][y];
12809
12810   SetElementIntelliDraw(-1, -1, -1, -1, EL_UNDEFINED, FALSE, -1);
12811 }
12812
12813 static boolean draw_mode_hires = FALSE;
12814
12815 static boolean isHiresTileElement(int element)
12816 {
12817   return (IS_MM_WALL(element)        || element == EL_EMPTY);
12818 }
12819
12820 static boolean isHiresDrawElement(int element)
12821 {
12822   return (IS_MM_WALL_EDITOR(element) || element == EL_EMPTY);
12823 }
12824
12825 static int numHiresTiles(int element)
12826 {
12827   if (IS_MM_WALL(element))
12828     return get_number_of_bits(MM_WALL_BITS(element));
12829
12830   return 1;
12831 }
12832
12833 static void SetDrawModeHiRes(int element)
12834 {
12835   draw_mode_hires =
12836     (level.game_engine_type == GAME_ENGINE_TYPE_MM &&
12837      isHiresDrawElement(element));
12838 }
12839
12840 static boolean getDrawModeHiRes(void)
12841 {
12842   return draw_mode_hires;
12843 }
12844
12845 static int getLoResScreenPos(int pos)
12846 {
12847   return (getDrawModeHiRes() ? pos / 2 : pos);
12848 }
12849
12850 static int getLoResScreenMod(int pos)
12851 {
12852   return (getDrawModeHiRes() ? pos % 2 : 0);
12853 }
12854
12855 static void SetElementExt(int x, int y, int dx, int dy, int element,
12856                           boolean change_level, int button)
12857 {
12858   if (element < 0)
12859     SetElementSimple(x, y, Tile[x][y], change_level);
12860   else if (GetKeyModState() & KMOD_Shift)
12861     SetElementIntelliDraw(x, y, dx, dy, element, change_level, button);
12862   else
12863     SetElementSimpleExt(x, y, dx, dy, element, change_level);
12864 }
12865
12866 static void SetElement(int x, int y, int element)
12867 {
12868   SetElementExt(x, y, 0, 0, element, TRUE, -1);
12869 }
12870
12871 static void SetElementButton(int x, int y, int dx, int dy, int element,
12872                              int button)
12873 {
12874   SetElementExt(x, y, dx, dy, element, TRUE, button);
12875 }
12876
12877 static void SetElementHiRes(int sx2, int sy2, int element, boolean change_level)
12878 {
12879   int lx = getLoResScreenPos(sx2) + level_xpos;
12880   int ly = getLoResScreenPos(sy2) + level_ypos;
12881   int dx = getLoResScreenMod(sx2);
12882   int dy = getLoResScreenMod(sy2);
12883
12884   SetElementExt(lx, ly, dx, dy, element, change_level, -1);
12885 }
12886
12887 static void SetLevelElementHiRes(int lx2, int ly2, int element)
12888 {
12889   int lx = lx2 / 2;
12890   int ly = ly2 / 2;
12891   int dx = lx2 % 2;
12892   int dy = ly2 % 2;
12893
12894   SetElementExt(lx, ly, dx, dy, element, TRUE, -1);
12895 }
12896
12897 static int getLevelElementHiRes(int lx2, int ly2)
12898 {
12899   int lx = lx2 / 2;
12900   int ly = ly2 / 2;
12901   int dx = lx2 % 2;
12902   int dy = ly2 % 2;
12903   int element = Tile[lx][ly];
12904   unsigned int bitmask = (dx + 1) << (dy * 2);
12905
12906   if (IS_MM_WALL(element))
12907   {
12908     if (element & bitmask)
12909       return map_mm_wall_element(element);
12910     else
12911       return EL_EMPTY;
12912   }
12913
12914   return element;
12915 }
12916
12917 static void DrawLineElement(int x, int y, int element, boolean change_level)
12918 {
12919   SetElementHiRes(x, y, element, change_level);
12920 }
12921
12922 static void DrawLine(int from_x, int from_y, int to_x, int to_y,
12923                      int element, boolean change_level)
12924 {
12925   int xsize = ABS(to_x - from_x);
12926   int ysize = ABS(to_y - from_y);
12927   int dx = (to_x < from_x ? -1 : +1);
12928   int dy = (to_y < from_y ? -1 : +1);
12929   int i;
12930
12931   if (from_y == to_y)                   // horizontal line
12932   {
12933     for (i = 0; i <= xsize; i++)
12934       DrawLineElement(from_x + i * dx, from_y, element, change_level);
12935   }
12936   else if (from_x == to_x)              // vertical line
12937   {
12938     for (i = 0; i <= ysize; i++)
12939       DrawLineElement(from_x, from_y + i * dy, element, change_level);
12940   }
12941   else                                  // diagonal line
12942   {
12943     if (ysize < xsize)                  // a < 1
12944     {
12945       float a = (float)ysize / (float)xsize;
12946
12947       for (i = 0; i <= xsize; i++)
12948       {
12949         int x = dx * i;
12950         int y = dy * (int)(a * i + 0.5);
12951
12952         DrawLineElement(from_x + x, from_y + y, element, change_level);
12953       }
12954     }
12955     else                                // a >= 1
12956     {
12957       float a = (float)xsize / (float)ysize;
12958
12959       for (i = 0; i <= ysize; i++)
12960       {
12961         int x = dx * (int)(a * i + 0.5);
12962         int y = dy * i;
12963
12964         DrawLineElement(from_x + x, from_y + y, element, change_level);
12965       }
12966     }
12967   }
12968 }
12969
12970 static void DrawBox(int from_x, int from_y, int to_x, int to_y,
12971                     int element, boolean change_level)
12972 {
12973   DrawLine(from_x, from_y, from_x, to_y, element, change_level);
12974   DrawLine(from_x, to_y, to_x, to_y, element, change_level);
12975   DrawLine(to_x, to_y, to_x, from_y, element, change_level);
12976   DrawLine(to_x, from_y, from_x, from_y, element, change_level);
12977 }
12978
12979 static void DrawFilledBox(int from_x, int from_y, int to_x, int to_y,
12980                           int element, boolean change_level)
12981 {
12982   int y;
12983
12984   if (from_y > to_y)
12985     swap_number_pairs(&from_x, &from_y, &to_x, &to_y);
12986
12987   for (y = from_y; y <= to_y; y++)
12988     DrawLine(from_x, y, to_x, y, element, change_level);
12989 }
12990
12991 static void DrawArcExt(int from_x, int from_y, int to_x2, int to_y2,
12992                        int element, boolean change_level)
12993 {
12994   int to_x = to_x2 - (to_x2 > from_x ? +1 : -1);
12995   int to_y = to_y2 - (to_y2 > from_y ? +1 : -1);
12996   int len_x = ABS(to_x - from_x);
12997   int len_y = ABS(to_y - from_y);
12998   int radius, x, y;
12999
13000   radius = (int)(sqrt((float)(len_x * len_x + len_y * len_y)) + 0.5);
13001
13002   // not optimal (some points get drawn twice) but simple,
13003   // and fast enough for the few points we are drawing
13004
13005   for (x = 0; x <= radius; x++)
13006   {
13007     int sx, sy, sx2, sy2, lx, ly;
13008
13009     y = (int)(sqrt((float)(radius * radius - x * x)) + 0.5);
13010
13011     sx2 = from_x + x * (from_x < to_x2 ? +1 : -1);
13012     sy2 = from_y + y * (from_y < to_y2 ? +1 : -1);
13013     sx = getLoResScreenPos(sx2);
13014     sy = getLoResScreenPos(sy2);
13015     lx = sx + level_xpos;
13016     ly = sy + level_ypos;
13017
13018     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
13019       DrawLineElement(sx2, sy2, element, change_level);
13020   }
13021
13022   for (y = 0; y <= radius; y++)
13023   {
13024     int sx, sy, sx2, sy2, lx, ly;
13025
13026     x = (int)(sqrt((float)(radius * radius - y * y)) + 0.5);
13027
13028     sx2 = from_x + x * (from_x < to_x2 ? +1 : -1);
13029     sy2 = from_y + y * (from_y < to_y2 ? +1 : -1);
13030     sx = getLoResScreenPos(sx2);
13031     sy = getLoResScreenPos(sy2);
13032     lx = sx + level_xpos;
13033     ly = sy + level_ypos;
13034
13035     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
13036       DrawLineElement(sx2, sy2, element, change_level);
13037   }
13038 }
13039
13040 static void DrawArc(int from_x, int from_y, int to_x, int to_y,
13041                     int element, boolean change_level)
13042 {
13043   int to_x2 = to_x + (to_x < from_x ? -1 : +1);
13044   int to_y2 = to_y + (to_y > from_y ? +1 : -1);
13045
13046   DrawArcExt(from_x, from_y, to_x2, to_y2, element, change_level);
13047 }
13048
13049 #define DRAW_CIRCLES_BUTTON_AVAILABLE   0
13050
13051 #if DRAW_CIRCLES_BUTTON_AVAILABLE
13052 static void DrawCircle(int from_x, int from_y, int to_x, int to_y,
13053                        int element, boolean change_level)
13054 {
13055   int to_x2 = to_x + (to_x < from_x ? -1 : +1);
13056   int to_y2 = to_y + (to_y > from_y ? +1 : -1);
13057   int mirror_to_x2 = from_x - (to_x2 - from_x);
13058   int mirror_to_y2 = from_y - (to_y2 - from_y);
13059
13060   DrawArcExt(from_x, from_y, to_x2, to_y2, element, change_level);
13061   DrawArcExt(from_x, from_y, mirror_to_x2, to_y2, element, change_level);
13062   DrawArcExt(from_x, from_y, to_x2, mirror_to_y2, element, change_level);
13063   DrawArcExt(from_x, from_y, mirror_to_x2, mirror_to_y2, element, change_level);
13064 }
13065 #endif
13066
13067 static void DrawAreaBorder(int from_x, int from_y, int to_x, int to_y)
13068 {
13069   int from_sx, from_sy;
13070   int to_sx, to_sy;
13071
13072   if (from_x > to_x)
13073     swap_numbers(&from_x, &to_x);
13074
13075   if (from_y > to_y)
13076     swap_numbers(&from_y, &to_y);
13077
13078   from_sx = SX + from_x * ed_tilesize;
13079   from_sy = SY + from_y * ed_tilesize;
13080   to_sx = SX + (to_x + 1) * ed_tilesize - 1;
13081   to_sy = SY + (to_y + 1) * ed_tilesize - 1;
13082
13083   DrawSimpleWhiteLine(drawto, from_sx, from_sy, to_sx, from_sy);
13084   DrawSimpleWhiteLine(drawto, to_sx, from_sy, to_sx, to_sy);
13085   DrawSimpleWhiteLine(drawto, to_sx, to_sy, from_sx, to_sy);
13086   DrawSimpleWhiteLine(drawto, from_sx, to_sy, from_sx, from_sy);
13087
13088   if (from_x == to_x && from_y == to_y)
13089     MarkTileDirty(from_x/2, from_y/2);
13090   else
13091     redraw_mask |= REDRAW_FIELD;
13092 }
13093
13094 static void DrawAreaBox(int from_x, int from_y, int to_x, int to_y,
13095                         int element, boolean change_level)
13096 {
13097   DrawBox(from_x, from_y, to_x, to_y, element, change_level);
13098 }
13099
13100 static void SelectArea(int from_x, int from_y, int to_x, int to_y,
13101                        int element, boolean change_level)
13102 {
13103   if (element == -1 || change_level)
13104     DrawAreaBox(from_x, from_y, to_x, to_y, -1, FALSE);
13105   else
13106     DrawAreaBorder(from_x, from_y, to_x, to_y);
13107 }
13108
13109 // values for CopyBrushExt()
13110 #define CB_AREA_TO_BRUSH                0
13111 #define CB_BRUSH_TO_CURSOR              1
13112 #define CB_BRUSH_TO_LEVEL               2
13113 #define CB_DELETE_OLD_CURSOR            3
13114 #define CB_DUMP_BRUSH                   4
13115 #define CB_DUMP_BRUSH_SMALL             5
13116 #define CB_CLIPBOARD_TO_BRUSH           6
13117 #define CB_BRUSH_TO_CLIPBOARD           7
13118 #define CB_BRUSH_TO_CLIPBOARD_SMALL     8
13119 #define CB_UPDATE_BRUSH_POSITION        9
13120 #define CB_FLIP_BRUSH_X                 10
13121 #define CB_FLIP_BRUSH_Y                 11
13122 #define CB_FLIP_BRUSH_XY                12
13123
13124 #define MAX_CB_PART_SIZE        10
13125 #define MAX_CB_LINE_SIZE        (MAX_LEV_FIELDX + 1)    // text plus newline
13126 #define MAX_CB_NUM_LINES        (MAX_LEV_FIELDY)
13127 #define MAX_CB_TEXT_SIZE        (MAX_CB_LINE_SIZE *     \
13128                                  MAX_CB_NUM_LINES *     \
13129                                  MAX_CB_PART_SIZE)
13130
13131 static int getFlippedTileExt(int map[], int element)
13132 {
13133   int i;
13134
13135   for (i = 0; map[i] != -1; i++)
13136     if (map[i] == element)
13137       return map[i ^ 1];        // get flipped element by flipping LSB of index
13138
13139   return element;
13140 }
13141
13142 static int getFlippedTileX(int element)
13143 {
13144   int map[] =
13145   {
13146     EL_BD_BUTTERFLY_LEFT,               EL_BD_BUTTERFLY_RIGHT,
13147     EL_BD_FIREFLY_LEFT,                 EL_BD_FIREFLY_RIGHT,
13148     EL_BUG_LEFT,                        EL_BUG_RIGHT,
13149     EL_SPACESHIP_LEFT,                  EL_SPACESHIP_RIGHT,
13150     EL_PACMAN_LEFT,                     EL_PACMAN_RIGHT,
13151     EL_ARROW_LEFT,                      EL_ARROW_RIGHT,
13152     EL_MOLE_LEFT,                       EL_MOLE_RIGHT,
13153     EL_BALLOON_SWITCH_LEFT,             EL_BALLOON_SWITCH_RIGHT,
13154     EL_YAMYAM_LEFT,                     EL_YAMYAM_RIGHT,
13155     EL_SP_PORT_LEFT,                    EL_SP_PORT_RIGHT,
13156     EL_SP_GRAVITY_PORT_LEFT,            EL_SP_GRAVITY_PORT_RIGHT,
13157     EL_SP_GRAVITY_ON_PORT_LEFT,         EL_SP_GRAVITY_ON_PORT_RIGHT,
13158     EL_SP_GRAVITY_OFF_PORT_LEFT,        EL_SP_GRAVITY_OFF_PORT_RIGHT,
13159     EL_CONVEYOR_BELT_1_LEFT,            EL_CONVEYOR_BELT_1_RIGHT,
13160     EL_CONVEYOR_BELT_2_LEFT,            EL_CONVEYOR_BELT_2_RIGHT,
13161     EL_CONVEYOR_BELT_3_LEFT,            EL_CONVEYOR_BELT_3_RIGHT,
13162     EL_CONVEYOR_BELT_4_LEFT,            EL_CONVEYOR_BELT_4_RIGHT,
13163     EL_SPRING_LEFT,                     EL_SPRING_RIGHT,
13164     EL_SP_CHIP_LEFT,                    EL_SP_CHIP_RIGHT,
13165     EL_TUBE_VERTICAL_LEFT,              EL_TUBE_VERTICAL_RIGHT,
13166     EL_TUBE_LEFT_UP,                    EL_TUBE_RIGHT_UP,
13167     EL_TUBE_LEFT_DOWN,                  EL_TUBE_RIGHT_DOWN,
13168     EL_DC_STEELWALL_1_LEFT,             EL_DC_STEELWALL_1_RIGHT,
13169     EL_DC_STEELWALL_1_TOPLEFT,          EL_DC_STEELWALL_1_TOPRIGHT,
13170     EL_DC_STEELWALL_1_BOTTOMLEFT,       EL_DC_STEELWALL_1_BOTTOMRIGHT,
13171     EL_DC_STEELWALL_1_TOPLEFT_2,        EL_DC_STEELWALL_1_TOPRIGHT_2,
13172     EL_DC_STEELWALL_1_BOTTOMLEFT_2,     EL_DC_STEELWALL_1_BOTTOMRIGHT_2,
13173     EL_DC_STEELWALL_2_LEFT,             EL_DC_STEELWALL_2_RIGHT,
13174     EL_ACID_POOL_TOPLEFT,               EL_ACID_POOL_TOPRIGHT,
13175     EL_ACID_POOL_BOTTOMLEFT,            EL_ACID_POOL_BOTTOMRIGHT,
13176
13177     -1
13178   };
13179
13180   return getFlippedTileExt(map, element);
13181 }
13182
13183 static int getFlippedTileY(int element)
13184 {
13185   int map[] =
13186   {
13187     EL_BD_BUTTERFLY_UP,                 EL_BD_BUTTERFLY_DOWN,
13188     EL_BD_FIREFLY_UP,                   EL_BD_FIREFLY_DOWN,
13189     EL_BUG_UP,                          EL_BUG_DOWN,
13190     EL_SPACESHIP_UP,                    EL_SPACESHIP_DOWN,
13191     EL_PACMAN_UP,                       EL_PACMAN_DOWN,
13192     EL_ARROW_UP,                        EL_ARROW_DOWN,
13193     EL_MOLE_UP,                         EL_MOLE_DOWN,
13194     EL_BALLOON_SWITCH_UP,               EL_BALLOON_SWITCH_DOWN,
13195     EL_YAMYAM_UP,                       EL_YAMYAM_DOWN,
13196     EL_SP_PORT_UP,                      EL_SP_PORT_DOWN,
13197     EL_SP_GRAVITY_PORT_UP,              EL_SP_GRAVITY_PORT_DOWN,
13198     EL_SP_GRAVITY_ON_PORT_UP,           EL_SP_GRAVITY_ON_PORT_DOWN,
13199     EL_SP_GRAVITY_OFF_PORT_UP,          EL_SP_GRAVITY_OFF_PORT_DOWN,
13200     EL_SP_CHIP_TOP,                     EL_SP_CHIP_BOTTOM,
13201     EL_TUBE_HORIZONTAL_UP,              EL_TUBE_HORIZONTAL_DOWN,
13202     EL_TUBE_LEFT_UP,                    EL_TUBE_LEFT_DOWN,
13203     EL_TUBE_RIGHT_UP,                   EL_TUBE_RIGHT_DOWN,
13204     EL_DC_STEELWALL_1_TOP,              EL_DC_STEELWALL_1_BOTTOM,
13205     EL_DC_STEELWALL_1_TOPLEFT,          EL_DC_STEELWALL_1_BOTTOMLEFT,
13206     EL_DC_STEELWALL_1_TOPRIGHT,         EL_DC_STEELWALL_1_BOTTOMRIGHT,
13207     EL_DC_STEELWALL_1_TOPLEFT_2,        EL_DC_STEELWALL_1_BOTTOMLEFT_2,
13208     EL_DC_STEELWALL_1_TOPRIGHT_2,       EL_DC_STEELWALL_1_BOTTOMRIGHT_2,
13209     EL_DC_STEELWALL_2_TOP,              EL_DC_STEELWALL_2_BOTTOM,
13210     EL_EMC_WALL_1,                      EL_EMC_WALL_3,
13211
13212     -1
13213   };
13214
13215   return getFlippedTileExt(map, element);
13216 }
13217
13218 static int getFlippedTileXY(int element)
13219 {
13220   int map[] =
13221   {
13222     EL_BD_BUTTERFLY_LEFT,               EL_BD_BUTTERFLY_UP,
13223     EL_BD_BUTTERFLY_RIGHT,              EL_BD_BUTTERFLY_DOWN,
13224     EL_BD_FIREFLY_LEFT,                 EL_BD_FIREFLY_UP,
13225     EL_BD_FIREFLY_RIGHT,                EL_BD_FIREFLY_DOWN,
13226     EL_BUG_LEFT,                        EL_BUG_UP,
13227     EL_BUG_RIGHT,                       EL_BUG_DOWN,
13228     EL_SPACESHIP_LEFT,                  EL_SPACESHIP_UP,
13229     EL_SPACESHIP_RIGHT,                 EL_SPACESHIP_DOWN,
13230     EL_PACMAN_LEFT,                     EL_PACMAN_UP,
13231     EL_PACMAN_RIGHT,                    EL_PACMAN_DOWN,
13232     EL_ARROW_LEFT,                      EL_ARROW_UP,
13233     EL_ARROW_RIGHT,                     EL_ARROW_DOWN,
13234     EL_MOLE_LEFT,                       EL_MOLE_UP,
13235     EL_MOLE_RIGHT,                      EL_MOLE_DOWN,
13236     EL_BALLOON_SWITCH_LEFT,             EL_BALLOON_SWITCH_UP,
13237     EL_BALLOON_SWITCH_RIGHT,            EL_BALLOON_SWITCH_DOWN,
13238     EL_YAMYAM_LEFT,                     EL_YAMYAM_UP,
13239     EL_YAMYAM_RIGHT,                    EL_YAMYAM_DOWN,
13240     EL_SP_PORT_LEFT,                    EL_SP_PORT_UP,
13241     EL_SP_PORT_RIGHT,                   EL_SP_PORT_DOWN,
13242     EL_SP_GRAVITY_PORT_LEFT,            EL_SP_GRAVITY_PORT_UP,
13243     EL_SP_GRAVITY_PORT_RIGHT,           EL_SP_GRAVITY_PORT_DOWN,
13244     EL_SP_GRAVITY_ON_PORT_LEFT,         EL_SP_GRAVITY_ON_PORT_UP,
13245     EL_SP_GRAVITY_ON_PORT_RIGHT,        EL_SP_GRAVITY_ON_PORT_DOWN,
13246     EL_SP_GRAVITY_OFF_PORT_LEFT,        EL_SP_GRAVITY_OFF_PORT_UP,
13247     EL_SP_GRAVITY_OFF_PORT_RIGHT,       EL_SP_GRAVITY_OFF_PORT_DOWN,
13248     EL_SP_CHIP_LEFT,                    EL_SP_CHIP_TOP,
13249     EL_SP_CHIP_RIGHT,                   EL_SP_CHIP_BOTTOM,
13250     EL_TUBE_VERTICAL,                   EL_TUBE_HORIZONTAL,
13251     EL_TUBE_VERTICAL_LEFT,              EL_TUBE_HORIZONTAL_UP,
13252     EL_TUBE_VERTICAL_RIGHT,             EL_TUBE_HORIZONTAL_DOWN,
13253     EL_TUBE_LEFT_DOWN,                  EL_TUBE_RIGHT_UP,
13254     EL_DC_STEELWALL_1_LEFT,             EL_DC_STEELWALL_1_TOP,
13255     EL_DC_STEELWALL_1_RIGHT,            EL_DC_STEELWALL_1_BOTTOM,
13256     EL_DC_STEELWALL_1_HORIZONTAL,       EL_DC_STEELWALL_1_VERTICAL,
13257     EL_DC_STEELWALL_1_TOPRIGHT,         EL_DC_STEELWALL_1_BOTTOMLEFT,
13258     EL_DC_STEELWALL_1_TOPRIGHT_2,       EL_DC_STEELWALL_1_BOTTOMLEFT_2,
13259     EL_DC_STEELWALL_2_LEFT,             EL_DC_STEELWALL_2_TOP,
13260     EL_DC_STEELWALL_2_RIGHT,            EL_DC_STEELWALL_2_BOTTOM,
13261     EL_DC_STEELWALL_2_HORIZONTAL,       EL_DC_STEELWALL_2_VERTICAL,
13262     EL_EXPANDABLE_WALL_HORIZONTAL,      EL_EXPANDABLE_WALL_VERTICAL,
13263     EL_EXPANDABLE_STEELWALL_HORIZONTAL, EL_EXPANDABLE_STEELWALL_VERTICAL,
13264
13265     -1
13266   };
13267
13268   return getFlippedTileExt(map, element);
13269 }
13270
13271 static int getFlippedTile(int element, int mode)
13272 {
13273   if (IS_MM_ELEMENT(element))
13274   {
13275     // get MM game element
13276     element = map_element_RND_to_MM(element);
13277
13278     // get flipped game element
13279     element = (mode == CB_FLIP_BRUSH_X  ? getFlippedTileX_MM(element) :
13280                mode == CB_FLIP_BRUSH_Y  ? getFlippedTileY_MM(element) :
13281                mode == CB_FLIP_BRUSH_XY ? getFlippedTileXY_MM(element) :
13282                element);
13283
13284     // get RND game element again
13285     element = map_element_MM_to_RND(element);
13286   }
13287   else
13288   {
13289     // get flipped game element
13290     element = (mode == CB_FLIP_BRUSH_X  ? getFlippedTileX(element) :
13291                mode == CB_FLIP_BRUSH_Y  ? getFlippedTileY(element) :
13292                mode == CB_FLIP_BRUSH_XY ? getFlippedTileXY(element) :
13293                element);
13294   }
13295
13296   return element;
13297 }
13298
13299 static void SwapFlippedTiles(short *tile1, short *tile2, int mode)
13300 {
13301   // flip tiles
13302   short tile1_flipped = getFlippedTile(*tile1, mode);
13303   short tile2_flipped = getFlippedTile(*tile2, mode);
13304
13305   // swap tiles
13306   *tile1 = tile2_flipped;
13307   *tile2 = tile1_flipped;
13308 }
13309
13310 static void DrawBrushElement(int sx, int sy, int element, boolean change_level)
13311 {
13312   DrawLineElement(sx, sy, element, change_level);
13313 }
13314
13315 static void CopyBrushExt(int from_x, int from_y, int to_x, int to_y,
13316                          int button, int mode)
13317 {
13318   static short brush_buffer[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
13319   static int brush_width, brush_height;
13320   static int last_cursor_x = -1, last_cursor_y = -1;
13321   static boolean delete_old_brush = FALSE;
13322   int new_element = BUTTON_ELEMENT(button);
13323   int x, y;
13324
13325   if (mode == CB_DUMP_BRUSH ||
13326       mode == CB_DUMP_BRUSH_SMALL ||
13327       mode == CB_BRUSH_TO_CLIPBOARD ||
13328       mode == CB_BRUSH_TO_CLIPBOARD_SMALL)
13329   {
13330     if (edit_mode != ED_MODE_DRAWING)
13331       return;
13332
13333     char part[MAX_CB_PART_SIZE + 1] = "";
13334     char text[MAX_CB_TEXT_SIZE + 1] = "";
13335     int width  = (draw_with_brush ? brush_width  : lev_fieldx);
13336     int height = (draw_with_brush ? brush_height : lev_fieldy);
13337     char *format = "%s%03d";
13338
13339     for (y = 0; y < height; y++)
13340       for (x = 0; x < width; x++)
13341         if ((draw_with_brush ? brush_buffer[x][y] : Tile[x][y]) > 999)
13342           format = "%s%04d";
13343
13344     for (y = 0; y < height; y++)
13345     {
13346       for (x = 0; x < width; x++)
13347       {
13348         int element = (draw_with_brush ? brush_buffer[x][y] : Tile[x][y]);
13349         char *prefix = (mode == CB_DUMP_BRUSH ||
13350                         mode == CB_BRUSH_TO_CLIPBOARD ? "`" : "¸");
13351
13352         if (element >= NUM_FILE_ELEMENTS)
13353           element = EL_UNKNOWN;
13354
13355         // copy brush to level sketch text buffer for the R'n'D forum:
13356         // - large tiles: `xxx or `xxxx (0x60 ASCII)
13357         // - small tiles: Â¸xxx or Â¸xxxx (0xb8 ISO-8859-1, 0xc2b8 UTF-8)
13358         snprintf(part, MAX_CB_PART_SIZE + 1, format, prefix, element);
13359         strcat(text, part);
13360       }
13361
13362       strcat(text, "\n");
13363     }
13364
13365     if (mode == CB_BRUSH_TO_CLIPBOARD ||
13366         mode == CB_BRUSH_TO_CLIPBOARD_SMALL)
13367       SDL_SetClipboardText(text);
13368     else
13369       Print("%s", text);        // print brush data to console and log file
13370
13371     return;
13372   }
13373
13374   if (mode == CB_CLIPBOARD_TO_BRUSH)
13375   {
13376     if (edit_mode != ED_MODE_DRAWING)
13377       return;
13378
13379     if (!SDL_HasClipboardText())
13380     {
13381       Request("Clipboard is empty!", REQ_CONFIRM);
13382
13383       return;
13384     }
13385
13386     boolean copy_to_brush = (draw_with_brush ||
13387                              drawing_function == GADGET_ID_GRAB_BRUSH);
13388
13389     // this will delete the old brush, if already drawing with a brush
13390     if (copy_to_brush)
13391       ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS], MB_LEFTBUTTON);
13392
13393     // initialization is required for "odd" (incomplete) clipboard content
13394     for (x = 0; x < MAX_LEV_FIELDX; x++)
13395       for (y = 0; y < MAX_LEV_FIELDY; y++)
13396         brush_buffer[x][y] = EL_EMPTY;
13397
13398     brush_width  = 0;
13399     brush_height = 0;
13400     x = 0;
13401     y = 0;
13402
13403     char *clipboard_text = SDL_GetClipboardText();
13404     char *ptr = clipboard_text;
13405     boolean allow_new_row = FALSE;
13406     boolean stop = FALSE;
13407
13408     while (*ptr && !stop)
13409     {
13410       boolean prefix_found = FALSE;
13411       boolean start_new_row = FALSE;
13412
13413       // level sketch element number prefixes (may be multi-byte characters)
13414       char *prefix_list[] = { "`", "¸" };
13415       int i;
13416
13417       for (i = 0; i < ARRAY_SIZE(prefix_list); i++)
13418       {
13419         char *prefix = prefix_list[i];
13420
13421         // check if string is large enough for prefix
13422         if (strlen(ptr) < strlen(prefix))
13423         {
13424           stop = TRUE;
13425
13426           break;
13427         }
13428
13429         // check if string starts with prefix
13430         if (strPrefix(ptr, prefix))
13431         {
13432           ptr += strlen(prefix);
13433
13434           prefix_found = TRUE;
13435
13436           break;
13437         }
13438       }
13439
13440       // check if prefix found and followed by (at least) three digits
13441       if (prefix_found &&
13442           strlen(ptr) >= 3 &&
13443           ptr[0] >= '0' && ptr[0] <= '9' &&
13444           ptr[1] >= '0' && ptr[1] <= '9' &&
13445           ptr[2] >= '0' && ptr[2] <= '9')
13446       {
13447         int element = ((ptr[0] - '0') * 100 +
13448                        (ptr[1] - '0') * 10 +
13449                        (ptr[2] - '0'));
13450
13451         ptr += 3;
13452
13453         // level sketch element number might consist of four digits
13454         if (ptr[0] >= '0' && ptr[0] <= '9')
13455         {
13456           element = element * 10 + (ptr[0] - '0');
13457           ptr++;
13458         }
13459
13460         // remap some (historic, now obsolete) elements
13461         element = getMappedElement(element);
13462
13463         if (element >= NUM_FILE_ELEMENTS)
13464           element = EL_UNKNOWN;
13465
13466         brush_buffer[x][y] = element;
13467
13468         brush_width  = MAX(x + 1, brush_width);
13469         brush_height = MAX(y + 1, brush_height);
13470
13471         x++;
13472
13473         if (x >= MAX_LEV_FIELDX)
13474           start_new_row = TRUE;
13475
13476         allow_new_row = TRUE;
13477       }
13478       else
13479       {
13480         if ((*ptr == '\n' || *ptr == '\r') && allow_new_row)
13481           start_new_row = TRUE;
13482
13483         ptr++;          // !!! FIX THIS for real UTF-8 handling !!!
13484       }
13485
13486       if (start_new_row)
13487       {
13488         x = 0;
13489         y++;
13490
13491         if (y >= MAX_LEV_FIELDY)
13492           stop = TRUE;
13493
13494         allow_new_row = FALSE;
13495       }
13496     }
13497
13498     SDL_free(clipboard_text);
13499
13500     if (brush_width == 0 || brush_height == 0)
13501     {
13502       Request("No level sketch found in clipboard!", REQ_CONFIRM);
13503
13504       return;
13505     }
13506
13507     if (copy_to_brush)
13508     {
13509       struct GadgetInfo *gi = level_editor_gadget[GADGET_ID_DRAWING_LEVEL];
13510       int mx, my;
13511
13512       SDL_GetMouseState(&mx, &my);
13513
13514       // if inside drawing area, activate and draw brush at last mouse position
13515       if (mx >= gi->x && mx < gi->x + gi->width &&
13516           my >= gi->y && my < gi->y + gi->height)
13517         CopyBrushToCursor(last_cursor_x, last_cursor_y);
13518
13519       draw_with_brush = TRUE;
13520     }
13521     else
13522     {
13523       char request[100];
13524
13525       sprintf(request, "Replace level with %dx%d level sketch from clipboard?",
13526               brush_width, brush_height);
13527
13528       if (!Request(request, REQ_ASK))
13529         return;
13530
13531       for (x = 0; x < MAX_LEV_FIELDX; x++)
13532         for (y = 0; y < MAX_LEV_FIELDY; y++)
13533           Tile[x][y] = brush_buffer[x][y];
13534
13535       lev_fieldx = level.fieldx = brush_width;
13536       lev_fieldy = level.fieldy = brush_height;
13537
13538       boolean use_bd_engine = TRUE;
13539       boolean use_em_engine = TRUE;
13540       boolean use_sp_engine = TRUE;
13541       boolean use_mm_engine = TRUE;
13542
13543       for (x = 0; x < MAX_LEV_FIELDX; x++)
13544       {
13545         for (y = 0; y < MAX_LEV_FIELDY; y++)
13546         {
13547           int element = Tile[x][y];
13548
13549           if (!IS_BD_ELEMENT(element) && !IS_PLAYER_ELEMENT(element))
13550             use_bd_engine = FALSE;
13551
13552           if (!IS_EM_ELEMENT(element) && !IS_PLAYER_ELEMENT(element))
13553             use_em_engine = FALSE;
13554
13555           if (!IS_SP_ELEMENT(element))
13556             use_sp_engine = FALSE;
13557
13558           if (!IS_MM_ELEMENT(element) && element != EL_EMPTY)
13559             use_mm_engine = FALSE;
13560         }
13561       }
13562
13563       level.game_engine_type = (use_bd_engine ? GAME_ENGINE_TYPE_BD :
13564                                 use_em_engine ? GAME_ENGINE_TYPE_EM :
13565                                 use_sp_engine ? GAME_ENGINE_TYPE_SP :
13566                                 use_mm_engine ? GAME_ENGINE_TYPE_MM :
13567                                 GAME_ENGINE_TYPE_RND);
13568
13569       // update element selection list
13570       ReinitializeElementList();
13571       ModifyEditorElementList();
13572
13573       SetBorderElement();
13574
13575       DrawEditModeWindow();
13576       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13577     }
13578
13579     return;
13580   }
13581
13582   if (mode == CB_DELETE_OLD_CURSOR && !delete_old_brush)
13583     return;
13584
13585   if (mode == CB_AREA_TO_BRUSH)
13586   {
13587     int from_lx, from_ly;
13588
13589     if (from_x > to_x)
13590       swap_numbers(&from_x, &to_x);
13591
13592     if (from_y > to_y)
13593       swap_numbers(&from_y, &to_y);
13594
13595     brush_width = to_x - from_x + 1;
13596     brush_height = to_y - from_y + 1;
13597
13598     from_lx = from_x + level_xpos;
13599     from_ly = from_y + level_ypos;
13600
13601     for (y = 0; y < brush_height; y++)
13602     {
13603       for (x = 0; x < brush_width; x++)
13604       {
13605         brush_buffer[x][y] = Tile[from_lx + x][from_ly + y];
13606
13607         if (button != 1)
13608           DrawBrushElement(from_x + x, from_y + y, new_element, TRUE);
13609       }
13610     }
13611
13612     if (button != 1)
13613       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13614
13615     delete_old_brush = FALSE;
13616   }
13617   else if (mode == CB_BRUSH_TO_CURSOR || mode == CB_DELETE_OLD_CURSOR ||
13618            mode == CB_BRUSH_TO_LEVEL)
13619   {
13620     int cursor_x = (mode == CB_DELETE_OLD_CURSOR ? last_cursor_x : from_x);
13621     int cursor_y = (mode == CB_DELETE_OLD_CURSOR ? last_cursor_y : from_y);
13622     int cursor_from_x = cursor_x - brush_width / 2;
13623     int cursor_from_y = cursor_y - brush_height / 2;
13624     int border_from_x = cursor_x, border_from_y = cursor_y;
13625     int border_to_x = cursor_x, border_to_y = cursor_y;
13626
13627     if (mode != CB_DELETE_OLD_CURSOR && delete_old_brush)
13628       CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
13629
13630     if (!IN_ED_FIELD(cursor_x, cursor_y) ||
13631         !IN_LEV_FIELD(cursor_x + level_xpos, cursor_y + level_ypos))
13632     {
13633       delete_old_brush = FALSE;
13634
13635       return;
13636     }
13637
13638     for (y = 0; y < brush_height; y++)
13639     {
13640       for (x = 0; x < brush_width; x++)
13641       {
13642         int sx = cursor_from_x + x;
13643         int sy = cursor_from_y + y;
13644         int lx = sx + level_xpos;
13645         int ly = sy + level_ypos;
13646         boolean change_level = (mode == CB_BRUSH_TO_LEVEL);
13647         int element = (mode == CB_DELETE_OLD_CURSOR ? -1 :
13648                        mode == CB_BRUSH_TO_CURSOR || button == 1 ?
13649                        brush_buffer[x][y] : new_element);
13650
13651         if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
13652         {
13653           if (sx < border_from_x)
13654             border_from_x = sx;
13655           else if (sx > border_to_x)
13656             border_to_x = sx;
13657           if (sy < border_from_y)
13658             border_from_y = sy;
13659           else if (sy > border_to_y)
13660             border_to_y = sy;
13661
13662           DrawBrushElement(sx, sy, element, change_level);
13663         }
13664       }
13665     }
13666
13667     if (mode != CB_DELETE_OLD_CURSOR)
13668       DrawAreaBorder(border_from_x, border_from_y, border_to_x, border_to_y);
13669
13670     last_cursor_x = cursor_x;
13671     last_cursor_y = cursor_y;
13672
13673     delete_old_brush = TRUE;
13674   }
13675   else if (mode == CB_FLIP_BRUSH_X)
13676   {
13677     for (y = 0; y < brush_height; y++)
13678       for (x = 0; x < (brush_width + 1) / 2; x++)
13679         SwapFlippedTiles(&brush_buffer[x][y],
13680                          &brush_buffer[brush_width - x - 1][y], mode);
13681
13682     CopyBrushExt(last_cursor_x, last_cursor_y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13683   }
13684   else if (mode == CB_FLIP_BRUSH_Y)
13685   {
13686     for (y = 0; y < (brush_height + 1) / 2; y++)
13687       for (x = 0; x < brush_width; x++)
13688         SwapFlippedTiles(&brush_buffer[x][y],
13689                          &brush_buffer[x][brush_height - y - 1], mode);
13690
13691     CopyBrushExt(last_cursor_x, last_cursor_y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13692   }
13693   else if (mode == CB_FLIP_BRUSH_XY)
13694   {
13695     CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
13696
13697     for (y = 0; y < MAX(brush_width, brush_height); y++)
13698       for (x = 0; x <= y; x++)
13699         SwapFlippedTiles(&brush_buffer[x][y],
13700                          &brush_buffer[y][x], mode);
13701
13702     swap_numbers(&brush_width, &brush_height);
13703
13704     CopyBrushExt(last_cursor_x, last_cursor_y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13705   }
13706
13707   if (mode == CB_UPDATE_BRUSH_POSITION)
13708   {
13709     last_cursor_x = from_x;
13710     last_cursor_y = from_y;
13711   }
13712 }
13713
13714 static void CopyAreaToBrush(int from_x, int from_y, int to_x, int to_y,
13715                             int button)
13716 {
13717   CopyBrushExt(from_x, from_y, to_x, to_y, button, CB_AREA_TO_BRUSH);
13718 }
13719
13720 static void CopyBrushToLevel(int x, int y, int button)
13721 {
13722   CopyBrushExt(x, y, 0, 0, button, CB_BRUSH_TO_LEVEL);
13723 }
13724
13725 static void CopyBrushToCursor(int x, int y)
13726 {
13727   CopyBrushExt(x, y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13728 }
13729
13730 static void UpdateBrushPosition(int x, int y)
13731 {
13732   CopyBrushExt(x, y, 0, 0, 0, CB_UPDATE_BRUSH_POSITION);
13733 }
13734
13735 static void DeleteBrushFromCursor(void)
13736 {
13737   CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
13738 }
13739
13740 static void FlipBrushX(void)
13741 {
13742   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_X);
13743 }
13744
13745 static void FlipBrushY(void)
13746 {
13747   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_Y);
13748 }
13749
13750 static void RotateBrush(void)
13751 {
13752   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_XY);
13753   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_X);
13754 }
13755
13756 void DumpBrush(void)
13757 {
13758   CopyBrushExt(0, 0, 0, 0, 0, CB_DUMP_BRUSH);
13759 }
13760
13761 void DumpBrush_Small(void)
13762 {
13763   CopyBrushExt(0, 0, 0, 0, 0, CB_DUMP_BRUSH_SMALL);
13764 }
13765
13766 void CopyClipboardToBrush(void)
13767 {
13768   CopyBrushExt(0, 0, 0, 0, 0, CB_CLIPBOARD_TO_BRUSH);
13769 }
13770
13771 void CopyBrushToClipboard(void)
13772 {
13773   CopyBrushExt(0, 0, 0, 0, 0, CB_BRUSH_TO_CLIPBOARD);
13774 }
13775
13776 void CopyBrushToClipboard_Small(void)
13777 {
13778   CopyBrushExt(0, 0, 0, 0, 0, CB_BRUSH_TO_CLIPBOARD_SMALL);
13779 }
13780
13781 void UndoLevelEditorOperation(void)
13782 {
13783   ClickOnGadget(level_editor_gadget[GADGET_ID_UNDO], -1);
13784 }
13785
13786 void RedoLevelEditorOperation(void)
13787 {
13788   ClickOnGadget(level_editor_gadget[GADGET_ID_UNDO], 3);
13789 }
13790
13791 static void FloodFill(int from_x, int from_y, int fill_element)
13792 {
13793   FloodFillLevel(from_x, from_y, fill_element, Tile, lev_fieldx, lev_fieldy);
13794 }
13795
13796 static void FloodFillWall_MM(int from_sx2, int from_sy2, int fill_element)
13797 {
13798   int from_x = from_sx2 + 2 * level_xpos;
13799   int from_y = from_sy2 + 2 * level_ypos;
13800   int max_fillx = lev_fieldx * 2;
13801   int max_filly = lev_fieldy * 2;
13802   short Fill[max_fillx][max_filly];
13803   int x, y;
13804
13805   for (x = 0; x < max_fillx; x++)
13806     for (y = 0; y < max_filly; y++)
13807       Fill[x][y] = getLevelElementHiRes(x, y);
13808
13809   FloodFillLevelExt(from_x, from_y, fill_element, max_fillx, max_filly,
13810                     Fill, max_fillx, max_filly);
13811
13812   for (x = 0; x < max_fillx; x++)
13813     for (y = 0; y < max_filly; y++)
13814       if (Fill[x][y] == fill_element)
13815         SetLevelElementHiRes(x, y, Fill[x][y]);
13816 }
13817
13818 // values for DrawLevelText() modes
13819 #define TEXT_INIT               0
13820 #define TEXT_SETCURSOR          1
13821 #define TEXT_WRITECHAR          2
13822 #define TEXT_BACKSPACE          3
13823 #define TEXT_NEWLINE            4
13824 #define TEXT_END                5
13825 #define TEXT_QUERY_TYPING       6
13826
13827 static int DrawLevelText(int sx, int sy, char letter, int mode)
13828 {
13829   static short delete_buffer[MAX_LEV_FIELDX];
13830   static int start_sx;
13831   static int last_sx, last_sy;
13832   static boolean typing = FALSE;
13833   int letter_element;
13834   int lx = 0, ly = 0;
13835
13836   // map lower case letters to upper case and convert special characters
13837   if (letter >= 'a' && letter <= 'z')
13838     letter_element = EL_CHAR_ASCII0 + letter + (int)('A' - 'a');
13839   else if (letter == CHAR_BYTE_UMLAUT_a || letter == CHAR_BYTE_UMLAUT_A)
13840     letter_element = EL_CHAR_AUMLAUT;
13841   else if (letter == CHAR_BYTE_UMLAUT_o || letter == CHAR_BYTE_UMLAUT_O)
13842     letter_element = EL_CHAR_OUMLAUT;
13843   else if (letter == CHAR_BYTE_UMLAUT_u || letter == CHAR_BYTE_UMLAUT_U)
13844     letter_element = EL_CHAR_UUMLAUT;
13845   else if (letter == '^')
13846     letter_element = EL_CHAR_COPYRIGHT;
13847   else
13848     letter_element = EL_CHAR_ASCII0 + letter;
13849
13850   if (mode != TEXT_INIT)
13851   {
13852     if (!typing)
13853       return FALSE;
13854
13855     if (mode != TEXT_SETCURSOR)
13856     {
13857       sx = last_sx;
13858       sy = last_sy;
13859     }
13860
13861     lx = last_sx + level_xpos;
13862     ly = last_sy + level_ypos;
13863   }
13864
13865   switch (mode)
13866   {
13867     case TEXT_INIT:
13868       if (typing)
13869         DrawLevelText(0, 0, 0, TEXT_END);
13870
13871       typing = TRUE;
13872       start_sx = sx;
13873       last_sx = sx;
13874       last_sy = sy;
13875       DrawLevelText(sx, sy, 0, TEXT_SETCURSOR);
13876       break;
13877
13878     case TEXT_SETCURSOR:
13879       DrawEditorElement(last_sx, last_sy, Tile[lx][ly]);
13880       DrawAreaBorder(sx, sy, sx, sy);
13881       StartTextInput(SX + sx * ed_tilesize, SY + sy * ed_tilesize,
13882                      ed_tilesize, ed_tilesize);
13883       last_sx = sx;
13884       last_sy = sy;
13885       break;
13886
13887     case TEXT_WRITECHAR:
13888       if (letter_element >= EL_CHAR_START && letter_element <= EL_CHAR_END)
13889       {
13890         if (new_element1 >= EL_STEEL_CHAR_START &&
13891             new_element1 <= EL_STEEL_CHAR_END)
13892           letter_element = letter_element - EL_CHAR_START + EL_STEEL_CHAR_START;
13893
13894         delete_buffer[sx - start_sx] = Tile[lx][ly];
13895         Tile[lx][ly] = letter_element;
13896
13897         if (sx + 1 < ed_fieldx && lx + 1 < lev_fieldx)
13898           DrawLevelText(sx + 1, sy, 0, TEXT_SETCURSOR);
13899         else if (sy + 1 < ed_fieldy && ly + 1 < lev_fieldy)
13900           DrawLevelText(start_sx, sy + 1, 0, TEXT_SETCURSOR);
13901         else
13902           DrawLevelText(0, 0, 0, TEXT_END);
13903
13904         level.changed = TRUE;
13905       }
13906       break;
13907
13908     case TEXT_BACKSPACE:
13909       if (sx > start_sx)
13910       {
13911         Tile[lx - 1][ly] = delete_buffer[sx - start_sx - 1];
13912         DrawEditorElement(sx - 1, sy, Tile[lx - 1][ly]);
13913         DrawLevelText(sx - 1, sy, 0, TEXT_SETCURSOR);
13914       }
13915       break;
13916
13917     case TEXT_NEWLINE:
13918       if (sy + 1 < ed_fieldy && ly + 1 < lev_fieldy)
13919         DrawLevelText(start_sx, sy + 1, 0, TEXT_SETCURSOR);
13920       else
13921         DrawLevelText(0, 0, 0, TEXT_END);
13922       break;
13923
13924     case TEXT_END:
13925       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13926       DrawEditorElement(sx, sy, Tile[lx][ly]);
13927       StopTextInput();
13928       typing = FALSE;
13929       break;
13930
13931     case TEXT_QUERY_TYPING:
13932       break;
13933
13934     default:
13935       break;
13936   }
13937
13938   return typing;
13939 }
13940
13941 static void SetTextCursor(int unused_sx, int unused_sy, int sx, int sy,
13942                           int element, boolean change_level)
13943 {
13944   int lx = sx + level_xpos;
13945   int ly = sy + level_ypos;
13946
13947   if (element == -1)
13948     DrawEditorElement(sx, sy, Tile[lx][ly]);
13949   else
13950     DrawAreaBorder(sx, sy, sx, sy);
13951 }
13952
13953 static void CheckLevelBorderElement(boolean redraw_playfield)
13954 {
13955   int last_border_element = BorderElement;
13956
13957   SetBorderElement();
13958
13959   if (redraw_playfield && BorderElement != last_border_element)
13960     DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
13961 }
13962
13963 static void CopyLevelToUndoBuffer(int mode)
13964 {
13965   static boolean accumulated_undo = FALSE;
13966   boolean new_undo_buffer_position = TRUE;
13967   int x, y;
13968
13969   if (undo_buffer_steps == 0)
13970     accumulated_undo = FALSE;
13971
13972   switch (mode)
13973   {
13974     case UNDO_IMMEDIATE:
13975       accumulated_undo = FALSE;
13976       break;
13977
13978     case UNDO_ACCUMULATE:
13979       if (accumulated_undo)
13980         new_undo_buffer_position = FALSE;
13981       accumulated_undo = TRUE;
13982       break;
13983
13984     default:
13985       break;
13986   }
13987
13988   if (new_undo_buffer_position)
13989   {
13990     // advance position in undo buffer ring
13991     undo_buffer_position = (undo_buffer_position + 1) % NUM_UNDO_STEPS;
13992
13993     if (undo_buffer_steps < NUM_UNDO_STEPS - 1)
13994       undo_buffer_steps++;
13995   }
13996
13997   // always reset redo buffer when storing level change into undo buffer
13998   redo_buffer_steps = 0;
13999
14000   for (x = 0; x < lev_fieldx; x++)
14001     for (y = 0; y < lev_fieldy; y++)
14002       UndoBuffer[undo_buffer_position][x][y] = Tile[x][y];
14003
14004   // check if drawing operation forces change of border style
14005   CheckLevelBorderElement(TRUE);
14006
14007   level.changed = TRUE;
14008 }
14009
14010 static void RandomPlacement(int new_element)
14011 {
14012   static boolean free_position[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
14013   int num_free_positions = 0;
14014   int num_percentage, num_elements;
14015   int x, y;
14016
14017   ResetIntelliDraw();
14018
14019   // determine number of free positions for randomly placing the new element
14020   for (x = 0; x < lev_fieldx; x++) for (y = 0; y < lev_fieldy; y++)
14021   {
14022     free_position[x][y] =
14023       (random_placement_background_restricted ?
14024        Tile[x][y] == random_placement_background_element :
14025        Tile[x][y] != new_element);
14026
14027     if (free_position[x][y])
14028       num_free_positions++;
14029   }
14030
14031   // determine number of new elements to place there
14032   num_percentage = num_free_positions * random_placement_value / 100;
14033   num_elements = (random_placement_method == RANDOM_USE_PERCENTAGE ?
14034                   num_percentage : random_placement_value);
14035
14036   // if less free positions than elements to place, fill all these positions
14037   if (num_free_positions < num_elements)
14038   {
14039     for (x = 0; x < lev_fieldx; x++)
14040       for (y = 0; y < lev_fieldy; y++)
14041         if (free_position[x][y])
14042           SetElement(x, y, new_element);
14043   }
14044   else
14045   {
14046     while (num_elements > 0)
14047     {
14048       x = GetSimpleRandom(lev_fieldx);
14049       y = GetSimpleRandom(lev_fieldy);
14050
14051       // don't place element at the same position twice
14052       if (free_position[x][y])
14053       {
14054         free_position[x][y] = FALSE;
14055         SetElement(x, y, new_element);
14056         num_elements--;
14057       }
14058     }
14059   }
14060
14061   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14062   CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14063 }
14064
14065 static void WrapLevel(int dx, int dy)
14066 {
14067   int wrap_dx = lev_fieldx - dx;
14068   int wrap_dy = lev_fieldy - dy;
14069   int x, y;
14070
14071   for (x = 0; x < lev_fieldx; x++)
14072     for (y = 0; y < lev_fieldy; y++)
14073       TileBackup[x][y] = Tile[x][y];
14074
14075   for (x = 0; x < lev_fieldx; x++)
14076     for (y = 0; y < lev_fieldy; y++)
14077       Tile[x][y] =
14078         TileBackup[(x + wrap_dx) % lev_fieldx][(y + wrap_dy) % lev_fieldy];
14079
14080   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14081   CopyLevelToUndoBuffer(UNDO_ACCUMULATE);
14082 }
14083
14084 static void DrawAreaElementHighlight(boolean highlighted,
14085                                      boolean highlighted_similar)
14086 {
14087   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14088
14089   if (!highlighted)
14090     return;
14091
14092   int x, y;
14093
14094   for (x = 0; x < ed_fieldx; x++)
14095   {
14096     for (y = 0; y < ed_fieldy; y++)
14097     {
14098       boolean highlight = FALSE;
14099       int lx = x + level_xpos;
14100       int ly = y + level_ypos;
14101
14102       if (!IN_LEV_FIELD(lx, ly))
14103         continue;
14104
14105       // check if element is the same
14106       if (Tile[lx][ly] == new_element1)
14107         highlight = TRUE;
14108
14109       // check if element is similar
14110       if (highlighted_similar &&
14111           strEqual(element_info[Tile[lx][ly]].class_name,
14112                    element_info[new_element1].class_name))
14113         highlight = TRUE;
14114
14115       // check if element is matching MM style wall
14116       if (IS_MM_WALL(Tile[lx][ly]) &&
14117           map_mm_wall_element(Tile[lx][ly]) == new_element1)
14118         highlight = TRUE;
14119
14120       if (!highlight)
14121         continue;
14122
14123       if (IS_MM_WALL(Tile[lx][ly]) && !highlighted_similar)
14124       {
14125         int i;
14126
14127         for (i = 0; i < 4; i++)
14128         {
14129           if (!(MM_WALL_BITS(Tile[lx][ly]) & (1 << i)))
14130             continue;
14131
14132           int xx = x * 2 + (i % 2);
14133           int yy = y * 2 + (i / 2);
14134           int sx = SX + xx * ed_tilesize / 2;
14135           int sy = SY + yy * ed_tilesize / 2;
14136           int from_sx = sx;
14137           int from_sy = sy;
14138           int to_sx = sx + ed_tilesize / 2 - 1;
14139           int to_sy = sy + ed_tilesize / 2 - 1;
14140
14141           DrawSimpleWhiteLine(drawto, from_sx, from_sy, to_sx,   from_sy);
14142           DrawSimpleWhiteLine(drawto, to_sx,   from_sy, to_sx,   to_sy);
14143           DrawSimpleWhiteLine(drawto, to_sx,   to_sy,   from_sx, to_sy);
14144           DrawSimpleWhiteLine(drawto, from_sx, to_sy,   from_sx, from_sy);
14145         }
14146       }
14147       else
14148       {
14149         int sx = SX + x * ed_tilesize;
14150         int sy = SY + y * ed_tilesize;
14151         int from_sx = sx;
14152         int from_sy = sy;
14153         int to_sx = sx + ed_tilesize - 1;
14154         int to_sy = sy + ed_tilesize - 1;
14155
14156         DrawSimpleWhiteLine(drawto, from_sx, from_sy, to_sx,   from_sy);
14157         DrawSimpleWhiteLine(drawto, to_sx,   from_sy, to_sx,   to_sy);
14158         DrawSimpleWhiteLine(drawto, to_sx,   to_sy,   from_sx, to_sy);
14159         DrawSimpleWhiteLine(drawto, from_sx, to_sy,   from_sx, from_sy);
14160       }
14161     }
14162   }
14163 }
14164
14165 static void CopyLevelTemplateToUserLevelSet(char *levelset_subdir)
14166 {
14167   char *template_filename_old = getLocalLevelTemplateFilename();
14168   char *template_filename_new =
14169     getPath2(getUserLevelDir(levelset_subdir), LEVELTEMPLATE_FILENAME);
14170
14171   if (copyFile(template_filename_old, template_filename_new) != 0)
14172     Request("Cannot copy level template!", REQ_CONFIRM);
14173
14174   free(template_filename_new);
14175 }
14176
14177 static void HandleDrawingAreas(struct GadgetInfo *gi)
14178 {
14179   static boolean started_inside_drawing_area = FALSE;
14180   static int last_sx = -1;
14181   static int last_sy = -1;
14182   static int last_sx2 = -1;
14183   static int last_sy2 = -1;
14184   int id = gi->custom_id;
14185   int type_id = gi->custom_type_id;
14186   boolean button_press_event;
14187   boolean button_release_event;
14188   boolean inside_drawing_area = !gi->event.off_borders;
14189   boolean draw_level = (id == GADGET_ID_DRAWING_LEVEL);
14190   int actual_drawing_function;
14191   int button = gi->event.button;
14192   int new_element = BUTTON_ELEMENT(button);
14193   int sx = gi->event.x, sy = gi->event.y;
14194   int min_sx = 0, min_sy = 0;
14195   int max_sx = gi->drawing.area_xsize - 1, max_sy = gi->drawing.area_ysize - 1;
14196   int item_xsize = gi->drawing.item_xsize, item_ysize = gi->drawing.item_ysize;
14197   int mini_item_xsize = item_xsize / 2, mini_item_ysize = item_ysize / 2;
14198   int sx2 = gi->event.mx / mini_item_xsize;
14199   int sy2 = gi->event.my / mini_item_ysize;
14200   int dx = sx2 % 2;
14201   int dy = sy2 % 2;
14202   int lx = 0, ly = 0;
14203   int x, y;
14204
14205   button_press_event = (gi->event.type == GD_EVENT_PRESSED);
14206   button_release_event = (gi->event.type == GD_EVENT_RELEASED);
14207
14208   // make sure to stay inside drawing area boundaries
14209   sx = (sx < min_sx ? min_sx : sx > max_sx ? max_sx : sx);
14210   sy = (sy < min_sy ? min_sy : sy > max_sy ? max_sy : sy);
14211
14212   if (draw_level)
14213   {
14214     int min_lx = 0, min_ly = 0;
14215     int max_lx = lev_fieldx - 1, max_ly = lev_fieldy - 1;
14216
14217     // get positions inside level field
14218     lx = sx + level_xpos;
14219     ly = sy + level_ypos;
14220
14221     if (!IN_LEV_FIELD(lx, ly))
14222       inside_drawing_area = FALSE;
14223
14224     // make sure to stay inside level field boundaries
14225     lx = (lx < min_lx ? min_lx : lx > max_lx ? max_lx : lx);
14226     ly = (ly < min_ly ? min_ly : ly > max_ly ? max_ly : ly);
14227
14228     // correct drawing area positions accordingly
14229     sx = lx - level_xpos;
14230     sy = ly - level_ypos;
14231   }
14232
14233   // also correct MM wall-sized (double) drawing area positions accordingly
14234   if (sx2 / 2 < sx || sx2 / 2 > sx)
14235   {
14236     dx = (sx2 / 2 < sx ? 0 : 1);
14237     sx2 = sx * 2 + dx;
14238   }
14239   if (sy2 / 2 < sy || sy2 / 2 > sy)
14240   {
14241     dy = (sy2 / 2 < sy ? 0 : 1);
14242     sy2 = sy * 2 + dy;
14243   }
14244
14245   if (!button_press_event && !button_release_event)
14246   {
14247     int old_element = (IN_LEV_FIELD(lx, ly) ? Tile[lx][ly] : EL_UNDEFINED);
14248     boolean hires_drawing = (level.game_engine_type == GAME_ENGINE_TYPE_MM &&
14249                              isHiresTileElement(old_element) &&
14250                              isHiresDrawElement(new_element));
14251
14252     // prevent handling events for every pixel position when moving mouse
14253     if ((sx == last_sx && sy == last_sy && !hires_drawing) ||
14254         (sx2 == last_sx2 && sy2 == last_sy2))
14255       return;
14256   }
14257
14258   last_sx = sx;
14259   last_sy = sy;
14260   last_sx2 = sx2;
14261   last_sy2 = sy2;
14262
14263   if (button_press_event)
14264     started_inside_drawing_area = inside_drawing_area;
14265
14266   if (!started_inside_drawing_area)
14267     return;
14268
14269   if (!IS_VALID_BUTTON(button))
14270     return;
14271
14272   // handle info callback for each invocation of action callback
14273   gi->callback_info(gi);
14274
14275   // automatically switch to 'single item' drawing mode, if needed
14276   actual_drawing_function =
14277     (draw_level || drawing_function == GADGET_ID_PICK_ELEMENT ?
14278      drawing_function : GADGET_ID_SINGLE_ITEMS);
14279
14280   // clicking into drawing area with pressed Control key picks element
14281   if (GetKeyModState() & KMOD_Control)
14282   {
14283     last_drawing_function = drawing_function;
14284     actual_drawing_function = GADGET_ID_PICK_ELEMENT;
14285   }
14286
14287   if (GetKeyModState() & KMOD_Shift)
14288   {
14289     if (button_press_event || button_release_event)
14290       ResetIntelliDraw();
14291   }
14292
14293   SetDrawModeHiRes(-1);         // reset to normal draw mode
14294
14295   switch (actual_drawing_function)
14296   {
14297     case GADGET_ID_SINGLE_ITEMS:
14298       if (draw_level)
14299       {
14300         if (button_release_event)
14301         {
14302           CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14303
14304           if (edit_mode == ED_MODE_DRAWING && draw_with_brush &&
14305               !inside_drawing_area)
14306             DeleteBrushFromCursor();
14307
14308           break;
14309         }
14310
14311         if (draw_with_brush)
14312         {
14313           CopyBrushToLevel(sx, sy, button);
14314         }
14315         else
14316         {
14317           SetDrawModeHiRes(new_element);
14318
14319           if (IS_PLAYER_ELEMENT(new_element) || IS_MM_MCDUFFIN(new_element))
14320           {
14321             // remove player at old position
14322             for (y = 0; y < lev_fieldy; y++)
14323             {
14324               for (x = 0; x < lev_fieldx; x++)
14325               {
14326                 int old_element = Tile[x][y];
14327
14328                 if (IS_PLAYER_ELEMENT(old_element) &&
14329                     IS_PLAYER_ELEMENT(new_element))
14330                 {
14331                   int replaced_with_element =
14332                     (old_element == EL_SOKOBAN_FIELD_PLAYER &&
14333                      new_element == EL_PLAYER_1 ? EL_SOKOBAN_FIELD_EMPTY :
14334
14335                      old_element == EL_SOKOBAN_FIELD_PLAYER &&
14336                      new_element == old_element ? EL_SOKOBAN_FIELD_EMPTY :
14337
14338                      new_element == EL_SOKOBAN_FIELD_PLAYER &&
14339                      old_element == EL_PLAYER_1 ? EL_EMPTY :
14340
14341                      new_element >= EL_PLAYER_1 &&
14342                      new_element <= EL_PLAYER_4 &&
14343                      new_element == old_element ? EL_EMPTY :
14344
14345                      old_element);
14346
14347                   SetElement(x, y, replaced_with_element);
14348                 }
14349                 else if (IS_MM_MCDUFFIN(old_element) &&
14350                          IS_MM_MCDUFFIN(new_element))
14351                 {
14352                   // remove McDuffin at old position
14353                   SetElement(x, y, EL_EMPTY);
14354                 }
14355               }
14356             }
14357           }
14358
14359           SetElementButton(lx, ly, dx, dy, new_element, button);
14360         }
14361       }
14362       else if (!button_release_event)
14363       {
14364         int pos = sx * drawingarea_info[type_id].area_ysize + sy;
14365
14366         if (item_xsize == MINI_TILEX && item_ysize == MINI_TILEY)
14367           DrawMiniGraphicExt(drawto,
14368                              gi->x + sx * MINI_TILEX,
14369                              gi->y + sy * MINI_TILEY,
14370                              el2edimg(new_element));
14371         else
14372           DrawFixedGraphicExt(drawto,
14373                               gi->x + sx * TILEX,
14374                               gi->y + sy * TILEY,
14375                               el2edimg(new_element), 0);
14376
14377         if (id == GADGET_ID_CUSTOM_GRAPHIC)
14378           new_element = GFX_ELEMENT(new_element);
14379
14380         drawingarea_info[type_id].value[pos] = new_element;
14381
14382         CopyElementPropertiesToGame(properties_element);
14383
14384         if (id == GADGET_ID_CUSTOM_GRAPHIC)
14385         {
14386           UpdateCustomElementGraphicGadgets();
14387
14388           FrameCounter = 0;     // restart animation frame counter
14389         }
14390       }
14391       break;
14392
14393     case GADGET_ID_CONNECTED_ITEMS:
14394       {
14395         static int last_sx = -1;
14396         static int last_sy = -1;
14397
14398         if (button_release_event)
14399           CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14400
14401         SetDrawModeHiRes(new_element);
14402
14403         if (getDrawModeHiRes())
14404         {
14405           sx = sx2;
14406           sy = sy2;
14407         }
14408
14409         if (!button_press_event)
14410           DrawLine(last_sx, last_sy, sx, sy, new_element, TRUE);
14411
14412         last_sx = sx;
14413         last_sy = sy;
14414       }
14415       break;
14416
14417     case GADGET_ID_LINE:
14418     case GADGET_ID_ARC:
14419     case GADGET_ID_RECTANGLE:
14420     case GADGET_ID_FILLED_BOX:
14421       SetDrawModeHiRes(new_element);
14422
14423       if (getDrawModeHiRes())
14424       {
14425         sx = sx2;
14426         sy = sy2;
14427       }
14428       // FALLTHROUGH
14429     case GADGET_ID_GRAB_BRUSH:
14430     case GADGET_ID_TEXT:
14431       {
14432         static int last_sx = -1;
14433         static int last_sy = -1;
14434         static int start_sx = -1;
14435         static int start_sy = -1;
14436         void (*draw_func)(int, int, int, int, int, boolean);
14437
14438         if (drawing_function == GADGET_ID_LINE)
14439           draw_func = DrawLine;
14440         else if (drawing_function == GADGET_ID_ARC)
14441           draw_func = DrawArc;
14442         else if (drawing_function == GADGET_ID_RECTANGLE)
14443           draw_func = DrawBox;
14444         else if (drawing_function == GADGET_ID_FILLED_BOX)
14445           draw_func = DrawFilledBox;
14446         else if (drawing_function == GADGET_ID_GRAB_BRUSH)
14447           draw_func = SelectArea;
14448         else // (drawing_function == GADGET_ID_TEXT)
14449           draw_func = SetTextCursor;
14450
14451         if (button_press_event)
14452         {
14453           draw_func(sx, sy, sx, sy, new_element, FALSE);
14454           start_sx = last_sx = sx;
14455           start_sy = last_sy = sy;
14456
14457           if (drawing_function == GADGET_ID_TEXT)
14458             DrawLevelText(0, 0, 0, TEXT_END);
14459         }
14460         else if (button_release_event)
14461         {
14462           draw_func(start_sx, start_sy, sx, sy, new_element, TRUE);
14463           if (drawing_function == GADGET_ID_GRAB_BRUSH)
14464           {
14465             CopyAreaToBrush(start_sx, start_sy, sx, sy, button);
14466             CopyBrushToCursor(sx, sy);
14467             ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS],
14468                           MB_LEFTBUTTON);
14469             draw_with_brush = TRUE;
14470           }
14471           else if (drawing_function == GADGET_ID_TEXT)
14472             DrawLevelText(sx, sy, 0, TEXT_INIT);
14473           else
14474             CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14475         }
14476         else if (last_sx != sx || last_sy != sy)
14477         {
14478           draw_func(start_sx, start_sy, last_sx, last_sy, -1, FALSE);
14479           if (IS_MM_WALL_EDITOR(new_element))   // clear wall background
14480             draw_func(start_sx, start_sy, sx, sy, EL_EMPTY, FALSE);
14481           draw_func(start_sx, start_sy, sx, sy, new_element, FALSE);
14482           last_sx = sx;
14483           last_sy = sy;
14484         }
14485       }
14486       break;
14487
14488     case GADGET_ID_FLOOD_FILL:
14489       if (button_press_event && Tile[lx][ly] != new_element)
14490       {
14491         if (IS_MM_WALL_EDITOR(new_element))
14492           FloodFillWall_MM(sx2, sy2, new_element);
14493         else
14494           FloodFill(lx, ly, new_element);
14495
14496         DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14497         CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14498       }
14499       break;
14500
14501     case GADGET_ID_PICK_ELEMENT:
14502       if (button_release_event)
14503         ClickOnGadget(level_editor_gadget[last_drawing_function],
14504                       MB_LEFTBUTTON);
14505       else if (draw_level)
14506         PickDrawingElement(button, Tile[lx][ly]);
14507       else
14508       {
14509         int pos = sx * drawingarea_info[type_id].area_ysize + sy;
14510
14511         PickDrawingElement(button, drawingarea_info[type_id].value[pos]);
14512       }
14513
14514     default:
14515       break;
14516   }
14517
14518   // do not mark level as modified for certain non-level-changing gadgets
14519   if ((type_id >= ED_DRAWING_ID_EDITOR_FIRST &&
14520        type_id <= ED_DRAWING_ID_EDITOR_LAST) ||
14521       actual_drawing_function == GADGET_ID_GRAB_BRUSH ||
14522       actual_drawing_function == GADGET_ID_PICK_ELEMENT)
14523     return;
14524
14525   level.changed = TRUE;
14526 }
14527
14528 static void HandleCounterButtons(struct GadgetInfo *gi)
14529 {
14530   int gadget_id = gi->custom_id;
14531   int counter_id = gi->custom_type_id;
14532   int button = gi->event.button;
14533   int *counter_value = counterbutton_info[counter_id].value;
14534   int step = BUTTON_STEPSIZE(button) *
14535     (gadget_id == counterbutton_info[counter_id].gadget_id_down ? -1 : +1);
14536
14537   if (counter_id == ED_COUNTER_ID_SELECT_LEVEL)
14538   {
14539     boolean pressed = (gi->event.type == GD_EVENT_PRESSED);
14540     boolean released = (gi->event.type == GD_EVENT_RELEASED);
14541     boolean level_changed = LevelChanged();
14542
14543     if ((level_changed && pressed) || (!level_changed && released))
14544       return;
14545
14546     if (level_changed && !Request("Level has changed! Discard changes?",
14547                                   REQ_ASK))
14548     {
14549       if (gadget_id == counterbutton_info[counter_id].gadget_id_text)
14550         ModifyEditorCounterValue(counter_id, *counter_value);
14551
14552       return;
14553     }
14554   }
14555
14556   if (gadget_id == counterbutton_info[counter_id].gadget_id_text)
14557     *counter_value = gi->textinput.number_value;
14558   else
14559     ModifyEditorCounterValue(counter_id, *counter_value + step);
14560
14561   if (counter_id == ED_COUNTER_ID_SELECT_LEVEL)
14562   {
14563     int last_game_engine_type = level.game_engine_type;
14564
14565     LoadLevel(level_nr);
14566     LoadScore(level_nr);
14567
14568     SaveLevelSetup_SeriesInfo();
14569
14570     TapeErase();
14571
14572     ResetUndoBuffer();
14573     DrawEditModeWindow();
14574
14575     if (level.game_engine_type != last_game_engine_type)
14576     {
14577       // update element selection list
14578       ReinitializeElementList();
14579       ModifyEditorElementList();
14580     }
14581
14582     return;
14583   }
14584
14585   switch (counter_id)
14586   {
14587     case ED_COUNTER_ID_YAMYAM_CONTENT:
14588       DrawYamYamContentAreas();
14589       break;
14590
14591     case ED_COUNTER_ID_BALL_CONTENT:
14592       DrawMagicBallContentAreas();
14593       break;
14594
14595     case ED_COUNTER_ID_ANDROID_CONTENT:
14596       DrawAndroidElementArea();
14597       break;
14598
14599     case ED_COUNTER_ID_GROUP_CONTENT:
14600       DrawGroupElementArea();
14601       CopyGroupElementPropertiesToGame(properties_element);
14602       break;
14603
14604     case ED_COUNTER_ID_INVENTORY_SIZE:
14605       DrawPlayerInitialInventoryArea(properties_element);
14606       break;
14607
14608     case ED_COUNTER_ID_MM_BALL_CONTENT:
14609       DrawMMBallContentArea();
14610       break;
14611
14612     case ED_COUNTER_ID_ENVELOPE_XSIZE:
14613     case ED_COUNTER_ID_ENVELOPE_YSIZE:
14614       DrawEnvelopeTextArea(-1);
14615       break;
14616
14617     case ED_COUNTER_ID_LEVEL_XSIZE:
14618     case ED_COUNTER_ID_LEVEL_YSIZE:
14619       lev_fieldx = level.fieldx;
14620       lev_fieldy = level.fieldy;
14621
14622       // check if resizing of level results in change of border border
14623       SetBorderElement();
14624
14625       break;
14626
14627     default:
14628       break;
14629   }
14630
14631   if ((counter_id >= ED_COUNTER_ID_CUSTOM_FIRST &&
14632        counter_id <= ED_COUNTER_ID_CUSTOM_LAST) ||
14633       (counter_id >= ED_COUNTER_ID_CHANGE_FIRST &&
14634        counter_id <= ED_COUNTER_ID_CHANGE_LAST))
14635     CopyElementPropertiesToGame(properties_element);
14636
14637   // do not mark level as modified for certain non-level-changing gadgets
14638   if ((counter_id >= ED_COUNTER_ID_LEVELSET_FIRST &&
14639        counter_id <= ED_COUNTER_ID_LEVELSET_LAST) ||
14640       (counter_id >= ED_COUNTER_ID_EDITOR_FIRST &&
14641        counter_id <= ED_COUNTER_ID_EDITOR_LAST))
14642     return;
14643
14644   level.changed = TRUE;
14645 }
14646
14647 static void HandleTextInputGadgets(struct GadgetInfo *gi)
14648 {
14649   int type_id = gi->custom_type_id;
14650
14651   strcpy(textinput_info[type_id].value, gi->textinput.value);
14652
14653   if (type_id == ED_TEXTINPUT_ID_ELEMENT_NAME)
14654   {
14655     CopyElementPropertiesToGame(properties_element);
14656
14657     ModifyEditorElementList();  // update changed button info text
14658   }
14659
14660   // do not mark level as modified for certain non-level-changing gadgets
14661   if (type_id >= ED_TEXTINPUT_ID_LEVELSET_FIRST &&
14662       type_id <= ED_TEXTINPUT_ID_LEVELSET_LAST)
14663     return;
14664
14665   level.changed = TRUE;
14666 }
14667
14668 static void HandleTextAreaGadgets(struct GadgetInfo *gi)
14669 {
14670   int type_id = gi->custom_type_id;
14671
14672   strncpy(textarea_info[type_id].value, gi->textarea.value,
14673           MAX_ENVELOPE_TEXT_LEN);
14674   textarea_info[type_id].value[MAX_ENVELOPE_TEXT_LEN] = '\0';
14675
14676   level.changed = TRUE;
14677 }
14678
14679 static void HandleSelectboxGadgets(struct GadgetInfo *gi)
14680 {
14681   int type_id = gi->custom_type_id;
14682   int value_old = *selectbox_info[type_id].value;
14683   int value_new = selectbox_info[type_id].options[gi->selectbox.index].value;
14684
14685   *selectbox_info[type_id].value = value_new;
14686
14687   if (type_id == ED_SELECTBOX_ID_LEVELSET_SAVE_MODE)
14688   {
14689     DrawLevelConfigWindow();
14690   }
14691   else if (type_id == ED_SELECTBOX_ID_SELECT_CHANGE_PAGE)
14692   {
14693     element_info[properties_element].current_change_page = gi->selectbox.index;
14694
14695     DrawPropertiesWindow();
14696   }
14697   else if ((type_id >= ED_SELECTBOX_ID_CUSTOM_FIRST &&
14698             type_id <= ED_SELECTBOX_ID_CUSTOM_LAST) ||
14699            (type_id >= ED_SELECTBOX_ID_CHANGE_FIRST &&
14700             type_id <= ED_SELECTBOX_ID_CHANGE_LAST) ||
14701            (type_id == ED_SELECTBOX_ID_GROUP_CHOICE_MODE))
14702   {
14703     if (type_id == ED_SELECTBOX_ID_ACTION_TYPE)
14704     {
14705       // when changing action type, also check action mode and action arg
14706       if (value_old != value_new)
14707         setSelectboxSpecialActionVariablesIfNeeded();
14708
14709       DrawPropertiesChange();
14710     }
14711
14712     CopyElementPropertiesToGame(properties_element);
14713   }
14714   else if (type_id == ED_SELECTBOX_ID_GAME_ENGINE_TYPE)
14715   {
14716     // show or hide "engine" tabulator depending on game engine type
14717     DrawLevelConfigWindow();
14718
14719     // update element selection list depending on game engine type
14720     ReinitializeElementList();
14721     ModifyEditorElementList();
14722   }
14723   else if (type_id == ED_SELECTBOX_ID_BD_SCHEDULING_TYPE)
14724   {
14725     // update BD cycle delay counter gadgets depending on BD scheduling type
14726     DrawLevelConfigWindow();
14727   }
14728
14729   // do not mark level as modified for certain non-level-changing gadgets
14730   if (type_id == ED_SELECTBOX_ID_LEVELSET_SAVE_MODE ||
14731       type_id == ED_SELECTBOX_ID_SELECT_CHANGE_PAGE)
14732     return;
14733
14734   level.changed = TRUE;
14735 }
14736
14737 static void HandleTextbuttonGadgets(struct GadgetInfo *gi)
14738 {
14739   int type_id = gi->custom_type_id;
14740   int i;
14741
14742   if (type_id >= ED_TAB_BUTTON_ID_LEVELCONFIG_FIRST &&
14743       type_id <= ED_TAB_BUTTON_ID_LEVELCONFIG_LAST)
14744   {
14745     edit_mode_levelconfig = gi->custom_type_id;
14746
14747     DrawLevelConfigWindow();
14748   }
14749   else if (type_id >= ED_TAB_BUTTON_ID_PROPERTIES_FIRST &&
14750            type_id <= ED_TAB_BUTTON_ID_PROPERTIES_LAST)
14751   {
14752     edit_mode_properties = gi->custom_type_id;
14753
14754     DrawPropertiesWindow();
14755   }
14756   else if (type_id == ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1 ||
14757            type_id == ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2)
14758   {
14759     boolean new_template = !fileExists(getLocalLevelTemplateFilename());
14760
14761     // backup original "level.field" (needed to track playfield changes)
14762     CopyPlayfield(level.field, TileBackup);
14763
14764     // "SaveLevelTemplate()" uses "level.field", so copy editor playfield
14765     CopyPlayfield(Tile, level.field);
14766
14767     if (new_template ||
14768         Request("Save this template and kill the old?", REQ_ASK))
14769       SaveLevelTemplate();
14770
14771     if (new_template)
14772       Request("Template saved!", REQ_CONFIRM);
14773
14774     // restore original "level.field" (needed to track playfield changes)
14775     CopyPlayfield(TileBackup, level.field);
14776   }
14777   else if (type_id == ED_TEXTBUTTON_ID_SAVE_LEVELSET)
14778   {
14779     char *levelset_subdir = getLevelSubdirFromSaveMode(levelset_save_mode);
14780
14781     if (levelset_save_mode == LEVELSET_SAVE_MODE_UPDATE &&
14782         leveldir_current->readonly)
14783     {
14784       Request("This level set is read-only!", REQ_CONFIRM);
14785
14786       return;
14787     }
14788
14789     if (strEqual(levelset_name, ""))
14790     {
14791       Request("Please enter level set title!", REQ_CONFIRM);
14792
14793       return;
14794     }
14795
14796     if (strEqual(levelset_author, ""))
14797     {
14798       Request("Please enter level set author!", REQ_CONFIRM);
14799
14800       return;
14801     }
14802
14803     if (levelset_save_mode == LEVELSET_SAVE_MODE_UPDATE)
14804     {
14805       if (UpdateUserLevelSet(levelset_subdir,
14806                              levelset_name,
14807                              levelset_author,
14808                              levelset_num_levels))
14809       {
14810         Request("Level set updated!", REQ_CONFIRM);
14811       }
14812       else
14813       {
14814         Request("Updating level set failed!", REQ_CONFIRM);
14815       }
14816     }
14817     else if (levelset_save_mode == LEVELSET_SAVE_MODE_CREATE)
14818     {
14819       if (level.changed && !Request("Level has changed! Discard changes?",
14820                                      REQ_ASK))
14821         return;
14822
14823       if (CreateUserLevelSet(levelset_subdir,
14824                              levelset_name,
14825                              levelset_author,
14826                              levelset_num_levels,
14827                              levelset_use_levelset_artwork))
14828       {
14829         if (levelset_copy_level_template)
14830           CopyLevelTemplateToUserLevelSet(levelset_subdir);
14831
14832         Request("New level set created!", REQ_CONFIRM);
14833
14834         AddUserLevelSetToLevelInfo(levelset_subdir);
14835         ChangeEditorToLevelSet(levelset_subdir);
14836       }
14837       else
14838       {
14839         Request("Creating new level set failed!", REQ_CONFIRM);
14840       }
14841     }
14842   }
14843   else if (type_id == ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE &&
14844            custom_element.num_change_pages < MAX_CHANGE_PAGES)
14845   {
14846     struct ElementInfo *ei = &element_info[properties_element];
14847
14848     // when modifying custom element, ask for copying level template
14849     if (level.use_custom_template && !AskToCopyAndModifyLevelTemplate())
14850       return;
14851
14852     setElementChangePages(ei, ei->num_change_pages + 1);
14853
14854     // set new change page to be new current change page
14855     ei->current_change_page = ei->num_change_pages - 1;
14856     ei->change = &ei->change_page[ei->current_change_page];
14857
14858     setElementChangeInfoToDefaults(ei->change);
14859
14860     DrawPropertiesWindow();
14861
14862     level.changed = TRUE;
14863   }
14864   else if (type_id == ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE &&
14865            custom_element.num_change_pages > MIN_CHANGE_PAGES)
14866   {
14867     struct ElementInfo *ei = &element_info[properties_element];
14868
14869     // when modifying custom element, ask for copying level template
14870     if (level.use_custom_template && !AskToCopyAndModifyLevelTemplate())
14871       return;
14872
14873     // copy all change pages after change page to be deleted
14874     for (i = ei->current_change_page; i < ei->num_change_pages - 1; i++)
14875       ei->change_page[i] = ei->change_page[i + 1];
14876
14877     setElementChangePages(ei, ei->num_change_pages - 1);
14878
14879     DrawPropertiesWindow();
14880
14881     level.changed = TRUE;
14882   }
14883 }
14884
14885 static void HandleGraphicbuttonGadgets(struct GadgetInfo *gi)
14886 {
14887   int type_id = gi->custom_type_id;
14888
14889   if (type_id == ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE ||
14890       type_id == ED_GRAPHICBUTTON_ID_NEXT_CHANGE_PAGE)
14891   {
14892     struct ElementInfo *ei = &element_info[properties_element];
14893     int step = BUTTON_STEPSIZE(gi->event.button);
14894
14895     step *= (type_id == ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE ? -1 : +1);
14896     ei->current_change_page += step;
14897
14898     if (ei->current_change_page < 0)
14899       ei->current_change_page = 0;
14900     else if (ei->current_change_page >= ei->num_change_pages)
14901       ei->current_change_page = ei->num_change_pages - 1;
14902
14903     DrawPropertiesWindow();
14904   }
14905   else if (type_id == ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE ||
14906            type_id == ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE)
14907   {
14908     struct ElementInfo *ei = &element_info[properties_element];
14909     int current_change_page = ei->current_change_page;
14910
14911     if (type_id == ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE)
14912     {
14913       element_info[EL_INTERNAL_CLIPBOARD_CHANGE].change_page[0] =
14914         ei->change_page[current_change_page];
14915     }
14916     else if (type_id == ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE)
14917     {
14918       // when modifying custom element, ask for copying level template
14919       if (level.use_custom_template && !AskToCopyAndModifyLevelTemplate())
14920         return;
14921
14922       ei->change_page[current_change_page] =
14923         element_info[EL_INTERNAL_CLIPBOARD_CHANGE].change_page[0];
14924
14925       level.changed = TRUE;
14926     }
14927
14928     DrawPropertiesWindow();
14929   }
14930 }
14931
14932 static void HandleRadiobuttons(struct GadgetInfo *gi)
14933 {
14934   int type_id = gi->custom_type_id;
14935
14936   *radiobutton_info[type_id].value =
14937     radiobutton_info[type_id].checked_value;
14938
14939   // do not mark level as modified for certain non-level-changing gadgets
14940   if (type_id >= ED_RADIOBUTTON_ID_EDITOR_FIRST &&
14941       type_id <= ED_RADIOBUTTON_ID_EDITOR_LAST)
14942     return;
14943
14944   level.changed = TRUE;
14945 }
14946
14947 static void HandleCheckbuttons(struct GadgetInfo *gi)
14948 {
14949   int type_id = gi->custom_type_id;
14950
14951   *checkbutton_info[type_id].value ^= TRUE;
14952
14953   if (type_id == ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID ||
14954       type_id == ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID ||
14955       type_id == ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH ||
14956       (((type_id >= ED_CHECKBUTTON_ID_CUSTOM_FIRST &&
14957          type_id <= ED_CHECKBUTTON_ID_CUSTOM_LAST) ||
14958         (type_id >= ED_CHECKBUTTON_ID_CHANGE_FIRST &&
14959          type_id <= ED_CHECKBUTTON_ID_CHANGE_LAST)) &&
14960        type_id != ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1))
14961   {
14962     CopyElementPropertiesToGame(properties_element);
14963   }
14964
14965   if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC)
14966   {
14967     UpdateCustomElementGraphicGadgets();
14968   }
14969   else if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1 ||
14970            type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2)
14971   {
14972     boolean template_related_changes_found = FALSE;
14973     int i;
14974
14975     // check if any custom, group or empty elements have been changed
14976     for (i = 0; i < NUM_FILE_ELEMENTS; i++)
14977       if ((IS_CUSTOM_ELEMENT(i) ||
14978            IS_GROUP_ELEMENT(i) ||
14979            IS_EMPTY_ELEMENT(i)) &&
14980           element_info[i].modified_settings)
14981         template_related_changes_found = TRUE;
14982
14983     if (level.use_custom_template &&
14984         !fileExists(getGlobalLevelTemplateFilename()))
14985     {
14986       Request("No level template found!", REQ_CONFIRM);
14987
14988       level.use_custom_template = FALSE;
14989
14990       ModifyGadget(gi, GDI_CHECKED, FALSE, GDI_END);
14991
14992       return;
14993     }
14994
14995     if (level.use_custom_template &&
14996         template_related_changes_found &&
14997         !Request("Discard changes and use level template?", REQ_ASK))
14998     {
14999       level.use_custom_template = FALSE;
15000
15001       ModifyGadget(gi, GDI_CHECKED, FALSE, GDI_END);
15002
15003       return;
15004     }
15005
15006     if (!level.use_custom_template &&
15007         Request("Copy settings from level template?", REQ_ASK))
15008     {
15009       return;
15010     }
15011
15012     LoadLevelTemplate(level.use_custom_template ? -1 : level_nr);
15013
15014     DrawEditModeWindow();
15015   }
15016   else if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_3)
15017   {
15018     if (setup.editor.use_template_for_new_levels &&
15019         !fileExists(getGlobalLevelTemplateFilename()))
15020     {
15021       Request("No level template found!", REQ_CONFIRM);
15022
15023       return;
15024     }
15025
15026     if (setup.editor.use_template_for_new_levels &&
15027         level.changed &&
15028         !Request("Discard level and load template?", REQ_ASK))
15029     {
15030       return;
15031     }
15032
15033     if (!setup.editor.use_template_for_new_levels &&
15034         level.changed &&
15035         !Request("Discard level and use empty level?", REQ_ASK))
15036     {
15037       return;
15038     }
15039
15040     LoadLevel(level_nr);
15041     LoadScore(level_nr);
15042
15043     TapeErase();
15044
15045     ResetUndoBuffer();
15046     DrawEditModeWindow();
15047   }
15048   else if (type_id == ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS)
15049   {
15050     SetAutomaticNumberOfGemsNeeded();
15051   }
15052
15053   // do not mark level as modified for certain non-level-changing gadgets
15054   if ((type_id >= ED_CHECKBUTTON_ID_LEVELSET_FIRST &&
15055        type_id <= ED_CHECKBUTTON_ID_LEVELSET_LAST) ||
15056       (type_id >= ED_CHECKBUTTON_ID_EDITOR_FIRST &&
15057        type_id <= ED_CHECKBUTTON_ID_EDITOR_LAST &&
15058        type_id != ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2) ||
15059       type_id == ED_CHECKBUTTON_ID_STICK_ELEMENT)
15060     return;
15061
15062   level.changed = TRUE;
15063 }
15064
15065 static void HandleControlButtons(struct GadgetInfo *gi)
15066 {
15067   static int last_level_drawing_function = GADGET_ID_SINGLE_ITEMS;
15068   static int last_edit_mode = ED_MODE_DRAWING;
15069   static int last_custom_copy_mode = -1;
15070   static int last_button = 0;
15071   int id = gi->custom_id;
15072   int button = gi->event.button;
15073   int step = BUTTON_STEPSIZE(button);
15074   int new_element = BUTTON_ELEMENT(button);
15075   int last_properties_element = properties_element;
15076   int x, y;
15077
15078   if (edit_mode == ED_MODE_DRAWING && drawing_function == GADGET_ID_TEXT)
15079     DrawLevelText(0, 0, 0, TEXT_END);
15080
15081   if (id < ED_NUM_CTRL1_BUTTONS &&
15082       id != GADGET_ID_SINGLE_ITEMS &&
15083       id != GADGET_ID_PICK_ELEMENT &&
15084       edit_mode != ED_MODE_DRAWING &&
15085       drawing_function != GADGET_ID_PICK_ELEMENT &&
15086       !(GetKeyModState() & KMOD_Control))
15087     ChangeEditModeWindow(ED_MODE_DRAWING);
15088
15089   // element copy mode active, but no element button pressed => deactivate
15090   if (last_custom_copy_mode != -1 && id < ED_NUM_CTRL_BUTTONS)
15091     last_custom_copy_mode = -1;
15092
15093   // when showing palette on element buttons, change element of button used
15094   if (editor.palette.show_on_element_buttons &&
15095       id >= GADGET_ID_ELEMENT_LEFT && id <= GADGET_ID_ELEMENT_RIGHT)
15096   {
15097     last_button = id - GADGET_ID_ELEMENT_LEFT + 1;
15098
15099     id = GADGET_ID_PALETTE;
15100   }
15101
15102   switch (id)
15103   {
15104     case GADGET_ID_SCROLL_LEFT:
15105       if (level_xpos >= 0)
15106       {
15107         if (lev_fieldx < ed_fieldx - 2)
15108           break;
15109
15110         level_xpos -= step;
15111         if (level_xpos < -1)
15112           level_xpos = -1;
15113         if (button == 1)
15114           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_RIGHT);
15115         else
15116           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15117
15118         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
15119                      GDI_SCROLLBAR_ITEM_POSITION, level_xpos + 1, GDI_END);
15120       }
15121       break;
15122
15123     case GADGET_ID_SCROLL_RIGHT:
15124       if (level_xpos <= lev_fieldx - ed_fieldx)
15125       {
15126         if (lev_fieldx < ed_fieldx - 2)
15127           break;
15128
15129         level_xpos += step;
15130         if (level_xpos > lev_fieldx - ed_fieldx + 1)
15131           level_xpos = lev_fieldx - ed_fieldx + 1;
15132         if (button == 1)
15133           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_LEFT);
15134         else
15135           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15136
15137         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
15138                      GDI_SCROLLBAR_ITEM_POSITION, level_xpos + 1, GDI_END);
15139       }
15140       break;
15141
15142     case GADGET_ID_SCROLL_UP:
15143       if (level_ypos >= 0)
15144       {
15145         if (lev_fieldy < ed_fieldy - 2)
15146           break;
15147
15148         level_ypos -= step;
15149         if (level_ypos < -1)
15150           level_ypos = -1;
15151         if (button == 1)
15152           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_DOWN);
15153         else
15154           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15155
15156         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
15157                      GDI_SCROLLBAR_ITEM_POSITION, level_ypos + 1, GDI_END);
15158       }
15159       break;
15160
15161     case GADGET_ID_SCROLL_DOWN:
15162       if (level_ypos <= lev_fieldy - ed_fieldy)
15163       {
15164         if (lev_fieldy < ed_fieldy - 2)
15165           break;
15166
15167         level_ypos += step;
15168         if (level_ypos > lev_fieldy - ed_fieldy + 1)
15169           level_ypos = lev_fieldy - ed_fieldy + 1;
15170         if (button == 1)
15171           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_UP);
15172         else
15173           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15174
15175         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
15176                      GDI_SCROLLBAR_ITEM_POSITION, level_ypos + 1, GDI_END);
15177       }
15178       break;
15179
15180     case GADGET_ID_SCROLL_HORIZONTAL:
15181       level_xpos = gi->event.item_position - 1;
15182
15183       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15184       BackToFront();
15185
15186       break;
15187
15188     case GADGET_ID_SCROLL_VERTICAL:
15189       level_ypos = gi->event.item_position - 1;
15190
15191       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15192       BackToFront();
15193
15194       break;
15195
15196     case GADGET_ID_SCROLL_LIST_UP:
15197     case GADGET_ID_SCROLL_LIST_DOWN:
15198     case GADGET_ID_SCROLL_LIST_VERTICAL:
15199       if (id == GADGET_ID_SCROLL_LIST_VERTICAL)
15200         element_shift = gi->event.item_position * ED_ELEMENTLIST_BUTTONS_HORIZ;
15201       else
15202       {
15203         step *= (id == GADGET_ID_SCROLL_LIST_UP ? -1 : +1);
15204         element_shift += step * ED_ELEMENTLIST_BUTTONS_HORIZ;
15205
15206         if (element_shift < 0)
15207           element_shift = 0;
15208         if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
15209           element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
15210
15211         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15212                      GDI_SCROLLBAR_ITEM_POSITION,
15213                      element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15214       }
15215
15216       ModifyEditorElementList();
15217
15218       break;
15219
15220     case GADGET_ID_PROPERTIES:
15221       // always switch off element properties when they are already displayed
15222       last_properties_element = new_element;
15223     case GADGET_ID_ELEMENT_LEFT:
15224     case GADGET_ID_ELEMENT_MIDDLE:
15225     case GADGET_ID_ELEMENT_RIGHT:
15226       properties_element = (id == GADGET_ID_ELEMENT_LEFT   ? new_element1 :
15227                             id == GADGET_ID_ELEMENT_MIDDLE ? new_element2 :
15228                             id == GADGET_ID_ELEMENT_RIGHT  ? new_element3 :
15229                             new_element);
15230
15231       if (edit_mode != ED_MODE_PROPERTIES)
15232       {
15233         last_edit_mode = edit_mode;
15234
15235         ChangeEditModeWindow(ED_MODE_PROPERTIES);
15236
15237         last_level_drawing_function = drawing_function;
15238         ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS],
15239                       MB_LEFTBUTTON);
15240       }
15241       else if (properties_element != last_properties_element)
15242       {
15243         DrawEditModeWindow();
15244       }
15245       else
15246       {
15247         ChangeEditModeWindow(ED_MODE_DRAWING);
15248
15249         ClickOnGadget(level_editor_gadget[last_level_drawing_function],
15250                       MB_LEFTBUTTON);
15251       }
15252       break;
15253
15254     case GADGET_ID_PALETTE:
15255       if (edit_mode != ED_MODE_PALETTE)
15256       {
15257         last_edit_mode = edit_mode;
15258
15259         ChangeEditModeWindow(ED_MODE_PALETTE);
15260       }
15261       else
15262       {
15263         ChangeEditModeWindow(last_edit_mode);
15264       }
15265       break;
15266
15267     case GADGET_ID_WRAP_LEFT:
15268       WrapLevel(-step, 0);
15269       break;
15270
15271     case GADGET_ID_WRAP_RIGHT:
15272       WrapLevel(step, 0);
15273       break;
15274
15275     case GADGET_ID_WRAP_UP:
15276       WrapLevel(0, -step);
15277       break;
15278
15279     case GADGET_ID_WRAP_DOWN:
15280       WrapLevel(0, step);
15281       break;
15282
15283     case GADGET_ID_SINGLE_ITEMS:
15284     case GADGET_ID_CONNECTED_ITEMS:
15285     case GADGET_ID_LINE:
15286     case GADGET_ID_ARC:
15287     case GADGET_ID_TEXT:
15288     case GADGET_ID_RECTANGLE:
15289     case GADGET_ID_FILLED_BOX:
15290     case GADGET_ID_FLOOD_FILL:
15291     case GADGET_ID_GRAB_BRUSH:
15292     case GADGET_ID_PICK_ELEMENT:
15293       if (drawing_function != GADGET_ID_PICK_ELEMENT)
15294         last_drawing_function = drawing_function;
15295       drawing_function = id;
15296       draw_with_brush = FALSE;
15297       break;
15298
15299     case GADGET_ID_RANDOM_PLACEMENT:
15300       RandomPlacement(new_element);
15301       break;
15302
15303     case GADGET_ID_ZOOM:
15304       // zoom level editor tile size in or out (or reset to default size)
15305       ed_tilesize = (button == 1 ? ed_tilesize * 2 :
15306                      button == 2 ? ed_tilesize_default :
15307                      button == 3 ? ed_tilesize / 2 : ed_tilesize);
15308
15309       // when using touch device, cycle through all zoom tilesizes
15310       if (runtime.uses_touch_device && ed_tilesize > TILESIZE)
15311         ed_tilesize = MICRO_TILESIZE;
15312
15313       // limit zoom level by upper and lower bound
15314       ed_tilesize = MIN(MAX(MICRO_TILESIZE, ed_tilesize), TILESIZE);
15315
15316       InitZoomLevelSettings(ed_tilesize);
15317
15318       if (edit_mode == ED_MODE_DRAWING)
15319       {
15320         DrawDrawingWindow();
15321
15322         // redraw zoom gadget info text
15323         PrintEditorGadgetInfoText(level_editor_gadget[id]);
15324       }
15325
15326       // save current editor zoom tilesize
15327       SaveSetup_AutoSetup();
15328
15329       break;
15330
15331     case GADGET_ID_CUSTOM_COPY_FROM:
15332     case GADGET_ID_CUSTOM_COPY_TO:
15333     case GADGET_ID_CUSTOM_EXCHANGE:
15334       last_custom_copy_mode = id;
15335       last_drawing_function = drawing_function;
15336       break;
15337
15338     case GADGET_ID_CUSTOM_COPY:
15339       CopyCustomElement(properties_element, -1, id);
15340       break;
15341
15342     case GADGET_ID_CUSTOM_PASTE:
15343       CopyCustomElement(-1, properties_element, id);
15344       break;
15345
15346     case GADGET_ID_UNDO:
15347       if (button < 0)   // keep button value (even if modifier keys are pressed)
15348         button = -button;
15349       else if (button == 1 && GetKeyModState() & (KMOD_Shift | KMOD_Control))
15350         button = 3;
15351
15352       if (button == 1 && undo_buffer_steps == 0)
15353       {
15354         Request("Undo buffer empty!", REQ_CONFIRM);
15355
15356         break;
15357       }
15358       else if (button == 2)
15359       {
15360         break;
15361       }
15362       else if (button == 3 && redo_buffer_steps == 0)
15363       {
15364         Request("Redo buffer empty!", REQ_CONFIRM);
15365
15366         break;
15367       }
15368
15369       if (edit_mode != ED_MODE_DRAWING)
15370         ChangeEditModeWindow(ED_MODE_DRAWING);
15371
15372       if (button == 1)
15373       {
15374         // undo
15375
15376         undo_buffer_position =
15377           (undo_buffer_position - 1 + NUM_UNDO_STEPS) % NUM_UNDO_STEPS;
15378
15379         undo_buffer_steps--;
15380         redo_buffer_steps++;
15381       }
15382       else
15383       {
15384         // redo
15385
15386         undo_buffer_position = (undo_buffer_position + 1) % NUM_UNDO_STEPS;
15387
15388         undo_buffer_steps++;
15389         redo_buffer_steps--;
15390       }
15391
15392       for (x = 0; x < lev_fieldx; x++)
15393         for (y = 0; y < lev_fieldy; y++)
15394           Tile[x][y] = UndoBuffer[undo_buffer_position][x][y];
15395
15396       // check if undo operation forces change of border style
15397       CheckLevelBorderElement(FALSE);
15398
15399       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15400
15401       break;
15402
15403     case GADGET_ID_CONF:
15404       if (edit_mode != ED_MODE_LEVELCONFIG)
15405       {
15406         last_edit_mode = edit_mode;
15407
15408         ChangeEditModeWindow(ED_MODE_LEVELCONFIG);
15409       }
15410       else
15411       {
15412         ChangeEditModeWindow(ED_MODE_DRAWING);
15413       }
15414       break;
15415
15416     case GADGET_ID_CLEAR:
15417       if (edit_mode != ED_MODE_DRAWING)
15418         ChangeEditModeWindow(ED_MODE_DRAWING);
15419
15420       for (x = 0; x < MAX_LEV_FIELDX; x++)
15421         for (y = 0; y < MAX_LEV_FIELDY; y++)
15422           Tile[x][y] = (button == 1 ? EL_EMPTY : new_element);
15423
15424       CopyLevelToUndoBuffer(GADGET_ID_CLEAR);
15425
15426       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15427       break;
15428
15429     case GADGET_ID_SAVE:
15430     {
15431       // saving read-only levels into personal level set modifies global vars
15432       // "leveldir_current" and "level_nr"; restore them after saving level
15433       LevelDirTree *leveldir_former = leveldir_current;
15434       int level_nr_former = level_nr;
15435       char *level_filename;
15436       boolean new_level;
15437
15438       if (leveldir_current->readonly &&
15439           !PrepareSavingIntoPersonalLevelSet())
15440         break;
15441
15442       level_filename = getDefaultLevelFilename(level_nr);
15443       new_level = !fileExists(level_filename);
15444
15445       if (new_level ||
15446           Request("Save this level and kill the old?", REQ_ASK))
15447       {
15448         if (leveldir_former->readonly)
15449           ModifyLevelConfigForSavingIntoPersonalLevelSet(leveldir_former->name);
15450
15451         SetAutomaticNumberOfGemsNeeded();
15452
15453         CopyPlayfield(Tile, level.field);
15454         SaveLevel(level_nr);
15455
15456         level.changed = FALSE;
15457
15458         if (new_level)
15459         {
15460           char level_saved_msg[64];
15461
15462           if (leveldir_former->readonly)
15463             sprintf(level_saved_msg,
15464                     "Level saved as level %d into personal level set!",
15465                     level_nr);
15466           else
15467             strcpy(level_saved_msg, "Level saved!");
15468
15469           Request(level_saved_msg, REQ_CONFIRM);
15470         }
15471       }
15472
15473       // "cd" back to copied-from levelset (in case of saved read-only level)
15474       leveldir_current = leveldir_former;
15475       level_nr = level_nr_former;
15476
15477       break;
15478     }
15479
15480     case GADGET_ID_TEST:
15481       if (LevelChanged())
15482         level.game_version = GAME_VERSION_ACTUAL;
15483
15484       CopyPlayfield(level.field, TileBackup);
15485       CopyPlayfield(Tile, level.field);
15486
15487       CopyNativeLevel_RND_to_Native(&level);
15488
15489       UnmapLevelEditorGadgets();
15490       UndrawSpecialEditorDoor();
15491
15492       CloseDoor(DOOR_CLOSE_ALL);
15493
15494       // needed before playing if editor playfield area has different size
15495       ClearRectangle(drawto, REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
15496
15497       // redraw_mask = REDRAW_ALL;
15498
15499       level_editor_test_game = TRUE;
15500
15501       StartGameActions(FALSE, setup.autorecord, level.random_seed);
15502
15503       break;
15504
15505     case GADGET_ID_EXIT:
15506       RequestExitLevelEditor(TRUE, FALSE);  // if level has changed, ask user
15507       break;
15508
15509     default:
15510       if (id >= GADGET_ID_ELEMENTLIST_FIRST &&
15511           id <= GADGET_ID_ELEMENTLIST_LAST)
15512       {
15513         int element_position = id - GADGET_ID_ELEMENTLIST_FIRST;
15514
15515         new_element = editor_elements[element_position + element_shift];
15516
15517         if (IS_EDITOR_CASCADE(new_element))
15518         {
15519           int i;
15520
15521           for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
15522           {
15523             int *cascade_element = &(*editor_elements_info[i].headline_list)[0];
15524             boolean *cascade_value = editor_elements_info[i].setup_cascade_value;
15525
15526             if (*cascade_element == new_element)
15527             {
15528               *cascade_element = EL_CASCADE_TOGGLE(*cascade_element);
15529               *cascade_value = IS_EDITOR_CASCADE_ACTIVE(*cascade_element);
15530
15531               // update element selection list
15532               ReinitializeElementList();
15533               ModifyEditorElementList();
15534
15535               // update cascading gadget info text
15536               PrintEditorGadgetInfoText(level_editor_gadget[id]);
15537
15538               // save current editor cascading state
15539               SaveSetup_EditorCascade();
15540
15541               break;
15542             }
15543           }
15544
15545           break;
15546         }
15547
15548         if (last_custom_copy_mode != -1)
15549         {
15550           if (CopyCustomElement(properties_element, new_element,
15551                                 last_custom_copy_mode))
15552           {
15553             ClickOnGadget(level_editor_gadget[last_drawing_function],
15554                           MB_LEFTBUTTON);
15555
15556             last_custom_copy_mode = -1;
15557           }
15558
15559           break;
15560         }
15561
15562         // change element of button used to show palette
15563         if (editor.palette.show_on_element_buttons)
15564           button = last_button;
15565
15566         PickDrawingElement(button, new_element);
15567
15568         if (!stick_element_properties_window &&
15569             drawing_function != GADGET_ID_PICK_ELEMENT &&
15570             !(GetKeyModState() & KMOD_Control))
15571         {
15572           properties_element = new_element;
15573           if (edit_mode == ED_MODE_PROPERTIES)
15574             DrawPropertiesWindow();
15575         }
15576
15577         if (drawing_function == GADGET_ID_PICK_ELEMENT)
15578           ClickOnGadget(level_editor_gadget[last_drawing_function],
15579                         MB_LEFTBUTTON);
15580
15581         if (!use_permanent_palette)
15582           ChangeEditModeWindow(last_edit_mode);
15583       }
15584 #ifdef DEBUG
15585       else if (gi->event.type == GD_EVENT_PRESSED)
15586         Debug("editor", "default: HandleControlButtons: GD_EVENT_PRESSED(%d)", id);
15587       else if (gi->event.type == GD_EVENT_RELEASED)
15588         Debug("editor", "default: HandleControlButtons: GD_EVENT_RELEASED(%d)", id);
15589       else if (gi->event.type == GD_EVENT_MOVING)
15590         Debug("editor", "default: HandleControlButtons: GD_EVENT_MOVING(%d)", id);
15591       else
15592         Debug("editor", "default: HandleControlButtons: ? (id == %d)", id);
15593 #endif
15594       break;
15595   }
15596 }
15597
15598 void HandleLevelEditorKeyInput(Key key)
15599 {
15600   char letter = getCharFromKey(key);
15601
15602   if (drawing_function == GADGET_ID_TEXT &&
15603       DrawLevelText(0, 0, 0, TEXT_QUERY_TYPING) == TRUE)
15604   {
15605     if (letter)
15606       DrawLevelText(0, 0, letter, TEXT_WRITECHAR);
15607     else if (key == KSYM_Delete || key == KSYM_BackSpace)
15608       DrawLevelText(0, 0, 0, TEXT_BACKSPACE);
15609     else if (key == KSYM_Return)
15610       DrawLevelText(0, 0, 0, TEXT_NEWLINE);
15611     else if (key == KSYM_Escape)
15612       DrawLevelText(0, 0, 0, TEXT_END);
15613
15614     return;
15615   }
15616
15617   int id = GADGET_ID_NONE;
15618   int new_element_shift = element_shift;
15619   int step = ED_ELEMENTLIST_BUTTONS_VERT - 1;
15620   int button = MB_LEFTBUTTON;
15621   int i;
15622
15623   switch (key)
15624   {
15625     case KSYM_Left:
15626       id = GADGET_ID_SCROLL_LEFT;
15627       break;
15628     case KSYM_Right:
15629       id = GADGET_ID_SCROLL_RIGHT;
15630       break;
15631     case KSYM_Up:
15632       id = GADGET_ID_SCROLL_UP;
15633       break;
15634     case KSYM_Down:
15635       id = GADGET_ID_SCROLL_DOWN;
15636       break;
15637
15638     case KSYM_Page_Up:
15639     case KSYM_Page_Down:
15640       step *= (key == KSYM_Page_Up ? -1 : +1);
15641       element_shift += step * ED_ELEMENTLIST_BUTTONS_HORIZ;
15642
15643       if (element_shift < 0)
15644         element_shift = 0;
15645       if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
15646         element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
15647
15648       ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15649                    GDI_SCROLLBAR_ITEM_POSITION,
15650                    element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15651
15652       ModifyEditorElementList();
15653
15654       break;
15655
15656     case KSYM_Home:
15657     case KSYM_End:
15658       element_shift = (key == KSYM_Home ? 0 :
15659                        num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS);
15660
15661       ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15662                    GDI_SCROLLBAR_ITEM_POSITION,
15663                    element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15664
15665       ModifyEditorElementList();
15666
15667       break;
15668
15669     case KSYM_Insert:
15670     case KSYM_Delete:
15671
15672       // this is needed to prevent interference with running "True X-Mouse"
15673       if (GetKeyModStateFromEvents() & KMOD_Control)
15674         break;
15675
15676       // check for last or next editor cascade block in element list
15677       for (i = 0; i < num_editor_elements; i++)
15678       {
15679         if ((key == KSYM_Insert && i == element_shift) ||
15680             (key == KSYM_Delete && new_element_shift > element_shift))
15681           break;
15682
15683         // jump to next cascade block (or to start of element list)
15684         if (i == 0 || IS_EDITOR_CASCADE(editor_elements[i]))
15685           new_element_shift = i;
15686       }
15687
15688       if (i < num_editor_elements)
15689         element_shift = new_element_shift;
15690
15691       if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
15692         element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
15693
15694       ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15695                    GDI_SCROLLBAR_ITEM_POSITION,
15696                    element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15697
15698       ModifyEditorElementList();
15699
15700       break;
15701
15702     case KSYM_Escape:
15703       if (edit_mode == ED_MODE_DRAWING)
15704         RequestExitLevelEditor(setup.ask_on_escape_editor, TRUE);
15705       else if (edit_mode == ED_MODE_LEVELCONFIG)
15706         HandleControlButtons(level_editor_gadget[GADGET_ID_CONF]);
15707       else if (edit_mode == ED_MODE_PROPERTIES)
15708         HandleControlButtons(level_editor_gadget[GADGET_ID_PROPERTIES]);
15709       else if (edit_mode == ED_MODE_PALETTE)
15710         HandleControlButtons(level_editor_gadget[GADGET_ID_PALETTE]);
15711       else              // should never happen
15712         ChangeEditModeWindow(ED_MODE_DRAWING);
15713
15714       break;
15715
15716     default:
15717       break;
15718   }
15719
15720   if (id != GADGET_ID_NONE)
15721     ClickOnGadget(level_editor_gadget[id], button);
15722   else if (letter == '1' || letter == '?')
15723     ClickOnGadget(level_editor_gadget[GADGET_ID_ELEMENT_LEFT], button);
15724   else if (letter == '2')
15725     ClickOnGadget(level_editor_gadget[GADGET_ID_ELEMENT_MIDDLE], button);
15726   else if (letter == '3')
15727     ClickOnGadget(level_editor_gadget[GADGET_ID_ELEMENT_RIGHT], button);
15728   else if (letter == '.')
15729     ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS], button);
15730   else if (letter == 'U')
15731     ClickOnGadget(level_editor_gadget[GADGET_ID_UNDO], 3);
15732   else if (letter == '-' || key == KSYM_KP_Subtract)
15733     ClickOnGadget(level_editor_gadget[GADGET_ID_ZOOM], 3);
15734   else if (letter == '0' || key == KSYM_KP_0)
15735     ClickOnGadget(level_editor_gadget[GADGET_ID_ZOOM], 2);
15736   else if (letter == '+' || key == KSYM_KP_Add ||
15737            letter == '=')       // ("Shift-=" is "+" on US keyboards)
15738     ClickOnGadget(level_editor_gadget[GADGET_ID_ZOOM], 1);
15739   else if (key == KSYM_Return ||
15740            key == KSYM_space ||
15741            key == setup.shortcut.toggle_pause)
15742     ClickOnGadget(level_editor_gadget[GADGET_ID_TEST], button);
15743   else
15744     for (i = 0; i < ED_NUM_CTRL_BUTTONS; i++)
15745       if (letter && letter == controlbutton_info[i].shortcut)
15746         if (!anyTextGadgetActive())
15747           ClickOnGadget(level_editor_gadget[i], button);
15748
15749   if (draw_with_brush)
15750   {
15751     if (letter == 'x')
15752       FlipBrushX();
15753     else if (letter == 'y')
15754       FlipBrushY();
15755     else if (letter == 'z')
15756       RotateBrush();
15757   }
15758 }
15759
15760 static void HandleLevelEditorIdle_Properties(void)
15761 {
15762   int element_border = graphic_info[IMG_EDITOR_ELEMENT_BORDER].border_size;
15763   int x = editor.settings.element_graphic.x + element_border;
15764   int y = editor.settings.element_graphic.y + element_border;
15765   static DelayCounter action_delay = { 0 };
15766   int i;
15767
15768   action_delay.value = GameFrameDelay;
15769
15770   if (!DelayReached(&action_delay))
15771     return;
15772
15773   for (i = 0; i < ED_NUM_SELECTBOX; i++)
15774   {
15775     struct GadgetInfo *gi = level_editor_gadget[selectbox_info[i].gadget_id];
15776
15777     if (gi->mapped && gi->active && gi->selectbox.open)
15778       return;
15779   }
15780
15781   DrawEditorElementAnimation(SX + x, SY + y);
15782
15783   redraw_mask |= REDRAW_FIELD;
15784
15785   FrameCounter++;       // increase animation frame counter
15786 }
15787
15788 static void HandleLevelEditorIdle_Drawing(void)
15789 {
15790   static boolean last_highlighted = FALSE;
15791   static boolean last_highlighted_similar = FALSE;
15792   boolean highlighted = (GetKeyModState() & KMOD_Alt);
15793   boolean highlighted_similar = (GetKeyModState() & KMOD_Shift);
15794
15795   if (highlighted != last_highlighted ||
15796       (highlighted && highlighted_similar != last_highlighted_similar))
15797   {
15798     DrawAreaElementHighlight(highlighted, highlighted_similar);
15799
15800     redraw_mask |= REDRAW_FIELD;
15801   }
15802
15803   last_highlighted = highlighted;
15804   last_highlighted_similar = highlighted_similar;
15805 }
15806
15807 void HandleLevelEditorIdle(void)
15808 {
15809   if (edit_mode == ED_MODE_PROPERTIES)
15810     HandleLevelEditorIdle_Properties();
15811   else if (edit_mode == ED_MODE_DRAWING)
15812     HandleLevelEditorIdle_Drawing();
15813 }
15814
15815 static void ClearEditorGadgetInfoText(void)
15816 {
15817   DrawBackground(INFOTEXT_XPOS, INFOTEXT_YPOS, INFOTEXT_XSIZE, INFOTEXT_YSIZE);
15818 }
15819
15820 void PrintEditorGadgetInfoText(struct GadgetInfo *gi)
15821 {
15822   char infotext[MAX_OUTPUT_LINESIZE + 1];
15823   int max_infotext_len = getMaxInfoTextLength();
15824
15825   if (gi == NULL || strlen(gi->info_text) == 0)
15826     return;
15827
15828   strncpy(infotext, gi->info_text, max_infotext_len);
15829   infotext[max_infotext_len] = '\0';
15830
15831   if (gi->custom_id < ED_NUM_CTRL_BUTTONS)
15832   {
15833     int key = controlbutton_info[gi->custom_id].shortcut;
15834
15835     if (key)
15836     {
15837       char shortcut[MAX_OUTPUT_LINESIZE + 1];
15838
15839       if (gi->custom_id == GADGET_ID_SINGLE_ITEMS)
15840         sprintf(shortcut, " ('.' or '%c')", key);
15841       else if (gi->custom_id == GADGET_ID_PICK_ELEMENT)
15842         sprintf(shortcut, " ('%c' or 'Ctrl')", key);
15843       else if (gi->custom_id == GADGET_ID_TEST)
15844         sprintf(shortcut, " ('Enter' or 'Shift-%c')", key);
15845       else if (gi->custom_id == GADGET_ID_UNDO)
15846         sprintf(shortcut, " ('%c/Shift-U')", key);
15847       else if (gi->custom_id == GADGET_ID_ZOOM)
15848         sprintf(shortcut, " ('%c', '0', '-')", key);
15849       else
15850         sprintf(shortcut, " ('%s%c')",
15851                 (key >= 'A' && key <= 'Z' ? "Shift-" : ""), key);
15852
15853       if (strlen(infotext) + strlen(shortcut) <= max_infotext_len)
15854         strcat(infotext, shortcut);
15855     }
15856   }
15857
15858   DrawText(INFOTEXT_XPOS, INFOTEXT_YPOS, infotext, INFOTEXT_FONT);
15859 }
15860
15861 void HandleEditorGadgetInfoText(void *ptr)
15862 {
15863   struct GadgetInfo *gi = (struct GadgetInfo *)ptr;
15864
15865   if (game_status != GAME_MODE_EDITOR)
15866     return;
15867
15868   ClearEditorGadgetInfoText();
15869
15870   if (gi == NULL || gi->event.type == GD_EVENT_INFO_LEAVING)
15871     return;
15872
15873   // misuse this function to delete brush cursor, if needed
15874   if (edit_mode == ED_MODE_DRAWING && draw_with_brush)
15875     DeleteBrushFromCursor();
15876
15877   PrintEditorGadgetInfoText(gi);
15878 }
15879
15880 static void HandleDrawingAreaInfo(struct GadgetInfo *gi)
15881 {
15882   int id = gi->custom_id;
15883   int type_id = gi->custom_type_id;
15884   int sx = gi->event.x;
15885   int sy = gi->event.y;
15886   int lx = sx + level_xpos;
15887   int ly = sy + level_ypos;
15888   int min_sx = 0, min_sy = 0;
15889   int max_sx = gi->drawing.area_xsize - 1;
15890   int max_sy = gi->drawing.area_ysize - 1;
15891   int actual_drawing_function = drawing_function;
15892   int max_infotext_len = getMaxInfoTextLength();
15893   char infotext[MAX_OUTPUT_LINESIZE + 1];
15894
15895   infotext[0] = '\0';           // start with empty info text
15896
15897   // pressed Control key: simulate picking element
15898   if (GetKeyModState() & KMOD_Control)
15899     actual_drawing_function = GADGET_ID_PICK_ELEMENT;
15900
15901   ClearEditorGadgetInfoText();
15902
15903   if (gi->event.type == GD_EVENT_INFO_LEAVING)
15904     return;
15905
15906   // make sure to stay inside drawing area boundaries
15907   sx = (sx < min_sx ? min_sx : sx > max_sx ? max_sx : sx);
15908   sy = (sy < min_sy ? min_sy : sy > max_sy ? max_sy : sy);
15909
15910   if (id == GADGET_ID_DRAWING_LEVEL)
15911   {
15912     if (button_status)
15913     {
15914       int min_lx = 0, min_ly = 0;
15915       int max_lx = lev_fieldx - 1, max_ly = lev_fieldy - 1;
15916
15917       // get positions inside level field
15918       lx = sx + level_xpos;
15919       ly = sy + level_ypos;
15920
15921       // make sure to stay inside level field boundaries
15922       lx = (lx < min_lx ? min_lx : lx > max_lx ? max_lx : lx);
15923       ly = (ly < min_ly ? min_ly : ly > max_ly ? max_ly : ly);
15924
15925       // correct drawing area positions accordingly
15926       sx = lx - level_xpos;
15927       sy = ly - level_ypos;
15928     }
15929
15930     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
15931     {
15932       if (button_status)        // if (gi->state == GD_BUTTON_PRESSED)
15933       {
15934         static int start_lx = 0;
15935         static int start_ly = 0;
15936         char *text;
15937
15938         if (gi->event.type == GD_EVENT_PRESSED)
15939         {
15940           start_lx = lx;
15941           start_ly = ly;
15942         }
15943
15944         switch (actual_drawing_function)
15945         {
15946           case GADGET_ID_SINGLE_ITEMS:
15947             text = "Drawing single items";
15948             break;
15949           case GADGET_ID_CONNECTED_ITEMS:
15950             text = "Drawing connected items";
15951             break;
15952           case GADGET_ID_LINE:
15953             text = "Drawing line";
15954             break;
15955           case GADGET_ID_ARC:
15956             text = "Drawing arc";
15957             break;
15958           case GADGET_ID_TEXT:
15959             text = "Setting text cursor";
15960             break;
15961           case GADGET_ID_RECTANGLE:
15962             text = "Drawing rectangle";
15963             break;
15964           case GADGET_ID_FILLED_BOX:
15965             text = "Drawing filled box";
15966             break;
15967           case GADGET_ID_FLOOD_FILL:
15968             text = "Flood fill";
15969             break;
15970           case GADGET_ID_GRAB_BRUSH:
15971             text = "Grabbing brush";
15972             break;
15973           case GADGET_ID_PICK_ELEMENT:
15974             text = "Picking element";
15975             break;
15976
15977           default:
15978             text = "Drawing position";
15979             break;
15980         }
15981
15982         if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
15983           sprintf(infotext, "%s: %d, %d", text, lx, ly);
15984         else
15985           sprintf(infotext, "%s: %d, %d", text,
15986                   ABS(lx - start_lx) + 1, ABS(ly - start_ly) + 1);
15987       }
15988       else if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
15989         strncpy(infotext, getElementInfoText(Tile[lx][ly]), max_infotext_len);
15990       else
15991         sprintf(infotext, "Level position: %d, %d", lx, ly);
15992     }
15993
15994     // misuse this function to draw brush cursor, if needed
15995     if (edit_mode == ED_MODE_DRAWING && draw_with_brush && !button_status)
15996     {
15997       if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
15998         CopyBrushToCursor(sx, sy);
15999       else
16000         DeleteBrushFromCursor();
16001     }
16002
16003     if (!draw_with_brush)
16004       UpdateBrushPosition(sx, sy);
16005   }
16006   else if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
16007   {
16008     int pos = sx * drawingarea_info[type_id].area_ysize + sy;
16009     int element = drawingarea_info[type_id].value[pos];
16010
16011     strncpy(infotext, getElementInfoText(element), max_infotext_len);
16012   }
16013   else
16014   {
16015     if (id == GADGET_ID_CUSTOM_CONTENT)
16016       sprintf(infotext, "custom element content position: %d, %d", sx, sy);
16017     else if (id == GADGET_ID_GROUP_CONTENT)
16018       sprintf(infotext, "group element position: %d", sx + 1);
16019     else if (id >= GADGET_ID_YAMYAM_CONTENT_0 &&
16020              id <= GADGET_ID_YAMYAM_CONTENT_7)
16021       sprintf(infotext, "content area %d position: %d, %d",
16022               id - GADGET_ID_YAMYAM_CONTENT_0 + 1, sx, sy);
16023     else if (id >= GADGET_ID_MAGIC_BALL_CONTENT_0 &&
16024              id <= GADGET_ID_MAGIC_BALL_CONTENT_7)
16025       sprintf(infotext, "content area %d position: %d, %d",
16026               id - GADGET_ID_MAGIC_BALL_CONTENT_0 + 1, sx, sy);
16027     else if (id == GADGET_ID_ANDROID_CONTENT)
16028       sprintf(infotext, "android element position: %d", sx + 1);
16029     else if (drawingarea_info[type_id].infotext != NULL)
16030       strcpy(infotext, drawingarea_info[type_id].infotext);
16031   }
16032
16033   infotext[max_infotext_len] = '\0';
16034
16035   if (strlen(infotext) > 0)
16036     DrawTextS(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY, INFOTEXT_FONT, infotext);
16037 }
16038
16039 void RequestExitLevelEditor(boolean ask_if_level_has_changed,
16040                             boolean quick_quit)
16041 {
16042   if (!ask_if_level_has_changed ||
16043       !LevelChanged() ||
16044       Request("Level has changed! Exit without saving?",
16045               REQ_ASK | REQ_STAY_OPEN))
16046   {
16047     struct RectWithBorder *vp_door_1 = &viewport.door_1[GAME_MODE_MAIN];
16048     struct RectWithBorder *vp_door_2 = &viewport.door_2[GAME_MODE_MAIN];
16049
16050     // draw normal door
16051     UndrawSpecialEditorDoor();
16052
16053     // use door animation if door 1 viewport is unchanged and contains toolbox
16054     if (useEditorDoorAnimation())
16055       CloseDoor(DOOR_CLOSE_1 | DOOR_FORCE_ANIM);
16056
16057     // close editor doors if viewport definition is the same as in main menu
16058     if (vp_door_1->x      == DX     &&
16059         vp_door_1->y      == DY     &&
16060         vp_door_1->width  == DXSIZE &&
16061         vp_door_1->height == DYSIZE &&
16062         vp_door_2->x      == VX     &&
16063         vp_door_2->y      == VY     &&
16064         vp_door_2->width  == VXSIZE &&
16065         vp_door_2->height == VYSIZE)
16066       CloseDoor(DOOR_CLOSE_ALL | DOOR_NO_DELAY);
16067     else
16068       SetDoorState(DOOR_CLOSE_ALL);
16069
16070     BackToFront();
16071
16072     if (quick_quit)
16073       FadeSkipNextFadeIn();
16074
16075     SetGameStatus(GAME_MODE_MAIN);
16076
16077     DrawMainMenu();
16078   }
16079   else
16080   {
16081     if (!global.use_envelope_request)
16082     {
16083       CloseDoor(DOOR_CLOSE_1);
16084       OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
16085     }
16086   }
16087 }