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