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