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