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