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