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