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