white space changes
[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   if (IS_GEM(element) ||
10640       IS_CUSTOM_ELEMENT(element) ||
10641       IS_GROUP_ELEMENT(element) ||
10642       IS_EMPTY_ELEMENT(element) ||
10643       IS_BALLOON_ELEMENT(element) ||
10644       IS_ENVELOPE(element) ||
10645       IS_MM_ENVELOPE(element) ||
10646       IS_MM_MCDUFFIN(element) ||
10647       IS_DF_LASER(element) ||
10648       IS_PLAYER_ELEMENT(element) ||
10649       IS_BD_PLAYER_ELEMENT(element) ||
10650       IS_SOKOBAN_OBJECT_OR_FIELD(element) ||
10651       HAS_EDITOR_CONTENT(element) ||
10652       CAN_GROW(element) ||
10653       COULD_MOVE_INTO_ACID(element) ||
10654       MAYBE_DONT_COLLIDE_WITH(element))
10655     return TRUE;
10656   else
10657     for (i = 0; elements_with_counter[i].element != -1; i++)
10658       if (elements_with_counter[i].element == element)
10659         return TRUE;
10660
10661   return FALSE;
10662 }
10663
10664 static void SetAutomaticNumberOfGemsNeeded(void)
10665 {
10666   int x, y;
10667
10668   if (!level.auto_count_gems)
10669     return;
10670
10671   level.gems_needed = 0;
10672
10673   for (x = 0; x < lev_fieldx; x++)
10674   {
10675     for (y = 0; y < lev_fieldy; y++)
10676     {
10677       int element = Tile[x][y];
10678
10679       switch (element)
10680       {
10681         case EL_EMERALD:
10682         case EL_EMERALD_YELLOW:
10683         case EL_EMERALD_RED:
10684         case EL_EMERALD_PURPLE:
10685         case EL_BD_DIAMOND:
10686         case EL_WALL_EMERALD:
10687         case EL_WALL_EMERALD_YELLOW:
10688         case EL_WALL_EMERALD_RED:
10689         case EL_WALL_EMERALD_PURPLE:
10690         case EL_WALL_BD_DIAMOND:
10691         case EL_NUT:
10692         case EL_SP_INFOTRON:
10693         case EL_MM_KETTLE:
10694         case EL_DF_CELL:
10695           level.gems_needed++;
10696           break;
10697
10698         case EL_DIAMOND:
10699         case EL_WALL_DIAMOND:
10700           level.gems_needed += 3;
10701           break;
10702
10703         case EL_PEARL:
10704         case EL_WALL_PEARL:
10705           level.gems_needed += 5;
10706           break;
10707
10708         case EL_CRYSTAL:
10709         case EL_WALL_CRYSTAL:
10710           level.gems_needed += 8;
10711           break;
10712
10713         default:
10714           break;
10715       }
10716     }
10717   }
10718
10719   ModifyEditorCounterValue(ED_COUNTER_ID_LEVEL_GEMSLIMIT, level.gems_needed);
10720 }
10721
10722 static void DrawPropertiesConfig(void)
10723 {
10724   boolean draw_footer_line = FALSE;
10725   int max_num_element_counters = 4;
10726   int num_element_counters = 0;
10727   int i;
10728
10729   if (!checkPropertiesConfig(properties_element))
10730   {
10731     int xpos = ED_ELEMENT_SETTINGS_X(0);
10732     int ypos = ED_ELEMENT_SETTINGS_Y(0) + ED_GADGET_SMALL_DISTANCE;
10733
10734     PrintInfoText("No configuration options available.",
10735                   FONT_TEXT_1, xpos, ypos);
10736
10737     return;
10738   }
10739
10740   // check if there are elements where a value can be chosen for
10741   for (i = 0; elements_with_counter[i].element != -1; i++)
10742   {
10743     if (elements_with_counter[i].element == properties_element)
10744     {
10745       int counter_id = ED_COUNTER_ID_ELEMENT_VALUE1 + num_element_counters;
10746
10747       counterbutton_info[counter_id].y =
10748         ED_ELEMENT_SETTINGS_YPOS(
10749                 (HAS_EDITOR_CONTENT(properties_element)      ? 1 : 0) +
10750                 (CAN_GROW(properties_element)                ? 1 : 0) +
10751                 (COULD_MOVE_INTO_ACID(properties_element)    ? 1 : 0) +
10752                 (MAYBE_DONT_COLLIDE_WITH(properties_element) ? 1 : 0) +
10753                 (properties_element == EL_EMC_MAGIC_BALL     ? 2 : 0) +
10754                 num_element_counters);
10755
10756       counterbutton_info[counter_id].value = elements_with_counter[i].value;
10757       counterbutton_info[counter_id].text_right = elements_with_counter[i].text;
10758
10759       if (properties_element == EL_GAME_OF_LIFE ||
10760           properties_element == EL_BIOMAZE)
10761       {
10762         counterbutton_info[counter_id].min_value = 0;   // min neighbours
10763         counterbutton_info[counter_id].max_value = 8;   // max neighbours
10764       }
10765       else
10766       {
10767         // !!! CHANGE THIS FOR CERTAIN ELEMENTS !!!
10768         counterbutton_info[counter_id].min_value = MIN_SCORE;
10769         counterbutton_info[counter_id].max_value = MAX_SCORE;
10770       }
10771
10772       MapCounterButtons(counter_id);
10773
10774       num_element_counters++;
10775       if (num_element_counters >= max_num_element_counters)
10776         break;
10777     }
10778   }
10779
10780   if (HAS_EDITOR_CONTENT(properties_element))
10781   {
10782     // draw stickybutton gadget
10783     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
10784
10785     if (IS_AMOEBOID(properties_element))
10786       MapDrawingArea(ED_DRAWING_ID_AMOEBA_CONTENT);
10787     else if (properties_element == EL_YAMYAM ||
10788              properties_element == EL_YAMYAM_LEFT ||
10789              properties_element == EL_YAMYAM_RIGHT ||
10790              properties_element == EL_YAMYAM_UP ||
10791              properties_element == EL_YAMYAM_DOWN)
10792       DrawYamYamContentAreas();
10793     else if (properties_element == EL_EMC_MAGIC_BALL)
10794     {
10795       DrawMagicBallContentAreas();
10796
10797       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_RANDOM_BALL_CONTENT);
10798       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_INITIAL_BALL_ACTIVE);
10799     }
10800     else if (properties_element == EL_EMC_ANDROID)
10801       DrawAndroidElementArea();
10802     else if (properties_element == EL_MM_GRAY_BALL)
10803     {
10804       MapCounterButtons(ED_COUNTER_ID_MM_BALL_CONTENT);
10805       MapSelectboxGadget(ED_SELECTBOX_ID_MM_BALL_CHOICE_MODE);
10806       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_ROTATE_MM_BALL_CONTENT);
10807       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_EXPLODE_MM_BALL);
10808
10809       DrawMMBallContentArea();
10810     }
10811   }
10812
10813   if (IS_PLAYER_ELEMENT(properties_element))
10814   {
10815     int player_nr = GET_PLAYER_NR(properties_element);
10816
10817     // these properties can be set for every player individually
10818
10819     if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_1)
10820     {
10821       drawingarea_info[ED_DRAWING_ID_START_ELEMENT].value =
10822         &level.start_element[player_nr];
10823       drawingarea_info[ED_DRAWING_ID_ARTWORK_ELEMENT].value =
10824         &level.artwork_element[player_nr];
10825       drawingarea_info[ED_DRAWING_ID_EXPLOSION_ELEMENT].value =
10826         &level.explosion_element[player_nr];
10827
10828       checkbutton_info[ED_CHECKBUTTON_ID_USE_START_ELEMENT].value =
10829         &level.use_start_element[player_nr];
10830       checkbutton_info[ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT].value =
10831         &level.use_artwork_element[player_nr];
10832       checkbutton_info[ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT].value =
10833         &level.use_explosion_element[player_nr];
10834       checkbutton_info[ED_CHECKBUTTON_ID_INITIAL_GRAVITY].value =
10835         &level.initial_player_gravity[player_nr];
10836
10837       selectbox_info[ED_SELECTBOX_ID_PLAYER_SPEED].value =
10838         &level.initial_player_stepsize[player_nr];
10839
10840       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID);
10841       MapCheckbuttonGadget(properties_element == EL_SP_MURPHY ?
10842                            ED_CHECKBUTTON_ID_SP_BLOCK_LAST_FIELD :
10843                            ED_CHECKBUTTON_ID_BLOCK_LAST_FIELD);
10844       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BLOCK_SNAP_FIELD);
10845       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CONTINUOUS_SNAPPING);
10846       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_INSTANT_RELOCATION);
10847       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SHIFTED_RELOCATION);
10848       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_LAZY_RELOCATION);
10849       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_START_ELEMENT);
10850       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT);
10851       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT);
10852       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_INITIAL_GRAVITY);
10853       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CAN_PASS_TO_WALKABLE);
10854       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SOLVED_BY_ONE_PLAYER);
10855
10856       MapDrawingArea(ED_DRAWING_ID_START_ELEMENT);
10857       MapDrawingArea(ED_DRAWING_ID_ARTWORK_ELEMENT);
10858       MapDrawingArea(ED_DRAWING_ID_EXPLOSION_ELEMENT);
10859
10860       MapSelectboxGadget(ED_SELECTBOX_ID_PLAYER_SPEED);
10861     }
10862     else if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_2)
10863     {
10864       drawingarea_info[ED_DRAWING_ID_INVENTORY_CONTENT].value =
10865         &level.initial_inventory_content[player_nr][0];
10866
10867       counterbutton_info[ED_COUNTER_ID_INVENTORY_SIZE].value =
10868         &level.initial_inventory_size[player_nr];
10869
10870       checkbutton_info[ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY].value =
10871         &level.use_initial_inventory[player_nr];
10872
10873       // draw checkbutton gadgets
10874       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY);
10875       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_FINISH_DIG_COLLECT);
10876       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_KEEP_WALKABLE_CE);
10877
10878       // draw counter gadgets
10879       MapCounterButtons(ED_COUNTER_ID_INVENTORY_SIZE);
10880
10881       // draw drawing area gadgets
10882       DrawPlayerInitialInventoryArea(properties_element);
10883     }
10884   }
10885
10886   if (IS_BD_PLAYER_ELEMENT(properties_element))
10887     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BD_DIAGONAL_MOVEMENTS);
10888
10889   if (IS_GEM(properties_element))
10890   {
10891     checkbutton_info[ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS].y =
10892       ED_ELEMENT_SETTINGS_XPOS(properties_element == EL_BD_DIAMOND ? 2 : 1);
10893
10894     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS);
10895   }
10896
10897   if (properties_element == EL_EM_DYNAMITE)
10898     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_EM_EXPLODES_BY_FIRE);
10899
10900   if (COULD_MOVE_INTO_ACID(properties_element) &&
10901       !IS_PLAYER_ELEMENT(properties_element) &&
10902       (!IS_CUSTOM_ELEMENT(properties_element) ||
10903        edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_2))
10904   {
10905     // set position for checkbutton for "can move into acid"
10906     checkbutton_info[ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID].x =
10907       ED_ELEMENT_SETTINGS_XPOS(IS_CUSTOM_ELEMENT(properties_element) ? 1 : 0);
10908     checkbutton_info[ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID].y =
10909       ED_ELEMENT_SETTINGS_YPOS(IS_CUSTOM_ELEMENT(properties_element) ? 7 :
10910                                IS_BALLOON_ELEMENT(properties_element) ||
10911                                HAS_EDITOR_CONTENT(properties_element) ? 1 : 0);
10912
10913     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID);
10914   }
10915
10916   if (MAYBE_DONT_COLLIDE_WITH(properties_element))
10917     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH);
10918
10919   if (properties_element == EL_SPRING ||
10920       properties_element == EL_SPRING_LEFT ||
10921       properties_element == EL_SPRING_RIGHT)
10922     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_SPRING_BUG);
10923
10924   if (properties_element == EL_TIME_ORB_FULL)
10925     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_TIME_ORB_BUG);
10926
10927   if (properties_element == EL_GAME_OF_LIFE ||
10928       properties_element == EL_BIOMAZE)
10929     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_LIFE_BUGS);
10930
10931   if (CAN_GROW(properties_element))
10932   {
10933     checkbutton_info[ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE].y =
10934       ED_ELEMENT_SETTINGS_YPOS(HAS_EDITOR_CONTENT(properties_element) ? 1 : 0);
10935
10936     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE);
10937   }
10938
10939   if (properties_element == EL_SOKOBAN_FIELD_EMPTY)
10940     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SB_FIELDS_NEEDED);
10941
10942   if (properties_element == EL_SOKOBAN_OBJECT)
10943     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SB_OBJECTS_NEEDED);
10944
10945   if (properties_element == EL_SOKOBAN_OBJECT ||
10946       properties_element == EL_SOKOBAN_FIELD_EMPTY ||
10947       properties_element == EL_SOKOBAN_FIELD_FULL)
10948   {
10949     checkbutton_info[ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN].y =
10950       ED_ELEMENT_SETTINGS_XPOS(properties_element == EL_SOKOBAN_FIELD_FULL ?
10951                                0 : 1);
10952
10953     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN);
10954   }
10955
10956   if (IS_BALLOON_ELEMENT(properties_element))
10957     MapSelectboxGadget(ED_SELECTBOX_ID_WIND_DIRECTION);
10958
10959   if (IS_ENVELOPE(properties_element) ||
10960       IS_MM_ENVELOPE(properties_element))
10961   {
10962     int counter1_id = ED_COUNTER_ID_ENVELOPE_XSIZE;
10963     int counter2_id = ED_COUNTER_ID_ENVELOPE_YSIZE;
10964     int button1_id = ED_CHECKBUTTON_ID_ENVELOPE_AUTOWRAP;
10965     int button2_id = ED_CHECKBUTTON_ID_ENVELOPE_CENTERED;
10966     int envelope_nr = ENVELOPE_NR(properties_element);
10967
10968     counterbutton_info[counter1_id].value = &level.envelope[envelope_nr].xsize;
10969     counterbutton_info[counter2_id].value = &level.envelope[envelope_nr].ysize;
10970
10971     checkbutton_info[button1_id].value = &level.envelope[envelope_nr].autowrap;
10972     checkbutton_info[button2_id].value = &level.envelope[envelope_nr].centered;
10973
10974     // display counter to choose size of envelope text area
10975     MapCounterButtons(counter1_id);
10976     MapCounterButtons(counter2_id);
10977
10978     // display checkbuttons to choose auto-wrap and alignment properties
10979     MapCheckbuttonGadget(button1_id);
10980     MapCheckbuttonGadget(button2_id);
10981
10982     DrawEnvelopeTextArea(envelope_nr);
10983   }
10984
10985   if (IS_MM_MCDUFFIN(properties_element))
10986   {
10987     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_MM_LASER_RED);
10988     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_MM_LASER_GREEN);
10989     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_MM_LASER_BLUE);
10990   }
10991
10992   if (IS_DF_LASER(properties_element))
10993   {
10994     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DF_LASER_RED);
10995     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DF_LASER_GREEN);
10996     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DF_LASER_BLUE);
10997   }
10998
10999   if (IS_CUSTOM_ELEMENT(properties_element))
11000   {
11001     // draw stickybutton gadget
11002     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11003
11004     if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_1)
11005     {
11006       // draw checkbutton gadgets
11007       for (i =  ED_CHECKBUTTON_ID_CUSTOM1_FIRST;
11008            i <= ED_CHECKBUTTON_ID_CUSTOM1_LAST; i++)
11009         MapCheckbuttonGadget(i);
11010
11011       // draw counter gadgets
11012       for (i =  ED_COUNTER_ID_CUSTOM1_FIRST;
11013            i <= ED_COUNTER_ID_CUSTOM1_LAST; i++)
11014         MapCounterButtons(i);
11015
11016       // draw selectbox gadgets
11017       for (i =  ED_SELECTBOX_ID_CUSTOM1_FIRST;
11018            i <= ED_SELECTBOX_ID_CUSTOM1_LAST; i++)
11019         MapSelectboxGadget(i);
11020
11021       // draw textbutton gadgets
11022       MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1);
11023
11024       // draw text input gadgets
11025       MapTextInputGadget(ED_TEXTINPUT_ID_ELEMENT_NAME);
11026
11027       // draw drawing area gadgets
11028       MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
11029
11030       draw_footer_line = TRUE;
11031     }
11032     else if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_2)
11033     {
11034       // draw checkbutton gadgets
11035       for (i =  ED_CHECKBUTTON_ID_CUSTOM2_FIRST;
11036            i <= ED_CHECKBUTTON_ID_CUSTOM2_LAST; i++)
11037         MapCheckbuttonGadget(i);
11038
11039       // draw counter gadgets
11040       for (i =  ED_COUNTER_ID_CUSTOM2_FIRST;
11041            i <= ED_COUNTER_ID_CUSTOM2_LAST; i++)
11042         MapCounterButtons(i);
11043
11044       // draw selectbox gadgets
11045       for (i =  ED_SELECTBOX_ID_CUSTOM2_FIRST;
11046            i <= ED_SELECTBOX_ID_CUSTOM2_LAST; i++)
11047         MapSelectboxGadget(i);
11048
11049       // draw drawing area gadgets
11050       MapDrawingArea(ED_DRAWING_ID_CUSTOM_MOVE_ENTER);
11051       MapDrawingArea(ED_DRAWING_ID_CUSTOM_MOVE_LEAVE);
11052       DrawCustomContentArea();
11053     }
11054   }
11055   else if (IS_GROUP_ELEMENT(properties_element))
11056   {
11057     // draw stickybutton gadget
11058     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11059
11060     // draw checkbutton gadgets
11061     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC);
11062     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1);
11063
11064     // draw counter gadgets
11065     MapCounterButtons(ED_COUNTER_ID_GROUP_CONTENT);
11066
11067     // draw selectbox gadgets
11068     MapSelectboxGadget(ED_SELECTBOX_ID_GROUP_CHOICE_MODE);
11069
11070     // draw textbutton gadgets
11071     MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1);
11072
11073     // draw drawing area gadgets
11074     DrawGroupElementArea();
11075
11076     // draw text input gadgets
11077     MapTextInputGadget(ED_TEXTINPUT_ID_ELEMENT_NAME);
11078
11079     // draw drawing area gadgets
11080     MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
11081
11082     draw_footer_line = TRUE;
11083   }
11084   else if (IS_EMPTY_ELEMENT(properties_element))
11085   {
11086     // draw stickybutton gadget
11087     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11088
11089     // draw checkbutton gadgets
11090     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC);
11091     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1);
11092
11093     // draw textbutton gadgets
11094     MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1);
11095
11096     // draw drawing area gadgets
11097     MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
11098
11099     draw_footer_line = TRUE;
11100   }
11101
11102   // draw little footer border line above CE/GE use/save template gadgets
11103   if (draw_footer_line)
11104   {
11105     struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_PROPERTIES_INFO];
11106     struct GadgetDesign *gd = &gd_gi1->alt_design[GD_BUTTON_UNPRESSED];
11107     int gd_x = gd->x + gd_gi1->border.width / 2;
11108     int gd_y = gd->y + gd_gi1->height - 1;
11109     Pixel tab_color = GetPixel(gd->bitmap, gd_x, gd_y);
11110
11111     if (tab_color != BLACK_PIXEL)               // black => transparent
11112       FillRectangle(drawto,
11113                     SX + ED_ELEMENT_SETTINGS_X(0),
11114                     SY + ED_ELEMENT_SETTINGS_Y(14) - ED_SETTINGS_TABS_YOFFSET -
11115                     ED_TAB_BAR_HEIGHT,
11116                     getTabulatorBarWidth(), getTabulatorBarHeight(), tab_color);
11117   }
11118 }
11119
11120 static void DrawPropertiesChangeDrawingAreas(void)
11121 {
11122   if (IS_CUSTOM_ELEMENT(properties_element))
11123   {
11124     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_TARGET);
11125     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_TRIGGER);
11126     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_ACTION);
11127
11128     DrawCustomChangeContentArea();
11129   }
11130
11131   redraw_mask |= REDRAW_FIELD;
11132 }
11133
11134 static void DrawPropertiesChange(void)
11135 {
11136   int i;
11137
11138   // needed to initially set selectbox options for special action options
11139   setSelectboxSpecialActionOptions();
11140
11141   // draw stickybutton gadget
11142   MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11143
11144   // draw checkbutton gadgets
11145   for (i =  ED_CHECKBUTTON_ID_CHANGE_FIRST;
11146        i <= ED_CHECKBUTTON_ID_CHANGE_LAST; i++)
11147     MapCheckbuttonGadget(i);
11148
11149   // draw counter gadgets
11150   for (i =  ED_COUNTER_ID_CHANGE_FIRST;
11151        i <= ED_COUNTER_ID_CHANGE_LAST; i++)
11152     MapCounterButtons(i);
11153
11154   // draw selectbox gadgets
11155   for (i =  ED_SELECTBOX_ID_CHANGE_FIRST;
11156        i <= ED_SELECTBOX_ID_CHANGE_LAST; i++)
11157     MapSelectboxGadget(i);
11158
11159   // draw textbutton gadgets
11160   for (i =  ED_TEXTBUTTON_ID_CHANGE_FIRST;
11161        i <= ED_TEXTBUTTON_ID_CHANGE_LAST; i++)
11162     MapTextbuttonGadget(i);
11163
11164   // draw graphicbutton gadgets
11165   for (i =  ED_GRAPHICBUTTON_ID_CHANGE_FIRST;
11166        i <= ED_GRAPHICBUTTON_ID_CHANGE_LAST; i++)
11167     MapGraphicbuttonGadget(i);
11168
11169   // draw drawing area gadgets
11170   DrawPropertiesChangeDrawingAreas();
11171 }
11172
11173 static void DrawEditorElementAnimation(int x, int y)
11174 {
11175   int graphic = el2img(properties_element);
11176   int frame = (ANIM_MODE(graphic) == ANIM_CE_VALUE ?
11177                custom_element.ce_value_fixed_initial :
11178                ANIM_MODE(graphic) == ANIM_CE_SCORE ?
11179                custom_element.collect_score_initial : FrameCounter);
11180
11181   DrawFixedGraphicAnimationExt(drawto, x, y, graphic, frame, NO_MASKING);
11182 }
11183
11184 static void DrawEditorElementName(int x, int y, int font_nr)
11185 {
11186   char *element_name = getElementInfoText(properties_element);
11187   int font_width = getFontWidth(font_nr);
11188   int font_height = getFontHeight(font_nr);
11189   int max_text_width = SXSIZE - x - ED_ELEMENT_SETTINGS_X(0);
11190   int max_chars_per_line = max_text_width / font_width;
11191
11192   if (strlen(element_name) <= max_chars_per_line)
11193     DrawTextS(x, y, font_nr, element_name);
11194   else
11195   {
11196     char buffer[max_chars_per_line + 1];
11197     int next_pos = max_chars_per_line;
11198
11199     strncpy(buffer, element_name, max_chars_per_line);
11200     buffer[max_chars_per_line] = '\0';
11201
11202     if (element_name[max_chars_per_line] == ' ')
11203       next_pos++;
11204     else
11205     {
11206       int i;
11207
11208       for (i = max_chars_per_line - 1; i >= 0; i--)
11209         if (buffer[i] == ' ')
11210           break;
11211
11212       if (strlen(&element_name[i + 1]) <= max_chars_per_line)
11213       {
11214         buffer[i] = '\0';
11215         next_pos = i + 1;
11216       }
11217     }
11218
11219     DrawTextS(x, y - font_height / 2, font_nr, buffer);
11220
11221     strncpy(buffer, &element_name[next_pos], max_chars_per_line);
11222     buffer[max_chars_per_line] = '\0';
11223
11224     DrawTextS(x, y + font_height / 2, font_nr, buffer);
11225   }
11226 }
11227
11228 static void DrawPropertiesWindow(void)
11229 {
11230   struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_INPUT_TEXT];
11231   int element_border = graphic_info[IMG_EDITOR_ELEMENT_BORDER].border_size;
11232   int border_size = gd->border_size;
11233   int font_nr = FONT_TEXT_1;
11234   int font_height = getFontHeight(font_nr);
11235   int xoffset = TILEX + element_border + 3 * border_size;
11236   int yoffset = (TILEY - font_height) / 2;
11237   int x1 = editor.settings.element_graphic.x + element_border;
11238   int y1 = editor.settings.element_graphic.y + element_border;
11239   int x2 = (editor.settings.element_name.x == -1 ? x1 + xoffset :
11240             editor.settings.element_name.x);
11241   int y2 = (editor.settings.element_name.y == -1 ? y1 + yoffset :
11242             editor.settings.element_name.y);
11243   char *text = "Element Settings";
11244   int font2_nr = FONT_TITLE_1;
11245   struct MenuPosInfo *pos = &editor.settings.headline;
11246   int sx = SX + ALIGNED_XPOS(pos->x, getTextWidth(text, font2_nr), pos->align);
11247   int sy = SY + pos->y;
11248
11249   stick_element_properties_window = FALSE;
11250
11251   // make sure that previous properties edit mode exists for this element
11252   if (edit_mode_properties > ED_MODE_PROPERTIES_CONFIG_2 &&
11253       !IS_CUSTOM_ELEMENT(properties_element))
11254     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG_2;
11255
11256   if (edit_mode_properties > ED_MODE_PROPERTIES_CONFIG &&
11257       !IS_PLAYER_ELEMENT(properties_element) &&
11258       !IS_CUSTOM_ELEMENT(properties_element))
11259     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG;
11260
11261   if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG &&
11262       (IS_PLAYER_ELEMENT(properties_element) ||
11263        IS_CUSTOM_ELEMENT(properties_element)))
11264     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG_1;
11265
11266   CopyElementPropertiesToEditor(properties_element);
11267
11268   UnmapLevelEditorFieldGadgets();
11269   UnmapLevelEditorToolboxDrawingGadgets();
11270   UnmapLevelEditorToolboxCustomGadgets();
11271
11272   MapLevelEditorToolboxCustomGadgetsIfNeeded();
11273
11274   SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
11275   ClearField();
11276
11277   DrawText(sx, sy, text, font2_nr);
11278
11279   FrameCounter = 0;     // restart animation frame counter
11280
11281   DrawElementBorder(SX + x1, SY + y1, TILEX, TILEY, FALSE);
11282   DrawEditorElementAnimation(SX + x1, SY + y1);
11283   DrawEditorElementName(x2, y2, font_nr);
11284
11285   DrawPropertiesTabulatorGadgets();
11286
11287   if (edit_mode_properties == ED_MODE_PROPERTIES_INFO)
11288     DrawPropertiesInfo();
11289   else if (edit_mode_properties == ED_MODE_PROPERTIES_CHANGE)
11290     DrawPropertiesChange();
11291   else  // (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG[_1|_2])
11292     DrawPropertiesConfig();
11293 }
11294
11295 static void DrawPaletteWindow(void)
11296 {
11297   int i;
11298
11299   UnmapLevelEditorFieldGadgets();
11300
11301   SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
11302   ClearField();
11303
11304   // map buttons to select elements
11305   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
11306     MapGadget(level_editor_gadget[GADGET_ID_ELEMENTLIST_FIRST + i]);
11307   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL]);
11308   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_UP]);
11309   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_DOWN]);
11310 }
11311
11312 static void UpdateCustomElementGraphicGadgets(void)
11313 {
11314   int i;
11315
11316   InitElementPropertiesGfxElement();
11317
11318   ModifyEditorElementList();
11319   RedrawDrawingElements();
11320
11321   // force redraw of all mapped drawing area gadgets
11322   for (i = 0; i < ED_NUM_DRAWING_AREAS; i++)
11323   {
11324     struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[i].gadget_id];
11325
11326     if (gi->mapped)
11327       MapDrawingArea(i);
11328   }
11329 }
11330
11331 static int getOpenDirectionFromTube(int element)
11332 {
11333   switch (element)
11334   {
11335     case EL_TUBE_LEFT_UP:               return (MV_LEFT | MV_UP);
11336     case EL_TUBE_LEFT_DOWN:             return (MV_LEFT | MV_DOWN);
11337     case EL_TUBE_RIGHT_UP:              return (MV_RIGHT | MV_UP);
11338     case EL_TUBE_RIGHT_DOWN:            return (MV_RIGHT | MV_DOWN);
11339     case EL_TUBE_HORIZONTAL:            return (MV_HORIZONTAL);
11340     case EL_TUBE_HORIZONTAL_UP:         return (MV_HORIZONTAL | MV_UP);
11341     case EL_TUBE_HORIZONTAL_DOWN:       return (MV_HORIZONTAL | MV_DOWN);
11342     case EL_TUBE_VERTICAL:              return (MV_VERTICAL);
11343     case EL_TUBE_VERTICAL_LEFT:         return (MV_VERTICAL | MV_LEFT);
11344     case EL_TUBE_VERTICAL_RIGHT:        return (MV_VERTICAL | MV_RIGHT);
11345     case EL_TUBE_ANY:                   return (MV_ANY_DIRECTION);
11346   }
11347
11348   return MV_NONE;
11349 }
11350
11351 static int getTubeFromOpenDirection(int direction)
11352 {
11353   switch (direction)
11354   {
11355     case (MV_LEFT | MV_UP):             return EL_TUBE_LEFT_UP;
11356     case (MV_LEFT | MV_DOWN):           return EL_TUBE_LEFT_DOWN;
11357     case (MV_RIGHT | MV_UP):            return EL_TUBE_RIGHT_UP;
11358     case (MV_RIGHT | MV_DOWN):          return EL_TUBE_RIGHT_DOWN;
11359     case (MV_HORIZONTAL):               return EL_TUBE_HORIZONTAL;
11360     case (MV_HORIZONTAL | MV_UP):       return EL_TUBE_HORIZONTAL_UP;
11361     case (MV_HORIZONTAL | MV_DOWN):     return EL_TUBE_HORIZONTAL_DOWN;
11362     case (MV_VERTICAL):                 return EL_TUBE_VERTICAL;
11363     case (MV_VERTICAL | MV_LEFT):       return EL_TUBE_VERTICAL_LEFT;
11364     case (MV_VERTICAL | MV_RIGHT):      return EL_TUBE_VERTICAL_RIGHT;
11365     case (MV_ANY_DIRECTION):            return EL_TUBE_ANY;
11366
11367     // if only one direction, fall back to simple tube with that direction
11368     case (MV_LEFT):                     return EL_TUBE_HORIZONTAL;
11369     case (MV_RIGHT):                    return EL_TUBE_HORIZONTAL;
11370     case (MV_UP):                       return EL_TUBE_VERTICAL;
11371     case (MV_DOWN):                     return EL_TUBE_VERTICAL;
11372   }
11373
11374   return EL_EMPTY;
11375 }
11376
11377 static int getTubeFromOpenDirectionNotEmpty(int direction, int element_old)
11378 {
11379   int element_new = getTubeFromOpenDirection(direction);
11380
11381   return (element_new != EL_EMPTY ? element_new : element_old);
11382 }
11383
11384 static int getOpenDirectionFromBelt(int element)
11385 {
11386   int belt_dir = getBeltDirFromBeltElement(element);
11387
11388   return (belt_dir == MV_LEFT ? MV_RIGHT :
11389           belt_dir == MV_RIGHT ? MV_LEFT :
11390           belt_dir == MV_NONE ? MV_HORIZONTAL : belt_dir);
11391 }
11392
11393 static int getBeltFromNrAndOpenDirection(int nr, int direction)
11394 {
11395   int belt_dir = (direction == MV_LEFT ? MV_RIGHT :
11396                   direction == MV_RIGHT ? MV_LEFT :
11397                   direction == MV_HORIZONTAL ? MV_NONE : direction);
11398
11399   if (direction == MV_NONE)
11400     return EL_EMPTY;
11401
11402   return getBeltElementFromBeltNrAndBeltDir(nr, belt_dir);
11403 }
11404
11405 static int getBeltFromNrAndOpenDirectionNotEmpty(int nr, int direction,
11406                                                  int element_old)
11407 {
11408   int element_new = getBeltFromNrAndOpenDirection(nr, direction);
11409
11410   return (element_new != EL_EMPTY ? element_new : element_old);
11411 }
11412
11413 static int getOpenDirectionFromPool(int element)
11414 {
11415   switch (element)
11416   {
11417     case EL_ACID_POOL_TOPLEFT:          return (MV_DOWN | MV_RIGHT);
11418     case EL_ACID_POOL_TOPRIGHT:         return (MV_DOWN | MV_LEFT);
11419     case EL_ACID_POOL_BOTTOMLEFT:       return (MV_UP | MV_RIGHT);
11420     case EL_ACID_POOL_BOTTOMRIGHT:      return (MV_UP | MV_LEFT);
11421     case EL_ACID_POOL_BOTTOM:           return (MV_HORIZONTAL | MV_UP);
11422     case EL_ACID:                       return (MV_HORIZONTAL | MV_DOWN);
11423   }
11424
11425   return MV_NONE;
11426 }
11427
11428 static int getPoolFromOpenDirection(int direction)
11429 {
11430   switch (direction)
11431   {
11432     case (MV_DOWN | MV_RIGHT):          return EL_ACID_POOL_TOPLEFT;
11433     case (MV_DOWN | MV_LEFT):           return EL_ACID_POOL_TOPRIGHT;
11434     case (MV_UP | MV_RIGHT):            return EL_ACID_POOL_BOTTOMLEFT;
11435     case (MV_UP | MV_LEFT):             return EL_ACID_POOL_BOTTOMRIGHT;
11436     case (MV_HORIZONTAL | MV_UP):       return EL_ACID_POOL_BOTTOM;
11437     case (MV_HORIZONTAL | MV_DOWN):     return EL_ACID;
11438   }
11439
11440   return EL_EMPTY;
11441 }
11442
11443 static int getPoolFromOpenDirectionExt(int direction, int help_element)
11444 {
11445   int element = getPoolFromOpenDirection(direction);
11446   int help_direction = getOpenDirectionFromPool(help_element);
11447
11448   if (element == EL_EMPTY)
11449   {
11450     int help_direction_vertical = help_direction & MV_VERTICAL;
11451
11452     element = getPoolFromOpenDirection(direction | help_direction_vertical);
11453   }
11454
11455   if (element == EL_EMPTY)
11456   {
11457     int help_direction_horizontal = help_direction & MV_HORIZONTAL;
11458
11459     element = getPoolFromOpenDirection(direction | help_direction_horizontal);
11460   }
11461
11462   return element;
11463 }
11464
11465 static int getPoolFromOpenDirectionNotEmpty(int direction, int element_old)
11466 {
11467   int element_new = getPoolFromOpenDirectionExt(direction, element_old);
11468
11469   return (element_new != EL_EMPTY ? element_new : element_old);
11470 }
11471
11472 static int getOpenDirectionFromPillar(int element)
11473 {
11474   switch (element)
11475   {
11476     case EL_EMC_WALL_1:                 return (MV_DOWN);
11477     case EL_EMC_WALL_2:                 return (MV_VERTICAL);
11478     case EL_EMC_WALL_3:                 return (MV_UP);
11479   }
11480
11481   return MV_NONE;
11482 }
11483
11484 static int getPillarFromOpenDirection(int direction)
11485 {
11486   switch (direction)
11487   {
11488     case (MV_DOWN):                     return EL_EMC_WALL_1;
11489     case (MV_VERTICAL):                 return EL_EMC_WALL_2;
11490     case (MV_UP):                       return EL_EMC_WALL_3;
11491   }
11492
11493   return EL_EMPTY;
11494 }
11495
11496 static int getPillarFromOpenDirectionNotEmpty(int direction, int element_old)
11497 {
11498   int element_new = getPillarFromOpenDirection(direction);
11499
11500   return (element_new != EL_EMPTY ? element_new : element_old);
11501 }
11502
11503 static int getOpenDirectionFromSteel2(int element)
11504 {
11505   switch (element)
11506   {
11507     case EL_DC_STEELWALL_2_LEFT:        return (MV_RIGHT);
11508     case EL_DC_STEELWALL_2_RIGHT:       return (MV_LEFT);
11509     case EL_DC_STEELWALL_2_TOP:         return (MV_DOWN);
11510     case EL_DC_STEELWALL_2_BOTTOM:      return (MV_UP);
11511     case EL_DC_STEELWALL_2_HORIZONTAL:  return (MV_HORIZONTAL);
11512     case EL_DC_STEELWALL_2_VERTICAL:    return (MV_VERTICAL);
11513     case EL_DC_STEELWALL_2_MIDDLE:      return (MV_ANY_DIRECTION);
11514     case EL_DC_STEELWALL_2_SINGLE:      return (MV_NONE);
11515   }
11516
11517   return MV_NONE;
11518 }
11519
11520 static int getSteel2FromOpenDirection(int direction)
11521 {
11522   switch (direction)
11523   {
11524     case (MV_RIGHT):                    return EL_DC_STEELWALL_2_LEFT;
11525     case (MV_LEFT):                     return EL_DC_STEELWALL_2_RIGHT;
11526     case (MV_DOWN):                     return EL_DC_STEELWALL_2_TOP;
11527     case (MV_UP):                       return EL_DC_STEELWALL_2_BOTTOM;
11528     case (MV_HORIZONTAL):               return EL_DC_STEELWALL_2_HORIZONTAL;
11529     case (MV_VERTICAL):                 return EL_DC_STEELWALL_2_VERTICAL;
11530     case (MV_ANY_DIRECTION):            return EL_DC_STEELWALL_2_MIDDLE;
11531     case (MV_NONE):                     return EL_DC_STEELWALL_2_SINGLE;
11532   }
11533
11534   return EL_EMPTY;
11535 }
11536
11537 static int getSteel2FromOpenDirectionNotEmpty(int direction, int element_old)
11538 {
11539   int element_new = getSteel2FromOpenDirection(direction);
11540
11541   return (element_new != EL_EMPTY ? element_new : element_old);
11542 }
11543
11544 static int getOpenDirectionFromChip(int element)
11545 {
11546   switch (element)
11547   {
11548     case EL_SP_CHIP_SINGLE:             return (MV_NONE);
11549     case EL_SP_CHIP_LEFT:               return (MV_RIGHT);
11550     case EL_SP_CHIP_RIGHT:              return (MV_LEFT);
11551     case EL_SP_CHIP_TOP:                return (MV_DOWN);
11552     case EL_SP_CHIP_BOTTOM:             return (MV_UP);
11553   }
11554
11555   return MV_NONE;
11556 }
11557
11558 static int getChipFromOpenDirection(int direction)
11559 {
11560   switch (direction)
11561   {
11562     case (MV_NONE):                     return EL_SP_CHIP_SINGLE;
11563     case (MV_LEFT):                     return EL_SP_CHIP_RIGHT;
11564     case (MV_RIGHT):                    return EL_SP_CHIP_LEFT;
11565     case (MV_UP):                       return EL_SP_CHIP_BOTTOM;
11566     case (MV_DOWN):                     return EL_SP_CHIP_TOP;
11567   }
11568
11569   return EL_EMPTY;
11570 }
11571
11572 static int getChipFromOpenDirectionNotEmpty(int direction, int element_old)
11573 {
11574   int element_new = getChipFromOpenDirection(direction);
11575
11576   return (element_new != EL_EMPTY ? element_new : element_old);
11577 }
11578
11579 static int getClosedTube(int x, int y)
11580 {
11581   struct XY *xy = xy_directions;
11582   int element_old = IntelliDrawBuffer[x][y];
11583   int direction_old = getOpenDirectionFromTube(element_old);
11584   int direction_new = MV_NONE;
11585   int i;
11586
11587   for (i = 0; i < NUM_DIRECTIONS; i++)
11588   {
11589     int xx = x + xy[i].x;
11590     int yy = y + xy[i].y;
11591     int dir = MV_DIR_FROM_BIT(i);
11592     int dir_opposite = MV_DIR_OPPOSITE(dir);
11593
11594     if (IN_LEV_FIELD(xx, yy) && IS_TUBE(IntelliDrawBuffer[xx][yy]) &&
11595         (direction_old & dir) &&
11596         (getOpenDirectionFromTube(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11597       direction_new |= dir;
11598   }
11599
11600   return getTubeFromOpenDirectionNotEmpty(direction_new, element_old);
11601 }
11602
11603 static int getClosedBelt(int x, int y)
11604 {
11605   struct XY *xy = xy_directions;
11606   int element_old = IntelliDrawBuffer[x][y];
11607   int nr = getBeltNrFromBeltElement(element_old);
11608   int direction_old = getOpenDirectionFromBelt(element_old);
11609   int direction_new = MV_NONE;
11610   int i;
11611
11612   for (i = MV_BIT_LEFT; i <= MV_BIT_RIGHT; i++)
11613   {
11614     int xx = x + xy[i].x;
11615     int yy = y + xy[i].y;
11616     int dir = MV_DIR_FROM_BIT(i);
11617     int dir_opposite = MV_DIR_OPPOSITE(dir);
11618
11619     if (IN_LEV_FIELD(xx, yy) && IS_BELT(IntelliDrawBuffer[xx][yy]) &&
11620         (direction_old & dir) &&
11621         (getOpenDirectionFromBelt(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11622       direction_new |= dir;
11623   }
11624
11625   return getBeltFromNrAndOpenDirection(nr, direction_new);
11626 }
11627
11628 static int getClosedPool(int x, int y)
11629 {
11630   struct XY *xy = xy_directions;
11631   int element_old = IntelliDrawBuffer[x][y];
11632   int direction_old = getOpenDirectionFromPool(element_old);
11633   int direction_new = MV_NONE;
11634   int i;
11635
11636   for (i = 0; i < NUM_DIRECTIONS; i++)
11637   {
11638     int xx = x + xy[i].x;
11639     int yy = y + xy[i].y;
11640     int dir = MV_DIR_FROM_BIT(i);
11641     int dir_opposite = MV_DIR_OPPOSITE(dir);
11642
11643     if (IN_LEV_FIELD(xx, yy) &&
11644         IS_ACID_POOL_OR_ACID(IntelliDrawBuffer[xx][yy]) &&
11645         (direction_old & dir) &&
11646         (getOpenDirectionFromPool(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11647       direction_new |= dir;
11648   }
11649
11650   return getPoolFromOpenDirectionNotEmpty(direction_new, element_old);
11651 }
11652
11653 static int getClosedPillar(int x, int y)
11654 {
11655   struct XY *xy = xy_directions;
11656   int element_old = IntelliDrawBuffer[x][y];
11657   int direction_old = getOpenDirectionFromPillar(element_old);
11658   int direction_new = MV_NONE;
11659   int i;
11660
11661   for (i = MV_BIT_UP; i <= MV_BIT_DOWN; i++)
11662   {
11663     int xx = x + xy[i].x;
11664     int yy = y + xy[i].y;
11665     int dir = MV_DIR_FROM_BIT(i);
11666     int dir_opposite = MV_DIR_OPPOSITE(dir);
11667
11668     if (IN_LEV_FIELD(xx, yy) && IS_EMC_PILLAR(IntelliDrawBuffer[xx][yy]) &&
11669         (direction_old & dir) &&
11670         (getOpenDirectionFromPillar(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11671       direction_new |= dir;
11672   }
11673
11674   return getPillarFromOpenDirectionNotEmpty(direction_new, element_old);
11675 }
11676
11677 static int getClosedSteel2(int x, int y)
11678 {
11679   struct XY *xy = xy_directions;
11680   int element_old = IntelliDrawBuffer[x][y];
11681   int direction_old = getOpenDirectionFromSteel2(element_old);
11682   int direction_new = MV_NONE;
11683   int i;
11684
11685   for (i = 0; i < NUM_DIRECTIONS; i++)
11686   {
11687     int xx = x + xy[i].x;
11688     int yy = y + xy[i].y;
11689     int dir = MV_DIR_FROM_BIT(i);
11690     int dir_opposite = MV_DIR_OPPOSITE(dir);
11691
11692     if (IN_LEV_FIELD(xx, yy) && IS_DC_STEELWALL_2(IntelliDrawBuffer[xx][yy]) &&
11693         (direction_old & dir) &&
11694         (getOpenDirectionFromSteel2(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11695       direction_new |= dir;
11696   }
11697
11698   return getSteel2FromOpenDirectionNotEmpty(direction_new, element_old);
11699 }
11700
11701 static int getClosedChip(int x, int y)
11702 {
11703   struct XY *xy = xy_directions;
11704   int element_old = IntelliDrawBuffer[x][y];
11705   int direction_old = getOpenDirectionFromChip(element_old);
11706   int direction_new = MV_NONE;
11707   int i;
11708
11709   for (i = 0; i < NUM_DIRECTIONS; i++)
11710   {
11711     int xx = x + xy[i].x;
11712     int yy = y + xy[i].y;
11713     int dir = MV_DIR_FROM_BIT(i);
11714     int dir_opposite = MV_DIR_OPPOSITE(dir);
11715
11716     if (IN_LEV_FIELD(xx, yy) && IS_SP_CHIP(IntelliDrawBuffer[xx][yy]) &&
11717         (direction_old & dir) &&
11718         (getOpenDirectionFromChip(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11719       direction_new |= dir;
11720   }
11721
11722   return getChipFromOpenDirectionNotEmpty(direction_new, element_old);
11723 }
11724
11725 static void SetElementSimpleExt(int x, int y, int dx, int dy, int element,
11726                                 boolean change_level)
11727 {
11728   int sx = x - level_xpos;
11729   int sy = y - level_ypos;
11730   int old_element = Tile[x][y];
11731   int new_element = element;
11732   unsigned int new_bitmask = (getDrawModeHiRes() ? (dx + 1) << (dy * 2) : 0x0f);
11733   boolean draw_masked = FALSE;
11734
11735   if (IS_MM_WALL_EDITOR(element))
11736   {
11737     element = map_mm_wall_element_editor(element) | new_bitmask;
11738
11739     if (IS_MM_WALL(old_element))
11740       element |= MM_WALL_BITS(old_element);
11741
11742     if (!change_level)
11743       draw_masked = TRUE;
11744   }
11745   else if (IS_MM_WALL(old_element) && element == EL_EMPTY)
11746   {
11747     int element_changed = old_element & ~new_bitmask;
11748
11749     if (MM_WALL_BITS(element_changed) != 0)
11750       element = element_changed;
11751   }
11752
11753   IntelliDrawBuffer[x][y] = element;
11754
11755   if (change_level)
11756     Tile[x][y] = element;
11757
11758   if (IN_ED_FIELD(sx, sy))
11759   {
11760     if (IS_MM_WALL(old_element) && new_element == EL_EMPTY)
11761       DrawSizedWallParts_MM(sx, sy, EL_EMPTY, ed_tilesize, FALSE, new_bitmask);
11762     else if (draw_masked)
11763       DrawEditorElementThruMask(sx, sy, element);
11764     else
11765       DrawEditorElement(sx, sy, element);
11766   }
11767 }
11768
11769 static void SetElementSimple(int x, int y, int element, boolean change_level)
11770 {
11771   SetElementSimpleExt(x, y, 0, 0, element, change_level);
11772 }
11773
11774 static void MergeAndCloseNeighbourElements(int x1, int y1, int *element1,
11775                                            int x2, int y2, int *element2,
11776                                            int (*close_function)(int, int),
11777                                            boolean change_level)
11778 {
11779   // set neighbour elements to newly determined connections
11780   SetElementSimple(x1, y1, *element1, change_level);
11781   SetElementSimple(x2, y2, *element2, change_level);
11782
11783   // remove all open connections of neighbour elements
11784   *element1 = close_function(x1, y1);
11785   *element2 = close_function(x2, y2);
11786
11787   // set neighbour elements to new, minimized connections
11788   SetElementSimple(x1, y1, *element1, change_level);
11789   SetElementSimple(x2, y2, *element2, change_level);
11790 }
11791
11792 static void SetElementIntelliDraw(int x, int y, int dx, int dy, int new_element,
11793                                   boolean change_level, int button)
11794 {
11795   struct XY *xy = xy_directions;
11796   static int last_x = -1;
11797   static int last_y = -1;
11798
11799   if (new_element == EL_UNDEFINED)
11800   {
11801     last_x = -1;
11802     last_y = -1;
11803
11804     return;
11805   }
11806
11807   int old_element = IntelliDrawBuffer[x][y];
11808
11809   if (IS_TUBE(new_element))
11810   {
11811     int last_element_new = EL_UNDEFINED;
11812     int direction = MV_NONE;
11813     int i;
11814
11815     // if old element is of same kind, keep all existing directions
11816     if (IS_TUBE(old_element))
11817       direction |= getOpenDirectionFromTube(old_element);
11818
11819     for (i = 0; i < NUM_DIRECTIONS; i++)
11820     {
11821       int xx = x + xy[i].x;
11822       int yy = y + xy[i].y;
11823
11824       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
11825           IS_TUBE(IntelliDrawBuffer[last_x][last_y]))
11826       {
11827         int dir = MV_DIR_FROM_BIT(i);
11828         int dir_opposite = MV_DIR_OPPOSITE(dir);
11829         int last_element_old = IntelliDrawBuffer[last_x][last_y];
11830         int last_direction_old = getOpenDirectionFromTube(last_element_old);
11831         int last_direction_new = last_direction_old | dir_opposite;
11832
11833         last_element_new = getTubeFromOpenDirection(last_direction_new);
11834
11835         direction |= dir;
11836       }
11837     }
11838
11839     new_element = getTubeFromOpenDirectionNotEmpty(direction, new_element);
11840
11841     if (last_element_new != EL_UNDEFINED)
11842       MergeAndCloseNeighbourElements(x, y, &new_element,
11843                                      last_x, last_y, &last_element_new,
11844                                      getClosedTube, change_level);
11845   }
11846   else if (IS_BELT(new_element))
11847   {
11848     int belt_nr = getBeltNrFromBeltElement(new_element);
11849     int last_element_new = EL_UNDEFINED;
11850     int direction = MV_NONE;
11851     int i;
11852
11853     // if old element is of same kind, keep all existing directions
11854     if (IS_BELT(old_element))
11855       direction |= getOpenDirectionFromBelt(old_element);
11856
11857     for (i = MV_BIT_LEFT; i <= MV_BIT_RIGHT; i++)
11858     {
11859       int xx = x + xy[i].x;
11860       int yy = y + xy[i].y;
11861
11862       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
11863           IS_BELT(IntelliDrawBuffer[last_x][last_y]))
11864       {
11865         int dir = MV_DIR_FROM_BIT(i);
11866         int dir_opposite = MV_DIR_OPPOSITE(dir);
11867         int last_element_old = IntelliDrawBuffer[last_x][last_y];
11868         int last_belt_nr = getBeltNrFromBeltElement(last_element_old);
11869         int last_direction_old = getOpenDirectionFromBelt(last_element_old);
11870         int last_direction_new = last_direction_old | dir_opposite;
11871
11872         last_element_new = getBeltFromNrAndOpenDirection(last_belt_nr,
11873                                                          last_direction_new);
11874         direction |= dir;
11875       }
11876     }
11877
11878     new_element = getBeltFromNrAndOpenDirectionNotEmpty(belt_nr, direction,
11879                                                         new_element);
11880     if (last_element_new != EL_UNDEFINED)
11881       MergeAndCloseNeighbourElements(x, y, &new_element,
11882                                      last_x, last_y, &last_element_new,
11883                                      getClosedBelt, change_level);
11884   }
11885   else if (IS_ACID_POOL_OR_ACID(new_element))
11886   {
11887     int last_element_new = EL_UNDEFINED;
11888     int direction = MV_NONE;
11889     int i;
11890
11891     // if old element is of same kind, keep all existing directions
11892     if (IS_ACID_POOL_OR_ACID(old_element))
11893       direction |= getOpenDirectionFromPool(old_element);
11894
11895     for (i = 0; i < NUM_DIRECTIONS; i++)
11896     {
11897       int xx = x + xy[i].x;
11898       int yy = y + xy[i].y;
11899
11900       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
11901           IS_ACID_POOL_OR_ACID(IntelliDrawBuffer[last_x][last_y]))
11902       {
11903         int dir = MV_DIR_FROM_BIT(i);
11904         int dir_opposite = MV_DIR_OPPOSITE(dir);
11905         int last_element_old = IntelliDrawBuffer[last_x][last_y];
11906         int last_direction_old = getOpenDirectionFromPool(last_element_old);
11907         int last_direction_new = last_direction_old | dir_opposite;
11908
11909         last_element_new = getPoolFromOpenDirection(last_direction_new);
11910
11911         direction |= dir;
11912       }
11913     }
11914
11915     // special corrections needed for intuitively correct acid pool drawing
11916     if (last_element_new == EL_EMPTY)
11917       last_element_new = new_element;
11918     else if (last_element_new != EL_UNDEFINED)
11919       new_element = last_element_new;
11920
11921     new_element = getPoolFromOpenDirectionNotEmpty(direction, new_element);
11922
11923     if (last_element_new != EL_UNDEFINED)
11924       MergeAndCloseNeighbourElements(x, y, &new_element,
11925                                      last_x, last_y, &last_element_new,
11926                                      getClosedPool, change_level);
11927   }
11928   else if (IS_EMC_PILLAR(new_element))
11929   {
11930     int last_element_new = EL_UNDEFINED;
11931     int direction = MV_NONE;
11932     int i;
11933
11934     // if old element is of same kind, keep all existing directions
11935     if (IS_EMC_PILLAR(old_element))
11936       direction |= getOpenDirectionFromPillar(old_element);
11937
11938     for (i = MV_BIT_UP; i <= MV_BIT_DOWN; i++)
11939     {
11940       int xx = x + xy[i].x;
11941       int yy = y + xy[i].y;
11942
11943       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
11944           IS_EMC_PILLAR(IntelliDrawBuffer[last_x][last_y]))
11945       {
11946         int dir = MV_DIR_FROM_BIT(i);
11947         int dir_opposite = MV_DIR_OPPOSITE(dir);
11948         int last_element_old = IntelliDrawBuffer[last_x][last_y];
11949         int last_direction_old = getOpenDirectionFromPillar(last_element_old);
11950         int last_direction_new = last_direction_old | dir_opposite;
11951
11952         last_element_new = getPillarFromOpenDirection(last_direction_new);
11953
11954         direction |= dir;
11955       }
11956     }
11957
11958     new_element = getPillarFromOpenDirectionNotEmpty(direction, new_element);
11959
11960     if (last_element_new != EL_UNDEFINED)
11961       MergeAndCloseNeighbourElements(x, y, &new_element,
11962                                      last_x, last_y, &last_element_new,
11963                                      getClosedPillar, change_level);
11964   }
11965   else if (IS_DC_STEELWALL_2(new_element))
11966   {
11967     int last_element_new = EL_UNDEFINED;
11968     int direction = MV_NONE;
11969     int i;
11970
11971     // if old element is of same kind, keep all existing directions
11972     if (IS_DC_STEELWALL_2(old_element))
11973       direction |= getOpenDirectionFromSteel2(old_element);
11974
11975     for (i = 0; i < NUM_DIRECTIONS; i++)
11976     {
11977       int xx = x + xy[i].x;
11978       int yy = y + xy[i].y;
11979
11980       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
11981           IS_DC_STEELWALL_2(IntelliDrawBuffer[last_x][last_y]))
11982       {
11983         int dir = MV_DIR_FROM_BIT(i);
11984         int dir_opposite = MV_DIR_OPPOSITE(dir);
11985         int last_element_old = IntelliDrawBuffer[last_x][last_y];
11986         int last_direction_old = getOpenDirectionFromSteel2(last_element_old);
11987         int last_direction_new = last_direction_old | dir_opposite;
11988
11989         last_element_new = getSteel2FromOpenDirection(last_direction_new);
11990
11991         direction |= dir;
11992       }
11993     }
11994
11995     new_element = getSteel2FromOpenDirectionNotEmpty(direction, new_element);
11996
11997     if (last_element_new != EL_UNDEFINED)
11998       MergeAndCloseNeighbourElements(x, y, &new_element,
11999                                      last_x, last_y, &last_element_new,
12000                                      getClosedSteel2, change_level);
12001   }
12002   else if (IS_SP_CHIP(new_element))
12003   {
12004     int last_element_new = EL_UNDEFINED;
12005     int direction = MV_NONE;
12006     int i;
12007
12008     // (do not keep existing directions, regardless of kind of old element)
12009
12010     for (i = 0; i < NUM_DIRECTIONS; i++)
12011     {
12012       int xx = x + xy[i].x;
12013       int yy = y + xy[i].y;
12014
12015       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12016           IS_SP_CHIP(IntelliDrawBuffer[last_x][last_y]))
12017       {
12018         int dir = MV_DIR_FROM_BIT(i);
12019         int dir_opposite = MV_DIR_OPPOSITE(dir);
12020         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12021         int last_direction_old = getOpenDirectionFromChip(last_element_old);
12022         int last_direction_new = last_direction_old | dir_opposite;
12023
12024         if (last_direction_old == MV_NONE)
12025         {
12026           last_element_new = getChipFromOpenDirection(last_direction_new);
12027           direction |= dir;
12028         }
12029         else if (last_direction_old & (dir | dir_opposite))
12030         {
12031           direction |= MV_DIR_OPPOSITE(last_direction_old);
12032         }
12033         else
12034         {
12035           direction |= MV_DIR_OPPOSITE(dir);
12036         }
12037       }
12038     }
12039
12040     new_element = getChipFromOpenDirectionNotEmpty(direction, new_element);
12041
12042     if (last_element_new != EL_UNDEFINED)
12043       MergeAndCloseNeighbourElements(x, y, &new_element,
12044                                      last_x, last_y, &last_element_new,
12045                                      getClosedChip, change_level);
12046   }
12047   else if (IS_SP_HARDWARE_BASE(new_element))
12048   {
12049     int nr = GetSimpleRandom(6);
12050
12051     new_element = (nr == 0 ? EL_SP_HARDWARE_BASE_1 :
12052                    nr == 1 ? EL_SP_HARDWARE_BASE_2 :
12053                    nr == 2 ? EL_SP_HARDWARE_BASE_3 :
12054                    nr == 3 ? EL_SP_HARDWARE_BASE_4 :
12055                    nr == 4 ? EL_SP_HARDWARE_BASE_5 : EL_SP_HARDWARE_BASE_6);
12056   }
12057   else if (new_element == EL_SP_HARDWARE_GREEN ||
12058            new_element == EL_SP_HARDWARE_BLUE ||
12059            new_element == EL_SP_HARDWARE_RED)
12060   {
12061     int nr = GetSimpleRandom(3);
12062
12063     new_element = (nr == 0 ? EL_SP_HARDWARE_GREEN :
12064                    nr == 1 ? EL_SP_HARDWARE_BLUE : EL_SP_HARDWARE_RED);
12065   }
12066   else if (IS_GROUP_ELEMENT(new_element))
12067   {
12068     boolean connected_drawing = FALSE;
12069     int i;
12070
12071     for (i = 0; i < NUM_DIRECTIONS; i++)
12072     {
12073       int xx = x + xy[i].x;
12074       int yy = y + xy[i].y;
12075
12076       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12077           IS_IN_GROUP_EL(IntelliDrawBuffer[last_x][last_y], new_element))
12078         connected_drawing = TRUE;
12079     }
12080
12081     if (!connected_drawing)
12082       ResolveGroupElement(new_element);
12083
12084     new_element = GetElementFromGroupElement(new_element);
12085   }
12086   else if (IS_BELT_SWITCH(old_element))
12087   {
12088     int belt_nr = getBeltNrFromBeltSwitchElement(old_element);
12089     int belt_dir = getBeltDirFromBeltSwitchElement(old_element);
12090
12091     belt_dir = (belt_dir == MV_LEFT ? MV_NONE :
12092                 belt_dir == MV_NONE ? MV_RIGHT : MV_LEFT);
12093
12094     new_element = getBeltSwitchElementFromBeltNrAndBeltDir(belt_nr, belt_dir);
12095   }
12096   else
12097   {
12098     static int swappable_elements[][2] =
12099     {
12100       { EL_EXIT_CLOSED,                 EL_EXIT_OPEN                    },
12101       { EL_DYNAMITE,                    EL_DYNAMITE_ACTIVE              },
12102       { EL_EM_DYNAMITE,                 EL_EM_DYNAMITE_ACTIVE           },
12103       { EL_QUICKSAND_EMPTY,             EL_QUICKSAND_FULL               },
12104       { EL_EMERALD,                     EL_WALL_EMERALD                 },
12105       { EL_EMERALD_YELLOW,              EL_WALL_EMERALD_YELLOW          },
12106       { EL_EMERALD_RED,                 EL_WALL_EMERALD_RED             },
12107       { EL_EMERALD_PURPLE,              EL_WALL_EMERALD_PURPLE          },
12108       { EL_DIAMOND,                     EL_WALL_DIAMOND                 },
12109       { EL_BD_DIAMOND,                  EL_WALL_BD_DIAMOND              },
12110       { EL_GATE_1,                      EL_GATE_1_GRAY                  },
12111       { EL_GATE_2,                      EL_GATE_2_GRAY                  },
12112       { EL_GATE_3,                      EL_GATE_3_GRAY                  },
12113       { EL_GATE_4,                      EL_GATE_4_GRAY                  },
12114       { EL_EM_GATE_1,                   EL_EM_GATE_1_GRAY               },
12115       { EL_EM_GATE_2,                   EL_EM_GATE_2_GRAY               },
12116       { EL_EM_GATE_3,                   EL_EM_GATE_3_GRAY               },
12117       { EL_EM_GATE_4,                   EL_EM_GATE_4_GRAY               },
12118       { EL_EMC_GATE_5,                  EL_EMC_GATE_5_GRAY              },
12119       { EL_EMC_GATE_6,                  EL_EMC_GATE_6_GRAY              },
12120       { EL_EMC_GATE_7,                  EL_EMC_GATE_7_GRAY              },
12121       { EL_EMC_GATE_8,                  EL_EMC_GATE_8_GRAY              },
12122       { EL_DC_GATE_WHITE,               EL_DC_GATE_WHITE_GRAY           },
12123       { EL_TIME_ORB_EMPTY,              EL_TIME_ORB_FULL                },
12124       { EL_LAMP,                        EL_LAMP_ACTIVE                  },
12125       { EL_SOKOBAN_FIELD_EMPTY,         EL_SOKOBAN_FIELD_FULL           },
12126       { EL_SP_BASE,                     EL_SP_BUGGY_BASE                },
12127       { EL_PEARL,                       EL_WALL_PEARL                   },
12128       { EL_CRYSTAL,                     EL_WALL_CRYSTAL                 },
12129       { EL_TIMEGATE_CLOSED,             EL_TIMEGATE_OPEN                },
12130       { EL_SWITCHGATE_CLOSED,           EL_SWITCHGATE_OPEN              },
12131       { EL_SWITCHGATE_SWITCH_UP,        EL_SWITCHGATE_SWITCH_DOWN       },
12132       { EL_DC_SWITCHGATE_SWITCH_UP,     EL_DC_SWITCHGATE_SWITCH_DOWN    },
12133       { EL_LIGHT_SWITCH,                EL_LIGHT_SWITCH_ACTIVE          },
12134       { EL_LANDMINE,                    EL_DC_LANDMINE                  },
12135       { EL_SHIELD_NORMAL,               EL_SHIELD_DEADLY                },
12136       { EL_STEEL_EXIT_CLOSED,           EL_STEEL_EXIT_OPEN              },
12137       { EL_EM_EXIT_CLOSED,              EL_EM_EXIT_OPEN                 },
12138       { EL_EM_STEEL_EXIT_CLOSED,        EL_EM_STEEL_EXIT_OPEN           },
12139       { EL_QUICKSAND_FAST_EMPTY,        EL_QUICKSAND_FAST_FULL          },
12140       { EL_MM_EXIT_CLOSED,              EL_MM_EXIT_OPEN                 },
12141       { EL_MM_FUSE,                     EL_MM_FUSE_ACTIVE               },
12142       { EL_MM_LIGHTBULB,                EL_MM_LIGHTBULB_ACTIVE          },
12143       { EL_MM_FUEL_EMPTY,               EL_MM_FUEL_FULL                 },
12144
12145       { -1,                             -1                              },
12146     };
12147     static int rotatable_elements_4[][4] =
12148     {
12149       {
12150         EL_BUG_UP,
12151         EL_BUG_RIGHT,
12152         EL_BUG_DOWN,
12153         EL_BUG_LEFT
12154       },
12155       {
12156         EL_SPACESHIP_UP,
12157         EL_SPACESHIP_RIGHT,
12158         EL_SPACESHIP_DOWN,
12159         EL_SPACESHIP_LEFT
12160       },
12161       {
12162         EL_BD_BUTTERFLY_UP,
12163         EL_BD_BUTTERFLY_RIGHT,
12164         EL_BD_BUTTERFLY_DOWN,
12165         EL_BD_BUTTERFLY_LEFT
12166       },
12167       {
12168         EL_BD_FIREFLY_UP,
12169         EL_BD_FIREFLY_RIGHT,
12170         EL_BD_FIREFLY_DOWN,
12171         EL_BD_FIREFLY_LEFT
12172       },
12173       {
12174         EL_PACMAN_UP,
12175         EL_PACMAN_RIGHT,
12176         EL_PACMAN_DOWN,
12177         EL_PACMAN_LEFT
12178       },
12179       {
12180         EL_YAMYAM_UP,
12181         EL_YAMYAM_RIGHT,
12182         EL_YAMYAM_DOWN,
12183         EL_YAMYAM_LEFT
12184       },
12185       {
12186         EL_ARROW_UP,
12187         EL_ARROW_RIGHT,
12188         EL_ARROW_DOWN,
12189         EL_ARROW_LEFT
12190       },
12191       {
12192         EL_SP_PORT_UP,
12193         EL_SP_PORT_RIGHT,
12194         EL_SP_PORT_DOWN,
12195         EL_SP_PORT_LEFT
12196       },
12197       {
12198         EL_SP_GRAVITY_PORT_UP,
12199         EL_SP_GRAVITY_PORT_RIGHT,
12200         EL_SP_GRAVITY_PORT_DOWN,
12201         EL_SP_GRAVITY_PORT_LEFT
12202       },
12203       {
12204         EL_SP_GRAVITY_ON_PORT_UP,
12205         EL_SP_GRAVITY_ON_PORT_RIGHT,
12206         EL_SP_GRAVITY_ON_PORT_DOWN,
12207         EL_SP_GRAVITY_ON_PORT_LEFT
12208       },
12209       {
12210         EL_SP_GRAVITY_OFF_PORT_UP,
12211         EL_SP_GRAVITY_OFF_PORT_RIGHT,
12212         EL_SP_GRAVITY_OFF_PORT_DOWN,
12213         EL_SP_GRAVITY_OFF_PORT_LEFT
12214       },
12215       {
12216         EL_MOLE_UP,
12217         EL_MOLE_RIGHT,
12218         EL_MOLE_DOWN,
12219         EL_MOLE_LEFT
12220       },
12221       {
12222         EL_BALLOON_SWITCH_UP,
12223         EL_BALLOON_SWITCH_RIGHT,
12224         EL_BALLOON_SWITCH_DOWN,
12225         EL_BALLOON_SWITCH_LEFT
12226       },
12227       {
12228         EL_MM_MCDUFFIN_UP,
12229         EL_MM_MCDUFFIN_RIGHT,
12230         EL_MM_MCDUFFIN_DOWN,
12231         EL_MM_MCDUFFIN_LEFT
12232       },
12233       {
12234         EL_MM_MIRROR_FIXED_1,
12235         EL_MM_MIRROR_FIXED_4,
12236         EL_MM_MIRROR_FIXED_3,
12237         EL_MM_MIRROR_FIXED_2
12238       },
12239       {
12240         EL_MM_STEEL_GRID_FIXED_1,
12241         EL_MM_STEEL_GRID_FIXED_4,
12242         EL_MM_STEEL_GRID_FIXED_2,
12243         EL_MM_STEEL_GRID_FIXED_3
12244       },
12245       {
12246         EL_MM_WOODEN_GRID_FIXED_1,
12247         EL_MM_WOODEN_GRID_FIXED_4,
12248         EL_MM_WOODEN_GRID_FIXED_2,
12249         EL_MM_WOODEN_GRID_FIXED_3
12250       },
12251       {
12252         EL_MM_POLARIZER_CROSS_1,
12253         EL_MM_POLARIZER_CROSS_4,
12254         EL_MM_POLARIZER_CROSS_3,
12255         EL_MM_POLARIZER_CROSS_2
12256       },
12257       {
12258         EL_MM_PACMAN_UP,
12259         EL_MM_PACMAN_RIGHT,
12260         EL_MM_PACMAN_DOWN,
12261         EL_MM_PACMAN_LEFT
12262       },
12263       {
12264         EL_DF_LASER_UP,
12265         EL_DF_LASER_RIGHT,
12266         EL_DF_LASER_DOWN,
12267         EL_DF_LASER_LEFT
12268       },
12269       {
12270         EL_DF_RECEIVER_UP,
12271         EL_DF_RECEIVER_RIGHT,
12272         EL_DF_RECEIVER_DOWN,
12273         EL_DF_RECEIVER_LEFT
12274       },
12275       {
12276         EL_DF_SLOPE_1,
12277         EL_DF_SLOPE_4,
12278         EL_DF_SLOPE_3,
12279         EL_DF_SLOPE_2
12280       },
12281
12282       {
12283         -1,
12284       },
12285     };
12286     static int rotatable_elements_8[][8] =
12287     {
12288       {
12289         EL_DF_STEEL_GRID_FIXED_1,
12290         EL_DF_STEEL_GRID_FIXED_8,
12291         EL_DF_STEEL_GRID_FIXED_7,
12292         EL_DF_STEEL_GRID_FIXED_6,
12293         EL_DF_STEEL_GRID_FIXED_5,
12294         EL_DF_STEEL_GRID_FIXED_4,
12295         EL_DF_STEEL_GRID_FIXED_3,
12296         EL_DF_STEEL_GRID_FIXED_2
12297       },
12298       {
12299         EL_DF_WOODEN_GRID_FIXED_1,
12300         EL_DF_WOODEN_GRID_FIXED_8,
12301         EL_DF_WOODEN_GRID_FIXED_7,
12302         EL_DF_WOODEN_GRID_FIXED_6,
12303         EL_DF_WOODEN_GRID_FIXED_5,
12304         EL_DF_WOODEN_GRID_FIXED_4,
12305         EL_DF_WOODEN_GRID_FIXED_3,
12306         EL_DF_WOODEN_GRID_FIXED_2
12307       },
12308       {
12309         EL_DF_STEEL_GRID_ROTATING_1,
12310         EL_DF_STEEL_GRID_ROTATING_8,
12311         EL_DF_STEEL_GRID_ROTATING_7,
12312         EL_DF_STEEL_GRID_ROTATING_6,
12313         EL_DF_STEEL_GRID_ROTATING_5,
12314         EL_DF_STEEL_GRID_ROTATING_4,
12315         EL_DF_STEEL_GRID_ROTATING_3,
12316         EL_DF_STEEL_GRID_ROTATING_2
12317       },
12318       {
12319         EL_DF_WOODEN_GRID_ROTATING_1,
12320         EL_DF_WOODEN_GRID_ROTATING_8,
12321         EL_DF_WOODEN_GRID_ROTATING_7,
12322         EL_DF_WOODEN_GRID_ROTATING_6,
12323         EL_DF_WOODEN_GRID_ROTATING_5,
12324         EL_DF_WOODEN_GRID_ROTATING_4,
12325         EL_DF_WOODEN_GRID_ROTATING_3,
12326         EL_DF_WOODEN_GRID_ROTATING_2
12327       },
12328
12329       {
12330         -1,
12331       },
12332     };
12333     static int rotatable_elements_16[][16] =
12334     {
12335       {
12336         EL_MM_MIRROR_1,
12337         EL_MM_MIRROR_16,
12338         EL_MM_MIRROR_15,
12339         EL_MM_MIRROR_14,
12340         EL_MM_MIRROR_13,
12341         EL_MM_MIRROR_12,
12342         EL_MM_MIRROR_11,
12343         EL_MM_MIRROR_10,
12344         EL_MM_MIRROR_9,
12345         EL_MM_MIRROR_8,
12346         EL_MM_MIRROR_7,
12347         EL_MM_MIRROR_6,
12348         EL_MM_MIRROR_5,
12349         EL_MM_MIRROR_4,
12350         EL_MM_MIRROR_3,
12351         EL_MM_MIRROR_2
12352       },
12353       {
12354         EL_MM_TELEPORTER_5,
12355         EL_MM_TELEPORTER_4,
12356         EL_MM_TELEPORTER_3,
12357         EL_MM_TELEPORTER_2,
12358         EL_MM_TELEPORTER_1,
12359         EL_MM_TELEPORTER_16,
12360         EL_MM_TELEPORTER_15,
12361         EL_MM_TELEPORTER_14,
12362         EL_MM_TELEPORTER_13,
12363         EL_MM_TELEPORTER_12,
12364         EL_MM_TELEPORTER_11,
12365         EL_MM_TELEPORTER_10,
12366         EL_MM_TELEPORTER_9,
12367         EL_MM_TELEPORTER_8,
12368         EL_MM_TELEPORTER_7,
12369         EL_MM_TELEPORTER_6
12370       },
12371       {
12372         EL_MM_TELEPORTER_RED_5,
12373         EL_MM_TELEPORTER_RED_4,
12374         EL_MM_TELEPORTER_RED_3,
12375         EL_MM_TELEPORTER_RED_2,
12376         EL_MM_TELEPORTER_RED_1,
12377         EL_MM_TELEPORTER_RED_16,
12378         EL_MM_TELEPORTER_RED_15,
12379         EL_MM_TELEPORTER_RED_14,
12380         EL_MM_TELEPORTER_RED_13,
12381         EL_MM_TELEPORTER_RED_12,
12382         EL_MM_TELEPORTER_RED_11,
12383         EL_MM_TELEPORTER_RED_10,
12384         EL_MM_TELEPORTER_RED_9,
12385         EL_MM_TELEPORTER_RED_8,
12386         EL_MM_TELEPORTER_RED_7,
12387         EL_MM_TELEPORTER_RED_6
12388       },
12389       {
12390         EL_MM_TELEPORTER_YELLOW_5,
12391         EL_MM_TELEPORTER_YELLOW_4,
12392         EL_MM_TELEPORTER_YELLOW_3,
12393         EL_MM_TELEPORTER_YELLOW_2,
12394         EL_MM_TELEPORTER_YELLOW_1,
12395         EL_MM_TELEPORTER_YELLOW_16,
12396         EL_MM_TELEPORTER_YELLOW_15,
12397         EL_MM_TELEPORTER_YELLOW_14,
12398         EL_MM_TELEPORTER_YELLOW_13,
12399         EL_MM_TELEPORTER_YELLOW_12,
12400         EL_MM_TELEPORTER_YELLOW_11,
12401         EL_MM_TELEPORTER_YELLOW_10,
12402         EL_MM_TELEPORTER_YELLOW_9,
12403         EL_MM_TELEPORTER_YELLOW_8,
12404         EL_MM_TELEPORTER_YELLOW_7,
12405         EL_MM_TELEPORTER_YELLOW_6
12406       },
12407       {
12408         EL_MM_TELEPORTER_GREEN_5,
12409         EL_MM_TELEPORTER_GREEN_4,
12410         EL_MM_TELEPORTER_GREEN_3,
12411         EL_MM_TELEPORTER_GREEN_2,
12412         EL_MM_TELEPORTER_GREEN_1,
12413         EL_MM_TELEPORTER_GREEN_16,
12414         EL_MM_TELEPORTER_GREEN_15,
12415         EL_MM_TELEPORTER_GREEN_14,
12416         EL_MM_TELEPORTER_GREEN_13,
12417         EL_MM_TELEPORTER_GREEN_12,
12418         EL_MM_TELEPORTER_GREEN_11,
12419         EL_MM_TELEPORTER_GREEN_10,
12420         EL_MM_TELEPORTER_GREEN_9,
12421         EL_MM_TELEPORTER_GREEN_8,
12422         EL_MM_TELEPORTER_GREEN_7,
12423         EL_MM_TELEPORTER_GREEN_6
12424       },
12425       {
12426         EL_MM_TELEPORTER_BLUE_5,
12427         EL_MM_TELEPORTER_BLUE_4,
12428         EL_MM_TELEPORTER_BLUE_3,
12429         EL_MM_TELEPORTER_BLUE_2,
12430         EL_MM_TELEPORTER_BLUE_1,
12431         EL_MM_TELEPORTER_BLUE_16,
12432         EL_MM_TELEPORTER_BLUE_15,
12433         EL_MM_TELEPORTER_BLUE_14,
12434         EL_MM_TELEPORTER_BLUE_13,
12435         EL_MM_TELEPORTER_BLUE_12,
12436         EL_MM_TELEPORTER_BLUE_11,
12437         EL_MM_TELEPORTER_BLUE_10,
12438         EL_MM_TELEPORTER_BLUE_9,
12439         EL_MM_TELEPORTER_BLUE_8,
12440         EL_MM_TELEPORTER_BLUE_7,
12441         EL_MM_TELEPORTER_BLUE_6
12442       },
12443       {
12444         EL_MM_POLARIZER_1,
12445         EL_MM_POLARIZER_16,
12446         EL_MM_POLARIZER_15,
12447         EL_MM_POLARIZER_14,
12448         EL_MM_POLARIZER_13,
12449         EL_MM_POLARIZER_12,
12450         EL_MM_POLARIZER_11,
12451         EL_MM_POLARIZER_10,
12452         EL_MM_POLARIZER_9,
12453         EL_MM_POLARIZER_8,
12454         EL_MM_POLARIZER_7,
12455         EL_MM_POLARIZER_6,
12456         EL_MM_POLARIZER_5,
12457         EL_MM_POLARIZER_4,
12458         EL_MM_POLARIZER_3,
12459         EL_MM_POLARIZER_2
12460       },
12461       {
12462         EL_DF_MIRROR_1,
12463         EL_DF_MIRROR_16,
12464         EL_DF_MIRROR_15,
12465         EL_DF_MIRROR_14,
12466         EL_DF_MIRROR_13,
12467         EL_DF_MIRROR_12,
12468         EL_DF_MIRROR_11,
12469         EL_DF_MIRROR_10,
12470         EL_DF_MIRROR_9,
12471         EL_DF_MIRROR_8,
12472         EL_DF_MIRROR_7,
12473         EL_DF_MIRROR_6,
12474         EL_DF_MIRROR_5,
12475         EL_DF_MIRROR_4,
12476         EL_DF_MIRROR_3,
12477         EL_DF_MIRROR_2
12478       },
12479       {
12480         EL_DF_MIRROR_ROTATING_1,
12481         EL_DF_MIRROR_ROTATING_16,
12482         EL_DF_MIRROR_ROTATING_15,
12483         EL_DF_MIRROR_ROTATING_14,
12484         EL_DF_MIRROR_ROTATING_13,
12485         EL_DF_MIRROR_ROTATING_12,
12486         EL_DF_MIRROR_ROTATING_11,
12487         EL_DF_MIRROR_ROTATING_10,
12488         EL_DF_MIRROR_ROTATING_9,
12489         EL_DF_MIRROR_ROTATING_8,
12490         EL_DF_MIRROR_ROTATING_7,
12491         EL_DF_MIRROR_ROTATING_6,
12492         EL_DF_MIRROR_ROTATING_5,
12493         EL_DF_MIRROR_ROTATING_4,
12494         EL_DF_MIRROR_ROTATING_3,
12495         EL_DF_MIRROR_ROTATING_2
12496       },
12497       {
12498         EL_DF_MIRROR_FIXED_1,
12499         EL_DF_MIRROR_FIXED_16,
12500         EL_DF_MIRROR_FIXED_15,
12501         EL_DF_MIRROR_FIXED_14,
12502         EL_DF_MIRROR_FIXED_13,
12503         EL_DF_MIRROR_FIXED_12,
12504         EL_DF_MIRROR_FIXED_11,
12505         EL_DF_MIRROR_FIXED_10,
12506         EL_DF_MIRROR_FIXED_9,
12507         EL_DF_MIRROR_FIXED_8,
12508         EL_DF_MIRROR_FIXED_7,
12509         EL_DF_MIRROR_FIXED_6,
12510         EL_DF_MIRROR_FIXED_5,
12511         EL_DF_MIRROR_FIXED_4,
12512         EL_DF_MIRROR_FIXED_3,
12513         EL_DF_MIRROR_FIXED_2
12514       },
12515
12516       {
12517         -1,
12518       },
12519     };
12520     int i, j;
12521
12522     for (i = 0; swappable_elements[i][0] != -1; i++)
12523     {
12524       int element1 = swappable_elements[i][0];
12525       int element2 = swappable_elements[i][1];
12526
12527       if (old_element == element1 || old_element == element2)
12528         new_element = (old_element == element1 ? element2 : element1);
12529     }
12530
12531     for (i = 0; rotatable_elements_4[i][0] != -1; i++)
12532     {
12533       for (j = 0; j < 4; j++)
12534       {
12535         int element = rotatable_elements_4[i][j];
12536
12537         if (old_element == element)
12538           new_element = (button == 1 ? rotatable_elements_4[i][(j + 3) % 4] :
12539                          button == 2 ? rotatable_elements_4[i][0]           :
12540                          button == 3 ? rotatable_elements_4[i][(j + 1) % 4] :
12541                          old_element);
12542       }
12543     }
12544
12545     for (i = 0; rotatable_elements_8[i][0] != -1; i++)
12546     {
12547       for (j = 0; j < 8; j++)
12548       {
12549         int element = rotatable_elements_8[i][j];
12550
12551         if (old_element == element)
12552           new_element = (button == 1 ? rotatable_elements_8[i][(j + 7) % 8] :
12553                          button == 2 ? rotatable_elements_8[i][0]           :
12554                          button == 3 ? rotatable_elements_8[i][(j + 1) % 8] :
12555                          old_element);
12556       }
12557     }
12558
12559     for (i = 0; rotatable_elements_16[i][0] != -1; i++)
12560     {
12561       for (j = 0; j < 16; j++)
12562       {
12563         int element = rotatable_elements_16[i][j];
12564
12565         if (old_element == element)
12566           new_element = (button == 1 ? rotatable_elements_16[i][(j + 15) % 16] :
12567                          button == 2 ? rotatable_elements_16[i][0]             :
12568                          button == 3 ? rotatable_elements_16[i][(j + 1)  % 16] :
12569                          old_element);
12570       }
12571     }
12572
12573     if (old_element != new_element)
12574     {
12575       int max_infotext_len = getMaxInfoTextLength();
12576       char infotext[MAX_OUTPUT_LINESIZE + 1];
12577
12578       strncpy(infotext, getElementInfoText(new_element), max_infotext_len);
12579       infotext[max_infotext_len] = '\0';
12580
12581       ClearEditorGadgetInfoText();
12582
12583       DrawTextS(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY, INFOTEXT_FONT,
12584                 infotext);
12585     }
12586   }
12587
12588   if (IS_MM_WALL_EDITOR(new_element))
12589     SetElementSimpleExt(x, y, dx, dy, new_element, change_level);
12590   else
12591     SetElementSimple(x, y, new_element, change_level);
12592
12593   last_x = x;
12594   last_y = y;
12595 }
12596
12597 static void ResetIntelliDraw(void)
12598 {
12599   int x, y;
12600
12601   for (x = 0; x < lev_fieldx; x++)
12602     for (y = 0; y < lev_fieldy; y++)
12603       IntelliDrawBuffer[x][y] = Tile[x][y];
12604
12605   SetElementIntelliDraw(-1, -1, -1, -1, EL_UNDEFINED, FALSE, -1);
12606 }
12607
12608 static boolean draw_mode_hires = FALSE;
12609
12610 static boolean isHiresTileElement(int element)
12611 {
12612   return (IS_MM_WALL(element)        || element == EL_EMPTY);
12613 }
12614
12615 static boolean isHiresDrawElement(int element)
12616 {
12617   return (IS_MM_WALL_EDITOR(element) || element == EL_EMPTY);
12618 }
12619
12620 static int numHiresTiles(int element)
12621 {
12622   if (IS_MM_WALL(element))
12623     return get_number_of_bits(MM_WALL_BITS(element));
12624
12625   return 1;
12626 }
12627
12628 static void SetDrawModeHiRes(int element)
12629 {
12630   draw_mode_hires =
12631     (level.game_engine_type == GAME_ENGINE_TYPE_MM &&
12632      isHiresDrawElement(element));
12633 }
12634
12635 static boolean getDrawModeHiRes(void)
12636 {
12637   return draw_mode_hires;
12638 }
12639
12640 static int getLoResScreenPos(int pos)
12641 {
12642   return (getDrawModeHiRes() ? pos / 2 : pos);
12643 }
12644
12645 static int getLoResScreenMod(int pos)
12646 {
12647   return (getDrawModeHiRes() ? pos % 2 : 0);
12648 }
12649
12650 static void SetElementExt(int x, int y, int dx, int dy, int element,
12651                           boolean change_level, int button)
12652 {
12653   if (element < 0)
12654     SetElementSimple(x, y, Tile[x][y], change_level);
12655   else if (GetKeyModState() & KMOD_Shift)
12656     SetElementIntelliDraw(x, y, dx, dy, element, change_level, button);
12657   else
12658     SetElementSimpleExt(x, y, dx, dy, element, change_level);
12659 }
12660
12661 static void SetElement(int x, int y, int element)
12662 {
12663   SetElementExt(x, y, 0, 0, element, TRUE, -1);
12664 }
12665
12666 static void SetElementButton(int x, int y, int dx, int dy, int element,
12667                              int button)
12668 {
12669   SetElementExt(x, y, dx, dy, element, TRUE, button);
12670 }
12671
12672 static void SetElementHiRes(int sx2, int sy2, int element, boolean change_level)
12673 {
12674   int lx = getLoResScreenPos(sx2) + level_xpos;
12675   int ly = getLoResScreenPos(sy2) + level_ypos;
12676   int dx = getLoResScreenMod(sx2);
12677   int dy = getLoResScreenMod(sy2);
12678
12679   SetElementExt(lx, ly, dx, dy, element, change_level, -1);
12680 }
12681
12682 static void SetLevelElementHiRes(int lx2, int ly2, int element)
12683 {
12684   int lx = lx2 / 2;
12685   int ly = ly2 / 2;
12686   int dx = lx2 % 2;
12687   int dy = ly2 % 2;
12688
12689   SetElementExt(lx, ly, dx, dy, element, TRUE, -1);
12690 }
12691
12692 static int getLevelElementHiRes(int lx2, int ly2)
12693 {
12694   int lx = lx2 / 2;
12695   int ly = ly2 / 2;
12696   int dx = lx2 % 2;
12697   int dy = ly2 % 2;
12698   int element = Tile[lx][ly];
12699   unsigned int bitmask = (dx + 1) << (dy * 2);
12700
12701   if (IS_MM_WALL(element))
12702   {
12703     if (element & bitmask)
12704       return map_mm_wall_element(element);
12705     else
12706       return EL_EMPTY;
12707   }
12708
12709   return element;
12710 }
12711
12712 static void DrawLineElement(int x, int y, int element, boolean change_level)
12713 {
12714   SetElementHiRes(x, y, element, change_level);
12715 }
12716
12717 static void DrawLine(int from_x, int from_y, int to_x, int to_y,
12718                      int element, boolean change_level)
12719 {
12720   int xsize = ABS(to_x - from_x);
12721   int ysize = ABS(to_y - from_y);
12722   int dx = (to_x < from_x ? -1 : +1);
12723   int dy = (to_y < from_y ? -1 : +1);
12724   int i;
12725
12726   if (from_y == to_y)                   // horizontal line
12727   {
12728     for (i = 0; i <= xsize; i++)
12729       DrawLineElement(from_x + i * dx, from_y, element, change_level);
12730   }
12731   else if (from_x == to_x)              // vertical line
12732   {
12733     for (i = 0; i <= ysize; i++)
12734       DrawLineElement(from_x, from_y + i * dy, element, change_level);
12735   }
12736   else                                  // diagonal line
12737   {
12738     if (ysize < xsize)                  // a < 1
12739     {
12740       float a = (float)ysize / (float)xsize;
12741
12742       for (i = 0; i <= xsize; i++)
12743       {
12744         int x = dx * i;
12745         int y = dy * (int)(a * i + 0.5);
12746
12747         DrawLineElement(from_x + x, from_y + y, element, change_level);
12748       }
12749     }
12750     else                                // a >= 1
12751     {
12752       float a = (float)xsize / (float)ysize;
12753
12754       for (i = 0; i <= ysize; i++)
12755       {
12756         int x = dx * (int)(a * i + 0.5);
12757         int y = dy * i;
12758
12759         DrawLineElement(from_x + x, from_y + y, element, change_level);
12760       }
12761     }
12762   }
12763 }
12764
12765 static void DrawBox(int from_x, int from_y, int to_x, int to_y,
12766                     int element, boolean change_level)
12767 {
12768   DrawLine(from_x, from_y, from_x, to_y, element, change_level);
12769   DrawLine(from_x, to_y, to_x, to_y, element, change_level);
12770   DrawLine(to_x, to_y, to_x, from_y, element, change_level);
12771   DrawLine(to_x, from_y, from_x, from_y, element, change_level);
12772 }
12773
12774 static void DrawFilledBox(int from_x, int from_y, int to_x, int to_y,
12775                           int element, boolean change_level)
12776 {
12777   int y;
12778
12779   if (from_y > to_y)
12780     swap_number_pairs(&from_x, &from_y, &to_x, &to_y);
12781
12782   for (y = from_y; y <= to_y; y++)
12783     DrawLine(from_x, y, to_x, y, element, change_level);
12784 }
12785
12786 static void DrawArcExt(int from_x, int from_y, int to_x2, int to_y2,
12787                        int element, boolean change_level)
12788 {
12789   int to_x = to_x2 - (to_x2 > from_x ? +1 : -1);
12790   int to_y = to_y2 - (to_y2 > from_y ? +1 : -1);
12791   int len_x = ABS(to_x - from_x);
12792   int len_y = ABS(to_y - from_y);
12793   int radius, x, y;
12794
12795   radius = (int)(sqrt((float)(len_x * len_x + len_y * len_y)) + 0.5);
12796
12797   // not optimal (some points get drawn twice) but simple,
12798   // and fast enough for the few points we are drawing
12799
12800   for (x = 0; x <= radius; x++)
12801   {
12802     int sx, sy, sx2, sy2, lx, ly;
12803
12804     y = (int)(sqrt((float)(radius * radius - x * x)) + 0.5);
12805
12806     sx2 = from_x + x * (from_x < to_x2 ? +1 : -1);
12807     sy2 = from_y + y * (from_y < to_y2 ? +1 : -1);
12808     sx = getLoResScreenPos(sx2);
12809     sy = getLoResScreenPos(sy2);
12810     lx = sx + level_xpos;
12811     ly = sy + level_ypos;
12812
12813     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
12814       DrawLineElement(sx2, sy2, element, change_level);
12815   }
12816
12817   for (y = 0; y <= radius; y++)
12818   {
12819     int sx, sy, sx2, sy2, lx, ly;
12820
12821     x = (int)(sqrt((float)(radius * radius - y * y)) + 0.5);
12822
12823     sx2 = from_x + x * (from_x < to_x2 ? +1 : -1);
12824     sy2 = from_y + y * (from_y < to_y2 ? +1 : -1);
12825     sx = getLoResScreenPos(sx2);
12826     sy = getLoResScreenPos(sy2);
12827     lx = sx + level_xpos;
12828     ly = sy + level_ypos;
12829
12830     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
12831       DrawLineElement(sx2, sy2, element, change_level);
12832   }
12833 }
12834
12835 static void DrawArc(int from_x, int from_y, int to_x, int to_y,
12836                     int element, boolean change_level)
12837 {
12838   int to_x2 = to_x + (to_x < from_x ? -1 : +1);
12839   int to_y2 = to_y + (to_y > from_y ? +1 : -1);
12840
12841   DrawArcExt(from_x, from_y, to_x2, to_y2, element, change_level);
12842 }
12843
12844 #define DRAW_CIRCLES_BUTTON_AVAILABLE   0
12845
12846 #if DRAW_CIRCLES_BUTTON_AVAILABLE
12847 static void DrawCircle(int from_x, int from_y, int to_x, int to_y,
12848                        int element, boolean change_level)
12849 {
12850   int to_x2 = to_x + (to_x < from_x ? -1 : +1);
12851   int to_y2 = to_y + (to_y > from_y ? +1 : -1);
12852   int mirror_to_x2 = from_x - (to_x2 - from_x);
12853   int mirror_to_y2 = from_y - (to_y2 - from_y);
12854
12855   DrawArcExt(from_x, from_y, to_x2, to_y2, element, change_level);
12856   DrawArcExt(from_x, from_y, mirror_to_x2, to_y2, element, change_level);
12857   DrawArcExt(from_x, from_y, to_x2, mirror_to_y2, element, change_level);
12858   DrawArcExt(from_x, from_y, mirror_to_x2, mirror_to_y2, element, change_level);
12859 }
12860 #endif
12861
12862 static void DrawAreaBorder(int from_x, int from_y, int to_x, int to_y)
12863 {
12864   int from_sx, from_sy;
12865   int to_sx, to_sy;
12866
12867   if (from_x > to_x)
12868     swap_numbers(&from_x, &to_x);
12869
12870   if (from_y > to_y)
12871     swap_numbers(&from_y, &to_y);
12872
12873   from_sx = SX + from_x * ed_tilesize;
12874   from_sy = SY + from_y * ed_tilesize;
12875   to_sx = SX + (to_x + 1) * ed_tilesize - 1;
12876   to_sy = SY + (to_y + 1) * ed_tilesize - 1;
12877
12878   DrawSimpleWhiteLine(drawto, from_sx, from_sy, to_sx, from_sy);
12879   DrawSimpleWhiteLine(drawto, to_sx, from_sy, to_sx, to_sy);
12880   DrawSimpleWhiteLine(drawto, to_sx, to_sy, from_sx, to_sy);
12881   DrawSimpleWhiteLine(drawto, from_sx, to_sy, from_sx, from_sy);
12882
12883   if (from_x == to_x && from_y == to_y)
12884     MarkTileDirty(from_x/2, from_y/2);
12885   else
12886     redraw_mask |= REDRAW_FIELD;
12887 }
12888
12889 static void DrawAreaBox(int from_x, int from_y, int to_x, int to_y,
12890                         int element, boolean change_level)
12891 {
12892   DrawBox(from_x, from_y, to_x, to_y, element, change_level);
12893 }
12894
12895 static void SelectArea(int from_x, int from_y, int to_x, int to_y,
12896                        int element, boolean change_level)
12897 {
12898   if (element == -1 || change_level)
12899     DrawAreaBox(from_x, from_y, to_x, to_y, -1, FALSE);
12900   else
12901     DrawAreaBorder(from_x, from_y, to_x, to_y);
12902 }
12903
12904 // values for CopyBrushExt()
12905 #define CB_AREA_TO_BRUSH                0
12906 #define CB_BRUSH_TO_CURSOR              1
12907 #define CB_BRUSH_TO_LEVEL               2
12908 #define CB_DELETE_OLD_CURSOR            3
12909 #define CB_DUMP_BRUSH                   4
12910 #define CB_DUMP_BRUSH_SMALL             5
12911 #define CB_CLIPBOARD_TO_BRUSH           6
12912 #define CB_BRUSH_TO_CLIPBOARD           7
12913 #define CB_BRUSH_TO_CLIPBOARD_SMALL     8
12914 #define CB_UPDATE_BRUSH_POSITION        9
12915 #define CB_FLIP_BRUSH_X                 10
12916 #define CB_FLIP_BRUSH_Y                 11
12917 #define CB_FLIP_BRUSH_XY                12
12918
12919 #define MAX_CB_PART_SIZE        10
12920 #define MAX_CB_LINE_SIZE        (MAX_LEV_FIELDX + 1)    // text plus newline
12921 #define MAX_CB_NUM_LINES        (MAX_LEV_FIELDY)
12922 #define MAX_CB_TEXT_SIZE        (MAX_CB_LINE_SIZE *     \
12923                                  MAX_CB_NUM_LINES *     \
12924                                  MAX_CB_PART_SIZE)
12925
12926 static int getFlippedTileExt(int map[], int element)
12927 {
12928   int i;
12929
12930   for (i = 0; map[i] != -1; i++)
12931     if (map[i] == element)
12932       return map[i ^ 1];        // get flipped element by flipping LSB of index
12933
12934   return element;
12935 }
12936
12937 static int getFlippedTileX(int element)
12938 {
12939   int map[] =
12940   {
12941     EL_BD_BUTTERFLY_LEFT,               EL_BD_BUTTERFLY_RIGHT,
12942     EL_BD_FIREFLY_LEFT,                 EL_BD_FIREFLY_RIGHT,
12943     EL_BUG_LEFT,                        EL_BUG_RIGHT,
12944     EL_SPACESHIP_LEFT,                  EL_SPACESHIP_RIGHT,
12945     EL_PACMAN_LEFT,                     EL_PACMAN_RIGHT,
12946     EL_ARROW_LEFT,                      EL_ARROW_RIGHT,
12947     EL_MOLE_LEFT,                       EL_MOLE_RIGHT,
12948     EL_BALLOON_SWITCH_LEFT,             EL_BALLOON_SWITCH_RIGHT,
12949     EL_YAMYAM_LEFT,                     EL_YAMYAM_RIGHT,
12950     EL_SP_PORT_LEFT,                    EL_SP_PORT_RIGHT,
12951     EL_SP_GRAVITY_PORT_LEFT,            EL_SP_GRAVITY_PORT_RIGHT,
12952     EL_SP_GRAVITY_ON_PORT_LEFT,         EL_SP_GRAVITY_ON_PORT_RIGHT,
12953     EL_SP_GRAVITY_OFF_PORT_LEFT,        EL_SP_GRAVITY_OFF_PORT_RIGHT,
12954     EL_CONVEYOR_BELT_1_LEFT,            EL_CONVEYOR_BELT_1_RIGHT,
12955     EL_CONVEYOR_BELT_2_LEFT,            EL_CONVEYOR_BELT_2_RIGHT,
12956     EL_CONVEYOR_BELT_3_LEFT,            EL_CONVEYOR_BELT_3_RIGHT,
12957     EL_CONVEYOR_BELT_4_LEFT,            EL_CONVEYOR_BELT_4_RIGHT,
12958     EL_SPRING_LEFT,                     EL_SPRING_RIGHT,
12959     EL_SP_CHIP_LEFT,                    EL_SP_CHIP_RIGHT,
12960     EL_TUBE_VERTICAL_LEFT,              EL_TUBE_VERTICAL_RIGHT,
12961     EL_TUBE_LEFT_UP,                    EL_TUBE_RIGHT_UP,
12962     EL_TUBE_LEFT_DOWN,                  EL_TUBE_RIGHT_DOWN,
12963     EL_DC_STEELWALL_1_LEFT,             EL_DC_STEELWALL_1_RIGHT,
12964     EL_DC_STEELWALL_1_TOPLEFT,          EL_DC_STEELWALL_1_TOPRIGHT,
12965     EL_DC_STEELWALL_1_BOTTOMLEFT,       EL_DC_STEELWALL_1_BOTTOMRIGHT,
12966     EL_DC_STEELWALL_1_TOPLEFT_2,        EL_DC_STEELWALL_1_TOPRIGHT_2,
12967     EL_DC_STEELWALL_1_BOTTOMLEFT_2,     EL_DC_STEELWALL_1_BOTTOMRIGHT_2,
12968     EL_DC_STEELWALL_2_LEFT,             EL_DC_STEELWALL_2_RIGHT,
12969     EL_ACID_POOL_TOPLEFT,               EL_ACID_POOL_TOPRIGHT,
12970     EL_ACID_POOL_BOTTOMLEFT,            EL_ACID_POOL_BOTTOMRIGHT,
12971
12972     -1
12973   };
12974
12975   return getFlippedTileExt(map, element);
12976 }
12977
12978 static int getFlippedTileY(int element)
12979 {
12980   int map[] =
12981   {
12982     EL_BD_BUTTERFLY_UP,                 EL_BD_BUTTERFLY_DOWN,
12983     EL_BD_FIREFLY_UP,                   EL_BD_FIREFLY_DOWN,
12984     EL_BUG_UP,                          EL_BUG_DOWN,
12985     EL_SPACESHIP_UP,                    EL_SPACESHIP_DOWN,
12986     EL_PACMAN_UP,                       EL_PACMAN_DOWN,
12987     EL_ARROW_UP,                        EL_ARROW_DOWN,
12988     EL_MOLE_UP,                         EL_MOLE_DOWN,
12989     EL_BALLOON_SWITCH_UP,               EL_BALLOON_SWITCH_DOWN,
12990     EL_YAMYAM_UP,                       EL_YAMYAM_DOWN,
12991     EL_SP_PORT_UP,                      EL_SP_PORT_DOWN,
12992     EL_SP_GRAVITY_PORT_UP,              EL_SP_GRAVITY_PORT_DOWN,
12993     EL_SP_GRAVITY_ON_PORT_UP,           EL_SP_GRAVITY_ON_PORT_DOWN,
12994     EL_SP_GRAVITY_OFF_PORT_UP,          EL_SP_GRAVITY_OFF_PORT_DOWN,
12995     EL_SP_CHIP_TOP,                     EL_SP_CHIP_BOTTOM,
12996     EL_TUBE_HORIZONTAL_UP,              EL_TUBE_HORIZONTAL_DOWN,
12997     EL_TUBE_LEFT_UP,                    EL_TUBE_LEFT_DOWN,
12998     EL_TUBE_RIGHT_UP,                   EL_TUBE_RIGHT_DOWN,
12999     EL_DC_STEELWALL_1_TOP,              EL_DC_STEELWALL_1_BOTTOM,
13000     EL_DC_STEELWALL_1_TOPLEFT,          EL_DC_STEELWALL_1_BOTTOMLEFT,
13001     EL_DC_STEELWALL_1_TOPRIGHT,         EL_DC_STEELWALL_1_BOTTOMRIGHT,
13002     EL_DC_STEELWALL_1_TOPLEFT_2,        EL_DC_STEELWALL_1_BOTTOMLEFT_2,
13003     EL_DC_STEELWALL_1_TOPRIGHT_2,       EL_DC_STEELWALL_1_BOTTOMRIGHT_2,
13004     EL_DC_STEELWALL_2_TOP,              EL_DC_STEELWALL_2_BOTTOM,
13005     EL_EMC_WALL_1,                      EL_EMC_WALL_3,
13006
13007     -1
13008   };
13009
13010   return getFlippedTileExt(map, element);
13011 }
13012
13013 static int getFlippedTileXY(int element)
13014 {
13015   int map[] =
13016   {
13017     EL_BD_BUTTERFLY_LEFT,               EL_BD_BUTTERFLY_UP,
13018     EL_BD_BUTTERFLY_RIGHT,              EL_BD_BUTTERFLY_DOWN,
13019     EL_BD_FIREFLY_LEFT,                 EL_BD_FIREFLY_UP,
13020     EL_BD_FIREFLY_RIGHT,                EL_BD_FIREFLY_DOWN,
13021     EL_BUG_LEFT,                        EL_BUG_UP,
13022     EL_BUG_RIGHT,                       EL_BUG_DOWN,
13023     EL_SPACESHIP_LEFT,                  EL_SPACESHIP_UP,
13024     EL_SPACESHIP_RIGHT,                 EL_SPACESHIP_DOWN,
13025     EL_PACMAN_LEFT,                     EL_PACMAN_UP,
13026     EL_PACMAN_RIGHT,                    EL_PACMAN_DOWN,
13027     EL_ARROW_LEFT,                      EL_ARROW_UP,
13028     EL_ARROW_RIGHT,                     EL_ARROW_DOWN,
13029     EL_MOLE_LEFT,                       EL_MOLE_UP,
13030     EL_MOLE_RIGHT,                      EL_MOLE_DOWN,
13031     EL_BALLOON_SWITCH_LEFT,             EL_BALLOON_SWITCH_UP,
13032     EL_BALLOON_SWITCH_RIGHT,            EL_BALLOON_SWITCH_DOWN,
13033     EL_YAMYAM_LEFT,                     EL_YAMYAM_UP,
13034     EL_YAMYAM_RIGHT,                    EL_YAMYAM_DOWN,
13035     EL_SP_PORT_LEFT,                    EL_SP_PORT_UP,
13036     EL_SP_PORT_RIGHT,                   EL_SP_PORT_DOWN,
13037     EL_SP_GRAVITY_PORT_LEFT,            EL_SP_GRAVITY_PORT_UP,
13038     EL_SP_GRAVITY_PORT_RIGHT,           EL_SP_GRAVITY_PORT_DOWN,
13039     EL_SP_GRAVITY_ON_PORT_LEFT,         EL_SP_GRAVITY_ON_PORT_UP,
13040     EL_SP_GRAVITY_ON_PORT_RIGHT,        EL_SP_GRAVITY_ON_PORT_DOWN,
13041     EL_SP_GRAVITY_OFF_PORT_LEFT,        EL_SP_GRAVITY_OFF_PORT_UP,
13042     EL_SP_GRAVITY_OFF_PORT_RIGHT,       EL_SP_GRAVITY_OFF_PORT_DOWN,
13043     EL_SP_CHIP_LEFT,                    EL_SP_CHIP_TOP,
13044     EL_SP_CHIP_RIGHT,                   EL_SP_CHIP_BOTTOM,
13045     EL_TUBE_VERTICAL,                   EL_TUBE_HORIZONTAL,
13046     EL_TUBE_VERTICAL_LEFT,              EL_TUBE_HORIZONTAL_UP,
13047     EL_TUBE_VERTICAL_RIGHT,             EL_TUBE_HORIZONTAL_DOWN,
13048     EL_TUBE_LEFT_DOWN,                  EL_TUBE_RIGHT_UP,
13049     EL_DC_STEELWALL_1_LEFT,             EL_DC_STEELWALL_1_TOP,
13050     EL_DC_STEELWALL_1_RIGHT,            EL_DC_STEELWALL_1_BOTTOM,
13051     EL_DC_STEELWALL_1_HORIZONTAL,       EL_DC_STEELWALL_1_VERTICAL,
13052     EL_DC_STEELWALL_1_TOPRIGHT,         EL_DC_STEELWALL_1_BOTTOMLEFT,
13053     EL_DC_STEELWALL_1_TOPRIGHT_2,       EL_DC_STEELWALL_1_BOTTOMLEFT_2,
13054     EL_DC_STEELWALL_2_LEFT,             EL_DC_STEELWALL_2_TOP,
13055     EL_DC_STEELWALL_2_RIGHT,            EL_DC_STEELWALL_2_BOTTOM,
13056     EL_DC_STEELWALL_2_HORIZONTAL,       EL_DC_STEELWALL_2_VERTICAL,
13057     EL_EXPANDABLE_WALL_HORIZONTAL,      EL_EXPANDABLE_WALL_VERTICAL,
13058     EL_EXPANDABLE_STEELWALL_HORIZONTAL, EL_EXPANDABLE_STEELWALL_VERTICAL,
13059
13060     -1
13061   };
13062
13063   return getFlippedTileExt(map, element);
13064 }
13065
13066 static int getFlippedTile(int element, int mode)
13067 {
13068   if (IS_MM_ELEMENT(element))
13069   {
13070     // get MM game element
13071     element = map_element_RND_to_MM(element);
13072
13073     // get flipped game element
13074     element = (mode == CB_FLIP_BRUSH_X  ? getFlippedTileX_MM(element) :
13075                mode == CB_FLIP_BRUSH_Y  ? getFlippedTileY_MM(element) :
13076                mode == CB_FLIP_BRUSH_XY ? getFlippedTileXY_MM(element) :
13077                element);
13078
13079     // get RND game element again
13080     element = map_element_MM_to_RND(element);
13081   }
13082   else
13083   {
13084     // get flipped game element
13085     element = (mode == CB_FLIP_BRUSH_X  ? getFlippedTileX(element) :
13086                mode == CB_FLIP_BRUSH_Y  ? getFlippedTileY(element) :
13087                mode == CB_FLIP_BRUSH_XY ? getFlippedTileXY(element) :
13088                element);
13089   }
13090
13091   return element;
13092 }
13093
13094 static void SwapFlippedTiles(short *tile1, short *tile2, int mode)
13095 {
13096   // flip tiles
13097   short tile1_flipped = getFlippedTile(*tile1, mode);
13098   short tile2_flipped = getFlippedTile(*tile2, mode);
13099
13100   // swap tiles
13101   *tile1 = tile2_flipped;
13102   *tile2 = tile1_flipped;
13103 }
13104
13105 static void DrawBrushElement(int sx, int sy, int element, boolean change_level)
13106 {
13107   DrawLineElement(sx, sy, element, change_level);
13108 }
13109
13110 static void CopyBrushExt(int from_x, int from_y, int to_x, int to_y,
13111                          int button, int mode)
13112 {
13113   static short brush_buffer[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
13114   static int brush_width, brush_height;
13115   static int last_cursor_x = -1, last_cursor_y = -1;
13116   static boolean delete_old_brush = FALSE;
13117   int new_element = BUTTON_ELEMENT(button);
13118   int x, y;
13119
13120   if (mode == CB_DUMP_BRUSH ||
13121       mode == CB_DUMP_BRUSH_SMALL ||
13122       mode == CB_BRUSH_TO_CLIPBOARD ||
13123       mode == CB_BRUSH_TO_CLIPBOARD_SMALL)
13124   {
13125     if (edit_mode != ED_MODE_DRAWING)
13126       return;
13127
13128     char part[MAX_CB_PART_SIZE + 1] = "";
13129     char text[MAX_CB_TEXT_SIZE + 1] = "";
13130     int width  = (draw_with_brush ? brush_width  : lev_fieldx);
13131     int height = (draw_with_brush ? brush_height : lev_fieldy);
13132     char *format = "%s%03d";
13133
13134     for (y = 0; y < height; y++)
13135       for (x = 0; x < width; x++)
13136         if ((draw_with_brush ? brush_buffer[x][y] : Tile[x][y]) > 999)
13137           format = "%s%04d";
13138
13139     for (y = 0; y < height; y++)
13140     {
13141       for (x = 0; x < width; x++)
13142       {
13143         int element = (draw_with_brush ? brush_buffer[x][y] : Tile[x][y]);
13144         char *prefix = (mode == CB_DUMP_BRUSH ||
13145                         mode == CB_BRUSH_TO_CLIPBOARD ? "`" : "¸");
13146
13147         if (element >= NUM_FILE_ELEMENTS)
13148           element = EL_UNKNOWN;
13149
13150         // copy brush to level sketch text buffer for the R'n'D forum:
13151         // - large tiles: `xxx or `xxxx (0x60 ASCII)
13152         // - small tiles: Â¸xxx or Â¸xxxx (0xb8 ISO-8859-1, 0xc2b8 UTF-8)
13153         snprintf(part, MAX_CB_PART_SIZE + 1, format, prefix, element);
13154         strcat(text, part);
13155       }
13156
13157       strcat(text, "\n");
13158     }
13159
13160     if (mode == CB_BRUSH_TO_CLIPBOARD ||
13161         mode == CB_BRUSH_TO_CLIPBOARD_SMALL)
13162       SDL_SetClipboardText(text);
13163     else
13164       Print("%s", text);        // print brush data to console and log file
13165
13166     return;
13167   }
13168
13169   if (mode == CB_CLIPBOARD_TO_BRUSH)
13170   {
13171     if (edit_mode != ED_MODE_DRAWING)
13172       return;
13173
13174     if (!SDL_HasClipboardText())
13175     {
13176       Request("Clipboard is empty!", REQ_CONFIRM);
13177
13178       return;
13179     }
13180
13181     boolean copy_to_brush = (draw_with_brush ||
13182                              drawing_function == GADGET_ID_GRAB_BRUSH);
13183
13184     // this will delete the old brush, if already drawing with a brush
13185     if (copy_to_brush)
13186       ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS], MB_LEFTBUTTON);
13187
13188     // initialization is required for "odd" (incomplete) clipboard content
13189     for (x = 0; x < MAX_LEV_FIELDX; x++)
13190       for (y = 0; y < MAX_LEV_FIELDY; y++)
13191         brush_buffer[x][y] = EL_EMPTY;
13192
13193     brush_width  = 0;
13194     brush_height = 0;
13195     x = 0;
13196     y = 0;
13197
13198     char *clipboard_text = SDL_GetClipboardText();
13199     char *ptr = clipboard_text;
13200     boolean allow_new_row = FALSE;
13201     boolean stop = FALSE;
13202
13203     while (*ptr && !stop)
13204     {
13205       boolean prefix_found = FALSE;
13206       boolean start_new_row = FALSE;
13207
13208       // level sketch element number prefixes (may be multi-byte characters)
13209       char *prefix_list[] = { "`", "¸" };
13210       int i;
13211
13212       for (i = 0; i < ARRAY_SIZE(prefix_list); i++)
13213       {
13214         char *prefix = prefix_list[i];
13215
13216         // check if string is large enough for prefix
13217         if (strlen(ptr) < strlen(prefix))
13218         {
13219           stop = TRUE;
13220
13221           break;
13222         }
13223
13224         // check if string starts with prefix
13225         if (strPrefix(ptr, prefix))
13226         {
13227           ptr += strlen(prefix);
13228
13229           prefix_found = TRUE;
13230
13231           break;
13232         }
13233       }
13234
13235       // check if prefix found and followed by (at least) three digits
13236       if (prefix_found &&
13237           strlen(ptr) >= 3 &&
13238           ptr[0] >= '0' && ptr[0] <= '9' &&
13239           ptr[1] >= '0' && ptr[1] <= '9' &&
13240           ptr[2] >= '0' && ptr[2] <= '9')
13241       {
13242         int element = ((ptr[0] - '0') * 100 +
13243                        (ptr[1] - '0') * 10 +
13244                        (ptr[2] - '0'));
13245
13246         ptr += 3;
13247
13248         // level sketch element number might consist of four digits
13249         if (ptr[0] >= '0' && ptr[0] <= '9')
13250         {
13251           element = element * 10 + (ptr[0] - '0');
13252           ptr++;
13253         }
13254
13255         // remap some (historic, now obsolete) elements
13256         element = getMappedElement(element);
13257
13258         if (element >= NUM_FILE_ELEMENTS)
13259           element = EL_UNKNOWN;
13260
13261         brush_buffer[x][y] = element;
13262
13263         brush_width  = MAX(x + 1, brush_width);
13264         brush_height = MAX(y + 1, brush_height);
13265
13266         x++;
13267
13268         if (x >= MAX_LEV_FIELDX)
13269           start_new_row = TRUE;
13270
13271         allow_new_row = TRUE;
13272       }
13273       else
13274       {
13275         if ((*ptr == '\n' || *ptr == '\r') && allow_new_row)
13276           start_new_row = TRUE;
13277
13278         ptr++;          // !!! FIX THIS for real UTF-8 handling !!!
13279       }
13280
13281       if (start_new_row)
13282       {
13283         x = 0;
13284         y++;
13285
13286         if (y >= MAX_LEV_FIELDY)
13287           stop = TRUE;
13288
13289         allow_new_row = FALSE;
13290       }
13291     }
13292
13293     SDL_free(clipboard_text);
13294
13295     if (brush_width == 0 || brush_height == 0)
13296     {
13297       Request("No level sketch found in clipboard!", REQ_CONFIRM);
13298
13299       return;
13300     }
13301
13302     if (copy_to_brush)
13303     {
13304       struct GadgetInfo *gi = level_editor_gadget[GADGET_ID_DRAWING_LEVEL];
13305       int mx, my;
13306
13307       SDL_GetMouseState(&mx, &my);
13308
13309       // if inside drawing area, activate and draw brush at last mouse position
13310       if (mx >= gi->x && mx < gi->x + gi->width &&
13311           my >= gi->y && my < gi->y + gi->height)
13312         CopyBrushToCursor(last_cursor_x, last_cursor_y);
13313
13314       draw_with_brush = TRUE;
13315     }
13316     else
13317     {
13318       char request[100];
13319
13320       sprintf(request, "Replace level with %dx%d level sketch from clipboard?",
13321               brush_width, brush_height);
13322
13323       if (!Request(request, REQ_ASK))
13324         return;
13325
13326       for (x = 0; x < MAX_LEV_FIELDX; x++)
13327         for (y = 0; y < MAX_LEV_FIELDY; y++)
13328           Tile[x][y] = brush_buffer[x][y];
13329
13330       lev_fieldx = level.fieldx = brush_width;
13331       lev_fieldy = level.fieldy = brush_height;
13332
13333       boolean use_bd_engine = TRUE;
13334       boolean use_em_engine = TRUE;
13335       boolean use_sp_engine = TRUE;
13336       boolean use_mm_engine = TRUE;
13337
13338       for (x = 0; x < MAX_LEV_FIELDX; x++)
13339       {
13340         for (y = 0; y < MAX_LEV_FIELDY; y++)
13341         {
13342           int element = Tile[x][y];
13343
13344           if (!IS_BD_ELEMENT(element) && !IS_PLAYER_ELEMENT(element))
13345             use_bd_engine = FALSE;
13346
13347           if (!IS_EM_ELEMENT(element) && !IS_PLAYER_ELEMENT(element))
13348             use_em_engine = FALSE;
13349
13350           if (!IS_SP_ELEMENT(element))
13351             use_sp_engine = FALSE;
13352
13353           if (!IS_MM_ELEMENT(element) && element != EL_EMPTY)
13354             use_mm_engine = FALSE;
13355         }
13356       }
13357
13358       level.game_engine_type = (use_bd_engine ? GAME_ENGINE_TYPE_BD :
13359                                 use_em_engine ? GAME_ENGINE_TYPE_EM :
13360                                 use_sp_engine ? GAME_ENGINE_TYPE_SP :
13361                                 use_mm_engine ? GAME_ENGINE_TYPE_MM :
13362                                 GAME_ENGINE_TYPE_RND);
13363
13364       // update element selection list
13365       ReinitializeElementList();
13366       ModifyEditorElementList();
13367
13368       SetBorderElement();
13369
13370       DrawEditModeWindow();
13371       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13372     }
13373
13374     return;
13375   }
13376
13377   if (mode == CB_DELETE_OLD_CURSOR && !delete_old_brush)
13378     return;
13379
13380   if (mode == CB_AREA_TO_BRUSH)
13381   {
13382     int from_lx, from_ly;
13383
13384     if (from_x > to_x)
13385       swap_numbers(&from_x, &to_x);
13386
13387     if (from_y > to_y)
13388       swap_numbers(&from_y, &to_y);
13389
13390     brush_width = to_x - from_x + 1;
13391     brush_height = to_y - from_y + 1;
13392
13393     from_lx = from_x + level_xpos;
13394     from_ly = from_y + level_ypos;
13395
13396     for (y = 0; y < brush_height; y++)
13397     {
13398       for (x = 0; x < brush_width; x++)
13399       {
13400         brush_buffer[x][y] = Tile[from_lx + x][from_ly + y];
13401
13402         if (button != 1)
13403           DrawBrushElement(from_x + x, from_y + y, new_element, TRUE);
13404       }
13405     }
13406
13407     if (button != 1)
13408       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13409
13410     delete_old_brush = FALSE;
13411   }
13412   else if (mode == CB_BRUSH_TO_CURSOR || mode == CB_DELETE_OLD_CURSOR ||
13413            mode == CB_BRUSH_TO_LEVEL)
13414   {
13415     int cursor_x = (mode == CB_DELETE_OLD_CURSOR ? last_cursor_x : from_x);
13416     int cursor_y = (mode == CB_DELETE_OLD_CURSOR ? last_cursor_y : from_y);
13417     int cursor_from_x = cursor_x - brush_width / 2;
13418     int cursor_from_y = cursor_y - brush_height / 2;
13419     int border_from_x = cursor_x, border_from_y = cursor_y;
13420     int border_to_x = cursor_x, border_to_y = cursor_y;
13421
13422     if (mode != CB_DELETE_OLD_CURSOR && delete_old_brush)
13423       CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
13424
13425     if (!IN_ED_FIELD(cursor_x, cursor_y) ||
13426         !IN_LEV_FIELD(cursor_x + level_xpos, cursor_y + level_ypos))
13427     {
13428       delete_old_brush = FALSE;
13429
13430       return;
13431     }
13432
13433     for (y = 0; y < brush_height; y++)
13434     {
13435       for (x = 0; x < brush_width; x++)
13436       {
13437         int sx = cursor_from_x + x;
13438         int sy = cursor_from_y + y;
13439         int lx = sx + level_xpos;
13440         int ly = sy + level_ypos;
13441         boolean change_level = (mode == CB_BRUSH_TO_LEVEL);
13442         int element = (mode == CB_DELETE_OLD_CURSOR ? -1 :
13443                        mode == CB_BRUSH_TO_CURSOR || button == 1 ?
13444                        brush_buffer[x][y] : new_element);
13445
13446         if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
13447         {
13448           if (sx < border_from_x)
13449             border_from_x = sx;
13450           else if (sx > border_to_x)
13451             border_to_x = sx;
13452           if (sy < border_from_y)
13453             border_from_y = sy;
13454           else if (sy > border_to_y)
13455             border_to_y = sy;
13456
13457           DrawBrushElement(sx, sy, element, change_level);
13458         }
13459       }
13460     }
13461
13462     if (mode != CB_DELETE_OLD_CURSOR)
13463       DrawAreaBorder(border_from_x, border_from_y, border_to_x, border_to_y);
13464
13465     last_cursor_x = cursor_x;
13466     last_cursor_y = cursor_y;
13467
13468     delete_old_brush = TRUE;
13469   }
13470   else if (mode == CB_FLIP_BRUSH_X)
13471   {
13472     for (y = 0; y < brush_height; y++)
13473       for (x = 0; x < (brush_width + 1) / 2; x++)
13474         SwapFlippedTiles(&brush_buffer[x][y],
13475                          &brush_buffer[brush_width - x - 1][y], mode);
13476
13477     CopyBrushExt(last_cursor_x, last_cursor_y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13478   }
13479   else if (mode == CB_FLIP_BRUSH_Y)
13480   {
13481     for (y = 0; y < (brush_height + 1) / 2; y++)
13482       for (x = 0; x < brush_width; x++)
13483         SwapFlippedTiles(&brush_buffer[x][y],
13484                          &brush_buffer[x][brush_height - y - 1], mode);
13485
13486     CopyBrushExt(last_cursor_x, last_cursor_y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13487   }
13488   else if (mode == CB_FLIP_BRUSH_XY)
13489   {
13490     CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
13491
13492     for (y = 0; y < MAX(brush_width, brush_height); y++)
13493       for (x = 0; x <= y; x++)
13494         SwapFlippedTiles(&brush_buffer[x][y],
13495                          &brush_buffer[y][x], mode);
13496
13497     swap_numbers(&brush_width, &brush_height);
13498
13499     CopyBrushExt(last_cursor_x, last_cursor_y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13500   }
13501
13502   if (mode == CB_UPDATE_BRUSH_POSITION)
13503   {
13504     last_cursor_x = from_x;
13505     last_cursor_y = from_y;
13506   }
13507 }
13508
13509 static void CopyAreaToBrush(int from_x, int from_y, int to_x, int to_y,
13510                             int button)
13511 {
13512   CopyBrushExt(from_x, from_y, to_x, to_y, button, CB_AREA_TO_BRUSH);
13513 }
13514
13515 static void CopyBrushToLevel(int x, int y, int button)
13516 {
13517   CopyBrushExt(x, y, 0, 0, button, CB_BRUSH_TO_LEVEL);
13518 }
13519
13520 static void CopyBrushToCursor(int x, int y)
13521 {
13522   CopyBrushExt(x, y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13523 }
13524
13525 static void UpdateBrushPosition(int x, int y)
13526 {
13527   CopyBrushExt(x, y, 0, 0, 0, CB_UPDATE_BRUSH_POSITION);
13528 }
13529
13530 static void DeleteBrushFromCursor(void)
13531 {
13532   CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
13533 }
13534
13535 static void FlipBrushX(void)
13536 {
13537   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_X);
13538 }
13539
13540 static void FlipBrushY(void)
13541 {
13542   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_Y);
13543 }
13544
13545 static void RotateBrush(void)
13546 {
13547   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_XY);
13548   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_X);
13549 }
13550
13551 void DumpBrush(void)
13552 {
13553   CopyBrushExt(0, 0, 0, 0, 0, CB_DUMP_BRUSH);
13554 }
13555
13556 void DumpBrush_Small(void)
13557 {
13558   CopyBrushExt(0, 0, 0, 0, 0, CB_DUMP_BRUSH_SMALL);
13559 }
13560
13561 void CopyClipboardToBrush(void)
13562 {
13563   CopyBrushExt(0, 0, 0, 0, 0, CB_CLIPBOARD_TO_BRUSH);
13564 }
13565
13566 void CopyBrushToClipboard(void)
13567 {
13568   CopyBrushExt(0, 0, 0, 0, 0, CB_BRUSH_TO_CLIPBOARD);
13569 }
13570
13571 void CopyBrushToClipboard_Small(void)
13572 {
13573   CopyBrushExt(0, 0, 0, 0, 0, CB_BRUSH_TO_CLIPBOARD_SMALL);
13574 }
13575
13576 void UndoLevelEditorOperation(void)
13577 {
13578   ClickOnGadget(level_editor_gadget[GADGET_ID_UNDO], -1);
13579 }
13580
13581 void RedoLevelEditorOperation(void)
13582 {
13583   ClickOnGadget(level_editor_gadget[GADGET_ID_UNDO], 3);
13584 }
13585
13586 static void FloodFill(int from_x, int from_y, int fill_element)
13587 {
13588   FloodFillLevel(from_x, from_y, fill_element, Tile, lev_fieldx, lev_fieldy);
13589 }
13590
13591 static void FloodFillWall_MM(int from_sx2, int from_sy2, int fill_element)
13592 {
13593   int from_x = from_sx2 + 2 * level_xpos;
13594   int from_y = from_sy2 + 2 * level_ypos;
13595   int max_fillx = lev_fieldx * 2;
13596   int max_filly = lev_fieldy * 2;
13597   short Fill[max_fillx][max_filly];
13598   int x, y;
13599
13600   for (x = 0; x < max_fillx; x++)
13601     for (y = 0; y < max_filly; y++)
13602       Fill[x][y] = getLevelElementHiRes(x, y);
13603
13604   FloodFillLevelExt(from_x, from_y, fill_element, max_fillx, max_filly,
13605                     Fill, max_fillx, max_filly);
13606
13607   for (x = 0; x < max_fillx; x++)
13608     for (y = 0; y < max_filly; y++)
13609       if (Fill[x][y] == fill_element)
13610         SetLevelElementHiRes(x, y, Fill[x][y]);
13611 }
13612
13613 // values for DrawLevelText() modes
13614 #define TEXT_INIT               0
13615 #define TEXT_SETCURSOR          1
13616 #define TEXT_WRITECHAR          2
13617 #define TEXT_BACKSPACE          3
13618 #define TEXT_NEWLINE            4
13619 #define TEXT_END                5
13620 #define TEXT_QUERY_TYPING       6
13621
13622 static int DrawLevelText(int sx, int sy, char letter, int mode)
13623 {
13624   static short delete_buffer[MAX_LEV_FIELDX];
13625   static int start_sx;
13626   static int last_sx, last_sy;
13627   static boolean typing = FALSE;
13628   int letter_element;
13629   int lx = 0, ly = 0;
13630
13631   // map lower case letters to upper case and convert special characters
13632   if (letter >= 'a' && letter <= 'z')
13633     letter_element = EL_CHAR_ASCII0 + letter + (int)('A' - 'a');
13634   else if (letter == CHAR_BYTE_UMLAUT_a || letter == CHAR_BYTE_UMLAUT_A)
13635     letter_element = EL_CHAR_AUMLAUT;
13636   else if (letter == CHAR_BYTE_UMLAUT_o || letter == CHAR_BYTE_UMLAUT_O)
13637     letter_element = EL_CHAR_OUMLAUT;
13638   else if (letter == CHAR_BYTE_UMLAUT_u || letter == CHAR_BYTE_UMLAUT_U)
13639     letter_element = EL_CHAR_UUMLAUT;
13640   else if (letter == '^')
13641     letter_element = EL_CHAR_COPYRIGHT;
13642   else
13643     letter_element = EL_CHAR_ASCII0 + letter;
13644
13645   if (mode != TEXT_INIT)
13646   {
13647     if (!typing)
13648       return FALSE;
13649
13650     if (mode != TEXT_SETCURSOR)
13651     {
13652       sx = last_sx;
13653       sy = last_sy;
13654     }
13655
13656     lx = last_sx + level_xpos;
13657     ly = last_sy + level_ypos;
13658   }
13659
13660   switch (mode)
13661   {
13662     case TEXT_INIT:
13663       if (typing)
13664         DrawLevelText(0, 0, 0, TEXT_END);
13665
13666       typing = TRUE;
13667       start_sx = sx;
13668       last_sx = sx;
13669       last_sy = sy;
13670       DrawLevelText(sx, sy, 0, TEXT_SETCURSOR);
13671       break;
13672
13673     case TEXT_SETCURSOR:
13674       DrawEditorElement(last_sx, last_sy, Tile[lx][ly]);
13675       DrawAreaBorder(sx, sy, sx, sy);
13676       StartTextInput(SX + sx * ed_tilesize, SY + sy * ed_tilesize,
13677                      ed_tilesize, ed_tilesize);
13678       last_sx = sx;
13679       last_sy = sy;
13680       break;
13681
13682     case TEXT_WRITECHAR:
13683       if (letter_element >= EL_CHAR_START && letter_element <= EL_CHAR_END)
13684       {
13685         if (new_element1 >= EL_STEEL_CHAR_START &&
13686             new_element1 <= EL_STEEL_CHAR_END)
13687           letter_element = letter_element - EL_CHAR_START + EL_STEEL_CHAR_START;
13688
13689         delete_buffer[sx - start_sx] = Tile[lx][ly];
13690         Tile[lx][ly] = letter_element;
13691
13692         if (sx + 1 < ed_fieldx && lx + 1 < lev_fieldx)
13693           DrawLevelText(sx + 1, sy, 0, TEXT_SETCURSOR);
13694         else if (sy + 1 < ed_fieldy && ly + 1 < lev_fieldy)
13695           DrawLevelText(start_sx, sy + 1, 0, TEXT_SETCURSOR);
13696         else
13697           DrawLevelText(0, 0, 0, TEXT_END);
13698
13699         level.changed = TRUE;
13700       }
13701       break;
13702
13703     case TEXT_BACKSPACE:
13704       if (sx > start_sx)
13705       {
13706         Tile[lx - 1][ly] = delete_buffer[sx - start_sx - 1];
13707         DrawEditorElement(sx - 1, sy, Tile[lx - 1][ly]);
13708         DrawLevelText(sx - 1, sy, 0, TEXT_SETCURSOR);
13709       }
13710       break;
13711
13712     case TEXT_NEWLINE:
13713       if (sy + 1 < ed_fieldy && ly + 1 < lev_fieldy)
13714         DrawLevelText(start_sx, sy + 1, 0, TEXT_SETCURSOR);
13715       else
13716         DrawLevelText(0, 0, 0, TEXT_END);
13717       break;
13718
13719     case TEXT_END:
13720       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13721       DrawEditorElement(sx, sy, Tile[lx][ly]);
13722       StopTextInput();
13723       typing = FALSE;
13724       break;
13725
13726     case TEXT_QUERY_TYPING:
13727       break;
13728
13729     default:
13730       break;
13731   }
13732
13733   return typing;
13734 }
13735
13736 static void SetTextCursor(int unused_sx, int unused_sy, int sx, int sy,
13737                           int element, boolean change_level)
13738 {
13739   int lx = sx + level_xpos;
13740   int ly = sy + level_ypos;
13741
13742   if (element == -1)
13743     DrawEditorElement(sx, sy, Tile[lx][ly]);
13744   else
13745     DrawAreaBorder(sx, sy, sx, sy);
13746 }
13747
13748 static void CheckLevelBorderElement(boolean redraw_playfield)
13749 {
13750   int last_border_element = BorderElement;
13751
13752   SetBorderElement();
13753
13754   if (redraw_playfield && BorderElement != last_border_element)
13755     DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
13756 }
13757
13758 static void CopyLevelToUndoBuffer(int mode)
13759 {
13760   static boolean accumulated_undo = FALSE;
13761   boolean new_undo_buffer_position = TRUE;
13762   int x, y;
13763
13764   if (undo_buffer_steps == 0)
13765     accumulated_undo = FALSE;
13766
13767   switch (mode)
13768   {
13769     case UNDO_IMMEDIATE:
13770       accumulated_undo = FALSE;
13771       break;
13772
13773     case UNDO_ACCUMULATE:
13774       if (accumulated_undo)
13775         new_undo_buffer_position = FALSE;
13776       accumulated_undo = TRUE;
13777       break;
13778
13779     default:
13780       break;
13781   }
13782
13783   if (new_undo_buffer_position)
13784   {
13785     // advance position in undo buffer ring
13786     undo_buffer_position = (undo_buffer_position + 1) % NUM_UNDO_STEPS;
13787
13788     if (undo_buffer_steps < NUM_UNDO_STEPS - 1)
13789       undo_buffer_steps++;
13790   }
13791
13792   // always reset redo buffer when storing level change into undo buffer
13793   redo_buffer_steps = 0;
13794
13795   for (x = 0; x < lev_fieldx; x++)
13796     for (y = 0; y < lev_fieldy; y++)
13797       UndoBuffer[undo_buffer_position][x][y] = Tile[x][y];
13798
13799   // check if drawing operation forces change of border style
13800   CheckLevelBorderElement(TRUE);
13801
13802   level.changed = TRUE;
13803 }
13804
13805 static void RandomPlacement(int new_element)
13806 {
13807   static boolean free_position[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
13808   int num_free_positions = 0;
13809   int num_percentage, num_elements;
13810   int x, y;
13811
13812   ResetIntelliDraw();
13813
13814   // determine number of free positions for randomly placing the new element
13815   for (x = 0; x < lev_fieldx; x++) for (y = 0; y < lev_fieldy; y++)
13816   {
13817     free_position[x][y] =
13818       (random_placement_background_restricted ?
13819        Tile[x][y] == random_placement_background_element :
13820        Tile[x][y] != new_element);
13821
13822     if (free_position[x][y])
13823       num_free_positions++;
13824   }
13825
13826   // determine number of new elements to place there
13827   num_percentage = num_free_positions * random_placement_value / 100;
13828   num_elements = (random_placement_method == RANDOM_USE_PERCENTAGE ?
13829                   num_percentage : random_placement_value);
13830
13831   // if less free positions than elements to place, fill all these positions
13832   if (num_free_positions < num_elements)
13833   {
13834     for (x = 0; x < lev_fieldx; x++)
13835       for (y = 0; y < lev_fieldy; y++)
13836         if (free_position[x][y])
13837           SetElement(x, y, new_element);
13838   }
13839   else
13840   {
13841     while (num_elements > 0)
13842     {
13843       x = GetSimpleRandom(lev_fieldx);
13844       y = GetSimpleRandom(lev_fieldy);
13845
13846       // don't place element at the same position twice
13847       if (free_position[x][y])
13848       {
13849         free_position[x][y] = FALSE;
13850         SetElement(x, y, new_element);
13851         num_elements--;
13852       }
13853     }
13854   }
13855
13856   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
13857   CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13858 }
13859
13860 static void WrapLevel(int dx, int dy)
13861 {
13862   int wrap_dx = lev_fieldx - dx;
13863   int wrap_dy = lev_fieldy - dy;
13864   int x, y;
13865
13866   for (x = 0; x < lev_fieldx; x++)
13867     for (y = 0; y < lev_fieldy; y++)
13868       TileBackup[x][y] = Tile[x][y];
13869
13870   for (x = 0; x < lev_fieldx; x++)
13871     for (y = 0; y < lev_fieldy; y++)
13872       Tile[x][y] =
13873         TileBackup[(x + wrap_dx) % lev_fieldx][(y + wrap_dy) % lev_fieldy];
13874
13875   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
13876   CopyLevelToUndoBuffer(UNDO_ACCUMULATE);
13877 }
13878
13879 static void DrawAreaElementHighlight(boolean highlighted,
13880                                      boolean highlighted_similar)
13881 {
13882   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
13883
13884   if (!highlighted)
13885     return;
13886
13887   int x, y;
13888
13889   for (x = 0; x < ed_fieldx; x++)
13890   {
13891     for (y = 0; y < ed_fieldy; y++)
13892     {
13893       boolean highlight = FALSE;
13894       int lx = x + level_xpos;
13895       int ly = y + level_ypos;
13896
13897       if (!IN_LEV_FIELD(lx, ly))
13898         continue;
13899
13900       // check if element is the same
13901       if (Tile[lx][ly] == new_element1)
13902         highlight = TRUE;
13903
13904       // check if element is similar
13905       if (highlighted_similar &&
13906           strEqual(element_info[Tile[lx][ly]].class_name,
13907                    element_info[new_element1].class_name))
13908         highlight = TRUE;
13909
13910       // check if element is matching MM style wall
13911       if (IS_MM_WALL(Tile[lx][ly]) &&
13912           map_mm_wall_element(Tile[lx][ly]) == new_element1)
13913         highlight = TRUE;
13914
13915       if (!highlight)
13916         continue;
13917
13918       if (IS_MM_WALL(Tile[lx][ly]) && !highlighted_similar)
13919       {
13920         int i;
13921
13922         for (i = 0; i < 4; i++)
13923         {
13924           if (!(MM_WALL_BITS(Tile[lx][ly]) & (1 << i)))
13925             continue;
13926
13927           int xx = x * 2 + (i % 2);
13928           int yy = y * 2 + (i / 2);
13929           int sx = SX + xx * ed_tilesize / 2;
13930           int sy = SY + yy * ed_tilesize / 2;
13931           int from_sx = sx;
13932           int from_sy = sy;
13933           int to_sx = sx + ed_tilesize / 2 - 1;
13934           int to_sy = sy + ed_tilesize / 2 - 1;
13935
13936           DrawSimpleWhiteLine(drawto, from_sx, from_sy, to_sx,   from_sy);
13937           DrawSimpleWhiteLine(drawto, to_sx,   from_sy, to_sx,   to_sy);
13938           DrawSimpleWhiteLine(drawto, to_sx,   to_sy,   from_sx, to_sy);
13939           DrawSimpleWhiteLine(drawto, from_sx, to_sy,   from_sx, from_sy);
13940         }
13941       }
13942       else
13943       {
13944         int sx = SX + x * ed_tilesize;
13945         int sy = SY + y * ed_tilesize;
13946         int from_sx = sx;
13947         int from_sy = sy;
13948         int to_sx = sx + ed_tilesize - 1;
13949         int to_sy = sy + ed_tilesize - 1;
13950
13951         DrawSimpleWhiteLine(drawto, from_sx, from_sy, to_sx,   from_sy);
13952         DrawSimpleWhiteLine(drawto, to_sx,   from_sy, to_sx,   to_sy);
13953         DrawSimpleWhiteLine(drawto, to_sx,   to_sy,   from_sx, to_sy);
13954         DrawSimpleWhiteLine(drawto, from_sx, to_sy,   from_sx, from_sy);
13955       }
13956     }
13957   }
13958 }
13959
13960 static void CopyLevelTemplateToUserLevelSet(char *levelset_subdir)
13961 {
13962   char *template_filename_old = getLocalLevelTemplateFilename();
13963   char *template_filename_new =
13964     getPath2(getUserLevelDir(levelset_subdir), LEVELTEMPLATE_FILENAME);
13965
13966   if (copyFile(template_filename_old, template_filename_new) != 0)
13967     Request("Cannot copy level template!", REQ_CONFIRM);
13968
13969   free(template_filename_new);
13970 }
13971
13972 static void HandleDrawingAreas(struct GadgetInfo *gi)
13973 {
13974   static boolean started_inside_drawing_area = FALSE;
13975   static int last_sx = -1;
13976   static int last_sy = -1;
13977   static int last_sx2 = -1;
13978   static int last_sy2 = -1;
13979   int id = gi->custom_id;
13980   int type_id = gi->custom_type_id;
13981   boolean button_press_event;
13982   boolean button_release_event;
13983   boolean inside_drawing_area = !gi->event.off_borders;
13984   boolean draw_level = (id == GADGET_ID_DRAWING_LEVEL);
13985   int actual_drawing_function;
13986   int button = gi->event.button;
13987   int new_element = BUTTON_ELEMENT(button);
13988   int sx = gi->event.x, sy = gi->event.y;
13989   int min_sx = 0, min_sy = 0;
13990   int max_sx = gi->drawing.area_xsize - 1, max_sy = gi->drawing.area_ysize - 1;
13991   int item_xsize = gi->drawing.item_xsize, item_ysize = gi->drawing.item_ysize;
13992   int mini_item_xsize = item_xsize / 2, mini_item_ysize = item_ysize / 2;
13993   int sx2 = gi->event.mx / mini_item_xsize;
13994   int sy2 = gi->event.my / mini_item_ysize;
13995   int dx = sx2 % 2;
13996   int dy = sy2 % 2;
13997   int lx = 0, ly = 0;
13998   int x, y;
13999
14000   button_press_event = (gi->event.type == GD_EVENT_PRESSED);
14001   button_release_event = (gi->event.type == GD_EVENT_RELEASED);
14002
14003   // make sure to stay inside drawing area boundaries
14004   sx = (sx < min_sx ? min_sx : sx > max_sx ? max_sx : sx);
14005   sy = (sy < min_sy ? min_sy : sy > max_sy ? max_sy : sy);
14006
14007   if (draw_level)
14008   {
14009     int min_lx = 0, min_ly = 0;
14010     int max_lx = lev_fieldx - 1, max_ly = lev_fieldy - 1;
14011
14012     // get positions inside level field
14013     lx = sx + level_xpos;
14014     ly = sy + level_ypos;
14015
14016     if (!IN_LEV_FIELD(lx, ly))
14017       inside_drawing_area = FALSE;
14018
14019     // make sure to stay inside level field boundaries
14020     lx = (lx < min_lx ? min_lx : lx > max_lx ? max_lx : lx);
14021     ly = (ly < min_ly ? min_ly : ly > max_ly ? max_ly : ly);
14022
14023     // correct drawing area positions accordingly
14024     sx = lx - level_xpos;
14025     sy = ly - level_ypos;
14026   }
14027
14028   // also correct MM wall-sized (double) drawing area positions accordingly
14029   if (sx2 / 2 < sx || sx2 / 2 > sx)
14030   {
14031     dx = (sx2 / 2 < sx ? 0 : 1);
14032     sx2 = sx * 2 + dx;
14033   }
14034   if (sy2 / 2 < sy || sy2 / 2 > sy)
14035   {
14036     dy = (sy2 / 2 < sy ? 0 : 1);
14037     sy2 = sy * 2 + dy;
14038   }
14039
14040   if (!button_press_event && !button_release_event)
14041   {
14042     int old_element = (IN_LEV_FIELD(lx, ly) ? Tile[lx][ly] : EL_UNDEFINED);
14043     boolean hires_drawing = (level.game_engine_type == GAME_ENGINE_TYPE_MM &&
14044                              isHiresTileElement(old_element) &&
14045                              isHiresDrawElement(new_element));
14046
14047     // prevent handling events for every pixel position when moving mouse
14048     if ((sx == last_sx && sy == last_sy && !hires_drawing) ||
14049         (sx2 == last_sx2 && sy2 == last_sy2))
14050       return;
14051   }
14052
14053   last_sx = sx;
14054   last_sy = sy;
14055   last_sx2 = sx2;
14056   last_sy2 = sy2;
14057
14058   if (button_press_event)
14059     started_inside_drawing_area = inside_drawing_area;
14060
14061   if (!started_inside_drawing_area)
14062     return;
14063
14064   if (!IS_VALID_BUTTON(button))
14065     return;
14066
14067   // handle info callback for each invocation of action callback
14068   gi->callback_info(gi);
14069
14070   // automatically switch to 'single item' drawing mode, if needed
14071   actual_drawing_function =
14072     (draw_level || drawing_function == GADGET_ID_PICK_ELEMENT ?
14073      drawing_function : GADGET_ID_SINGLE_ITEMS);
14074
14075   // clicking into drawing area with pressed Control key picks element
14076   if (GetKeyModState() & KMOD_Control)
14077   {
14078     last_drawing_function = drawing_function;
14079     actual_drawing_function = GADGET_ID_PICK_ELEMENT;
14080   }
14081
14082   if (GetKeyModState() & KMOD_Shift)
14083   {
14084     if (button_press_event || button_release_event)
14085       ResetIntelliDraw();
14086   }
14087
14088   SetDrawModeHiRes(-1);         // reset to normal draw mode
14089
14090   switch (actual_drawing_function)
14091   {
14092     case GADGET_ID_SINGLE_ITEMS:
14093       if (draw_level)
14094       {
14095         if (button_release_event)
14096         {
14097           CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14098
14099           if (edit_mode == ED_MODE_DRAWING && draw_with_brush &&
14100               !inside_drawing_area)
14101             DeleteBrushFromCursor();
14102
14103           break;
14104         }
14105
14106         if (draw_with_brush)
14107         {
14108           CopyBrushToLevel(sx, sy, button);
14109         }
14110         else
14111         {
14112           SetDrawModeHiRes(new_element);
14113
14114           if (IS_PLAYER_ELEMENT(new_element) || IS_MM_MCDUFFIN(new_element))
14115           {
14116             // remove player at old position
14117             for (y = 0; y < lev_fieldy; y++)
14118             {
14119               for (x = 0; x < lev_fieldx; x++)
14120               {
14121                 int old_element = Tile[x][y];
14122
14123                 if (IS_PLAYER_ELEMENT(old_element) &&
14124                     IS_PLAYER_ELEMENT(new_element))
14125                 {
14126                   int replaced_with_element =
14127                     (old_element == EL_SOKOBAN_FIELD_PLAYER &&
14128                      new_element == EL_PLAYER_1 ? EL_SOKOBAN_FIELD_EMPTY :
14129
14130                      old_element == EL_SOKOBAN_FIELD_PLAYER &&
14131                      new_element == old_element ? EL_SOKOBAN_FIELD_EMPTY :
14132
14133                      new_element == EL_SOKOBAN_FIELD_PLAYER &&
14134                      old_element == EL_PLAYER_1 ? EL_EMPTY :
14135
14136                      new_element >= EL_PLAYER_1 &&
14137                      new_element <= EL_PLAYER_4 &&
14138                      new_element == old_element ? EL_EMPTY :
14139
14140                      old_element);
14141
14142                   SetElement(x, y, replaced_with_element);
14143                 }
14144                 else if (IS_MM_MCDUFFIN(old_element) &&
14145                          IS_MM_MCDUFFIN(new_element))
14146                 {
14147                   // remove McDuffin at old position
14148                   SetElement(x, y, EL_EMPTY);
14149                 }
14150               }
14151             }
14152           }
14153
14154           SetElementButton(lx, ly, dx, dy, new_element, button);
14155         }
14156       }
14157       else if (!button_release_event)
14158       {
14159         int pos = sx * drawingarea_info[type_id].area_ysize + sy;
14160
14161         if (item_xsize == MINI_TILEX && item_ysize == MINI_TILEY)
14162           DrawMiniGraphicExt(drawto,
14163                              gi->x + sx * MINI_TILEX,
14164                              gi->y + sy * MINI_TILEY,
14165                              el2edimg(new_element));
14166         else
14167           DrawFixedGraphicExt(drawto,
14168                               gi->x + sx * TILEX,
14169                               gi->y + sy * TILEY,
14170                               el2edimg(new_element), 0);
14171
14172         if (id == GADGET_ID_CUSTOM_GRAPHIC)
14173           new_element = GFX_ELEMENT(new_element);
14174
14175         drawingarea_info[type_id].value[pos] = new_element;
14176
14177         CopyElementPropertiesToGame(properties_element);
14178
14179         if (id == GADGET_ID_CUSTOM_GRAPHIC)
14180         {
14181           UpdateCustomElementGraphicGadgets();
14182
14183           FrameCounter = 0;     // restart animation frame counter
14184         }
14185       }
14186       break;
14187
14188     case GADGET_ID_CONNECTED_ITEMS:
14189       {
14190         static int last_sx = -1;
14191         static int last_sy = -1;
14192
14193         if (button_release_event)
14194           CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14195
14196         SetDrawModeHiRes(new_element);
14197
14198         if (getDrawModeHiRes())
14199         {
14200           sx = sx2;
14201           sy = sy2;
14202         }
14203
14204         if (!button_press_event)
14205           DrawLine(last_sx, last_sy, sx, sy, new_element, TRUE);
14206
14207         last_sx = sx;
14208         last_sy = sy;
14209       }
14210       break;
14211
14212     case GADGET_ID_LINE:
14213     case GADGET_ID_ARC:
14214     case GADGET_ID_RECTANGLE:
14215     case GADGET_ID_FILLED_BOX:
14216       SetDrawModeHiRes(new_element);
14217
14218       if (getDrawModeHiRes())
14219       {
14220         sx = sx2;
14221         sy = sy2;
14222       }
14223       // FALLTHROUGH
14224     case GADGET_ID_GRAB_BRUSH:
14225     case GADGET_ID_TEXT:
14226       {
14227         static int last_sx = -1;
14228         static int last_sy = -1;
14229         static int start_sx = -1;
14230         static int start_sy = -1;
14231         void (*draw_func)(int, int, int, int, int, boolean);
14232
14233         if (drawing_function == GADGET_ID_LINE)
14234           draw_func = DrawLine;
14235         else if (drawing_function == GADGET_ID_ARC)
14236           draw_func = DrawArc;
14237         else if (drawing_function == GADGET_ID_RECTANGLE)
14238           draw_func = DrawBox;
14239         else if (drawing_function == GADGET_ID_FILLED_BOX)
14240           draw_func = DrawFilledBox;
14241         else if (drawing_function == GADGET_ID_GRAB_BRUSH)
14242           draw_func = SelectArea;
14243         else // (drawing_function == GADGET_ID_TEXT)
14244           draw_func = SetTextCursor;
14245
14246         if (button_press_event)
14247         {
14248           draw_func(sx, sy, sx, sy, new_element, FALSE);
14249           start_sx = last_sx = sx;
14250           start_sy = last_sy = sy;
14251
14252           if (drawing_function == GADGET_ID_TEXT)
14253             DrawLevelText(0, 0, 0, TEXT_END);
14254         }
14255         else if (button_release_event)
14256         {
14257           draw_func(start_sx, start_sy, sx, sy, new_element, TRUE);
14258           if (drawing_function == GADGET_ID_GRAB_BRUSH)
14259           {
14260             CopyAreaToBrush(start_sx, start_sy, sx, sy, button);
14261             CopyBrushToCursor(sx, sy);
14262             ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS],
14263                           MB_LEFTBUTTON);
14264             draw_with_brush = TRUE;
14265           }
14266           else if (drawing_function == GADGET_ID_TEXT)
14267             DrawLevelText(sx, sy, 0, TEXT_INIT);
14268           else
14269             CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14270         }
14271         else if (last_sx != sx || last_sy != sy)
14272         {
14273           draw_func(start_sx, start_sy, last_sx, last_sy, -1, FALSE);
14274           if (IS_MM_WALL_EDITOR(new_element))   // clear wall background
14275             draw_func(start_sx, start_sy, sx, sy, EL_EMPTY, FALSE);
14276           draw_func(start_sx, start_sy, sx, sy, new_element, FALSE);
14277           last_sx = sx;
14278           last_sy = sy;
14279         }
14280       }
14281       break;
14282
14283     case GADGET_ID_FLOOD_FILL:
14284       if (button_press_event && Tile[lx][ly] != new_element)
14285       {
14286         if (IS_MM_WALL_EDITOR(new_element))
14287           FloodFillWall_MM(sx2, sy2, new_element);
14288         else
14289           FloodFill(lx, ly, new_element);
14290
14291         DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14292         CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14293       }
14294       break;
14295
14296     case GADGET_ID_PICK_ELEMENT:
14297       if (button_release_event)
14298         ClickOnGadget(level_editor_gadget[last_drawing_function],
14299                       MB_LEFTBUTTON);
14300       else if (draw_level)
14301         PickDrawingElement(button, Tile[lx][ly]);
14302       else
14303       {
14304         int pos = sx * drawingarea_info[type_id].area_ysize + sy;
14305
14306         PickDrawingElement(button, drawingarea_info[type_id].value[pos]);
14307       }
14308
14309     default:
14310       break;
14311   }
14312
14313   // do not mark level as modified for certain non-level-changing gadgets
14314   if ((type_id >= ED_DRAWING_ID_EDITOR_FIRST &&
14315        type_id <= ED_DRAWING_ID_EDITOR_LAST) ||
14316       actual_drawing_function == GADGET_ID_GRAB_BRUSH ||
14317       actual_drawing_function == GADGET_ID_PICK_ELEMENT)
14318     return;
14319
14320   level.changed = TRUE;
14321 }
14322
14323 static void HandleCounterButtons(struct GadgetInfo *gi)
14324 {
14325   int gadget_id = gi->custom_id;
14326   int counter_id = gi->custom_type_id;
14327   int button = gi->event.button;
14328   int *counter_value = counterbutton_info[counter_id].value;
14329   int step = BUTTON_STEPSIZE(button) *
14330     (gadget_id == counterbutton_info[counter_id].gadget_id_down ? -1 : +1);
14331
14332   if (counter_id == ED_COUNTER_ID_SELECT_LEVEL)
14333   {
14334     boolean pressed = (gi->event.type == GD_EVENT_PRESSED);
14335     boolean released = (gi->event.type == GD_EVENT_RELEASED);
14336     boolean level_changed = LevelChanged();
14337
14338     if ((level_changed && pressed) || (!level_changed && released))
14339       return;
14340
14341     if (level_changed && !Request("Level has changed! Discard changes?",
14342                                   REQ_ASK))
14343     {
14344       if (gadget_id == counterbutton_info[counter_id].gadget_id_text)
14345         ModifyEditorCounterValue(counter_id, *counter_value);
14346
14347       return;
14348     }
14349   }
14350
14351   if (gadget_id == counterbutton_info[counter_id].gadget_id_text)
14352     *counter_value = gi->textinput.number_value;
14353   else
14354     ModifyEditorCounterValue(counter_id, *counter_value + step);
14355
14356   if (counter_id == ED_COUNTER_ID_SELECT_LEVEL)
14357   {
14358     int last_game_engine_type = level.game_engine_type;
14359
14360     LoadLevel(level_nr);
14361     LoadScore(level_nr);
14362
14363     SaveLevelSetup_SeriesInfo();
14364
14365     TapeErase();
14366
14367     ResetUndoBuffer();
14368     DrawEditModeWindow();
14369
14370     if (level.game_engine_type != last_game_engine_type)
14371     {
14372       // update element selection list
14373       ReinitializeElementList();
14374       ModifyEditorElementList();
14375     }
14376
14377     return;
14378   }
14379
14380   switch (counter_id)
14381   {
14382     case ED_COUNTER_ID_YAMYAM_CONTENT:
14383       DrawYamYamContentAreas();
14384       break;
14385
14386     case ED_COUNTER_ID_BALL_CONTENT:
14387       DrawMagicBallContentAreas();
14388       break;
14389
14390     case ED_COUNTER_ID_ANDROID_CONTENT:
14391       DrawAndroidElementArea();
14392       break;
14393
14394     case ED_COUNTER_ID_GROUP_CONTENT:
14395       DrawGroupElementArea();
14396       CopyGroupElementPropertiesToGame(properties_element);
14397       break;
14398
14399     case ED_COUNTER_ID_INVENTORY_SIZE:
14400       DrawPlayerInitialInventoryArea(properties_element);
14401       break;
14402
14403     case ED_COUNTER_ID_MM_BALL_CONTENT:
14404       DrawMMBallContentArea();
14405       break;
14406
14407     case ED_COUNTER_ID_ENVELOPE_XSIZE:
14408     case ED_COUNTER_ID_ENVELOPE_YSIZE:
14409       DrawEnvelopeTextArea(-1);
14410       break;
14411
14412     case ED_COUNTER_ID_LEVEL_XSIZE:
14413     case ED_COUNTER_ID_LEVEL_YSIZE:
14414       lev_fieldx = level.fieldx;
14415       lev_fieldy = level.fieldy;
14416
14417       // check if resizing of level results in change of border border
14418       SetBorderElement();
14419
14420       break;
14421
14422     default:
14423       break;
14424   }
14425
14426   if ((counter_id >= ED_COUNTER_ID_CUSTOM_FIRST &&
14427        counter_id <= ED_COUNTER_ID_CUSTOM_LAST) ||
14428       (counter_id >= ED_COUNTER_ID_CHANGE_FIRST &&
14429        counter_id <= ED_COUNTER_ID_CHANGE_LAST))
14430     CopyElementPropertiesToGame(properties_element);
14431
14432   // do not mark level as modified for certain non-level-changing gadgets
14433   if ((counter_id >= ED_COUNTER_ID_LEVELSET_FIRST &&
14434        counter_id <= ED_COUNTER_ID_LEVELSET_LAST) ||
14435       (counter_id >= ED_COUNTER_ID_EDITOR_FIRST &&
14436        counter_id <= ED_COUNTER_ID_EDITOR_LAST))
14437     return;
14438
14439   level.changed = TRUE;
14440 }
14441
14442 static void HandleTextInputGadgets(struct GadgetInfo *gi)
14443 {
14444   int type_id = gi->custom_type_id;
14445
14446   strcpy(textinput_info[type_id].value, gi->textinput.value);
14447
14448   if (type_id == ED_TEXTINPUT_ID_ELEMENT_NAME)
14449   {
14450     CopyElementPropertiesToGame(properties_element);
14451
14452     ModifyEditorElementList();  // update changed button info text
14453   }
14454
14455   // do not mark level as modified for certain non-level-changing gadgets
14456   if (type_id >= ED_TEXTINPUT_ID_LEVELSET_FIRST &&
14457       type_id <= ED_TEXTINPUT_ID_LEVELSET_LAST)
14458     return;
14459
14460   level.changed = TRUE;
14461 }
14462
14463 static void HandleTextAreaGadgets(struct GadgetInfo *gi)
14464 {
14465   int type_id = gi->custom_type_id;
14466
14467   strncpy(textarea_info[type_id].value, gi->textarea.value,
14468           MAX_ENVELOPE_TEXT_LEN);
14469   textarea_info[type_id].value[MAX_ENVELOPE_TEXT_LEN] = '\0';
14470
14471   level.changed = TRUE;
14472 }
14473
14474 static void HandleSelectboxGadgets(struct GadgetInfo *gi)
14475 {
14476   int type_id = gi->custom_type_id;
14477   int value_old = *selectbox_info[type_id].value;
14478   int value_new = selectbox_info[type_id].options[gi->selectbox.index].value;
14479
14480   *selectbox_info[type_id].value = value_new;
14481
14482   if (type_id == ED_SELECTBOX_ID_LEVELSET_SAVE_MODE)
14483   {
14484     DrawLevelConfigWindow();
14485   }
14486   else if (type_id == ED_SELECTBOX_ID_SELECT_CHANGE_PAGE)
14487   {
14488     element_info[properties_element].current_change_page = gi->selectbox.index;
14489
14490     DrawPropertiesWindow();
14491   }
14492   else if ((type_id >= ED_SELECTBOX_ID_CUSTOM_FIRST &&
14493             type_id <= ED_SELECTBOX_ID_CUSTOM_LAST) ||
14494            (type_id >= ED_SELECTBOX_ID_CHANGE_FIRST &&
14495             type_id <= ED_SELECTBOX_ID_CHANGE_LAST) ||
14496            (type_id == ED_SELECTBOX_ID_GROUP_CHOICE_MODE))
14497   {
14498     if (type_id == ED_SELECTBOX_ID_ACTION_TYPE)
14499     {
14500       // when changing action type, also check action mode and action arg
14501       if (value_old != value_new)
14502         setSelectboxSpecialActionVariablesIfNeeded();
14503
14504       DrawPropertiesChange();
14505     }
14506
14507     CopyElementPropertiesToGame(properties_element);
14508   }
14509   else if (type_id == ED_SELECTBOX_ID_GAME_ENGINE_TYPE)
14510   {
14511     // show or hide "engine" tabulator depending on game engine type
14512     DrawLevelConfigWindow();
14513
14514     // update element selection list depending on game engine type
14515     ReinitializeElementList();
14516     ModifyEditorElementList();
14517   }
14518
14519   // do not mark level as modified for certain non-level-changing gadgets
14520   if (type_id == ED_SELECTBOX_ID_LEVELSET_SAVE_MODE ||
14521       type_id == ED_SELECTBOX_ID_SELECT_CHANGE_PAGE)
14522     return;
14523
14524   level.changed = TRUE;
14525 }
14526
14527 static void HandleTextbuttonGadgets(struct GadgetInfo *gi)
14528 {
14529   int type_id = gi->custom_type_id;
14530   int i;
14531
14532   if (type_id >= ED_TAB_BUTTON_ID_LEVELCONFIG_FIRST &&
14533       type_id <= ED_TAB_BUTTON_ID_LEVELCONFIG_LAST)
14534   {
14535     edit_mode_levelconfig = gi->custom_type_id;
14536
14537     DrawLevelConfigWindow();
14538   }
14539   else if (type_id >= ED_TAB_BUTTON_ID_PROPERTIES_FIRST &&
14540            type_id <= ED_TAB_BUTTON_ID_PROPERTIES_LAST)
14541   {
14542     edit_mode_properties = gi->custom_type_id;
14543
14544     DrawPropertiesWindow();
14545   }
14546   else if (type_id == ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1 ||
14547            type_id == ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2)
14548   {
14549     boolean new_template = !fileExists(getLocalLevelTemplateFilename());
14550
14551     // backup original "level.field" (needed to track playfield changes)
14552     CopyPlayfield(level.field, TileBackup);
14553
14554     // "SaveLevelTemplate()" uses "level.field", so copy editor playfield
14555     CopyPlayfield(Tile, level.field);
14556
14557     if (new_template ||
14558         Request("Save this template and kill the old?", REQ_ASK))
14559       SaveLevelTemplate();
14560
14561     if (new_template)
14562       Request("Template saved!", REQ_CONFIRM);
14563
14564     // restore original "level.field" (needed to track playfield changes)
14565     CopyPlayfield(TileBackup, level.field);
14566   }
14567   else if (type_id == ED_TEXTBUTTON_ID_SAVE_LEVELSET)
14568   {
14569     char *levelset_subdir = getLevelSubdirFromSaveMode(levelset_save_mode);
14570
14571     if (levelset_save_mode == LEVELSET_SAVE_MODE_UPDATE &&
14572         leveldir_current->readonly)
14573     {
14574       Request("This level set is read-only!", REQ_CONFIRM);
14575
14576       return;
14577     }
14578
14579     if (strEqual(levelset_name, ""))
14580     {
14581       Request("Please enter level set title!", REQ_CONFIRM);
14582
14583       return;
14584     }
14585
14586     if (strEqual(levelset_author, ""))
14587     {
14588       Request("Please enter level set author!", REQ_CONFIRM);
14589
14590       return;
14591     }
14592
14593     if (levelset_save_mode == LEVELSET_SAVE_MODE_UPDATE)
14594     {
14595       if (UpdateUserLevelSet(levelset_subdir,
14596                              levelset_name,
14597                              levelset_author,
14598                              levelset_num_levels))
14599       {
14600         Request("Level set updated!", REQ_CONFIRM);
14601       }
14602       else
14603       {
14604         Request("Updating level set failed!", REQ_CONFIRM);
14605       }
14606     }
14607     else if (levelset_save_mode == LEVELSET_SAVE_MODE_CREATE)
14608     {
14609       if (level.changed && !Request("Level has changed! Discard changes?",
14610                                      REQ_ASK))
14611         return;
14612
14613       if (CreateUserLevelSet(levelset_subdir,
14614                              levelset_name,
14615                              levelset_author,
14616                              levelset_num_levels,
14617                              levelset_use_levelset_artwork))
14618       {
14619         if (levelset_copy_level_template)
14620           CopyLevelTemplateToUserLevelSet(levelset_subdir);
14621
14622         Request("New level set created!", REQ_CONFIRM);
14623
14624         AddUserLevelSetToLevelInfo(levelset_subdir);
14625         ChangeEditorToLevelSet(levelset_subdir);
14626       }
14627       else
14628       {
14629         Request("Creating new level set failed!", REQ_CONFIRM);
14630       }
14631     }
14632   }
14633   else if (type_id == ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE &&
14634            custom_element.num_change_pages < MAX_CHANGE_PAGES)
14635   {
14636     struct ElementInfo *ei = &element_info[properties_element];
14637
14638     // when modifying custom element, ask for copying level template
14639     if (level.use_custom_template && !AskToCopyAndModifyLevelTemplate())
14640       return;
14641
14642     setElementChangePages(ei, ei->num_change_pages + 1);
14643
14644     // set new change page to be new current change page
14645     ei->current_change_page = ei->num_change_pages - 1;
14646     ei->change = &ei->change_page[ei->current_change_page];
14647
14648     setElementChangeInfoToDefaults(ei->change);
14649
14650     DrawPropertiesWindow();
14651
14652     level.changed = TRUE;
14653   }
14654   else if (type_id == ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE &&
14655            custom_element.num_change_pages > MIN_CHANGE_PAGES)
14656   {
14657     struct ElementInfo *ei = &element_info[properties_element];
14658
14659     // when modifying custom element, ask for copying level template
14660     if (level.use_custom_template && !AskToCopyAndModifyLevelTemplate())
14661       return;
14662
14663     // copy all change pages after change page to be deleted
14664     for (i = ei->current_change_page; i < ei->num_change_pages - 1; i++)
14665       ei->change_page[i] = ei->change_page[i + 1];
14666
14667     setElementChangePages(ei, ei->num_change_pages - 1);
14668
14669     DrawPropertiesWindow();
14670
14671     level.changed = TRUE;
14672   }
14673 }
14674
14675 static void HandleGraphicbuttonGadgets(struct GadgetInfo *gi)
14676 {
14677   int type_id = gi->custom_type_id;
14678
14679   if (type_id == ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE ||
14680       type_id == ED_GRAPHICBUTTON_ID_NEXT_CHANGE_PAGE)
14681   {
14682     struct ElementInfo *ei = &element_info[properties_element];
14683     int step = BUTTON_STEPSIZE(gi->event.button);
14684
14685     step *= (type_id == ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE ? -1 : +1);
14686     ei->current_change_page += step;
14687
14688     if (ei->current_change_page < 0)
14689       ei->current_change_page = 0;
14690     else if (ei->current_change_page >= ei->num_change_pages)
14691       ei->current_change_page = ei->num_change_pages - 1;
14692
14693     DrawPropertiesWindow();
14694   }
14695   else if (type_id == ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE ||
14696            type_id == ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE)
14697   {
14698     struct ElementInfo *ei = &element_info[properties_element];
14699     int current_change_page = ei->current_change_page;
14700
14701     if (type_id == ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE)
14702     {
14703       element_info[EL_INTERNAL_CLIPBOARD_CHANGE].change_page[0] =
14704         ei->change_page[current_change_page];
14705     }
14706     else if (type_id == ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE)
14707     {
14708       // when modifying custom element, ask for copying level template
14709       if (level.use_custom_template && !AskToCopyAndModifyLevelTemplate())
14710         return;
14711
14712       ei->change_page[current_change_page] =
14713         element_info[EL_INTERNAL_CLIPBOARD_CHANGE].change_page[0];
14714
14715       level.changed = TRUE;
14716     }
14717
14718     DrawPropertiesWindow();
14719   }
14720 }
14721
14722 static void HandleRadiobuttons(struct GadgetInfo *gi)
14723 {
14724   int type_id = gi->custom_type_id;
14725
14726   *radiobutton_info[type_id].value =
14727     radiobutton_info[type_id].checked_value;
14728
14729   // do not mark level as modified for certain non-level-changing gadgets
14730   if (type_id >= ED_RADIOBUTTON_ID_EDITOR_FIRST &&
14731       type_id <= ED_RADIOBUTTON_ID_EDITOR_LAST)
14732     return;
14733
14734   level.changed = TRUE;
14735 }
14736
14737 static void HandleCheckbuttons(struct GadgetInfo *gi)
14738 {
14739   int type_id = gi->custom_type_id;
14740
14741   *checkbutton_info[type_id].value ^= TRUE;
14742
14743   if (type_id == ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID ||
14744       type_id == ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID ||
14745       type_id == ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH ||
14746       (((type_id >= ED_CHECKBUTTON_ID_CUSTOM_FIRST &&
14747          type_id <= ED_CHECKBUTTON_ID_CUSTOM_LAST) ||
14748         (type_id >= ED_CHECKBUTTON_ID_CHANGE_FIRST &&
14749          type_id <= ED_CHECKBUTTON_ID_CHANGE_LAST)) &&
14750        type_id != ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1))
14751   {
14752     CopyElementPropertiesToGame(properties_element);
14753   }
14754
14755   if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC)
14756   {
14757     UpdateCustomElementGraphicGadgets();
14758   }
14759   else if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1 ||
14760            type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2)
14761   {
14762     boolean template_related_changes_found = FALSE;
14763     int i;
14764
14765     // check if any custom, group or empty elements have been changed
14766     for (i = 0; i < NUM_FILE_ELEMENTS; i++)
14767       if ((IS_CUSTOM_ELEMENT(i) ||
14768            IS_GROUP_ELEMENT(i) ||
14769            IS_EMPTY_ELEMENT(i)) &&
14770           element_info[i].modified_settings)
14771         template_related_changes_found = TRUE;
14772
14773     if (level.use_custom_template &&
14774         !fileExists(getGlobalLevelTemplateFilename()))
14775     {
14776       Request("No level template found!", REQ_CONFIRM);
14777
14778       level.use_custom_template = FALSE;
14779
14780       ModifyGadget(gi, GDI_CHECKED, FALSE, GDI_END);
14781
14782       return;
14783     }
14784
14785     if (level.use_custom_template &&
14786         template_related_changes_found &&
14787         !Request("Discard changes and use level template?", REQ_ASK))
14788     {
14789       level.use_custom_template = FALSE;
14790
14791       ModifyGadget(gi, GDI_CHECKED, FALSE, GDI_END);
14792
14793       return;
14794     }
14795
14796     if (!level.use_custom_template &&
14797         Request("Copy settings from level template?", REQ_ASK))
14798     {
14799       return;
14800     }
14801
14802     LoadLevelTemplate(level.use_custom_template ? -1 : level_nr);
14803
14804     DrawEditModeWindow();
14805   }
14806   else if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_3)
14807   {
14808     if (setup.editor.use_template_for_new_levels &&
14809         !fileExists(getGlobalLevelTemplateFilename()))
14810     {
14811       Request("No level template found!", REQ_CONFIRM);
14812
14813       return;
14814     }
14815
14816     if (setup.editor.use_template_for_new_levels &&
14817         level.changed &&
14818         !Request("Discard level and load template?", REQ_ASK))
14819     {
14820       return;
14821     }
14822
14823     if (!setup.editor.use_template_for_new_levels &&
14824         level.changed &&
14825         !Request("Discard level and use empty level?", REQ_ASK))
14826     {
14827       return;
14828     }
14829
14830     LoadLevel(level_nr);
14831     LoadScore(level_nr);
14832
14833     TapeErase();
14834
14835     ResetUndoBuffer();
14836     DrawEditModeWindow();
14837   }
14838   else if (type_id == ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS)
14839   {
14840     SetAutomaticNumberOfGemsNeeded();
14841   }
14842
14843   // do not mark level as modified for certain non-level-changing gadgets
14844   if ((type_id >= ED_CHECKBUTTON_ID_LEVELSET_FIRST &&
14845        type_id <= ED_CHECKBUTTON_ID_LEVELSET_LAST) ||
14846       (type_id >= ED_CHECKBUTTON_ID_EDITOR_FIRST &&
14847        type_id <= ED_CHECKBUTTON_ID_EDITOR_LAST &&
14848        type_id != ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2) ||
14849       type_id == ED_CHECKBUTTON_ID_STICK_ELEMENT)
14850     return;
14851
14852   level.changed = TRUE;
14853 }
14854
14855 static void HandleControlButtons(struct GadgetInfo *gi)
14856 {
14857   static int last_level_drawing_function = GADGET_ID_SINGLE_ITEMS;
14858   static int last_edit_mode = ED_MODE_DRAWING;
14859   static int last_custom_copy_mode = -1;
14860   static int last_button = 0;
14861   int id = gi->custom_id;
14862   int button = gi->event.button;
14863   int step = BUTTON_STEPSIZE(button);
14864   int new_element = BUTTON_ELEMENT(button);
14865   int last_properties_element = properties_element;
14866   int x, y;
14867
14868   if (edit_mode == ED_MODE_DRAWING && drawing_function == GADGET_ID_TEXT)
14869     DrawLevelText(0, 0, 0, TEXT_END);
14870
14871   if (id < ED_NUM_CTRL1_BUTTONS &&
14872       id != GADGET_ID_SINGLE_ITEMS &&
14873       id != GADGET_ID_PICK_ELEMENT &&
14874       edit_mode != ED_MODE_DRAWING &&
14875       drawing_function != GADGET_ID_PICK_ELEMENT &&
14876       !(GetKeyModState() & KMOD_Control))
14877     ChangeEditModeWindow(ED_MODE_DRAWING);
14878
14879   // element copy mode active, but no element button pressed => deactivate
14880   if (last_custom_copy_mode != -1 && id < ED_NUM_CTRL_BUTTONS)
14881     last_custom_copy_mode = -1;
14882
14883   // when showing palette on element buttons, change element of button used
14884   if (editor.palette.show_on_element_buttons &&
14885       id >= GADGET_ID_ELEMENT_LEFT && id <= GADGET_ID_ELEMENT_RIGHT)
14886   {
14887     last_button = id - GADGET_ID_ELEMENT_LEFT + 1;
14888
14889     id = GADGET_ID_PALETTE;
14890   }
14891
14892   switch (id)
14893   {
14894     case GADGET_ID_SCROLL_LEFT:
14895       if (level_xpos >= 0)
14896       {
14897         if (lev_fieldx < ed_fieldx - 2)
14898           break;
14899
14900         level_xpos -= step;
14901         if (level_xpos < -1)
14902           level_xpos = -1;
14903         if (button == 1)
14904           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_RIGHT);
14905         else
14906           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14907
14908         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
14909                      GDI_SCROLLBAR_ITEM_POSITION, level_xpos + 1, GDI_END);
14910       }
14911       break;
14912
14913     case GADGET_ID_SCROLL_RIGHT:
14914       if (level_xpos <= lev_fieldx - ed_fieldx)
14915       {
14916         if (lev_fieldx < ed_fieldx - 2)
14917           break;
14918
14919         level_xpos += step;
14920         if (level_xpos > lev_fieldx - ed_fieldx + 1)
14921           level_xpos = lev_fieldx - ed_fieldx + 1;
14922         if (button == 1)
14923           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_LEFT);
14924         else
14925           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14926
14927         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
14928                      GDI_SCROLLBAR_ITEM_POSITION, level_xpos + 1, GDI_END);
14929       }
14930       break;
14931
14932     case GADGET_ID_SCROLL_UP:
14933       if (level_ypos >= 0)
14934       {
14935         if (lev_fieldy < ed_fieldy - 2)
14936           break;
14937
14938         level_ypos -= step;
14939         if (level_ypos < -1)
14940           level_ypos = -1;
14941         if (button == 1)
14942           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_DOWN);
14943         else
14944           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14945
14946         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
14947                      GDI_SCROLLBAR_ITEM_POSITION, level_ypos + 1, GDI_END);
14948       }
14949       break;
14950
14951     case GADGET_ID_SCROLL_DOWN:
14952       if (level_ypos <= lev_fieldy - ed_fieldy)
14953       {
14954         if (lev_fieldy < ed_fieldy - 2)
14955           break;
14956
14957         level_ypos += step;
14958         if (level_ypos > lev_fieldy - ed_fieldy + 1)
14959           level_ypos = lev_fieldy - ed_fieldy + 1;
14960         if (button == 1)
14961           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_UP);
14962         else
14963           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14964
14965         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
14966                      GDI_SCROLLBAR_ITEM_POSITION, level_ypos + 1, GDI_END);
14967       }
14968       break;
14969
14970     case GADGET_ID_SCROLL_HORIZONTAL:
14971       level_xpos = gi->event.item_position - 1;
14972
14973       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14974       BackToFront();
14975
14976       break;
14977
14978     case GADGET_ID_SCROLL_VERTICAL:
14979       level_ypos = gi->event.item_position - 1;
14980
14981       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14982       BackToFront();
14983
14984       break;
14985
14986     case GADGET_ID_SCROLL_LIST_UP:
14987     case GADGET_ID_SCROLL_LIST_DOWN:
14988     case GADGET_ID_SCROLL_LIST_VERTICAL:
14989       if (id == GADGET_ID_SCROLL_LIST_VERTICAL)
14990         element_shift = gi->event.item_position * ED_ELEMENTLIST_BUTTONS_HORIZ;
14991       else
14992       {
14993         step *= (id == GADGET_ID_SCROLL_LIST_UP ? -1 : +1);
14994         element_shift += step * ED_ELEMENTLIST_BUTTONS_HORIZ;
14995
14996         if (element_shift < 0)
14997           element_shift = 0;
14998         if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
14999           element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
15000
15001         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15002                      GDI_SCROLLBAR_ITEM_POSITION,
15003                      element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15004       }
15005
15006       ModifyEditorElementList();
15007
15008       break;
15009
15010     case GADGET_ID_PROPERTIES:
15011       // always switch off element properties when they are already displayed
15012       last_properties_element = new_element;
15013     case GADGET_ID_ELEMENT_LEFT:
15014     case GADGET_ID_ELEMENT_MIDDLE:
15015     case GADGET_ID_ELEMENT_RIGHT:
15016       properties_element = (id == GADGET_ID_ELEMENT_LEFT   ? new_element1 :
15017                             id == GADGET_ID_ELEMENT_MIDDLE ? new_element2 :
15018                             id == GADGET_ID_ELEMENT_RIGHT  ? new_element3 :
15019                             new_element);
15020
15021       if (edit_mode != ED_MODE_PROPERTIES)
15022       {
15023         last_edit_mode = edit_mode;
15024
15025         ChangeEditModeWindow(ED_MODE_PROPERTIES);
15026
15027         last_level_drawing_function = drawing_function;
15028         ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS],
15029                       MB_LEFTBUTTON);
15030       }
15031       else if (properties_element != last_properties_element)
15032       {
15033         DrawEditModeWindow();
15034       }
15035       else
15036       {
15037         ChangeEditModeWindow(ED_MODE_DRAWING);
15038
15039         ClickOnGadget(level_editor_gadget[last_level_drawing_function],
15040                       MB_LEFTBUTTON);
15041       }
15042       break;
15043
15044     case GADGET_ID_PALETTE:
15045       if (edit_mode != ED_MODE_PALETTE)
15046       {
15047         last_edit_mode = edit_mode;
15048
15049         ChangeEditModeWindow(ED_MODE_PALETTE);
15050       }
15051       else
15052       {
15053         ChangeEditModeWindow(last_edit_mode);
15054       }
15055       break;
15056
15057     case GADGET_ID_WRAP_LEFT:
15058       WrapLevel(-step, 0);
15059       break;
15060
15061     case GADGET_ID_WRAP_RIGHT:
15062       WrapLevel(step, 0);
15063       break;
15064
15065     case GADGET_ID_WRAP_UP:
15066       WrapLevel(0, -step);
15067       break;
15068
15069     case GADGET_ID_WRAP_DOWN:
15070       WrapLevel(0, step);
15071       break;
15072
15073     case GADGET_ID_SINGLE_ITEMS:
15074     case GADGET_ID_CONNECTED_ITEMS:
15075     case GADGET_ID_LINE:
15076     case GADGET_ID_ARC:
15077     case GADGET_ID_TEXT:
15078     case GADGET_ID_RECTANGLE:
15079     case GADGET_ID_FILLED_BOX:
15080     case GADGET_ID_FLOOD_FILL:
15081     case GADGET_ID_GRAB_BRUSH:
15082     case GADGET_ID_PICK_ELEMENT:
15083       if (drawing_function != GADGET_ID_PICK_ELEMENT)
15084         last_drawing_function = drawing_function;
15085       drawing_function = id;
15086       draw_with_brush = FALSE;
15087       break;
15088
15089     case GADGET_ID_RANDOM_PLACEMENT:
15090       RandomPlacement(new_element);
15091       break;
15092
15093     case GADGET_ID_ZOOM:
15094       // zoom level editor tile size in or out (or reset to default size)
15095       ed_tilesize = (button == 1 ? ed_tilesize * 2 :
15096                      button == 2 ? ed_tilesize_default :
15097                      button == 3 ? ed_tilesize / 2 : ed_tilesize);
15098
15099       // when using touch device, cycle through all zoom tilesizes
15100       if (runtime.uses_touch_device && ed_tilesize > TILESIZE)
15101         ed_tilesize = MICRO_TILESIZE;
15102
15103       // limit zoom level by upper and lower bound
15104       ed_tilesize = MIN(MAX(MICRO_TILESIZE, ed_tilesize), TILESIZE);
15105
15106       InitZoomLevelSettings(ed_tilesize);
15107
15108       if (edit_mode == ED_MODE_DRAWING)
15109       {
15110         DrawDrawingWindow();
15111
15112         // redraw zoom gadget info text
15113         PrintEditorGadgetInfoText(level_editor_gadget[id]);
15114       }
15115
15116       // save current editor zoom tilesize
15117       SaveSetup_AutoSetup();
15118
15119       break;
15120
15121     case GADGET_ID_CUSTOM_COPY_FROM:
15122     case GADGET_ID_CUSTOM_COPY_TO:
15123     case GADGET_ID_CUSTOM_EXCHANGE:
15124       last_custom_copy_mode = id;
15125       last_drawing_function = drawing_function;
15126       break;
15127
15128     case GADGET_ID_CUSTOM_COPY:
15129       CopyCustomElement(properties_element, -1, id);
15130       break;
15131
15132     case GADGET_ID_CUSTOM_PASTE:
15133       CopyCustomElement(-1, properties_element, id);
15134       break;
15135
15136     case GADGET_ID_UNDO:
15137       if (button < 0)   // keep button value (even if modifier keys are pressed)
15138         button = -button;
15139       else if (button == 1 && GetKeyModState() & (KMOD_Shift | KMOD_Control))
15140         button = 3;
15141
15142       if (button == 1 && undo_buffer_steps == 0)
15143       {
15144         Request("Undo buffer empty!", REQ_CONFIRM);
15145
15146         break;
15147       }
15148       else if (button == 2)
15149       {
15150         break;
15151       }
15152       else if (button == 3 && redo_buffer_steps == 0)
15153       {
15154         Request("Redo buffer empty!", REQ_CONFIRM);
15155
15156         break;
15157       }
15158
15159       if (edit_mode != ED_MODE_DRAWING)
15160         ChangeEditModeWindow(ED_MODE_DRAWING);
15161
15162       if (button == 1)
15163       {
15164         // undo
15165
15166         undo_buffer_position =
15167           (undo_buffer_position - 1 + NUM_UNDO_STEPS) % NUM_UNDO_STEPS;
15168
15169         undo_buffer_steps--;
15170         redo_buffer_steps++;
15171       }
15172       else
15173       {
15174         // redo
15175
15176         undo_buffer_position = (undo_buffer_position + 1) % NUM_UNDO_STEPS;
15177
15178         undo_buffer_steps++;
15179         redo_buffer_steps--;
15180       }
15181
15182       for (x = 0; x < lev_fieldx; x++)
15183         for (y = 0; y < lev_fieldy; y++)
15184           Tile[x][y] = UndoBuffer[undo_buffer_position][x][y];
15185
15186       // check if undo operation forces change of border style
15187       CheckLevelBorderElement(FALSE);
15188
15189       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15190
15191       break;
15192
15193     case GADGET_ID_CONF:
15194       if (edit_mode != ED_MODE_LEVELCONFIG)
15195       {
15196         last_edit_mode = edit_mode;
15197
15198         ChangeEditModeWindow(ED_MODE_LEVELCONFIG);
15199       }
15200       else
15201       {
15202         ChangeEditModeWindow(ED_MODE_DRAWING);
15203       }
15204       break;
15205
15206     case GADGET_ID_CLEAR:
15207       if (edit_mode != ED_MODE_DRAWING)
15208         ChangeEditModeWindow(ED_MODE_DRAWING);
15209
15210       for (x = 0; x < MAX_LEV_FIELDX; x++)
15211         for (y = 0; y < MAX_LEV_FIELDY; y++)
15212           Tile[x][y] = (button == 1 ? EL_EMPTY : new_element);
15213
15214       CopyLevelToUndoBuffer(GADGET_ID_CLEAR);
15215
15216       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15217       break;
15218
15219     case GADGET_ID_SAVE:
15220     {
15221       // saving read-only levels into personal level set modifies global vars
15222       // "leveldir_current" and "level_nr"; restore them after saving level
15223       LevelDirTree *leveldir_former = leveldir_current;
15224       int level_nr_former = level_nr;
15225       char *level_filename;
15226       boolean new_level;
15227
15228       if (leveldir_current->readonly &&
15229           !PrepareSavingIntoPersonalLevelSet())
15230         break;
15231
15232       level_filename = getDefaultLevelFilename(level_nr);
15233       new_level = !fileExists(level_filename);
15234
15235       if (new_level ||
15236           Request("Save this level and kill the old?", REQ_ASK))
15237       {
15238         if (leveldir_former->readonly)
15239           ModifyLevelConfigForSavingIntoPersonalLevelSet(leveldir_former->name);
15240
15241         SetAutomaticNumberOfGemsNeeded();
15242
15243         CopyPlayfield(Tile, level.field);
15244         SaveLevel(level_nr);
15245
15246         level.changed = FALSE;
15247
15248         if (new_level)
15249         {
15250           char level_saved_msg[64];
15251
15252           if (leveldir_former->readonly)
15253             sprintf(level_saved_msg,
15254                     "Level saved as level %d into personal level set!",
15255                     level_nr);
15256           else
15257             strcpy(level_saved_msg, "Level saved!");
15258
15259           Request(level_saved_msg, REQ_CONFIRM);
15260         }
15261       }
15262
15263       // "cd" back to copied-from levelset (in case of saved read-only level)
15264       leveldir_current = leveldir_former;
15265       level_nr = level_nr_former;
15266
15267       break;
15268     }
15269
15270     case GADGET_ID_TEST:
15271       if (LevelChanged())
15272         level.game_version = GAME_VERSION_ACTUAL;
15273
15274       CopyPlayfield(level.field, TileBackup);
15275       CopyPlayfield(Tile, level.field);
15276
15277       CopyNativeLevel_RND_to_Native(&level);
15278
15279       UnmapLevelEditorGadgets();
15280       UndrawSpecialEditorDoor();
15281
15282       CloseDoor(DOOR_CLOSE_ALL);
15283
15284       // needed before playing if editor playfield area has different size
15285       ClearRectangle(drawto, REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
15286
15287       // redraw_mask = REDRAW_ALL;
15288
15289       level_editor_test_game = TRUE;
15290
15291       StartGameActions(FALSE, setup.autorecord, level.random_seed);
15292
15293       break;
15294
15295     case GADGET_ID_EXIT:
15296       RequestExitLevelEditor(TRUE, FALSE);  // if level has changed, ask user
15297       break;
15298
15299     default:
15300       if (id >= GADGET_ID_ELEMENTLIST_FIRST &&
15301           id <= GADGET_ID_ELEMENTLIST_LAST)
15302       {
15303         int element_position = id - GADGET_ID_ELEMENTLIST_FIRST;
15304
15305         new_element = editor_elements[element_position + element_shift];
15306
15307         if (IS_EDITOR_CASCADE(new_element))
15308         {
15309           int i;
15310
15311           for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
15312           {
15313             int *cascade_element = &(*editor_elements_info[i].headline_list)[0];
15314             boolean *cascade_value = editor_elements_info[i].setup_cascade_value;
15315
15316             if (*cascade_element == new_element)
15317             {
15318               *cascade_element = EL_CASCADE_TOGGLE(*cascade_element);
15319               *cascade_value = IS_EDITOR_CASCADE_ACTIVE(*cascade_element);
15320
15321               // update element selection list
15322               ReinitializeElementList();
15323               ModifyEditorElementList();
15324
15325               // update cascading gadget info text
15326               PrintEditorGadgetInfoText(level_editor_gadget[id]);
15327
15328               // save current editor cascading state
15329               SaveSetup_EditorCascade();
15330
15331               break;
15332             }
15333           }
15334
15335           break;
15336         }
15337
15338         if (last_custom_copy_mode != -1)
15339         {
15340           if (CopyCustomElement(properties_element, new_element,
15341                                 last_custom_copy_mode))
15342           {
15343             ClickOnGadget(level_editor_gadget[last_drawing_function],
15344                           MB_LEFTBUTTON);
15345
15346             last_custom_copy_mode = -1;
15347           }
15348
15349           break;
15350         }
15351
15352         // change element of button used to show palette
15353         if (editor.palette.show_on_element_buttons)
15354           button = last_button;
15355
15356         PickDrawingElement(button, new_element);
15357
15358         if (!stick_element_properties_window &&
15359             drawing_function != GADGET_ID_PICK_ELEMENT &&
15360             !(GetKeyModState() & KMOD_Control))
15361         {
15362           properties_element = new_element;
15363           if (edit_mode == ED_MODE_PROPERTIES)
15364             DrawPropertiesWindow();
15365         }
15366
15367         if (drawing_function == GADGET_ID_PICK_ELEMENT)
15368           ClickOnGadget(level_editor_gadget[last_drawing_function],
15369                         MB_LEFTBUTTON);
15370
15371         if (!use_permanent_palette)
15372           ChangeEditModeWindow(last_edit_mode);
15373       }
15374 #ifdef DEBUG
15375       else if (gi->event.type == GD_EVENT_PRESSED)
15376         Debug("editor", "default: HandleControlButtons: GD_EVENT_PRESSED(%d)", id);
15377       else if (gi->event.type == GD_EVENT_RELEASED)
15378         Debug("editor", "default: HandleControlButtons: GD_EVENT_RELEASED(%d)", id);
15379       else if (gi->event.type == GD_EVENT_MOVING)
15380         Debug("editor", "default: HandleControlButtons: GD_EVENT_MOVING(%d)", id);
15381       else
15382         Debug("editor", "default: HandleControlButtons: ? (id == %d)", id);
15383 #endif
15384       break;
15385   }
15386 }
15387
15388 void HandleLevelEditorKeyInput(Key key)
15389 {
15390   char letter = getCharFromKey(key);
15391
15392   if (drawing_function == GADGET_ID_TEXT &&
15393       DrawLevelText(0, 0, 0, TEXT_QUERY_TYPING) == TRUE)
15394   {
15395     if (letter)
15396       DrawLevelText(0, 0, letter, TEXT_WRITECHAR);
15397     else if (key == KSYM_Delete || key == KSYM_BackSpace)
15398       DrawLevelText(0, 0, 0, TEXT_BACKSPACE);
15399     else if (key == KSYM_Return)
15400       DrawLevelText(0, 0, 0, TEXT_NEWLINE);
15401     else if (key == KSYM_Escape)
15402       DrawLevelText(0, 0, 0, TEXT_END);
15403
15404     return;
15405   }
15406
15407   int id = GADGET_ID_NONE;
15408   int new_element_shift = element_shift;
15409   int step = ED_ELEMENTLIST_BUTTONS_VERT - 1;
15410   int button = MB_LEFTBUTTON;
15411   int i;
15412
15413   switch (key)
15414   {
15415     case KSYM_Left:
15416       id = GADGET_ID_SCROLL_LEFT;
15417       break;
15418     case KSYM_Right:
15419       id = GADGET_ID_SCROLL_RIGHT;
15420       break;
15421     case KSYM_Up:
15422       id = GADGET_ID_SCROLL_UP;
15423       break;
15424     case KSYM_Down:
15425       id = GADGET_ID_SCROLL_DOWN;
15426       break;
15427
15428     case KSYM_Page_Up:
15429     case KSYM_Page_Down:
15430       step *= (key == KSYM_Page_Up ? -1 : +1);
15431       element_shift += step * ED_ELEMENTLIST_BUTTONS_HORIZ;
15432
15433       if (element_shift < 0)
15434         element_shift = 0;
15435       if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
15436         element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
15437
15438       ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15439                    GDI_SCROLLBAR_ITEM_POSITION,
15440                    element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15441
15442       ModifyEditorElementList();
15443
15444       break;
15445
15446     case KSYM_Home:
15447     case KSYM_End:
15448       element_shift = (key == KSYM_Home ? 0 :
15449                        num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS);
15450
15451       ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15452                    GDI_SCROLLBAR_ITEM_POSITION,
15453                    element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15454
15455       ModifyEditorElementList();
15456
15457       break;
15458
15459     case KSYM_Insert:
15460     case KSYM_Delete:
15461
15462       // this is needed to prevent interference with running "True X-Mouse"
15463       if (GetKeyModStateFromEvents() & KMOD_Control)
15464         break;
15465
15466       // check for last or next editor cascade block in element list
15467       for (i = 0; i < num_editor_elements; i++)
15468       {
15469         if ((key == KSYM_Insert && i == element_shift) ||
15470             (key == KSYM_Delete && new_element_shift > element_shift))
15471           break;
15472
15473         // jump to next cascade block (or to start of element list)
15474         if (i == 0 || IS_EDITOR_CASCADE(editor_elements[i]))
15475           new_element_shift = i;
15476       }
15477
15478       if (i < num_editor_elements)
15479         element_shift = new_element_shift;
15480
15481       if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
15482         element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
15483
15484       ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15485                    GDI_SCROLLBAR_ITEM_POSITION,
15486                    element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15487
15488       ModifyEditorElementList();
15489
15490       break;
15491
15492     case KSYM_Escape:
15493       if (edit_mode == ED_MODE_DRAWING)
15494         RequestExitLevelEditor(setup.ask_on_escape_editor, TRUE);
15495       else if (edit_mode == ED_MODE_LEVELCONFIG)
15496         HandleControlButtons(level_editor_gadget[GADGET_ID_CONF]);
15497       else if (edit_mode == ED_MODE_PROPERTIES)
15498         HandleControlButtons(level_editor_gadget[GADGET_ID_PROPERTIES]);
15499       else if (edit_mode == ED_MODE_PALETTE)
15500         HandleControlButtons(level_editor_gadget[GADGET_ID_PALETTE]);
15501       else              // should never happen
15502         ChangeEditModeWindow(ED_MODE_DRAWING);
15503
15504       break;
15505
15506     default:
15507       break;
15508   }
15509
15510   if (id != GADGET_ID_NONE)
15511     ClickOnGadget(level_editor_gadget[id], button);
15512   else if (letter == '1' || letter == '?')
15513     ClickOnGadget(level_editor_gadget[GADGET_ID_ELEMENT_LEFT], button);
15514   else if (letter == '2')
15515     ClickOnGadget(level_editor_gadget[GADGET_ID_ELEMENT_MIDDLE], button);
15516   else if (letter == '3')
15517     ClickOnGadget(level_editor_gadget[GADGET_ID_ELEMENT_RIGHT], button);
15518   else if (letter == '.')
15519     ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS], button);
15520   else if (letter == 'U')
15521     ClickOnGadget(level_editor_gadget[GADGET_ID_UNDO], 3);
15522   else if (letter == '-' || key == KSYM_KP_Subtract)
15523     ClickOnGadget(level_editor_gadget[GADGET_ID_ZOOM], 3);
15524   else if (letter == '0' || key == KSYM_KP_0)
15525     ClickOnGadget(level_editor_gadget[GADGET_ID_ZOOM], 2);
15526   else if (letter == '+' || key == KSYM_KP_Add ||
15527            letter == '=')       // ("Shift-=" is "+" on US keyboards)
15528     ClickOnGadget(level_editor_gadget[GADGET_ID_ZOOM], 1);
15529   else if (key == KSYM_Return ||
15530            key == KSYM_space ||
15531            key == setup.shortcut.toggle_pause)
15532     ClickOnGadget(level_editor_gadget[GADGET_ID_TEST], button);
15533   else
15534     for (i = 0; i < ED_NUM_CTRL_BUTTONS; i++)
15535       if (letter && letter == controlbutton_info[i].shortcut)
15536         if (!anyTextGadgetActive())
15537           ClickOnGadget(level_editor_gadget[i], button);
15538
15539   if (draw_with_brush)
15540   {
15541     if (letter == 'x')
15542       FlipBrushX();
15543     else if (letter == 'y')
15544       FlipBrushY();
15545     else if (letter == 'z')
15546       RotateBrush();
15547   }
15548 }
15549
15550 static void HandleLevelEditorIdle_Properties(void)
15551 {
15552   int element_border = graphic_info[IMG_EDITOR_ELEMENT_BORDER].border_size;
15553   int x = editor.settings.element_graphic.x + element_border;
15554   int y = editor.settings.element_graphic.y + element_border;
15555   static DelayCounter action_delay = { 0 };
15556   int i;
15557
15558   action_delay.value = GameFrameDelay;
15559
15560   if (!DelayReached(&action_delay))
15561     return;
15562
15563   for (i = 0; i < ED_NUM_SELECTBOX; i++)
15564   {
15565     struct GadgetInfo *gi = level_editor_gadget[selectbox_info[i].gadget_id];
15566
15567     if (gi->mapped && gi->active && gi->selectbox.open)
15568       return;
15569   }
15570
15571   DrawEditorElementAnimation(SX + x, SY + y);
15572
15573   redraw_mask |= REDRAW_FIELD;
15574
15575   FrameCounter++;       // increase animation frame counter
15576 }
15577
15578 static void HandleLevelEditorIdle_Drawing(void)
15579 {
15580   static boolean last_highlighted = FALSE;
15581   static boolean last_highlighted_similar = FALSE;
15582   boolean highlighted = (GetKeyModState() & KMOD_Alt);
15583   boolean highlighted_similar = (GetKeyModState() & KMOD_Shift);
15584
15585   if (highlighted != last_highlighted ||
15586       (highlighted && highlighted_similar != last_highlighted_similar))
15587   {
15588     DrawAreaElementHighlight(highlighted, highlighted_similar);
15589
15590     redraw_mask |= REDRAW_FIELD;
15591   }
15592
15593   last_highlighted = highlighted;
15594   last_highlighted_similar = highlighted_similar;
15595 }
15596
15597 void HandleLevelEditorIdle(void)
15598 {
15599   if (edit_mode == ED_MODE_PROPERTIES)
15600     HandleLevelEditorIdle_Properties();
15601   else if (edit_mode == ED_MODE_DRAWING)
15602     HandleLevelEditorIdle_Drawing();
15603 }
15604
15605 static void ClearEditorGadgetInfoText(void)
15606 {
15607   DrawBackground(INFOTEXT_XPOS, INFOTEXT_YPOS, INFOTEXT_XSIZE, INFOTEXT_YSIZE);
15608 }
15609
15610 void PrintEditorGadgetInfoText(struct GadgetInfo *gi)
15611 {
15612   char infotext[MAX_OUTPUT_LINESIZE + 1];
15613   int max_infotext_len = getMaxInfoTextLength();
15614
15615   if (gi == NULL || strlen(gi->info_text) == 0)
15616     return;
15617
15618   strncpy(infotext, gi->info_text, max_infotext_len);
15619   infotext[max_infotext_len] = '\0';
15620
15621   if (gi->custom_id < ED_NUM_CTRL_BUTTONS)
15622   {
15623     int key = controlbutton_info[gi->custom_id].shortcut;
15624
15625     if (key)
15626     {
15627       char shortcut[MAX_OUTPUT_LINESIZE + 1];
15628
15629       if (gi->custom_id == GADGET_ID_SINGLE_ITEMS)
15630         sprintf(shortcut, " ('.' or '%c')", key);
15631       else if (gi->custom_id == GADGET_ID_PICK_ELEMENT)
15632         sprintf(shortcut, " ('%c' or 'Ctrl')", key);
15633       else if (gi->custom_id == GADGET_ID_TEST)
15634         sprintf(shortcut, " ('Enter' or 'Shift-%c')", key);
15635       else if (gi->custom_id == GADGET_ID_UNDO)
15636         sprintf(shortcut, " ('%c/Shift-U')", key);
15637       else if (gi->custom_id == GADGET_ID_ZOOM)
15638         sprintf(shortcut, " ('%c', '0', '-')", key);
15639       else
15640         sprintf(shortcut, " ('%s%c')",
15641                 (key >= 'A' && key <= 'Z' ? "Shift-" : ""), key);
15642
15643       if (strlen(infotext) + strlen(shortcut) <= max_infotext_len)
15644         strcat(infotext, shortcut);
15645     }
15646   }
15647
15648   DrawText(INFOTEXT_XPOS, INFOTEXT_YPOS, infotext, INFOTEXT_FONT);
15649 }
15650
15651 void HandleEditorGadgetInfoText(void *ptr)
15652 {
15653   struct GadgetInfo *gi = (struct GadgetInfo *)ptr;
15654
15655   if (game_status != GAME_MODE_EDITOR)
15656     return;
15657
15658   ClearEditorGadgetInfoText();
15659
15660   if (gi == NULL || gi->event.type == GD_EVENT_INFO_LEAVING)
15661     return;
15662
15663   // misuse this function to delete brush cursor, if needed
15664   if (edit_mode == ED_MODE_DRAWING && draw_with_brush)
15665     DeleteBrushFromCursor();
15666
15667   PrintEditorGadgetInfoText(gi);
15668 }
15669
15670 static void HandleDrawingAreaInfo(struct GadgetInfo *gi)
15671 {
15672   int id = gi->custom_id;
15673   int type_id = gi->custom_type_id;
15674   int sx = gi->event.x;
15675   int sy = gi->event.y;
15676   int lx = sx + level_xpos;
15677   int ly = sy + level_ypos;
15678   int min_sx = 0, min_sy = 0;
15679   int max_sx = gi->drawing.area_xsize - 1;
15680   int max_sy = gi->drawing.area_ysize - 1;
15681   int actual_drawing_function = drawing_function;
15682   int max_infotext_len = getMaxInfoTextLength();
15683   char infotext[MAX_OUTPUT_LINESIZE + 1];
15684
15685   infotext[0] = '\0';           // start with empty info text
15686
15687   // pressed Control key: simulate picking element
15688   if (GetKeyModState() & KMOD_Control)
15689     actual_drawing_function = GADGET_ID_PICK_ELEMENT;
15690
15691   ClearEditorGadgetInfoText();
15692
15693   if (gi->event.type == GD_EVENT_INFO_LEAVING)
15694     return;
15695
15696   // make sure to stay inside drawing area boundaries
15697   sx = (sx < min_sx ? min_sx : sx > max_sx ? max_sx : sx);
15698   sy = (sy < min_sy ? min_sy : sy > max_sy ? max_sy : sy);
15699
15700   if (id == GADGET_ID_DRAWING_LEVEL)
15701   {
15702     if (button_status)
15703     {
15704       int min_lx = 0, min_ly = 0;
15705       int max_lx = lev_fieldx - 1, max_ly = lev_fieldy - 1;
15706
15707       // get positions inside level field
15708       lx = sx + level_xpos;
15709       ly = sy + level_ypos;
15710
15711       // make sure to stay inside level field boundaries
15712       lx = (lx < min_lx ? min_lx : lx > max_lx ? max_lx : lx);
15713       ly = (ly < min_ly ? min_ly : ly > max_ly ? max_ly : ly);
15714
15715       // correct drawing area positions accordingly
15716       sx = lx - level_xpos;
15717       sy = ly - level_ypos;
15718     }
15719
15720     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
15721     {
15722       if (button_status)        // if (gi->state == GD_BUTTON_PRESSED)
15723       {
15724         static int start_lx = 0;
15725         static int start_ly = 0;
15726         char *text;
15727
15728         if (gi->event.type == GD_EVENT_PRESSED)
15729         {
15730           start_lx = lx;
15731           start_ly = ly;
15732         }
15733
15734         switch (actual_drawing_function)
15735         {
15736           case GADGET_ID_SINGLE_ITEMS:
15737             text = "Drawing single items";
15738             break;
15739           case GADGET_ID_CONNECTED_ITEMS:
15740             text = "Drawing connected items";
15741             break;
15742           case GADGET_ID_LINE:
15743             text = "Drawing line";
15744             break;
15745           case GADGET_ID_ARC:
15746             text = "Drawing arc";
15747             break;
15748           case GADGET_ID_TEXT:
15749             text = "Setting text cursor";
15750             break;
15751           case GADGET_ID_RECTANGLE:
15752             text = "Drawing rectangle";
15753             break;
15754           case GADGET_ID_FILLED_BOX:
15755             text = "Drawing filled box";
15756             break;
15757           case GADGET_ID_FLOOD_FILL:
15758             text = "Flood fill";
15759             break;
15760           case GADGET_ID_GRAB_BRUSH:
15761             text = "Grabbing brush";
15762             break;
15763           case GADGET_ID_PICK_ELEMENT:
15764             text = "Picking element";
15765             break;
15766
15767           default:
15768             text = "Drawing position";
15769             break;
15770         }
15771
15772         if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
15773           sprintf(infotext, "%s: %d, %d", text, lx, ly);
15774         else
15775           sprintf(infotext, "%s: %d, %d", text,
15776                   ABS(lx - start_lx) + 1, ABS(ly - start_ly) + 1);
15777       }
15778       else if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
15779         strncpy(infotext, getElementInfoText(Tile[lx][ly]), max_infotext_len);
15780       else
15781         sprintf(infotext, "Level position: %d, %d", lx, ly);
15782     }
15783
15784     // misuse this function to draw brush cursor, if needed
15785     if (edit_mode == ED_MODE_DRAWING && draw_with_brush && !button_status)
15786     {
15787       if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
15788         CopyBrushToCursor(sx, sy);
15789       else
15790         DeleteBrushFromCursor();
15791     }
15792
15793     if (!draw_with_brush)
15794       UpdateBrushPosition(sx, sy);
15795   }
15796   else if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
15797   {
15798     int pos = sx * drawingarea_info[type_id].area_ysize + sy;
15799     int element = drawingarea_info[type_id].value[pos];
15800
15801     strncpy(infotext, getElementInfoText(element), max_infotext_len);
15802   }
15803   else
15804   {
15805     if (id == GADGET_ID_CUSTOM_CONTENT)
15806       sprintf(infotext, "custom element content position: %d, %d", sx, sy);
15807     else if (id == GADGET_ID_GROUP_CONTENT)
15808       sprintf(infotext, "group element position: %d", sx + 1);
15809     else if (id >= GADGET_ID_YAMYAM_CONTENT_0 &&
15810              id <= GADGET_ID_YAMYAM_CONTENT_7)
15811       sprintf(infotext, "content area %d position: %d, %d",
15812               id - GADGET_ID_YAMYAM_CONTENT_0 + 1, sx, sy);
15813     else if (id >= GADGET_ID_MAGIC_BALL_CONTENT_0 &&
15814              id <= GADGET_ID_MAGIC_BALL_CONTENT_7)
15815       sprintf(infotext, "content area %d position: %d, %d",
15816               id - GADGET_ID_MAGIC_BALL_CONTENT_0 + 1, sx, sy);
15817     else if (id == GADGET_ID_ANDROID_CONTENT)
15818       sprintf(infotext, "android element position: %d", sx + 1);
15819     else if (drawingarea_info[type_id].infotext != NULL)
15820       strcpy(infotext, drawingarea_info[type_id].infotext);
15821   }
15822
15823   infotext[max_infotext_len] = '\0';
15824
15825   if (strlen(infotext) > 0)
15826     DrawTextS(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY, INFOTEXT_FONT, infotext);
15827 }
15828
15829 void RequestExitLevelEditor(boolean ask_if_level_has_changed,
15830                             boolean quick_quit)
15831 {
15832   if (!ask_if_level_has_changed ||
15833       !LevelChanged() ||
15834       Request("Level has changed! Exit without saving?",
15835               REQ_ASK | REQ_STAY_OPEN))
15836   {
15837     struct RectWithBorder *vp_door_1 = &viewport.door_1[GAME_MODE_MAIN];
15838     struct RectWithBorder *vp_door_2 = &viewport.door_2[GAME_MODE_MAIN];
15839
15840     // draw normal door
15841     UndrawSpecialEditorDoor();
15842
15843     // use door animation if door 1 viewport is unchanged and contains toolbox
15844     if (useEditorDoorAnimation())
15845       CloseDoor(DOOR_CLOSE_1 | DOOR_FORCE_ANIM);
15846
15847     // close editor doors if viewport definition is the same as in main menu
15848     if (vp_door_1->x      == DX     &&
15849         vp_door_1->y      == DY     &&
15850         vp_door_1->width  == DXSIZE &&
15851         vp_door_1->height == DYSIZE &&
15852         vp_door_2->x      == VX     &&
15853         vp_door_2->y      == VY     &&
15854         vp_door_2->width  == VXSIZE &&
15855         vp_door_2->height == VYSIZE)
15856       CloseDoor(DOOR_CLOSE_ALL | DOOR_NO_DELAY);
15857     else
15858       SetDoorState(DOOR_CLOSE_ALL);
15859
15860     BackToFront();
15861
15862     if (quick_quit)
15863       FadeSkipNextFadeIn();
15864
15865     SetGameStatus(GAME_MODE_MAIN);
15866
15867     DrawMainMenu();
15868   }
15869   else
15870   {
15871     if (!global.use_envelope_request)
15872     {
15873       CloseDoor(DOOR_CLOSE_1);
15874       OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
15875     }
15876   }
15877 }