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