added support for magic wall settings in BD engine to level editor
[rocksndiamonds.git] / src / editor.c
1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // editor.c
10 // ============================================================================
11
12 #include <math.h>
13
14 #include "libgame/libgame.h"
15
16 #include "editor.h"
17 #include "screens.h"
18 #include "tools.h"
19 #include "files.h"
20 #include "game.h"
21 #include "init.h"
22 #include "tape.h"
23
24
25 #define INFOTEXT_UNKNOWN_ELEMENT        "unknown"
26
27
28 // ----------------------------------------------------------------------------
29 // screen and artwork graphic pixel position definitions
30 // ----------------------------------------------------------------------------
31
32 // values for the control window
33 #define ED_CTRL1_BUTTONS_HORIZ          4       // toolbox
34 #define ED_CTRL1_BUTTONS_VERT           4
35 #define ED_CTRL2_BUTTONS_HORIZ          3       // level
36 #define ED_CTRL2_BUTTONS_VERT           2
37 #define ED_CTRL3_BUTTONS_HORIZ          3       // CE and GE
38 #define ED_CTRL3_BUTTONS_VERT           1
39 #define ED_CTRL4_BUTTONS_HORIZ          2       // CE and GE
40 #define ED_CTRL4_BUTTONS_VERT           1
41 #define ED_CTRL5_BUTTONS_HORIZ          1       // properties
42 #define ED_CTRL5_BUTTONS_VERT           1
43 #define ED_CTRL6_BUTTONS_HORIZ          3       // properties
44 #define ED_CTRL6_BUTTONS_VERT           1
45 #define ED_CTRL7_BUTTONS_HORIZ          1       // palette
46 #define ED_CTRL7_BUTTONS_VERT           1
47
48 #define ED_NUM_CTRL1_BUTTONS   (ED_CTRL1_BUTTONS_HORIZ * ED_CTRL1_BUTTONS_VERT)
49 #define ED_NUM_CTRL2_BUTTONS   (ED_CTRL2_BUTTONS_HORIZ * ED_CTRL2_BUTTONS_VERT)
50 #define ED_NUM_CTRL3_BUTTONS   (ED_CTRL3_BUTTONS_HORIZ * ED_CTRL3_BUTTONS_VERT)
51 #define ED_NUM_CTRL4_BUTTONS   (ED_CTRL4_BUTTONS_HORIZ * ED_CTRL4_BUTTONS_VERT)
52 #define ED_NUM_CTRL5_BUTTONS   (ED_CTRL5_BUTTONS_HORIZ * ED_CTRL5_BUTTONS_VERT)
53 #define ED_NUM_CTRL6_BUTTONS   (ED_CTRL6_BUTTONS_HORIZ * ED_CTRL6_BUTTONS_VERT)
54 #define ED_NUM_CTRL7_BUTTONS   (ED_CTRL7_BUTTONS_HORIZ * ED_CTRL7_BUTTONS_VERT)
55 #define ED_NUM_CTRL1_2_BUTTONS (ED_NUM_CTRL1_BUTTONS   + ED_NUM_CTRL2_BUTTONS)
56 #define ED_NUM_CTRL1_3_BUTTONS (ED_NUM_CTRL1_2_BUTTONS + ED_NUM_CTRL3_BUTTONS)
57 #define ED_NUM_CTRL1_4_BUTTONS (ED_NUM_CTRL1_3_BUTTONS + ED_NUM_CTRL4_BUTTONS)
58 #define ED_NUM_CTRL1_5_BUTTONS (ED_NUM_CTRL1_4_BUTTONS + ED_NUM_CTRL5_BUTTONS)
59 #define ED_NUM_CTRL1_6_BUTTONS (ED_NUM_CTRL1_5_BUTTONS + ED_NUM_CTRL6_BUTTONS)
60 #define ED_NUM_CTRL1_7_BUTTONS (ED_NUM_CTRL1_6_BUTTONS + ED_NUM_CTRL7_BUTTONS)
61 #define ED_NUM_CTRL_BUTTONS    ED_NUM_CTRL1_7_BUTTONS
62
63 // values for the element list
64 #define ED_ELEMENTLIST_XPOS             (editor.palette.x)
65 #define ED_ELEMENTLIST_YPOS             (editor.palette.y)
66 #define ED_ELEMENTLIST_XSIZE            (graphic_info[IMG_EDITOR_PALETTE_BUTTON].width)
67 #define ED_ELEMENTLIST_YSIZE            (graphic_info[IMG_EDITOR_PALETTE_BUTTON].height)
68 #define ED_ELEMENTLIST_COLS             MAX(1, editor.palette.cols)
69 #define ED_ELEMENTLIST_ROWS             MAX(1, editor.palette.rows)
70 #define ED_ELEMENTLIST_BUTTONS_HORIZ    (ED_ELEMENTLIST_COLS)
71 #define ED_ELEMENTLIST_BUTTONS_VERT     (ED_ELEMENTLIST_ROWS)
72 #define ED_NUM_ELEMENTLIST_BUTTONS      (ED_ELEMENTLIST_BUTTONS_HORIZ * \
73                                          ED_ELEMENTLIST_BUTTONS_VERT)
74
75 // standard distances
76 #define ED_GADGET_NORMAL_DISTANCE       (editor.gadget.normal_spacing)
77 #define ED_GADGET_SMALL_DISTANCE        (editor.gadget.small_spacing)
78 #define ED_GADGET_TINY_DISTANCE         (editor.gadget.tiny_spacing)
79 #define ED_GADGET_LINE_DISTANCE         (editor.gadget.line_spacing)
80 #define ED_GADGET_TEXT_DISTANCE         (editor.gadget.text_spacing)
81 #define ED_TAB_BAR_HEIGHT               (editor.gadget.separator_line.height)
82 #define ED_DRAWINGAREA_TEXT_DISTANCE    (ED_GADGET_TEXT_DISTANCE +      \
83                                          ED_DRAWINGAREA_BORDER_SIZE)
84 #define ED_GADGET_SPACE_DISTANCE        (getFontWidth(FONT_TEXT_1))
85
86 // values for drawingarea gadgets
87 #define IMG_BORDER_1                    IMG_EDITOR_ELEMENT_BORDER
88 #define IMG_BORDER_2                    IMG_EDITOR_ELEMENT_BORDER_INPUT
89 #define ED_ELEMENT_BORDER               (graphic_info[IMG_BORDER_1].border_size)
90 #define ED_DRAWINGAREA_BORDER_SIZE      (graphic_info[IMG_BORDER_2].border_size)
91 #define ED_DRAWINGAREA_TILE_SIZE        (editor.drawingarea.tile_size)
92
93 // values for checkbutton gadgets
94 #define ED_CHECKBUTTON_XSIZE         (graphic_info[IMG_EDITOR_CHECKBOX].width)
95 #define ED_CHECKBUTTON_YSIZE         (graphic_info[IMG_EDITOR_CHECKBOX].height)
96
97 #define ED_TABBUTTON_XSIZE           (graphic_info[IMG_EDITOR_TABBUTTON].width)
98 #define ED_TABBUTTON_YSIZE           (graphic_info[IMG_EDITOR_TABBUTTON].height)
99
100 #define ED_SETTINGS_LEVEL_TABS_X        (editor.settings.tabs.x)
101 #define ED_SETTINGS_LEVEL_TABS_Y        (editor.settings.tabs.y)
102 #define ED_SETTINGS_ELEMENT_TABS_X      (editor.settings.tabs.x)
103 #define ED_SETTINGS_ELEMENT_TABS_Y      (editor.settings.tabs.y +       \
104                                          editor.settings.tabs.yoffset2)
105
106 #define ED_SETTINGS_TABS_XOFFSET        (editor.settings.tabs.draw_xoffset)
107 #define ED_SETTINGS_TABS_YOFFSET        (editor.settings.tabs.draw_yoffset)
108
109 #define ED_LEVEL_TABS_XSTART            (ED_SETTINGS_LEVEL_TABS_X)
110 #define ED_LEVEL_TABS_YSTART            (ED_SETTINGS_LEVEL_TABS_Y)
111 #define ED_LEVEL_SETTINGS_XSTART        (ED_SETTINGS_LEVEL_TABS_X +     \
112                                          ED_SETTINGS_TABS_XOFFSET)
113 #define ED_LEVEL_SETTINGS_YSTART        (ED_SETTINGS_LEVEL_TABS_Y +     \
114                                          ED_TABBUTTON_YSIZE +           \
115                                          ED_GADGET_TINY_DISTANCE +      \
116                                          ED_TAB_BAR_HEIGHT +            \
117                                          ED_SETTINGS_TABS_YOFFSET +     \
118                                          getFontHeight(FONT_TEXT_1) +   \
119                                          ED_GADGET_TEXT_DISTANCE)
120 #define ED_ELEMENT_TABS_XSTART          (ED_SETTINGS_ELEMENT_TABS_X)
121 #define ED_ELEMENT_TABS_YSTART          (ED_SETTINGS_ELEMENT_TABS_Y)
122 #define ED_ELEMENT_SETTINGS_XSTART      (ED_SETTINGS_ELEMENT_TABS_X +   \
123                                          ED_SETTINGS_TABS_XOFFSET)
124 #define ED_ELEMENT_SETTINGS_YSTART      (ED_SETTINGS_ELEMENT_TABS_Y +   \
125                                          ED_TABBUTTON_YSIZE +           \
126                                          ED_GADGET_TINY_DISTANCE +      \
127                                          ED_TAB_BAR_HEIGHT +            \
128                                          ED_SETTINGS_TABS_YOFFSET)
129
130 #define ED_SETTINGS_XOFFSET             (ED_CHECKBUTTON_XSIZE +         \
131                                          ED_GADGET_TEXT_DISTANCE)
132 #define ED_SETTINGS_YOFFSET             (ED_CHECKBUTTON_YSIZE +         \
133                                          ED_GADGET_LINE_DISTANCE)
134
135 #define ED_POS_RANGE                    (10000)
136 #define ED_POS_LEVEL_TABS_FIRST         (1 * ED_POS_RANGE)
137 #define ED_POS_LEVEL_TABS_LAST          (2 * ED_POS_RANGE - 1)
138 #define ED_POS_LEVEL_SETTINGS_FIRST     (2 * ED_POS_RANGE)
139 #define ED_POS_LEVEL_SETTINGS_LAST      (3 * ED_POS_RANGE - 1)
140 #define ED_POS_ELEMENT_TABS_FIRST       (3 * ED_POS_RANGE)
141 #define ED_POS_ELEMENT_TABS_LAST        (4 * ED_POS_RANGE - 1)
142 #define ED_POS_ELEMENT_SETTINGS_FIRST   (4 * ED_POS_RANGE)
143 #define ED_POS_ELEMENT_SETTINGS_LAST    (5 * ED_POS_RANGE - 1)
144
145 #define ED_LEVEL_TABS_XPOS(n)           (ED_POS_LEVEL_TABS_FIRST + (n))
146 #define ED_LEVEL_TABS_YPOS(n)           (ED_POS_LEVEL_TABS_FIRST + (n))
147
148 #define ED_LEVEL_SETTINGS_XPOS(n)       (ED_POS_LEVEL_SETTINGS_FIRST + (n))
149 #define ED_LEVEL_SETTINGS_YPOS(n)       (ED_POS_LEVEL_SETTINGS_FIRST + (n))
150
151 #define ED_ELEMENT_TABS_XPOS(n)         (ED_POS_ELEMENT_TABS_FIRST + (n))
152 #define ED_ELEMENT_TABS_YPOS(n)         (ED_POS_ELEMENT_TABS_FIRST + (n))
153
154 #define ED_ELEMENT_SETTINGS_XPOS(n)     (ED_POS_ELEMENT_SETTINGS_FIRST + (n))
155 #define ED_ELEMENT_SETTINGS_YPOS(n)     (ED_POS_ELEMENT_SETTINGS_FIRST + (n))
156
157 #define IS_POS_LEVEL_TABS(n)          ((n) >= ED_POS_LEVEL_TABS_FIRST && \
158                                        (n) <= ED_POS_LEVEL_TABS_LAST)
159 #define IS_POS_LEVEL_SETTINGS(n)      ((n) >= ED_POS_LEVEL_SETTINGS_FIRST && \
160                                        (n) <= ED_POS_LEVEL_SETTINGS_LAST)
161 #define IS_POS_ELEMENT_TABS(n)        ((n) >= ED_POS_ELEMENT_TABS_FIRST && \
162                                        (n) <= ED_POS_ELEMENT_TABS_LAST)
163 #define IS_POS_ELEMENT_SETTINGS(n)    ((n) >= ED_POS_ELEMENT_SETTINGS_FIRST && \
164                                        (n) <= ED_POS_ELEMENT_SETTINGS_LAST)
165
166 #define ED_LEVEL_TABS_LINE(n)           ((n) - ED_POS_LEVEL_TABS_FIRST)
167 #define ED_LEVEL_SETTINGS_LINE(n)       ((n) - ED_POS_LEVEL_SETTINGS_FIRST)
168 #define ED_ELEMENT_TABS_LINE(n)         ((n) - ED_POS_ELEMENT_TABS_FIRST)
169 #define ED_ELEMENT_SETTINGS_LINE(n)     ((n) - ED_POS_ELEMENT_SETTINGS_FIRST)
170
171 #define ED_LEVEL_TABS_X(n)              (ED_LEVEL_TABS_XSTART + \
172                                          (n) * ED_SETTINGS_TABS_XOFFSET)
173 #define ED_LEVEL_TABS_Y(n)              (ED_LEVEL_TABS_YSTART + \
174                                          (n) * ED_SETTINGS_TABS_YOFFSET)
175
176 #define ED_LEVEL_SETTINGS_X(n)          (ED_LEVEL_SETTINGS_XSTART +     \
177                                          (n) * ED_SETTINGS_XOFFSET)
178 #define ED_LEVEL_SETTINGS_Y(n)          (ED_LEVEL_SETTINGS_YSTART +     \
179                                          (n) * ED_SETTINGS_YOFFSET)
180
181 #define ED_ELEMENT_TABS_X(n)            (ED_ELEMENT_TABS_XSTART +       \
182                                          (n) * ED_SETTINGS_TABS_XOFFSET)
183 #define ED_ELEMENT_TABS_Y(n)            (ED_ELEMENT_TABS_YSTART +       \
184                                          (n) * ED_SETTINGS_TABS_YOFFSET)
185
186 #define ED_ELEMENT_SETTINGS_X(n)        (ED_ELEMENT_SETTINGS_XSTART +   \
187                                          (n) * ED_SETTINGS_XOFFSET)
188 #define ED_ELEMENT_SETTINGS_Y(n)        (ED_ELEMENT_SETTINGS_YSTART +   \
189                                          (n) * ED_SETTINGS_YOFFSET)
190
191 #define ED_POS_TO_LEVEL_TABS_X(n)       \
192   (ED_LEVEL_TABS_X(ED_LEVEL_TABS_LINE(n)))
193 #define ED_POS_TO_LEVEL_TABS_Y(n)       \
194   (ED_LEVEL_TABS_Y(ED_LEVEL_TABS_LINE(n)))
195
196 #define ED_POS_TO_LEVEL_SETTINGS_X(n)   \
197   (ED_LEVEL_SETTINGS_X(ED_LEVEL_SETTINGS_LINE(n)))
198 #define ED_POS_TO_LEVEL_SETTINGS_Y(n)   \
199   (ED_LEVEL_SETTINGS_Y(ED_LEVEL_SETTINGS_LINE(n)))
200
201 #define ED_POS_TO_ELEMENT_TABS_X(n)     \
202   (ED_ELEMENT_TABS_X(ED_ELEMENT_TABS_LINE(n)))
203 #define ED_POS_TO_ELEMENT_TABS_Y(n)     \
204   (ED_ELEMENT_TABS_Y(ED_ELEMENT_TABS_LINE(n)))
205
206 #define ED_POS_TO_ELEMENT_SETTINGS_X(n) \
207   (ED_ELEMENT_SETTINGS_X(ED_ELEMENT_SETTINGS_LINE(n)))
208 #define ED_POS_TO_ELEMENT_SETTINGS_Y(n) \
209   (ED_ELEMENT_SETTINGS_Y(ED_ELEMENT_SETTINGS_LINE(n)))
210
211 #define ED_SETTINGS_X(n)                (IS_POS_LEVEL_TABS(n) ? \
212                                          ED_POS_TO_LEVEL_TABS_X(n) : \
213                                          IS_POS_LEVEL_SETTINGS(n) ?     \
214                                          ED_POS_TO_LEVEL_SETTINGS_X(n) : \
215                                          IS_POS_ELEMENT_TABS(n) ?       \
216                                          ED_POS_TO_ELEMENT_TABS_X(n) : \
217                                          IS_POS_ELEMENT_SETTINGS(n) ?   \
218                                          ED_POS_TO_ELEMENT_SETTINGS_X(n) : (n))
219 #define ED_SETTINGS_Y(n)                (IS_POS_LEVEL_TABS(n) ? \
220                                          ED_POS_TO_LEVEL_TABS_Y(n) : \
221                                          IS_POS_LEVEL_SETTINGS(n) ?     \
222                                          ED_POS_TO_LEVEL_SETTINGS_Y(n) : \
223                                          IS_POS_ELEMENT_TABS(n) ?       \
224                                          ED_POS_TO_ELEMENT_TABS_Y(n) : \
225                                          IS_POS_ELEMENT_SETTINGS(n) ?   \
226                                          ED_POS_TO_ELEMENT_SETTINGS_Y(n) : (n))
227
228 #define ED_SETTINGS_XOFF(n)             (5 * ((n) % 4) *                \
229                                          ED_DRAWINGAREA_TILE_SIZE)
230 #define ED_SETTINGS_YOFF(n)             (5 * ((n) / 4) *                \
231                                          ED_DRAWINGAREA_TILE_SIZE)
232
233 #define ED_AREA_XOFFSET_1(n)            ((n) != 0 ?                     \
234                                          ED_DRAWINGAREA_BORDER_SIZE : 0)
235 #define ED_AREA_YOFFSET_1(n)            ((n) != 0 ?                     \
236                                          (ED_CHECKBUTTON_YSIZE -        \
237                                           ED_DRAWINGAREA_TILE_SIZE) / 2 : 0)
238
239 #define ED_AREA_XOFFSET_2(n)      (0)
240 #define ED_AREA_YOFFSET_2(n)      ((n) == 3 ?                   \
241                                    ((n) - 1) * ED_DRAWINGAREA_TILE_SIZE / 2 : 0)
242
243 #define ED_AREA_SETTINGS_X(i)      (ED_SETTINGS_X((i).x) +              \
244                                     ED_SETTINGS_XOFF((i).xoffset) +     \
245                                     ED_AREA_XOFFSET_1((i).x) -          \
246                                     ED_AREA_XOFFSET_2((i).area_xsize))
247 #define ED_AREA_SETTINGS_Y(i)      (ED_SETTINGS_Y((i).y) +              \
248                                     ED_SETTINGS_YOFF((i).yoffset) +     \
249                                     ED_AREA_YOFFSET_1((i).y) -          \
250                                     ED_AREA_YOFFSET_2((i).area_ysize))
251
252 // values for element content drawing areas
253 #define ED_AREA_1X1_LSETTINGS_XPOS(n)   ED_LEVEL_SETTINGS_XPOS(n)
254 #define ED_AREA_1X1_LSETTINGS_YPOS(n)   ED_LEVEL_SETTINGS_YPOS(n)
255 #define ED_AREA_1X1_LSETTINGS_XOFF      (0)
256 #define ED_AREA_1X1_LSETTINGS_YOFF      (0)
257
258 #define ED_AREA_1X1_SETTINGS_XPOS(n)    ED_ELEMENT_SETTINGS_XPOS(n)
259 #define ED_AREA_1X1_SETTINGS_YPOS(n)    ED_ELEMENT_SETTINGS_YPOS(n)
260 #define ED_AREA_1X1_SETTINGS_XOFF       (0)
261 #define ED_AREA_1X1_SETTINGS_YOFF       (0)
262
263 #define ED_AREA_3X3_SETTINGS_XPOS(n)    ED_ELEMENT_SETTINGS_XPOS(n)
264 #define ED_AREA_3X3_SETTINGS_YPOS(n)    ED_ELEMENT_SETTINGS_YPOS(n)
265 #define ED_AREA_3X3_SETTINGS_XOFF       (0)
266 #define ED_AREA_3X3_SETTINGS_YOFF       (0)
267
268 // element content
269 #define ED_AREA_ELEMENT_CONTENT_XOFF(n) (n)
270 #define ED_AREA_ELEMENT_CONTENT_YOFF(n) (n)
271
272 // yamyam content
273 #define ED_XPOS_YAM                     0
274 #define ED_YPOS_YAM                     5
275 #define ED_AREA_YAMYAM_CONTENT_XPOS     ED_ELEMENT_SETTINGS_XPOS(ED_XPOS_YAM)
276 #define ED_AREA_YAMYAM_CONTENT_YPOS     ED_ELEMENT_SETTINGS_YPOS(ED_YPOS_YAM)
277 #define ED_AREA_YAMYAM_CONTENT_XOFF(n)  ED_AREA_ELEMENT_CONTENT_XOFF(n)
278 #define ED_AREA_YAMYAM_CONTENT_YOFF(n)  ED_AREA_ELEMENT_CONTENT_YOFF(n)
279 #define ED_AREA_YAMYAM_CONTENT_X(n)     (ED_ELEMENT_SETTINGS_X(ED_XPOS_YAM) + \
280                                          ED_SETTINGS_XOFF(n))
281 #define ED_AREA_YAMYAM_CONTENT_Y(n)     (ED_ELEMENT_SETTINGS_Y(ED_YPOS_YAM) + \
282                                          ED_SETTINGS_YOFF(n) +          \
283                                          ED_AREA_YOFFSET_1(ED_YPOS_YAM) - \
284                                          ED_AREA_YOFFSET_2(3))
285
286 // magic ball content
287 #define ED_XPOS_BALL                    0
288 #define ED_YPOS_BALL                    6
289 #define ED_AREA_MAGIC_BALL_CONTENT_XPOS ED_ELEMENT_SETTINGS_XPOS(ED_XPOS_BALL)
290 #define ED_AREA_MAGIC_BALL_CONTENT_YPOS ED_ELEMENT_SETTINGS_YPOS(ED_YPOS_BALL)
291 #define ED_AREA_MAGIC_BALL_CONTENT_XOFF(n) ED_AREA_ELEMENT_CONTENT_XOFF(n)
292 #define ED_AREA_MAGIC_BALL_CONTENT_YOFF(n) ED_AREA_ELEMENT_CONTENT_YOFF(n)
293 #define ED_AREA_MAGIC_BALL_CONTENT_X(n) (ED_ELEMENT_SETTINGS_X(ED_XPOS_BALL) + \
294                                          ED_SETTINGS_XOFF(n))
295 #define ED_AREA_MAGIC_BALL_CONTENT_Y(n) (ED_ELEMENT_SETTINGS_Y(ED_YPOS_BALL) + \
296                                          ED_SETTINGS_YOFF(n) +          \
297                                          ED_AREA_YOFFSET_1(ED_YPOS_BALL) - \
298                                          ED_AREA_YOFFSET_2(3))
299
300 // values for scrolling gadgets for drawing area
301 #define ED_SCROLLBUTTON_XSIZE           (graphic_info[IMG_EDITOR_PLAYFIELD_SCROLLBAR].width)
302 #define ED_SCROLLBUTTON_YSIZE           (graphic_info[IMG_EDITOR_PLAYFIELD_SCROLLBAR].height)
303
304 #define ED_SCROLL_UP_XPOS               (SXSIZE - ED_SCROLLBUTTON_XSIZE)
305 #define ED_SCROLL_UP_YPOS               (0)
306 #define ED_SCROLL_DOWN_XPOS             ED_SCROLL_UP_XPOS
307 #define ED_SCROLL_DOWN_YPOS             (SYSIZE - 3 * ED_SCROLLBUTTON_YSIZE)
308 #define ED_SCROLL_LEFT_XPOS             (0)
309 #define ED_SCROLL_LEFT_YPOS             (SYSIZE - 2 * ED_SCROLLBUTTON_YSIZE)
310 #define ED_SCROLL_RIGHT_XPOS            (SXSIZE - 2 * ED_SCROLLBUTTON_XSIZE)
311 #define ED_SCROLL_RIGHT_YPOS            ED_SCROLL_LEFT_YPOS
312 #define ED_SCROLL_HORIZONTAL_XPOS       (ED_SCROLL_LEFT_XPOS +          \
313                                          ED_SCROLLBUTTON_XSIZE)
314 #define ED_SCROLL_HORIZONTAL_YPOS       ED_SCROLL_LEFT_YPOS
315 #define ED_SCROLL_HORIZONTAL_XSIZE      (SXSIZE - 3 * ED_SCROLLBUTTON_XSIZE)
316 #define ED_SCROLL_HORIZONTAL_YSIZE      ED_SCROLLBUTTON_YSIZE
317 #define ED_SCROLL_VERTICAL_XPOS         ED_SCROLL_UP_XPOS
318 #define ED_SCROLL_VERTICAL_YPOS         (ED_SCROLL_UP_YPOS +            \
319                                          ED_SCROLLBUTTON_YSIZE)
320 #define ED_SCROLL_VERTICAL_XSIZE        ED_SCROLLBUTTON_XSIZE
321 #define ED_SCROLL_VERTICAL_YSIZE        (SYSIZE - 4 * ED_SCROLLBUTTON_YSIZE)
322
323 // values for scrolling gadgets for element list
324 #define ED_SCROLLBUTTON2_XSIZE          (graphic_info[IMG_EDITOR_PALETTE_SCROLL_UP].width)
325 #define ED_SCROLLBUTTON2_YSIZE          (graphic_info[IMG_EDITOR_PALETTE_SCROLL_UP].height)
326
327 #define ED_SCROLL2_UP_XPOS              (ED_ELEMENTLIST_XPOS +          \
328                                          ED_ELEMENTLIST_BUTTONS_HORIZ * \
329                                          ED_ELEMENTLIST_XSIZE)
330 #define ED_SCROLL2_UP_YPOS              ED_ELEMENTLIST_YPOS
331 #define ED_SCROLL2_DOWN_XPOS            ED_SCROLL2_UP_XPOS
332 #define ED_SCROLL2_DOWN_YPOS            (ED_SCROLL2_UP_YPOS +           \
333                                          ED_ELEMENTLIST_BUTTONS_VERT *  \
334                                          ED_ELEMENTLIST_YSIZE -         \
335                                          ED_SCROLLBUTTON2_YSIZE)
336 #define ED_SCROLL2_VERTICAL_XPOS        ED_SCROLL2_UP_XPOS
337 #define ED_SCROLL2_VERTICAL_YPOS        (ED_SCROLL2_UP_YPOS +           \
338                                          ED_SCROLLBUTTON2_YSIZE)
339 #define ED_SCROLL2_VERTICAL_XSIZE       ED_SCROLLBUTTON2_XSIZE
340 #define ED_SCROLL2_VERTICAL_YSIZE       (ED_ELEMENTLIST_BUTTONS_VERT *  \
341                                          ED_ELEMENTLIST_YSIZE -         \
342                                          2 * ED_SCROLLBUTTON2_YSIZE)
343
344 // values for ClearEditorGadgetInfoText() and HandleEditorGadgetInfoText()
345 #define INFOTEXT_FONT           FONT_TEXT_2
346 #define INFOTEXT_XSIZE          SXSIZE
347 #define INFOTEXT_YSIZE          getFontHeight(INFOTEXT_FONT)
348 #define INFOTEXT_YSIZE_FULL     (INFOTEXT_YSIZE + ED_GADGET_SMALL_DISTANCE)
349 #define INFOTEXT_X              (editor.settings.tooltip.x)
350 #define INFOTEXT_Y              (editor.settings.tooltip.y)
351 #define INFOTEXT_XY_REDEFINED   (INFOTEXT_X != -1 || INFOTEXT_Y != -1)
352 #define INFOTEXT_XPOS           SX + (INFOTEXT_XY_REDEFINED ? INFOTEXT_X : 0)
353 #define INFOTEXT_YPOS           SY + (INFOTEXT_XY_REDEFINED ? INFOTEXT_Y : \
354                                       SYSIZE - INFOTEXT_YSIZE)
355
356
357 // ----------------------------------------------------------------------------
358 // editor gadget definitions
359 // ----------------------------------------------------------------------------
360
361 enum
362 {
363   GADGET_ID_NONE = -1,
364
365   // drawing toolbox buttons
366
367   GADGET_ID_SINGLE_ITEMS,
368   GADGET_ID_CONNECTED_ITEMS,
369   GADGET_ID_LINE,
370   GADGET_ID_ARC,
371   GADGET_ID_RECTANGLE,
372   GADGET_ID_FILLED_BOX,
373   GADGET_ID_WRAP_UP,
374   GADGET_ID_TEXT,
375   GADGET_ID_FLOOD_FILL,
376   GADGET_ID_WRAP_LEFT,
377   GADGET_ID_ZOOM,
378   GADGET_ID_WRAP_RIGHT,
379   GADGET_ID_RANDOM_PLACEMENT,
380   GADGET_ID_GRAB_BRUSH,
381   GADGET_ID_WRAP_DOWN,
382   GADGET_ID_PICK_ELEMENT,
383
384   GADGET_ID_UNDO,
385   GADGET_ID_CONF,
386   GADGET_ID_SAVE,
387   GADGET_ID_CLEAR,
388   GADGET_ID_TEST,
389   GADGET_ID_EXIT,
390
391   GADGET_ID_CUSTOM_COPY_FROM,
392   GADGET_ID_CUSTOM_COPY_TO,
393   GADGET_ID_CUSTOM_EXCHANGE,
394   GADGET_ID_CUSTOM_COPY,
395   GADGET_ID_CUSTOM_PASTE,
396
397   GADGET_ID_PROPERTIES,
398   GADGET_ID_ELEMENT_LEFT,
399   GADGET_ID_ELEMENT_MIDDLE,
400   GADGET_ID_ELEMENT_RIGHT,
401   GADGET_ID_PALETTE,
402
403   // counter gadget identifiers
404
405   GADGET_ID_SELECT_LEVEL_DOWN,
406   GADGET_ID_SELECT_LEVEL_TEXT,
407   GADGET_ID_SELECT_LEVEL_UP,
408   GADGET_ID_LEVEL_XSIZE_DOWN,
409   GADGET_ID_LEVEL_XSIZE_TEXT,
410   GADGET_ID_LEVEL_XSIZE_UP,
411   GADGET_ID_LEVEL_YSIZE_DOWN,
412   GADGET_ID_LEVEL_YSIZE_TEXT,
413   GADGET_ID_LEVEL_YSIZE_UP,
414   GADGET_ID_LEVEL_RANDOM_DOWN,
415   GADGET_ID_LEVEL_RANDOM_TEXT,
416   GADGET_ID_LEVEL_RANDOM_UP,
417   GADGET_ID_LEVEL_GEMSLIMIT_DOWN,
418   GADGET_ID_LEVEL_GEMSLIMIT_TEXT,
419   GADGET_ID_LEVEL_GEMSLIMIT_UP,
420   GADGET_ID_LEVEL_TIMELIMIT_DOWN,
421   GADGET_ID_LEVEL_TIMELIMIT_TEXT,
422   GADGET_ID_LEVEL_TIMELIMIT_UP,
423   GADGET_ID_LEVEL_TIMESCORE_DOWN,
424   GADGET_ID_LEVEL_TIMESCORE_TEXT,
425   GADGET_ID_LEVEL_TIMESCORE_UP,
426   GADGET_ID_LEVEL_RANDOM_SEED_DOWN,
427   GADGET_ID_LEVEL_RANDOM_SEED_TEXT,
428   GADGET_ID_LEVEL_RANDOM_SEED_UP,
429   GADGET_ID_LEVELSET_NUM_LEVELS_DOWN,
430   GADGET_ID_LEVELSET_NUM_LEVELS_TEXT,
431   GADGET_ID_LEVELSET_NUM_LEVELS_UP,
432   GADGET_ID_BD_CYCLE_DELAY_MS_DOWN,
433   GADGET_ID_BD_CYCLE_DELAY_MS_TEXT,
434   GADGET_ID_BD_CYCLE_DELAY_MS_UP,
435   GADGET_ID_BD_CYCLE_DELAY_C64_DOWN,
436   GADGET_ID_BD_CYCLE_DELAY_C64_TEXT,
437   GADGET_ID_BD_CYCLE_DELAY_C64_UP,
438   GADGET_ID_BD_HATCHING_DELAY_CYCLES_DOWN,
439   GADGET_ID_BD_HATCHING_DELAY_CYCLES_TEXT,
440   GADGET_ID_BD_HATCHING_DELAY_CYCLES_UP,
441   GADGET_ID_BD_HATCHING_DELAY_SECONDS_DOWN,
442   GADGET_ID_BD_HATCHING_DELAY_SECONDS_TEXT,
443   GADGET_ID_BD_HATCHING_DELAY_SECONDS_UP,
444   GADGET_ID_BD_PUSHING_PROB_DOWN,
445   GADGET_ID_BD_PUSHING_PROB_TEXT,
446   GADGET_ID_BD_PUSHING_PROB_UP,
447   GADGET_ID_BD_PUSHING_PROB_WITH_SWEET_DOWN,
448   GADGET_ID_BD_PUSHING_PROB_WITH_SWEET_TEXT,
449   GADGET_ID_BD_PUSHING_PROB_WITH_SWEET_UP,
450   GADGET_ID_ELEMENT_VALUE1_DOWN,
451   GADGET_ID_ELEMENT_VALUE1_TEXT,
452   GADGET_ID_ELEMENT_VALUE1_UP,
453   GADGET_ID_ELEMENT_VALUE2_DOWN,
454   GADGET_ID_ELEMENT_VALUE2_TEXT,
455   GADGET_ID_ELEMENT_VALUE2_UP,
456   GADGET_ID_ELEMENT_VALUE3_DOWN,
457   GADGET_ID_ELEMENT_VALUE3_TEXT,
458   GADGET_ID_ELEMENT_VALUE3_UP,
459   GADGET_ID_ELEMENT_VALUE4_DOWN,
460   GADGET_ID_ELEMENT_VALUE4_TEXT,
461   GADGET_ID_ELEMENT_VALUE4_UP,
462   GADGET_ID_YAMYAM_CONTENT_DOWN,
463   GADGET_ID_YAMYAM_CONTENT_TEXT,
464   GADGET_ID_YAMYAM_CONTENT_UP,
465   GADGET_ID_BALL_CONTENT_DOWN,
466   GADGET_ID_BALL_CONTENT_TEXT,
467   GADGET_ID_BALL_CONTENT_UP,
468   GADGET_ID_ANDROID_CONTENT_DOWN,
469   GADGET_ID_ANDROID_CONTENT_TEXT,
470   GADGET_ID_ANDROID_CONTENT_UP,
471   GADGET_ID_ENVELOPE_XSIZE_DOWN,
472   GADGET_ID_ENVELOPE_XSIZE_TEXT,
473   GADGET_ID_ENVELOPE_XSIZE_UP,
474   GADGET_ID_ENVELOPE_YSIZE_DOWN,
475   GADGET_ID_ENVELOPE_YSIZE_TEXT,
476   GADGET_ID_ENVELOPE_YSIZE_UP,
477   GADGET_ID_INVENTORY_SIZE_DOWN,
478   GADGET_ID_INVENTORY_SIZE_TEXT,
479   GADGET_ID_INVENTORY_SIZE_UP,
480   GADGET_ID_MM_BALL_CONTENT_DOWN,
481   GADGET_ID_MM_BALL_CONTENT_TEXT,
482   GADGET_ID_MM_BALL_CONTENT_UP,
483   GADGET_ID_CUSTOM_SCORE_DOWN,
484   GADGET_ID_CUSTOM_SCORE_TEXT,
485   GADGET_ID_CUSTOM_SCORE_UP,
486   GADGET_ID_CUSTOM_GEMCOUNT_DOWN,
487   GADGET_ID_CUSTOM_GEMCOUNT_TEXT,
488   GADGET_ID_CUSTOM_GEMCOUNT_UP,
489   GADGET_ID_CUSTOM_VALUE_FIX_DOWN,
490   GADGET_ID_CUSTOM_VALUE_FIX_TEXT,
491   GADGET_ID_CUSTOM_VALUE_FIX_UP,
492   GADGET_ID_CUSTOM_VALUE_RND_DOWN,
493   GADGET_ID_CUSTOM_VALUE_RND_TEXT,
494   GADGET_ID_CUSTOM_VALUE_RND_UP,
495   GADGET_ID_PUSH_DELAY_FIX_DOWN,
496   GADGET_ID_PUSH_DELAY_FIX_TEXT,
497   GADGET_ID_PUSH_DELAY_FIX_UP,
498   GADGET_ID_PUSH_DELAY_RND_DOWN,
499   GADGET_ID_PUSH_DELAY_RND_TEXT,
500   GADGET_ID_PUSH_DELAY_RND_UP,
501   GADGET_ID_DROP_DELAY_FIX_DOWN,
502   GADGET_ID_DROP_DELAY_FIX_TEXT,
503   GADGET_ID_DROP_DELAY_FIX_UP,
504   GADGET_ID_DROP_DELAY_RND_DOWN,
505   GADGET_ID_DROP_DELAY_RND_TEXT,
506   GADGET_ID_DROP_DELAY_RND_UP,
507   GADGET_ID_MOVE_DELAY_FIX_DOWN,
508   GADGET_ID_MOVE_DELAY_FIX_TEXT,
509   GADGET_ID_MOVE_DELAY_FIX_UP,
510   GADGET_ID_MOVE_DELAY_RND_DOWN,
511   GADGET_ID_MOVE_DELAY_RND_TEXT,
512   GADGET_ID_MOVE_DELAY_RND_UP,
513   GADGET_ID_STEP_DELAY_FIX_DOWN,
514   GADGET_ID_STEP_DELAY_FIX_TEXT,
515   GADGET_ID_STEP_DELAY_FIX_UP,
516   GADGET_ID_STEP_DELAY_RND_DOWN,
517   GADGET_ID_STEP_DELAY_RND_TEXT,
518   GADGET_ID_STEP_DELAY_RND_UP,
519   GADGET_ID_EXPLOSION_DELAY_DOWN,
520   GADGET_ID_EXPLOSION_DELAY_TEXT,
521   GADGET_ID_EXPLOSION_DELAY_UP,
522   GADGET_ID_IGNITION_DELAY_DOWN,
523   GADGET_ID_IGNITION_DELAY_TEXT,
524   GADGET_ID_IGNITION_DELAY_UP,
525   GADGET_ID_CHANGE_DELAY_FIX_DOWN,
526   GADGET_ID_CHANGE_DELAY_FIX_TEXT,
527   GADGET_ID_CHANGE_DELAY_FIX_UP,
528   GADGET_ID_CHANGE_DELAY_RND_DOWN,
529   GADGET_ID_CHANGE_DELAY_RND_TEXT,
530   GADGET_ID_CHANGE_DELAY_RND_UP,
531   GADGET_ID_CHANGE_CONT_RND_DOWN,
532   GADGET_ID_CHANGE_CONT_RND_TEXT,
533   GADGET_ID_CHANGE_CONT_RND_UP,
534   GADGET_ID_GROUP_CONTENT_DOWN,
535   GADGET_ID_GROUP_CONTENT_TEXT,
536   GADGET_ID_GROUP_CONTENT_UP,
537
538   // drawing area identifiers
539
540   GADGET_ID_DRAWING_LEVEL,
541   GADGET_ID_YAMYAM_CONTENT_0,
542   GADGET_ID_YAMYAM_CONTENT_1,
543   GADGET_ID_YAMYAM_CONTENT_2,
544   GADGET_ID_YAMYAM_CONTENT_3,
545   GADGET_ID_YAMYAM_CONTENT_4,
546   GADGET_ID_YAMYAM_CONTENT_5,
547   GADGET_ID_YAMYAM_CONTENT_6,
548   GADGET_ID_YAMYAM_CONTENT_7,
549   GADGET_ID_MAGIC_BALL_CONTENT_0,
550   GADGET_ID_MAGIC_BALL_CONTENT_1,
551   GADGET_ID_MAGIC_BALL_CONTENT_2,
552   GADGET_ID_MAGIC_BALL_CONTENT_3,
553   GADGET_ID_MAGIC_BALL_CONTENT_4,
554   GADGET_ID_MAGIC_BALL_CONTENT_5,
555   GADGET_ID_MAGIC_BALL_CONTENT_6,
556   GADGET_ID_MAGIC_BALL_CONTENT_7,
557   GADGET_ID_ANDROID_CONTENT,
558   GADGET_ID_AMOEBA_CONTENT,
559   GADGET_ID_START_ELEMENT,
560   GADGET_ID_ARTWORK_ELEMENT,
561   GADGET_ID_EXPLOSION_ELEMENT,
562   GADGET_ID_INVENTORY_CONTENT,
563   GADGET_ID_MM_BALL_CONTENT,
564   GADGET_ID_CUSTOM_GRAPHIC,
565   GADGET_ID_CUSTOM_CONTENT,
566   GADGET_ID_CUSTOM_MOVE_ENTER,
567   GADGET_ID_CUSTOM_MOVE_LEAVE,
568   GADGET_ID_CUSTOM_CHANGE_TARGET,
569   GADGET_ID_CUSTOM_CHANGE_CONTENT,
570   GADGET_ID_CUSTOM_CHANGE_TRIGGER,
571   GADGET_ID_CUSTOM_CHANGE_ACTION,
572   GADGET_ID_GROUP_CONTENT,
573   GADGET_ID_RANDOM_BACKGROUND,
574
575   // text input identifiers
576
577   GADGET_ID_LEVEL_NAME,
578   GADGET_ID_LEVEL_AUTHOR,
579   GADGET_ID_LEVELSET_NAME,
580   GADGET_ID_LEVELSET_AUTHOR,
581   GADGET_ID_ELEMENT_NAME,
582
583   // text area identifiers
584
585   GADGET_ID_ENVELOPE_INFO,
586
587   // selectbox identifiers
588
589   GADGET_ID_TIME_OR_STEPS,
590   GADGET_ID_TIME_SCORE_BASE,
591   GADGET_ID_GAME_ENGINE_TYPE,
592   GADGET_ID_BD_SCHEDULING_TYPE,
593   GADGET_ID_LEVELSET_SAVE_MODE,
594   GADGET_ID_WIND_DIRECTION,
595   GADGET_ID_PLAYER_SPEED,
596   GADGET_ID_MM_BALL_CHOICE_MODE,
597   GADGET_ID_CUSTOM_WALK_TO_ACTION,
598   GADGET_ID_CUSTOM_EXPLOSION_TYPE,
599   GADGET_ID_CUSTOM_DEADLINESS,
600   GADGET_ID_CUSTOM_MOVE_PATTERN,
601   GADGET_ID_CUSTOM_MOVE_DIRECTION,
602   GADGET_ID_CUSTOM_MOVE_STEPSIZE,
603   GADGET_ID_CUSTOM_MOVE_LEAVE_TYPE,
604   GADGET_ID_CUSTOM_SMASH_TARGETS,
605   GADGET_ID_CUSTOM_SLIPPERY_TYPE,
606   GADGET_ID_CUSTOM_ACCESS_TYPE,
607   GADGET_ID_CUSTOM_ACCESS_LAYER,
608   GADGET_ID_CUSTOM_ACCESS_PROTECTED,
609   GADGET_ID_CUSTOM_ACCESS_DIRECTION,
610   GADGET_ID_CHANGE_TIME_UNITS,
611   GADGET_ID_CHANGE_DIRECT_ACTION,
612   GADGET_ID_CHANGE_OTHER_ACTION,
613   GADGET_ID_CHANGE_SIDE,
614   GADGET_ID_CHANGE_PLAYER,
615   GADGET_ID_CHANGE_PAGE,
616   GADGET_ID_CHANGE_REPLACE_WHEN,
617   GADGET_ID_ACTION_TYPE,
618   GADGET_ID_ACTION_MODE,
619   GADGET_ID_ACTION_ARG,
620   GADGET_ID_SELECT_CHANGE_PAGE,
621   GADGET_ID_GROUP_CHOICE_MODE,
622
623   // textbutton identifiers
624
625   GADGET_ID_LEVELCONFIG_LEVEL,
626   GADGET_ID_LEVELCONFIG_LEVELSET,
627   GADGET_ID_LEVELCONFIG_EDITOR,
628   GADGET_ID_LEVELCONFIG_ENGINE,
629   GADGET_ID_PROPERTIES_INFO,
630   GADGET_ID_PROPERTIES_CONFIG,
631   GADGET_ID_PROPERTIES_CONFIG_1,
632   GADGET_ID_PROPERTIES_CONFIG_2,
633   GADGET_ID_PROPERTIES_CHANGE,
634   GADGET_ID_SAVE_AS_TEMPLATE_1,
635   GADGET_ID_SAVE_AS_TEMPLATE_2,
636   GADGET_ID_SAVE_LEVELSET,
637   GADGET_ID_ADD_CHANGE_PAGE,
638   GADGET_ID_DEL_CHANGE_PAGE,
639
640   // graphicbutton identifiers
641
642   GADGET_ID_PREV_CHANGE_PAGE,
643   GADGET_ID_NEXT_CHANGE_PAGE,
644   GADGET_ID_COPY_CHANGE_PAGE,
645   GADGET_ID_PASTE_CHANGE_PAGE,
646
647   // gadgets for scrolling of drawing area
648
649   GADGET_ID_SCROLL_UP,
650   GADGET_ID_SCROLL_DOWN,
651   GADGET_ID_SCROLL_LEFT,
652   GADGET_ID_SCROLL_RIGHT,
653   GADGET_ID_SCROLL_HORIZONTAL,
654   GADGET_ID_SCROLL_VERTICAL,
655
656   // gadgets for scrolling element list
657
658   GADGET_ID_SCROLL_LIST_UP,
659   GADGET_ID_SCROLL_LIST_DOWN,
660   GADGET_ID_SCROLL_LIST_VERTICAL,
661
662   // checkbuttons/radiobuttons for level/element properties
663
664   GADGET_ID_AUTO_COUNT_GEMS,
665   GADGET_ID_RATE_TIME_OVER_SCORE,
666   GADGET_ID_USE_LEVELSET_ARTWORK,
667   GADGET_ID_COPY_LEVEL_TEMPLATE,
668   GADGET_ID_RANDOM_PERCENTAGE,
669   GADGET_ID_RANDOM_QUANTITY,
670   GADGET_ID_RANDOM_RESTRICTED,
671   GADGET_ID_BD_INTERMISSION,
672   GADGET_ID_BD_PAL_TIMING,
673   GADGET_ID_BD_LINE_SHIFTING_BORDERS,
674   GADGET_ID_BD_SCAN_FIRST_AND_LAST_ROW,
675   GADGET_ID_BD_SHORT_EXPLOSIONS,
676   GADGET_ID_BD_GRAVITY_AFFECTS_ALL,
677   GADGET_ID_STICK_ELEMENT,
678   GADGET_ID_EM_SLIPPERY_GEMS,
679   GADGET_ID_EM_EXPLODES_BY_FIRE,
680   GADGET_ID_USE_SPRING_BUG,
681   GADGET_ID_USE_TIME_ORB_BUG,
682   GADGET_ID_USE_LIFE_BUGS,
683   GADGET_ID_RANDOM_BALL_CONTENT,
684   GADGET_ID_INITIAL_BALL_ACTIVE,
685   GADGET_ID_GROW_INTO_DIGGABLE,
686   GADGET_ID_SB_FIELDS_NEEDED,
687   GADGET_ID_SB_OBJECTS_NEEDED,
688   GADGET_ID_AUTO_EXIT_SOKOBAN,
689   GADGET_ID_SOLVED_BY_ONE_PLAYER,
690   GADGET_ID_FINISH_DIG_COLLECT,
691   GADGET_ID_KEEP_WALKABLE_CE,
692   GADGET_ID_CONTINUOUS_SNAPPING,
693   GADGET_ID_BLOCK_SNAP_FIELD,
694   GADGET_ID_BLOCK_LAST_FIELD,
695   GADGET_ID_SP_BLOCK_LAST_FIELD,
696   GADGET_ID_INSTANT_RELOCATION,
697   GADGET_ID_SHIFTED_RELOCATION,
698   GADGET_ID_LAZY_RELOCATION,
699   GADGET_ID_USE_START_ELEMENT,
700   GADGET_ID_USE_ARTWORK_ELEMENT,
701   GADGET_ID_USE_EXPLOSION_ELEMENT,
702   GADGET_ID_INITIAL_GRAVITY,
703   GADGET_ID_USE_INITIAL_INVENTORY,
704   GADGET_ID_CAN_PASS_TO_WALKABLE,
705   GADGET_ID_CAN_FALL_INTO_ACID,
706   GADGET_ID_CAN_MOVE_INTO_ACID,
707   GADGET_ID_DONT_COLLIDE_WITH,
708   GADGET_ID_BD_DIAGONAL_MOVEMENTS,
709   GADGET_ID_BD_TOPMOST_PLAYER_ACTIVE,
710   GADGET_ID_BD_PUSH_MEGA_ROCK_WITH_SWEET,
711   GADGET_ID_BD_MAGIC_WALL_WAIT_HATCHING,
712   GADGET_ID_BD_MAGIC_WALL_STOPS_AMOEBA,
713   GADGET_ID_ENVELOPE_AUTOWRAP,
714   GADGET_ID_ENVELOPE_CENTERED,
715   GADGET_ID_MM_LASER_RED,
716   GADGET_ID_MM_LASER_GREEN,
717   GADGET_ID_MM_LASER_BLUE,
718   GADGET_ID_DF_LASER_RED,
719   GADGET_ID_DF_LASER_GREEN,
720   GADGET_ID_DF_LASER_BLUE,
721   GADGET_ID_ROTATE_MM_BALL_CONTENT,
722   GADGET_ID_EXPLODE_MM_BALL,
723   GADGET_ID_CUSTOM_INDESTRUCTIBLE,
724   GADGET_ID_CUSTOM_CAN_EXPLODE,
725   GADGET_ID_CUSTOM_EXPLODE_FIRE,
726   GADGET_ID_CUSTOM_EXPLODE_SMASH,
727   GADGET_ID_CUSTOM_EXPLODE_IMPACT,
728   GADGET_ID_CUSTOM_WALK_TO_OBJECT,
729   GADGET_ID_CUSTOM_DEADLY,
730   GADGET_ID_CUSTOM_CAN_MOVE,
731   GADGET_ID_CUSTOM_CAN_FALL,
732   GADGET_ID_CUSTOM_CAN_SMASH,
733   GADGET_ID_CUSTOM_SLIPPERY,
734   GADGET_ID_CUSTOM_ACCESSIBLE,
735   GADGET_ID_CUSTOM_GRAV_REACHABLE,
736   GADGET_ID_CUSTOM_USE_LAST_VALUE,
737   GADGET_ID_CUSTOM_USE_GRAPHIC,
738   GADGET_ID_CUSTOM_USE_TEMPLATE_1,
739   GADGET_ID_CUSTOM_USE_TEMPLATE_2,
740   GADGET_ID_CUSTOM_USE_TEMPLATE_3,
741   GADGET_ID_CUSTOM_CAN_CHANGE,
742   GADGET_ID_CHANGE_USE_CONTENT,
743   GADGET_ID_CHANGE_USE_EXPLOSION,
744   GADGET_ID_CHANGE_ONLY_COMPLETE,
745   GADGET_ID_CHANGE_USE_RANDOM,
746   GADGET_ID_CHANGE_HAS_ACTION,
747   GADGET_ID_CHANGE_DELAY,
748   GADGET_ID_CHANGE_BY_DIRECT_ACT,
749   GADGET_ID_CHANGE_BY_OTHER_ACT,
750
751   NUM_STATIC_GADGET_IDS
752 };
753
754 // gadgets for buttons in element list (dynamic)
755 #define GADGET_ID_ELEMENTLIST_FIRST     (NUM_STATIC_GADGET_IDS)
756 #define GADGET_ID_ELEMENTLIST_LAST      (GADGET_ID_ELEMENTLIST_FIRST +  \
757                                         ED_NUM_ELEMENTLIST_BUTTONS - 1)
758
759 #define NUM_EDITOR_GADGETS              (GADGET_ID_ELEMENTLIST_LAST + 1)
760
761 // radio button numbers
762 enum
763 {
764   RADIO_NR_NONE,
765   RADIO_NR_DRAWING_TOOLBOX,
766   RADIO_NR_RANDOM_ELEMENTS
767 };
768
769 // values for counter gadgets
770 enum
771 {
772   ED_COUNTER_ID_SELECT_LEVEL,
773   ED_COUNTER_ID_LEVEL_XSIZE,
774   ED_COUNTER_ID_LEVEL_YSIZE,
775   ED_COUNTER_ID_LEVEL_GEMSLIMIT,
776   ED_COUNTER_ID_LEVEL_TIMELIMIT,
777   ED_COUNTER_ID_LEVEL_TIMESCORE,
778   ED_COUNTER_ID_LEVEL_RANDOM_SEED,
779   ED_COUNTER_ID_LEVELSET_NUM_LEVELS,
780   ED_COUNTER_ID_LEVEL_RANDOM,
781   ED_COUNTER_ID_BD_CYCLE_DELAY_MS,
782   ED_COUNTER_ID_BD_CYCLE_DELAY_C64,
783   ED_COUNTER_ID_BD_HATCHING_DELAY_CYCLES,
784   ED_COUNTER_ID_BD_HATCHING_DELAY_SECONDS,
785   ED_COUNTER_ID_BD_PUSHING_PROB,
786   ED_COUNTER_ID_BD_PUSHING_PROB_WITH_SWEET,
787   ED_COUNTER_ID_ELEMENT_VALUE1,
788   ED_COUNTER_ID_ELEMENT_VALUE2,
789   ED_COUNTER_ID_ELEMENT_VALUE3,
790   ED_COUNTER_ID_ELEMENT_VALUE4,
791   ED_COUNTER_ID_YAMYAM_CONTENT,
792   ED_COUNTER_ID_BALL_CONTENT,
793   ED_COUNTER_ID_ANDROID_CONTENT,
794   ED_COUNTER_ID_ENVELOPE_XSIZE,
795   ED_COUNTER_ID_ENVELOPE_YSIZE,
796   ED_COUNTER_ID_INVENTORY_SIZE,
797   ED_COUNTER_ID_MM_BALL_CONTENT,
798   ED_COUNTER_ID_CUSTOM_SCORE,
799   ED_COUNTER_ID_CUSTOM_GEMCOUNT,
800   ED_COUNTER_ID_CUSTOM_VALUE_FIX,
801   ED_COUNTER_ID_CUSTOM_VALUE_RND,
802   ED_COUNTER_ID_PUSH_DELAY_FIX,
803   ED_COUNTER_ID_PUSH_DELAY_RND,
804   ED_COUNTER_ID_DROP_DELAY_FIX,
805   ED_COUNTER_ID_DROP_DELAY_RND,
806   ED_COUNTER_ID_MOVE_DELAY_FIX,
807   ED_COUNTER_ID_MOVE_DELAY_RND,
808   ED_COUNTER_ID_STEP_DELAY_FIX,
809   ED_COUNTER_ID_STEP_DELAY_RND,
810   ED_COUNTER_ID_EXPLOSION_DELAY,
811   ED_COUNTER_ID_IGNITION_DELAY,
812   ED_COUNTER_ID_GROUP_CONTENT,
813   ED_COUNTER_ID_CHANGE_DELAY_FIX,
814   ED_COUNTER_ID_CHANGE_DELAY_RND,
815   ED_COUNTER_ID_CHANGE_CONT_RND,
816
817   ED_NUM_COUNTERBUTTONS
818 };
819
820 #define ED_COUNTER_ID_LEVEL_FIRST       ED_COUNTER_ID_LEVEL_XSIZE
821 #define ED_COUNTER_ID_LEVEL_LAST        ED_COUNTER_ID_LEVEL_RANDOM_SEED
822 #define ED_COUNTER_ID_LEVELSET_FIRST    ED_COUNTER_ID_LEVELSET_NUM_LEVELS
823 #define ED_COUNTER_ID_LEVELSET_LAST     ED_COUNTER_ID_LEVELSET_NUM_LEVELS
824 #define ED_COUNTER_ID_EDITOR_FIRST      ED_COUNTER_ID_LEVEL_RANDOM
825 #define ED_COUNTER_ID_EDITOR_LAST       ED_COUNTER_ID_LEVEL_RANDOM
826
827 #define ED_COUNTER_ID_CUSTOM1_FIRST     ED_COUNTER_ID_CUSTOM_SCORE
828 #define ED_COUNTER_ID_CUSTOM1_LAST      ED_COUNTER_ID_DROP_DELAY_RND
829 #define ED_COUNTER_ID_CUSTOM2_FIRST     ED_COUNTER_ID_MOVE_DELAY_FIX
830 #define ED_COUNTER_ID_CUSTOM2_LAST      ED_COUNTER_ID_IGNITION_DELAY
831 #define ED_COUNTER_ID_CUSTOM_FIRST      ED_COUNTER_ID_CUSTOM1_FIRST
832 #define ED_COUNTER_ID_CUSTOM_LAST       ED_COUNTER_ID_CUSTOM2_LAST
833
834 #define ED_COUNTER_ID_CHANGE_FIRST      ED_COUNTER_ID_CHANGE_DELAY_FIX
835 #define ED_COUNTER_ID_CHANGE_LAST       ED_COUNTER_ID_CHANGE_CONT_RND
836
837 // values for scrollbutton gadgets
838 enum
839 {
840   ED_SCROLLBUTTON_ID_AREA_UP,
841   ED_SCROLLBUTTON_ID_AREA_DOWN,
842   ED_SCROLLBUTTON_ID_AREA_LEFT,
843   ED_SCROLLBUTTON_ID_AREA_RIGHT,
844   ED_SCROLLBUTTON_ID_LIST_UP,
845   ED_SCROLLBUTTON_ID_LIST_DOWN,
846
847   ED_NUM_SCROLLBUTTONS
848 };
849
850 #define ED_SCROLLBUTTON_ID_AREA_FIRST   ED_SCROLLBUTTON_ID_AREA_UP
851 #define ED_SCROLLBUTTON_ID_AREA_LAST    ED_SCROLLBUTTON_ID_AREA_RIGHT
852
853 // values for scrollbar gadgets
854 enum
855 {
856   ED_SCROLLBAR_ID_AREA_HORIZONTAL,
857   ED_SCROLLBAR_ID_AREA_VERTICAL,
858   ED_SCROLLBAR_ID_LIST_VERTICAL,
859
860   ED_NUM_SCROLLBARS
861 };
862
863 #define ED_SCROLLBAR_ID_AREA_FIRST      ED_SCROLLBAR_ID_AREA_HORIZONTAL
864 #define ED_SCROLLBAR_ID_AREA_LAST       ED_SCROLLBAR_ID_AREA_VERTICAL
865
866 // values for text input gadgets
867 enum
868 {
869   ED_TEXTINPUT_ID_LEVEL_NAME,
870   ED_TEXTINPUT_ID_LEVEL_AUTHOR,
871   ED_TEXTINPUT_ID_LEVELSET_NAME,
872   ED_TEXTINPUT_ID_LEVELSET_AUTHOR,
873   ED_TEXTINPUT_ID_ELEMENT_NAME,
874
875   ED_NUM_TEXTINPUT
876 };
877
878 #define ED_TEXTINPUT_ID_LEVEL_FIRST     ED_TEXTINPUT_ID_LEVEL_NAME
879 #define ED_TEXTINPUT_ID_LEVEL_LAST      ED_TEXTINPUT_ID_LEVEL_AUTHOR
880
881 #define ED_TEXTINPUT_ID_LEVELSET_FIRST  ED_TEXTINPUT_ID_LEVELSET_NAME
882 #define ED_TEXTINPUT_ID_LEVELSET_LAST   ED_TEXTINPUT_ID_LEVELSET_AUTHOR
883
884 // values for text area gadgets
885 enum
886 {
887   ED_TEXTAREA_ID_ENVELOPE_INFO,
888
889   ED_NUM_TEXTAREAS
890 };
891
892 #define ED_TEXTAREA_ID_LEVEL_FIRST      ED_TEXTAREA_ID_ENVELOPE
893 #define ED_TEXTAREA_ID_LEVEL_LAST       ED_TEXTAREA_ID_ENVELOPE
894
895 // values for selectbox gadgets
896 enum
897 {
898   ED_SELECTBOX_ID_TIME_OR_STEPS,
899   ED_SELECTBOX_ID_TIME_SCORE_BASE,
900   ED_SELECTBOX_ID_GAME_ENGINE_TYPE,
901   ED_SELECTBOX_ID_BD_SCHEDULING_TYPE,
902   ED_SELECTBOX_ID_LEVELSET_SAVE_MODE,
903   ED_SELECTBOX_ID_WIND_DIRECTION,
904   ED_SELECTBOX_ID_PLAYER_SPEED,
905   ED_SELECTBOX_ID_MM_BALL_CHOICE_MODE,
906   ED_SELECTBOX_ID_CUSTOM_ACCESS_TYPE,
907   ED_SELECTBOX_ID_CUSTOM_ACCESS_LAYER,
908   ED_SELECTBOX_ID_CUSTOM_ACCESS_PROTECTED,
909   ED_SELECTBOX_ID_CUSTOM_ACCESS_DIRECTION,
910   ED_SELECTBOX_ID_CUSTOM_WALK_TO_ACTION,
911   ED_SELECTBOX_ID_CUSTOM_MOVE_PATTERN,
912   ED_SELECTBOX_ID_CUSTOM_MOVE_DIRECTION,
913   ED_SELECTBOX_ID_CUSTOM_MOVE_STEPSIZE,
914   ED_SELECTBOX_ID_CUSTOM_MOVE_LEAVE_TYPE,
915   ED_SELECTBOX_ID_CUSTOM_SMASH_TARGETS,
916   ED_SELECTBOX_ID_CUSTOM_SLIPPERY_TYPE,
917   ED_SELECTBOX_ID_CUSTOM_DEADLINESS,
918   ED_SELECTBOX_ID_CUSTOM_EXPLOSION_TYPE,
919   ED_SELECTBOX_ID_CHANGE_TIME_UNITS,
920   ED_SELECTBOX_ID_CHANGE_DIRECT_ACTION,
921   ED_SELECTBOX_ID_CHANGE_OTHER_ACTION,
922   ED_SELECTBOX_ID_CHANGE_SIDE,
923   ED_SELECTBOX_ID_CHANGE_PLAYER,
924   ED_SELECTBOX_ID_CHANGE_PAGE,
925   ED_SELECTBOX_ID_CHANGE_REPLACE_WHEN,
926   ED_SELECTBOX_ID_ACTION_TYPE,
927   ED_SELECTBOX_ID_ACTION_MODE,
928   ED_SELECTBOX_ID_ACTION_ARG,
929   ED_SELECTBOX_ID_SELECT_CHANGE_PAGE,
930   ED_SELECTBOX_ID_GROUP_CHOICE_MODE,
931
932   ED_NUM_SELECTBOX
933 };
934
935 #define ED_SELECTBOX_ID_LEVEL_FIRST     ED_SELECTBOX_ID_TIME_OR_STEPS
936 #define ED_SELECTBOX_ID_LEVEL_LAST      ED_SELECTBOX_ID_GAME_ENGINE_TYPE
937
938 #define ED_SELECTBOX_ID_LEVELSET_FIRST  ED_SELECTBOX_ID_LEVELSET_SAVE_MODE
939 #define ED_SELECTBOX_ID_LEVELSET_LAST   ED_SELECTBOX_ID_LEVELSET_SAVE_MODE
940
941 #define ED_SELECTBOX_ID_ENGINE_FIRST    ED_SELECTBOX_ID_BD_SCHEDULING_TYPE
942 #define ED_SELECTBOX_ID_ENGINE_LAST     ED_SELECTBOX_ID_BD_SCHEDULING_TYPE
943
944 #define ED_SELECTBOX_ID_CUSTOM1_FIRST   ED_SELECTBOX_ID_CUSTOM_ACCESS_TYPE
945 #define ED_SELECTBOX_ID_CUSTOM1_LAST    ED_SELECTBOX_ID_CUSTOM_WALK_TO_ACTION
946 #define ED_SELECTBOX_ID_CUSTOM2_FIRST   ED_SELECTBOX_ID_CUSTOM_MOVE_PATTERN
947 #define ED_SELECTBOX_ID_CUSTOM2_LAST    ED_SELECTBOX_ID_CUSTOM_EXPLOSION_TYPE
948 #define ED_SELECTBOX_ID_CUSTOM_FIRST    ED_SELECTBOX_ID_CUSTOM1_FIRST
949 #define ED_SELECTBOX_ID_CUSTOM_LAST     ED_SELECTBOX_ID_CUSTOM2_LAST
950
951 #define ED_SELECTBOX_ID_CHANGE_FIRST    ED_SELECTBOX_ID_CHANGE_TIME_UNITS
952 #define ED_SELECTBOX_ID_CHANGE_LAST     ED_SELECTBOX_ID_SELECT_CHANGE_PAGE
953
954 // values for textbutton gadgets
955 enum
956 {
957   ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL,
958   ED_TEXTBUTTON_ID_LEVELCONFIG_LEVELSET,
959   ED_TEXTBUTTON_ID_LEVELCONFIG_EDITOR,
960   ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE,
961   ED_TEXTBUTTON_ID_PROPERTIES_INFO,
962   ED_TEXTBUTTON_ID_PROPERTIES_CONFIG,
963   ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_1,
964   ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2,
965   ED_TEXTBUTTON_ID_PROPERTIES_CHANGE,
966   ED_TEXTBUTTON_ID_SAVE_LEVELSET,
967   ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2,
968   ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1,
969   ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE,
970   ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE,
971
972   ED_NUM_TEXTBUTTONS
973 };
974
975 #define ED_TAB_BUTTON_ID_LEVELCONFIG_FIRST ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL
976 #define ED_TAB_BUTTON_ID_LEVELCONFIG_LAST  ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE
977
978 #define ED_TAB_BUTTON_ID_PROPERTIES_FIRST ED_TEXTBUTTON_ID_PROPERTIES_INFO
979 #define ED_TAB_BUTTON_ID_PROPERTIES_LAST  ED_TEXTBUTTON_ID_PROPERTIES_CHANGE
980
981 #define ED_TEXTBUTTON_ID_CHANGE_FIRST   ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE
982 #define ED_TEXTBUTTON_ID_CHANGE_LAST    ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE
983
984 // values for graphicbutton gadgets
985 enum
986 {
987   ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE,
988   ED_GRAPHICBUTTON_ID_NEXT_CHANGE_PAGE,
989   ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE,
990   ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE,
991
992   ED_NUM_GRAPHICBUTTONS
993 };
994
995 #define ED_GRAPHICBUTTON_ID_CHANGE_FIRST  ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE
996 #define ED_GRAPHICBUTTON_ID_CHANGE_LAST   ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE
997
998 // values for checkbutton gadgets
999 enum
1000 {
1001   ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS,
1002   ED_CHECKBUTTON_ID_RATE_TIME_OVER_SCORE,
1003   ED_CHECKBUTTON_ID_USE_LEVELSET_ARTWORK,
1004   ED_CHECKBUTTON_ID_COPY_LEVEL_TEMPLATE,
1005   ED_CHECKBUTTON_ID_RANDOM_RESTRICTED,
1006   ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_3,
1007   ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2,
1008   ED_CHECKBUTTON_ID_BD_INTERMISSION,
1009   ED_CHECKBUTTON_ID_BD_PAL_TIMING,
1010   ED_CHECKBUTTON_ID_BD_LINE_SHIFTING_BORDERS,
1011   ED_CHECKBUTTON_ID_BD_SCAN_FIRST_AND_LAST_ROW,
1012   ED_CHECKBUTTON_ID_BD_SHORT_EXPLOSIONS,
1013   ED_CHECKBUTTON_ID_BD_GRAVITY_AFFECTS_ALL,
1014   ED_CHECKBUTTON_ID_STICK_ELEMENT,
1015   ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS,
1016   ED_CHECKBUTTON_ID_EM_EXPLODES_BY_FIRE,
1017   ED_CHECKBUTTON_ID_USE_SPRING_BUG,
1018   ED_CHECKBUTTON_ID_USE_TIME_ORB_BUG,
1019   ED_CHECKBUTTON_ID_USE_LIFE_BUGS,
1020   ED_CHECKBUTTON_ID_RANDOM_BALL_CONTENT,
1021   ED_CHECKBUTTON_ID_INITIAL_BALL_ACTIVE,
1022   ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE,
1023   ED_CHECKBUTTON_ID_SB_FIELDS_NEEDED,
1024   ED_CHECKBUTTON_ID_SB_OBJECTS_NEEDED,
1025   ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN,
1026   ED_CHECKBUTTON_ID_SOLVED_BY_ONE_PLAYER,
1027   ED_CHECKBUTTON_ID_FINISH_DIG_COLLECT,
1028   ED_CHECKBUTTON_ID_KEEP_WALKABLE_CE,
1029   ED_CHECKBUTTON_ID_CONTINUOUS_SNAPPING,
1030   ED_CHECKBUTTON_ID_BLOCK_SNAP_FIELD,
1031   ED_CHECKBUTTON_ID_BLOCK_LAST_FIELD,
1032   ED_CHECKBUTTON_ID_SP_BLOCK_LAST_FIELD,
1033   ED_CHECKBUTTON_ID_INSTANT_RELOCATION,
1034   ED_CHECKBUTTON_ID_SHIFTED_RELOCATION,
1035   ED_CHECKBUTTON_ID_LAZY_RELOCATION,
1036   ED_CHECKBUTTON_ID_USE_START_ELEMENT,
1037   ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT,
1038   ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT,
1039   ED_CHECKBUTTON_ID_INITIAL_GRAVITY,
1040   ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY,
1041   ED_CHECKBUTTON_ID_CAN_PASS_TO_WALKABLE,
1042   ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID,
1043   ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID,
1044   ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH,
1045   ED_CHECKBUTTON_ID_BD_DIAGONAL_MOVEMENTS,
1046   ED_CHECKBUTTON_ID_BD_TOPMOST_PLAYER_ACTIVE,
1047   ED_CHECKBUTTON_ID_BD_PUSH_MEGA_ROCK_WITH_SWEET,
1048   ED_CHECKBUTTON_ID_BD_MAGIC_WALL_WAIT_HATCHING,
1049   ED_CHECKBUTTON_ID_BD_MAGIC_WALL_STOPS_AMOEBA,
1050   ED_CHECKBUTTON_ID_ENVELOPE_AUTOWRAP,
1051   ED_CHECKBUTTON_ID_ENVELOPE_CENTERED,
1052   ED_CHECKBUTTON_ID_MM_LASER_RED,
1053   ED_CHECKBUTTON_ID_MM_LASER_GREEN,
1054   ED_CHECKBUTTON_ID_MM_LASER_BLUE,
1055   ED_CHECKBUTTON_ID_DF_LASER_RED,
1056   ED_CHECKBUTTON_ID_DF_LASER_GREEN,
1057   ED_CHECKBUTTON_ID_DF_LASER_BLUE,
1058   ED_CHECKBUTTON_ID_ROTATE_MM_BALL_CONTENT,
1059   ED_CHECKBUTTON_ID_EXPLODE_MM_BALL,
1060   ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC,
1061   ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1,
1062   ED_CHECKBUTTON_ID_CUSTOM_ACCESSIBLE,
1063   ED_CHECKBUTTON_ID_CUSTOM_GRAV_REACHABLE,
1064   ED_CHECKBUTTON_ID_CUSTOM_USE_LAST_VALUE,
1065   ED_CHECKBUTTON_ID_CUSTOM_WALK_TO_OBJECT,
1066   ED_CHECKBUTTON_ID_CUSTOM_INDESTRUCTIBLE,
1067   ED_CHECKBUTTON_ID_CUSTOM_CAN_MOVE,
1068   ED_CHECKBUTTON_ID_CUSTOM_CAN_FALL,
1069   ED_CHECKBUTTON_ID_CUSTOM_CAN_SMASH,
1070   ED_CHECKBUTTON_ID_CUSTOM_SLIPPERY,
1071   ED_CHECKBUTTON_ID_CUSTOM_DEADLY,
1072   ED_CHECKBUTTON_ID_CUSTOM_CAN_EXPLODE,
1073   ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_FIRE,
1074   ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_SMASH,
1075   ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_IMPACT,
1076   ED_CHECKBUTTON_ID_CUSTOM_CAN_CHANGE,
1077   ED_CHECKBUTTON_ID_CHANGE_DELAY,
1078   ED_CHECKBUTTON_ID_CHANGE_BY_DIRECT_ACT,
1079   ED_CHECKBUTTON_ID_CHANGE_BY_OTHER_ACT,
1080   ED_CHECKBUTTON_ID_CHANGE_USE_EXPLOSION,
1081   ED_CHECKBUTTON_ID_CHANGE_USE_CONTENT,
1082   ED_CHECKBUTTON_ID_CHANGE_ONLY_COMPLETE,
1083   ED_CHECKBUTTON_ID_CHANGE_USE_RANDOM,
1084   ED_CHECKBUTTON_ID_CHANGE_HAS_ACTION,
1085
1086   ED_NUM_CHECKBUTTONS
1087 };
1088
1089 #define ED_CHECKBUTTON_ID_LEVEL_FIRST   ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS
1090 #define ED_CHECKBUTTON_ID_LEVEL_LAST    ED_CHECKBUTTON_ID_RATE_TIME_OVER_SCORE
1091
1092 #define ED_CHECKBUTTON_ID_LEVELSET_FIRST ED_CHECKBUTTON_ID_USE_LEVELSET_ARTWORK
1093 #define ED_CHECKBUTTON_ID_LEVELSET_LAST  ED_CHECKBUTTON_ID_COPY_LEVEL_TEMPLATE
1094
1095 #define ED_CHECKBUTTON_ID_EDITOR_FIRST  ED_CHECKBUTTON_ID_RANDOM_RESTRICTED
1096 #define ED_CHECKBUTTON_ID_EDITOR_LAST   ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2
1097
1098 #define ED_CHECKBUTTON_ID_ENGINE_FIRST  ED_CHECKBUTTON_ID_BD_INTERMISSION
1099 #define ED_CHECKBUTTON_ID_ENGINE_LAST   ED_CHECKBUTTON_ID_BD_GRAVITY_AFFECTS_ALL
1100
1101 #define ED_CHECKBUTTON_ID_CUSTOM1_FIRST ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC
1102 #define ED_CHECKBUTTON_ID_CUSTOM1_LAST  ED_CHECKBUTTON_ID_CUSTOM_INDESTRUCTIBLE
1103 #define ED_CHECKBUTTON_ID_CUSTOM2_FIRST ED_CHECKBUTTON_ID_CUSTOM_CAN_MOVE
1104 #define ED_CHECKBUTTON_ID_CUSTOM2_LAST  ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_IMPACT
1105 #define ED_CHECKBUTTON_ID_CUSTOM_FIRST  ED_CHECKBUTTON_ID_CUSTOM1_FIRST
1106 #define ED_CHECKBUTTON_ID_CUSTOM_LAST   ED_CHECKBUTTON_ID_CUSTOM2_LAST
1107
1108 #define ED_CHECKBUTTON_ID_CHANGE_FIRST  ED_CHECKBUTTON_ID_CUSTOM_CAN_CHANGE
1109 #define ED_CHECKBUTTON_ID_CHANGE_LAST   ED_CHECKBUTTON_ID_CHANGE_HAS_ACTION
1110
1111 // values for radiobutton gadgets
1112 enum
1113 {
1114   ED_RADIOBUTTON_ID_PERCENTAGE,
1115   ED_RADIOBUTTON_ID_QUANTITY,
1116
1117   ED_NUM_RADIOBUTTONS
1118 };
1119
1120 #define ED_RADIOBUTTON_ID_EDITOR_FIRST  ED_RADIOBUTTON_ID_PERCENTAGE
1121 #define ED_RADIOBUTTON_ID_EDITOR_LAST   ED_RADIOBUTTON_ID_QUANTITY
1122
1123 // values for drawing area gadgets
1124 enum
1125 {
1126   ED_DRAWING_ID_DRAWING_LEVEL,
1127   ED_DRAWING_ID_YAMYAM_CONTENT_0,
1128   ED_DRAWING_ID_YAMYAM_CONTENT_1,
1129   ED_DRAWING_ID_YAMYAM_CONTENT_2,
1130   ED_DRAWING_ID_YAMYAM_CONTENT_3,
1131   ED_DRAWING_ID_YAMYAM_CONTENT_4,
1132   ED_DRAWING_ID_YAMYAM_CONTENT_5,
1133   ED_DRAWING_ID_YAMYAM_CONTENT_6,
1134   ED_DRAWING_ID_YAMYAM_CONTENT_7,
1135   ED_DRAWING_ID_MAGIC_BALL_CONTENT_0,
1136   ED_DRAWING_ID_MAGIC_BALL_CONTENT_1,
1137   ED_DRAWING_ID_MAGIC_BALL_CONTENT_2,
1138   ED_DRAWING_ID_MAGIC_BALL_CONTENT_3,
1139   ED_DRAWING_ID_MAGIC_BALL_CONTENT_4,
1140   ED_DRAWING_ID_MAGIC_BALL_CONTENT_5,
1141   ED_DRAWING_ID_MAGIC_BALL_CONTENT_6,
1142   ED_DRAWING_ID_MAGIC_BALL_CONTENT_7,
1143   ED_DRAWING_ID_ANDROID_CONTENT,
1144   ED_DRAWING_ID_AMOEBA_CONTENT,
1145   ED_DRAWING_ID_START_ELEMENT,
1146   ED_DRAWING_ID_ARTWORK_ELEMENT,
1147   ED_DRAWING_ID_EXPLOSION_ELEMENT,
1148   ED_DRAWING_ID_INVENTORY_CONTENT,
1149   ED_DRAWING_ID_MM_BALL_CONTENT,
1150   ED_DRAWING_ID_CUSTOM_GRAPHIC,
1151   ED_DRAWING_ID_CUSTOM_CONTENT,
1152   ED_DRAWING_ID_CUSTOM_MOVE_ENTER,
1153   ED_DRAWING_ID_CUSTOM_MOVE_LEAVE,
1154   ED_DRAWING_ID_CUSTOM_CHANGE_TARGET,
1155   ED_DRAWING_ID_CUSTOM_CHANGE_CONTENT,
1156   ED_DRAWING_ID_CUSTOM_CHANGE_TRIGGER,
1157   ED_DRAWING_ID_CUSTOM_CHANGE_ACTION,
1158   ED_DRAWING_ID_GROUP_CONTENT,
1159   ED_DRAWING_ID_RANDOM_BACKGROUND,
1160
1161   ED_NUM_DRAWING_AREAS
1162 };
1163
1164 #define ED_DRAWING_ID_EDITOR_FIRST      ED_DRAWING_ID_RANDOM_BACKGROUND
1165 #define ED_DRAWING_ID_EDITOR_LAST       ED_DRAWING_ID_RANDOM_BACKGROUND
1166
1167
1168 // ----------------------------------------------------------------------------
1169 // some internally used definitions
1170 // ----------------------------------------------------------------------------
1171
1172 // values for CopyLevelToUndoBuffer()
1173 #define UNDO_IMMEDIATE                  0
1174 #define UNDO_ACCUMULATE                 1
1175
1176 // values for scrollbars
1177 #define ED_SCROLL_NO                    0
1178 #define ED_SCROLL_LEFT                  1
1179 #define ED_SCROLL_RIGHT                 2
1180 #define ED_SCROLL_UP                    4
1181 #define ED_SCROLL_DOWN                  8
1182
1183 // screens in the level editor
1184 #define ED_MODE_DRAWING                 0
1185 #define ED_MODE_LEVELCONFIG             1
1186 #define ED_MODE_PROPERTIES              2
1187 #define ED_MODE_PALETTE                 3
1188
1189 // sub-screens in the global settings section
1190 #define ED_MODE_LEVELCONFIG_LEVEL       ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL
1191 #define ED_MODE_LEVELCONFIG_LEVELSET    ED_TEXTBUTTON_ID_LEVELCONFIG_LEVELSET
1192 #define ED_MODE_LEVELCONFIG_EDITOR      ED_TEXTBUTTON_ID_LEVELCONFIG_EDITOR
1193 #define ED_MODE_LEVELCONFIG_ENGINE      ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE
1194
1195 // sub-screens in the element properties section
1196 #define ED_MODE_PROPERTIES_INFO         ED_TEXTBUTTON_ID_PROPERTIES_INFO
1197 #define ED_MODE_PROPERTIES_CONFIG       ED_TEXTBUTTON_ID_PROPERTIES_CONFIG
1198 #define ED_MODE_PROPERTIES_CONFIG_1     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_1
1199 #define ED_MODE_PROPERTIES_CONFIG_2     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2
1200 #define ED_MODE_PROPERTIES_CHANGE       ED_TEXTBUTTON_ID_PROPERTIES_CHANGE
1201
1202 // how many steps can be cancelled
1203 #define NUM_UNDO_STEPS                  (64 + 1)
1204
1205 // values for elements with score for certain actions
1206 #define MIN_SCORE                       0
1207 #define MAX_SCORE                       999
1208
1209 // values for elements with count for collecting
1210 #define MIN_COLLECT_COUNT               0
1211 #define MAX_COLLECT_COUNT               999
1212
1213 // values for random placement
1214 #define RANDOM_USE_PERCENTAGE           0
1215 #define RANDOM_USE_QUANTITY             1
1216
1217 // values for level set save mode
1218 #define LEVELSET_SAVE_MODE_UPDATE       0
1219 #define LEVELSET_SAVE_MODE_CREATE       1
1220
1221 // default value for element tile size in drawing area
1222 #define DEFAULT_EDITOR_TILESIZE         MINI_TILESIZE
1223 #define DEFAULT_EDITOR_TILESIZE_MM      TILESIZE
1224
1225
1226 // ----------------------------------------------------------------------------
1227 // some internally used data structure definitions
1228 // ----------------------------------------------------------------------------
1229
1230 static struct
1231 {
1232   int graphic;
1233   int gadget_id;
1234   struct XYTileSize *pos;
1235   int gadget_type;
1236   char *infotext;
1237   char shortcut;
1238 } controlbutton_info[ED_NUM_CTRL_BUTTONS] =
1239 {
1240   // note: some additional characters are already reserved for "cheat mode"
1241   // shortcuts (":XYZ" style) -- for details, see "events.c"
1242
1243   // ---------- toolbox control buttons ---------------------------------------
1244
1245   {
1246     IMG_GFX_EDITOR_BUTTON_DRAW_SINGLE,          GADGET_ID_SINGLE_ITEMS,
1247     &editor.button.draw_single,                 GD_TYPE_RADIO_BUTTON,
1248     "draw single items",                        's'
1249   },
1250   {
1251     IMG_GFX_EDITOR_BUTTON_DRAW_CONNECTED,       GADGET_ID_CONNECTED_ITEMS,
1252     &editor.button.draw_connected,              GD_TYPE_RADIO_BUTTON,
1253     "draw connected items",                     'd'
1254   },
1255   {
1256     IMG_GFX_EDITOR_BUTTON_DRAW_LINE,            GADGET_ID_LINE,
1257     &editor.button.draw_line,                   GD_TYPE_RADIO_BUTTON,
1258     "draw lines",                               'l'
1259   },
1260   {
1261     IMG_GFX_EDITOR_BUTTON_DRAW_ARC,             GADGET_ID_ARC,
1262     &editor.button.draw_arc,                    GD_TYPE_RADIO_BUTTON,
1263     "draw arcs",                                'a'
1264   },
1265   {
1266     IMG_GFX_EDITOR_BUTTON_DRAW_RECTANGLE,       GADGET_ID_RECTANGLE,
1267     &editor.button.draw_rectangle,              GD_TYPE_RADIO_BUTTON,
1268     "draw outline rectangles",                  'r'
1269   },
1270   {
1271     IMG_GFX_EDITOR_BUTTON_DRAW_FILLED_BOX,      GADGET_ID_FILLED_BOX,
1272     &editor.button.draw_filled_box,             GD_TYPE_RADIO_BUTTON,
1273     "draw filled rectangles",                   'R'
1274   },
1275   {
1276     IMG_GFX_EDITOR_BUTTON_ROTATE_UP,            GADGET_ID_WRAP_UP,
1277     &editor.button.rotate_up,                   GD_TYPE_NORMAL_BUTTON,
1278     "wrap (rotate) level up",                   0
1279   },
1280   {
1281     IMG_GFX_EDITOR_BUTTON_DRAW_TEXT,            GADGET_ID_TEXT,
1282     &editor.button.draw_text,                   GD_TYPE_RADIO_BUTTON,
1283     "enter text elements",                      't'
1284   },
1285   {
1286     IMG_GFX_EDITOR_BUTTON_FLOOD_FILL,           GADGET_ID_FLOOD_FILL,
1287     &editor.button.flood_fill,                  GD_TYPE_RADIO_BUTTON,
1288     "flood fill",                               'f'
1289   },
1290   {
1291     IMG_GFX_EDITOR_BUTTON_ROTATE_LEFT,          GADGET_ID_WRAP_LEFT,
1292     &editor.button.rotate_left,                 GD_TYPE_NORMAL_BUTTON,
1293     "wrap (rotate) level left",                 0
1294   },
1295   {
1296     IMG_GFX_EDITOR_BUTTON_ZOOM_LEVEL,           GADGET_ID_ZOOM,
1297     &editor.button.zoom_level,                  GD_TYPE_NORMAL_BUTTON,
1298     "zoom level tile size",                     '+'
1299   },
1300   {
1301     IMG_GFX_EDITOR_BUTTON_ROTATE_RIGHT,         GADGET_ID_WRAP_RIGHT,
1302     &editor.button.rotate_right,                GD_TYPE_NORMAL_BUTTON,
1303     "wrap (rotate) level right",                0
1304   },
1305   {
1306     IMG_GFX_EDITOR_BUTTON_DRAW_RANDOM,          GADGET_ID_RANDOM_PLACEMENT,
1307     &editor.button.draw_random,                 GD_TYPE_NORMAL_BUTTON,
1308     "random element placement",                 0
1309   },
1310   {
1311     IMG_GFX_EDITOR_BUTTON_GRAB_BRUSH,           GADGET_ID_GRAB_BRUSH,
1312     &editor.button.grab_brush,                  GD_TYPE_RADIO_BUTTON,
1313     "grab brush",                               'b'
1314   },
1315   {
1316     IMG_GFX_EDITOR_BUTTON_ROTATE_DOWN,          GADGET_ID_WRAP_DOWN,
1317     &editor.button.rotate_down,                 GD_TYPE_NORMAL_BUTTON,
1318     "wrap (rotate) level down",                 0
1319   },
1320   {
1321     IMG_GFX_EDITOR_BUTTON_PICK_ELEMENT,         GADGET_ID_PICK_ELEMENT,
1322     &editor.button.pick_element,                GD_TYPE_RADIO_BUTTON,
1323     "pick drawing element",                     ','
1324   },
1325
1326   // ---------- level control buttons -----------------------------------------
1327
1328   {
1329     IMG_GFX_EDITOR_BUTTON_UNDO,                 GADGET_ID_UNDO,
1330     &editor.button.undo,                        GD_TYPE_NORMAL_BUTTON,
1331     "undo/redo last operation",                 'u'
1332   },
1333   {
1334     IMG_GFX_EDITOR_BUTTON_CONF,                 GADGET_ID_CONF,
1335     &editor.button.conf,                        GD_TYPE_NORMAL_BUTTON,
1336     "level and editor settings",                'I'
1337   },
1338   {
1339     IMG_GFX_EDITOR_BUTTON_SAVE,                 GADGET_ID_SAVE,
1340     &editor.button.save,                        GD_TYPE_NORMAL_BUTTON,
1341     "save level",                               'S'
1342   },
1343   {
1344     IMG_GFX_EDITOR_BUTTON_CLEAR,                GADGET_ID_CLEAR,
1345     &editor.button.clear,                       GD_TYPE_NORMAL_BUTTON,
1346     "clear level",                              'C'
1347   },
1348   {
1349     IMG_GFX_EDITOR_BUTTON_TEST,                 GADGET_ID_TEST,
1350     &editor.button.test,                        GD_TYPE_NORMAL_BUTTON,
1351     "test level",                               'T'
1352   },
1353   {
1354     IMG_GFX_EDITOR_BUTTON_EXIT,                 GADGET_ID_EXIT,
1355     &editor.button.exit,                        GD_TYPE_NORMAL_BUTTON,
1356     "exit level editor",                        'E'
1357   },
1358
1359   // ---------- CE and GE control buttons -------------------------------------
1360
1361   {
1362     IMG_GFX_EDITOR_BUTTON_CE_COPY_FROM,         GADGET_ID_CUSTOM_COPY_FROM,
1363     &editor.button.ce_copy_from,                GD_TYPE_RADIO_BUTTON,
1364     "copy settings from other element",         0
1365   },
1366   {
1367     IMG_GFX_EDITOR_BUTTON_CE_COPY_TO,           GADGET_ID_CUSTOM_COPY_TO,
1368     &editor.button.ce_copy_to,                  GD_TYPE_RADIO_BUTTON,
1369     "copy settings to other element",           0
1370   },
1371   {
1372     IMG_GFX_EDITOR_BUTTON_CE_SWAP,              GADGET_ID_CUSTOM_EXCHANGE,
1373     &editor.button.ce_swap,                     GD_TYPE_RADIO_BUTTON,
1374     "exchange element with other element",      0
1375   },
1376   {
1377     IMG_GFX_EDITOR_BUTTON_CE_COPY,              GADGET_ID_CUSTOM_COPY,
1378     &editor.button.ce_copy,                     GD_TYPE_NORMAL_BUTTON,
1379     "copy settings from this element",          0
1380   },
1381   {
1382     IMG_GFX_EDITOR_BUTTON_CE_PASTE,             GADGET_ID_CUSTOM_PASTE,
1383     &editor.button.ce_paste,                    GD_TYPE_NORMAL_BUTTON,
1384     "paste settings to this element",           0
1385   },
1386
1387   // ---------- palette control buttons ---------------------------------------
1388
1389   {
1390     IMG_GFX_EDITOR_BUTTON_PROPERTIES,           GADGET_ID_PROPERTIES,
1391     &editor.button.properties,                  GD_TYPE_NORMAL_BUTTON,
1392     "properties of drawing element",            'p'
1393   },
1394   {
1395     IMG_GFX_EDITOR_BUTTON_ELEMENT_LEFT,         GADGET_ID_ELEMENT_LEFT,
1396     &editor.button.element_left,                GD_TYPE_NORMAL_BUTTON,
1397     "properties of drawing element 1",          '1'
1398   },
1399   {
1400     IMG_GFX_EDITOR_BUTTON_ELEMENT_MIDDLE,       GADGET_ID_ELEMENT_MIDDLE,
1401     &editor.button.element_middle,              GD_TYPE_NORMAL_BUTTON,
1402     "properties of drawing element 2",          '2'
1403   },
1404   {
1405     IMG_GFX_EDITOR_BUTTON_ELEMENT_RIGHT,        GADGET_ID_ELEMENT_RIGHT,
1406     &editor.button.element_right,               GD_TYPE_NORMAL_BUTTON,
1407     "properties of drawing element 3",          '3'
1408   },
1409   {
1410     IMG_GFX_EDITOR_BUTTON_PALETTE,              GADGET_ID_PALETTE,
1411     &editor.button.palette,                     GD_TYPE_NORMAL_BUTTON,
1412     "show list of elements",                    'e'
1413   }
1414 };
1415
1416 static int random_placement_value = 10;
1417 static int random_placement_method = RANDOM_USE_QUANTITY;
1418 static int random_placement_background_element = EL_SAND;
1419 static boolean random_placement_background_restricted = FALSE;
1420 static boolean stick_element_properties_window = FALSE;
1421 static boolean custom_element_properties[NUM_ELEMENT_PROPERTIES];
1422 static boolean custom_element_change_events[NUM_CHANGE_EVENTS];
1423 static struct ElementChangeInfo custom_element_change;
1424 static struct ElementGroupInfo group_element_info;
1425 static struct ElementInfo custom_element;
1426
1427 static char levelset_name[MAX_LEVEL_NAME_LEN + 1];
1428 static char levelset_author[MAX_LEVEL_AUTHOR_LEN + 1];
1429 static int levelset_num_levels = 100;
1430 static boolean levelset_use_levelset_artwork = FALSE;
1431 static boolean levelset_copy_level_template = FALSE;
1432 static int levelset_save_mode = LEVELSET_SAVE_MODE_UPDATE;
1433
1434 static struct
1435 {
1436   int gadget_type_id;
1437   int x, y;
1438   int min_value, max_value;
1439   int gadget_id_down, gadget_id_up;
1440   int gadget_id_text;
1441   int gadget_id_align;
1442   int *value;
1443   char *text_above, *text_left, *text_right;
1444 } counterbutton_info[ED_NUM_COUNTERBUTTONS] =
1445 {
1446   // ---------- current level number ------------------------------------------
1447
1448   {
1449     ED_COUNTER_ID_SELECT_LEVEL,
1450     -1, -1,     // these values are not constant, but can change at runtime
1451     1,                                  100,
1452     GADGET_ID_SELECT_LEVEL_DOWN,        GADGET_ID_SELECT_LEVEL_UP,
1453     GADGET_ID_SELECT_LEVEL_TEXT,        GADGET_ID_NONE,
1454     &level_nr,
1455     NULL,                               NULL, NULL
1456   },
1457
1458   // ---------- level and editor settings -------------------------------------
1459
1460   {
1461     ED_COUNTER_ID_LEVEL_XSIZE,
1462     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
1463     MIN_LEV_FIELDX,                     MAX_LEV_FIELDX,
1464     GADGET_ID_LEVEL_XSIZE_DOWN,         GADGET_ID_LEVEL_XSIZE_UP,
1465     GADGET_ID_LEVEL_XSIZE_TEXT,         GADGET_ID_NONE,
1466     &level.fieldx,
1467     "playfield size:",                  NULL, "width",
1468   },
1469   {
1470     ED_COUNTER_ID_LEVEL_YSIZE,
1471     -1,                                 ED_LEVEL_SETTINGS_YPOS(4),
1472     MIN_LEV_FIELDY,                     MAX_LEV_FIELDY,
1473     GADGET_ID_LEVEL_YSIZE_DOWN,         GADGET_ID_LEVEL_YSIZE_UP,
1474     GADGET_ID_LEVEL_YSIZE_TEXT,         GADGET_ID_LEVEL_XSIZE_UP,
1475     &level.fieldy,
1476     NULL,                               " ", "height",
1477   },
1478   {
1479     ED_COUNTER_ID_LEVEL_GEMSLIMIT,
1480     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(5),
1481     0,                                  999,
1482     GADGET_ID_LEVEL_GEMSLIMIT_DOWN,     GADGET_ID_LEVEL_GEMSLIMIT_UP,
1483     GADGET_ID_LEVEL_GEMSLIMIT_TEXT,     GADGET_ID_NONE,
1484     &level.gems_needed,
1485     NULL,                               "number of gems to collect:", NULL
1486   },
1487   {
1488     ED_COUNTER_ID_LEVEL_TIMELIMIT,
1489     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(8),
1490     0,                                  9999,
1491     GADGET_ID_LEVEL_TIMELIMIT_DOWN,     GADGET_ID_LEVEL_TIMELIMIT_UP,
1492     GADGET_ID_LEVEL_TIMELIMIT_TEXT,     GADGET_ID_NONE,
1493     &level.time,
1494     "time or step limit to solve level:", NULL, NULL
1495   },
1496   {
1497     ED_COUNTER_ID_LEVEL_TIMESCORE,
1498     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(10),
1499     0,                                  999,
1500     GADGET_ID_LEVEL_TIMESCORE_DOWN,     GADGET_ID_LEVEL_TIMESCORE_UP,
1501     GADGET_ID_LEVEL_TIMESCORE_TEXT,     GADGET_ID_NONE,
1502     &level.score[SC_TIME_BONUS],
1503     "score for time or steps left:",    NULL, NULL
1504   },
1505   {
1506     ED_COUNTER_ID_LEVEL_RANDOM_SEED,
1507     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(13),
1508     0,                                  9999,
1509     GADGET_ID_LEVEL_RANDOM_SEED_DOWN,   GADGET_ID_LEVEL_RANDOM_SEED_UP,
1510     GADGET_ID_LEVEL_RANDOM_SEED_TEXT,   GADGET_ID_NONE,
1511     &level.random_seed,
1512     NULL,                               "random seed:", "(0 => random)"
1513   },
1514   {
1515     ED_COUNTER_ID_LEVELSET_NUM_LEVELS,
1516     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
1517     1,                                  MAX_LEVELS,
1518     GADGET_ID_LEVELSET_NUM_LEVELS_DOWN, GADGET_ID_LEVELSET_NUM_LEVELS_UP,
1519     GADGET_ID_LEVELSET_NUM_LEVELS_TEXT, GADGET_ID_NONE,
1520     &levelset_num_levels,
1521     "number of levels:",                NULL, NULL,
1522   },
1523   {
1524     ED_COUNTER_ID_LEVEL_RANDOM,
1525     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(0),
1526     1,                                  100,
1527     GADGET_ID_LEVEL_RANDOM_DOWN,        GADGET_ID_LEVEL_RANDOM_UP,
1528     GADGET_ID_LEVEL_RANDOM_TEXT,        GADGET_ID_NONE,
1529     &random_placement_value,
1530     "random element placement:",        NULL, "in"
1531   },
1532   {
1533     ED_COUNTER_ID_BD_CYCLE_DELAY_MS,
1534     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(3),
1535     50,                                 500,
1536     GADGET_ID_BD_CYCLE_DELAY_MS_DOWN,   GADGET_ID_BD_CYCLE_DELAY_MS_UP,
1537     GADGET_ID_BD_CYCLE_DELAY_MS_TEXT,   GADGET_ID_NONE,
1538     &level.bd_cycle_delay_ms,
1539     NULL,                               NULL, "game cycle delay (ms)"
1540   },
1541   {
1542     ED_COUNTER_ID_BD_CYCLE_DELAY_C64,
1543     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(3),
1544     0,                                  32,
1545     GADGET_ID_BD_CYCLE_DELAY_C64_DOWN,  GADGET_ID_BD_CYCLE_DELAY_C64_UP,
1546     GADGET_ID_BD_CYCLE_DELAY_C64_TEXT,  GADGET_ID_NONE,
1547     &level.bd_cycle_delay_c64,
1548     NULL,                               NULL, "game cycle delay (C64-style)"
1549   },
1550   {
1551     ED_COUNTER_ID_BD_HATCHING_DELAY_CYCLES,
1552     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
1553     1,                                  40,
1554     GADGET_ID_BD_HATCHING_DELAY_CYCLES_DOWN,    GADGET_ID_BD_HATCHING_DELAY_CYCLES_UP,
1555     GADGET_ID_BD_HATCHING_DELAY_CYCLES_TEXT,    GADGET_ID_NONE,
1556     &level.bd_hatching_delay_cycles,
1557     NULL,                               NULL, "hatching delay (cycles)"
1558   },
1559   {
1560     ED_COUNTER_ID_BD_HATCHING_DELAY_SECONDS,
1561     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
1562     1,                                  40,
1563     GADGET_ID_BD_HATCHING_DELAY_SECONDS_DOWN,   GADGET_ID_BD_HATCHING_DELAY_SECONDS_UP,
1564     GADGET_ID_BD_HATCHING_DELAY_SECONDS_TEXT,   GADGET_ID_NONE,
1565     &level.bd_hatching_delay_seconds,
1566     NULL,                               NULL, "hatching delay (seconds)"
1567   },
1568
1569   // ---------- element settings: configure (various elements) ----------------
1570
1571   {
1572     ED_COUNTER_ID_BD_PUSHING_PROB,
1573     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
1574     0,                                  100,
1575     GADGET_ID_BD_PUSHING_PROB_DOWN,     GADGET_ID_BD_PUSHING_PROB_UP,
1576     GADGET_ID_BD_PUSHING_PROB_TEXT,     GADGET_ID_NONE,
1577     &level.bd_pushing_prob,
1578     NULL,                               NULL, "push probability"
1579   },
1580   {
1581     ED_COUNTER_ID_BD_PUSHING_PROB_WITH_SWEET,
1582     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1583     0,                                  100,
1584     GADGET_ID_BD_PUSHING_PROB_WITH_SWEET_DOWN,  GADGET_ID_BD_PUSHING_PROB_WITH_SWEET_UP,
1585     GADGET_ID_BD_PUSHING_PROB_WITH_SWEET_TEXT,  GADGET_ID_NONE,
1586     &level.bd_pushing_prob_with_sweet,
1587     NULL,                               NULL, "push probability with sweet"
1588   },
1589   {
1590     ED_COUNTER_ID_ELEMENT_VALUE1,
1591     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
1592     MIN_SCORE,                          MAX_SCORE,
1593     GADGET_ID_ELEMENT_VALUE1_DOWN,      GADGET_ID_ELEMENT_VALUE1_UP,
1594     GADGET_ID_ELEMENT_VALUE1_TEXT,      GADGET_ID_NONE,
1595     NULL,                               // will be set when used
1596     NULL,                               NULL, NULL
1597   },
1598   {
1599     ED_COUNTER_ID_ELEMENT_VALUE2,
1600     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
1601     MIN_SCORE,                          MAX_SCORE,
1602     GADGET_ID_ELEMENT_VALUE2_DOWN,      GADGET_ID_ELEMENT_VALUE2_UP,
1603     GADGET_ID_ELEMENT_VALUE2_TEXT,      GADGET_ID_NONE,
1604     NULL,                               // will be set when used
1605     NULL,                               NULL, NULL
1606   },
1607   {
1608     ED_COUNTER_ID_ELEMENT_VALUE3,
1609     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
1610     MIN_SCORE,                          MAX_SCORE,
1611     GADGET_ID_ELEMENT_VALUE3_DOWN,      GADGET_ID_ELEMENT_VALUE3_UP,
1612     GADGET_ID_ELEMENT_VALUE3_TEXT,      GADGET_ID_NONE,
1613     NULL,                               // will be set when used
1614     NULL,                               NULL, NULL
1615   },
1616   {
1617     ED_COUNTER_ID_ELEMENT_VALUE4,
1618     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1619     MIN_SCORE,                          MAX_SCORE,
1620     GADGET_ID_ELEMENT_VALUE4_DOWN,      GADGET_ID_ELEMENT_VALUE4_UP,
1621     GADGET_ID_ELEMENT_VALUE4_TEXT,      GADGET_ID_NONE,
1622     NULL,                               // will be set when used
1623     NULL,                               NULL, NULL
1624   },
1625   {
1626     ED_COUNTER_ID_YAMYAM_CONTENT,
1627     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1628     MIN_ELEMENT_CONTENTS,               MAX_ELEMENT_CONTENTS,
1629     GADGET_ID_YAMYAM_CONTENT_DOWN,      GADGET_ID_YAMYAM_CONTENT_UP,
1630     GADGET_ID_YAMYAM_CONTENT_TEXT,      GADGET_ID_NONE,
1631     &level.num_yamyam_contents,
1632     NULL,                               NULL, "number of content areas"
1633   },
1634   {
1635     ED_COUNTER_ID_BALL_CONTENT,
1636     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
1637     MIN_ELEMENT_CONTENTS,               MAX_ELEMENT_CONTENTS,
1638     GADGET_ID_BALL_CONTENT_DOWN,        GADGET_ID_BALL_CONTENT_UP,
1639     GADGET_ID_BALL_CONTENT_TEXT,        GADGET_ID_NONE,
1640     &level.num_ball_contents,
1641     NULL,                               NULL, "number of content areas"
1642   },
1643   {
1644     ED_COUNTER_ID_ANDROID_CONTENT,
1645     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
1646     MIN_ANDROID_ELEMENTS,               MAX_ANDROID_ELEMENTS,
1647     GADGET_ID_ANDROID_CONTENT_DOWN,     GADGET_ID_ANDROID_CONTENT_UP,
1648     GADGET_ID_ANDROID_CONTENT_TEXT,     GADGET_ID_NONE,
1649     &level.num_android_clone_elements,
1650     NULL,                               NULL, "number of clonable elements"
1651   },
1652   {
1653     ED_COUNTER_ID_ENVELOPE_XSIZE,
1654     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
1655     MIN_ENVELOPE_XSIZE,                 MAX_ENVELOPE_XSIZE,
1656     GADGET_ID_ENVELOPE_XSIZE_DOWN,      GADGET_ID_ENVELOPE_XSIZE_UP,
1657     GADGET_ID_ENVELOPE_XSIZE_TEXT,      GADGET_ID_NONE,
1658     NULL,                               // will be set when used
1659     NULL,                               NULL, "width",
1660   },
1661   {
1662     ED_COUNTER_ID_ENVELOPE_YSIZE,
1663     -1,                                 ED_ELEMENT_SETTINGS_YPOS(0),
1664     MIN_ENVELOPE_YSIZE,                 MAX_ENVELOPE_YSIZE,
1665     GADGET_ID_ENVELOPE_YSIZE_DOWN,      GADGET_ID_ENVELOPE_YSIZE_UP,
1666     GADGET_ID_ENVELOPE_YSIZE_TEXT,      GADGET_ID_ENVELOPE_XSIZE_UP,
1667     NULL,                               // will be set when used
1668     NULL,                               " ", "height",
1669   },
1670   {
1671     ED_COUNTER_ID_INVENTORY_SIZE,
1672     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
1673     MIN_INITIAL_INVENTORY_SIZE,         MAX_INITIAL_INVENTORY_SIZE,
1674     GADGET_ID_INVENTORY_SIZE_DOWN,      GADGET_ID_INVENTORY_SIZE_UP,
1675     GADGET_ID_INVENTORY_SIZE_TEXT,      GADGET_ID_NONE,
1676     &level.initial_inventory_size[0],
1677     NULL,                               NULL, "number of inventory elements"
1678   },
1679   {
1680     ED_COUNTER_ID_MM_BALL_CONTENT,
1681     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1682     MIN_ELEMENTS_IN_GROUP,              MAX_MM_BALL_CONTENTS,
1683     GADGET_ID_MM_BALL_CONTENT_DOWN,     GADGET_ID_MM_BALL_CONTENT_UP,
1684     GADGET_ID_MM_BALL_CONTENT_TEXT,     GADGET_ID_NONE,
1685     &level.num_mm_ball_contents,
1686     NULL,                               NULL, "number of content elements"
1687   },
1688
1689   // ---------- element settings: configure 1 (custom elements) ---------------
1690
1691   {
1692     ED_COUNTER_ID_CUSTOM_SCORE,
1693     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(5),
1694     MIN_SCORE,                          MAX_SCORE,
1695     GADGET_ID_CUSTOM_SCORE_DOWN,        GADGET_ID_CUSTOM_SCORE_UP,
1696     GADGET_ID_CUSTOM_SCORE_TEXT,        GADGET_ID_NONE,
1697     &custom_element.collect_score_initial,
1698     NULL,                               "CE score", " "
1699   },
1700   {
1701     ED_COUNTER_ID_CUSTOM_GEMCOUNT,
1702     -1,                                 ED_ELEMENT_SETTINGS_YPOS(5),
1703     MIN_COLLECT_COUNT,                  MAX_COLLECT_COUNT,
1704     GADGET_ID_CUSTOM_GEMCOUNT_DOWN,     GADGET_ID_CUSTOM_GEMCOUNT_UP,
1705     GADGET_ID_CUSTOM_GEMCOUNT_TEXT,     GADGET_ID_CUSTOM_SCORE_UP,
1706     &custom_element.collect_count_initial,
1707     NULL,                               "CE count", NULL
1708   },
1709   {
1710     ED_COUNTER_ID_CUSTOM_VALUE_FIX,
1711     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(10),
1712     0,                                  9999,
1713     GADGET_ID_CUSTOM_VALUE_FIX_DOWN,    GADGET_ID_CUSTOM_VALUE_FIX_UP,
1714     GADGET_ID_CUSTOM_VALUE_FIX_TEXT,    GADGET_ID_NONE,
1715     &custom_element.ce_value_fixed_initial,
1716     NULL,                               "CE value", NULL
1717   },
1718   {
1719     ED_COUNTER_ID_CUSTOM_VALUE_RND,
1720     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(10),
1721     0,                                  9999,
1722     GADGET_ID_CUSTOM_VALUE_RND_DOWN,    GADGET_ID_CUSTOM_VALUE_RND_UP,
1723     GADGET_ID_CUSTOM_VALUE_RND_TEXT,    GADGET_ID_CUSTOM_VALUE_FIX_UP,
1724     &custom_element.ce_value_random_initial,
1725     NULL,                               "+random", NULL
1726   },
1727   {
1728     ED_COUNTER_ID_PUSH_DELAY_FIX,
1729     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(6),
1730     0,                                  999,
1731     GADGET_ID_PUSH_DELAY_FIX_DOWN,      GADGET_ID_PUSH_DELAY_FIX_UP,
1732     GADGET_ID_PUSH_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1733     &custom_element.push_delay_fixed,
1734     NULL,                               "push delay", NULL
1735   },
1736   {
1737     ED_COUNTER_ID_PUSH_DELAY_RND,
1738     -1,                                 ED_ELEMENT_SETTINGS_YPOS(6),
1739     0,                                  999,
1740     GADGET_ID_PUSH_DELAY_RND_DOWN,      GADGET_ID_PUSH_DELAY_RND_UP,
1741     GADGET_ID_PUSH_DELAY_RND_TEXT,      GADGET_ID_PUSH_DELAY_FIX_UP,
1742     &custom_element.push_delay_random,
1743     NULL,                               "+random", NULL
1744   },
1745   {
1746     ED_COUNTER_ID_DROP_DELAY_FIX,
1747     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(7),
1748     0,                                  999,
1749     GADGET_ID_DROP_DELAY_FIX_DOWN,      GADGET_ID_DROP_DELAY_FIX_UP,
1750     GADGET_ID_DROP_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1751     &custom_element.drop_delay_fixed,
1752     NULL,                               "drop delay", NULL
1753   },
1754   {
1755     ED_COUNTER_ID_DROP_DELAY_RND,
1756     -1,                                 ED_ELEMENT_SETTINGS_YPOS(7),
1757     0,                                  999,
1758     GADGET_ID_DROP_DELAY_RND_DOWN,      GADGET_ID_DROP_DELAY_RND_UP,
1759     GADGET_ID_DROP_DELAY_RND_TEXT,      GADGET_ID_DROP_DELAY_FIX_UP,
1760     &custom_element.drop_delay_random,
1761     NULL,                               "+random", NULL
1762   },
1763
1764   // ---------- element settings: configure 2 (custom elements) ---------------
1765
1766   {
1767     ED_COUNTER_ID_MOVE_DELAY_FIX,
1768     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(5),
1769     0,                                  999,
1770     GADGET_ID_MOVE_DELAY_FIX_DOWN,      GADGET_ID_MOVE_DELAY_FIX_UP,
1771     GADGET_ID_MOVE_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1772     &custom_element.move_delay_fixed,
1773     NULL,                               "move delay", NULL
1774   },
1775   {
1776     ED_COUNTER_ID_MOVE_DELAY_RND,
1777     -1,                                 ED_ELEMENT_SETTINGS_YPOS(5),
1778     0,                                  999,
1779     GADGET_ID_MOVE_DELAY_RND_DOWN,      GADGET_ID_MOVE_DELAY_RND_UP,
1780     GADGET_ID_MOVE_DELAY_RND_TEXT,      GADGET_ID_MOVE_DELAY_FIX_UP,
1781     &custom_element.move_delay_random,
1782     NULL,                               "+random", NULL
1783   },
1784   {
1785     ED_COUNTER_ID_STEP_DELAY_FIX,
1786     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(6),
1787     0,                                  999,
1788     GADGET_ID_STEP_DELAY_FIX_DOWN,      GADGET_ID_STEP_DELAY_FIX_UP,
1789     GADGET_ID_STEP_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1790     &custom_element.step_delay_fixed,
1791     NULL,                               "step delay", NULL
1792   },
1793   {
1794     ED_COUNTER_ID_STEP_DELAY_RND,
1795     -1,                                 ED_ELEMENT_SETTINGS_YPOS(6),
1796     0,                                  999,
1797     GADGET_ID_STEP_DELAY_RND_DOWN,      GADGET_ID_STEP_DELAY_RND_UP,
1798     GADGET_ID_STEP_DELAY_RND_TEXT,      GADGET_ID_STEP_DELAY_FIX_UP,
1799     &custom_element.step_delay_random,
1800     NULL,                               "+random", NULL
1801   },
1802   {
1803     ED_COUNTER_ID_EXPLOSION_DELAY,
1804     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(13),
1805     0,                                  999,
1806     GADGET_ID_EXPLOSION_DELAY_DOWN,     GADGET_ID_EXPLOSION_DELAY_UP,
1807     GADGET_ID_EXPLOSION_DELAY_TEXT,     GADGET_ID_NONE,
1808     &custom_element.explosion_delay,
1809     NULL,                               "explosion delay", NULL
1810   },
1811   {
1812     ED_COUNTER_ID_IGNITION_DELAY,
1813     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(14),
1814     0,                                  999,
1815     GADGET_ID_IGNITION_DELAY_DOWN,      GADGET_ID_IGNITION_DELAY_UP,
1816     GADGET_ID_IGNITION_DELAY_TEXT,      GADGET_ID_NONE,
1817     &custom_element.ignition_delay,
1818     NULL,                               "ignition delay", "(by fire)"
1819   },
1820
1821   // ---------- element settings: configure (group elements) ------------------
1822
1823   {
1824     ED_COUNTER_ID_GROUP_CONTENT,
1825     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1826     MIN_ELEMENTS_IN_GROUP,              MAX_ELEMENTS_IN_GROUP,
1827     GADGET_ID_GROUP_CONTENT_DOWN,       GADGET_ID_GROUP_CONTENT_UP,
1828     GADGET_ID_GROUP_CONTENT_TEXT,       GADGET_ID_NONE,
1829     &group_element_info.num_elements,
1830     NULL,                               NULL, "number of elements in group"
1831   },
1832
1833   // ---------- element settings: advanced (custom elements) ------------------
1834
1835   {
1836     ED_COUNTER_ID_CHANGE_DELAY_FIX,
1837     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(2),
1838     0,                                  999,
1839     GADGET_ID_CHANGE_DELAY_FIX_DOWN,    GADGET_ID_CHANGE_DELAY_FIX_UP,
1840     GADGET_ID_CHANGE_DELAY_FIX_TEXT,    GADGET_ID_NONE,
1841     &custom_element_change.delay_fixed,
1842     NULL,                               "CE delay", NULL,
1843   },
1844   {
1845     ED_COUNTER_ID_CHANGE_DELAY_RND,
1846     -1,                                 ED_ELEMENT_SETTINGS_YPOS(2),
1847     0,                                  999,
1848     GADGET_ID_CHANGE_DELAY_RND_DOWN,    GADGET_ID_CHANGE_DELAY_RND_UP,
1849     GADGET_ID_CHANGE_DELAY_RND_TEXT,    GADGET_ID_CHANGE_DELAY_FIX_UP,
1850     &custom_element_change.delay_random,
1851     NULL,                               "+random", NULL
1852   },
1853   {
1854     ED_COUNTER_ID_CHANGE_CONT_RND,
1855     ED_ELEMENT_SETTINGS_XPOS(3),        ED_ELEMENT_SETTINGS_YPOS(12),
1856     0,                                  100,
1857     GADGET_ID_CHANGE_CONT_RND_DOWN,     GADGET_ID_CHANGE_CONT_RND_UP,
1858     GADGET_ID_CHANGE_CONT_RND_TEXT,     GADGET_ID_NONE,
1859     &custom_element_change.random_percentage,
1860     NULL,                               "use random replace:", "%"
1861   },
1862 };
1863
1864 static struct
1865 {
1866   int gadget_type_id;
1867   int x, y;
1868   int gadget_id;
1869   int size;
1870   char *value;
1871   char *text_above, *infotext;
1872 } textinput_info[ED_NUM_TEXTINPUT] =
1873 {
1874   {
1875     ED_TEXTINPUT_ID_LEVEL_NAME,
1876     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(0),
1877     GADGET_ID_LEVEL_NAME,
1878     MAX_LEVEL_NAME_LEN,
1879     level.name,
1880     "Title:", "Title for this level"
1881   },
1882   {
1883     ED_TEXTINPUT_ID_LEVEL_AUTHOR,
1884     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(2),
1885     GADGET_ID_LEVEL_AUTHOR,
1886     MAX_LEVEL_AUTHOR_LEN,
1887     level.author,
1888     "Author:", "Author for this level"
1889   },
1890   {
1891     ED_TEXTINPUT_ID_LEVELSET_NAME,
1892     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(0),
1893     GADGET_ID_LEVELSET_NAME,
1894     MAX_LEVEL_NAME_LEN,
1895     levelset_name,
1896     "Title:", "Title for this or new level set"
1897   },
1898   {
1899     ED_TEXTINPUT_ID_LEVELSET_AUTHOR,
1900     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(2),
1901     GADGET_ID_LEVELSET_AUTHOR,
1902     MAX_LEVEL_AUTHOR_LEN,
1903     levelset_author,
1904     "Author:", "Author for this or new level set"
1905   },
1906   {
1907     ED_TEXTINPUT_ID_ELEMENT_NAME,
1908     -1, -1,     // these values are not constant, but can change at runtime
1909     GADGET_ID_ELEMENT_NAME,
1910     MAX_ELEMENT_NAME_LEN - 2,           // currently 2 chars less editable
1911     custom_element.description,
1912     NULL, "Element name"
1913   }
1914 };
1915
1916 static struct
1917 {
1918   int gadget_type_id;
1919   int x, y;
1920   int gadget_id;
1921   int xsize, ysize;
1922   char *value;
1923   char *text_above, *text_above_cropped, *infotext;
1924 } textarea_info[ED_NUM_TEXTAREAS] =
1925 {
1926   {
1927     ED_TEXTAREA_ID_ENVELOPE_INFO,
1928     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1929     GADGET_ID_ENVELOPE_INFO,
1930     MAX_ENVELOPE_XSIZE, MAX_ENVELOPE_YSIZE,
1931     NULL,
1932     "Envelope Content:", "Envelope Content (cropped):", "Envelope Content"
1933   }
1934 };
1935
1936 static struct ValueTextInfo options_time_or_steps[] =
1937 {
1938   { 0,                          "seconds"                       },
1939   { 1,                          "steps"                         },
1940
1941   { -1,                         NULL                            }
1942 };
1943
1944 static struct ValueTextInfo options_time_score_base[] =
1945 {
1946   { 1,                          "per second/step"               },
1947   { 10,                         "per 10 seconds/steps"          },
1948
1949   { -1,                         NULL                            }
1950 };
1951
1952 static struct ValueTextInfo options_game_engine_type[] =
1953 {
1954   { GAME_ENGINE_TYPE_RND,       "Rocks'n'Diamonds"              },
1955   { GAME_ENGINE_TYPE_BD,        "Boulder Dash"                  },
1956   { GAME_ENGINE_TYPE_EM,        "Emerald Mine"                  },
1957   { GAME_ENGINE_TYPE_SP,        "Supaplex"                      },
1958   { GAME_ENGINE_TYPE_MM,        "Mirror Magic"                  },
1959
1960   { -1,                         NULL                            }
1961 };
1962
1963 static struct ValueTextInfo options_levelset_save_mode[] =
1964 {
1965   { LEVELSET_SAVE_MODE_UPDATE,  "Update this level set"         },
1966   { LEVELSET_SAVE_MODE_CREATE,  "Create new level set"          },
1967
1968   { -1,                         NULL                            }
1969 };
1970
1971 static struct ValueTextInfo options_wind_direction[] =
1972 {
1973   { MV_START_NONE,              "none"                          },
1974   { MV_START_LEFT,              "left"                          },
1975   { MV_START_RIGHT,             "right"                         },
1976   { MV_START_UP,                "up"                            },
1977   { MV_START_DOWN,              "down"                          },
1978
1979   { -1,                         NULL                            }
1980 };
1981
1982 static struct ValueTextInfo options_player_speed[] =
1983 {
1984   { 0,                          "frozen"                        },
1985   { 1,                          "very slow"                     },
1986   { 2,                          "slow"                          },
1987   { 4,                          "normal"                        },
1988   { 8,                          "fast"                          },
1989   { 16,                         "very fast"                     },
1990   { 32,                         "ultrafast"                     },
1991
1992   { -1,                         NULL                            }
1993 };
1994
1995 static struct ValueTextInfo options_access_type[] =
1996 {
1997   { EP_WALKABLE,                "walkable"                      },
1998   { EP_PASSABLE,                "passable"                      },
1999
2000   { -1,                         NULL                            }
2001 };
2002
2003 static struct ValueTextInfo options_access_layer[] =
2004 {
2005   { EP_ACCESSIBLE_OVER,         "over"                          },
2006   { EP_ACCESSIBLE_INSIDE,       "inside"                        },
2007   { EP_ACCESSIBLE_UNDER,        "under"                         },
2008
2009   { -1,                         NULL                            }
2010 };
2011
2012 static struct ValueTextInfo options_access_protected[] =
2013 {
2014   { 0,                          "unprotected"                   },
2015   { 1,                          "protected"                     },
2016
2017   { -1,                         NULL                            }
2018 };
2019
2020 static struct ValueTextInfo options_access_direction[] =
2021 {
2022   { MV_NO_DIRECTION,            "no direction"                  },
2023   { MV_LEFT,                    "left"                          },
2024   { MV_RIGHT,                   "right"                         },
2025   { MV_UP,                      "up"                            },
2026   { MV_DOWN,                    "down"                          },
2027   { MV_LEFT  | MV_UP,           "left + up"                     },
2028   { MV_LEFT  | MV_DOWN,         "left + down"                   },
2029   { MV_RIGHT | MV_UP,           "right + up"                    },
2030   { MV_RIGHT | MV_DOWN,         "right + down"                  },
2031   { MV_HORIZONTAL,              "horizontal"                    },
2032   { MV_VERTICAL,                "vertical"                      },
2033   { MV_HORIZONTAL | MV_UP,      "horizontal + up"               },
2034   { MV_HORIZONTAL | MV_DOWN,    "horizontal + down"             },
2035   { MV_VERTICAL   | MV_LEFT,    "vertical + left"               },
2036   { MV_VERTICAL   | MV_RIGHT,   "vertical + right"              },
2037   { MV_ALL_DIRECTIONS,          "all directions"                },
2038
2039   { -1,                         NULL                            }
2040 };
2041
2042 static struct ValueTextInfo options_walk_to_action[] =
2043 {
2044   { EP_DIGGABLE,                "diggable"                      },
2045   { EP_COLLECTIBLE_ONLY,        "collectible"                   },
2046   { EP_DROPPABLE,               "collectible & droppable"       },
2047   { EP_THROWABLE,               "collectible & throwable"       },
2048   { EP_PUSHABLE,                "pushable"                      },
2049
2050   { -1,                         NULL                            }
2051 };
2052
2053 static struct ValueTextInfo options_move_pattern[] =
2054 {
2055   { MV_LEFT,                    "left"                          },
2056   { MV_RIGHT,                   "right"                         },
2057   { MV_UP,                      "up"                            },
2058   { MV_DOWN,                    "down"                          },
2059   { MV_HORIZONTAL,              "horizontal"                    },
2060   { MV_VERTICAL,                "vertical"                      },
2061   { MV_ALL_DIRECTIONS,          "all directions"                },
2062   { MV_WIND_DIRECTION,          "wind direction"                },
2063   { MV_TOWARDS_PLAYER,          "towards player"                },
2064   { MV_AWAY_FROM_PLAYER,        "away from player"              },
2065   { MV_ALONG_LEFT_SIDE,         "along left side"               },
2066   { MV_ALONG_RIGHT_SIDE,        "along right side"              },
2067   { MV_TURNING_LEFT,            "turning left"                  },
2068   { MV_TURNING_RIGHT,           "turning right"                 },
2069   { MV_TURNING_LEFT_RIGHT,      "turning left, right"           },
2070   { MV_TURNING_RIGHT_LEFT,      "turning right, left"           },
2071   { MV_TURNING_RANDOM,          "turning random"                },
2072   { MV_MAZE_RUNNER,             "maze runner style"             },
2073   { MV_MAZE_HUNTER,             "maze hunter style"             },
2074   { MV_WHEN_PUSHED,             "when pushed"                   },
2075   { MV_WHEN_DROPPED,            "when dropped/thrown"           },
2076
2077   { -1,                         NULL                            }
2078 };
2079
2080 static struct ValueTextInfo options_move_direction[] =
2081 {
2082   { MV_START_AUTOMATIC,         "automatic"                     },
2083   { MV_START_LEFT,              "left"                          },
2084   { MV_START_RIGHT,             "right"                         },
2085   { MV_START_UP,                "up"                            },
2086   { MV_START_DOWN,              "down"                          },
2087   { MV_START_RANDOM,            "random"                        },
2088   { MV_START_PREVIOUS,          "previous"                      },
2089
2090   { -1,                         NULL                            }
2091 };
2092
2093 static struct ValueTextInfo options_move_stepsize[] =
2094 {
2095   { 0,                          "not moving"                    },
2096   { 1,                          "very slow"                     },
2097   { 2,                          "slow"                          },
2098   { 4,                          "normal"                        },
2099   { 8,                          "fast"                          },
2100   { 16,                         "very fast"                     },
2101   { 32,                         "even faster"                   },
2102
2103   { -1,                         NULL                            }
2104 };
2105
2106 static struct ValueTextInfo options_move_leave_type[] =
2107 {
2108   { LEAVE_TYPE_UNLIMITED,       "leave behind"                  },
2109   { LEAVE_TYPE_LIMITED,         "change it to"                  },
2110
2111   { -1,                         NULL                            }
2112 };
2113
2114 static struct ValueTextInfo options_smash_targets[] =
2115 {
2116   { EP_CAN_SMASH_PLAYER,        "player"                        },
2117 #if 0
2118   { EP_CAN_SMASH_ENEMIES,       "enemies"                       },
2119 #endif
2120   { EP_CAN_SMASH_EVERYTHING,    "everything"                    },
2121
2122   { -1,                         NULL                            }
2123 };
2124
2125 static struct ValueTextInfo options_slippery_type[] =
2126 {
2127   { SLIPPERY_ANY_RANDOM,        "random"                        },
2128   { SLIPPERY_ANY_LEFT_RIGHT,    "left, right"                   },
2129   { SLIPPERY_ANY_RIGHT_LEFT,    "right, left"                   },
2130   { SLIPPERY_ONLY_LEFT,         "only left"                     },
2131   { SLIPPERY_ONLY_RIGHT,        "only right"                    },
2132
2133   { -1,                         NULL                            }
2134 };
2135
2136 static struct ValueTextInfo options_deadliness[] =
2137 {
2138   { EP_DONT_RUN_INTO,           "running into"                  },
2139   { EP_DONT_COLLIDE_WITH,       "colliding with"                },
2140   { EP_DONT_GET_HIT_BY,         "getting hit by"                },
2141   { EP_DONT_TOUCH,              "touching"                      },
2142
2143   { -1,                         NULL                            }
2144 };
2145
2146 static struct ValueTextInfo options_explosion_type[] =
2147 {
2148   { EXPLODES_3X3,               "3x3"                           },
2149   { EXPLODES_CROSS,             "3+3"                           },
2150   { EXPLODES_1X1,               "1x1"                           },
2151
2152   { -1,                         NULL                            }
2153 };
2154
2155 static struct ValueTextInfo options_time_units[] =
2156 {
2157   { 1,                          "frames"                        },
2158   { FRAMES_PER_SECOND,          "seconds"                       },
2159
2160   { -1,                         NULL                            }
2161 };
2162
2163 static struct ValueTextInfo options_change_direct_action[] =
2164 {
2165   { CE_TOUCHED_BY_PLAYER,       "touched by player"             },
2166   { CE_PRESSED_BY_PLAYER,       "pressed by player"             },
2167   { CE_SWITCHED_BY_PLAYER,      "switched by player"            },
2168   { CE_SNAPPED_BY_PLAYER,       "snapped by player"             },
2169   { CE_PUSHED_BY_PLAYER,        "pushed by player"              },
2170   { CE_ENTERED_BY_PLAYER,       "entered by player"             },
2171   { CE_LEFT_BY_PLAYER,          "left by player"                },
2172   { CE_DROPPED_BY_PLAYER,       "dropped/thrown by player"      },
2173   { CE_SWITCHED,                "switched"                      },
2174   { CE_HITTING_SOMETHING,       "hitting something"             },
2175   { CE_HIT_BY_SOMETHING,        "hit by something"              },
2176 #if 0
2177   { CE_BLOCKED,                 "blocked"                       },
2178 #endif
2179   { CE_IMPACT,                  "impact (on something)"         },
2180   { CE_SMASHED,                 "smashed (from above)"          },
2181 #if 0
2182   { CE_VALUE_CHANGES,           "CE value changes"              },
2183   { CE_SCORE_CHANGES,           "CE score changes"              },
2184 #endif
2185   { CE_VALUE_GETS_ZERO,         "CE value gets 0"               },
2186   { CE_SCORE_GETS_ZERO,         "CE score gets 0"               },
2187   { CE_UNDEFINED,               " "                             },
2188   { CE_HEADLINE_SPECIAL_EVENTS, "[mouse events]"                },
2189   { CE_CLICKED_BY_MOUSE,        "clicked by mouse"              },
2190   { CE_PRESSED_BY_MOUSE,        "pressed by mouse"              },
2191   { CE_UNDEFINED,               " "                             },
2192   { CE_HEADLINE_SPECIAL_EVENTS, "[static states]"               },
2193   { CE_NEXT_TO_PLAYER,          "next to player"                },
2194
2195   { -1,                         NULL                            }
2196 };
2197
2198 static struct ValueTextInfo options_change_other_action[] =
2199 {
2200   { CE_PLAYER_TOUCHES_X,        "player touches"                },
2201   { CE_PLAYER_PRESSES_X,        "player presses"                },
2202   { CE_PLAYER_SWITCHES_X,       "player switches"               },
2203   { CE_PLAYER_SNAPS_X,          "player snaps"                  },
2204   { CE_PLAYER_PUSHES_X,         "player pushes"                 },
2205   { CE_PLAYER_ENTERS_X,         "player enters"                 },
2206   { CE_PLAYER_LEAVES_X,         "player leaves"                 },
2207   { CE_PLAYER_DIGS_X,           "player digs"                   },
2208   { CE_PLAYER_COLLECTS_X,       "player collects"               },
2209   { CE_PLAYER_DROPS_X,          "player drops/throws"           },
2210   { CE_TOUCHING_X,              "touching"                      },
2211   { CE_HITTING_X,               "hitting"                       },
2212   { CE_DIGGING_X,               "digging"                       },
2213   { CE_HIT_BY_X,                "hit by"                        },
2214   { CE_SWITCH_OF_X,             "switch of"                     },
2215   { CE_CHANGE_OF_X,             "change by page of"             },
2216   { CE_EXPLOSION_OF_X,          "explosion of"                  },
2217   { CE_MOVE_OF_X,               "move of"                       },
2218   { CE_CREATION_OF_X,           "creation of"                   },
2219   { CE_VALUE_CHANGES_OF_X,      "CE value changes of"           },
2220   { CE_SCORE_CHANGES_OF_X,      "CE score changes of"           },
2221   { CE_VALUE_GETS_ZERO_OF_X,    "CE value gets 0 of"            },
2222   { CE_SCORE_GETS_ZERO_OF_X,    "CE score gets 0 of"            },
2223   { CE_UNDEFINED,               " "                             },
2224   { CE_HEADLINE_SPECIAL_EVENTS, "[mouse events]"                },
2225   { CE_MOUSE_CLICKED_ON_X,      "mouse clicked on"              },
2226   { CE_MOUSE_PRESSED_ON_X,      "mouse pressed on"              },
2227   { CE_UNDEFINED,               " "                             },
2228   { CE_HEADLINE_SPECIAL_EVENTS, "[static states]"               },
2229   { CE_PLAYER_NEXT_TO_X,        "player next to"                },
2230   { CE_NEXT_TO_X,               "next to"                       },
2231
2232   { -1,                         NULL                            }
2233 };
2234
2235 static struct ValueTextInfo options_change_trigger_side[] =
2236 {
2237   { CH_SIDE_LEFT,               "left"                          },
2238   { CH_SIDE_RIGHT,              "right"                         },
2239   { CH_SIDE_TOP,                "top"                           },
2240   { CH_SIDE_BOTTOM,             "bottom"                        },
2241   { CH_SIDE_LEFT_RIGHT,         "left/right"                    },
2242   { CH_SIDE_TOP_BOTTOM,         "top/bottom"                    },
2243   { CH_SIDE_ANY,                "any"                           },
2244
2245   { -1,                         NULL                            }
2246 };
2247
2248 static struct ValueTextInfo options_change_trigger_player[] =
2249 {
2250   { CH_PLAYER_1,                "1"                             },
2251   { CH_PLAYER_2,                "2"                             },
2252   { CH_PLAYER_3,                "3"                             },
2253   { CH_PLAYER_4,                "4"                             },
2254   { CH_PLAYER_ANY,              "any"                           },
2255
2256   { -1,                         NULL                            }
2257 };
2258
2259 static struct ValueTextInfo options_change_trigger_page[] =
2260 {
2261   { (1u << 0),                  "1"                             },
2262   { (1u << 1),                  "2"                             },
2263   { (1u << 2),                  "3"                             },
2264   { (1u << 3),                  "4"                             },
2265   { (1u << 4),                  "5"                             },
2266   { (1u << 5),                  "6"                             },
2267   { (1u << 6),                  "7"                             },
2268   { (1u << 7),                  "8"                             },
2269   { (1u << 8),                  "9"                             },
2270   { (1u << 9),                  "10"                            },
2271   { (1u << 10),                 "11"                            },
2272   { (1u << 11),                 "12"                            },
2273   { (1u << 12),                 "13"                            },
2274   { (1u << 13),                 "14"                            },
2275   { (1u << 14),                 "15"                            },
2276   { (1u << 15),                 "16"                            },
2277   { (1u << 16),                 "17"                            },
2278   { (1u << 17),                 "18"                            },
2279   { (1u << 18),                 "19"                            },
2280   { (1u << 19),                 "20"                            },
2281   { (1u << 20),                 "21"                            },
2282   { (1u << 21),                 "22"                            },
2283   { (1u << 22),                 "23"                            },
2284   { (1u << 23),                 "24"                            },
2285   { (1u << 24),                 "25"                            },
2286   { (1u << 25),                 "26"                            },
2287   { (1u << 26),                 "27"                            },
2288   { (1u << 27),                 "28"                            },
2289   { (1u << 28),                 "29"                            },
2290   { (1u << 29),                 "30"                            },
2291   { (1u << 30),                 "31"                            },
2292   { (1u << 31),                 "32"                            },
2293   { CH_PAGE_ANY,                "any"                           },
2294
2295   { -1,                         NULL                            }
2296 };
2297
2298 static struct ValueTextInfo options_change_replace_when[] =
2299 {
2300   { CP_WHEN_EMPTY,              "empty"                         },
2301   { CP_WHEN_WALKABLE,           "walkable"                      },
2302   { CP_WHEN_DIGGABLE,           "diggable"                      },
2303   { CP_WHEN_COLLECTIBLE,        "collectible"                   },
2304   { CP_WHEN_REMOVABLE,          "removable"                     },
2305   { CP_WHEN_DESTRUCTIBLE,       "destructible"                  },
2306
2307   { -1,                         NULL                            }
2308 };
2309
2310 static struct ValueTextInfo options_action_type[] =
2311 {
2312   { CA_NO_ACTION,               "no action"                     },
2313   { CA_UNDEFINED,               " "                             },
2314   { CA_HEADLINE_LEVEL_ACTIONS,  "[level]"                       },
2315   { CA_RESTART_LEVEL,           "restart level"                 },
2316   { CA_SHOW_ENVELOPE,           "show envelope"                 },
2317   { CA_SET_LEVEL_TIME,          "set time"                      },
2318   { CA_SET_LEVEL_SCORE,         "set score"                     },
2319   { CA_SET_LEVEL_GEMS,          "set gems"                      },
2320   { CA_SET_LEVEL_WIND,          "set wind dir."                 },
2321   { CA_SET_LEVEL_RANDOM_SEED,   "set random seed"               },
2322   { CA_UNDEFINED,               " "                             },
2323   { CA_HEADLINE_PLAYER_ACTIONS, "[player]"                      },
2324   { CA_MOVE_PLAYER,             "move player"                   },
2325   { CA_MOVE_PLAYER_NEW,         "move player new"               },
2326   { CA_EXIT_PLAYER,             "exit player"                   },
2327   { CA_KILL_PLAYER,             "kill player"                   },
2328   { CA_SET_PLAYER_KEYS,         "set keys"                      },
2329   { CA_SET_PLAYER_SPEED,        "set speed"                     },
2330   { CA_SET_PLAYER_SHIELD,       "set shield"                    },
2331   { CA_SET_PLAYER_GRAVITY,      "set gravity"                   },
2332   { CA_SET_PLAYER_ARTWORK,      "set artwork"                   },
2333   { CA_SET_PLAYER_INVENTORY,    "set inventory"                 },
2334   { CA_UNDEFINED,               " "                             },
2335   { CA_HEADLINE_CE_ACTIONS,     "[CE]"                          },
2336   { CA_SET_CE_VALUE,            "set CE value"                  },
2337   { CA_SET_CE_SCORE,            "set CE score"                  },
2338   { CA_SET_CE_ARTWORK,          "set CE artwork"                },
2339   { CA_UNDEFINED,               " "                             },
2340   { CA_HEADLINE_ENGINE_ACTIONS, "[engine]"                      },
2341   { CA_SET_ENGINE_SCAN_MODE,    "set scan mode"                 },
2342
2343   { -1,                         NULL                            }
2344 };
2345
2346 static struct ValueTextInfo options_action_mode_none[] =
2347 {
2348   { CA_MODE_UNDEFINED,          " "                             },
2349
2350   { -1,                         NULL                            }
2351 };
2352
2353 static struct ValueTextInfo options_action_mode_assign[] =
2354 {
2355   { CA_MODE_SET,                "="                             },
2356
2357   { -1,                         NULL                            }
2358 };
2359
2360 static struct ValueTextInfo options_action_mode_add_remove[] =
2361 {
2362   { CA_MODE_ADD,                "+"                             },
2363   { CA_MODE_SUBTRACT,           "-"                             },
2364
2365   { -1,                         NULL                            }
2366 };
2367
2368 static struct ValueTextInfo options_action_mode_calculate[] =
2369 {
2370   { CA_MODE_SET,                "="                             },
2371   { CA_MODE_ADD,                "+"                             },
2372   { CA_MODE_SUBTRACT,           "-"                             },
2373   { CA_MODE_MULTIPLY,           "*"                             },
2374   { CA_MODE_DIVIDE,             "/"                             },
2375   { CA_MODE_MODULO,             "%"                             },
2376
2377   { -1,                         NULL                            }
2378 };
2379
2380 static struct ValueTextInfo options_action_arg_none[] =
2381 {
2382   { CA_ARG_UNDEFINED,           "         "                     },
2383
2384   { -1,                         NULL                            }
2385 };
2386
2387 static struct ValueTextInfo options_action_arg_player[] =
2388 {
2389   { CA_ARG_PLAYER_HEADLINE,     "[player]"                      },
2390   { CA_ARG_PLAYER_1,            "1"                             },
2391   { CA_ARG_PLAYER_2,            "2"                             },
2392   { CA_ARG_PLAYER_3,            "3"                             },
2393   { CA_ARG_PLAYER_4,            "4"                             },
2394   { CA_ARG_PLAYER_ANY,          "any"                           },
2395   { CA_ARG_PLAYER_TRIGGER,      "trigger"                       },
2396   { CA_ARG_PLAYER_ACTION,       "action ->"                     },
2397
2398   { -1,                         NULL                            }
2399 };
2400
2401 static struct ValueTextInfo options_action_arg_number[] =
2402 {
2403   { CA_ARG_NUMBER_HEADLINE,     "[number]"                      },
2404   { CA_ARG_0,                   "0"                             },
2405   { CA_ARG_1,                   "1"                             },
2406   { CA_ARG_2,                   "2"                             },
2407   { CA_ARG_3,                   "3"                             },
2408   { CA_ARG_4,                   "4"                             },
2409   { CA_ARG_5,                   "5"                             },
2410   { CA_ARG_10,                  "10"                            },
2411   { CA_ARG_100,                 "100"                           },
2412   { CA_ARG_1000,                "1000"                          },
2413   { CA_ARG_UNDEFINED,           " "                             },
2414   { CA_ARG_NUMBER_MIN,          "min"                           },
2415   { CA_ARG_NUMBER_MAX,          "max"                           },
2416   { CA_ARG_UNDEFINED,           " "                             },
2417   { CA_ARG_NUMBER_RESET,        "reset"                         },
2418   { CA_ARG_UNDEFINED,           " "                             },
2419   { CA_ARG_NUMBER_CE_VALUE,     "CE value"                      },
2420   { CA_ARG_NUMBER_CE_SCORE,     "CE score"                      },
2421   { CA_ARG_NUMBER_CE_DELAY,     "CE delay"                      },
2422   { CA_ARG_UNDEFINED,           " "                             },
2423   { CA_ARG_NUMBER_LEVEL_TIME,   "time"                          },
2424   { CA_ARG_NUMBER_LEVEL_GEMS,   "gems"                          },
2425   { CA_ARG_NUMBER_LEVEL_SCORE,  "score"                         },
2426   { CA_ARG_UNDEFINED,           " "                             },
2427   { CA_ARG_ELEMENT_CV_HEADLINE, "[CE value]"                    },
2428   { CA_ARG_ELEMENT_CV_TARGET,   "target"                        },
2429   { CA_ARG_ELEMENT_CV_TRIGGER,  "trigger"                       },
2430   { CA_ARG_ELEMENT_CV_ACTION,   "action ->"                     },
2431   { CA_ARG_UNDEFINED,           " "                             },
2432   { CA_ARG_ELEMENT_CS_HEADLINE, "[CE score]"                    },
2433   { CA_ARG_ELEMENT_CS_TARGET,   "target"                        },
2434   { CA_ARG_ELEMENT_CS_TRIGGER,  "trigger"                       },
2435   { CA_ARG_ELEMENT_CS_ACTION,   "action ->"                     },
2436
2437   { -1,                         NULL                            }
2438 };
2439
2440 static struct ValueTextInfo options_action_arg_value[] =
2441 {
2442   { CA_ARG_NUMBER_HEADLINE,     "[number]"                      },
2443   { CA_ARG_0,                   "0"                             },
2444   { CA_ARG_1,                   "1"                             },
2445   { CA_ARG_2,                   "2"                             },
2446   { CA_ARG_3,                   "3"                             },
2447   { CA_ARG_4,                   "4"                             },
2448   { CA_ARG_5,                   "5"                             },
2449   { CA_ARG_10,                  "10"                            },
2450   { CA_ARG_100,                 "100"                           },
2451   { CA_ARG_1000,                "1000"                          },
2452   { CA_ARG_UNDEFINED,           " "                             },
2453   { CA_ARG_NUMBER_MIN,          "min"                           },
2454   { CA_ARG_NUMBER_MAX,          "max"                           },
2455   { CA_ARG_UNDEFINED,           " "                             },
2456   { CA_ARG_NUMBER_RESET,        "reset"                         },
2457   { CA_ARG_UNDEFINED,           " "                             },
2458   { CA_ARG_NUMBER_CE_VALUE,     "CE value"                      },
2459   { CA_ARG_NUMBER_CE_SCORE,     "CE score"                      },
2460   { CA_ARG_NUMBER_CE_DELAY,     "CE delay"                      },
2461   { CA_ARG_UNDEFINED,           " "                             },
2462   { CA_ARG_NUMBER_LEVEL_TIME,   "time"                          },
2463   { CA_ARG_NUMBER_LEVEL_GEMS,   "gems"                          },
2464   { CA_ARG_NUMBER_LEVEL_SCORE,  "score"                         },
2465   { CA_ARG_UNDEFINED,           " "                             },
2466   { CA_ARG_ELEMENT_CV_HEADLINE, "[CE value]"                    },
2467   { CA_ARG_ELEMENT_CV_TARGET,   "target"                        },
2468   { CA_ARG_ELEMENT_CV_TRIGGER,  "trigger"                       },
2469   { CA_ARG_ELEMENT_CV_ACTION,   "action ->"                     },
2470   { CA_ARG_UNDEFINED,           " "                             },
2471   { CA_ARG_ELEMENT_CS_HEADLINE, "[CE score]"                    },
2472   { CA_ARG_ELEMENT_CS_TARGET,   "target"                        },
2473   { CA_ARG_ELEMENT_CS_TRIGGER,  "trigger"                       },
2474   { CA_ARG_ELEMENT_CS_ACTION,   "action ->"                     },
2475   { CA_ARG_UNDEFINED,           " "                             },
2476   { CA_ARG_ELEMENT_NR_HEADLINE, "[element]"                     },
2477   { CA_ARG_ELEMENT_NR_TARGET,   "target"                        },
2478   { CA_ARG_ELEMENT_NR_TRIGGER,  "trigger"                       },
2479   { CA_ARG_ELEMENT_NR_ACTION,   "action ->"                     },
2480
2481   { -1,                         NULL                            }
2482 };
2483
2484 static struct ValueTextInfo options_action_arg_envelope[] =
2485 {
2486   { CA_ARG_NUMBER_HEADLINE,     "[number]"                      },
2487   { CA_ARG_1,                   "1"                             },
2488   { CA_ARG_2,                   "2"                             },
2489   { CA_ARG_3,                   "3"                             },
2490   { CA_ARG_4,                   "4"                             },
2491   { CA_ARG_UNDEFINED,           " "                             },
2492   { CA_ARG_ELEMENT_HEADLINE,    "[element]"                     },
2493   { CA_ARG_ELEMENT_TARGET,      "target"                        },
2494   { CA_ARG_ELEMENT_TRIGGER,     "trigger"                       },
2495   { CA_ARG_ELEMENT_ACTION,      "action ->"                     },
2496
2497   { -1,                         NULL                            }
2498 };
2499
2500 static struct ValueTextInfo options_action_arg_key[] =
2501 {
2502   { CA_ARG_NUMBER_HEADLINE,     "[number]"                      },
2503   { CA_ARG_1,                   "1"                             },
2504   { CA_ARG_2,                   "2"                             },
2505   { CA_ARG_3,                   "3"                             },
2506   { CA_ARG_4,                   "4"                             },
2507   { CA_ARG_5,                   "5"                             },
2508   { CA_ARG_6,                   "6"                             },
2509   { CA_ARG_7,                   "7"                             },
2510   { CA_ARG_8,                   "8"                             },
2511   { CA_ARG_UNDEFINED,           " "                             },
2512   { CA_ARG_ELEMENT_HEADLINE,    "[element]"                     },
2513   { CA_ARG_ELEMENT_TARGET,      "target"                        },
2514   { CA_ARG_ELEMENT_TRIGGER,     "trigger"                       },
2515   { CA_ARG_ELEMENT_ACTION,      "action ->"                     },
2516
2517   { -1,                         NULL                            }
2518 };
2519
2520 static struct ValueTextInfo options_action_arg_speed[] =
2521 {
2522   { CA_ARG_SPEED_HEADLINE,      "[speed]"                       },
2523   { CA_ARG_SPEED_NOT_MOVING,    "frozen"                        },
2524   { CA_ARG_SPEED_VERY_SLOW,     "very slow"                     },
2525   { CA_ARG_SPEED_SLOW,          "slow"                          },
2526   { CA_ARG_SPEED_NORMAL,        "normal"                        },
2527   { CA_ARG_SPEED_FAST,          "fast"                          },
2528   { CA_ARG_SPEED_VERY_FAST,     "very fast"                     },
2529   { CA_ARG_SPEED_EVEN_FASTER,   "ultrafast"                     },
2530   { CA_ARG_UNDEFINED,           " "                             },
2531   { CA_ARG_SPEED_SLOWER,        "slower"                        },
2532   { CA_ARG_SPEED_FASTER,        "faster"                        },
2533   { CA_ARG_UNDEFINED,           " "                             },
2534   { CA_ARG_SPEED_RESET,         "reset"                         },
2535
2536   { -1,                         NULL                            }
2537 };
2538
2539 static struct ValueTextInfo options_action_arg_shield[] =
2540 {
2541   { CA_ARG_SHIELD_HEADLINE,     "[shield]"                      },
2542   { CA_ARG_SHIELD_OFF,          "off"                           },
2543   { CA_ARG_SHIELD_NORMAL,       "normal"                        },
2544   { CA_ARG_SHIELD_DEADLY,       "deadly"                        },
2545
2546   { -1,                         NULL                            }
2547 };
2548
2549 static struct ValueTextInfo options_action_arg_artwork[] =
2550 {
2551   { CA_ARG_ELEMENT_HEADLINE,    "[element]"                     },
2552   { CA_ARG_ELEMENT_TARGET,      "target"                        },
2553   { CA_ARG_ELEMENT_TRIGGER,     "trigger"                       },
2554   { CA_ARG_ELEMENT_ACTION,      "action ->"                     },
2555   { CA_ARG_UNDEFINED,           " "                             },
2556   { CA_ARG_ELEMENT_RESET,       "reset"                         },
2557
2558   { -1,                         NULL                            }
2559 };
2560
2561 static struct ValueTextInfo options_action_arg_gravity[] =
2562 {
2563   { CA_ARG_GRAVITY_HEADLINE,    "[gravity]"                     },
2564   { CA_ARG_GRAVITY_ON,          "on"                            },
2565   { CA_ARG_GRAVITY_OFF,         "off"                           },
2566   { CA_ARG_GRAVITY_TOGGLE,      "toggle"                        },
2567
2568   { -1,                         NULL                            }
2569 };
2570
2571 static struct ValueTextInfo options_action_arg_direction[] =
2572 {
2573   { CA_ARG_DIRECTION_HEADLINE,  "[dir.]"                        },
2574   { CA_ARG_DIRECTION_NONE,      "none"                          },
2575   { CA_ARG_DIRECTION_LEFT,      "left"                          },
2576   { CA_ARG_DIRECTION_RIGHT,     "right"                         },
2577   { CA_ARG_DIRECTION_UP,        "up"                            },
2578   { CA_ARG_DIRECTION_DOWN,      "down"                          },
2579   { CA_ARG_DIRECTION_TRIGGER,   "trigger"                       },
2580   { CA_ARG_DIRECTION_TRIGGER_BACK, "-trigger"                   },
2581
2582   { -1,                         NULL                            }
2583 };
2584
2585 static struct ValueTextInfo options_action_arg_scan_mode[] =
2586 {
2587   { CA_ARG_SCAN_MODE_HEADLINE,  "[mode]"                        },
2588   { CA_ARG_SCAN_MODE_NORMAL,    "normal"                        },
2589   { CA_ARG_SCAN_MODE_REVERSE,   "reverse"                       },
2590
2591   { -1,                         NULL                            }
2592 };
2593
2594 static struct ValueTextInfo options_action_arg_inventory[] =
2595 {
2596   { CA_ARG_INVENTORY_HEADLINE,  "[add]"                         },
2597   { CA_ARG_ELEMENT_TARGET,      "+ target"                      },
2598   { CA_ARG_ELEMENT_TRIGGER,     "+ trigger"                     },
2599   { CA_ARG_ELEMENT_ACTION,      "+ action"                      },
2600   { CA_ARG_UNDEFINED,           " "                             },
2601   { CA_ARG_INVENTORY_RM_HEADLINE,"[remove]"                     },
2602   { CA_ARG_INVENTORY_RM_TARGET, "- target"                      },
2603   { CA_ARG_INVENTORY_RM_TRIGGER,"- trigger"                     },
2604   { CA_ARG_INVENTORY_RM_ACTION, "- action"                      },
2605   { CA_ARG_INVENTORY_RM_FIRST,  "- first"                       },
2606   { CA_ARG_INVENTORY_RM_LAST,   "- last"                        },
2607   { CA_ARG_INVENTORY_RM_ALL,    "- all"                         },
2608   { CA_ARG_UNDEFINED,           " "                             },
2609   { CA_ARG_INVENTORY_RESET,     "reset"                         },
2610
2611   { -1,                         NULL                            }
2612 };
2613
2614 static char options_change_page_strings[MAX_CHANGE_PAGES][10];
2615 static struct ValueTextInfo options_change_page[MAX_CHANGE_PAGES + 1] =
2616 {
2617   { -1,                         NULL                            }
2618 };
2619
2620 static struct ValueTextInfo options_group_choice_mode[] =
2621 {
2622   { ANIM_RANDOM,                "random"                        },
2623   { ANIM_LOOP,                  "loop"                          },
2624   { ANIM_LINEAR,                "linear"                        },
2625   { ANIM_PINGPONG,              "pingpong"                      },
2626   { ANIM_PINGPONG2,             "pingpong 2"                    },
2627   { ANIM_LEVEL_NR,              "level number"                  },
2628
2629   { -1,                         NULL                            }
2630 };
2631
2632 static struct ValueTextInfo options_bd_scheduling_type[] =
2633 {
2634   { GD_SCHEDULING_MILLISECONDS,   "Milliseconds"                },
2635   { GD_SCHEDULING_BD1,            "BD1"                         },
2636   { GD_SCHEDULING_BD2,            "BD2"                         },
2637   { GD_SCHEDULING_PLCK,           "Construction Kit"            },
2638   { GD_SCHEDULING_CRDR,           "Crazy Dream 7"               },
2639   { GD_SCHEDULING_BD1_ATARI,      "Atari BD1"                   },
2640   { GD_SCHEDULING_BD2_PLCK_ATARI, "Atari BD2 / PLCK"            },
2641
2642   { -1,                           NULL                          }
2643 };
2644
2645 static struct ValueTextInfo *action_arg_modes[] =
2646 {
2647   options_action_mode_none,
2648   options_action_mode_assign,
2649   options_action_mode_add_remove,
2650   options_action_mode_calculate,
2651 };
2652
2653 static struct
2654 {
2655   int value;
2656   int mode;
2657   struct ValueTextInfo *options;
2658 }
2659 action_arg_options[] =
2660 {
2661   { CA_NO_ACTION,               0,      options_action_arg_none,        },
2662   { CA_EXIT_PLAYER,             0,      options_action_arg_player,      },
2663   { CA_KILL_PLAYER,             0,      options_action_arg_player,      },
2664   { CA_MOVE_PLAYER,             0,      options_action_arg_direction,   },
2665   { CA_MOVE_PLAYER_NEW,         0,      options_action_arg_direction,   },
2666   { CA_RESTART_LEVEL,           0,      options_action_arg_none,        },
2667   { CA_SHOW_ENVELOPE,           0,      options_action_arg_envelope,    },
2668   { CA_SET_LEVEL_TIME,          3,      options_action_arg_number,      },
2669   { CA_SET_LEVEL_GEMS,          3,      options_action_arg_number,      },
2670   { CA_SET_LEVEL_SCORE,         3,      options_action_arg_number,      },
2671   { CA_SET_LEVEL_WIND,          1,      options_action_arg_direction,   },
2672   { CA_SET_LEVEL_RANDOM_SEED,   1,      options_action_arg_number,      },
2673   { CA_SET_PLAYER_KEYS,         2,      options_action_arg_key,         },
2674   { CA_SET_PLAYER_SPEED,        1,      options_action_arg_speed,       },
2675   { CA_SET_PLAYER_SHIELD,       1,      options_action_arg_shield,      },
2676   { CA_SET_PLAYER_GRAVITY,      1,      options_action_arg_gravity,     },
2677   { CA_SET_PLAYER_ARTWORK,      1,      options_action_arg_artwork,     },
2678   { CA_SET_PLAYER_INVENTORY,    0,      options_action_arg_inventory,   },
2679   { CA_SET_CE_VALUE,            3,      options_action_arg_value,       },
2680   { CA_SET_CE_SCORE,            3,      options_action_arg_value,       },
2681   { CA_SET_CE_ARTWORK,          1,      options_action_arg_artwork,     },
2682   { CA_SET_ENGINE_SCAN_MODE,    1,      options_action_arg_scan_mode,   },
2683
2684   { -1,                         FALSE,  NULL                            }
2685 };
2686
2687 static struct
2688 {
2689   int gadget_type_id;
2690   int x, y;
2691   int gadget_id;
2692   int gadget_id_align;
2693   int size;     // char size of selectbox or '-1' (dynamically determined)
2694   struct ValueTextInfo *options;
2695   int *value;
2696   char *text_above, *text_left, *text_right, *infotext;
2697 } selectbox_info[ED_NUM_SELECTBOX] =
2698 {
2699   // ---------- level and editor settings -------------------------------------
2700
2701   {
2702     ED_SELECTBOX_ID_TIME_OR_STEPS,
2703     -1,                                 ED_LEVEL_SETTINGS_YPOS(8),
2704     GADGET_ID_TIME_OR_STEPS,            GADGET_ID_LEVEL_TIMELIMIT_UP,
2705     -1,
2706     options_time_or_steps,
2707     &level.use_step_counter,
2708     NULL, NULL, "(0 => no limit)",      "time or step limit"
2709   },
2710   {
2711     ED_SELECTBOX_ID_TIME_SCORE_BASE,
2712     -1,                                 ED_LEVEL_SETTINGS_YPOS(10),
2713     GADGET_ID_TIME_SCORE_BASE,          GADGET_ID_LEVEL_TIMESCORE_UP,
2714     -1,
2715     options_time_score_base,
2716     &level.time_score_base,
2717     NULL, NULL, NULL,                   "time score for 1 or 10 seconds/steps"
2718   },
2719   {
2720     ED_SELECTBOX_ID_GAME_ENGINE_TYPE,
2721     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(12),
2722     GADGET_ID_GAME_ENGINE_TYPE,         GADGET_ID_NONE,
2723     -1,
2724     options_game_engine_type,
2725     &level.game_engine_type,
2726     NULL, "game engine:", NULL,         "game engine"
2727   },
2728   {
2729     ED_SELECTBOX_ID_BD_SCHEDULING_TYPE,
2730     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(1),
2731     GADGET_ID_BD_SCHEDULING_TYPE,       GADGET_ID_NONE,
2732     -1,
2733     options_bd_scheduling_type,
2734     &level.bd_scheduling_type,
2735     NULL, "scheduling type:", NULL,     "level timing"
2736   },
2737   {
2738     ED_SELECTBOX_ID_LEVELSET_SAVE_MODE,
2739     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(10),
2740     GADGET_ID_LEVELSET_SAVE_MODE,       GADGET_ID_NONE,
2741     -1,
2742     options_levelset_save_mode,
2743     &levelset_save_mode,
2744     "Action:", NULL, NULL,              "action when saving level set"
2745   },
2746
2747   // ---------- element settings: configure (several elements) ----------------
2748
2749   {
2750     ED_SELECTBOX_ID_WIND_DIRECTION,
2751     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
2752     GADGET_ID_WIND_DIRECTION,           GADGET_ID_NONE,
2753     -1,
2754     options_wind_direction,
2755     &level.wind_direction_initial,
2756     NULL, "initial wind direction:", NULL,      "initial wind direction"
2757   },
2758   {
2759     ED_SELECTBOX_ID_PLAYER_SPEED,
2760     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(7),
2761     GADGET_ID_PLAYER_SPEED,             GADGET_ID_NONE,
2762     -1,
2763     options_player_speed,
2764     &level.initial_player_stepsize[0],
2765     NULL, "initial player speed:", NULL,        "initial player speed"
2766   },
2767   {
2768     ED_SELECTBOX_ID_MM_BALL_CHOICE_MODE,
2769     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
2770     GADGET_ID_MM_BALL_CHOICE_MODE,      GADGET_ID_NONE,
2771     -1,
2772     options_group_choice_mode,
2773     &level.mm_ball_choice_mode,
2774     NULL, "choice type:", NULL,         "type of content choice"
2775   },
2776
2777   // ---------- element settings: configure 1 (custom elements) ---------------
2778
2779   {
2780     ED_SELECTBOX_ID_CUSTOM_ACCESS_TYPE,
2781     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(2),
2782     GADGET_ID_CUSTOM_ACCESS_TYPE,       GADGET_ID_NONE,
2783     -1,
2784     options_access_type,
2785     &custom_element.access_type,
2786     NULL, NULL, NULL,                   "type of access to this field"
2787   },
2788   {
2789     ED_SELECTBOX_ID_CUSTOM_ACCESS_LAYER,
2790     -1,                                 ED_ELEMENT_SETTINGS_YPOS(2),
2791     GADGET_ID_CUSTOM_ACCESS_LAYER,      GADGET_ID_CUSTOM_ACCESS_TYPE,
2792     -1,
2793     options_access_layer,
2794     &custom_element.access_layer,
2795     NULL, NULL, NULL,                   "layer of access for this field"
2796   },
2797   {
2798     ED_SELECTBOX_ID_CUSTOM_ACCESS_PROTECTED,
2799     -1,                                 ED_ELEMENT_SETTINGS_YPOS(2),
2800     GADGET_ID_CUSTOM_ACCESS_PROTECTED,  GADGET_ID_CUSTOM_ACCESS_LAYER,
2801     -1,
2802     options_access_protected,
2803     &custom_element.access_protected,
2804     NULL, NULL, NULL,                   "protected access for this field"
2805   },
2806   {
2807     ED_SELECTBOX_ID_CUSTOM_ACCESS_DIRECTION,
2808     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(3),
2809     GADGET_ID_CUSTOM_ACCESS_DIRECTION,  GADGET_ID_NONE,
2810     -1,
2811     options_access_direction,
2812     &custom_element.access_direction,
2813     NULL, "from", NULL,                 "access direction for this field"
2814   },
2815   {
2816     ED_SELECTBOX_ID_CUSTOM_WALK_TO_ACTION,
2817     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(4),
2818     GADGET_ID_CUSTOM_WALK_TO_ACTION,    GADGET_ID_NONE,
2819     -1,
2820     options_walk_to_action,
2821     &custom_element.walk_to_action,
2822     NULL, NULL, NULL,                   "diggable/collectible/pushable"
2823   },
2824
2825   // ---------- element settings: configure 2 (custom elements) ---------------
2826
2827   {
2828     ED_SELECTBOX_ID_CUSTOM_MOVE_PATTERN,
2829     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(1),
2830     GADGET_ID_CUSTOM_MOVE_PATTERN,      GADGET_ID_NONE,
2831     -1,
2832     options_move_pattern,
2833     &custom_element.move_pattern,
2834     NULL, "can move", NULL,             "element move pattern"
2835   },
2836   {
2837     ED_SELECTBOX_ID_CUSTOM_MOVE_DIRECTION,
2838     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(2),
2839     GADGET_ID_CUSTOM_MOVE_DIRECTION,    GADGET_ID_NONE,
2840     -1,
2841     options_move_direction,
2842     &custom_element.move_direction_initial,
2843     NULL, "starts moving", NULL,        "initial element move direction"
2844   },
2845   {
2846     ED_SELECTBOX_ID_CUSTOM_MOVE_STEPSIZE,
2847     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(4),
2848     GADGET_ID_CUSTOM_MOVE_STEPSIZE,     GADGET_ID_NONE,
2849     -1,
2850     options_move_stepsize,
2851     &custom_element.move_stepsize,
2852     NULL, "move/fall speed", NULL,      "speed of element movement"
2853   },
2854   {
2855     ED_SELECTBOX_ID_CUSTOM_MOVE_LEAVE_TYPE,
2856     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(3),
2857     GADGET_ID_CUSTOM_MOVE_LEAVE_TYPE,   GADGET_ID_NONE,
2858     -1,
2859     options_move_leave_type,
2860     &custom_element.move_leave_type,
2861     // left text with leading spaces to place gadget next to "can dig" gadget
2862     // (needed because drawing area gadgets created after selectbox gadgets)
2863     // NULL, "can dig:    can", ":",    "leave behind or change element"
2864     NULL, "            can", ":",       "leave behind or change element"
2865   },
2866   {
2867     ED_SELECTBOX_ID_CUSTOM_SMASH_TARGETS,
2868     -1,                                 ED_ELEMENT_SETTINGS_YPOS(8),
2869     GADGET_ID_CUSTOM_SMASH_TARGETS,     GADGET_ID_CUSTOM_CAN_SMASH,
2870     -1,
2871     options_smash_targets,
2872     &custom_element.smash_targets,
2873     NULL, "can smash", NULL,            "elements that can be smashed"
2874   },
2875   {
2876     ED_SELECTBOX_ID_CUSTOM_SLIPPERY_TYPE,
2877     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(9),
2878     GADGET_ID_CUSTOM_SLIPPERY_TYPE,     GADGET_ID_NONE,
2879     -1,
2880     options_slippery_type,
2881     &custom_element.slippery_type,
2882     NULL, "slippery", NULL,             "where other elements fall down"
2883   },
2884   {
2885     ED_SELECTBOX_ID_CUSTOM_DEADLINESS,
2886     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(10),
2887     GADGET_ID_CUSTOM_DEADLINESS,        GADGET_ID_NONE,
2888     -1,
2889     options_deadliness,
2890     &custom_element.deadliness,
2891     NULL, "deadly when", NULL,          "deadliness of element"
2892   },
2893   {
2894     ED_SELECTBOX_ID_CUSTOM_EXPLOSION_TYPE,
2895     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(11),
2896     GADGET_ID_CUSTOM_EXPLOSION_TYPE,    GADGET_ID_NONE,
2897     -1,
2898     options_explosion_type,
2899     &custom_element.explosion_type,
2900     NULL, "can explode", NULL,          "explosion type"
2901   },
2902
2903   // ---------- element settings: advanced (custom elements) ------------------
2904
2905   {
2906     ED_SELECTBOX_ID_CHANGE_TIME_UNITS,
2907     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(3),
2908     GADGET_ID_CHANGE_TIME_UNITS,        GADGET_ID_NONE,
2909     -1,
2910     options_time_units,
2911     &custom_element_change.delay_frames,
2912     NULL, "delay time given in", NULL,  "delay time units for change"
2913   },
2914   {
2915     ED_SELECTBOX_ID_CHANGE_DIRECT_ACTION,
2916     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(4),
2917     GADGET_ID_CHANGE_DIRECT_ACTION,     GADGET_ID_NONE,
2918     -1,
2919     options_change_direct_action,
2920     &custom_element_change.direct_action,
2921     NULL, NULL, NULL,                   "type of direct action"
2922   },
2923   {
2924     ED_SELECTBOX_ID_CHANGE_OTHER_ACTION,
2925     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(5),
2926     GADGET_ID_CHANGE_OTHER_ACTION,      GADGET_ID_NONE,
2927     -1,
2928     options_change_other_action,
2929     &custom_element_change.other_action,
2930     NULL, NULL, "element:",             "type of other element action"
2931   },
2932   {
2933     ED_SELECTBOX_ID_CHANGE_SIDE,
2934     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(6),
2935     GADGET_ID_CHANGE_SIDE,              GADGET_ID_NONE,
2936     -1,
2937     options_change_trigger_side,
2938     &custom_element_change.trigger_side,
2939     NULL, "at", "side",                 "element side triggering change"
2940   },
2941   {
2942     ED_SELECTBOX_ID_CHANGE_PLAYER,
2943     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(7),
2944     GADGET_ID_CHANGE_PLAYER,            GADGET_ID_NONE,
2945     -1,
2946     options_change_trigger_player,
2947     &custom_element_change.trigger_player,
2948     NULL, "player:", " ",               "player that causes change"
2949   },
2950   {
2951     ED_SELECTBOX_ID_CHANGE_PAGE,
2952     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(7),
2953     GADGET_ID_CHANGE_PAGE,              GADGET_ID_CHANGE_PLAYER,
2954     -1,
2955     options_change_trigger_page,
2956     &custom_element_change.trigger_page,
2957     NULL, "page:", NULL,                "change page that causes change"
2958   },
2959   {
2960     ED_SELECTBOX_ID_CHANGE_REPLACE_WHEN,
2961     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(10),
2962     GADGET_ID_CHANGE_REPLACE_WHEN,      GADGET_ID_NONE,
2963     -1,
2964     options_change_replace_when,
2965     &custom_element_change.replace_when,
2966     NULL, "replace when", NULL,         "which elements can be replaced"
2967   },
2968   {
2969     ED_SELECTBOX_ID_ACTION_TYPE,
2970     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(13),
2971     GADGET_ID_ACTION_TYPE,              GADGET_ID_NONE,
2972     15,
2973     options_action_type,
2974     &custom_element_change.action_type,
2975     NULL, NULL, NULL,                   "action on specified condition"
2976   },
2977   {
2978     ED_SELECTBOX_ID_ACTION_MODE,
2979     -1,                                 ED_ELEMENT_SETTINGS_YPOS(13),
2980     GADGET_ID_ACTION_MODE,              GADGET_ID_ACTION_TYPE,
2981     -1,
2982     options_action_mode_none,
2983     &custom_element_change.action_mode,
2984     NULL, NULL, NULL,                   "action operator"
2985   },
2986   {
2987     ED_SELECTBOX_ID_ACTION_ARG,
2988     -1,                                 ED_ELEMENT_SETTINGS_YPOS(13),
2989     GADGET_ID_ACTION_ARG,               GADGET_ID_ACTION_MODE,
2990     -1,
2991     options_action_arg_none,
2992     &custom_element_change.action_arg,
2993     NULL, NULL, NULL,                   "action parameter"
2994   },
2995   {
2996     ED_SELECTBOX_ID_SELECT_CHANGE_PAGE,
2997     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(14),
2998     GADGET_ID_SELECT_CHANGE_PAGE,       GADGET_ID_NONE,
2999     3,
3000     options_change_page,
3001     &custom_element.current_change_page,
3002     NULL, NULL, NULL,                   "element change page"
3003   },
3004
3005   // ---------- element settings: configure (group elements) ------------------
3006
3007   {
3008     ED_SELECTBOX_ID_GROUP_CHOICE_MODE,
3009     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
3010     GADGET_ID_GROUP_CHOICE_MODE,        GADGET_ID_NONE,
3011     -1,
3012     options_group_choice_mode,
3013     &group_element_info.choice_mode,
3014     NULL, "choice type:", NULL,         "type of group element choice"
3015   },
3016 };
3017
3018 static struct
3019 {
3020   int gadget_type_id;
3021   int x, y;
3022   int gadget_id;
3023   int gadget_id_align;
3024   int size;
3025   char *text;
3026   char *text_above, *text_left, *text_right, *infotext;
3027 } textbutton_info[ED_NUM_TEXTBUTTONS] =
3028 {
3029   // ---------- level and editor settings (tabs) ------------------------------
3030
3031   {
3032     ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL,
3033     ED_LEVEL_TABS_XPOS(0),              ED_LEVEL_TABS_YPOS(0),
3034     GADGET_ID_LEVELCONFIG_LEVEL,        GADGET_ID_NONE,
3035     8,                                  "Level",
3036     NULL, NULL, NULL,                   "Configure level settings"
3037   },
3038   {
3039     ED_TEXTBUTTON_ID_LEVELCONFIG_LEVELSET,
3040     -1,                                 -1,
3041     GADGET_ID_LEVELCONFIG_LEVELSET,     GADGET_ID_LEVELCONFIG_LEVEL,
3042     8,                                  "Levelset",
3043     NULL, NULL, NULL,                   "Update this or create new level set"
3044   },
3045   {
3046     ED_TEXTBUTTON_ID_LEVELCONFIG_EDITOR,
3047     -1,                                 -1,
3048     GADGET_ID_LEVELCONFIG_EDITOR,       GADGET_ID_LEVELCONFIG_LEVELSET,
3049     8,                                  "Editor",
3050     NULL, NULL, NULL,                   "Configure editor settings"
3051   },
3052   {
3053     ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE,
3054     -1,                                 -1,
3055     GADGET_ID_LEVELCONFIG_ENGINE,       GADGET_ID_LEVELCONFIG_EDITOR,
3056     8,                                  "Engine",
3057     NULL, NULL, NULL,                   "Configure engine settings"
3058   },
3059
3060   // ---------- element settings (tabs) ---------------------------------------
3061
3062   {
3063     ED_TEXTBUTTON_ID_PROPERTIES_INFO,
3064     ED_ELEMENT_TABS_XPOS(0),            ED_ELEMENT_TABS_YPOS(0),
3065     GADGET_ID_PROPERTIES_INFO,          GADGET_ID_NONE,
3066     8,                                  "Info",
3067     NULL, NULL, NULL,                   "Show information about element"
3068   },
3069   {
3070     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG,
3071     -1,                                 -1,
3072     GADGET_ID_PROPERTIES_CONFIG,        GADGET_ID_PROPERTIES_INFO,
3073     8,                                  "Config",
3074     NULL, NULL, NULL,                   "Configure element properties"
3075   },
3076   {
3077     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_1,
3078     -1,                                 -1,
3079     GADGET_ID_PROPERTIES_CONFIG_1,      GADGET_ID_PROPERTIES_INFO,
3080     8,                                  "Config 1",
3081     NULL, NULL, NULL,                   "Configure element properties, part 1"
3082   },
3083   {
3084     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2,
3085     -1,                                 -1,
3086     GADGET_ID_PROPERTIES_CONFIG_2,      GADGET_ID_PROPERTIES_CONFIG_1,
3087     8,                                  "Config 2",
3088     NULL, NULL, NULL,                   "Configure element properties, part 2"
3089   },
3090   {
3091     ED_TEXTBUTTON_ID_PROPERTIES_CHANGE,
3092     -1,                                 -1,
3093     GADGET_ID_PROPERTIES_CHANGE,        GADGET_ID_PROPERTIES_CONFIG_2,
3094     8,                                  "Change",
3095     NULL, NULL, NULL,                   "Configure custom element change pages"
3096   },
3097
3098   // ---------- level and editor settings (buttons) ---------------------------
3099
3100   {
3101     ED_TEXTBUTTON_ID_SAVE_LEVELSET,
3102     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(10),
3103     GADGET_ID_SAVE_LEVELSET,            GADGET_ID_LEVELSET_SAVE_MODE,
3104     -1,                                 "Save",
3105     NULL, NULL, NULL,                   "Update or create level set"
3106   },
3107   {
3108     ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2,
3109     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(6),
3110     GADGET_ID_SAVE_AS_TEMPLATE_2,       GADGET_ID_NONE,
3111     -1,                                 "Save",
3112     NULL, NULL,                         "this level as level template",
3113     "Save current settings as new template"
3114   },
3115
3116   // ---------- element settings (buttons) ------------------------------------
3117
3118   {
3119     ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1,
3120     -1,                                 -1,
3121     GADGET_ID_SAVE_AS_TEMPLATE_1,       GADGET_ID_CUSTOM_USE_TEMPLATE_1,
3122     -1,                                 "Save",
3123     NULL, " ",                          "As Template",
3124     "Save current settings as new template"
3125   },
3126   {
3127     ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE,
3128     -1,                                 -1,
3129     GADGET_ID_ADD_CHANGE_PAGE,          GADGET_ID_PASTE_CHANGE_PAGE,
3130     -1,                                 "New",
3131     NULL, NULL, NULL,                   "Add new change page"
3132   },
3133   {
3134     ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE,
3135     -1,                                 -1,
3136     GADGET_ID_DEL_CHANGE_PAGE,          GADGET_ID_ADD_CHANGE_PAGE,
3137     -1,                                 "Delete",
3138     NULL, NULL, NULL,                   "Delete current change page"
3139   },
3140 };
3141
3142 static struct
3143 {
3144   int gadget_type_id;
3145   int graphic;
3146   int x, y;
3147   int gadget_id;
3148   int gadget_id_align;
3149   char *text_left, *text_right, *infotext;
3150 } graphicbutton_info[ED_NUM_GRAPHICBUTTONS] =
3151 {
3152   {
3153     ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE,
3154     IMG_EDITOR_COUNTER_DOWN,
3155     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(14),
3156     GADGET_ID_PREV_CHANGE_PAGE,         GADGET_ID_NONE,
3157     NULL, NULL,                         "select previous change page"
3158   },
3159   {
3160     ED_GRAPHICBUTTON_ID_NEXT_CHANGE_PAGE,
3161     IMG_EDITOR_COUNTER_UP,
3162     -1,                                 ED_ELEMENT_SETTINGS_YPOS(14),
3163     GADGET_ID_NEXT_CHANGE_PAGE,         GADGET_ID_SELECT_CHANGE_PAGE,
3164     NULL, "change page",                "select next change page"
3165   },
3166   {
3167     ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE,
3168     IMG_GFX_EDITOR_BUTTON_CP_COPY,
3169     -1,                                 ED_ELEMENT_SETTINGS_YPOS(14),
3170     GADGET_ID_COPY_CHANGE_PAGE,         GADGET_ID_NEXT_CHANGE_PAGE,
3171     " ", NULL,                          "copy settings from this change page"
3172   },
3173   {
3174     ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE,
3175     IMG_GFX_EDITOR_BUTTON_CP_PASTE,
3176     -1,                                 ED_ELEMENT_SETTINGS_YPOS(14),
3177     GADGET_ID_PASTE_CHANGE_PAGE,        GADGET_ID_COPY_CHANGE_PAGE,
3178     NULL, NULL,                         "paste settings to this change page"
3179   },
3180 };
3181
3182 static struct
3183 {
3184   int x, y;
3185 } scrollbutton_pos[ED_NUM_SCROLLBUTTONS];
3186
3187 static struct
3188 {
3189   int gadget_type_id;
3190   int graphic;
3191   int gadget_id;
3192   char *infotext;
3193 } scrollbutton_info[ED_NUM_SCROLLBUTTONS] =
3194 {
3195   {
3196     ED_SCROLLBUTTON_ID_AREA_UP,
3197     IMG_EDITOR_PLAYFIELD_SCROLL_UP,
3198     GADGET_ID_SCROLL_UP,
3199     "scroll level editing area up"
3200   },
3201   {
3202     ED_SCROLLBUTTON_ID_AREA_DOWN,
3203     IMG_EDITOR_PLAYFIELD_SCROLL_DOWN,
3204     GADGET_ID_SCROLL_DOWN,
3205     "scroll level editing area down"
3206   },
3207   {
3208     ED_SCROLLBUTTON_ID_AREA_LEFT,
3209     IMG_EDITOR_PLAYFIELD_SCROLL_LEFT,
3210     GADGET_ID_SCROLL_LEFT,
3211     "scroll level editing area left"
3212   },
3213   {
3214     ED_SCROLLBUTTON_ID_AREA_RIGHT,
3215     IMG_EDITOR_PLAYFIELD_SCROLL_RIGHT,
3216     GADGET_ID_SCROLL_RIGHT,
3217     "scroll level editing area right"
3218   },
3219   {
3220     ED_SCROLLBUTTON_ID_LIST_UP,
3221     IMG_EDITOR_PALETTE_SCROLL_UP,
3222     GADGET_ID_SCROLL_LIST_UP,
3223     "scroll element list up ('Page Up')"
3224   },
3225   {
3226     ED_SCROLLBUTTON_ID_LIST_DOWN,
3227     IMG_EDITOR_PALETTE_SCROLL_DOWN,
3228     GADGET_ID_SCROLL_LIST_DOWN,
3229     "scroll element list down ('Page Down')"
3230   },
3231 };
3232
3233 static struct
3234 {
3235   int x, y;
3236   int width, height;
3237   int wheel_x, wheel_y;
3238   int wheel_width, wheel_height;
3239 } scrollbar_pos[ED_NUM_SCROLLBARS];
3240
3241 static struct
3242 {
3243   int gadget_type_id;
3244   int graphic;
3245   int type;
3246   int gadget_id;
3247   char *infotext;
3248 } scrollbar_info[ED_NUM_SCROLLBARS] =
3249 {
3250   {
3251     ED_SCROLLBAR_ID_AREA_HORIZONTAL,
3252     IMG_EDITOR_PLAYFIELD_SCROLLBAR,
3253     GD_TYPE_SCROLLBAR_HORIZONTAL,
3254     GADGET_ID_SCROLL_HORIZONTAL,
3255     "scroll level editing area horizontally"
3256   },
3257   {
3258     ED_SCROLLBAR_ID_AREA_VERTICAL,
3259     IMG_EDITOR_PLAYFIELD_SCROLLBAR,
3260     GD_TYPE_SCROLLBAR_VERTICAL,
3261     GADGET_ID_SCROLL_VERTICAL,
3262     "scroll level editing area vertically"
3263   },
3264   {
3265     ED_SCROLLBAR_ID_LIST_VERTICAL,
3266     IMG_EDITOR_PALETTE_SCROLLBAR,
3267     GD_TYPE_SCROLLBAR_VERTICAL,
3268     GADGET_ID_SCROLL_LIST_VERTICAL,
3269     "scroll element list vertically"
3270   }
3271 };
3272
3273
3274 static struct
3275 {
3276   int gadget_type_id;
3277   int x, y;
3278   int gadget_id;
3279   int gadget_id_align;
3280   int radio_button_nr;
3281   int *value;
3282   int checked_value;
3283   char *text_left, *text_right, *infotext;
3284 } radiobutton_info[ED_NUM_RADIOBUTTONS] =
3285 {
3286   {
3287     ED_RADIOBUTTON_ID_PERCENTAGE,
3288     -1,                                 ED_LEVEL_SETTINGS_YPOS(0),
3289     GADGET_ID_RANDOM_PERCENTAGE,        GADGET_ID_LEVEL_RANDOM_UP,
3290     RADIO_NR_RANDOM_ELEMENTS,
3291     &random_placement_method,           RANDOM_USE_PERCENTAGE,
3292     " ", "percentage",                  "use percentage for random elements"
3293   },
3294   {
3295     ED_RADIOBUTTON_ID_QUANTITY,
3296     -1,                                 ED_LEVEL_SETTINGS_YPOS(0),
3297     GADGET_ID_RANDOM_QUANTITY,          GADGET_ID_RANDOM_PERCENTAGE,
3298     RADIO_NR_RANDOM_ELEMENTS,
3299     &random_placement_method,           RANDOM_USE_QUANTITY,
3300     " ", "quantity",                    "use quantity for random elements"
3301   }
3302 };
3303
3304 static struct
3305 {
3306   int gadget_type_id;
3307   int x, y;
3308   int gadget_id;
3309   int gadget_id_align;
3310   boolean *value;
3311   char *text_above, *text_left, *text_right, *infotext;
3312 } checkbutton_info[ED_NUM_CHECKBUTTONS] =
3313 {
3314   // ---------- level and editor settings -------------------------------------
3315
3316   {
3317     ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS,
3318     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(6),
3319     GADGET_ID_AUTO_COUNT_GEMS,          GADGET_ID_NONE,
3320     &level.auto_count_gems,
3321     NULL, NULL,
3322     "automatically count gems needed",  "set counter to number of gems"
3323   },
3324   {
3325     ED_CHECKBUTTON_ID_RATE_TIME_OVER_SCORE,
3326     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(11),
3327     GADGET_ID_RATE_TIME_OVER_SCORE,     GADGET_ID_NONE,
3328     &level.rate_time_over_score,
3329     NULL, NULL,
3330     "rate time/steps used over score",  "sort high scores by playing time/steps"
3331   },
3332   {
3333     ED_CHECKBUTTON_ID_USE_LEVELSET_ARTWORK,
3334     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(7),
3335     GADGET_ID_USE_LEVELSET_ARTWORK,     GADGET_ID_NONE,
3336     &levelset_use_levelset_artwork,
3337     NULL, NULL,
3338     "use current custom artwork",       "use custom artwork of this level set"
3339   },
3340   {
3341     ED_CHECKBUTTON_ID_COPY_LEVEL_TEMPLATE,
3342     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(8),
3343     GADGET_ID_COPY_LEVEL_TEMPLATE,      GADGET_ID_NONE,
3344     &levelset_copy_level_template,
3345     NULL, NULL,
3346     "copy current level template",      "copy level template of this level set"
3347   },
3348   {
3349     ED_CHECKBUTTON_ID_RANDOM_RESTRICTED,
3350     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(1),
3351     GADGET_ID_RANDOM_RESTRICTED,        GADGET_ID_NONE,
3352     &random_placement_background_restricted,
3353     NULL, NULL,
3354     "restrict random placement to:",    "set random placement restriction"
3355   },
3356   {
3357     ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_3,
3358     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
3359     GADGET_ID_CUSTOM_USE_TEMPLATE_3,    GADGET_ID_NONE,
3360     &setup.editor.use_template_for_new_levels,
3361     "Template for new levels and CE/GE:", NULL,
3362     "use template for new levels",      "use template for level properties"
3363   },
3364   {
3365     ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2,
3366     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(5),
3367     GADGET_ID_CUSTOM_USE_TEMPLATE_2,    GADGET_ID_NONE,
3368     &level.use_custom_template,
3369     NULL, NULL,
3370     "use template for custom elements", "use template for custom properties"
3371   },
3372   {
3373     ED_CHECKBUTTON_ID_BD_INTERMISSION,
3374     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(0),
3375     GADGET_ID_BD_INTERMISSION,          GADGET_ID_NONE,
3376     &level.bd_intermission,
3377     "Boulder Dash game engine settings:", NULL,
3378     "intermission",                     "level is an intermission level"
3379   },
3380   {
3381     ED_CHECKBUTTON_ID_BD_PAL_TIMING,
3382     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(2),
3383     GADGET_ID_BD_PAL_TIMING,            GADGET_ID_NONE,
3384     &level.bd_pal_timing,
3385     NULL, NULL,
3386     "PAL timing",                       "use slower timer (like PAL C64)"
3387   },
3388   {
3389     ED_CHECKBUTTON_ID_BD_LINE_SHIFTING_BORDERS,
3390     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(6),
3391     GADGET_ID_BD_LINE_SHIFTING_BORDERS, GADGET_ID_NONE,
3392     &level.bd_line_shifting_borders,
3393     "Compatibility settings:", NULL,
3394     "line-shifting borders",            "use line-shifting wrap-around"
3395   },
3396   {
3397     ED_CHECKBUTTON_ID_BD_SCAN_FIRST_AND_LAST_ROW,
3398     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(7),
3399     GADGET_ID_BD_SCAN_FIRST_AND_LAST_ROW, GADGET_ID_NONE,
3400     &level.bd_scan_first_and_last_row,
3401     NULL, NULL,
3402     "scan first and last row",          "also process top/bottom border rows"
3403   },
3404   {
3405     ED_CHECKBUTTON_ID_BD_SHORT_EXPLOSIONS,
3406     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(8),
3407     GADGET_ID_BD_SHORT_EXPLOSIONS,      GADGET_ID_NONE,
3408     &level.bd_short_explosions,
3409     NULL, NULL,
3410     "short explosions",                 "use four game cycles for explosions"
3411   },
3412   {
3413     ED_CHECKBUTTON_ID_BD_GRAVITY_AFFECTS_ALL,
3414     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(9),
3415     GADGET_ID_BD_GRAVITY_AFFECTS_ALL,   GADGET_ID_NONE,
3416     &level.bd_gravity_affects_all,
3417     NULL, NULL,
3418     "gravity change affects everything", "gravity affects all falling objects"
3419   },
3420
3421   // ---------- element settings: configure (various elements) ----------------
3422
3423   {
3424     ED_CHECKBUTTON_ID_STICK_ELEMENT,
3425     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3426     GADGET_ID_STICK_ELEMENT,            GADGET_ID_NONE,
3427     &stick_element_properties_window,
3428     NULL, NULL,
3429     "stick this screen to edit content","stick this screen to edit content"
3430   },
3431   {
3432     ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS,
3433     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3434     GADGET_ID_EM_SLIPPERY_GEMS,         GADGET_ID_NONE,
3435     &level.em_slippery_gems,
3436     NULL, NULL,
3437     "slip down from certain flat walls","use EM/DC style slipping behaviour"
3438   },
3439   {
3440     ED_CHECKBUTTON_ID_EM_EXPLODES_BY_FIRE,
3441     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3442     GADGET_ID_EM_EXPLODES_BY_FIRE,      GADGET_ID_NONE,
3443     &level.em_explodes_by_fire,
3444     NULL, NULL,
3445     "explodes with chain reaction",     "use R'n'D style explosion behaviour"
3446   },
3447   {
3448     ED_CHECKBUTTON_ID_USE_SPRING_BUG,
3449     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3450     GADGET_ID_USE_SPRING_BUG,           GADGET_ID_NONE,
3451     &level.use_spring_bug,
3452     NULL, NULL,
3453     "use spring pushing bug",           "use odd spring pushing behaviour"
3454   },
3455   {
3456     ED_CHECKBUTTON_ID_USE_TIME_ORB_BUG,
3457     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3458     GADGET_ID_USE_TIME_ORB_BUG,         GADGET_ID_NONE,
3459     &level.use_time_orb_bug,
3460     NULL, NULL,
3461     "use time orb bug",                 "use odd time orb behaviour"
3462   },
3463   {
3464     ED_CHECKBUTTON_ID_USE_LIFE_BUGS,
3465     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(5),
3466     GADGET_ID_USE_LIFE_BUGS,            GADGET_ID_NONE,
3467     &level.use_life_bugs,
3468     NULL, NULL,
3469     "use buggy element behaviour",      "use odd (historic) element behaviour"
3470   },
3471   {
3472     ED_CHECKBUTTON_ID_RANDOM_BALL_CONTENT,
3473     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3474     GADGET_ID_RANDOM_BALL_CONTENT,      GADGET_ID_NONE,
3475     &level.ball_random,
3476     NULL, NULL,
3477     "create single random element",     "only create one element from content"
3478   },
3479   {
3480     ED_CHECKBUTTON_ID_INITIAL_BALL_ACTIVE,
3481     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3482     GADGET_ID_INITIAL_BALL_ACTIVE,      GADGET_ID_NONE,
3483     &level.ball_active_initial,
3484     NULL, NULL,
3485     "magic ball initially activated",   "activate magic ball after level start"
3486   },
3487   {
3488     ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE,
3489     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3490     GADGET_ID_GROW_INTO_DIGGABLE,       GADGET_ID_NONE,
3491     &level.grow_into_diggable,
3492     NULL, NULL,
3493     "can grow into anything diggable",  "grow into more than just sand"
3494   },
3495   {
3496     ED_CHECKBUTTON_ID_SB_FIELDS_NEEDED,
3497     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3498     GADGET_ID_SB_FIELDS_NEEDED,         GADGET_ID_NONE,
3499     &level.sb_fields_needed,
3500     NULL, NULL,
3501     "all fields need to be filled",     "require all SB fields to be solved"
3502   },
3503   {
3504     ED_CHECKBUTTON_ID_SB_OBJECTS_NEEDED,
3505     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3506     GADGET_ID_SB_OBJECTS_NEEDED,        GADGET_ID_NONE,
3507     &level.sb_objects_needed,
3508     NULL, NULL,
3509     "all objects need to be placed",    "require all SB objects to be solved"
3510   },
3511   {
3512     ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN,
3513     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3514     GADGET_ID_AUTO_EXIT_SOKOBAN,        GADGET_ID_NONE,
3515     &level.auto_exit_sokoban,
3516     NULL, NULL,
3517     "exit level if all tasks solved",   "automatically finish Sokoban levels"
3518   },
3519   {
3520     ED_CHECKBUTTON_ID_SOLVED_BY_ONE_PLAYER,
3521     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(14),
3522     GADGET_ID_SOLVED_BY_ONE_PLAYER,     GADGET_ID_NONE,
3523     &level.solved_by_one_player,
3524     NULL, NULL,
3525     "only one player must enter exit",  "level solved by first player in exit"
3526   },
3527   {
3528     ED_CHECKBUTTON_ID_FINISH_DIG_COLLECT,
3529     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
3530     GADGET_ID_FINISH_DIG_COLLECT,       GADGET_ID_NONE,
3531     &level.finish_dig_collect,
3532     NULL, NULL,
3533     "CE action on finished dig/collect", "only finished dig/collect triggers CE"
3534   },
3535   {
3536     ED_CHECKBUTTON_ID_KEEP_WALKABLE_CE,
3537     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
3538     GADGET_ID_KEEP_WALKABLE_CE,         GADGET_ID_NONE,
3539     &level.keep_walkable_ce,
3540     NULL, NULL,
3541     "keep walkable CE changed to player", "keep CE changing to player if walkable"
3542   },
3543   {
3544     ED_CHECKBUTTON_ID_CONTINUOUS_SNAPPING,
3545     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(9),
3546     GADGET_ID_CONTINUOUS_SNAPPING,      GADGET_ID_NONE,
3547     &level.continuous_snapping,
3548     NULL, NULL,
3549     "continuous snapping",              "use snapping without releasing key"
3550   },
3551   {
3552     ED_CHECKBUTTON_ID_BLOCK_SNAP_FIELD,
3553     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(8),
3554     GADGET_ID_BLOCK_SNAP_FIELD,         GADGET_ID_NONE,
3555     &level.block_snap_field,
3556     NULL, NULL,
3557     "block snapped field when snapping", "use snapping delay to show animation"
3558   },
3559   {
3560     ED_CHECKBUTTON_ID_BLOCK_LAST_FIELD,
3561     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3562     GADGET_ID_BLOCK_LAST_FIELD,         GADGET_ID_NONE,
3563     &level.block_last_field,
3564     NULL, NULL,
3565     "block last field when moving",     "player blocks last field when moving"
3566   },
3567   {
3568     ED_CHECKBUTTON_ID_SP_BLOCK_LAST_FIELD,
3569     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3570     GADGET_ID_SP_BLOCK_LAST_FIELD,      GADGET_ID_NONE,
3571     &level.sp_block_last_field,
3572     NULL, NULL,
3573     "block last field when moving",     "player blocks last field when moving"
3574   },
3575   {
3576     ED_CHECKBUTTON_ID_INSTANT_RELOCATION,
3577     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
3578     GADGET_ID_INSTANT_RELOCATION,       GADGET_ID_NONE,
3579     &level.instant_relocation,
3580     NULL, NULL,
3581     "no scrolling when relocating",     "player gets relocated without delay"
3582   },
3583   {
3584     ED_CHECKBUTTON_ID_SHIFTED_RELOCATION,
3585     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
3586     GADGET_ID_SHIFTED_RELOCATION,       GADGET_ID_NONE,
3587     &level.shifted_relocation,
3588     NULL, NULL,
3589     "no centering when relocating",     "level not centered after relocation"
3590   },
3591   {
3592     ED_CHECKBUTTON_ID_LAZY_RELOCATION,
3593     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(5),
3594     GADGET_ID_LAZY_RELOCATION,          GADGET_ID_NONE,
3595     &level.lazy_relocation,
3596     NULL, NULL,
3597     "only redraw off-screen relocation","no redraw if relocation target visible"
3598   },
3599   {
3600     ED_CHECKBUTTON_ID_USE_START_ELEMENT,
3601     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(10),
3602     GADGET_ID_USE_START_ELEMENT,        GADGET_ID_NONE,
3603     &level.use_start_element[0],
3604     NULL, NULL,
3605     "use level start element:",        "start level at this element's position"
3606   },
3607   {
3608     ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT,
3609     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(11),
3610     GADGET_ID_USE_ARTWORK_ELEMENT,      GADGET_ID_NONE,
3611     &level.use_artwork_element[0],
3612     NULL, NULL,
3613     "use artwork from element:",        "use player artwork from other element"
3614   },
3615   {
3616     ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT,
3617     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(12),
3618     GADGET_ID_USE_EXPLOSION_ELEMENT,    GADGET_ID_NONE,
3619     &level.use_explosion_element[0],
3620     NULL, NULL,
3621     "use explosion from element:",      "use explosion properties from element"
3622   },
3623   {
3624     ED_CHECKBUTTON_ID_INITIAL_GRAVITY,
3625     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(13),
3626     GADGET_ID_INITIAL_GRAVITY,          GADGET_ID_NONE,
3627     &level.initial_player_gravity[0],
3628     NULL, NULL,
3629     "use initial gravity",              "set initial player gravity"
3630   },
3631   {
3632     ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY,
3633     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3634     GADGET_ID_USE_INITIAL_INVENTORY,    GADGET_ID_NONE,
3635     &level.use_initial_inventory[0],
3636     NULL, NULL,
3637     "use initial inventory:",           "use collected elements on level start"
3638   },
3639   {
3640     ED_CHECKBUTTON_ID_CAN_PASS_TO_WALKABLE,
3641     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(6),
3642     GADGET_ID_CAN_PASS_TO_WALKABLE,     GADGET_ID_NONE,
3643     &level.can_pass_to_walkable,
3644     NULL, NULL,
3645     "can pass to walkable element",     "player can pass to empty or walkable"
3646   },
3647   {
3648     ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID,
3649     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3650     GADGET_ID_CAN_FALL_INTO_ACID,       GADGET_ID_NONE,
3651     &custom_element_properties[EP_CAN_MOVE_INTO_ACID],
3652     NULL, NULL,
3653     "can fall into acid (with gravity)","player can fall into acid pool"
3654   },
3655   {
3656     ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID,
3657     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3658     GADGET_ID_CAN_MOVE_INTO_ACID,       GADGET_ID_NONE,
3659     &custom_element_properties[EP_CAN_MOVE_INTO_ACID],
3660     NULL, NULL,
3661     "can move into acid",               "element can move into acid pool"
3662   },
3663   {
3664     ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH,
3665     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3666     GADGET_ID_DONT_COLLIDE_WITH,        GADGET_ID_NONE,
3667     &custom_element_properties[EP_DONT_COLLIDE_WITH],
3668     NULL, NULL,
3669     "deadly when colliding with",       "element is deadly when hitting player"
3670   },
3671   {
3672     ED_CHECKBUTTON_ID_BD_DIAGONAL_MOVEMENTS,
3673     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3674     GADGET_ID_BD_DIAGONAL_MOVEMENTS,    GADGET_ID_NONE,
3675     &level.bd_diagonal_movements,
3676     NULL, NULL,
3677     "can move diagonally",              "player can move diagonally"
3678   },
3679   {
3680     ED_CHECKBUTTON_ID_BD_TOPMOST_PLAYER_ACTIVE,
3681     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3682     GADGET_ID_BD_TOPMOST_PLAYER_ACTIVE, GADGET_ID_NONE,
3683     &level.bd_topmost_player_active,
3684     NULL, NULL,
3685     "topmost player is active",         "use first player found on playfield"
3686   },
3687   {
3688     ED_CHECKBUTTON_ID_BD_PUSH_MEGA_ROCK_WITH_SWEET,
3689     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
3690     GADGET_ID_BD_PUSH_MEGA_ROCK_WITH_SWEET, GADGET_ID_NONE,
3691     &level.bd_push_mega_rock_with_sweet,
3692     NULL, NULL,
3693     "mega rocks pushable with sweet",   "push mega rocks after eating sweet"
3694   },
3695   {
3696     ED_CHECKBUTTON_ID_BD_MAGIC_WALL_WAIT_HATCHING,
3697     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3698     GADGET_ID_BD_MAGIC_WALL_WAIT_HATCHING, GADGET_ID_NONE,
3699     &level.bd_magic_wall_wait_hatching,
3700     NULL, NULL,
3701     "wait for player's birth",          "timer start waits for player's birth"
3702   },
3703   {
3704     ED_CHECKBUTTON_ID_BD_MAGIC_WALL_STOPS_AMOEBA,
3705     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3706     GADGET_ID_BD_MAGIC_WALL_STOPS_AMOEBA, GADGET_ID_NONE,
3707     &level.bd_magic_wall_stops_amoeba,
3708     NULL, NULL,
3709     "turn amoeba to diamonds",          "activation changes amoeba to diamonds"
3710   },
3711   {
3712     ED_CHECKBUTTON_ID_ENVELOPE_AUTOWRAP,
3713     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3714     GADGET_ID_ENVELOPE_AUTOWRAP,        GADGET_ID_NONE,
3715     &level.envelope[0].autowrap,
3716     NULL, NULL,
3717     "auto-wrap",                        "automatically wrap envelope text"
3718   },
3719   {
3720     ED_CHECKBUTTON_ID_ENVELOPE_CENTERED,
3721     -1,                                 ED_ELEMENT_SETTINGS_YPOS(1),
3722     GADGET_ID_ENVELOPE_CENTERED,        GADGET_ID_ENVELOPE_AUTOWRAP,
3723     &level.envelope[0].centered,
3724     NULL, " ",
3725     "centered",                         "automatically center envelope text"
3726   },
3727   {
3728     ED_CHECKBUTTON_ID_MM_LASER_RED,
3729     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3730     GADGET_ID_MM_LASER_RED,             GADGET_ID_NONE,
3731     &level.mm_laser_red,
3732     "choose color components for laser:", NULL,
3733     "red",                              "use red color components in laser"
3734   },
3735   {
3736     ED_CHECKBUTTON_ID_MM_LASER_GREEN,
3737     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3738     GADGET_ID_MM_LASER_GREEN,           GADGET_ID_NONE,
3739     &level.mm_laser_green,
3740     NULL, NULL,
3741     "green",                            "use green color components in laser"
3742   },
3743   {
3744     ED_CHECKBUTTON_ID_MM_LASER_BLUE,
3745     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
3746     GADGET_ID_MM_LASER_BLUE,            GADGET_ID_NONE,
3747     &level.mm_laser_blue,
3748     NULL, NULL,
3749     "blue",                             "use blue color components in laser"
3750   },
3751   {
3752     ED_CHECKBUTTON_ID_DF_LASER_RED,
3753     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3754     GADGET_ID_DF_LASER_RED,             GADGET_ID_NONE,
3755     &level.df_laser_red,
3756     "choose color components for laser:", NULL,
3757     "red",                              "use red color components in laser"
3758   },
3759   {
3760     ED_CHECKBUTTON_ID_DF_LASER_GREEN,
3761     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3762     GADGET_ID_DF_LASER_GREEN,           GADGET_ID_NONE,
3763     &level.df_laser_green,
3764     NULL, NULL,
3765     "green",                            "use green color components in laser"
3766   },
3767   {
3768     ED_CHECKBUTTON_ID_DF_LASER_BLUE,
3769     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
3770     GADGET_ID_DF_LASER_BLUE,            GADGET_ID_NONE,
3771     &level.df_laser_blue,
3772     NULL, NULL,
3773     "blue",                             "use blue color components in laser"
3774   },
3775   {
3776     ED_CHECKBUTTON_ID_ROTATE_MM_BALL_CONTENT,
3777     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(5),
3778     GADGET_ID_ROTATE_MM_BALL_CONTENT,   GADGET_ID_NONE,
3779     &level.rotate_mm_ball_content,
3780     NULL, NULL,
3781     "randomly rotate created content",  "randomly rotate newly created content"
3782   },
3783   {
3784     ED_CHECKBUTTON_ID_EXPLODE_MM_BALL,
3785     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(6),
3786     GADGET_ID_EXPLODE_MM_BALL,          GADGET_ID_NONE,
3787     &level.explode_mm_ball,
3788     NULL, NULL,
3789     "explode ball instead of melting",  "use explosion to release ball content"
3790   },
3791
3792   // ---------- element settings: configure 1 (custom elements) ---------------
3793
3794   {
3795     ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC,
3796     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3797     GADGET_ID_CUSTOM_USE_GRAPHIC,       GADGET_ID_NONE,
3798     &custom_element.use_gfx_element,
3799     NULL, NULL,
3800     "use graphic of element:",          "use existing element graphic"
3801   },
3802   {
3803     ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1,
3804     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(14),
3805     GADGET_ID_CUSTOM_USE_TEMPLATE_1,    GADGET_ID_NONE,
3806     &level.use_custom_template,
3807     NULL, NULL,
3808     "use template",                     "use template for custom properties"
3809   },
3810   {
3811     ED_CHECKBUTTON_ID_CUSTOM_ACCESSIBLE,
3812     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3813     GADGET_ID_CUSTOM_ACCESSIBLE,        GADGET_ID_NONE,
3814     &custom_element_properties[EP_ACCESSIBLE],
3815     NULL, NULL,
3816     NULL,                               "player can walk to or pass this field"
3817   },
3818   {
3819     ED_CHECKBUTTON_ID_CUSTOM_GRAV_REACHABLE,
3820     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(9),
3821     GADGET_ID_CUSTOM_GRAV_REACHABLE,    GADGET_ID_NONE,
3822     &custom_element_properties[EP_GRAVITY_REACHABLE],
3823     NULL, NULL,
3824     "reachable despite gravity",        "player can walk/dig despite gravity"
3825   },
3826   {
3827     ED_CHECKBUTTON_ID_CUSTOM_USE_LAST_VALUE,
3828     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(11),
3829     GADGET_ID_CUSTOM_USE_LAST_VALUE,    GADGET_ID_NONE,
3830     &custom_element.use_last_ce_value,
3831     NULL, NULL,
3832     "use last CE value after change",   "use last CE value after change"
3833   },
3834   {
3835     ED_CHECKBUTTON_ID_CUSTOM_WALK_TO_OBJECT,
3836     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
3837     GADGET_ID_CUSTOM_WALK_TO_OBJECT,    GADGET_ID_NONE,
3838     &custom_element_properties[EP_WALK_TO_OBJECT],
3839     NULL, NULL,
3840     NULL,                               "player can dig/collect/push element"
3841   },
3842   {
3843     ED_CHECKBUTTON_ID_CUSTOM_INDESTRUCTIBLE,
3844     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(8),
3845     GADGET_ID_CUSTOM_INDESTRUCTIBLE,    GADGET_ID_NONE,
3846     &custom_element_properties[EP_INDESTRUCTIBLE],
3847     NULL, NULL,
3848     "indestructible",                   "element is indestructible"
3849   },
3850
3851   // ---------- element settings: configure 2 (custom elements) ---------------
3852
3853   {
3854     ED_CHECKBUTTON_ID_CUSTOM_CAN_MOVE,
3855     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3856     GADGET_ID_CUSTOM_CAN_MOVE,          GADGET_ID_NONE,
3857     &custom_element_properties[EP_CAN_MOVE],
3858     NULL, NULL,
3859     NULL,                               "element can move with some pattern"
3860   },
3861   {
3862     ED_CHECKBUTTON_ID_CUSTOM_CAN_FALL,
3863     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(8),
3864     GADGET_ID_CUSTOM_CAN_FALL,          GADGET_ID_NONE,
3865     &custom_element_properties[EP_CAN_FALL],
3866     NULL, NULL,
3867     "can fall",                         "element can fall down"
3868   },
3869   {
3870     ED_CHECKBUTTON_ID_CUSTOM_CAN_SMASH,
3871     -1,                                 ED_ELEMENT_SETTINGS_YPOS(8),
3872     GADGET_ID_CUSTOM_CAN_SMASH,         GADGET_ID_CUSTOM_CAN_FALL,
3873     &custom_element_properties[EP_CAN_SMASH],
3874     NULL, " ",
3875     NULL,                               "element can smash other elements"
3876   },
3877   {
3878     ED_CHECKBUTTON_ID_CUSTOM_SLIPPERY,
3879     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(9),
3880     GADGET_ID_CUSTOM_SLIPPERY,          GADGET_ID_NONE,
3881     &custom_element_properties[EP_SLIPPERY],
3882     NULL, NULL,
3883     NULL,                               "other elements can fall down from it"
3884   },
3885   {
3886     ED_CHECKBUTTON_ID_CUSTOM_DEADLY,
3887     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(10),
3888     GADGET_ID_CUSTOM_DEADLY,            GADGET_ID_NONE,
3889     &custom_element_properties[EP_DEADLY],
3890     NULL, NULL,
3891     NULL,                               "element can kill the player"
3892   },
3893   {
3894     ED_CHECKBUTTON_ID_CUSTOM_CAN_EXPLODE,
3895     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(11),
3896     GADGET_ID_CUSTOM_CAN_EXPLODE,       GADGET_ID_NONE,
3897     &custom_element_properties[EP_CAN_EXPLODE],
3898     NULL, NULL,
3899     NULL,                               "element can explode"
3900   },
3901   {
3902     ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_FIRE,
3903     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(12),
3904     GADGET_ID_CUSTOM_EXPLODE_FIRE,      GADGET_ID_NONE,
3905     &custom_element_properties[EP_EXPLODES_BY_FIRE],
3906     NULL, NULL,
3907     "by fire",                          "element can explode by fire/explosion"
3908   },
3909   {
3910     ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_SMASH,
3911     -1,                                 ED_ELEMENT_SETTINGS_YPOS(12),
3912     GADGET_ID_CUSTOM_EXPLODE_SMASH,     GADGET_ID_CUSTOM_EXPLODE_FIRE,
3913     &custom_element_properties[EP_EXPLODES_SMASHED],
3914     NULL, " ",
3915     "smashed",                          "element can explode when smashed"
3916   },
3917   {
3918     ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_IMPACT,
3919     -1,                                 ED_ELEMENT_SETTINGS_YPOS(12),
3920     GADGET_ID_CUSTOM_EXPLODE_IMPACT,    GADGET_ID_CUSTOM_EXPLODE_SMASH,
3921     &custom_element_properties[EP_EXPLODES_IMPACT],
3922     NULL, " ",
3923     "impact",                           "element can explode on impact"
3924   },
3925
3926   // ---------- element settings: advanced (custom elements) ------------------
3927
3928   {
3929     ED_CHECKBUTTON_ID_CUSTOM_CAN_CHANGE,
3930     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3931     GADGET_ID_CUSTOM_CAN_CHANGE,        GADGET_ID_NONE,
3932     &custom_element_change.can_change,
3933     NULL, NULL,
3934     "element changes to:",              "change element on specified condition"
3935   },
3936   {
3937     ED_CHECKBUTTON_ID_CHANGE_DELAY,
3938     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(2),
3939     GADGET_ID_CHANGE_DELAY,             GADGET_ID_NONE,
3940     &custom_element_change_events[CE_DELAY],
3941     NULL, NULL,
3942     NULL,                               "element changes after delay"
3943   },
3944   {
3945     ED_CHECKBUTTON_ID_CHANGE_BY_DIRECT_ACT,
3946     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(4),
3947     GADGET_ID_CHANGE_BY_DIRECT_ACT,     GADGET_ID_NONE,
3948     &custom_element_change_events[CE_BY_DIRECT_ACTION],
3949     NULL, NULL,
3950     NULL,                               "element changes by direct action"
3951   },
3952   {
3953     ED_CHECKBUTTON_ID_CHANGE_BY_OTHER_ACT,
3954     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(5),
3955     GADGET_ID_CHANGE_BY_OTHER_ACT,      GADGET_ID_NONE,
3956     &custom_element_change_events[CE_BY_OTHER_ACTION],
3957     NULL, NULL,
3958     NULL,                               "element changes by other element"
3959   },
3960   {
3961     ED_CHECKBUTTON_ID_CHANGE_USE_EXPLOSION,
3962     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(8),
3963     GADGET_ID_CHANGE_USE_EXPLOSION,     GADGET_ID_NONE,
3964     &custom_element_change.explode,
3965     NULL, NULL,
3966     "explode instead of change",        "element explodes instead of change"
3967   },
3968   {
3969     ED_CHECKBUTTON_ID_CHANGE_USE_CONTENT,
3970     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(9),
3971     GADGET_ID_CHANGE_USE_CONTENT,       GADGET_ID_NONE,
3972     &custom_element_change.use_target_content,
3973     NULL, NULL,
3974     "use extended change target:",      "element changes to more elements"
3975   },
3976   {
3977     ED_CHECKBUTTON_ID_CHANGE_ONLY_COMPLETE,
3978     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(11),
3979     GADGET_ID_CHANGE_ONLY_COMPLETE,     GADGET_ID_NONE,
3980     &custom_element_change.only_if_complete,
3981     NULL, NULL,
3982     "replace all or nothing",           "only replace when all can be changed"
3983   },
3984   {
3985     ED_CHECKBUTTON_ID_CHANGE_USE_RANDOM,
3986     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(12),
3987     GADGET_ID_CHANGE_USE_RANDOM,        GADGET_ID_NONE,
3988     &custom_element_change.use_random_replace,
3989     NULL, NULL,
3990     NULL,                               "use percentage for random replace"
3991   },
3992   {
3993     ED_CHECKBUTTON_ID_CHANGE_HAS_ACTION,
3994     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(13),
3995     GADGET_ID_CHANGE_HAS_ACTION,        GADGET_ID_NONE,
3996     &custom_element_change.has_action,
3997     NULL, NULL,
3998     NULL,                               "execute action on specified condition"
3999   },
4000 };
4001
4002 static struct
4003 {
4004   int gadget_type_id;
4005   int x, y;
4006   int xoffset, yoffset;
4007   int gadget_id;
4008   int gadget_id_align;
4009   int *value;
4010   int area_xsize, area_ysize;
4011   char *text_left, *text_right, *text_above, *text_below, *infotext;
4012 } drawingarea_info[ED_NUM_DRAWING_AREAS] =
4013 {
4014   // ---------- level playfield content ---------------------------------------
4015
4016   {
4017     ED_DRAWING_ID_DRAWING_LEVEL,
4018     0,                                  0,
4019     0,                                  0,
4020     GADGET_ID_DRAWING_LEVEL,            GADGET_ID_NONE,
4021     NULL,
4022     -1, -1,     // these values are not constant, but can change at runtime
4023     NULL, NULL, NULL, NULL,             NULL
4024   },
4025
4026   // ---------- yam yam content -----------------------------------------------
4027
4028   {
4029     ED_DRAWING_ID_YAMYAM_CONTENT_0,
4030     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
4031     ED_AREA_YAMYAM_CONTENT_XOFF(0),     ED_AREA_YAMYAM_CONTENT_YOFF(0),
4032     GADGET_ID_YAMYAM_CONTENT_0,         GADGET_ID_NONE,
4033     &level.yamyam_content[0].e[0][0],   3, 3,
4034     NULL, NULL, NULL, "1",              NULL
4035   },
4036   {
4037     ED_DRAWING_ID_YAMYAM_CONTENT_1,
4038     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
4039     ED_AREA_YAMYAM_CONTENT_XOFF(1),     ED_AREA_YAMYAM_CONTENT_YOFF(1),
4040     GADGET_ID_YAMYAM_CONTENT_1,         GADGET_ID_NONE,
4041     &level.yamyam_content[1].e[0][0],   3, 3,
4042     NULL, NULL, NULL, "2",              NULL
4043   },
4044   {
4045     ED_DRAWING_ID_YAMYAM_CONTENT_2,
4046     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
4047     ED_AREA_YAMYAM_CONTENT_XOFF(2),     ED_AREA_YAMYAM_CONTENT_YOFF(2),
4048     GADGET_ID_YAMYAM_CONTENT_2,         GADGET_ID_NONE,
4049     &level.yamyam_content[2].e[0][0],   3, 3,
4050     NULL, NULL, NULL, "3",              NULL
4051   },
4052   {
4053     ED_DRAWING_ID_YAMYAM_CONTENT_3,
4054     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
4055     ED_AREA_YAMYAM_CONTENT_XOFF(3),     ED_AREA_YAMYAM_CONTENT_YOFF(3),
4056     GADGET_ID_YAMYAM_CONTENT_3,         GADGET_ID_NONE,
4057     &level.yamyam_content[3].e[0][0],   3, 3,
4058     NULL, NULL, NULL, "4",              NULL
4059   },
4060   {
4061     ED_DRAWING_ID_YAMYAM_CONTENT_4,
4062     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
4063     ED_AREA_YAMYAM_CONTENT_XOFF(4),     ED_AREA_YAMYAM_CONTENT_YOFF(4),
4064     GADGET_ID_YAMYAM_CONTENT_4,         GADGET_ID_NONE,
4065     &level.yamyam_content[4].e[0][0],   3, 3,
4066     NULL, NULL, NULL, "5",              NULL
4067   },
4068   {
4069     ED_DRAWING_ID_YAMYAM_CONTENT_5,
4070     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
4071     ED_AREA_YAMYAM_CONTENT_XOFF(5),     ED_AREA_YAMYAM_CONTENT_YOFF(5),
4072     GADGET_ID_YAMYAM_CONTENT_5,         GADGET_ID_NONE,
4073     &level.yamyam_content[5].e[0][0],   3, 3,
4074     NULL, NULL, NULL, "6",              NULL
4075   },
4076   {
4077     ED_DRAWING_ID_YAMYAM_CONTENT_6,
4078     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
4079     ED_AREA_YAMYAM_CONTENT_XOFF(6),     ED_AREA_YAMYAM_CONTENT_YOFF(6),
4080     GADGET_ID_YAMYAM_CONTENT_6,         GADGET_ID_NONE,
4081     &level.yamyam_content[6].e[0][0],   3, 3,
4082     NULL, NULL, NULL, "7",              NULL
4083   },
4084   {
4085     ED_DRAWING_ID_YAMYAM_CONTENT_7,
4086     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
4087     ED_AREA_YAMYAM_CONTENT_XOFF(7),     ED_AREA_YAMYAM_CONTENT_YOFF(7),
4088     GADGET_ID_YAMYAM_CONTENT_7,         GADGET_ID_NONE,
4089     &level.yamyam_content[7].e[0][0],   3, 3,
4090     NULL, NULL, NULL, "8",              NULL
4091   },
4092
4093   // ---------- magic ball content --------------------------------------------
4094
4095   {
4096     ED_DRAWING_ID_MAGIC_BALL_CONTENT_0,
4097     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4098     ED_AREA_MAGIC_BALL_CONTENT_XOFF(0), ED_AREA_MAGIC_BALL_CONTENT_YOFF(0),
4099     GADGET_ID_MAGIC_BALL_CONTENT_0,     GADGET_ID_NONE,
4100     &level.ball_content[0].e[0][0],     3, 3,
4101     NULL, NULL, NULL, "1",              NULL
4102   },
4103   {
4104     ED_DRAWING_ID_MAGIC_BALL_CONTENT_1,
4105     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4106     ED_AREA_MAGIC_BALL_CONTENT_XOFF(1), ED_AREA_MAGIC_BALL_CONTENT_YOFF(1),
4107     GADGET_ID_MAGIC_BALL_CONTENT_1,     GADGET_ID_NONE,
4108     &level.ball_content[1].e[0][0],     3, 3,
4109     NULL, NULL, NULL, "2",              NULL
4110   },
4111   {
4112     ED_DRAWING_ID_MAGIC_BALL_CONTENT_2,
4113     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4114     ED_AREA_MAGIC_BALL_CONTENT_XOFF(2), ED_AREA_MAGIC_BALL_CONTENT_YOFF(2),
4115     GADGET_ID_MAGIC_BALL_CONTENT_2,     GADGET_ID_NONE,
4116     &level.ball_content[2].e[0][0],     3, 3,
4117     NULL, NULL, NULL, "3",              NULL
4118   },
4119   {
4120     ED_DRAWING_ID_MAGIC_BALL_CONTENT_3,
4121     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4122     ED_AREA_MAGIC_BALL_CONTENT_XOFF(3), ED_AREA_MAGIC_BALL_CONTENT_YOFF(3),
4123     GADGET_ID_MAGIC_BALL_CONTENT_3,     GADGET_ID_NONE,
4124     &level.ball_content[3].e[0][0],     3, 3,
4125     NULL, NULL, NULL, "4",              NULL
4126   },
4127   {
4128     ED_DRAWING_ID_MAGIC_BALL_CONTENT_4,
4129     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4130     ED_AREA_MAGIC_BALL_CONTENT_XOFF(4), ED_AREA_MAGIC_BALL_CONTENT_YOFF(4),
4131     GADGET_ID_MAGIC_BALL_CONTENT_4,     GADGET_ID_NONE,
4132     &level.ball_content[4].e[0][0],     3, 3,
4133     NULL, NULL, NULL, "5",              NULL
4134   },
4135   {
4136     ED_DRAWING_ID_MAGIC_BALL_CONTENT_5,
4137     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4138     ED_AREA_MAGIC_BALL_CONTENT_XOFF(5), ED_AREA_MAGIC_BALL_CONTENT_YOFF(5),
4139     GADGET_ID_MAGIC_BALL_CONTENT_5,     GADGET_ID_NONE,
4140     &level.ball_content[5].e[0][0],     3, 3,
4141     NULL, NULL, NULL, "6",              NULL
4142   },
4143   {
4144     ED_DRAWING_ID_MAGIC_BALL_CONTENT_6,
4145     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4146     ED_AREA_MAGIC_BALL_CONTENT_XOFF(6), ED_AREA_MAGIC_BALL_CONTENT_YOFF(6),
4147     GADGET_ID_MAGIC_BALL_CONTENT_6,     GADGET_ID_NONE,
4148     &level.ball_content[6].e[0][0],     3, 3,
4149     NULL, NULL, NULL, "7",              NULL
4150   },
4151   {
4152     ED_DRAWING_ID_MAGIC_BALL_CONTENT_7,
4153     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4154     ED_AREA_MAGIC_BALL_CONTENT_XOFF(7), ED_AREA_MAGIC_BALL_CONTENT_YOFF(7),
4155     GADGET_ID_MAGIC_BALL_CONTENT_7,     GADGET_ID_NONE,
4156     &level.ball_content[7].e[0][0],     3, 3,
4157     NULL, NULL, NULL, "8",              NULL
4158   },
4159
4160   // ---------- android content -----------------------------------------------
4161
4162   {
4163     ED_DRAWING_ID_ANDROID_CONTENT,
4164     ED_AREA_1X1_SETTINGS_XPOS(0),       ED_AREA_1X1_SETTINGS_YPOS(6),
4165     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4166     GADGET_ID_ANDROID_CONTENT,          GADGET_ID_NONE,
4167     &level.android_clone_element[0],    MAX_ANDROID_ELEMENTS, 1,
4168     NULL, NULL, "elements:", NULL,      "elements android can clone"
4169   },
4170
4171   // ---------- amoeba content ------------------------------------------------
4172
4173   {
4174     ED_DRAWING_ID_AMOEBA_CONTENT,
4175     ED_AREA_1X1_SETTINGS_XPOS(0),       ED_AREA_1X1_SETTINGS_YPOS(3),
4176     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4177     GADGET_ID_AMOEBA_CONTENT,           GADGET_ID_NONE,
4178     &level.amoeba_content,              1, 1,
4179     "content:", NULL, NULL, NULL,       "amoeba content"
4180   },
4181
4182   // ---------- level start element -------------------------------------------
4183
4184   {
4185     ED_DRAWING_ID_START_ELEMENT,
4186     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(10),
4187     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4188     GADGET_ID_START_ELEMENT,            GADGET_ID_USE_START_ELEMENT,
4189     &level.start_element[0],            1, 1,
4190     NULL, NULL, NULL, NULL,             "level start element"
4191   },
4192
4193   // ---------- player artwork element ----------------------------------------
4194
4195   {
4196     ED_DRAWING_ID_ARTWORK_ELEMENT,
4197     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(11),
4198     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4199     GADGET_ID_ARTWORK_ELEMENT,          GADGET_ID_USE_ARTWORK_ELEMENT,
4200     &level.artwork_element[0],          1, 1,
4201     NULL, NULL, NULL, NULL,             "element for player artwork"
4202   },
4203
4204   // ---------- player explosion element --------------------------------------
4205
4206   {
4207     ED_DRAWING_ID_EXPLOSION_ELEMENT,
4208     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(12),
4209     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4210     GADGET_ID_EXPLOSION_ELEMENT,        GADGET_ID_USE_EXPLOSION_ELEMENT,
4211     &level.explosion_element[0],        1, 1,
4212     NULL, NULL, NULL, NULL,             "element for player explosion"
4213   },
4214
4215   // ---------- player initial inventory --------------------------------------
4216
4217   {
4218     ED_DRAWING_ID_INVENTORY_CONTENT,
4219     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(1),
4220     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4221     GADGET_ID_INVENTORY_CONTENT,        GADGET_ID_USE_INITIAL_INVENTORY,
4222     &level.initial_inventory_content[0][0], MAX_INITIAL_INVENTORY_SIZE, 1,
4223     NULL, NULL, NULL, NULL,             "content for initial inventory"
4224   },
4225
4226   // ---------- gray ball content -----------------------------------------
4227
4228   {
4229     ED_DRAWING_ID_MM_BALL_CONTENT,
4230     ED_AREA_1X1_SETTINGS_XPOS(0),       ED_AREA_1X1_SETTINGS_YPOS(2),
4231     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4232     GADGET_ID_MM_BALL_CONTENT,          GADGET_ID_NONE,
4233     &level.mm_ball_content[0],          MAX_MM_BALL_CONTENTS, 1,
4234     "content:", NULL, NULL, NULL,       "content for gray ball"
4235   },
4236
4237   // ---------- element settings: configure 1 (custom elements) ---------------
4238
4239   // ---------- custom graphic ------------------------------------------------
4240
4241   {
4242     ED_DRAWING_ID_CUSTOM_GRAPHIC,
4243     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(1),
4244     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4245     GADGET_ID_CUSTOM_GRAPHIC,           GADGET_ID_CUSTOM_USE_GRAPHIC,
4246     &custom_element.gfx_element_initial, 1, 1,
4247     NULL, NULL, NULL, NULL,             "custom graphic element"
4248   },
4249
4250   // ---------- element settings: configure 2 (custom elements) ---------------
4251
4252   // ---------- custom content (when exploding) -------------------------------
4253
4254   {
4255     ED_DRAWING_ID_CUSTOM_CONTENT,
4256     -1,                                 ED_AREA_3X3_SETTINGS_YPOS(11),
4257     0,                                  ED_AREA_3X3_SETTINGS_YOFF,
4258     GADGET_ID_CUSTOM_CONTENT,           GADGET_ID_NONE, // align three rows
4259     &custom_element.content.e[0][0],    3, 3,
4260     "content:", NULL, NULL, NULL,       NULL
4261   },
4262
4263   // ---------- custom enter and leave element (when moving) ------------------
4264
4265   {
4266     ED_DRAWING_ID_CUSTOM_MOVE_ENTER,
4267     ED_AREA_1X1_SETTINGS_XPOS(1),       ED_AREA_1X1_SETTINGS_YPOS(3),
4268     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4269     GADGET_ID_CUSTOM_MOVE_ENTER,        GADGET_ID_NONE,
4270     &custom_element.move_enter_element, 1, 1,
4271     "can dig:", " ", NULL, NULL,        "element that can be digged/collected"
4272   },
4273   {
4274     ED_DRAWING_ID_CUSTOM_MOVE_LEAVE,
4275     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(3),
4276     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4277     GADGET_ID_CUSTOM_MOVE_LEAVE,        GADGET_ID_CUSTOM_MOVE_LEAVE_TYPE,
4278     &custom_element.move_leave_element, 1, 1,
4279     NULL, NULL, NULL, NULL,             "element that will be left behind"
4280   },
4281
4282   // ---------- element settings: advanced (custom elements) ------------------
4283
4284   // ---------- custom change target ------------------------------------------
4285
4286   {
4287     ED_DRAWING_ID_CUSTOM_CHANGE_TARGET,
4288     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(1),
4289     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4290     GADGET_ID_CUSTOM_CHANGE_TARGET,     GADGET_ID_CUSTOM_CAN_CHANGE,
4291     &custom_element_change.target_element, 1, 1,
4292     NULL, "after/when:", NULL, NULL,    "new target element after change"
4293   },
4294
4295   // ---------- custom change content (extended change target) ----------------
4296
4297   {
4298     ED_DRAWING_ID_CUSTOM_CHANGE_CONTENT,
4299     -1,                                 ED_AREA_3X3_SETTINGS_YPOS(9),
4300     0,                                  ED_AREA_3X3_SETTINGS_YOFF,
4301     GADGET_ID_CUSTOM_CHANGE_CONTENT,    GADGET_ID_NONE, // align three rows
4302     &custom_element_change.target_content.e[0][0], 3, 3,
4303     NULL, NULL, NULL, NULL,             "new extended elements after change"
4304   },
4305
4306   // ---------- custom change trigger (element causing change) ----------------
4307
4308   {
4309     ED_DRAWING_ID_CUSTOM_CHANGE_TRIGGER,
4310     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(5),
4311     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4312     GADGET_ID_CUSTOM_CHANGE_TRIGGER,    GADGET_ID_CHANGE_OTHER_ACTION,
4313     &custom_element_change.initial_trigger_element, 1, 1,
4314     NULL, NULL, NULL, NULL,             "other element triggering change"
4315   },
4316
4317   // ---------- custom change action (element used for action) ----------------
4318
4319   {
4320     ED_DRAWING_ID_CUSTOM_CHANGE_ACTION,
4321     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(13),
4322     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4323     GADGET_ID_CUSTOM_CHANGE_ACTION,     GADGET_ID_ACTION_ARG,
4324     &custom_element_change.action_element, 1, 1,
4325     NULL, NULL, NULL, NULL,             "element used as action parameter"
4326   },
4327
4328   // ---------- group element content -----------------------------------------
4329
4330   {
4331     ED_DRAWING_ID_GROUP_CONTENT,
4332     ED_AREA_1X1_SETTINGS_XPOS(0),       ED_AREA_1X1_SETTINGS_YPOS(2),
4333     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4334     GADGET_ID_GROUP_CONTENT,            GADGET_ID_NONE,
4335     &group_element_info.element[0],     MAX_ELEMENTS_IN_GROUP, 1,
4336     "content:", NULL, NULL, NULL,       NULL
4337   },
4338
4339   // ---------- random background (for random painting) -----------------------
4340
4341   {
4342     ED_DRAWING_ID_RANDOM_BACKGROUND,
4343     -1,                                 ED_AREA_1X1_LSETTINGS_YPOS(1),
4344     0,                                  ED_AREA_1X1_LSETTINGS_YOFF,
4345     GADGET_ID_RANDOM_BACKGROUND,        GADGET_ID_RANDOM_RESTRICTED,
4346     &random_placement_background_element, 1, 1,
4347     NULL, NULL, NULL, NULL,             "random placement background"
4348   },
4349 };
4350
4351
4352 // ----------------------------------------------------------------------------
4353 // some internally used variables
4354 // ----------------------------------------------------------------------------
4355
4356 // maximal size of level editor drawing area
4357 static int MAX_ED_FIELDX, MAX_ED_FIELDY;
4358
4359 // actual size of level editor drawing area
4360 static int ed_fieldx, ed_fieldy;
4361
4362 // actual position of level editor drawing area in level playfield
4363 static int level_xpos = -1, level_ypos = -1;
4364
4365 // actual tile size used to display playfield drawing area
4366 static int ed_tilesize = DEFAULT_EDITOR_TILESIZE;
4367 static int ed_tilesize_default = DEFAULT_EDITOR_TILESIZE;
4368
4369 #define IN_ED_FIELD(x, y)       IN_FIELD(x, y, ed_fieldx, ed_fieldy)
4370
4371 // drawing elements on the three mouse buttons
4372 static int new_element1 = EL_WALL;
4373 static int new_element2 = EL_EMPTY;
4374 static int new_element3 = EL_SAND;
4375
4376 #define IS_VALID_BUTTON(button) (button >= 1 && button <= 3)
4377 #define BUTTON_ELEMENT(button) ((button) == 1 ? new_element1 : \
4378                                 (button) == 2 ? new_element2 : \
4379                                 (button) == 3 ? new_element3 : EL_EMPTY)
4380
4381 #define BUTTON_TILE_SIZE(x)     ((x) >= TILESIZE ? TILESIZE : MINI_TILESIZE)
4382
4383 static int use_permanent_palette = TRUE;
4384
4385 #define PX              (use_permanent_palette ? DX : SX)
4386 #define PY              (use_permanent_palette ? DY : SY)
4387 #define PXSIZE          (use_permanent_palette ? DXSIZE : SXSIZE)
4388 #define PYSIZE          (use_permanent_palette ? DYSIZE : SYSIZE)
4389
4390 // forward declaration for internal use
4391 static void CopyBrushToCursor(int, int);
4392 static void DeleteBrushFromCursor(void);
4393 static void ModifyEditorCounterValue(int, int);
4394 static void ModifyEditorCounterLimits(int, int, int);
4395 static void ModifyEditorSelectboxValue(int, int);
4396 static void ModifyEditorSelectboxOptions(int, struct ValueTextInfo *);
4397 static void ModifyEditorDrawingArea(int, int, int);
4398 static void ModifyEditorElementList(void);
4399 static void AdjustElementListScrollbar(void);
4400 static void RedrawDrawingElements(void);
4401 static void DrawDrawingWindowExt(boolean);
4402 static void DrawDrawingWindow(void);
4403 static void DrawLevelConfigWindow(void);
4404 static void DrawPropertiesWindow(void);
4405 static void DrawPaletteWindow(void);
4406 static void UpdateCustomElementGraphicGadgets(void);
4407 static boolean checkPropertiesConfig(int);
4408 static void SetAutomaticNumberOfGemsNeeded(void);
4409 static void ClearEditorGadgetInfoText(void);
4410 static void CopyLevelToUndoBuffer(int);
4411 static void HandleDrawingAreas(struct GadgetInfo *);
4412 static void HandleCounterButtons(struct GadgetInfo *);
4413 static void HandleTextInputGadgets(struct GadgetInfo *);
4414 static void HandleTextAreaGadgets(struct GadgetInfo *);
4415 static void HandleSelectboxGadgets(struct GadgetInfo *);
4416 static void HandleTextbuttonGadgets(struct GadgetInfo *);
4417 static void HandleGraphicbuttonGadgets(struct GadgetInfo *);
4418 static void HandleRadiobuttons(struct GadgetInfo *);
4419 static void HandleCheckbuttons(struct GadgetInfo *);
4420 static void HandleControlButtons(struct GadgetInfo *);
4421 static void HandleDrawingAreaInfo(struct GadgetInfo *);
4422 static void PrintEditorGadgetInfoText(struct GadgetInfo *);
4423 static boolean AskToCopyAndModifyLevelTemplate(void);
4424 static boolean getDrawModeHiRes(void);
4425 static int getTabulatorBarWidth(void);
4426 static int getTabulatorBarHeight(void);
4427 static Pixel getTabulatorBarColor(void);
4428 static int numHiresTiles(int);
4429
4430 static int num_editor_gadgets = 0;      // dynamically determined
4431
4432 static struct GadgetInfo **level_editor_gadget = NULL;
4433 static int *right_gadget_border = NULL;
4434
4435 static int drawing_function = GADGET_ID_SINGLE_ITEMS;
4436 static int last_drawing_function = GADGET_ID_SINGLE_ITEMS;
4437 static boolean draw_with_brush = FALSE;
4438 static int properties_element = 0;
4439
4440 static short TileBackup[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
4441 static short UndoBuffer[NUM_UNDO_STEPS][MAX_LEV_FIELDX][MAX_LEV_FIELDY];
4442 static short IntelliDrawBuffer[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
4443 static int undo_buffer_position = 0;
4444 static int undo_buffer_steps = 0;
4445 static int redo_buffer_steps = 0;
4446
4447 static int edit_mode;
4448 static int edit_mode_levelconfig;
4449 static int edit_mode_properties;
4450
4451 static int element_shift = 0;
4452
4453 static int editor_el_players[] =
4454 {
4455   EL_PLAYER_1,
4456   EL_PLAYER_2,
4457   EL_PLAYER_3,
4458   EL_PLAYER_4
4459 };
4460 static int *editor_el_players_ptr = editor_el_players;
4461 static int num_editor_el_players = ARRAY_SIZE(editor_el_players);
4462
4463 static int editor_hl_boulderdash[] =
4464 {
4465   EL_INTERNAL_CASCADE_BD_ACTIVE,
4466   EL_CHAR('B'),
4467   EL_CHAR('D'),
4468   EL_EMPTY,
4469 };
4470
4471 static int editor_el_boulderdash[] =
4472 {
4473   EL_EMPTY,
4474   EL_SAND,
4475   EL_BD_ROCK,
4476   EL_BD_DIAMOND,
4477
4478   EL_STEELWALL,
4479   EL_BD_WALL,
4480   EL_BD_EXPANDABLE_WALL,
4481   EL_BD_MAGIC_WALL,
4482
4483   EL_BD_AMOEBA,
4484   EL_BD_BUTTERFLY_UP,
4485   EL_BD_FIREFLY_UP,
4486   EL_EXIT_CLOSED,
4487
4488   EL_BD_BUTTERFLY_LEFT,
4489   EL_BD_FIREFLY_LEFT,
4490   EL_BD_BUTTERFLY_RIGHT,
4491   EL_BD_FIREFLY_RIGHT,
4492
4493   EL_EMPTY,
4494   EL_BD_BUTTERFLY_DOWN,
4495   EL_BD_FIREFLY_DOWN,
4496   EL_EXIT_OPEN,
4497 };
4498 static int *editor_hl_boulderdash_ptr = editor_hl_boulderdash;
4499 static int *editor_el_boulderdash_ptr = editor_el_boulderdash;
4500 static int num_editor_hl_boulderdash = ARRAY_SIZE(editor_hl_boulderdash);
4501 static int num_editor_el_boulderdash = ARRAY_SIZE(editor_el_boulderdash);
4502
4503 static int editor_hl_boulderdash_native[] =
4504 {
4505   EL_INTERNAL_CASCADE_BD_NATIVE_ACTIVE,
4506   EL_CHAR('B'),
4507   EL_CHAR('D'),
4508   EL_EMPTY,
4509 };
4510
4511 static int editor_el_boulderdash_native[] =
4512 {
4513   EL_EMPTY,
4514   EL_BD_SAND,
4515   EL_BD_ROCK,
4516   EL_BD_DIAMOND,
4517
4518   EL_BD_STEELWALL,
4519   EL_BD_WALL,
4520   EL_EMPTY,
4521   EL_BD_MAGIC_WALL,
4522
4523   EL_BD_AMOEBA,
4524   EL_BD_BUTTERFLY_UP,
4525   EL_BD_FIREFLY_UP,
4526   EL_BD_EXIT_CLOSED,
4527
4528   EL_BD_BUTTERFLY_LEFT,
4529   EL_BD_FIREFLY_LEFT,
4530   EL_BD_BUTTERFLY_RIGHT,
4531   EL_BD_FIREFLY_RIGHT,
4532
4533   EL_BD_INBOX,
4534   EL_BD_BUTTERFLY_DOWN,
4535   EL_BD_FIREFLY_DOWN,
4536   EL_BD_EXIT_OPEN,
4537
4538   EL_BD_AMOEBA_2,
4539   EL_BD_BUTTERFLY_2_UP,
4540   EL_BD_FIREFLY_2_UP,
4541   EL_EMPTY,
4542
4543   EL_BD_BUTTERFLY_2_LEFT,
4544   EL_BD_FIREFLY_2_LEFT,
4545   EL_BD_BUTTERFLY_2_RIGHT,
4546   EL_BD_FIREFLY_2_RIGHT,
4547
4548   EL_EMPTY,
4549   EL_BD_BUTTERFLY_2_DOWN,
4550   EL_BD_FIREFLY_2_DOWN,
4551   EL_EMPTY,
4552
4553   EL_EMPTY,
4554   EL_BD_DRAGONFLY_UP,
4555   EL_BD_STONEFLY_UP,
4556   EL_EMPTY,
4557
4558   EL_BD_DRAGONFLY_LEFT,
4559   EL_BD_STONEFLY_LEFT,
4560   EL_BD_DRAGONFLY_RIGHT,
4561   EL_BD_STONEFLY_RIGHT,
4562
4563   EL_EMPTY,
4564   EL_BD_DRAGONFLY_DOWN,
4565   EL_BD_STONEFLY_DOWN,
4566   EL_EMPTY,
4567
4568   EL_EMPTY,
4569   EL_BD_BITER_UP,
4570   EL_BD_COW_UP,
4571   EL_EMPTY,
4572
4573   EL_BD_BITER_LEFT,
4574   EL_BD_COW_LEFT,
4575   EL_BD_BITER_RIGHT,
4576   EL_BD_COW_RIGHT,
4577
4578   EL_EMPTY,
4579   EL_BD_BITER_DOWN,
4580   EL_BD_COW_DOWN,
4581   EL_EMPTY,
4582
4583   EL_EMPTY,
4584   EL_BD_SAND,
4585   EL_BD_SAND_BALL,
4586   EL_BD_SAND_LOOSE,
4587
4588   EL_BD_SAND_SLOPED_UP_LEFT,
4589   EL_BD_SAND_SLOPED_UP_RIGHT,
4590   EL_BD_WALL_SLOPED_UP_LEFT,
4591   EL_BD_WALL_SLOPED_UP_RIGHT,
4592
4593   EL_BD_SAND_SLOPED_DOWN_LEFT,
4594   EL_BD_SAND_SLOPED_DOWN_RIGHT,
4595   EL_BD_WALL_SLOPED_DOWN_LEFT,
4596   EL_BD_WALL_SLOPED_DOWN_RIGHT,
4597
4598   EL_BD_FLYING_DIAMOND,
4599   EL_BD_FLYING_ROCK,
4600   EL_BD_STEELWALL_SLOPED_UP_LEFT,
4601   EL_BD_STEELWALL_SLOPED_UP_RIGHT,
4602
4603   EL_BD_NUT,
4604   EL_BD_MEGA_ROCK,
4605   EL_BD_STEELWALL_SLOPED_DOWN_LEFT,
4606   EL_BD_STEELWALL_SLOPED_DOWN_RIGHT,
4607
4608   EL_BD_SAND_2,
4609   EL_BD_WALL_NON_SLOPED,
4610   EL_BD_WALL,
4611   EL_BD_MAGIC_WALL,
4612
4613   EL_BD_EXIT_CLOSED,
4614   EL_BD_EXIT_OPEN,
4615   EL_BD_INVISIBLE_EXIT_CLOSED,
4616   EL_BD_INVISIBLE_EXIT_OPEN,
4617
4618   EL_BD_STEELWALL,
4619   EL_BD_STEELWALL_EXPLODABLE,
4620   EL_BD_STEELWALL_DIGGABLE,
4621   EL_BD_WALL_DIGGABLE,
4622
4623   EL_BD_EXPANDABLE_WALL_HORIZONTAL,
4624   EL_BD_EXPANDABLE_WALL_VERTICAL,
4625   EL_BD_EXPANDABLE_WALL_ANY,
4626   EL_BD_CREATURE_SWITCH,
4627
4628   EL_BD_EXPANDABLE_STEELWALL_HORIZONTAL,
4629   EL_BD_EXPANDABLE_STEELWALL_VERTICAL,
4630   EL_BD_EXPANDABLE_STEELWALL_ANY,
4631   EL_BD_EXPANDABLE_WALL_SWITCH_HORIZONTAL,
4632
4633   EL_BD_BITER_SWITCH_1,
4634   EL_BD_REPLICATOR_SWITCH,
4635   EL_BD_CONVEYOR_SWITCH,
4636   EL_BD_CONVEYOR_DIR_SWITCH_RIGHT,
4637
4638   EL_BD_ACID,
4639   EL_BD_FALLING_WALL,
4640   EL_BD_BOX,
4641   EL_BD_TIME_PENALTY,
4642
4643   EL_BD_GRAVESTONE,
4644   EL_BD_ROCK_GLUED,
4645   EL_BD_DIAMOND_GLUED,
4646   EL_BD_DIAMOND_KEY,
4647
4648   EL_BD_TRAPPED_DIAMOND,
4649   EL_BD_CLOCK,
4650   EL_BD_SAND_GLUED,
4651   EL_BD_WATER,
4652
4653   EL_BD_KEY_1,
4654   EL_BD_KEY_2,
4655   EL_BD_KEY_3,
4656   EL_EMPTY,
4657
4658   EL_BD_WALL_KEY_1,
4659   EL_BD_WALL_KEY_2,
4660   EL_BD_WALL_KEY_3,
4661   EL_BD_WALL_DIAMOND,
4662
4663   EL_BD_GATE_1,
4664   EL_BD_GATE_2,
4665   EL_BD_GATE_3,
4666   EL_BD_POT,
4667
4668   EL_BD_GRAVITY_SWITCH,
4669   EL_BD_PNEUMATIC_HAMMER,
4670   EL_BD_TELEPORTER,
4671   EL_BD_SKELETON,
4672
4673   EL_BD_AMOEBA,
4674   EL_BD_AMOEBA_2,
4675   EL_BD_REPLICATOR,
4676   EL_BD_BLADDER_SPENDER,
4677
4678   EL_BD_CONVEYOR_LEFT,
4679   EL_BD_CONVEYOR_RIGHT,
4680   EL_BD_BOMB,
4681   EL_BD_NITRO_PACK,
4682
4683   EL_BD_LAVA,
4684   EL_BD_SWEET,
4685   EL_BD_VOODOO_DOLL,
4686   EL_BD_SLIME,
4687
4688   EL_BD_BLADDER,
4689   EL_BD_WAITING_ROCK,
4690   EL_BD_CHASING_ROCK,
4691   EL_BD_GHOST,
4692
4693   EL_BD_PLAYER,
4694   EL_BD_PLAYER_WITH_BOMB,
4695   EL_BD_PLAYER_GLUED,
4696   EL_BD_PLAYER_STIRRING,
4697 };
4698 static int *editor_hl_boulderdash_native_ptr = editor_hl_boulderdash_native;
4699 static int *editor_el_boulderdash_native_ptr = editor_el_boulderdash_native;
4700 static int num_editor_hl_boulderdash_native = ARRAY_SIZE(editor_hl_boulderdash_native);
4701 static int num_editor_el_boulderdash_native = ARRAY_SIZE(editor_el_boulderdash_native);
4702
4703 static int editor_hl_emerald_mine[] =
4704 {
4705   EL_INTERNAL_CASCADE_EM_ACTIVE,
4706   EL_CHAR('E'),
4707   EL_CHAR('M'),
4708   EL_EMPTY,
4709 };
4710
4711 static int editor_el_emerald_mine[] =
4712 {
4713   EL_SAND,
4714   EL_ROCK,
4715   EL_QUICKSAND_EMPTY,
4716   EL_QUICKSAND_FULL,
4717
4718   EL_STEELWALL,
4719   EL_WALL,
4720   EL_WALL_SLIPPERY,
4721   EL_MAGIC_WALL,
4722
4723   EL_EMERALD,
4724   EL_DIAMOND,
4725   EL_NUT,
4726   EL_BOMB,
4727
4728   EL_EM_DYNAMITE,
4729   EL_EM_DYNAMITE_ACTIVE,
4730   EL_EM_EXIT_CLOSED,
4731   EL_EM_EXIT_OPEN,
4732
4733   EL_YAMYAM_UP,
4734   EL_BUG_UP,
4735   EL_SPACESHIP_UP,
4736   EL_ROBOT,
4737
4738   EL_BUG_LEFT,
4739   EL_SPACESHIP_LEFT,
4740   EL_BUG_RIGHT,
4741   EL_SPACESHIP_RIGHT,
4742
4743   EL_ROBOT_WHEEL,
4744   EL_BUG_DOWN,
4745   EL_SPACESHIP_DOWN,
4746   EL_INVISIBLE_WALL,
4747
4748   EL_ACID_POOL_TOPLEFT,
4749   EL_ACID,
4750   EL_ACID_POOL_TOPRIGHT,
4751   EL_AMOEBA_DROP,
4752
4753   EL_ACID_POOL_BOTTOMLEFT,
4754   EL_ACID_POOL_BOTTOM,
4755   EL_ACID_POOL_BOTTOMRIGHT,
4756   EL_AMOEBA_WET,
4757
4758   EL_EM_KEY_1,
4759   EL_EM_KEY_2,
4760   EL_EM_KEY_3,
4761   EL_EM_KEY_4,
4762
4763   EL_EM_GATE_1,
4764   EL_EM_GATE_2,
4765   EL_EM_GATE_3,
4766   EL_EM_GATE_4,
4767
4768   EL_EM_GATE_1_GRAY,
4769   EL_EM_GATE_2_GRAY,
4770   EL_EM_GATE_3_GRAY,
4771   EL_EM_GATE_4_GRAY,
4772 };
4773 static int *editor_hl_emerald_mine_ptr = editor_hl_emerald_mine;
4774 static int *editor_el_emerald_mine_ptr = editor_el_emerald_mine;
4775 static int num_editor_hl_emerald_mine = ARRAY_SIZE(editor_hl_emerald_mine);
4776 static int num_editor_el_emerald_mine = ARRAY_SIZE(editor_el_emerald_mine);
4777
4778 static int editor_hl_emerald_mine_club[] =
4779 {
4780   EL_INTERNAL_CASCADE_EMC_ACTIVE,
4781   EL_CHAR('E'),
4782   EL_CHAR('M'),
4783   EL_CHAR('C'),
4784 };
4785
4786 static int editor_el_emerald_mine_club[] =
4787 {
4788   EL_EMC_KEY_5,
4789   EL_EMC_KEY_6,
4790   EL_EMC_KEY_7,
4791   EL_EMC_KEY_8,
4792
4793   EL_EMC_GATE_5,
4794   EL_EMC_GATE_6,
4795   EL_EMC_GATE_7,
4796   EL_EMC_GATE_8,
4797
4798   EL_EMC_GATE_5_GRAY,
4799   EL_EMC_GATE_6_GRAY,
4800   EL_EMC_GATE_7_GRAY,
4801   EL_EMC_GATE_8_GRAY,
4802
4803   EL_EMC_STEELWALL_1,
4804   EL_EMC_STEELWALL_2,
4805   EL_EMC_STEELWALL_3,
4806   EL_EMC_STEELWALL_4,
4807
4808   EL_EMC_WALL_13,
4809   EL_EMC_WALL_14,
4810   EL_EMC_WALL_15,
4811   EL_EMC_WALL_16,
4812
4813   EL_EMC_WALL_SLIPPERY_1,
4814   EL_EMC_WALL_SLIPPERY_2,
4815   EL_EMC_WALL_SLIPPERY_3,
4816   EL_EMC_WALL_SLIPPERY_4,
4817
4818   EL_EMC_WALL_1,
4819   EL_EMC_WALL_2,
4820   EL_EMC_WALL_3,
4821   EL_EMC_WALL_4,
4822
4823   EL_EMC_WALL_5,
4824   EL_EMC_WALL_6,
4825   EL_EMC_WALL_7,
4826   EL_EMC_WALL_8,
4827
4828   EL_EMC_WALL_9,
4829   EL_EMC_WALL_10,
4830   EL_EMC_WALL_11,
4831   EL_EMC_WALL_12,
4832
4833   EL_EMC_GRASS,
4834   EL_EMC_FAKE_GRASS,
4835   EL_EMC_PLANT,
4836   EL_EMC_DRIPPER,
4837
4838   EL_EMC_MAGIC_BALL,
4839   EL_EMC_MAGIC_BALL_SWITCH,
4840   EL_EMC_LENSES,
4841   EL_EMC_MAGNIFIER,
4842
4843   EL_SPRING_LEFT,
4844   EL_SPRING,
4845   EL_SPRING_RIGHT,
4846   EL_EMC_SPRING_BUMPER,
4847
4848   EL_BALLOON,
4849   EL_YAMYAM_UP,
4850   EL_BALLOON_SWITCH_UP,
4851   EL_BALLOON_SWITCH_ANY,
4852
4853   EL_YAMYAM_LEFT,
4854   EL_BALLOON_SWITCH_LEFT,
4855   EL_YAMYAM_RIGHT,
4856   EL_BALLOON_SWITCH_RIGHT,
4857
4858   EL_EMC_ANDROID,
4859   EL_YAMYAM_DOWN,
4860   EL_BALLOON_SWITCH_DOWN,
4861   EL_BALLOON_SWITCH_NONE,
4862 };
4863 static int *editor_hl_emerald_mine_club_ptr = editor_hl_emerald_mine_club;
4864 static int *editor_el_emerald_mine_club_ptr = editor_el_emerald_mine_club;
4865 static int num_editor_hl_emerald_mine_club = ARRAY_SIZE(editor_hl_emerald_mine_club);
4866 static int num_editor_el_emerald_mine_club = ARRAY_SIZE(editor_el_emerald_mine_club);
4867
4868 static int editor_hl_rnd[] =
4869 {
4870   EL_INTERNAL_CASCADE_RND_ACTIVE,
4871   EL_CHAR('R'),
4872   EL_CHAR('N'),
4873   EL_CHAR('D'),
4874 };
4875
4876 static int editor_el_rnd[] =
4877 {
4878   EL_DYNAMITE,                  // RND
4879   EL_DYNAMITE_ACTIVE,           // RND
4880   EL_EMPTY,
4881   EL_EMPTY,
4882
4883   EL_KEY_1,
4884   EL_KEY_2,
4885   EL_KEY_3,
4886   EL_KEY_4,
4887
4888   EL_GATE_1,
4889   EL_GATE_2,
4890   EL_GATE_3,
4891   EL_GATE_4,
4892
4893   EL_GATE_1_GRAY,
4894   EL_GATE_2_GRAY,
4895   EL_GATE_3_GRAY,
4896   EL_GATE_4_GRAY,
4897
4898   EL_ARROW_LEFT,
4899   EL_ARROW_RIGHT,
4900   EL_ARROW_UP,
4901   EL_ARROW_DOWN,
4902
4903   EL_AMOEBA_DEAD,
4904   EL_AMOEBA_DRY,
4905   EL_AMOEBA_FULL,
4906   EL_GAME_OF_LIFE,
4907
4908   EL_EMERALD_YELLOW,
4909   EL_EMERALD_RED,
4910   EL_EMERALD_PURPLE,
4911   EL_BIOMAZE,
4912
4913   EL_WALL_EMERALD_YELLOW,
4914   EL_WALL_EMERALD_RED,
4915   EL_WALL_EMERALD_PURPLE,
4916   EL_WALL_BD_DIAMOND,
4917
4918   EL_SPEED_PILL,
4919   EL_PACMAN_UP,
4920   EL_TIME_ORB_FULL,
4921   EL_TIME_ORB_EMPTY,
4922
4923   EL_PACMAN_LEFT,
4924   EL_DARK_YAMYAM,
4925   EL_PACMAN_RIGHT,
4926   EL_YAMYAM,                    // RND
4927
4928   EL_BLACK_ORB,
4929   EL_PACMAN_DOWN,
4930   EL_LAMP,
4931   EL_LAMP_ACTIVE,
4932
4933   EL_DYNABOMB_INCREASE_NUMBER,
4934   EL_DYNABOMB_INCREASE_SIZE,
4935   EL_DYNABOMB_INCREASE_POWER,
4936   EL_STONEBLOCK,
4937
4938   EL_MOLE,
4939   EL_PENGUIN,
4940   EL_PIG,
4941   EL_DRAGON,
4942
4943   EL_BUG,
4944   EL_MOLE_UP,
4945   EL_BD_BUTTERFLY,
4946   EL_BD_FIREFLY,
4947
4948   EL_MOLE_LEFT,
4949   EL_SATELLITE,
4950   EL_MOLE_RIGHT,
4951   EL_PACMAN,
4952
4953   EL_SPACESHIP,
4954   EL_MOLE_DOWN,
4955   EL_INVISIBLE_STEELWALL,
4956   EL_INVISIBLE_WALL,
4957
4958   EL_EXPANDABLE_WALL,
4959   EL_EXPANDABLE_WALL_HORIZONTAL,
4960   EL_EXPANDABLE_WALL_VERTICAL,
4961   EL_EXPANDABLE_WALL_ANY,
4962 };
4963 static int *editor_hl_rnd_ptr = editor_hl_rnd;
4964 static int *editor_el_rnd_ptr = editor_el_rnd;
4965 static int num_editor_hl_rnd = ARRAY_SIZE(editor_hl_rnd);
4966 static int num_editor_el_rnd = ARRAY_SIZE(editor_el_rnd);
4967
4968 static int editor_hl_sokoban[] =
4969 {
4970   EL_INTERNAL_CASCADE_SB_ACTIVE,
4971   EL_CHAR('S'),
4972   EL_CHAR('B'),
4973   EL_EMPTY,
4974 };
4975
4976 static int editor_el_sokoban[] =
4977 {
4978   EL_SOKOBAN_OBJECT,
4979   EL_SOKOBAN_FIELD_EMPTY,
4980   EL_SOKOBAN_FIELD_FULL,
4981   EL_SOKOBAN_FIELD_PLAYER,
4982 };
4983 static int *editor_hl_sokoban_ptr = editor_hl_sokoban;
4984 static int *editor_el_sokoban_ptr = editor_el_sokoban;
4985 static int num_editor_hl_sokoban = ARRAY_SIZE(editor_hl_sokoban);
4986 static int num_editor_el_sokoban = ARRAY_SIZE(editor_el_sokoban);
4987
4988 static int editor_hl_supaplex[] =
4989 {
4990   EL_INTERNAL_CASCADE_SP_ACTIVE,
4991   EL_CHAR('S'),
4992   EL_CHAR('P'),
4993   EL_EMPTY,
4994 };
4995
4996 static int editor_el_supaplex[] =
4997 {
4998   EL_SP_MURPHY,
4999   EL_EMPTY,
5000   EL_SP_BASE,
5001   EL_SP_BUGGY_BASE,
5002
5003   EL_SP_INFOTRON,
5004   EL_SP_ZONK,
5005   EL_SP_SNIKSNAK,
5006   EL_SP_ELECTRON,
5007
5008   EL_SP_DISK_RED,
5009   EL_SP_DISK_ORANGE,
5010   EL_SP_DISK_YELLOW,
5011   EL_SP_TERMINAL,
5012
5013   EL_SP_EXIT_CLOSED,
5014   EL_SP_PORT_HORIZONTAL,
5015   EL_SP_PORT_VERTICAL,
5016   EL_SP_PORT_ANY,
5017
5018   EL_SP_PORT_LEFT,
5019   EL_SP_PORT_RIGHT,
5020   EL_SP_PORT_UP,
5021   EL_SP_PORT_DOWN,
5022
5023   EL_SP_GRAVITY_PORT_LEFT,
5024   EL_SP_GRAVITY_PORT_RIGHT,
5025   EL_SP_GRAVITY_PORT_UP,
5026   EL_SP_GRAVITY_PORT_DOWN,
5027
5028   EL_SP_GRAVITY_ON_PORT_LEFT,
5029   EL_SP_GRAVITY_ON_PORT_RIGHT,
5030   EL_SP_GRAVITY_ON_PORT_UP,
5031   EL_SP_GRAVITY_ON_PORT_DOWN,
5032
5033   EL_SP_GRAVITY_OFF_PORT_LEFT,
5034   EL_SP_GRAVITY_OFF_PORT_RIGHT,
5035   EL_SP_GRAVITY_OFF_PORT_UP,
5036   EL_SP_GRAVITY_OFF_PORT_DOWN,
5037
5038   EL_SP_HARDWARE_GRAY,
5039   EL_SP_HARDWARE_GREEN,
5040   EL_SP_HARDWARE_BLUE,
5041   EL_SP_HARDWARE_RED,
5042
5043   EL_SP_HARDWARE_BASE_1,
5044   EL_SP_HARDWARE_BASE_2,
5045   EL_SP_HARDWARE_BASE_3,
5046   EL_SP_HARDWARE_BASE_4,
5047
5048   EL_SP_HARDWARE_BASE_5,
5049   EL_SP_HARDWARE_BASE_6,
5050   EL_SP_HARDWARE_YELLOW,
5051   EL_SP_CHIP_TOP,
5052
5053   EL_SP_CHIP_SINGLE,
5054   EL_SP_CHIP_LEFT,
5055   EL_SP_CHIP_RIGHT,
5056   EL_SP_CHIP_BOTTOM,
5057 };
5058 static int *editor_hl_supaplex_ptr = editor_hl_supaplex;
5059 static int *editor_el_supaplex_ptr = editor_el_supaplex;
5060 static int num_editor_hl_supaplex = ARRAY_SIZE(editor_hl_supaplex);
5061 static int num_editor_el_supaplex = ARRAY_SIZE(editor_el_supaplex);
5062
5063 static int editor_hl_diamond_caves[] =
5064 {
5065   EL_INTERNAL_CASCADE_DC_ACTIVE,
5066   EL_CHAR('D'),
5067   EL_CHAR('C'),
5068   EL_CHAR('2'),
5069 };
5070
5071 static int editor_el_diamond_caves[] =
5072 {
5073   EL_EM_STEEL_EXIT_CLOSED,      // DC2
5074   EL_EM_STEEL_EXIT_OPEN,        // DC2
5075   EL_WALL_EMERALD,              // DC2
5076   EL_WALL_DIAMOND,              // DC2
5077
5078   EL_PEARL,
5079   EL_CRYSTAL,
5080   EL_WALL_PEARL,
5081   EL_WALL_CRYSTAL,
5082
5083   EL_CONVEYOR_BELT_1_LEFT,
5084   EL_CONVEYOR_BELT_1_MIDDLE,
5085   EL_CONVEYOR_BELT_1_RIGHT,
5086   EL_CONVEYOR_BELT_1_SWITCH_MIDDLE,
5087
5088   EL_CONVEYOR_BELT_2_LEFT,
5089   EL_CONVEYOR_BELT_2_MIDDLE,
5090   EL_CONVEYOR_BELT_2_RIGHT,
5091   EL_CONVEYOR_BELT_2_SWITCH_MIDDLE,
5092
5093   EL_CONVEYOR_BELT_3_LEFT,
5094   EL_CONVEYOR_BELT_3_MIDDLE,
5095   EL_CONVEYOR_BELT_3_RIGHT,
5096   EL_CONVEYOR_BELT_3_SWITCH_MIDDLE,
5097
5098   EL_CONVEYOR_BELT_4_LEFT,
5099   EL_CONVEYOR_BELT_4_MIDDLE,
5100   EL_CONVEYOR_BELT_4_RIGHT,
5101   EL_CONVEYOR_BELT_4_SWITCH_MIDDLE,
5102
5103   EL_CONVEYOR_BELT_1_SWITCH_LEFT,
5104   EL_CONVEYOR_BELT_2_SWITCH_LEFT,
5105   EL_CONVEYOR_BELT_3_SWITCH_LEFT,
5106   EL_CONVEYOR_BELT_4_SWITCH_LEFT,
5107
5108   EL_CONVEYOR_BELT_1_SWITCH_RIGHT,
5109   EL_CONVEYOR_BELT_2_SWITCH_RIGHT,
5110   EL_CONVEYOR_BELT_3_SWITCH_RIGHT,
5111   EL_CONVEYOR_BELT_4_SWITCH_RIGHT,
5112
5113   EL_TIMEGATE_CLOSED,
5114   EL_TIMEGATE_OPEN,
5115   EL_TIMEGATE_SWITCH,
5116   EL_DC_TIMEGATE_SWITCH,
5117
5118   EL_SWITCHGATE_CLOSED,
5119   EL_SWITCHGATE_OPEN,
5120   EL_SWITCHGATE_SWITCH_UP,
5121   EL_SWITCHGATE_SWITCH_DOWN,
5122
5123   EL_LIGHT_SWITCH,
5124   EL_LIGHT_SWITCH_ACTIVE,
5125   EL_DC_SWITCHGATE_SWITCH_UP,
5126   EL_DC_SWITCHGATE_SWITCH_DOWN,
5127
5128   EL_STEEL_EXIT_CLOSED,
5129   EL_STEEL_EXIT_OPEN,
5130   EL_STEELWALL_SLIPPERY,
5131   EL_INVISIBLE_SAND,
5132
5133   EL_QUICKSAND_FAST_EMPTY,
5134   EL_QUICKSAND_FAST_FULL,
5135   EL_LANDMINE,
5136   EL_DC_LANDMINE,
5137
5138   EL_SHIELD_NORMAL,
5139   EL_SHIELD_DEADLY,
5140   EL_EXTRA_TIME,
5141   EL_DC_MAGIC_WALL,
5142
5143   EL_ENVELOPE_1,
5144   EL_ENVELOPE_2,
5145   EL_ENVELOPE_3,
5146   EL_ENVELOPE_4,
5147
5148   EL_SIGN_RADIOACTIVITY,
5149   EL_SIGN_WHEELCHAIR,
5150   EL_SIGN_PARKING,
5151   EL_SIGN_NO_ENTRY,
5152
5153   EL_SIGN_GIVE_WAY,
5154   EL_SIGN_ENTRY_FORBIDDEN,
5155   EL_SIGN_EMERGENCY_EXIT,
5156   EL_SIGN_YIN_YANG,
5157
5158 #if 0
5159   EL_SIGN_SPERMS,
5160   EL_SIGN_BULLET,
5161   EL_SIGN_HEART,
5162   EL_SIGN_CROSS,
5163
5164   EL_SIGN_FRANKIE,
5165   EL_EMPTY,
5166   EL_EMPTY,
5167   EL_EMPTY,
5168
5169   EL_SPERMS,
5170   EL_BULLET,
5171   EL_HEART,
5172   EL_CROSS,
5173
5174   EL_FRANKIE,
5175   EL_EMPTY,
5176   EL_EMPTY,
5177   EL_EMPTY,
5178 #endif
5179
5180   EL_DC_STEELWALL_2_SINGLE,
5181   EL_DC_STEELWALL_2_TOP,
5182   EL_SIGN_EXCLAMATION,
5183   EL_SIGN_STOP,
5184
5185   EL_DC_STEELWALL_2_LEFT,
5186   EL_DC_STEELWALL_2_MIDDLE,
5187   EL_DC_STEELWALL_2_HORIZONTAL,
5188   EL_DC_STEELWALL_2_RIGHT,
5189
5190   EL_DC_STEELWALL_1_TOPLEFT,
5191   EL_DC_STEELWALL_2_VERTICAL,
5192   EL_DC_STEELWALL_1_TOPRIGHT,
5193   EL_DC_GATE_WHITE,
5194
5195   EL_DC_STEELWALL_1_VERTICAL,
5196   EL_DC_STEELWALL_2_BOTTOM,
5197   EL_DC_KEY_WHITE,
5198   EL_DC_GATE_WHITE_GRAY,
5199
5200   EL_DC_STEELWALL_1_BOTTOMLEFT,
5201   EL_DC_STEELWALL_1_HORIZONTAL,
5202   EL_DC_STEELWALL_1_BOTTOMRIGHT,
5203   EL_DC_GATE_FAKE_GRAY,
5204
5205   EL_DC_STEELWALL_1_BOTTOMRIGHT_2,
5206   EL_DC_STEELWALL_1_BOTTOM,
5207   EL_DC_STEELWALL_1_BOTTOMLEFT_2,
5208   EL_EXPANDABLE_STEELWALL_HORIZONTAL,
5209
5210   EL_DC_STEELWALL_1_RIGHT,
5211   EL_EMPTY,
5212   EL_DC_STEELWALL_1_LEFT,
5213   EL_EXPANDABLE_STEELWALL_VERTICAL,
5214
5215   EL_DC_STEELWALL_1_TOPRIGHT_2,
5216   EL_DC_STEELWALL_1_TOP,
5217   EL_DC_STEELWALL_1_TOPLEFT_2,
5218   EL_EXPANDABLE_STEELWALL_ANY,
5219 };
5220 static int *editor_hl_diamond_caves_ptr = editor_hl_diamond_caves;
5221 static int *editor_el_diamond_caves_ptr = editor_el_diamond_caves;
5222 static int num_editor_hl_diamond_caves = ARRAY_SIZE(editor_hl_diamond_caves);
5223 static int num_editor_el_diamond_caves = ARRAY_SIZE(editor_el_diamond_caves);
5224
5225 static int editor_hl_dx_boulderdash[] =
5226 {
5227   EL_INTERNAL_CASCADE_DX_ACTIVE,
5228   EL_CHAR('D'),
5229   EL_CHAR('X'),
5230   EL_EMPTY,
5231 };
5232
5233 static int editor_el_dx_boulderdash[] =
5234 {
5235   EL_EMPTY,
5236   EL_TUBE_RIGHT_DOWN,
5237   EL_TUBE_HORIZONTAL_DOWN,
5238   EL_TUBE_LEFT_DOWN,
5239
5240   EL_TUBE_HORIZONTAL,
5241   EL_TUBE_VERTICAL_RIGHT,
5242   EL_TUBE_ANY,
5243   EL_TUBE_VERTICAL_LEFT,
5244
5245   EL_TUBE_VERTICAL,
5246   EL_TUBE_RIGHT_UP,
5247   EL_TUBE_HORIZONTAL_UP,
5248   EL_TUBE_LEFT_UP,
5249
5250   EL_TRAP,
5251   EL_DX_SUPABOMB,
5252   EL_EMPTY,
5253   EL_EMPTY
5254 };
5255 static int *editor_hl_dx_boulderdash_ptr = editor_hl_dx_boulderdash;
5256 static int *editor_el_dx_boulderdash_ptr = editor_el_dx_boulderdash;
5257 static int num_editor_hl_dx_boulderdash = ARRAY_SIZE(editor_hl_dx_boulderdash);
5258 static int num_editor_el_dx_boulderdash = ARRAY_SIZE(editor_el_dx_boulderdash);
5259
5260 static int editor_hl_mirror_magic[] =
5261 {
5262   EL_INTERNAL_CASCADE_MM_ACTIVE,
5263   EL_CHAR('M'),
5264   EL_CHAR('M'),
5265   EL_EMPTY,
5266 };
5267
5268 static int editor_el_mirror_magic[] =
5269 {
5270   EL_MM_MCDUFFIN_RIGHT,
5271   EL_MM_MCDUFFIN_UP,
5272   EL_MM_MCDUFFIN_LEFT,
5273   EL_MM_MCDUFFIN_DOWN,
5274
5275   EL_MM_MIRROR_START,
5276   EL_MM_MIRROR_FIXED_START,
5277   EL_MM_POLARIZER_START,
5278   EL_MM_POLARIZER_CROSS_START,
5279
5280   EL_MM_TELEPORTER_RED_START,
5281   EL_MM_TELEPORTER_YELLOW_START,
5282   EL_MM_TELEPORTER_GREEN_START,
5283   EL_MM_TELEPORTER_BLUE_START,
5284
5285   EL_MM_PRISM,
5286   EL_MM_FUSE_ACTIVE,
5287   EL_MM_PACMAN_RIGHT,
5288   EL_MM_EXIT_CLOSED,
5289
5290   EL_MM_KETTLE,
5291   EL_MM_BOMB,
5292   EL_MM_KEY,
5293   EL_MM_FUEL_FULL,
5294
5295   EL_MM_LIGHTBULB,
5296   EL_MM_LIGHTBULB_ACTIVE,
5297   EL_MM_GRAY_BALL,
5298   EL_MM_LIGHTBALL,
5299
5300   EL_MM_STEEL_WALL,
5301   EL_MM_WOODEN_WALL,
5302   EL_MM_ICE_WALL,
5303   EL_MM_AMOEBA_WALL,
5304
5305   EL_MM_STEEL_LOCK,
5306   EL_MM_WOODEN_LOCK,
5307   EL_MM_STEEL_BLOCK,
5308   EL_MM_WOODEN_BLOCK,
5309
5310   EL_MM_STEEL_GRID_FIXED_1,
5311   EL_MM_STEEL_GRID_FIXED_2,
5312   EL_MM_STEEL_GRID_FIXED_3,
5313   EL_MM_STEEL_GRID_FIXED_4,
5314
5315   EL_MM_WOODEN_GRID_FIXED_1,
5316   EL_MM_WOODEN_GRID_FIXED_2,
5317   EL_MM_WOODEN_GRID_FIXED_3,
5318   EL_MM_WOODEN_GRID_FIXED_4,
5319
5320   EL_MM_ENVELOPE_1,
5321   EL_MM_ENVELOPE_2,
5322   EL_MM_ENVELOPE_3,
5323   EL_MM_ENVELOPE_4
5324 };
5325 static int *editor_hl_mirror_magic_ptr = editor_hl_mirror_magic;
5326 static int *editor_el_mirror_magic_ptr = editor_el_mirror_magic;
5327 static int num_editor_hl_mirror_magic = ARRAY_SIZE(editor_hl_mirror_magic);
5328 static int num_editor_el_mirror_magic = ARRAY_SIZE(editor_el_mirror_magic);
5329
5330 static int editor_hl_deflektor[] =
5331 {
5332   EL_INTERNAL_CASCADE_DF_ACTIVE,
5333   EL_CHAR('D'),
5334   EL_CHAR('F'),
5335   EL_EMPTY,
5336 };
5337
5338 static int editor_el_deflektor[] =
5339 {
5340   EL_DF_LASER_RIGHT,
5341   EL_DF_LASER_UP,
5342   EL_DF_LASER_LEFT,
5343   EL_DF_LASER_DOWN,
5344
5345   EL_DF_RECEIVER_RIGHT,
5346   EL_DF_RECEIVER_UP,
5347   EL_DF_RECEIVER_LEFT,
5348   EL_DF_RECEIVER_DOWN,
5349
5350   EL_DF_MIRROR_START,
5351   EL_DF_MIRROR_ROTATING_START,
5352   EL_DF_MIRROR_FIXED_START,
5353   EL_DF_CELL,
5354
5355   EL_DF_FIBRE_OPTIC_RED_1,
5356   EL_DF_FIBRE_OPTIC_YELLOW_1,
5357   EL_DF_FIBRE_OPTIC_GREEN_1,
5358   EL_DF_FIBRE_OPTIC_BLUE_1,
5359
5360   EL_DF_STEEL_GRID_FIXED_START,
5361   EL_DF_STEEL_GRID_ROTATING_START,
5362   EL_DF_WOODEN_GRID_FIXED_START,
5363   EL_DF_WOODEN_GRID_ROTATING_START,
5364
5365   EL_DF_STEEL_WALL,
5366   EL_DF_WOODEN_WALL,
5367   EL_DF_REFRACTOR,
5368   EL_DF_MINE,
5369
5370   EL_DF_SLOPE_1,
5371   EL_DF_SLOPE_2,
5372   EL_DF_SLOPE_3,
5373   EL_DF_SLOPE_4
5374 };
5375 static int *editor_hl_deflektor_ptr = editor_hl_deflektor;
5376 static int *editor_el_deflektor_ptr = editor_el_deflektor;
5377 static int num_editor_hl_deflektor = ARRAY_SIZE(editor_hl_deflektor);
5378 static int num_editor_el_deflektor = ARRAY_SIZE(editor_el_deflektor);
5379
5380 static int editor_hl_chars[] =
5381 {
5382   EL_INTERNAL_CASCADE_CHARS_ACTIVE,
5383   EL_CHAR('T'),
5384   EL_CHAR('X'),
5385   EL_CHAR('T'),
5386 };
5387
5388 static int editor_el_chars[] =
5389 {
5390   EL_CHAR(' '),
5391   EL_CHAR('!'),
5392   EL_CHAR('"'),
5393   EL_CHAR('#'),
5394
5395   EL_CHAR('$'),
5396   EL_CHAR('%'),
5397   EL_CHAR('&'),
5398   EL_CHAR('\''),
5399
5400   EL_CHAR('('),
5401   EL_CHAR(')'),
5402   EL_CHAR('*'),
5403   EL_CHAR('+'),
5404
5405   EL_CHAR(','),
5406   EL_CHAR('-'),
5407   EL_CHAR('.'),
5408   EL_CHAR('/'),
5409
5410   EL_CHAR('0'),
5411   EL_CHAR('1'),
5412   EL_CHAR('2'),
5413   EL_CHAR('3'),
5414
5415   EL_CHAR('4'),
5416   EL_CHAR('5'),
5417   EL_CHAR('6'),
5418   EL_CHAR('7'),
5419
5420   EL_CHAR('8'),
5421   EL_CHAR('9'),
5422   EL_CHAR(':'),
5423   EL_CHAR(';'),
5424
5425   EL_CHAR('<'),
5426   EL_CHAR('='),
5427   EL_CHAR('>'),
5428   EL_CHAR('?'),
5429
5430   EL_CHAR('@'),
5431   EL_CHAR('A'),
5432   EL_CHAR('B'),
5433   EL_CHAR('C'),
5434
5435   EL_CHAR('D'),
5436   EL_CHAR('E'),
5437   EL_CHAR('F'),
5438   EL_CHAR('G'),
5439
5440   EL_CHAR('H'),
5441   EL_CHAR('I'),
5442   EL_CHAR('J'),
5443   EL_CHAR('K'),
5444
5445   EL_CHAR('L'),
5446   EL_CHAR('M'),
5447   EL_CHAR('N'),
5448   EL_CHAR('O'),
5449
5450   EL_CHAR('P'),
5451   EL_CHAR('Q'),
5452   EL_CHAR('R'),
5453   EL_CHAR('S'),
5454
5455   EL_CHAR('T'),
5456   EL_CHAR('U'),
5457   EL_CHAR('V'),
5458   EL_CHAR('W'),
5459
5460   EL_CHAR('X'),
5461   EL_CHAR('Y'),
5462   EL_CHAR('Z'),
5463   EL_CHAR('['),
5464
5465   EL_CHAR('\\'),
5466   EL_CHAR(']'),
5467   EL_CHAR('^'),
5468   EL_CHAR('_'),
5469
5470   EL_CHAR(CHAR_BYTE_COPYRIGHT),
5471   EL_CHAR(CHAR_BYTE_UMLAUT_A),
5472   EL_CHAR(CHAR_BYTE_UMLAUT_O),
5473   EL_CHAR(CHAR_BYTE_UMLAUT_U),
5474
5475   EL_CHAR(CHAR_BYTE_DEGREE),
5476   EL_CHAR(CHAR_BYTE_REGISTERED),
5477   EL_CHAR(FONT_ASCII_CURSOR),
5478   EL_CHAR(FONT_ASCII_BUTTON),
5479
5480   EL_CHAR(FONT_ASCII_UP),
5481   EL_CHAR(FONT_ASCII_DOWN),
5482   EL_CHAR(' '),
5483   EL_CHAR(' ')
5484 };
5485 static int *editor_hl_chars_ptr = editor_hl_chars;
5486 static int *editor_el_chars_ptr = editor_el_chars;
5487 static int num_editor_hl_chars = ARRAY_SIZE(editor_hl_chars);
5488 static int num_editor_el_chars = ARRAY_SIZE(editor_el_chars);
5489
5490 static int editor_hl_steel_chars[] =
5491 {
5492   EL_INTERNAL_CASCADE_STEEL_CHARS_ACTIVE,
5493   EL_STEEL_CHAR('T'),
5494   EL_STEEL_CHAR('X'),
5495   EL_STEEL_CHAR('T'),
5496 };
5497
5498 static int editor_el_steel_chars[] =
5499 {
5500   EL_STEEL_CHAR(' '),
5501   EL_STEEL_CHAR('!'),
5502   EL_STEEL_CHAR('"'),
5503   EL_STEEL_CHAR('#'),
5504
5505   EL_STEEL_CHAR('$'),
5506   EL_STEEL_CHAR('%'),
5507   EL_STEEL_CHAR('&'),
5508   EL_STEEL_CHAR('\''),
5509
5510   EL_STEEL_CHAR('('),
5511   EL_STEEL_CHAR(')'),
5512   EL_STEEL_CHAR('*'),
5513   EL_STEEL_CHAR('+'),
5514
5515   EL_STEEL_CHAR(','),
5516   EL_STEEL_CHAR('-'),
5517   EL_STEEL_CHAR('.'),
5518   EL_STEEL_CHAR('/'),
5519
5520   EL_STEEL_CHAR('0'),
5521   EL_STEEL_CHAR('1'),
5522   EL_STEEL_CHAR('2'),
5523   EL_STEEL_CHAR('3'),
5524
5525   EL_STEEL_CHAR('4'),
5526   EL_STEEL_CHAR('5'),
5527   EL_STEEL_CHAR('6'),
5528   EL_STEEL_CHAR('7'),
5529
5530   EL_STEEL_CHAR('8'),
5531   EL_STEEL_CHAR('9'),
5532   EL_STEEL_CHAR(':'),
5533   EL_STEEL_CHAR(';'),
5534
5535   EL_STEEL_CHAR('<'),
5536   EL_STEEL_CHAR('='),
5537   EL_STEEL_CHAR('>'),
5538   EL_STEEL_CHAR('?'),
5539
5540   EL_STEEL_CHAR('@'),
5541   EL_STEEL_CHAR('A'),
5542   EL_STEEL_CHAR('B'),
5543   EL_STEEL_CHAR('C'),
5544
5545   EL_STEEL_CHAR('D'),
5546   EL_STEEL_CHAR('E'),
5547   EL_STEEL_CHAR('F'),
5548   EL_STEEL_CHAR('G'),
5549
5550   EL_STEEL_CHAR('H'),
5551   EL_STEEL_CHAR('I'),
5552   EL_STEEL_CHAR('J'),
5553   EL_STEEL_CHAR('K'),
5554
5555   EL_STEEL_CHAR('L'),
5556   EL_STEEL_CHAR('M'),
5557   EL_STEEL_CHAR('N'),
5558   EL_STEEL_CHAR('O'),
5559
5560   EL_STEEL_CHAR('P'),
5561   EL_STEEL_CHAR('Q'),
5562   EL_STEEL_CHAR('R'),
5563   EL_STEEL_CHAR('S'),
5564
5565   EL_STEEL_CHAR('T'),
5566   EL_STEEL_CHAR('U'),
5567   EL_STEEL_CHAR('V'),
5568   EL_STEEL_CHAR('W'),
5569
5570   EL_STEEL_CHAR('X'),
5571   EL_STEEL_CHAR('Y'),
5572   EL_STEEL_CHAR('Z'),
5573   EL_STEEL_CHAR('['),
5574
5575   EL_STEEL_CHAR('\\'),
5576   EL_STEEL_CHAR(']'),
5577   EL_STEEL_CHAR('^'),
5578   EL_STEEL_CHAR('_'),
5579
5580   EL_STEEL_CHAR(CHAR_BYTE_COPYRIGHT),
5581   EL_STEEL_CHAR(CHAR_BYTE_UMLAUT_A),
5582   EL_STEEL_CHAR(CHAR_BYTE_UMLAUT_O),
5583   EL_STEEL_CHAR(CHAR_BYTE_UMLAUT_U),
5584
5585   EL_STEEL_CHAR(CHAR_BYTE_DEGREE),
5586   EL_STEEL_CHAR(CHAR_BYTE_REGISTERED),
5587   EL_STEEL_CHAR(FONT_ASCII_CURSOR),
5588   EL_STEEL_CHAR(FONT_ASCII_BUTTON),
5589
5590   EL_STEEL_CHAR(FONT_ASCII_UP),
5591   EL_STEEL_CHAR(FONT_ASCII_DOWN),
5592   EL_STEEL_CHAR(' '),
5593   EL_STEEL_CHAR(' ')
5594 };
5595 static int *editor_hl_steel_chars_ptr = editor_hl_steel_chars;
5596 static int *editor_el_steel_chars_ptr = editor_el_steel_chars;
5597 static int num_editor_hl_steel_chars = ARRAY_SIZE(editor_hl_steel_chars);
5598 static int num_editor_el_steel_chars = ARRAY_SIZE(editor_el_steel_chars);
5599
5600 static int editor_hl_custom[] =
5601 {
5602   EL_INTERNAL_CASCADE_CE_ACTIVE,
5603   EL_CHAR('C'),
5604   EL_CHAR('E'),
5605   EL_EMPTY,
5606 };
5607
5608 static int editor_el_custom[] =
5609 {
5610   EL_CUSTOM_START + 0,
5611   EL_CUSTOM_START + 1,
5612   EL_CUSTOM_START + 2,
5613   EL_CUSTOM_START + 3,
5614
5615   EL_CUSTOM_START + 4,
5616   EL_CUSTOM_START + 5,
5617   EL_CUSTOM_START + 6,
5618   EL_CUSTOM_START + 7,
5619
5620   EL_CUSTOM_START + 8,
5621   EL_CUSTOM_START + 9,
5622   EL_CUSTOM_START + 10,
5623   EL_CUSTOM_START + 11,
5624
5625   EL_CUSTOM_START + 12,
5626   EL_CUSTOM_START + 13,
5627   EL_CUSTOM_START + 14,
5628   EL_CUSTOM_START + 15,
5629
5630   EL_CUSTOM_START + 16,
5631   EL_CUSTOM_START + 17,
5632   EL_CUSTOM_START + 18,
5633   EL_CUSTOM_START + 19,
5634
5635   EL_CUSTOM_START + 20,
5636   EL_CUSTOM_START + 21,
5637   EL_CUSTOM_START + 22,
5638   EL_CUSTOM_START + 23,
5639
5640   EL_CUSTOM_START + 24,
5641   EL_CUSTOM_START + 25,
5642   EL_CUSTOM_START + 26,
5643   EL_CUSTOM_START + 27,
5644
5645   EL_CUSTOM_START + 28,
5646   EL_CUSTOM_START + 29,
5647   EL_CUSTOM_START + 30,
5648   EL_CUSTOM_START + 31,
5649
5650   EL_CUSTOM_START + 32,
5651   EL_CUSTOM_START + 33,
5652   EL_CUSTOM_START + 34,
5653   EL_CUSTOM_START + 35,
5654
5655   EL_CUSTOM_START + 36,
5656   EL_CUSTOM_START + 37,
5657   EL_CUSTOM_START + 38,
5658   EL_CUSTOM_START + 39,
5659
5660   EL_CUSTOM_START + 40,
5661   EL_CUSTOM_START + 41,
5662   EL_CUSTOM_START + 42,
5663   EL_CUSTOM_START + 43,
5664
5665   EL_CUSTOM_START + 44,
5666   EL_CUSTOM_START + 45,
5667   EL_CUSTOM_START + 46,
5668   EL_CUSTOM_START + 47,
5669
5670   EL_CUSTOM_START + 48,
5671   EL_CUSTOM_START + 49,
5672   EL_CUSTOM_START + 50,
5673   EL_CUSTOM_START + 51,
5674
5675   EL_CUSTOM_START + 52,
5676   EL_CUSTOM_START + 53,
5677   EL_CUSTOM_START + 54,
5678   EL_CUSTOM_START + 55,
5679
5680   EL_CUSTOM_START + 56,
5681   EL_CUSTOM_START + 57,
5682   EL_CUSTOM_START + 58,
5683   EL_CUSTOM_START + 59,
5684
5685   EL_CUSTOM_START + 60,
5686   EL_CUSTOM_START + 61,
5687   EL_CUSTOM_START + 62,
5688   EL_CUSTOM_START + 63,
5689
5690   EL_CUSTOM_START + 64,
5691   EL_CUSTOM_START + 65,
5692   EL_CUSTOM_START + 66,
5693   EL_CUSTOM_START + 67,
5694
5695   EL_CUSTOM_START + 68,
5696   EL_CUSTOM_START + 69,
5697   EL_CUSTOM_START + 70,
5698   EL_CUSTOM_START + 71,
5699
5700   EL_CUSTOM_START + 72,
5701   EL_CUSTOM_START + 73,
5702   EL_CUSTOM_START + 74,
5703   EL_CUSTOM_START + 75,
5704
5705   EL_CUSTOM_START + 76,
5706   EL_CUSTOM_START + 77,
5707   EL_CUSTOM_START + 78,
5708   EL_CUSTOM_START + 79,
5709
5710   EL_CUSTOM_START + 80,
5711   EL_CUSTOM_START + 81,
5712   EL_CUSTOM_START + 82,
5713   EL_CUSTOM_START + 83,
5714
5715   EL_CUSTOM_START + 84,
5716   EL_CUSTOM_START + 85,
5717   EL_CUSTOM_START + 86,
5718   EL_CUSTOM_START + 87,
5719
5720   EL_CUSTOM_START + 88,
5721   EL_CUSTOM_START + 89,
5722   EL_CUSTOM_START + 90,
5723   EL_CUSTOM_START + 91,
5724
5725   EL_CUSTOM_START + 92,
5726   EL_CUSTOM_START + 93,
5727   EL_CUSTOM_START + 94,
5728   EL_CUSTOM_START + 95,
5729
5730   EL_CUSTOM_START + 96,
5731   EL_CUSTOM_START + 97,
5732   EL_CUSTOM_START + 98,
5733   EL_CUSTOM_START + 99,
5734
5735   EL_CUSTOM_START + 100,
5736   EL_CUSTOM_START + 101,
5737   EL_CUSTOM_START + 102,
5738   EL_CUSTOM_START + 103,
5739
5740   EL_CUSTOM_START + 104,
5741   EL_CUSTOM_START + 105,
5742   EL_CUSTOM_START + 106,
5743   EL_CUSTOM_START + 107,
5744
5745   EL_CUSTOM_START + 108,
5746   EL_CUSTOM_START + 109,
5747   EL_CUSTOM_START + 110,
5748   EL_CUSTOM_START + 111,
5749
5750   EL_CUSTOM_START + 112,
5751   EL_CUSTOM_START + 113,
5752   EL_CUSTOM_START + 114,
5753   EL_CUSTOM_START + 115,
5754
5755   EL_CUSTOM_START + 116,
5756   EL_CUSTOM_START + 117,
5757   EL_CUSTOM_START + 118,
5758   EL_CUSTOM_START + 119,
5759
5760   EL_CUSTOM_START + 120,
5761   EL_CUSTOM_START + 121,
5762   EL_CUSTOM_START + 122,
5763   EL_CUSTOM_START + 123,
5764
5765   EL_CUSTOM_START + 124,
5766   EL_CUSTOM_START + 125,
5767   EL_CUSTOM_START + 126,
5768   EL_CUSTOM_START + 127,
5769
5770   EL_CUSTOM_START + 128,
5771   EL_CUSTOM_START + 129,
5772   EL_CUSTOM_START + 130,
5773   EL_CUSTOM_START + 131,
5774
5775   EL_CUSTOM_START + 132,
5776   EL_CUSTOM_START + 133,
5777   EL_CUSTOM_START + 134,
5778   EL_CUSTOM_START + 135,
5779
5780   EL_CUSTOM_START + 136,
5781   EL_CUSTOM_START + 137,
5782   EL_CUSTOM_START + 138,
5783   EL_CUSTOM_START + 139,
5784
5785   EL_CUSTOM_START + 140,
5786   EL_CUSTOM_START + 141,
5787   EL_CUSTOM_START + 142,
5788   EL_CUSTOM_START + 143,
5789
5790   EL_CUSTOM_START + 144,
5791   EL_CUSTOM_START + 145,
5792   EL_CUSTOM_START + 146,
5793   EL_CUSTOM_START + 147,
5794
5795   EL_CUSTOM_START + 148,
5796   EL_CUSTOM_START + 149,
5797   EL_CUSTOM_START + 150,
5798   EL_CUSTOM_START + 151,
5799
5800   EL_CUSTOM_START + 152,
5801   EL_CUSTOM_START + 153,
5802   EL_CUSTOM_START + 154,
5803   EL_CUSTOM_START + 155,
5804
5805   EL_CUSTOM_START + 156,
5806   EL_CUSTOM_START + 157,
5807   EL_CUSTOM_START + 158,
5808   EL_CUSTOM_START + 159,
5809
5810   EL_CUSTOM_START + 160,
5811   EL_CUSTOM_START + 161,
5812   EL_CUSTOM_START + 162,
5813   EL_CUSTOM_START + 163,
5814
5815   EL_CUSTOM_START + 164,
5816   EL_CUSTOM_START + 165,
5817   EL_CUSTOM_START + 166,
5818   EL_CUSTOM_START + 167,
5819
5820   EL_CUSTOM_START + 168,
5821   EL_CUSTOM_START + 169,
5822   EL_CUSTOM_START + 170,
5823   EL_CUSTOM_START + 171,
5824
5825   EL_CUSTOM_START + 172,
5826   EL_CUSTOM_START + 173,
5827   EL_CUSTOM_START + 174,
5828   EL_CUSTOM_START + 175,
5829
5830   EL_CUSTOM_START + 176,
5831   EL_CUSTOM_START + 177,
5832   EL_CUSTOM_START + 178,
5833   EL_CUSTOM_START + 179,
5834
5835   EL_CUSTOM_START + 180,
5836   EL_CUSTOM_START + 181,
5837   EL_CUSTOM_START + 182,
5838   EL_CUSTOM_START + 183,
5839
5840   EL_CUSTOM_START + 184,
5841   EL_CUSTOM_START + 185,
5842   EL_CUSTOM_START + 186,
5843   EL_CUSTOM_START + 187,
5844
5845   EL_CUSTOM_START + 188,
5846   EL_CUSTOM_START + 189,
5847   EL_CUSTOM_START + 190,
5848   EL_CUSTOM_START + 191,
5849
5850   EL_CUSTOM_START + 192,
5851   EL_CUSTOM_START + 193,
5852   EL_CUSTOM_START + 194,
5853   EL_CUSTOM_START + 195,
5854
5855   EL_CUSTOM_START + 196,
5856   EL_CUSTOM_START + 197,
5857   EL_CUSTOM_START + 198,
5858   EL_CUSTOM_START + 199,
5859
5860   EL_CUSTOM_START + 200,
5861   EL_CUSTOM_START + 201,
5862   EL_CUSTOM_START + 202,
5863   EL_CUSTOM_START + 203,
5864
5865   EL_CUSTOM_START + 204,
5866   EL_CUSTOM_START + 205,
5867   EL_CUSTOM_START + 206,
5868   EL_CUSTOM_START + 207,
5869
5870   EL_CUSTOM_START + 208,
5871   EL_CUSTOM_START + 209,
5872   EL_CUSTOM_START + 210,
5873   EL_CUSTOM_START + 211,
5874
5875   EL_CUSTOM_START + 212,
5876   EL_CUSTOM_START + 213,
5877   EL_CUSTOM_START + 214,
5878   EL_CUSTOM_START + 215,
5879
5880   EL_CUSTOM_START + 216,
5881   EL_CUSTOM_START + 217,
5882   EL_CUSTOM_START + 218,
5883   EL_CUSTOM_START + 219,
5884
5885   EL_CUSTOM_START + 220,
5886   EL_CUSTOM_START + 221,
5887   EL_CUSTOM_START + 222,
5888   EL_CUSTOM_START + 223,
5889
5890   EL_CUSTOM_START + 224,
5891   EL_CUSTOM_START + 225,
5892   EL_CUSTOM_START + 226,
5893   EL_CUSTOM_START + 227,
5894
5895   EL_CUSTOM_START + 228,
5896   EL_CUSTOM_START + 229,
5897   EL_CUSTOM_START + 230,
5898   EL_CUSTOM_START + 231,
5899
5900   EL_CUSTOM_START + 232,
5901   EL_CUSTOM_START + 233,
5902   EL_CUSTOM_START + 234,
5903   EL_CUSTOM_START + 235,
5904
5905   EL_CUSTOM_START + 236,
5906   EL_CUSTOM_START + 237,
5907   EL_CUSTOM_START + 238,
5908   EL_CUSTOM_START + 239,
5909
5910   EL_CUSTOM_START + 240,
5911   EL_CUSTOM_START + 241,
5912   EL_CUSTOM_START + 242,
5913   EL_CUSTOM_START + 243,
5914
5915   EL_CUSTOM_START + 244,
5916   EL_CUSTOM_START + 245,
5917   EL_CUSTOM_START + 246,
5918   EL_CUSTOM_START + 247,
5919
5920   EL_CUSTOM_START + 248,
5921   EL_CUSTOM_START + 249,
5922   EL_CUSTOM_START + 250,
5923   EL_CUSTOM_START + 251,
5924
5925   EL_CUSTOM_START + 252,
5926   EL_CUSTOM_START + 253,
5927   EL_CUSTOM_START + 254,
5928   EL_CUSTOM_START + 255
5929 };
5930 static int *editor_hl_custom_ptr = editor_hl_custom;
5931 static int *editor_el_custom_ptr = editor_el_custom;
5932 static int num_editor_hl_custom = ARRAY_SIZE(editor_hl_custom);
5933 static int num_editor_el_custom = ARRAY_SIZE(editor_el_custom);
5934
5935 static int editor_hl_group[] =
5936 {
5937   EL_INTERNAL_CASCADE_GE_ACTIVE,
5938   EL_CHAR('G'),
5939   EL_CHAR('E'),
5940   EL_EMPTY,
5941 };
5942
5943 static int editor_el_group[] =
5944 {
5945   EL_GROUP_START + 0,
5946   EL_GROUP_START + 1,
5947   EL_GROUP_START + 2,
5948   EL_GROUP_START + 3,
5949
5950   EL_GROUP_START + 4,
5951   EL_GROUP_START + 5,
5952   EL_GROUP_START + 6,
5953   EL_GROUP_START + 7,
5954
5955   EL_GROUP_START + 8,
5956   EL_GROUP_START + 9,
5957   EL_GROUP_START + 10,
5958   EL_GROUP_START + 11,
5959
5960   EL_GROUP_START + 12,
5961   EL_GROUP_START + 13,
5962   EL_GROUP_START + 14,
5963   EL_GROUP_START + 15,
5964
5965   EL_GROUP_START + 16,
5966   EL_GROUP_START + 17,
5967   EL_GROUP_START + 18,
5968   EL_GROUP_START + 19,
5969
5970   EL_GROUP_START + 20,
5971   EL_GROUP_START + 21,
5972   EL_GROUP_START + 22,
5973   EL_GROUP_START + 23,
5974
5975   EL_GROUP_START + 24,
5976   EL_GROUP_START + 25,
5977   EL_GROUP_START + 26,
5978   EL_GROUP_START + 27,
5979
5980   EL_GROUP_START + 28,
5981   EL_GROUP_START + 29,
5982   EL_GROUP_START + 30,
5983   EL_GROUP_START + 31
5984 };
5985 static int *editor_hl_group_ptr = editor_hl_group;
5986 static int *editor_el_group_ptr = editor_el_group;
5987 static int num_editor_hl_group = ARRAY_SIZE(editor_hl_group);
5988 static int num_editor_el_group = ARRAY_SIZE(editor_el_group);
5989
5990 static int editor_hl_empty_space[] =
5991 {
5992   EL_INTERNAL_CASCADE_ES_ACTIVE,
5993   EL_CHAR('E'),
5994   EL_CHAR('S'),
5995   EL_EMPTY,
5996 };
5997
5998 static int editor_el_empty_space[] =
5999 {
6000   EL_EMPTY_SPACE_1,
6001   EL_EMPTY_SPACE_2,
6002   EL_EMPTY_SPACE_3,
6003   EL_EMPTY_SPACE_4,
6004
6005   EL_EMPTY_SPACE_5,
6006   EL_EMPTY_SPACE_6,
6007   EL_EMPTY_SPACE_7,
6008   EL_EMPTY_SPACE_8,
6009
6010   EL_EMPTY_SPACE_9,
6011   EL_EMPTY_SPACE_10,
6012   EL_EMPTY_SPACE_11,
6013   EL_EMPTY_SPACE_12,
6014
6015   EL_EMPTY_SPACE_13,
6016   EL_EMPTY_SPACE_14,
6017   EL_EMPTY_SPACE_15,
6018   EL_EMPTY_SPACE_16
6019 };
6020 static int *editor_hl_empty_space_ptr = editor_hl_empty_space;
6021 static int *editor_el_empty_space_ptr = editor_el_empty_space;
6022 static int num_editor_hl_empty_space = ARRAY_SIZE(editor_hl_empty_space);
6023 static int num_editor_el_empty_space = ARRAY_SIZE(editor_el_empty_space);
6024
6025 static int editor_hl_reference[] =
6026 {
6027   EL_INTERNAL_CASCADE_REF_ACTIVE,
6028   EL_CHAR('R'),
6029   EL_CHAR('E'),
6030   EL_CHAR('F')
6031 };
6032
6033 static int editor_el_reference[] =
6034 {
6035   EL_TRIGGER_PLAYER,
6036   EL_TRIGGER_ELEMENT,
6037   EL_TRIGGER_CE_VALUE,
6038   EL_TRIGGER_CE_SCORE,
6039
6040   EL_SELF,
6041   EL_ANY_ELEMENT,
6042   EL_CURRENT_CE_VALUE,
6043   EL_CURRENT_CE_SCORE,
6044
6045   EL_PREV_CE_8,
6046   EL_PREV_CE_7,
6047   EL_PREV_CE_6,
6048   EL_PREV_CE_5,
6049
6050   EL_PREV_CE_4,
6051   EL_PREV_CE_3,
6052   EL_PREV_CE_2,
6053   EL_PREV_CE_1,
6054
6055   EL_NEXT_CE_1,
6056   EL_NEXT_CE_2,
6057   EL_NEXT_CE_3,
6058   EL_NEXT_CE_4,
6059
6060   EL_NEXT_CE_5,
6061   EL_NEXT_CE_6,
6062   EL_NEXT_CE_7,
6063   EL_NEXT_CE_8,
6064 };
6065 static int *editor_hl_reference_ptr = editor_hl_reference;
6066 static int *editor_el_reference_ptr = editor_el_reference;
6067 static int num_editor_hl_reference = ARRAY_SIZE(editor_hl_reference);
6068 static int num_editor_el_reference = ARRAY_SIZE(editor_el_reference);
6069
6070 static int editor_hl_user_defined[] =
6071 {
6072   EL_INTERNAL_CASCADE_USER_ACTIVE,
6073   EL_CHAR('M'),
6074   EL_CHAR('Y'),
6075   EL_EMPTY,
6076 };
6077
6078 static int *editor_hl_user_defined_ptr = editor_hl_user_defined;
6079 static int *editor_el_user_defined_ptr = NULL;
6080 static int num_editor_hl_user_defined = ARRAY_SIZE(editor_hl_user_defined);
6081 static int num_editor_el_user_defined = 0;
6082
6083 static int editor_hl_dynamic[] =
6084 {
6085   EL_INTERNAL_CASCADE_DYNAMIC_ACTIVE,
6086   EL_CHAR('U'),
6087   EL_CHAR('S'),
6088   EL_CHAR('E'),
6089 };
6090
6091 static int *editor_hl_dynamic_ptr = editor_hl_dynamic;
6092 static int *editor_el_dynamic_ptr = NULL;
6093 static int num_editor_hl_dynamic = ARRAY_SIZE(editor_hl_dynamic);
6094 static int num_editor_el_dynamic = 0;
6095
6096 static int editor_hl_empty[] = { EL_EMPTY };
6097 static int *editor_el_empty = NULL;     // dynamically allocated
6098
6099 static int *editor_hl_empty_ptr = editor_hl_empty;
6100 static int *editor_el_empty_ptr = NULL;
6101 static int num_editor_hl_empty = 0;
6102 static int num_editor_el_empty = 0;     // dynamically determined, if needed
6103
6104 static boolean use_el_empty = FALSE;
6105
6106 static int *editor_elements = NULL;     // dynamically allocated
6107 static int num_editor_elements = 0;     // dynamically determined
6108
6109 static boolean setup_editor_cascade_never = FALSE;
6110
6111 static boolean setup_editor_el_players                  = TRUE;
6112 static boolean setup_editor_el_boulderdash              = TRUE;
6113 static boolean setup_editor_el_boulderdash_native       = TRUE;
6114 static boolean setup_editor_el_emerald_mine             = TRUE;
6115 static boolean setup_editor_el_emerald_mine_club        = TRUE;
6116 static boolean setup_editor_el_more                     = TRUE;
6117 static boolean setup_editor_el_sokoban                  = TRUE;
6118 static boolean setup_editor_el_supaplex                 = TRUE;
6119 static boolean setup_editor_el_diamond_caves            = TRUE;
6120 static boolean setup_editor_el_dx_boulderdash           = TRUE;
6121 static boolean setup_editor_el_mirror_magic             = TRUE;
6122 static boolean setup_editor_el_deflektor                = TRUE;
6123 static boolean setup_editor_el_chars                    = TRUE;
6124 static boolean setup_editor_el_steel_chars              = TRUE;
6125 static boolean setup_editor_el_custom                   = TRUE;
6126 static boolean setup_editor_el_user_defined             = TRUE;
6127 static boolean setup_editor_el_dynamic                  = TRUE;
6128
6129 static int editor_hl_unused[] = { EL_EMPTY };
6130 static int *editor_hl_unused_ptr = editor_hl_unused;
6131 static int num_editor_hl_unused = 0;
6132
6133 static struct
6134 {
6135   boolean *setup_value;
6136   boolean *setup_cascade_value;
6137
6138   int **headline_list;
6139   int *headline_list_size;
6140
6141   int **element_list;
6142   int *element_list_size;
6143
6144   boolean last_setup_value;
6145 }
6146 editor_elements_info[] =
6147 {
6148   {
6149     &setup_editor_el_players,
6150     &setup_editor_cascade_never,
6151     &editor_hl_unused_ptr,              &num_editor_hl_unused,
6152     &editor_el_players_ptr,             &num_editor_el_players
6153   },
6154   {
6155     &setup_editor_el_boulderdash,
6156     &setup.editor_cascade.el_bd,
6157     &editor_hl_boulderdash_ptr,         &num_editor_hl_boulderdash,
6158     &editor_el_boulderdash_ptr,         &num_editor_el_boulderdash
6159   },
6160   {
6161     &setup_editor_el_boulderdash_native,
6162     &setup.editor_cascade.el_bd_native,
6163     &editor_hl_boulderdash_native_ptr,  &num_editor_hl_boulderdash_native,
6164     &editor_el_boulderdash_native_ptr,  &num_editor_el_boulderdash_native
6165   },
6166   {
6167     &setup_editor_el_emerald_mine,
6168     &setup.editor_cascade.el_em,
6169     &editor_hl_emerald_mine_ptr,        &num_editor_hl_emerald_mine,
6170     &editor_el_emerald_mine_ptr,        &num_editor_el_emerald_mine
6171   },
6172   {
6173     &setup_editor_el_emerald_mine_club,
6174     &setup.editor_cascade.el_emc,
6175     &editor_hl_emerald_mine_club_ptr,   &num_editor_hl_emerald_mine_club,
6176     &editor_el_emerald_mine_club_ptr,   &num_editor_el_emerald_mine_club
6177   },
6178   {
6179     &setup_editor_el_more,
6180     &setup.editor_cascade.el_rnd,
6181     &editor_hl_rnd_ptr,                 &num_editor_hl_rnd,
6182     &editor_el_rnd_ptr,                 &num_editor_el_rnd
6183   },
6184   {
6185     &setup_editor_el_sokoban,
6186     &setup.editor_cascade.el_sb,
6187     &editor_hl_sokoban_ptr,             &num_editor_hl_sokoban,
6188     &editor_el_sokoban_ptr,             &num_editor_el_sokoban
6189   },
6190   {
6191     &setup_editor_el_supaplex,
6192     &setup.editor_cascade.el_sp,
6193     &editor_hl_supaplex_ptr,            &num_editor_hl_supaplex,
6194     &editor_el_supaplex_ptr,            &num_editor_el_supaplex
6195   },
6196   {
6197     &setup_editor_el_diamond_caves,
6198     &setup.editor_cascade.el_dc,
6199     &editor_hl_diamond_caves_ptr,       &num_editor_hl_diamond_caves,
6200     &editor_el_diamond_caves_ptr,       &num_editor_el_diamond_caves
6201   },
6202   {
6203     &setup_editor_el_dx_boulderdash,
6204     &setup.editor_cascade.el_dx,
6205     &editor_hl_dx_boulderdash_ptr,      &num_editor_hl_dx_boulderdash,
6206     &editor_el_dx_boulderdash_ptr,      &num_editor_el_dx_boulderdash
6207   },
6208   {
6209     &setup_editor_el_mirror_magic,
6210     &setup.editor_cascade.el_mm,
6211     &editor_hl_mirror_magic_ptr,        &num_editor_hl_mirror_magic,
6212     &editor_el_mirror_magic_ptr,        &num_editor_el_mirror_magic
6213   },
6214   {
6215     &setup_editor_el_deflektor,
6216     &setup.editor_cascade.el_df,
6217     &editor_hl_deflektor_ptr,           &num_editor_hl_deflektor,
6218     &editor_el_deflektor_ptr,           &num_editor_el_deflektor
6219   },
6220   {
6221     &setup_editor_el_chars,
6222     &setup.editor_cascade.el_chars,
6223     &editor_hl_chars_ptr,               &num_editor_hl_chars,
6224     &editor_el_chars_ptr,               &num_editor_el_chars
6225   },
6226   {
6227     &setup_editor_el_steel_chars,
6228     &setup.editor_cascade.el_steel_chars,
6229     &editor_hl_steel_chars_ptr,         &num_editor_hl_steel_chars,
6230     &editor_el_steel_chars_ptr,         &num_editor_el_steel_chars
6231   },
6232   {
6233     &setup_editor_el_custom,
6234     &setup.editor_cascade.el_ce,
6235     &editor_hl_custom_ptr,              &num_editor_hl_custom,
6236     &editor_el_custom_ptr,              &num_editor_el_custom
6237   },
6238   {
6239     &setup_editor_el_custom,
6240     &setup.editor_cascade.el_ge,
6241     &editor_hl_group_ptr,               &num_editor_hl_group,
6242     &editor_el_group_ptr,               &num_editor_el_group
6243   },
6244   {
6245     &setup_editor_el_custom,
6246     &setup.editor_cascade.el_es,
6247     &editor_hl_empty_space_ptr,         &num_editor_hl_empty_space,
6248     &editor_el_empty_space_ptr,         &num_editor_el_empty_space
6249   },
6250   {
6251     &setup_editor_el_custom,
6252     &setup.editor_cascade.el_ref,
6253     &editor_hl_reference_ptr,           &num_editor_hl_reference,
6254     &editor_el_reference_ptr,           &num_editor_el_reference
6255   },
6256   {
6257     &setup_editor_el_user_defined,
6258     &setup.editor_cascade.el_user,
6259     &editor_hl_user_defined_ptr,        &num_editor_hl_user_defined,
6260     &editor_el_user_defined_ptr,        &num_editor_el_user_defined
6261   },
6262   {
6263     &setup_editor_el_dynamic,
6264     &setup.editor_cascade.el_dynamic,
6265     &editor_hl_dynamic_ptr,             &num_editor_hl_dynamic,
6266     &editor_el_dynamic_ptr,             &num_editor_el_dynamic,
6267   },
6268   {
6269     &use_el_empty,
6270     &use_el_empty,
6271     &editor_hl_empty_ptr,               &num_editor_hl_empty,
6272     &editor_el_empty_ptr,               &num_editor_el_empty,
6273   },
6274   {
6275     NULL,
6276     NULL,
6277     NULL,                               NULL,
6278     NULL,                               NULL
6279   }
6280 };
6281
6282 static struct XY xy_directions[] =
6283 {
6284   { -1,  0 },
6285   { +1,  0 },
6286   {  0, -1 },
6287   {  0, +1 }
6288 };
6289
6290
6291 // ----------------------------------------------------------------------------
6292 // functions
6293 // ----------------------------------------------------------------------------
6294
6295 static int getMaxInfoTextLength(void)
6296 {
6297   return (SXSIZE / getFontWidth(INFOTEXT_FONT));
6298 }
6299
6300 static int getTextWidthForGadget(char *text)
6301 {
6302   if (text == NULL)
6303     return 0;
6304
6305   return (getTextWidth(text, FONT_TEXT_1) + ED_GADGET_TEXT_DISTANCE);
6306 }
6307
6308 static int getTextWidthForDrawingArea(char *text)
6309 {
6310   if (text == NULL)
6311     return 0;
6312
6313   return (getTextWidth(text, FONT_TEXT_1) + ED_DRAWINGAREA_TEXT_DISTANCE);
6314 }
6315
6316 static int getRightGadgetBorder(struct GadgetInfo *gi, char *text)
6317 {
6318   return (gi->x + gi->width + getTextWidthForGadget(text));
6319 }
6320
6321 static char *getElementInfoText(int element)
6322 {
6323   char *info_text = NULL;
6324
6325   if (element < MAX_NUM_ELEMENTS)
6326   {
6327     if (strlen(element_info[element].description) > 0)
6328       info_text = element_info[element].description;
6329     else if (element_info[element].custom_description != NULL)
6330       info_text = element_info[element].custom_description;
6331     else if (element_info[element].editor_description != NULL)
6332       info_text = element_info[element].editor_description;
6333   }
6334
6335   if (info_text == NULL)
6336     info_text = INFOTEXT_UNKNOWN_ELEMENT;
6337
6338   return info_text;
6339 }
6340
6341 static char *getElementDescriptionFilenameExt(char *basename)
6342 {
6343   char *elements_subdir = ELEMENTS_DIRECTORY;
6344   static char *elements_subdir2 = NULL;
6345   static char *filename = NULL;
6346
6347   if (elements_subdir2 == NULL)
6348     elements_subdir2 = getPath2(DOCS_DIRECTORY, elements_subdir);
6349
6350   checked_free(filename);
6351
6352   // 1st try: look for element description in current level set directory
6353   filename = getPath3(getCurrentLevelDir(), elements_subdir2, basename);
6354   if (fileExists(filename))
6355     return filename;
6356
6357   free(filename);
6358
6359   // 2nd try: look for element description in the game's base directory
6360   filename = getPath3(options.docs_directory, elements_subdir, basename);
6361   if (fileExists(filename))
6362     return filename;
6363
6364   return NULL;
6365 }
6366
6367 static char *getElementDescriptionFilename(int element)
6368 {
6369   char basename[MAX_FILENAME_LEN];
6370   char *filename;
6371
6372   // 1st try: look for element description file for exactly this element
6373   sprintf(basename, "%s.txt", element_info[element].token_name);
6374   filename = getElementDescriptionFilenameExt(basename);
6375   if (filename != NULL)
6376     return filename;
6377
6378   // 2nd try: look for element description file for this element's class
6379   sprintf(basename, "%s.txt", element_info[element].class_name);
6380   filename = getElementDescriptionFilenameExt(basename);
6381   if (filename != NULL)
6382     return filename;
6383
6384   // 3rd try: look for generic fallback text file for any element
6385   filename = getElementDescriptionFilenameExt(FALLBACK_TEXT_FILENAME);
6386   if (filename != NULL)
6387     return filename;
6388
6389   return NULL;
6390 }
6391
6392 static boolean suppressBorderElement(void)
6393 {
6394   return (level.game_engine_type == GAME_ENGINE_TYPE_MM &&
6395           lev_fieldx <= MAX_ED_FIELDX &&
6396           lev_fieldy <= MAX_ED_FIELDY);
6397 }
6398
6399 static void InitDynamicEditorElementList(int **elements, int *num_elements)
6400 {
6401   boolean element_found[NUM_FILE_ELEMENTS];
6402   int i, x, y;
6403
6404   // initialize list of used elements to "not used"
6405   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6406     element_found[i] = FALSE;
6407
6408   // find all elements used in current level
6409   for (y = 0; y < lev_fieldy; y++)
6410   {
6411     for (x = 0; x < lev_fieldx; x++)
6412     {
6413       if (Tile[x][y] >= NUM_FILE_ELEMENTS)      // should never happen
6414         continue;
6415
6416       if (IS_MM_WALL(Tile[x][y]))
6417         element_found[map_mm_wall_element(Tile[x][y])] = TRUE;
6418       else
6419         element_found[Tile[x][y]] = TRUE;
6420     }
6421   }
6422
6423   *num_elements = 0;
6424
6425   // count number of elements used in current level
6426   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6427     if (element_found[i])
6428       (*num_elements)++;
6429
6430   // add space for up to 3 more elements for padding that may be needed
6431   *num_elements += 3;
6432
6433   // free memory for old list of elements, if needed
6434   checked_free(*elements);
6435
6436   // allocate memory for new list of elements
6437   *elements = checked_malloc(*num_elements * sizeof(int));
6438
6439   *num_elements = 0;
6440
6441   // add all elements used in current level (non-custom/group/empty elements)
6442   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6443     if (element_found[i] && !(IS_CUSTOM_ELEMENT(i) ||
6444                               IS_GROUP_ELEMENT(i) ||
6445                               IS_EMPTY_ELEMENT(i)))
6446       (*elements)[(*num_elements)++] = i;
6447
6448   // add all elements used in current level (custom/group/empty elements)
6449   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6450     if (element_found[i] && (IS_CUSTOM_ELEMENT(i) ||
6451                              IS_GROUP_ELEMENT(i) ||
6452                              IS_EMPTY_ELEMENT(i)))
6453       (*elements)[(*num_elements)++] = i;
6454
6455   while (*num_elements % 4)     // pad with empty elements, if needed
6456     (*elements)[(*num_elements)++] = EL_EMPTY;
6457 }
6458
6459 static void ReinitializeElementList_EnableSections(void)
6460 {
6461   // default: enable all element sections
6462
6463   setup_editor_el_players               = TRUE;
6464   setup_editor_el_boulderdash           = TRUE;
6465   setup_editor_el_boulderdash_native    = TRUE;
6466   setup_editor_el_emerald_mine          = TRUE;
6467   setup_editor_el_emerald_mine_club     = TRUE;
6468   setup_editor_el_more                  = TRUE;
6469   setup_editor_el_sokoban               = TRUE;
6470   setup_editor_el_supaplex              = TRUE;
6471   setup_editor_el_diamond_caves         = TRUE;
6472   setup_editor_el_dx_boulderdash        = TRUE;
6473   setup_editor_el_mirror_magic          = TRUE;
6474   setup_editor_el_deflektor             = TRUE;
6475   setup_editor_el_chars                 = TRUE;
6476   setup_editor_el_steel_chars           = TRUE;
6477
6478   setup_editor_el_custom                = TRUE;
6479   setup_editor_el_user_defined          = TRUE;
6480   setup_editor_el_dynamic               = TRUE;
6481
6482   // now disable all element sections not to be displayed
6483
6484   if (!setup.editor.el_classic)
6485   {
6486     setup_editor_el_players             = FALSE;
6487     setup_editor_el_boulderdash         = FALSE;
6488     setup_editor_el_boulderdash_native  = FALSE;
6489     setup_editor_el_emerald_mine        = FALSE;
6490     setup_editor_el_emerald_mine_club   = FALSE;
6491     setup_editor_el_more                = FALSE;
6492     setup_editor_el_sokoban             = FALSE;
6493     setup_editor_el_supaplex            = FALSE;
6494     setup_editor_el_diamond_caves       = FALSE;
6495     setup_editor_el_dx_boulderdash      = FALSE;
6496     setup_editor_el_mirror_magic        = FALSE;
6497     setup_editor_el_deflektor           = FALSE;
6498     setup_editor_el_chars               = FALSE;
6499     setup_editor_el_steel_chars         = FALSE;
6500   }
6501
6502   if (!setup.editor.el_custom)
6503   {
6504     setup_editor_el_custom              = FALSE;
6505   }
6506
6507   if (!setup.editor.el_user_defined)
6508   {
6509     setup_editor_el_user_defined        = FALSE;
6510   }
6511
6512   if (!setup.editor.el_dynamic)
6513   {
6514     setup_editor_el_dynamic             = FALSE;
6515   }
6516
6517   if (level.game_engine_type == GAME_ENGINE_TYPE_RND)
6518   {
6519     setup_editor_el_boulderdash_native  = FALSE;
6520     setup_editor_el_mirror_magic        = FALSE;
6521     setup_editor_el_deflektor           = FALSE;
6522   }
6523   else if (level.game_engine_type == GAME_ENGINE_TYPE_BD)
6524   {
6525     setup_editor_el_players             = FALSE;
6526     setup_editor_el_boulderdash         = FALSE;
6527     setup_editor_el_emerald_mine        = FALSE;
6528     setup_editor_el_emerald_mine_club   = FALSE;
6529     setup_editor_el_more                = FALSE;
6530     setup_editor_el_sokoban             = FALSE;
6531     setup_editor_el_supaplex            = FALSE;
6532     setup_editor_el_diamond_caves       = FALSE;
6533     setup_editor_el_dx_boulderdash      = FALSE;
6534     setup_editor_el_mirror_magic        = FALSE;
6535     setup_editor_el_deflektor           = FALSE;
6536     setup_editor_el_chars               = FALSE;
6537     setup_editor_el_steel_chars         = FALSE;
6538
6539     setup_editor_el_custom              = FALSE;
6540   }
6541   else if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
6542   {
6543     setup_editor_el_boulderdash         = FALSE;
6544     setup_editor_el_boulderdash_native  = FALSE;
6545     setup_editor_el_more                = FALSE;
6546     setup_editor_el_sokoban             = FALSE;
6547     setup_editor_el_supaplex            = FALSE;
6548     setup_editor_el_diamond_caves       = FALSE;
6549     setup_editor_el_dx_boulderdash      = FALSE;
6550     setup_editor_el_mirror_magic        = FALSE;
6551     setup_editor_el_deflektor           = FALSE;
6552     setup_editor_el_steel_chars         = FALSE;
6553
6554     setup_editor_el_custom              = FALSE;
6555   }
6556   else if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
6557   {
6558     setup_editor_el_players             = FALSE;
6559     setup_editor_el_boulderdash         = FALSE;
6560     setup_editor_el_boulderdash_native  = FALSE;
6561     setup_editor_el_emerald_mine        = FALSE;
6562     setup_editor_el_emerald_mine_club   = FALSE;
6563     setup_editor_el_more                = FALSE;
6564     setup_editor_el_sokoban             = FALSE;
6565     setup_editor_el_diamond_caves       = FALSE;
6566     setup_editor_el_dx_boulderdash      = FALSE;
6567     setup_editor_el_mirror_magic        = FALSE;
6568     setup_editor_el_deflektor           = FALSE;
6569     setup_editor_el_chars               = FALSE;
6570     setup_editor_el_steel_chars         = FALSE;
6571
6572     setup_editor_el_custom              = FALSE;
6573   }
6574   else if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
6575   {
6576     setup_editor_el_players             = FALSE;
6577     setup_editor_el_boulderdash         = FALSE;
6578     setup_editor_el_boulderdash_native  = FALSE;
6579     setup_editor_el_emerald_mine        = FALSE;
6580     setup_editor_el_emerald_mine_club   = FALSE;
6581     setup_editor_el_more                = FALSE;
6582     setup_editor_el_sokoban             = FALSE;
6583     setup_editor_el_supaplex            = FALSE;
6584     setup_editor_el_diamond_caves       = FALSE;
6585     setup_editor_el_dx_boulderdash      = FALSE;
6586     setup_editor_el_steel_chars         = FALSE;
6587
6588     setup_editor_el_custom              = FALSE;
6589   }
6590 }
6591
6592 static void ReinitializeElementList(void)
6593 {
6594   static boolean initialization_needed = TRUE;
6595   int pos = 0;
6596   int i, j;
6597
6598   ReinitializeElementList_EnableSections();
6599
6600   if (initialization_needed)
6601   {
6602     LoadSetup_EditorCascade();          // load last editor cascade state
6603
6604     // initialize editor cascade element from saved cascade state
6605     for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6606     {
6607       int *cascade_element = &(*editor_elements_info[i].headline_list)[0];
6608       boolean cascade_value = *editor_elements_info[i].setup_cascade_value;
6609
6610       if (IS_EDITOR_CASCADE(*cascade_element))
6611         *cascade_element =
6612           (cascade_value ? EL_CASCADE_ACTIVE(*cascade_element) :
6613            EL_CASCADE_INACTIVE(*cascade_element));
6614     }
6615
6616     initialization_needed = FALSE;
6617   }
6618
6619   checked_free(editor_elements);
6620
6621   // reload optional user defined element list for each invocation of editor
6622   LoadUserDefinedEditorElementList(&editor_el_user_defined_ptr,
6623                                    &num_editor_el_user_defined);
6624
6625   // initialize dynamic level element list for each invocation of editor
6626   InitDynamicEditorElementList(&editor_el_dynamic_ptr,
6627                                &num_editor_el_dynamic);
6628
6629   // initialize list of empty elements (used for padding, if needed)
6630   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
6631     editor_el_empty[i] = EL_EMPTY;
6632
6633   // do some sanity checks for each element from element list
6634   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6635   {
6636     for (j = 0; j < *editor_elements_info[i].element_list_size; j++)
6637     {
6638       int element = (*editor_elements_info[i].element_list)[j];
6639
6640       if (element >= NUM_FILE_ELEMENTS)
6641         Warn("editor element %d is runtime element", element);
6642
6643       if (strEqual(getElementInfoText(element), INFOTEXT_UNKNOWN_ELEMENT))
6644         Warn("no element description text for element %d", element);
6645     }
6646   }
6647
6648   num_editor_elements = 0;
6649   use_el_empty = FALSE;
6650
6651   // determine size of element list
6652   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6653   {
6654     if (*editor_elements_info[i].setup_value)
6655     {
6656       boolean found_inactive_cascade = FALSE;
6657
6658       if (setup.editor.el_headlines)
6659       {
6660         // required for correct padding of palette headline buttons
6661         if (*editor_elements_info[i].headline_list_size > 0)
6662           num_editor_elements += ED_ELEMENTLIST_COLS;
6663
6664         for (j = 0; j < *editor_elements_info[i].headline_list_size; j++)
6665         {
6666           int element = (*editor_elements_info[i].headline_list)[j];
6667
6668           if (IS_EDITOR_CASCADE_INACTIVE(element))
6669             found_inactive_cascade = TRUE;
6670         }
6671       }
6672
6673       if (found_inactive_cascade)
6674         continue;
6675
6676       // required for correct padding of palette element buttons
6677       int element_list_size = *editor_elements_info[i].element_list_size;
6678       int element_rows =
6679         (element_list_size + ED_ELEMENTLIST_COLS - 1) / ED_ELEMENTLIST_COLS;
6680       int element_buttons = ED_ELEMENTLIST_COLS * element_rows;
6681
6682       num_editor_elements += element_buttons;
6683     }
6684   }
6685
6686   if (num_editor_elements < ED_NUM_ELEMENTLIST_BUTTONS)
6687   {
6688     // offer at least as many elements as element buttons exist
6689     use_el_empty = TRUE;
6690     num_editor_el_empty = ED_NUM_ELEMENTLIST_BUTTONS - num_editor_elements;
6691
6692     num_editor_elements += num_editor_el_empty;
6693   }
6694   else
6695   {
6696     num_editor_el_empty = 0;
6697   }
6698
6699   editor_elements = checked_malloc(num_editor_elements * sizeof(int));
6700
6701   // fill element list
6702   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6703   {
6704     boolean found_inactive_cascade = FALSE;
6705
6706     if (*editor_elements_info[i].setup_value)
6707     {
6708       if (setup.editor.el_headlines)
6709       {
6710         // required for correct padding of palette headline buttons
6711         int headline_size = (*editor_elements_info[i].headline_list_size > 0 ?
6712                              ED_ELEMENTLIST_COLS : 0);
6713
6714         for (j = 0; j < headline_size; j++)
6715         {
6716           // use empty elements for padding of palette headline buttons
6717           int element = (j < *editor_elements_info[i].headline_list_size ?
6718                          (*editor_elements_info[i].headline_list)[j] :
6719                          editor_el_empty[0]);
6720
6721           editor_elements[pos++] = element;
6722
6723           if (IS_EDITOR_CASCADE_INACTIVE(element))
6724             found_inactive_cascade = TRUE;
6725         }
6726       }
6727
6728       if (found_inactive_cascade)
6729         continue;
6730
6731       // required for correct padding of palette element buttons
6732       int element_list_size = *editor_elements_info[i].element_list_size;
6733       int element_rows =
6734         (element_list_size + ED_ELEMENTLIST_COLS - 1) / ED_ELEMENTLIST_COLS;
6735       int element_buttons = ED_ELEMENTLIST_COLS * element_rows;
6736
6737       // copy all elements from element list
6738       for (j = 0; j < element_list_size; j++)
6739         editor_elements[pos++] = (*editor_elements_info[i].element_list)[j];
6740
6741       // use empty elements for padding of palette element buttons
6742       for (j = 0; j < element_buttons - element_list_size; j++)
6743         editor_elements[pos++] = editor_el_empty[0];
6744     }
6745   }
6746
6747   // (this function is also called before editor gadgets are initialized!)
6748   AdjustElementListScrollbar();
6749 }
6750
6751 void PrintEditorElementList(void)
6752 {
6753   boolean *stop = &setup_editor_el_user_defined;
6754   int i, j;
6755
6756   for (i = 0; editor_elements_info[i].setup_value != stop; i++)
6757   {
6758     int cascade_element = (*editor_elements_info[i].headline_list)[0];
6759
6760     if (IS_EDITOR_CASCADE(cascade_element))
6761     {
6762       int cascade_element_show = EL_CASCADE_INACTIVE(cascade_element);
6763       char *headline = element_info[cascade_element_show].editor_description;
6764
6765       PrintLineWithPrefix("# ", "-", 77);
6766       Print("# %s\n", headline);
6767       PrintLineWithPrefix("# ", "-", 77);
6768     }
6769
6770     for (j = 0; j < *editor_elements_info[i].headline_list_size; j++)
6771     {
6772       int element = (*editor_elements_info[i].headline_list)[j];
6773
6774       if (IS_EDITOR_CASCADE(element))
6775         element = EL_CHAR_MINUS;
6776
6777       Print("# %s\n", element_info[element].token_name);
6778     }
6779
6780     if (j > 0)
6781       Print("#\n");
6782
6783     for (j = 0; j < *editor_elements_info[i].element_list_size; j++)
6784     {
6785       int element = (*editor_elements_info[i].element_list)[j];
6786
6787       Print("# %s\n", element_info[element].token_name);
6788     }
6789
6790     if (j > 0)
6791       Print("#\n");
6792   }
6793 }
6794
6795 static void ReinitializeElementListButtons(void)
6796 {
6797   static boolean last_setup_value_headlines = FALSE;
6798   static boolean initialization_needed = TRUE;
6799   int i;
6800
6801   if (!initialization_needed)   // check if editor element setup has changed
6802   {
6803     if (last_setup_value_headlines != setup.editor.el_headlines)
6804       initialization_needed = TRUE;
6805
6806     for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6807       if (editor_elements_info[i].last_setup_value !=
6808           *editor_elements_info[i].setup_value)
6809         initialization_needed = TRUE;
6810   }
6811
6812   if (!initialization_needed)
6813     return;
6814
6815   FreeLevelEditorGadgets();
6816   CreateLevelEditorGadgets();
6817
6818   // store current setup values for next invocation of this function
6819   last_setup_value_headlines = setup.editor.el_headlines;
6820   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6821     editor_elements_info[i].last_setup_value =
6822       *editor_elements_info[i].setup_value;
6823
6824   initialization_needed = FALSE;
6825 }
6826
6827 static void DrawElementBorder(int dest_x, int dest_y, int width, int height,
6828                               boolean input)
6829 {
6830   int border_graphic =
6831     (input ? IMG_EDITOR_ELEMENT_BORDER_INPUT : IMG_EDITOR_ELEMENT_BORDER);
6832   struct GraphicInfo *g = &graphic_info[border_graphic];
6833   Bitmap *src_bitmap = g->bitmap;
6834   int src_x = g->src_x;
6835   int src_y = g->src_y;
6836   int border_size = g->border_size;
6837   int border_xpos = g->width  - border_size;
6838   int border_ypos = g->height - border_size;
6839   int tilesize = ED_DRAWINGAREA_TILE_SIZE;
6840   int i;
6841
6842   BlitBitmap(src_bitmap, drawto, src_x, src_y,
6843              border_size, border_size,
6844              dest_x - border_size, dest_y - border_size);
6845   BlitBitmap(src_bitmap, drawto, src_x + border_xpos, src_y,
6846              border_size, border_size,
6847              dest_x + width, dest_y - border_size);
6848   BlitBitmap(src_bitmap, drawto, src_x, src_y + border_ypos,
6849              border_size, border_size,
6850              dest_x - border_size, dest_y + height);
6851   BlitBitmap(src_bitmap, drawto, src_x + border_xpos, src_y + border_ypos,
6852              border_size, border_size,
6853              dest_x + width, dest_y + height);
6854
6855   for (i = 0; i < width / tilesize; i++)
6856   {
6857     BlitBitmap(src_bitmap, drawto, src_x + border_size, src_y,
6858                tilesize, border_size,
6859                dest_x + i * tilesize, dest_y - border_size);
6860     BlitBitmap(src_bitmap, drawto, src_x + border_size, src_y + border_ypos,
6861                tilesize, border_size,
6862                dest_x + i * tilesize, dest_y + height);
6863   }
6864
6865   for (i = 0; i < height / tilesize; i++)
6866   {
6867     BlitBitmap(src_bitmap, drawto, src_x, src_y + border_size,
6868                border_size, tilesize,
6869                dest_x - border_size, dest_y + i * tilesize);
6870     BlitBitmap(src_bitmap, drawto, src_x + border_xpos, src_y + border_size,
6871                border_size, tilesize,
6872                dest_x + width, dest_y + i * tilesize);
6873   }
6874
6875   ClearRectangle(drawto, dest_x - 1, dest_y - 1, width + 2, height + 2);
6876 }
6877
6878 static void DrawEditorLevelBorderLine(int x, int y, int xsize, int ysize)
6879 {
6880   int xsize_tile = MAX(ed_tilesize, xsize);
6881   int ysize_tile = MAX(ed_tilesize, ysize);
6882   int xsize_full = xsize + 1;
6883   int ysize_full = ysize + 1;
6884   int xsize_thin = (xsize < ed_tilesize ? 1 : xsize);
6885   int ysize_thin = (ysize < ed_tilesize ? 1 : ysize);
6886   Pixel line_color = getTabulatorBarColor();
6887
6888   if (line_color == BLACK_PIXEL)                // black => transparent
6889     return;
6890
6891   FillRectangle(drawto, SX + x, SY + y, xsize_tile, ysize_tile, BLACK_PIXEL);
6892   FillRectangle(drawto, SX + x, SY + y, xsize_full, ysize_full, line_color);
6893   FillRectangle(drawto, SX + x, SY + y, xsize_thin, ysize_thin, BLACK_PIXEL);
6894 }
6895
6896 static void DrawEditorLevelBorderLinesIfNeeded(void)
6897 {
6898   int xsize = lev_fieldx * ed_tilesize;
6899   int ysize = lev_fieldy * ed_tilesize;
6900   int line_size = getTabulatorBarHeight();
6901
6902   if (!suppressBorderElement())
6903     return;
6904
6905   // draw little border line around editable level playfield
6906
6907   if (xsize < SXSIZE)
6908     DrawEditorLevelBorderLine(xsize, 0, line_size, ysize);
6909
6910   if (ysize < SYSIZE)
6911     DrawEditorLevelBorderLine(0, ysize, xsize, line_size);
6912
6913   if (xsize < SXSIZE && ysize < SYSIZE)
6914     DrawEditorLevelBorderLine(xsize, ysize, line_size, line_size);
6915 }
6916
6917 static void DrawEditorElement(int x, int y, int element)
6918 {
6919   DrawSizedElement(x, y, element, ed_tilesize);
6920 }
6921
6922 static void DrawEditorElementThruMask(int x, int y, int element)
6923 {
6924   DrawSizedElementThruMask(x, y, element, ed_tilesize);
6925 }
6926
6927 static void DrawEditorElementOrWall(int x, int y, int scroll_x, int scroll_y)
6928 {
6929   DrawSizedElementOrWall(x, y, scroll_x, scroll_y, ed_tilesize);
6930 }
6931
6932 static void DrawEditorLevel(int size_x, int size_y, int scroll_x, int scroll_y)
6933 {
6934   DrawSizedLevel(size_x, size_y, scroll_x, scroll_y, ed_tilesize);
6935   DrawEditorLevelBorderLinesIfNeeded();
6936 }
6937
6938 static void DrawDrawingArea(int id)
6939 {
6940   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
6941   int x, y;
6942
6943   int *value = drawingarea_info[id].value;
6944   int area_xsize = drawingarea_info[id].area_xsize;
6945   int area_ysize = drawingarea_info[id].area_ysize;
6946   int tilesize = ED_DRAWINGAREA_TILE_SIZE;
6947
6948   for (x = 0; x < area_xsize; x++)
6949     for (y = 0; y < area_ysize; y++)
6950       DrawSizedGraphicExt(drawto,
6951                           gi->x + x * tilesize,
6952                           gi->y + y * tilesize,
6953                           el2edimg(value[x * area_ysize + y]), 0, tilesize);
6954 }
6955
6956 static void ScrollEditorLevel(int from_x, int from_y, int scroll)
6957 {
6958   int x, y;
6959   int dx = (scroll == ED_SCROLL_LEFT ? -1 : scroll == ED_SCROLL_RIGHT ? 1 : 0);
6960   int dy = (scroll == ED_SCROLL_UP   ? -1 : scroll == ED_SCROLL_DOWN  ? 1 : 0);
6961
6962   BlitBitmap(drawto, drawto,
6963              SX + (dx == -1 ? ed_tilesize : 0),
6964              SY + (dy == -1 ? ed_tilesize : 0),
6965              (ed_fieldx * ed_tilesize) - (dx != 0 ? ed_tilesize : 0),
6966              (ed_fieldy * ed_tilesize) - (dy != 0 ? ed_tilesize : 0),
6967              SX + (dx == +1 ? ed_tilesize : 0),
6968              SY + (dy == +1 ? ed_tilesize : 0));
6969
6970   if (dx)
6971   {
6972     x = (dx == 1 ? 0 : ed_fieldx - 1);
6973     for (y = 0; y < ed_fieldy; y++)
6974       DrawEditorElementOrWall(x, y, from_x, from_y);
6975   }
6976   else if (dy)
6977   {
6978     y = (dy == 1 ? 0 : ed_fieldy - 1);
6979     for (x = 0; x < ed_fieldx; x++)
6980       DrawEditorElementOrWall(x, y, from_x, from_y);
6981   }
6982
6983   redraw_mask |= REDRAW_FIELD;
6984   BackToFront();
6985 }
6986
6987 static void getEditorGraphicSource(int element, int tile_size, Bitmap **bitmap,
6988                                    int *x, int *y)
6989 {
6990   getSizedGraphicSource(el2edimg(element), 0, tile_size, bitmap, x, y);
6991 }
6992
6993 static void CreateControlButtons(void)
6994 {
6995   struct GadgetInfo *gi;
6996   int i;
6997
6998   // create toolbox buttons
6999   for (i = 0; i < ED_NUM_CTRL_BUTTONS; i++)
7000   {
7001     int type_id = controlbutton_info[i].gadget_id;      // same as gadget ID here
7002     int id = controlbutton_info[i].gadget_id;
7003     int type = controlbutton_info[i].gadget_type;
7004     int graphic = controlbutton_info[i].graphic;
7005     struct XYTileSize *pos = controlbutton_info[i].pos;
7006     struct GraphicInfo *gd = &graphic_info[graphic];
7007     Bitmap *deco_bitmap = NULL;
7008     int deco_x = 0, deco_y = 0, deco_xpos = 0, deco_ypos = 0;
7009     int tile_size = 0, deco_shift = 0;
7010     boolean deco_masked = FALSE;
7011     int gd_x1 = gd->src_x;
7012     int gd_y1 = gd->src_y;
7013     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7014     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7015     int gd_x1a = gd->src_x + gd->active_xoffset;
7016     int gd_y1a = gd->src_y + gd->active_yoffset;
7017     int gd_x2a = gd->src_x + gd->active_xoffset + gd->pressed_xoffset;
7018     int gd_y2a = gd->src_y + gd->active_yoffset + gd->pressed_yoffset;
7019     int x = pos->x;
7020     int y = pos->y;
7021     unsigned int event_mask;
7022     int radio_button_nr = RADIO_NR_NONE;
7023     boolean checked = FALSE;
7024
7025     if (type_id != i)
7026       Fail("'controlbutton_info' structure corrupted at index %d -- please fix", i);
7027
7028     if (type == GD_TYPE_RADIO_BUTTON)
7029     {
7030       event_mask = GD_EVENT_PRESSED;
7031       radio_button_nr = RADIO_NR_DRAWING_TOOLBOX;
7032
7033       if (id == drawing_function)
7034         checked = TRUE;
7035     }
7036     else
7037     {
7038       if (id == GADGET_ID_WRAP_LEFT ||
7039           id == GADGET_ID_WRAP_RIGHT ||
7040           id == GADGET_ID_WRAP_UP ||
7041           id == GADGET_ID_WRAP_DOWN)
7042         event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
7043       else
7044         event_mask = GD_EVENT_RELEASED;
7045     }
7046
7047     if (id == GADGET_ID_PROPERTIES ||
7048         id == GADGET_ID_PALETTE)
7049     {
7050       x += DX;
7051       y += DY;
7052     }
7053     else if (id == GADGET_ID_ELEMENT_LEFT ||
7054              id == GADGET_ID_ELEMENT_MIDDLE ||
7055              id == GADGET_ID_ELEMENT_RIGHT)
7056     {
7057       x += DX;
7058       y += DY;
7059
7060       int element = (id == GADGET_ID_ELEMENT_LEFT   ? new_element1 :
7061                      id == GADGET_ID_ELEMENT_MIDDLE ? new_element2 :
7062                      id == GADGET_ID_ELEMENT_RIGHT  ? new_element3 : EL_EMPTY);
7063
7064       tile_size = BUTTON_TILE_SIZE(id == GADGET_ID_ELEMENT_LEFT ?
7065                                    editor.button.element_left.tile_size :
7066                                    id == GADGET_ID_ELEMENT_MIDDLE ?
7067                                    editor.button.element_middle.tile_size :
7068                                    id == GADGET_ID_ELEMENT_RIGHT ?
7069                                    editor.button.element_right.tile_size : 0);
7070
7071       // make sure that decoration does not overlap gadget border
7072       tile_size = MIN(tile_size, MIN(gd->width, gd->height));
7073
7074       getEditorGraphicSource(element, tile_size, &deco_bitmap, &deco_x,&deco_y);
7075
7076       deco_xpos = (gd->width  - tile_size) / 2;
7077       deco_ypos = (gd->height - tile_size) / 2;
7078       deco_shift = 1;
7079       deco_masked = gd->draw_masked;
7080     }
7081     else
7082     {
7083       x += EX;
7084       y += EY;
7085     }
7086
7087     gi = CreateGadget(GDI_CUSTOM_ID, id,
7088                       GDI_CUSTOM_TYPE_ID, type_id,
7089                       GDI_IMAGE_ID, graphic,
7090                       GDI_INFO_TEXT, controlbutton_info[i].infotext,
7091                       GDI_X, x,
7092                       GDI_Y, y,
7093                       GDI_WIDTH, gd->width,
7094                       GDI_HEIGHT, gd->height,
7095                       GDI_TYPE, type,
7096                       GDI_STATE, GD_BUTTON_UNPRESSED,
7097                       GDI_RADIO_NR, radio_button_nr,
7098                       GDI_CHECKED, checked,
7099                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7100                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7101                       GDI_ALT_DESIGN_UNPRESSED, gd->bitmap, gd_x1a, gd_y1a,
7102                       GDI_ALT_DESIGN_PRESSED, gd->bitmap, gd_x2a, gd_y2a,
7103                       GDI_DECORATION_DESIGN, deco_bitmap, deco_x, deco_y,
7104                       GDI_DECORATION_POSITION, deco_xpos, deco_ypos,
7105                       GDI_DECORATION_SIZE, tile_size, tile_size,
7106                       GDI_DECORATION_SHIFTING, deco_shift, deco_shift,
7107                       GDI_DECORATION_MASKED, deco_masked,
7108                       GDI_EVENT_MASK, event_mask,
7109                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7110                       GDI_CALLBACK_ACTION, HandleControlButtons,
7111                       GDI_END);
7112
7113     if (gi == NULL)
7114       Fail("cannot create gadget");
7115
7116     level_editor_gadget[id] = gi;
7117   }
7118
7119   // these values are not constant, but can change at runtime
7120   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_UP].x    = ED_SCROLL_UP_XPOS;
7121   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_UP].y    = ED_SCROLL_UP_YPOS;
7122   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_DOWN].x  = ED_SCROLL_DOWN_XPOS;
7123   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_DOWN].y  = ED_SCROLL_DOWN_YPOS;
7124   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_LEFT].x  = ED_SCROLL_LEFT_XPOS;
7125   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_LEFT].y  = ED_SCROLL_LEFT_YPOS;
7126   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_RIGHT].x = ED_SCROLL_RIGHT_XPOS;
7127   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_RIGHT].y = ED_SCROLL_RIGHT_YPOS;
7128   scrollbutton_pos[ED_SCROLLBUTTON_ID_LIST_UP].x    = ED_SCROLL2_UP_XPOS;
7129   scrollbutton_pos[ED_SCROLLBUTTON_ID_LIST_UP].y    = ED_SCROLL2_UP_YPOS;
7130   scrollbutton_pos[ED_SCROLLBUTTON_ID_LIST_DOWN].x  = ED_SCROLL2_DOWN_XPOS;
7131   scrollbutton_pos[ED_SCROLLBUTTON_ID_LIST_DOWN].y  = ED_SCROLL2_DOWN_YPOS;
7132
7133   // create buttons for scrolling of drawing area and element list
7134   for (i = 0; i < ED_NUM_SCROLLBUTTONS; i++)
7135   {
7136     int id = scrollbutton_info[i].gadget_id;
7137     int type_id = scrollbutton_info[i].gadget_type_id;
7138     int graphic = scrollbutton_info[i].graphic;
7139     struct GraphicInfo *gd = &graphic_info[graphic];
7140     Bitmap *gd_bitmap = gd->bitmap;
7141     int gd_x1 = gd->src_x;
7142     int gd_y1 = gd->src_y;
7143     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7144     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7145     int width  = gd->width;
7146     int height = gd->height;
7147     int x = scrollbutton_pos[i].x;
7148     int y = scrollbutton_pos[i].y;
7149     unsigned int event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
7150
7151     if (type_id != i)
7152       Fail("'scrollbutton_info' structure corrupted at index %d -- please fix", i);
7153
7154     if (id == GADGET_ID_SCROLL_LIST_UP ||
7155         id == GADGET_ID_SCROLL_LIST_DOWN)
7156     {
7157       x += PX;
7158       y += PY;
7159     }
7160     else
7161     {
7162       x += SX;
7163       y += SY;
7164     }
7165
7166     gi = CreateGadget(GDI_CUSTOM_ID, id,
7167                       GDI_CUSTOM_TYPE_ID, type_id,
7168                       GDI_IMAGE_ID, graphic,
7169                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
7170                       GDI_X, x,
7171                       GDI_Y, y,
7172                       GDI_WIDTH, width,
7173                       GDI_HEIGHT, height,
7174                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7175                       GDI_STATE, GD_BUTTON_UNPRESSED,
7176                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y1,
7177                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y2,
7178                       GDI_EVENT_MASK, event_mask,
7179                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7180                       GDI_CALLBACK_ACTION, HandleControlButtons,
7181                       GDI_END);
7182
7183     if (gi == NULL)
7184       Fail("cannot create gadget");
7185
7186     level_editor_gadget[id] = gi;
7187   }
7188
7189   // create buttons for element list
7190   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
7191   {
7192     int type_id = i;
7193     int id = GADGET_ID_ELEMENTLIST_FIRST + i;
7194     int graphic = IMG_EDITOR_PALETTE_BUTTON;
7195     struct GraphicInfo *gd = &graphic_info[graphic];
7196     Bitmap *gd_bitmap = gd->bitmap;
7197     Bitmap *deco_bitmap;
7198     int deco_x, deco_y, deco_xpos, deco_ypos;
7199     int gd_x1 = gd->src_x;
7200     int gd_y1 = gd->src_y;
7201     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7202     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7203     int xx = i % ED_ELEMENTLIST_BUTTONS_HORIZ;
7204     int yy = i / ED_ELEMENTLIST_BUTTONS_HORIZ;
7205     int x = PX + ED_ELEMENTLIST_XPOS + xx * gd->width;
7206     int y = PY + ED_ELEMENTLIST_YPOS + yy * gd->height;
7207     int element = editor_elements[i];
7208     int tile_size = BUTTON_TILE_SIZE(editor.palette.tile_size);
7209     unsigned int event_mask = GD_EVENT_RELEASED;
7210
7211     getEditorGraphicSource(element, tile_size, &deco_bitmap, &deco_x, &deco_y);
7212
7213     deco_xpos = (gd->width  - tile_size) / 2;
7214     deco_ypos = (gd->height - tile_size) / 2;
7215
7216     gi = CreateGadget(GDI_CUSTOM_ID, id,
7217                       GDI_CUSTOM_TYPE_ID, type_id,
7218                       GDI_IMAGE_ID, graphic,
7219                       GDI_INFO_TEXT, getElementInfoText(element),
7220                       GDI_X, x,
7221                       GDI_Y, y,
7222                       GDI_WIDTH, gd->width,
7223                       GDI_HEIGHT, gd->height,
7224                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7225                       GDI_STATE, GD_BUTTON_UNPRESSED,
7226                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y1,
7227                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y2,
7228                       GDI_DECORATION_DESIGN, deco_bitmap, deco_x, deco_y,
7229                       GDI_DECORATION_POSITION, deco_xpos, deco_ypos,
7230                       GDI_DECORATION_SIZE, tile_size, tile_size,
7231                       GDI_DECORATION_SHIFTING, 1, 1,
7232                       GDI_EVENT_MASK, event_mask,
7233                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7234                       GDI_CALLBACK_ACTION, HandleControlButtons,
7235                       GDI_END);
7236
7237     if (gi == NULL)
7238       Fail("cannot create gadget");
7239
7240     level_editor_gadget[id] = gi;
7241   }
7242 }
7243
7244 static void CreateCounterButtons(void)
7245 {
7246   int max_infotext_len = getMaxInfoTextLength();
7247   int i;
7248
7249   for (i = 0; i < ED_NUM_COUNTERBUTTONS; i++)
7250   {
7251     int type_id = counterbutton_info[i].gadget_type_id;
7252     int x = SX + ED_SETTINGS_X(counterbutton_info[i].x); // down count button
7253     int y = SY + ED_SETTINGS_Y(counterbutton_info[i].y);
7254     int j;
7255
7256     if (type_id != i)
7257       Fail("'counterbutton_info' structure corrupted at index %d -- please fix", i);
7258
7259     // determine horizontal position to the right of specified gadget
7260     if (counterbutton_info[i].gadget_id_align != GADGET_ID_NONE)
7261       x = (right_gadget_border[counterbutton_info[i].gadget_id_align] +
7262            ED_GADGET_TEXT_DISTANCE);
7263
7264     // determine horizontal offset for leading text
7265     if (counterbutton_info[i].text_left != NULL)
7266       x += getTextWidthForGadget(counterbutton_info[i].text_left);
7267
7268     for (j = 0; j < 2; j++)
7269     {
7270       struct GadgetInfo *gi;
7271       int id = (j == 0 ?
7272                 counterbutton_info[i].gadget_id_down :
7273                 counterbutton_info[i].gadget_id_up);
7274       int graphic;
7275       struct GraphicInfo *gd;
7276       int gd_x1, gd_x2, gd_y1, gd_y2;
7277       unsigned int event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
7278       char infotext[max_infotext_len + 1];
7279
7280       if (i == ED_COUNTER_ID_SELECT_LEVEL)
7281       {
7282         graphic = (j == 0 ?
7283                    IMG_GFX_EDITOR_BUTTON_PREV_LEVEL :
7284                    IMG_GFX_EDITOR_BUTTON_NEXT_LEVEL);
7285
7286         event_mask |= GD_EVENT_RELEASED;
7287
7288         if (j == 0)
7289         {
7290           x = DX + editor.button.prev_level.x;
7291           y = DY + editor.button.prev_level.y;
7292         }
7293         else
7294         {
7295           x = DX + editor.button.next_level.x;
7296           y = DY + editor.button.next_level.y;
7297         }
7298       }
7299       else
7300       {
7301         graphic = (j == 0 ?
7302                    IMG_EDITOR_COUNTER_DOWN :
7303                    IMG_EDITOR_COUNTER_UP);
7304       }
7305
7306       gd = &graphic_info[graphic];
7307
7308       gd_x1 = gd->src_x;
7309       gd_y1 = gd->src_y;
7310       gd_x2 = gd->src_x + gd->pressed_xoffset;
7311       gd_y2 = gd->src_y + gd->pressed_yoffset;
7312
7313       sprintf(infotext, "%s counter value by 1, 5 or 10",
7314               (j == 0 ? "decrease" : "increase"));
7315
7316       gi = CreateGadget(GDI_CUSTOM_ID, id,
7317                         GDI_CUSTOM_TYPE_ID, type_id,
7318                         GDI_IMAGE_ID, graphic,
7319                         GDI_INFO_TEXT, infotext,
7320                         GDI_X, x,
7321                         GDI_Y, y,
7322                         GDI_WIDTH, gd->width,
7323                         GDI_HEIGHT, gd->height,
7324                         GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7325                         GDI_STATE, GD_BUTTON_UNPRESSED,
7326                         GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7327                         GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7328                         GDI_EVENT_MASK, event_mask,
7329                         GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7330                         GDI_CALLBACK_ACTION, HandleCounterButtons,
7331                         GDI_END);
7332
7333       if (gi == NULL)
7334         Fail("cannot create gadget");
7335
7336       level_editor_gadget[id] = gi;
7337       right_gadget_border[id] =
7338         getRightGadgetBorder(gi, counterbutton_info[i].text_right);
7339
7340       x += gi->width + ED_GADGET_SMALL_DISTANCE;        // text count button
7341
7342       if (j == 0)
7343       {
7344         int font_type = FONT_INPUT_1;
7345         int font_type_active = FONT_INPUT_1_ACTIVE;
7346
7347         id = counterbutton_info[i].gadget_id_text;
7348
7349         event_mask = GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
7350
7351         if (i == ED_COUNTER_ID_SELECT_LEVEL)
7352         {
7353           graphic = IMG_GFX_EDITOR_INPUT_LEVEL_NUMBER;
7354
7355           font_type = FONT_LEVEL_NUMBER;
7356           font_type_active = FONT_LEVEL_NUMBER_ACTIVE;
7357
7358           x = DX + editor.input.level_number.x;
7359           y = DY + editor.input.level_number.y;
7360         }
7361         else
7362         {
7363           graphic = IMG_EDITOR_COUNTER_INPUT;
7364         }
7365
7366         gd = &graphic_info[graphic];
7367
7368         gd_x1 = gd->src_x;
7369         gd_y1 = gd->src_y;
7370         gd_x2 = gd->src_x + gd->active_xoffset;
7371         gd_y2 = gd->src_y + gd->active_yoffset;
7372
7373         gi = CreateGadget(GDI_CUSTOM_ID, id,
7374                           GDI_CUSTOM_TYPE_ID, type_id,
7375                           GDI_IMAGE_ID, graphic,
7376                           GDI_INFO_TEXT, "enter counter value",
7377                           GDI_X, x,
7378                           GDI_Y, y,
7379                           GDI_TYPE, GD_TYPE_TEXT_INPUT_NUMERIC,
7380                           GDI_NUMBER_VALUE, 0,
7381                           GDI_NUMBER_MIN, counterbutton_info[i].min_value,
7382                           GDI_NUMBER_MAX, counterbutton_info[i].max_value,
7383                           GDI_TEXT_SIZE, 3,     // minimal counter text size
7384                           GDI_TEXT_FONT, font_type,
7385                           GDI_TEXT_FONT_ACTIVE, font_type_active,
7386                           GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7387                           GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7388                           GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7389                           GDI_DESIGN_WIDTH, gd->width,
7390                           GDI_EVENT_MASK, event_mask,
7391                           GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7392                           GDI_CALLBACK_ACTION, HandleCounterButtons,
7393                           GDI_END);
7394
7395         if (gi == NULL)
7396           Fail("cannot create gadget");
7397
7398         level_editor_gadget[id] = gi;
7399         right_gadget_border[id] =
7400           getRightGadgetBorder(gi, counterbutton_info[i].text_right);
7401
7402         x += gi->width + ED_GADGET_SMALL_DISTANCE;      // up count button
7403       }
7404     }
7405   }
7406 }
7407
7408 static void CreateDrawingAreas(void)
7409 {
7410   int i;
7411
7412   // these values are not constant, but can change at runtime
7413   drawingarea_info[ED_DRAWING_ID_DRAWING_LEVEL].area_xsize = MAX_ED_FIELDX;
7414   drawingarea_info[ED_DRAWING_ID_DRAWING_LEVEL].area_ysize = MAX_ED_FIELDY;
7415
7416   for (i = 0; i < ED_NUM_DRAWING_AREAS; i++)
7417   {
7418     struct GadgetInfo *gi;
7419     int id = drawingarea_info[i].gadget_id;
7420     int type_id = drawingarea_info[i].gadget_type_id;
7421     int x = SX + ED_AREA_SETTINGS_X(drawingarea_info[i]);
7422     int y = SY + ED_AREA_SETTINGS_Y(drawingarea_info[i]);
7423     int area_xsize = drawingarea_info[i].area_xsize;
7424     int area_ysize = drawingarea_info[i].area_ysize;
7425     int item_size = (id == GADGET_ID_DRAWING_LEVEL ?
7426                      ed_tilesize : ED_DRAWINGAREA_TILE_SIZE);
7427     unsigned int event_mask =
7428       GD_EVENT_PRESSED | GD_EVENT_RELEASED | GD_EVENT_MOVING |
7429       GD_EVENT_OFF_BORDERS | GD_EVENT_PIXEL_PRECISE;
7430
7431     if (type_id != i)
7432       Fail("'drawingarea_info' structure corrupted at index %d -- please fix", i);
7433
7434     // determine horizontal position to the right of specified gadget
7435     if (drawingarea_info[i].gadget_id_align != GADGET_ID_NONE)
7436       x = (right_gadget_border[drawingarea_info[i].gadget_id_align] +
7437            ED_DRAWINGAREA_TEXT_DISTANCE);
7438
7439     // determine horizontal offset for leading text
7440     if (drawingarea_info[i].text_left != NULL)
7441       x += getTextWidthForDrawingArea(drawingarea_info[i].text_left);
7442
7443     gi = CreateGadget(GDI_CUSTOM_ID, id,
7444                       GDI_CUSTOM_TYPE_ID, type_id,
7445                       GDI_X, x,
7446                       GDI_Y, y,
7447                       GDI_TYPE, GD_TYPE_DRAWING_AREA,
7448                       GDI_AREA_SIZE, area_xsize, area_ysize,
7449                       GDI_ITEM_SIZE, item_size, item_size,
7450                       GDI_EVENT_MASK, event_mask,
7451                       GDI_CALLBACK_INFO, HandleDrawingAreaInfo,
7452                       GDI_CALLBACK_ACTION, HandleDrawingAreas,
7453                       GDI_END);
7454
7455     if (gi == NULL)
7456       Fail("cannot create gadget");
7457
7458     level_editor_gadget[id] = gi;
7459     right_gadget_border[id] =
7460       getRightGadgetBorder(gi, drawingarea_info[i].text_right);
7461   }
7462 }
7463
7464 static void CreateTextInputGadgets(void)
7465 {
7466   struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_INPUT_TEXT];
7467   int max_infotext_len = getMaxInfoTextLength();
7468   int i;
7469
7470   for (i = 0; i < ED_NUM_TEXTINPUT; i++)
7471   {
7472     int gd_x1 = gd->src_x;
7473     int gd_y1 = gd->src_y;
7474     int gd_x2 = gd->src_x + gd->active_xoffset;
7475     int gd_y2 = gd->src_y + gd->active_yoffset;
7476     struct GadgetInfo *gi;
7477     unsigned int event_mask = GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
7478     char infotext[MAX_OUTPUT_LINESIZE + 1];
7479     int id = textinput_info[i].gadget_id;
7480     int type_id = textinput_info[i].gadget_type_id;
7481     int x, y;
7482
7483     if (type_id != i)
7484       Fail("'textinput_info' structure corrupted at index %d -- please fix", i);
7485
7486     if (i == ED_TEXTINPUT_ID_ELEMENT_NAME)
7487     {
7488       int element_border = graphic_info[IMG_EDITOR_ELEMENT_BORDER].border_size;
7489       int border_size = gd->border_size;
7490       int font_nr = FONT_INPUT_1;
7491       int font_height = getFontHeight(font_nr);
7492       int xoffset = element_border + TILEX + element_border + 3 * border_size;
7493       int yoffset = element_border + (TILEY - font_height) / 2;
7494
7495       x = (editor.settings.element_name.x != -1 ?
7496            editor.settings.element_name.x :
7497            editor.settings.element_graphic.x + xoffset) - border_size;
7498       y = (editor.settings.element_name.y != -1 ?
7499            editor.settings.element_name.y :
7500            editor.settings.element_graphic.y + yoffset) - border_size;
7501     }
7502     else
7503     {
7504       x = ED_SETTINGS_X(textinput_info[i].x);
7505       y = ED_SETTINGS_Y(textinput_info[i].y);
7506     }
7507
7508     sprintf(infotext, "Enter %s", textinput_info[i].infotext);
7509     infotext[max_infotext_len] = '\0';
7510
7511     gi = CreateGadget(GDI_CUSTOM_ID, id,
7512                       GDI_CUSTOM_TYPE_ID, type_id,
7513                       GDI_INFO_TEXT, infotext,
7514                       GDI_X, SX + x,
7515                       GDI_Y, SY + y,
7516                       GDI_TYPE, GD_TYPE_TEXT_INPUT_ALPHANUMERIC,
7517                       GDI_TEXT_VALUE, textinput_info[i].value,
7518                       GDI_TEXT_SIZE, textinput_info[i].size,
7519                       GDI_TEXT_FONT, FONT_INPUT_1,
7520                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_1_ACTIVE,
7521                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7522                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7523                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7524                       GDI_DESIGN_WIDTH, gd->width,
7525                       GDI_EVENT_MASK, event_mask,
7526                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7527                       GDI_CALLBACK_ACTION, HandleTextInputGadgets,
7528                       GDI_END);
7529
7530     if (gi == NULL)
7531       Fail("cannot create gadget");
7532
7533     level_editor_gadget[id] = gi;
7534   }
7535 }
7536
7537 static void CreateTextAreaGadgets(void)
7538 {
7539   int max_infotext_len = getMaxInfoTextLength();
7540   int i;
7541
7542   for (i = 0; i < ED_NUM_TEXTAREAS; i++)
7543   {
7544     struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_INPUT_TEXTAREA];
7545     int gd_x1 = gd->src_x;
7546     int gd_y1 = gd->src_y;
7547     int gd_x2 = gd->src_x + gd->active_xoffset;
7548     int gd_y2 = gd->src_y + gd->active_yoffset;
7549     struct GadgetInfo *gi;
7550     unsigned int event_mask = GD_EVENT_TEXT_LEAVING;
7551     char infotext[MAX_OUTPUT_LINESIZE + 1];
7552     int id = textarea_info[i].gadget_id;
7553     int type_id = textarea_info[i].gadget_type_id;
7554     int area_xsize = textarea_info[i].xsize;
7555     int area_ysize = textarea_info[i].ysize;
7556
7557     if (type_id != i)
7558       Fail("'textarea_info' structure corrupted at index %d -- please fix", i);
7559
7560     sprintf(infotext, "Enter %s", textarea_info[i].infotext);
7561     infotext[max_infotext_len] = '\0';
7562
7563     gi = CreateGadget(GDI_CUSTOM_ID, id,
7564                       GDI_CUSTOM_TYPE_ID, type_id,
7565                       GDI_INFO_TEXT, infotext,
7566                       GDI_X, SX + ED_SETTINGS_X(textarea_info[i].x),
7567                       GDI_Y, SY + ED_SETTINGS_Y(textarea_info[i].y),
7568                       GDI_TYPE, GD_TYPE_TEXT_AREA,
7569                       GDI_AREA_SIZE, area_xsize, area_ysize,
7570                       GDI_TEXT_FONT, FONT_INPUT_1,
7571                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_1_ACTIVE,
7572                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7573                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7574                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7575                       GDI_DESIGN_WIDTH, gd->width,
7576                       GDI_EVENT_MASK, event_mask,
7577                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7578                       GDI_CALLBACK_ACTION, HandleTextAreaGadgets,
7579                       GDI_END);
7580
7581     if (gi == NULL)
7582       Fail("cannot create gadget");
7583
7584     level_editor_gadget[id] = gi;
7585   }
7586 }
7587
7588 static void CreateSelectboxGadgets(void)
7589 {
7590   int max_infotext_len = getMaxInfoTextLength();
7591   int i, j;
7592
7593   for (i = 0; i < ED_NUM_SELECTBOX; i++)
7594   {
7595     struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_SELECTBOX_INPUT];
7596     struct GraphicInfo *gd2 = &graphic_info[IMG_EDITOR_SELECTBOX_BUTTON];
7597     int gd_x1 = gd->src_x;
7598     int gd_y1 = gd->src_y;
7599     int gd_x2 = gd->src_x + gd->active_xoffset;
7600     int gd_y2 = gd->src_y + gd->active_yoffset;
7601     int selectbox_button_xsize = gd2->width;
7602     struct GadgetInfo *gi;
7603     char infotext[MAX_OUTPUT_LINESIZE + 1];
7604     int id = selectbox_info[i].gadget_id;
7605     int type_id = selectbox_info[i].gadget_type_id;
7606     int x = SX + ED_SETTINGS_X(selectbox_info[i].x);
7607     int y = SY + ED_SETTINGS_Y(selectbox_info[i].y);
7608     unsigned int event_mask =
7609       GD_EVENT_RELEASED | GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
7610
7611     if (type_id != i)
7612       Fail("'selectbox_info' structure corrupted at index %d -- please fix", i);
7613
7614     if (selectbox_info[i].size == -1)   // dynamically determine size
7615     {
7616       // (we cannot use -1 for uninitialized values if we directly compare
7617       // with results from strlen(), because the '<' and '>' operation will
7618       // implicitely cast -1 to an unsigned integer value!)
7619       selectbox_info[i].size = 0;
7620
7621       for (j = 0; selectbox_info[i].options[j].text != NULL; j++)
7622         if (strlen(selectbox_info[i].options[j].text) > selectbox_info[i].size)
7623           selectbox_info[i].size = strlen(selectbox_info[i].options[j].text);
7624
7625       selectbox_info[i].size++;         // add one character empty space
7626     }
7627
7628     // determine horizontal position to the right of specified gadget
7629     if (selectbox_info[i].gadget_id_align != GADGET_ID_NONE)
7630       x = (right_gadget_border[selectbox_info[i].gadget_id_align] +
7631            ED_GADGET_TEXT_DISTANCE);
7632
7633     // determine horizontal offset for leading text
7634     if (selectbox_info[i].text_left != NULL)
7635       x += getTextWidthForGadget(selectbox_info[i].text_left);
7636
7637     sprintf(infotext, "Select %s", selectbox_info[i].infotext);
7638     infotext[max_infotext_len] = '\0';
7639
7640     gi = CreateGadget(GDI_CUSTOM_ID, id,
7641                       GDI_CUSTOM_TYPE_ID, type_id,
7642                       GDI_INFO_TEXT, infotext,
7643                       GDI_X, x,
7644                       GDI_Y, y,
7645                       GDI_TYPE, GD_TYPE_SELECTBOX,
7646                       GDI_SELECTBOX_OPTIONS, selectbox_info[i].options,
7647                       GDI_SELECTBOX_CHAR_UNSELECTABLE, '[',
7648                       GDI_TEXT_SIZE, selectbox_info[i].size,
7649                       GDI_TEXT_FONT, FONT_INPUT_1,
7650                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_1_ACTIVE,
7651                       GDI_TEXT_FONT_UNSELECTABLE, FONT_TEXT_1,
7652                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7653                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7654                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7655                       GDI_BORDER_SIZE_SELECTBUTTON, selectbox_button_xsize,
7656                       GDI_DESIGN_WIDTH, gd->width,
7657                       GDI_EVENT_MASK, event_mask,
7658                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7659                       GDI_CALLBACK_ACTION, HandleSelectboxGadgets,
7660                       GDI_END);
7661
7662     if (gi == NULL)
7663       Fail("cannot create gadget");
7664
7665     level_editor_gadget[id] = gi;
7666     right_gadget_border[id] =
7667       getRightGadgetBorder(gi, selectbox_info[i].text_right);
7668   }
7669 }
7670
7671 static void CreateTextbuttonGadgets(void)
7672 {
7673   int max_infotext_len = getMaxInfoTextLength();
7674   int i;
7675
7676   for (i = 0; i < ED_NUM_TEXTBUTTONS; i++)
7677   {
7678     int id = textbutton_info[i].gadget_id;
7679     int type_id = textbutton_info[i].gadget_type_id;
7680     int is_tab_button =
7681       ((id >= GADGET_ID_LEVELCONFIG_LEVEL && id <= GADGET_ID_LEVELCONFIG_ENGINE) ||
7682        (id >= GADGET_ID_PROPERTIES_INFO && id <= GADGET_ID_PROPERTIES_CHANGE));
7683     int graphic =
7684       (is_tab_button ? IMG_EDITOR_TABBUTTON : IMG_EDITOR_TEXTBUTTON);
7685     int gadget_distance =
7686       (is_tab_button ? ED_GADGET_SMALL_DISTANCE : ED_GADGET_TEXT_DISTANCE);
7687     struct GraphicInfo *gd = &graphic_info[graphic];
7688     int gd_x1 = gd->src_x;
7689     int gd_y1 = gd->src_y;
7690     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7691     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7692     int gd_x1a = gd->src_x + gd->active_xoffset;
7693     int gd_y1a = gd->src_y + gd->active_yoffset;
7694     int border_xsize = gd->border_size + gd->draw_xoffset;
7695     int border_ysize = gd->border_size;
7696     struct GadgetInfo *gi;
7697     unsigned int event_mask = GD_EVENT_RELEASED;
7698     char infotext[MAX_OUTPUT_LINESIZE + 1];
7699     int x = SX + ED_SETTINGS_X(textbutton_info[i].x);
7700     int y = SY + ED_SETTINGS_Y(textbutton_info[i].y);
7701
7702     if (type_id != i)
7703       Fail("'textbutton_info' structure corrupted at index %d -- please fix", i);
7704
7705     if (textbutton_info[i].size == -1)  // dynamically determine size
7706       textbutton_info[i].size = strlen(textbutton_info[i].text);
7707
7708     sprintf(infotext, "%s", textbutton_info[i].infotext);
7709     infotext[max_infotext_len] = '\0';
7710
7711     // determine horizontal position to the right of specified gadget
7712     if (textbutton_info[i].gadget_id_align != GADGET_ID_NONE)
7713     {
7714       int gadget_id_align = textbutton_info[i].gadget_id_align;
7715
7716       x = right_gadget_border[gadget_id_align] + gadget_distance;
7717
7718       if (textbutton_info[i].y == -1)
7719         y = level_editor_gadget[gadget_id_align]->y;
7720     }
7721
7722     // determine horizontal offset for leading text
7723     if (textbutton_info[i].text_left != NULL)
7724       x += getTextWidthForGadget(textbutton_info[i].text_left);
7725
7726     gi = CreateGadget(GDI_CUSTOM_ID, id,
7727                       GDI_CUSTOM_TYPE_ID, type_id,
7728                       GDI_IMAGE_ID, graphic,
7729                       GDI_INFO_TEXT, infotext,
7730                       GDI_X, x,
7731                       GDI_Y, y,
7732                       GDI_TYPE, GD_TYPE_TEXT_BUTTON,
7733                       GDI_TEXT_VALUE, textbutton_info[i].text,
7734                       GDI_TEXT_SIZE, textbutton_info[i].size,
7735                       GDI_TEXT_FONT, FONT_INPUT_2,
7736                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_2_ACTIVE,
7737                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7738                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7739                       GDI_ALT_DESIGN_UNPRESSED, gd->bitmap, gd_x1a, gd_y1a,
7740                       GDI_BORDER_SIZE, border_xsize, border_ysize,
7741                       GDI_DESIGN_WIDTH, gd->width,
7742                       GDI_DECORATION_SHIFTING, 1, 1,
7743                       GDI_EVENT_MASK, event_mask,
7744                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7745                       GDI_CALLBACK_ACTION, HandleTextbuttonGadgets,
7746                       GDI_END);
7747
7748     if (gi == NULL)
7749       Fail("cannot create gadget");
7750
7751     level_editor_gadget[id] = gi;
7752     right_gadget_border[id] =
7753       getRightGadgetBorder(gi, textbutton_info[i].text_right);
7754   }
7755 }
7756
7757 static void CreateGraphicbuttonGadgets(void)
7758 {
7759   struct GadgetInfo *gi;
7760   int i;
7761
7762   // create buttons for scrolling of drawing area and element list
7763   for (i = 0; i < ED_NUM_GRAPHICBUTTONS; i++)
7764   {
7765     int id = graphicbutton_info[i].gadget_id;
7766     int type_id = graphicbutton_info[i].gadget_type_id;
7767     int x = SX + ED_SETTINGS_X(graphicbutton_info[i].x);
7768     int y = SY + ED_SETTINGS_Y(graphicbutton_info[i].y);
7769     int graphic = graphicbutton_info[i].graphic;
7770     struct GraphicInfo *gd = &graphic_info[graphic];
7771     int gd_x1 = gd->src_x;
7772     int gd_y1 = gd->src_y;
7773     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7774     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7775     unsigned int event_mask = GD_EVENT_RELEASED;
7776
7777     if (type_id != i)
7778       Fail("'graphicbutton_info' structure corrupted at index %d -- please fix", i);
7779
7780     // determine horizontal position to the right of specified gadget
7781     if (graphicbutton_info[i].gadget_id_align != GADGET_ID_NONE)
7782       x = (right_gadget_border[graphicbutton_info[i].gadget_id_align] +
7783            ED_GADGET_TEXT_DISTANCE);
7784
7785     // determine horizontal offset for leading text
7786     if (graphicbutton_info[i].text_left != NULL)
7787       x += getTextWidthForGadget(graphicbutton_info[i].text_left);
7788
7789     gi = CreateGadget(GDI_CUSTOM_ID, id,
7790                       GDI_CUSTOM_TYPE_ID, type_id,
7791                       GDI_IMAGE_ID, graphic,
7792                       GDI_INFO_TEXT, graphicbutton_info[i].infotext,
7793                       GDI_X, x,
7794                       GDI_Y, y,
7795                       GDI_WIDTH, gd->width,
7796                       GDI_HEIGHT, gd->height,
7797                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7798                       GDI_STATE, GD_BUTTON_UNPRESSED,
7799                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7800                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7801                       GDI_EVENT_MASK, event_mask,
7802                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7803                       GDI_CALLBACK_ACTION, HandleGraphicbuttonGadgets,
7804                       GDI_END);
7805
7806     if (gi == NULL)
7807       Fail("cannot create gadget");
7808
7809     level_editor_gadget[id] = gi;
7810     right_gadget_border[id] =
7811       getRightGadgetBorder(gi, graphicbutton_info[i].text_right);
7812   }
7813 }
7814
7815 static void CreateScrollbarGadgets(void)
7816 {
7817   int i;
7818
7819   // these values are not constant, but can change at runtime
7820   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].x =
7821     SX + ED_SCROLL_HORIZONTAL_XPOS;
7822   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].y =
7823     SY + ED_SCROLL_HORIZONTAL_YPOS;
7824   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].width =
7825     ED_SCROLL_HORIZONTAL_XSIZE;
7826   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].height =
7827     ED_SCROLL_HORIZONTAL_YSIZE;
7828   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].wheel_x      = SX;
7829   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].wheel_y      = SY;
7830   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].wheel_width  = SXSIZE;
7831   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].wheel_height = SYSIZE;
7832
7833   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].x =
7834     SX + ED_SCROLL_VERTICAL_XPOS;
7835   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].y =
7836     SY + ED_SCROLL_VERTICAL_YPOS;
7837   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].width =
7838     ED_SCROLL_VERTICAL_XSIZE;
7839   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].height =
7840     ED_SCROLL_VERTICAL_YSIZE;
7841   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].wheel_x      = SX;
7842   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].wheel_y      = SY;
7843   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].wheel_width  = SXSIZE;
7844   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].wheel_height = SYSIZE;
7845
7846   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].x =
7847     PX + ED_SCROLL2_VERTICAL_XPOS;
7848   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].y =
7849     PY + ED_SCROLL2_VERTICAL_YPOS;
7850   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].width =
7851     ED_SCROLL2_VERTICAL_XSIZE;
7852   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].height =
7853     ED_SCROLL2_VERTICAL_YSIZE;
7854   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].wheel_x = PX;
7855   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].wheel_y = PY;
7856   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].wheel_width  = PXSIZE;
7857   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].wheel_height = PYSIZE;
7858
7859   for (i = 0; i < ED_NUM_SCROLLBARS; i++)
7860   {
7861     int id = scrollbar_info[i].gadget_id;
7862     int type_id = scrollbar_info[i].gadget_type_id;
7863     int graphic = scrollbar_info[i].graphic;
7864     struct GraphicInfo *gd = &graphic_info[graphic];
7865     int gd_x1 = gd->src_x;
7866     int gd_y1 = gd->src_y;
7867     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7868     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7869     struct GadgetInfo *gi;
7870     int items_max, items_visible, item_position;
7871     unsigned int event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
7872
7873     if (type_id != i)
7874       Fail("'scrollbar_info' structure corrupted at index %d -- please fix", i);
7875
7876     if (i == ED_SCROLLBAR_ID_LIST_VERTICAL)
7877     {
7878       items_max = num_editor_elements / ED_ELEMENTLIST_BUTTONS_HORIZ;
7879       items_visible = ED_ELEMENTLIST_BUTTONS_VERT;
7880       item_position = element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ;
7881     }
7882     else        // drawing area scrollbars
7883     {
7884       if (scrollbar_info[i].type == GD_TYPE_SCROLLBAR_HORIZONTAL)
7885       {
7886         items_max = MAX(lev_fieldx + 2, ed_fieldx);
7887         items_visible = ed_fieldx;
7888         item_position = 0;
7889       }
7890       else
7891       {
7892         items_max = MAX(lev_fieldy + 2, ed_fieldy);
7893         items_visible = ed_fieldy;
7894         item_position = 0;
7895       }
7896     }
7897
7898     gi = CreateGadget(GDI_CUSTOM_ID, id,
7899                       GDI_CUSTOM_TYPE_ID, type_id,
7900                       GDI_IMAGE_ID, graphic,
7901                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
7902                       GDI_X, scrollbar_pos[i].x,
7903                       GDI_Y, scrollbar_pos[i].y,
7904                       GDI_WIDTH, scrollbar_pos[i].width,
7905                       GDI_HEIGHT, scrollbar_pos[i].height,
7906                       GDI_TYPE, scrollbar_info[i].type,
7907                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
7908                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
7909                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
7910                       GDI_WHEEL_AREA_X, scrollbar_pos[i].wheel_x,
7911                       GDI_WHEEL_AREA_Y, scrollbar_pos[i].wheel_y,
7912                       GDI_WHEEL_AREA_WIDTH, scrollbar_pos[i].wheel_width,
7913                       GDI_WHEEL_AREA_HEIGHT, scrollbar_pos[i].wheel_height,
7914                       GDI_STATE, GD_BUTTON_UNPRESSED,
7915                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7916                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7917                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7918                       GDI_EVENT_MASK, event_mask,
7919                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7920                       GDI_CALLBACK_ACTION, HandleControlButtons,
7921                       GDI_END);
7922
7923     if (gi == NULL)
7924       Fail("cannot create gadget");
7925
7926     level_editor_gadget[id] = gi;
7927   }
7928 }
7929
7930 static void CreateCheckbuttonGadgets(void)
7931 {
7932   struct GadgetInfo *gi;
7933   int i;
7934
7935   for (i = 0; i < ED_NUM_CHECKBUTTONS; i++)
7936   {
7937     int id = checkbutton_info[i].gadget_id;
7938     int type_id = checkbutton_info[i].gadget_type_id;
7939     int graphic = (id == GADGET_ID_STICK_ELEMENT ? IMG_EDITOR_STICKYBUTTON :
7940                    IMG_EDITOR_CHECKBOX);
7941     struct GraphicInfo *gd = &graphic_info[graphic];
7942     int gd_x1 = gd->src_x;
7943     int gd_y1 = gd->src_y;
7944     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7945     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7946     int gd_x1a = gd->src_x + gd->active_xoffset;
7947     int gd_y1a = gd->src_y + gd->active_yoffset;
7948     int gd_x2a = gd->src_x + gd->active_xoffset + gd->pressed_xoffset;
7949     int gd_y2a = gd->src_y + gd->active_yoffset + gd->pressed_yoffset;
7950     int x = SX + ED_SETTINGS_X(checkbutton_info[i].x);
7951     int y = SY + ED_SETTINGS_Y(checkbutton_info[i].y);
7952     unsigned int event_mask = GD_EVENT_PRESSED;
7953
7954     if (type_id != i)
7955       Fail("'checkbutton_info' structure corrupted at index %d -- please fix", i);
7956
7957     // determine horizontal position to the right of specified gadget
7958     if (checkbutton_info[i].gadget_id_align != GADGET_ID_NONE)
7959       x = (right_gadget_border[checkbutton_info[i].gadget_id_align] +
7960            ED_GADGET_TEXT_DISTANCE);
7961
7962     // determine horizontal offset for leading text
7963     if (checkbutton_info[i].text_left != NULL)
7964       x += getTextWidthForGadget(checkbutton_info[i].text_left);
7965
7966     gi = CreateGadget(GDI_CUSTOM_ID, id,
7967                       GDI_CUSTOM_TYPE_ID, type_id,
7968                       GDI_IMAGE_ID, graphic,
7969                       GDI_INFO_TEXT, checkbutton_info[i].infotext,
7970                       GDI_X, x,
7971                       GDI_Y, y,
7972                       GDI_WIDTH, gd->width,
7973                       GDI_HEIGHT, gd->height,
7974                       GDI_TYPE, GD_TYPE_CHECK_BUTTON,
7975                       GDI_CHECKED, *checkbutton_info[i].value,
7976                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7977                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7978                       GDI_ALT_DESIGN_UNPRESSED, gd->bitmap, gd_x1a, gd_y1a,
7979                       GDI_ALT_DESIGN_PRESSED, gd->bitmap, gd_x2a, gd_y2a,
7980                       GDI_EVENT_MASK, event_mask,
7981                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7982                       GDI_CALLBACK_ACTION, HandleCheckbuttons,
7983                       GDI_END);
7984
7985     if (gi == NULL)
7986       Fail("cannot create gadget");
7987
7988     level_editor_gadget[id] = gi;
7989     right_gadget_border[id] =
7990       getRightGadgetBorder(gi, checkbutton_info[i].text_right);
7991   }
7992 }
7993
7994 static void CreateRadiobuttonGadgets(void)
7995 {
7996   int graphic = IMG_EDITOR_RADIOBUTTON;
7997   struct GraphicInfo *gd = &graphic_info[graphic];
7998   int gd_x1 = gd->src_x;
7999   int gd_y1 = gd->src_y;
8000   int gd_x2 = gd->src_x + gd->pressed_xoffset;
8001   int gd_y2 = gd->src_y + gd->pressed_yoffset;
8002   int gd_x1a = gd->src_x + gd->active_xoffset;
8003   int gd_y1a = gd->src_y + gd->active_yoffset;
8004   int gd_x2a = gd->src_x + gd->active_xoffset + gd->pressed_xoffset;
8005   int gd_y2a = gd->src_y + gd->active_yoffset + gd->pressed_yoffset;
8006   struct GadgetInfo *gi;
8007   int i;
8008
8009   for (i = 0; i < ED_NUM_RADIOBUTTONS; i++)
8010   {
8011     int id = radiobutton_info[i].gadget_id;
8012     int type_id = radiobutton_info[i].gadget_type_id;
8013     int x = SX + ED_SETTINGS_X(radiobutton_info[i].x);
8014     int y = SY + ED_SETTINGS_Y(radiobutton_info[i].y);
8015     unsigned int event_mask = GD_EVENT_PRESSED;
8016
8017     if (type_id != i)
8018       Fail("'radiobutton_info' structure corrupted at index %d -- please fix", i);
8019
8020     int checked =
8021       (*radiobutton_info[i].value == radiobutton_info[i].checked_value);
8022
8023     // determine horizontal position to the right of specified gadget
8024     if (radiobutton_info[i].gadget_id_align != GADGET_ID_NONE)
8025       x = (right_gadget_border[radiobutton_info[i].gadget_id_align] +
8026            ED_GADGET_TEXT_DISTANCE);
8027
8028     // determine horizontal offset for leading text
8029     if (radiobutton_info[i].text_left != NULL)
8030       x += getTextWidthForGadget(radiobutton_info[i].text_left);
8031
8032     gi = CreateGadget(GDI_CUSTOM_ID, id,
8033                       GDI_CUSTOM_TYPE_ID, type_id,
8034                       GDI_IMAGE_ID, graphic,
8035                       GDI_INFO_TEXT, radiobutton_info[i].infotext,
8036                       GDI_X, x,
8037                       GDI_Y, y,
8038                       GDI_WIDTH, gd->width,
8039                       GDI_HEIGHT, gd->height,
8040                       GDI_TYPE, GD_TYPE_RADIO_BUTTON,
8041                       GDI_RADIO_NR, radiobutton_info[i].radio_button_nr,
8042                       GDI_CHECKED, checked,
8043                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
8044                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
8045                       GDI_ALT_DESIGN_UNPRESSED, gd->bitmap, gd_x1a, gd_y1a,
8046                       GDI_ALT_DESIGN_PRESSED, gd->bitmap, gd_x2a, gd_y2a,
8047                       GDI_EVENT_MASK, event_mask,
8048                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
8049                       GDI_CALLBACK_ACTION, HandleRadiobuttons,
8050                       GDI_END);
8051
8052     if (gi == NULL)
8053       Fail("cannot create gadget");
8054
8055     level_editor_gadget[id] = gi;
8056     right_gadget_border[id] =
8057       getRightGadgetBorder(gi, radiobutton_info[i].text_right);
8058   }
8059 }
8060
8061 void CreateLevelEditorGadgets(void)
8062 {
8063   // force EDITOR font inside level editor
8064   SetFontStatus(GAME_MODE_EDITOR);
8065
8066   // these values are not constant, but can change at runtime
8067   ed_fieldx = MAX_ED_FIELDX - 1;
8068   ed_fieldy = MAX_ED_FIELDY - 1;
8069
8070   num_editor_gadgets = NUM_EDITOR_GADGETS;
8071
8072   // Debug("editor", "allocating %d gadgets ...\n", num_editor_gadgets);
8073
8074   level_editor_gadget =
8075     checked_calloc(num_editor_gadgets * sizeof(struct GadgetInfo *));
8076   right_gadget_border =
8077     checked_calloc(num_editor_gadgets * sizeof(int));
8078
8079   // set number of empty (padding) element buttons to maximum number of buttons
8080   num_editor_el_empty = ED_NUM_ELEMENTLIST_BUTTONS;
8081
8082   editor_el_empty = checked_calloc(num_editor_el_empty * sizeof(int));
8083   editor_el_empty_ptr = editor_el_empty;
8084
8085   use_permanent_palette = !editor.palette.show_as_separate_screen;
8086
8087   InitGadgetScreenBorders(-1, INFOTEXT_YPOS);
8088
8089   ReinitializeElementList();
8090
8091   CreateControlButtons();
8092   CreateScrollbarGadgets();
8093
8094   // order of function calls is important because of cross-references
8095   CreateCheckbuttonGadgets();
8096   CreateCounterButtons();
8097   CreateRadiobuttonGadgets();
8098   CreateTextInputGadgets();
8099   CreateTextAreaGadgets();
8100   CreateSelectboxGadgets();
8101   CreateGraphicbuttonGadgets();
8102   CreateTextbuttonGadgets();
8103   CreateDrawingAreas();
8104
8105   ResetFontStatus();
8106 }
8107
8108 void FreeLevelEditorGadgets(void)
8109 {
8110   int i;
8111
8112   // Debug("editor", "freeing %d gadgets ...\n", num_editor_gadgets);
8113
8114   for (i = 0; i < num_editor_gadgets; i++)
8115   {
8116     FreeGadget(level_editor_gadget[i]);
8117
8118     level_editor_gadget[i] = NULL;
8119   }
8120
8121   checked_free(level_editor_gadget);
8122   checked_free(right_gadget_border);
8123
8124   checked_free(editor_el_empty);
8125 }
8126
8127 static void MapCounterButtons(int id)
8128 {
8129   int font_nr = FONT_TEXT_1;
8130   int font_height = getFontHeight(font_nr);
8131   int gadget_id_down = counterbutton_info[id].gadget_id_down;
8132   int gadget_id_text = counterbutton_info[id].gadget_id_text;
8133   int gadget_id_up   = counterbutton_info[id].gadget_id_up;
8134   struct GadgetInfo *gi_down = level_editor_gadget[gadget_id_down];
8135   struct GadgetInfo *gi_text = level_editor_gadget[gadget_id_text];
8136   struct GadgetInfo *gi_up   = level_editor_gadget[gadget_id_up];
8137   int xoffset_left = getTextWidthForGadget(counterbutton_info[id].text_left);
8138   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8139   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8140   int yoffset = (gi_down->height - font_height) / 2;
8141   int x_left = gi_down->x - xoffset_left;
8142   int x_right;  // set after gadget position was modified
8143   int y_above = gi_down->y - yoffset_above;
8144   int x = gi_down->x;
8145   int y;        // set after gadget position was modified
8146
8147   // counter limits must be changed first to prevent value truncation
8148   ModifyEditorCounterLimits(id, counterbutton_info[id].min_value,
8149                             counterbutton_info[id].max_value);
8150
8151   // right text position might have changed after setting position above
8152   x_right = gi_up->x + gi_up->width + xoffset_right;
8153
8154   ModifyEditorCounterValue(id, *counterbutton_info[id].value);
8155
8156   // set position for counter gadgets with dynamically determined position
8157   ModifyGadget(gi_down, GDI_Y, SY + ED_SETTINGS_Y(counterbutton_info[id].y), GDI_END);
8158   ModifyGadget(gi_text, GDI_Y, SY + ED_SETTINGS_Y(counterbutton_info[id].y), GDI_END);
8159   ModifyGadget(gi_up,   GDI_Y, SY + ED_SETTINGS_Y(counterbutton_info[id].y), GDI_END);
8160
8161   // vertical position might have changed after setting position above
8162   y = gi_up->y + yoffset;
8163
8164   if (counterbutton_info[id].text_above)
8165     DrawText(x, y_above, counterbutton_info[id].text_above, font_nr);
8166
8167   if (counterbutton_info[id].text_left)
8168     DrawText(x_left, y, counterbutton_info[id].text_left, font_nr);
8169
8170   if (counterbutton_info[id].text_right)
8171     DrawText(x_right, y, counterbutton_info[id].text_right, font_nr);
8172
8173   MapGadget(gi_down);
8174   MapGadget(gi_text);
8175   MapGadget(gi_up);
8176 }
8177
8178 static void MapControlButtons(void)
8179 {
8180   int counter_id;
8181   int i;
8182
8183   // map toolbox buttons (excluding special CE toolbox buttons)
8184   for (i = 0; i < ED_NUM_CTRL1_2_BUTTONS; i++)
8185     MapGadget(level_editor_gadget[i]);
8186
8187   // map toolbox buttons (element properties buttons)
8188   for (i = ED_NUM_CTRL1_4_BUTTONS; i < ED_NUM_CTRL1_7_BUTTONS; i++)
8189     MapGadget(level_editor_gadget[i]);
8190
8191   if (use_permanent_palette)
8192   {
8193     // map buttons to select elements
8194     for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
8195       MapGadget(level_editor_gadget[GADGET_ID_ELEMENTLIST_FIRST + i]);
8196     MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL]);
8197     MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_UP]);
8198     MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_DOWN]);
8199   }
8200
8201   // map buttons to select level
8202   counter_id = ED_COUNTER_ID_SELECT_LEVEL;
8203   counterbutton_info[counter_id].min_value = leveldir_current->first_level;
8204   counterbutton_info[counter_id].max_value = leveldir_current->last_level;
8205   MapCounterButtons(counter_id);
8206 }
8207
8208 static void MapDrawingArea(int id)
8209 {
8210   int font_nr = FONT_TEXT_1;
8211   int font_height = getFontHeight(font_nr);
8212   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
8213   int area_xsize = gi->drawing.area_xsize;
8214   int area_ysize = gi->drawing.area_ysize;
8215   int xoffset_left = getTextWidthForDrawingArea(drawingarea_info[id].text_left);
8216   int xoffset_below = getTextWidth(drawingarea_info[id].text_below, font_nr);
8217   int x_left  = gi->x - xoffset_left;
8218   int x_right = gi->x + gi->width + ED_DRAWINGAREA_TEXT_DISTANCE;
8219   int x_above = gi->x - ED_DRAWINGAREA_BORDER_SIZE;
8220   int x_below = gi->x + (gi->width - xoffset_below) / 2;
8221   int y_side  = gi->y + (gi->height - font_height) / 2;
8222   int y_above = gi->y - font_height - ED_DRAWINGAREA_TEXT_DISTANCE;
8223   int y_below = gi->y + gi->height + ED_DRAWINGAREA_TEXT_DISTANCE;
8224
8225   if (drawingarea_info[id].text_left)
8226     DrawText(x_left, y_side, drawingarea_info[id].text_left, font_nr);
8227
8228   if (drawingarea_info[id].text_right)
8229     DrawText(x_right, y_side, drawingarea_info[id].text_right, font_nr);
8230
8231   if (drawingarea_info[id].text_above)
8232     DrawText(x_above, y_above, drawingarea_info[id].text_above, font_nr);
8233
8234   if (drawingarea_info[id].text_below)
8235     DrawText(x_below, y_below, drawingarea_info[id].text_below, font_nr);
8236
8237   if (id != ED_DRAWING_ID_DRAWING_LEVEL)
8238   {
8239     DrawElementBorder(gi->x, gi->y,
8240                       area_xsize * ED_DRAWINGAREA_TILE_SIZE,
8241                       area_ysize * ED_DRAWINGAREA_TILE_SIZE, TRUE);
8242
8243     DrawDrawingArea(id);
8244   }
8245
8246   MapGadget(gi);
8247 }
8248
8249 static void MapTextInputGadget(int id)
8250 {
8251   int font_nr = FONT_TEXT_1;
8252   int font_height = getFontHeight(font_nr);
8253   struct GadgetInfo *gi = level_editor_gadget[textinput_info[id].gadget_id];
8254   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8255   int x_above = ED_SETTINGS_X(textinput_info[id].x);
8256   int y_above = ED_SETTINGS_Y(textinput_info[id].y) - yoffset_above;
8257
8258   if (textinput_info[id].text_above)
8259     DrawTextS(x_above, y_above, font_nr, textinput_info[id].text_above);
8260
8261   ModifyGadget(gi, GDI_TEXT_VALUE, textinput_info[id].value, GDI_END);
8262
8263   MapGadget(gi);
8264 }
8265
8266 static void MapTextAreaGadget(int id)
8267 {
8268   int font_nr = FONT_TEXT_1;
8269   int font_height = getFontHeight(font_nr);
8270   struct GadgetInfo *gi = level_editor_gadget[textarea_info[id].gadget_id];
8271   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8272   int x_above = ED_SETTINGS_X(textarea_info[id].x);
8273   int y_above = ED_SETTINGS_Y(textarea_info[id].y) - yoffset_above;
8274   char *text_above = textarea_info[id].text_above;
8275
8276   if (gi->textarea.cropped && textarea_info[id].text_above_cropped)
8277     text_above = textarea_info[id].text_above_cropped;
8278
8279   if (text_above)
8280     DrawTextS(x_above, y_above, font_nr, text_above);
8281
8282   ModifyGadget(gi, GDI_TEXT_VALUE, textarea_info[id].value, GDI_END);
8283
8284   MapGadget(gi);
8285 }
8286
8287 static void MapSelectboxGadget(int id)
8288 {
8289   int font_nr = FONT_TEXT_1;
8290   int font_height = getFontHeight(font_nr);
8291   struct GadgetInfo *gi = level_editor_gadget[selectbox_info[id].gadget_id];
8292   int xoffset_left = getTextWidthForGadget(selectbox_info[id].text_left);
8293   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8294   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8295   int yoffset = (gi->height - font_height) / 2;
8296   int x_left = gi->x - xoffset_left;
8297   int x_right = gi->x + gi->width + xoffset_right;
8298   int y_above = gi->y - yoffset_above;
8299   int x = gi->x;
8300   int y = gi->y + yoffset;
8301
8302   if (selectbox_info[id].text_above)
8303     DrawText(x, y_above, selectbox_info[id].text_above, font_nr);
8304
8305   if (selectbox_info[id].text_left)
8306     DrawText(x_left, y, selectbox_info[id].text_left, font_nr);
8307
8308   if (selectbox_info[id].text_right)
8309     DrawText(x_right, y, selectbox_info[id].text_right, font_nr);
8310
8311   ModifyEditorSelectboxValue(id, *selectbox_info[id].value);
8312
8313   MapGadget(gi);
8314 }
8315
8316 static void MapTextbuttonGadget(int id)
8317 {
8318   int font_nr = FONT_TEXT_1;
8319   int font_height = getFontHeight(font_nr);
8320   struct GadgetInfo *gi = level_editor_gadget[textbutton_info[id].gadget_id];
8321   int xoffset_left = getTextWidthForGadget(textbutton_info[id].text_left);
8322   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8323   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8324   int yoffset = (gi->height - font_height) / 2;
8325   int x_left = gi->x - xoffset_left;
8326   int x_right = gi->x + gi->width + xoffset_right;
8327   int y_above = gi->y - yoffset_above;
8328   int x = gi->x;
8329   int y = gi->y + yoffset;
8330
8331   // only show button to delete change pages when more than minimum pages
8332   if (id == ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE &&
8333       custom_element.num_change_pages == MIN_CHANGE_PAGES)
8334     return;
8335
8336   if (textbutton_info[id].text_above)
8337     DrawText(x, y_above, textbutton_info[id].text_above, font_nr);
8338
8339   if (textbutton_info[id].text_left)
8340     DrawText(x_left, y, textbutton_info[id].text_left, font_nr);
8341
8342   if (textbutton_info[id].text_right)
8343     DrawText(x_right, y, textbutton_info[id].text_right, font_nr);
8344
8345   MapGadget(gi);
8346 }
8347
8348 static void MapGraphicbuttonGadget(int id)
8349 {
8350   int font_nr = FONT_TEXT_1;
8351   int font_height = getFontHeight(font_nr);
8352   struct GadgetInfo *gi = level_editor_gadget[graphicbutton_info[id].gadget_id];
8353   int xoffset_left = getTextWidthForGadget(graphicbutton_info[id].text_left);
8354   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8355   int yoffset = (gi->height - font_height) / 2;
8356   int x_left = gi->x - xoffset_left;
8357   int x_right = gi->x + gi->width + xoffset_right;
8358   int y = gi->y + yoffset;
8359
8360   if (graphicbutton_info[id].text_left)
8361     DrawText(x_left, y, graphicbutton_info[id].text_left, font_nr);
8362
8363   if (graphicbutton_info[id].text_right)
8364     DrawText(x_right, y, graphicbutton_info[id].text_right, font_nr);
8365
8366   MapGadget(gi);
8367 }
8368
8369 static void MapRadiobuttonGadget(int id)
8370 {
8371   int font_nr = FONT_TEXT_1;
8372   int font_height = getFontHeight(font_nr);
8373   struct GadgetInfo *gi = level_editor_gadget[radiobutton_info[id].gadget_id];
8374   int xoffset_left = getTextWidthForGadget(checkbutton_info[id].text_left);
8375   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8376   int yoffset = (gi->height - font_height) / 2;
8377   int x_left = gi->x - xoffset_left;
8378   int x_right = gi->x + gi->width + xoffset_right;
8379   int y = gi->y + yoffset;
8380   boolean checked =
8381     (*radiobutton_info[id].value == radiobutton_info[id].checked_value);
8382
8383   if (radiobutton_info[id].text_left)
8384     DrawText(x_left, y, radiobutton_info[id].text_left, font_nr);
8385
8386   if (radiobutton_info[id].text_right)
8387     DrawText(x_right, y, radiobutton_info[id].text_right, font_nr);
8388
8389   ModifyGadget(gi, GDI_CHECKED, checked, GDI_END);
8390
8391   MapGadget(gi);
8392 }
8393
8394 static void MapCheckbuttonGadget(int id)
8395 {
8396   int font_nr = FONT_TEXT_1;
8397   int font_height = getFontHeight(font_nr);
8398   struct GadgetInfo *gi = level_editor_gadget[checkbutton_info[id].gadget_id];
8399   int xoffset_left = getTextWidthForGadget(checkbutton_info[id].text_left);
8400   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8401   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8402   int yoffset = (gi->height - font_height) / 2;
8403   int y_above = gi->y - yoffset_above;
8404   int x = gi->x;
8405   int x_left, x_right, y;       // set after gadget position was modified
8406
8407   // set position for gadgets with dynamically determined position
8408   if (checkbutton_info[id].x != -1)     // do not change dynamic positions
8409     ModifyGadget(gi, GDI_X, SX + ED_SETTINGS_X(checkbutton_info[id].x), GDI_END);
8410   ModifyGadget(gi, GDI_Y, SY + ED_SETTINGS_Y(checkbutton_info[id].y), GDI_END);
8411
8412   x_left = gi->x - xoffset_left;
8413   x_right = gi->x + gi->width + xoffset_right;
8414   y = gi->y + yoffset;
8415
8416   if (checkbutton_info[id].text_above)
8417     DrawText(x, y_above, checkbutton_info[id].text_above, font_nr);
8418
8419   if (checkbutton_info[id].text_left)
8420     DrawText(x_left, y, checkbutton_info[id].text_left, font_nr);
8421
8422   if (checkbutton_info[id].text_right)
8423     DrawText(x_right, y, checkbutton_info[id].text_right, font_nr);
8424
8425   ModifyGadget(gi, GDI_CHECKED, *checkbutton_info[id].value, GDI_END);
8426
8427   MapGadget(gi);
8428 }
8429
8430 static void MapMainDrawingArea(void)
8431 {
8432   boolean no_horizontal_scrollbar = (lev_fieldx + 2 <= ed_fieldx);
8433   boolean no_vertical_scrollbar = (lev_fieldy + 2 <= ed_fieldy);
8434   int i;
8435
8436   if (suppressBorderElement())
8437   {
8438     no_horizontal_scrollbar = (lev_fieldx <= ed_fieldx);
8439     no_vertical_scrollbar   = (lev_fieldy <= ed_fieldy);
8440   }
8441
8442   for (i = ED_SCROLLBUTTON_ID_AREA_FIRST; i <= ED_SCROLLBUTTON_ID_AREA_LAST; i++)
8443   {
8444     if (((i == ED_SCROLLBUTTON_ID_AREA_LEFT ||
8445           i == ED_SCROLLBUTTON_ID_AREA_RIGHT) &&
8446          no_horizontal_scrollbar) ||
8447         ((i == ED_SCROLLBUTTON_ID_AREA_UP ||
8448           i == ED_SCROLLBUTTON_ID_AREA_DOWN) &&
8449          no_vertical_scrollbar))
8450       continue;
8451
8452     MapGadget(level_editor_gadget[scrollbutton_info[i].gadget_id]);
8453   }
8454
8455   for (i = ED_SCROLLBAR_ID_AREA_FIRST; i <= ED_SCROLLBAR_ID_AREA_LAST; i++)
8456   {
8457     if ((i == ED_SCROLLBAR_ID_AREA_HORIZONTAL && no_horizontal_scrollbar) ||
8458         (i == ED_SCROLLBAR_ID_AREA_VERTICAL && no_vertical_scrollbar))
8459       continue;
8460
8461     MapGadget(level_editor_gadget[scrollbar_info[i].gadget_id]);
8462   }
8463
8464   MapDrawingArea(ED_DRAWING_ID_DRAWING_LEVEL);
8465 }
8466
8467 static void MapOrUnmapLevelEditorToolboxCustomGadgets(boolean map)
8468 {
8469   int i;
8470
8471   for (i = 0; i < ED_NUM_CTRL_BUTTONS; i++)
8472   {
8473     if (i == GADGET_ID_CUSTOM_COPY_FROM ||
8474         i == GADGET_ID_CUSTOM_COPY_TO ||
8475         i == GADGET_ID_CUSTOM_EXCHANGE ||
8476         i == GADGET_ID_CUSTOM_COPY ||
8477         i == GADGET_ID_CUSTOM_PASTE)
8478     {
8479       if (map)
8480         MapGadget(level_editor_gadget[i]);
8481       else
8482         UnmapGadget(level_editor_gadget[i]);
8483     }
8484   }
8485 }
8486
8487 static void MapLevelEditorToolboxCustomGadgets(void)
8488 {
8489   MapOrUnmapLevelEditorToolboxCustomGadgets(TRUE);
8490 }
8491
8492 static void MapLevelEditorToolboxCustomGadgetsIfNeeded(void)
8493 {
8494   if (IS_CUSTOM_ELEMENT(properties_element) ||
8495       IS_GROUP_ELEMENT(properties_element) ||
8496       IS_EMPTY_ELEMENT(properties_element))
8497     MapLevelEditorToolboxCustomGadgets();
8498 }
8499
8500 static void UnmapLevelEditorToolboxCustomGadgets(void)
8501 {
8502   MapOrUnmapLevelEditorToolboxCustomGadgets(FALSE);
8503 }
8504
8505 static void MapOrUnmapLevelEditorToolboxDrawingGadgets(boolean map)
8506 {
8507   int i;
8508
8509   for (i = 0; i < ED_NUM_CTRL1_BUTTONS; i++)
8510   {
8511     if (i != GADGET_ID_SINGLE_ITEMS &&
8512         i != GADGET_ID_PICK_ELEMENT)
8513     {
8514       struct GadgetInfo *gi = level_editor_gadget[i];
8515
8516       if (map)
8517       {
8518         MapGadget(gi);
8519       }
8520       else
8521       {
8522         int graphic = IMG_EDITOR_NO_TOOLBOX_BUTTON;
8523         struct GraphicInfo *gd = &graphic_info[graphic];
8524
8525         UnmapGadget(gi);
8526
8527         BlitBitmap(gd->bitmap, drawto, gd->src_x, gd->src_y,
8528                    gi->width, gi->height, gi->x, gi->y);
8529
8530         redraw_mask |= REDRAW_DOOR_3;
8531       }
8532     }
8533   }
8534 }
8535
8536 static void MapLevelEditorToolboxDrawingGadgets(void)
8537 {
8538   MapOrUnmapLevelEditorToolboxDrawingGadgets(TRUE);
8539 }
8540
8541 static void UnmapLevelEditorToolboxDrawingGadgets(void)
8542 {
8543   MapOrUnmapLevelEditorToolboxDrawingGadgets(FALSE);
8544 }
8545
8546 static void UnmapDrawingArea(int id)
8547 {
8548   UnmapGadget(level_editor_gadget[drawingarea_info[id].gadget_id]);
8549 }
8550
8551 static void UnmapLevelEditorFieldGadgets(void)
8552 {
8553   int i;
8554
8555   for (i = 0; i < num_editor_gadgets; i++)
8556     if (IN_GFX_FIELD_FULL(level_editor_gadget[i]->x,
8557                           level_editor_gadget[i]->y))
8558       UnmapGadget(level_editor_gadget[i]);
8559 }
8560
8561 void UnmapLevelEditorGadgets(void)
8562 {
8563   int i;
8564
8565   for (i = 0; i < num_editor_gadgets; i++)
8566     UnmapGadget(level_editor_gadget[i]);
8567 }
8568
8569 static void ResetUndoBuffer(void)
8570 {
8571   undo_buffer_position = -1;
8572   undo_buffer_steps = -1;
8573   redo_buffer_steps = 0;
8574
8575   CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
8576
8577   level.changed = FALSE;
8578 }
8579
8580 static void DrawEditModeWindowExt(boolean remap_toolbox_gadgets)
8581 {
8582   if (remap_toolbox_gadgets)
8583   {
8584     ModifyEditorElementList();
8585     RedrawDrawingElements();
8586   }
8587
8588   if (edit_mode == ED_MODE_LEVELCONFIG)
8589     DrawLevelConfigWindow();
8590   else if (edit_mode == ED_MODE_PROPERTIES)
8591     DrawPropertiesWindow();
8592   else if (edit_mode == ED_MODE_PALETTE)
8593     DrawPaletteWindow();
8594   else  // edit_mode == ED_MODE_DRAWING
8595     DrawDrawingWindowExt(remap_toolbox_gadgets);
8596 }
8597
8598 static void DrawEditModeWindow(void)
8599 {
8600   DrawEditModeWindowExt(TRUE);
8601 }
8602
8603 static void DrawEditModeWindow_PlayfieldOnly(void)
8604 {
8605   DrawEditModeWindowExt(FALSE);
8606 }
8607
8608 static void ChangeEditModeWindow(int new_edit_mode)
8609 {
8610   edit_mode = (new_edit_mode != edit_mode ? new_edit_mode : ED_MODE_DRAWING);
8611
8612   DrawEditModeWindow();
8613 }
8614
8615 static boolean LevelChanged(void)
8616 {
8617   boolean field_changed = FALSE;
8618   int x, y;
8619
8620   for (y = 0; y < lev_fieldy; y++) 
8621     for (x = 0; x < lev_fieldx; x++)
8622       if (Tile[x][y] != level.field[x][y])
8623         field_changed = TRUE;
8624
8625   return (level.changed || field_changed);
8626 }
8627
8628 static boolean PrepareSavingIntoPersonalLevelSet(void)
8629 {
8630   static LevelDirTree *last_copied_leveldir = NULL;
8631   static LevelDirTree *last_written_leveldir = NULL;
8632   static int last_copied_level_nr = -1;
8633   static int last_written_level_nr = -1;
8634   LevelDirTree *leveldir_former = leveldir_current;
8635   int level_nr_former = level_nr;
8636   int new_level_nr;
8637
8638   // remember last mod/save so that for current session, we write
8639   // back to the same personal copy, asking only about overwrite.
8640   if (leveldir_current == last_copied_leveldir &&
8641       level_nr == last_copied_level_nr)
8642   {
8643     // "cd" to personal level set dir (as used when writing last copy)
8644     leveldir_current = last_written_leveldir;
8645     level_nr = last_written_level_nr;
8646
8647     return TRUE;
8648   }
8649
8650   if (!Request("This level is read-only! "
8651                "Save into personal level set?", REQ_ASK))
8652     return FALSE;
8653
8654   // "cd" to personal level set dir (for writing copy the first time)
8655   leveldir_current =
8656     getTreeInfoFromIdentifier(leveldir_first, getLoginName());
8657
8658   // this may happen if "setup.internal.create_user_levelset" is FALSE
8659   // or if file "levelinfo.conf" is missing in personal user level set
8660   if (leveldir_current == NULL)
8661   {
8662     Request("Cannot find personal level set?!", REQ_CONFIRM);
8663
8664     leveldir_current = leveldir_former;
8665
8666     return FALSE;
8667   }
8668
8669   // find unused level number
8670   for (new_level_nr = leveldir_current->first_level; ; new_level_nr++)
8671   {
8672     static char *level_filename = NULL;
8673
8674     setString(&level_filename, getDefaultLevelFilename(new_level_nr));
8675
8676     if (!fileExists(level_filename))
8677       break;
8678   }
8679
8680   last_copied_leveldir = leveldir_former;
8681   last_copied_level_nr = level_nr_former;
8682
8683   last_written_leveldir = leveldir_current;
8684   last_written_level_nr = level_nr = new_level_nr;
8685
8686   return TRUE;
8687 }
8688
8689 static void ModifyLevelConfigForSavingIntoPersonalLevelSet(char *former_name)
8690 {
8691   static char *filename_levelinfo = NULL, *mod_name = NULL;
8692   FILE *file;
8693
8694   // annotate this copy-and-mod in personal levelinfo.conf
8695   setString(&filename_levelinfo,
8696             getPath2(getCurrentLevelDir(), LEVELINFO_FILENAME));
8697
8698   if ((file = fopen(filename_levelinfo, MODE_APPEND)))
8699   {
8700     fprintf(file, "\n");
8701     fprintf(file, "# level %d was modified from:\n", level_nr);
8702     fprintf(file, "# - previous level set name:    %s\n",
8703             former_name);
8704     fprintf(file, "# - level within previous set:  %d \"%s\"\n",
8705             level.file_info.nr, level.name);
8706     fprintf(file, "# - previous author:            %s\n",
8707             level.author);
8708     fprintf(file, "# - previous save date:         ");
8709
8710     if (level.creation_date.src == DATE_SRC_LEVELFILE)
8711     {
8712       fprintf(file, "%04d-%02d-%02d\n",
8713               level.creation_date.year,
8714               level.creation_date.month,
8715               level.creation_date.day);
8716     }
8717     else
8718     {
8719       fprintf(file, "not recorded\n");
8720     }
8721
8722     fclose(file);
8723   }
8724
8725   if (level_nr > leveldir_current->last_level)
8726     UpdateUserLevelSet(getLoginName(), NULL, NULL, level_nr + 9);
8727
8728   // else: allow the save even if annotation failed
8729
8730   // now... spray graffiti on the old level vital statistics
8731   // user can change these; just trying to set a good baseline
8732
8733   // don't truncate names for fear of making offensive or silly:
8734   // long-named original author only recorded in levelinfo.conf.
8735   // try to fit "Joe after Bob", "Joe (ed.)", then just "Joe"
8736   if (!strEqual(level.author, leveldir_current->author))
8737   {
8738     setString(&mod_name, getStringCat3(leveldir_current->author,
8739                                        " after ", level.author));
8740
8741     if (strlen(mod_name) > MAX_LEVEL_AUTHOR_LEN)
8742       setString(&mod_name,
8743                 getStringCat2(leveldir_current->author, " (ed.)"));
8744
8745     if (strlen(mod_name) > MAX_LEVEL_AUTHOR_LEN)
8746       setString(&mod_name, leveldir_current->author);
8747
8748     strncpy(level.author, mod_name, MAX_LEVEL_AUTHOR_LEN);
8749
8750     // less worried about truncation here
8751     setString(&mod_name, getStringCat2("Mod: ", level.name));
8752     strncpy(level.name, mod_name, MAX_LEVEL_NAME_LEN);
8753   }
8754 }
8755
8756 static void CopyPlayfield(short src[MAX_LEV_FIELDX][MAX_LEV_FIELDY],
8757                           short dst[MAX_LEV_FIELDX][MAX_LEV_FIELDY])
8758 {
8759   int x, y;
8760
8761   for (x = 0; x < lev_fieldx; x++)
8762     for (y = 0; y < lev_fieldy; y++) 
8763       dst[x][y] = src[x][y];
8764 }
8765
8766 static int setSelectboxValue(int selectbox_id, int new_value)
8767 {
8768   int new_index_value = 0;
8769   int i;
8770
8771   for (i = 0; selectbox_info[selectbox_id].options[i].text != NULL; i++)
8772     if (selectbox_info[selectbox_id].options[i].value == new_value)
8773       new_index_value = i;
8774
8775   *selectbox_info[selectbox_id].value =
8776     selectbox_info[selectbox_id].options[new_index_value].value;
8777
8778   return new_index_value;
8779 }
8780
8781 static void setSelectboxSpecialActionVariablesIfNeeded(void)
8782 {
8783   int i;
8784
8785   // change action mode and arg variables according to action type variable
8786   for (i = 0; action_arg_options[i].value != -1; i++)
8787   {
8788     if (action_arg_options[i].value == custom_element_change.action_type)
8789     {
8790       int mode = action_arg_options[i].mode;
8791
8792       // only change if corresponding selectbox has changed
8793       if (selectbox_info[ED_SELECTBOX_ID_ACTION_MODE].options !=
8794           action_arg_modes[mode])
8795         custom_element_change.action_mode = -1;
8796
8797       // only change if corresponding selectbox has changed
8798       if (selectbox_info[ED_SELECTBOX_ID_ACTION_ARG].options !=
8799           action_arg_options[i].options)
8800         custom_element_change.action_arg = -1;
8801
8802       break;
8803     }
8804   }
8805 }
8806
8807 static void setSelectboxSpecialActionOptions(void)
8808 {
8809   int i;
8810
8811   // change action mode and arg selectbox according to action type selectbox
8812   for (i = 0; action_arg_options[i].value != -1; i++)
8813   {
8814     if (action_arg_options[i].value == custom_element_change.action_type)
8815     {
8816       int mode = action_arg_options[i].mode;
8817
8818       ModifyEditorSelectboxOptions(ED_SELECTBOX_ID_ACTION_MODE,
8819                                    action_arg_modes[mode]);
8820       ModifyEditorSelectboxValue(ED_SELECTBOX_ID_ACTION_MODE,
8821                                  custom_element_change.action_mode);
8822
8823       ModifyEditorSelectboxOptions(ED_SELECTBOX_ID_ACTION_ARG,
8824                                    action_arg_options[i].options);
8825       ModifyEditorSelectboxValue(ED_SELECTBOX_ID_ACTION_ARG,
8826                                  custom_element_change.action_arg);
8827       break;
8828     }
8829   }
8830 }
8831
8832 static void copy_custom_element_settings(int element_from, int element_to)
8833 {
8834   struct ElementInfo *ei_from = &element_info[element_from];
8835   struct ElementInfo *ei_to = &element_info[element_to];
8836
8837   copyElementInfo(ei_from, ei_to);
8838 }
8839
8840 static void replace_custom_element_in_settings(int element_from,
8841                                                int element_to)
8842 {
8843   int i, j, x, y;
8844
8845   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
8846   {
8847     struct ElementInfo *ei = &element_info[i];
8848
8849     for (y = 0; y < 3; y++)
8850       for (x = 0; x < 3; x++)
8851         if (ei->content.e[x][y] == element_from)
8852           ei->content.e[x][y] = element_to;
8853
8854     for (j = 0; j < ei->num_change_pages; j++)
8855     {
8856       struct ElementChangeInfo *change = &ei->change_page[j];
8857
8858       if (change->target_element == element_from)
8859         change->target_element = element_to;
8860
8861       if (change->initial_trigger_element == element_from)
8862         change->initial_trigger_element = element_to;
8863
8864       if (change->action_element == element_from)
8865         change->action_element = element_to;
8866
8867       for (y = 0; y < 3; y++)
8868         for (x = 0; x < 3; x++)
8869           if (change->target_content.e[x][y] == element_from)
8870             change->target_content.e[x][y] = element_to;
8871     }
8872
8873     if (ei->group != NULL)                              // group or internal
8874       for (j = 0; j < MAX_ELEMENTS_IN_GROUP; j++)
8875         if (ei->group->element[j] == element_from)
8876           ei->group->element[j] = element_to;
8877   }
8878 }
8879
8880 static void replace_custom_element_in_playfield(int element_from,
8881                                                 int element_to)
8882 {
8883   int x, y;
8884
8885   for (x = 0; x < lev_fieldx; x++)
8886     for (y = 0; y < lev_fieldy; y++)
8887       if (Tile[x][y] == element_from)
8888         Tile[x][y] = element_to;
8889 }
8890
8891 static boolean CopyCustomElement(int element_old, int element_new,
8892                                  int copy_mode)
8893 {
8894   int copy_mode_orig = copy_mode;
8895
8896   if (copy_mode == GADGET_ID_CUSTOM_COPY)
8897   {
8898     element_new = (IS_CUSTOM_ELEMENT(element_old) ?
8899                    EL_INTERNAL_CLIPBOARD_CUSTOM : EL_INTERNAL_CLIPBOARD_GROUP);
8900     copy_mode = GADGET_ID_CUSTOM_COPY_TO;
8901   }
8902   else if (copy_mode == GADGET_ID_CUSTOM_PASTE)
8903   {
8904     element_old = (IS_CUSTOM_ELEMENT(element_new) ?
8905                    EL_INTERNAL_CLIPBOARD_CUSTOM : EL_INTERNAL_CLIPBOARD_GROUP);
8906     copy_mode = GADGET_ID_CUSTOM_COPY_TO;
8907
8908     level.changed = TRUE;
8909   }
8910   else if (IS_CUSTOM_ELEMENT(element_old) && !IS_CUSTOM_ELEMENT(element_new))
8911   {
8912     Request("Please choose custom element!", REQ_CONFIRM);
8913
8914     return FALSE;
8915   }
8916   else if (IS_GROUP_ELEMENT(element_old) && !IS_GROUP_ELEMENT(element_new))
8917   {
8918     Request("Please choose group element!", REQ_CONFIRM);
8919
8920     return FALSE;
8921   }
8922   else if (IS_EMPTY_ELEMENT(element_old) && !IS_EMPTY_ELEMENT(element_new))
8923   {
8924     Request("Please choose empty element!", REQ_CONFIRM);
8925
8926     return FALSE;
8927   }
8928   else
8929   {
8930     level.changed = TRUE;
8931   }
8932
8933   // when modifying custom/group element, ask for copying level template
8934   if (copy_mode_orig != GADGET_ID_CUSTOM_COPY && level.use_custom_template)
8935   {
8936     if (!AskToCopyAndModifyLevelTemplate())
8937       return FALSE;
8938   }
8939
8940   if (copy_mode == GADGET_ID_CUSTOM_COPY_FROM)
8941   {
8942     copy_custom_element_settings(element_new, element_old);
8943   }
8944   else if (copy_mode == GADGET_ID_CUSTOM_COPY_TO)
8945   {
8946     copy_custom_element_settings(element_old, element_new);
8947   }
8948   else if (copy_mode == GADGET_ID_CUSTOM_EXCHANGE)
8949   {
8950     copy_custom_element_settings(element_old, EL_INTERNAL_DUMMY);
8951     copy_custom_element_settings(element_new, element_old);
8952     copy_custom_element_settings(EL_INTERNAL_DUMMY, element_new);
8953
8954     replace_custom_element_in_settings(element_old, EL_INTERNAL_DUMMY);
8955     replace_custom_element_in_settings(element_new, element_old);
8956     replace_custom_element_in_settings(EL_INTERNAL_DUMMY, element_new);
8957
8958     replace_custom_element_in_playfield(element_old, EL_INTERNAL_DUMMY);
8959     replace_custom_element_in_playfield(element_new, element_old);
8960     replace_custom_element_in_playfield(EL_INTERNAL_DUMMY, element_new);
8961   }
8962
8963   UpdateCustomElementGraphicGadgets();
8964   DrawPropertiesWindow();
8965
8966   return TRUE;
8967 }
8968
8969 static void CopyCustomElementPropertiesToEditor(int element)
8970 {
8971   int i;
8972   int current_change_page = element_info[element].current_change_page;
8973
8974   // dynamically (re)build selectbox for selecting change page
8975   for (i = 0; i < element_info[element].num_change_pages; i++)
8976   {
8977     sprintf(options_change_page_strings[i], "%d", i + 1);
8978
8979     options_change_page[i].value = i;
8980     options_change_page[i].text = options_change_page_strings[i];
8981   }
8982
8983   options_change_page[i].value = -1;
8984   options_change_page[i].text = NULL;
8985
8986   // needed here to initialize combined element properties
8987   InitElementPropertiesEngine(level.game_version);
8988
8989   element_info[element].change =
8990     &element_info[element].change_page[current_change_page];
8991
8992   custom_element = element_info[element];
8993   custom_element_change = *element_info[element].change;
8994
8995   // needed to initially set selectbox options for special action options
8996   setSelectboxSpecialActionOptions();
8997
8998   // needed to initially set selectbox value variables to reliable defaults
8999   for (i = 0; i < ED_NUM_SELECTBOX; i++)
9000     setSelectboxValue(i, *selectbox_info[i].value);
9001
9002   for (i = 0; i < NUM_ELEMENT_PROPERTIES; i++)
9003     custom_element_properties[i] = HAS_PROPERTY(element, i);
9004
9005   for (i = 0; i < NUM_CHANGE_EVENTS; i++)
9006     custom_element_change_events[i] = HAS_CHANGE_EVENT(element, i);
9007
9008   // ---------- element settings: configure (custom elements) -----------------
9009
9010   // set accessible layer selectbox help value
9011   custom_element.access_type =
9012     (IS_WALKABLE(element) ? EP_WALKABLE :
9013      IS_PASSABLE(element) ? EP_PASSABLE :
9014      custom_element.access_type);
9015   custom_element.access_layer =
9016     (IS_ACCESSIBLE_OVER(element) ? EP_ACCESSIBLE_OVER :
9017      IS_ACCESSIBLE_INSIDE(element) ? EP_ACCESSIBLE_INSIDE :
9018      IS_ACCESSIBLE_UNDER(element) ? EP_ACCESSIBLE_UNDER :
9019      custom_element.access_layer);
9020   custom_element.access_protected =
9021     (IS_PROTECTED(element) ? 1 : 0);
9022   custom_element_properties[EP_ACCESSIBLE] =
9023     (IS_ACCESSIBLE_OVER(element) ||
9024      IS_ACCESSIBLE_INSIDE(element) ||
9025      IS_ACCESSIBLE_UNDER(element));
9026
9027   // set walk-to-object action selectbox help value
9028   custom_element.walk_to_action =
9029     (IS_DIGGABLE(element) ? EP_DIGGABLE :
9030      IS_COLLECTIBLE_ONLY(element) ? EP_COLLECTIBLE_ONLY :
9031      IS_DROPPABLE(element) ? EP_DROPPABLE :
9032      IS_THROWABLE(element) ? EP_THROWABLE :
9033      IS_PUSHABLE(element) ? EP_PUSHABLE :
9034      custom_element.walk_to_action);
9035   custom_element_properties[EP_WALK_TO_OBJECT] =
9036     (IS_DIGGABLE(element) ||
9037      IS_COLLECTIBLE_ONLY(element) ||
9038      IS_DROPPABLE(element) ||
9039      IS_THROWABLE(element) ||
9040      IS_PUSHABLE(element));
9041
9042   // set smash targets selectbox help value
9043   custom_element.smash_targets =
9044     (CAN_SMASH_EVERYTHING(element) ? EP_CAN_SMASH_EVERYTHING :
9045      CAN_SMASH_ENEMIES(element) ? EP_CAN_SMASH_ENEMIES :
9046      CAN_SMASH_PLAYER(element) ? EP_CAN_SMASH_PLAYER :
9047      custom_element.smash_targets);
9048   custom_element_properties[EP_CAN_SMASH] =
9049     (CAN_SMASH_EVERYTHING(element) ||
9050      CAN_SMASH_ENEMIES(element) ||
9051      CAN_SMASH_PLAYER(element));
9052
9053   // set deadliness selectbox help value
9054   custom_element.deadliness =
9055     (DONT_TOUCH(element) ? EP_DONT_TOUCH :
9056      DONT_GET_HIT_BY(element) ? EP_DONT_GET_HIT_BY :
9057      DONT_COLLIDE_WITH(element) ? EP_DONT_COLLIDE_WITH :
9058      DONT_RUN_INTO(element) ? EP_DONT_RUN_INTO :
9059      custom_element.deadliness);
9060   custom_element_properties[EP_DEADLY] =
9061     (DONT_TOUCH(element) ||
9062      DONT_GET_HIT_BY(element) ||
9063      DONT_COLLIDE_WITH(element) ||
9064      DONT_RUN_INTO(element));
9065
9066   // ---------- element settings: advanced (custom elements) ------------------
9067
9068   // set "change by direct action" selectbox help value
9069   custom_element_change.direct_action =
9070     (HAS_CHANGE_EVENT(element, CE_NEXT_TO_PLAYER) ? CE_NEXT_TO_PLAYER :
9071      HAS_CHANGE_EVENT(element, CE_TOUCHED_BY_PLAYER) ? CE_TOUCHED_BY_PLAYER :
9072      HAS_CHANGE_EVENT(element, CE_PRESSED_BY_PLAYER) ? CE_PRESSED_BY_PLAYER :
9073      HAS_CHANGE_EVENT(element, CE_SWITCHED_BY_PLAYER) ? CE_SWITCHED_BY_PLAYER :
9074      HAS_CHANGE_EVENT(element, CE_SNAPPED_BY_PLAYER) ? CE_SNAPPED_BY_PLAYER :
9075      HAS_CHANGE_EVENT(element, CE_PUSHED_BY_PLAYER) ? CE_PUSHED_BY_PLAYER :
9076      HAS_CHANGE_EVENT(element, CE_ENTERED_BY_PLAYER) ? CE_ENTERED_BY_PLAYER :
9077      HAS_CHANGE_EVENT(element, CE_LEFT_BY_PLAYER) ? CE_LEFT_BY_PLAYER :
9078      HAS_CHANGE_EVENT(element, CE_DROPPED_BY_PLAYER) ? CE_DROPPED_BY_PLAYER :
9079      HAS_CHANGE_EVENT(element, CE_SWITCHED) ? CE_SWITCHED :
9080      HAS_CHANGE_EVENT(element, CE_HITTING_SOMETHING) ? CE_HITTING_SOMETHING :
9081      HAS_CHANGE_EVENT(element, CE_HIT_BY_SOMETHING) ? CE_HIT_BY_SOMETHING :
9082      HAS_CHANGE_EVENT(element, CE_BLOCKED) ? CE_BLOCKED :
9083      HAS_CHANGE_EVENT(element, CE_IMPACT) ? CE_IMPACT :
9084      HAS_CHANGE_EVENT(element, CE_SMASHED) ? CE_SMASHED :
9085      HAS_CHANGE_EVENT(element, CE_VALUE_CHANGES) ? CE_VALUE_CHANGES :
9086      HAS_CHANGE_EVENT(element, CE_SCORE_CHANGES) ? CE_SCORE_CHANGES :
9087      HAS_CHANGE_EVENT(element, CE_VALUE_GETS_ZERO) ? CE_VALUE_GETS_ZERO :
9088      HAS_CHANGE_EVENT(element, CE_SCORE_GETS_ZERO) ? CE_SCORE_GETS_ZERO :
9089      HAS_CHANGE_EVENT(element, CE_CLICKED_BY_MOUSE) ? CE_CLICKED_BY_MOUSE :
9090      HAS_CHANGE_EVENT(element, CE_PRESSED_BY_MOUSE) ? CE_PRESSED_BY_MOUSE :
9091      custom_element_change.direct_action);
9092
9093   // set "change by other element action" selectbox help value
9094   custom_element_change.other_action =
9095     (HAS_CHANGE_EVENT(element, CE_PLAYER_NEXT_TO_X) ? CE_PLAYER_NEXT_TO_X :
9096      HAS_CHANGE_EVENT(element, CE_PLAYER_TOUCHES_X) ? CE_PLAYER_TOUCHES_X :
9097      HAS_CHANGE_EVENT(element, CE_PLAYER_PRESSES_X) ? CE_PLAYER_PRESSES_X :
9098      HAS_CHANGE_EVENT(element, CE_PLAYER_SWITCHES_X) ? CE_PLAYER_SWITCHES_X :
9099      HAS_CHANGE_EVENT(element, CE_PLAYER_SNAPS_X) ? CE_PLAYER_SNAPS_X :
9100      HAS_CHANGE_EVENT(element, CE_PLAYER_PUSHES_X) ? CE_PLAYER_PUSHES_X :
9101      HAS_CHANGE_EVENT(element, CE_PLAYER_ENTERS_X) ? CE_PLAYER_ENTERS_X :
9102      HAS_CHANGE_EVENT(element, CE_PLAYER_LEAVES_X) ? CE_PLAYER_LEAVES_X :
9103      HAS_CHANGE_EVENT(element, CE_PLAYER_DIGS_X) ? CE_PLAYER_DIGS_X :
9104      HAS_CHANGE_EVENT(element, CE_PLAYER_COLLECTS_X) ? CE_PLAYER_COLLECTS_X :
9105      HAS_CHANGE_EVENT(element, CE_PLAYER_DROPS_X) ? CE_PLAYER_DROPS_X :
9106      HAS_CHANGE_EVENT(element, CE_NEXT_TO_X) ? CE_NEXT_TO_X :
9107      HAS_CHANGE_EVENT(element, CE_TOUCHING_X) ? CE_TOUCHING_X :
9108      HAS_CHANGE_EVENT(element, CE_HITTING_X) ? CE_HITTING_X :
9109      HAS_CHANGE_EVENT(element, CE_DIGGING_X) ? CE_DIGGING_X :
9110      HAS_CHANGE_EVENT(element, CE_HIT_BY_X) ? CE_HIT_BY_X :
9111      HAS_CHANGE_EVENT(element, CE_SWITCH_OF_X) ? CE_SWITCH_OF_X :
9112      HAS_CHANGE_EVENT(element, CE_CHANGE_OF_X) ? CE_CHANGE_OF_X :
9113      HAS_CHANGE_EVENT(element, CE_EXPLOSION_OF_X) ? CE_EXPLOSION_OF_X :
9114      HAS_CHANGE_EVENT(element, CE_MOVE_OF_X) ? CE_MOVE_OF_X :
9115      HAS_CHANGE_EVENT(element, CE_CREATION_OF_X) ? CE_CREATION_OF_X :
9116      HAS_CHANGE_EVENT(element, CE_VALUE_CHANGES_OF_X) ? CE_VALUE_CHANGES_OF_X :
9117      HAS_CHANGE_EVENT(element, CE_SCORE_CHANGES_OF_X) ? CE_SCORE_CHANGES_OF_X :
9118      HAS_CHANGE_EVENT(element, CE_VALUE_GETS_ZERO_OF_X) ? CE_VALUE_GETS_ZERO_OF_X :
9119      HAS_CHANGE_EVENT(element, CE_SCORE_GETS_ZERO_OF_X) ? CE_SCORE_GETS_ZERO_OF_X :
9120      HAS_CHANGE_EVENT(element, CE_MOUSE_CLICKED_ON_X) ? CE_MOUSE_CLICKED_ON_X :
9121      HAS_CHANGE_EVENT(element, CE_MOUSE_PRESSED_ON_X) ? CE_MOUSE_PRESSED_ON_X :
9122      custom_element_change.other_action);
9123 }
9124
9125 static void CopyGroupElementPropertiesToEditor(int element)
9126 {
9127   group_element_info = *element_info[element].group;
9128   custom_element = element_info[element];       // needed for description
9129 }
9130
9131 static void CopyEmptyElementPropertiesToEditor(int element)
9132 {
9133   custom_element = element_info[element];
9134 }
9135
9136 static void CopyClassicElementPropertiesToEditor(int element)
9137 {
9138   if (IS_PLAYER_ELEMENT(element) || COULD_MOVE_INTO_ACID(element))
9139     custom_element_properties[EP_CAN_MOVE_INTO_ACID] =
9140       getMoveIntoAcidProperty(&level, element);
9141
9142   if (MAYBE_DONT_COLLIDE_WITH(element))
9143     custom_element_properties[EP_DONT_COLLIDE_WITH] =
9144       getDontCollideWithProperty(&level, element);
9145 }
9146
9147 static void CopyElementPropertiesToEditor(int element)
9148 {
9149   if (IS_CUSTOM_ELEMENT(element))
9150     CopyCustomElementPropertiesToEditor(element);
9151   else if (IS_GROUP_ELEMENT(element))
9152     CopyGroupElementPropertiesToEditor(element);
9153   else if (IS_EMPTY_ELEMENT(element))
9154     CopyEmptyElementPropertiesToEditor(element);
9155   else
9156     CopyClassicElementPropertiesToEditor(element);
9157 }
9158
9159 static boolean AskToCopyAndModifyLevelTemplate(void)
9160 {
9161   if (Request("Copy and modify settings from level template?", REQ_ASK))
9162   {
9163     level.use_custom_template = FALSE;
9164
9165     ModifyGadget(level_editor_gadget[GADGET_ID_CUSTOM_USE_TEMPLATE_1],
9166                  GDI_CHECKED, FALSE, GDI_END);
9167     ModifyGadget(level_editor_gadget[GADGET_ID_CUSTOM_USE_TEMPLATE_2],
9168                  GDI_CHECKED, FALSE, GDI_END);
9169
9170     return TRUE;
9171   }
9172   else
9173   {
9174     LoadLevelTemplate(-1);      // this resets all element modifications ...
9175
9176     DrawEditModeWindow();       // ... and copies them to 'custom_element'
9177
9178     return FALSE;
9179   }
9180 }
9181
9182 static void CopyCustomElementPropertiesToGame(int element)
9183 {
9184   int i;
9185   int access_type_and_layer;
9186
9187   // mark that this custom element has been modified
9188   custom_element.modified_settings = TRUE;
9189   level.changed = TRUE;
9190
9191   if (level.use_custom_template)
9192     AskToCopyAndModifyLevelTemplate();
9193
9194   element_info[element] = custom_element;
9195   *element_info[element].change = custom_element_change;
9196
9197   // ---------- element settings: configure (custom elements) -----------------
9198
9199   // set accessible property from checkbox and selectbox
9200   custom_element_properties[EP_WALKABLE_OVER] = FALSE;
9201   custom_element_properties[EP_WALKABLE_INSIDE] = FALSE;
9202   custom_element_properties[EP_WALKABLE_UNDER] = FALSE;
9203   custom_element_properties[EP_PASSABLE_OVER] = FALSE;
9204   custom_element_properties[EP_PASSABLE_INSIDE] = FALSE;
9205   custom_element_properties[EP_PASSABLE_UNDER] = FALSE;
9206   access_type_and_layer = ((custom_element.access_type == EP_WALKABLE ?
9207                             EP_WALKABLE_OVER : EP_PASSABLE_OVER) +
9208                            (custom_element.access_layer - EP_ACCESSIBLE_OVER));
9209   custom_element_properties[access_type_and_layer] =
9210     custom_element_properties[EP_ACCESSIBLE];
9211   custom_element_properties[EP_PROTECTED] =
9212     (custom_element.access_protected != 0 &&
9213      custom_element_properties[EP_ACCESSIBLE]);
9214
9215   // set walk-to-object property from checkbox and selectbox
9216   custom_element_properties[EP_DIGGABLE] = FALSE;
9217   custom_element_properties[EP_COLLECTIBLE_ONLY] = FALSE;
9218   custom_element_properties[EP_DROPPABLE] = FALSE;
9219   custom_element_properties[EP_THROWABLE] = FALSE;
9220   custom_element_properties[EP_PUSHABLE] = FALSE;
9221   custom_element_properties[custom_element.walk_to_action] =
9222     custom_element_properties[EP_WALK_TO_OBJECT];
9223
9224   // set smash property from checkbox and selectbox
9225   custom_element_properties[EP_CAN_SMASH_PLAYER] = FALSE;
9226   custom_element_properties[EP_CAN_SMASH_ENEMIES] = FALSE;
9227   custom_element_properties[EP_CAN_SMASH_EVERYTHING] = FALSE;
9228   custom_element_properties[custom_element.smash_targets] =
9229     custom_element_properties[EP_CAN_SMASH];
9230
9231   // set deadliness property from checkbox and selectbox
9232   custom_element_properties[EP_DONT_RUN_INTO] = FALSE;
9233   custom_element_properties[EP_DONT_COLLIDE_WITH] = FALSE;
9234   custom_element_properties[EP_DONT_GET_HIT_BY] = FALSE;
9235   custom_element_properties[EP_DONT_TOUCH] = FALSE;
9236   custom_element_properties[custom_element.deadliness] =
9237     custom_element_properties[EP_DEADLY];
9238
9239   // ---------- element settings: advanced (custom elements) ------------------
9240
9241   // set player change event from checkbox and selectbox
9242   custom_element_change_events[CE_NEXT_TO_PLAYER] = FALSE;
9243   custom_element_change_events[CE_TOUCHED_BY_PLAYER] = FALSE;
9244   custom_element_change_events[CE_PRESSED_BY_PLAYER] = FALSE;
9245   custom_element_change_events[CE_SWITCHED_BY_PLAYER] = FALSE;
9246   custom_element_change_events[CE_SNAPPED_BY_PLAYER] = FALSE;
9247   custom_element_change_events[CE_PUSHED_BY_PLAYER] = FALSE;
9248   custom_element_change_events[CE_ENTERED_BY_PLAYER] = FALSE;
9249   custom_element_change_events[CE_LEFT_BY_PLAYER] = FALSE;
9250   custom_element_change_events[CE_DROPPED_BY_PLAYER] = FALSE;
9251   custom_element_change_events[CE_SWITCHED] = FALSE;
9252   custom_element_change_events[CE_HITTING_SOMETHING] = FALSE;
9253   custom_element_change_events[CE_HIT_BY_SOMETHING] = FALSE;
9254   custom_element_change_events[CE_BLOCKED] = FALSE;
9255   custom_element_change_events[CE_IMPACT] = FALSE;
9256   custom_element_change_events[CE_SMASHED] = FALSE;
9257   custom_element_change_events[CE_VALUE_CHANGES] = FALSE;
9258   custom_element_change_events[CE_SCORE_CHANGES] = FALSE;
9259   custom_element_change_events[CE_VALUE_GETS_ZERO] = FALSE;
9260   custom_element_change_events[CE_SCORE_GETS_ZERO] = FALSE;
9261   custom_element_change_events[CE_CLICKED_BY_MOUSE] = FALSE;
9262   custom_element_change_events[CE_PRESSED_BY_MOUSE] = FALSE;
9263   custom_element_change_events[custom_element_change.direct_action] =
9264     custom_element_change_events[CE_BY_DIRECT_ACTION];
9265
9266   // set other element action change event from checkbox and selectbox
9267   custom_element_change_events[CE_PLAYER_NEXT_TO_X] = FALSE;
9268   custom_element_change_events[CE_PLAYER_TOUCHES_X] = FALSE;
9269   custom_element_change_events[CE_PLAYER_PRESSES_X] = FALSE;
9270   custom_element_change_events[CE_PLAYER_SWITCHES_X] = FALSE;
9271   custom_element_change_events[CE_PLAYER_SNAPS_X] = FALSE;
9272   custom_element_change_events[CE_PLAYER_PUSHES_X] = FALSE;
9273   custom_element_change_events[CE_PLAYER_ENTERS_X] = FALSE;
9274   custom_element_change_events[CE_PLAYER_LEAVES_X] = FALSE;
9275   custom_element_change_events[CE_PLAYER_DIGS_X] = FALSE;
9276   custom_element_change_events[CE_PLAYER_COLLECTS_X] = FALSE;
9277   custom_element_change_events[CE_PLAYER_DROPS_X] = FALSE;
9278   custom_element_change_events[CE_NEXT_TO_X] = FALSE;
9279   custom_element_change_events[CE_TOUCHING_X] = FALSE;
9280   custom_element_change_events[CE_HITTING_X] = FALSE;
9281   custom_element_change_events[CE_DIGGING_X] = FALSE;
9282   custom_element_change_events[CE_HIT_BY_X] = FALSE;
9283   custom_element_change_events[CE_SWITCH_OF_X] = FALSE;
9284   custom_element_change_events[CE_CHANGE_OF_X] = FALSE;
9285   custom_element_change_events[CE_EXPLOSION_OF_X] = FALSE;
9286   custom_element_change_events[CE_MOVE_OF_X] = FALSE;
9287   custom_element_change_events[CE_CREATION_OF_X] = FALSE;
9288   custom_element_change_events[CE_VALUE_CHANGES_OF_X] = FALSE;
9289   custom_element_change_events[CE_SCORE_CHANGES_OF_X] = FALSE;
9290   custom_element_change_events[CE_VALUE_GETS_ZERO_OF_X] = FALSE;
9291   custom_element_change_events[CE_SCORE_GETS_ZERO_OF_X] = FALSE;
9292   custom_element_change_events[CE_MOUSE_CLICKED_ON_X] = FALSE;
9293   custom_element_change_events[CE_MOUSE_PRESSED_ON_X] = FALSE;
9294   custom_element_change_events[custom_element_change.other_action] =
9295     custom_element_change_events[CE_BY_OTHER_ACTION];
9296
9297   for (i = 0; i < NUM_ELEMENT_PROPERTIES; i++)
9298     SET_PROPERTY(element, i, custom_element_properties[i]);
9299
9300   for (i = 0; i < NUM_CHANGE_EVENTS; i++)
9301     SET_CHANGE_EVENT(element, i, custom_element_change_events[i]);
9302
9303   // copy change events also to special level editor variable
9304   custom_element = element_info[element];
9305   custom_element_change = *element_info[element].change;
9306
9307   // needed here to restore runtime value "element_info[element].gfx_element"
9308   InitElementPropertiesGfxElement();
9309 }
9310
9311 static void CopyGroupElementPropertiesToGame(int element)
9312 {
9313   // mark that this group element has been modified
9314   custom_element.modified_settings = TRUE;
9315   level.changed = TRUE;
9316
9317   if (level.use_custom_template)
9318     AskToCopyAndModifyLevelTemplate();
9319
9320   element_info[element] = custom_element;
9321   *element_info[element].group = group_element_info;
9322
9323   // needed here to restore runtime value "element_info[element].gfx_element"
9324   InitElementPropertiesGfxElement();
9325 }
9326
9327 static void CopyEmptyElementPropertiesToGame(int element)
9328 {
9329   // mark that this empty element has been modified
9330   custom_element.modified_settings = TRUE;
9331   level.changed = TRUE;
9332
9333   if (level.use_custom_template)
9334     AskToCopyAndModifyLevelTemplate();
9335
9336   element_info[element] = custom_element;
9337
9338   // needed here to restore runtime value "element_info[element].gfx_element"
9339   InitElementPropertiesGfxElement();
9340 }
9341
9342 static void CopyClassicElementPropertiesToGame(int element)
9343 {
9344   if (IS_PLAYER_ELEMENT(element) || COULD_MOVE_INTO_ACID(element))
9345     setMoveIntoAcidProperty(&level, element,
9346                             custom_element_properties[EP_CAN_MOVE_INTO_ACID]);
9347
9348   if (MAYBE_DONT_COLLIDE_WITH(element))
9349     setDontCollideWithProperty(&level, element,
9350                               custom_element_properties[EP_DONT_COLLIDE_WITH]);
9351 }
9352
9353 static void CopyElementPropertiesToGame(int element)
9354 {
9355   if (IS_CUSTOM_ELEMENT(element))
9356     CopyCustomElementPropertiesToGame(element);
9357   else if (IS_GROUP_ELEMENT(element))
9358     CopyGroupElementPropertiesToGame(element);
9359   else if (IS_EMPTY_ELEMENT(element))
9360     CopyEmptyElementPropertiesToGame(element);
9361   else
9362     CopyClassicElementPropertiesToGame(element);
9363 }
9364
9365 #if DEBUG
9366 static void CheckElementDescriptions(void)
9367 {
9368   int i;
9369
9370   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
9371     if (getElementDescriptionFilename(i) == NULL && !IS_OBSOLETE(i))
9372       Warn("no element description file for element '%s'", EL_NAME(i));
9373 }
9374 #endif
9375
9376 static int getMaxEdFieldX(boolean has_scrollbar)
9377 {
9378   int scrollbar_width = (has_scrollbar ? ED_SCROLLBUTTON_XSIZE : 0);
9379   int sxsize = SXSIZE - scrollbar_width;
9380   int max_ed_fieldx = sxsize / ed_tilesize;
9381
9382   return max_ed_fieldx;
9383 }
9384
9385 static int getMaxEdFieldY(boolean has_scrollbar)
9386 {
9387   int infotext_height = (IN_PIX_FIELD(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY) ?
9388                          INFOTEXT_YSIZE_FULL : 0);
9389   int scrollbar_height = (has_scrollbar ? ED_SCROLLBUTTON_YSIZE : 0);
9390   int sysize = SYSIZE - scrollbar_height - infotext_height;
9391   int max_ed_fieldy = sysize / ed_tilesize;
9392
9393   return max_ed_fieldy;
9394 }
9395
9396 static void InitZoomLevelSettings(int zoom_tilesize)
9397 {
9398   static int last_game_engine_type = GAME_ENGINE_TYPE_UNKNOWN;
9399
9400   if (zoom_tilesize == -1 && level.game_engine_type != last_game_engine_type)
9401   {
9402     ed_tilesize = setup.auto_setup.editor_zoom_tilesize;
9403     ed_tilesize_default = DEFAULT_EDITOR_TILESIZE;
9404
9405     // make sure that tile size is always a power of 2
9406     ed_tilesize = (1 << log_2(ed_tilesize));
9407
9408     if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
9409     {
9410       ed_tilesize = DEFAULT_EDITOR_TILESIZE_MM;
9411       ed_tilesize_default = DEFAULT_EDITOR_TILESIZE_MM;
9412     }
9413   }
9414
9415   last_game_engine_type = level.game_engine_type;
9416
9417   // limit zoom tilesize by upper and lower bound
9418   ed_tilesize = MIN(MAX(MICRO_TILESIZE, ed_tilesize), TILESIZE);
9419
9420   // store zoom tilesize in auto setup file only if it was manually changed
9421   if (zoom_tilesize != -1)
9422     setup.auto_setup.editor_zoom_tilesize = ed_tilesize;
9423
9424   MAX_ED_FIELDX = getMaxEdFieldX(FALSE);
9425   MAX_ED_FIELDY = getMaxEdFieldY(FALSE);
9426 }
9427
9428 static void InitDrawingElements(void)
9429 {
9430   static int game_engine_type_last = GAME_ENGINE_TYPE_UNKNOWN;
9431
9432   if (level.game_engine_type == game_engine_type_last)
9433     return;
9434
9435   if (level.game_engine_type == GAME_ENGINE_TYPE_BD)
9436   {
9437     new_element1 = EL_BD_WALL;
9438     new_element2 = EL_EMPTY;
9439     new_element3 = EL_BD_SAND;
9440   }
9441   else if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
9442   {
9443     new_element1 = EL_SP_CHIP_SINGLE;
9444     new_element2 = EL_EMPTY;
9445     new_element3 = EL_SP_BASE;
9446   }
9447   else if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
9448   {
9449     new_element1 = EL_MM_MIRROR_START;
9450     new_element2 = EL_EMPTY;
9451     new_element3 = EL_MM_WOODEN_WALL;
9452   }
9453   else
9454   {
9455     new_element1 = EL_WALL;
9456     new_element2 = EL_EMPTY;
9457     new_element3 = EL_SAND;
9458   }
9459
9460   game_engine_type_last = level.game_engine_type;
9461 }
9462
9463 static void InitLevelSetInfo(void)
9464 {
9465   snprintf(levelset_name,   MAX_LEVEL_NAME_LEN + 1,
9466            "%s", leveldir_current->name);
9467   snprintf(levelset_author, MAX_LEVEL_AUTHOR_LEN + 1,
9468            "%s", leveldir_current->author);
9469
9470   levelset_num_levels = leveldir_current->levels;
9471
9472   levelset_use_levelset_artwork = FALSE;
9473   levelset_copy_level_template = FALSE;
9474
9475   levelset_save_mode = LEVELSET_SAVE_MODE_UPDATE;
9476 }
9477
9478 static void ChangeEditorToLevelSet(char *levelset_subdir)
9479 {
9480   leveldir_current = getTreeInfoFromIdentifier(leveldir_first, levelset_subdir);
9481
9482   // the previous level set might have used custom artwork
9483   ReloadCustomArtwork(0);
9484
9485   LoadLevelSetup_SeriesInfo();
9486
9487   SaveLevelSetup_LastSeries();
9488   SaveLevelSetup_SeriesInfo();
9489
9490   TapeErase();
9491
9492   LoadLevel(level_nr);
9493   LoadScore(level_nr);
9494
9495   DrawLevelEd();
9496 }
9497
9498 static boolean useEditorDoorAnimation(void)
9499 {
9500   struct RectWithBorder *vp_door_1 = &viewport.door_1[GAME_MODE_MAIN];
9501   boolean door_1_viewport_unchanged =
9502     (vp_door_1->x      == DX     &&
9503      vp_door_1->y      == DY     &&
9504      vp_door_1->width  == DXSIZE &&
9505      vp_door_1->height == DYSIZE);
9506   boolean door_1_contains_toolbox =
9507     (EX >= DX &&
9508      EY >= DY &&
9509      EX + EXSIZE <= DX + DXSIZE &&
9510      EY + EYSIZE <= DY + DYSIZE);
9511
9512   return (door_1_viewport_unchanged && door_1_contains_toolbox);
9513 }
9514
9515 static void DrawEditorDoorBackground(int graphic, int x, int y,
9516                                      int width, int height)
9517 {
9518   struct GraphicInfo *g = &graphic_info[graphic];
9519
9520   if (g->bitmap != NULL)
9521     BlitBitmap(g->bitmap, drawto, g->src_x, g->src_y,
9522                MIN(width, g->width), MIN(height, g->height), x, y);
9523   else
9524     ClearRectangle(drawto, x, y, width, height);
9525 }
9526
9527 static void DrawEditorDoorContent(void)
9528 {
9529   // needed for gadgets drawn on background (like palette scrollbar)
9530   SetDoorBackgroundImage(IMG_UNDEFINED);
9531
9532   // copy default editor door content to main double buffer
9533   DrawEditorDoorBackground(IMG_BACKGROUND_PALETTE, DX, DY, DXSIZE, DYSIZE);
9534
9535   // draw bigger door
9536   DrawSpecialEditorDoor();
9537
9538   // draw new control window
9539   DrawEditorDoorBackground(IMG_BACKGROUND_TOOLBOX, EX, EY, EXSIZE, EYSIZE);
9540
9541   // draw all toolbox gadgets to editor doors
9542   MapControlButtons();
9543
9544   // when returning from test game to properties page, redraw toolbox gadgets
9545   if (edit_mode == ED_MODE_PROPERTIES)
9546   {
9547     UnmapLevelEditorToolboxDrawingGadgets();
9548     UnmapLevelEditorToolboxCustomGadgets();
9549
9550     MapLevelEditorToolboxCustomGadgetsIfNeeded();
9551   }
9552
9553   // draw all palette gadgets to editor doors
9554   ModifyEditorElementList();
9555   RedrawDrawingElements();
9556
9557   // copy actual editor door content to door double buffer for OpenDoor()
9558   BlitBitmap(drawto, bitmap_db_door_1, DX, DY, DXSIZE, DYSIZE, 0, 0);
9559 }
9560
9561 void DrawLevelEd(void)
9562 {
9563   int fade_mask = REDRAW_FIELD;
9564
9565   FadeSoundsAndMusic();
9566
9567   if (CheckFadeAll())
9568     fade_mask = REDRAW_ALL;
9569
9570   FadeOut(fade_mask);
9571
9572   // needed if different viewport properties defined for editor
9573   ChangeViewportPropertiesIfNeeded();
9574
9575   ClearField();
9576
9577   InitZoomLevelSettings(-1);
9578   InitDrawingElements();
9579   InitLevelSetInfo();
9580
9581 #if DEBUG
9582   CheckElementDescriptions();
9583 #endif
9584
9585   if (level_editor_test_game)
9586   {
9587     CopyPlayfield(level.field, Tile);
9588     CopyPlayfield(TileBackup, level.field);
9589
9590     level_editor_test_game = FALSE;
9591   }
9592   else
9593   {
9594     edit_mode = ED_MODE_DRAWING;
9595     edit_mode_levelconfig = ED_MODE_LEVELCONFIG_LEVEL;
9596     edit_mode_properties = ED_MODE_PROPERTIES_INFO;
9597
9598     ResetUndoBuffer();
9599
9600     level_xpos = -1;
9601     level_ypos = -1;
9602   }
9603
9604   // redraw_mask |= REDRAW_ALL;
9605
9606   FreeLevelEditorGadgets();
9607   CreateLevelEditorGadgets();
9608
9609   ReinitializeElementList();            // update dynamic level element list
9610   ReinitializeElementListButtons();     // custom element may look different
9611
9612   InitElementPropertiesGfxElement();
9613
9614   UnmapAllGadgets();
9615
9616   DrawEditModeWindow_PlayfieldOnly();
9617
9618   DrawMaskedBorder(fade_mask);
9619
9620   // use door animation if door 1 viewport is unchanged and contains toolbox
9621   if (useEditorDoorAnimation())
9622   {
9623     FadeIn(fade_mask);
9624
9625     DrawEditorDoorContent();
9626
9627     OpenDoor(DOOR_OPEN_1 | DOOR_FORCE_ANIM);
9628   }
9629   else
9630   {
9631     DrawEditorDoorContent();
9632
9633     FadeIn(fade_mask);
9634   }
9635
9636   SetDoorState(DOOR_OPEN_1 | DOOR_OPEN_2);
9637 }
9638
9639 static void AdjustDrawingAreaGadgets(void)
9640 {
9641   int ed_xsize = lev_fieldx + 2;
9642   int ed_ysize = lev_fieldy + 2;
9643   int max_ed_fieldx = MAX_ED_FIELDX;
9644   int max_ed_fieldy = MAX_ED_FIELDY;
9645   boolean horizontal_scrollbar_needed;
9646   boolean vertical_scrollbar_needed;
9647   int x, y, width, height;
9648
9649   if (suppressBorderElement())
9650   {
9651     ed_xsize = lev_fieldx;
9652     ed_ysize = lev_fieldy;
9653   }
9654
9655   // check if we need any scrollbars
9656   horizontal_scrollbar_needed = (ed_xsize > max_ed_fieldx);
9657   vertical_scrollbar_needed   = (ed_ysize > max_ed_fieldy);
9658
9659   // check if we have a smaller editor field because of scrollbars
9660   max_ed_fieldx = getMaxEdFieldX(vertical_scrollbar_needed);
9661   max_ed_fieldy = getMaxEdFieldY(horizontal_scrollbar_needed);
9662
9663   // check again if we now need more scrollbars because of less space
9664   horizontal_scrollbar_needed = (ed_xsize > max_ed_fieldx);
9665   vertical_scrollbar_needed   = (ed_ysize > max_ed_fieldy);
9666
9667   // check if editor field gets even smaller after adding new scrollbars
9668   max_ed_fieldx = getMaxEdFieldX(vertical_scrollbar_needed);
9669   max_ed_fieldy = getMaxEdFieldY(horizontal_scrollbar_needed);
9670
9671   ed_fieldx = (ed_xsize > max_ed_fieldx ? max_ed_fieldx : ed_xsize);
9672   ed_fieldy = (ed_ysize > max_ed_fieldy ? max_ed_fieldy : ed_ysize);
9673
9674   x = SX + ed_fieldx * ed_tilesize;
9675   y = SY + ed_fieldy * ed_tilesize;
9676
9677   width  = ed_fieldx * ed_tilesize - 2 * ED_SCROLLBUTTON_XSIZE;
9678   height = ed_fieldy * ed_tilesize - 2 * ED_SCROLLBUTTON_YSIZE;
9679
9680   // adjust drawing area gadget
9681   ModifyGadget(level_editor_gadget[GADGET_ID_DRAWING_LEVEL],
9682                GDI_AREA_SIZE, ed_fieldx, ed_fieldy,
9683                GDI_ITEM_SIZE, ed_tilesize, ed_tilesize,
9684                GDI_END);
9685
9686   // adjust horizontal scrollbar gadgets
9687   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LEFT],
9688                GDI_Y, y,
9689                GDI_END);
9690   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_RIGHT],
9691                GDI_X, x - ED_SCROLLBUTTON_XSIZE,
9692                GDI_Y, y,
9693                GDI_END);
9694   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
9695                GDI_Y, y,
9696                GDI_WIDTH, width,
9697                GDI_SCROLLBAR_ITEMS_VISIBLE, ed_fieldx,
9698                GDI_END);
9699
9700   // adjust vertical scrollbar gadgets
9701   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_UP],
9702                GDI_X, x,
9703                GDI_END);
9704   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_DOWN],
9705                GDI_X, x,
9706                GDI_Y, y - ED_SCROLLBUTTON_YSIZE,
9707                GDI_END);
9708   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
9709                GDI_X, x,
9710                GDI_HEIGHT, height,
9711                GDI_SCROLLBAR_ITEMS_VISIBLE, ed_fieldy,
9712                GDI_END);
9713 }
9714
9715 static void AdjustLevelScrollPosition(void)
9716 {
9717   if (level_xpos < -1)
9718     level_xpos = -1;
9719   if (level_xpos > lev_fieldx - ed_fieldx + 1)
9720     level_xpos = lev_fieldx - ed_fieldx + 1;
9721   if (lev_fieldx < ed_fieldx - 2)
9722     level_xpos = -1;
9723
9724   if (level_ypos < -1)
9725     level_ypos = -1;
9726   if (level_ypos > lev_fieldy - ed_fieldy + 1)
9727     level_ypos = lev_fieldy - ed_fieldy + 1;
9728   if (lev_fieldy < ed_fieldy - 2)
9729     level_ypos = -1;
9730
9731   if (suppressBorderElement())
9732   {
9733     level_xpos = 0;
9734     level_ypos = 0;
9735   }
9736 }
9737
9738 static void AdjustEditorScrollbar(int id)
9739 {
9740   struct GadgetInfo *gi = level_editor_gadget[id];
9741   int items_max, items_visible, item_position;
9742
9743   if (id == GADGET_ID_SCROLL_HORIZONTAL)
9744   {
9745     items_max = MAX(lev_fieldx + 2, ed_fieldx);
9746     items_visible = ed_fieldx;
9747     item_position = level_xpos + 1;
9748   }
9749   else
9750   {
9751     items_max = MAX(lev_fieldy + 2, ed_fieldy);
9752     items_visible = ed_fieldy;
9753     item_position = level_ypos + 1;
9754   }
9755
9756   if (item_position > items_max - items_visible)
9757     item_position = items_max - items_visible;
9758
9759   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
9760                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
9761 }
9762
9763 static void AdjustElementListScrollbar(void)
9764 {
9765   struct GadgetInfo *gi = level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL];
9766   int items_max, items_visible, item_position;
9767
9768   // correct position of element list scrollbar
9769   if (element_shift < 0)
9770     element_shift = 0;
9771   if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
9772     element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
9773
9774   items_max = num_editor_elements / ED_ELEMENTLIST_BUTTONS_HORIZ;
9775   items_visible = ED_ELEMENTLIST_BUTTONS_VERT;
9776   item_position = element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ;
9777
9778   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
9779                GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
9780                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
9781 }
9782
9783 static void ModifyEditorCounterValue(int counter_id, int new_value)
9784 {
9785   int *counter_value = counterbutton_info[counter_id].value;
9786   int gadget_id = counterbutton_info[counter_id].gadget_id_text;
9787   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9788
9789   ModifyGadget(gi, GDI_NUMBER_VALUE, new_value, GDI_END);
9790
9791   if (counter_value != NULL)
9792     *counter_value = gi->textinput.number_value;
9793 }
9794
9795 static void ModifyEditorCounterLimits(int counter_id, int min, int max)
9796 {
9797   int gadget_id = counterbutton_info[counter_id].gadget_id_text;
9798   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9799
9800   ModifyGadget(gi, GDI_NUMBER_MIN, min, GDI_NUMBER_MAX, max, GDI_END);
9801
9802   if (counter_id >= ED_COUNTER_ID_ELEMENT_VALUE1 &&
9803       counter_id <= ED_COUNTER_ID_ELEMENT_VALUE4)
9804   {
9805     int gadget_id_up = counterbutton_info[counter_id].gadget_id_up;
9806     struct GadgetInfo *gi_up = level_editor_gadget[gadget_id_up];
9807
9808     ModifyGadget(gi, GDI_TEXT_SIZE, (max < 10 ? 1 : 3), GDI_END);
9809     ModifyGadget(gi_up, GDI_X, gi->x + gi->width + ED_GADGET_SMALL_DISTANCE,
9810                  GDI_END);
9811   }
9812 }
9813
9814 static void ModifyEditorSelectboxValue(int selectbox_id, int new_value)
9815 {
9816   int gadget_id = selectbox_info[selectbox_id].gadget_id;
9817   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9818   int new_index_value = setSelectboxValue(selectbox_id, new_value);
9819
9820   ModifyGadget(gi, GDI_SELECTBOX_INDEX, new_index_value, GDI_END);
9821 }
9822
9823 static void ModifyEditorSelectboxOptions(int selectbox_id,
9824                                          struct ValueTextInfo *options)
9825 {
9826   int gadget_id = selectbox_info[selectbox_id].gadget_id;
9827   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9828
9829   selectbox_info[selectbox_id].options = options;
9830
9831   // set index to zero -- list may be shorter now (correct later, if needed)
9832   ModifyGadget(gi, GDI_SELECTBOX_INDEX, 0,
9833                GDI_SELECTBOX_OPTIONS, options, GDI_END);
9834 }
9835
9836 static void ModifyEditorDrawingArea(int drawingarea_id, int xsize, int ysize)
9837 {
9838   int gadget_id = drawingarea_info[drawingarea_id].gadget_id;
9839   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9840
9841   drawingarea_info[drawingarea_id].area_xsize = xsize;
9842   drawingarea_info[drawingarea_id].area_ysize = ysize;
9843
9844   ModifyGadget(gi, GDI_AREA_SIZE, xsize, ysize, GDI_END);
9845 }
9846
9847 static void ModifyEditorElementList(void)
9848 {
9849   int i;
9850
9851   if (!use_permanent_palette && edit_mode != ED_MODE_PALETTE)
9852     return;
9853
9854   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
9855   {
9856     int gadget_id = GADGET_ID_ELEMENTLIST_FIRST + i;
9857     struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9858     struct GadgetDesign *gd = &gi->deco.design;
9859     int element = editor_elements[element_shift + i];
9860     int tile_size = BUTTON_TILE_SIZE(editor.palette.tile_size);
9861
9862     UnmapGadget(gi);
9863
9864     getEditorGraphicSource(element, tile_size, &gd->bitmap, &gd->x, &gd->y);
9865
9866     ModifyGadget(gi, GDI_INFO_TEXT, getElementInfoText(element), GDI_END);
9867
9868     MapGadget(gi);
9869   }
9870 }
9871
9872 static void DrawDrawingElementGraphic(int element, struct XYTileSize *pos)
9873 {
9874   int graphic = el2edimg(element);
9875   int tile_size = BUTTON_TILE_SIZE(pos->tile_size);
9876
9877   if (pos->x == -1 &&
9878       pos->y == -1)
9879     return;
9880
9881   DrawSizedGraphicExt(drawto, DX + pos->x, DY + pos->y, graphic, 0, tile_size);
9882 }
9883
9884 static void ModifyDrawingElementButton(int element, int id)
9885 {
9886   struct GadgetInfo *gi = level_editor_gadget[id];
9887   Bitmap *deco_bitmap;
9888   int deco_x, deco_y;
9889   int tile_size = gi->deco.width;
9890
9891   getEditorGraphicSource(element, tile_size, &deco_bitmap, &deco_x, &deco_y);
9892
9893   ModifyGadget(gi, GDI_DECORATION_DESIGN, deco_bitmap, deco_x, deco_y, GDI_END);
9894 }
9895
9896 static void PickDrawingElement(int button, int element)
9897 {
9898   struct
9899   {
9900     int *new_element;
9901     struct XYTileSize *pos;
9902     int id;
9903   } de, drawing_elements[] =
9904   {
9905     { &new_element1, &editor.palette.element_left,   GADGET_ID_ELEMENT_LEFT   },
9906     { &new_element2, &editor.palette.element_middle, GADGET_ID_ELEMENT_MIDDLE },
9907     { &new_element3, &editor.palette.element_right,  GADGET_ID_ELEMENT_RIGHT  },
9908   };
9909
9910   if (button < 1 || button > 3)
9911     return;
9912
9913   if (IS_MM_WALL(element))
9914     element = map_mm_wall_element(element);
9915
9916   de = drawing_elements[button - 1];
9917
9918   *de.new_element = element;    // update global drawing element variable
9919
9920   DrawDrawingElementGraphic(element, de.pos);
9921   ModifyDrawingElementButton(element, de.id);
9922
9923   redraw_mask |= REDRAW_DOOR_1;
9924 }
9925
9926 static void RedrawDrawingElements(void)
9927 {
9928   PickDrawingElement(1, new_element1);
9929   PickDrawingElement(2, new_element2);
9930   PickDrawingElement(3, new_element3);
9931 }
9932
9933 static void DrawDrawingWindowExt(boolean remap_toolbox_gadgets)
9934 {
9935   stick_element_properties_window = FALSE;
9936
9937   SetMainBackgroundImage(IMG_UNDEFINED);
9938   ClearField();
9939
9940   UnmapLevelEditorFieldGadgets();
9941
9942   AdjustDrawingAreaGadgets();
9943   AdjustLevelScrollPosition();
9944   AdjustEditorScrollbar(GADGET_ID_SCROLL_HORIZONTAL);
9945   AdjustEditorScrollbar(GADGET_ID_SCROLL_VERTICAL);
9946
9947   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
9948
9949   MapMainDrawingArea();
9950
9951   if (remap_toolbox_gadgets)
9952   {
9953     UnmapLevelEditorToolboxCustomGadgets();
9954     MapLevelEditorToolboxDrawingGadgets();
9955   }
9956 }
9957
9958 static void DrawDrawingWindow(void)
9959 {
9960   DrawDrawingWindowExt(TRUE);
9961 }
9962
9963 static int getTabulatorBarWidth(void)
9964 {
9965   struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_PROPERTIES_INFO];
9966   struct GadgetInfo *gd_gi4 = level_editor_gadget[GADGET_ID_PROPERTIES_CHANGE];
9967
9968   return gd_gi4->x - gd_gi1->x + gd_gi4->width;
9969 }
9970
9971 static int getTabulatorBarHeight(void)
9972 {
9973   return ED_TAB_BAR_HEIGHT;
9974 }
9975
9976 static Pixel getTabulatorBarColor(void)
9977 {
9978   struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_LEVELCONFIG_LEVEL];
9979   struct GadgetDesign *gd = &gd_gi1->alt_design[GD_BUTTON_UNPRESSED];
9980   int gd_x = gd->x + gd_gi1->border.width / 2;
9981   int gd_y = gd->y + gd_gi1->height - 1;
9982
9983   return GetPixel(gd->bitmap, gd_x, gd_y);
9984 }
9985
9986 static void DrawLevelConfigTabulatorGadgets(void)
9987 {
9988   struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_LEVELCONFIG_LEVEL];
9989   Pixel tab_color = getTabulatorBarColor();
9990   int id_first = ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL;
9991   int id_last  = ED_TEXTBUTTON_ID_LEVELCONFIG_EDITOR;
9992   int i;
9993
9994   // draw additional "engine" tabulator when using native BD engine
9995   if (level.game_engine_type == GAME_ENGINE_TYPE_BD)
9996     id_last = ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE;
9997
9998   for (i = id_first; i <= id_last; i++)
9999   {
10000     int gadget_id = textbutton_info[i].gadget_id;
10001     struct GadgetInfo *gi = level_editor_gadget[gadget_id];
10002     boolean active = (i != edit_mode_levelconfig);
10003
10004     // draw background line below tabulator button
10005     ClearRectangleOnBackground(drawto, gi->x, gi->y + gi->height, gi->width, 1);
10006
10007     // draw solid line below inactive tabulator buttons
10008     if (!active && tab_color != BLACK_PIXEL)    // black => transparent
10009       FillRectangle(drawto, gi->x, gi->y + gi->height, gi->width,
10010                     ED_GADGET_TINY_DISTANCE, tab_color);
10011
10012     ModifyGadget(gi, GDI_ACTIVE, active, GDI_END);
10013     MapTextbuttonGadget(i);
10014   }
10015
10016   // draw little border line below tabulator buttons
10017   if (tab_color != BLACK_PIXEL)                 // black => transparent
10018     FillRectangle(drawto, gd_gi1->x, gd_gi1->y + gd_gi1->height +
10019                   ED_GADGET_TINY_DISTANCE,
10020                   getTabulatorBarWidth(), getTabulatorBarHeight(), tab_color);
10021 }
10022
10023 static void DrawPropertiesTabulatorGadgets(void)
10024 {
10025   struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_PROPERTIES_INFO];
10026   struct GadgetDesign *gd = &gd_gi1->alt_design[GD_BUTTON_UNPRESSED];
10027   int gd_x = gd->x + gd_gi1->border.width / 2;
10028   int gd_y = gd->y + gd_gi1->height - 1;
10029   Pixel tab_color = GetPixel(gd->bitmap, gd_x, gd_y);
10030   int id_first = ED_TEXTBUTTON_ID_PROPERTIES_INFO;
10031   int id_last  = ED_TEXTBUTTON_ID_PROPERTIES_CONFIG;
10032   int i;
10033
10034   // draw two config tabulators for player elements
10035   if (IS_PLAYER_ELEMENT(properties_element))
10036     id_last = ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2;
10037
10038   // draw two config and one "change" tabulator for custom elements
10039   if (IS_CUSTOM_ELEMENT(properties_element))
10040     id_last = ED_TEXTBUTTON_ID_PROPERTIES_CHANGE;
10041
10042   for (i = id_first; i <= id_last; i++)
10043   {
10044     int gadget_id = textbutton_info[i].gadget_id;
10045     struct GadgetInfo *gi = level_editor_gadget[gadget_id];
10046     boolean active = (i != edit_mode_properties);
10047
10048     // use "config 1" and "config 2" instead of "config" for players and CEs
10049     if (i == ED_TEXTBUTTON_ID_PROPERTIES_CONFIG &&
10050         (IS_PLAYER_ELEMENT(properties_element) ||
10051          IS_CUSTOM_ELEMENT(properties_element)))
10052       continue;
10053
10054     // draw background line below tabulator button
10055     ClearRectangleOnBackground(drawto, gi->x, gi->y + gi->height, gi->width, 1);
10056
10057     // draw solid line below inactive tabulator buttons
10058     if (!active && tab_color != BLACK_PIXEL)    // black => transparent
10059       FillRectangle(drawto, gi->x, gi->y + gi->height, gi->width,
10060                     ED_GADGET_TINY_DISTANCE, tab_color);
10061
10062     ModifyGadget(gi, GDI_ACTIVE, active, GDI_END);
10063     MapTextbuttonGadget(i);
10064   }
10065
10066   // draw little border line below tabulator buttons
10067   if (tab_color != BLACK_PIXEL)                 // black => transparent
10068     FillRectangle(drawto, gd_gi1->x, gd_gi1->y + gd_gi1->height +
10069                   ED_GADGET_TINY_DISTANCE,
10070                   getTabulatorBarWidth(), getTabulatorBarHeight(), tab_color);
10071 }
10072
10073 static void PrintInfoText(char *text, int font_nr, int xpos, int ypos)
10074 {
10075   DrawText(SX + xpos, SY + ypos, text, font_nr);
10076 }
10077
10078 static int PrintElementDescriptionFromFile(char *filename, int font_nr,
10079                                            int xpos, int ypos)
10080 {
10081   int font_width = getFontWidth(font_nr);
10082   int font_height = getFontHeight(font_nr);
10083   int max_chars_per_line = (SXSIZE - 2 * xpos) / font_width;
10084   int max_lines_drawable = (SYSIZE - ypos) / font_height - 1;
10085
10086   return DrawTextFile(SX + xpos, SY + ypos, filename, font_nr,
10087                       max_chars_per_line, -1, max_lines_drawable, 0, -1,
10088                       TRUE, FALSE, FALSE);
10089 }
10090
10091 static void DrawLevelConfigLevel(void)
10092 {
10093   int i;
10094
10095   // draw counter gadgets
10096   for (i = ED_COUNTER_ID_LEVEL_FIRST; i <= ED_COUNTER_ID_LEVEL_LAST; i++)
10097     MapCounterButtons(i);
10098
10099   // draw checkbutton gadgets
10100   for (i = ED_CHECKBUTTON_ID_LEVEL_FIRST; i<= ED_CHECKBUTTON_ID_LEVEL_LAST; i++)
10101     MapCheckbuttonGadget(i);
10102
10103   // draw selectbox gadgets
10104   for (i = ED_SELECTBOX_ID_LEVEL_FIRST; i <= ED_SELECTBOX_ID_LEVEL_LAST; i++)
10105     MapSelectboxGadget(i);
10106
10107   // draw text input gadgets
10108   for (i = ED_TEXTINPUT_ID_LEVEL_FIRST; i <= ED_TEXTINPUT_ID_LEVEL_LAST; i++)
10109     MapTextInputGadget(i);
10110 }
10111
10112 static char *getLevelSubdirFromSaveMode(int save_mode)
10113 {
10114   if (save_mode == LEVELSET_SAVE_MODE_CREATE)
10115     return getNewUserLevelSubdir();
10116
10117   return leveldir_current->subdir;
10118 }
10119
10120 static void DrawLevelConfigLevelSet_DirectoryInfo(void)
10121 {
10122   char *directory_text = "Level set directory:";
10123   char *directory_name = getLevelSubdirFromSaveMode(levelset_save_mode);
10124   int font1_nr = FONT_TEXT_1;
10125   int font2_nr = FONT_TEXT_2;
10126   int font1_height = getFontHeight(font1_nr);
10127   int yoffset_above = font1_height + ED_GADGET_LINE_DISTANCE;
10128   int x = ED_LEVEL_SETTINGS_X(0);
10129   int y = ED_LEVEL_SETTINGS_Y(6);
10130
10131   PrintInfoText(directory_text, font1_nr, x, y - yoffset_above);
10132   PrintInfoText(directory_name, font2_nr, x, y);
10133 }
10134
10135 static void DrawLevelConfigLevelSet(void)
10136 {
10137   boolean artwork_exists = checkIfCustomArtworkExistsForCurrentLevelSet();
10138   boolean template_exists = fileExists(getLocalLevelTemplateFilename());
10139   int i;
10140
10141   // draw counter gadgets
10142   for (i = ED_COUNTER_ID_LEVELSET_FIRST; i <= ED_COUNTER_ID_LEVELSET_LAST; i++)
10143     MapCounterButtons(i);
10144
10145   // draw checkbutton gadgets
10146   for (i = ED_CHECKBUTTON_ID_LEVELSET_FIRST; i <= ED_CHECKBUTTON_ID_LEVELSET_LAST; i++)
10147   {
10148     if (levelset_save_mode == LEVELSET_SAVE_MODE_UPDATE ||
10149         (i == ED_CHECKBUTTON_ID_USE_LEVELSET_ARTWORK && !artwork_exists) ||
10150         (i == ED_CHECKBUTTON_ID_COPY_LEVEL_TEMPLATE  && !template_exists))
10151       continue;
10152
10153     MapCheckbuttonGadget(i);
10154   }
10155
10156   // draw selectbox gadgets
10157   for (i = ED_SELECTBOX_ID_LEVELSET_FIRST; i <= ED_SELECTBOX_ID_LEVELSET_LAST; i++)
10158     MapSelectboxGadget(i);
10159
10160   // draw text input gadgets
10161   for (i = ED_TEXTINPUT_ID_LEVELSET_FIRST; i <= ED_TEXTINPUT_ID_LEVELSET_LAST; i++)
10162     MapTextInputGadget(i);
10163
10164   // draw textbutton gadgets
10165   MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_LEVELSET);
10166
10167   // draw info text
10168   DrawLevelConfigLevelSet_DirectoryInfo();
10169 }
10170
10171 static void DrawLevelConfigEditor(void)
10172 {
10173   int i;
10174
10175   // draw counter gadgets
10176   for (i = ED_COUNTER_ID_EDITOR_FIRST; i <= ED_COUNTER_ID_EDITOR_LAST; i++)
10177     MapCounterButtons(i);
10178
10179   // draw checkbutton gadgets
10180   for (i = ED_CHECKBUTTON_ID_EDITOR_FIRST; i <= ED_CHECKBUTTON_ID_EDITOR_LAST; i++)
10181     MapCheckbuttonGadget(i);
10182
10183   // draw radiobutton gadgets
10184   for (i = ED_RADIOBUTTON_ID_EDITOR_FIRST; i <= ED_RADIOBUTTON_ID_EDITOR_LAST; i++)
10185     MapRadiobuttonGadget(i);
10186
10187   // draw drawing area
10188   MapDrawingArea(ED_DRAWING_ID_RANDOM_BACKGROUND);
10189
10190   // draw textbutton gadgets
10191   MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2);
10192 }
10193
10194 static void DrawLevelConfigEngine(void)
10195 {
10196   int i;
10197
10198   // draw counter gadgets
10199   if (level.bd_scheduling_type == GD_SCHEDULING_MILLISECONDS)
10200   {
10201     MapCounterButtons(ED_COUNTER_ID_BD_CYCLE_DELAY_MS);
10202     MapCounterButtons(ED_COUNTER_ID_BD_HATCHING_DELAY_CYCLES);
10203   }
10204   else
10205   {
10206     MapCounterButtons(ED_COUNTER_ID_BD_CYCLE_DELAY_C64);
10207     MapCounterButtons(ED_COUNTER_ID_BD_HATCHING_DELAY_SECONDS);
10208   }
10209
10210   // draw checkbutton gadgets
10211   for (i = ED_CHECKBUTTON_ID_ENGINE_FIRST; i <= ED_CHECKBUTTON_ID_ENGINE_LAST; i++)
10212     MapCheckbuttonGadget(i);
10213
10214   // draw selectbox gadgets
10215   for (i = ED_SELECTBOX_ID_ENGINE_FIRST; i <= ED_SELECTBOX_ID_ENGINE_LAST; i++)
10216     MapSelectboxGadget(i);
10217 }
10218
10219 static void DrawLevelConfigWindow(void)
10220 {
10221   char *text = "Global Settings";
10222   int font_nr = FONT_TITLE_1;
10223   struct MenuPosInfo *pos = &editor.settings.headline;
10224   int sx = SX + ALIGNED_XPOS(pos->x, getTextWidth(text, font_nr), pos->align);
10225   int sy = SY + pos->y;
10226
10227   stick_element_properties_window = FALSE;
10228
10229   SetAutomaticNumberOfGemsNeeded();
10230
10231   UnmapLevelEditorFieldGadgets();
10232
10233   SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
10234   ClearField();
10235
10236   DrawText(sx, sy, text, font_nr);
10237
10238   DrawLevelConfigTabulatorGadgets();
10239
10240   if (edit_mode_levelconfig == ED_MODE_LEVELCONFIG_LEVEL)
10241     DrawLevelConfigLevel();
10242   else if (edit_mode_levelconfig == ED_MODE_LEVELCONFIG_LEVELSET)
10243     DrawLevelConfigLevelSet();
10244   else if (edit_mode_levelconfig == ED_MODE_LEVELCONFIG_EDITOR)
10245     DrawLevelConfigEditor();
10246   else if (edit_mode_levelconfig == ED_MODE_LEVELCONFIG_ENGINE)
10247     DrawLevelConfigEngine();
10248 }
10249
10250 static void DrawCustomContentArea(void)
10251 {
10252   int id = ED_DRAWING_ID_CUSTOM_CONTENT;
10253   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
10254   int x1 = right_gadget_border[GADGET_ID_CUSTOM_DEADLINESS];
10255   int x2 = right_gadget_border[GADGET_ID_CUSTOM_EXPLOSION_TYPE];
10256   int x3 = right_gadget_border[GADGET_ID_CUSTOM_EXPLODE_IMPACT];
10257   int xoffset = ED_GADGET_SPACE_DISTANCE;
10258
10259   // add distance for potential left text (without drawing area border)
10260   x2 += getTextWidthForGadget(drawingarea_info[id].text_left);
10261
10262   ModifyGadget(gi, GDI_X, MAX(x1, MAX(x2, x3)) + xoffset, GDI_END);
10263
10264   MapDrawingArea(ED_DRAWING_ID_CUSTOM_CONTENT);
10265 }
10266
10267 static void DrawCustomChangeContentArea(void)
10268 {
10269   int id = ED_DRAWING_ID_CUSTOM_CHANGE_CONTENT;
10270   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
10271   int x1 = right_gadget_border[GADGET_ID_CHANGE_USE_CONTENT];
10272   int x2 = right_gadget_border[GADGET_ID_CHANGE_REPLACE_WHEN];
10273   int x3 = right_gadget_border[GADGET_ID_CHANGE_ONLY_COMPLETE];
10274   int xoffset = ED_GADGET_SPACE_DISTANCE;
10275
10276   ModifyGadget(gi, GDI_X, MAX(x1, MAX(x2, x3)) + xoffset, GDI_END);
10277
10278   MapDrawingArea(id);
10279 }
10280
10281 static void RemoveElementContentArea(int id, int font_height)
10282 {
10283   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10284
10285   DrawBackground(SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size,
10286                  SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size,
10287                  3 * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10288                  3 * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size +
10289                  ED_GADGET_TEXT_DISTANCE + font_height);
10290 }
10291
10292 static void DrawYamYamContentAreas(void)
10293 {
10294   int font_nr = FONT_TEXT_1;
10295   int font_height = getFontHeight(font_nr);
10296   int tilesize = ED_DRAWINGAREA_TILE_SIZE;
10297   int yoffset = (tilesize - font_height) / 2;
10298   int x = SX + ED_AREA_YAMYAM_CONTENT_X(3) + 4 * tilesize;
10299   int y = SY + ED_AREA_YAMYAM_CONTENT_Y(3) + yoffset;
10300   int i;
10301
10302   // display counter to choose number of element content areas
10303   MapCounterButtons(ED_COUNTER_ID_YAMYAM_CONTENT);
10304
10305   for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
10306   {
10307     int id = ED_DRAWING_ID_YAMYAM_CONTENT_0 + i;
10308
10309     if (i < level.num_yamyam_contents)
10310     {
10311       MapDrawingArea(id);
10312     }
10313     else
10314     {
10315       UnmapDrawingArea(id);
10316
10317       // delete content areas in case of reducing number of them
10318       RemoveElementContentArea(id, font_height);
10319     }
10320   }
10321
10322   DrawText(x, y + 0 * tilesize, "content", font_nr);
10323   DrawText(x, y + 1 * tilesize, "when",    font_nr);
10324   DrawText(x, y + 2 * tilesize, "smashed", font_nr);
10325 }
10326
10327 static void DrawMagicBallContentAreas(void)
10328 {
10329   int font_nr = FONT_TEXT_1;
10330   int font_height = getFontHeight(font_nr);
10331   int tilesize = ED_DRAWINGAREA_TILE_SIZE;
10332   int yoffset = (tilesize - font_height) / 2;
10333   int x = SX + ED_AREA_MAGIC_BALL_CONTENT_X(3) + 4 * tilesize;
10334   int y = SY + ED_AREA_MAGIC_BALL_CONTENT_Y(3) + yoffset;
10335   int i;
10336
10337   // display counter to choose number of element content areas
10338   MapCounterButtons(ED_COUNTER_ID_BALL_CONTENT);
10339
10340   for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
10341   {
10342     int id = ED_DRAWING_ID_MAGIC_BALL_CONTENT_0 + i;
10343
10344     if (i < level.num_ball_contents)
10345     {
10346       MapDrawingArea(id);
10347     }
10348     else
10349     {
10350       UnmapDrawingArea(id);
10351
10352       // delete content areas in case of reducing number of them
10353       RemoveElementContentArea(id, font_height);
10354     }
10355   }
10356
10357   DrawText(x, y + 0 * tilesize, "generated", font_nr);
10358   DrawText(x, y + 1 * tilesize, "when",      font_nr);
10359   DrawText(x, y + 2 * tilesize, "active",    font_nr);
10360 }
10361
10362 static void DrawAndroidElementArea(void)
10363 {
10364   int id = ED_DRAWING_ID_ANDROID_CONTENT;
10365   int num_elements = level.num_android_clone_elements;
10366   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10367   int sx = SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size;
10368   int sy = SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size;
10369   int xsize = MAX_ANDROID_ELEMENTS;
10370   int ysize = 1;
10371
10372   // display counter to choose number of element areas
10373   MapCounterButtons(ED_COUNTER_ID_ANDROID_CONTENT);
10374
10375   if (drawingarea_info[id].text_left != NULL)
10376     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
10377
10378   UnmapDrawingArea(id);
10379
10380   ModifyEditorDrawingArea(id, num_elements, 1);
10381
10382   // delete content areas in case of reducing number of them
10383   DrawBackground(sx, sy,
10384                  xsize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10385                  ysize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size);
10386
10387   MapDrawingArea(id);
10388 }
10389
10390 static void DrawGroupElementArea(void)
10391 {
10392   int id = ED_DRAWING_ID_GROUP_CONTENT;
10393   int num_elements = group_element_info.num_elements;
10394   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10395   int sx = SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size;
10396   int sy = SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size;
10397   int xsize = MAX_ELEMENTS_IN_GROUP;
10398   int ysize = 1;
10399
10400   if (drawingarea_info[id].text_left != NULL)
10401     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
10402
10403   UnmapDrawingArea(id);
10404
10405   ModifyEditorDrawingArea(id, num_elements, 1);
10406
10407   // delete content areas in case of reducing number of them
10408   DrawBackground(sx, sy,
10409                  xsize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10410                  ysize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size);
10411
10412   MapDrawingArea(id);
10413 }
10414
10415 static void DrawPlayerInitialInventoryArea(int element)
10416 {
10417   int id = ED_DRAWING_ID_INVENTORY_CONTENT;
10418   int player_nr = GET_PLAYER_NR(element);
10419   int num_elements = level.initial_inventory_size[player_nr];
10420   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10421   int sx = SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size;
10422   int sy = SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size;
10423   int xsize = MAX_INITIAL_INVENTORY_SIZE;
10424   int ysize = 1;
10425
10426   // determine horizontal position to the right of specified gadget
10427   if (drawingarea_info[id].gadget_id_align != GADGET_ID_NONE)
10428     sx = (right_gadget_border[drawingarea_info[id].gadget_id_align] +
10429           ED_DRAWINGAREA_TEXT_DISTANCE);
10430
10431   // determine horizontal offset for leading text
10432   if (drawingarea_info[id].text_left != NULL)
10433     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
10434
10435   UnmapDrawingArea(id);
10436
10437   ModifyEditorDrawingArea(id, num_elements, 1);
10438
10439   // delete content areas in case of reducing number of them
10440   DrawBackground(sx, sy,
10441                  xsize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10442                  ysize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size);
10443
10444   MapDrawingArea(id);
10445 }
10446
10447 static void DrawMMBallContentArea(void)
10448 {
10449   int id = ED_DRAWING_ID_MM_BALL_CONTENT;
10450   int num_elements = level.num_mm_ball_contents;
10451   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10452   int sx = SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size;
10453   int sy = SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size;
10454   int xsize = MAX_MM_BALL_CONTENTS;
10455   int ysize = 1;
10456
10457   if (drawingarea_info[id].text_left != NULL)
10458     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
10459
10460   UnmapDrawingArea(id);
10461
10462   ModifyEditorDrawingArea(id, num_elements, 1);
10463
10464   // delete content areas in case of reducing number of them
10465   DrawBackground(sx, sy,
10466                  xsize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10467                  ysize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size);
10468
10469   MapDrawingArea(id);
10470 }
10471
10472 static void DrawEnvelopeTextArea(int envelope_nr)
10473 {
10474   int id = ED_TEXTAREA_ID_ENVELOPE_INFO;
10475   struct GadgetInfo *gi = level_editor_gadget[textarea_info[id].gadget_id];
10476
10477   UnmapGadget(gi);
10478
10479   DrawBackground(gi->x, gi->y,
10480                  gi->textarea.crop_width, gi->textarea.crop_height);
10481
10482   if (envelope_nr != -1)
10483     textarea_info[id].value = level.envelope[envelope_nr].text;
10484
10485   ModifyGadget(gi, GDI_AREA_SIZE,
10486                *counterbutton_info[ED_COUNTER_ID_ENVELOPE_XSIZE].value,
10487                *counterbutton_info[ED_COUNTER_ID_ENVELOPE_YSIZE].value,
10488                GDI_END);
10489
10490   MapTextAreaGadget(ED_TEXTAREA_ID_ENVELOPE_INFO);
10491 }
10492
10493 static void DrawPropertiesInfo(void)
10494 {
10495   static struct
10496   {
10497     int value;
10498     char *text;
10499   }
10500   properties[] =
10501   {
10502     // configurable properties
10503
10504     { EP_WALKABLE_OVER,         "- player can walk over it"             },
10505     { EP_WALKABLE_INSIDE,       "- player can walk inside it"           },
10506     { EP_WALKABLE_UNDER,        "- player can walk under it"            },
10507     { EP_PASSABLE_OVER,         "- player can pass over it"             },
10508     { EP_PASSABLE_INSIDE,       "- player can pass through it"          },
10509     { EP_PASSABLE_UNDER,        "- player can pass under it"            },
10510     { EP_PROTECTED,             "- player is protected by it"           },
10511
10512     { EP_DIGGABLE,              "- can be digged away"                  },
10513     { EP_COLLECTIBLE,           "- can be collected"                    },
10514     { EP_DROPPABLE,             "- can be dropped after collecting"     },
10515     { EP_THROWABLE,             "- can be thrown after collecting"      },
10516     { EP_PUSHABLE,              "- can be pushed"                       },
10517
10518     { EP_CAN_FALL,              "- can fall"                            },
10519     { EP_CAN_MOVE,              "- can move"                            },
10520
10521     { EP_CAN_SMASH_PLAYER,      "- can smash player"                    },
10522 #if 0
10523     { EP_CAN_SMASH_ENEMIES,     "- can smash good and bad guys"         },
10524 #endif
10525     { EP_CAN_SMASH_EVERYTHING,  "- can smash everything smashable"      },
10526
10527     { EP_SLIPPERY,              "- slippery for falling elements"       },
10528     { EP_EM_SLIPPERY_WALL,      "- slippery for some gems (EM style)"   },
10529
10530     { EP_DONT_RUN_INTO,         "- deadly when running into"            },
10531     { EP_DONT_COLLIDE_WITH,     "- deadly when colliding with"          },
10532     { EP_DONT_GET_HIT_BY,       "- deadly when getting hit by"          },
10533     { EP_DONT_TOUCH,            "- deadly when touching"                },
10534
10535     { EP_INDESTRUCTIBLE,        "- indestructible"                      },
10536
10537     { EP_CAN_EXPLODE_BY_FIRE,   "- can explode by fire or explosions"   },
10538     { EP_CAN_EXPLODE_SMASHED,   "- can explode when smashed"            },
10539     { EP_CAN_EXPLODE_IMPACT,    "- can explode on impact"               },
10540
10541     { EP_CAN_CHANGE,            "- can change to other element"         },
10542
10543     // pre-defined properties
10544     { EP_CAN_PASS_MAGIC_WALL,   "- can pass magic walls"                },
10545     { EP_CAN_PASS_DC_MAGIC_WALL,"- can pass magic walls (DC style)"     },
10546     { EP_SWITCHABLE,            "- can be switched"                     },
10547 #if 0
10548     { EP_HAS_EDITOR_CONTENT,    "- can contain other elements"          },
10549 #endif
10550
10551     { -1,                       NULL                                    }
10552   };
10553   char *filename = getElementDescriptionFilename(properties_element);
10554   char *num_elements_text = "In this level: ";
10555   char *num_similar_text = "Similar tiles: ";
10556   char *properties_text = "Standard properties: ";
10557   char *description_text = "Description:";
10558   char *no_description_text = "No description available.";
10559   char *none_text = "None";
10560   float percentage;
10561   int num_elements_in_level = 0;
10562   int num_similar_in_level = 0;
10563   int num_hires_tiles_in_level = 0;
10564   int num_standard_properties = 0;
10565   int font1_nr = FONT_TEXT_1;
10566   int font2_nr = FONT_TEXT_2;
10567   int font1_width = getFontWidth(font1_nr);
10568   int font1_height = getFontHeight(font1_nr);
10569   int font2_height = getFontHeight(font2_nr);
10570   int line1_height = font1_height + ED_GADGET_LINE_DISTANCE;
10571   int font2_yoffset = (font1_height - font2_height) / 2;
10572   int num_elements_text_len = strlen(num_elements_text) * font1_width;
10573   int num_similar_text_len = strlen(num_similar_text) * font1_width;
10574   int properties_text_len = strlen(properties_text) * font1_width;
10575   int xpos = ED_ELEMENT_SETTINGS_X(0);
10576   int ypos = ED_ELEMENT_SETTINGS_Y(0) + ED_GADGET_SMALL_DISTANCE;
10577   int i, x, y;
10578
10579   if (setup.editor.show_element_token)
10580   {
10581     int font3_nr = FONT_TEXT_3;
10582     int font3_height = getFontHeight(font3_nr);
10583
10584     DrawTextF(xpos, ypos, font3_nr,
10585               "[%s]", element_info[properties_element].token_name);
10586
10587     ypos += 2 * font3_height;
10588   }
10589
10590   // ----- print number of elements / percentage of this element in level
10591
10592   for (y = 0; y < lev_fieldy; y++)
10593   {
10594     for (x = 0; x < lev_fieldx; x++)
10595     {
10596       if (Tile[x][y] == properties_element)
10597       {
10598         num_elements_in_level++;
10599       }
10600       else if (IS_MM_WALL(Tile[x][y]) &&
10601                map_mm_wall_element(Tile[x][y]) == properties_element)
10602       {
10603         num_hires_tiles_in_level += numHiresTiles(Tile[x][y]);
10604       }
10605     }
10606   }
10607
10608   percentage = num_elements_in_level * 100.0 / (lev_fieldx * lev_fieldy);
10609
10610   DrawTextS(xpos, ypos, font1_nr, num_elements_text);
10611
10612   if (num_hires_tiles_in_level > 0)
10613     DrawTextF(xpos + num_elements_text_len, ypos + font2_yoffset, font2_nr,
10614               "%d wall tiles", num_hires_tiles_in_level);
10615   else if (num_elements_in_level > 0)
10616     DrawTextF(xpos + num_elements_text_len, ypos + font2_yoffset, font2_nr,
10617               "%d (%.2f %%)", num_elements_in_level, percentage);
10618   else
10619     DrawTextF(xpos + num_elements_text_len, ypos + font2_yoffset, font2_nr,
10620               none_text);
10621
10622   // ----- print number of similar elements / percentage of them in level
10623
10624   for (y = 0; y < lev_fieldy; y++)
10625   {
10626     for (x = 0; x < lev_fieldx; x++)
10627     {
10628       if (strEqual(element_info[Tile[x][y]].class_name,
10629                    element_info[properties_element].class_name))
10630       {
10631         num_similar_in_level++;
10632       }
10633     }
10634   }
10635
10636   if (num_similar_in_level != num_elements_in_level)
10637   {
10638     ypos += 1 * MAX(font1_height, font2_height);
10639
10640     percentage = num_similar_in_level * 100.0 / (lev_fieldx * lev_fieldy);
10641
10642     DrawTextS(xpos, ypos, font1_nr, num_similar_text);
10643
10644     if (num_similar_in_level > 0)
10645       DrawTextF(xpos + num_similar_text_len, ypos + font2_yoffset, font2_nr,
10646                 "%d (%.2f %%)", num_similar_in_level, percentage);
10647     else
10648       DrawTextF(xpos + num_similar_text_len, ypos + font2_yoffset, font2_nr,
10649                 none_text);
10650   }
10651
10652   ypos += 2 * MAX(font1_height, font2_height);
10653
10654   // ----- print standard properties of this element
10655
10656   DrawTextS(xpos, ypos, font1_nr, properties_text);
10657
10658   ypos += line1_height;
10659
10660   for (i = 0; properties[i].value != -1; i++)
10661   {
10662     if (!HAS_PROPERTY(properties_element, properties[i].value))
10663       continue;
10664
10665     DrawTextS(xpos, ypos, font2_nr, properties[i].text);
10666
10667     ypos += font2_height;
10668
10669     num_standard_properties++;
10670   }
10671
10672   if (num_standard_properties == 0)
10673   {
10674     DrawTextS(xpos + properties_text_len, ypos - line1_height + font2_yoffset,
10675               font2_nr, none_text);
10676
10677     ypos -= (line1_height - font1_height);
10678   }
10679
10680   ypos += MAX(font1_height, font2_height);
10681
10682   // ----- print special description of this element
10683
10684   PrintInfoText(description_text, font1_nr, xpos, ypos);
10685
10686   ypos += line1_height;
10687
10688   if (PrintElementDescriptionFromFile(filename, font2_nr, xpos, ypos) == 0)
10689     PrintInfoText(no_description_text, font1_nr, xpos, ypos - line1_height);
10690 }
10691
10692 #define TEXT_COLLECTING         "Score for collecting"
10693 #define TEXT_COLLECTING_EXTRA   "Score for extra diamonds"
10694 #define TEXT_SMASHING           "Score for smashing"
10695 #define TEXT_SLURPING           "Score for slurping robot"
10696 #define TEXT_CRACKING           "Score for cracking"
10697 #define TEXT_AMOEBA_SPEED       "Speed of amoeba growth"
10698 #define TEXT_DURATION           "Duration when activated"
10699 #define TEXT_DELAY_ON           "Delay before activating"
10700 #define TEXT_DELAY_OFF          "Delay before deactivating"
10701 #define TEXT_DELAY_CHANGING     "Delay before changing"
10702 #define TEXT_DELAY_EXPLODING    "Delay before exploding"
10703 #define TEXT_DELAY_MOVING       "Delay before moving"
10704 #define TEXT_BALL_DELAY         "Element generation delay"
10705 #define TEXT_MOVE_SPEED         "Speed of android moving"
10706 #define TEXT_CLONE_SPEED        "Speed of android cloning"
10707 #define TEXT_GAME_OF_LIFE_1     "Min neighbours to survive"
10708 #define TEXT_GAME_OF_LIFE_2     "Max neighbours to survive"
10709 #define TEXT_GAME_OF_LIFE_3     "Min neighbours to create"
10710 #define TEXT_GAME_OF_LIFE_4     "Max neighbours to create"
10711 #define TEXT_TIME_BONUS         "Extra time to solve level"
10712
10713 static struct
10714 {
10715   int element;
10716   int *value;
10717   char *text;
10718 } elements_with_counter[] =
10719 {
10720   { EL_EMERALD,                 &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10721   { EL_BD_DIAMOND,              &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10722   { EL_BD_DIAMOND,              &level.score[SC_DIAMOND_EXTRA], TEXT_COLLECTING_EXTRA   },
10723   { EL_EMERALD_YELLOW,          &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10724   { EL_EMERALD_RED,             &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10725   { EL_EMERALD_PURPLE,          &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10726   { EL_SP_INFOTRON,             &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10727   { EL_DIAMOND,                 &level.score[SC_DIAMOND],       TEXT_COLLECTING         },
10728   { EL_CRYSTAL,                 &level.score[SC_CRYSTAL],       TEXT_COLLECTING         },
10729   { EL_PEARL,                   &level.score[SC_PEARL],         TEXT_COLLECTING         },
10730   { EL_BUG,                     &level.score[SC_BUG],           TEXT_SMASHING           },
10731   { EL_BUG_RIGHT,               &level.score[SC_BUG],           TEXT_SMASHING           },
10732   { EL_BUG_UP,                  &level.score[SC_BUG],           TEXT_SMASHING           },
10733   { EL_BUG_LEFT,                &level.score[SC_BUG],           TEXT_SMASHING           },
10734   { EL_BUG_DOWN,                &level.score[SC_BUG],           TEXT_SMASHING           },
10735   { EL_BD_BUTTERFLY,            &level.score[SC_BUG],           TEXT_SMASHING           },
10736   { EL_BD_BUTTERFLY_RIGHT,      &level.score[SC_BUG],           TEXT_SMASHING           },
10737   { EL_BD_BUTTERFLY_UP,         &level.score[SC_BUG],           TEXT_SMASHING           },
10738   { EL_BD_BUTTERFLY_LEFT,       &level.score[SC_BUG],           TEXT_SMASHING           },
10739   { EL_BD_BUTTERFLY_DOWN,       &level.score[SC_BUG],           TEXT_SMASHING           },
10740   { EL_SP_ELECTRON,             &level.score[SC_BUG],           TEXT_SMASHING           },
10741   { EL_SPACESHIP,               &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10742   { EL_SPACESHIP_RIGHT,         &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10743   { EL_SPACESHIP_UP,            &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10744   { EL_SPACESHIP_LEFT,          &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10745   { EL_SPACESHIP_DOWN,          &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10746   { EL_BD_FIREFLY,              &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10747   { EL_BD_FIREFLY_RIGHT,        &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10748   { EL_BD_FIREFLY_UP,           &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10749   { EL_BD_FIREFLY_LEFT,         &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10750   { EL_BD_FIREFLY_DOWN,         &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10751   { EL_SP_SNIKSNAK,             &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10752   { EL_YAMYAM,                  &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10753   { EL_YAMYAM_LEFT,             &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10754   { EL_YAMYAM_RIGHT,            &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10755   { EL_YAMYAM_UP,               &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10756   { EL_YAMYAM_DOWN,             &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10757   { EL_DARK_YAMYAM,             &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10758   { EL_ROBOT,                   &level.score[SC_ROBOT],         TEXT_SMASHING           },
10759   { EL_PACMAN,                  &level.score[SC_PACMAN],        TEXT_SMASHING           },
10760   { EL_PACMAN_RIGHT,            &level.score[SC_PACMAN],        TEXT_SMASHING           },
10761   { EL_PACMAN_UP,               &level.score[SC_PACMAN],        TEXT_SMASHING           },
10762   { EL_PACMAN_LEFT,             &level.score[SC_PACMAN],        TEXT_SMASHING           },
10763   { EL_PACMAN_DOWN,             &level.score[SC_PACMAN],        TEXT_SMASHING           },
10764   { EL_NUT,                     &level.score[SC_NUT],           TEXT_CRACKING           },
10765   { EL_DYNAMITE,                &level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10766   { EL_EM_DYNAMITE,             &level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10767   { EL_DYNABOMB_INCREASE_NUMBER,&level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10768   { EL_DYNABOMB_INCREASE_SIZE,  &level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10769   { EL_DYNABOMB_INCREASE_POWER, &level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10770   { EL_SHIELD_NORMAL,           &level.score[SC_SHIELD],        TEXT_COLLECTING         },
10771   { EL_SHIELD_DEADLY,           &level.score[SC_SHIELD],        TEXT_COLLECTING         },
10772   { EL_EXTRA_TIME,              &level.extra_time_score,        TEXT_COLLECTING         },
10773   { EL_KEY_1,                   &level.score[SC_KEY],           TEXT_COLLECTING         },
10774   { EL_KEY_2,                   &level.score[SC_KEY],           TEXT_COLLECTING         },
10775   { EL_KEY_3,                   &level.score[SC_KEY],           TEXT_COLLECTING         },
10776   { EL_KEY_4,                   &level.score[SC_KEY],           TEXT_COLLECTING         },
10777   { EL_EM_KEY_1,                &level.score[SC_KEY],           TEXT_COLLECTING         },
10778   { EL_EM_KEY_2,                &level.score[SC_KEY],           TEXT_COLLECTING         },
10779   { EL_EM_KEY_3,                &level.score[SC_KEY],           TEXT_COLLECTING         },
10780   { EL_EM_KEY_4,                &level.score[SC_KEY],           TEXT_COLLECTING         },
10781   { EL_EMC_KEY_5,               &level.score[SC_KEY],           TEXT_COLLECTING         },
10782   { EL_EMC_KEY_6,               &level.score[SC_KEY],           TEXT_COLLECTING         },
10783   { EL_EMC_KEY_7,               &level.score[SC_KEY],           TEXT_COLLECTING         },
10784   { EL_EMC_KEY_8,               &level.score[SC_KEY],           TEXT_COLLECTING         },
10785   { EL_DC_KEY_WHITE,            &level.score[SC_KEY],           TEXT_COLLECTING         },
10786   { EL_MM_KETTLE,               &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10787   { EL_DF_CELL,                 &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10788   { EL_MM_KEY,                  &level.score[SC_KEY],           TEXT_COLLECTING         },
10789   { EL_MM_LIGHTBALL,            &level.score[SC_ELEM_BONUS],    TEXT_COLLECTING         },
10790   { EL_MM_PACMAN,               &level.score[SC_PACMAN],        TEXT_SMASHING           },
10791   { EL_MM_PACMAN_RIGHT,         &level.score[SC_PACMAN],        TEXT_SMASHING           },
10792   { EL_MM_PACMAN_UP,            &level.score[SC_PACMAN],        TEXT_SMASHING           },
10793   { EL_MM_PACMAN_LEFT,          &level.score[SC_PACMAN],        TEXT_SMASHING           },
10794   { EL_MM_PACMAN_DOWN,          &level.score[SC_PACMAN],        TEXT_SMASHING           },
10795   { EL_AMOEBA_WET,              &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10796   { EL_AMOEBA_DRY,              &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10797   { EL_AMOEBA_FULL,             &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10798   { EL_BD_AMOEBA,               &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10799   { EL_EMC_DRIPPER,             &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10800   { EL_MAGIC_WALL,              &level.time_magic_wall,         TEXT_DURATION           },
10801   { EL_BD_MAGIC_WALL,           &level.time_magic_wall,         TEXT_DURATION           },
10802   { EL_DC_MAGIC_WALL,           &level.time_magic_wall,         TEXT_DURATION           },
10803   { EL_ROBOT_WHEEL,             &level.time_wheel,              TEXT_DURATION           },
10804   { EL_TIMEGATE_SWITCH,         &level.time_timegate,           TEXT_DURATION           },
10805   { EL_DC_TIMEGATE_SWITCH,      &level.time_timegate,           TEXT_DURATION           },
10806   { EL_LIGHT_SWITCH,            &level.time_light,              TEXT_DURATION           },
10807   { EL_LIGHT_SWITCH_ACTIVE,     &level.time_light,              TEXT_DURATION           },
10808   { EL_SHIELD_NORMAL,           &level.shield_normal_time,      TEXT_DURATION           },
10809   { EL_SHIELD_DEADLY,           &level.shield_deadly_time,      TEXT_DURATION           },
10810   { EL_EXTRA_TIME,              &level.extra_time,              TEXT_TIME_BONUS         },
10811   { EL_TIME_ORB_FULL,           &level.time_orb_time,           TEXT_TIME_BONUS         },
10812   { EL_GAME_OF_LIFE,            &level.game_of_life[0],         TEXT_GAME_OF_LIFE_1     },
10813   { EL_GAME_OF_LIFE,            &level.game_of_life[1],         TEXT_GAME_OF_LIFE_2     },
10814   { EL_GAME_OF_LIFE,            &level.game_of_life[2],         TEXT_GAME_OF_LIFE_3     },
10815   { EL_GAME_OF_LIFE,            &level.game_of_life[3],         TEXT_GAME_OF_LIFE_4     },
10816   { EL_BIOMAZE,                 &level.biomaze[0],              TEXT_GAME_OF_LIFE_1     },
10817   { EL_BIOMAZE,                 &level.biomaze[1],              TEXT_GAME_OF_LIFE_2     },
10818   { EL_BIOMAZE,                 &level.biomaze[2],              TEXT_GAME_OF_LIFE_3     },
10819   { EL_BIOMAZE,                 &level.biomaze[3],              TEXT_GAME_OF_LIFE_4     },
10820   { EL_EMC_ANDROID,             &level.android_move_time,       TEXT_MOVE_SPEED         },
10821   { EL_EMC_ANDROID,             &level.android_clone_time,      TEXT_CLONE_SPEED        },
10822   { EL_EMC_MAGIC_BALL,          &level.ball_time,               TEXT_BALL_DELAY         },
10823   { EL_EMC_LENSES,              &level.lenses_score,            TEXT_COLLECTING         },
10824   { EL_EMC_MAGNIFIER,           &level.magnify_score,           TEXT_COLLECTING         },
10825   { EL_SPRING,                  &level.slurp_score,             TEXT_SLURPING           },
10826   { EL_SPRING_LEFT,             &level.slurp_score,             TEXT_SLURPING           },
10827   { EL_SPRING_RIGHT,            &level.slurp_score,             TEXT_SLURPING           },
10828   { EL_EMC_LENSES,              &level.lenses_time,             TEXT_DURATION           },
10829   { EL_EMC_MAGNIFIER,           &level.magnify_time,            TEXT_DURATION           },
10830   { EL_MM_FUSE_ACTIVE,          &level.mm_time_fuse,            TEXT_DELAY_OFF          },
10831   { EL_MM_BOMB,                 &level.mm_time_bomb,            TEXT_DELAY_EXPLODING    },
10832   { EL_MM_GRAY_BALL,            &level.mm_time_ball,            TEXT_DELAY_CHANGING     },
10833   { EL_MM_STEEL_BLOCK,          &level.mm_time_block,           TEXT_DELAY_MOVING       },
10834   { EL_MM_WOODEN_BLOCK,         &level.mm_time_block,           TEXT_DELAY_MOVING       },
10835
10836   { -1,                         NULL,                           NULL                    }
10837 };
10838
10839 static boolean checkPropertiesConfig(int element)
10840 {
10841   int i;
10842
10843   // special case: empty space customization only available in R'n'D game engine
10844   if (element == EL_EMPTY_SPACE && level.game_engine_type != GAME_ENGINE_TYPE_RND)
10845     return FALSE;
10846
10847   // special case: BD style rock customization only available in BD game engine
10848   if (element == EL_BD_ROCK && level.game_engine_type != GAME_ENGINE_TYPE_BD)
10849     return FALSE;
10850
10851   if (IS_GEM(element) ||
10852       IS_CUSTOM_ELEMENT(element) ||
10853       IS_GROUP_ELEMENT(element) ||
10854       IS_EMPTY_ELEMENT(element) ||
10855       IS_BALLOON_ELEMENT(element) ||
10856       IS_ENVELOPE(element) ||
10857       IS_MM_ENVELOPE(element) ||
10858       IS_MM_MCDUFFIN(element) ||
10859       IS_DF_LASER(element) ||
10860       IS_PLAYER_ELEMENT(element) ||
10861       IS_BD_PLAYER_ELEMENT(element) ||
10862       IS_SOKOBAN_OBJECT_OR_FIELD(element) ||
10863       HAS_EDITOR_CONTENT(element) ||
10864       CAN_GROW(element) ||
10865       COULD_MOVE_INTO_ACID(element) ||
10866       MAYBE_DONT_COLLIDE_WITH(element) ||
10867       element == EL_BD_ROCK ||
10868       element == EL_BD_MEGA_ROCK ||
10869       element == EL_BD_SWEET)
10870   {
10871     return TRUE;
10872   }
10873   else
10874   {
10875     for (i = 0; elements_with_counter[i].element != -1; i++)
10876       if (elements_with_counter[i].element == element)
10877         return TRUE;
10878   }
10879
10880   return FALSE;
10881 }
10882
10883 static void SetAutomaticNumberOfGemsNeeded(void)
10884 {
10885   int x, y;
10886
10887   if (!level.auto_count_gems)
10888     return;
10889
10890   level.gems_needed = 0;
10891
10892   for (x = 0; x < lev_fieldx; x++)
10893   {
10894     for (y = 0; y < lev_fieldy; y++)
10895     {
10896       int element = Tile[x][y];
10897
10898       switch (element)
10899       {
10900         case EL_EMERALD:
10901         case EL_EMERALD_YELLOW:
10902         case EL_EMERALD_RED:
10903         case EL_EMERALD_PURPLE:
10904         case EL_BD_DIAMOND:
10905         case EL_WALL_EMERALD:
10906         case EL_WALL_EMERALD_YELLOW:
10907         case EL_WALL_EMERALD_RED:
10908         case EL_WALL_EMERALD_PURPLE:
10909         case EL_WALL_BD_DIAMOND:
10910         case EL_NUT:
10911         case EL_SP_INFOTRON:
10912         case EL_MM_KETTLE:
10913         case EL_DF_CELL:
10914           level.gems_needed++;
10915           break;
10916
10917         case EL_DIAMOND:
10918         case EL_WALL_DIAMOND:
10919           level.gems_needed += 3;
10920           break;
10921
10922         case EL_PEARL:
10923         case EL_WALL_PEARL:
10924           level.gems_needed += 5;
10925           break;
10926
10927         case EL_CRYSTAL:
10928         case EL_WALL_CRYSTAL:
10929           level.gems_needed += 8;
10930           break;
10931
10932         default:
10933           break;
10934       }
10935     }
10936   }
10937
10938   ModifyEditorCounterValue(ED_COUNTER_ID_LEVEL_GEMSLIMIT, level.gems_needed);
10939 }
10940
10941 static void DrawPropertiesConfig(void)
10942 {
10943   boolean draw_footer_line = FALSE;
10944   int max_num_element_counters = 4;
10945   int num_element_counters = 0;
10946   int i;
10947
10948   if (!checkPropertiesConfig(properties_element))
10949   {
10950     int xpos = ED_ELEMENT_SETTINGS_X(0);
10951     int ypos = ED_ELEMENT_SETTINGS_Y(0) + ED_GADGET_SMALL_DISTANCE;
10952
10953     PrintInfoText("No configuration options available.",
10954                   FONT_TEXT_1, xpos, ypos);
10955
10956     return;
10957   }
10958
10959   // check if there are elements where a value can be chosen for
10960   for (i = 0; elements_with_counter[i].element != -1; i++)
10961   {
10962     if (elements_with_counter[i].element == properties_element)
10963     {
10964       // special case: score for extra diamonds only available in BD game engine
10965       if (elements_with_counter[i].element == EL_BD_DIAMOND &&
10966           elements_with_counter[i].value == &level.score[SC_DIAMOND_EXTRA] &&
10967           level.game_engine_type != GAME_ENGINE_TYPE_BD)
10968         continue;
10969
10970       int counter_id = ED_COUNTER_ID_ELEMENT_VALUE1 + num_element_counters;
10971
10972       counterbutton_info[counter_id].y =
10973         ED_ELEMENT_SETTINGS_YPOS(
10974                 (HAS_EDITOR_CONTENT(properties_element)      ? 1 : 0) +
10975                 (CAN_GROW(properties_element)                ? 1 : 0) +
10976                 (COULD_MOVE_INTO_ACID(properties_element)    ? 1 : 0) +
10977                 (MAYBE_DONT_COLLIDE_WITH(properties_element) ? 1 : 0) +
10978                 (properties_element == EL_EMC_MAGIC_BALL     ? 2 : 0) +
10979                 num_element_counters);
10980
10981       counterbutton_info[counter_id].value = elements_with_counter[i].value;
10982       counterbutton_info[counter_id].text_right = elements_with_counter[i].text;
10983
10984       if (properties_element == EL_GAME_OF_LIFE ||
10985           properties_element == EL_BIOMAZE)
10986       {
10987         counterbutton_info[counter_id].min_value = 0;   // min neighbours
10988         counterbutton_info[counter_id].max_value = 8;   // max neighbours
10989       }
10990       else
10991       {
10992         // !!! CHANGE THIS FOR CERTAIN ELEMENTS !!!
10993         counterbutton_info[counter_id].min_value = MIN_SCORE;
10994         counterbutton_info[counter_id].max_value = MAX_SCORE;
10995       }
10996
10997       MapCounterButtons(counter_id);
10998
10999       num_element_counters++;
11000       if (num_element_counters >= max_num_element_counters)
11001         break;
11002     }
11003   }
11004
11005   if (HAS_EDITOR_CONTENT(properties_element))
11006   {
11007     // draw stickybutton gadget
11008     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11009
11010     if (IS_AMOEBOID(properties_element))
11011       MapDrawingArea(ED_DRAWING_ID_AMOEBA_CONTENT);
11012     else if (properties_element == EL_YAMYAM ||
11013              properties_element == EL_YAMYAM_LEFT ||
11014              properties_element == EL_YAMYAM_RIGHT ||
11015              properties_element == EL_YAMYAM_UP ||
11016              properties_element == EL_YAMYAM_DOWN)
11017       DrawYamYamContentAreas();
11018     else if (properties_element == EL_EMC_MAGIC_BALL)
11019     {
11020       DrawMagicBallContentAreas();
11021
11022       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_RANDOM_BALL_CONTENT);
11023       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_INITIAL_BALL_ACTIVE);
11024     }
11025     else if (properties_element == EL_EMC_ANDROID)
11026       DrawAndroidElementArea();
11027     else if (properties_element == EL_MM_GRAY_BALL)
11028     {
11029       MapCounterButtons(ED_COUNTER_ID_MM_BALL_CONTENT);
11030       MapSelectboxGadget(ED_SELECTBOX_ID_MM_BALL_CHOICE_MODE);
11031       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_ROTATE_MM_BALL_CONTENT);
11032       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_EXPLODE_MM_BALL);
11033
11034       DrawMMBallContentArea();
11035     }
11036   }
11037
11038   if (IS_PLAYER_ELEMENT(properties_element))
11039   {
11040     int player_nr = GET_PLAYER_NR(properties_element);
11041
11042     // these properties can be set for every player individually
11043
11044     if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_1)
11045     {
11046       drawingarea_info[ED_DRAWING_ID_START_ELEMENT].value =
11047         &level.start_element[player_nr];
11048       drawingarea_info[ED_DRAWING_ID_ARTWORK_ELEMENT].value =
11049         &level.artwork_element[player_nr];
11050       drawingarea_info[ED_DRAWING_ID_EXPLOSION_ELEMENT].value =
11051         &level.explosion_element[player_nr];
11052
11053       checkbutton_info[ED_CHECKBUTTON_ID_USE_START_ELEMENT].value =
11054         &level.use_start_element[player_nr];
11055       checkbutton_info[ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT].value =
11056         &level.use_artwork_element[player_nr];
11057       checkbutton_info[ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT].value =
11058         &level.use_explosion_element[player_nr];
11059       checkbutton_info[ED_CHECKBUTTON_ID_INITIAL_GRAVITY].value =
11060         &level.initial_player_gravity[player_nr];
11061
11062       selectbox_info[ED_SELECTBOX_ID_PLAYER_SPEED].value =
11063         &level.initial_player_stepsize[player_nr];
11064
11065       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID);
11066       MapCheckbuttonGadget(properties_element == EL_SP_MURPHY ?
11067                            ED_CHECKBUTTON_ID_SP_BLOCK_LAST_FIELD :
11068                            ED_CHECKBUTTON_ID_BLOCK_LAST_FIELD);
11069       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BLOCK_SNAP_FIELD);
11070       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CONTINUOUS_SNAPPING);
11071       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_INSTANT_RELOCATION);
11072       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SHIFTED_RELOCATION);
11073       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_LAZY_RELOCATION);
11074       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_START_ELEMENT);
11075       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT);
11076       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT);
11077       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_INITIAL_GRAVITY);
11078       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CAN_PASS_TO_WALKABLE);
11079       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SOLVED_BY_ONE_PLAYER);
11080
11081       MapDrawingArea(ED_DRAWING_ID_START_ELEMENT);
11082       MapDrawingArea(ED_DRAWING_ID_ARTWORK_ELEMENT);
11083       MapDrawingArea(ED_DRAWING_ID_EXPLOSION_ELEMENT);
11084
11085       MapSelectboxGadget(ED_SELECTBOX_ID_PLAYER_SPEED);
11086     }
11087     else if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_2)
11088     {
11089       drawingarea_info[ED_DRAWING_ID_INVENTORY_CONTENT].value =
11090         &level.initial_inventory_content[player_nr][0];
11091
11092       counterbutton_info[ED_COUNTER_ID_INVENTORY_SIZE].value =
11093         &level.initial_inventory_size[player_nr];
11094
11095       checkbutton_info[ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY].value =
11096         &level.use_initial_inventory[player_nr];
11097
11098       // draw checkbutton gadgets
11099       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY);
11100       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_FINISH_DIG_COLLECT);
11101       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_KEEP_WALKABLE_CE);
11102
11103       // draw counter gadgets
11104       MapCounterButtons(ED_COUNTER_ID_INVENTORY_SIZE);
11105
11106       // draw drawing area gadgets
11107       DrawPlayerInitialInventoryArea(properties_element);
11108     }
11109   }
11110
11111   if (IS_BD_PLAYER_ELEMENT(properties_element))
11112   {
11113     counterbutton_info[ED_COUNTER_ID_BD_PUSHING_PROB].y =
11114       ED_ELEMENT_SETTINGS_YPOS(2);
11115     counterbutton_info[ED_COUNTER_ID_BD_PUSHING_PROB_WITH_SWEET].y =
11116       ED_ELEMENT_SETTINGS_YPOS(3);
11117     checkbutton_info[ED_CHECKBUTTON_ID_BD_PUSH_MEGA_ROCK_WITH_SWEET].y =
11118       ED_ELEMENT_SETTINGS_YPOS(4);
11119
11120     // draw checkbutton gadgets
11121     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BD_DIAGONAL_MOVEMENTS);
11122     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BD_TOPMOST_PLAYER_ACTIVE);
11123     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BD_PUSH_MEGA_ROCK_WITH_SWEET);
11124
11125     // draw counter gadgets
11126     MapCounterButtons(ED_COUNTER_ID_BD_PUSHING_PROB);
11127     MapCounterButtons(ED_COUNTER_ID_BD_PUSHING_PROB_WITH_SWEET);
11128   }
11129
11130   if (properties_element == EL_BD_ROCK && level.game_engine_type == GAME_ENGINE_TYPE_BD)
11131   {
11132     counterbutton_info[ED_COUNTER_ID_BD_PUSHING_PROB].y =
11133       ED_ELEMENT_SETTINGS_YPOS(0);
11134     counterbutton_info[ED_COUNTER_ID_BD_PUSHING_PROB_WITH_SWEET].y =
11135       ED_ELEMENT_SETTINGS_YPOS(1);
11136
11137     MapCounterButtons(ED_COUNTER_ID_BD_PUSHING_PROB);
11138     MapCounterButtons(ED_COUNTER_ID_BD_PUSHING_PROB_WITH_SWEET);
11139   }
11140
11141   if (properties_element == EL_BD_MEGA_ROCK ||
11142       properties_element == EL_BD_SWEET)
11143   {
11144     counterbutton_info[ED_COUNTER_ID_BD_PUSHING_PROB_WITH_SWEET].y =
11145       ED_ELEMENT_SETTINGS_YPOS(0);
11146     checkbutton_info[ED_CHECKBUTTON_ID_BD_PUSH_MEGA_ROCK_WITH_SWEET].y =
11147       ED_ELEMENT_SETTINGS_YPOS(1);
11148
11149     MapCounterButtons(ED_COUNTER_ID_BD_PUSHING_PROB_WITH_SWEET);
11150     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BD_PUSH_MEGA_ROCK_WITH_SWEET);
11151   }
11152
11153   if (properties_element == EL_BD_MAGIC_WALL)
11154   {
11155     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BD_MAGIC_WALL_WAIT_HATCHING);
11156     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BD_MAGIC_WALL_STOPS_AMOEBA);
11157   }
11158
11159   // special case: slippery walls option for gems only available in R'n'D game engine
11160   if (IS_GEM(properties_element) && level.game_engine_type == GAME_ENGINE_TYPE_RND)
11161     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS);
11162
11163   if (properties_element == EL_EM_DYNAMITE)
11164     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_EM_EXPLODES_BY_FIRE);
11165
11166   if (COULD_MOVE_INTO_ACID(properties_element) &&
11167       !IS_PLAYER_ELEMENT(properties_element) &&
11168       (!IS_CUSTOM_ELEMENT(properties_element) ||
11169        edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_2))
11170   {
11171     // set position for checkbutton for "can move into acid"
11172     checkbutton_info[ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID].x =
11173       ED_ELEMENT_SETTINGS_XPOS(IS_CUSTOM_ELEMENT(properties_element) ? 1 : 0);
11174     checkbutton_info[ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID].y =
11175       ED_ELEMENT_SETTINGS_YPOS(IS_CUSTOM_ELEMENT(properties_element) ? 7 :
11176                                IS_BALLOON_ELEMENT(properties_element) ||
11177                                HAS_EDITOR_CONTENT(properties_element) ? 1 : 0);
11178
11179     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID);
11180   }
11181
11182   if (MAYBE_DONT_COLLIDE_WITH(properties_element))
11183     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH);
11184
11185   if (properties_element == EL_SPRING ||
11186       properties_element == EL_SPRING_LEFT ||
11187       properties_element == EL_SPRING_RIGHT)
11188     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_SPRING_BUG);
11189
11190   if (properties_element == EL_TIME_ORB_FULL)
11191     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_TIME_ORB_BUG);
11192
11193   if (properties_element == EL_GAME_OF_LIFE ||
11194       properties_element == EL_BIOMAZE)
11195     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_LIFE_BUGS);
11196
11197   if (CAN_GROW(properties_element))
11198   {
11199     checkbutton_info[ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE].y =
11200       ED_ELEMENT_SETTINGS_YPOS(HAS_EDITOR_CONTENT(properties_element) ? 1 : 0);
11201
11202     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE);
11203   }
11204
11205   if (properties_element == EL_SOKOBAN_FIELD_EMPTY)
11206     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SB_FIELDS_NEEDED);
11207
11208   if (properties_element == EL_SOKOBAN_OBJECT)
11209     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SB_OBJECTS_NEEDED);
11210
11211   if (properties_element == EL_SOKOBAN_OBJECT ||
11212       properties_element == EL_SOKOBAN_FIELD_EMPTY ||
11213       properties_element == EL_SOKOBAN_FIELD_FULL)
11214   {
11215     checkbutton_info[ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN].y =
11216       ED_ELEMENT_SETTINGS_XPOS(properties_element == EL_SOKOBAN_FIELD_FULL ?
11217                                0 : 1);
11218
11219     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN);
11220   }
11221
11222   if (IS_BALLOON_ELEMENT(properties_element))
11223     MapSelectboxGadget(ED_SELECTBOX_ID_WIND_DIRECTION);
11224
11225   if (IS_ENVELOPE(properties_element) ||
11226       IS_MM_ENVELOPE(properties_element))
11227   {
11228     int counter1_id = ED_COUNTER_ID_ENVELOPE_XSIZE;
11229     int counter2_id = ED_COUNTER_ID_ENVELOPE_YSIZE;
11230     int button1_id = ED_CHECKBUTTON_ID_ENVELOPE_AUTOWRAP;
11231     int button2_id = ED_CHECKBUTTON_ID_ENVELOPE_CENTERED;
11232     int envelope_nr = ENVELOPE_NR(properties_element);
11233
11234     counterbutton_info[counter1_id].value = &level.envelope[envelope_nr].xsize;
11235     counterbutton_info[counter2_id].value = &level.envelope[envelope_nr].ysize;
11236
11237     checkbutton_info[button1_id].value = &level.envelope[envelope_nr].autowrap;
11238     checkbutton_info[button2_id].value = &level.envelope[envelope_nr].centered;
11239
11240     // display counter to choose size of envelope text area
11241     MapCounterButtons(counter1_id);
11242     MapCounterButtons(counter2_id);
11243
11244     // display checkbuttons to choose auto-wrap and alignment properties
11245     MapCheckbuttonGadget(button1_id);
11246     MapCheckbuttonGadget(button2_id);
11247
11248     DrawEnvelopeTextArea(envelope_nr);
11249   }
11250
11251   if (IS_MM_MCDUFFIN(properties_element))
11252   {
11253     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_MM_LASER_RED);
11254     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_MM_LASER_GREEN);
11255     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_MM_LASER_BLUE);
11256   }
11257
11258   if (IS_DF_LASER(properties_element))
11259   {
11260     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DF_LASER_RED);
11261     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DF_LASER_GREEN);
11262     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DF_LASER_BLUE);
11263   }
11264
11265   if (IS_CUSTOM_ELEMENT(properties_element))
11266   {
11267     // draw stickybutton gadget
11268     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11269
11270     if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_1)
11271     {
11272       // draw checkbutton gadgets
11273       for (i =  ED_CHECKBUTTON_ID_CUSTOM1_FIRST;
11274            i <= ED_CHECKBUTTON_ID_CUSTOM1_LAST; i++)
11275         MapCheckbuttonGadget(i);
11276
11277       // draw counter gadgets
11278       for (i =  ED_COUNTER_ID_CUSTOM1_FIRST;
11279            i <= ED_COUNTER_ID_CUSTOM1_LAST; i++)
11280         MapCounterButtons(i);
11281
11282       // draw selectbox gadgets
11283       for (i =  ED_SELECTBOX_ID_CUSTOM1_FIRST;
11284            i <= ED_SELECTBOX_ID_CUSTOM1_LAST; i++)
11285         MapSelectboxGadget(i);
11286
11287       // draw textbutton gadgets
11288       MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1);
11289
11290       // draw text input gadgets
11291       MapTextInputGadget(ED_TEXTINPUT_ID_ELEMENT_NAME);
11292
11293       // draw drawing area gadgets
11294       MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
11295
11296       draw_footer_line = TRUE;
11297     }
11298     else if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_2)
11299     {
11300       // draw checkbutton gadgets
11301       for (i =  ED_CHECKBUTTON_ID_CUSTOM2_FIRST;
11302            i <= ED_CHECKBUTTON_ID_CUSTOM2_LAST; i++)
11303         MapCheckbuttonGadget(i);
11304
11305       // draw counter gadgets
11306       for (i =  ED_COUNTER_ID_CUSTOM2_FIRST;
11307            i <= ED_COUNTER_ID_CUSTOM2_LAST; i++)
11308         MapCounterButtons(i);
11309
11310       // draw selectbox gadgets
11311       for (i =  ED_SELECTBOX_ID_CUSTOM2_FIRST;
11312            i <= ED_SELECTBOX_ID_CUSTOM2_LAST; i++)
11313         MapSelectboxGadget(i);
11314
11315       // draw drawing area gadgets
11316       MapDrawingArea(ED_DRAWING_ID_CUSTOM_MOVE_ENTER);
11317       MapDrawingArea(ED_DRAWING_ID_CUSTOM_MOVE_LEAVE);
11318       DrawCustomContentArea();
11319     }
11320   }
11321   else if (IS_GROUP_ELEMENT(properties_element))
11322   {
11323     // draw stickybutton gadget
11324     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11325
11326     // draw checkbutton gadgets
11327     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC);
11328     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1);
11329
11330     // draw counter gadgets
11331     MapCounterButtons(ED_COUNTER_ID_GROUP_CONTENT);
11332
11333     // draw selectbox gadgets
11334     MapSelectboxGadget(ED_SELECTBOX_ID_GROUP_CHOICE_MODE);
11335
11336     // draw textbutton gadgets
11337     MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1);
11338
11339     // draw drawing area gadgets
11340     DrawGroupElementArea();
11341
11342     // draw text input gadgets
11343     MapTextInputGadget(ED_TEXTINPUT_ID_ELEMENT_NAME);
11344
11345     // draw drawing area gadgets
11346     MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
11347
11348     draw_footer_line = TRUE;
11349   }
11350   else if (IS_EMPTY_ELEMENT(properties_element))
11351   {
11352     // draw stickybutton gadget
11353     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11354
11355     // draw checkbutton gadgets
11356     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC);
11357     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1);
11358
11359     // draw textbutton gadgets
11360     MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1);
11361
11362     // draw drawing area gadgets
11363     MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
11364
11365     draw_footer_line = TRUE;
11366   }
11367
11368   // draw little footer border line above CE/GE use/save template gadgets
11369   if (draw_footer_line)
11370   {
11371     struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_PROPERTIES_INFO];
11372     struct GadgetDesign *gd = &gd_gi1->alt_design[GD_BUTTON_UNPRESSED];
11373     int gd_x = gd->x + gd_gi1->border.width / 2;
11374     int gd_y = gd->y + gd_gi1->height - 1;
11375     Pixel tab_color = GetPixel(gd->bitmap, gd_x, gd_y);
11376
11377     if (tab_color != BLACK_PIXEL)               // black => transparent
11378       FillRectangle(drawto,
11379                     SX + ED_ELEMENT_SETTINGS_X(0),
11380                     SY + ED_ELEMENT_SETTINGS_Y(14) - ED_SETTINGS_TABS_YOFFSET -
11381                     ED_TAB_BAR_HEIGHT,
11382                     getTabulatorBarWidth(), getTabulatorBarHeight(), tab_color);
11383   }
11384 }
11385
11386 static void DrawPropertiesChangeDrawingAreas(void)
11387 {
11388   if (IS_CUSTOM_ELEMENT(properties_element))
11389   {
11390     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_TARGET);
11391     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_TRIGGER);
11392     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_ACTION);
11393
11394     DrawCustomChangeContentArea();
11395   }
11396
11397   redraw_mask |= REDRAW_FIELD;
11398 }
11399
11400 static void DrawPropertiesChange(void)
11401 {
11402   int i;
11403
11404   // needed to initially set selectbox options for special action options
11405   setSelectboxSpecialActionOptions();
11406
11407   // draw stickybutton gadget
11408   MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11409
11410   // draw checkbutton gadgets
11411   for (i =  ED_CHECKBUTTON_ID_CHANGE_FIRST;
11412        i <= ED_CHECKBUTTON_ID_CHANGE_LAST; i++)
11413     MapCheckbuttonGadget(i);
11414
11415   // draw counter gadgets
11416   for (i =  ED_COUNTER_ID_CHANGE_FIRST;
11417        i <= ED_COUNTER_ID_CHANGE_LAST; i++)
11418     MapCounterButtons(i);
11419
11420   // draw selectbox gadgets
11421   for (i =  ED_SELECTBOX_ID_CHANGE_FIRST;
11422        i <= ED_SELECTBOX_ID_CHANGE_LAST; i++)
11423     MapSelectboxGadget(i);
11424
11425   // draw textbutton gadgets
11426   for (i =  ED_TEXTBUTTON_ID_CHANGE_FIRST;
11427        i <= ED_TEXTBUTTON_ID_CHANGE_LAST; i++)
11428     MapTextbuttonGadget(i);
11429
11430   // draw graphicbutton gadgets
11431   for (i =  ED_GRAPHICBUTTON_ID_CHANGE_FIRST;
11432        i <= ED_GRAPHICBUTTON_ID_CHANGE_LAST; i++)
11433     MapGraphicbuttonGadget(i);
11434
11435   // draw drawing area gadgets
11436   DrawPropertiesChangeDrawingAreas();
11437 }
11438
11439 static void DrawEditorElementAnimation(int x, int y)
11440 {
11441   int graphic = el2img(properties_element);
11442   int frame = (ANIM_MODE(graphic) == ANIM_CE_VALUE ?
11443                custom_element.ce_value_fixed_initial :
11444                ANIM_MODE(graphic) == ANIM_CE_SCORE ?
11445                custom_element.collect_score_initial : FrameCounter);
11446
11447   DrawFixedGraphicAnimationExt(drawto, x, y, graphic, frame, NO_MASKING);
11448 }
11449
11450 static void DrawEditorElementName(int x, int y, int font_nr)
11451 {
11452   char *element_name = getElementInfoText(properties_element);
11453   int font_width = getFontWidth(font_nr);
11454   int font_height = getFontHeight(font_nr);
11455   int max_text_width = SXSIZE - x - ED_ELEMENT_SETTINGS_X(0);
11456   int max_chars_per_line = max_text_width / font_width;
11457
11458   if (strlen(element_name) <= max_chars_per_line)
11459     DrawTextS(x, y, font_nr, element_name);
11460   else
11461   {
11462     char buffer[max_chars_per_line + 1];
11463     int next_pos = max_chars_per_line;
11464
11465     strncpy(buffer, element_name, max_chars_per_line);
11466     buffer[max_chars_per_line] = '\0';
11467
11468     if (element_name[max_chars_per_line] == ' ')
11469       next_pos++;
11470     else
11471     {
11472       int i;
11473
11474       for (i = max_chars_per_line - 1; i >= 0; i--)
11475         if (buffer[i] == ' ')
11476           break;
11477
11478       if (strlen(&element_name[i + 1]) <= max_chars_per_line)
11479       {
11480         buffer[i] = '\0';
11481         next_pos = i + 1;
11482       }
11483     }
11484
11485     DrawTextS(x, y - font_height / 2, font_nr, buffer);
11486
11487     strncpy(buffer, &element_name[next_pos], max_chars_per_line);
11488     buffer[max_chars_per_line] = '\0';
11489
11490     DrawTextS(x, y + font_height / 2, font_nr, buffer);
11491   }
11492 }
11493
11494 static void DrawPropertiesWindow(void)
11495 {
11496   struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_INPUT_TEXT];
11497   int element_border = graphic_info[IMG_EDITOR_ELEMENT_BORDER].border_size;
11498   int border_size = gd->border_size;
11499   int font_nr = FONT_TEXT_1;
11500   int font_height = getFontHeight(font_nr);
11501   int xoffset = TILEX + element_border + 3 * border_size;
11502   int yoffset = (TILEY - font_height) / 2;
11503   int x1 = editor.settings.element_graphic.x + element_border;
11504   int y1 = editor.settings.element_graphic.y + element_border;
11505   int x2 = (editor.settings.element_name.x == -1 ? x1 + xoffset :
11506             editor.settings.element_name.x);
11507   int y2 = (editor.settings.element_name.y == -1 ? y1 + yoffset :
11508             editor.settings.element_name.y);
11509   char *text = "Element Settings";
11510   int font2_nr = FONT_TITLE_1;
11511   struct MenuPosInfo *pos = &editor.settings.headline;
11512   int sx = SX + ALIGNED_XPOS(pos->x, getTextWidth(text, font2_nr), pos->align);
11513   int sy = SY + pos->y;
11514
11515   stick_element_properties_window = FALSE;
11516
11517   // make sure that previous properties edit mode exists for this element
11518   if (edit_mode_properties > ED_MODE_PROPERTIES_CONFIG_2 &&
11519       !IS_CUSTOM_ELEMENT(properties_element))
11520     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG_2;
11521
11522   if (edit_mode_properties > ED_MODE_PROPERTIES_CONFIG &&
11523       !IS_PLAYER_ELEMENT(properties_element) &&
11524       !IS_CUSTOM_ELEMENT(properties_element))
11525     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG;
11526
11527   if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG &&
11528       (IS_PLAYER_ELEMENT(properties_element) ||
11529        IS_CUSTOM_ELEMENT(properties_element)))
11530     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG_1;
11531
11532   CopyElementPropertiesToEditor(properties_element);
11533
11534   UnmapLevelEditorFieldGadgets();
11535   UnmapLevelEditorToolboxDrawingGadgets();
11536   UnmapLevelEditorToolboxCustomGadgets();
11537
11538   MapLevelEditorToolboxCustomGadgetsIfNeeded();
11539
11540   SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
11541   ClearField();
11542
11543   DrawText(sx, sy, text, font2_nr);
11544
11545   FrameCounter = 0;     // restart animation frame counter
11546
11547   DrawElementBorder(SX + x1, SY + y1, TILEX, TILEY, FALSE);
11548   DrawEditorElementAnimation(SX + x1, SY + y1);
11549   DrawEditorElementName(x2, y2, font_nr);
11550
11551   DrawPropertiesTabulatorGadgets();
11552
11553   if (edit_mode_properties == ED_MODE_PROPERTIES_INFO)
11554     DrawPropertiesInfo();
11555   else if (edit_mode_properties == ED_MODE_PROPERTIES_CHANGE)
11556     DrawPropertiesChange();
11557   else  // (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG[_1|_2])
11558     DrawPropertiesConfig();
11559 }
11560
11561 static void DrawPaletteWindow(void)
11562 {
11563   int i;
11564
11565   UnmapLevelEditorFieldGadgets();
11566
11567   SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
11568   ClearField();
11569
11570   // map buttons to select elements
11571   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
11572     MapGadget(level_editor_gadget[GADGET_ID_ELEMENTLIST_FIRST + i]);
11573   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL]);
11574   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_UP]);
11575   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_DOWN]);
11576 }
11577
11578 static void UpdateCustomElementGraphicGadgets(void)
11579 {
11580   int i;
11581
11582   InitElementPropertiesGfxElement();
11583
11584   ModifyEditorElementList();
11585   RedrawDrawingElements();
11586
11587   // force redraw of all mapped drawing area gadgets
11588   for (i = 0; i < ED_NUM_DRAWING_AREAS; i++)
11589   {
11590     struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[i].gadget_id];
11591
11592     if (gi->mapped)
11593       MapDrawingArea(i);
11594   }
11595 }
11596
11597 static int getOpenDirectionFromTube(int element)
11598 {
11599   switch (element)
11600   {
11601     case EL_TUBE_LEFT_UP:               return (MV_LEFT | MV_UP);
11602     case EL_TUBE_LEFT_DOWN:             return (MV_LEFT | MV_DOWN);
11603     case EL_TUBE_RIGHT_UP:              return (MV_RIGHT | MV_UP);
11604     case EL_TUBE_RIGHT_DOWN:            return (MV_RIGHT | MV_DOWN);
11605     case EL_TUBE_HORIZONTAL:            return (MV_HORIZONTAL);
11606     case EL_TUBE_HORIZONTAL_UP:         return (MV_HORIZONTAL | MV_UP);
11607     case EL_TUBE_HORIZONTAL_DOWN:       return (MV_HORIZONTAL | MV_DOWN);
11608     case EL_TUBE_VERTICAL:              return (MV_VERTICAL);
11609     case EL_TUBE_VERTICAL_LEFT:         return (MV_VERTICAL | MV_LEFT);
11610     case EL_TUBE_VERTICAL_RIGHT:        return (MV_VERTICAL | MV_RIGHT);
11611     case EL_TUBE_ANY:                   return (MV_ANY_DIRECTION);
11612   }
11613
11614   return MV_NONE;
11615 }
11616
11617 static int getTubeFromOpenDirection(int direction)
11618 {
11619   switch (direction)
11620   {
11621     case (MV_LEFT | MV_UP):             return EL_TUBE_LEFT_UP;
11622     case (MV_LEFT | MV_DOWN):           return EL_TUBE_LEFT_DOWN;
11623     case (MV_RIGHT | MV_UP):            return EL_TUBE_RIGHT_UP;
11624     case (MV_RIGHT | MV_DOWN):          return EL_TUBE_RIGHT_DOWN;
11625     case (MV_HORIZONTAL):               return EL_TUBE_HORIZONTAL;
11626     case (MV_HORIZONTAL | MV_UP):       return EL_TUBE_HORIZONTAL_UP;
11627     case (MV_HORIZONTAL | MV_DOWN):     return EL_TUBE_HORIZONTAL_DOWN;
11628     case (MV_VERTICAL):                 return EL_TUBE_VERTICAL;
11629     case (MV_VERTICAL | MV_LEFT):       return EL_TUBE_VERTICAL_LEFT;
11630     case (MV_VERTICAL | MV_RIGHT):      return EL_TUBE_VERTICAL_RIGHT;
11631     case (MV_ANY_DIRECTION):            return EL_TUBE_ANY;
11632
11633     // if only one direction, fall back to simple tube with that direction
11634     case (MV_LEFT):                     return EL_TUBE_HORIZONTAL;
11635     case (MV_RIGHT):                    return EL_TUBE_HORIZONTAL;
11636     case (MV_UP):                       return EL_TUBE_VERTICAL;
11637     case (MV_DOWN):                     return EL_TUBE_VERTICAL;
11638   }
11639
11640   return EL_EMPTY;
11641 }
11642
11643 static int getTubeFromOpenDirectionNotEmpty(int direction, int element_old)
11644 {
11645   int element_new = getTubeFromOpenDirection(direction);
11646
11647   return (element_new != EL_EMPTY ? element_new : element_old);
11648 }
11649
11650 static int getOpenDirectionFromBelt(int element)
11651 {
11652   int belt_dir = getBeltDirFromBeltElement(element);
11653
11654   return (belt_dir == MV_LEFT ? MV_RIGHT :
11655           belt_dir == MV_RIGHT ? MV_LEFT :
11656           belt_dir == MV_NONE ? MV_HORIZONTAL : belt_dir);
11657 }
11658
11659 static int getBeltFromNrAndOpenDirection(int nr, int direction)
11660 {
11661   int belt_dir = (direction == MV_LEFT ? MV_RIGHT :
11662                   direction == MV_RIGHT ? MV_LEFT :
11663                   direction == MV_HORIZONTAL ? MV_NONE : direction);
11664
11665   if (direction == MV_NONE)
11666     return EL_EMPTY;
11667
11668   return getBeltElementFromBeltNrAndBeltDir(nr, belt_dir);
11669 }
11670
11671 static int getBeltFromNrAndOpenDirectionNotEmpty(int nr, int direction,
11672                                                  int element_old)
11673 {
11674   int element_new = getBeltFromNrAndOpenDirection(nr, direction);
11675
11676   return (element_new != EL_EMPTY ? element_new : element_old);
11677 }
11678
11679 static int getOpenDirectionFromPool(int element)
11680 {
11681   switch (element)
11682   {
11683     case EL_ACID_POOL_TOPLEFT:          return (MV_DOWN | MV_RIGHT);
11684     case EL_ACID_POOL_TOPRIGHT:         return (MV_DOWN | MV_LEFT);
11685     case EL_ACID_POOL_BOTTOMLEFT:       return (MV_UP | MV_RIGHT);
11686     case EL_ACID_POOL_BOTTOMRIGHT:      return (MV_UP | MV_LEFT);
11687     case EL_ACID_POOL_BOTTOM:           return (MV_HORIZONTAL | MV_UP);
11688     case EL_ACID:                       return (MV_HORIZONTAL | MV_DOWN);
11689   }
11690
11691   return MV_NONE;
11692 }
11693
11694 static int getPoolFromOpenDirection(int direction)
11695 {
11696   switch (direction)
11697   {
11698     case (MV_DOWN | MV_RIGHT):          return EL_ACID_POOL_TOPLEFT;
11699     case (MV_DOWN | MV_LEFT):           return EL_ACID_POOL_TOPRIGHT;
11700     case (MV_UP | MV_RIGHT):            return EL_ACID_POOL_BOTTOMLEFT;
11701     case (MV_UP | MV_LEFT):             return EL_ACID_POOL_BOTTOMRIGHT;
11702     case (MV_HORIZONTAL | MV_UP):       return EL_ACID_POOL_BOTTOM;
11703     case (MV_HORIZONTAL | MV_DOWN):     return EL_ACID;
11704   }
11705
11706   return EL_EMPTY;
11707 }
11708
11709 static int getPoolFromOpenDirectionExt(int direction, int help_element)
11710 {
11711   int element = getPoolFromOpenDirection(direction);
11712   int help_direction = getOpenDirectionFromPool(help_element);
11713
11714   if (element == EL_EMPTY)
11715   {
11716     int help_direction_vertical = help_direction & MV_VERTICAL;
11717
11718     element = getPoolFromOpenDirection(direction | help_direction_vertical);
11719   }
11720
11721   if (element == EL_EMPTY)
11722   {
11723     int help_direction_horizontal = help_direction & MV_HORIZONTAL;
11724
11725     element = getPoolFromOpenDirection(direction | help_direction_horizontal);
11726   }
11727
11728   return element;
11729 }
11730
11731 static int getPoolFromOpenDirectionNotEmpty(int direction, int element_old)
11732 {
11733   int element_new = getPoolFromOpenDirectionExt(direction, element_old);
11734
11735   return (element_new != EL_EMPTY ? element_new : element_old);
11736 }
11737
11738 static int getOpenDirectionFromPillar(int element)
11739 {
11740   switch (element)
11741   {
11742     case EL_EMC_WALL_1:                 return (MV_DOWN);
11743     case EL_EMC_WALL_2:                 return (MV_VERTICAL);
11744     case EL_EMC_WALL_3:                 return (MV_UP);
11745   }
11746
11747   return MV_NONE;
11748 }
11749
11750 static int getPillarFromOpenDirection(int direction)
11751 {
11752   switch (direction)
11753   {
11754     case (MV_DOWN):                     return EL_EMC_WALL_1;
11755     case (MV_VERTICAL):                 return EL_EMC_WALL_2;
11756     case (MV_UP):                       return EL_EMC_WALL_3;
11757   }
11758
11759   return EL_EMPTY;
11760 }
11761
11762 static int getPillarFromOpenDirectionNotEmpty(int direction, int element_old)
11763 {
11764   int element_new = getPillarFromOpenDirection(direction);
11765
11766   return (element_new != EL_EMPTY ? element_new : element_old);
11767 }
11768
11769 static int getOpenDirectionFromSteel2(int element)
11770 {
11771   switch (element)
11772   {
11773     case EL_DC_STEELWALL_2_LEFT:        return (MV_RIGHT);
11774     case EL_DC_STEELWALL_2_RIGHT:       return (MV_LEFT);
11775     case EL_DC_STEELWALL_2_TOP:         return (MV_DOWN);
11776     case EL_DC_STEELWALL_2_BOTTOM:      return (MV_UP);
11777     case EL_DC_STEELWALL_2_HORIZONTAL:  return (MV_HORIZONTAL);
11778     case EL_DC_STEELWALL_2_VERTICAL:    return (MV_VERTICAL);
11779     case EL_DC_STEELWALL_2_MIDDLE:      return (MV_ANY_DIRECTION);
11780     case EL_DC_STEELWALL_2_SINGLE:      return (MV_NONE);
11781   }
11782
11783   return MV_NONE;
11784 }
11785
11786 static int getSteel2FromOpenDirection(int direction)
11787 {
11788   switch (direction)
11789   {
11790     case (MV_RIGHT):                    return EL_DC_STEELWALL_2_LEFT;
11791     case (MV_LEFT):                     return EL_DC_STEELWALL_2_RIGHT;
11792     case (MV_DOWN):                     return EL_DC_STEELWALL_2_TOP;
11793     case (MV_UP):                       return EL_DC_STEELWALL_2_BOTTOM;
11794     case (MV_HORIZONTAL):               return EL_DC_STEELWALL_2_HORIZONTAL;
11795     case (MV_VERTICAL):                 return EL_DC_STEELWALL_2_VERTICAL;
11796     case (MV_ANY_DIRECTION):            return EL_DC_STEELWALL_2_MIDDLE;
11797     case (MV_NONE):                     return EL_DC_STEELWALL_2_SINGLE;
11798   }
11799
11800   return EL_EMPTY;
11801 }
11802
11803 static int getSteel2FromOpenDirectionNotEmpty(int direction, int element_old)
11804 {
11805   int element_new = getSteel2FromOpenDirection(direction);
11806
11807   return (element_new != EL_EMPTY ? element_new : element_old);
11808 }
11809
11810 static int getOpenDirectionFromChip(int element)
11811 {
11812   switch (element)
11813   {
11814     case EL_SP_CHIP_SINGLE:             return (MV_NONE);
11815     case EL_SP_CHIP_LEFT:               return (MV_RIGHT);
11816     case EL_SP_CHIP_RIGHT:              return (MV_LEFT);
11817     case EL_SP_CHIP_TOP:                return (MV_DOWN);
11818     case EL_SP_CHIP_BOTTOM:             return (MV_UP);
11819   }
11820
11821   return MV_NONE;
11822 }
11823
11824 static int getChipFromOpenDirection(int direction)
11825 {
11826   switch (direction)
11827   {
11828     case (MV_NONE):                     return EL_SP_CHIP_SINGLE;
11829     case (MV_LEFT):                     return EL_SP_CHIP_RIGHT;
11830     case (MV_RIGHT):                    return EL_SP_CHIP_LEFT;
11831     case (MV_UP):                       return EL_SP_CHIP_BOTTOM;
11832     case (MV_DOWN):                     return EL_SP_CHIP_TOP;
11833   }
11834
11835   return EL_EMPTY;
11836 }
11837
11838 static int getChipFromOpenDirectionNotEmpty(int direction, int element_old)
11839 {
11840   int element_new = getChipFromOpenDirection(direction);
11841
11842   return (element_new != EL_EMPTY ? element_new : element_old);
11843 }
11844
11845 static int getClosedTube(int x, int y)
11846 {
11847   struct XY *xy = xy_directions;
11848   int element_old = IntelliDrawBuffer[x][y];
11849   int direction_old = getOpenDirectionFromTube(element_old);
11850   int direction_new = MV_NONE;
11851   int i;
11852
11853   for (i = 0; i < NUM_DIRECTIONS; i++)
11854   {
11855     int xx = x + xy[i].x;
11856     int yy = y + xy[i].y;
11857     int dir = MV_DIR_FROM_BIT(i);
11858     int dir_opposite = MV_DIR_OPPOSITE(dir);
11859
11860     if (IN_LEV_FIELD(xx, yy) && IS_TUBE(IntelliDrawBuffer[xx][yy]) &&
11861         (direction_old & dir) &&
11862         (getOpenDirectionFromTube(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11863       direction_new |= dir;
11864   }
11865
11866   return getTubeFromOpenDirectionNotEmpty(direction_new, element_old);
11867 }
11868
11869 static int getClosedBelt(int x, int y)
11870 {
11871   struct XY *xy = xy_directions;
11872   int element_old = IntelliDrawBuffer[x][y];
11873   int nr = getBeltNrFromBeltElement(element_old);
11874   int direction_old = getOpenDirectionFromBelt(element_old);
11875   int direction_new = MV_NONE;
11876   int i;
11877
11878   for (i = MV_BIT_LEFT; i <= MV_BIT_RIGHT; i++)
11879   {
11880     int xx = x + xy[i].x;
11881     int yy = y + xy[i].y;
11882     int dir = MV_DIR_FROM_BIT(i);
11883     int dir_opposite = MV_DIR_OPPOSITE(dir);
11884
11885     if (IN_LEV_FIELD(xx, yy) && IS_BELT(IntelliDrawBuffer[xx][yy]) &&
11886         (direction_old & dir) &&
11887         (getOpenDirectionFromBelt(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11888       direction_new |= dir;
11889   }
11890
11891   return getBeltFromNrAndOpenDirection(nr, direction_new);
11892 }
11893
11894 static int getClosedPool(int x, int y)
11895 {
11896   struct XY *xy = xy_directions;
11897   int element_old = IntelliDrawBuffer[x][y];
11898   int direction_old = getOpenDirectionFromPool(element_old);
11899   int direction_new = MV_NONE;
11900   int i;
11901
11902   for (i = 0; i < NUM_DIRECTIONS; i++)
11903   {
11904     int xx = x + xy[i].x;
11905     int yy = y + xy[i].y;
11906     int dir = MV_DIR_FROM_BIT(i);
11907     int dir_opposite = MV_DIR_OPPOSITE(dir);
11908
11909     if (IN_LEV_FIELD(xx, yy) &&
11910         IS_ACID_POOL_OR_ACID(IntelliDrawBuffer[xx][yy]) &&
11911         (direction_old & dir) &&
11912         (getOpenDirectionFromPool(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11913       direction_new |= dir;
11914   }
11915
11916   return getPoolFromOpenDirectionNotEmpty(direction_new, element_old);
11917 }
11918
11919 static int getClosedPillar(int x, int y)
11920 {
11921   struct XY *xy = xy_directions;
11922   int element_old = IntelliDrawBuffer[x][y];
11923   int direction_old = getOpenDirectionFromPillar(element_old);
11924   int direction_new = MV_NONE;
11925   int i;
11926
11927   for (i = MV_BIT_UP; i <= MV_BIT_DOWN; i++)
11928   {
11929     int xx = x + xy[i].x;
11930     int yy = y + xy[i].y;
11931     int dir = MV_DIR_FROM_BIT(i);
11932     int dir_opposite = MV_DIR_OPPOSITE(dir);
11933
11934     if (IN_LEV_FIELD(xx, yy) && IS_EMC_PILLAR(IntelliDrawBuffer[xx][yy]) &&
11935         (direction_old & dir) &&
11936         (getOpenDirectionFromPillar(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11937       direction_new |= dir;
11938   }
11939
11940   return getPillarFromOpenDirectionNotEmpty(direction_new, element_old);
11941 }
11942
11943 static int getClosedSteel2(int x, int y)
11944 {
11945   struct XY *xy = xy_directions;
11946   int element_old = IntelliDrawBuffer[x][y];
11947   int direction_old = getOpenDirectionFromSteel2(element_old);
11948   int direction_new = MV_NONE;
11949   int i;
11950
11951   for (i = 0; i < NUM_DIRECTIONS; i++)
11952   {
11953     int xx = x + xy[i].x;
11954     int yy = y + xy[i].y;
11955     int dir = MV_DIR_FROM_BIT(i);
11956     int dir_opposite = MV_DIR_OPPOSITE(dir);
11957
11958     if (IN_LEV_FIELD(xx, yy) && IS_DC_STEELWALL_2(IntelliDrawBuffer[xx][yy]) &&
11959         (direction_old & dir) &&
11960         (getOpenDirectionFromSteel2(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11961       direction_new |= dir;
11962   }
11963
11964   return getSteel2FromOpenDirectionNotEmpty(direction_new, element_old);
11965 }
11966
11967 static int getClosedChip(int x, int y)
11968 {
11969   struct XY *xy = xy_directions;
11970   int element_old = IntelliDrawBuffer[x][y];
11971   int direction_old = getOpenDirectionFromChip(element_old);
11972   int direction_new = MV_NONE;
11973   int i;
11974
11975   for (i = 0; i < NUM_DIRECTIONS; i++)
11976   {
11977     int xx = x + xy[i].x;
11978     int yy = y + xy[i].y;
11979     int dir = MV_DIR_FROM_BIT(i);
11980     int dir_opposite = MV_DIR_OPPOSITE(dir);
11981
11982     if (IN_LEV_FIELD(xx, yy) && IS_SP_CHIP(IntelliDrawBuffer[xx][yy]) &&
11983         (direction_old & dir) &&
11984         (getOpenDirectionFromChip(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11985       direction_new |= dir;
11986   }
11987
11988   return getChipFromOpenDirectionNotEmpty(direction_new, element_old);
11989 }
11990
11991 static void SetElementSimpleExt(int x, int y, int dx, int dy, int element,
11992                                 boolean change_level)
11993 {
11994   int sx = x - level_xpos;
11995   int sy = y - level_ypos;
11996   int old_element = Tile[x][y];
11997   int new_element = element;
11998   unsigned int new_bitmask = (getDrawModeHiRes() ? (dx + 1) << (dy * 2) : 0x0f);
11999   boolean draw_masked = FALSE;
12000
12001   if (IS_MM_WALL_EDITOR(element))
12002   {
12003     element = map_mm_wall_element_editor(element) | new_bitmask;
12004
12005     if (IS_MM_WALL(old_element))
12006       element |= MM_WALL_BITS(old_element);
12007
12008     if (!change_level)
12009       draw_masked = TRUE;
12010   }
12011   else if (IS_MM_WALL(old_element) && element == EL_EMPTY)
12012   {
12013     int element_changed = old_element & ~new_bitmask;
12014
12015     if (MM_WALL_BITS(element_changed) != 0)
12016       element = element_changed;
12017   }
12018
12019   IntelliDrawBuffer[x][y] = element;
12020
12021   if (change_level)
12022     Tile[x][y] = element;
12023
12024   if (IN_ED_FIELD(sx, sy))
12025   {
12026     if (IS_MM_WALL(old_element) && new_element == EL_EMPTY)
12027       DrawSizedWallParts_MM(sx, sy, EL_EMPTY, ed_tilesize, FALSE, new_bitmask);
12028     else if (draw_masked)
12029       DrawEditorElementThruMask(sx, sy, element);
12030     else
12031       DrawEditorElement(sx, sy, element);
12032   }
12033 }
12034
12035 static void SetElementSimple(int x, int y, int element, boolean change_level)
12036 {
12037   SetElementSimpleExt(x, y, 0, 0, element, change_level);
12038 }
12039
12040 static void MergeAndCloseNeighbourElements(int x1, int y1, int *element1,
12041                                            int x2, int y2, int *element2,
12042                                            int (*close_function)(int, int),
12043                                            boolean change_level)
12044 {
12045   // set neighbour elements to newly determined connections
12046   SetElementSimple(x1, y1, *element1, change_level);
12047   SetElementSimple(x2, y2, *element2, change_level);
12048
12049   // remove all open connections of neighbour elements
12050   *element1 = close_function(x1, y1);
12051   *element2 = close_function(x2, y2);
12052
12053   // set neighbour elements to new, minimized connections
12054   SetElementSimple(x1, y1, *element1, change_level);
12055   SetElementSimple(x2, y2, *element2, change_level);
12056 }
12057
12058 static void SetElementIntelliDraw(int x, int y, int dx, int dy, int new_element,
12059                                   boolean change_level, int button)
12060 {
12061   struct XY *xy = xy_directions;
12062   static int last_x = -1;
12063   static int last_y = -1;
12064
12065   if (new_element == EL_UNDEFINED)
12066   {
12067     last_x = -1;
12068     last_y = -1;
12069
12070     return;
12071   }
12072
12073   int old_element = IntelliDrawBuffer[x][y];
12074
12075   if (IS_TUBE(new_element))
12076   {
12077     int last_element_new = EL_UNDEFINED;
12078     int direction = MV_NONE;
12079     int i;
12080
12081     // if old element is of same kind, keep all existing directions
12082     if (IS_TUBE(old_element))
12083       direction |= getOpenDirectionFromTube(old_element);
12084
12085     for (i = 0; i < NUM_DIRECTIONS; i++)
12086     {
12087       int xx = x + xy[i].x;
12088       int yy = y + xy[i].y;
12089
12090       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12091           IS_TUBE(IntelliDrawBuffer[last_x][last_y]))
12092       {
12093         int dir = MV_DIR_FROM_BIT(i);
12094         int dir_opposite = MV_DIR_OPPOSITE(dir);
12095         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12096         int last_direction_old = getOpenDirectionFromTube(last_element_old);
12097         int last_direction_new = last_direction_old | dir_opposite;
12098
12099         last_element_new = getTubeFromOpenDirection(last_direction_new);
12100
12101         direction |= dir;
12102       }
12103     }
12104
12105     new_element = getTubeFromOpenDirectionNotEmpty(direction, new_element);
12106
12107     if (last_element_new != EL_UNDEFINED)
12108       MergeAndCloseNeighbourElements(x, y, &new_element,
12109                                      last_x, last_y, &last_element_new,
12110                                      getClosedTube, change_level);
12111   }
12112   else if (IS_BELT(new_element))
12113   {
12114     int belt_nr = getBeltNrFromBeltElement(new_element);
12115     int last_element_new = EL_UNDEFINED;
12116     int direction = MV_NONE;
12117     int i;
12118
12119     // if old element is of same kind, keep all existing directions
12120     if (IS_BELT(old_element))
12121       direction |= getOpenDirectionFromBelt(old_element);
12122
12123     for (i = MV_BIT_LEFT; i <= MV_BIT_RIGHT; i++)
12124     {
12125       int xx = x + xy[i].x;
12126       int yy = y + xy[i].y;
12127
12128       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12129           IS_BELT(IntelliDrawBuffer[last_x][last_y]))
12130       {
12131         int dir = MV_DIR_FROM_BIT(i);
12132         int dir_opposite = MV_DIR_OPPOSITE(dir);
12133         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12134         int last_belt_nr = getBeltNrFromBeltElement(last_element_old);
12135         int last_direction_old = getOpenDirectionFromBelt(last_element_old);
12136         int last_direction_new = last_direction_old | dir_opposite;
12137
12138         last_element_new = getBeltFromNrAndOpenDirection(last_belt_nr,
12139                                                          last_direction_new);
12140         direction |= dir;
12141       }
12142     }
12143
12144     new_element = getBeltFromNrAndOpenDirectionNotEmpty(belt_nr, direction,
12145                                                         new_element);
12146     if (last_element_new != EL_UNDEFINED)
12147       MergeAndCloseNeighbourElements(x, y, &new_element,
12148                                      last_x, last_y, &last_element_new,
12149                                      getClosedBelt, change_level);
12150   }
12151   else if (IS_ACID_POOL_OR_ACID(new_element))
12152   {
12153     int last_element_new = EL_UNDEFINED;
12154     int direction = MV_NONE;
12155     int i;
12156
12157     // if old element is of same kind, keep all existing directions
12158     if (IS_ACID_POOL_OR_ACID(old_element))
12159       direction |= getOpenDirectionFromPool(old_element);
12160
12161     for (i = 0; i < NUM_DIRECTIONS; i++)
12162     {
12163       int xx = x + xy[i].x;
12164       int yy = y + xy[i].y;
12165
12166       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12167           IS_ACID_POOL_OR_ACID(IntelliDrawBuffer[last_x][last_y]))
12168       {
12169         int dir = MV_DIR_FROM_BIT(i);
12170         int dir_opposite = MV_DIR_OPPOSITE(dir);
12171         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12172         int last_direction_old = getOpenDirectionFromPool(last_element_old);
12173         int last_direction_new = last_direction_old | dir_opposite;
12174
12175         last_element_new = getPoolFromOpenDirection(last_direction_new);
12176
12177         direction |= dir;
12178       }
12179     }
12180
12181     // special corrections needed for intuitively correct acid pool drawing
12182     if (last_element_new == EL_EMPTY)
12183       last_element_new = new_element;
12184     else if (last_element_new != EL_UNDEFINED)
12185       new_element = last_element_new;
12186
12187     new_element = getPoolFromOpenDirectionNotEmpty(direction, new_element);
12188
12189     if (last_element_new != EL_UNDEFINED)
12190       MergeAndCloseNeighbourElements(x, y, &new_element,
12191                                      last_x, last_y, &last_element_new,
12192                                      getClosedPool, change_level);
12193   }
12194   else if (IS_EMC_PILLAR(new_element))
12195   {
12196     int last_element_new = EL_UNDEFINED;
12197     int direction = MV_NONE;
12198     int i;
12199
12200     // if old element is of same kind, keep all existing directions
12201     if (IS_EMC_PILLAR(old_element))
12202       direction |= getOpenDirectionFromPillar(old_element);
12203
12204     for (i = MV_BIT_UP; i <= MV_BIT_DOWN; i++)
12205     {
12206       int xx = x + xy[i].x;
12207       int yy = y + xy[i].y;
12208
12209       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12210           IS_EMC_PILLAR(IntelliDrawBuffer[last_x][last_y]))
12211       {
12212         int dir = MV_DIR_FROM_BIT(i);
12213         int dir_opposite = MV_DIR_OPPOSITE(dir);
12214         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12215         int last_direction_old = getOpenDirectionFromPillar(last_element_old);
12216         int last_direction_new = last_direction_old | dir_opposite;
12217
12218         last_element_new = getPillarFromOpenDirection(last_direction_new);
12219
12220         direction |= dir;
12221       }
12222     }
12223
12224     new_element = getPillarFromOpenDirectionNotEmpty(direction, new_element);
12225
12226     if (last_element_new != EL_UNDEFINED)
12227       MergeAndCloseNeighbourElements(x, y, &new_element,
12228                                      last_x, last_y, &last_element_new,
12229                                      getClosedPillar, change_level);
12230   }
12231   else if (IS_DC_STEELWALL_2(new_element))
12232   {
12233     int last_element_new = EL_UNDEFINED;
12234     int direction = MV_NONE;
12235     int i;
12236
12237     // if old element is of same kind, keep all existing directions
12238     if (IS_DC_STEELWALL_2(old_element))
12239       direction |= getOpenDirectionFromSteel2(old_element);
12240
12241     for (i = 0; i < NUM_DIRECTIONS; i++)
12242     {
12243       int xx = x + xy[i].x;
12244       int yy = y + xy[i].y;
12245
12246       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12247           IS_DC_STEELWALL_2(IntelliDrawBuffer[last_x][last_y]))
12248       {
12249         int dir = MV_DIR_FROM_BIT(i);
12250         int dir_opposite = MV_DIR_OPPOSITE(dir);
12251         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12252         int last_direction_old = getOpenDirectionFromSteel2(last_element_old);
12253         int last_direction_new = last_direction_old | dir_opposite;
12254
12255         last_element_new = getSteel2FromOpenDirection(last_direction_new);
12256
12257         direction |= dir;
12258       }
12259     }
12260
12261     new_element = getSteel2FromOpenDirectionNotEmpty(direction, new_element);
12262
12263     if (last_element_new != EL_UNDEFINED)
12264       MergeAndCloseNeighbourElements(x, y, &new_element,
12265                                      last_x, last_y, &last_element_new,
12266                                      getClosedSteel2, change_level);
12267   }
12268   else if (IS_SP_CHIP(new_element))
12269   {
12270     int last_element_new = EL_UNDEFINED;
12271     int direction = MV_NONE;
12272     int i;
12273
12274     // (do not keep existing directions, regardless of kind of old element)
12275
12276     for (i = 0; i < NUM_DIRECTIONS; i++)
12277     {
12278       int xx = x + xy[i].x;
12279       int yy = y + xy[i].y;
12280
12281       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12282           IS_SP_CHIP(IntelliDrawBuffer[last_x][last_y]))
12283       {
12284         int dir = MV_DIR_FROM_BIT(i);
12285         int dir_opposite = MV_DIR_OPPOSITE(dir);
12286         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12287         int last_direction_old = getOpenDirectionFromChip(last_element_old);
12288         int last_direction_new = last_direction_old | dir_opposite;
12289
12290         if (last_direction_old == MV_NONE)
12291         {
12292           last_element_new = getChipFromOpenDirection(last_direction_new);
12293           direction |= dir;
12294         }
12295         else if (last_direction_old & (dir | dir_opposite))
12296         {
12297           direction |= MV_DIR_OPPOSITE(last_direction_old);
12298         }
12299         else
12300         {
12301           direction |= MV_DIR_OPPOSITE(dir);
12302         }
12303       }
12304     }
12305
12306     new_element = getChipFromOpenDirectionNotEmpty(direction, new_element);
12307
12308     if (last_element_new != EL_UNDEFINED)
12309       MergeAndCloseNeighbourElements(x, y, &new_element,
12310                                      last_x, last_y, &last_element_new,
12311                                      getClosedChip, change_level);
12312   }
12313   else if (IS_SP_HARDWARE_BASE(new_element))
12314   {
12315     int nr = GetSimpleRandom(6);
12316
12317     new_element = (nr == 0 ? EL_SP_HARDWARE_BASE_1 :
12318                    nr == 1 ? EL_SP_HARDWARE_BASE_2 :
12319                    nr == 2 ? EL_SP_HARDWARE_BASE_3 :
12320                    nr == 3 ? EL_SP_HARDWARE_BASE_4 :
12321                    nr == 4 ? EL_SP_HARDWARE_BASE_5 : EL_SP_HARDWARE_BASE_6);
12322   }
12323   else if (new_element == EL_SP_HARDWARE_GREEN ||
12324            new_element == EL_SP_HARDWARE_BLUE ||
12325            new_element == EL_SP_HARDWARE_RED)
12326   {
12327     int nr = GetSimpleRandom(3);
12328
12329     new_element = (nr == 0 ? EL_SP_HARDWARE_GREEN :
12330                    nr == 1 ? EL_SP_HARDWARE_BLUE : EL_SP_HARDWARE_RED);
12331   }
12332   else if (IS_GROUP_ELEMENT(new_element))
12333   {
12334     boolean connected_drawing = FALSE;
12335     int i;
12336
12337     for (i = 0; i < NUM_DIRECTIONS; i++)
12338     {
12339       int xx = x + xy[i].x;
12340       int yy = y + xy[i].y;
12341
12342       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12343           IS_IN_GROUP_EL(IntelliDrawBuffer[last_x][last_y], new_element))
12344         connected_drawing = TRUE;
12345     }
12346
12347     if (!connected_drawing)
12348       ResolveGroupElement(new_element);
12349
12350     new_element = GetElementFromGroupElement(new_element);
12351   }
12352   else if (IS_BELT_SWITCH(old_element))
12353   {
12354     int belt_nr = getBeltNrFromBeltSwitchElement(old_element);
12355     int belt_dir = getBeltDirFromBeltSwitchElement(old_element);
12356
12357     belt_dir = (belt_dir == MV_LEFT ? MV_NONE :
12358                 belt_dir == MV_NONE ? MV_RIGHT : MV_LEFT);
12359
12360     new_element = getBeltSwitchElementFromBeltNrAndBeltDir(belt_nr, belt_dir);
12361   }
12362   else
12363   {
12364     static int swappable_elements[][2] =
12365     {
12366       { EL_EXIT_CLOSED,                 EL_EXIT_OPEN                    },
12367       { EL_DYNAMITE,                    EL_DYNAMITE_ACTIVE              },
12368       { EL_EM_DYNAMITE,                 EL_EM_DYNAMITE_ACTIVE           },
12369       { EL_QUICKSAND_EMPTY,             EL_QUICKSAND_FULL               },
12370       { EL_EMERALD,                     EL_WALL_EMERALD                 },
12371       { EL_EMERALD_YELLOW,              EL_WALL_EMERALD_YELLOW          },
12372       { EL_EMERALD_RED,                 EL_WALL_EMERALD_RED             },
12373       { EL_EMERALD_PURPLE,              EL_WALL_EMERALD_PURPLE          },
12374       { EL_DIAMOND,                     EL_WALL_DIAMOND                 },
12375       { EL_BD_DIAMOND,                  EL_WALL_BD_DIAMOND              },
12376       { EL_GATE_1,                      EL_GATE_1_GRAY                  },
12377       { EL_GATE_2,                      EL_GATE_2_GRAY                  },
12378       { EL_GATE_3,                      EL_GATE_3_GRAY                  },
12379       { EL_GATE_4,                      EL_GATE_4_GRAY                  },
12380       { EL_EM_GATE_1,                   EL_EM_GATE_1_GRAY               },
12381       { EL_EM_GATE_2,                   EL_EM_GATE_2_GRAY               },
12382       { EL_EM_GATE_3,                   EL_EM_GATE_3_GRAY               },
12383       { EL_EM_GATE_4,                   EL_EM_GATE_4_GRAY               },
12384       { EL_EMC_GATE_5,                  EL_EMC_GATE_5_GRAY              },
12385       { EL_EMC_GATE_6,                  EL_EMC_GATE_6_GRAY              },
12386       { EL_EMC_GATE_7,                  EL_EMC_GATE_7_GRAY              },
12387       { EL_EMC_GATE_8,                  EL_EMC_GATE_8_GRAY              },
12388       { EL_DC_GATE_WHITE,               EL_DC_GATE_WHITE_GRAY           },
12389       { EL_TIME_ORB_EMPTY,              EL_TIME_ORB_FULL                },
12390       { EL_LAMP,                        EL_LAMP_ACTIVE                  },
12391       { EL_SOKOBAN_FIELD_EMPTY,         EL_SOKOBAN_FIELD_FULL           },
12392       { EL_SP_BASE,                     EL_SP_BUGGY_BASE                },
12393       { EL_PEARL,                       EL_WALL_PEARL                   },
12394       { EL_CRYSTAL,                     EL_WALL_CRYSTAL                 },
12395       { EL_TIMEGATE_CLOSED,             EL_TIMEGATE_OPEN                },
12396       { EL_SWITCHGATE_CLOSED,           EL_SWITCHGATE_OPEN              },
12397       { EL_SWITCHGATE_SWITCH_UP,        EL_SWITCHGATE_SWITCH_DOWN       },
12398       { EL_DC_SWITCHGATE_SWITCH_UP,     EL_DC_SWITCHGATE_SWITCH_DOWN    },
12399       { EL_LIGHT_SWITCH,                EL_LIGHT_SWITCH_ACTIVE          },
12400       { EL_LANDMINE,                    EL_DC_LANDMINE                  },
12401       { EL_SHIELD_NORMAL,               EL_SHIELD_DEADLY                },
12402       { EL_STEEL_EXIT_CLOSED,           EL_STEEL_EXIT_OPEN              },
12403       { EL_EM_EXIT_CLOSED,              EL_EM_EXIT_OPEN                 },
12404       { EL_EM_STEEL_EXIT_CLOSED,        EL_EM_STEEL_EXIT_OPEN           },
12405       { EL_QUICKSAND_FAST_EMPTY,        EL_QUICKSAND_FAST_FULL          },
12406       { EL_MM_EXIT_CLOSED,              EL_MM_EXIT_OPEN                 },
12407       { EL_MM_FUSE,                     EL_MM_FUSE_ACTIVE               },
12408       { EL_MM_LIGHTBULB,                EL_MM_LIGHTBULB_ACTIVE          },
12409       { EL_MM_FUEL_EMPTY,               EL_MM_FUEL_FULL                 },
12410
12411       { -1,                             -1                              },
12412     };
12413     static int rotatable_elements_4[][4] =
12414     {
12415       {
12416         EL_BUG_UP,
12417         EL_BUG_RIGHT,
12418         EL_BUG_DOWN,
12419         EL_BUG_LEFT
12420       },
12421       {
12422         EL_SPACESHIP_UP,
12423         EL_SPACESHIP_RIGHT,
12424         EL_SPACESHIP_DOWN,
12425         EL_SPACESHIP_LEFT
12426       },
12427       {
12428         EL_BD_BUTTERFLY_UP,
12429         EL_BD_BUTTERFLY_RIGHT,
12430         EL_BD_BUTTERFLY_DOWN,
12431         EL_BD_BUTTERFLY_LEFT
12432       },
12433       {
12434         EL_BD_FIREFLY_UP,
12435         EL_BD_FIREFLY_RIGHT,
12436         EL_BD_FIREFLY_DOWN,
12437         EL_BD_FIREFLY_LEFT
12438       },
12439       {
12440         EL_PACMAN_UP,
12441         EL_PACMAN_RIGHT,
12442         EL_PACMAN_DOWN,
12443         EL_PACMAN_LEFT
12444       },
12445       {
12446         EL_YAMYAM_UP,
12447         EL_YAMYAM_RIGHT,
12448         EL_YAMYAM_DOWN,
12449         EL_YAMYAM_LEFT
12450       },
12451       {
12452         EL_ARROW_UP,
12453         EL_ARROW_RIGHT,
12454         EL_ARROW_DOWN,
12455         EL_ARROW_LEFT
12456       },
12457       {
12458         EL_SP_PORT_UP,
12459         EL_SP_PORT_RIGHT,
12460         EL_SP_PORT_DOWN,
12461         EL_SP_PORT_LEFT
12462       },
12463       {
12464         EL_SP_GRAVITY_PORT_UP,
12465         EL_SP_GRAVITY_PORT_RIGHT,
12466         EL_SP_GRAVITY_PORT_DOWN,
12467         EL_SP_GRAVITY_PORT_LEFT
12468       },
12469       {
12470         EL_SP_GRAVITY_ON_PORT_UP,
12471         EL_SP_GRAVITY_ON_PORT_RIGHT,
12472         EL_SP_GRAVITY_ON_PORT_DOWN,
12473         EL_SP_GRAVITY_ON_PORT_LEFT
12474       },
12475       {
12476         EL_SP_GRAVITY_OFF_PORT_UP,
12477         EL_SP_GRAVITY_OFF_PORT_RIGHT,
12478         EL_SP_GRAVITY_OFF_PORT_DOWN,
12479         EL_SP_GRAVITY_OFF_PORT_LEFT
12480       },
12481       {
12482         EL_MOLE_UP,
12483         EL_MOLE_RIGHT,
12484         EL_MOLE_DOWN,
12485         EL_MOLE_LEFT
12486       },
12487       {
12488         EL_BALLOON_SWITCH_UP,
12489         EL_BALLOON_SWITCH_RIGHT,
12490         EL_BALLOON_SWITCH_DOWN,
12491         EL_BALLOON_SWITCH_LEFT
12492       },
12493       {
12494         EL_MM_MCDUFFIN_UP,
12495         EL_MM_MCDUFFIN_RIGHT,
12496         EL_MM_MCDUFFIN_DOWN,
12497         EL_MM_MCDUFFIN_LEFT
12498       },
12499       {
12500         EL_MM_MIRROR_FIXED_1,
12501         EL_MM_MIRROR_FIXED_4,
12502         EL_MM_MIRROR_FIXED_3,
12503         EL_MM_MIRROR_FIXED_2
12504       },
12505       {
12506         EL_MM_STEEL_GRID_FIXED_1,
12507         EL_MM_STEEL_GRID_FIXED_4,
12508         EL_MM_STEEL_GRID_FIXED_2,
12509         EL_MM_STEEL_GRID_FIXED_3
12510       },
12511       {
12512         EL_MM_WOODEN_GRID_FIXED_1,
12513         EL_MM_WOODEN_GRID_FIXED_4,
12514         EL_MM_WOODEN_GRID_FIXED_2,
12515         EL_MM_WOODEN_GRID_FIXED_3
12516       },
12517       {
12518         EL_MM_POLARIZER_CROSS_1,
12519         EL_MM_POLARIZER_CROSS_4,
12520         EL_MM_POLARIZER_CROSS_3,
12521         EL_MM_POLARIZER_CROSS_2
12522       },
12523       {
12524         EL_MM_PACMAN_UP,
12525         EL_MM_PACMAN_RIGHT,
12526         EL_MM_PACMAN_DOWN,
12527         EL_MM_PACMAN_LEFT
12528       },
12529       {
12530         EL_DF_LASER_UP,
12531         EL_DF_LASER_RIGHT,
12532         EL_DF_LASER_DOWN,
12533         EL_DF_LASER_LEFT
12534       },
12535       {
12536         EL_DF_RECEIVER_UP,
12537         EL_DF_RECEIVER_RIGHT,
12538         EL_DF_RECEIVER_DOWN,
12539         EL_DF_RECEIVER_LEFT
12540       },
12541       {
12542         EL_DF_SLOPE_1,
12543         EL_DF_SLOPE_4,
12544         EL_DF_SLOPE_3,
12545         EL_DF_SLOPE_2
12546       },
12547
12548       {
12549         -1,
12550       },
12551     };
12552     static int rotatable_elements_8[][8] =
12553     {
12554       {
12555         EL_DF_STEEL_GRID_FIXED_1,
12556         EL_DF_STEEL_GRID_FIXED_8,
12557         EL_DF_STEEL_GRID_FIXED_7,
12558         EL_DF_STEEL_GRID_FIXED_6,
12559         EL_DF_STEEL_GRID_FIXED_5,
12560         EL_DF_STEEL_GRID_FIXED_4,
12561         EL_DF_STEEL_GRID_FIXED_3,
12562         EL_DF_STEEL_GRID_FIXED_2
12563       },
12564       {
12565         EL_DF_WOODEN_GRID_FIXED_1,
12566         EL_DF_WOODEN_GRID_FIXED_8,
12567         EL_DF_WOODEN_GRID_FIXED_7,
12568         EL_DF_WOODEN_GRID_FIXED_6,
12569         EL_DF_WOODEN_GRID_FIXED_5,
12570         EL_DF_WOODEN_GRID_FIXED_4,
12571         EL_DF_WOODEN_GRID_FIXED_3,
12572         EL_DF_WOODEN_GRID_FIXED_2
12573       },
12574       {
12575         EL_DF_STEEL_GRID_ROTATING_1,
12576         EL_DF_STEEL_GRID_ROTATING_8,
12577         EL_DF_STEEL_GRID_ROTATING_7,
12578         EL_DF_STEEL_GRID_ROTATING_6,
12579         EL_DF_STEEL_GRID_ROTATING_5,
12580         EL_DF_STEEL_GRID_ROTATING_4,
12581         EL_DF_STEEL_GRID_ROTATING_3,
12582         EL_DF_STEEL_GRID_ROTATING_2
12583       },
12584       {
12585         EL_DF_WOODEN_GRID_ROTATING_1,
12586         EL_DF_WOODEN_GRID_ROTATING_8,
12587         EL_DF_WOODEN_GRID_ROTATING_7,
12588         EL_DF_WOODEN_GRID_ROTATING_6,
12589         EL_DF_WOODEN_GRID_ROTATING_5,
12590         EL_DF_WOODEN_GRID_ROTATING_4,
12591         EL_DF_WOODEN_GRID_ROTATING_3,
12592         EL_DF_WOODEN_GRID_ROTATING_2
12593       },
12594
12595       {
12596         -1,
12597       },
12598     };
12599     static int rotatable_elements_16[][16] =
12600     {
12601       {
12602         EL_MM_MIRROR_1,
12603         EL_MM_MIRROR_16,
12604         EL_MM_MIRROR_15,
12605         EL_MM_MIRROR_14,
12606         EL_MM_MIRROR_13,
12607         EL_MM_MIRROR_12,
12608         EL_MM_MIRROR_11,
12609         EL_MM_MIRROR_10,
12610         EL_MM_MIRROR_9,
12611         EL_MM_MIRROR_8,
12612         EL_MM_MIRROR_7,
12613         EL_MM_MIRROR_6,
12614         EL_MM_MIRROR_5,
12615         EL_MM_MIRROR_4,
12616         EL_MM_MIRROR_3,
12617         EL_MM_MIRROR_2
12618       },
12619       {
12620         EL_MM_TELEPORTER_5,
12621         EL_MM_TELEPORTER_4,
12622         EL_MM_TELEPORTER_3,
12623         EL_MM_TELEPORTER_2,
12624         EL_MM_TELEPORTER_1,
12625         EL_MM_TELEPORTER_16,
12626         EL_MM_TELEPORTER_15,
12627         EL_MM_TELEPORTER_14,
12628         EL_MM_TELEPORTER_13,
12629         EL_MM_TELEPORTER_12,
12630         EL_MM_TELEPORTER_11,
12631         EL_MM_TELEPORTER_10,
12632         EL_MM_TELEPORTER_9,
12633         EL_MM_TELEPORTER_8,
12634         EL_MM_TELEPORTER_7,
12635         EL_MM_TELEPORTER_6
12636       },
12637       {
12638         EL_MM_TELEPORTER_RED_5,
12639         EL_MM_TELEPORTER_RED_4,
12640         EL_MM_TELEPORTER_RED_3,
12641         EL_MM_TELEPORTER_RED_2,
12642         EL_MM_TELEPORTER_RED_1,
12643         EL_MM_TELEPORTER_RED_16,
12644         EL_MM_TELEPORTER_RED_15,
12645         EL_MM_TELEPORTER_RED_14,
12646         EL_MM_TELEPORTER_RED_13,
12647         EL_MM_TELEPORTER_RED_12,
12648         EL_MM_TELEPORTER_RED_11,
12649         EL_MM_TELEPORTER_RED_10,
12650         EL_MM_TELEPORTER_RED_9,
12651         EL_MM_TELEPORTER_RED_8,
12652         EL_MM_TELEPORTER_RED_7,
12653         EL_MM_TELEPORTER_RED_6
12654       },
12655       {
12656         EL_MM_TELEPORTER_YELLOW_5,
12657         EL_MM_TELEPORTER_YELLOW_4,
12658         EL_MM_TELEPORTER_YELLOW_3,
12659         EL_MM_TELEPORTER_YELLOW_2,
12660         EL_MM_TELEPORTER_YELLOW_1,
12661         EL_MM_TELEPORTER_YELLOW_16,
12662         EL_MM_TELEPORTER_YELLOW_15,
12663         EL_MM_TELEPORTER_YELLOW_14,
12664         EL_MM_TELEPORTER_YELLOW_13,
12665         EL_MM_TELEPORTER_YELLOW_12,
12666         EL_MM_TELEPORTER_YELLOW_11,
12667         EL_MM_TELEPORTER_YELLOW_10,
12668         EL_MM_TELEPORTER_YELLOW_9,
12669         EL_MM_TELEPORTER_YELLOW_8,
12670         EL_MM_TELEPORTER_YELLOW_7,
12671         EL_MM_TELEPORTER_YELLOW_6
12672       },
12673       {
12674         EL_MM_TELEPORTER_GREEN_5,
12675         EL_MM_TELEPORTER_GREEN_4,
12676         EL_MM_TELEPORTER_GREEN_3,
12677         EL_MM_TELEPORTER_GREEN_2,
12678         EL_MM_TELEPORTER_GREEN_1,
12679         EL_MM_TELEPORTER_GREEN_16,
12680         EL_MM_TELEPORTER_GREEN_15,
12681         EL_MM_TELEPORTER_GREEN_14,
12682         EL_MM_TELEPORTER_GREEN_13,
12683         EL_MM_TELEPORTER_GREEN_12,
12684         EL_MM_TELEPORTER_GREEN_11,
12685         EL_MM_TELEPORTER_GREEN_10,
12686         EL_MM_TELEPORTER_GREEN_9,
12687         EL_MM_TELEPORTER_GREEN_8,
12688         EL_MM_TELEPORTER_GREEN_7,
12689         EL_MM_TELEPORTER_GREEN_6
12690       },
12691       {
12692         EL_MM_TELEPORTER_BLUE_5,
12693         EL_MM_TELEPORTER_BLUE_4,
12694         EL_MM_TELEPORTER_BLUE_3,
12695         EL_MM_TELEPORTER_BLUE_2,
12696         EL_MM_TELEPORTER_BLUE_1,
12697         EL_MM_TELEPORTER_BLUE_16,
12698         EL_MM_TELEPORTER_BLUE_15,
12699         EL_MM_TELEPORTER_BLUE_14,
12700         EL_MM_TELEPORTER_BLUE_13,
12701         EL_MM_TELEPORTER_BLUE_12,
12702         EL_MM_TELEPORTER_BLUE_11,
12703         EL_MM_TELEPORTER_BLUE_10,
12704         EL_MM_TELEPORTER_BLUE_9,
12705         EL_MM_TELEPORTER_BLUE_8,
12706         EL_MM_TELEPORTER_BLUE_7,
12707         EL_MM_TELEPORTER_BLUE_6
12708       },
12709       {
12710         EL_MM_POLARIZER_1,
12711         EL_MM_POLARIZER_16,
12712         EL_MM_POLARIZER_15,
12713         EL_MM_POLARIZER_14,
12714         EL_MM_POLARIZER_13,
12715         EL_MM_POLARIZER_12,
12716         EL_MM_POLARIZER_11,
12717         EL_MM_POLARIZER_10,
12718         EL_MM_POLARIZER_9,
12719         EL_MM_POLARIZER_8,
12720         EL_MM_POLARIZER_7,
12721         EL_MM_POLARIZER_6,
12722         EL_MM_POLARIZER_5,
12723         EL_MM_POLARIZER_4,
12724         EL_MM_POLARIZER_3,
12725         EL_MM_POLARIZER_2
12726       },
12727       {
12728         EL_DF_MIRROR_1,
12729         EL_DF_MIRROR_16,
12730         EL_DF_MIRROR_15,
12731         EL_DF_MIRROR_14,
12732         EL_DF_MIRROR_13,
12733         EL_DF_MIRROR_12,
12734         EL_DF_MIRROR_11,
12735         EL_DF_MIRROR_10,
12736         EL_DF_MIRROR_9,
12737         EL_DF_MIRROR_8,
12738         EL_DF_MIRROR_7,
12739         EL_DF_MIRROR_6,
12740         EL_DF_MIRROR_5,
12741         EL_DF_MIRROR_4,
12742         EL_DF_MIRROR_3,
12743         EL_DF_MIRROR_2
12744       },
12745       {
12746         EL_DF_MIRROR_ROTATING_1,
12747         EL_DF_MIRROR_ROTATING_16,
12748         EL_DF_MIRROR_ROTATING_15,
12749         EL_DF_MIRROR_ROTATING_14,
12750         EL_DF_MIRROR_ROTATING_13,
12751         EL_DF_MIRROR_ROTATING_12,
12752         EL_DF_MIRROR_ROTATING_11,
12753         EL_DF_MIRROR_ROTATING_10,
12754         EL_DF_MIRROR_ROTATING_9,
12755         EL_DF_MIRROR_ROTATING_8,
12756         EL_DF_MIRROR_ROTATING_7,
12757         EL_DF_MIRROR_ROTATING_6,
12758         EL_DF_MIRROR_ROTATING_5,
12759         EL_DF_MIRROR_ROTATING_4,
12760         EL_DF_MIRROR_ROTATING_3,
12761         EL_DF_MIRROR_ROTATING_2
12762       },
12763       {
12764         EL_DF_MIRROR_FIXED_1,
12765         EL_DF_MIRROR_FIXED_16,
12766         EL_DF_MIRROR_FIXED_15,
12767         EL_DF_MIRROR_FIXED_14,
12768         EL_DF_MIRROR_FIXED_13,
12769         EL_DF_MIRROR_FIXED_12,
12770         EL_DF_MIRROR_FIXED_11,
12771         EL_DF_MIRROR_FIXED_10,
12772         EL_DF_MIRROR_FIXED_9,
12773         EL_DF_MIRROR_FIXED_8,
12774         EL_DF_MIRROR_FIXED_7,
12775         EL_DF_MIRROR_FIXED_6,
12776         EL_DF_MIRROR_FIXED_5,
12777         EL_DF_MIRROR_FIXED_4,
12778         EL_DF_MIRROR_FIXED_3,
12779         EL_DF_MIRROR_FIXED_2
12780       },
12781
12782       {
12783         -1,
12784       },
12785     };
12786     int i, j;
12787
12788     for (i = 0; swappable_elements[i][0] != -1; i++)
12789     {
12790       int element1 = swappable_elements[i][0];
12791       int element2 = swappable_elements[i][1];
12792
12793       if (old_element == element1 || old_element == element2)
12794         new_element = (old_element == element1 ? element2 : element1);
12795     }
12796
12797     for (i = 0; rotatable_elements_4[i][0] != -1; i++)
12798     {
12799       for (j = 0; j < 4; j++)
12800       {
12801         int element = rotatable_elements_4[i][j];
12802
12803         if (old_element == element)
12804           new_element = (button == 1 ? rotatable_elements_4[i][(j + 3) % 4] :
12805                          button == 2 ? rotatable_elements_4[i][0]           :
12806                          button == 3 ? rotatable_elements_4[i][(j + 1) % 4] :
12807                          old_element);
12808       }
12809     }
12810
12811     for (i = 0; rotatable_elements_8[i][0] != -1; i++)
12812     {
12813       for (j = 0; j < 8; j++)
12814       {
12815         int element = rotatable_elements_8[i][j];
12816
12817         if (old_element == element)
12818           new_element = (button == 1 ? rotatable_elements_8[i][(j + 7) % 8] :
12819                          button == 2 ? rotatable_elements_8[i][0]           :
12820                          button == 3 ? rotatable_elements_8[i][(j + 1) % 8] :
12821                          old_element);
12822       }
12823     }
12824
12825     for (i = 0; rotatable_elements_16[i][0] != -1; i++)
12826     {
12827       for (j = 0; j < 16; j++)
12828       {
12829         int element = rotatable_elements_16[i][j];
12830
12831         if (old_element == element)
12832           new_element = (button == 1 ? rotatable_elements_16[i][(j + 15) % 16] :
12833                          button == 2 ? rotatable_elements_16[i][0]             :
12834                          button == 3 ? rotatable_elements_16[i][(j + 1)  % 16] :
12835                          old_element);
12836       }
12837     }
12838
12839     if (old_element != new_element)
12840     {
12841       int max_infotext_len = getMaxInfoTextLength();
12842       char infotext[MAX_OUTPUT_LINESIZE + 1];
12843
12844       strncpy(infotext, getElementInfoText(new_element), max_infotext_len);
12845       infotext[max_infotext_len] = '\0';
12846
12847       ClearEditorGadgetInfoText();
12848
12849       DrawTextS(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY, INFOTEXT_FONT,
12850                 infotext);
12851     }
12852   }
12853
12854   if (IS_MM_WALL_EDITOR(new_element))
12855     SetElementSimpleExt(x, y, dx, dy, new_element, change_level);
12856   else
12857     SetElementSimple(x, y, new_element, change_level);
12858
12859   last_x = x;
12860   last_y = y;
12861 }
12862
12863 static void ResetIntelliDraw(void)
12864 {
12865   int x, y;
12866
12867   for (x = 0; x < lev_fieldx; x++)
12868     for (y = 0; y < lev_fieldy; y++)
12869       IntelliDrawBuffer[x][y] = Tile[x][y];
12870
12871   SetElementIntelliDraw(-1, -1, -1, -1, EL_UNDEFINED, FALSE, -1);
12872 }
12873
12874 static boolean draw_mode_hires = FALSE;
12875
12876 static boolean isHiresTileElement(int element)
12877 {
12878   return (IS_MM_WALL(element)        || element == EL_EMPTY);
12879 }
12880
12881 static boolean isHiresDrawElement(int element)
12882 {
12883   return (IS_MM_WALL_EDITOR(element) || element == EL_EMPTY);
12884 }
12885
12886 static int numHiresTiles(int element)
12887 {
12888   if (IS_MM_WALL(element))
12889     return get_number_of_bits(MM_WALL_BITS(element));
12890
12891   return 1;
12892 }
12893
12894 static void SetDrawModeHiRes(int element)
12895 {
12896   draw_mode_hires =
12897     (level.game_engine_type == GAME_ENGINE_TYPE_MM &&
12898      isHiresDrawElement(element));
12899 }
12900
12901 static boolean getDrawModeHiRes(void)
12902 {
12903   return draw_mode_hires;
12904 }
12905
12906 static int getLoResScreenPos(int pos)
12907 {
12908   return (getDrawModeHiRes() ? pos / 2 : pos);
12909 }
12910
12911 static int getLoResScreenMod(int pos)
12912 {
12913   return (getDrawModeHiRes() ? pos % 2 : 0);
12914 }
12915
12916 static void SetElementExt(int x, int y, int dx, int dy, int element,
12917                           boolean change_level, int button)
12918 {
12919   if (element < 0)
12920     SetElementSimple(x, y, Tile[x][y], change_level);
12921   else if (GetKeyModState() & KMOD_Shift)
12922     SetElementIntelliDraw(x, y, dx, dy, element, change_level, button);
12923   else
12924     SetElementSimpleExt(x, y, dx, dy, element, change_level);
12925 }
12926
12927 static void SetElement(int x, int y, int element)
12928 {
12929   SetElementExt(x, y, 0, 0, element, TRUE, -1);
12930 }
12931
12932 static void SetElementButton(int x, int y, int dx, int dy, int element,
12933                              int button)
12934 {
12935   SetElementExt(x, y, dx, dy, element, TRUE, button);
12936 }
12937
12938 static void SetElementHiRes(int sx2, int sy2, int element, boolean change_level)
12939 {
12940   int lx = getLoResScreenPos(sx2) + level_xpos;
12941   int ly = getLoResScreenPos(sy2) + level_ypos;
12942   int dx = getLoResScreenMod(sx2);
12943   int dy = getLoResScreenMod(sy2);
12944
12945   SetElementExt(lx, ly, dx, dy, element, change_level, -1);
12946 }
12947
12948 static void SetLevelElementHiRes(int lx2, int ly2, int element)
12949 {
12950   int lx = lx2 / 2;
12951   int ly = ly2 / 2;
12952   int dx = lx2 % 2;
12953   int dy = ly2 % 2;
12954
12955   SetElementExt(lx, ly, dx, dy, element, TRUE, -1);
12956 }
12957
12958 static int getLevelElementHiRes(int lx2, int ly2)
12959 {
12960   int lx = lx2 / 2;
12961   int ly = ly2 / 2;
12962   int dx = lx2 % 2;
12963   int dy = ly2 % 2;
12964   int element = Tile[lx][ly];
12965   unsigned int bitmask = (dx + 1) << (dy * 2);
12966
12967   if (IS_MM_WALL(element))
12968   {
12969     if (element & bitmask)
12970       return map_mm_wall_element(element);
12971     else
12972       return EL_EMPTY;
12973   }
12974
12975   return element;
12976 }
12977
12978 static void DrawLineElement(int x, int y, int element, boolean change_level)
12979 {
12980   SetElementHiRes(x, y, element, change_level);
12981 }
12982
12983 static void DrawLine(int from_x, int from_y, int to_x, int to_y,
12984                      int element, boolean change_level)
12985 {
12986   int xsize = ABS(to_x - from_x);
12987   int ysize = ABS(to_y - from_y);
12988   int dx = (to_x < from_x ? -1 : +1);
12989   int dy = (to_y < from_y ? -1 : +1);
12990   int i;
12991
12992   if (from_y == to_y)                   // horizontal line
12993   {
12994     for (i = 0; i <= xsize; i++)
12995       DrawLineElement(from_x + i * dx, from_y, element, change_level);
12996   }
12997   else if (from_x == to_x)              // vertical line
12998   {
12999     for (i = 0; i <= ysize; i++)
13000       DrawLineElement(from_x, from_y + i * dy, element, change_level);
13001   }
13002   else                                  // diagonal line
13003   {
13004     if (ysize < xsize)                  // a < 1
13005     {
13006       float a = (float)ysize / (float)xsize;
13007
13008       for (i = 0; i <= xsize; i++)
13009       {
13010         int x = dx * i;
13011         int y = dy * (int)(a * i + 0.5);
13012
13013         DrawLineElement(from_x + x, from_y + y, element, change_level);
13014       }
13015     }
13016     else                                // a >= 1
13017     {
13018       float a = (float)xsize / (float)ysize;
13019
13020       for (i = 0; i <= ysize; i++)
13021       {
13022         int x = dx * (int)(a * i + 0.5);
13023         int y = dy * i;
13024
13025         DrawLineElement(from_x + x, from_y + y, element, change_level);
13026       }
13027     }
13028   }
13029 }
13030
13031 static void DrawBox(int from_x, int from_y, int to_x, int to_y,
13032                     int element, boolean change_level)
13033 {
13034   DrawLine(from_x, from_y, from_x, to_y, element, change_level);
13035   DrawLine(from_x, to_y, to_x, to_y, element, change_level);
13036   DrawLine(to_x, to_y, to_x, from_y, element, change_level);
13037   DrawLine(to_x, from_y, from_x, from_y, element, change_level);
13038 }
13039
13040 static void DrawFilledBox(int from_x, int from_y, int to_x, int to_y,
13041                           int element, boolean change_level)
13042 {
13043   int y;
13044
13045   if (from_y > to_y)
13046     swap_number_pairs(&from_x, &from_y, &to_x, &to_y);
13047
13048   for (y = from_y; y <= to_y; y++)
13049     DrawLine(from_x, y, to_x, y, element, change_level);
13050 }
13051
13052 static void DrawArcExt(int from_x, int from_y, int to_x2, int to_y2,
13053                        int element, boolean change_level)
13054 {
13055   int to_x = to_x2 - (to_x2 > from_x ? +1 : -1);
13056   int to_y = to_y2 - (to_y2 > from_y ? +1 : -1);
13057   int len_x = ABS(to_x - from_x);
13058   int len_y = ABS(to_y - from_y);
13059   int radius, x, y;
13060
13061   radius = (int)(sqrt((float)(len_x * len_x + len_y * len_y)) + 0.5);
13062
13063   // not optimal (some points get drawn twice) but simple,
13064   // and fast enough for the few points we are drawing
13065
13066   for (x = 0; x <= radius; x++)
13067   {
13068     int sx, sy, sx2, sy2, lx, ly;
13069
13070     y = (int)(sqrt((float)(radius * radius - x * x)) + 0.5);
13071
13072     sx2 = from_x + x * (from_x < to_x2 ? +1 : -1);
13073     sy2 = from_y + y * (from_y < to_y2 ? +1 : -1);
13074     sx = getLoResScreenPos(sx2);
13075     sy = getLoResScreenPos(sy2);
13076     lx = sx + level_xpos;
13077     ly = sy + level_ypos;
13078
13079     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
13080       DrawLineElement(sx2, sy2, element, change_level);
13081   }
13082
13083   for (y = 0; y <= radius; y++)
13084   {
13085     int sx, sy, sx2, sy2, lx, ly;
13086
13087     x = (int)(sqrt((float)(radius * radius - y * y)) + 0.5);
13088
13089     sx2 = from_x + x * (from_x < to_x2 ? +1 : -1);
13090     sy2 = from_y + y * (from_y < to_y2 ? +1 : -1);
13091     sx = getLoResScreenPos(sx2);
13092     sy = getLoResScreenPos(sy2);
13093     lx = sx + level_xpos;
13094     ly = sy + level_ypos;
13095
13096     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
13097       DrawLineElement(sx2, sy2, element, change_level);
13098   }
13099 }
13100
13101 static void DrawArc(int from_x, int from_y, int to_x, int to_y,
13102                     int element, boolean change_level)
13103 {
13104   int to_x2 = to_x + (to_x < from_x ? -1 : +1);
13105   int to_y2 = to_y + (to_y > from_y ? +1 : -1);
13106
13107   DrawArcExt(from_x, from_y, to_x2, to_y2, element, change_level);
13108 }
13109
13110 #define DRAW_CIRCLES_BUTTON_AVAILABLE   0
13111
13112 #if DRAW_CIRCLES_BUTTON_AVAILABLE
13113 static void DrawCircle(int from_x, int from_y, int to_x, int to_y,
13114                        int element, boolean change_level)
13115 {
13116   int to_x2 = to_x + (to_x < from_x ? -1 : +1);
13117   int to_y2 = to_y + (to_y > from_y ? +1 : -1);
13118   int mirror_to_x2 = from_x - (to_x2 - from_x);
13119   int mirror_to_y2 = from_y - (to_y2 - from_y);
13120
13121   DrawArcExt(from_x, from_y, to_x2, to_y2, element, change_level);
13122   DrawArcExt(from_x, from_y, mirror_to_x2, to_y2, element, change_level);
13123   DrawArcExt(from_x, from_y, to_x2, mirror_to_y2, element, change_level);
13124   DrawArcExt(from_x, from_y, mirror_to_x2, mirror_to_y2, element, change_level);
13125 }
13126 #endif
13127
13128 static void DrawAreaBorder(int from_x, int from_y, int to_x, int to_y)
13129 {
13130   int from_sx, from_sy;
13131   int to_sx, to_sy;
13132
13133   if (from_x > to_x)
13134     swap_numbers(&from_x, &to_x);
13135
13136   if (from_y > to_y)
13137     swap_numbers(&from_y, &to_y);
13138
13139   from_sx = SX + from_x * ed_tilesize;
13140   from_sy = SY + from_y * ed_tilesize;
13141   to_sx = SX + (to_x + 1) * ed_tilesize - 1;
13142   to_sy = SY + (to_y + 1) * ed_tilesize - 1;
13143
13144   DrawSimpleWhiteLine(drawto, from_sx, from_sy, to_sx, from_sy);
13145   DrawSimpleWhiteLine(drawto, to_sx, from_sy, to_sx, to_sy);
13146   DrawSimpleWhiteLine(drawto, to_sx, to_sy, from_sx, to_sy);
13147   DrawSimpleWhiteLine(drawto, from_sx, to_sy, from_sx, from_sy);
13148
13149   if (from_x == to_x && from_y == to_y)
13150     MarkTileDirty(from_x/2, from_y/2);
13151   else
13152     redraw_mask |= REDRAW_FIELD;
13153 }
13154
13155 static void DrawAreaBox(int from_x, int from_y, int to_x, int to_y,
13156                         int element, boolean change_level)
13157 {
13158   DrawBox(from_x, from_y, to_x, to_y, element, change_level);
13159 }
13160
13161 static void SelectArea(int from_x, int from_y, int to_x, int to_y,
13162                        int element, boolean change_level)
13163 {
13164   if (element == -1 || change_level)
13165     DrawAreaBox(from_x, from_y, to_x, to_y, -1, FALSE);
13166   else
13167     DrawAreaBorder(from_x, from_y, to_x, to_y);
13168 }
13169
13170 // values for CopyBrushExt()
13171 #define CB_AREA_TO_BRUSH                0
13172 #define CB_BRUSH_TO_CURSOR              1
13173 #define CB_BRUSH_TO_LEVEL               2
13174 #define CB_DELETE_OLD_CURSOR            3
13175 #define CB_DUMP_BRUSH                   4
13176 #define CB_DUMP_BRUSH_SMALL             5
13177 #define CB_CLIPBOARD_TO_BRUSH           6
13178 #define CB_BRUSH_TO_CLIPBOARD           7
13179 #define CB_BRUSH_TO_CLIPBOARD_SMALL     8
13180 #define CB_UPDATE_BRUSH_POSITION        9
13181 #define CB_FLIP_BRUSH_X                 10
13182 #define CB_FLIP_BRUSH_Y                 11
13183 #define CB_FLIP_BRUSH_XY                12
13184
13185 #define MAX_CB_PART_SIZE        10
13186 #define MAX_CB_LINE_SIZE        (MAX_LEV_FIELDX + 1)    // text plus newline
13187 #define MAX_CB_NUM_LINES        (MAX_LEV_FIELDY)
13188 #define MAX_CB_TEXT_SIZE        (MAX_CB_LINE_SIZE *     \
13189                                  MAX_CB_NUM_LINES *     \
13190                                  MAX_CB_PART_SIZE)
13191
13192 static int getFlippedTileExt(int map[], int element)
13193 {
13194   int i;
13195
13196   for (i = 0; map[i] != -1; i++)
13197     if (map[i] == element)
13198       return map[i ^ 1];        // get flipped element by flipping LSB of index
13199
13200   return element;
13201 }
13202
13203 static int getFlippedTileX(int element)
13204 {
13205   int map[] =
13206   {
13207     EL_BD_BUTTERFLY_LEFT,               EL_BD_BUTTERFLY_RIGHT,
13208     EL_BD_FIREFLY_LEFT,                 EL_BD_FIREFLY_RIGHT,
13209     EL_BUG_LEFT,                        EL_BUG_RIGHT,
13210     EL_SPACESHIP_LEFT,                  EL_SPACESHIP_RIGHT,
13211     EL_PACMAN_LEFT,                     EL_PACMAN_RIGHT,
13212     EL_ARROW_LEFT,                      EL_ARROW_RIGHT,
13213     EL_MOLE_LEFT,                       EL_MOLE_RIGHT,
13214     EL_BALLOON_SWITCH_LEFT,             EL_BALLOON_SWITCH_RIGHT,
13215     EL_YAMYAM_LEFT,                     EL_YAMYAM_RIGHT,
13216     EL_SP_PORT_LEFT,                    EL_SP_PORT_RIGHT,
13217     EL_SP_GRAVITY_PORT_LEFT,            EL_SP_GRAVITY_PORT_RIGHT,
13218     EL_SP_GRAVITY_ON_PORT_LEFT,         EL_SP_GRAVITY_ON_PORT_RIGHT,
13219     EL_SP_GRAVITY_OFF_PORT_LEFT,        EL_SP_GRAVITY_OFF_PORT_RIGHT,
13220     EL_CONVEYOR_BELT_1_LEFT,            EL_CONVEYOR_BELT_1_RIGHT,
13221     EL_CONVEYOR_BELT_2_LEFT,            EL_CONVEYOR_BELT_2_RIGHT,
13222     EL_CONVEYOR_BELT_3_LEFT,            EL_CONVEYOR_BELT_3_RIGHT,
13223     EL_CONVEYOR_BELT_4_LEFT,            EL_CONVEYOR_BELT_4_RIGHT,
13224     EL_SPRING_LEFT,                     EL_SPRING_RIGHT,
13225     EL_SP_CHIP_LEFT,                    EL_SP_CHIP_RIGHT,
13226     EL_TUBE_VERTICAL_LEFT,              EL_TUBE_VERTICAL_RIGHT,
13227     EL_TUBE_LEFT_UP,                    EL_TUBE_RIGHT_UP,
13228     EL_TUBE_LEFT_DOWN,                  EL_TUBE_RIGHT_DOWN,
13229     EL_DC_STEELWALL_1_LEFT,             EL_DC_STEELWALL_1_RIGHT,
13230     EL_DC_STEELWALL_1_TOPLEFT,          EL_DC_STEELWALL_1_TOPRIGHT,
13231     EL_DC_STEELWALL_1_BOTTOMLEFT,       EL_DC_STEELWALL_1_BOTTOMRIGHT,
13232     EL_DC_STEELWALL_1_TOPLEFT_2,        EL_DC_STEELWALL_1_TOPRIGHT_2,
13233     EL_DC_STEELWALL_1_BOTTOMLEFT_2,     EL_DC_STEELWALL_1_BOTTOMRIGHT_2,
13234     EL_DC_STEELWALL_2_LEFT,             EL_DC_STEELWALL_2_RIGHT,
13235     EL_ACID_POOL_TOPLEFT,               EL_ACID_POOL_TOPRIGHT,
13236     EL_ACID_POOL_BOTTOMLEFT,            EL_ACID_POOL_BOTTOMRIGHT,
13237
13238     -1
13239   };
13240
13241   return getFlippedTileExt(map, element);
13242 }
13243
13244 static int getFlippedTileY(int element)
13245 {
13246   int map[] =
13247   {
13248     EL_BD_BUTTERFLY_UP,                 EL_BD_BUTTERFLY_DOWN,
13249     EL_BD_FIREFLY_UP,                   EL_BD_FIREFLY_DOWN,
13250     EL_BUG_UP,                          EL_BUG_DOWN,
13251     EL_SPACESHIP_UP,                    EL_SPACESHIP_DOWN,
13252     EL_PACMAN_UP,                       EL_PACMAN_DOWN,
13253     EL_ARROW_UP,                        EL_ARROW_DOWN,
13254     EL_MOLE_UP,                         EL_MOLE_DOWN,
13255     EL_BALLOON_SWITCH_UP,               EL_BALLOON_SWITCH_DOWN,
13256     EL_YAMYAM_UP,                       EL_YAMYAM_DOWN,
13257     EL_SP_PORT_UP,                      EL_SP_PORT_DOWN,
13258     EL_SP_GRAVITY_PORT_UP,              EL_SP_GRAVITY_PORT_DOWN,
13259     EL_SP_GRAVITY_ON_PORT_UP,           EL_SP_GRAVITY_ON_PORT_DOWN,
13260     EL_SP_GRAVITY_OFF_PORT_UP,          EL_SP_GRAVITY_OFF_PORT_DOWN,
13261     EL_SP_CHIP_TOP,                     EL_SP_CHIP_BOTTOM,
13262     EL_TUBE_HORIZONTAL_UP,              EL_TUBE_HORIZONTAL_DOWN,
13263     EL_TUBE_LEFT_UP,                    EL_TUBE_LEFT_DOWN,
13264     EL_TUBE_RIGHT_UP,                   EL_TUBE_RIGHT_DOWN,
13265     EL_DC_STEELWALL_1_TOP,              EL_DC_STEELWALL_1_BOTTOM,
13266     EL_DC_STEELWALL_1_TOPLEFT,          EL_DC_STEELWALL_1_BOTTOMLEFT,
13267     EL_DC_STEELWALL_1_TOPRIGHT,         EL_DC_STEELWALL_1_BOTTOMRIGHT,
13268     EL_DC_STEELWALL_1_TOPLEFT_2,        EL_DC_STEELWALL_1_BOTTOMLEFT_2,
13269     EL_DC_STEELWALL_1_TOPRIGHT_2,       EL_DC_STEELWALL_1_BOTTOMRIGHT_2,
13270     EL_DC_STEELWALL_2_TOP,              EL_DC_STEELWALL_2_BOTTOM,
13271     EL_EMC_WALL_1,                      EL_EMC_WALL_3,
13272
13273     -1
13274   };
13275
13276   return getFlippedTileExt(map, element);
13277 }
13278
13279 static int getFlippedTileXY(int element)
13280 {
13281   int map[] =
13282   {
13283     EL_BD_BUTTERFLY_LEFT,               EL_BD_BUTTERFLY_UP,
13284     EL_BD_BUTTERFLY_RIGHT,              EL_BD_BUTTERFLY_DOWN,
13285     EL_BD_FIREFLY_LEFT,                 EL_BD_FIREFLY_UP,
13286     EL_BD_FIREFLY_RIGHT,                EL_BD_FIREFLY_DOWN,
13287     EL_BUG_LEFT,                        EL_BUG_UP,
13288     EL_BUG_RIGHT,                       EL_BUG_DOWN,
13289     EL_SPACESHIP_LEFT,                  EL_SPACESHIP_UP,
13290     EL_SPACESHIP_RIGHT,                 EL_SPACESHIP_DOWN,
13291     EL_PACMAN_LEFT,                     EL_PACMAN_UP,
13292     EL_PACMAN_RIGHT,                    EL_PACMAN_DOWN,
13293     EL_ARROW_LEFT,                      EL_ARROW_UP,
13294     EL_ARROW_RIGHT,                     EL_ARROW_DOWN,
13295     EL_MOLE_LEFT,                       EL_MOLE_UP,
13296     EL_MOLE_RIGHT,                      EL_MOLE_DOWN,
13297     EL_BALLOON_SWITCH_LEFT,             EL_BALLOON_SWITCH_UP,
13298     EL_BALLOON_SWITCH_RIGHT,            EL_BALLOON_SWITCH_DOWN,
13299     EL_YAMYAM_LEFT,                     EL_YAMYAM_UP,
13300     EL_YAMYAM_RIGHT,                    EL_YAMYAM_DOWN,
13301     EL_SP_PORT_LEFT,                    EL_SP_PORT_UP,
13302     EL_SP_PORT_RIGHT,                   EL_SP_PORT_DOWN,
13303     EL_SP_GRAVITY_PORT_LEFT,            EL_SP_GRAVITY_PORT_UP,
13304     EL_SP_GRAVITY_PORT_RIGHT,           EL_SP_GRAVITY_PORT_DOWN,
13305     EL_SP_GRAVITY_ON_PORT_LEFT,         EL_SP_GRAVITY_ON_PORT_UP,
13306     EL_SP_GRAVITY_ON_PORT_RIGHT,        EL_SP_GRAVITY_ON_PORT_DOWN,
13307     EL_SP_GRAVITY_OFF_PORT_LEFT,        EL_SP_GRAVITY_OFF_PORT_UP,
13308     EL_SP_GRAVITY_OFF_PORT_RIGHT,       EL_SP_GRAVITY_OFF_PORT_DOWN,
13309     EL_SP_CHIP_LEFT,                    EL_SP_CHIP_TOP,
13310     EL_SP_CHIP_RIGHT,                   EL_SP_CHIP_BOTTOM,
13311     EL_TUBE_VERTICAL,                   EL_TUBE_HORIZONTAL,
13312     EL_TUBE_VERTICAL_LEFT,              EL_TUBE_HORIZONTAL_UP,
13313     EL_TUBE_VERTICAL_RIGHT,             EL_TUBE_HORIZONTAL_DOWN,
13314     EL_TUBE_LEFT_DOWN,                  EL_TUBE_RIGHT_UP,
13315     EL_DC_STEELWALL_1_LEFT,             EL_DC_STEELWALL_1_TOP,
13316     EL_DC_STEELWALL_1_RIGHT,            EL_DC_STEELWALL_1_BOTTOM,
13317     EL_DC_STEELWALL_1_HORIZONTAL,       EL_DC_STEELWALL_1_VERTICAL,
13318     EL_DC_STEELWALL_1_TOPRIGHT,         EL_DC_STEELWALL_1_BOTTOMLEFT,
13319     EL_DC_STEELWALL_1_TOPRIGHT_2,       EL_DC_STEELWALL_1_BOTTOMLEFT_2,
13320     EL_DC_STEELWALL_2_LEFT,             EL_DC_STEELWALL_2_TOP,
13321     EL_DC_STEELWALL_2_RIGHT,            EL_DC_STEELWALL_2_BOTTOM,
13322     EL_DC_STEELWALL_2_HORIZONTAL,       EL_DC_STEELWALL_2_VERTICAL,
13323     EL_EXPANDABLE_WALL_HORIZONTAL,      EL_EXPANDABLE_WALL_VERTICAL,
13324     EL_EXPANDABLE_STEELWALL_HORIZONTAL, EL_EXPANDABLE_STEELWALL_VERTICAL,
13325
13326     -1
13327   };
13328
13329   return getFlippedTileExt(map, element);
13330 }
13331
13332 static int getFlippedTile(int element, int mode)
13333 {
13334   if (IS_MM_ELEMENT(element))
13335   {
13336     // get MM game element
13337     element = map_element_RND_to_MM(element);
13338
13339     // get flipped game element
13340     element = (mode == CB_FLIP_BRUSH_X  ? getFlippedTileX_MM(element) :
13341                mode == CB_FLIP_BRUSH_Y  ? getFlippedTileY_MM(element) :
13342                mode == CB_FLIP_BRUSH_XY ? getFlippedTileXY_MM(element) :
13343                element);
13344
13345     // get RND game element again
13346     element = map_element_MM_to_RND(element);
13347   }
13348   else
13349   {
13350     // get flipped game element
13351     element = (mode == CB_FLIP_BRUSH_X  ? getFlippedTileX(element) :
13352                mode == CB_FLIP_BRUSH_Y  ? getFlippedTileY(element) :
13353                mode == CB_FLIP_BRUSH_XY ? getFlippedTileXY(element) :
13354                element);
13355   }
13356
13357   return element;
13358 }
13359
13360 static void SwapFlippedTiles(short *tile1, short *tile2, int mode)
13361 {
13362   // flip tiles
13363   short tile1_flipped = getFlippedTile(*tile1, mode);
13364   short tile2_flipped = getFlippedTile(*tile2, mode);
13365
13366   // swap tiles
13367   *tile1 = tile2_flipped;
13368   *tile2 = tile1_flipped;
13369 }
13370
13371 static void DrawBrushElement(int sx, int sy, int element, boolean change_level)
13372 {
13373   DrawLineElement(sx, sy, element, change_level);
13374 }
13375
13376 static void CopyBrushExt(int from_x, int from_y, int to_x, int to_y,
13377                          int button, int mode)
13378 {
13379   static short brush_buffer[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
13380   static int brush_width, brush_height;
13381   static int last_cursor_x = -1, last_cursor_y = -1;
13382   static boolean delete_old_brush = FALSE;
13383   int new_element = BUTTON_ELEMENT(button);
13384   int x, y;
13385
13386   if (mode == CB_DUMP_BRUSH ||
13387       mode == CB_DUMP_BRUSH_SMALL ||
13388       mode == CB_BRUSH_TO_CLIPBOARD ||
13389       mode == CB_BRUSH_TO_CLIPBOARD_SMALL)
13390   {
13391     if (edit_mode != ED_MODE_DRAWING)
13392       return;
13393
13394     char part[MAX_CB_PART_SIZE + 1] = "";
13395     char text[MAX_CB_TEXT_SIZE + 1] = "";
13396     int width  = (draw_with_brush ? brush_width  : lev_fieldx);
13397     int height = (draw_with_brush ? brush_height : lev_fieldy);
13398     char *format = "%s%03d";
13399
13400     for (y = 0; y < height; y++)
13401       for (x = 0; x < width; x++)
13402         if ((draw_with_brush ? brush_buffer[x][y] : Tile[x][y]) > 999)
13403           format = "%s%04d";
13404
13405     for (y = 0; y < height; y++)
13406     {
13407       for (x = 0; x < width; x++)
13408       {
13409         int element = (draw_with_brush ? brush_buffer[x][y] : Tile[x][y]);
13410         char *prefix = (mode == CB_DUMP_BRUSH ||
13411                         mode == CB_BRUSH_TO_CLIPBOARD ? "`" : "¸");
13412
13413         if (element >= NUM_FILE_ELEMENTS)
13414           element = EL_UNKNOWN;
13415
13416         // copy brush to level sketch text buffer for the R'n'D forum:
13417         // - large tiles: `xxx or `xxxx (0x60 ASCII)
13418         // - small tiles: Â¸xxx or Â¸xxxx (0xb8 ISO-8859-1, 0xc2b8 UTF-8)
13419         snprintf(part, MAX_CB_PART_SIZE + 1, format, prefix, element);
13420         strcat(text, part);
13421       }
13422
13423       strcat(text, "\n");
13424     }
13425
13426     if (mode == CB_BRUSH_TO_CLIPBOARD ||
13427         mode == CB_BRUSH_TO_CLIPBOARD_SMALL)
13428       SDL_SetClipboardText(text);
13429     else
13430       Print("%s", text);        // print brush data to console and log file
13431
13432     return;
13433   }
13434
13435   if (mode == CB_CLIPBOARD_TO_BRUSH)
13436   {
13437     if (edit_mode != ED_MODE_DRAWING)
13438       return;
13439
13440     if (!SDL_HasClipboardText())
13441     {
13442       Request("Clipboard is empty!", REQ_CONFIRM);
13443
13444       return;
13445     }
13446
13447     boolean copy_to_brush = (draw_with_brush ||
13448                              drawing_function == GADGET_ID_GRAB_BRUSH);
13449
13450     // this will delete the old brush, if already drawing with a brush
13451     if (copy_to_brush)
13452       ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS], MB_LEFTBUTTON);
13453
13454     // initialization is required for "odd" (incomplete) clipboard content
13455     for (x = 0; x < MAX_LEV_FIELDX; x++)
13456       for (y = 0; y < MAX_LEV_FIELDY; y++)
13457         brush_buffer[x][y] = EL_EMPTY;
13458
13459     brush_width  = 0;
13460     brush_height = 0;
13461     x = 0;
13462     y = 0;
13463
13464     char *clipboard_text = SDL_GetClipboardText();
13465     char *ptr = clipboard_text;
13466     boolean allow_new_row = FALSE;
13467     boolean stop = FALSE;
13468
13469     while (*ptr && !stop)
13470     {
13471       boolean prefix_found = FALSE;
13472       boolean start_new_row = FALSE;
13473
13474       // level sketch element number prefixes (may be multi-byte characters)
13475       char *prefix_list[] = { "`", "¸" };
13476       int i;
13477
13478       for (i = 0; i < ARRAY_SIZE(prefix_list); i++)
13479       {
13480         char *prefix = prefix_list[i];
13481
13482         // check if string is large enough for prefix
13483         if (strlen(ptr) < strlen(prefix))
13484         {
13485           stop = TRUE;
13486
13487           break;
13488         }
13489
13490         // check if string starts with prefix
13491         if (strPrefix(ptr, prefix))
13492         {
13493           ptr += strlen(prefix);
13494
13495           prefix_found = TRUE;
13496
13497           break;
13498         }
13499       }
13500
13501       // check if prefix found and followed by (at least) three digits
13502       if (prefix_found &&
13503           strlen(ptr) >= 3 &&
13504           ptr[0] >= '0' && ptr[0] <= '9' &&
13505           ptr[1] >= '0' && ptr[1] <= '9' &&
13506           ptr[2] >= '0' && ptr[2] <= '9')
13507       {
13508         int element = ((ptr[0] - '0') * 100 +
13509                        (ptr[1] - '0') * 10 +
13510                        (ptr[2] - '0'));
13511
13512         ptr += 3;
13513
13514         // level sketch element number might consist of four digits
13515         if (ptr[0] >= '0' && ptr[0] <= '9')
13516         {
13517           element = element * 10 + (ptr[0] - '0');
13518           ptr++;
13519         }
13520
13521         // remap some (historic, now obsolete) elements
13522         element = getMappedElement(element);
13523
13524         if (element >= NUM_FILE_ELEMENTS)
13525           element = EL_UNKNOWN;
13526
13527         brush_buffer[x][y] = element;
13528
13529         brush_width  = MAX(x + 1, brush_width);
13530         brush_height = MAX(y + 1, brush_height);
13531
13532         x++;
13533
13534         if (x >= MAX_LEV_FIELDX)
13535           start_new_row = TRUE;
13536
13537         allow_new_row = TRUE;
13538       }
13539       else
13540       {
13541         if ((*ptr == '\n' || *ptr == '\r') && allow_new_row)
13542           start_new_row = TRUE;
13543
13544         ptr++;          // !!! FIX THIS for real UTF-8 handling !!!
13545       }
13546
13547       if (start_new_row)
13548       {
13549         x = 0;
13550         y++;
13551
13552         if (y >= MAX_LEV_FIELDY)
13553           stop = TRUE;
13554
13555         allow_new_row = FALSE;
13556       }
13557     }
13558
13559     SDL_free(clipboard_text);
13560
13561     if (brush_width == 0 || brush_height == 0)
13562     {
13563       Request("No level sketch found in clipboard!", REQ_CONFIRM);
13564
13565       return;
13566     }
13567
13568     if (copy_to_brush)
13569     {
13570       struct GadgetInfo *gi = level_editor_gadget[GADGET_ID_DRAWING_LEVEL];
13571       int mx, my;
13572
13573       SDL_GetMouseState(&mx, &my);
13574
13575       // if inside drawing area, activate and draw brush at last mouse position
13576       if (mx >= gi->x && mx < gi->x + gi->width &&
13577           my >= gi->y && my < gi->y + gi->height)
13578         CopyBrushToCursor(last_cursor_x, last_cursor_y);
13579
13580       draw_with_brush = TRUE;
13581     }
13582     else
13583     {
13584       char request[100];
13585
13586       sprintf(request, "Replace level with %dx%d level sketch from clipboard?",
13587               brush_width, brush_height);
13588
13589       if (!Request(request, REQ_ASK))
13590         return;
13591
13592       for (x = 0; x < MAX_LEV_FIELDX; x++)
13593         for (y = 0; y < MAX_LEV_FIELDY; y++)
13594           Tile[x][y] = brush_buffer[x][y];
13595
13596       lev_fieldx = level.fieldx = brush_width;
13597       lev_fieldy = level.fieldy = brush_height;
13598
13599       boolean use_bd_engine = TRUE;
13600       boolean use_em_engine = TRUE;
13601       boolean use_sp_engine = TRUE;
13602       boolean use_mm_engine = TRUE;
13603
13604       for (x = 0; x < MAX_LEV_FIELDX; x++)
13605       {
13606         for (y = 0; y < MAX_LEV_FIELDY; y++)
13607         {
13608           int element = Tile[x][y];
13609
13610           if (!IS_BD_ELEMENT(element) && !IS_PLAYER_ELEMENT(element))
13611             use_bd_engine = FALSE;
13612
13613           if (!IS_EM_ELEMENT(element) && !IS_PLAYER_ELEMENT(element))
13614             use_em_engine = FALSE;
13615
13616           if (!IS_SP_ELEMENT(element))
13617             use_sp_engine = FALSE;
13618
13619           if (!IS_MM_ELEMENT(element) && element != EL_EMPTY)
13620             use_mm_engine = FALSE;
13621         }
13622       }
13623
13624       level.game_engine_type = (use_bd_engine ? GAME_ENGINE_TYPE_BD :
13625                                 use_em_engine ? GAME_ENGINE_TYPE_EM :
13626                                 use_sp_engine ? GAME_ENGINE_TYPE_SP :
13627                                 use_mm_engine ? GAME_ENGINE_TYPE_MM :
13628                                 GAME_ENGINE_TYPE_RND);
13629
13630       // update element selection list
13631       ReinitializeElementList();
13632       ModifyEditorElementList();
13633
13634       SetBorderElement();
13635
13636       DrawEditModeWindow();
13637       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13638     }
13639
13640     return;
13641   }
13642
13643   if (mode == CB_DELETE_OLD_CURSOR && !delete_old_brush)
13644     return;
13645
13646   if (mode == CB_AREA_TO_BRUSH)
13647   {
13648     int from_lx, from_ly;
13649
13650     if (from_x > to_x)
13651       swap_numbers(&from_x, &to_x);
13652
13653     if (from_y > to_y)
13654       swap_numbers(&from_y, &to_y);
13655
13656     brush_width = to_x - from_x + 1;
13657     brush_height = to_y - from_y + 1;
13658
13659     from_lx = from_x + level_xpos;
13660     from_ly = from_y + level_ypos;
13661
13662     for (y = 0; y < brush_height; y++)
13663     {
13664       for (x = 0; x < brush_width; x++)
13665       {
13666         brush_buffer[x][y] = Tile[from_lx + x][from_ly + y];
13667
13668         if (button != 1)
13669           DrawBrushElement(from_x + x, from_y + y, new_element, TRUE);
13670       }
13671     }
13672
13673     if (button != 1)
13674       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13675
13676     delete_old_brush = FALSE;
13677   }
13678   else if (mode == CB_BRUSH_TO_CURSOR || mode == CB_DELETE_OLD_CURSOR ||
13679            mode == CB_BRUSH_TO_LEVEL)
13680   {
13681     int cursor_x = (mode == CB_DELETE_OLD_CURSOR ? last_cursor_x : from_x);
13682     int cursor_y = (mode == CB_DELETE_OLD_CURSOR ? last_cursor_y : from_y);
13683     int cursor_from_x = cursor_x - brush_width / 2;
13684     int cursor_from_y = cursor_y - brush_height / 2;
13685     int border_from_x = cursor_x, border_from_y = cursor_y;
13686     int border_to_x = cursor_x, border_to_y = cursor_y;
13687
13688     if (mode != CB_DELETE_OLD_CURSOR && delete_old_brush)
13689       CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
13690
13691     if (!IN_ED_FIELD(cursor_x, cursor_y) ||
13692         !IN_LEV_FIELD(cursor_x + level_xpos, cursor_y + level_ypos))
13693     {
13694       delete_old_brush = FALSE;
13695
13696       return;
13697     }
13698
13699     for (y = 0; y < brush_height; y++)
13700     {
13701       for (x = 0; x < brush_width; x++)
13702       {
13703         int sx = cursor_from_x + x;
13704         int sy = cursor_from_y + y;
13705         int lx = sx + level_xpos;
13706         int ly = sy + level_ypos;
13707         boolean change_level = (mode == CB_BRUSH_TO_LEVEL);
13708         int element = (mode == CB_DELETE_OLD_CURSOR ? -1 :
13709                        mode == CB_BRUSH_TO_CURSOR || button == 1 ?
13710                        brush_buffer[x][y] : new_element);
13711
13712         if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
13713         {
13714           if (sx < border_from_x)
13715             border_from_x = sx;
13716           else if (sx > border_to_x)
13717             border_to_x = sx;
13718           if (sy < border_from_y)
13719             border_from_y = sy;
13720           else if (sy > border_to_y)
13721             border_to_y = sy;
13722
13723           DrawBrushElement(sx, sy, element, change_level);
13724         }
13725       }
13726     }
13727
13728     if (mode != CB_DELETE_OLD_CURSOR)
13729       DrawAreaBorder(border_from_x, border_from_y, border_to_x, border_to_y);
13730
13731     last_cursor_x = cursor_x;
13732     last_cursor_y = cursor_y;
13733
13734     delete_old_brush = TRUE;
13735   }
13736   else if (mode == CB_FLIP_BRUSH_X)
13737   {
13738     for (y = 0; y < brush_height; y++)
13739       for (x = 0; x < (brush_width + 1) / 2; x++)
13740         SwapFlippedTiles(&brush_buffer[x][y],
13741                          &brush_buffer[brush_width - x - 1][y], mode);
13742
13743     CopyBrushExt(last_cursor_x, last_cursor_y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13744   }
13745   else if (mode == CB_FLIP_BRUSH_Y)
13746   {
13747     for (y = 0; y < (brush_height + 1) / 2; y++)
13748       for (x = 0; x < brush_width; x++)
13749         SwapFlippedTiles(&brush_buffer[x][y],
13750                          &brush_buffer[x][brush_height - y - 1], mode);
13751
13752     CopyBrushExt(last_cursor_x, last_cursor_y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13753   }
13754   else if (mode == CB_FLIP_BRUSH_XY)
13755   {
13756     CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
13757
13758     for (y = 0; y < MAX(brush_width, brush_height); y++)
13759       for (x = 0; x <= y; x++)
13760         SwapFlippedTiles(&brush_buffer[x][y],
13761                          &brush_buffer[y][x], mode);
13762
13763     swap_numbers(&brush_width, &brush_height);
13764
13765     CopyBrushExt(last_cursor_x, last_cursor_y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13766   }
13767
13768   if (mode == CB_UPDATE_BRUSH_POSITION)
13769   {
13770     last_cursor_x = from_x;
13771     last_cursor_y = from_y;
13772   }
13773 }
13774
13775 static void CopyAreaToBrush(int from_x, int from_y, int to_x, int to_y,
13776                             int button)
13777 {
13778   CopyBrushExt(from_x, from_y, to_x, to_y, button, CB_AREA_TO_BRUSH);
13779 }
13780
13781 static void CopyBrushToLevel(int x, int y, int button)
13782 {
13783   CopyBrushExt(x, y, 0, 0, button, CB_BRUSH_TO_LEVEL);
13784 }
13785
13786 static void CopyBrushToCursor(int x, int y)
13787 {
13788   CopyBrushExt(x, y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13789 }
13790
13791 static void UpdateBrushPosition(int x, int y)
13792 {
13793   CopyBrushExt(x, y, 0, 0, 0, CB_UPDATE_BRUSH_POSITION);
13794 }
13795
13796 static void DeleteBrushFromCursor(void)
13797 {
13798   CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
13799 }
13800
13801 static void FlipBrushX(void)
13802 {
13803   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_X);
13804 }
13805
13806 static void FlipBrushY(void)
13807 {
13808   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_Y);
13809 }
13810
13811 static void RotateBrush(void)
13812 {
13813   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_XY);
13814   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_X);
13815 }
13816
13817 void DumpBrush(void)
13818 {
13819   CopyBrushExt(0, 0, 0, 0, 0, CB_DUMP_BRUSH);
13820 }
13821
13822 void DumpBrush_Small(void)
13823 {
13824   CopyBrushExt(0, 0, 0, 0, 0, CB_DUMP_BRUSH_SMALL);
13825 }
13826
13827 void CopyClipboardToBrush(void)
13828 {
13829   CopyBrushExt(0, 0, 0, 0, 0, CB_CLIPBOARD_TO_BRUSH);
13830 }
13831
13832 void CopyBrushToClipboard(void)
13833 {
13834   CopyBrushExt(0, 0, 0, 0, 0, CB_BRUSH_TO_CLIPBOARD);
13835 }
13836
13837 void CopyBrushToClipboard_Small(void)
13838 {
13839   CopyBrushExt(0, 0, 0, 0, 0, CB_BRUSH_TO_CLIPBOARD_SMALL);
13840 }
13841
13842 void UndoLevelEditorOperation(void)
13843 {
13844   ClickOnGadget(level_editor_gadget[GADGET_ID_UNDO], -1);
13845 }
13846
13847 void RedoLevelEditorOperation(void)
13848 {
13849   ClickOnGadget(level_editor_gadget[GADGET_ID_UNDO], 3);
13850 }
13851
13852 static void FloodFill(int from_x, int from_y, int fill_element)
13853 {
13854   FloodFillLevel(from_x, from_y, fill_element, Tile, lev_fieldx, lev_fieldy);
13855 }
13856
13857 static void FloodFillWall_MM(int from_sx2, int from_sy2, int fill_element)
13858 {
13859   int from_x = from_sx2 + 2 * level_xpos;
13860   int from_y = from_sy2 + 2 * level_ypos;
13861   int max_fillx = lev_fieldx * 2;
13862   int max_filly = lev_fieldy * 2;
13863   short Fill[max_fillx][max_filly];
13864   int x, y;
13865
13866   for (x = 0; x < max_fillx; x++)
13867     for (y = 0; y < max_filly; y++)
13868       Fill[x][y] = getLevelElementHiRes(x, y);
13869
13870   FloodFillLevelExt(from_x, from_y, fill_element, max_fillx, max_filly,
13871                     Fill, max_fillx, max_filly);
13872
13873   for (x = 0; x < max_fillx; x++)
13874     for (y = 0; y < max_filly; y++)
13875       if (Fill[x][y] == fill_element)
13876         SetLevelElementHiRes(x, y, Fill[x][y]);
13877 }
13878
13879 // values for DrawLevelText() modes
13880 #define TEXT_INIT               0
13881 #define TEXT_SETCURSOR          1
13882 #define TEXT_WRITECHAR          2
13883 #define TEXT_BACKSPACE          3
13884 #define TEXT_NEWLINE            4
13885 #define TEXT_END                5
13886 #define TEXT_QUERY_TYPING       6
13887
13888 static int DrawLevelText(int sx, int sy, char letter, int mode)
13889 {
13890   static short delete_buffer[MAX_LEV_FIELDX];
13891   static int start_sx;
13892   static int last_sx, last_sy;
13893   static boolean typing = FALSE;
13894   int letter_element;
13895   int lx = 0, ly = 0;
13896
13897   // map lower case letters to upper case and convert special characters
13898   if (letter >= 'a' && letter <= 'z')
13899     letter_element = EL_CHAR_ASCII0 + letter + (int)('A' - 'a');
13900   else if (letter == CHAR_BYTE_UMLAUT_a || letter == CHAR_BYTE_UMLAUT_A)
13901     letter_element = EL_CHAR_AUMLAUT;
13902   else if (letter == CHAR_BYTE_UMLAUT_o || letter == CHAR_BYTE_UMLAUT_O)
13903     letter_element = EL_CHAR_OUMLAUT;
13904   else if (letter == CHAR_BYTE_UMLAUT_u || letter == CHAR_BYTE_UMLAUT_U)
13905     letter_element = EL_CHAR_UUMLAUT;
13906   else if (letter == '^')
13907     letter_element = EL_CHAR_COPYRIGHT;
13908   else
13909     letter_element = EL_CHAR_ASCII0 + letter;
13910
13911   if (mode != TEXT_INIT)
13912   {
13913     if (!typing)
13914       return FALSE;
13915
13916     if (mode != TEXT_SETCURSOR)
13917     {
13918       sx = last_sx;
13919       sy = last_sy;
13920     }
13921
13922     lx = last_sx + level_xpos;
13923     ly = last_sy + level_ypos;
13924   }
13925
13926   switch (mode)
13927   {
13928     case TEXT_INIT:
13929       if (typing)
13930         DrawLevelText(0, 0, 0, TEXT_END);
13931
13932       typing = TRUE;
13933       start_sx = sx;
13934       last_sx = sx;
13935       last_sy = sy;
13936       DrawLevelText(sx, sy, 0, TEXT_SETCURSOR);
13937       break;
13938
13939     case TEXT_SETCURSOR:
13940       DrawEditorElement(last_sx, last_sy, Tile[lx][ly]);
13941       DrawAreaBorder(sx, sy, sx, sy);
13942       StartTextInput(SX + sx * ed_tilesize, SY + sy * ed_tilesize,
13943                      ed_tilesize, ed_tilesize);
13944       last_sx = sx;
13945       last_sy = sy;
13946       break;
13947
13948     case TEXT_WRITECHAR:
13949       if (letter_element >= EL_CHAR_START && letter_element <= EL_CHAR_END)
13950       {
13951         if (new_element1 >= EL_STEEL_CHAR_START &&
13952             new_element1 <= EL_STEEL_CHAR_END)
13953           letter_element = letter_element - EL_CHAR_START + EL_STEEL_CHAR_START;
13954
13955         delete_buffer[sx - start_sx] = Tile[lx][ly];
13956         Tile[lx][ly] = letter_element;
13957
13958         if (sx + 1 < ed_fieldx && lx + 1 < lev_fieldx)
13959           DrawLevelText(sx + 1, sy, 0, TEXT_SETCURSOR);
13960         else if (sy + 1 < ed_fieldy && ly + 1 < lev_fieldy)
13961           DrawLevelText(start_sx, sy + 1, 0, TEXT_SETCURSOR);
13962         else
13963           DrawLevelText(0, 0, 0, TEXT_END);
13964
13965         level.changed = TRUE;
13966       }
13967       break;
13968
13969     case TEXT_BACKSPACE:
13970       if (sx > start_sx)
13971       {
13972         Tile[lx - 1][ly] = delete_buffer[sx - start_sx - 1];
13973         DrawEditorElement(sx - 1, sy, Tile[lx - 1][ly]);
13974         DrawLevelText(sx - 1, sy, 0, TEXT_SETCURSOR);
13975       }
13976       break;
13977
13978     case TEXT_NEWLINE:
13979       if (sy + 1 < ed_fieldy && ly + 1 < lev_fieldy)
13980         DrawLevelText(start_sx, sy + 1, 0, TEXT_SETCURSOR);
13981       else
13982         DrawLevelText(0, 0, 0, TEXT_END);
13983       break;
13984
13985     case TEXT_END:
13986       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13987       DrawEditorElement(sx, sy, Tile[lx][ly]);
13988       StopTextInput();
13989       typing = FALSE;
13990       break;
13991
13992     case TEXT_QUERY_TYPING:
13993       break;
13994
13995     default:
13996       break;
13997   }
13998
13999   return typing;
14000 }
14001
14002 static void SetTextCursor(int unused_sx, int unused_sy, int sx, int sy,
14003                           int element, boolean change_level)
14004 {
14005   int lx = sx + level_xpos;
14006   int ly = sy + level_ypos;
14007
14008   if (element == -1)
14009     DrawEditorElement(sx, sy, Tile[lx][ly]);
14010   else
14011     DrawAreaBorder(sx, sy, sx, sy);
14012 }
14013
14014 static void CheckLevelBorderElement(boolean redraw_playfield)
14015 {
14016   int last_border_element = BorderElement;
14017
14018   SetBorderElement();
14019
14020   if (redraw_playfield && BorderElement != last_border_element)
14021     DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14022 }
14023
14024 static void CopyLevelToUndoBuffer(int mode)
14025 {
14026   static boolean accumulated_undo = FALSE;
14027   boolean new_undo_buffer_position = TRUE;
14028   int x, y;
14029
14030   if (undo_buffer_steps == 0)
14031     accumulated_undo = FALSE;
14032
14033   switch (mode)
14034   {
14035     case UNDO_IMMEDIATE:
14036       accumulated_undo = FALSE;
14037       break;
14038
14039     case UNDO_ACCUMULATE:
14040       if (accumulated_undo)
14041         new_undo_buffer_position = FALSE;
14042       accumulated_undo = TRUE;
14043       break;
14044
14045     default:
14046       break;
14047   }
14048
14049   if (new_undo_buffer_position)
14050   {
14051     // advance position in undo buffer ring
14052     undo_buffer_position = (undo_buffer_position + 1) % NUM_UNDO_STEPS;
14053
14054     if (undo_buffer_steps < NUM_UNDO_STEPS - 1)
14055       undo_buffer_steps++;
14056   }
14057
14058   // always reset redo buffer when storing level change into undo buffer
14059   redo_buffer_steps = 0;
14060
14061   for (x = 0; x < lev_fieldx; x++)
14062     for (y = 0; y < lev_fieldy; y++)
14063       UndoBuffer[undo_buffer_position][x][y] = Tile[x][y];
14064
14065   // check if drawing operation forces change of border style
14066   CheckLevelBorderElement(TRUE);
14067
14068   level.changed = TRUE;
14069 }
14070
14071 static void RandomPlacement(int new_element)
14072 {
14073   static boolean free_position[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
14074   int num_free_positions = 0;
14075   int num_percentage, num_elements;
14076   int x, y;
14077
14078   ResetIntelliDraw();
14079
14080   // determine number of free positions for randomly placing the new element
14081   for (x = 0; x < lev_fieldx; x++) for (y = 0; y < lev_fieldy; y++)
14082   {
14083     free_position[x][y] =
14084       (random_placement_background_restricted ?
14085        Tile[x][y] == random_placement_background_element :
14086        Tile[x][y] != new_element);
14087
14088     if (free_position[x][y])
14089       num_free_positions++;
14090   }
14091
14092   // determine number of new elements to place there
14093   num_percentage = num_free_positions * random_placement_value / 100;
14094   num_elements = (random_placement_method == RANDOM_USE_PERCENTAGE ?
14095                   num_percentage : random_placement_value);
14096
14097   // if less free positions than elements to place, fill all these positions
14098   if (num_free_positions < num_elements)
14099   {
14100     for (x = 0; x < lev_fieldx; x++)
14101       for (y = 0; y < lev_fieldy; y++)
14102         if (free_position[x][y])
14103           SetElement(x, y, new_element);
14104   }
14105   else
14106   {
14107     while (num_elements > 0)
14108     {
14109       x = GetSimpleRandom(lev_fieldx);
14110       y = GetSimpleRandom(lev_fieldy);
14111
14112       // don't place element at the same position twice
14113       if (free_position[x][y])
14114       {
14115         free_position[x][y] = FALSE;
14116         SetElement(x, y, new_element);
14117         num_elements--;
14118       }
14119     }
14120   }
14121
14122   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14123   CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14124 }
14125
14126 static void WrapLevel(int dx, int dy)
14127 {
14128   int wrap_dx = lev_fieldx - dx;
14129   int wrap_dy = lev_fieldy - dy;
14130   int x, y;
14131
14132   for (x = 0; x < lev_fieldx; x++)
14133     for (y = 0; y < lev_fieldy; y++)
14134       TileBackup[x][y] = Tile[x][y];
14135
14136   for (x = 0; x < lev_fieldx; x++)
14137     for (y = 0; y < lev_fieldy; y++)
14138       Tile[x][y] =
14139         TileBackup[(x + wrap_dx) % lev_fieldx][(y + wrap_dy) % lev_fieldy];
14140
14141   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14142   CopyLevelToUndoBuffer(UNDO_ACCUMULATE);
14143 }
14144
14145 static void DrawAreaElementHighlight(boolean highlighted,
14146                                      boolean highlighted_similar)
14147 {
14148   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14149
14150   if (!highlighted)
14151     return;
14152
14153   int x, y;
14154
14155   for (x = 0; x < ed_fieldx; x++)
14156   {
14157     for (y = 0; y < ed_fieldy; y++)
14158     {
14159       boolean highlight = FALSE;
14160       int lx = x + level_xpos;
14161       int ly = y + level_ypos;
14162
14163       if (!IN_LEV_FIELD(lx, ly))
14164         continue;
14165
14166       // check if element is the same
14167       if (Tile[lx][ly] == new_element1)
14168         highlight = TRUE;
14169
14170       // check if element is similar
14171       if (highlighted_similar &&
14172           strEqual(element_info[Tile[lx][ly]].class_name,
14173                    element_info[new_element1].class_name))
14174         highlight = TRUE;
14175
14176       // check if element is matching MM style wall
14177       if (IS_MM_WALL(Tile[lx][ly]) &&
14178           map_mm_wall_element(Tile[lx][ly]) == new_element1)
14179         highlight = TRUE;
14180
14181       if (!highlight)
14182         continue;
14183
14184       if (IS_MM_WALL(Tile[lx][ly]) && !highlighted_similar)
14185       {
14186         int i;
14187
14188         for (i = 0; i < 4; i++)
14189         {
14190           if (!(MM_WALL_BITS(Tile[lx][ly]) & (1 << i)))
14191             continue;
14192
14193           int xx = x * 2 + (i % 2);
14194           int yy = y * 2 + (i / 2);
14195           int sx = SX + xx * ed_tilesize / 2;
14196           int sy = SY + yy * ed_tilesize / 2;
14197           int from_sx = sx;
14198           int from_sy = sy;
14199           int to_sx = sx + ed_tilesize / 2 - 1;
14200           int to_sy = sy + ed_tilesize / 2 - 1;
14201
14202           DrawSimpleWhiteLine(drawto, from_sx, from_sy, to_sx,   from_sy);
14203           DrawSimpleWhiteLine(drawto, to_sx,   from_sy, to_sx,   to_sy);
14204           DrawSimpleWhiteLine(drawto, to_sx,   to_sy,   from_sx, to_sy);
14205           DrawSimpleWhiteLine(drawto, from_sx, to_sy,   from_sx, from_sy);
14206         }
14207       }
14208       else
14209       {
14210         int sx = SX + x * ed_tilesize;
14211         int sy = SY + y * ed_tilesize;
14212         int from_sx = sx;
14213         int from_sy = sy;
14214         int to_sx = sx + ed_tilesize - 1;
14215         int to_sy = sy + ed_tilesize - 1;
14216
14217         DrawSimpleWhiteLine(drawto, from_sx, from_sy, to_sx,   from_sy);
14218         DrawSimpleWhiteLine(drawto, to_sx,   from_sy, to_sx,   to_sy);
14219         DrawSimpleWhiteLine(drawto, to_sx,   to_sy,   from_sx, to_sy);
14220         DrawSimpleWhiteLine(drawto, from_sx, to_sy,   from_sx, from_sy);
14221       }
14222     }
14223   }
14224 }
14225
14226 static void CopyLevelTemplateToUserLevelSet(char *levelset_subdir)
14227 {
14228   char *template_filename_old = getLocalLevelTemplateFilename();
14229   char *template_filename_new =
14230     getPath2(getUserLevelDir(levelset_subdir), LEVELTEMPLATE_FILENAME);
14231
14232   if (copyFile(template_filename_old, template_filename_new) != 0)
14233     Request("Cannot copy level template!", REQ_CONFIRM);
14234
14235   free(template_filename_new);
14236 }
14237
14238 static void HandleDrawingAreas(struct GadgetInfo *gi)
14239 {
14240   static boolean started_inside_drawing_area = FALSE;
14241   static int last_sx = -1;
14242   static int last_sy = -1;
14243   static int last_sx2 = -1;
14244   static int last_sy2 = -1;
14245   int id = gi->custom_id;
14246   int type_id = gi->custom_type_id;
14247   boolean button_press_event;
14248   boolean button_release_event;
14249   boolean inside_drawing_area = !gi->event.off_borders;
14250   boolean draw_level = (id == GADGET_ID_DRAWING_LEVEL);
14251   int actual_drawing_function;
14252   int button = gi->event.button;
14253   int new_element = BUTTON_ELEMENT(button);
14254   int sx = gi->event.x, sy = gi->event.y;
14255   int min_sx = 0, min_sy = 0;
14256   int max_sx = gi->drawing.area_xsize - 1, max_sy = gi->drawing.area_ysize - 1;
14257   int item_xsize = gi->drawing.item_xsize, item_ysize = gi->drawing.item_ysize;
14258   int mini_item_xsize = item_xsize / 2, mini_item_ysize = item_ysize / 2;
14259   int sx2 = gi->event.mx / mini_item_xsize;
14260   int sy2 = gi->event.my / mini_item_ysize;
14261   int dx = sx2 % 2;
14262   int dy = sy2 % 2;
14263   int lx = 0, ly = 0;
14264   int x, y;
14265
14266   button_press_event = (gi->event.type == GD_EVENT_PRESSED);
14267   button_release_event = (gi->event.type == GD_EVENT_RELEASED);
14268
14269   // make sure to stay inside drawing area boundaries
14270   sx = (sx < min_sx ? min_sx : sx > max_sx ? max_sx : sx);
14271   sy = (sy < min_sy ? min_sy : sy > max_sy ? max_sy : sy);
14272
14273   if (draw_level)
14274   {
14275     int min_lx = 0, min_ly = 0;
14276     int max_lx = lev_fieldx - 1, max_ly = lev_fieldy - 1;
14277
14278     // get positions inside level field
14279     lx = sx + level_xpos;
14280     ly = sy + level_ypos;
14281
14282     if (!IN_LEV_FIELD(lx, ly))
14283       inside_drawing_area = FALSE;
14284
14285     // make sure to stay inside level field boundaries
14286     lx = (lx < min_lx ? min_lx : lx > max_lx ? max_lx : lx);
14287     ly = (ly < min_ly ? min_ly : ly > max_ly ? max_ly : ly);
14288
14289     // correct drawing area positions accordingly
14290     sx = lx - level_xpos;
14291     sy = ly - level_ypos;
14292   }
14293
14294   // also correct MM wall-sized (double) drawing area positions accordingly
14295   if (sx2 / 2 < sx || sx2 / 2 > sx)
14296   {
14297     dx = (sx2 / 2 < sx ? 0 : 1);
14298     sx2 = sx * 2 + dx;
14299   }
14300   if (sy2 / 2 < sy || sy2 / 2 > sy)
14301   {
14302     dy = (sy2 / 2 < sy ? 0 : 1);
14303     sy2 = sy * 2 + dy;
14304   }
14305
14306   if (!button_press_event && !button_release_event)
14307   {
14308     int old_element = (IN_LEV_FIELD(lx, ly) ? Tile[lx][ly] : EL_UNDEFINED);
14309     boolean hires_drawing = (level.game_engine_type == GAME_ENGINE_TYPE_MM &&
14310                              isHiresTileElement(old_element) &&
14311                              isHiresDrawElement(new_element));
14312
14313     // prevent handling events for every pixel position when moving mouse
14314     if ((sx == last_sx && sy == last_sy && !hires_drawing) ||
14315         (sx2 == last_sx2 && sy2 == last_sy2))
14316       return;
14317   }
14318
14319   last_sx = sx;
14320   last_sy = sy;
14321   last_sx2 = sx2;
14322   last_sy2 = sy2;
14323
14324   if (button_press_event)
14325     started_inside_drawing_area = inside_drawing_area;
14326
14327   if (!started_inside_drawing_area)
14328     return;
14329
14330   if (!IS_VALID_BUTTON(button))
14331     return;
14332
14333   // handle info callback for each invocation of action callback
14334   gi->callback_info(gi);
14335
14336   // automatically switch to 'single item' drawing mode, if needed
14337   actual_drawing_function =
14338     (draw_level || drawing_function == GADGET_ID_PICK_ELEMENT ?
14339      drawing_function : GADGET_ID_SINGLE_ITEMS);
14340
14341   // clicking into drawing area with pressed Control key picks element
14342   if (GetKeyModState() & KMOD_Control)
14343   {
14344     last_drawing_function = drawing_function;
14345     actual_drawing_function = GADGET_ID_PICK_ELEMENT;
14346   }
14347
14348   if (GetKeyModState() & KMOD_Shift)
14349   {
14350     if (button_press_event || button_release_event)
14351       ResetIntelliDraw();
14352   }
14353
14354   SetDrawModeHiRes(-1);         // reset to normal draw mode
14355
14356   switch (actual_drawing_function)
14357   {
14358     case GADGET_ID_SINGLE_ITEMS:
14359       if (draw_level)
14360       {
14361         if (button_release_event)
14362         {
14363           CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14364
14365           if (edit_mode == ED_MODE_DRAWING && draw_with_brush &&
14366               !inside_drawing_area)
14367             DeleteBrushFromCursor();
14368
14369           break;
14370         }
14371
14372         if (draw_with_brush)
14373         {
14374           CopyBrushToLevel(sx, sy, button);
14375         }
14376         else
14377         {
14378           SetDrawModeHiRes(new_element);
14379
14380           if (IS_PLAYER_ELEMENT(new_element) || IS_MM_MCDUFFIN(new_element))
14381           {
14382             // remove player at old position
14383             for (y = 0; y < lev_fieldy; y++)
14384             {
14385               for (x = 0; x < lev_fieldx; x++)
14386               {
14387                 int old_element = Tile[x][y];
14388
14389                 if (IS_PLAYER_ELEMENT(old_element) &&
14390                     IS_PLAYER_ELEMENT(new_element))
14391                 {
14392                   int replaced_with_element =
14393                     (old_element == EL_SOKOBAN_FIELD_PLAYER &&
14394                      new_element == EL_PLAYER_1 ? EL_SOKOBAN_FIELD_EMPTY :
14395
14396                      old_element == EL_SOKOBAN_FIELD_PLAYER &&
14397                      new_element == old_element ? EL_SOKOBAN_FIELD_EMPTY :
14398
14399                      new_element == EL_SOKOBAN_FIELD_PLAYER &&
14400                      old_element == EL_PLAYER_1 ? EL_EMPTY :
14401
14402                      new_element >= EL_PLAYER_1 &&
14403                      new_element <= EL_PLAYER_4 &&
14404                      new_element == old_element ? EL_EMPTY :
14405
14406                      old_element);
14407
14408                   SetElement(x, y, replaced_with_element);
14409                 }
14410                 else if (IS_MM_MCDUFFIN(old_element) &&
14411                          IS_MM_MCDUFFIN(new_element))
14412                 {
14413                   // remove McDuffin at old position
14414                   SetElement(x, y, EL_EMPTY);
14415                 }
14416               }
14417             }
14418           }
14419
14420           SetElementButton(lx, ly, dx, dy, new_element, button);
14421         }
14422       }
14423       else if (!button_release_event)
14424       {
14425         int pos = sx * drawingarea_info[type_id].area_ysize + sy;
14426
14427         if (item_xsize == MINI_TILEX && item_ysize == MINI_TILEY)
14428           DrawMiniGraphicExt(drawto,
14429                              gi->x + sx * MINI_TILEX,
14430                              gi->y + sy * MINI_TILEY,
14431                              el2edimg(new_element));
14432         else
14433           DrawFixedGraphicExt(drawto,
14434                               gi->x + sx * TILEX,
14435                               gi->y + sy * TILEY,
14436                               el2edimg(new_element), 0);
14437
14438         if (id == GADGET_ID_CUSTOM_GRAPHIC)
14439           new_element = GFX_ELEMENT(new_element);
14440
14441         drawingarea_info[type_id].value[pos] = new_element;
14442
14443         CopyElementPropertiesToGame(properties_element);
14444
14445         if (id == GADGET_ID_CUSTOM_GRAPHIC)
14446         {
14447           UpdateCustomElementGraphicGadgets();
14448
14449           FrameCounter = 0;     // restart animation frame counter
14450         }
14451       }
14452       break;
14453
14454     case GADGET_ID_CONNECTED_ITEMS:
14455       {
14456         static int last_sx = -1;
14457         static int last_sy = -1;
14458
14459         if (button_release_event)
14460           CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14461
14462         SetDrawModeHiRes(new_element);
14463
14464         if (getDrawModeHiRes())
14465         {
14466           sx = sx2;
14467           sy = sy2;
14468         }
14469
14470         if (!button_press_event)
14471           DrawLine(last_sx, last_sy, sx, sy, new_element, TRUE);
14472
14473         last_sx = sx;
14474         last_sy = sy;
14475       }
14476       break;
14477
14478     case GADGET_ID_LINE:
14479     case GADGET_ID_ARC:
14480     case GADGET_ID_RECTANGLE:
14481     case GADGET_ID_FILLED_BOX:
14482       SetDrawModeHiRes(new_element);
14483
14484       if (getDrawModeHiRes())
14485       {
14486         sx = sx2;
14487         sy = sy2;
14488       }
14489       // FALLTHROUGH
14490     case GADGET_ID_GRAB_BRUSH:
14491     case GADGET_ID_TEXT:
14492       {
14493         static int last_sx = -1;
14494         static int last_sy = -1;
14495         static int start_sx = -1;
14496         static int start_sy = -1;
14497         void (*draw_func)(int, int, int, int, int, boolean);
14498
14499         if (drawing_function == GADGET_ID_LINE)
14500           draw_func = DrawLine;
14501         else if (drawing_function == GADGET_ID_ARC)
14502           draw_func = DrawArc;
14503         else if (drawing_function == GADGET_ID_RECTANGLE)
14504           draw_func = DrawBox;
14505         else if (drawing_function == GADGET_ID_FILLED_BOX)
14506           draw_func = DrawFilledBox;
14507         else if (drawing_function == GADGET_ID_GRAB_BRUSH)
14508           draw_func = SelectArea;
14509         else // (drawing_function == GADGET_ID_TEXT)
14510           draw_func = SetTextCursor;
14511
14512         if (button_press_event)
14513         {
14514           draw_func(sx, sy, sx, sy, new_element, FALSE);
14515           start_sx = last_sx = sx;
14516           start_sy = last_sy = sy;
14517
14518           if (drawing_function == GADGET_ID_TEXT)
14519             DrawLevelText(0, 0, 0, TEXT_END);
14520         }
14521         else if (button_release_event)
14522         {
14523           draw_func(start_sx, start_sy, sx, sy, new_element, TRUE);
14524           if (drawing_function == GADGET_ID_GRAB_BRUSH)
14525           {
14526             CopyAreaToBrush(start_sx, start_sy, sx, sy, button);
14527             CopyBrushToCursor(sx, sy);
14528             ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS],
14529                           MB_LEFTBUTTON);
14530             draw_with_brush = TRUE;
14531           }
14532           else if (drawing_function == GADGET_ID_TEXT)
14533             DrawLevelText(sx, sy, 0, TEXT_INIT);
14534           else
14535             CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14536         }
14537         else if (last_sx != sx || last_sy != sy)
14538         {
14539           draw_func(start_sx, start_sy, last_sx, last_sy, -1, FALSE);
14540           if (IS_MM_WALL_EDITOR(new_element))   // clear wall background
14541             draw_func(start_sx, start_sy, sx, sy, EL_EMPTY, FALSE);
14542           draw_func(start_sx, start_sy, sx, sy, new_element, FALSE);
14543           last_sx = sx;
14544           last_sy = sy;
14545         }
14546       }
14547       break;
14548
14549     case GADGET_ID_FLOOD_FILL:
14550       if (button_press_event && Tile[lx][ly] != new_element)
14551       {
14552         if (IS_MM_WALL_EDITOR(new_element))
14553           FloodFillWall_MM(sx2, sy2, new_element);
14554         else
14555           FloodFill(lx, ly, new_element);
14556
14557         DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14558         CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14559       }
14560       break;
14561
14562     case GADGET_ID_PICK_ELEMENT:
14563       if (button_release_event)
14564         ClickOnGadget(level_editor_gadget[last_drawing_function],
14565                       MB_LEFTBUTTON);
14566       else if (draw_level)
14567         PickDrawingElement(button, Tile[lx][ly]);
14568       else
14569       {
14570         int pos = sx * drawingarea_info[type_id].area_ysize + sy;
14571
14572         PickDrawingElement(button, drawingarea_info[type_id].value[pos]);
14573       }
14574
14575     default:
14576       break;
14577   }
14578
14579   // do not mark level as modified for certain non-level-changing gadgets
14580   if ((type_id >= ED_DRAWING_ID_EDITOR_FIRST &&
14581        type_id <= ED_DRAWING_ID_EDITOR_LAST) ||
14582       actual_drawing_function == GADGET_ID_GRAB_BRUSH ||
14583       actual_drawing_function == GADGET_ID_PICK_ELEMENT)
14584     return;
14585
14586   level.changed = TRUE;
14587 }
14588
14589 static void HandleCounterButtons(struct GadgetInfo *gi)
14590 {
14591   int gadget_id = gi->custom_id;
14592   int counter_id = gi->custom_type_id;
14593   int button = gi->event.button;
14594   int *counter_value = counterbutton_info[counter_id].value;
14595   int step = BUTTON_STEPSIZE(button) *
14596     (gadget_id == counterbutton_info[counter_id].gadget_id_down ? -1 : +1);
14597
14598   if (counter_id == ED_COUNTER_ID_SELECT_LEVEL)
14599   {
14600     boolean pressed = (gi->event.type == GD_EVENT_PRESSED);
14601     boolean released = (gi->event.type == GD_EVENT_RELEASED);
14602     boolean level_changed = LevelChanged();
14603
14604     if ((level_changed && pressed) || (!level_changed && released))
14605       return;
14606
14607     if (level_changed && !Request("Level has changed! Discard changes?",
14608                                   REQ_ASK))
14609     {
14610       if (gadget_id == counterbutton_info[counter_id].gadget_id_text)
14611         ModifyEditorCounterValue(counter_id, *counter_value);
14612
14613       return;
14614     }
14615   }
14616
14617   if (gadget_id == counterbutton_info[counter_id].gadget_id_text)
14618     *counter_value = gi->textinput.number_value;
14619   else
14620     ModifyEditorCounterValue(counter_id, *counter_value + step);
14621
14622   if (counter_id == ED_COUNTER_ID_SELECT_LEVEL)
14623   {
14624     int last_game_engine_type = level.game_engine_type;
14625
14626     LoadLevel(level_nr);
14627     LoadScore(level_nr);
14628
14629     SaveLevelSetup_SeriesInfo();
14630
14631     TapeErase();
14632
14633     ResetUndoBuffer();
14634     DrawEditModeWindow();
14635
14636     if (level.game_engine_type != last_game_engine_type)
14637     {
14638       // update element selection list
14639       ReinitializeElementList();
14640       ModifyEditorElementList();
14641     }
14642
14643     return;
14644   }
14645
14646   switch (counter_id)
14647   {
14648     case ED_COUNTER_ID_YAMYAM_CONTENT:
14649       DrawYamYamContentAreas();
14650       break;
14651
14652     case ED_COUNTER_ID_BALL_CONTENT:
14653       DrawMagicBallContentAreas();
14654       break;
14655
14656     case ED_COUNTER_ID_ANDROID_CONTENT:
14657       DrawAndroidElementArea();
14658       break;
14659
14660     case ED_COUNTER_ID_GROUP_CONTENT:
14661       DrawGroupElementArea();
14662       CopyGroupElementPropertiesToGame(properties_element);
14663       break;
14664
14665     case ED_COUNTER_ID_INVENTORY_SIZE:
14666       DrawPlayerInitialInventoryArea(properties_element);
14667       break;
14668
14669     case ED_COUNTER_ID_MM_BALL_CONTENT:
14670       DrawMMBallContentArea();
14671       break;
14672
14673     case ED_COUNTER_ID_ENVELOPE_XSIZE:
14674     case ED_COUNTER_ID_ENVELOPE_YSIZE:
14675       DrawEnvelopeTextArea(-1);
14676       break;
14677
14678     case ED_COUNTER_ID_LEVEL_XSIZE:
14679     case ED_COUNTER_ID_LEVEL_YSIZE:
14680       lev_fieldx = level.fieldx;
14681       lev_fieldy = level.fieldy;
14682
14683       // check if resizing of level results in change of border border
14684       SetBorderElement();
14685
14686       break;
14687
14688     default:
14689       break;
14690   }
14691
14692   if ((counter_id >= ED_COUNTER_ID_CUSTOM_FIRST &&
14693        counter_id <= ED_COUNTER_ID_CUSTOM_LAST) ||
14694       (counter_id >= ED_COUNTER_ID_CHANGE_FIRST &&
14695        counter_id <= ED_COUNTER_ID_CHANGE_LAST))
14696     CopyElementPropertiesToGame(properties_element);
14697
14698   // do not mark level as modified for certain non-level-changing gadgets
14699   if ((counter_id >= ED_COUNTER_ID_LEVELSET_FIRST &&
14700        counter_id <= ED_COUNTER_ID_LEVELSET_LAST) ||
14701       (counter_id >= ED_COUNTER_ID_EDITOR_FIRST &&
14702        counter_id <= ED_COUNTER_ID_EDITOR_LAST))
14703     return;
14704
14705   level.changed = TRUE;
14706 }
14707
14708 static void HandleTextInputGadgets(struct GadgetInfo *gi)
14709 {
14710   int type_id = gi->custom_type_id;
14711
14712   strcpy(textinput_info[type_id].value, gi->textinput.value);
14713
14714   if (type_id == ED_TEXTINPUT_ID_ELEMENT_NAME)
14715   {
14716     CopyElementPropertiesToGame(properties_element);
14717
14718     ModifyEditorElementList();  // update changed button info text
14719   }
14720
14721   // do not mark level as modified for certain non-level-changing gadgets
14722   if (type_id >= ED_TEXTINPUT_ID_LEVELSET_FIRST &&
14723       type_id <= ED_TEXTINPUT_ID_LEVELSET_LAST)
14724     return;
14725
14726   level.changed = TRUE;
14727 }
14728
14729 static void HandleTextAreaGadgets(struct GadgetInfo *gi)
14730 {
14731   int type_id = gi->custom_type_id;
14732
14733   strncpy(textarea_info[type_id].value, gi->textarea.value,
14734           MAX_ENVELOPE_TEXT_LEN);
14735   textarea_info[type_id].value[MAX_ENVELOPE_TEXT_LEN] = '\0';
14736
14737   level.changed = TRUE;
14738 }
14739
14740 static void HandleSelectboxGadgets(struct GadgetInfo *gi)
14741 {
14742   int type_id = gi->custom_type_id;
14743   int value_old = *selectbox_info[type_id].value;
14744   int value_new = selectbox_info[type_id].options[gi->selectbox.index].value;
14745
14746   *selectbox_info[type_id].value = value_new;
14747
14748   if (type_id == ED_SELECTBOX_ID_LEVELSET_SAVE_MODE)
14749   {
14750     DrawLevelConfigWindow();
14751   }
14752   else if (type_id == ED_SELECTBOX_ID_SELECT_CHANGE_PAGE)
14753   {
14754     element_info[properties_element].current_change_page = gi->selectbox.index;
14755
14756     DrawPropertiesWindow();
14757   }
14758   else if ((type_id >= ED_SELECTBOX_ID_CUSTOM_FIRST &&
14759             type_id <= ED_SELECTBOX_ID_CUSTOM_LAST) ||
14760            (type_id >= ED_SELECTBOX_ID_CHANGE_FIRST &&
14761             type_id <= ED_SELECTBOX_ID_CHANGE_LAST) ||
14762            (type_id == ED_SELECTBOX_ID_GROUP_CHOICE_MODE))
14763   {
14764     if (type_id == ED_SELECTBOX_ID_ACTION_TYPE)
14765     {
14766       // when changing action type, also check action mode and action arg
14767       if (value_old != value_new)
14768         setSelectboxSpecialActionVariablesIfNeeded();
14769
14770       DrawPropertiesChange();
14771     }
14772
14773     CopyElementPropertiesToGame(properties_element);
14774   }
14775   else if (type_id == ED_SELECTBOX_ID_GAME_ENGINE_TYPE)
14776   {
14777     // show or hide "engine" tabulator depending on game engine type
14778     DrawLevelConfigWindow();
14779
14780     // update element selection list depending on game engine type
14781     ReinitializeElementList();
14782     ModifyEditorElementList();
14783   }
14784   else if (type_id == ED_SELECTBOX_ID_BD_SCHEDULING_TYPE)
14785   {
14786     // update BD cycle delay counter gadgets depending on BD scheduling type
14787     DrawLevelConfigWindow();
14788   }
14789
14790   // do not mark level as modified for certain non-level-changing gadgets
14791   if (type_id == ED_SELECTBOX_ID_LEVELSET_SAVE_MODE ||
14792       type_id == ED_SELECTBOX_ID_SELECT_CHANGE_PAGE)
14793     return;
14794
14795   level.changed = TRUE;
14796 }
14797
14798 static void HandleTextbuttonGadgets(struct GadgetInfo *gi)
14799 {
14800   int type_id = gi->custom_type_id;
14801   int i;
14802
14803   if (type_id >= ED_TAB_BUTTON_ID_LEVELCONFIG_FIRST &&
14804       type_id <= ED_TAB_BUTTON_ID_LEVELCONFIG_LAST)
14805   {
14806     edit_mode_levelconfig = gi->custom_type_id;
14807
14808     DrawLevelConfigWindow();
14809   }
14810   else if (type_id >= ED_TAB_BUTTON_ID_PROPERTIES_FIRST &&
14811            type_id <= ED_TAB_BUTTON_ID_PROPERTIES_LAST)
14812   {
14813     edit_mode_properties = gi->custom_type_id;
14814
14815     DrawPropertiesWindow();
14816   }
14817   else if (type_id == ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1 ||
14818            type_id == ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2)
14819   {
14820     boolean new_template = !fileExists(getLocalLevelTemplateFilename());
14821
14822     // backup original "level.field" (needed to track playfield changes)
14823     CopyPlayfield(level.field, TileBackup);
14824
14825     // "SaveLevelTemplate()" uses "level.field", so copy editor playfield
14826     CopyPlayfield(Tile, level.field);
14827
14828     if (new_template ||
14829         Request("Save this template and kill the old?", REQ_ASK))
14830       SaveLevelTemplate();
14831
14832     if (new_template)
14833       Request("Template saved!", REQ_CONFIRM);
14834
14835     // restore original "level.field" (needed to track playfield changes)
14836     CopyPlayfield(TileBackup, level.field);
14837   }
14838   else if (type_id == ED_TEXTBUTTON_ID_SAVE_LEVELSET)
14839   {
14840     char *levelset_subdir = getLevelSubdirFromSaveMode(levelset_save_mode);
14841
14842     if (levelset_save_mode == LEVELSET_SAVE_MODE_UPDATE &&
14843         leveldir_current->readonly)
14844     {
14845       Request("This level set is read-only!", REQ_CONFIRM);
14846
14847       return;
14848     }
14849
14850     if (strEqual(levelset_name, ""))
14851     {
14852       Request("Please enter level set title!", REQ_CONFIRM);
14853
14854       return;
14855     }
14856
14857     if (strEqual(levelset_author, ""))
14858     {
14859       Request("Please enter level set author!", REQ_CONFIRM);
14860
14861       return;
14862     }
14863
14864     if (levelset_save_mode == LEVELSET_SAVE_MODE_UPDATE)
14865     {
14866       if (UpdateUserLevelSet(levelset_subdir,
14867                              levelset_name,
14868                              levelset_author,
14869                              levelset_num_levels))
14870       {
14871         Request("Level set updated!", REQ_CONFIRM);
14872       }
14873       else
14874       {
14875         Request("Updating level set failed!", REQ_CONFIRM);
14876       }
14877     }
14878     else if (levelset_save_mode == LEVELSET_SAVE_MODE_CREATE)
14879     {
14880       if (level.changed && !Request("Level has changed! Discard changes?",
14881                                      REQ_ASK))
14882         return;
14883
14884       if (CreateUserLevelSet(levelset_subdir,
14885                              levelset_name,
14886                              levelset_author,
14887                              levelset_num_levels,
14888                              levelset_use_levelset_artwork))
14889       {
14890         if (levelset_copy_level_template)
14891           CopyLevelTemplateToUserLevelSet(levelset_subdir);
14892
14893         Request("New level set created!", REQ_CONFIRM);
14894
14895         AddUserLevelSetToLevelInfo(levelset_subdir);
14896         ChangeEditorToLevelSet(levelset_subdir);
14897       }
14898       else
14899       {
14900         Request("Creating new level set failed!", REQ_CONFIRM);
14901       }
14902     }
14903   }
14904   else if (type_id == ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE &&
14905            custom_element.num_change_pages < MAX_CHANGE_PAGES)
14906   {
14907     struct ElementInfo *ei = &element_info[properties_element];
14908
14909     // when modifying custom element, ask for copying level template
14910     if (level.use_custom_template && !AskToCopyAndModifyLevelTemplate())
14911       return;
14912
14913     setElementChangePages(ei, ei->num_change_pages + 1);
14914
14915     // set new change page to be new current change page
14916     ei->current_change_page = ei->num_change_pages - 1;
14917     ei->change = &ei->change_page[ei->current_change_page];
14918
14919     setElementChangeInfoToDefaults(ei->change);
14920
14921     DrawPropertiesWindow();
14922
14923     level.changed = TRUE;
14924   }
14925   else if (type_id == ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE &&
14926            custom_element.num_change_pages > MIN_CHANGE_PAGES)
14927   {
14928     struct ElementInfo *ei = &element_info[properties_element];
14929
14930     // when modifying custom element, ask for copying level template
14931     if (level.use_custom_template && !AskToCopyAndModifyLevelTemplate())
14932       return;
14933
14934     // copy all change pages after change page to be deleted
14935     for (i = ei->current_change_page; i < ei->num_change_pages - 1; i++)
14936       ei->change_page[i] = ei->change_page[i + 1];
14937
14938     setElementChangePages(ei, ei->num_change_pages - 1);
14939
14940     DrawPropertiesWindow();
14941
14942     level.changed = TRUE;
14943   }
14944 }
14945
14946 static void HandleGraphicbuttonGadgets(struct GadgetInfo *gi)
14947 {
14948   int type_id = gi->custom_type_id;
14949
14950   if (type_id == ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE ||
14951       type_id == ED_GRAPHICBUTTON_ID_NEXT_CHANGE_PAGE)
14952   {
14953     struct ElementInfo *ei = &element_info[properties_element];
14954     int step = BUTTON_STEPSIZE(gi->event.button);
14955
14956     step *= (type_id == ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE ? -1 : +1);
14957     ei->current_change_page += step;
14958
14959     if (ei->current_change_page < 0)
14960       ei->current_change_page = 0;
14961     else if (ei->current_change_page >= ei->num_change_pages)
14962       ei->current_change_page = ei->num_change_pages - 1;
14963
14964     DrawPropertiesWindow();
14965   }
14966   else if (type_id == ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE ||
14967            type_id == ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE)
14968   {
14969     struct ElementInfo *ei = &element_info[properties_element];
14970     int current_change_page = ei->current_change_page;
14971
14972     if (type_id == ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE)
14973     {
14974       element_info[EL_INTERNAL_CLIPBOARD_CHANGE].change_page[0] =
14975         ei->change_page[current_change_page];
14976     }
14977     else if (type_id == ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE)
14978     {
14979       // when modifying custom element, ask for copying level template
14980       if (level.use_custom_template && !AskToCopyAndModifyLevelTemplate())
14981         return;
14982
14983       ei->change_page[current_change_page] =
14984         element_info[EL_INTERNAL_CLIPBOARD_CHANGE].change_page[0];
14985
14986       level.changed = TRUE;
14987     }
14988
14989     DrawPropertiesWindow();
14990   }
14991 }
14992
14993 static void HandleRadiobuttons(struct GadgetInfo *gi)
14994 {
14995   int type_id = gi->custom_type_id;
14996
14997   *radiobutton_info[type_id].value =
14998     radiobutton_info[type_id].checked_value;
14999
15000   // do not mark level as modified for certain non-level-changing gadgets
15001   if (type_id >= ED_RADIOBUTTON_ID_EDITOR_FIRST &&
15002       type_id <= ED_RADIOBUTTON_ID_EDITOR_LAST)
15003     return;
15004
15005   level.changed = TRUE;
15006 }
15007
15008 static void HandleCheckbuttons(struct GadgetInfo *gi)
15009 {
15010   int type_id = gi->custom_type_id;
15011
15012   *checkbutton_info[type_id].value ^= TRUE;
15013
15014   if (type_id == ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID ||
15015       type_id == ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID ||
15016       type_id == ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH ||
15017       (((type_id >= ED_CHECKBUTTON_ID_CUSTOM_FIRST &&
15018          type_id <= ED_CHECKBUTTON_ID_CUSTOM_LAST) ||
15019         (type_id >= ED_CHECKBUTTON_ID_CHANGE_FIRST &&
15020          type_id <= ED_CHECKBUTTON_ID_CHANGE_LAST)) &&
15021        type_id != ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1))
15022   {
15023     CopyElementPropertiesToGame(properties_element);
15024   }
15025
15026   if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC)
15027   {
15028     UpdateCustomElementGraphicGadgets();
15029   }
15030   else if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1 ||
15031            type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2)
15032   {
15033     boolean template_related_changes_found = FALSE;
15034     int i;
15035
15036     // check if any custom, group or empty elements have been changed
15037     for (i = 0; i < NUM_FILE_ELEMENTS; i++)
15038       if ((IS_CUSTOM_ELEMENT(i) ||
15039            IS_GROUP_ELEMENT(i) ||
15040            IS_EMPTY_ELEMENT(i)) &&
15041           element_info[i].modified_settings)
15042         template_related_changes_found = TRUE;
15043
15044     if (level.use_custom_template &&
15045         !fileExists(getGlobalLevelTemplateFilename()))
15046     {
15047       Request("No level template found!", REQ_CONFIRM);
15048
15049       level.use_custom_template = FALSE;
15050
15051       ModifyGadget(gi, GDI_CHECKED, FALSE, GDI_END);
15052
15053       return;
15054     }
15055
15056     if (level.use_custom_template &&
15057         template_related_changes_found &&
15058         !Request("Discard changes and use level template?", REQ_ASK))
15059     {
15060       level.use_custom_template = FALSE;
15061
15062       ModifyGadget(gi, GDI_CHECKED, FALSE, GDI_END);
15063
15064       return;
15065     }
15066
15067     if (!level.use_custom_template &&
15068         Request("Copy settings from level template?", REQ_ASK))
15069     {
15070       return;
15071     }
15072
15073     LoadLevelTemplate(level.use_custom_template ? -1 : level_nr);
15074
15075     DrawEditModeWindow();
15076   }
15077   else if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_3)
15078   {
15079     if (setup.editor.use_template_for_new_levels &&
15080         !fileExists(getGlobalLevelTemplateFilename()))
15081     {
15082       Request("No level template found!", REQ_CONFIRM);
15083
15084       return;
15085     }
15086
15087     if (setup.editor.use_template_for_new_levels &&
15088         level.changed &&
15089         !Request("Discard level and load template?", REQ_ASK))
15090     {
15091       return;
15092     }
15093
15094     if (!setup.editor.use_template_for_new_levels &&
15095         level.changed &&
15096         !Request("Discard level and use empty level?", REQ_ASK))
15097     {
15098       return;
15099     }
15100
15101     LoadLevel(level_nr);
15102     LoadScore(level_nr);
15103
15104     TapeErase();
15105
15106     ResetUndoBuffer();
15107     DrawEditModeWindow();
15108   }
15109   else if (type_id == ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS)
15110   {
15111     SetAutomaticNumberOfGemsNeeded();
15112   }
15113
15114   // do not mark level as modified for certain non-level-changing gadgets
15115   if ((type_id >= ED_CHECKBUTTON_ID_LEVELSET_FIRST &&
15116        type_id <= ED_CHECKBUTTON_ID_LEVELSET_LAST) ||
15117       (type_id >= ED_CHECKBUTTON_ID_EDITOR_FIRST &&
15118        type_id <= ED_CHECKBUTTON_ID_EDITOR_LAST &&
15119        type_id != ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2) ||
15120       type_id == ED_CHECKBUTTON_ID_STICK_ELEMENT)
15121     return;
15122
15123   level.changed = TRUE;
15124 }
15125
15126 static void HandleControlButtons(struct GadgetInfo *gi)
15127 {
15128   static int last_level_drawing_function = GADGET_ID_SINGLE_ITEMS;
15129   static int last_edit_mode = ED_MODE_DRAWING;
15130   static int last_custom_copy_mode = -1;
15131   static int last_button = 0;
15132   int id = gi->custom_id;
15133   int button = gi->event.button;
15134   int step = BUTTON_STEPSIZE(button);
15135   int new_element = BUTTON_ELEMENT(button);
15136   int last_properties_element = properties_element;
15137   int x, y;
15138
15139   if (edit_mode == ED_MODE_DRAWING && drawing_function == GADGET_ID_TEXT)
15140     DrawLevelText(0, 0, 0, TEXT_END);
15141
15142   if (id < ED_NUM_CTRL1_BUTTONS &&
15143       id != GADGET_ID_SINGLE_ITEMS &&
15144       id != GADGET_ID_PICK_ELEMENT &&
15145       edit_mode != ED_MODE_DRAWING &&
15146       drawing_function != GADGET_ID_PICK_ELEMENT &&
15147       !(GetKeyModState() & KMOD_Control))
15148     ChangeEditModeWindow(ED_MODE_DRAWING);
15149
15150   // element copy mode active, but no element button pressed => deactivate
15151   if (last_custom_copy_mode != -1 && id < ED_NUM_CTRL_BUTTONS)
15152     last_custom_copy_mode = -1;
15153
15154   // when showing palette on element buttons, change element of button used
15155   if (editor.palette.show_on_element_buttons &&
15156       id >= GADGET_ID_ELEMENT_LEFT && id <= GADGET_ID_ELEMENT_RIGHT)
15157   {
15158     last_button = id - GADGET_ID_ELEMENT_LEFT + 1;
15159
15160     id = GADGET_ID_PALETTE;
15161   }
15162
15163   switch (id)
15164   {
15165     case GADGET_ID_SCROLL_LEFT:
15166       if (level_xpos >= 0)
15167       {
15168         if (lev_fieldx < ed_fieldx - 2)
15169           break;
15170
15171         level_xpos -= step;
15172         if (level_xpos < -1)
15173           level_xpos = -1;
15174         if (button == 1)
15175           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_RIGHT);
15176         else
15177           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15178
15179         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
15180                      GDI_SCROLLBAR_ITEM_POSITION, level_xpos + 1, GDI_END);
15181       }
15182       break;
15183
15184     case GADGET_ID_SCROLL_RIGHT:
15185       if (level_xpos <= lev_fieldx - ed_fieldx)
15186       {
15187         if (lev_fieldx < ed_fieldx - 2)
15188           break;
15189
15190         level_xpos += step;
15191         if (level_xpos > lev_fieldx - ed_fieldx + 1)
15192           level_xpos = lev_fieldx - ed_fieldx + 1;
15193         if (button == 1)
15194           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_LEFT);
15195         else
15196           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15197
15198         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
15199                      GDI_SCROLLBAR_ITEM_POSITION, level_xpos + 1, GDI_END);
15200       }
15201       break;
15202
15203     case GADGET_ID_SCROLL_UP:
15204       if (level_ypos >= 0)
15205       {
15206         if (lev_fieldy < ed_fieldy - 2)
15207           break;
15208
15209         level_ypos -= step;
15210         if (level_ypos < -1)
15211           level_ypos = -1;
15212         if (button == 1)
15213           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_DOWN);
15214         else
15215           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15216
15217         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
15218                      GDI_SCROLLBAR_ITEM_POSITION, level_ypos + 1, GDI_END);
15219       }
15220       break;
15221
15222     case GADGET_ID_SCROLL_DOWN:
15223       if (level_ypos <= lev_fieldy - ed_fieldy)
15224       {
15225         if (lev_fieldy < ed_fieldy - 2)
15226           break;
15227
15228         level_ypos += step;
15229         if (level_ypos > lev_fieldy - ed_fieldy + 1)
15230           level_ypos = lev_fieldy - ed_fieldy + 1;
15231         if (button == 1)
15232           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_UP);
15233         else
15234           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15235
15236         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
15237                      GDI_SCROLLBAR_ITEM_POSITION, level_ypos + 1, GDI_END);
15238       }
15239       break;
15240
15241     case GADGET_ID_SCROLL_HORIZONTAL:
15242       level_xpos = gi->event.item_position - 1;
15243
15244       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15245       BackToFront();
15246
15247       break;
15248
15249     case GADGET_ID_SCROLL_VERTICAL:
15250       level_ypos = gi->event.item_position - 1;
15251
15252       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15253       BackToFront();
15254
15255       break;
15256
15257     case GADGET_ID_SCROLL_LIST_UP:
15258     case GADGET_ID_SCROLL_LIST_DOWN:
15259     case GADGET_ID_SCROLL_LIST_VERTICAL:
15260       if (id == GADGET_ID_SCROLL_LIST_VERTICAL)
15261         element_shift = gi->event.item_position * ED_ELEMENTLIST_BUTTONS_HORIZ;
15262       else
15263       {
15264         step *= (id == GADGET_ID_SCROLL_LIST_UP ? -1 : +1);
15265         element_shift += step * ED_ELEMENTLIST_BUTTONS_HORIZ;
15266
15267         if (element_shift < 0)
15268           element_shift = 0;
15269         if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
15270           element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
15271
15272         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15273                      GDI_SCROLLBAR_ITEM_POSITION,
15274                      element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15275       }
15276
15277       ModifyEditorElementList();
15278
15279       break;
15280
15281     case GADGET_ID_PROPERTIES:
15282       // always switch off element properties when they are already displayed
15283       last_properties_element = new_element;
15284     case GADGET_ID_ELEMENT_LEFT:
15285     case GADGET_ID_ELEMENT_MIDDLE:
15286     case GADGET_ID_ELEMENT_RIGHT:
15287       properties_element = (id == GADGET_ID_ELEMENT_LEFT   ? new_element1 :
15288                             id == GADGET_ID_ELEMENT_MIDDLE ? new_element2 :
15289                             id == GADGET_ID_ELEMENT_RIGHT  ? new_element3 :
15290                             new_element);
15291
15292       if (edit_mode != ED_MODE_PROPERTIES)
15293       {
15294         last_edit_mode = edit_mode;
15295
15296         ChangeEditModeWindow(ED_MODE_PROPERTIES);
15297
15298         last_level_drawing_function = drawing_function;
15299         ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS],
15300                       MB_LEFTBUTTON);
15301       }
15302       else if (properties_element != last_properties_element)
15303       {
15304         DrawEditModeWindow();
15305       }
15306       else
15307       {
15308         ChangeEditModeWindow(ED_MODE_DRAWING);
15309
15310         ClickOnGadget(level_editor_gadget[last_level_drawing_function],
15311                       MB_LEFTBUTTON);
15312       }
15313       break;
15314
15315     case GADGET_ID_PALETTE:
15316       if (edit_mode != ED_MODE_PALETTE)
15317       {
15318         last_edit_mode = edit_mode;
15319
15320         ChangeEditModeWindow(ED_MODE_PALETTE);
15321       }
15322       else
15323       {
15324         ChangeEditModeWindow(last_edit_mode);
15325       }
15326       break;
15327
15328     case GADGET_ID_WRAP_LEFT:
15329       WrapLevel(-step, 0);
15330       break;
15331
15332     case GADGET_ID_WRAP_RIGHT:
15333       WrapLevel(step, 0);
15334       break;
15335
15336     case GADGET_ID_WRAP_UP:
15337       WrapLevel(0, -step);
15338       break;
15339
15340     case GADGET_ID_WRAP_DOWN:
15341       WrapLevel(0, step);
15342       break;
15343
15344     case GADGET_ID_SINGLE_ITEMS:
15345     case GADGET_ID_CONNECTED_ITEMS:
15346     case GADGET_ID_LINE:
15347     case GADGET_ID_ARC:
15348     case GADGET_ID_TEXT:
15349     case GADGET_ID_RECTANGLE:
15350     case GADGET_ID_FILLED_BOX:
15351     case GADGET_ID_FLOOD_FILL:
15352     case GADGET_ID_GRAB_BRUSH:
15353     case GADGET_ID_PICK_ELEMENT:
15354       if (drawing_function != GADGET_ID_PICK_ELEMENT)
15355         last_drawing_function = drawing_function;
15356       drawing_function = id;
15357       draw_with_brush = FALSE;
15358       break;
15359
15360     case GADGET_ID_RANDOM_PLACEMENT:
15361       RandomPlacement(new_element);
15362       break;
15363
15364     case GADGET_ID_ZOOM:
15365       // zoom level editor tile size in or out (or reset to default size)
15366       ed_tilesize = (button == 1 ? ed_tilesize * 2 :
15367                      button == 2 ? ed_tilesize_default :
15368                      button == 3 ? ed_tilesize / 2 : ed_tilesize);
15369
15370       // when using touch device, cycle through all zoom tilesizes
15371       if (runtime.uses_touch_device && ed_tilesize > TILESIZE)
15372         ed_tilesize = MICRO_TILESIZE;
15373
15374       // limit zoom level by upper and lower bound
15375       ed_tilesize = MIN(MAX(MICRO_TILESIZE, ed_tilesize), TILESIZE);
15376
15377       InitZoomLevelSettings(ed_tilesize);
15378
15379       if (edit_mode == ED_MODE_DRAWING)
15380       {
15381         DrawDrawingWindow();
15382
15383         // redraw zoom gadget info text
15384         PrintEditorGadgetInfoText(level_editor_gadget[id]);
15385       }
15386
15387       // save current editor zoom tilesize
15388       SaveSetup_AutoSetup();
15389
15390       break;
15391
15392     case GADGET_ID_CUSTOM_COPY_FROM:
15393     case GADGET_ID_CUSTOM_COPY_TO:
15394     case GADGET_ID_CUSTOM_EXCHANGE:
15395       last_custom_copy_mode = id;
15396       last_drawing_function = drawing_function;
15397       break;
15398
15399     case GADGET_ID_CUSTOM_COPY:
15400       CopyCustomElement(properties_element, -1, id);
15401       break;
15402
15403     case GADGET_ID_CUSTOM_PASTE:
15404       CopyCustomElement(-1, properties_element, id);
15405       break;
15406
15407     case GADGET_ID_UNDO:
15408       if (button < 0)   // keep button value (even if modifier keys are pressed)
15409         button = -button;
15410       else if (button == 1 && GetKeyModState() & (KMOD_Shift | KMOD_Control))
15411         button = 3;
15412
15413       if (button == 1 && undo_buffer_steps == 0)
15414       {
15415         Request("Undo buffer empty!", REQ_CONFIRM);
15416
15417         break;
15418       }
15419       else if (button == 2)
15420       {
15421         break;
15422       }
15423       else if (button == 3 && redo_buffer_steps == 0)
15424       {
15425         Request("Redo buffer empty!", REQ_CONFIRM);
15426
15427         break;
15428       }
15429
15430       if (edit_mode != ED_MODE_DRAWING)
15431         ChangeEditModeWindow(ED_MODE_DRAWING);
15432
15433       if (button == 1)
15434       {
15435         // undo
15436
15437         undo_buffer_position =
15438           (undo_buffer_position - 1 + NUM_UNDO_STEPS) % NUM_UNDO_STEPS;
15439
15440         undo_buffer_steps--;
15441         redo_buffer_steps++;
15442       }
15443       else
15444       {
15445         // redo
15446
15447         undo_buffer_position = (undo_buffer_position + 1) % NUM_UNDO_STEPS;
15448
15449         undo_buffer_steps++;
15450         redo_buffer_steps--;
15451       }
15452
15453       for (x = 0; x < lev_fieldx; x++)
15454         for (y = 0; y < lev_fieldy; y++)
15455           Tile[x][y] = UndoBuffer[undo_buffer_position][x][y];
15456
15457       // check if undo operation forces change of border style
15458       CheckLevelBorderElement(FALSE);
15459
15460       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15461
15462       break;
15463
15464     case GADGET_ID_CONF:
15465       if (edit_mode != ED_MODE_LEVELCONFIG)
15466       {
15467         last_edit_mode = edit_mode;
15468
15469         ChangeEditModeWindow(ED_MODE_LEVELCONFIG);
15470       }
15471       else
15472       {
15473         ChangeEditModeWindow(ED_MODE_DRAWING);
15474       }
15475       break;
15476
15477     case GADGET_ID_CLEAR:
15478       if (edit_mode != ED_MODE_DRAWING)
15479         ChangeEditModeWindow(ED_MODE_DRAWING);
15480
15481       for (x = 0; x < MAX_LEV_FIELDX; x++)
15482         for (y = 0; y < MAX_LEV_FIELDY; y++)
15483           Tile[x][y] = (button == 1 ? EL_EMPTY : new_element);
15484
15485       CopyLevelToUndoBuffer(GADGET_ID_CLEAR);
15486
15487       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15488       break;
15489
15490     case GADGET_ID_SAVE:
15491     {
15492       // saving read-only levels into personal level set modifies global vars
15493       // "leveldir_current" and "level_nr"; restore them after saving level
15494       LevelDirTree *leveldir_former = leveldir_current;
15495       int level_nr_former = level_nr;
15496       char *level_filename;
15497       boolean new_level;
15498
15499       if (leveldir_current->readonly &&
15500           !PrepareSavingIntoPersonalLevelSet())
15501         break;
15502
15503       level_filename = getDefaultLevelFilename(level_nr);
15504       new_level = !fileExists(level_filename);
15505
15506       if (new_level ||
15507           Request("Save this level and kill the old?", REQ_ASK))
15508       {
15509         if (leveldir_former->readonly)
15510           ModifyLevelConfigForSavingIntoPersonalLevelSet(leveldir_former->name);
15511
15512         SetAutomaticNumberOfGemsNeeded();
15513
15514         CopyPlayfield(Tile, level.field);
15515         SaveLevel(level_nr);
15516
15517         level.changed = FALSE;
15518
15519         if (new_level)
15520         {
15521           char level_saved_msg[64];
15522
15523           if (leveldir_former->readonly)
15524             sprintf(level_saved_msg,
15525                     "Level saved as level %d into personal level set!",
15526                     level_nr);
15527           else
15528             strcpy(level_saved_msg, "Level saved!");
15529
15530           Request(level_saved_msg, REQ_CONFIRM);
15531         }
15532       }
15533
15534       // "cd" back to copied-from levelset (in case of saved read-only level)
15535       leveldir_current = leveldir_former;
15536       level_nr = level_nr_former;
15537
15538       break;
15539     }
15540
15541     case GADGET_ID_TEST:
15542       if (LevelChanged())
15543         level.game_version = GAME_VERSION_ACTUAL;
15544
15545       CopyPlayfield(level.field, TileBackup);
15546       CopyPlayfield(Tile, level.field);
15547
15548       CopyNativeLevel_RND_to_Native(&level);
15549
15550       UnmapLevelEditorGadgets();
15551       UndrawSpecialEditorDoor();
15552
15553       CloseDoor(DOOR_CLOSE_ALL);
15554
15555       // needed before playing if editor playfield area has different size
15556       ClearRectangle(drawto, REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
15557
15558       // redraw_mask = REDRAW_ALL;
15559
15560       level_editor_test_game = TRUE;
15561
15562       StartGameActions(FALSE, setup.autorecord, level.random_seed);
15563
15564       break;
15565
15566     case GADGET_ID_EXIT:
15567       RequestExitLevelEditor(TRUE, FALSE);  // if level has changed, ask user
15568       break;
15569
15570     default:
15571       if (id >= GADGET_ID_ELEMENTLIST_FIRST &&
15572           id <= GADGET_ID_ELEMENTLIST_LAST)
15573       {
15574         int element_position = id - GADGET_ID_ELEMENTLIST_FIRST;
15575
15576         new_element = editor_elements[element_position + element_shift];
15577
15578         if (IS_EDITOR_CASCADE(new_element))
15579         {
15580           int i;
15581
15582           for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
15583           {
15584             int *cascade_element = &(*editor_elements_info[i].headline_list)[0];
15585             boolean *cascade_value = editor_elements_info[i].setup_cascade_value;
15586
15587             if (*cascade_element == new_element)
15588             {
15589               *cascade_element = EL_CASCADE_TOGGLE(*cascade_element);
15590               *cascade_value = IS_EDITOR_CASCADE_ACTIVE(*cascade_element);
15591
15592               // update element selection list
15593               ReinitializeElementList();
15594               ModifyEditorElementList();
15595
15596               // update cascading gadget info text
15597               PrintEditorGadgetInfoText(level_editor_gadget[id]);
15598
15599               // save current editor cascading state
15600               SaveSetup_EditorCascade();
15601
15602               break;
15603             }
15604           }
15605
15606           break;
15607         }
15608
15609         if (last_custom_copy_mode != -1)
15610         {
15611           if (CopyCustomElement(properties_element, new_element,
15612                                 last_custom_copy_mode))
15613           {
15614             ClickOnGadget(level_editor_gadget[last_drawing_function],
15615                           MB_LEFTBUTTON);
15616
15617             last_custom_copy_mode = -1;
15618           }
15619
15620           break;
15621         }
15622
15623         // change element of button used to show palette
15624         if (editor.palette.show_on_element_buttons)
15625           button = last_button;
15626
15627         PickDrawingElement(button, new_element);
15628
15629         if (!stick_element_properties_window &&
15630             drawing_function != GADGET_ID_PICK_ELEMENT &&
15631             !(GetKeyModState() & KMOD_Control))
15632         {
15633           properties_element = new_element;
15634           if (edit_mode == ED_MODE_PROPERTIES)
15635             DrawPropertiesWindow();
15636         }
15637
15638         if (drawing_function == GADGET_ID_PICK_ELEMENT)
15639           ClickOnGadget(level_editor_gadget[last_drawing_function],
15640                         MB_LEFTBUTTON);
15641
15642         if (!use_permanent_palette)
15643           ChangeEditModeWindow(last_edit_mode);
15644       }
15645 #ifdef DEBUG
15646       else if (gi->event.type == GD_EVENT_PRESSED)
15647         Debug("editor", "default: HandleControlButtons: GD_EVENT_PRESSED(%d)", id);
15648       else if (gi->event.type == GD_EVENT_RELEASED)
15649         Debug("editor", "default: HandleControlButtons: GD_EVENT_RELEASED(%d)", id);
15650       else if (gi->event.type == GD_EVENT_MOVING)
15651         Debug("editor", "default: HandleControlButtons: GD_EVENT_MOVING(%d)", id);
15652       else
15653         Debug("editor", "default: HandleControlButtons: ? (id == %d)", id);
15654 #endif
15655       break;
15656   }
15657 }
15658
15659 void HandleLevelEditorKeyInput(Key key)
15660 {
15661   char letter = getCharFromKey(key);
15662
15663   if (drawing_function == GADGET_ID_TEXT &&
15664       DrawLevelText(0, 0, 0, TEXT_QUERY_TYPING) == TRUE)
15665   {
15666     if (letter)
15667       DrawLevelText(0, 0, letter, TEXT_WRITECHAR);
15668     else if (key == KSYM_Delete || key == KSYM_BackSpace)
15669       DrawLevelText(0, 0, 0, TEXT_BACKSPACE);
15670     else if (key == KSYM_Return)
15671       DrawLevelText(0, 0, 0, TEXT_NEWLINE);
15672     else if (key == KSYM_Escape)
15673       DrawLevelText(0, 0, 0, TEXT_END);
15674
15675     return;
15676   }
15677
15678   int id = GADGET_ID_NONE;
15679   int new_element_shift = element_shift;
15680   int step = ED_ELEMENTLIST_BUTTONS_VERT - 1;
15681   int button = MB_LEFTBUTTON;
15682   int i;
15683
15684   switch (key)
15685   {
15686     case KSYM_Left:
15687       id = GADGET_ID_SCROLL_LEFT;
15688       break;
15689     case KSYM_Right:
15690       id = GADGET_ID_SCROLL_RIGHT;
15691       break;
15692     case KSYM_Up:
15693       id = GADGET_ID_SCROLL_UP;
15694       break;
15695     case KSYM_Down:
15696       id = GADGET_ID_SCROLL_DOWN;
15697       break;
15698
15699     case KSYM_Page_Up:
15700     case KSYM_Page_Down:
15701       step *= (key == KSYM_Page_Up ? -1 : +1);
15702       element_shift += step * ED_ELEMENTLIST_BUTTONS_HORIZ;
15703
15704       if (element_shift < 0)
15705         element_shift = 0;
15706       if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
15707         element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
15708
15709       ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15710                    GDI_SCROLLBAR_ITEM_POSITION,
15711                    element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15712
15713       ModifyEditorElementList();
15714
15715       break;
15716
15717     case KSYM_Home:
15718     case KSYM_End:
15719       element_shift = (key == KSYM_Home ? 0 :
15720                        num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS);
15721
15722       ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15723                    GDI_SCROLLBAR_ITEM_POSITION,
15724                    element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15725
15726       ModifyEditorElementList();
15727
15728       break;
15729
15730     case KSYM_Insert:
15731     case KSYM_Delete:
15732
15733       // this is needed to prevent interference with running "True X-Mouse"
15734       if (GetKeyModStateFromEvents() & KMOD_Control)
15735         break;
15736
15737       // check for last or next editor cascade block in element list
15738       for (i = 0; i < num_editor_elements; i++)
15739       {
15740         if ((key == KSYM_Insert && i == element_shift) ||
15741             (key == KSYM_Delete && new_element_shift > element_shift))
15742           break;
15743
15744         // jump to next cascade block (or to start of element list)
15745         if (i == 0 || IS_EDITOR_CASCADE(editor_elements[i]))
15746           new_element_shift = i;
15747       }
15748
15749       if (i < num_editor_elements)
15750         element_shift = new_element_shift;
15751
15752       if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
15753         element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
15754
15755       ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15756                    GDI_SCROLLBAR_ITEM_POSITION,
15757                    element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15758
15759       ModifyEditorElementList();
15760
15761       break;
15762
15763     case KSYM_Escape:
15764       if (edit_mode == ED_MODE_DRAWING)
15765         RequestExitLevelEditor(setup.ask_on_escape_editor, TRUE);
15766       else if (edit_mode == ED_MODE_LEVELCONFIG)
15767         HandleControlButtons(level_editor_gadget[GADGET_ID_CONF]);
15768       else if (edit_mode == ED_MODE_PROPERTIES)
15769         HandleControlButtons(level_editor_gadget[GADGET_ID_PROPERTIES]);
15770       else if (edit_mode == ED_MODE_PALETTE)
15771         HandleControlButtons(level_editor_gadget[GADGET_ID_PALETTE]);
15772       else              // should never happen
15773         ChangeEditModeWindow(ED_MODE_DRAWING);
15774
15775       break;
15776
15777     default:
15778       break;
15779   }
15780
15781   if (id != GADGET_ID_NONE)
15782     ClickOnGadget(level_editor_gadget[id], button);
15783   else if (letter == '1' || letter == '?')
15784     ClickOnGadget(level_editor_gadget[GADGET_ID_ELEMENT_LEFT], button);
15785   else if (letter == '2')
15786     ClickOnGadget(level_editor_gadget[GADGET_ID_ELEMENT_MIDDLE], button);
15787   else if (letter == '3')
15788     ClickOnGadget(level_editor_gadget[GADGET_ID_ELEMENT_RIGHT], button);
15789   else if (letter == '.')
15790     ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS], button);
15791   else if (letter == 'U')
15792     ClickOnGadget(level_editor_gadget[GADGET_ID_UNDO], 3);
15793   else if (letter == '-' || key == KSYM_KP_Subtract)
15794     ClickOnGadget(level_editor_gadget[GADGET_ID_ZOOM], 3);
15795   else if (letter == '0' || key == KSYM_KP_0)
15796     ClickOnGadget(level_editor_gadget[GADGET_ID_ZOOM], 2);
15797   else if (letter == '+' || key == KSYM_KP_Add ||
15798            letter == '=')       // ("Shift-=" is "+" on US keyboards)
15799     ClickOnGadget(level_editor_gadget[GADGET_ID_ZOOM], 1);
15800   else if (key == KSYM_Return ||
15801            key == KSYM_space ||
15802            key == setup.shortcut.toggle_pause)
15803     ClickOnGadget(level_editor_gadget[GADGET_ID_TEST], button);
15804   else
15805     for (i = 0; i < ED_NUM_CTRL_BUTTONS; i++)
15806       if (letter && letter == controlbutton_info[i].shortcut)
15807         if (!anyTextGadgetActive())
15808           ClickOnGadget(level_editor_gadget[i], button);
15809
15810   if (draw_with_brush)
15811   {
15812     if (letter == 'x')
15813       FlipBrushX();
15814     else if (letter == 'y')
15815       FlipBrushY();
15816     else if (letter == 'z')
15817       RotateBrush();
15818   }
15819 }
15820
15821 static void HandleLevelEditorIdle_Properties(void)
15822 {
15823   int element_border = graphic_info[IMG_EDITOR_ELEMENT_BORDER].border_size;
15824   int x = editor.settings.element_graphic.x + element_border;
15825   int y = editor.settings.element_graphic.y + element_border;
15826   static DelayCounter action_delay = { 0 };
15827   int i;
15828
15829   action_delay.value = GameFrameDelay;
15830
15831   if (!DelayReached(&action_delay))
15832     return;
15833
15834   for (i = 0; i < ED_NUM_SELECTBOX; i++)
15835   {
15836     struct GadgetInfo *gi = level_editor_gadget[selectbox_info[i].gadget_id];
15837
15838     if (gi->mapped && gi->active && gi->selectbox.open)
15839       return;
15840   }
15841
15842   DrawEditorElementAnimation(SX + x, SY + y);
15843
15844   redraw_mask |= REDRAW_FIELD;
15845
15846   FrameCounter++;       // increase animation frame counter
15847 }
15848
15849 static void HandleLevelEditorIdle_Drawing(void)
15850 {
15851   static boolean last_highlighted = FALSE;
15852   static boolean last_highlighted_similar = FALSE;
15853   boolean highlighted = (GetKeyModState() & KMOD_Alt);
15854   boolean highlighted_similar = (GetKeyModState() & KMOD_Shift);
15855
15856   if (highlighted != last_highlighted ||
15857       (highlighted && highlighted_similar != last_highlighted_similar))
15858   {
15859     DrawAreaElementHighlight(highlighted, highlighted_similar);
15860
15861     redraw_mask |= REDRAW_FIELD;
15862   }
15863
15864   last_highlighted = highlighted;
15865   last_highlighted_similar = highlighted_similar;
15866 }
15867
15868 void HandleLevelEditorIdle(void)
15869 {
15870   if (edit_mode == ED_MODE_PROPERTIES)
15871     HandleLevelEditorIdle_Properties();
15872   else if (edit_mode == ED_MODE_DRAWING)
15873     HandleLevelEditorIdle_Drawing();
15874 }
15875
15876 static void ClearEditorGadgetInfoText(void)
15877 {
15878   DrawBackground(INFOTEXT_XPOS, INFOTEXT_YPOS, INFOTEXT_XSIZE, INFOTEXT_YSIZE);
15879 }
15880
15881 void PrintEditorGadgetInfoText(struct GadgetInfo *gi)
15882 {
15883   char infotext[MAX_OUTPUT_LINESIZE + 1];
15884   int max_infotext_len = getMaxInfoTextLength();
15885
15886   if (gi == NULL || strlen(gi->info_text) == 0)
15887     return;
15888
15889   strncpy(infotext, gi->info_text, max_infotext_len);
15890   infotext[max_infotext_len] = '\0';
15891
15892   if (gi->custom_id < ED_NUM_CTRL_BUTTONS)
15893   {
15894     int key = controlbutton_info[gi->custom_id].shortcut;
15895
15896     if (key)
15897     {
15898       char shortcut[MAX_OUTPUT_LINESIZE + 1];
15899
15900       if (gi->custom_id == GADGET_ID_SINGLE_ITEMS)
15901         sprintf(shortcut, " ('.' or '%c')", key);
15902       else if (gi->custom_id == GADGET_ID_PICK_ELEMENT)
15903         sprintf(shortcut, " ('%c' or 'Ctrl')", key);
15904       else if (gi->custom_id == GADGET_ID_TEST)
15905         sprintf(shortcut, " ('Enter' or 'Shift-%c')", key);
15906       else if (gi->custom_id == GADGET_ID_UNDO)
15907         sprintf(shortcut, " ('%c/Shift-U')", key);
15908       else if (gi->custom_id == GADGET_ID_ZOOM)
15909         sprintf(shortcut, " ('%c', '0', '-')", key);
15910       else
15911         sprintf(shortcut, " ('%s%c')",
15912                 (key >= 'A' && key <= 'Z' ? "Shift-" : ""), key);
15913
15914       if (strlen(infotext) + strlen(shortcut) <= max_infotext_len)
15915         strcat(infotext, shortcut);
15916     }
15917   }
15918
15919   DrawText(INFOTEXT_XPOS, INFOTEXT_YPOS, infotext, INFOTEXT_FONT);
15920 }
15921
15922 void HandleEditorGadgetInfoText(void *ptr)
15923 {
15924   struct GadgetInfo *gi = (struct GadgetInfo *)ptr;
15925
15926   if (game_status != GAME_MODE_EDITOR)
15927     return;
15928
15929   ClearEditorGadgetInfoText();
15930
15931   if (gi == NULL || gi->event.type == GD_EVENT_INFO_LEAVING)
15932     return;
15933
15934   // misuse this function to delete brush cursor, if needed
15935   if (edit_mode == ED_MODE_DRAWING && draw_with_brush)
15936     DeleteBrushFromCursor();
15937
15938   PrintEditorGadgetInfoText(gi);
15939 }
15940
15941 static void HandleDrawingAreaInfo(struct GadgetInfo *gi)
15942 {
15943   int id = gi->custom_id;
15944   int type_id = gi->custom_type_id;
15945   int sx = gi->event.x;
15946   int sy = gi->event.y;
15947   int lx = sx + level_xpos;
15948   int ly = sy + level_ypos;
15949   int min_sx = 0, min_sy = 0;
15950   int max_sx = gi->drawing.area_xsize - 1;
15951   int max_sy = gi->drawing.area_ysize - 1;
15952   int actual_drawing_function = drawing_function;
15953   int max_infotext_len = getMaxInfoTextLength();
15954   char infotext[MAX_OUTPUT_LINESIZE + 1];
15955
15956   infotext[0] = '\0';           // start with empty info text
15957
15958   // pressed Control key: simulate picking element
15959   if (GetKeyModState() & KMOD_Control)
15960     actual_drawing_function = GADGET_ID_PICK_ELEMENT;
15961
15962   ClearEditorGadgetInfoText();
15963
15964   if (gi->event.type == GD_EVENT_INFO_LEAVING)
15965     return;
15966
15967   // make sure to stay inside drawing area boundaries
15968   sx = (sx < min_sx ? min_sx : sx > max_sx ? max_sx : sx);
15969   sy = (sy < min_sy ? min_sy : sy > max_sy ? max_sy : sy);
15970
15971   if (id == GADGET_ID_DRAWING_LEVEL)
15972   {
15973     if (button_status)
15974     {
15975       int min_lx = 0, min_ly = 0;
15976       int max_lx = lev_fieldx - 1, max_ly = lev_fieldy - 1;
15977
15978       // get positions inside level field
15979       lx = sx + level_xpos;
15980       ly = sy + level_ypos;
15981
15982       // make sure to stay inside level field boundaries
15983       lx = (lx < min_lx ? min_lx : lx > max_lx ? max_lx : lx);
15984       ly = (ly < min_ly ? min_ly : ly > max_ly ? max_ly : ly);
15985
15986       // correct drawing area positions accordingly
15987       sx = lx - level_xpos;
15988       sy = ly - level_ypos;
15989     }
15990
15991     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
15992     {
15993       if (button_status)        // if (gi->state == GD_BUTTON_PRESSED)
15994       {
15995         static int start_lx = 0;
15996         static int start_ly = 0;
15997         char *text;
15998
15999         if (gi->event.type == GD_EVENT_PRESSED)
16000         {
16001           start_lx = lx;
16002           start_ly = ly;
16003         }
16004
16005         switch (actual_drawing_function)
16006         {
16007           case GADGET_ID_SINGLE_ITEMS:
16008             text = "Drawing single items";
16009             break;
16010           case GADGET_ID_CONNECTED_ITEMS:
16011             text = "Drawing connected items";
16012             break;
16013           case GADGET_ID_LINE:
16014             text = "Drawing line";
16015             break;
16016           case GADGET_ID_ARC:
16017             text = "Drawing arc";
16018             break;
16019           case GADGET_ID_TEXT:
16020             text = "Setting text cursor";
16021             break;
16022           case GADGET_ID_RECTANGLE:
16023             text = "Drawing rectangle";
16024             break;
16025           case GADGET_ID_FILLED_BOX:
16026             text = "Drawing filled box";
16027             break;
16028           case GADGET_ID_FLOOD_FILL:
16029             text = "Flood fill";
16030             break;
16031           case GADGET_ID_GRAB_BRUSH:
16032             text = "Grabbing brush";
16033             break;
16034           case GADGET_ID_PICK_ELEMENT:
16035             text = "Picking element";
16036             break;
16037
16038           default:
16039             text = "Drawing position";
16040             break;
16041         }
16042
16043         if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
16044           sprintf(infotext, "%s: %d, %d", text, lx, ly);
16045         else
16046           sprintf(infotext, "%s: %d, %d", text,
16047                   ABS(lx - start_lx) + 1, ABS(ly - start_ly) + 1);
16048       }
16049       else if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
16050         strncpy(infotext, getElementInfoText(Tile[lx][ly]), max_infotext_len);
16051       else
16052         sprintf(infotext, "Level position: %d, %d", lx, ly);
16053     }
16054
16055     // misuse this function to draw brush cursor, if needed
16056     if (edit_mode == ED_MODE_DRAWING && draw_with_brush && !button_status)
16057     {
16058       if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
16059         CopyBrushToCursor(sx, sy);
16060       else
16061         DeleteBrushFromCursor();
16062     }
16063
16064     if (!draw_with_brush)
16065       UpdateBrushPosition(sx, sy);
16066   }
16067   else if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
16068   {
16069     int pos = sx * drawingarea_info[type_id].area_ysize + sy;
16070     int element = drawingarea_info[type_id].value[pos];
16071
16072     strncpy(infotext, getElementInfoText(element), max_infotext_len);
16073   }
16074   else
16075   {
16076     if (id == GADGET_ID_CUSTOM_CONTENT)
16077       sprintf(infotext, "custom element content position: %d, %d", sx, sy);
16078     else if (id == GADGET_ID_GROUP_CONTENT)
16079       sprintf(infotext, "group element position: %d", sx + 1);
16080     else if (id >= GADGET_ID_YAMYAM_CONTENT_0 &&
16081              id <= GADGET_ID_YAMYAM_CONTENT_7)
16082       sprintf(infotext, "content area %d position: %d, %d",
16083               id - GADGET_ID_YAMYAM_CONTENT_0 + 1, sx, sy);
16084     else if (id >= GADGET_ID_MAGIC_BALL_CONTENT_0 &&
16085              id <= GADGET_ID_MAGIC_BALL_CONTENT_7)
16086       sprintf(infotext, "content area %d position: %d, %d",
16087               id - GADGET_ID_MAGIC_BALL_CONTENT_0 + 1, sx, sy);
16088     else if (id == GADGET_ID_ANDROID_CONTENT)
16089       sprintf(infotext, "android element position: %d", sx + 1);
16090     else if (drawingarea_info[type_id].infotext != NULL)
16091       strcpy(infotext, drawingarea_info[type_id].infotext);
16092   }
16093
16094   infotext[max_infotext_len] = '\0';
16095
16096   if (strlen(infotext) > 0)
16097     DrawTextS(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY, INFOTEXT_FONT, infotext);
16098 }
16099
16100 void RequestExitLevelEditor(boolean ask_if_level_has_changed,
16101                             boolean quick_quit)
16102 {
16103   if (!ask_if_level_has_changed ||
16104       !LevelChanged() ||
16105       Request("Level has changed! Exit without saving?",
16106               REQ_ASK | REQ_STAY_OPEN))
16107   {
16108     struct RectWithBorder *vp_door_1 = &viewport.door_1[GAME_MODE_MAIN];
16109     struct RectWithBorder *vp_door_2 = &viewport.door_2[GAME_MODE_MAIN];
16110
16111     // draw normal door
16112     UndrawSpecialEditorDoor();
16113
16114     // use door animation if door 1 viewport is unchanged and contains toolbox
16115     if (useEditorDoorAnimation())
16116       CloseDoor(DOOR_CLOSE_1 | DOOR_FORCE_ANIM);
16117
16118     // close editor doors if viewport definition is the same as in main menu
16119     if (vp_door_1->x      == DX     &&
16120         vp_door_1->y      == DY     &&
16121         vp_door_1->width  == DXSIZE &&
16122         vp_door_1->height == DYSIZE &&
16123         vp_door_2->x      == VX     &&
16124         vp_door_2->y      == VY     &&
16125         vp_door_2->width  == VXSIZE &&
16126         vp_door_2->height == VYSIZE)
16127       CloseDoor(DOOR_CLOSE_ALL | DOOR_NO_DELAY);
16128     else
16129       SetDoorState(DOOR_CLOSE_ALL);
16130
16131     BackToFront();
16132
16133     if (quick_quit)
16134       FadeSkipNextFadeIn();
16135
16136     SetGameStatus(GAME_MODE_MAIN);
16137
16138     DrawMainMenu();
16139   }
16140   else
16141   {
16142     if (!global.use_envelope_request)
16143     {
16144       CloseDoor(DOOR_CLOSE_1);
16145       OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
16146     }
16147   }
16148 }