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