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