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