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