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