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