increased scrolling speed when wrapping around for BD engine
[rocksndiamonds.git] / src / editor.c
1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // editor.c
10 // ============================================================================
11
12 #include <math.h>
13
14 #include "libgame/libgame.h"
15
16 #include "editor.h"
17 #include "screens.h"
18 #include "tools.h"
19 #include "files.h"
20 #include "game.h"
21 #include "init.h"
22 #include "tape.h"
23
24
25 #define INFOTEXT_UNKNOWN_ELEMENT        "unknown"
26
27
28 // ----------------------------------------------------------------------------
29 // screen and artwork graphic pixel position definitions
30 // ----------------------------------------------------------------------------
31
32 // values for the control window
33 #define ED_CTRL1_BUTTONS_HORIZ          4       // toolbox
34 #define ED_CTRL1_BUTTONS_VERT           4
35 #define ED_CTRL2_BUTTONS_HORIZ          3       // level
36 #define ED_CTRL2_BUTTONS_VERT           2
37 #define ED_CTRL3_BUTTONS_HORIZ          3       // CE and GE
38 #define ED_CTRL3_BUTTONS_VERT           1
39 #define ED_CTRL4_BUTTONS_HORIZ          2       // CE and GE
40 #define ED_CTRL4_BUTTONS_VERT           1
41 #define ED_CTRL5_BUTTONS_HORIZ          1       // properties
42 #define ED_CTRL5_BUTTONS_VERT           1
43 #define ED_CTRL6_BUTTONS_HORIZ          3       // properties
44 #define ED_CTRL6_BUTTONS_VERT           1
45 #define ED_CTRL7_BUTTONS_HORIZ          1       // palette
46 #define ED_CTRL7_BUTTONS_VERT           1
47
48 #define ED_NUM_CTRL1_BUTTONS   (ED_CTRL1_BUTTONS_HORIZ * ED_CTRL1_BUTTONS_VERT)
49 #define ED_NUM_CTRL2_BUTTONS   (ED_CTRL2_BUTTONS_HORIZ * ED_CTRL2_BUTTONS_VERT)
50 #define ED_NUM_CTRL3_BUTTONS   (ED_CTRL3_BUTTONS_HORIZ * ED_CTRL3_BUTTONS_VERT)
51 #define ED_NUM_CTRL4_BUTTONS   (ED_CTRL4_BUTTONS_HORIZ * ED_CTRL4_BUTTONS_VERT)
52 #define ED_NUM_CTRL5_BUTTONS   (ED_CTRL5_BUTTONS_HORIZ * ED_CTRL5_BUTTONS_VERT)
53 #define ED_NUM_CTRL6_BUTTONS   (ED_CTRL6_BUTTONS_HORIZ * ED_CTRL6_BUTTONS_VERT)
54 #define ED_NUM_CTRL7_BUTTONS   (ED_CTRL7_BUTTONS_HORIZ * ED_CTRL7_BUTTONS_VERT)
55 #define ED_NUM_CTRL1_2_BUTTONS (ED_NUM_CTRL1_BUTTONS   + ED_NUM_CTRL2_BUTTONS)
56 #define ED_NUM_CTRL1_3_BUTTONS (ED_NUM_CTRL1_2_BUTTONS + ED_NUM_CTRL3_BUTTONS)
57 #define ED_NUM_CTRL1_4_BUTTONS (ED_NUM_CTRL1_3_BUTTONS + ED_NUM_CTRL4_BUTTONS)
58 #define ED_NUM_CTRL1_5_BUTTONS (ED_NUM_CTRL1_4_BUTTONS + ED_NUM_CTRL5_BUTTONS)
59 #define ED_NUM_CTRL1_6_BUTTONS (ED_NUM_CTRL1_5_BUTTONS + ED_NUM_CTRL6_BUTTONS)
60 #define ED_NUM_CTRL1_7_BUTTONS (ED_NUM_CTRL1_6_BUTTONS + ED_NUM_CTRL7_BUTTONS)
61 #define ED_NUM_CTRL_BUTTONS    ED_NUM_CTRL1_7_BUTTONS
62
63 // values for the element list
64 #define ED_ELEMENTLIST_XPOS             (editor.palette.x)
65 #define ED_ELEMENTLIST_YPOS             (editor.palette.y)
66 #define ED_ELEMENTLIST_XSIZE            (graphic_info[IMG_EDITOR_PALETTE_BUTTON].width)
67 #define ED_ELEMENTLIST_YSIZE            (graphic_info[IMG_EDITOR_PALETTE_BUTTON].height)
68 #define ED_ELEMENTLIST_COLS             MAX(1, editor.palette.cols)
69 #define ED_ELEMENTLIST_ROWS             MAX(1, editor.palette.rows)
70 #define ED_ELEMENTLIST_BUTTONS_HORIZ    (ED_ELEMENTLIST_COLS)
71 #define ED_ELEMENTLIST_BUTTONS_VERT     (ED_ELEMENTLIST_ROWS)
72 #define ED_NUM_ELEMENTLIST_BUTTONS      (ED_ELEMENTLIST_BUTTONS_HORIZ * \
73                                          ED_ELEMENTLIST_BUTTONS_VERT)
74
75 // standard distances
76 #define ED_GADGET_NORMAL_DISTANCE       (editor.gadget.normal_spacing)
77 #define ED_GADGET_SMALL_DISTANCE        (editor.gadget.small_spacing)
78 #define ED_GADGET_TINY_DISTANCE         (editor.gadget.tiny_spacing)
79 #define ED_GADGET_LINE_DISTANCE         (editor.gadget.line_spacing)
80 #define ED_GADGET_TEXT_DISTANCE         (editor.gadget.text_spacing)
81 #define ED_TAB_BAR_HEIGHT               (editor.gadget.separator_line.height)
82 #define ED_DRAWINGAREA_TEXT_DISTANCE    (ED_GADGET_TEXT_DISTANCE +      \
83                                          ED_DRAWINGAREA_BORDER_SIZE)
84 #define ED_GADGET_SPACE_DISTANCE        (getFontWidth(FONT_TEXT_1))
85
86 // values for drawingarea gadgets
87 #define IMG_BORDER_1                    IMG_EDITOR_ELEMENT_BORDER
88 #define IMG_BORDER_2                    IMG_EDITOR_ELEMENT_BORDER_INPUT
89 #define ED_ELEMENT_BORDER               (graphic_info[IMG_BORDER_1].border_size)
90 #define ED_DRAWINGAREA_BORDER_SIZE      (graphic_info[IMG_BORDER_2].border_size)
91 #define ED_DRAWINGAREA_TILE_SIZE        (editor.drawingarea.tile_size)
92
93 // values for checkbutton gadgets
94 #define ED_CHECKBUTTON_XSIZE         (graphic_info[IMG_EDITOR_CHECKBOX].width)
95 #define ED_CHECKBUTTON_YSIZE         (graphic_info[IMG_EDITOR_CHECKBOX].height)
96
97 #define ED_TABBUTTON_XSIZE           (graphic_info[IMG_EDITOR_TABBUTTON].width)
98 #define ED_TABBUTTON_YSIZE           (graphic_info[IMG_EDITOR_TABBUTTON].height)
99
100 #define ED_SETTINGS_LEVEL_TABS_X        (editor.settings.tabs.x)
101 #define ED_SETTINGS_LEVEL_TABS_Y        (editor.settings.tabs.y)
102 #define ED_SETTINGS_ELEMENT_TABS_X      (editor.settings.tabs.x)
103 #define ED_SETTINGS_ELEMENT_TABS_Y      (editor.settings.tabs.y +       \
104                                          editor.settings.tabs.yoffset2)
105
106 #define ED_SETTINGS_TABS_XOFFSET        (editor.settings.tabs.draw_xoffset)
107 #define ED_SETTINGS_TABS_YOFFSET        (editor.settings.tabs.draw_yoffset)
108
109 #define ED_LEVEL_TABS_XSTART            (ED_SETTINGS_LEVEL_TABS_X)
110 #define ED_LEVEL_TABS_YSTART            (ED_SETTINGS_LEVEL_TABS_Y)
111 #define ED_LEVEL_SETTINGS_XSTART        (ED_SETTINGS_LEVEL_TABS_X +     \
112                                          ED_SETTINGS_TABS_XOFFSET)
113 #define ED_LEVEL_SETTINGS_YSTART        (ED_SETTINGS_LEVEL_TABS_Y +     \
114                                          ED_TABBUTTON_YSIZE +           \
115                                          ED_GADGET_TINY_DISTANCE +      \
116                                          ED_TAB_BAR_HEIGHT +            \
117                                          ED_SETTINGS_TABS_YOFFSET +     \
118                                          getFontHeight(FONT_TEXT_1) +   \
119                                          ED_GADGET_TEXT_DISTANCE)
120 #define ED_ELEMENT_TABS_XSTART          (ED_SETTINGS_ELEMENT_TABS_X)
121 #define ED_ELEMENT_TABS_YSTART          (ED_SETTINGS_ELEMENT_TABS_Y)
122 #define ED_ELEMENT_SETTINGS_XSTART      (ED_SETTINGS_ELEMENT_TABS_X +   \
123                                          ED_SETTINGS_TABS_XOFFSET)
124 #define ED_ELEMENT_SETTINGS_YSTART      (ED_SETTINGS_ELEMENT_TABS_Y +   \
125                                          ED_TABBUTTON_YSIZE +           \
126                                          ED_GADGET_TINY_DISTANCE +      \
127                                          ED_TAB_BAR_HEIGHT +            \
128                                          ED_SETTINGS_TABS_YOFFSET)
129
130 #define ED_SETTINGS_XOFFSET             (ED_CHECKBUTTON_XSIZE +         \
131                                          ED_GADGET_TEXT_DISTANCE)
132 #define ED_SETTINGS_YOFFSET             (ED_CHECKBUTTON_YSIZE +         \
133                                          ED_GADGET_LINE_DISTANCE)
134
135 #define ED_POS_RANGE                    (10000)
136 #define ED_POS_LEVEL_TABS_FIRST         (1 * ED_POS_RANGE)
137 #define ED_POS_LEVEL_TABS_LAST          (2 * ED_POS_RANGE - 1)
138 #define ED_POS_LEVEL_SETTINGS_FIRST     (2 * ED_POS_RANGE)
139 #define ED_POS_LEVEL_SETTINGS_LAST      (3 * ED_POS_RANGE - 1)
140 #define ED_POS_ELEMENT_TABS_FIRST       (3 * ED_POS_RANGE)
141 #define ED_POS_ELEMENT_TABS_LAST        (4 * ED_POS_RANGE - 1)
142 #define ED_POS_ELEMENT_SETTINGS_FIRST   (4 * ED_POS_RANGE)
143 #define ED_POS_ELEMENT_SETTINGS_LAST    (5 * ED_POS_RANGE - 1)
144
145 #define ED_LEVEL_TABS_XPOS(n)           (ED_POS_LEVEL_TABS_FIRST + (n))
146 #define ED_LEVEL_TABS_YPOS(n)           (ED_POS_LEVEL_TABS_FIRST + (n))
147
148 #define ED_LEVEL_SETTINGS_XPOS(n)       (ED_POS_LEVEL_SETTINGS_FIRST + (n))
149 #define ED_LEVEL_SETTINGS_YPOS(n)       (ED_POS_LEVEL_SETTINGS_FIRST + (n))
150
151 #define ED_ELEMENT_TABS_XPOS(n)         (ED_POS_ELEMENT_TABS_FIRST + (n))
152 #define ED_ELEMENT_TABS_YPOS(n)         (ED_POS_ELEMENT_TABS_FIRST + (n))
153
154 #define ED_ELEMENT_SETTINGS_XPOS(n)     (ED_POS_ELEMENT_SETTINGS_FIRST + (n))
155 #define ED_ELEMENT_SETTINGS_YPOS(n)     (ED_POS_ELEMENT_SETTINGS_FIRST + (n))
156
157 #define IS_POS_LEVEL_TABS(n)          ((n) >= ED_POS_LEVEL_TABS_FIRST && \
158                                        (n) <= ED_POS_LEVEL_TABS_LAST)
159 #define IS_POS_LEVEL_SETTINGS(n)      ((n) >= ED_POS_LEVEL_SETTINGS_FIRST && \
160                                        (n) <= ED_POS_LEVEL_SETTINGS_LAST)
161 #define IS_POS_ELEMENT_TABS(n)        ((n) >= ED_POS_ELEMENT_TABS_FIRST && \
162                                        (n) <= ED_POS_ELEMENT_TABS_LAST)
163 #define IS_POS_ELEMENT_SETTINGS(n)    ((n) >= ED_POS_ELEMENT_SETTINGS_FIRST && \
164                                        (n) <= ED_POS_ELEMENT_SETTINGS_LAST)
165
166 #define ED_LEVEL_TABS_LINE(n)           ((n) - ED_POS_LEVEL_TABS_FIRST)
167 #define ED_LEVEL_SETTINGS_LINE(n)       ((n) - ED_POS_LEVEL_SETTINGS_FIRST)
168 #define ED_ELEMENT_TABS_LINE(n)         ((n) - ED_POS_ELEMENT_TABS_FIRST)
169 #define ED_ELEMENT_SETTINGS_LINE(n)     ((n) - ED_POS_ELEMENT_SETTINGS_FIRST)
170
171 #define ED_LEVEL_TABS_X(n)              (ED_LEVEL_TABS_XSTART + \
172                                          (n) * ED_SETTINGS_TABS_XOFFSET)
173 #define ED_LEVEL_TABS_Y(n)              (ED_LEVEL_TABS_YSTART + \
174                                          (n) * ED_SETTINGS_TABS_YOFFSET)
175
176 #define ED_LEVEL_SETTINGS_X(n)          (ED_LEVEL_SETTINGS_XSTART +     \
177                                          (n) * ED_SETTINGS_XOFFSET)
178 #define ED_LEVEL_SETTINGS_Y(n)          (ED_LEVEL_SETTINGS_YSTART +     \
179                                          (n) * ED_SETTINGS_YOFFSET)
180
181 #define ED_ELEMENT_TABS_X(n)            (ED_ELEMENT_TABS_XSTART +       \
182                                          (n) * ED_SETTINGS_TABS_XOFFSET)
183 #define ED_ELEMENT_TABS_Y(n)            (ED_ELEMENT_TABS_YSTART +       \
184                                          (n) * ED_SETTINGS_TABS_YOFFSET)
185
186 #define ED_ELEMENT_SETTINGS_X(n)        (ED_ELEMENT_SETTINGS_XSTART +   \
187                                          (n) * ED_SETTINGS_XOFFSET)
188 #define ED_ELEMENT_SETTINGS_Y(n)        (ED_ELEMENT_SETTINGS_YSTART +   \
189                                          (n) * ED_SETTINGS_YOFFSET)
190
191 #define ED_POS_TO_LEVEL_TABS_X(n)       \
192   (ED_LEVEL_TABS_X(ED_LEVEL_TABS_LINE(n)))
193 #define ED_POS_TO_LEVEL_TABS_Y(n)       \
194   (ED_LEVEL_TABS_Y(ED_LEVEL_TABS_LINE(n)))
195
196 #define ED_POS_TO_LEVEL_SETTINGS_X(n)   \
197   (ED_LEVEL_SETTINGS_X(ED_LEVEL_SETTINGS_LINE(n)))
198 #define ED_POS_TO_LEVEL_SETTINGS_Y(n)   \
199   (ED_LEVEL_SETTINGS_Y(ED_LEVEL_SETTINGS_LINE(n)))
200
201 #define ED_POS_TO_ELEMENT_TABS_X(n)     \
202   (ED_ELEMENT_TABS_X(ED_ELEMENT_TABS_LINE(n)))
203 #define ED_POS_TO_ELEMENT_TABS_Y(n)     \
204   (ED_ELEMENT_TABS_Y(ED_ELEMENT_TABS_LINE(n)))
205
206 #define ED_POS_TO_ELEMENT_SETTINGS_X(n) \
207   (ED_ELEMENT_SETTINGS_X(ED_ELEMENT_SETTINGS_LINE(n)))
208 #define ED_POS_TO_ELEMENT_SETTINGS_Y(n) \
209   (ED_ELEMENT_SETTINGS_Y(ED_ELEMENT_SETTINGS_LINE(n)))
210
211 #define ED_SETTINGS_X(n)                (IS_POS_LEVEL_TABS(n) ? \
212                                          ED_POS_TO_LEVEL_TABS_X(n) : \
213                                          IS_POS_LEVEL_SETTINGS(n) ?     \
214                                          ED_POS_TO_LEVEL_SETTINGS_X(n) : \
215                                          IS_POS_ELEMENT_TABS(n) ?       \
216                                          ED_POS_TO_ELEMENT_TABS_X(n) : \
217                                          IS_POS_ELEMENT_SETTINGS(n) ?   \
218                                          ED_POS_TO_ELEMENT_SETTINGS_X(n) : (n))
219 #define ED_SETTINGS_Y(n)                (IS_POS_LEVEL_TABS(n) ? \
220                                          ED_POS_TO_LEVEL_TABS_Y(n) : \
221                                          IS_POS_LEVEL_SETTINGS(n) ?     \
222                                          ED_POS_TO_LEVEL_SETTINGS_Y(n) : \
223                                          IS_POS_ELEMENT_TABS(n) ?       \
224                                          ED_POS_TO_ELEMENT_TABS_Y(n) : \
225                                          IS_POS_ELEMENT_SETTINGS(n) ?   \
226                                          ED_POS_TO_ELEMENT_SETTINGS_Y(n) : (n))
227
228 #define ED_SETTINGS_XOFF(n)             (5 * ((n) % 4) *                \
229                                          ED_DRAWINGAREA_TILE_SIZE)
230 #define ED_SETTINGS_YOFF(n)             (5 * ((n) / 4) *                \
231                                          ED_DRAWINGAREA_TILE_SIZE)
232
233 #define ED_AREA_XOFFSET_1(n)            ((n) != 0 ?                     \
234                                          ED_DRAWINGAREA_BORDER_SIZE : 0)
235 #define ED_AREA_YOFFSET_1(n)            ((n) != 0 ?                     \
236                                          (ED_CHECKBUTTON_YSIZE -        \
237                                           ED_DRAWINGAREA_TILE_SIZE) / 2 : 0)
238
239 #define ED_AREA_XOFFSET_2(n)      (0)
240 #define ED_AREA_YOFFSET_2(n)      ((n) == 3 ?                   \
241                                    ((n) - 1) * ED_DRAWINGAREA_TILE_SIZE / 2 : 0)
242
243 #define ED_AREA_SETTINGS_X(i)      (ED_SETTINGS_X((i).x) +              \
244                                     ED_SETTINGS_XOFF((i).xoffset) +     \
245                                     ED_AREA_XOFFSET_1((i).x) -          \
246                                     ED_AREA_XOFFSET_2((i).area_xsize))
247 #define ED_AREA_SETTINGS_Y(i)      (ED_SETTINGS_Y((i).y) +              \
248                                     ED_SETTINGS_YOFF((i).yoffset) +     \
249                                     ED_AREA_YOFFSET_1((i).y) -          \
250                                     ED_AREA_YOFFSET_2((i).area_ysize))
251
252 // values for element content drawing areas
253 #define ED_AREA_1X1_LSETTINGS_XPOS(n)   ED_LEVEL_SETTINGS_XPOS(n)
254 #define ED_AREA_1X1_LSETTINGS_YPOS(n)   ED_LEVEL_SETTINGS_YPOS(n)
255 #define ED_AREA_1X1_LSETTINGS_XOFF      (0)
256 #define ED_AREA_1X1_LSETTINGS_YOFF      (0)
257
258 #define ED_AREA_1X1_SETTINGS_XPOS(n)    ED_ELEMENT_SETTINGS_XPOS(n)
259 #define ED_AREA_1X1_SETTINGS_YPOS(n)    ED_ELEMENT_SETTINGS_YPOS(n)
260 #define ED_AREA_1X1_SETTINGS_XOFF       (0)
261 #define ED_AREA_1X1_SETTINGS_YOFF       (0)
262
263 #define ED_AREA_3X3_SETTINGS_XPOS(n)    ED_ELEMENT_SETTINGS_XPOS(n)
264 #define ED_AREA_3X3_SETTINGS_YPOS(n)    ED_ELEMENT_SETTINGS_YPOS(n)
265 #define ED_AREA_3X3_SETTINGS_XOFF       (0)
266 #define ED_AREA_3X3_SETTINGS_YOFF       (0)
267
268 // element content
269 #define ED_AREA_ELEMENT_CONTENT_XOFF(n) (n)
270 #define ED_AREA_ELEMENT_CONTENT_YOFF(n) (n)
271
272 // yamyam content
273 #define ED_XPOS_YAM                     0
274 #define ED_YPOS_YAM                     5
275 #define ED_AREA_YAMYAM_CONTENT_XPOS     ED_ELEMENT_SETTINGS_XPOS(ED_XPOS_YAM)
276 #define ED_AREA_YAMYAM_CONTENT_YPOS     ED_ELEMENT_SETTINGS_YPOS(ED_YPOS_YAM)
277 #define ED_AREA_YAMYAM_CONTENT_XOFF(n)  ED_AREA_ELEMENT_CONTENT_XOFF(n)
278 #define ED_AREA_YAMYAM_CONTENT_YOFF(n)  ED_AREA_ELEMENT_CONTENT_YOFF(n)
279 #define ED_AREA_YAMYAM_CONTENT_X(n)     (ED_ELEMENT_SETTINGS_X(ED_XPOS_YAM) + \
280                                          ED_SETTINGS_XOFF(n))
281 #define ED_AREA_YAMYAM_CONTENT_Y(n)     (ED_ELEMENT_SETTINGS_Y(ED_YPOS_YAM) + \
282                                          ED_SETTINGS_YOFF(n) +          \
283                                          ED_AREA_YOFFSET_1(ED_YPOS_YAM) - \
284                                          ED_AREA_YOFFSET_2(3))
285
286 // magic ball content
287 #define ED_XPOS_BALL                    0
288 #define ED_YPOS_BALL                    6
289 #define ED_AREA_MAGIC_BALL_CONTENT_XPOS ED_ELEMENT_SETTINGS_XPOS(ED_XPOS_BALL)
290 #define ED_AREA_MAGIC_BALL_CONTENT_YPOS ED_ELEMENT_SETTINGS_YPOS(ED_YPOS_BALL)
291 #define ED_AREA_MAGIC_BALL_CONTENT_XOFF(n) ED_AREA_ELEMENT_CONTENT_XOFF(n)
292 #define ED_AREA_MAGIC_BALL_CONTENT_YOFF(n) ED_AREA_ELEMENT_CONTENT_YOFF(n)
293 #define ED_AREA_MAGIC_BALL_CONTENT_X(n) (ED_ELEMENT_SETTINGS_X(ED_XPOS_BALL) + \
294                                          ED_SETTINGS_XOFF(n))
295 #define ED_AREA_MAGIC_BALL_CONTENT_Y(n) (ED_ELEMENT_SETTINGS_Y(ED_YPOS_BALL) + \
296                                          ED_SETTINGS_YOFF(n) +          \
297                                          ED_AREA_YOFFSET_1(ED_YPOS_BALL) - \
298                                          ED_AREA_YOFFSET_2(3))
299
300 // values for scrolling gadgets for drawing area
301 #define ED_SCROLLBUTTON_XSIZE           (graphic_info[IMG_EDITOR_PLAYFIELD_SCROLLBAR].width)
302 #define ED_SCROLLBUTTON_YSIZE           (graphic_info[IMG_EDITOR_PLAYFIELD_SCROLLBAR].height)
303
304 #define ED_SCROLL_UP_XPOS               (SXSIZE - ED_SCROLLBUTTON_XSIZE)
305 #define ED_SCROLL_UP_YPOS               (0)
306 #define ED_SCROLL_DOWN_XPOS             ED_SCROLL_UP_XPOS
307 #define ED_SCROLL_DOWN_YPOS             (SYSIZE - 3 * ED_SCROLLBUTTON_YSIZE)
308 #define ED_SCROLL_LEFT_XPOS             (0)
309 #define ED_SCROLL_LEFT_YPOS             (SYSIZE - 2 * ED_SCROLLBUTTON_YSIZE)
310 #define ED_SCROLL_RIGHT_XPOS            (SXSIZE - 2 * ED_SCROLLBUTTON_XSIZE)
311 #define ED_SCROLL_RIGHT_YPOS            ED_SCROLL_LEFT_YPOS
312 #define ED_SCROLL_HORIZONTAL_XPOS       (ED_SCROLL_LEFT_XPOS +          \
313                                          ED_SCROLLBUTTON_XSIZE)
314 #define ED_SCROLL_HORIZONTAL_YPOS       ED_SCROLL_LEFT_YPOS
315 #define ED_SCROLL_HORIZONTAL_XSIZE      (SXSIZE - 3 * ED_SCROLLBUTTON_XSIZE)
316 #define ED_SCROLL_HORIZONTAL_YSIZE      ED_SCROLLBUTTON_YSIZE
317 #define ED_SCROLL_VERTICAL_XPOS         ED_SCROLL_UP_XPOS
318 #define ED_SCROLL_VERTICAL_YPOS         (ED_SCROLL_UP_YPOS +            \
319                                          ED_SCROLLBUTTON_YSIZE)
320 #define ED_SCROLL_VERTICAL_XSIZE        ED_SCROLLBUTTON_XSIZE
321 #define ED_SCROLL_VERTICAL_YSIZE        (SYSIZE - 4 * ED_SCROLLBUTTON_YSIZE)
322
323 // values for scrolling gadgets for element list
324 #define ED_SCROLLBUTTON2_XSIZE          (graphic_info[IMG_EDITOR_PALETTE_SCROLL_UP].width)
325 #define ED_SCROLLBUTTON2_YSIZE          (graphic_info[IMG_EDITOR_PALETTE_SCROLL_UP].height)
326
327 #define ED_SCROLL2_UP_XPOS              (ED_ELEMENTLIST_XPOS +          \
328                                          ED_ELEMENTLIST_BUTTONS_HORIZ * \
329                                          ED_ELEMENTLIST_XSIZE)
330 #define ED_SCROLL2_UP_YPOS              ED_ELEMENTLIST_YPOS
331 #define ED_SCROLL2_DOWN_XPOS            ED_SCROLL2_UP_XPOS
332 #define ED_SCROLL2_DOWN_YPOS            (ED_SCROLL2_UP_YPOS +           \
333                                          ED_ELEMENTLIST_BUTTONS_VERT *  \
334                                          ED_ELEMENTLIST_YSIZE -         \
335                                          ED_SCROLLBUTTON2_YSIZE)
336 #define ED_SCROLL2_VERTICAL_XPOS        ED_SCROLL2_UP_XPOS
337 #define ED_SCROLL2_VERTICAL_YPOS        (ED_SCROLL2_UP_YPOS +           \
338                                          ED_SCROLLBUTTON2_YSIZE)
339 #define ED_SCROLL2_VERTICAL_XSIZE       ED_SCROLLBUTTON2_XSIZE
340 #define ED_SCROLL2_VERTICAL_YSIZE       (ED_ELEMENTLIST_BUTTONS_VERT *  \
341                                          ED_ELEMENTLIST_YSIZE -         \
342                                          2 * ED_SCROLLBUTTON2_YSIZE)
343
344 // values for ClearEditorGadgetInfoText() and HandleEditorGadgetInfoText()
345 #define INFOTEXT_FONT           FONT_TEXT_2
346 #define INFOTEXT_XSIZE          SXSIZE
347 #define INFOTEXT_YSIZE          getFontHeight(INFOTEXT_FONT)
348 #define INFOTEXT_YSIZE_FULL     (INFOTEXT_YSIZE + ED_GADGET_SMALL_DISTANCE)
349 #define INFOTEXT_X              (editor.settings.tooltip.x)
350 #define INFOTEXT_Y              (editor.settings.tooltip.y)
351 #define INFOTEXT_XY_REDEFINED   (INFOTEXT_X != -1 || INFOTEXT_Y != -1)
352 #define INFOTEXT_XPOS           SX + (INFOTEXT_XY_REDEFINED ? INFOTEXT_X : 0)
353 #define INFOTEXT_YPOS           SY + (INFOTEXT_XY_REDEFINED ? INFOTEXT_Y : \
354                                       SYSIZE - INFOTEXT_YSIZE)
355
356
357 // ----------------------------------------------------------------------------
358 // editor gadget definitions
359 // ----------------------------------------------------------------------------
360
361 enum
362 {
363   GADGET_ID_NONE = -1,
364
365   // drawing toolbox buttons
366
367   GADGET_ID_SINGLE_ITEMS,
368   GADGET_ID_CONNECTED_ITEMS,
369   GADGET_ID_LINE,
370   GADGET_ID_ARC,
371   GADGET_ID_RECTANGLE,
372   GADGET_ID_FILLED_BOX,
373   GADGET_ID_WRAP_UP,
374   GADGET_ID_TEXT,
375   GADGET_ID_FLOOD_FILL,
376   GADGET_ID_WRAP_LEFT,
377   GADGET_ID_ZOOM,
378   GADGET_ID_WRAP_RIGHT,
379   GADGET_ID_RANDOM_PLACEMENT,
380   GADGET_ID_GRAB_BRUSH,
381   GADGET_ID_WRAP_DOWN,
382   GADGET_ID_PICK_ELEMENT,
383
384   GADGET_ID_UNDO,
385   GADGET_ID_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_BD_CYCLE_DELAY_MS_DOWN,
433   GADGET_ID_BD_CYCLE_DELAY_MS_TEXT,
434   GADGET_ID_BD_CYCLE_DELAY_MS_UP,
435   GADGET_ID_BD_CYCLE_DELAY_C64_DOWN,
436   GADGET_ID_BD_CYCLE_DELAY_C64_TEXT,
437   GADGET_ID_BD_CYCLE_DELAY_C64_UP,
438   GADGET_ID_BD_HATCHING_DELAY_CYCLES_DOWN,
439   GADGET_ID_BD_HATCHING_DELAY_CYCLES_TEXT,
440   GADGET_ID_BD_HATCHING_DELAY_CYCLES_UP,
441   GADGET_ID_BD_HATCHING_DELAY_SECONDS_DOWN,
442   GADGET_ID_BD_HATCHING_DELAY_SECONDS_TEXT,
443   GADGET_ID_BD_HATCHING_DELAY_SECONDS_UP,
444   GADGET_ID_ELEMENT_VALUE1_DOWN,
445   GADGET_ID_ELEMENT_VALUE1_TEXT,
446   GADGET_ID_ELEMENT_VALUE1_UP,
447   GADGET_ID_ELEMENT_VALUE2_DOWN,
448   GADGET_ID_ELEMENT_VALUE2_TEXT,
449   GADGET_ID_ELEMENT_VALUE2_UP,
450   GADGET_ID_ELEMENT_VALUE3_DOWN,
451   GADGET_ID_ELEMENT_VALUE3_TEXT,
452   GADGET_ID_ELEMENT_VALUE3_UP,
453   GADGET_ID_ELEMENT_VALUE4_DOWN,
454   GADGET_ID_ELEMENT_VALUE4_TEXT,
455   GADGET_ID_ELEMENT_VALUE4_UP,
456   GADGET_ID_YAMYAM_CONTENT_DOWN,
457   GADGET_ID_YAMYAM_CONTENT_TEXT,
458   GADGET_ID_YAMYAM_CONTENT_UP,
459   GADGET_ID_BALL_CONTENT_DOWN,
460   GADGET_ID_BALL_CONTENT_TEXT,
461   GADGET_ID_BALL_CONTENT_UP,
462   GADGET_ID_ANDROID_CONTENT_DOWN,
463   GADGET_ID_ANDROID_CONTENT_TEXT,
464   GADGET_ID_ANDROID_CONTENT_UP,
465   GADGET_ID_ENVELOPE_XSIZE_DOWN,
466   GADGET_ID_ENVELOPE_XSIZE_TEXT,
467   GADGET_ID_ENVELOPE_XSIZE_UP,
468   GADGET_ID_ENVELOPE_YSIZE_DOWN,
469   GADGET_ID_ENVELOPE_YSIZE_TEXT,
470   GADGET_ID_ENVELOPE_YSIZE_UP,
471   GADGET_ID_INVENTORY_SIZE_DOWN,
472   GADGET_ID_INVENTORY_SIZE_TEXT,
473   GADGET_ID_INVENTORY_SIZE_UP,
474   GADGET_ID_MM_BALL_CONTENT_DOWN,
475   GADGET_ID_MM_BALL_CONTENT_TEXT,
476   GADGET_ID_MM_BALL_CONTENT_UP,
477   GADGET_ID_CUSTOM_SCORE_DOWN,
478   GADGET_ID_CUSTOM_SCORE_TEXT,
479   GADGET_ID_CUSTOM_SCORE_UP,
480   GADGET_ID_CUSTOM_GEMCOUNT_DOWN,
481   GADGET_ID_CUSTOM_GEMCOUNT_TEXT,
482   GADGET_ID_CUSTOM_GEMCOUNT_UP,
483   GADGET_ID_CUSTOM_VALUE_FIX_DOWN,
484   GADGET_ID_CUSTOM_VALUE_FIX_TEXT,
485   GADGET_ID_CUSTOM_VALUE_FIX_UP,
486   GADGET_ID_CUSTOM_VALUE_RND_DOWN,
487   GADGET_ID_CUSTOM_VALUE_RND_TEXT,
488   GADGET_ID_CUSTOM_VALUE_RND_UP,
489   GADGET_ID_PUSH_DELAY_FIX_DOWN,
490   GADGET_ID_PUSH_DELAY_FIX_TEXT,
491   GADGET_ID_PUSH_DELAY_FIX_UP,
492   GADGET_ID_PUSH_DELAY_RND_DOWN,
493   GADGET_ID_PUSH_DELAY_RND_TEXT,
494   GADGET_ID_PUSH_DELAY_RND_UP,
495   GADGET_ID_DROP_DELAY_FIX_DOWN,
496   GADGET_ID_DROP_DELAY_FIX_TEXT,
497   GADGET_ID_DROP_DELAY_FIX_UP,
498   GADGET_ID_DROP_DELAY_RND_DOWN,
499   GADGET_ID_DROP_DELAY_RND_TEXT,
500   GADGET_ID_DROP_DELAY_RND_UP,
501   GADGET_ID_MOVE_DELAY_FIX_DOWN,
502   GADGET_ID_MOVE_DELAY_FIX_TEXT,
503   GADGET_ID_MOVE_DELAY_FIX_UP,
504   GADGET_ID_MOVE_DELAY_RND_DOWN,
505   GADGET_ID_MOVE_DELAY_RND_TEXT,
506   GADGET_ID_MOVE_DELAY_RND_UP,
507   GADGET_ID_STEP_DELAY_FIX_DOWN,
508   GADGET_ID_STEP_DELAY_FIX_TEXT,
509   GADGET_ID_STEP_DELAY_FIX_UP,
510   GADGET_ID_STEP_DELAY_RND_DOWN,
511   GADGET_ID_STEP_DELAY_RND_TEXT,
512   GADGET_ID_STEP_DELAY_RND_UP,
513   GADGET_ID_EXPLOSION_DELAY_DOWN,
514   GADGET_ID_EXPLOSION_DELAY_TEXT,
515   GADGET_ID_EXPLOSION_DELAY_UP,
516   GADGET_ID_IGNITION_DELAY_DOWN,
517   GADGET_ID_IGNITION_DELAY_TEXT,
518   GADGET_ID_IGNITION_DELAY_UP,
519   GADGET_ID_CHANGE_DELAY_FIX_DOWN,
520   GADGET_ID_CHANGE_DELAY_FIX_TEXT,
521   GADGET_ID_CHANGE_DELAY_FIX_UP,
522   GADGET_ID_CHANGE_DELAY_RND_DOWN,
523   GADGET_ID_CHANGE_DELAY_RND_TEXT,
524   GADGET_ID_CHANGE_DELAY_RND_UP,
525   GADGET_ID_CHANGE_CONT_RND_DOWN,
526   GADGET_ID_CHANGE_CONT_RND_TEXT,
527   GADGET_ID_CHANGE_CONT_RND_UP,
528   GADGET_ID_GROUP_CONTENT_DOWN,
529   GADGET_ID_GROUP_CONTENT_TEXT,
530   GADGET_ID_GROUP_CONTENT_UP,
531
532   // drawing area identifiers
533
534   GADGET_ID_DRAWING_LEVEL,
535   GADGET_ID_YAMYAM_CONTENT_0,
536   GADGET_ID_YAMYAM_CONTENT_1,
537   GADGET_ID_YAMYAM_CONTENT_2,
538   GADGET_ID_YAMYAM_CONTENT_3,
539   GADGET_ID_YAMYAM_CONTENT_4,
540   GADGET_ID_YAMYAM_CONTENT_5,
541   GADGET_ID_YAMYAM_CONTENT_6,
542   GADGET_ID_YAMYAM_CONTENT_7,
543   GADGET_ID_MAGIC_BALL_CONTENT_0,
544   GADGET_ID_MAGIC_BALL_CONTENT_1,
545   GADGET_ID_MAGIC_BALL_CONTENT_2,
546   GADGET_ID_MAGIC_BALL_CONTENT_3,
547   GADGET_ID_MAGIC_BALL_CONTENT_4,
548   GADGET_ID_MAGIC_BALL_CONTENT_5,
549   GADGET_ID_MAGIC_BALL_CONTENT_6,
550   GADGET_ID_MAGIC_BALL_CONTENT_7,
551   GADGET_ID_ANDROID_CONTENT,
552   GADGET_ID_AMOEBA_CONTENT,
553   GADGET_ID_START_ELEMENT,
554   GADGET_ID_ARTWORK_ELEMENT,
555   GADGET_ID_EXPLOSION_ELEMENT,
556   GADGET_ID_INVENTORY_CONTENT,
557   GADGET_ID_MM_BALL_CONTENT,
558   GADGET_ID_CUSTOM_GRAPHIC,
559   GADGET_ID_CUSTOM_CONTENT,
560   GADGET_ID_CUSTOM_MOVE_ENTER,
561   GADGET_ID_CUSTOM_MOVE_LEAVE,
562   GADGET_ID_CUSTOM_CHANGE_TARGET,
563   GADGET_ID_CUSTOM_CHANGE_CONTENT,
564   GADGET_ID_CUSTOM_CHANGE_TRIGGER,
565   GADGET_ID_CUSTOM_CHANGE_ACTION,
566   GADGET_ID_GROUP_CONTENT,
567   GADGET_ID_RANDOM_BACKGROUND,
568
569   // text input identifiers
570
571   GADGET_ID_LEVEL_NAME,
572   GADGET_ID_LEVEL_AUTHOR,
573   GADGET_ID_LEVELSET_NAME,
574   GADGET_ID_LEVELSET_AUTHOR,
575   GADGET_ID_ELEMENT_NAME,
576
577   // text area identifiers
578
579   GADGET_ID_ENVELOPE_INFO,
580
581   // selectbox identifiers
582
583   GADGET_ID_TIME_OR_STEPS,
584   GADGET_ID_TIME_SCORE_BASE,
585   GADGET_ID_GAME_ENGINE_TYPE,
586   GADGET_ID_BD_SCHEDULING_TYPE,
587   GADGET_ID_LEVELSET_SAVE_MODE,
588   GADGET_ID_WIND_DIRECTION,
589   GADGET_ID_PLAYER_SPEED,
590   GADGET_ID_MM_BALL_CHOICE_MODE,
591   GADGET_ID_CUSTOM_WALK_TO_ACTION,
592   GADGET_ID_CUSTOM_EXPLOSION_TYPE,
593   GADGET_ID_CUSTOM_DEADLINESS,
594   GADGET_ID_CUSTOM_MOVE_PATTERN,
595   GADGET_ID_CUSTOM_MOVE_DIRECTION,
596   GADGET_ID_CUSTOM_MOVE_STEPSIZE,
597   GADGET_ID_CUSTOM_MOVE_LEAVE_TYPE,
598   GADGET_ID_CUSTOM_SMASH_TARGETS,
599   GADGET_ID_CUSTOM_SLIPPERY_TYPE,
600   GADGET_ID_CUSTOM_ACCESS_TYPE,
601   GADGET_ID_CUSTOM_ACCESS_LAYER,
602   GADGET_ID_CUSTOM_ACCESS_PROTECTED,
603   GADGET_ID_CUSTOM_ACCESS_DIRECTION,
604   GADGET_ID_CHANGE_TIME_UNITS,
605   GADGET_ID_CHANGE_DIRECT_ACTION,
606   GADGET_ID_CHANGE_OTHER_ACTION,
607   GADGET_ID_CHANGE_SIDE,
608   GADGET_ID_CHANGE_PLAYER,
609   GADGET_ID_CHANGE_PAGE,
610   GADGET_ID_CHANGE_REPLACE_WHEN,
611   GADGET_ID_ACTION_TYPE,
612   GADGET_ID_ACTION_MODE,
613   GADGET_ID_ACTION_ARG,
614   GADGET_ID_SELECT_CHANGE_PAGE,
615   GADGET_ID_GROUP_CHOICE_MODE,
616
617   // textbutton identifiers
618
619   GADGET_ID_LEVELCONFIG_LEVEL,
620   GADGET_ID_LEVELCONFIG_LEVELSET,
621   GADGET_ID_LEVELCONFIG_EDITOR,
622   GADGET_ID_LEVELCONFIG_ENGINE,
623   GADGET_ID_PROPERTIES_INFO,
624   GADGET_ID_PROPERTIES_CONFIG,
625   GADGET_ID_PROPERTIES_CONFIG_1,
626   GADGET_ID_PROPERTIES_CONFIG_2,
627   GADGET_ID_PROPERTIES_CHANGE,
628   GADGET_ID_SAVE_AS_TEMPLATE_1,
629   GADGET_ID_SAVE_AS_TEMPLATE_2,
630   GADGET_ID_SAVE_LEVELSET,
631   GADGET_ID_ADD_CHANGE_PAGE,
632   GADGET_ID_DEL_CHANGE_PAGE,
633
634   // graphicbutton identifiers
635
636   GADGET_ID_PREV_CHANGE_PAGE,
637   GADGET_ID_NEXT_CHANGE_PAGE,
638   GADGET_ID_COPY_CHANGE_PAGE,
639   GADGET_ID_PASTE_CHANGE_PAGE,
640
641   // gadgets for scrolling of drawing area
642
643   GADGET_ID_SCROLL_UP,
644   GADGET_ID_SCROLL_DOWN,
645   GADGET_ID_SCROLL_LEFT,
646   GADGET_ID_SCROLL_RIGHT,
647   GADGET_ID_SCROLL_HORIZONTAL,
648   GADGET_ID_SCROLL_VERTICAL,
649
650   // gadgets for scrolling element list
651
652   GADGET_ID_SCROLL_LIST_UP,
653   GADGET_ID_SCROLL_LIST_DOWN,
654   GADGET_ID_SCROLL_LIST_VERTICAL,
655
656   // checkbuttons/radiobuttons for level/element properties
657
658   GADGET_ID_AUTO_COUNT_GEMS,
659   GADGET_ID_RATE_TIME_OVER_SCORE,
660   GADGET_ID_USE_LEVELSET_ARTWORK,
661   GADGET_ID_COPY_LEVEL_TEMPLATE,
662   GADGET_ID_RANDOM_PERCENTAGE,
663   GADGET_ID_RANDOM_QUANTITY,
664   GADGET_ID_RANDOM_RESTRICTED,
665   GADGET_ID_BD_INTERMISSION,
666   GADGET_ID_BD_PAL_TIMING,
667   GADGET_ID_STICK_ELEMENT,
668   GADGET_ID_EM_SLIPPERY_GEMS,
669   GADGET_ID_EM_EXPLODES_BY_FIRE,
670   GADGET_ID_USE_SPRING_BUG,
671   GADGET_ID_USE_TIME_ORB_BUG,
672   GADGET_ID_USE_LIFE_BUGS,
673   GADGET_ID_RANDOM_BALL_CONTENT,
674   GADGET_ID_INITIAL_BALL_ACTIVE,
675   GADGET_ID_GROW_INTO_DIGGABLE,
676   GADGET_ID_SB_FIELDS_NEEDED,
677   GADGET_ID_SB_OBJECTS_NEEDED,
678   GADGET_ID_AUTO_EXIT_SOKOBAN,
679   GADGET_ID_SOLVED_BY_ONE_PLAYER,
680   GADGET_ID_FINISH_DIG_COLLECT,
681   GADGET_ID_KEEP_WALKABLE_CE,
682   GADGET_ID_CONTINUOUS_SNAPPING,
683   GADGET_ID_BLOCK_SNAP_FIELD,
684   GADGET_ID_BLOCK_LAST_FIELD,
685   GADGET_ID_SP_BLOCK_LAST_FIELD,
686   GADGET_ID_INSTANT_RELOCATION,
687   GADGET_ID_SHIFTED_RELOCATION,
688   GADGET_ID_LAZY_RELOCATION,
689   GADGET_ID_USE_START_ELEMENT,
690   GADGET_ID_USE_ARTWORK_ELEMENT,
691   GADGET_ID_USE_EXPLOSION_ELEMENT,
692   GADGET_ID_INITIAL_GRAVITY,
693   GADGET_ID_USE_INITIAL_INVENTORY,
694   GADGET_ID_CAN_PASS_TO_WALKABLE,
695   GADGET_ID_CAN_FALL_INTO_ACID,
696   GADGET_ID_CAN_MOVE_INTO_ACID,
697   GADGET_ID_DONT_COLLIDE_WITH,
698   GADGET_ID_BD_DIAGONAL_MOVEMENTS,
699   GADGET_ID_ENVELOPE_AUTOWRAP,
700   GADGET_ID_ENVELOPE_CENTERED,
701   GADGET_ID_MM_LASER_RED,
702   GADGET_ID_MM_LASER_GREEN,
703   GADGET_ID_MM_LASER_BLUE,
704   GADGET_ID_DF_LASER_RED,
705   GADGET_ID_DF_LASER_GREEN,
706   GADGET_ID_DF_LASER_BLUE,
707   GADGET_ID_ROTATE_MM_BALL_CONTENT,
708   GADGET_ID_EXPLODE_MM_BALL,
709   GADGET_ID_CUSTOM_INDESTRUCTIBLE,
710   GADGET_ID_CUSTOM_CAN_EXPLODE,
711   GADGET_ID_CUSTOM_EXPLODE_FIRE,
712   GADGET_ID_CUSTOM_EXPLODE_SMASH,
713   GADGET_ID_CUSTOM_EXPLODE_IMPACT,
714   GADGET_ID_CUSTOM_WALK_TO_OBJECT,
715   GADGET_ID_CUSTOM_DEADLY,
716   GADGET_ID_CUSTOM_CAN_MOVE,
717   GADGET_ID_CUSTOM_CAN_FALL,
718   GADGET_ID_CUSTOM_CAN_SMASH,
719   GADGET_ID_CUSTOM_SLIPPERY,
720   GADGET_ID_CUSTOM_ACCESSIBLE,
721   GADGET_ID_CUSTOM_GRAV_REACHABLE,
722   GADGET_ID_CUSTOM_USE_LAST_VALUE,
723   GADGET_ID_CUSTOM_USE_GRAPHIC,
724   GADGET_ID_CUSTOM_USE_TEMPLATE_1,
725   GADGET_ID_CUSTOM_USE_TEMPLATE_2,
726   GADGET_ID_CUSTOM_USE_TEMPLATE_3,
727   GADGET_ID_CUSTOM_CAN_CHANGE,
728   GADGET_ID_CHANGE_USE_CONTENT,
729   GADGET_ID_CHANGE_USE_EXPLOSION,
730   GADGET_ID_CHANGE_ONLY_COMPLETE,
731   GADGET_ID_CHANGE_USE_RANDOM,
732   GADGET_ID_CHANGE_HAS_ACTION,
733   GADGET_ID_CHANGE_DELAY,
734   GADGET_ID_CHANGE_BY_DIRECT_ACT,
735   GADGET_ID_CHANGE_BY_OTHER_ACT,
736
737   NUM_STATIC_GADGET_IDS
738 };
739
740 // gadgets for buttons in element list (dynamic)
741 #define GADGET_ID_ELEMENTLIST_FIRST     (NUM_STATIC_GADGET_IDS)
742 #define GADGET_ID_ELEMENTLIST_LAST      (GADGET_ID_ELEMENTLIST_FIRST +  \
743                                         ED_NUM_ELEMENTLIST_BUTTONS - 1)
744
745 #define NUM_EDITOR_GADGETS              (GADGET_ID_ELEMENTLIST_LAST + 1)
746
747 // radio button numbers
748 enum
749 {
750   RADIO_NR_NONE,
751   RADIO_NR_DRAWING_TOOLBOX,
752   RADIO_NR_RANDOM_ELEMENTS
753 };
754
755 // values for counter gadgets
756 enum
757 {
758   ED_COUNTER_ID_SELECT_LEVEL,
759   ED_COUNTER_ID_LEVEL_XSIZE,
760   ED_COUNTER_ID_LEVEL_YSIZE,
761   ED_COUNTER_ID_LEVEL_GEMSLIMIT,
762   ED_COUNTER_ID_LEVEL_TIMELIMIT,
763   ED_COUNTER_ID_LEVEL_TIMESCORE,
764   ED_COUNTER_ID_LEVEL_RANDOM_SEED,
765   ED_COUNTER_ID_LEVELSET_NUM_LEVELS,
766   ED_COUNTER_ID_LEVEL_RANDOM,
767   ED_COUNTER_ID_BD_CYCLE_DELAY_MS,
768   ED_COUNTER_ID_BD_CYCLE_DELAY_C64,
769   ED_COUNTER_ID_BD_HATCHING_DELAY_CYCLES,
770   ED_COUNTER_ID_BD_HATCHING_DELAY_SECONDS,
771   ED_COUNTER_ID_ELEMENT_VALUE1,
772   ED_COUNTER_ID_ELEMENT_VALUE2,
773   ED_COUNTER_ID_ELEMENT_VALUE3,
774   ED_COUNTER_ID_ELEMENT_VALUE4,
775   ED_COUNTER_ID_YAMYAM_CONTENT,
776   ED_COUNTER_ID_BALL_CONTENT,
777   ED_COUNTER_ID_ANDROID_CONTENT,
778   ED_COUNTER_ID_ENVELOPE_XSIZE,
779   ED_COUNTER_ID_ENVELOPE_YSIZE,
780   ED_COUNTER_ID_INVENTORY_SIZE,
781   ED_COUNTER_ID_MM_BALL_CONTENT,
782   ED_COUNTER_ID_CUSTOM_SCORE,
783   ED_COUNTER_ID_CUSTOM_GEMCOUNT,
784   ED_COUNTER_ID_CUSTOM_VALUE_FIX,
785   ED_COUNTER_ID_CUSTOM_VALUE_RND,
786   ED_COUNTER_ID_PUSH_DELAY_FIX,
787   ED_COUNTER_ID_PUSH_DELAY_RND,
788   ED_COUNTER_ID_DROP_DELAY_FIX,
789   ED_COUNTER_ID_DROP_DELAY_RND,
790   ED_COUNTER_ID_MOVE_DELAY_FIX,
791   ED_COUNTER_ID_MOVE_DELAY_RND,
792   ED_COUNTER_ID_STEP_DELAY_FIX,
793   ED_COUNTER_ID_STEP_DELAY_RND,
794   ED_COUNTER_ID_EXPLOSION_DELAY,
795   ED_COUNTER_ID_IGNITION_DELAY,
796   ED_COUNTER_ID_GROUP_CONTENT,
797   ED_COUNTER_ID_CHANGE_DELAY_FIX,
798   ED_COUNTER_ID_CHANGE_DELAY_RND,
799   ED_COUNTER_ID_CHANGE_CONT_RND,
800
801   ED_NUM_COUNTERBUTTONS
802 };
803
804 #define ED_COUNTER_ID_LEVEL_FIRST       ED_COUNTER_ID_LEVEL_XSIZE
805 #define ED_COUNTER_ID_LEVEL_LAST        ED_COUNTER_ID_LEVEL_RANDOM_SEED
806 #define ED_COUNTER_ID_LEVELSET_FIRST    ED_COUNTER_ID_LEVELSET_NUM_LEVELS
807 #define ED_COUNTER_ID_LEVELSET_LAST     ED_COUNTER_ID_LEVELSET_NUM_LEVELS
808 #define ED_COUNTER_ID_EDITOR_FIRST      ED_COUNTER_ID_LEVEL_RANDOM
809 #define ED_COUNTER_ID_EDITOR_LAST       ED_COUNTER_ID_LEVEL_RANDOM
810
811 #define ED_COUNTER_ID_CUSTOM1_FIRST     ED_COUNTER_ID_CUSTOM_SCORE
812 #define ED_COUNTER_ID_CUSTOM1_LAST      ED_COUNTER_ID_DROP_DELAY_RND
813 #define ED_COUNTER_ID_CUSTOM2_FIRST     ED_COUNTER_ID_MOVE_DELAY_FIX
814 #define ED_COUNTER_ID_CUSTOM2_LAST      ED_COUNTER_ID_IGNITION_DELAY
815 #define ED_COUNTER_ID_CUSTOM_FIRST      ED_COUNTER_ID_CUSTOM1_FIRST
816 #define ED_COUNTER_ID_CUSTOM_LAST       ED_COUNTER_ID_CUSTOM2_LAST
817
818 #define ED_COUNTER_ID_CHANGE_FIRST      ED_COUNTER_ID_CHANGE_DELAY_FIX
819 #define ED_COUNTER_ID_CHANGE_LAST       ED_COUNTER_ID_CHANGE_CONT_RND
820
821 // values for scrollbutton gadgets
822 enum
823 {
824   ED_SCROLLBUTTON_ID_AREA_UP,
825   ED_SCROLLBUTTON_ID_AREA_DOWN,
826   ED_SCROLLBUTTON_ID_AREA_LEFT,
827   ED_SCROLLBUTTON_ID_AREA_RIGHT,
828   ED_SCROLLBUTTON_ID_LIST_UP,
829   ED_SCROLLBUTTON_ID_LIST_DOWN,
830
831   ED_NUM_SCROLLBUTTONS
832 };
833
834 #define ED_SCROLLBUTTON_ID_AREA_FIRST   ED_SCROLLBUTTON_ID_AREA_UP
835 #define ED_SCROLLBUTTON_ID_AREA_LAST    ED_SCROLLBUTTON_ID_AREA_RIGHT
836
837 // values for scrollbar gadgets
838 enum
839 {
840   ED_SCROLLBAR_ID_AREA_HORIZONTAL,
841   ED_SCROLLBAR_ID_AREA_VERTICAL,
842   ED_SCROLLBAR_ID_LIST_VERTICAL,
843
844   ED_NUM_SCROLLBARS
845 };
846
847 #define ED_SCROLLBAR_ID_AREA_FIRST      ED_SCROLLBAR_ID_AREA_HORIZONTAL
848 #define ED_SCROLLBAR_ID_AREA_LAST       ED_SCROLLBAR_ID_AREA_VERTICAL
849
850 // values for text input gadgets
851 enum
852 {
853   ED_TEXTINPUT_ID_LEVEL_NAME,
854   ED_TEXTINPUT_ID_LEVEL_AUTHOR,
855   ED_TEXTINPUT_ID_LEVELSET_NAME,
856   ED_TEXTINPUT_ID_LEVELSET_AUTHOR,
857   ED_TEXTINPUT_ID_ELEMENT_NAME,
858
859   ED_NUM_TEXTINPUT
860 };
861
862 #define ED_TEXTINPUT_ID_LEVEL_FIRST     ED_TEXTINPUT_ID_LEVEL_NAME
863 #define ED_TEXTINPUT_ID_LEVEL_LAST      ED_TEXTINPUT_ID_LEVEL_AUTHOR
864
865 #define ED_TEXTINPUT_ID_LEVELSET_FIRST  ED_TEXTINPUT_ID_LEVELSET_NAME
866 #define ED_TEXTINPUT_ID_LEVELSET_LAST   ED_TEXTINPUT_ID_LEVELSET_AUTHOR
867
868 // values for text area gadgets
869 enum
870 {
871   ED_TEXTAREA_ID_ENVELOPE_INFO,
872
873   ED_NUM_TEXTAREAS
874 };
875
876 #define ED_TEXTAREA_ID_LEVEL_FIRST      ED_TEXTAREA_ID_ENVELOPE
877 #define ED_TEXTAREA_ID_LEVEL_LAST       ED_TEXTAREA_ID_ENVELOPE
878
879 // values for selectbox gadgets
880 enum
881 {
882   ED_SELECTBOX_ID_TIME_OR_STEPS,
883   ED_SELECTBOX_ID_TIME_SCORE_BASE,
884   ED_SELECTBOX_ID_GAME_ENGINE_TYPE,
885   ED_SELECTBOX_ID_BD_SCHEDULING_TYPE,
886   ED_SELECTBOX_ID_LEVELSET_SAVE_MODE,
887   ED_SELECTBOX_ID_WIND_DIRECTION,
888   ED_SELECTBOX_ID_PLAYER_SPEED,
889   ED_SELECTBOX_ID_MM_BALL_CHOICE_MODE,
890   ED_SELECTBOX_ID_CUSTOM_ACCESS_TYPE,
891   ED_SELECTBOX_ID_CUSTOM_ACCESS_LAYER,
892   ED_SELECTBOX_ID_CUSTOM_ACCESS_PROTECTED,
893   ED_SELECTBOX_ID_CUSTOM_ACCESS_DIRECTION,
894   ED_SELECTBOX_ID_CUSTOM_WALK_TO_ACTION,
895   ED_SELECTBOX_ID_CUSTOM_MOVE_PATTERN,
896   ED_SELECTBOX_ID_CUSTOM_MOVE_DIRECTION,
897   ED_SELECTBOX_ID_CUSTOM_MOVE_STEPSIZE,
898   ED_SELECTBOX_ID_CUSTOM_MOVE_LEAVE_TYPE,
899   ED_SELECTBOX_ID_CUSTOM_SMASH_TARGETS,
900   ED_SELECTBOX_ID_CUSTOM_SLIPPERY_TYPE,
901   ED_SELECTBOX_ID_CUSTOM_DEADLINESS,
902   ED_SELECTBOX_ID_CUSTOM_EXPLOSION_TYPE,
903   ED_SELECTBOX_ID_CHANGE_TIME_UNITS,
904   ED_SELECTBOX_ID_CHANGE_DIRECT_ACTION,
905   ED_SELECTBOX_ID_CHANGE_OTHER_ACTION,
906   ED_SELECTBOX_ID_CHANGE_SIDE,
907   ED_SELECTBOX_ID_CHANGE_PLAYER,
908   ED_SELECTBOX_ID_CHANGE_PAGE,
909   ED_SELECTBOX_ID_CHANGE_REPLACE_WHEN,
910   ED_SELECTBOX_ID_ACTION_TYPE,
911   ED_SELECTBOX_ID_ACTION_MODE,
912   ED_SELECTBOX_ID_ACTION_ARG,
913   ED_SELECTBOX_ID_SELECT_CHANGE_PAGE,
914   ED_SELECTBOX_ID_GROUP_CHOICE_MODE,
915
916   ED_NUM_SELECTBOX
917 };
918
919 #define ED_SELECTBOX_ID_LEVEL_FIRST     ED_SELECTBOX_ID_TIME_OR_STEPS
920 #define ED_SELECTBOX_ID_LEVEL_LAST      ED_SELECTBOX_ID_GAME_ENGINE_TYPE
921
922 #define ED_SELECTBOX_ID_LEVELSET_FIRST  ED_SELECTBOX_ID_LEVELSET_SAVE_MODE
923 #define ED_SELECTBOX_ID_LEVELSET_LAST   ED_SELECTBOX_ID_LEVELSET_SAVE_MODE
924
925 #define ED_SELECTBOX_ID_ENGINE_FIRST    ED_SELECTBOX_ID_BD_SCHEDULING_TYPE
926 #define ED_SELECTBOX_ID_ENGINE_LAST     ED_SELECTBOX_ID_BD_SCHEDULING_TYPE
927
928 #define ED_SELECTBOX_ID_CUSTOM1_FIRST   ED_SELECTBOX_ID_CUSTOM_ACCESS_TYPE
929 #define ED_SELECTBOX_ID_CUSTOM1_LAST    ED_SELECTBOX_ID_CUSTOM_WALK_TO_ACTION
930 #define ED_SELECTBOX_ID_CUSTOM2_FIRST   ED_SELECTBOX_ID_CUSTOM_MOVE_PATTERN
931 #define ED_SELECTBOX_ID_CUSTOM2_LAST    ED_SELECTBOX_ID_CUSTOM_EXPLOSION_TYPE
932 #define ED_SELECTBOX_ID_CUSTOM_FIRST    ED_SELECTBOX_ID_CUSTOM1_FIRST
933 #define ED_SELECTBOX_ID_CUSTOM_LAST     ED_SELECTBOX_ID_CUSTOM2_LAST
934
935 #define ED_SELECTBOX_ID_CHANGE_FIRST    ED_SELECTBOX_ID_CHANGE_TIME_UNITS
936 #define ED_SELECTBOX_ID_CHANGE_LAST     ED_SELECTBOX_ID_SELECT_CHANGE_PAGE
937
938 // values for textbutton gadgets
939 enum
940 {
941   ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL,
942   ED_TEXTBUTTON_ID_LEVELCONFIG_LEVELSET,
943   ED_TEXTBUTTON_ID_LEVELCONFIG_EDITOR,
944   ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE,
945   ED_TEXTBUTTON_ID_PROPERTIES_INFO,
946   ED_TEXTBUTTON_ID_PROPERTIES_CONFIG,
947   ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_1,
948   ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2,
949   ED_TEXTBUTTON_ID_PROPERTIES_CHANGE,
950   ED_TEXTBUTTON_ID_SAVE_LEVELSET,
951   ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2,
952   ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1,
953   ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE,
954   ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE,
955
956   ED_NUM_TEXTBUTTONS
957 };
958
959 #define ED_TAB_BUTTON_ID_LEVELCONFIG_FIRST ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL
960 #define ED_TAB_BUTTON_ID_LEVELCONFIG_LAST  ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE
961
962 #define ED_TAB_BUTTON_ID_PROPERTIES_FIRST ED_TEXTBUTTON_ID_PROPERTIES_INFO
963 #define ED_TAB_BUTTON_ID_PROPERTIES_LAST  ED_TEXTBUTTON_ID_PROPERTIES_CHANGE
964
965 #define ED_TEXTBUTTON_ID_CHANGE_FIRST   ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE
966 #define ED_TEXTBUTTON_ID_CHANGE_LAST    ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE
967
968 // values for graphicbutton gadgets
969 enum
970 {
971   ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE,
972   ED_GRAPHICBUTTON_ID_NEXT_CHANGE_PAGE,
973   ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE,
974   ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE,
975
976   ED_NUM_GRAPHICBUTTONS
977 };
978
979 #define ED_GRAPHICBUTTON_ID_CHANGE_FIRST  ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE
980 #define ED_GRAPHICBUTTON_ID_CHANGE_LAST   ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE
981
982 // values for checkbutton gadgets
983 enum
984 {
985   ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS,
986   ED_CHECKBUTTON_ID_RATE_TIME_OVER_SCORE,
987   ED_CHECKBUTTON_ID_USE_LEVELSET_ARTWORK,
988   ED_CHECKBUTTON_ID_COPY_LEVEL_TEMPLATE,
989   ED_CHECKBUTTON_ID_RANDOM_RESTRICTED,
990   ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_3,
991   ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2,
992   ED_CHECKBUTTON_ID_BD_INTERMISSION,
993   ED_CHECKBUTTON_ID_BD_PAL_TIMING,
994   ED_CHECKBUTTON_ID_STICK_ELEMENT,
995   ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS,
996   ED_CHECKBUTTON_ID_EM_EXPLODES_BY_FIRE,
997   ED_CHECKBUTTON_ID_USE_SPRING_BUG,
998   ED_CHECKBUTTON_ID_USE_TIME_ORB_BUG,
999   ED_CHECKBUTTON_ID_USE_LIFE_BUGS,
1000   ED_CHECKBUTTON_ID_RANDOM_BALL_CONTENT,
1001   ED_CHECKBUTTON_ID_INITIAL_BALL_ACTIVE,
1002   ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE,
1003   ED_CHECKBUTTON_ID_SB_FIELDS_NEEDED,
1004   ED_CHECKBUTTON_ID_SB_OBJECTS_NEEDED,
1005   ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN,
1006   ED_CHECKBUTTON_ID_SOLVED_BY_ONE_PLAYER,
1007   ED_CHECKBUTTON_ID_FINISH_DIG_COLLECT,
1008   ED_CHECKBUTTON_ID_KEEP_WALKABLE_CE,
1009   ED_CHECKBUTTON_ID_CONTINUOUS_SNAPPING,
1010   ED_CHECKBUTTON_ID_BLOCK_SNAP_FIELD,
1011   ED_CHECKBUTTON_ID_BLOCK_LAST_FIELD,
1012   ED_CHECKBUTTON_ID_SP_BLOCK_LAST_FIELD,
1013   ED_CHECKBUTTON_ID_INSTANT_RELOCATION,
1014   ED_CHECKBUTTON_ID_SHIFTED_RELOCATION,
1015   ED_CHECKBUTTON_ID_LAZY_RELOCATION,
1016   ED_CHECKBUTTON_ID_USE_START_ELEMENT,
1017   ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT,
1018   ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT,
1019   ED_CHECKBUTTON_ID_INITIAL_GRAVITY,
1020   ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY,
1021   ED_CHECKBUTTON_ID_CAN_PASS_TO_WALKABLE,
1022   ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID,
1023   ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID,
1024   ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH,
1025   ED_CHECKBUTTON_ID_BD_DIAGONAL_MOVEMENTS,
1026   ED_CHECKBUTTON_ID_ENVELOPE_AUTOWRAP,
1027   ED_CHECKBUTTON_ID_ENVELOPE_CENTERED,
1028   ED_CHECKBUTTON_ID_MM_LASER_RED,
1029   ED_CHECKBUTTON_ID_MM_LASER_GREEN,
1030   ED_CHECKBUTTON_ID_MM_LASER_BLUE,
1031   ED_CHECKBUTTON_ID_DF_LASER_RED,
1032   ED_CHECKBUTTON_ID_DF_LASER_GREEN,
1033   ED_CHECKBUTTON_ID_DF_LASER_BLUE,
1034   ED_CHECKBUTTON_ID_ROTATE_MM_BALL_CONTENT,
1035   ED_CHECKBUTTON_ID_EXPLODE_MM_BALL,
1036   ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC,
1037   ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1,
1038   ED_CHECKBUTTON_ID_CUSTOM_ACCESSIBLE,
1039   ED_CHECKBUTTON_ID_CUSTOM_GRAV_REACHABLE,
1040   ED_CHECKBUTTON_ID_CUSTOM_USE_LAST_VALUE,
1041   ED_CHECKBUTTON_ID_CUSTOM_WALK_TO_OBJECT,
1042   ED_CHECKBUTTON_ID_CUSTOM_INDESTRUCTIBLE,
1043   ED_CHECKBUTTON_ID_CUSTOM_CAN_MOVE,
1044   ED_CHECKBUTTON_ID_CUSTOM_CAN_FALL,
1045   ED_CHECKBUTTON_ID_CUSTOM_CAN_SMASH,
1046   ED_CHECKBUTTON_ID_CUSTOM_SLIPPERY,
1047   ED_CHECKBUTTON_ID_CUSTOM_DEADLY,
1048   ED_CHECKBUTTON_ID_CUSTOM_CAN_EXPLODE,
1049   ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_FIRE,
1050   ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_SMASH,
1051   ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_IMPACT,
1052   ED_CHECKBUTTON_ID_CUSTOM_CAN_CHANGE,
1053   ED_CHECKBUTTON_ID_CHANGE_DELAY,
1054   ED_CHECKBUTTON_ID_CHANGE_BY_DIRECT_ACT,
1055   ED_CHECKBUTTON_ID_CHANGE_BY_OTHER_ACT,
1056   ED_CHECKBUTTON_ID_CHANGE_USE_EXPLOSION,
1057   ED_CHECKBUTTON_ID_CHANGE_USE_CONTENT,
1058   ED_CHECKBUTTON_ID_CHANGE_ONLY_COMPLETE,
1059   ED_CHECKBUTTON_ID_CHANGE_USE_RANDOM,
1060   ED_CHECKBUTTON_ID_CHANGE_HAS_ACTION,
1061
1062   ED_NUM_CHECKBUTTONS
1063 };
1064
1065 #define ED_CHECKBUTTON_ID_LEVEL_FIRST   ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS
1066 #define ED_CHECKBUTTON_ID_LEVEL_LAST    ED_CHECKBUTTON_ID_RATE_TIME_OVER_SCORE
1067
1068 #define ED_CHECKBUTTON_ID_LEVELSET_FIRST ED_CHECKBUTTON_ID_USE_LEVELSET_ARTWORK
1069 #define ED_CHECKBUTTON_ID_LEVELSET_LAST  ED_CHECKBUTTON_ID_COPY_LEVEL_TEMPLATE
1070
1071 #define ED_CHECKBUTTON_ID_EDITOR_FIRST  ED_CHECKBUTTON_ID_RANDOM_RESTRICTED
1072 #define ED_CHECKBUTTON_ID_EDITOR_LAST   ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2
1073
1074 #define ED_CHECKBUTTON_ID_ENGINE_FIRST  ED_CHECKBUTTON_ID_BD_INTERMISSION
1075 #define ED_CHECKBUTTON_ID_ENGINE_LAST   ED_CHECKBUTTON_ID_BD_PAL_TIMING
1076
1077 #define ED_CHECKBUTTON_ID_CUSTOM1_FIRST ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC
1078 #define ED_CHECKBUTTON_ID_CUSTOM1_LAST  ED_CHECKBUTTON_ID_CUSTOM_INDESTRUCTIBLE
1079 #define ED_CHECKBUTTON_ID_CUSTOM2_FIRST ED_CHECKBUTTON_ID_CUSTOM_CAN_MOVE
1080 #define ED_CHECKBUTTON_ID_CUSTOM2_LAST  ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_IMPACT
1081 #define ED_CHECKBUTTON_ID_CUSTOM_FIRST  ED_CHECKBUTTON_ID_CUSTOM1_FIRST
1082 #define ED_CHECKBUTTON_ID_CUSTOM_LAST   ED_CHECKBUTTON_ID_CUSTOM2_LAST
1083
1084 #define ED_CHECKBUTTON_ID_CHANGE_FIRST  ED_CHECKBUTTON_ID_CUSTOM_CAN_CHANGE
1085 #define ED_CHECKBUTTON_ID_CHANGE_LAST   ED_CHECKBUTTON_ID_CHANGE_HAS_ACTION
1086
1087 // values for radiobutton gadgets
1088 enum
1089 {
1090   ED_RADIOBUTTON_ID_PERCENTAGE,
1091   ED_RADIOBUTTON_ID_QUANTITY,
1092
1093   ED_NUM_RADIOBUTTONS
1094 };
1095
1096 #define ED_RADIOBUTTON_ID_EDITOR_FIRST  ED_RADIOBUTTON_ID_PERCENTAGE
1097 #define ED_RADIOBUTTON_ID_EDITOR_LAST   ED_RADIOBUTTON_ID_QUANTITY
1098
1099 // values for drawing area gadgets
1100 enum
1101 {
1102   ED_DRAWING_ID_DRAWING_LEVEL,
1103   ED_DRAWING_ID_YAMYAM_CONTENT_0,
1104   ED_DRAWING_ID_YAMYAM_CONTENT_1,
1105   ED_DRAWING_ID_YAMYAM_CONTENT_2,
1106   ED_DRAWING_ID_YAMYAM_CONTENT_3,
1107   ED_DRAWING_ID_YAMYAM_CONTENT_4,
1108   ED_DRAWING_ID_YAMYAM_CONTENT_5,
1109   ED_DRAWING_ID_YAMYAM_CONTENT_6,
1110   ED_DRAWING_ID_YAMYAM_CONTENT_7,
1111   ED_DRAWING_ID_MAGIC_BALL_CONTENT_0,
1112   ED_DRAWING_ID_MAGIC_BALL_CONTENT_1,
1113   ED_DRAWING_ID_MAGIC_BALL_CONTENT_2,
1114   ED_DRAWING_ID_MAGIC_BALL_CONTENT_3,
1115   ED_DRAWING_ID_MAGIC_BALL_CONTENT_4,
1116   ED_DRAWING_ID_MAGIC_BALL_CONTENT_5,
1117   ED_DRAWING_ID_MAGIC_BALL_CONTENT_6,
1118   ED_DRAWING_ID_MAGIC_BALL_CONTENT_7,
1119   ED_DRAWING_ID_ANDROID_CONTENT,
1120   ED_DRAWING_ID_AMOEBA_CONTENT,
1121   ED_DRAWING_ID_START_ELEMENT,
1122   ED_DRAWING_ID_ARTWORK_ELEMENT,
1123   ED_DRAWING_ID_EXPLOSION_ELEMENT,
1124   ED_DRAWING_ID_INVENTORY_CONTENT,
1125   ED_DRAWING_ID_MM_BALL_CONTENT,
1126   ED_DRAWING_ID_CUSTOM_GRAPHIC,
1127   ED_DRAWING_ID_CUSTOM_CONTENT,
1128   ED_DRAWING_ID_CUSTOM_MOVE_ENTER,
1129   ED_DRAWING_ID_CUSTOM_MOVE_LEAVE,
1130   ED_DRAWING_ID_CUSTOM_CHANGE_TARGET,
1131   ED_DRAWING_ID_CUSTOM_CHANGE_CONTENT,
1132   ED_DRAWING_ID_CUSTOM_CHANGE_TRIGGER,
1133   ED_DRAWING_ID_CUSTOM_CHANGE_ACTION,
1134   ED_DRAWING_ID_GROUP_CONTENT,
1135   ED_DRAWING_ID_RANDOM_BACKGROUND,
1136
1137   ED_NUM_DRAWING_AREAS
1138 };
1139
1140 #define ED_DRAWING_ID_EDITOR_FIRST      ED_DRAWING_ID_RANDOM_BACKGROUND
1141 #define ED_DRAWING_ID_EDITOR_LAST       ED_DRAWING_ID_RANDOM_BACKGROUND
1142
1143
1144 // ----------------------------------------------------------------------------
1145 // some internally used definitions
1146 // ----------------------------------------------------------------------------
1147
1148 // values for CopyLevelToUndoBuffer()
1149 #define UNDO_IMMEDIATE                  0
1150 #define UNDO_ACCUMULATE                 1
1151
1152 // values for scrollbars
1153 #define ED_SCROLL_NO                    0
1154 #define ED_SCROLL_LEFT                  1
1155 #define ED_SCROLL_RIGHT                 2
1156 #define ED_SCROLL_UP                    4
1157 #define ED_SCROLL_DOWN                  8
1158
1159 // screens in the level editor
1160 #define ED_MODE_DRAWING                 0
1161 #define ED_MODE_LEVELCONFIG             1
1162 #define ED_MODE_PROPERTIES              2
1163 #define ED_MODE_PALETTE                 3
1164
1165 // sub-screens in the global settings section
1166 #define ED_MODE_LEVELCONFIG_LEVEL       ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL
1167 #define ED_MODE_LEVELCONFIG_LEVELSET    ED_TEXTBUTTON_ID_LEVELCONFIG_LEVELSET
1168 #define ED_MODE_LEVELCONFIG_EDITOR      ED_TEXTBUTTON_ID_LEVELCONFIG_EDITOR
1169 #define ED_MODE_LEVELCONFIG_ENGINE      ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE
1170
1171 // sub-screens in the element properties section
1172 #define ED_MODE_PROPERTIES_INFO         ED_TEXTBUTTON_ID_PROPERTIES_INFO
1173 #define ED_MODE_PROPERTIES_CONFIG       ED_TEXTBUTTON_ID_PROPERTIES_CONFIG
1174 #define ED_MODE_PROPERTIES_CONFIG_1     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_1
1175 #define ED_MODE_PROPERTIES_CONFIG_2     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2
1176 #define ED_MODE_PROPERTIES_CHANGE       ED_TEXTBUTTON_ID_PROPERTIES_CHANGE
1177
1178 // how many steps can be cancelled
1179 #define NUM_UNDO_STEPS                  (64 + 1)
1180
1181 // values for elements with score for certain actions
1182 #define MIN_SCORE                       0
1183 #define MAX_SCORE                       999
1184
1185 // values for elements with count for collecting
1186 #define MIN_COLLECT_COUNT               0
1187 #define MAX_COLLECT_COUNT               999
1188
1189 // values for random placement
1190 #define RANDOM_USE_PERCENTAGE           0
1191 #define RANDOM_USE_QUANTITY             1
1192
1193 // values for level set save mode
1194 #define LEVELSET_SAVE_MODE_UPDATE       0
1195 #define LEVELSET_SAVE_MODE_CREATE       1
1196
1197 // default value for element tile size in drawing area
1198 #define DEFAULT_EDITOR_TILESIZE         MINI_TILESIZE
1199 #define DEFAULT_EDITOR_TILESIZE_MM      TILESIZE
1200
1201
1202 // ----------------------------------------------------------------------------
1203 // some internally used data structure definitions
1204 // ----------------------------------------------------------------------------
1205
1206 static struct
1207 {
1208   int graphic;
1209   int gadget_id;
1210   struct XYTileSize *pos;
1211   int gadget_type;
1212   char *infotext;
1213   char shortcut;
1214 } controlbutton_info[ED_NUM_CTRL_BUTTONS] =
1215 {
1216   // note: some additional characters are already reserved for "cheat mode"
1217   // shortcuts (":XYZ" style) -- for details, see "events.c"
1218
1219   // ---------- toolbox control buttons ---------------------------------------
1220
1221   {
1222     IMG_GFX_EDITOR_BUTTON_DRAW_SINGLE,          GADGET_ID_SINGLE_ITEMS,
1223     &editor.button.draw_single,                 GD_TYPE_RADIO_BUTTON,
1224     "draw single items",                        's'
1225   },
1226   {
1227     IMG_GFX_EDITOR_BUTTON_DRAW_CONNECTED,       GADGET_ID_CONNECTED_ITEMS,
1228     &editor.button.draw_connected,              GD_TYPE_RADIO_BUTTON,
1229     "draw connected items",                     'd'
1230   },
1231   {
1232     IMG_GFX_EDITOR_BUTTON_DRAW_LINE,            GADGET_ID_LINE,
1233     &editor.button.draw_line,                   GD_TYPE_RADIO_BUTTON,
1234     "draw lines",                               'l'
1235   },
1236   {
1237     IMG_GFX_EDITOR_BUTTON_DRAW_ARC,             GADGET_ID_ARC,
1238     &editor.button.draw_arc,                    GD_TYPE_RADIO_BUTTON,
1239     "draw arcs",                                'a'
1240   },
1241   {
1242     IMG_GFX_EDITOR_BUTTON_DRAW_RECTANGLE,       GADGET_ID_RECTANGLE,
1243     &editor.button.draw_rectangle,              GD_TYPE_RADIO_BUTTON,
1244     "draw outline rectangles",                  'r'
1245   },
1246   {
1247     IMG_GFX_EDITOR_BUTTON_DRAW_FILLED_BOX,      GADGET_ID_FILLED_BOX,
1248     &editor.button.draw_filled_box,             GD_TYPE_RADIO_BUTTON,
1249     "draw filled rectangles",                   'R'
1250   },
1251   {
1252     IMG_GFX_EDITOR_BUTTON_ROTATE_UP,            GADGET_ID_WRAP_UP,
1253     &editor.button.rotate_up,                   GD_TYPE_NORMAL_BUTTON,
1254     "wrap (rotate) level up",                   0
1255   },
1256   {
1257     IMG_GFX_EDITOR_BUTTON_DRAW_TEXT,            GADGET_ID_TEXT,
1258     &editor.button.draw_text,                   GD_TYPE_RADIO_BUTTON,
1259     "enter text elements",                      't'
1260   },
1261   {
1262     IMG_GFX_EDITOR_BUTTON_FLOOD_FILL,           GADGET_ID_FLOOD_FILL,
1263     &editor.button.flood_fill,                  GD_TYPE_RADIO_BUTTON,
1264     "flood fill",                               'f'
1265   },
1266   {
1267     IMG_GFX_EDITOR_BUTTON_ROTATE_LEFT,          GADGET_ID_WRAP_LEFT,
1268     &editor.button.rotate_left,                 GD_TYPE_NORMAL_BUTTON,
1269     "wrap (rotate) level left",                 0
1270   },
1271   {
1272     IMG_GFX_EDITOR_BUTTON_ZOOM_LEVEL,           GADGET_ID_ZOOM,
1273     &editor.button.zoom_level,                  GD_TYPE_NORMAL_BUTTON,
1274     "zoom level tile size",                     '+'
1275   },
1276   {
1277     IMG_GFX_EDITOR_BUTTON_ROTATE_RIGHT,         GADGET_ID_WRAP_RIGHT,
1278     &editor.button.rotate_right,                GD_TYPE_NORMAL_BUTTON,
1279     "wrap (rotate) level right",                0
1280   },
1281   {
1282     IMG_GFX_EDITOR_BUTTON_DRAW_RANDOM,          GADGET_ID_RANDOM_PLACEMENT,
1283     &editor.button.draw_random,                 GD_TYPE_NORMAL_BUTTON,
1284     "random element placement",                 0
1285   },
1286   {
1287     IMG_GFX_EDITOR_BUTTON_GRAB_BRUSH,           GADGET_ID_GRAB_BRUSH,
1288     &editor.button.grab_brush,                  GD_TYPE_RADIO_BUTTON,
1289     "grab brush",                               'b'
1290   },
1291   {
1292     IMG_GFX_EDITOR_BUTTON_ROTATE_DOWN,          GADGET_ID_WRAP_DOWN,
1293     &editor.button.rotate_down,                 GD_TYPE_NORMAL_BUTTON,
1294     "wrap (rotate) level down",                 0
1295   },
1296   {
1297     IMG_GFX_EDITOR_BUTTON_PICK_ELEMENT,         GADGET_ID_PICK_ELEMENT,
1298     &editor.button.pick_element,                GD_TYPE_RADIO_BUTTON,
1299     "pick drawing element",                     ','
1300   },
1301
1302   // ---------- level control buttons -----------------------------------------
1303
1304   {
1305     IMG_GFX_EDITOR_BUTTON_UNDO,                 GADGET_ID_UNDO,
1306     &editor.button.undo,                        GD_TYPE_NORMAL_BUTTON,
1307     "undo/redo last operation",                 'u'
1308   },
1309   {
1310     IMG_GFX_EDITOR_BUTTON_CONF,                 GADGET_ID_CONF,
1311     &editor.button.conf,                        GD_TYPE_NORMAL_BUTTON,
1312     "level and editor settings",                'I'
1313   },
1314   {
1315     IMG_GFX_EDITOR_BUTTON_SAVE,                 GADGET_ID_SAVE,
1316     &editor.button.save,                        GD_TYPE_NORMAL_BUTTON,
1317     "save level",                               'S'
1318   },
1319   {
1320     IMG_GFX_EDITOR_BUTTON_CLEAR,                GADGET_ID_CLEAR,
1321     &editor.button.clear,                       GD_TYPE_NORMAL_BUTTON,
1322     "clear level",                              'C'
1323   },
1324   {
1325     IMG_GFX_EDITOR_BUTTON_TEST,                 GADGET_ID_TEST,
1326     &editor.button.test,                        GD_TYPE_NORMAL_BUTTON,
1327     "test level",                               'T'
1328   },
1329   {
1330     IMG_GFX_EDITOR_BUTTON_EXIT,                 GADGET_ID_EXIT,
1331     &editor.button.exit,                        GD_TYPE_NORMAL_BUTTON,
1332     "exit level editor",                        'E'
1333   },
1334
1335   // ---------- CE and GE control buttons -------------------------------------
1336
1337   {
1338     IMG_GFX_EDITOR_BUTTON_CE_COPY_FROM,         GADGET_ID_CUSTOM_COPY_FROM,
1339     &editor.button.ce_copy_from,                GD_TYPE_RADIO_BUTTON,
1340     "copy settings from other element",         0
1341   },
1342   {
1343     IMG_GFX_EDITOR_BUTTON_CE_COPY_TO,           GADGET_ID_CUSTOM_COPY_TO,
1344     &editor.button.ce_copy_to,                  GD_TYPE_RADIO_BUTTON,
1345     "copy settings to other element",           0
1346   },
1347   {
1348     IMG_GFX_EDITOR_BUTTON_CE_SWAP,              GADGET_ID_CUSTOM_EXCHANGE,
1349     &editor.button.ce_swap,                     GD_TYPE_RADIO_BUTTON,
1350     "exchange element with other element",      0
1351   },
1352   {
1353     IMG_GFX_EDITOR_BUTTON_CE_COPY,              GADGET_ID_CUSTOM_COPY,
1354     &editor.button.ce_copy,                     GD_TYPE_NORMAL_BUTTON,
1355     "copy settings from this element",          0
1356   },
1357   {
1358     IMG_GFX_EDITOR_BUTTON_CE_PASTE,             GADGET_ID_CUSTOM_PASTE,
1359     &editor.button.ce_paste,                    GD_TYPE_NORMAL_BUTTON,
1360     "paste settings to this element",           0
1361   },
1362
1363   // ---------- palette control buttons ---------------------------------------
1364
1365   {
1366     IMG_GFX_EDITOR_BUTTON_PROPERTIES,           GADGET_ID_PROPERTIES,
1367     &editor.button.properties,                  GD_TYPE_NORMAL_BUTTON,
1368     "properties of drawing element",            'p'
1369   },
1370   {
1371     IMG_GFX_EDITOR_BUTTON_ELEMENT_LEFT,         GADGET_ID_ELEMENT_LEFT,
1372     &editor.button.element_left,                GD_TYPE_NORMAL_BUTTON,
1373     "properties of drawing element 1",          '1'
1374   },
1375   {
1376     IMG_GFX_EDITOR_BUTTON_ELEMENT_MIDDLE,       GADGET_ID_ELEMENT_MIDDLE,
1377     &editor.button.element_middle,              GD_TYPE_NORMAL_BUTTON,
1378     "properties of drawing element 2",          '2'
1379   },
1380   {
1381     IMG_GFX_EDITOR_BUTTON_ELEMENT_RIGHT,        GADGET_ID_ELEMENT_RIGHT,
1382     &editor.button.element_right,               GD_TYPE_NORMAL_BUTTON,
1383     "properties of drawing element 3",          '3'
1384   },
1385   {
1386     IMG_GFX_EDITOR_BUTTON_PALETTE,              GADGET_ID_PALETTE,
1387     &editor.button.palette,                     GD_TYPE_NORMAL_BUTTON,
1388     "show list of elements",                    'e'
1389   }
1390 };
1391
1392 static int random_placement_value = 10;
1393 static int random_placement_method = RANDOM_USE_QUANTITY;
1394 static int random_placement_background_element = EL_SAND;
1395 static boolean random_placement_background_restricted = FALSE;
1396 static boolean stick_element_properties_window = FALSE;
1397 static boolean custom_element_properties[NUM_ELEMENT_PROPERTIES];
1398 static boolean custom_element_change_events[NUM_CHANGE_EVENTS];
1399 static struct ElementChangeInfo custom_element_change;
1400 static struct ElementGroupInfo group_element_info;
1401 static struct ElementInfo custom_element;
1402
1403 static char levelset_name[MAX_LEVEL_NAME_LEN + 1];
1404 static char levelset_author[MAX_LEVEL_AUTHOR_LEN + 1];
1405 static int levelset_num_levels = 100;
1406 static boolean levelset_use_levelset_artwork = FALSE;
1407 static boolean levelset_copy_level_template = FALSE;
1408 static int levelset_save_mode = LEVELSET_SAVE_MODE_UPDATE;
1409
1410 static struct
1411 {
1412   int gadget_type_id;
1413   int x, y;
1414   int min_value, max_value;
1415   int gadget_id_down, gadget_id_up;
1416   int gadget_id_text;
1417   int gadget_id_align;
1418   int *value;
1419   char *text_above, *text_left, *text_right;
1420 } counterbutton_info[ED_NUM_COUNTERBUTTONS] =
1421 {
1422   // ---------- current level number ------------------------------------------
1423
1424   {
1425     ED_COUNTER_ID_SELECT_LEVEL,
1426     -1, -1,     // these values are not constant, but can change at runtime
1427     1,                                  100,
1428     GADGET_ID_SELECT_LEVEL_DOWN,        GADGET_ID_SELECT_LEVEL_UP,
1429     GADGET_ID_SELECT_LEVEL_TEXT,        GADGET_ID_NONE,
1430     &level_nr,
1431     NULL,                               NULL, NULL
1432   },
1433
1434   // ---------- level and editor settings -------------------------------------
1435
1436   {
1437     ED_COUNTER_ID_LEVEL_XSIZE,
1438     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
1439     MIN_LEV_FIELDX,                     MAX_LEV_FIELDX,
1440     GADGET_ID_LEVEL_XSIZE_DOWN,         GADGET_ID_LEVEL_XSIZE_UP,
1441     GADGET_ID_LEVEL_XSIZE_TEXT,         GADGET_ID_NONE,
1442     &level.fieldx,
1443     "playfield size:",                  NULL, "width",
1444   },
1445   {
1446     ED_COUNTER_ID_LEVEL_YSIZE,
1447     -1,                                 ED_LEVEL_SETTINGS_YPOS(4),
1448     MIN_LEV_FIELDY,                     MAX_LEV_FIELDY,
1449     GADGET_ID_LEVEL_YSIZE_DOWN,         GADGET_ID_LEVEL_YSIZE_UP,
1450     GADGET_ID_LEVEL_YSIZE_TEXT,         GADGET_ID_LEVEL_XSIZE_UP,
1451     &level.fieldy,
1452     NULL,                               " ", "height",
1453   },
1454   {
1455     ED_COUNTER_ID_LEVEL_GEMSLIMIT,
1456     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(5),
1457     0,                                  999,
1458     GADGET_ID_LEVEL_GEMSLIMIT_DOWN,     GADGET_ID_LEVEL_GEMSLIMIT_UP,
1459     GADGET_ID_LEVEL_GEMSLIMIT_TEXT,     GADGET_ID_NONE,
1460     &level.gems_needed,
1461     NULL,                               "number of gems to collect:", NULL
1462   },
1463   {
1464     ED_COUNTER_ID_LEVEL_TIMELIMIT,
1465     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(8),
1466     0,                                  9999,
1467     GADGET_ID_LEVEL_TIMELIMIT_DOWN,     GADGET_ID_LEVEL_TIMELIMIT_UP,
1468     GADGET_ID_LEVEL_TIMELIMIT_TEXT,     GADGET_ID_NONE,
1469     &level.time,
1470     "time or step limit to solve level:", NULL, NULL
1471   },
1472   {
1473     ED_COUNTER_ID_LEVEL_TIMESCORE,
1474     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(10),
1475     0,                                  999,
1476     GADGET_ID_LEVEL_TIMESCORE_DOWN,     GADGET_ID_LEVEL_TIMESCORE_UP,
1477     GADGET_ID_LEVEL_TIMESCORE_TEXT,     GADGET_ID_NONE,
1478     &level.score[SC_TIME_BONUS],
1479     "score for time or steps left:",    NULL, NULL
1480   },
1481   {
1482     ED_COUNTER_ID_LEVEL_RANDOM_SEED,
1483     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(13),
1484     0,                                  9999,
1485     GADGET_ID_LEVEL_RANDOM_SEED_DOWN,   GADGET_ID_LEVEL_RANDOM_SEED_UP,
1486     GADGET_ID_LEVEL_RANDOM_SEED_TEXT,   GADGET_ID_NONE,
1487     &level.random_seed,
1488     NULL,                               "random seed:", "(0 => random)"
1489   },
1490   {
1491     ED_COUNTER_ID_LEVELSET_NUM_LEVELS,
1492     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
1493     1,                                  MAX_LEVELS,
1494     GADGET_ID_LEVELSET_NUM_LEVELS_DOWN, GADGET_ID_LEVELSET_NUM_LEVELS_UP,
1495     GADGET_ID_LEVELSET_NUM_LEVELS_TEXT, GADGET_ID_NONE,
1496     &levelset_num_levels,
1497     "number of levels:",                NULL, NULL,
1498   },
1499   {
1500     ED_COUNTER_ID_LEVEL_RANDOM,
1501     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(0),
1502     1,                                  100,
1503     GADGET_ID_LEVEL_RANDOM_DOWN,        GADGET_ID_LEVEL_RANDOM_UP,
1504     GADGET_ID_LEVEL_RANDOM_TEXT,        GADGET_ID_NONE,
1505     &random_placement_value,
1506     "random element placement:",        NULL, "in"
1507   },
1508   {
1509     ED_COUNTER_ID_BD_CYCLE_DELAY_MS,
1510     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(3),
1511     50,                                 500,
1512     GADGET_ID_BD_CYCLE_DELAY_MS_DOWN,   GADGET_ID_BD_CYCLE_DELAY_MS_UP,
1513     GADGET_ID_BD_CYCLE_DELAY_MS_TEXT,   GADGET_ID_NONE,
1514     &level.bd_cycle_delay_ms,
1515     NULL,                               NULL, "game cycle delay (ms)"
1516   },
1517   {
1518     ED_COUNTER_ID_BD_CYCLE_DELAY_C64,
1519     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(3),
1520     0,                                  32,
1521     GADGET_ID_BD_CYCLE_DELAY_C64_DOWN,  GADGET_ID_BD_CYCLE_DELAY_C64_UP,
1522     GADGET_ID_BD_CYCLE_DELAY_C64_TEXT,  GADGET_ID_NONE,
1523     &level.bd_cycle_delay_c64,
1524     NULL,                               NULL, "game cycle delay (C64-style)"
1525   },
1526   {
1527     ED_COUNTER_ID_BD_HATCHING_DELAY_CYCLES,
1528     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
1529     1,                                  40,
1530     GADGET_ID_BD_HATCHING_DELAY_CYCLES_DOWN,    GADGET_ID_BD_HATCHING_DELAY_CYCLES_UP,
1531     GADGET_ID_BD_HATCHING_DELAY_CYCLES_TEXT,    GADGET_ID_NONE,
1532     &level.bd_hatching_delay_cycles,
1533     NULL,                               NULL, "hatching delay (cycles)"
1534   },
1535   {
1536     ED_COUNTER_ID_BD_HATCHING_DELAY_SECONDS,
1537     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
1538     1,                                  40,
1539     GADGET_ID_BD_HATCHING_DELAY_SECONDS_DOWN,   GADGET_ID_BD_HATCHING_DELAY_SECONDS_UP,
1540     GADGET_ID_BD_HATCHING_DELAY_SECONDS_TEXT,   GADGET_ID_NONE,
1541     &level.bd_hatching_delay_seconds,
1542     NULL,                               NULL, "hatching delay (seconds)"
1543   },
1544
1545   // ---------- element settings: configure (various elements) ----------------
1546
1547   {
1548     ED_COUNTER_ID_ELEMENT_VALUE1,
1549     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
1550     MIN_SCORE,                          MAX_SCORE,
1551     GADGET_ID_ELEMENT_VALUE1_DOWN,      GADGET_ID_ELEMENT_VALUE1_UP,
1552     GADGET_ID_ELEMENT_VALUE1_TEXT,      GADGET_ID_NONE,
1553     NULL,                               // will be set when used
1554     NULL,                               NULL, NULL
1555   },
1556   {
1557     ED_COUNTER_ID_ELEMENT_VALUE2,
1558     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
1559     MIN_SCORE,                          MAX_SCORE,
1560     GADGET_ID_ELEMENT_VALUE2_DOWN,      GADGET_ID_ELEMENT_VALUE2_UP,
1561     GADGET_ID_ELEMENT_VALUE2_TEXT,      GADGET_ID_NONE,
1562     NULL,                               // will be set when used
1563     NULL,                               NULL, NULL
1564   },
1565   {
1566     ED_COUNTER_ID_ELEMENT_VALUE3,
1567     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
1568     MIN_SCORE,                          MAX_SCORE,
1569     GADGET_ID_ELEMENT_VALUE3_DOWN,      GADGET_ID_ELEMENT_VALUE3_UP,
1570     GADGET_ID_ELEMENT_VALUE3_TEXT,      GADGET_ID_NONE,
1571     NULL,                               // will be set when used
1572     NULL,                               NULL, NULL
1573   },
1574   {
1575     ED_COUNTER_ID_ELEMENT_VALUE4,
1576     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1577     MIN_SCORE,                          MAX_SCORE,
1578     GADGET_ID_ELEMENT_VALUE4_DOWN,      GADGET_ID_ELEMENT_VALUE4_UP,
1579     GADGET_ID_ELEMENT_VALUE4_TEXT,      GADGET_ID_NONE,
1580     NULL,                               // will be set when used
1581     NULL,                               NULL, NULL
1582   },
1583   {
1584     ED_COUNTER_ID_YAMYAM_CONTENT,
1585     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1586     MIN_ELEMENT_CONTENTS,               MAX_ELEMENT_CONTENTS,
1587     GADGET_ID_YAMYAM_CONTENT_DOWN,      GADGET_ID_YAMYAM_CONTENT_UP,
1588     GADGET_ID_YAMYAM_CONTENT_TEXT,      GADGET_ID_NONE,
1589     &level.num_yamyam_contents,
1590     NULL,                               NULL, "number of content areas"
1591   },
1592   {
1593     ED_COUNTER_ID_BALL_CONTENT,
1594     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
1595     MIN_ELEMENT_CONTENTS,               MAX_ELEMENT_CONTENTS,
1596     GADGET_ID_BALL_CONTENT_DOWN,        GADGET_ID_BALL_CONTENT_UP,
1597     GADGET_ID_BALL_CONTENT_TEXT,        GADGET_ID_NONE,
1598     &level.num_ball_contents,
1599     NULL,                               NULL, "number of content areas"
1600   },
1601   {
1602     ED_COUNTER_ID_ANDROID_CONTENT,
1603     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
1604     MIN_ANDROID_ELEMENTS,               MAX_ANDROID_ELEMENTS,
1605     GADGET_ID_ANDROID_CONTENT_DOWN,     GADGET_ID_ANDROID_CONTENT_UP,
1606     GADGET_ID_ANDROID_CONTENT_TEXT,     GADGET_ID_NONE,
1607     &level.num_android_clone_elements,
1608     NULL,                               NULL, "number of clonable elements"
1609   },
1610   {
1611     ED_COUNTER_ID_ENVELOPE_XSIZE,
1612     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
1613     MIN_ENVELOPE_XSIZE,                 MAX_ENVELOPE_XSIZE,
1614     GADGET_ID_ENVELOPE_XSIZE_DOWN,      GADGET_ID_ENVELOPE_XSIZE_UP,
1615     GADGET_ID_ENVELOPE_XSIZE_TEXT,      GADGET_ID_NONE,
1616     NULL,                               // will be set when used
1617     NULL,                               NULL, "width",
1618   },
1619   {
1620     ED_COUNTER_ID_ENVELOPE_YSIZE,
1621     -1,                                 ED_ELEMENT_SETTINGS_YPOS(0),
1622     MIN_ENVELOPE_YSIZE,                 MAX_ENVELOPE_YSIZE,
1623     GADGET_ID_ENVELOPE_YSIZE_DOWN,      GADGET_ID_ENVELOPE_YSIZE_UP,
1624     GADGET_ID_ENVELOPE_YSIZE_TEXT,      GADGET_ID_ENVELOPE_XSIZE_UP,
1625     NULL,                               // will be set when used
1626     NULL,                               " ", "height",
1627   },
1628   {
1629     ED_COUNTER_ID_INVENTORY_SIZE,
1630     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
1631     MIN_INITIAL_INVENTORY_SIZE,         MAX_INITIAL_INVENTORY_SIZE,
1632     GADGET_ID_INVENTORY_SIZE_DOWN,      GADGET_ID_INVENTORY_SIZE_UP,
1633     GADGET_ID_INVENTORY_SIZE_TEXT,      GADGET_ID_NONE,
1634     &level.initial_inventory_size[0],
1635     NULL,                               NULL, "number of inventory elements"
1636   },
1637   {
1638     ED_COUNTER_ID_MM_BALL_CONTENT,
1639     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1640     MIN_ELEMENTS_IN_GROUP,              MAX_MM_BALL_CONTENTS,
1641     GADGET_ID_MM_BALL_CONTENT_DOWN,     GADGET_ID_MM_BALL_CONTENT_UP,
1642     GADGET_ID_MM_BALL_CONTENT_TEXT,     GADGET_ID_NONE,
1643     &level.num_mm_ball_contents,
1644     NULL,                               NULL, "number of content elements"
1645   },
1646
1647   // ---------- element settings: configure 1 (custom elements) ---------------
1648
1649   {
1650     ED_COUNTER_ID_CUSTOM_SCORE,
1651     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(5),
1652     MIN_SCORE,                          MAX_SCORE,
1653     GADGET_ID_CUSTOM_SCORE_DOWN,        GADGET_ID_CUSTOM_SCORE_UP,
1654     GADGET_ID_CUSTOM_SCORE_TEXT,        GADGET_ID_NONE,
1655     &custom_element.collect_score_initial,
1656     NULL,                               "CE score", " "
1657   },
1658   {
1659     ED_COUNTER_ID_CUSTOM_GEMCOUNT,
1660     -1,                                 ED_ELEMENT_SETTINGS_YPOS(5),
1661     MIN_COLLECT_COUNT,                  MAX_COLLECT_COUNT,
1662     GADGET_ID_CUSTOM_GEMCOUNT_DOWN,     GADGET_ID_CUSTOM_GEMCOUNT_UP,
1663     GADGET_ID_CUSTOM_GEMCOUNT_TEXT,     GADGET_ID_CUSTOM_SCORE_UP,
1664     &custom_element.collect_count_initial,
1665     NULL,                               "CE count", NULL
1666   },
1667   {
1668     ED_COUNTER_ID_CUSTOM_VALUE_FIX,
1669     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(10),
1670     0,                                  9999,
1671     GADGET_ID_CUSTOM_VALUE_FIX_DOWN,    GADGET_ID_CUSTOM_VALUE_FIX_UP,
1672     GADGET_ID_CUSTOM_VALUE_FIX_TEXT,    GADGET_ID_NONE,
1673     &custom_element.ce_value_fixed_initial,
1674     NULL,                               "CE value", NULL
1675   },
1676   {
1677     ED_COUNTER_ID_CUSTOM_VALUE_RND,
1678     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(10),
1679     0,                                  9999,
1680     GADGET_ID_CUSTOM_VALUE_RND_DOWN,    GADGET_ID_CUSTOM_VALUE_RND_UP,
1681     GADGET_ID_CUSTOM_VALUE_RND_TEXT,    GADGET_ID_CUSTOM_VALUE_FIX_UP,
1682     &custom_element.ce_value_random_initial,
1683     NULL,                               "+random", NULL
1684   },
1685   {
1686     ED_COUNTER_ID_PUSH_DELAY_FIX,
1687     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(6),
1688     0,                                  999,
1689     GADGET_ID_PUSH_DELAY_FIX_DOWN,      GADGET_ID_PUSH_DELAY_FIX_UP,
1690     GADGET_ID_PUSH_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1691     &custom_element.push_delay_fixed,
1692     NULL,                               "push delay", NULL
1693   },
1694   {
1695     ED_COUNTER_ID_PUSH_DELAY_RND,
1696     -1,                                 ED_ELEMENT_SETTINGS_YPOS(6),
1697     0,                                  999,
1698     GADGET_ID_PUSH_DELAY_RND_DOWN,      GADGET_ID_PUSH_DELAY_RND_UP,
1699     GADGET_ID_PUSH_DELAY_RND_TEXT,      GADGET_ID_PUSH_DELAY_FIX_UP,
1700     &custom_element.push_delay_random,
1701     NULL,                               "+random", NULL
1702   },
1703   {
1704     ED_COUNTER_ID_DROP_DELAY_FIX,
1705     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(7),
1706     0,                                  999,
1707     GADGET_ID_DROP_DELAY_FIX_DOWN,      GADGET_ID_DROP_DELAY_FIX_UP,
1708     GADGET_ID_DROP_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1709     &custom_element.drop_delay_fixed,
1710     NULL,                               "drop delay", NULL
1711   },
1712   {
1713     ED_COUNTER_ID_DROP_DELAY_RND,
1714     -1,                                 ED_ELEMENT_SETTINGS_YPOS(7),
1715     0,                                  999,
1716     GADGET_ID_DROP_DELAY_RND_DOWN,      GADGET_ID_DROP_DELAY_RND_UP,
1717     GADGET_ID_DROP_DELAY_RND_TEXT,      GADGET_ID_DROP_DELAY_FIX_UP,
1718     &custom_element.drop_delay_random,
1719     NULL,                               "+random", NULL
1720   },
1721
1722   // ---------- element settings: configure 2 (custom elements) ---------------
1723
1724   {
1725     ED_COUNTER_ID_MOVE_DELAY_FIX,
1726     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(5),
1727     0,                                  999,
1728     GADGET_ID_MOVE_DELAY_FIX_DOWN,      GADGET_ID_MOVE_DELAY_FIX_UP,
1729     GADGET_ID_MOVE_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1730     &custom_element.move_delay_fixed,
1731     NULL,                               "move delay", NULL
1732   },
1733   {
1734     ED_COUNTER_ID_MOVE_DELAY_RND,
1735     -1,                                 ED_ELEMENT_SETTINGS_YPOS(5),
1736     0,                                  999,
1737     GADGET_ID_MOVE_DELAY_RND_DOWN,      GADGET_ID_MOVE_DELAY_RND_UP,
1738     GADGET_ID_MOVE_DELAY_RND_TEXT,      GADGET_ID_MOVE_DELAY_FIX_UP,
1739     &custom_element.move_delay_random,
1740     NULL,                               "+random", NULL
1741   },
1742   {
1743     ED_COUNTER_ID_STEP_DELAY_FIX,
1744     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(6),
1745     0,                                  999,
1746     GADGET_ID_STEP_DELAY_FIX_DOWN,      GADGET_ID_STEP_DELAY_FIX_UP,
1747     GADGET_ID_STEP_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1748     &custom_element.step_delay_fixed,
1749     NULL,                               "step delay", NULL
1750   },
1751   {
1752     ED_COUNTER_ID_STEP_DELAY_RND,
1753     -1,                                 ED_ELEMENT_SETTINGS_YPOS(6),
1754     0,                                  999,
1755     GADGET_ID_STEP_DELAY_RND_DOWN,      GADGET_ID_STEP_DELAY_RND_UP,
1756     GADGET_ID_STEP_DELAY_RND_TEXT,      GADGET_ID_STEP_DELAY_FIX_UP,
1757     &custom_element.step_delay_random,
1758     NULL,                               "+random", NULL
1759   },
1760   {
1761     ED_COUNTER_ID_EXPLOSION_DELAY,
1762     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(13),
1763     0,                                  999,
1764     GADGET_ID_EXPLOSION_DELAY_DOWN,     GADGET_ID_EXPLOSION_DELAY_UP,
1765     GADGET_ID_EXPLOSION_DELAY_TEXT,     GADGET_ID_NONE,
1766     &custom_element.explosion_delay,
1767     NULL,                               "explosion delay", NULL
1768   },
1769   {
1770     ED_COUNTER_ID_IGNITION_DELAY,
1771     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(14),
1772     0,                                  999,
1773     GADGET_ID_IGNITION_DELAY_DOWN,      GADGET_ID_IGNITION_DELAY_UP,
1774     GADGET_ID_IGNITION_DELAY_TEXT,      GADGET_ID_NONE,
1775     &custom_element.ignition_delay,
1776     NULL,                               "ignition delay", "(by fire)"
1777   },
1778
1779   // ---------- element settings: configure (group elements) ------------------
1780
1781   {
1782     ED_COUNTER_ID_GROUP_CONTENT,
1783     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1784     MIN_ELEMENTS_IN_GROUP,              MAX_ELEMENTS_IN_GROUP,
1785     GADGET_ID_GROUP_CONTENT_DOWN,       GADGET_ID_GROUP_CONTENT_UP,
1786     GADGET_ID_GROUP_CONTENT_TEXT,       GADGET_ID_NONE,
1787     &group_element_info.num_elements,
1788     NULL,                               NULL, "number of elements in group"
1789   },
1790
1791   // ---------- element settings: advanced (custom elements) ------------------
1792
1793   {
1794     ED_COUNTER_ID_CHANGE_DELAY_FIX,
1795     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(2),
1796     0,                                  999,
1797     GADGET_ID_CHANGE_DELAY_FIX_DOWN,    GADGET_ID_CHANGE_DELAY_FIX_UP,
1798     GADGET_ID_CHANGE_DELAY_FIX_TEXT,    GADGET_ID_NONE,
1799     &custom_element_change.delay_fixed,
1800     NULL,                               "CE delay", NULL,
1801   },
1802   {
1803     ED_COUNTER_ID_CHANGE_DELAY_RND,
1804     -1,                                 ED_ELEMENT_SETTINGS_YPOS(2),
1805     0,                                  999,
1806     GADGET_ID_CHANGE_DELAY_RND_DOWN,    GADGET_ID_CHANGE_DELAY_RND_UP,
1807     GADGET_ID_CHANGE_DELAY_RND_TEXT,    GADGET_ID_CHANGE_DELAY_FIX_UP,
1808     &custom_element_change.delay_random,
1809     NULL,                               "+random", NULL
1810   },
1811   {
1812     ED_COUNTER_ID_CHANGE_CONT_RND,
1813     ED_ELEMENT_SETTINGS_XPOS(3),        ED_ELEMENT_SETTINGS_YPOS(12),
1814     0,                                  100,
1815     GADGET_ID_CHANGE_CONT_RND_DOWN,     GADGET_ID_CHANGE_CONT_RND_UP,
1816     GADGET_ID_CHANGE_CONT_RND_TEXT,     GADGET_ID_NONE,
1817     &custom_element_change.random_percentage,
1818     NULL,                               "use random replace:", "%"
1819   },
1820 };
1821
1822 static struct
1823 {
1824   int gadget_type_id;
1825   int x, y;
1826   int gadget_id;
1827   int size;
1828   char *value;
1829   char *text_above, *infotext;
1830 } textinput_info[ED_NUM_TEXTINPUT] =
1831 {
1832   {
1833     ED_TEXTINPUT_ID_LEVEL_NAME,
1834     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(0),
1835     GADGET_ID_LEVEL_NAME,
1836     MAX_LEVEL_NAME_LEN,
1837     level.name,
1838     "Title:", "Title for this level"
1839   },
1840   {
1841     ED_TEXTINPUT_ID_LEVEL_AUTHOR,
1842     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(2),
1843     GADGET_ID_LEVEL_AUTHOR,
1844     MAX_LEVEL_AUTHOR_LEN,
1845     level.author,
1846     "Author:", "Author for this level"
1847   },
1848   {
1849     ED_TEXTINPUT_ID_LEVELSET_NAME,
1850     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(0),
1851     GADGET_ID_LEVELSET_NAME,
1852     MAX_LEVEL_NAME_LEN,
1853     levelset_name,
1854     "Title:", "Title for this or new level set"
1855   },
1856   {
1857     ED_TEXTINPUT_ID_LEVELSET_AUTHOR,
1858     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(2),
1859     GADGET_ID_LEVELSET_AUTHOR,
1860     MAX_LEVEL_AUTHOR_LEN,
1861     levelset_author,
1862     "Author:", "Author for this or new level set"
1863   },
1864   {
1865     ED_TEXTINPUT_ID_ELEMENT_NAME,
1866     -1, -1,     // these values are not constant, but can change at runtime
1867     GADGET_ID_ELEMENT_NAME,
1868     MAX_ELEMENT_NAME_LEN - 2,           // currently 2 chars less editable
1869     custom_element.description,
1870     NULL, "Element name"
1871   }
1872 };
1873
1874 static struct
1875 {
1876   int gadget_type_id;
1877   int x, y;
1878   int gadget_id;
1879   int xsize, ysize;
1880   char *value;
1881   char *text_above, *text_above_cropped, *infotext;
1882 } textarea_info[ED_NUM_TEXTAREAS] =
1883 {
1884   {
1885     ED_TEXTAREA_ID_ENVELOPE_INFO,
1886     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
1887     GADGET_ID_ENVELOPE_INFO,
1888     MAX_ENVELOPE_XSIZE, MAX_ENVELOPE_YSIZE,
1889     NULL,
1890     "Envelope Content:", "Envelope Content (cropped):", "Envelope Content"
1891   }
1892 };
1893
1894 static struct ValueTextInfo options_time_or_steps[] =
1895 {
1896   { 0,                          "seconds"                       },
1897   { 1,                          "steps"                         },
1898
1899   { -1,                         NULL                            }
1900 };
1901
1902 static struct ValueTextInfo options_time_score_base[] =
1903 {
1904   { 1,                          "per second/step"               },
1905   { 10,                         "per 10 seconds/steps"          },
1906
1907   { -1,                         NULL                            }
1908 };
1909
1910 static struct ValueTextInfo options_game_engine_type[] =
1911 {
1912   { GAME_ENGINE_TYPE_RND,       "Rocks'n'Diamonds"              },
1913   { GAME_ENGINE_TYPE_BD,        "Boulder Dash"                  },
1914   { GAME_ENGINE_TYPE_EM,        "Emerald Mine"                  },
1915   { GAME_ENGINE_TYPE_SP,        "Supaplex"                      },
1916   { GAME_ENGINE_TYPE_MM,        "Mirror Magic"                  },
1917
1918   { -1,                         NULL                            }
1919 };
1920
1921 static struct ValueTextInfo options_levelset_save_mode[] =
1922 {
1923   { LEVELSET_SAVE_MODE_UPDATE,  "Update this level set"         },
1924   { LEVELSET_SAVE_MODE_CREATE,  "Create new level set"          },
1925
1926   { -1,                         NULL                            }
1927 };
1928
1929 static struct ValueTextInfo options_wind_direction[] =
1930 {
1931   { MV_START_NONE,              "none"                          },
1932   { MV_START_LEFT,              "left"                          },
1933   { MV_START_RIGHT,             "right"                         },
1934   { MV_START_UP,                "up"                            },
1935   { MV_START_DOWN,              "down"                          },
1936
1937   { -1,                         NULL                            }
1938 };
1939
1940 static struct ValueTextInfo options_player_speed[] =
1941 {
1942   { 0,                          "frozen"                        },
1943   { 1,                          "very slow"                     },
1944   { 2,                          "slow"                          },
1945   { 4,                          "normal"                        },
1946   { 8,                          "fast"                          },
1947   { 16,                         "very fast"                     },
1948   { 32,                         "ultrafast"                     },
1949
1950   { -1,                         NULL                            }
1951 };
1952
1953 static struct ValueTextInfo options_access_type[] =
1954 {
1955   { EP_WALKABLE,                "walkable"                      },
1956   { EP_PASSABLE,                "passable"                      },
1957
1958   { -1,                         NULL                            }
1959 };
1960
1961 static struct ValueTextInfo options_access_layer[] =
1962 {
1963   { EP_ACCESSIBLE_OVER,         "over"                          },
1964   { EP_ACCESSIBLE_INSIDE,       "inside"                        },
1965   { EP_ACCESSIBLE_UNDER,        "under"                         },
1966
1967   { -1,                         NULL                            }
1968 };
1969
1970 static struct ValueTextInfo options_access_protected[] =
1971 {
1972   { 0,                          "unprotected"                   },
1973   { 1,                          "protected"                     },
1974
1975   { -1,                         NULL                            }
1976 };
1977
1978 static struct ValueTextInfo options_access_direction[] =
1979 {
1980   { MV_NO_DIRECTION,            "no direction"                  },
1981   { MV_LEFT,                    "left"                          },
1982   { MV_RIGHT,                   "right"                         },
1983   { MV_UP,                      "up"                            },
1984   { MV_DOWN,                    "down"                          },
1985   { MV_LEFT  | MV_UP,           "left + up"                     },
1986   { MV_LEFT  | MV_DOWN,         "left + down"                   },
1987   { MV_RIGHT | MV_UP,           "right + up"                    },
1988   { MV_RIGHT | MV_DOWN,         "right + down"                  },
1989   { MV_HORIZONTAL,              "horizontal"                    },
1990   { MV_VERTICAL,                "vertical"                      },
1991   { MV_HORIZONTAL | MV_UP,      "horizontal + up"               },
1992   { MV_HORIZONTAL | MV_DOWN,    "horizontal + down"             },
1993   { MV_VERTICAL   | MV_LEFT,    "vertical + left"               },
1994   { MV_VERTICAL   | MV_RIGHT,   "vertical + right"              },
1995   { MV_ALL_DIRECTIONS,          "all directions"                },
1996
1997   { -1,                         NULL                            }
1998 };
1999
2000 static struct ValueTextInfo options_walk_to_action[] =
2001 {
2002   { EP_DIGGABLE,                "diggable"                      },
2003   { EP_COLLECTIBLE_ONLY,        "collectible"                   },
2004   { EP_DROPPABLE,               "collectible & droppable"       },
2005   { EP_THROWABLE,               "collectible & throwable"       },
2006   { EP_PUSHABLE,                "pushable"                      },
2007
2008   { -1,                         NULL                            }
2009 };
2010
2011 static struct ValueTextInfo options_move_pattern[] =
2012 {
2013   { MV_LEFT,                    "left"                          },
2014   { MV_RIGHT,                   "right"                         },
2015   { MV_UP,                      "up"                            },
2016   { MV_DOWN,                    "down"                          },
2017   { MV_HORIZONTAL,              "horizontal"                    },
2018   { MV_VERTICAL,                "vertical"                      },
2019   { MV_ALL_DIRECTIONS,          "all directions"                },
2020   { MV_WIND_DIRECTION,          "wind direction"                },
2021   { MV_TOWARDS_PLAYER,          "towards player"                },
2022   { MV_AWAY_FROM_PLAYER,        "away from player"              },
2023   { MV_ALONG_LEFT_SIDE,         "along left side"               },
2024   { MV_ALONG_RIGHT_SIDE,        "along right side"              },
2025   { MV_TURNING_LEFT,            "turning left"                  },
2026   { MV_TURNING_RIGHT,           "turning right"                 },
2027   { MV_TURNING_LEFT_RIGHT,      "turning left, right"           },
2028   { MV_TURNING_RIGHT_LEFT,      "turning right, left"           },
2029   { MV_TURNING_RANDOM,          "turning random"                },
2030   { MV_MAZE_RUNNER,             "maze runner style"             },
2031   { MV_MAZE_HUNTER,             "maze hunter style"             },
2032   { MV_WHEN_PUSHED,             "when pushed"                   },
2033   { MV_WHEN_DROPPED,            "when dropped/thrown"           },
2034
2035   { -1,                         NULL                            }
2036 };
2037
2038 static struct ValueTextInfo options_move_direction[] =
2039 {
2040   { MV_START_AUTOMATIC,         "automatic"                     },
2041   { MV_START_LEFT,              "left"                          },
2042   { MV_START_RIGHT,             "right"                         },
2043   { MV_START_UP,                "up"                            },
2044   { MV_START_DOWN,              "down"                          },
2045   { MV_START_RANDOM,            "random"                        },
2046   { MV_START_PREVIOUS,          "previous"                      },
2047
2048   { -1,                         NULL                            }
2049 };
2050
2051 static struct ValueTextInfo options_move_stepsize[] =
2052 {
2053   { 0,                          "not moving"                    },
2054   { 1,                          "very slow"                     },
2055   { 2,                          "slow"                          },
2056   { 4,                          "normal"                        },
2057   { 8,                          "fast"                          },
2058   { 16,                         "very fast"                     },
2059   { 32,                         "even faster"                   },
2060
2061   { -1,                         NULL                            }
2062 };
2063
2064 static struct ValueTextInfo options_move_leave_type[] =
2065 {
2066   { LEAVE_TYPE_UNLIMITED,       "leave behind"                  },
2067   { LEAVE_TYPE_LIMITED,         "change it to"                  },
2068
2069   { -1,                         NULL                            }
2070 };
2071
2072 static struct ValueTextInfo options_smash_targets[] =
2073 {
2074   { EP_CAN_SMASH_PLAYER,        "player"                        },
2075 #if 0
2076   { EP_CAN_SMASH_ENEMIES,       "enemies"                       },
2077 #endif
2078   { EP_CAN_SMASH_EVERYTHING,    "everything"                    },
2079
2080   { -1,                         NULL                            }
2081 };
2082
2083 static struct ValueTextInfo options_slippery_type[] =
2084 {
2085   { SLIPPERY_ANY_RANDOM,        "random"                        },
2086   { SLIPPERY_ANY_LEFT_RIGHT,    "left, right"                   },
2087   { SLIPPERY_ANY_RIGHT_LEFT,    "right, left"                   },
2088   { SLIPPERY_ONLY_LEFT,         "only left"                     },
2089   { SLIPPERY_ONLY_RIGHT,        "only right"                    },
2090
2091   { -1,                         NULL                            }
2092 };
2093
2094 static struct ValueTextInfo options_deadliness[] =
2095 {
2096   { EP_DONT_RUN_INTO,           "running into"                  },
2097   { EP_DONT_COLLIDE_WITH,       "colliding with"                },
2098   { EP_DONT_GET_HIT_BY,         "getting hit by"                },
2099   { EP_DONT_TOUCH,              "touching"                      },
2100
2101   { -1,                         NULL                            }
2102 };
2103
2104 static struct ValueTextInfo options_explosion_type[] =
2105 {
2106   { EXPLODES_3X3,               "3x3"                           },
2107   { EXPLODES_CROSS,             "3+3"                           },
2108   { EXPLODES_1X1,               "1x1"                           },
2109
2110   { -1,                         NULL                            }
2111 };
2112
2113 static struct ValueTextInfo options_time_units[] =
2114 {
2115   { 1,                          "frames"                        },
2116   { FRAMES_PER_SECOND,          "seconds"                       },
2117
2118   { -1,                         NULL                            }
2119 };
2120
2121 static struct ValueTextInfo options_change_direct_action[] =
2122 {
2123   { CE_TOUCHED_BY_PLAYER,       "touched by player"             },
2124   { CE_PRESSED_BY_PLAYER,       "pressed by player"             },
2125   { CE_SWITCHED_BY_PLAYER,      "switched by player"            },
2126   { CE_SNAPPED_BY_PLAYER,       "snapped by player"             },
2127   { CE_PUSHED_BY_PLAYER,        "pushed by player"              },
2128   { CE_ENTERED_BY_PLAYER,       "entered by player"             },
2129   { CE_LEFT_BY_PLAYER,          "left by player"                },
2130   { CE_DROPPED_BY_PLAYER,       "dropped/thrown by player"      },
2131   { CE_SWITCHED,                "switched"                      },
2132   { CE_HITTING_SOMETHING,       "hitting something"             },
2133   { CE_HIT_BY_SOMETHING,        "hit by something"              },
2134 #if 0
2135   { CE_BLOCKED,                 "blocked"                       },
2136 #endif
2137   { CE_IMPACT,                  "impact (on something)"         },
2138   { CE_SMASHED,                 "smashed (from above)"          },
2139 #if 0
2140   { CE_VALUE_CHANGES,           "CE value changes"              },
2141   { CE_SCORE_CHANGES,           "CE score changes"              },
2142 #endif
2143   { CE_VALUE_GETS_ZERO,         "CE value gets 0"               },
2144   { CE_SCORE_GETS_ZERO,         "CE score gets 0"               },
2145   { CE_UNDEFINED,               " "                             },
2146   { CE_HEADLINE_SPECIAL_EVENTS, "[mouse events]"                },
2147   { CE_CLICKED_BY_MOUSE,        "clicked by mouse"              },
2148   { CE_PRESSED_BY_MOUSE,        "pressed by mouse"              },
2149   { CE_UNDEFINED,               " "                             },
2150   { CE_HEADLINE_SPECIAL_EVENTS, "[static states]"               },
2151   { CE_NEXT_TO_PLAYER,          "next to player"                },
2152
2153   { -1,                         NULL                            }
2154 };
2155
2156 static struct ValueTextInfo options_change_other_action[] =
2157 {
2158   { CE_PLAYER_TOUCHES_X,        "player touches"                },
2159   { CE_PLAYER_PRESSES_X,        "player presses"                },
2160   { CE_PLAYER_SWITCHES_X,       "player switches"               },
2161   { CE_PLAYER_SNAPS_X,          "player snaps"                  },
2162   { CE_PLAYER_PUSHES_X,         "player pushes"                 },
2163   { CE_PLAYER_ENTERS_X,         "player enters"                 },
2164   { CE_PLAYER_LEAVES_X,         "player leaves"                 },
2165   { CE_PLAYER_DIGS_X,           "player digs"                   },
2166   { CE_PLAYER_COLLECTS_X,       "player collects"               },
2167   { CE_PLAYER_DROPS_X,          "player drops/throws"           },
2168   { CE_TOUCHING_X,              "touching"                      },
2169   { CE_HITTING_X,               "hitting"                       },
2170   { CE_DIGGING_X,               "digging"                       },
2171   { CE_HIT_BY_X,                "hit by"                        },
2172   { CE_SWITCH_OF_X,             "switch of"                     },
2173   { CE_CHANGE_OF_X,             "change by page of"             },
2174   { CE_EXPLOSION_OF_X,          "explosion of"                  },
2175   { CE_MOVE_OF_X,               "move of"                       },
2176   { CE_CREATION_OF_X,           "creation of"                   },
2177   { CE_VALUE_CHANGES_OF_X,      "CE value changes of"           },
2178   { CE_SCORE_CHANGES_OF_X,      "CE score changes of"           },
2179   { CE_VALUE_GETS_ZERO_OF_X,    "CE value gets 0 of"            },
2180   { CE_SCORE_GETS_ZERO_OF_X,    "CE score gets 0 of"            },
2181   { CE_UNDEFINED,               " "                             },
2182   { CE_HEADLINE_SPECIAL_EVENTS, "[mouse events]"                },
2183   { CE_MOUSE_CLICKED_ON_X,      "mouse clicked on"              },
2184   { CE_MOUSE_PRESSED_ON_X,      "mouse pressed on"              },
2185   { CE_UNDEFINED,               " "                             },
2186   { CE_HEADLINE_SPECIAL_EVENTS, "[static states]"               },
2187   { CE_PLAYER_NEXT_TO_X,        "player next to"                },
2188   { CE_NEXT_TO_X,               "next to"                       },
2189
2190   { -1,                         NULL                            }
2191 };
2192
2193 static struct ValueTextInfo options_change_trigger_side[] =
2194 {
2195   { CH_SIDE_LEFT,               "left"                          },
2196   { CH_SIDE_RIGHT,              "right"                         },
2197   { CH_SIDE_TOP,                "top"                           },
2198   { CH_SIDE_BOTTOM,             "bottom"                        },
2199   { CH_SIDE_LEFT_RIGHT,         "left/right"                    },
2200   { CH_SIDE_TOP_BOTTOM,         "top/bottom"                    },
2201   { CH_SIDE_ANY,                "any"                           },
2202
2203   { -1,                         NULL                            }
2204 };
2205
2206 static struct ValueTextInfo options_change_trigger_player[] =
2207 {
2208   { CH_PLAYER_1,                "1"                             },
2209   { CH_PLAYER_2,                "2"                             },
2210   { CH_PLAYER_3,                "3"                             },
2211   { CH_PLAYER_4,                "4"                             },
2212   { CH_PLAYER_ANY,              "any"                           },
2213
2214   { -1,                         NULL                            }
2215 };
2216
2217 static struct ValueTextInfo options_change_trigger_page[] =
2218 {
2219   { (1u << 0),                  "1"                             },
2220   { (1u << 1),                  "2"                             },
2221   { (1u << 2),                  "3"                             },
2222   { (1u << 3),                  "4"                             },
2223   { (1u << 4),                  "5"                             },
2224   { (1u << 5),                  "6"                             },
2225   { (1u << 6),                  "7"                             },
2226   { (1u << 7),                  "8"                             },
2227   { (1u << 8),                  "9"                             },
2228   { (1u << 9),                  "10"                            },
2229   { (1u << 10),                 "11"                            },
2230   { (1u << 11),                 "12"                            },
2231   { (1u << 12),                 "13"                            },
2232   { (1u << 13),                 "14"                            },
2233   { (1u << 14),                 "15"                            },
2234   { (1u << 15),                 "16"                            },
2235   { (1u << 16),                 "17"                            },
2236   { (1u << 17),                 "18"                            },
2237   { (1u << 18),                 "19"                            },
2238   { (1u << 19),                 "20"                            },
2239   { (1u << 20),                 "21"                            },
2240   { (1u << 21),                 "22"                            },
2241   { (1u << 22),                 "23"                            },
2242   { (1u << 23),                 "24"                            },
2243   { (1u << 24),                 "25"                            },
2244   { (1u << 25),                 "26"                            },
2245   { (1u << 26),                 "27"                            },
2246   { (1u << 27),                 "28"                            },
2247   { (1u << 28),                 "29"                            },
2248   { (1u << 29),                 "30"                            },
2249   { (1u << 30),                 "31"                            },
2250   { (1u << 31),                 "32"                            },
2251   { CH_PAGE_ANY,                "any"                           },
2252
2253   { -1,                         NULL                            }
2254 };
2255
2256 static struct ValueTextInfo options_change_replace_when[] =
2257 {
2258   { CP_WHEN_EMPTY,              "empty"                         },
2259   { CP_WHEN_WALKABLE,           "walkable"                      },
2260   { CP_WHEN_DIGGABLE,           "diggable"                      },
2261   { CP_WHEN_COLLECTIBLE,        "collectible"                   },
2262   { CP_WHEN_REMOVABLE,          "removable"                     },
2263   { CP_WHEN_DESTRUCTIBLE,       "destructible"                  },
2264
2265   { -1,                         NULL                            }
2266 };
2267
2268 static struct ValueTextInfo options_action_type[] =
2269 {
2270   { CA_NO_ACTION,               "no action"                     },
2271   { CA_UNDEFINED,               " "                             },
2272   { CA_HEADLINE_LEVEL_ACTIONS,  "[level]"                       },
2273   { CA_RESTART_LEVEL,           "restart level"                 },
2274   { CA_SHOW_ENVELOPE,           "show envelope"                 },
2275   { CA_SET_LEVEL_TIME,          "set time"                      },
2276   { CA_SET_LEVEL_SCORE,         "set score"                     },
2277   { CA_SET_LEVEL_GEMS,          "set gems"                      },
2278   { CA_SET_LEVEL_WIND,          "set wind dir."                 },
2279   { CA_SET_LEVEL_RANDOM_SEED,   "set random seed"               },
2280   { CA_UNDEFINED,               " "                             },
2281   { CA_HEADLINE_PLAYER_ACTIONS, "[player]"                      },
2282   { CA_MOVE_PLAYER,             "move player"                   },
2283   { CA_MOVE_PLAYER_NEW,         "move player new"               },
2284   { CA_EXIT_PLAYER,             "exit player"                   },
2285   { CA_KILL_PLAYER,             "kill player"                   },
2286   { CA_SET_PLAYER_KEYS,         "set keys"                      },
2287   { CA_SET_PLAYER_SPEED,        "set speed"                     },
2288   { CA_SET_PLAYER_SHIELD,       "set shield"                    },
2289   { CA_SET_PLAYER_GRAVITY,      "set gravity"                   },
2290   { CA_SET_PLAYER_ARTWORK,      "set artwork"                   },
2291   { CA_SET_PLAYER_INVENTORY,    "set inventory"                 },
2292   { CA_UNDEFINED,               " "                             },
2293   { CA_HEADLINE_CE_ACTIONS,     "[CE]"                          },
2294   { CA_SET_CE_VALUE,            "set CE value"                  },
2295   { CA_SET_CE_SCORE,            "set CE score"                  },
2296   { CA_SET_CE_ARTWORK,          "set CE artwork"                },
2297   { CA_UNDEFINED,               " "                             },
2298   { CA_HEADLINE_ENGINE_ACTIONS, "[engine]"                      },
2299   { CA_SET_ENGINE_SCAN_MODE,    "set scan mode"                 },
2300
2301   { -1,                         NULL                            }
2302 };
2303
2304 static struct ValueTextInfo options_action_mode_none[] =
2305 {
2306   { CA_MODE_UNDEFINED,          " "                             },
2307
2308   { -1,                         NULL                            }
2309 };
2310
2311 static struct ValueTextInfo options_action_mode_assign[] =
2312 {
2313   { CA_MODE_SET,                "="                             },
2314
2315   { -1,                         NULL                            }
2316 };
2317
2318 static struct ValueTextInfo options_action_mode_add_remove[] =
2319 {
2320   { CA_MODE_ADD,                "+"                             },
2321   { CA_MODE_SUBTRACT,           "-"                             },
2322
2323   { -1,                         NULL                            }
2324 };
2325
2326 static struct ValueTextInfo options_action_mode_calculate[] =
2327 {
2328   { CA_MODE_SET,                "="                             },
2329   { CA_MODE_ADD,                "+"                             },
2330   { CA_MODE_SUBTRACT,           "-"                             },
2331   { CA_MODE_MULTIPLY,           "*"                             },
2332   { CA_MODE_DIVIDE,             "/"                             },
2333   { CA_MODE_MODULO,             "%"                             },
2334
2335   { -1,                         NULL                            }
2336 };
2337
2338 static struct ValueTextInfo options_action_arg_none[] =
2339 {
2340   { CA_ARG_UNDEFINED,           "         "                     },
2341
2342   { -1,                         NULL                            }
2343 };
2344
2345 static struct ValueTextInfo options_action_arg_player[] =
2346 {
2347   { CA_ARG_PLAYER_HEADLINE,     "[player]"                      },
2348   { CA_ARG_PLAYER_1,            "1"                             },
2349   { CA_ARG_PLAYER_2,            "2"                             },
2350   { CA_ARG_PLAYER_3,            "3"                             },
2351   { CA_ARG_PLAYER_4,            "4"                             },
2352   { CA_ARG_PLAYER_ANY,          "any"                           },
2353   { CA_ARG_PLAYER_TRIGGER,      "trigger"                       },
2354   { CA_ARG_PLAYER_ACTION,       "action ->"                     },
2355
2356   { -1,                         NULL                            }
2357 };
2358
2359 static struct ValueTextInfo options_action_arg_number[] =
2360 {
2361   { CA_ARG_NUMBER_HEADLINE,     "[number]"                      },
2362   { CA_ARG_0,                   "0"                             },
2363   { CA_ARG_1,                   "1"                             },
2364   { CA_ARG_2,                   "2"                             },
2365   { CA_ARG_3,                   "3"                             },
2366   { CA_ARG_4,                   "4"                             },
2367   { CA_ARG_5,                   "5"                             },
2368   { CA_ARG_10,                  "10"                            },
2369   { CA_ARG_100,                 "100"                           },
2370   { CA_ARG_1000,                "1000"                          },
2371   { CA_ARG_UNDEFINED,           " "                             },
2372   { CA_ARG_NUMBER_MIN,          "min"                           },
2373   { CA_ARG_NUMBER_MAX,          "max"                           },
2374   { CA_ARG_UNDEFINED,           " "                             },
2375   { CA_ARG_NUMBER_RESET,        "reset"                         },
2376   { CA_ARG_UNDEFINED,           " "                             },
2377   { CA_ARG_NUMBER_CE_VALUE,     "CE value"                      },
2378   { CA_ARG_NUMBER_CE_SCORE,     "CE score"                      },
2379   { CA_ARG_NUMBER_CE_DELAY,     "CE delay"                      },
2380   { CA_ARG_UNDEFINED,           " "                             },
2381   { CA_ARG_NUMBER_LEVEL_TIME,   "time"                          },
2382   { CA_ARG_NUMBER_LEVEL_GEMS,   "gems"                          },
2383   { CA_ARG_NUMBER_LEVEL_SCORE,  "score"                         },
2384   { CA_ARG_UNDEFINED,           " "                             },
2385   { CA_ARG_ELEMENT_CV_HEADLINE, "[CE value]"                    },
2386   { CA_ARG_ELEMENT_CV_TARGET,   "target"                        },
2387   { CA_ARG_ELEMENT_CV_TRIGGER,  "trigger"                       },
2388   { CA_ARG_ELEMENT_CV_ACTION,   "action ->"                     },
2389   { CA_ARG_UNDEFINED,           " "                             },
2390   { CA_ARG_ELEMENT_CS_HEADLINE, "[CE score]"                    },
2391   { CA_ARG_ELEMENT_CS_TARGET,   "target"                        },
2392   { CA_ARG_ELEMENT_CS_TRIGGER,  "trigger"                       },
2393   { CA_ARG_ELEMENT_CS_ACTION,   "action ->"                     },
2394
2395   { -1,                         NULL                            }
2396 };
2397
2398 static struct ValueTextInfo options_action_arg_value[] =
2399 {
2400   { CA_ARG_NUMBER_HEADLINE,     "[number]"                      },
2401   { CA_ARG_0,                   "0"                             },
2402   { CA_ARG_1,                   "1"                             },
2403   { CA_ARG_2,                   "2"                             },
2404   { CA_ARG_3,                   "3"                             },
2405   { CA_ARG_4,                   "4"                             },
2406   { CA_ARG_5,                   "5"                             },
2407   { CA_ARG_10,                  "10"                            },
2408   { CA_ARG_100,                 "100"                           },
2409   { CA_ARG_1000,                "1000"                          },
2410   { CA_ARG_UNDEFINED,           " "                             },
2411   { CA_ARG_NUMBER_MIN,          "min"                           },
2412   { CA_ARG_NUMBER_MAX,          "max"                           },
2413   { CA_ARG_UNDEFINED,           " "                             },
2414   { CA_ARG_NUMBER_RESET,        "reset"                         },
2415   { CA_ARG_UNDEFINED,           " "                             },
2416   { CA_ARG_NUMBER_CE_VALUE,     "CE value"                      },
2417   { CA_ARG_NUMBER_CE_SCORE,     "CE score"                      },
2418   { CA_ARG_NUMBER_CE_DELAY,     "CE delay"                      },
2419   { CA_ARG_UNDEFINED,           " "                             },
2420   { CA_ARG_NUMBER_LEVEL_TIME,   "time"                          },
2421   { CA_ARG_NUMBER_LEVEL_GEMS,   "gems"                          },
2422   { CA_ARG_NUMBER_LEVEL_SCORE,  "score"                         },
2423   { CA_ARG_UNDEFINED,           " "                             },
2424   { CA_ARG_ELEMENT_CV_HEADLINE, "[CE value]"                    },
2425   { CA_ARG_ELEMENT_CV_TARGET,   "target"                        },
2426   { CA_ARG_ELEMENT_CV_TRIGGER,  "trigger"                       },
2427   { CA_ARG_ELEMENT_CV_ACTION,   "action ->"                     },
2428   { CA_ARG_UNDEFINED,           " "                             },
2429   { CA_ARG_ELEMENT_CS_HEADLINE, "[CE score]"                    },
2430   { CA_ARG_ELEMENT_CS_TARGET,   "target"                        },
2431   { CA_ARG_ELEMENT_CS_TRIGGER,  "trigger"                       },
2432   { CA_ARG_ELEMENT_CS_ACTION,   "action ->"                     },
2433   { CA_ARG_UNDEFINED,           " "                             },
2434   { CA_ARG_ELEMENT_NR_HEADLINE, "[element]"                     },
2435   { CA_ARG_ELEMENT_NR_TARGET,   "target"                        },
2436   { CA_ARG_ELEMENT_NR_TRIGGER,  "trigger"                       },
2437   { CA_ARG_ELEMENT_NR_ACTION,   "action ->"                     },
2438
2439   { -1,                         NULL                            }
2440 };
2441
2442 static struct ValueTextInfo options_action_arg_envelope[] =
2443 {
2444   { CA_ARG_NUMBER_HEADLINE,     "[number]"                      },
2445   { CA_ARG_1,                   "1"                             },
2446   { CA_ARG_2,                   "2"                             },
2447   { CA_ARG_3,                   "3"                             },
2448   { CA_ARG_4,                   "4"                             },
2449   { CA_ARG_UNDEFINED,           " "                             },
2450   { CA_ARG_ELEMENT_HEADLINE,    "[element]"                     },
2451   { CA_ARG_ELEMENT_TARGET,      "target"                        },
2452   { CA_ARG_ELEMENT_TRIGGER,     "trigger"                       },
2453   { CA_ARG_ELEMENT_ACTION,      "action ->"                     },
2454
2455   { -1,                         NULL                            }
2456 };
2457
2458 static struct ValueTextInfo options_action_arg_key[] =
2459 {
2460   { CA_ARG_NUMBER_HEADLINE,     "[number]"                      },
2461   { CA_ARG_1,                   "1"                             },
2462   { CA_ARG_2,                   "2"                             },
2463   { CA_ARG_3,                   "3"                             },
2464   { CA_ARG_4,                   "4"                             },
2465   { CA_ARG_5,                   "5"                             },
2466   { CA_ARG_6,                   "6"                             },
2467   { CA_ARG_7,                   "7"                             },
2468   { CA_ARG_8,                   "8"                             },
2469   { CA_ARG_UNDEFINED,           " "                             },
2470   { CA_ARG_ELEMENT_HEADLINE,    "[element]"                     },
2471   { CA_ARG_ELEMENT_TARGET,      "target"                        },
2472   { CA_ARG_ELEMENT_TRIGGER,     "trigger"                       },
2473   { CA_ARG_ELEMENT_ACTION,      "action ->"                     },
2474
2475   { -1,                         NULL                            }
2476 };
2477
2478 static struct ValueTextInfo options_action_arg_speed[] =
2479 {
2480   { CA_ARG_SPEED_HEADLINE,      "[speed]"                       },
2481   { CA_ARG_SPEED_NOT_MOVING,    "frozen"                        },
2482   { CA_ARG_SPEED_VERY_SLOW,     "very slow"                     },
2483   { CA_ARG_SPEED_SLOW,          "slow"                          },
2484   { CA_ARG_SPEED_NORMAL,        "normal"                        },
2485   { CA_ARG_SPEED_FAST,          "fast"                          },
2486   { CA_ARG_SPEED_VERY_FAST,     "very fast"                     },
2487   { CA_ARG_SPEED_EVEN_FASTER,   "ultrafast"                     },
2488   { CA_ARG_UNDEFINED,           " "                             },
2489   { CA_ARG_SPEED_SLOWER,        "slower"                        },
2490   { CA_ARG_SPEED_FASTER,        "faster"                        },
2491   { CA_ARG_UNDEFINED,           " "                             },
2492   { CA_ARG_SPEED_RESET,         "reset"                         },
2493
2494   { -1,                         NULL                            }
2495 };
2496
2497 static struct ValueTextInfo options_action_arg_shield[] =
2498 {
2499   { CA_ARG_SHIELD_HEADLINE,     "[shield]"                      },
2500   { CA_ARG_SHIELD_OFF,          "off"                           },
2501   { CA_ARG_SHIELD_NORMAL,       "normal"                        },
2502   { CA_ARG_SHIELD_DEADLY,       "deadly"                        },
2503
2504   { -1,                         NULL                            }
2505 };
2506
2507 static struct ValueTextInfo options_action_arg_artwork[] =
2508 {
2509   { CA_ARG_ELEMENT_HEADLINE,    "[element]"                     },
2510   { CA_ARG_ELEMENT_TARGET,      "target"                        },
2511   { CA_ARG_ELEMENT_TRIGGER,     "trigger"                       },
2512   { CA_ARG_ELEMENT_ACTION,      "action ->"                     },
2513   { CA_ARG_UNDEFINED,           " "                             },
2514   { CA_ARG_ELEMENT_RESET,       "reset"                         },
2515
2516   { -1,                         NULL                            }
2517 };
2518
2519 static struct ValueTextInfo options_action_arg_gravity[] =
2520 {
2521   { CA_ARG_GRAVITY_HEADLINE,    "[gravity]"                     },
2522   { CA_ARG_GRAVITY_ON,          "on"                            },
2523   { CA_ARG_GRAVITY_OFF,         "off"                           },
2524   { CA_ARG_GRAVITY_TOGGLE,      "toggle"                        },
2525
2526   { -1,                         NULL                            }
2527 };
2528
2529 static struct ValueTextInfo options_action_arg_direction[] =
2530 {
2531   { CA_ARG_DIRECTION_HEADLINE,  "[dir.]"                        },
2532   { CA_ARG_DIRECTION_NONE,      "none"                          },
2533   { CA_ARG_DIRECTION_LEFT,      "left"                          },
2534   { CA_ARG_DIRECTION_RIGHT,     "right"                         },
2535   { CA_ARG_DIRECTION_UP,        "up"                            },
2536   { CA_ARG_DIRECTION_DOWN,      "down"                          },
2537   { CA_ARG_DIRECTION_TRIGGER,   "trigger"                       },
2538   { CA_ARG_DIRECTION_TRIGGER_BACK, "-trigger"                   },
2539
2540   { -1,                         NULL                            }
2541 };
2542
2543 static struct ValueTextInfo options_action_arg_scan_mode[] =
2544 {
2545   { CA_ARG_SCAN_MODE_HEADLINE,  "[mode]"                        },
2546   { CA_ARG_SCAN_MODE_NORMAL,    "normal"                        },
2547   { CA_ARG_SCAN_MODE_REVERSE,   "reverse"                       },
2548
2549   { -1,                         NULL                            }
2550 };
2551
2552 static struct ValueTextInfo options_action_arg_inventory[] =
2553 {
2554   { CA_ARG_INVENTORY_HEADLINE,  "[add]"                         },
2555   { CA_ARG_ELEMENT_TARGET,      "+ target"                      },
2556   { CA_ARG_ELEMENT_TRIGGER,     "+ trigger"                     },
2557   { CA_ARG_ELEMENT_ACTION,      "+ action"                      },
2558   { CA_ARG_UNDEFINED,           " "                             },
2559   { CA_ARG_INVENTORY_RM_HEADLINE,"[remove]"                     },
2560   { CA_ARG_INVENTORY_RM_TARGET, "- target"                      },
2561   { CA_ARG_INVENTORY_RM_TRIGGER,"- trigger"                     },
2562   { CA_ARG_INVENTORY_RM_ACTION, "- action"                      },
2563   { CA_ARG_INVENTORY_RM_FIRST,  "- first"                       },
2564   { CA_ARG_INVENTORY_RM_LAST,   "- last"                        },
2565   { CA_ARG_INVENTORY_RM_ALL,    "- all"                         },
2566   { CA_ARG_UNDEFINED,           " "                             },
2567   { CA_ARG_INVENTORY_RESET,     "reset"                         },
2568
2569   { -1,                         NULL                            }
2570 };
2571
2572 static char options_change_page_strings[MAX_CHANGE_PAGES][10];
2573 static struct ValueTextInfo options_change_page[MAX_CHANGE_PAGES + 1] =
2574 {
2575   { -1,                         NULL                            }
2576 };
2577
2578 static struct ValueTextInfo options_group_choice_mode[] =
2579 {
2580   { ANIM_RANDOM,                "random"                        },
2581   { ANIM_LOOP,                  "loop"                          },
2582   { ANIM_LINEAR,                "linear"                        },
2583   { ANIM_PINGPONG,              "pingpong"                      },
2584   { ANIM_PINGPONG2,             "pingpong 2"                    },
2585   { ANIM_LEVEL_NR,              "level number"                  },
2586
2587   { -1,                         NULL                            }
2588 };
2589
2590 static struct ValueTextInfo options_bd_scheduling_type[] =
2591 {
2592   { GD_SCHEDULING_MILLISECONDS,   "Milliseconds"                },
2593   { GD_SCHEDULING_BD1,            "BD1"                         },
2594   { GD_SCHEDULING_BD2,            "BD2"                         },
2595   { GD_SCHEDULING_PLCK,           "Construction Kit"            },
2596   { GD_SCHEDULING_CRDR,           "Crazy Dream 7"               },
2597   { GD_SCHEDULING_BD1_ATARI,      "Atari BD1"                   },
2598   { GD_SCHEDULING_BD2_PLCK_ATARI, "Atari BD2 / PLCK"            },
2599
2600   { -1,                           NULL                          }
2601 };
2602
2603 static struct ValueTextInfo *action_arg_modes[] =
2604 {
2605   options_action_mode_none,
2606   options_action_mode_assign,
2607   options_action_mode_add_remove,
2608   options_action_mode_calculate,
2609 };
2610
2611 static struct
2612 {
2613   int value;
2614   int mode;
2615   struct ValueTextInfo *options;
2616 }
2617 action_arg_options[] =
2618 {
2619   { CA_NO_ACTION,               0,      options_action_arg_none,        },
2620   { CA_EXIT_PLAYER,             0,      options_action_arg_player,      },
2621   { CA_KILL_PLAYER,             0,      options_action_arg_player,      },
2622   { CA_MOVE_PLAYER,             0,      options_action_arg_direction,   },
2623   { CA_MOVE_PLAYER_NEW,         0,      options_action_arg_direction,   },
2624   { CA_RESTART_LEVEL,           0,      options_action_arg_none,        },
2625   { CA_SHOW_ENVELOPE,           0,      options_action_arg_envelope,    },
2626   { CA_SET_LEVEL_TIME,          3,      options_action_arg_number,      },
2627   { CA_SET_LEVEL_GEMS,          3,      options_action_arg_number,      },
2628   { CA_SET_LEVEL_SCORE,         3,      options_action_arg_number,      },
2629   { CA_SET_LEVEL_WIND,          1,      options_action_arg_direction,   },
2630   { CA_SET_LEVEL_RANDOM_SEED,   1,      options_action_arg_number,      },
2631   { CA_SET_PLAYER_KEYS,         2,      options_action_arg_key,         },
2632   { CA_SET_PLAYER_SPEED,        1,      options_action_arg_speed,       },
2633   { CA_SET_PLAYER_SHIELD,       1,      options_action_arg_shield,      },
2634   { CA_SET_PLAYER_GRAVITY,      1,      options_action_arg_gravity,     },
2635   { CA_SET_PLAYER_ARTWORK,      1,      options_action_arg_artwork,     },
2636   { CA_SET_PLAYER_INVENTORY,    0,      options_action_arg_inventory,   },
2637   { CA_SET_CE_VALUE,            3,      options_action_arg_value,       },
2638   { CA_SET_CE_SCORE,            3,      options_action_arg_value,       },
2639   { CA_SET_CE_ARTWORK,          1,      options_action_arg_artwork,     },
2640   { CA_SET_ENGINE_SCAN_MODE,    1,      options_action_arg_scan_mode,   },
2641
2642   { -1,                         FALSE,  NULL                            }
2643 };
2644
2645 static struct
2646 {
2647   int gadget_type_id;
2648   int x, y;
2649   int gadget_id;
2650   int gadget_id_align;
2651   int size;     // char size of selectbox or '-1' (dynamically determined)
2652   struct ValueTextInfo *options;
2653   int *value;
2654   char *text_above, *text_left, *text_right, *infotext;
2655 } selectbox_info[ED_NUM_SELECTBOX] =
2656 {
2657   // ---------- level and editor settings -------------------------------------
2658
2659   {
2660     ED_SELECTBOX_ID_TIME_OR_STEPS,
2661     -1,                                 ED_LEVEL_SETTINGS_YPOS(8),
2662     GADGET_ID_TIME_OR_STEPS,            GADGET_ID_LEVEL_TIMELIMIT_UP,
2663     -1,
2664     options_time_or_steps,
2665     &level.use_step_counter,
2666     NULL, NULL, "(0 => no limit)",      "time or step limit"
2667   },
2668   {
2669     ED_SELECTBOX_ID_TIME_SCORE_BASE,
2670     -1,                                 ED_LEVEL_SETTINGS_YPOS(10),
2671     GADGET_ID_TIME_SCORE_BASE,          GADGET_ID_LEVEL_TIMESCORE_UP,
2672     -1,
2673     options_time_score_base,
2674     &level.time_score_base,
2675     NULL, NULL, NULL,                   "time score for 1 or 10 seconds/steps"
2676   },
2677   {
2678     ED_SELECTBOX_ID_GAME_ENGINE_TYPE,
2679     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(12),
2680     GADGET_ID_GAME_ENGINE_TYPE,         GADGET_ID_NONE,
2681     -1,
2682     options_game_engine_type,
2683     &level.game_engine_type,
2684     NULL, "game engine:", NULL,         "game engine"
2685   },
2686   {
2687     ED_SELECTBOX_ID_BD_SCHEDULING_TYPE,
2688     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(1),
2689     GADGET_ID_BD_SCHEDULING_TYPE,       GADGET_ID_NONE,
2690     -1,
2691     options_bd_scheduling_type,
2692     &level.bd_scheduling_type,
2693     NULL, "scheduling type:", NULL,     "level timing"
2694   },
2695   {
2696     ED_SELECTBOX_ID_LEVELSET_SAVE_MODE,
2697     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(10),
2698     GADGET_ID_LEVELSET_SAVE_MODE,       GADGET_ID_NONE,
2699     -1,
2700     options_levelset_save_mode,
2701     &levelset_save_mode,
2702     "Action:", NULL, NULL,              "action when saving level set"
2703   },
2704
2705   // ---------- element settings: configure (several elements) ----------------
2706
2707   {
2708     ED_SELECTBOX_ID_WIND_DIRECTION,
2709     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
2710     GADGET_ID_WIND_DIRECTION,           GADGET_ID_NONE,
2711     -1,
2712     options_wind_direction,
2713     &level.wind_direction_initial,
2714     NULL, "initial wind direction:", NULL,      "initial wind direction"
2715   },
2716   {
2717     ED_SELECTBOX_ID_PLAYER_SPEED,
2718     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(7),
2719     GADGET_ID_PLAYER_SPEED,             GADGET_ID_NONE,
2720     -1,
2721     options_player_speed,
2722     &level.initial_player_stepsize[0],
2723     NULL, "initial player speed:", NULL,        "initial player speed"
2724   },
2725   {
2726     ED_SELECTBOX_ID_MM_BALL_CHOICE_MODE,
2727     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
2728     GADGET_ID_MM_BALL_CHOICE_MODE,      GADGET_ID_NONE,
2729     -1,
2730     options_group_choice_mode,
2731     &level.mm_ball_choice_mode,
2732     NULL, "choice type:", NULL,         "type of content choice"
2733   },
2734
2735   // ---------- element settings: configure 1 (custom elements) ---------------
2736
2737   {
2738     ED_SELECTBOX_ID_CUSTOM_ACCESS_TYPE,
2739     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(2),
2740     GADGET_ID_CUSTOM_ACCESS_TYPE,       GADGET_ID_NONE,
2741     -1,
2742     options_access_type,
2743     &custom_element.access_type,
2744     NULL, NULL, NULL,                   "type of access to this field"
2745   },
2746   {
2747     ED_SELECTBOX_ID_CUSTOM_ACCESS_LAYER,
2748     -1,                                 ED_ELEMENT_SETTINGS_YPOS(2),
2749     GADGET_ID_CUSTOM_ACCESS_LAYER,      GADGET_ID_CUSTOM_ACCESS_TYPE,
2750     -1,
2751     options_access_layer,
2752     &custom_element.access_layer,
2753     NULL, NULL, NULL,                   "layer of access for this field"
2754   },
2755   {
2756     ED_SELECTBOX_ID_CUSTOM_ACCESS_PROTECTED,
2757     -1,                                 ED_ELEMENT_SETTINGS_YPOS(2),
2758     GADGET_ID_CUSTOM_ACCESS_PROTECTED,  GADGET_ID_CUSTOM_ACCESS_LAYER,
2759     -1,
2760     options_access_protected,
2761     &custom_element.access_protected,
2762     NULL, NULL, NULL,                   "protected access for this field"
2763   },
2764   {
2765     ED_SELECTBOX_ID_CUSTOM_ACCESS_DIRECTION,
2766     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(3),
2767     GADGET_ID_CUSTOM_ACCESS_DIRECTION,  GADGET_ID_NONE,
2768     -1,
2769     options_access_direction,
2770     &custom_element.access_direction,
2771     NULL, "from", NULL,                 "access direction for this field"
2772   },
2773   {
2774     ED_SELECTBOX_ID_CUSTOM_WALK_TO_ACTION,
2775     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(4),
2776     GADGET_ID_CUSTOM_WALK_TO_ACTION,    GADGET_ID_NONE,
2777     -1,
2778     options_walk_to_action,
2779     &custom_element.walk_to_action,
2780     NULL, NULL, NULL,                   "diggable/collectible/pushable"
2781   },
2782
2783   // ---------- element settings: configure 2 (custom elements) ---------------
2784
2785   {
2786     ED_SELECTBOX_ID_CUSTOM_MOVE_PATTERN,
2787     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(1),
2788     GADGET_ID_CUSTOM_MOVE_PATTERN,      GADGET_ID_NONE,
2789     -1,
2790     options_move_pattern,
2791     &custom_element.move_pattern,
2792     NULL, "can move", NULL,             "element move pattern"
2793   },
2794   {
2795     ED_SELECTBOX_ID_CUSTOM_MOVE_DIRECTION,
2796     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(2),
2797     GADGET_ID_CUSTOM_MOVE_DIRECTION,    GADGET_ID_NONE,
2798     -1,
2799     options_move_direction,
2800     &custom_element.move_direction_initial,
2801     NULL, "starts moving", NULL,        "initial element move direction"
2802   },
2803   {
2804     ED_SELECTBOX_ID_CUSTOM_MOVE_STEPSIZE,
2805     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(4),
2806     GADGET_ID_CUSTOM_MOVE_STEPSIZE,     GADGET_ID_NONE,
2807     -1,
2808     options_move_stepsize,
2809     &custom_element.move_stepsize,
2810     NULL, "move/fall speed", NULL,      "speed of element movement"
2811   },
2812   {
2813     ED_SELECTBOX_ID_CUSTOM_MOVE_LEAVE_TYPE,
2814     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(3),
2815     GADGET_ID_CUSTOM_MOVE_LEAVE_TYPE,   GADGET_ID_NONE,
2816     -1,
2817     options_move_leave_type,
2818     &custom_element.move_leave_type,
2819     // left text with leading spaces to place gadget next to "can dig" gadget
2820     // (needed because drawing area gadgets created after selectbox gadgets)
2821     // NULL, "can dig:    can", ":",    "leave behind or change element"
2822     NULL, "            can", ":",       "leave behind or change element"
2823   },
2824   {
2825     ED_SELECTBOX_ID_CUSTOM_SMASH_TARGETS,
2826     -1,                                 ED_ELEMENT_SETTINGS_YPOS(8),
2827     GADGET_ID_CUSTOM_SMASH_TARGETS,     GADGET_ID_CUSTOM_CAN_SMASH,
2828     -1,
2829     options_smash_targets,
2830     &custom_element.smash_targets,
2831     NULL, "can smash", NULL,            "elements that can be smashed"
2832   },
2833   {
2834     ED_SELECTBOX_ID_CUSTOM_SLIPPERY_TYPE,
2835     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(9),
2836     GADGET_ID_CUSTOM_SLIPPERY_TYPE,     GADGET_ID_NONE,
2837     -1,
2838     options_slippery_type,
2839     &custom_element.slippery_type,
2840     NULL, "slippery", NULL,             "where other elements fall down"
2841   },
2842   {
2843     ED_SELECTBOX_ID_CUSTOM_DEADLINESS,
2844     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(10),
2845     GADGET_ID_CUSTOM_DEADLINESS,        GADGET_ID_NONE,
2846     -1,
2847     options_deadliness,
2848     &custom_element.deadliness,
2849     NULL, "deadly when", NULL,          "deadliness of element"
2850   },
2851   {
2852     ED_SELECTBOX_ID_CUSTOM_EXPLOSION_TYPE,
2853     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(11),
2854     GADGET_ID_CUSTOM_EXPLOSION_TYPE,    GADGET_ID_NONE,
2855     -1,
2856     options_explosion_type,
2857     &custom_element.explosion_type,
2858     NULL, "can explode", NULL,          "explosion type"
2859   },
2860
2861   // ---------- element settings: advanced (custom elements) ------------------
2862
2863   {
2864     ED_SELECTBOX_ID_CHANGE_TIME_UNITS,
2865     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(3),
2866     GADGET_ID_CHANGE_TIME_UNITS,        GADGET_ID_NONE,
2867     -1,
2868     options_time_units,
2869     &custom_element_change.delay_frames,
2870     NULL, "delay time given in", NULL,  "delay time units for change"
2871   },
2872   {
2873     ED_SELECTBOX_ID_CHANGE_DIRECT_ACTION,
2874     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(4),
2875     GADGET_ID_CHANGE_DIRECT_ACTION,     GADGET_ID_NONE,
2876     -1,
2877     options_change_direct_action,
2878     &custom_element_change.direct_action,
2879     NULL, NULL, NULL,                   "type of direct action"
2880   },
2881   {
2882     ED_SELECTBOX_ID_CHANGE_OTHER_ACTION,
2883     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(5),
2884     GADGET_ID_CHANGE_OTHER_ACTION,      GADGET_ID_NONE,
2885     -1,
2886     options_change_other_action,
2887     &custom_element_change.other_action,
2888     NULL, NULL, "element:",             "type of other element action"
2889   },
2890   {
2891     ED_SELECTBOX_ID_CHANGE_SIDE,
2892     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(6),
2893     GADGET_ID_CHANGE_SIDE,              GADGET_ID_NONE,
2894     -1,
2895     options_change_trigger_side,
2896     &custom_element_change.trigger_side,
2897     NULL, "at", "side",                 "element side triggering change"
2898   },
2899   {
2900     ED_SELECTBOX_ID_CHANGE_PLAYER,
2901     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(7),
2902     GADGET_ID_CHANGE_PLAYER,            GADGET_ID_NONE,
2903     -1,
2904     options_change_trigger_player,
2905     &custom_element_change.trigger_player,
2906     NULL, "player:", " ",               "player that causes change"
2907   },
2908   {
2909     ED_SELECTBOX_ID_CHANGE_PAGE,
2910     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(7),
2911     GADGET_ID_CHANGE_PAGE,              GADGET_ID_CHANGE_PLAYER,
2912     -1,
2913     options_change_trigger_page,
2914     &custom_element_change.trigger_page,
2915     NULL, "page:", NULL,                "change page that causes change"
2916   },
2917   {
2918     ED_SELECTBOX_ID_CHANGE_REPLACE_WHEN,
2919     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(10),
2920     GADGET_ID_CHANGE_REPLACE_WHEN,      GADGET_ID_NONE,
2921     -1,
2922     options_change_replace_when,
2923     &custom_element_change.replace_when,
2924     NULL, "replace when", NULL,         "which elements can be replaced"
2925   },
2926   {
2927     ED_SELECTBOX_ID_ACTION_TYPE,
2928     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(13),
2929     GADGET_ID_ACTION_TYPE,              GADGET_ID_NONE,
2930     15,
2931     options_action_type,
2932     &custom_element_change.action_type,
2933     NULL, NULL, NULL,                   "action on specified condition"
2934   },
2935   {
2936     ED_SELECTBOX_ID_ACTION_MODE,
2937     -1,                                 ED_ELEMENT_SETTINGS_YPOS(13),
2938     GADGET_ID_ACTION_MODE,              GADGET_ID_ACTION_TYPE,
2939     -1,
2940     options_action_mode_none,
2941     &custom_element_change.action_mode,
2942     NULL, NULL, NULL,                   "action operator"
2943   },
2944   {
2945     ED_SELECTBOX_ID_ACTION_ARG,
2946     -1,                                 ED_ELEMENT_SETTINGS_YPOS(13),
2947     GADGET_ID_ACTION_ARG,               GADGET_ID_ACTION_MODE,
2948     -1,
2949     options_action_arg_none,
2950     &custom_element_change.action_arg,
2951     NULL, NULL, NULL,                   "action parameter"
2952   },
2953   {
2954     ED_SELECTBOX_ID_SELECT_CHANGE_PAGE,
2955     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(14),
2956     GADGET_ID_SELECT_CHANGE_PAGE,       GADGET_ID_NONE,
2957     3,
2958     options_change_page,
2959     &custom_element.current_change_page,
2960     NULL, NULL, NULL,                   "element change page"
2961   },
2962
2963   // ---------- element settings: configure (group elements) ------------------
2964
2965   {
2966     ED_SELECTBOX_ID_GROUP_CHOICE_MODE,
2967     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
2968     GADGET_ID_GROUP_CHOICE_MODE,        GADGET_ID_NONE,
2969     -1,
2970     options_group_choice_mode,
2971     &group_element_info.choice_mode,
2972     NULL, "choice type:", NULL,         "type of group element choice"
2973   },
2974 };
2975
2976 static struct
2977 {
2978   int gadget_type_id;
2979   int x, y;
2980   int gadget_id;
2981   int gadget_id_align;
2982   int size;
2983   char *text;
2984   char *text_above, *text_left, *text_right, *infotext;
2985 } textbutton_info[ED_NUM_TEXTBUTTONS] =
2986 {
2987   // ---------- level and editor settings (tabs) ------------------------------
2988
2989   {
2990     ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL,
2991     ED_LEVEL_TABS_XPOS(0),              ED_LEVEL_TABS_YPOS(0),
2992     GADGET_ID_LEVELCONFIG_LEVEL,        GADGET_ID_NONE,
2993     8,                                  "Level",
2994     NULL, NULL, NULL,                   "Configure level settings"
2995   },
2996   {
2997     ED_TEXTBUTTON_ID_LEVELCONFIG_LEVELSET,
2998     -1,                                 -1,
2999     GADGET_ID_LEVELCONFIG_LEVELSET,     GADGET_ID_LEVELCONFIG_LEVEL,
3000     8,                                  "Levelset",
3001     NULL, NULL, NULL,                   "Update this or create new level set"
3002   },
3003   {
3004     ED_TEXTBUTTON_ID_LEVELCONFIG_EDITOR,
3005     -1,                                 -1,
3006     GADGET_ID_LEVELCONFIG_EDITOR,       GADGET_ID_LEVELCONFIG_LEVELSET,
3007     8,                                  "Editor",
3008     NULL, NULL, NULL,                   "Configure editor settings"
3009   },
3010   {
3011     ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE,
3012     -1,                                 -1,
3013     GADGET_ID_LEVELCONFIG_ENGINE,       GADGET_ID_LEVELCONFIG_EDITOR,
3014     8,                                  "Engine",
3015     NULL, NULL, NULL,                   "Configure engine settings"
3016   },
3017
3018   // ---------- element settings (tabs) ---------------------------------------
3019
3020   {
3021     ED_TEXTBUTTON_ID_PROPERTIES_INFO,
3022     ED_ELEMENT_TABS_XPOS(0),            ED_ELEMENT_TABS_YPOS(0),
3023     GADGET_ID_PROPERTIES_INFO,          GADGET_ID_NONE,
3024     8,                                  "Info",
3025     NULL, NULL, NULL,                   "Show information about element"
3026   },
3027   {
3028     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG,
3029     -1,                                 -1,
3030     GADGET_ID_PROPERTIES_CONFIG,        GADGET_ID_PROPERTIES_INFO,
3031     8,                                  "Config",
3032     NULL, NULL, NULL,                   "Configure element properties"
3033   },
3034   {
3035     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_1,
3036     -1,                                 -1,
3037     GADGET_ID_PROPERTIES_CONFIG_1,      GADGET_ID_PROPERTIES_INFO,
3038     8,                                  "Config 1",
3039     NULL, NULL, NULL,                   "Configure element properties, part 1"
3040   },
3041   {
3042     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2,
3043     -1,                                 -1,
3044     GADGET_ID_PROPERTIES_CONFIG_2,      GADGET_ID_PROPERTIES_CONFIG_1,
3045     8,                                  "Config 2",
3046     NULL, NULL, NULL,                   "Configure element properties, part 2"
3047   },
3048   {
3049     ED_TEXTBUTTON_ID_PROPERTIES_CHANGE,
3050     -1,                                 -1,
3051     GADGET_ID_PROPERTIES_CHANGE,        GADGET_ID_PROPERTIES_CONFIG_2,
3052     8,                                  "Change",
3053     NULL, NULL, NULL,                   "Configure custom element change pages"
3054   },
3055
3056   // ---------- level and editor settings (buttons) ---------------------------
3057
3058   {
3059     ED_TEXTBUTTON_ID_SAVE_LEVELSET,
3060     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(10),
3061     GADGET_ID_SAVE_LEVELSET,            GADGET_ID_LEVELSET_SAVE_MODE,
3062     -1,                                 "Save",
3063     NULL, NULL, NULL,                   "Update or create level set"
3064   },
3065   {
3066     ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2,
3067     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(6),
3068     GADGET_ID_SAVE_AS_TEMPLATE_2,       GADGET_ID_NONE,
3069     -1,                                 "Save",
3070     NULL, NULL,                         "this level as level template",
3071     "Save current settings as new template"
3072   },
3073
3074   // ---------- element settings (buttons) ------------------------------------
3075
3076   {
3077     ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1,
3078     -1,                                 -1,
3079     GADGET_ID_SAVE_AS_TEMPLATE_1,       GADGET_ID_CUSTOM_USE_TEMPLATE_1,
3080     -1,                                 "Save",
3081     NULL, " ",                          "As Template",
3082     "Save current settings as new template"
3083   },
3084   {
3085     ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE,
3086     -1,                                 -1,
3087     GADGET_ID_ADD_CHANGE_PAGE,          GADGET_ID_PASTE_CHANGE_PAGE,
3088     -1,                                 "New",
3089     NULL, NULL, NULL,                   "Add new change page"
3090   },
3091   {
3092     ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE,
3093     -1,                                 -1,
3094     GADGET_ID_DEL_CHANGE_PAGE,          GADGET_ID_ADD_CHANGE_PAGE,
3095     -1,                                 "Delete",
3096     NULL, NULL, NULL,                   "Delete current change page"
3097   },
3098 };
3099
3100 static struct
3101 {
3102   int gadget_type_id;
3103   int graphic;
3104   int x, y;
3105   int gadget_id;
3106   int gadget_id_align;
3107   char *text_left, *text_right, *infotext;
3108 } graphicbutton_info[ED_NUM_GRAPHICBUTTONS] =
3109 {
3110   {
3111     ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE,
3112     IMG_EDITOR_COUNTER_DOWN,
3113     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(14),
3114     GADGET_ID_PREV_CHANGE_PAGE,         GADGET_ID_NONE,
3115     NULL, NULL,                         "select previous change page"
3116   },
3117   {
3118     ED_GRAPHICBUTTON_ID_NEXT_CHANGE_PAGE,
3119     IMG_EDITOR_COUNTER_UP,
3120     -1,                                 ED_ELEMENT_SETTINGS_YPOS(14),
3121     GADGET_ID_NEXT_CHANGE_PAGE,         GADGET_ID_SELECT_CHANGE_PAGE,
3122     NULL, "change page",                "select next change page"
3123   },
3124   {
3125     ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE,
3126     IMG_GFX_EDITOR_BUTTON_CP_COPY,
3127     -1,                                 ED_ELEMENT_SETTINGS_YPOS(14),
3128     GADGET_ID_COPY_CHANGE_PAGE,         GADGET_ID_NEXT_CHANGE_PAGE,
3129     " ", NULL,                          "copy settings from this change page"
3130   },
3131   {
3132     ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE,
3133     IMG_GFX_EDITOR_BUTTON_CP_PASTE,
3134     -1,                                 ED_ELEMENT_SETTINGS_YPOS(14),
3135     GADGET_ID_PASTE_CHANGE_PAGE,        GADGET_ID_COPY_CHANGE_PAGE,
3136     NULL, NULL,                         "paste settings to this change page"
3137   },
3138 };
3139
3140 static struct
3141 {
3142   int x, y;
3143 } scrollbutton_pos[ED_NUM_SCROLLBUTTONS];
3144
3145 static struct
3146 {
3147   int gadget_type_id;
3148   int graphic;
3149   int gadget_id;
3150   char *infotext;
3151 } scrollbutton_info[ED_NUM_SCROLLBUTTONS] =
3152 {
3153   {
3154     ED_SCROLLBUTTON_ID_AREA_UP,
3155     IMG_EDITOR_PLAYFIELD_SCROLL_UP,
3156     GADGET_ID_SCROLL_UP,
3157     "scroll level editing area up"
3158   },
3159   {
3160     ED_SCROLLBUTTON_ID_AREA_DOWN,
3161     IMG_EDITOR_PLAYFIELD_SCROLL_DOWN,
3162     GADGET_ID_SCROLL_DOWN,
3163     "scroll level editing area down"
3164   },
3165   {
3166     ED_SCROLLBUTTON_ID_AREA_LEFT,
3167     IMG_EDITOR_PLAYFIELD_SCROLL_LEFT,
3168     GADGET_ID_SCROLL_LEFT,
3169     "scroll level editing area left"
3170   },
3171   {
3172     ED_SCROLLBUTTON_ID_AREA_RIGHT,
3173     IMG_EDITOR_PLAYFIELD_SCROLL_RIGHT,
3174     GADGET_ID_SCROLL_RIGHT,
3175     "scroll level editing area right"
3176   },
3177   {
3178     ED_SCROLLBUTTON_ID_LIST_UP,
3179     IMG_EDITOR_PALETTE_SCROLL_UP,
3180     GADGET_ID_SCROLL_LIST_UP,
3181     "scroll element list up ('Page Up')"
3182   },
3183   {
3184     ED_SCROLLBUTTON_ID_LIST_DOWN,
3185     IMG_EDITOR_PALETTE_SCROLL_DOWN,
3186     GADGET_ID_SCROLL_LIST_DOWN,
3187     "scroll element list down ('Page Down')"
3188   },
3189 };
3190
3191 static struct
3192 {
3193   int x, y;
3194   int width, height;
3195   int wheel_x, wheel_y;
3196   int wheel_width, wheel_height;
3197 } scrollbar_pos[ED_NUM_SCROLLBARS];
3198
3199 static struct
3200 {
3201   int gadget_type_id;
3202   int graphic;
3203   int type;
3204   int gadget_id;
3205   char *infotext;
3206 } scrollbar_info[ED_NUM_SCROLLBARS] =
3207 {
3208   {
3209     ED_SCROLLBAR_ID_AREA_HORIZONTAL,
3210     IMG_EDITOR_PLAYFIELD_SCROLLBAR,
3211     GD_TYPE_SCROLLBAR_HORIZONTAL,
3212     GADGET_ID_SCROLL_HORIZONTAL,
3213     "scroll level editing area horizontally"
3214   },
3215   {
3216     ED_SCROLLBAR_ID_AREA_VERTICAL,
3217     IMG_EDITOR_PLAYFIELD_SCROLLBAR,
3218     GD_TYPE_SCROLLBAR_VERTICAL,
3219     GADGET_ID_SCROLL_VERTICAL,
3220     "scroll level editing area vertically"
3221   },
3222   {
3223     ED_SCROLLBAR_ID_LIST_VERTICAL,
3224     IMG_EDITOR_PALETTE_SCROLLBAR,
3225     GD_TYPE_SCROLLBAR_VERTICAL,
3226     GADGET_ID_SCROLL_LIST_VERTICAL,
3227     "scroll element list vertically"
3228   }
3229 };
3230
3231
3232 static struct
3233 {
3234   int gadget_type_id;
3235   int x, y;
3236   int gadget_id;
3237   int gadget_id_align;
3238   int radio_button_nr;
3239   int *value;
3240   int checked_value;
3241   char *text_left, *text_right, *infotext;
3242 } radiobutton_info[ED_NUM_RADIOBUTTONS] =
3243 {
3244   {
3245     ED_RADIOBUTTON_ID_PERCENTAGE,
3246     -1,                                 ED_LEVEL_SETTINGS_YPOS(0),
3247     GADGET_ID_RANDOM_PERCENTAGE,        GADGET_ID_LEVEL_RANDOM_UP,
3248     RADIO_NR_RANDOM_ELEMENTS,
3249     &random_placement_method,           RANDOM_USE_PERCENTAGE,
3250     " ", "percentage",                  "use percentage for random elements"
3251   },
3252   {
3253     ED_RADIOBUTTON_ID_QUANTITY,
3254     -1,                                 ED_LEVEL_SETTINGS_YPOS(0),
3255     GADGET_ID_RANDOM_QUANTITY,          GADGET_ID_RANDOM_PERCENTAGE,
3256     RADIO_NR_RANDOM_ELEMENTS,
3257     &random_placement_method,           RANDOM_USE_QUANTITY,
3258     " ", "quantity",                    "use quantity for random elements"
3259   }
3260 };
3261
3262 static struct
3263 {
3264   int gadget_type_id;
3265   int x, y;
3266   int gadget_id;
3267   int gadget_id_align;
3268   boolean *value;
3269   char *text_above, *text_left, *text_right, *infotext;
3270 } checkbutton_info[ED_NUM_CHECKBUTTONS] =
3271 {
3272   // ---------- level and editor settings -------------------------------------
3273
3274   {
3275     ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS,
3276     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(6),
3277     GADGET_ID_AUTO_COUNT_GEMS,          GADGET_ID_NONE,
3278     &level.auto_count_gems,
3279     NULL, NULL,
3280     "automatically count gems needed",  "set counter to number of gems"
3281   },
3282   {
3283     ED_CHECKBUTTON_ID_RATE_TIME_OVER_SCORE,
3284     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(11),
3285     GADGET_ID_RATE_TIME_OVER_SCORE,     GADGET_ID_NONE,
3286     &level.rate_time_over_score,
3287     NULL, NULL,
3288     "rate time/steps used over score",  "sort high scores by playing time/steps"
3289   },
3290   {
3291     ED_CHECKBUTTON_ID_USE_LEVELSET_ARTWORK,
3292     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(7),
3293     GADGET_ID_USE_LEVELSET_ARTWORK,     GADGET_ID_NONE,
3294     &levelset_use_levelset_artwork,
3295     NULL, NULL,
3296     "use current custom artwork",       "use custom artwork of this level set"
3297   },
3298   {
3299     ED_CHECKBUTTON_ID_COPY_LEVEL_TEMPLATE,
3300     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(8),
3301     GADGET_ID_COPY_LEVEL_TEMPLATE,      GADGET_ID_NONE,
3302     &levelset_copy_level_template,
3303     NULL, NULL,
3304     "copy current level template",      "copy level template of this level set"
3305   },
3306   {
3307     ED_CHECKBUTTON_ID_RANDOM_RESTRICTED,
3308     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(1),
3309     GADGET_ID_RANDOM_RESTRICTED,        GADGET_ID_NONE,
3310     &random_placement_background_restricted,
3311     NULL, NULL,
3312     "restrict random placement to:",    "set random placement restriction"
3313   },
3314   {
3315     ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_3,
3316     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(4),
3317     GADGET_ID_CUSTOM_USE_TEMPLATE_3,    GADGET_ID_NONE,
3318     &setup.editor.use_template_for_new_levels,
3319     "Template for new levels and CE/GE:", NULL,
3320     "use template for new levels",      "use template for level properties"
3321   },
3322   {
3323     ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2,
3324     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(5),
3325     GADGET_ID_CUSTOM_USE_TEMPLATE_2,    GADGET_ID_NONE,
3326     &level.use_custom_template,
3327     NULL, NULL,
3328     "use template for custom elements", "use template for custom properties"
3329   },
3330   {
3331     ED_CHECKBUTTON_ID_BD_INTERMISSION,
3332     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(0),
3333     GADGET_ID_BD_INTERMISSION,          GADGET_ID_NONE,
3334     &level.bd_intermission,
3335     "Boulder Dash game engine settings:", NULL,
3336     "intermission",                     "level is an intermission level"
3337   },
3338   {
3339     ED_CHECKBUTTON_ID_BD_PAL_TIMING,
3340     ED_LEVEL_SETTINGS_XPOS(0),          ED_LEVEL_SETTINGS_YPOS(2),
3341     GADGET_ID_BD_PAL_TIMING,            GADGET_ID_NONE,
3342     &level.bd_pal_timing,
3343     NULL, NULL,
3344     "PAL timing",                       "use slower timer (like PAL C64)"
3345   },
3346
3347   // ---------- element settings: configure (various elements) ----------------
3348
3349   {
3350     ED_CHECKBUTTON_ID_STICK_ELEMENT,
3351     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3352     GADGET_ID_STICK_ELEMENT,            GADGET_ID_NONE,
3353     &stick_element_properties_window,
3354     NULL, NULL,
3355     "stick this screen to edit content","stick this screen to edit content"
3356   },
3357   {
3358     ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS,
3359     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3360     GADGET_ID_EM_SLIPPERY_GEMS,         GADGET_ID_NONE,
3361     &level.em_slippery_gems,
3362     NULL, NULL,
3363     "slip down from certain flat walls","use EM/DC style slipping behaviour"
3364   },
3365   {
3366     ED_CHECKBUTTON_ID_EM_EXPLODES_BY_FIRE,
3367     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3368     GADGET_ID_EM_EXPLODES_BY_FIRE,      GADGET_ID_NONE,
3369     &level.em_explodes_by_fire,
3370     NULL, NULL,
3371     "explodes with chain reaction",     "use R'n'D style explosion behaviour"
3372   },
3373   {
3374     ED_CHECKBUTTON_ID_USE_SPRING_BUG,
3375     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3376     GADGET_ID_USE_SPRING_BUG,           GADGET_ID_NONE,
3377     &level.use_spring_bug,
3378     NULL, NULL,
3379     "use spring pushing bug",           "use odd spring pushing behaviour"
3380   },
3381   {
3382     ED_CHECKBUTTON_ID_USE_TIME_ORB_BUG,
3383     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3384     GADGET_ID_USE_TIME_ORB_BUG,         GADGET_ID_NONE,
3385     &level.use_time_orb_bug,
3386     NULL, NULL,
3387     "use time orb bug",                 "use odd time orb behaviour"
3388   },
3389   {
3390     ED_CHECKBUTTON_ID_USE_LIFE_BUGS,
3391     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(5),
3392     GADGET_ID_USE_LIFE_BUGS,            GADGET_ID_NONE,
3393     &level.use_life_bugs,
3394     NULL, NULL,
3395     "use buggy element behaviour",      "use odd (historic) element behaviour"
3396   },
3397   {
3398     ED_CHECKBUTTON_ID_RANDOM_BALL_CONTENT,
3399     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3400     GADGET_ID_RANDOM_BALL_CONTENT,      GADGET_ID_NONE,
3401     &level.ball_random,
3402     NULL, NULL,
3403     "create single random element",     "only create one element from content"
3404   },
3405   {
3406     ED_CHECKBUTTON_ID_INITIAL_BALL_ACTIVE,
3407     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3408     GADGET_ID_INITIAL_BALL_ACTIVE,      GADGET_ID_NONE,
3409     &level.ball_active_initial,
3410     NULL, NULL,
3411     "magic ball initially activated",   "activate magic ball after level start"
3412   },
3413   {
3414     ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE,
3415     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3416     GADGET_ID_GROW_INTO_DIGGABLE,       GADGET_ID_NONE,
3417     &level.grow_into_diggable,
3418     NULL, NULL,
3419     "can grow into anything diggable",  "grow into more than just sand"
3420   },
3421   {
3422     ED_CHECKBUTTON_ID_SB_FIELDS_NEEDED,
3423     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3424     GADGET_ID_SB_FIELDS_NEEDED,         GADGET_ID_NONE,
3425     &level.sb_fields_needed,
3426     NULL, NULL,
3427     "all fields need to be filled",     "require all SB fields to be solved"
3428   },
3429   {
3430     ED_CHECKBUTTON_ID_SB_OBJECTS_NEEDED,
3431     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3432     GADGET_ID_SB_OBJECTS_NEEDED,        GADGET_ID_NONE,
3433     &level.sb_objects_needed,
3434     NULL, NULL,
3435     "all objects need to be placed",    "require all SB objects to be solved"
3436   },
3437   {
3438     ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN,
3439     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3440     GADGET_ID_AUTO_EXIT_SOKOBAN,        GADGET_ID_NONE,
3441     &level.auto_exit_sokoban,
3442     NULL, NULL,
3443     "exit level if all tasks solved",   "automatically finish Sokoban levels"
3444   },
3445   {
3446     ED_CHECKBUTTON_ID_SOLVED_BY_ONE_PLAYER,
3447     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(14),
3448     GADGET_ID_SOLVED_BY_ONE_PLAYER,     GADGET_ID_NONE,
3449     &level.solved_by_one_player,
3450     NULL, NULL,
3451     "only one player must enter exit",  "level solved by first player in exit"
3452   },
3453   {
3454     ED_CHECKBUTTON_ID_FINISH_DIG_COLLECT,
3455     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
3456     GADGET_ID_FINISH_DIG_COLLECT,       GADGET_ID_NONE,
3457     &level.finish_dig_collect,
3458     NULL, NULL,
3459     "CE action on finished dig/collect", "only finished dig/collect triggers CE"
3460   },
3461   {
3462     ED_CHECKBUTTON_ID_KEEP_WALKABLE_CE,
3463     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
3464     GADGET_ID_KEEP_WALKABLE_CE,         GADGET_ID_NONE,
3465     &level.keep_walkable_ce,
3466     NULL, NULL,
3467     "keep walkable CE changed to player", "keep CE changing to player if walkable"
3468   },
3469   {
3470     ED_CHECKBUTTON_ID_CONTINUOUS_SNAPPING,
3471     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(9),
3472     GADGET_ID_CONTINUOUS_SNAPPING,      GADGET_ID_NONE,
3473     &level.continuous_snapping,
3474     NULL, NULL,
3475     "continuous snapping",              "use snapping without releasing key"
3476   },
3477   {
3478     ED_CHECKBUTTON_ID_BLOCK_SNAP_FIELD,
3479     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(8),
3480     GADGET_ID_BLOCK_SNAP_FIELD,         GADGET_ID_NONE,
3481     &level.block_snap_field,
3482     NULL, NULL,
3483     "block snapped field when snapping", "use snapping delay to show animation"
3484   },
3485   {
3486     ED_CHECKBUTTON_ID_BLOCK_LAST_FIELD,
3487     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3488     GADGET_ID_BLOCK_LAST_FIELD,         GADGET_ID_NONE,
3489     &level.block_last_field,
3490     NULL, NULL,
3491     "block last field when moving",     "player blocks last field when moving"
3492   },
3493   {
3494     ED_CHECKBUTTON_ID_SP_BLOCK_LAST_FIELD,
3495     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3496     GADGET_ID_SP_BLOCK_LAST_FIELD,      GADGET_ID_NONE,
3497     &level.sp_block_last_field,
3498     NULL, NULL,
3499     "block last field when moving",     "player blocks last field when moving"
3500   },
3501   {
3502     ED_CHECKBUTTON_ID_INSTANT_RELOCATION,
3503     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
3504     GADGET_ID_INSTANT_RELOCATION,       GADGET_ID_NONE,
3505     &level.instant_relocation,
3506     NULL, NULL,
3507     "no scrolling when relocating",     "player gets relocated without delay"
3508   },
3509   {
3510     ED_CHECKBUTTON_ID_SHIFTED_RELOCATION,
3511     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
3512     GADGET_ID_SHIFTED_RELOCATION,       GADGET_ID_NONE,
3513     &level.shifted_relocation,
3514     NULL, NULL,
3515     "no centering when relocating",     "level not centered after relocation"
3516   },
3517   {
3518     ED_CHECKBUTTON_ID_LAZY_RELOCATION,
3519     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(5),
3520     GADGET_ID_LAZY_RELOCATION,          GADGET_ID_NONE,
3521     &level.lazy_relocation,
3522     NULL, NULL,
3523     "only redraw off-screen relocation","no redraw if relocation target visible"
3524   },
3525   {
3526     ED_CHECKBUTTON_ID_USE_START_ELEMENT,
3527     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(10),
3528     GADGET_ID_USE_START_ELEMENT,        GADGET_ID_NONE,
3529     &level.use_start_element[0],
3530     NULL, NULL,
3531     "use level start element:",        "start level at this element's position"
3532   },
3533   {
3534     ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT,
3535     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(11),
3536     GADGET_ID_USE_ARTWORK_ELEMENT,      GADGET_ID_NONE,
3537     &level.use_artwork_element[0],
3538     NULL, NULL,
3539     "use artwork from element:",        "use player artwork from other element"
3540   },
3541   {
3542     ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT,
3543     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(12),
3544     GADGET_ID_USE_EXPLOSION_ELEMENT,    GADGET_ID_NONE,
3545     &level.use_explosion_element[0],
3546     NULL, NULL,
3547     "use explosion from element:",      "use explosion properties from element"
3548   },
3549   {
3550     ED_CHECKBUTTON_ID_INITIAL_GRAVITY,
3551     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(13),
3552     GADGET_ID_INITIAL_GRAVITY,          GADGET_ID_NONE,
3553     &level.initial_player_gravity[0],
3554     NULL, NULL,
3555     "use initial gravity",              "set initial player gravity"
3556   },
3557   {
3558     ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY,
3559     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3560     GADGET_ID_USE_INITIAL_INVENTORY,    GADGET_ID_NONE,
3561     &level.use_initial_inventory[0],
3562     NULL, NULL,
3563     "use initial inventory:",           "use collected elements on level start"
3564   },
3565   {
3566     ED_CHECKBUTTON_ID_CAN_PASS_TO_WALKABLE,
3567     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(6),
3568     GADGET_ID_CAN_PASS_TO_WALKABLE,     GADGET_ID_NONE,
3569     &level.can_pass_to_walkable,
3570     NULL, NULL,
3571     "can pass to walkable element",     "player can pass to empty or walkable"
3572   },
3573   {
3574     ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID,
3575     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3576     GADGET_ID_CAN_FALL_INTO_ACID,       GADGET_ID_NONE,
3577     &custom_element_properties[EP_CAN_MOVE_INTO_ACID],
3578     NULL, NULL,
3579     "can fall into acid (with gravity)","player can fall into acid pool"
3580   },
3581   {
3582     ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID,
3583     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3584     GADGET_ID_CAN_MOVE_INTO_ACID,       GADGET_ID_NONE,
3585     &custom_element_properties[EP_CAN_MOVE_INTO_ACID],
3586     NULL, NULL,
3587     "can move into acid",               "element can move into acid pool"
3588   },
3589   {
3590     ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH,
3591     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3592     GADGET_ID_DONT_COLLIDE_WITH,        GADGET_ID_NONE,
3593     &custom_element_properties[EP_DONT_COLLIDE_WITH],
3594     NULL, NULL,
3595     "deadly when colliding with",       "element is deadly when hitting player"
3596   },
3597   {
3598     ED_CHECKBUTTON_ID_BD_DIAGONAL_MOVEMENTS,
3599     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(0),
3600     GADGET_ID_BD_DIAGONAL_MOVEMENTS,    GADGET_ID_NONE,
3601     &level.bd_diagonal_movements,
3602     NULL, NULL,
3603     "can move diagonally",              "player can move diagonally"
3604   },
3605   {
3606     ED_CHECKBUTTON_ID_ENVELOPE_AUTOWRAP,
3607     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3608     GADGET_ID_ENVELOPE_AUTOWRAP,        GADGET_ID_NONE,
3609     &level.envelope[0].autowrap,
3610     NULL, NULL,
3611     "auto-wrap",                        "automatically wrap envelope text"
3612   },
3613   {
3614     ED_CHECKBUTTON_ID_ENVELOPE_CENTERED,
3615     -1,                                 ED_ELEMENT_SETTINGS_YPOS(1),
3616     GADGET_ID_ENVELOPE_CENTERED,        GADGET_ID_ENVELOPE_AUTOWRAP,
3617     &level.envelope[0].centered,
3618     NULL, " ",
3619     "centered",                         "automatically center envelope text"
3620   },
3621   {
3622     ED_CHECKBUTTON_ID_MM_LASER_RED,
3623     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3624     GADGET_ID_MM_LASER_RED,             GADGET_ID_NONE,
3625     &level.mm_laser_red,
3626     "choose color components for laser:", NULL,
3627     "red",                              "use red color components in laser"
3628   },
3629   {
3630     ED_CHECKBUTTON_ID_MM_LASER_GREEN,
3631     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3632     GADGET_ID_MM_LASER_GREEN,           GADGET_ID_NONE,
3633     &level.mm_laser_green,
3634     NULL, NULL,
3635     "green",                            "use green color components in laser"
3636   },
3637   {
3638     ED_CHECKBUTTON_ID_MM_LASER_BLUE,
3639     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
3640     GADGET_ID_MM_LASER_BLUE,            GADGET_ID_NONE,
3641     &level.mm_laser_blue,
3642     NULL, NULL,
3643     "blue",                             "use blue color components in laser"
3644   },
3645   {
3646     ED_CHECKBUTTON_ID_DF_LASER_RED,
3647     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3648     GADGET_ID_DF_LASER_RED,             GADGET_ID_NONE,
3649     &level.df_laser_red,
3650     "choose color components for laser:", NULL,
3651     "red",                              "use red color components in laser"
3652   },
3653   {
3654     ED_CHECKBUTTON_ID_DF_LASER_GREEN,
3655     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3656     GADGET_ID_DF_LASER_GREEN,           GADGET_ID_NONE,
3657     &level.df_laser_green,
3658     NULL, NULL,
3659     "green",                            "use green color components in laser"
3660   },
3661   {
3662     ED_CHECKBUTTON_ID_DF_LASER_BLUE,
3663     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(3),
3664     GADGET_ID_DF_LASER_BLUE,            GADGET_ID_NONE,
3665     &level.df_laser_blue,
3666     NULL, NULL,
3667     "blue",                             "use blue color components in laser"
3668   },
3669   {
3670     ED_CHECKBUTTON_ID_ROTATE_MM_BALL_CONTENT,
3671     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(5),
3672     GADGET_ID_ROTATE_MM_BALL_CONTENT,   GADGET_ID_NONE,
3673     &level.rotate_mm_ball_content,
3674     NULL, NULL,
3675     "randomly rotate created content",  "randomly rotate newly created content"
3676   },
3677   {
3678     ED_CHECKBUTTON_ID_EXPLODE_MM_BALL,
3679     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(6),
3680     GADGET_ID_EXPLODE_MM_BALL,          GADGET_ID_NONE,
3681     &level.explode_mm_ball,
3682     NULL, NULL,
3683     "explode ball instead of melting",  "use explosion to release ball content"
3684   },
3685
3686   // ---------- element settings: configure 1 (custom elements) ---------------
3687
3688   {
3689     ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC,
3690     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3691     GADGET_ID_CUSTOM_USE_GRAPHIC,       GADGET_ID_NONE,
3692     &custom_element.use_gfx_element,
3693     NULL, NULL,
3694     "use graphic of element:",          "use existing element graphic"
3695   },
3696   {
3697     ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1,
3698     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(14),
3699     GADGET_ID_CUSTOM_USE_TEMPLATE_1,    GADGET_ID_NONE,
3700     &level.use_custom_template,
3701     NULL, NULL,
3702     "use template",                     "use template for custom properties"
3703   },
3704   {
3705     ED_CHECKBUTTON_ID_CUSTOM_ACCESSIBLE,
3706     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(2),
3707     GADGET_ID_CUSTOM_ACCESSIBLE,        GADGET_ID_NONE,
3708     &custom_element_properties[EP_ACCESSIBLE],
3709     NULL, NULL,
3710     NULL,                               "player can walk to or pass this field"
3711   },
3712   {
3713     ED_CHECKBUTTON_ID_CUSTOM_GRAV_REACHABLE,
3714     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(9),
3715     GADGET_ID_CUSTOM_GRAV_REACHABLE,    GADGET_ID_NONE,
3716     &custom_element_properties[EP_GRAVITY_REACHABLE],
3717     NULL, NULL,
3718     "reachable despite gravity",        "player can walk/dig despite gravity"
3719   },
3720   {
3721     ED_CHECKBUTTON_ID_CUSTOM_USE_LAST_VALUE,
3722     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(11),
3723     GADGET_ID_CUSTOM_USE_LAST_VALUE,    GADGET_ID_NONE,
3724     &custom_element.use_last_ce_value,
3725     NULL, NULL,
3726     "use last CE value after change",   "use last CE value after change"
3727   },
3728   {
3729     ED_CHECKBUTTON_ID_CUSTOM_WALK_TO_OBJECT,
3730     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(4),
3731     GADGET_ID_CUSTOM_WALK_TO_OBJECT,    GADGET_ID_NONE,
3732     &custom_element_properties[EP_WALK_TO_OBJECT],
3733     NULL, NULL,
3734     NULL,                               "player can dig/collect/push element"
3735   },
3736   {
3737     ED_CHECKBUTTON_ID_CUSTOM_INDESTRUCTIBLE,
3738     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(8),
3739     GADGET_ID_CUSTOM_INDESTRUCTIBLE,    GADGET_ID_NONE,
3740     &custom_element_properties[EP_INDESTRUCTIBLE],
3741     NULL, NULL,
3742     "indestructible",                   "element is indestructible"
3743   },
3744
3745   // ---------- element settings: configure 2 (custom elements) ---------------
3746
3747   {
3748     ED_CHECKBUTTON_ID_CUSTOM_CAN_MOVE,
3749     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3750     GADGET_ID_CUSTOM_CAN_MOVE,          GADGET_ID_NONE,
3751     &custom_element_properties[EP_CAN_MOVE],
3752     NULL, NULL,
3753     NULL,                               "element can move with some pattern"
3754   },
3755   {
3756     ED_CHECKBUTTON_ID_CUSTOM_CAN_FALL,
3757     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(8),
3758     GADGET_ID_CUSTOM_CAN_FALL,          GADGET_ID_NONE,
3759     &custom_element_properties[EP_CAN_FALL],
3760     NULL, NULL,
3761     "can fall",                         "element can fall down"
3762   },
3763   {
3764     ED_CHECKBUTTON_ID_CUSTOM_CAN_SMASH,
3765     -1,                                 ED_ELEMENT_SETTINGS_YPOS(8),
3766     GADGET_ID_CUSTOM_CAN_SMASH,         GADGET_ID_CUSTOM_CAN_FALL,
3767     &custom_element_properties[EP_CAN_SMASH],
3768     NULL, " ",
3769     NULL,                               "element can smash other elements"
3770   },
3771   {
3772     ED_CHECKBUTTON_ID_CUSTOM_SLIPPERY,
3773     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(9),
3774     GADGET_ID_CUSTOM_SLIPPERY,          GADGET_ID_NONE,
3775     &custom_element_properties[EP_SLIPPERY],
3776     NULL, NULL,
3777     NULL,                               "other elements can fall down from it"
3778   },
3779   {
3780     ED_CHECKBUTTON_ID_CUSTOM_DEADLY,
3781     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(10),
3782     GADGET_ID_CUSTOM_DEADLY,            GADGET_ID_NONE,
3783     &custom_element_properties[EP_DEADLY],
3784     NULL, NULL,
3785     NULL,                               "element can kill the player"
3786   },
3787   {
3788     ED_CHECKBUTTON_ID_CUSTOM_CAN_EXPLODE,
3789     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(11),
3790     GADGET_ID_CUSTOM_CAN_EXPLODE,       GADGET_ID_NONE,
3791     &custom_element_properties[EP_CAN_EXPLODE],
3792     NULL, NULL,
3793     NULL,                               "element can explode"
3794   },
3795   {
3796     ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_FIRE,
3797     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(12),
3798     GADGET_ID_CUSTOM_EXPLODE_FIRE,      GADGET_ID_NONE,
3799     &custom_element_properties[EP_EXPLODES_BY_FIRE],
3800     NULL, NULL,
3801     "by fire",                          "element can explode by fire/explosion"
3802   },
3803   {
3804     ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_SMASH,
3805     -1,                                 ED_ELEMENT_SETTINGS_YPOS(12),
3806     GADGET_ID_CUSTOM_EXPLODE_SMASH,     GADGET_ID_CUSTOM_EXPLODE_FIRE,
3807     &custom_element_properties[EP_EXPLODES_SMASHED],
3808     NULL, " ",
3809     "smashed",                          "element can explode when smashed"
3810   },
3811   {
3812     ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_IMPACT,
3813     -1,                                 ED_ELEMENT_SETTINGS_YPOS(12),
3814     GADGET_ID_CUSTOM_EXPLODE_IMPACT,    GADGET_ID_CUSTOM_EXPLODE_SMASH,
3815     &custom_element_properties[EP_EXPLODES_IMPACT],
3816     NULL, " ",
3817     "impact",                           "element can explode on impact"
3818   },
3819
3820   // ---------- element settings: advanced (custom elements) ------------------
3821
3822   {
3823     ED_CHECKBUTTON_ID_CUSTOM_CAN_CHANGE,
3824     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(1),
3825     GADGET_ID_CUSTOM_CAN_CHANGE,        GADGET_ID_NONE,
3826     &custom_element_change.can_change,
3827     NULL, NULL,
3828     "element changes to:",              "change element on specified condition"
3829   },
3830   {
3831     ED_CHECKBUTTON_ID_CHANGE_DELAY,
3832     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(2),
3833     GADGET_ID_CHANGE_DELAY,             GADGET_ID_NONE,
3834     &custom_element_change_events[CE_DELAY],
3835     NULL, NULL,
3836     NULL,                               "element changes after delay"
3837   },
3838   {
3839     ED_CHECKBUTTON_ID_CHANGE_BY_DIRECT_ACT,
3840     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(4),
3841     GADGET_ID_CHANGE_BY_DIRECT_ACT,     GADGET_ID_NONE,
3842     &custom_element_change_events[CE_BY_DIRECT_ACTION],
3843     NULL, NULL,
3844     NULL,                               "element changes by direct action"
3845   },
3846   {
3847     ED_CHECKBUTTON_ID_CHANGE_BY_OTHER_ACT,
3848     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(5),
3849     GADGET_ID_CHANGE_BY_OTHER_ACT,      GADGET_ID_NONE,
3850     &custom_element_change_events[CE_BY_OTHER_ACTION],
3851     NULL, NULL,
3852     NULL,                               "element changes by other element"
3853   },
3854   {
3855     ED_CHECKBUTTON_ID_CHANGE_USE_EXPLOSION,
3856     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(8),
3857     GADGET_ID_CHANGE_USE_EXPLOSION,     GADGET_ID_NONE,
3858     &custom_element_change.explode,
3859     NULL, NULL,
3860     "explode instead of change",        "element explodes instead of change"
3861   },
3862   {
3863     ED_CHECKBUTTON_ID_CHANGE_USE_CONTENT,
3864     ED_ELEMENT_SETTINGS_XPOS(1),        ED_ELEMENT_SETTINGS_YPOS(9),
3865     GADGET_ID_CHANGE_USE_CONTENT,       GADGET_ID_NONE,
3866     &custom_element_change.use_target_content,
3867     NULL, NULL,
3868     "use extended change target:",      "element changes to more elements"
3869   },
3870   {
3871     ED_CHECKBUTTON_ID_CHANGE_ONLY_COMPLETE,
3872     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(11),
3873     GADGET_ID_CHANGE_ONLY_COMPLETE,     GADGET_ID_NONE,
3874     &custom_element_change.only_if_complete,
3875     NULL, NULL,
3876     "replace all or nothing",           "only replace when all can be changed"
3877   },
3878   {
3879     ED_CHECKBUTTON_ID_CHANGE_USE_RANDOM,
3880     ED_ELEMENT_SETTINGS_XPOS(2),        ED_ELEMENT_SETTINGS_YPOS(12),
3881     GADGET_ID_CHANGE_USE_RANDOM,        GADGET_ID_NONE,
3882     &custom_element_change.use_random_replace,
3883     NULL, NULL,
3884     NULL,                               "use percentage for random replace"
3885   },
3886   {
3887     ED_CHECKBUTTON_ID_CHANGE_HAS_ACTION,
3888     ED_ELEMENT_SETTINGS_XPOS(0),        ED_ELEMENT_SETTINGS_YPOS(13),
3889     GADGET_ID_CHANGE_HAS_ACTION,        GADGET_ID_NONE,
3890     &custom_element_change.has_action,
3891     NULL, NULL,
3892     NULL,                               "execute action on specified condition"
3893   },
3894 };
3895
3896 static struct
3897 {
3898   int gadget_type_id;
3899   int x, y;
3900   int xoffset, yoffset;
3901   int gadget_id;
3902   int gadget_id_align;
3903   int *value;
3904   int area_xsize, area_ysize;
3905   char *text_left, *text_right, *text_above, *text_below, *infotext;
3906 } drawingarea_info[ED_NUM_DRAWING_AREAS] =
3907 {
3908   // ---------- level playfield content ---------------------------------------
3909
3910   {
3911     ED_DRAWING_ID_DRAWING_LEVEL,
3912     0,                                  0,
3913     0,                                  0,
3914     GADGET_ID_DRAWING_LEVEL,            GADGET_ID_NONE,
3915     NULL,
3916     -1, -1,     // these values are not constant, but can change at runtime
3917     NULL, NULL, NULL, NULL,             NULL
3918   },
3919
3920   // ---------- yam yam content -----------------------------------------------
3921
3922   {
3923     ED_DRAWING_ID_YAMYAM_CONTENT_0,
3924     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
3925     ED_AREA_YAMYAM_CONTENT_XOFF(0),     ED_AREA_YAMYAM_CONTENT_YOFF(0),
3926     GADGET_ID_YAMYAM_CONTENT_0,         GADGET_ID_NONE,
3927     &level.yamyam_content[0].e[0][0],   3, 3,
3928     NULL, NULL, NULL, "1",              NULL
3929   },
3930   {
3931     ED_DRAWING_ID_YAMYAM_CONTENT_1,
3932     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
3933     ED_AREA_YAMYAM_CONTENT_XOFF(1),     ED_AREA_YAMYAM_CONTENT_YOFF(1),
3934     GADGET_ID_YAMYAM_CONTENT_1,         GADGET_ID_NONE,
3935     &level.yamyam_content[1].e[0][0],   3, 3,
3936     NULL, NULL, NULL, "2",              NULL
3937   },
3938   {
3939     ED_DRAWING_ID_YAMYAM_CONTENT_2,
3940     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
3941     ED_AREA_YAMYAM_CONTENT_XOFF(2),     ED_AREA_YAMYAM_CONTENT_YOFF(2),
3942     GADGET_ID_YAMYAM_CONTENT_2,         GADGET_ID_NONE,
3943     &level.yamyam_content[2].e[0][0],   3, 3,
3944     NULL, NULL, NULL, "3",              NULL
3945   },
3946   {
3947     ED_DRAWING_ID_YAMYAM_CONTENT_3,
3948     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
3949     ED_AREA_YAMYAM_CONTENT_XOFF(3),     ED_AREA_YAMYAM_CONTENT_YOFF(3),
3950     GADGET_ID_YAMYAM_CONTENT_3,         GADGET_ID_NONE,
3951     &level.yamyam_content[3].e[0][0],   3, 3,
3952     NULL, NULL, NULL, "4",              NULL
3953   },
3954   {
3955     ED_DRAWING_ID_YAMYAM_CONTENT_4,
3956     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
3957     ED_AREA_YAMYAM_CONTENT_XOFF(4),     ED_AREA_YAMYAM_CONTENT_YOFF(4),
3958     GADGET_ID_YAMYAM_CONTENT_4,         GADGET_ID_NONE,
3959     &level.yamyam_content[4].e[0][0],   3, 3,
3960     NULL, NULL, NULL, "5",              NULL
3961   },
3962   {
3963     ED_DRAWING_ID_YAMYAM_CONTENT_5,
3964     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
3965     ED_AREA_YAMYAM_CONTENT_XOFF(5),     ED_AREA_YAMYAM_CONTENT_YOFF(5),
3966     GADGET_ID_YAMYAM_CONTENT_5,         GADGET_ID_NONE,
3967     &level.yamyam_content[5].e[0][0],   3, 3,
3968     NULL, NULL, NULL, "6",              NULL
3969   },
3970   {
3971     ED_DRAWING_ID_YAMYAM_CONTENT_6,
3972     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
3973     ED_AREA_YAMYAM_CONTENT_XOFF(6),     ED_AREA_YAMYAM_CONTENT_YOFF(6),
3974     GADGET_ID_YAMYAM_CONTENT_6,         GADGET_ID_NONE,
3975     &level.yamyam_content[6].e[0][0],   3, 3,
3976     NULL, NULL, NULL, "7",              NULL
3977   },
3978   {
3979     ED_DRAWING_ID_YAMYAM_CONTENT_7,
3980     ED_AREA_YAMYAM_CONTENT_XPOS,        ED_AREA_YAMYAM_CONTENT_YPOS,
3981     ED_AREA_YAMYAM_CONTENT_XOFF(7),     ED_AREA_YAMYAM_CONTENT_YOFF(7),
3982     GADGET_ID_YAMYAM_CONTENT_7,         GADGET_ID_NONE,
3983     &level.yamyam_content[7].e[0][0],   3, 3,
3984     NULL, NULL, NULL, "8",              NULL
3985   },
3986
3987   // ---------- magic ball content --------------------------------------------
3988
3989   {
3990     ED_DRAWING_ID_MAGIC_BALL_CONTENT_0,
3991     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
3992     ED_AREA_MAGIC_BALL_CONTENT_XOFF(0), ED_AREA_MAGIC_BALL_CONTENT_YOFF(0),
3993     GADGET_ID_MAGIC_BALL_CONTENT_0,     GADGET_ID_NONE,
3994     &level.ball_content[0].e[0][0],     3, 3,
3995     NULL, NULL, NULL, "1",              NULL
3996   },
3997   {
3998     ED_DRAWING_ID_MAGIC_BALL_CONTENT_1,
3999     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4000     ED_AREA_MAGIC_BALL_CONTENT_XOFF(1), ED_AREA_MAGIC_BALL_CONTENT_YOFF(1),
4001     GADGET_ID_MAGIC_BALL_CONTENT_1,     GADGET_ID_NONE,
4002     &level.ball_content[1].e[0][0],     3, 3,
4003     NULL, NULL, NULL, "2",              NULL
4004   },
4005   {
4006     ED_DRAWING_ID_MAGIC_BALL_CONTENT_2,
4007     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4008     ED_AREA_MAGIC_BALL_CONTENT_XOFF(2), ED_AREA_MAGIC_BALL_CONTENT_YOFF(2),
4009     GADGET_ID_MAGIC_BALL_CONTENT_2,     GADGET_ID_NONE,
4010     &level.ball_content[2].e[0][0],     3, 3,
4011     NULL, NULL, NULL, "3",              NULL
4012   },
4013   {
4014     ED_DRAWING_ID_MAGIC_BALL_CONTENT_3,
4015     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4016     ED_AREA_MAGIC_BALL_CONTENT_XOFF(3), ED_AREA_MAGIC_BALL_CONTENT_YOFF(3),
4017     GADGET_ID_MAGIC_BALL_CONTENT_3,     GADGET_ID_NONE,
4018     &level.ball_content[3].e[0][0],     3, 3,
4019     NULL, NULL, NULL, "4",              NULL
4020   },
4021   {
4022     ED_DRAWING_ID_MAGIC_BALL_CONTENT_4,
4023     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4024     ED_AREA_MAGIC_BALL_CONTENT_XOFF(4), ED_AREA_MAGIC_BALL_CONTENT_YOFF(4),
4025     GADGET_ID_MAGIC_BALL_CONTENT_4,     GADGET_ID_NONE,
4026     &level.ball_content[4].e[0][0],     3, 3,
4027     NULL, NULL, NULL, "5",              NULL
4028   },
4029   {
4030     ED_DRAWING_ID_MAGIC_BALL_CONTENT_5,
4031     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4032     ED_AREA_MAGIC_BALL_CONTENT_XOFF(5), ED_AREA_MAGIC_BALL_CONTENT_YOFF(5),
4033     GADGET_ID_MAGIC_BALL_CONTENT_5,     GADGET_ID_NONE,
4034     &level.ball_content[5].e[0][0],     3, 3,
4035     NULL, NULL, NULL, "6",              NULL
4036   },
4037   {
4038     ED_DRAWING_ID_MAGIC_BALL_CONTENT_6,
4039     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4040     ED_AREA_MAGIC_BALL_CONTENT_XOFF(6), ED_AREA_MAGIC_BALL_CONTENT_YOFF(6),
4041     GADGET_ID_MAGIC_BALL_CONTENT_6,     GADGET_ID_NONE,
4042     &level.ball_content[6].e[0][0],     3, 3,
4043     NULL, NULL, NULL, "7",              NULL
4044   },
4045   {
4046     ED_DRAWING_ID_MAGIC_BALL_CONTENT_7,
4047     ED_AREA_MAGIC_BALL_CONTENT_XPOS,    ED_AREA_MAGIC_BALL_CONTENT_YPOS,
4048     ED_AREA_MAGIC_BALL_CONTENT_XOFF(7), ED_AREA_MAGIC_BALL_CONTENT_YOFF(7),
4049     GADGET_ID_MAGIC_BALL_CONTENT_7,     GADGET_ID_NONE,
4050     &level.ball_content[7].e[0][0],     3, 3,
4051     NULL, NULL, NULL, "8",              NULL
4052   },
4053
4054   // ---------- android content -----------------------------------------------
4055
4056   {
4057     ED_DRAWING_ID_ANDROID_CONTENT,
4058     ED_AREA_1X1_SETTINGS_XPOS(0),       ED_AREA_1X1_SETTINGS_YPOS(6),
4059     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4060     GADGET_ID_ANDROID_CONTENT,          GADGET_ID_NONE,
4061     &level.android_clone_element[0],    MAX_ANDROID_ELEMENTS, 1,
4062     NULL, NULL, "elements:", NULL,      "elements android can clone"
4063   },
4064
4065   // ---------- amoeba content ------------------------------------------------
4066
4067   {
4068     ED_DRAWING_ID_AMOEBA_CONTENT,
4069     ED_AREA_1X1_SETTINGS_XPOS(0),       ED_AREA_1X1_SETTINGS_YPOS(3),
4070     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4071     GADGET_ID_AMOEBA_CONTENT,           GADGET_ID_NONE,
4072     &level.amoeba_content,              1, 1,
4073     "content:", NULL, NULL, NULL,       "amoeba content"
4074   },
4075
4076   // ---------- level start element -------------------------------------------
4077
4078   {
4079     ED_DRAWING_ID_START_ELEMENT,
4080     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(10),
4081     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4082     GADGET_ID_START_ELEMENT,            GADGET_ID_USE_START_ELEMENT,
4083     &level.start_element[0],            1, 1,
4084     NULL, NULL, NULL, NULL,             "level start element"
4085   },
4086
4087   // ---------- player artwork element ----------------------------------------
4088
4089   {
4090     ED_DRAWING_ID_ARTWORK_ELEMENT,
4091     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(11),
4092     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4093     GADGET_ID_ARTWORK_ELEMENT,          GADGET_ID_USE_ARTWORK_ELEMENT,
4094     &level.artwork_element[0],          1, 1,
4095     NULL, NULL, NULL, NULL,             "element for player artwork"
4096   },
4097
4098   // ---------- player explosion element --------------------------------------
4099
4100   {
4101     ED_DRAWING_ID_EXPLOSION_ELEMENT,
4102     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(12),
4103     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4104     GADGET_ID_EXPLOSION_ELEMENT,        GADGET_ID_USE_EXPLOSION_ELEMENT,
4105     &level.explosion_element[0],        1, 1,
4106     NULL, NULL, NULL, NULL,             "element for player explosion"
4107   },
4108
4109   // ---------- player initial inventory --------------------------------------
4110
4111   {
4112     ED_DRAWING_ID_INVENTORY_CONTENT,
4113     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(1),
4114     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4115     GADGET_ID_INVENTORY_CONTENT,        GADGET_ID_USE_INITIAL_INVENTORY,
4116     &level.initial_inventory_content[0][0], MAX_INITIAL_INVENTORY_SIZE, 1,
4117     NULL, NULL, NULL, NULL,             "content for initial inventory"
4118   },
4119
4120   // ---------- gray ball content -----------------------------------------
4121
4122   {
4123     ED_DRAWING_ID_MM_BALL_CONTENT,
4124     ED_AREA_1X1_SETTINGS_XPOS(0),       ED_AREA_1X1_SETTINGS_YPOS(2),
4125     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4126     GADGET_ID_MM_BALL_CONTENT,          GADGET_ID_NONE,
4127     &level.mm_ball_content[0],          MAX_MM_BALL_CONTENTS, 1,
4128     "content:", NULL, NULL, NULL,       "content for gray ball"
4129   },
4130
4131   // ---------- element settings: configure 1 (custom elements) ---------------
4132
4133   // ---------- custom graphic ------------------------------------------------
4134
4135   {
4136     ED_DRAWING_ID_CUSTOM_GRAPHIC,
4137     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(1),
4138     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4139     GADGET_ID_CUSTOM_GRAPHIC,           GADGET_ID_CUSTOM_USE_GRAPHIC,
4140     &custom_element.gfx_element_initial, 1, 1,
4141     NULL, NULL, NULL, NULL,             "custom graphic element"
4142   },
4143
4144   // ---------- element settings: configure 2 (custom elements) ---------------
4145
4146   // ---------- custom content (when exploding) -------------------------------
4147
4148   {
4149     ED_DRAWING_ID_CUSTOM_CONTENT,
4150     -1,                                 ED_AREA_3X3_SETTINGS_YPOS(11),
4151     0,                                  ED_AREA_3X3_SETTINGS_YOFF,
4152     GADGET_ID_CUSTOM_CONTENT,           GADGET_ID_NONE, // align three rows
4153     &custom_element.content.e[0][0],    3, 3,
4154     "content:", NULL, NULL, NULL,       NULL
4155   },
4156
4157   // ---------- custom enter and leave element (when moving) ------------------
4158
4159   {
4160     ED_DRAWING_ID_CUSTOM_MOVE_ENTER,
4161     ED_AREA_1X1_SETTINGS_XPOS(1),       ED_AREA_1X1_SETTINGS_YPOS(3),
4162     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4163     GADGET_ID_CUSTOM_MOVE_ENTER,        GADGET_ID_NONE,
4164     &custom_element.move_enter_element, 1, 1,
4165     "can dig:", " ", NULL, NULL,        "element that can be digged/collected"
4166   },
4167   {
4168     ED_DRAWING_ID_CUSTOM_MOVE_LEAVE,
4169     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(3),
4170     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4171     GADGET_ID_CUSTOM_MOVE_LEAVE,        GADGET_ID_CUSTOM_MOVE_LEAVE_TYPE,
4172     &custom_element.move_leave_element, 1, 1,
4173     NULL, NULL, NULL, NULL,             "element that will be left behind"
4174   },
4175
4176   // ---------- element settings: advanced (custom elements) ------------------
4177
4178   // ---------- custom change target ------------------------------------------
4179
4180   {
4181     ED_DRAWING_ID_CUSTOM_CHANGE_TARGET,
4182     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(1),
4183     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4184     GADGET_ID_CUSTOM_CHANGE_TARGET,     GADGET_ID_CUSTOM_CAN_CHANGE,
4185     &custom_element_change.target_element, 1, 1,
4186     NULL, "after/when:", NULL, NULL,    "new target element after change"
4187   },
4188
4189   // ---------- custom change content (extended change target) ----------------
4190
4191   {
4192     ED_DRAWING_ID_CUSTOM_CHANGE_CONTENT,
4193     -1,                                 ED_AREA_3X3_SETTINGS_YPOS(9),
4194     0,                                  ED_AREA_3X3_SETTINGS_YOFF,
4195     GADGET_ID_CUSTOM_CHANGE_CONTENT,    GADGET_ID_NONE, // align three rows
4196     &custom_element_change.target_content.e[0][0], 3, 3,
4197     NULL, NULL, NULL, NULL,             "new extended elements after change"
4198   },
4199
4200   // ---------- custom change trigger (element causing change) ----------------
4201
4202   {
4203     ED_DRAWING_ID_CUSTOM_CHANGE_TRIGGER,
4204     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(5),
4205     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4206     GADGET_ID_CUSTOM_CHANGE_TRIGGER,    GADGET_ID_CHANGE_OTHER_ACTION,
4207     &custom_element_change.initial_trigger_element, 1, 1,
4208     NULL, NULL, NULL, NULL,             "other element triggering change"
4209   },
4210
4211   // ---------- custom change action (element used for action) ----------------
4212
4213   {
4214     ED_DRAWING_ID_CUSTOM_CHANGE_ACTION,
4215     -1,                                 ED_AREA_1X1_SETTINGS_YPOS(13),
4216     0,                                  ED_AREA_1X1_SETTINGS_YOFF,
4217     GADGET_ID_CUSTOM_CHANGE_ACTION,     GADGET_ID_ACTION_ARG,
4218     &custom_element_change.action_element, 1, 1,
4219     NULL, NULL, NULL, NULL,             "element used as action parameter"
4220   },
4221
4222   // ---------- group element content -----------------------------------------
4223
4224   {
4225     ED_DRAWING_ID_GROUP_CONTENT,
4226     ED_AREA_1X1_SETTINGS_XPOS(0),       ED_AREA_1X1_SETTINGS_YPOS(2),
4227     ED_AREA_1X1_SETTINGS_XOFF,          ED_AREA_1X1_SETTINGS_YOFF,
4228     GADGET_ID_GROUP_CONTENT,            GADGET_ID_NONE,
4229     &group_element_info.element[0],     MAX_ELEMENTS_IN_GROUP, 1,
4230     "content:", NULL, NULL, NULL,       NULL
4231   },
4232
4233   // ---------- random background (for random painting) -----------------------
4234
4235   {
4236     ED_DRAWING_ID_RANDOM_BACKGROUND,
4237     -1,                                 ED_AREA_1X1_LSETTINGS_YPOS(1),
4238     0,                                  ED_AREA_1X1_LSETTINGS_YOFF,
4239     GADGET_ID_RANDOM_BACKGROUND,        GADGET_ID_RANDOM_RESTRICTED,
4240     &random_placement_background_element, 1, 1,
4241     NULL, NULL, NULL, NULL,             "random placement background"
4242   },
4243 };
4244
4245
4246 // ----------------------------------------------------------------------------
4247 // some internally used variables
4248 // ----------------------------------------------------------------------------
4249
4250 // maximal size of level editor drawing area
4251 static int MAX_ED_FIELDX, MAX_ED_FIELDY;
4252
4253 // actual size of level editor drawing area
4254 static int ed_fieldx, ed_fieldy;
4255
4256 // actual position of level editor drawing area in level playfield
4257 static int level_xpos = -1, level_ypos = -1;
4258
4259 // actual tile size used to display playfield drawing area
4260 static int ed_tilesize = DEFAULT_EDITOR_TILESIZE;
4261 static int ed_tilesize_default = DEFAULT_EDITOR_TILESIZE;
4262
4263 #define IN_ED_FIELD(x, y)       IN_FIELD(x, y, ed_fieldx, ed_fieldy)
4264
4265 // drawing elements on the three mouse buttons
4266 static int new_element1 = EL_WALL;
4267 static int new_element2 = EL_EMPTY;
4268 static int new_element3 = EL_SAND;
4269
4270 #define IS_VALID_BUTTON(button) (button >= 1 && button <= 3)
4271 #define BUTTON_ELEMENT(button) ((button) == 1 ? new_element1 : \
4272                                 (button) == 2 ? new_element2 : \
4273                                 (button) == 3 ? new_element3 : EL_EMPTY)
4274
4275 #define BUTTON_TILE_SIZE(x)     ((x) >= TILESIZE ? TILESIZE : MINI_TILESIZE)
4276
4277 static int use_permanent_palette = TRUE;
4278
4279 #define PX              (use_permanent_palette ? DX : SX)
4280 #define PY              (use_permanent_palette ? DY : SY)
4281 #define PXSIZE          (use_permanent_palette ? DXSIZE : SXSIZE)
4282 #define PYSIZE          (use_permanent_palette ? DYSIZE : SYSIZE)
4283
4284 // forward declaration for internal use
4285 static void CopyBrushToCursor(int, int);
4286 static void DeleteBrushFromCursor(void);
4287 static void ModifyEditorCounterValue(int, int);
4288 static void ModifyEditorCounterLimits(int, int, int);
4289 static void ModifyEditorSelectboxValue(int, int);
4290 static void ModifyEditorSelectboxOptions(int, struct ValueTextInfo *);
4291 static void ModifyEditorDrawingArea(int, int, int);
4292 static void ModifyEditorElementList(void);
4293 static void AdjustElementListScrollbar(void);
4294 static void RedrawDrawingElements(void);
4295 static void DrawDrawingWindowExt(boolean);
4296 static void DrawDrawingWindow(void);
4297 static void DrawLevelConfigWindow(void);
4298 static void DrawPropertiesWindow(void);
4299 static void DrawPaletteWindow(void);
4300 static void UpdateCustomElementGraphicGadgets(void);
4301 static boolean checkPropertiesConfig(int);
4302 static void SetAutomaticNumberOfGemsNeeded(void);
4303 static void ClearEditorGadgetInfoText(void);
4304 static void CopyLevelToUndoBuffer(int);
4305 static void HandleDrawingAreas(struct GadgetInfo *);
4306 static void HandleCounterButtons(struct GadgetInfo *);
4307 static void HandleTextInputGadgets(struct GadgetInfo *);
4308 static void HandleTextAreaGadgets(struct GadgetInfo *);
4309 static void HandleSelectboxGadgets(struct GadgetInfo *);
4310 static void HandleTextbuttonGadgets(struct GadgetInfo *);
4311 static void HandleGraphicbuttonGadgets(struct GadgetInfo *);
4312 static void HandleRadiobuttons(struct GadgetInfo *);
4313 static void HandleCheckbuttons(struct GadgetInfo *);
4314 static void HandleControlButtons(struct GadgetInfo *);
4315 static void HandleDrawingAreaInfo(struct GadgetInfo *);
4316 static void PrintEditorGadgetInfoText(struct GadgetInfo *);
4317 static boolean AskToCopyAndModifyLevelTemplate(void);
4318 static boolean getDrawModeHiRes(void);
4319 static int getTabulatorBarWidth(void);
4320 static int getTabulatorBarHeight(void);
4321 static Pixel getTabulatorBarColor(void);
4322 static int numHiresTiles(int);
4323
4324 static int num_editor_gadgets = 0;      // dynamically determined
4325
4326 static struct GadgetInfo **level_editor_gadget = NULL;
4327 static int *right_gadget_border = NULL;
4328
4329 static int drawing_function = GADGET_ID_SINGLE_ITEMS;
4330 static int last_drawing_function = GADGET_ID_SINGLE_ITEMS;
4331 static boolean draw_with_brush = FALSE;
4332 static int properties_element = 0;
4333
4334 static short TileBackup[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
4335 static short UndoBuffer[NUM_UNDO_STEPS][MAX_LEV_FIELDX][MAX_LEV_FIELDY];
4336 static short IntelliDrawBuffer[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
4337 static int undo_buffer_position = 0;
4338 static int undo_buffer_steps = 0;
4339 static int redo_buffer_steps = 0;
4340
4341 static int edit_mode;
4342 static int edit_mode_levelconfig;
4343 static int edit_mode_properties;
4344
4345 static int element_shift = 0;
4346
4347 static int editor_el_players[] =
4348 {
4349   EL_PLAYER_1,
4350   EL_PLAYER_2,
4351   EL_PLAYER_3,
4352   EL_PLAYER_4
4353 };
4354 static int *editor_el_players_ptr = editor_el_players;
4355 static int num_editor_el_players = ARRAY_SIZE(editor_el_players);
4356
4357 static int editor_hl_boulderdash[] =
4358 {
4359   EL_INTERNAL_CASCADE_BD_ACTIVE,
4360   EL_CHAR('B'),
4361   EL_CHAR('D'),
4362   EL_EMPTY,
4363 };
4364
4365 static int editor_el_boulderdash[] =
4366 {
4367   EL_EMPTY,
4368   EL_SAND,
4369   EL_BD_ROCK,
4370   EL_BD_DIAMOND,
4371
4372   EL_STEELWALL,
4373   EL_BD_WALL,
4374   EL_BD_EXPANDABLE_WALL,
4375   EL_BD_MAGIC_WALL,
4376
4377   EL_BD_AMOEBA,
4378   EL_BD_BUTTERFLY_UP,
4379   EL_BD_FIREFLY_UP,
4380   EL_EXIT_CLOSED,
4381
4382   EL_BD_BUTTERFLY_LEFT,
4383   EL_BD_FIREFLY_LEFT,
4384   EL_BD_BUTTERFLY_RIGHT,
4385   EL_BD_FIREFLY_RIGHT,
4386
4387   EL_EMPTY,
4388   EL_BD_BUTTERFLY_DOWN,
4389   EL_BD_FIREFLY_DOWN,
4390   EL_EXIT_OPEN,
4391 };
4392 static int *editor_hl_boulderdash_ptr = editor_hl_boulderdash;
4393 static int *editor_el_boulderdash_ptr = editor_el_boulderdash;
4394 static int num_editor_hl_boulderdash = ARRAY_SIZE(editor_hl_boulderdash);
4395 static int num_editor_el_boulderdash = ARRAY_SIZE(editor_el_boulderdash);
4396
4397 static int editor_hl_boulderdash_native[] =
4398 {
4399   EL_INTERNAL_CASCADE_BD_NATIVE_ACTIVE,
4400   EL_CHAR('B'),
4401   EL_CHAR('D'),
4402   EL_EMPTY,
4403 };
4404
4405 static int editor_el_boulderdash_native[] =
4406 {
4407   EL_EMPTY,
4408   EL_BD_SAND,
4409   EL_BD_ROCK,
4410   EL_BD_DIAMOND,
4411
4412   EL_BD_STEELWALL,
4413   EL_BD_WALL,
4414   EL_EMPTY,
4415   EL_BD_MAGIC_WALL,
4416
4417   EL_BD_AMOEBA,
4418   EL_BD_BUTTERFLY_UP,
4419   EL_BD_FIREFLY_UP,
4420   EL_EXIT_CLOSED,
4421
4422   EL_BD_BUTTERFLY_LEFT,
4423   EL_BD_FIREFLY_LEFT,
4424   EL_BD_BUTTERFLY_RIGHT,
4425   EL_BD_FIREFLY_RIGHT,
4426
4427   EL_BD_INBOX,
4428   EL_BD_BUTTERFLY_DOWN,
4429   EL_BD_FIREFLY_DOWN,
4430   EL_EXIT_OPEN,
4431
4432   EL_BD_AMOEBA_2,
4433   EL_BD_BUTTERFLY_2_UP,
4434   EL_BD_FIREFLY_2_UP,
4435   EL_EMPTY,
4436
4437   EL_BD_BUTTERFLY_2_LEFT,
4438   EL_BD_FIREFLY_2_LEFT,
4439   EL_BD_BUTTERFLY_2_RIGHT,
4440   EL_BD_FIREFLY_2_RIGHT,
4441
4442   EL_EMPTY,
4443   EL_BD_BUTTERFLY_2_DOWN,
4444   EL_BD_FIREFLY_2_DOWN,
4445   EL_EMPTY,
4446
4447   EL_EMPTY,
4448   EL_BD_DRAGONFLY_UP,
4449   EL_BD_STONEFLY_UP,
4450   EL_EMPTY,
4451
4452   EL_BD_DRAGONFLY_LEFT,
4453   EL_BD_STONEFLY_LEFT,
4454   EL_BD_DRAGONFLY_RIGHT,
4455   EL_BD_STONEFLY_RIGHT,
4456
4457   EL_EMPTY,
4458   EL_BD_DRAGONFLY_DOWN,
4459   EL_BD_STONEFLY_DOWN,
4460   EL_EMPTY,
4461
4462   EL_EMPTY,
4463   EL_BD_BITER_UP,
4464   EL_BD_COW_UP,
4465   EL_EMPTY,
4466
4467   EL_BD_BITER_LEFT,
4468   EL_BD_COW_LEFT,
4469   EL_BD_BITER_RIGHT,
4470   EL_BD_COW_RIGHT,
4471
4472   EL_EMPTY,
4473   EL_BD_BITER_DOWN,
4474   EL_BD_COW_DOWN,
4475   EL_EMPTY,
4476
4477   EL_EMPTY,
4478   EL_BD_SAND,
4479   EL_BD_SAND_BALL,
4480   EL_BD_SAND_LOOSE,
4481
4482   EL_BD_SAND_SLOPED_UP_LEFT,
4483   EL_BD_SAND_SLOPED_UP_RIGHT,
4484   EL_BD_WALL_SLOPED_UP_LEFT,
4485   EL_BD_WALL_SLOPED_UP_RIGHT,
4486
4487   EL_BD_SAND_SLOPED_DOWN_LEFT,
4488   EL_BD_SAND_SLOPED_DOWN_RIGHT,
4489   EL_BD_WALL_SLOPED_DOWN_LEFT,
4490   EL_BD_WALL_SLOPED_DOWN_RIGHT,
4491
4492   EL_BD_FLYING_DIAMOND,
4493   EL_BD_FLYING_ROCK,
4494   EL_BD_STEELWALL_SLOPED_UP_LEFT,
4495   EL_BD_STEELWALL_SLOPED_UP_RIGHT,
4496
4497   EL_BD_NUT,
4498   EL_BD_MEGA_ROCK,
4499   EL_BD_STEELWALL_SLOPED_DOWN_LEFT,
4500   EL_BD_STEELWALL_SLOPED_DOWN_RIGHT,
4501
4502   EL_BD_SAND_2,
4503   EL_BD_WALL_NON_SLOPED,
4504   EL_BD_WALL,
4505   EL_BD_MAGIC_WALL,
4506
4507   EL_BD_EXIT_CLOSED,
4508   EL_BD_EXIT_OPEN,
4509   EL_BD_INVISIBLE_EXIT_CLOSED,
4510   EL_BD_INVISIBLE_EXIT_OPEN,
4511
4512   EL_BD_STEELWALL,
4513   EL_BD_STEELWALL_EXPLODABLE,
4514   EL_BD_STEELWALL_DIGGABLE,
4515   EL_BD_WALL_DIGGABLE,
4516
4517   EL_BD_EXPANDABLE_WALL_HORIZONTAL,
4518   EL_BD_EXPANDABLE_WALL_VERTICAL,
4519   EL_BD_EXPANDABLE_WALL_ANY,
4520   EL_BD_CREATURE_SWITCH,
4521
4522   EL_BD_EXPANDABLE_STEELWALL_HORIZONTAL,
4523   EL_BD_EXPANDABLE_STEELWALL_VERTICAL,
4524   EL_BD_EXPANDABLE_STEELWALL_ANY,
4525   EL_BD_EXPANDABLE_WALL_SWITCH_HORIZONTAL,
4526
4527   EL_BD_BITER_SWITCH_1,
4528   EL_BD_REPLICATOR_SWITCH,
4529   EL_BD_CONVEYOR_SWITCH,
4530   EL_BD_CONVEYOR_DIR_SWITCH_RIGHT,
4531
4532   EL_BD_ACID,
4533   EL_BD_FALLING_WALL,
4534   EL_BD_BOX,
4535   EL_BD_TIME_PENALTY,
4536
4537   EL_BD_GRAVESTONE,
4538   EL_BD_ROCK_GLUED,
4539   EL_BD_DIAMOND_GLUED,
4540   EL_BD_DIAMOND_KEY,
4541
4542   EL_BD_TRAPPED_DIAMOND,
4543   EL_BD_CLOCK,
4544   EL_BD_SAND_GLUED,
4545   EL_BD_WATER,
4546
4547   EL_BD_KEY_1,
4548   EL_BD_KEY_2,
4549   EL_BD_KEY_3,
4550   EL_EMPTY,
4551
4552   EL_BD_WALL_KEY_1,
4553   EL_BD_WALL_KEY_2,
4554   EL_BD_WALL_KEY_3,
4555   EL_BD_WALL_DIAMOND,
4556
4557   EL_BD_GATE_1,
4558   EL_BD_GATE_2,
4559   EL_BD_GATE_3,
4560   EL_BD_POT,
4561
4562   EL_BD_GRAVITY_SWITCH,
4563   EL_BD_PNEUMATIC_HAMMER,
4564   EL_BD_TELEPORTER,
4565   EL_BD_SKELETON,
4566
4567   EL_BD_AMOEBA,
4568   EL_BD_AMOEBA_2,
4569   EL_BD_REPLICATOR,
4570   EL_BD_BLADDER_SPENDER,
4571
4572   EL_BD_CONVEYOR_LEFT,
4573   EL_BD_CONVEYOR_RIGHT,
4574   EL_BD_BOMB,
4575   EL_BD_NITRO_PACK,
4576
4577   EL_BD_LAVA,
4578   EL_BD_SWEET,
4579   EL_BD_VOODOO_DOLL,
4580   EL_BD_SLIME,
4581
4582   EL_BD_BLADDER,
4583   EL_BD_WAITING_ROCK,
4584   EL_BD_CHASING_ROCK,
4585   EL_BD_GHOST,
4586
4587   EL_BD_PLAYER,
4588   EL_BD_PLAYER_WITH_BOMB,
4589   EL_BD_PLAYER_GLUED,
4590   EL_BD_PLAYER_STIRRING,
4591 };
4592 static int *editor_hl_boulderdash_native_ptr = editor_hl_boulderdash_native;
4593 static int *editor_el_boulderdash_native_ptr = editor_el_boulderdash_native;
4594 static int num_editor_hl_boulderdash_native = ARRAY_SIZE(editor_hl_boulderdash_native);
4595 static int num_editor_el_boulderdash_native = ARRAY_SIZE(editor_el_boulderdash_native);
4596
4597 static int editor_hl_emerald_mine[] =
4598 {
4599   EL_INTERNAL_CASCADE_EM_ACTIVE,
4600   EL_CHAR('E'),
4601   EL_CHAR('M'),
4602   EL_EMPTY,
4603 };
4604
4605 static int editor_el_emerald_mine[] =
4606 {
4607   EL_SAND,
4608   EL_ROCK,
4609   EL_QUICKSAND_EMPTY,
4610   EL_QUICKSAND_FULL,
4611
4612   EL_STEELWALL,
4613   EL_WALL,
4614   EL_WALL_SLIPPERY,
4615   EL_MAGIC_WALL,
4616
4617   EL_EMERALD,
4618   EL_DIAMOND,
4619   EL_NUT,
4620   EL_BOMB,
4621
4622   EL_EM_DYNAMITE,
4623   EL_EM_DYNAMITE_ACTIVE,
4624   EL_EM_EXIT_CLOSED,
4625   EL_EM_EXIT_OPEN,
4626
4627   EL_YAMYAM_UP,
4628   EL_BUG_UP,
4629   EL_SPACESHIP_UP,
4630   EL_ROBOT,
4631
4632   EL_BUG_LEFT,
4633   EL_SPACESHIP_LEFT,
4634   EL_BUG_RIGHT,
4635   EL_SPACESHIP_RIGHT,
4636
4637   EL_ROBOT_WHEEL,
4638   EL_BUG_DOWN,
4639   EL_SPACESHIP_DOWN,
4640   EL_INVISIBLE_WALL,
4641
4642   EL_ACID_POOL_TOPLEFT,
4643   EL_ACID,
4644   EL_ACID_POOL_TOPRIGHT,
4645   EL_AMOEBA_DROP,
4646
4647   EL_ACID_POOL_BOTTOMLEFT,
4648   EL_ACID_POOL_BOTTOM,
4649   EL_ACID_POOL_BOTTOMRIGHT,
4650   EL_AMOEBA_WET,
4651
4652   EL_EM_KEY_1,
4653   EL_EM_KEY_2,
4654   EL_EM_KEY_3,
4655   EL_EM_KEY_4,
4656
4657   EL_EM_GATE_1,
4658   EL_EM_GATE_2,
4659   EL_EM_GATE_3,
4660   EL_EM_GATE_4,
4661
4662   EL_EM_GATE_1_GRAY,
4663   EL_EM_GATE_2_GRAY,
4664   EL_EM_GATE_3_GRAY,
4665   EL_EM_GATE_4_GRAY,
4666 };
4667 static int *editor_hl_emerald_mine_ptr = editor_hl_emerald_mine;
4668 static int *editor_el_emerald_mine_ptr = editor_el_emerald_mine;
4669 static int num_editor_hl_emerald_mine = ARRAY_SIZE(editor_hl_emerald_mine);
4670 static int num_editor_el_emerald_mine = ARRAY_SIZE(editor_el_emerald_mine);
4671
4672 static int editor_hl_emerald_mine_club[] =
4673 {
4674   EL_INTERNAL_CASCADE_EMC_ACTIVE,
4675   EL_CHAR('E'),
4676   EL_CHAR('M'),
4677   EL_CHAR('C'),
4678 };
4679
4680 static int editor_el_emerald_mine_club[] =
4681 {
4682   EL_EMC_KEY_5,
4683   EL_EMC_KEY_6,
4684   EL_EMC_KEY_7,
4685   EL_EMC_KEY_8,
4686
4687   EL_EMC_GATE_5,
4688   EL_EMC_GATE_6,
4689   EL_EMC_GATE_7,
4690   EL_EMC_GATE_8,
4691
4692   EL_EMC_GATE_5_GRAY,
4693   EL_EMC_GATE_6_GRAY,
4694   EL_EMC_GATE_7_GRAY,
4695   EL_EMC_GATE_8_GRAY,
4696
4697   EL_EMC_STEELWALL_1,
4698   EL_EMC_STEELWALL_2,
4699   EL_EMC_STEELWALL_3,
4700   EL_EMC_STEELWALL_4,
4701
4702   EL_EMC_WALL_13,
4703   EL_EMC_WALL_14,
4704   EL_EMC_WALL_15,
4705   EL_EMC_WALL_16,
4706
4707   EL_EMC_WALL_SLIPPERY_1,
4708   EL_EMC_WALL_SLIPPERY_2,
4709   EL_EMC_WALL_SLIPPERY_3,
4710   EL_EMC_WALL_SLIPPERY_4,
4711
4712   EL_EMC_WALL_1,
4713   EL_EMC_WALL_2,
4714   EL_EMC_WALL_3,
4715   EL_EMC_WALL_4,
4716
4717   EL_EMC_WALL_5,
4718   EL_EMC_WALL_6,
4719   EL_EMC_WALL_7,
4720   EL_EMC_WALL_8,
4721
4722   EL_EMC_WALL_9,
4723   EL_EMC_WALL_10,
4724   EL_EMC_WALL_11,
4725   EL_EMC_WALL_12,
4726
4727   EL_EMC_GRASS,
4728   EL_EMC_FAKE_GRASS,
4729   EL_EMC_PLANT,
4730   EL_EMC_DRIPPER,
4731
4732   EL_EMC_MAGIC_BALL,
4733   EL_EMC_MAGIC_BALL_SWITCH,
4734   EL_EMC_LENSES,
4735   EL_EMC_MAGNIFIER,
4736
4737   EL_SPRING_LEFT,
4738   EL_SPRING,
4739   EL_SPRING_RIGHT,
4740   EL_EMC_SPRING_BUMPER,
4741
4742   EL_BALLOON,
4743   EL_YAMYAM_UP,
4744   EL_BALLOON_SWITCH_UP,
4745   EL_BALLOON_SWITCH_ANY,
4746
4747   EL_YAMYAM_LEFT,
4748   EL_BALLOON_SWITCH_LEFT,
4749   EL_YAMYAM_RIGHT,
4750   EL_BALLOON_SWITCH_RIGHT,
4751
4752   EL_EMC_ANDROID,
4753   EL_YAMYAM_DOWN,
4754   EL_BALLOON_SWITCH_DOWN,
4755   EL_BALLOON_SWITCH_NONE,
4756 };
4757 static int *editor_hl_emerald_mine_club_ptr = editor_hl_emerald_mine_club;
4758 static int *editor_el_emerald_mine_club_ptr = editor_el_emerald_mine_club;
4759 static int num_editor_hl_emerald_mine_club = ARRAY_SIZE(editor_hl_emerald_mine_club);
4760 static int num_editor_el_emerald_mine_club = ARRAY_SIZE(editor_el_emerald_mine_club);
4761
4762 static int editor_hl_rnd[] =
4763 {
4764   EL_INTERNAL_CASCADE_RND_ACTIVE,
4765   EL_CHAR('R'),
4766   EL_CHAR('N'),
4767   EL_CHAR('D'),
4768 };
4769
4770 static int editor_el_rnd[] =
4771 {
4772   EL_DYNAMITE,                  // RND
4773   EL_DYNAMITE_ACTIVE,           // RND
4774   EL_EMPTY,
4775   EL_EMPTY,
4776
4777   EL_KEY_1,
4778   EL_KEY_2,
4779   EL_KEY_3,
4780   EL_KEY_4,
4781
4782   EL_GATE_1,
4783   EL_GATE_2,
4784   EL_GATE_3,
4785   EL_GATE_4,
4786
4787   EL_GATE_1_GRAY,
4788   EL_GATE_2_GRAY,
4789   EL_GATE_3_GRAY,
4790   EL_GATE_4_GRAY,
4791
4792   EL_ARROW_LEFT,
4793   EL_ARROW_RIGHT,
4794   EL_ARROW_UP,
4795   EL_ARROW_DOWN,
4796
4797   EL_AMOEBA_DEAD,
4798   EL_AMOEBA_DRY,
4799   EL_AMOEBA_FULL,
4800   EL_GAME_OF_LIFE,
4801
4802   EL_EMERALD_YELLOW,
4803   EL_EMERALD_RED,
4804   EL_EMERALD_PURPLE,
4805   EL_BIOMAZE,
4806
4807   EL_WALL_EMERALD_YELLOW,
4808   EL_WALL_EMERALD_RED,
4809   EL_WALL_EMERALD_PURPLE,
4810   EL_WALL_BD_DIAMOND,
4811
4812   EL_SPEED_PILL,
4813   EL_PACMAN_UP,
4814   EL_TIME_ORB_FULL,
4815   EL_TIME_ORB_EMPTY,
4816
4817   EL_PACMAN_LEFT,
4818   EL_DARK_YAMYAM,
4819   EL_PACMAN_RIGHT,
4820   EL_YAMYAM,                    // RND
4821
4822   EL_BLACK_ORB,
4823   EL_PACMAN_DOWN,
4824   EL_LAMP,
4825   EL_LAMP_ACTIVE,
4826
4827   EL_DYNABOMB_INCREASE_NUMBER,
4828   EL_DYNABOMB_INCREASE_SIZE,
4829   EL_DYNABOMB_INCREASE_POWER,
4830   EL_STONEBLOCK,
4831
4832   EL_MOLE,
4833   EL_PENGUIN,
4834   EL_PIG,
4835   EL_DRAGON,
4836
4837   EL_BUG,
4838   EL_MOLE_UP,
4839   EL_BD_BUTTERFLY,
4840   EL_BD_FIREFLY,
4841
4842   EL_MOLE_LEFT,
4843   EL_SATELLITE,
4844   EL_MOLE_RIGHT,
4845   EL_PACMAN,
4846
4847   EL_SPACESHIP,
4848   EL_MOLE_DOWN,
4849   EL_INVISIBLE_STEELWALL,
4850   EL_INVISIBLE_WALL,
4851
4852   EL_EXPANDABLE_WALL,
4853   EL_EXPANDABLE_WALL_HORIZONTAL,
4854   EL_EXPANDABLE_WALL_VERTICAL,
4855   EL_EXPANDABLE_WALL_ANY,
4856 };
4857 static int *editor_hl_rnd_ptr = editor_hl_rnd;
4858 static int *editor_el_rnd_ptr = editor_el_rnd;
4859 static int num_editor_hl_rnd = ARRAY_SIZE(editor_hl_rnd);
4860 static int num_editor_el_rnd = ARRAY_SIZE(editor_el_rnd);
4861
4862 static int editor_hl_sokoban[] =
4863 {
4864   EL_INTERNAL_CASCADE_SB_ACTIVE,
4865   EL_CHAR('S'),
4866   EL_CHAR('B'),
4867   EL_EMPTY,
4868 };
4869
4870 static int editor_el_sokoban[] =
4871 {
4872   EL_SOKOBAN_OBJECT,
4873   EL_SOKOBAN_FIELD_EMPTY,
4874   EL_SOKOBAN_FIELD_FULL,
4875   EL_SOKOBAN_FIELD_PLAYER,
4876 };
4877 static int *editor_hl_sokoban_ptr = editor_hl_sokoban;
4878 static int *editor_el_sokoban_ptr = editor_el_sokoban;
4879 static int num_editor_hl_sokoban = ARRAY_SIZE(editor_hl_sokoban);
4880 static int num_editor_el_sokoban = ARRAY_SIZE(editor_el_sokoban);
4881
4882 static int editor_hl_supaplex[] =
4883 {
4884   EL_INTERNAL_CASCADE_SP_ACTIVE,
4885   EL_CHAR('S'),
4886   EL_CHAR('P'),
4887   EL_EMPTY,
4888 };
4889
4890 static int editor_el_supaplex[] =
4891 {
4892   EL_SP_MURPHY,
4893   EL_EMPTY,
4894   EL_SP_BASE,
4895   EL_SP_BUGGY_BASE,
4896
4897   EL_SP_INFOTRON,
4898   EL_SP_ZONK,
4899   EL_SP_SNIKSNAK,
4900   EL_SP_ELECTRON,
4901
4902   EL_SP_DISK_RED,
4903   EL_SP_DISK_ORANGE,
4904   EL_SP_DISK_YELLOW,
4905   EL_SP_TERMINAL,
4906
4907   EL_SP_EXIT_CLOSED,
4908   EL_SP_PORT_HORIZONTAL,
4909   EL_SP_PORT_VERTICAL,
4910   EL_SP_PORT_ANY,
4911
4912   EL_SP_PORT_LEFT,
4913   EL_SP_PORT_RIGHT,
4914   EL_SP_PORT_UP,
4915   EL_SP_PORT_DOWN,
4916
4917   EL_SP_GRAVITY_PORT_LEFT,
4918   EL_SP_GRAVITY_PORT_RIGHT,
4919   EL_SP_GRAVITY_PORT_UP,
4920   EL_SP_GRAVITY_PORT_DOWN,
4921
4922   EL_SP_GRAVITY_ON_PORT_LEFT,
4923   EL_SP_GRAVITY_ON_PORT_RIGHT,
4924   EL_SP_GRAVITY_ON_PORT_UP,
4925   EL_SP_GRAVITY_ON_PORT_DOWN,
4926
4927   EL_SP_GRAVITY_OFF_PORT_LEFT,
4928   EL_SP_GRAVITY_OFF_PORT_RIGHT,
4929   EL_SP_GRAVITY_OFF_PORT_UP,
4930   EL_SP_GRAVITY_OFF_PORT_DOWN,
4931
4932   EL_SP_HARDWARE_GRAY,
4933   EL_SP_HARDWARE_GREEN,
4934   EL_SP_HARDWARE_BLUE,
4935   EL_SP_HARDWARE_RED,
4936
4937   EL_SP_HARDWARE_BASE_1,
4938   EL_SP_HARDWARE_BASE_2,
4939   EL_SP_HARDWARE_BASE_3,
4940   EL_SP_HARDWARE_BASE_4,
4941
4942   EL_SP_HARDWARE_BASE_5,
4943   EL_SP_HARDWARE_BASE_6,
4944   EL_SP_HARDWARE_YELLOW,
4945   EL_SP_CHIP_TOP,
4946
4947   EL_SP_CHIP_SINGLE,
4948   EL_SP_CHIP_LEFT,
4949   EL_SP_CHIP_RIGHT,
4950   EL_SP_CHIP_BOTTOM,
4951 };
4952 static int *editor_hl_supaplex_ptr = editor_hl_supaplex;
4953 static int *editor_el_supaplex_ptr = editor_el_supaplex;
4954 static int num_editor_hl_supaplex = ARRAY_SIZE(editor_hl_supaplex);
4955 static int num_editor_el_supaplex = ARRAY_SIZE(editor_el_supaplex);
4956
4957 static int editor_hl_diamond_caves[] =
4958 {
4959   EL_INTERNAL_CASCADE_DC_ACTIVE,
4960   EL_CHAR('D'),
4961   EL_CHAR('C'),
4962   EL_CHAR('2'),
4963 };
4964
4965 static int editor_el_diamond_caves[] =
4966 {
4967   EL_EM_STEEL_EXIT_CLOSED,      // DC2
4968   EL_EM_STEEL_EXIT_OPEN,        // DC2
4969   EL_WALL_EMERALD,              // DC2
4970   EL_WALL_DIAMOND,              // DC2
4971
4972   EL_PEARL,
4973   EL_CRYSTAL,
4974   EL_WALL_PEARL,
4975   EL_WALL_CRYSTAL,
4976
4977   EL_CONVEYOR_BELT_1_LEFT,
4978   EL_CONVEYOR_BELT_1_MIDDLE,
4979   EL_CONVEYOR_BELT_1_RIGHT,
4980   EL_CONVEYOR_BELT_1_SWITCH_MIDDLE,
4981
4982   EL_CONVEYOR_BELT_2_LEFT,
4983   EL_CONVEYOR_BELT_2_MIDDLE,
4984   EL_CONVEYOR_BELT_2_RIGHT,
4985   EL_CONVEYOR_BELT_2_SWITCH_MIDDLE,
4986
4987   EL_CONVEYOR_BELT_3_LEFT,
4988   EL_CONVEYOR_BELT_3_MIDDLE,
4989   EL_CONVEYOR_BELT_3_RIGHT,
4990   EL_CONVEYOR_BELT_3_SWITCH_MIDDLE,
4991
4992   EL_CONVEYOR_BELT_4_LEFT,
4993   EL_CONVEYOR_BELT_4_MIDDLE,
4994   EL_CONVEYOR_BELT_4_RIGHT,
4995   EL_CONVEYOR_BELT_4_SWITCH_MIDDLE,
4996
4997   EL_CONVEYOR_BELT_1_SWITCH_LEFT,
4998   EL_CONVEYOR_BELT_2_SWITCH_LEFT,
4999   EL_CONVEYOR_BELT_3_SWITCH_LEFT,
5000   EL_CONVEYOR_BELT_4_SWITCH_LEFT,
5001
5002   EL_CONVEYOR_BELT_1_SWITCH_RIGHT,
5003   EL_CONVEYOR_BELT_2_SWITCH_RIGHT,
5004   EL_CONVEYOR_BELT_3_SWITCH_RIGHT,
5005   EL_CONVEYOR_BELT_4_SWITCH_RIGHT,
5006
5007   EL_TIMEGATE_CLOSED,
5008   EL_TIMEGATE_OPEN,
5009   EL_TIMEGATE_SWITCH,
5010   EL_DC_TIMEGATE_SWITCH,
5011
5012   EL_SWITCHGATE_CLOSED,
5013   EL_SWITCHGATE_OPEN,
5014   EL_SWITCHGATE_SWITCH_UP,
5015   EL_SWITCHGATE_SWITCH_DOWN,
5016
5017   EL_LIGHT_SWITCH,
5018   EL_LIGHT_SWITCH_ACTIVE,
5019   EL_DC_SWITCHGATE_SWITCH_UP,
5020   EL_DC_SWITCHGATE_SWITCH_DOWN,
5021
5022   EL_STEEL_EXIT_CLOSED,
5023   EL_STEEL_EXIT_OPEN,
5024   EL_STEELWALL_SLIPPERY,
5025   EL_INVISIBLE_SAND,
5026
5027   EL_QUICKSAND_FAST_EMPTY,
5028   EL_QUICKSAND_FAST_FULL,
5029   EL_LANDMINE,
5030   EL_DC_LANDMINE,
5031
5032   EL_SHIELD_NORMAL,
5033   EL_SHIELD_DEADLY,
5034   EL_EXTRA_TIME,
5035   EL_DC_MAGIC_WALL,
5036
5037   EL_ENVELOPE_1,
5038   EL_ENVELOPE_2,
5039   EL_ENVELOPE_3,
5040   EL_ENVELOPE_4,
5041
5042   EL_SIGN_RADIOACTIVITY,
5043   EL_SIGN_WHEELCHAIR,
5044   EL_SIGN_PARKING,
5045   EL_SIGN_NO_ENTRY,
5046
5047   EL_SIGN_GIVE_WAY,
5048   EL_SIGN_ENTRY_FORBIDDEN,
5049   EL_SIGN_EMERGENCY_EXIT,
5050   EL_SIGN_YIN_YANG,
5051
5052 #if 0
5053   EL_SIGN_SPERMS,
5054   EL_SIGN_BULLET,
5055   EL_SIGN_HEART,
5056   EL_SIGN_CROSS,
5057
5058   EL_SIGN_FRANKIE,
5059   EL_EMPTY,
5060   EL_EMPTY,
5061   EL_EMPTY,
5062
5063   EL_SPERMS,
5064   EL_BULLET,
5065   EL_HEART,
5066   EL_CROSS,
5067
5068   EL_FRANKIE,
5069   EL_EMPTY,
5070   EL_EMPTY,
5071   EL_EMPTY,
5072 #endif
5073
5074   EL_DC_STEELWALL_2_SINGLE,
5075   EL_DC_STEELWALL_2_TOP,
5076   EL_SIGN_EXCLAMATION,
5077   EL_SIGN_STOP,
5078
5079   EL_DC_STEELWALL_2_LEFT,
5080   EL_DC_STEELWALL_2_MIDDLE,
5081   EL_DC_STEELWALL_2_HORIZONTAL,
5082   EL_DC_STEELWALL_2_RIGHT,
5083
5084   EL_DC_STEELWALL_1_TOPLEFT,
5085   EL_DC_STEELWALL_2_VERTICAL,
5086   EL_DC_STEELWALL_1_TOPRIGHT,
5087   EL_DC_GATE_WHITE,
5088
5089   EL_DC_STEELWALL_1_VERTICAL,
5090   EL_DC_STEELWALL_2_BOTTOM,
5091   EL_DC_KEY_WHITE,
5092   EL_DC_GATE_WHITE_GRAY,
5093
5094   EL_DC_STEELWALL_1_BOTTOMLEFT,
5095   EL_DC_STEELWALL_1_HORIZONTAL,
5096   EL_DC_STEELWALL_1_BOTTOMRIGHT,
5097   EL_DC_GATE_FAKE_GRAY,
5098
5099   EL_DC_STEELWALL_1_BOTTOMRIGHT_2,
5100   EL_DC_STEELWALL_1_BOTTOM,
5101   EL_DC_STEELWALL_1_BOTTOMLEFT_2,
5102   EL_EXPANDABLE_STEELWALL_HORIZONTAL,
5103
5104   EL_DC_STEELWALL_1_RIGHT,
5105   EL_EMPTY,
5106   EL_DC_STEELWALL_1_LEFT,
5107   EL_EXPANDABLE_STEELWALL_VERTICAL,
5108
5109   EL_DC_STEELWALL_1_TOPRIGHT_2,
5110   EL_DC_STEELWALL_1_TOP,
5111   EL_DC_STEELWALL_1_TOPLEFT_2,
5112   EL_EXPANDABLE_STEELWALL_ANY,
5113 };
5114 static int *editor_hl_diamond_caves_ptr = editor_hl_diamond_caves;
5115 static int *editor_el_diamond_caves_ptr = editor_el_diamond_caves;
5116 static int num_editor_hl_diamond_caves = ARRAY_SIZE(editor_hl_diamond_caves);
5117 static int num_editor_el_diamond_caves = ARRAY_SIZE(editor_el_diamond_caves);
5118
5119 static int editor_hl_dx_boulderdash[] =
5120 {
5121   EL_INTERNAL_CASCADE_DX_ACTIVE,
5122   EL_CHAR('D'),
5123   EL_CHAR('X'),
5124   EL_EMPTY,
5125 };
5126
5127 static int editor_el_dx_boulderdash[] =
5128 {
5129   EL_EMPTY,
5130   EL_TUBE_RIGHT_DOWN,
5131   EL_TUBE_HORIZONTAL_DOWN,
5132   EL_TUBE_LEFT_DOWN,
5133
5134   EL_TUBE_HORIZONTAL,
5135   EL_TUBE_VERTICAL_RIGHT,
5136   EL_TUBE_ANY,
5137   EL_TUBE_VERTICAL_LEFT,
5138
5139   EL_TUBE_VERTICAL,
5140   EL_TUBE_RIGHT_UP,
5141   EL_TUBE_HORIZONTAL_UP,
5142   EL_TUBE_LEFT_UP,
5143
5144   EL_TRAP,
5145   EL_DX_SUPABOMB,
5146   EL_EMPTY,
5147   EL_EMPTY
5148 };
5149 static int *editor_hl_dx_boulderdash_ptr = editor_hl_dx_boulderdash;
5150 static int *editor_el_dx_boulderdash_ptr = editor_el_dx_boulderdash;
5151 static int num_editor_hl_dx_boulderdash = ARRAY_SIZE(editor_hl_dx_boulderdash);
5152 static int num_editor_el_dx_boulderdash = ARRAY_SIZE(editor_el_dx_boulderdash);
5153
5154 static int editor_hl_mirror_magic[] =
5155 {
5156   EL_INTERNAL_CASCADE_MM_ACTIVE,
5157   EL_CHAR('M'),
5158   EL_CHAR('M'),
5159   EL_EMPTY,
5160 };
5161
5162 static int editor_el_mirror_magic[] =
5163 {
5164   EL_MM_MCDUFFIN_RIGHT,
5165   EL_MM_MCDUFFIN_UP,
5166   EL_MM_MCDUFFIN_LEFT,
5167   EL_MM_MCDUFFIN_DOWN,
5168
5169   EL_MM_MIRROR_START,
5170   EL_MM_MIRROR_FIXED_START,
5171   EL_MM_POLARIZER_START,
5172   EL_MM_POLARIZER_CROSS_START,
5173
5174   EL_MM_TELEPORTER_RED_START,
5175   EL_MM_TELEPORTER_YELLOW_START,
5176   EL_MM_TELEPORTER_GREEN_START,
5177   EL_MM_TELEPORTER_BLUE_START,
5178
5179   EL_MM_PRISM,
5180   EL_MM_FUSE_ACTIVE,
5181   EL_MM_PACMAN_RIGHT,
5182   EL_MM_EXIT_CLOSED,
5183
5184   EL_MM_KETTLE,
5185   EL_MM_BOMB,
5186   EL_MM_KEY,
5187   EL_MM_FUEL_FULL,
5188
5189   EL_MM_LIGHTBULB,
5190   EL_MM_LIGHTBULB_ACTIVE,
5191   EL_MM_GRAY_BALL,
5192   EL_MM_LIGHTBALL,
5193
5194   EL_MM_STEEL_WALL,
5195   EL_MM_WOODEN_WALL,
5196   EL_MM_ICE_WALL,
5197   EL_MM_AMOEBA_WALL,
5198
5199   EL_MM_STEEL_LOCK,
5200   EL_MM_WOODEN_LOCK,
5201   EL_MM_STEEL_BLOCK,
5202   EL_MM_WOODEN_BLOCK,
5203
5204   EL_MM_STEEL_GRID_FIXED_1,
5205   EL_MM_STEEL_GRID_FIXED_2,
5206   EL_MM_STEEL_GRID_FIXED_3,
5207   EL_MM_STEEL_GRID_FIXED_4,
5208
5209   EL_MM_WOODEN_GRID_FIXED_1,
5210   EL_MM_WOODEN_GRID_FIXED_2,
5211   EL_MM_WOODEN_GRID_FIXED_3,
5212   EL_MM_WOODEN_GRID_FIXED_4,
5213
5214   EL_MM_ENVELOPE_1,
5215   EL_MM_ENVELOPE_2,
5216   EL_MM_ENVELOPE_3,
5217   EL_MM_ENVELOPE_4
5218 };
5219 static int *editor_hl_mirror_magic_ptr = editor_hl_mirror_magic;
5220 static int *editor_el_mirror_magic_ptr = editor_el_mirror_magic;
5221 static int num_editor_hl_mirror_magic = ARRAY_SIZE(editor_hl_mirror_magic);
5222 static int num_editor_el_mirror_magic = ARRAY_SIZE(editor_el_mirror_magic);
5223
5224 static int editor_hl_deflektor[] =
5225 {
5226   EL_INTERNAL_CASCADE_DF_ACTIVE,
5227   EL_CHAR('D'),
5228   EL_CHAR('F'),
5229   EL_EMPTY,
5230 };
5231
5232 static int editor_el_deflektor[] =
5233 {
5234   EL_DF_LASER_RIGHT,
5235   EL_DF_LASER_UP,
5236   EL_DF_LASER_LEFT,
5237   EL_DF_LASER_DOWN,
5238
5239   EL_DF_RECEIVER_RIGHT,
5240   EL_DF_RECEIVER_UP,
5241   EL_DF_RECEIVER_LEFT,
5242   EL_DF_RECEIVER_DOWN,
5243
5244   EL_DF_MIRROR_START,
5245   EL_DF_MIRROR_ROTATING_START,
5246   EL_DF_MIRROR_FIXED_START,
5247   EL_DF_CELL,
5248
5249   EL_DF_FIBRE_OPTIC_RED_1,
5250   EL_DF_FIBRE_OPTIC_YELLOW_1,
5251   EL_DF_FIBRE_OPTIC_GREEN_1,
5252   EL_DF_FIBRE_OPTIC_BLUE_1,
5253
5254   EL_DF_STEEL_GRID_FIXED_START,
5255   EL_DF_STEEL_GRID_ROTATING_START,
5256   EL_DF_WOODEN_GRID_FIXED_START,
5257   EL_DF_WOODEN_GRID_ROTATING_START,
5258
5259   EL_DF_STEEL_WALL,
5260   EL_DF_WOODEN_WALL,
5261   EL_DF_REFRACTOR,
5262   EL_DF_MINE,
5263
5264   EL_DF_SLOPE_1,
5265   EL_DF_SLOPE_2,
5266   EL_DF_SLOPE_3,
5267   EL_DF_SLOPE_4
5268 };
5269 static int *editor_hl_deflektor_ptr = editor_hl_deflektor;
5270 static int *editor_el_deflektor_ptr = editor_el_deflektor;
5271 static int num_editor_hl_deflektor = ARRAY_SIZE(editor_hl_deflektor);
5272 static int num_editor_el_deflektor = ARRAY_SIZE(editor_el_deflektor);
5273
5274 static int editor_hl_chars[] =
5275 {
5276   EL_INTERNAL_CASCADE_CHARS_ACTIVE,
5277   EL_CHAR('T'),
5278   EL_CHAR('X'),
5279   EL_CHAR('T'),
5280 };
5281
5282 static int editor_el_chars[] =
5283 {
5284   EL_CHAR(' '),
5285   EL_CHAR('!'),
5286   EL_CHAR('"'),
5287   EL_CHAR('#'),
5288
5289   EL_CHAR('$'),
5290   EL_CHAR('%'),
5291   EL_CHAR('&'),
5292   EL_CHAR('\''),
5293
5294   EL_CHAR('('),
5295   EL_CHAR(')'),
5296   EL_CHAR('*'),
5297   EL_CHAR('+'),
5298
5299   EL_CHAR(','),
5300   EL_CHAR('-'),
5301   EL_CHAR('.'),
5302   EL_CHAR('/'),
5303
5304   EL_CHAR('0'),
5305   EL_CHAR('1'),
5306   EL_CHAR('2'),
5307   EL_CHAR('3'),
5308
5309   EL_CHAR('4'),
5310   EL_CHAR('5'),
5311   EL_CHAR('6'),
5312   EL_CHAR('7'),
5313
5314   EL_CHAR('8'),
5315   EL_CHAR('9'),
5316   EL_CHAR(':'),
5317   EL_CHAR(';'),
5318
5319   EL_CHAR('<'),
5320   EL_CHAR('='),
5321   EL_CHAR('>'),
5322   EL_CHAR('?'),
5323
5324   EL_CHAR('@'),
5325   EL_CHAR('A'),
5326   EL_CHAR('B'),
5327   EL_CHAR('C'),
5328
5329   EL_CHAR('D'),
5330   EL_CHAR('E'),
5331   EL_CHAR('F'),
5332   EL_CHAR('G'),
5333
5334   EL_CHAR('H'),
5335   EL_CHAR('I'),
5336   EL_CHAR('J'),
5337   EL_CHAR('K'),
5338
5339   EL_CHAR('L'),
5340   EL_CHAR('M'),
5341   EL_CHAR('N'),
5342   EL_CHAR('O'),
5343
5344   EL_CHAR('P'),
5345   EL_CHAR('Q'),
5346   EL_CHAR('R'),
5347   EL_CHAR('S'),
5348
5349   EL_CHAR('T'),
5350   EL_CHAR('U'),
5351   EL_CHAR('V'),
5352   EL_CHAR('W'),
5353
5354   EL_CHAR('X'),
5355   EL_CHAR('Y'),
5356   EL_CHAR('Z'),
5357   EL_CHAR('['),
5358
5359   EL_CHAR('\\'),
5360   EL_CHAR(']'),
5361   EL_CHAR('^'),
5362   EL_CHAR('_'),
5363
5364   EL_CHAR(CHAR_BYTE_COPYRIGHT),
5365   EL_CHAR(CHAR_BYTE_UMLAUT_A),
5366   EL_CHAR(CHAR_BYTE_UMLAUT_O),
5367   EL_CHAR(CHAR_BYTE_UMLAUT_U),
5368
5369   EL_CHAR(CHAR_BYTE_DEGREE),
5370   EL_CHAR(CHAR_BYTE_REGISTERED),
5371   EL_CHAR(FONT_ASCII_CURSOR),
5372   EL_CHAR(FONT_ASCII_BUTTON),
5373
5374   EL_CHAR(FONT_ASCII_UP),
5375   EL_CHAR(FONT_ASCII_DOWN),
5376   EL_CHAR(' '),
5377   EL_CHAR(' ')
5378 };
5379 static int *editor_hl_chars_ptr = editor_hl_chars;
5380 static int *editor_el_chars_ptr = editor_el_chars;
5381 static int num_editor_hl_chars = ARRAY_SIZE(editor_hl_chars);
5382 static int num_editor_el_chars = ARRAY_SIZE(editor_el_chars);
5383
5384 static int editor_hl_steel_chars[] =
5385 {
5386   EL_INTERNAL_CASCADE_STEEL_CHARS_ACTIVE,
5387   EL_STEEL_CHAR('T'),
5388   EL_STEEL_CHAR('X'),
5389   EL_STEEL_CHAR('T'),
5390 };
5391
5392 static int editor_el_steel_chars[] =
5393 {
5394   EL_STEEL_CHAR(' '),
5395   EL_STEEL_CHAR('!'),
5396   EL_STEEL_CHAR('"'),
5397   EL_STEEL_CHAR('#'),
5398
5399   EL_STEEL_CHAR('$'),
5400   EL_STEEL_CHAR('%'),
5401   EL_STEEL_CHAR('&'),
5402   EL_STEEL_CHAR('\''),
5403
5404   EL_STEEL_CHAR('('),
5405   EL_STEEL_CHAR(')'),
5406   EL_STEEL_CHAR('*'),
5407   EL_STEEL_CHAR('+'),
5408
5409   EL_STEEL_CHAR(','),
5410   EL_STEEL_CHAR('-'),
5411   EL_STEEL_CHAR('.'),
5412   EL_STEEL_CHAR('/'),
5413
5414   EL_STEEL_CHAR('0'),
5415   EL_STEEL_CHAR('1'),
5416   EL_STEEL_CHAR('2'),
5417   EL_STEEL_CHAR('3'),
5418
5419   EL_STEEL_CHAR('4'),
5420   EL_STEEL_CHAR('5'),
5421   EL_STEEL_CHAR('6'),
5422   EL_STEEL_CHAR('7'),
5423
5424   EL_STEEL_CHAR('8'),
5425   EL_STEEL_CHAR('9'),
5426   EL_STEEL_CHAR(':'),
5427   EL_STEEL_CHAR(';'),
5428
5429   EL_STEEL_CHAR('<'),
5430   EL_STEEL_CHAR('='),
5431   EL_STEEL_CHAR('>'),
5432   EL_STEEL_CHAR('?'),
5433
5434   EL_STEEL_CHAR('@'),
5435   EL_STEEL_CHAR('A'),
5436   EL_STEEL_CHAR('B'),
5437   EL_STEEL_CHAR('C'),
5438
5439   EL_STEEL_CHAR('D'),
5440   EL_STEEL_CHAR('E'),
5441   EL_STEEL_CHAR('F'),
5442   EL_STEEL_CHAR('G'),
5443
5444   EL_STEEL_CHAR('H'),
5445   EL_STEEL_CHAR('I'),
5446   EL_STEEL_CHAR('J'),
5447   EL_STEEL_CHAR('K'),
5448
5449   EL_STEEL_CHAR('L'),
5450   EL_STEEL_CHAR('M'),
5451   EL_STEEL_CHAR('N'),
5452   EL_STEEL_CHAR('O'),
5453
5454   EL_STEEL_CHAR('P'),
5455   EL_STEEL_CHAR('Q'),
5456   EL_STEEL_CHAR('R'),
5457   EL_STEEL_CHAR('S'),
5458
5459   EL_STEEL_CHAR('T'),
5460   EL_STEEL_CHAR('U'),
5461   EL_STEEL_CHAR('V'),
5462   EL_STEEL_CHAR('W'),
5463
5464   EL_STEEL_CHAR('X'),
5465   EL_STEEL_CHAR('Y'),
5466   EL_STEEL_CHAR('Z'),
5467   EL_STEEL_CHAR('['),
5468
5469   EL_STEEL_CHAR('\\'),
5470   EL_STEEL_CHAR(']'),
5471   EL_STEEL_CHAR('^'),
5472   EL_STEEL_CHAR('_'),
5473
5474   EL_STEEL_CHAR(CHAR_BYTE_COPYRIGHT),
5475   EL_STEEL_CHAR(CHAR_BYTE_UMLAUT_A),
5476   EL_STEEL_CHAR(CHAR_BYTE_UMLAUT_O),
5477   EL_STEEL_CHAR(CHAR_BYTE_UMLAUT_U),
5478
5479   EL_STEEL_CHAR(CHAR_BYTE_DEGREE),
5480   EL_STEEL_CHAR(CHAR_BYTE_REGISTERED),
5481   EL_STEEL_CHAR(FONT_ASCII_CURSOR),
5482   EL_STEEL_CHAR(FONT_ASCII_BUTTON),
5483
5484   EL_STEEL_CHAR(FONT_ASCII_UP),
5485   EL_STEEL_CHAR(FONT_ASCII_DOWN),
5486   EL_STEEL_CHAR(' '),
5487   EL_STEEL_CHAR(' ')
5488 };
5489 static int *editor_hl_steel_chars_ptr = editor_hl_steel_chars;
5490 static int *editor_el_steel_chars_ptr = editor_el_steel_chars;
5491 static int num_editor_hl_steel_chars = ARRAY_SIZE(editor_hl_steel_chars);
5492 static int num_editor_el_steel_chars = ARRAY_SIZE(editor_el_steel_chars);
5493
5494 static int editor_hl_custom[] =
5495 {
5496   EL_INTERNAL_CASCADE_CE_ACTIVE,
5497   EL_CHAR('C'),
5498   EL_CHAR('E'),
5499   EL_EMPTY,
5500 };
5501
5502 static int editor_el_custom[] =
5503 {
5504   EL_CUSTOM_START + 0,
5505   EL_CUSTOM_START + 1,
5506   EL_CUSTOM_START + 2,
5507   EL_CUSTOM_START + 3,
5508
5509   EL_CUSTOM_START + 4,
5510   EL_CUSTOM_START + 5,
5511   EL_CUSTOM_START + 6,
5512   EL_CUSTOM_START + 7,
5513
5514   EL_CUSTOM_START + 8,
5515   EL_CUSTOM_START + 9,
5516   EL_CUSTOM_START + 10,
5517   EL_CUSTOM_START + 11,
5518
5519   EL_CUSTOM_START + 12,
5520   EL_CUSTOM_START + 13,
5521   EL_CUSTOM_START + 14,
5522   EL_CUSTOM_START + 15,
5523
5524   EL_CUSTOM_START + 16,
5525   EL_CUSTOM_START + 17,
5526   EL_CUSTOM_START + 18,
5527   EL_CUSTOM_START + 19,
5528
5529   EL_CUSTOM_START + 20,
5530   EL_CUSTOM_START + 21,
5531   EL_CUSTOM_START + 22,
5532   EL_CUSTOM_START + 23,
5533
5534   EL_CUSTOM_START + 24,
5535   EL_CUSTOM_START + 25,
5536   EL_CUSTOM_START + 26,
5537   EL_CUSTOM_START + 27,
5538
5539   EL_CUSTOM_START + 28,
5540   EL_CUSTOM_START + 29,
5541   EL_CUSTOM_START + 30,
5542   EL_CUSTOM_START + 31,
5543
5544   EL_CUSTOM_START + 32,
5545   EL_CUSTOM_START + 33,
5546   EL_CUSTOM_START + 34,
5547   EL_CUSTOM_START + 35,
5548
5549   EL_CUSTOM_START + 36,
5550   EL_CUSTOM_START + 37,
5551   EL_CUSTOM_START + 38,
5552   EL_CUSTOM_START + 39,
5553
5554   EL_CUSTOM_START + 40,
5555   EL_CUSTOM_START + 41,
5556   EL_CUSTOM_START + 42,
5557   EL_CUSTOM_START + 43,
5558
5559   EL_CUSTOM_START + 44,
5560   EL_CUSTOM_START + 45,
5561   EL_CUSTOM_START + 46,
5562   EL_CUSTOM_START + 47,
5563
5564   EL_CUSTOM_START + 48,
5565   EL_CUSTOM_START + 49,
5566   EL_CUSTOM_START + 50,
5567   EL_CUSTOM_START + 51,
5568
5569   EL_CUSTOM_START + 52,
5570   EL_CUSTOM_START + 53,
5571   EL_CUSTOM_START + 54,
5572   EL_CUSTOM_START + 55,
5573
5574   EL_CUSTOM_START + 56,
5575   EL_CUSTOM_START + 57,
5576   EL_CUSTOM_START + 58,
5577   EL_CUSTOM_START + 59,
5578
5579   EL_CUSTOM_START + 60,
5580   EL_CUSTOM_START + 61,
5581   EL_CUSTOM_START + 62,
5582   EL_CUSTOM_START + 63,
5583
5584   EL_CUSTOM_START + 64,
5585   EL_CUSTOM_START + 65,
5586   EL_CUSTOM_START + 66,
5587   EL_CUSTOM_START + 67,
5588
5589   EL_CUSTOM_START + 68,
5590   EL_CUSTOM_START + 69,
5591   EL_CUSTOM_START + 70,
5592   EL_CUSTOM_START + 71,
5593
5594   EL_CUSTOM_START + 72,
5595   EL_CUSTOM_START + 73,
5596   EL_CUSTOM_START + 74,
5597   EL_CUSTOM_START + 75,
5598
5599   EL_CUSTOM_START + 76,
5600   EL_CUSTOM_START + 77,
5601   EL_CUSTOM_START + 78,
5602   EL_CUSTOM_START + 79,
5603
5604   EL_CUSTOM_START + 80,
5605   EL_CUSTOM_START + 81,
5606   EL_CUSTOM_START + 82,
5607   EL_CUSTOM_START + 83,
5608
5609   EL_CUSTOM_START + 84,
5610   EL_CUSTOM_START + 85,
5611   EL_CUSTOM_START + 86,
5612   EL_CUSTOM_START + 87,
5613
5614   EL_CUSTOM_START + 88,
5615   EL_CUSTOM_START + 89,
5616   EL_CUSTOM_START + 90,
5617   EL_CUSTOM_START + 91,
5618
5619   EL_CUSTOM_START + 92,
5620   EL_CUSTOM_START + 93,
5621   EL_CUSTOM_START + 94,
5622   EL_CUSTOM_START + 95,
5623
5624   EL_CUSTOM_START + 96,
5625   EL_CUSTOM_START + 97,
5626   EL_CUSTOM_START + 98,
5627   EL_CUSTOM_START + 99,
5628
5629   EL_CUSTOM_START + 100,
5630   EL_CUSTOM_START + 101,
5631   EL_CUSTOM_START + 102,
5632   EL_CUSTOM_START + 103,
5633
5634   EL_CUSTOM_START + 104,
5635   EL_CUSTOM_START + 105,
5636   EL_CUSTOM_START + 106,
5637   EL_CUSTOM_START + 107,
5638
5639   EL_CUSTOM_START + 108,
5640   EL_CUSTOM_START + 109,
5641   EL_CUSTOM_START + 110,
5642   EL_CUSTOM_START + 111,
5643
5644   EL_CUSTOM_START + 112,
5645   EL_CUSTOM_START + 113,
5646   EL_CUSTOM_START + 114,
5647   EL_CUSTOM_START + 115,
5648
5649   EL_CUSTOM_START + 116,
5650   EL_CUSTOM_START + 117,
5651   EL_CUSTOM_START + 118,
5652   EL_CUSTOM_START + 119,
5653
5654   EL_CUSTOM_START + 120,
5655   EL_CUSTOM_START + 121,
5656   EL_CUSTOM_START + 122,
5657   EL_CUSTOM_START + 123,
5658
5659   EL_CUSTOM_START + 124,
5660   EL_CUSTOM_START + 125,
5661   EL_CUSTOM_START + 126,
5662   EL_CUSTOM_START + 127,
5663
5664   EL_CUSTOM_START + 128,
5665   EL_CUSTOM_START + 129,
5666   EL_CUSTOM_START + 130,
5667   EL_CUSTOM_START + 131,
5668
5669   EL_CUSTOM_START + 132,
5670   EL_CUSTOM_START + 133,
5671   EL_CUSTOM_START + 134,
5672   EL_CUSTOM_START + 135,
5673
5674   EL_CUSTOM_START + 136,
5675   EL_CUSTOM_START + 137,
5676   EL_CUSTOM_START + 138,
5677   EL_CUSTOM_START + 139,
5678
5679   EL_CUSTOM_START + 140,
5680   EL_CUSTOM_START + 141,
5681   EL_CUSTOM_START + 142,
5682   EL_CUSTOM_START + 143,
5683
5684   EL_CUSTOM_START + 144,
5685   EL_CUSTOM_START + 145,
5686   EL_CUSTOM_START + 146,
5687   EL_CUSTOM_START + 147,
5688
5689   EL_CUSTOM_START + 148,
5690   EL_CUSTOM_START + 149,
5691   EL_CUSTOM_START + 150,
5692   EL_CUSTOM_START + 151,
5693
5694   EL_CUSTOM_START + 152,
5695   EL_CUSTOM_START + 153,
5696   EL_CUSTOM_START + 154,
5697   EL_CUSTOM_START + 155,
5698
5699   EL_CUSTOM_START + 156,
5700   EL_CUSTOM_START + 157,
5701   EL_CUSTOM_START + 158,
5702   EL_CUSTOM_START + 159,
5703
5704   EL_CUSTOM_START + 160,
5705   EL_CUSTOM_START + 161,
5706   EL_CUSTOM_START + 162,
5707   EL_CUSTOM_START + 163,
5708
5709   EL_CUSTOM_START + 164,
5710   EL_CUSTOM_START + 165,
5711   EL_CUSTOM_START + 166,
5712   EL_CUSTOM_START + 167,
5713
5714   EL_CUSTOM_START + 168,
5715   EL_CUSTOM_START + 169,
5716   EL_CUSTOM_START + 170,
5717   EL_CUSTOM_START + 171,
5718
5719   EL_CUSTOM_START + 172,
5720   EL_CUSTOM_START + 173,
5721   EL_CUSTOM_START + 174,
5722   EL_CUSTOM_START + 175,
5723
5724   EL_CUSTOM_START + 176,
5725   EL_CUSTOM_START + 177,
5726   EL_CUSTOM_START + 178,
5727   EL_CUSTOM_START + 179,
5728
5729   EL_CUSTOM_START + 180,
5730   EL_CUSTOM_START + 181,
5731   EL_CUSTOM_START + 182,
5732   EL_CUSTOM_START + 183,
5733
5734   EL_CUSTOM_START + 184,
5735   EL_CUSTOM_START + 185,
5736   EL_CUSTOM_START + 186,
5737   EL_CUSTOM_START + 187,
5738
5739   EL_CUSTOM_START + 188,
5740   EL_CUSTOM_START + 189,
5741   EL_CUSTOM_START + 190,
5742   EL_CUSTOM_START + 191,
5743
5744   EL_CUSTOM_START + 192,
5745   EL_CUSTOM_START + 193,
5746   EL_CUSTOM_START + 194,
5747   EL_CUSTOM_START + 195,
5748
5749   EL_CUSTOM_START + 196,
5750   EL_CUSTOM_START + 197,
5751   EL_CUSTOM_START + 198,
5752   EL_CUSTOM_START + 199,
5753
5754   EL_CUSTOM_START + 200,
5755   EL_CUSTOM_START + 201,
5756   EL_CUSTOM_START + 202,
5757   EL_CUSTOM_START + 203,
5758
5759   EL_CUSTOM_START + 204,
5760   EL_CUSTOM_START + 205,
5761   EL_CUSTOM_START + 206,
5762   EL_CUSTOM_START + 207,
5763
5764   EL_CUSTOM_START + 208,
5765   EL_CUSTOM_START + 209,
5766   EL_CUSTOM_START + 210,
5767   EL_CUSTOM_START + 211,
5768
5769   EL_CUSTOM_START + 212,
5770   EL_CUSTOM_START + 213,
5771   EL_CUSTOM_START + 214,
5772   EL_CUSTOM_START + 215,
5773
5774   EL_CUSTOM_START + 216,
5775   EL_CUSTOM_START + 217,
5776   EL_CUSTOM_START + 218,
5777   EL_CUSTOM_START + 219,
5778
5779   EL_CUSTOM_START + 220,
5780   EL_CUSTOM_START + 221,
5781   EL_CUSTOM_START + 222,
5782   EL_CUSTOM_START + 223,
5783
5784   EL_CUSTOM_START + 224,
5785   EL_CUSTOM_START + 225,
5786   EL_CUSTOM_START + 226,
5787   EL_CUSTOM_START + 227,
5788
5789   EL_CUSTOM_START + 228,
5790   EL_CUSTOM_START + 229,
5791   EL_CUSTOM_START + 230,
5792   EL_CUSTOM_START + 231,
5793
5794   EL_CUSTOM_START + 232,
5795   EL_CUSTOM_START + 233,
5796   EL_CUSTOM_START + 234,
5797   EL_CUSTOM_START + 235,
5798
5799   EL_CUSTOM_START + 236,
5800   EL_CUSTOM_START + 237,
5801   EL_CUSTOM_START + 238,
5802   EL_CUSTOM_START + 239,
5803
5804   EL_CUSTOM_START + 240,
5805   EL_CUSTOM_START + 241,
5806   EL_CUSTOM_START + 242,
5807   EL_CUSTOM_START + 243,
5808
5809   EL_CUSTOM_START + 244,
5810   EL_CUSTOM_START + 245,
5811   EL_CUSTOM_START + 246,
5812   EL_CUSTOM_START + 247,
5813
5814   EL_CUSTOM_START + 248,
5815   EL_CUSTOM_START + 249,
5816   EL_CUSTOM_START + 250,
5817   EL_CUSTOM_START + 251,
5818
5819   EL_CUSTOM_START + 252,
5820   EL_CUSTOM_START + 253,
5821   EL_CUSTOM_START + 254,
5822   EL_CUSTOM_START + 255
5823 };
5824 static int *editor_hl_custom_ptr = editor_hl_custom;
5825 static int *editor_el_custom_ptr = editor_el_custom;
5826 static int num_editor_hl_custom = ARRAY_SIZE(editor_hl_custom);
5827 static int num_editor_el_custom = ARRAY_SIZE(editor_el_custom);
5828
5829 static int editor_hl_group[] =
5830 {
5831   EL_INTERNAL_CASCADE_GE_ACTIVE,
5832   EL_CHAR('G'),
5833   EL_CHAR('E'),
5834   EL_EMPTY,
5835 };
5836
5837 static int editor_el_group[] =
5838 {
5839   EL_GROUP_START + 0,
5840   EL_GROUP_START + 1,
5841   EL_GROUP_START + 2,
5842   EL_GROUP_START + 3,
5843
5844   EL_GROUP_START + 4,
5845   EL_GROUP_START + 5,
5846   EL_GROUP_START + 6,
5847   EL_GROUP_START + 7,
5848
5849   EL_GROUP_START + 8,
5850   EL_GROUP_START + 9,
5851   EL_GROUP_START + 10,
5852   EL_GROUP_START + 11,
5853
5854   EL_GROUP_START + 12,
5855   EL_GROUP_START + 13,
5856   EL_GROUP_START + 14,
5857   EL_GROUP_START + 15,
5858
5859   EL_GROUP_START + 16,
5860   EL_GROUP_START + 17,
5861   EL_GROUP_START + 18,
5862   EL_GROUP_START + 19,
5863
5864   EL_GROUP_START + 20,
5865   EL_GROUP_START + 21,
5866   EL_GROUP_START + 22,
5867   EL_GROUP_START + 23,
5868
5869   EL_GROUP_START + 24,
5870   EL_GROUP_START + 25,
5871   EL_GROUP_START + 26,
5872   EL_GROUP_START + 27,
5873
5874   EL_GROUP_START + 28,
5875   EL_GROUP_START + 29,
5876   EL_GROUP_START + 30,
5877   EL_GROUP_START + 31
5878 };
5879 static int *editor_hl_group_ptr = editor_hl_group;
5880 static int *editor_el_group_ptr = editor_el_group;
5881 static int num_editor_hl_group = ARRAY_SIZE(editor_hl_group);
5882 static int num_editor_el_group = ARRAY_SIZE(editor_el_group);
5883
5884 static int editor_hl_empty_space[] =
5885 {
5886   EL_INTERNAL_CASCADE_ES_ACTIVE,
5887   EL_CHAR('E'),
5888   EL_CHAR('S'),
5889   EL_EMPTY,
5890 };
5891
5892 static int editor_el_empty_space[] =
5893 {
5894   EL_EMPTY_SPACE_1,
5895   EL_EMPTY_SPACE_2,
5896   EL_EMPTY_SPACE_3,
5897   EL_EMPTY_SPACE_4,
5898
5899   EL_EMPTY_SPACE_5,
5900   EL_EMPTY_SPACE_6,
5901   EL_EMPTY_SPACE_7,
5902   EL_EMPTY_SPACE_8,
5903
5904   EL_EMPTY_SPACE_9,
5905   EL_EMPTY_SPACE_10,
5906   EL_EMPTY_SPACE_11,
5907   EL_EMPTY_SPACE_12,
5908
5909   EL_EMPTY_SPACE_13,
5910   EL_EMPTY_SPACE_14,
5911   EL_EMPTY_SPACE_15,
5912   EL_EMPTY_SPACE_16
5913 };
5914 static int *editor_hl_empty_space_ptr = editor_hl_empty_space;
5915 static int *editor_el_empty_space_ptr = editor_el_empty_space;
5916 static int num_editor_hl_empty_space = ARRAY_SIZE(editor_hl_empty_space);
5917 static int num_editor_el_empty_space = ARRAY_SIZE(editor_el_empty_space);
5918
5919 static int editor_hl_reference[] =
5920 {
5921   EL_INTERNAL_CASCADE_REF_ACTIVE,
5922   EL_CHAR('R'),
5923   EL_CHAR('E'),
5924   EL_CHAR('F')
5925 };
5926
5927 static int editor_el_reference[] =
5928 {
5929   EL_TRIGGER_PLAYER,
5930   EL_TRIGGER_ELEMENT,
5931   EL_TRIGGER_CE_VALUE,
5932   EL_TRIGGER_CE_SCORE,
5933
5934   EL_SELF,
5935   EL_ANY_ELEMENT,
5936   EL_CURRENT_CE_VALUE,
5937   EL_CURRENT_CE_SCORE,
5938
5939   EL_PREV_CE_8,
5940   EL_PREV_CE_7,
5941   EL_PREV_CE_6,
5942   EL_PREV_CE_5,
5943
5944   EL_PREV_CE_4,
5945   EL_PREV_CE_3,
5946   EL_PREV_CE_2,
5947   EL_PREV_CE_1,
5948
5949   EL_NEXT_CE_1,
5950   EL_NEXT_CE_2,
5951   EL_NEXT_CE_3,
5952   EL_NEXT_CE_4,
5953
5954   EL_NEXT_CE_5,
5955   EL_NEXT_CE_6,
5956   EL_NEXT_CE_7,
5957   EL_NEXT_CE_8,
5958 };
5959 static int *editor_hl_reference_ptr = editor_hl_reference;
5960 static int *editor_el_reference_ptr = editor_el_reference;
5961 static int num_editor_hl_reference = ARRAY_SIZE(editor_hl_reference);
5962 static int num_editor_el_reference = ARRAY_SIZE(editor_el_reference);
5963
5964 static int editor_hl_user_defined[] =
5965 {
5966   EL_INTERNAL_CASCADE_USER_ACTIVE,
5967   EL_CHAR('M'),
5968   EL_CHAR('Y'),
5969   EL_EMPTY,
5970 };
5971
5972 static int *editor_hl_user_defined_ptr = editor_hl_user_defined;
5973 static int *editor_el_user_defined_ptr = NULL;
5974 static int num_editor_hl_user_defined = ARRAY_SIZE(editor_hl_user_defined);
5975 static int num_editor_el_user_defined = 0;
5976
5977 static int editor_hl_dynamic[] =
5978 {
5979   EL_INTERNAL_CASCADE_DYNAMIC_ACTIVE,
5980   EL_CHAR('U'),
5981   EL_CHAR('S'),
5982   EL_CHAR('E'),
5983 };
5984
5985 static int *editor_hl_dynamic_ptr = editor_hl_dynamic;
5986 static int *editor_el_dynamic_ptr = NULL;
5987 static int num_editor_hl_dynamic = ARRAY_SIZE(editor_hl_dynamic);
5988 static int num_editor_el_dynamic = 0;
5989
5990 static int editor_hl_empty[] = { EL_EMPTY };
5991 static int *editor_el_empty = NULL;     // dynamically allocated
5992
5993 static int *editor_hl_empty_ptr = editor_hl_empty;
5994 static int *editor_el_empty_ptr = NULL;
5995 static int num_editor_hl_empty = 0;
5996 static int num_editor_el_empty = 0;     // dynamically determined, if needed
5997
5998 static boolean use_el_empty = FALSE;
5999
6000 static int *editor_elements = NULL;     // dynamically allocated
6001 static int num_editor_elements = 0;     // dynamically determined
6002
6003 static boolean setup_editor_cascade_never = FALSE;
6004
6005 static boolean setup_editor_el_players                  = TRUE;
6006 static boolean setup_editor_el_boulderdash              = TRUE;
6007 static boolean setup_editor_el_boulderdash_native       = TRUE;
6008 static boolean setup_editor_el_emerald_mine             = TRUE;
6009 static boolean setup_editor_el_emerald_mine_club        = TRUE;
6010 static boolean setup_editor_el_more                     = TRUE;
6011 static boolean setup_editor_el_sokoban                  = TRUE;
6012 static boolean setup_editor_el_supaplex                 = TRUE;
6013 static boolean setup_editor_el_diamond_caves            = TRUE;
6014 static boolean setup_editor_el_dx_boulderdash           = TRUE;
6015 static boolean setup_editor_el_mirror_magic             = TRUE;
6016 static boolean setup_editor_el_deflektor                = TRUE;
6017 static boolean setup_editor_el_chars                    = TRUE;
6018 static boolean setup_editor_el_steel_chars              = TRUE;
6019 static boolean setup_editor_el_custom                   = TRUE;
6020 static boolean setup_editor_el_user_defined             = TRUE;
6021 static boolean setup_editor_el_dynamic                  = TRUE;
6022
6023 static int editor_hl_unused[] = { EL_EMPTY };
6024 static int *editor_hl_unused_ptr = editor_hl_unused;
6025 static int num_editor_hl_unused = 0;
6026
6027 static struct
6028 {
6029   boolean *setup_value;
6030   boolean *setup_cascade_value;
6031
6032   int **headline_list;
6033   int *headline_list_size;
6034
6035   int **element_list;
6036   int *element_list_size;
6037
6038   boolean last_setup_value;
6039 }
6040 editor_elements_info[] =
6041 {
6042   {
6043     &setup_editor_el_players,
6044     &setup_editor_cascade_never,
6045     &editor_hl_unused_ptr,              &num_editor_hl_unused,
6046     &editor_el_players_ptr,             &num_editor_el_players
6047   },
6048   {
6049     &setup_editor_el_boulderdash,
6050     &setup.editor_cascade.el_bd,
6051     &editor_hl_boulderdash_ptr,         &num_editor_hl_boulderdash,
6052     &editor_el_boulderdash_ptr,         &num_editor_el_boulderdash
6053   },
6054   {
6055     &setup_editor_el_boulderdash_native,
6056     &setup.editor_cascade.el_bd_native,
6057     &editor_hl_boulderdash_native_ptr,  &num_editor_hl_boulderdash_native,
6058     &editor_el_boulderdash_native_ptr,  &num_editor_el_boulderdash_native
6059   },
6060   {
6061     &setup_editor_el_emerald_mine,
6062     &setup.editor_cascade.el_em,
6063     &editor_hl_emerald_mine_ptr,        &num_editor_hl_emerald_mine,
6064     &editor_el_emerald_mine_ptr,        &num_editor_el_emerald_mine
6065   },
6066   {
6067     &setup_editor_el_emerald_mine_club,
6068     &setup.editor_cascade.el_emc,
6069     &editor_hl_emerald_mine_club_ptr,   &num_editor_hl_emerald_mine_club,
6070     &editor_el_emerald_mine_club_ptr,   &num_editor_el_emerald_mine_club
6071   },
6072   {
6073     &setup_editor_el_more,
6074     &setup.editor_cascade.el_rnd,
6075     &editor_hl_rnd_ptr,                 &num_editor_hl_rnd,
6076     &editor_el_rnd_ptr,                 &num_editor_el_rnd
6077   },
6078   {
6079     &setup_editor_el_sokoban,
6080     &setup.editor_cascade.el_sb,
6081     &editor_hl_sokoban_ptr,             &num_editor_hl_sokoban,
6082     &editor_el_sokoban_ptr,             &num_editor_el_sokoban
6083   },
6084   {
6085     &setup_editor_el_supaplex,
6086     &setup.editor_cascade.el_sp,
6087     &editor_hl_supaplex_ptr,            &num_editor_hl_supaplex,
6088     &editor_el_supaplex_ptr,            &num_editor_el_supaplex
6089   },
6090   {
6091     &setup_editor_el_diamond_caves,
6092     &setup.editor_cascade.el_dc,
6093     &editor_hl_diamond_caves_ptr,       &num_editor_hl_diamond_caves,
6094     &editor_el_diamond_caves_ptr,       &num_editor_el_diamond_caves
6095   },
6096   {
6097     &setup_editor_el_dx_boulderdash,
6098     &setup.editor_cascade.el_dx,
6099     &editor_hl_dx_boulderdash_ptr,      &num_editor_hl_dx_boulderdash,
6100     &editor_el_dx_boulderdash_ptr,      &num_editor_el_dx_boulderdash
6101   },
6102   {
6103     &setup_editor_el_mirror_magic,
6104     &setup.editor_cascade.el_mm,
6105     &editor_hl_mirror_magic_ptr,        &num_editor_hl_mirror_magic,
6106     &editor_el_mirror_magic_ptr,        &num_editor_el_mirror_magic
6107   },
6108   {
6109     &setup_editor_el_deflektor,
6110     &setup.editor_cascade.el_df,
6111     &editor_hl_deflektor_ptr,           &num_editor_hl_deflektor,
6112     &editor_el_deflektor_ptr,           &num_editor_el_deflektor
6113   },
6114   {
6115     &setup_editor_el_chars,
6116     &setup.editor_cascade.el_chars,
6117     &editor_hl_chars_ptr,               &num_editor_hl_chars,
6118     &editor_el_chars_ptr,               &num_editor_el_chars
6119   },
6120   {
6121     &setup_editor_el_steel_chars,
6122     &setup.editor_cascade.el_steel_chars,
6123     &editor_hl_steel_chars_ptr,         &num_editor_hl_steel_chars,
6124     &editor_el_steel_chars_ptr,         &num_editor_el_steel_chars
6125   },
6126   {
6127     &setup_editor_el_custom,
6128     &setup.editor_cascade.el_ce,
6129     &editor_hl_custom_ptr,              &num_editor_hl_custom,
6130     &editor_el_custom_ptr,              &num_editor_el_custom
6131   },
6132   {
6133     &setup_editor_el_custom,
6134     &setup.editor_cascade.el_ge,
6135     &editor_hl_group_ptr,               &num_editor_hl_group,
6136     &editor_el_group_ptr,               &num_editor_el_group
6137   },
6138   {
6139     &setup_editor_el_custom,
6140     &setup.editor_cascade.el_es,
6141     &editor_hl_empty_space_ptr,         &num_editor_hl_empty_space,
6142     &editor_el_empty_space_ptr,         &num_editor_el_empty_space
6143   },
6144   {
6145     &setup_editor_el_custom,
6146     &setup.editor_cascade.el_ref,
6147     &editor_hl_reference_ptr,           &num_editor_hl_reference,
6148     &editor_el_reference_ptr,           &num_editor_el_reference
6149   },
6150   {
6151     &setup_editor_el_user_defined,
6152     &setup.editor_cascade.el_user,
6153     &editor_hl_user_defined_ptr,        &num_editor_hl_user_defined,
6154     &editor_el_user_defined_ptr,        &num_editor_el_user_defined
6155   },
6156   {
6157     &setup_editor_el_dynamic,
6158     &setup.editor_cascade.el_dynamic,
6159     &editor_hl_dynamic_ptr,             &num_editor_hl_dynamic,
6160     &editor_el_dynamic_ptr,             &num_editor_el_dynamic,
6161   },
6162   {
6163     &use_el_empty,
6164     &use_el_empty,
6165     &editor_hl_empty_ptr,               &num_editor_hl_empty,
6166     &editor_el_empty_ptr,               &num_editor_el_empty,
6167   },
6168   {
6169     NULL,
6170     NULL,
6171     NULL,                               NULL,
6172     NULL,                               NULL
6173   }
6174 };
6175
6176 static struct XY xy_directions[] =
6177 {
6178   { -1,  0 },
6179   { +1,  0 },
6180   {  0, -1 },
6181   {  0, +1 }
6182 };
6183
6184
6185 // ----------------------------------------------------------------------------
6186 // functions
6187 // ----------------------------------------------------------------------------
6188
6189 static int getMaxInfoTextLength(void)
6190 {
6191   return (SXSIZE / getFontWidth(INFOTEXT_FONT));
6192 }
6193
6194 static int getTextWidthForGadget(char *text)
6195 {
6196   if (text == NULL)
6197     return 0;
6198
6199   return (getTextWidth(text, FONT_TEXT_1) + ED_GADGET_TEXT_DISTANCE);
6200 }
6201
6202 static int getTextWidthForDrawingArea(char *text)
6203 {
6204   if (text == NULL)
6205     return 0;
6206
6207   return (getTextWidth(text, FONT_TEXT_1) + ED_DRAWINGAREA_TEXT_DISTANCE);
6208 }
6209
6210 static int getRightGadgetBorder(struct GadgetInfo *gi, char *text)
6211 {
6212   return (gi->x + gi->width + getTextWidthForGadget(text));
6213 }
6214
6215 static char *getElementInfoText(int element)
6216 {
6217   char *info_text = NULL;
6218
6219   if (element < MAX_NUM_ELEMENTS)
6220   {
6221     if (strlen(element_info[element].description) > 0)
6222       info_text = element_info[element].description;
6223     else if (element_info[element].custom_description != NULL)
6224       info_text = element_info[element].custom_description;
6225     else if (element_info[element].editor_description != NULL)
6226       info_text = element_info[element].editor_description;
6227   }
6228
6229   if (info_text == NULL)
6230     info_text = INFOTEXT_UNKNOWN_ELEMENT;
6231
6232   return info_text;
6233 }
6234
6235 static char *getElementDescriptionFilenameExt(char *basename)
6236 {
6237   char *elements_subdir = ELEMENTS_DIRECTORY;
6238   static char *elements_subdir2 = NULL;
6239   static char *filename = NULL;
6240
6241   if (elements_subdir2 == NULL)
6242     elements_subdir2 = getPath2(DOCS_DIRECTORY, elements_subdir);
6243
6244   checked_free(filename);
6245
6246   // 1st try: look for element description in current level set directory
6247   filename = getPath3(getCurrentLevelDir(), elements_subdir2, basename);
6248   if (fileExists(filename))
6249     return filename;
6250
6251   free(filename);
6252
6253   // 2nd try: look for element description in the game's base directory
6254   filename = getPath3(options.docs_directory, elements_subdir, basename);
6255   if (fileExists(filename))
6256     return filename;
6257
6258   return NULL;
6259 }
6260
6261 static char *getElementDescriptionFilename(int element)
6262 {
6263   char basename[MAX_FILENAME_LEN];
6264   char *filename;
6265
6266   // 1st try: look for element description file for exactly this element
6267   sprintf(basename, "%s.txt", element_info[element].token_name);
6268   filename = getElementDescriptionFilenameExt(basename);
6269   if (filename != NULL)
6270     return filename;
6271
6272   // 2nd try: look for element description file for this element's class
6273   sprintf(basename, "%s.txt", element_info[element].class_name);
6274   filename = getElementDescriptionFilenameExt(basename);
6275   if (filename != NULL)
6276     return filename;
6277
6278   // 3rd try: look for generic fallback text file for any element
6279   filename = getElementDescriptionFilenameExt(FALLBACK_TEXT_FILENAME);
6280   if (filename != NULL)
6281     return filename;
6282
6283   return NULL;
6284 }
6285
6286 static boolean suppressBorderElement(void)
6287 {
6288   return (level.game_engine_type == GAME_ENGINE_TYPE_MM &&
6289           lev_fieldx <= MAX_ED_FIELDX &&
6290           lev_fieldy <= MAX_ED_FIELDY);
6291 }
6292
6293 static void InitDynamicEditorElementList(int **elements, int *num_elements)
6294 {
6295   boolean element_found[NUM_FILE_ELEMENTS];
6296   int i, x, y;
6297
6298   // initialize list of used elements to "not used"
6299   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6300     element_found[i] = FALSE;
6301
6302   // find all elements used in current level
6303   for (y = 0; y < lev_fieldy; y++)
6304   {
6305     for (x = 0; x < lev_fieldx; x++)
6306     {
6307       if (Tile[x][y] >= NUM_FILE_ELEMENTS)      // should never happen
6308         continue;
6309
6310       if (IS_MM_WALL(Tile[x][y]))
6311         element_found[map_mm_wall_element(Tile[x][y])] = TRUE;
6312       else
6313         element_found[Tile[x][y]] = TRUE;
6314     }
6315   }
6316
6317   *num_elements = 0;
6318
6319   // count number of elements used in current level
6320   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6321     if (element_found[i])
6322       (*num_elements)++;
6323
6324   // add space for up to 3 more elements for padding that may be needed
6325   *num_elements += 3;
6326
6327   // free memory for old list of elements, if needed
6328   checked_free(*elements);
6329
6330   // allocate memory for new list of elements
6331   *elements = checked_malloc(*num_elements * sizeof(int));
6332
6333   *num_elements = 0;
6334
6335   // add all elements used in current level (non-custom/group/empty elements)
6336   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6337     if (element_found[i] && !(IS_CUSTOM_ELEMENT(i) ||
6338                               IS_GROUP_ELEMENT(i) ||
6339                               IS_EMPTY_ELEMENT(i)))
6340       (*elements)[(*num_elements)++] = i;
6341
6342   // add all elements used in current level (custom/group/empty elements)
6343   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
6344     if (element_found[i] && (IS_CUSTOM_ELEMENT(i) ||
6345                              IS_GROUP_ELEMENT(i) ||
6346                              IS_EMPTY_ELEMENT(i)))
6347       (*elements)[(*num_elements)++] = i;
6348
6349   while (*num_elements % 4)     // pad with empty elements, if needed
6350     (*elements)[(*num_elements)++] = EL_EMPTY;
6351 }
6352
6353 static void ReinitializeElementList_EnableSections(void)
6354 {
6355   // default: enable all element sections
6356
6357   setup_editor_el_players               = TRUE;
6358   setup_editor_el_boulderdash           = TRUE;
6359   setup_editor_el_boulderdash_native    = TRUE;
6360   setup_editor_el_emerald_mine          = TRUE;
6361   setup_editor_el_emerald_mine_club     = TRUE;
6362   setup_editor_el_more                  = TRUE;
6363   setup_editor_el_sokoban               = TRUE;
6364   setup_editor_el_supaplex              = TRUE;
6365   setup_editor_el_diamond_caves         = TRUE;
6366   setup_editor_el_dx_boulderdash        = TRUE;
6367   setup_editor_el_mirror_magic          = TRUE;
6368   setup_editor_el_deflektor             = TRUE;
6369   setup_editor_el_chars                 = TRUE;
6370   setup_editor_el_steel_chars           = TRUE;
6371
6372   setup_editor_el_custom                = TRUE;
6373   setup_editor_el_user_defined          = TRUE;
6374   setup_editor_el_dynamic               = TRUE;
6375
6376   // now disable all element sections not to be displayed
6377
6378   if (!setup.editor.el_classic)
6379   {
6380     setup_editor_el_players             = FALSE;
6381     setup_editor_el_boulderdash         = FALSE;
6382     setup_editor_el_boulderdash_native  = FALSE;
6383     setup_editor_el_emerald_mine        = FALSE;
6384     setup_editor_el_emerald_mine_club   = FALSE;
6385     setup_editor_el_more                = FALSE;
6386     setup_editor_el_sokoban             = FALSE;
6387     setup_editor_el_supaplex            = FALSE;
6388     setup_editor_el_diamond_caves       = FALSE;
6389     setup_editor_el_dx_boulderdash      = FALSE;
6390     setup_editor_el_mirror_magic        = FALSE;
6391     setup_editor_el_deflektor           = FALSE;
6392     setup_editor_el_chars               = FALSE;
6393     setup_editor_el_steel_chars         = FALSE;
6394   }
6395
6396   if (!setup.editor.el_custom)
6397   {
6398     setup_editor_el_custom              = FALSE;
6399   }
6400
6401   if (!setup.editor.el_user_defined)
6402   {
6403     setup_editor_el_user_defined        = FALSE;
6404   }
6405
6406   if (!setup.editor.el_dynamic)
6407   {
6408     setup_editor_el_dynamic             = FALSE;
6409   }
6410
6411   if (level.game_engine_type == GAME_ENGINE_TYPE_RND)
6412   {
6413     setup_editor_el_boulderdash_native  = FALSE;
6414     setup_editor_el_mirror_magic        = FALSE;
6415     setup_editor_el_deflektor           = FALSE;
6416   }
6417   else if (level.game_engine_type == GAME_ENGINE_TYPE_BD)
6418   {
6419     setup_editor_el_players             = FALSE;
6420     setup_editor_el_boulderdash         = FALSE;
6421     setup_editor_el_emerald_mine        = FALSE;
6422     setup_editor_el_emerald_mine_club   = FALSE;
6423     setup_editor_el_more                = FALSE;
6424     setup_editor_el_sokoban             = FALSE;
6425     setup_editor_el_supaplex            = FALSE;
6426     setup_editor_el_diamond_caves       = FALSE;
6427     setup_editor_el_dx_boulderdash      = FALSE;
6428     setup_editor_el_mirror_magic        = FALSE;
6429     setup_editor_el_deflektor           = FALSE;
6430     setup_editor_el_chars               = FALSE;
6431     setup_editor_el_steel_chars         = FALSE;
6432
6433     setup_editor_el_custom              = FALSE;
6434   }
6435   else if (level.game_engine_type == GAME_ENGINE_TYPE_EM)
6436   {
6437     setup_editor_el_boulderdash         = FALSE;
6438     setup_editor_el_boulderdash_native  = FALSE;
6439     setup_editor_el_more                = FALSE;
6440     setup_editor_el_sokoban             = FALSE;
6441     setup_editor_el_supaplex            = FALSE;
6442     setup_editor_el_diamond_caves       = FALSE;
6443     setup_editor_el_dx_boulderdash      = FALSE;
6444     setup_editor_el_mirror_magic        = FALSE;
6445     setup_editor_el_deflektor           = FALSE;
6446     setup_editor_el_steel_chars         = FALSE;
6447
6448     setup_editor_el_custom              = FALSE;
6449   }
6450   else if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
6451   {
6452     setup_editor_el_players             = FALSE;
6453     setup_editor_el_boulderdash         = FALSE;
6454     setup_editor_el_boulderdash_native  = FALSE;
6455     setup_editor_el_emerald_mine        = FALSE;
6456     setup_editor_el_emerald_mine_club   = FALSE;
6457     setup_editor_el_more                = FALSE;
6458     setup_editor_el_sokoban             = FALSE;
6459     setup_editor_el_diamond_caves       = FALSE;
6460     setup_editor_el_dx_boulderdash      = FALSE;
6461     setup_editor_el_mirror_magic        = FALSE;
6462     setup_editor_el_deflektor           = FALSE;
6463     setup_editor_el_chars               = FALSE;
6464     setup_editor_el_steel_chars         = FALSE;
6465
6466     setup_editor_el_custom              = FALSE;
6467   }
6468   else if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
6469   {
6470     setup_editor_el_players             = FALSE;
6471     setup_editor_el_boulderdash         = FALSE;
6472     setup_editor_el_boulderdash_native  = FALSE;
6473     setup_editor_el_emerald_mine        = FALSE;
6474     setup_editor_el_emerald_mine_club   = FALSE;
6475     setup_editor_el_more                = FALSE;
6476     setup_editor_el_sokoban             = FALSE;
6477     setup_editor_el_supaplex            = FALSE;
6478     setup_editor_el_diamond_caves       = FALSE;
6479     setup_editor_el_dx_boulderdash      = FALSE;
6480     setup_editor_el_steel_chars         = FALSE;
6481
6482     setup_editor_el_custom              = FALSE;
6483   }
6484 }
6485
6486 static void ReinitializeElementList(void)
6487 {
6488   static boolean initialization_needed = TRUE;
6489   int pos = 0;
6490   int i, j;
6491
6492   ReinitializeElementList_EnableSections();
6493
6494   if (initialization_needed)
6495   {
6496     LoadSetup_EditorCascade();          // load last editor cascade state
6497
6498     // initialize editor cascade element from saved cascade state
6499     for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6500     {
6501       int *cascade_element = &(*editor_elements_info[i].headline_list)[0];
6502       boolean cascade_value = *editor_elements_info[i].setup_cascade_value;
6503
6504       if (IS_EDITOR_CASCADE(*cascade_element))
6505         *cascade_element =
6506           (cascade_value ? EL_CASCADE_ACTIVE(*cascade_element) :
6507            EL_CASCADE_INACTIVE(*cascade_element));
6508     }
6509
6510     initialization_needed = FALSE;
6511   }
6512
6513   checked_free(editor_elements);
6514
6515   // reload optional user defined element list for each invocation of editor
6516   LoadUserDefinedEditorElementList(&editor_el_user_defined_ptr,
6517                                    &num_editor_el_user_defined);
6518
6519   // initialize dynamic level element list for each invocation of editor
6520   InitDynamicEditorElementList(&editor_el_dynamic_ptr,
6521                                &num_editor_el_dynamic);
6522
6523   // initialize list of empty elements (used for padding, if needed)
6524   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
6525     editor_el_empty[i] = EL_EMPTY;
6526
6527   // do some sanity checks for each element from element list
6528   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6529   {
6530     for (j = 0; j < *editor_elements_info[i].element_list_size; j++)
6531     {
6532       int element = (*editor_elements_info[i].element_list)[j];
6533
6534       if (element >= NUM_FILE_ELEMENTS)
6535         Warn("editor element %d is runtime element", element);
6536
6537       if (strEqual(getElementInfoText(element), INFOTEXT_UNKNOWN_ELEMENT))
6538         Warn("no element description text for element %d", element);
6539     }
6540   }
6541
6542   num_editor_elements = 0;
6543   use_el_empty = FALSE;
6544
6545   // determine size of element list
6546   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6547   {
6548     if (*editor_elements_info[i].setup_value)
6549     {
6550       boolean found_inactive_cascade = FALSE;
6551
6552       if (setup.editor.el_headlines)
6553       {
6554         // required for correct padding of palette headline buttons
6555         if (*editor_elements_info[i].headline_list_size > 0)
6556           num_editor_elements += ED_ELEMENTLIST_COLS;
6557
6558         for (j = 0; j < *editor_elements_info[i].headline_list_size; j++)
6559         {
6560           int element = (*editor_elements_info[i].headline_list)[j];
6561
6562           if (IS_EDITOR_CASCADE_INACTIVE(element))
6563             found_inactive_cascade = TRUE;
6564         }
6565       }
6566
6567       if (found_inactive_cascade)
6568         continue;
6569
6570       // required for correct padding of palette element buttons
6571       int element_list_size = *editor_elements_info[i].element_list_size;
6572       int element_rows =
6573         (element_list_size + ED_ELEMENTLIST_COLS - 1) / ED_ELEMENTLIST_COLS;
6574       int element_buttons = ED_ELEMENTLIST_COLS * element_rows;
6575
6576       num_editor_elements += element_buttons;
6577     }
6578   }
6579
6580   if (num_editor_elements < ED_NUM_ELEMENTLIST_BUTTONS)
6581   {
6582     // offer at least as many elements as element buttons exist
6583     use_el_empty = TRUE;
6584     num_editor_el_empty = ED_NUM_ELEMENTLIST_BUTTONS - num_editor_elements;
6585
6586     num_editor_elements += num_editor_el_empty;
6587   }
6588   else
6589   {
6590     num_editor_el_empty = 0;
6591   }
6592
6593   editor_elements = checked_malloc(num_editor_elements * sizeof(int));
6594
6595   // fill element list
6596   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6597   {
6598     boolean found_inactive_cascade = FALSE;
6599
6600     if (*editor_elements_info[i].setup_value)
6601     {
6602       if (setup.editor.el_headlines)
6603       {
6604         // required for correct padding of palette headline buttons
6605         int headline_size = (*editor_elements_info[i].headline_list_size > 0 ?
6606                              ED_ELEMENTLIST_COLS : 0);
6607
6608         for (j = 0; j < headline_size; j++)
6609         {
6610           // use empty elements for padding of palette headline buttons
6611           int element = (j < *editor_elements_info[i].headline_list_size ?
6612                          (*editor_elements_info[i].headline_list)[j] :
6613                          editor_el_empty[0]);
6614
6615           editor_elements[pos++] = element;
6616
6617           if (IS_EDITOR_CASCADE_INACTIVE(element))
6618             found_inactive_cascade = TRUE;
6619         }
6620       }
6621
6622       if (found_inactive_cascade)
6623         continue;
6624
6625       // required for correct padding of palette element buttons
6626       int element_list_size = *editor_elements_info[i].element_list_size;
6627       int element_rows =
6628         (element_list_size + ED_ELEMENTLIST_COLS - 1) / ED_ELEMENTLIST_COLS;
6629       int element_buttons = ED_ELEMENTLIST_COLS * element_rows;
6630
6631       // copy all elements from element list
6632       for (j = 0; j < element_list_size; j++)
6633         editor_elements[pos++] = (*editor_elements_info[i].element_list)[j];
6634
6635       // use empty elements for padding of palette element buttons
6636       for (j = 0; j < element_buttons - element_list_size; j++)
6637         editor_elements[pos++] = editor_el_empty[0];
6638     }
6639   }
6640
6641   // (this function is also called before editor gadgets are initialized!)
6642   AdjustElementListScrollbar();
6643 }
6644
6645 void PrintEditorElementList(void)
6646 {
6647   boolean *stop = &setup_editor_el_user_defined;
6648   int i, j;
6649
6650   for (i = 0; editor_elements_info[i].setup_value != stop; i++)
6651   {
6652     int cascade_element = (*editor_elements_info[i].headline_list)[0];
6653
6654     if (IS_EDITOR_CASCADE(cascade_element))
6655     {
6656       int cascade_element_show = EL_CASCADE_INACTIVE(cascade_element);
6657       char *headline = element_info[cascade_element_show].editor_description;
6658
6659       PrintLineWithPrefix("# ", "-", 77);
6660       Print("# %s\n", headline);
6661       PrintLineWithPrefix("# ", "-", 77);
6662     }
6663
6664     for (j = 0; j < *editor_elements_info[i].headline_list_size; j++)
6665     {
6666       int element = (*editor_elements_info[i].headline_list)[j];
6667
6668       if (IS_EDITOR_CASCADE(element))
6669         element = EL_CHAR_MINUS;
6670
6671       Print("# %s\n", element_info[element].token_name);
6672     }
6673
6674     if (j > 0)
6675       Print("#\n");
6676
6677     for (j = 0; j < *editor_elements_info[i].element_list_size; j++)
6678     {
6679       int element = (*editor_elements_info[i].element_list)[j];
6680
6681       Print("# %s\n", element_info[element].token_name);
6682     }
6683
6684     if (j > 0)
6685       Print("#\n");
6686   }
6687 }
6688
6689 static void ReinitializeElementListButtons(void)
6690 {
6691   static boolean last_setup_value_headlines = FALSE;
6692   static boolean initialization_needed = TRUE;
6693   int i;
6694
6695   if (!initialization_needed)   // check if editor element setup has changed
6696   {
6697     if (last_setup_value_headlines != setup.editor.el_headlines)
6698       initialization_needed = TRUE;
6699
6700     for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6701       if (editor_elements_info[i].last_setup_value !=
6702           *editor_elements_info[i].setup_value)
6703         initialization_needed = TRUE;
6704   }
6705
6706   if (!initialization_needed)
6707     return;
6708
6709   FreeLevelEditorGadgets();
6710   CreateLevelEditorGadgets();
6711
6712   // store current setup values for next invocation of this function
6713   last_setup_value_headlines = setup.editor.el_headlines;
6714   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
6715     editor_elements_info[i].last_setup_value =
6716       *editor_elements_info[i].setup_value;
6717
6718   initialization_needed = FALSE;
6719 }
6720
6721 static void DrawElementBorder(int dest_x, int dest_y, int width, int height,
6722                               boolean input)
6723 {
6724   int border_graphic =
6725     (input ? IMG_EDITOR_ELEMENT_BORDER_INPUT : IMG_EDITOR_ELEMENT_BORDER);
6726   struct GraphicInfo *g = &graphic_info[border_graphic];
6727   Bitmap *src_bitmap = g->bitmap;
6728   int src_x = g->src_x;
6729   int src_y = g->src_y;
6730   int border_size = g->border_size;
6731   int border_xpos = g->width  - border_size;
6732   int border_ypos = g->height - border_size;
6733   int tilesize = ED_DRAWINGAREA_TILE_SIZE;
6734   int i;
6735
6736   BlitBitmap(src_bitmap, drawto, src_x, src_y,
6737              border_size, border_size,
6738              dest_x - border_size, dest_y - border_size);
6739   BlitBitmap(src_bitmap, drawto, src_x + border_xpos, src_y,
6740              border_size, border_size,
6741              dest_x + width, dest_y - border_size);
6742   BlitBitmap(src_bitmap, drawto, src_x, src_y + border_ypos,
6743              border_size, border_size,
6744              dest_x - border_size, dest_y + height);
6745   BlitBitmap(src_bitmap, drawto, src_x + border_xpos, src_y + border_ypos,
6746              border_size, border_size,
6747              dest_x + width, dest_y + height);
6748
6749   for (i = 0; i < width / tilesize; i++)
6750   {
6751     BlitBitmap(src_bitmap, drawto, src_x + border_size, src_y,
6752                tilesize, border_size,
6753                dest_x + i * tilesize, dest_y - border_size);
6754     BlitBitmap(src_bitmap, drawto, src_x + border_size, src_y + border_ypos,
6755                tilesize, border_size,
6756                dest_x + i * tilesize, dest_y + height);
6757   }
6758
6759   for (i = 0; i < height / tilesize; i++)
6760   {
6761     BlitBitmap(src_bitmap, drawto, src_x, src_y + border_size,
6762                border_size, tilesize,
6763                dest_x - border_size, dest_y + i * tilesize);
6764     BlitBitmap(src_bitmap, drawto, src_x + border_xpos, src_y + border_size,
6765                border_size, tilesize,
6766                dest_x + width, dest_y + i * tilesize);
6767   }
6768
6769   ClearRectangle(drawto, dest_x - 1, dest_y - 1, width + 2, height + 2);
6770 }
6771
6772 static void DrawEditorLevelBorderLine(int x, int y, int xsize, int ysize)
6773 {
6774   int xsize_tile = MAX(ed_tilesize, xsize);
6775   int ysize_tile = MAX(ed_tilesize, ysize);
6776   int xsize_full = xsize + 1;
6777   int ysize_full = ysize + 1;
6778   int xsize_thin = (xsize < ed_tilesize ? 1 : xsize);
6779   int ysize_thin = (ysize < ed_tilesize ? 1 : ysize);
6780   Pixel line_color = getTabulatorBarColor();
6781
6782   if (line_color == BLACK_PIXEL)                // black => transparent
6783     return;
6784
6785   FillRectangle(drawto, SX + x, SY + y, xsize_tile, ysize_tile, BLACK_PIXEL);
6786   FillRectangle(drawto, SX + x, SY + y, xsize_full, ysize_full, line_color);
6787   FillRectangle(drawto, SX + x, SY + y, xsize_thin, ysize_thin, BLACK_PIXEL);
6788 }
6789
6790 static void DrawEditorLevelBorderLinesIfNeeded(void)
6791 {
6792   int xsize = lev_fieldx * ed_tilesize;
6793   int ysize = lev_fieldy * ed_tilesize;
6794   int line_size = getTabulatorBarHeight();
6795
6796   if (!suppressBorderElement())
6797     return;
6798
6799   // draw little border line around editable level playfield
6800
6801   if (xsize < SXSIZE)
6802     DrawEditorLevelBorderLine(xsize, 0, line_size, ysize);
6803
6804   if (ysize < SYSIZE)
6805     DrawEditorLevelBorderLine(0, ysize, xsize, line_size);
6806
6807   if (xsize < SXSIZE && ysize < SYSIZE)
6808     DrawEditorLevelBorderLine(xsize, ysize, line_size, line_size);
6809 }
6810
6811 static void DrawEditorElement(int x, int y, int element)
6812 {
6813   DrawSizedElement(x, y, element, ed_tilesize);
6814 }
6815
6816 static void DrawEditorElementThruMask(int x, int y, int element)
6817 {
6818   DrawSizedElementThruMask(x, y, element, ed_tilesize);
6819 }
6820
6821 static void DrawEditorElementOrWall(int x, int y, int scroll_x, int scroll_y)
6822 {
6823   DrawSizedElementOrWall(x, y, scroll_x, scroll_y, ed_tilesize);
6824 }
6825
6826 static void DrawEditorLevel(int size_x, int size_y, int scroll_x, int scroll_y)
6827 {
6828   DrawSizedLevel(size_x, size_y, scroll_x, scroll_y, ed_tilesize);
6829   DrawEditorLevelBorderLinesIfNeeded();
6830 }
6831
6832 static void DrawDrawingArea(int id)
6833 {
6834   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
6835   int x, y;
6836
6837   int *value = drawingarea_info[id].value;
6838   int area_xsize = drawingarea_info[id].area_xsize;
6839   int area_ysize = drawingarea_info[id].area_ysize;
6840   int tilesize = ED_DRAWINGAREA_TILE_SIZE;
6841
6842   for (x = 0; x < area_xsize; x++)
6843     for (y = 0; y < area_ysize; y++)
6844       DrawSizedGraphicExt(drawto,
6845                           gi->x + x * tilesize,
6846                           gi->y + y * tilesize,
6847                           el2edimg(value[x * area_ysize + y]), 0, tilesize);
6848 }
6849
6850 static void ScrollEditorLevel(int from_x, int from_y, int scroll)
6851 {
6852   int x, y;
6853   int dx = (scroll == ED_SCROLL_LEFT ? -1 : scroll == ED_SCROLL_RIGHT ? 1 : 0);
6854   int dy = (scroll == ED_SCROLL_UP   ? -1 : scroll == ED_SCROLL_DOWN  ? 1 : 0);
6855
6856   BlitBitmap(drawto, drawto,
6857              SX + (dx == -1 ? ed_tilesize : 0),
6858              SY + (dy == -1 ? ed_tilesize : 0),
6859              (ed_fieldx * ed_tilesize) - (dx != 0 ? ed_tilesize : 0),
6860              (ed_fieldy * ed_tilesize) - (dy != 0 ? ed_tilesize : 0),
6861              SX + (dx == +1 ? ed_tilesize : 0),
6862              SY + (dy == +1 ? ed_tilesize : 0));
6863
6864   if (dx)
6865   {
6866     x = (dx == 1 ? 0 : ed_fieldx - 1);
6867     for (y = 0; y < ed_fieldy; y++)
6868       DrawEditorElementOrWall(x, y, from_x, from_y);
6869   }
6870   else if (dy)
6871   {
6872     y = (dy == 1 ? 0 : ed_fieldy - 1);
6873     for (x = 0; x < ed_fieldx; x++)
6874       DrawEditorElementOrWall(x, y, from_x, from_y);
6875   }
6876
6877   redraw_mask |= REDRAW_FIELD;
6878   BackToFront();
6879 }
6880
6881 static void getEditorGraphicSource(int element, int tile_size, Bitmap **bitmap,
6882                                    int *x, int *y)
6883 {
6884   getSizedGraphicSource(el2edimg(element), 0, tile_size, bitmap, x, y);
6885 }
6886
6887 static void CreateControlButtons(void)
6888 {
6889   struct GadgetInfo *gi;
6890   int i;
6891
6892   // create toolbox buttons
6893   for (i = 0; i < ED_NUM_CTRL_BUTTONS; i++)
6894   {
6895     int type_id = controlbutton_info[i].gadget_id;      // same as gadget ID here
6896     int id = controlbutton_info[i].gadget_id;
6897     int type = controlbutton_info[i].gadget_type;
6898     int graphic = controlbutton_info[i].graphic;
6899     struct XYTileSize *pos = controlbutton_info[i].pos;
6900     struct GraphicInfo *gd = &graphic_info[graphic];
6901     Bitmap *deco_bitmap = NULL;
6902     int deco_x = 0, deco_y = 0, deco_xpos = 0, deco_ypos = 0;
6903     int tile_size = 0, deco_shift = 0;
6904     boolean deco_masked = FALSE;
6905     int gd_x1 = gd->src_x;
6906     int gd_y1 = gd->src_y;
6907     int gd_x2 = gd->src_x + gd->pressed_xoffset;
6908     int gd_y2 = gd->src_y + gd->pressed_yoffset;
6909     int gd_x1a = gd->src_x + gd->active_xoffset;
6910     int gd_y1a = gd->src_y + gd->active_yoffset;
6911     int gd_x2a = gd->src_x + gd->active_xoffset + gd->pressed_xoffset;
6912     int gd_y2a = gd->src_y + gd->active_yoffset + gd->pressed_yoffset;
6913     int x = pos->x;
6914     int y = pos->y;
6915     unsigned int event_mask;
6916     int radio_button_nr = RADIO_NR_NONE;
6917     boolean checked = FALSE;
6918
6919     if (type_id != i)
6920       Fail("'controlbutton_info' structure corrupted at index %d -- please fix", i);
6921
6922     if (type == GD_TYPE_RADIO_BUTTON)
6923     {
6924       event_mask = GD_EVENT_PRESSED;
6925       radio_button_nr = RADIO_NR_DRAWING_TOOLBOX;
6926
6927       if (id == drawing_function)
6928         checked = TRUE;
6929     }
6930     else
6931     {
6932       if (id == GADGET_ID_WRAP_LEFT ||
6933           id == GADGET_ID_WRAP_RIGHT ||
6934           id == GADGET_ID_WRAP_UP ||
6935           id == GADGET_ID_WRAP_DOWN)
6936         event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
6937       else
6938         event_mask = GD_EVENT_RELEASED;
6939     }
6940
6941     if (id == GADGET_ID_PROPERTIES ||
6942         id == GADGET_ID_PALETTE)
6943     {
6944       x += DX;
6945       y += DY;
6946     }
6947     else if (id == GADGET_ID_ELEMENT_LEFT ||
6948              id == GADGET_ID_ELEMENT_MIDDLE ||
6949              id == GADGET_ID_ELEMENT_RIGHT)
6950     {
6951       x += DX;
6952       y += DY;
6953
6954       int element = (id == GADGET_ID_ELEMENT_LEFT   ? new_element1 :
6955                      id == GADGET_ID_ELEMENT_MIDDLE ? new_element2 :
6956                      id == GADGET_ID_ELEMENT_RIGHT  ? new_element3 : EL_EMPTY);
6957
6958       tile_size = BUTTON_TILE_SIZE(id == GADGET_ID_ELEMENT_LEFT ?
6959                                    editor.button.element_left.tile_size :
6960                                    id == GADGET_ID_ELEMENT_MIDDLE ?
6961                                    editor.button.element_middle.tile_size :
6962                                    id == GADGET_ID_ELEMENT_RIGHT ?
6963                                    editor.button.element_right.tile_size : 0);
6964
6965       // make sure that decoration does not overlap gadget border
6966       tile_size = MIN(tile_size, MIN(gd->width, gd->height));
6967
6968       getEditorGraphicSource(element, tile_size, &deco_bitmap, &deco_x,&deco_y);
6969
6970       deco_xpos = (gd->width  - tile_size) / 2;
6971       deco_ypos = (gd->height - tile_size) / 2;
6972       deco_shift = 1;
6973       deco_masked = gd->draw_masked;
6974     }
6975     else
6976     {
6977       x += EX;
6978       y += EY;
6979     }
6980
6981     gi = CreateGadget(GDI_CUSTOM_ID, id,
6982                       GDI_CUSTOM_TYPE_ID, type_id,
6983                       GDI_IMAGE_ID, graphic,
6984                       GDI_INFO_TEXT, controlbutton_info[i].infotext,
6985                       GDI_X, x,
6986                       GDI_Y, y,
6987                       GDI_WIDTH, gd->width,
6988                       GDI_HEIGHT, gd->height,
6989                       GDI_TYPE, type,
6990                       GDI_STATE, GD_BUTTON_UNPRESSED,
6991                       GDI_RADIO_NR, radio_button_nr,
6992                       GDI_CHECKED, checked,
6993                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
6994                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
6995                       GDI_ALT_DESIGN_UNPRESSED, gd->bitmap, gd_x1a, gd_y1a,
6996                       GDI_ALT_DESIGN_PRESSED, gd->bitmap, gd_x2a, gd_y2a,
6997                       GDI_DECORATION_DESIGN, deco_bitmap, deco_x, deco_y,
6998                       GDI_DECORATION_POSITION, deco_xpos, deco_ypos,
6999                       GDI_DECORATION_SIZE, tile_size, tile_size,
7000                       GDI_DECORATION_SHIFTING, deco_shift, deco_shift,
7001                       GDI_DECORATION_MASKED, deco_masked,
7002                       GDI_EVENT_MASK, event_mask,
7003                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7004                       GDI_CALLBACK_ACTION, HandleControlButtons,
7005                       GDI_END);
7006
7007     if (gi == NULL)
7008       Fail("cannot create gadget");
7009
7010     level_editor_gadget[id] = gi;
7011   }
7012
7013   // these values are not constant, but can change at runtime
7014   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_UP].x    = ED_SCROLL_UP_XPOS;
7015   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_UP].y    = ED_SCROLL_UP_YPOS;
7016   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_DOWN].x  = ED_SCROLL_DOWN_XPOS;
7017   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_DOWN].y  = ED_SCROLL_DOWN_YPOS;
7018   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_LEFT].x  = ED_SCROLL_LEFT_XPOS;
7019   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_LEFT].y  = ED_SCROLL_LEFT_YPOS;
7020   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_RIGHT].x = ED_SCROLL_RIGHT_XPOS;
7021   scrollbutton_pos[ED_SCROLLBUTTON_ID_AREA_RIGHT].y = ED_SCROLL_RIGHT_YPOS;
7022   scrollbutton_pos[ED_SCROLLBUTTON_ID_LIST_UP].x    = ED_SCROLL2_UP_XPOS;
7023   scrollbutton_pos[ED_SCROLLBUTTON_ID_LIST_UP].y    = ED_SCROLL2_UP_YPOS;
7024   scrollbutton_pos[ED_SCROLLBUTTON_ID_LIST_DOWN].x  = ED_SCROLL2_DOWN_XPOS;
7025   scrollbutton_pos[ED_SCROLLBUTTON_ID_LIST_DOWN].y  = ED_SCROLL2_DOWN_YPOS;
7026
7027   // create buttons for scrolling of drawing area and element list
7028   for (i = 0; i < ED_NUM_SCROLLBUTTONS; i++)
7029   {
7030     int id = scrollbutton_info[i].gadget_id;
7031     int type_id = scrollbutton_info[i].gadget_type_id;
7032     int graphic = scrollbutton_info[i].graphic;
7033     struct GraphicInfo *gd = &graphic_info[graphic];
7034     Bitmap *gd_bitmap = gd->bitmap;
7035     int gd_x1 = gd->src_x;
7036     int gd_y1 = gd->src_y;
7037     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7038     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7039     int width  = gd->width;
7040     int height = gd->height;
7041     int x = scrollbutton_pos[i].x;
7042     int y = scrollbutton_pos[i].y;
7043     unsigned int event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
7044
7045     if (type_id != i)
7046       Fail("'scrollbutton_info' structure corrupted at index %d -- please fix", i);
7047
7048     if (id == GADGET_ID_SCROLL_LIST_UP ||
7049         id == GADGET_ID_SCROLL_LIST_DOWN)
7050     {
7051       x += PX;
7052       y += PY;
7053     }
7054     else
7055     {
7056       x += SX;
7057       y += SY;
7058     }
7059
7060     gi = CreateGadget(GDI_CUSTOM_ID, id,
7061                       GDI_CUSTOM_TYPE_ID, type_id,
7062                       GDI_IMAGE_ID, graphic,
7063                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
7064                       GDI_X, x,
7065                       GDI_Y, y,
7066                       GDI_WIDTH, width,
7067                       GDI_HEIGHT, height,
7068                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7069                       GDI_STATE, GD_BUTTON_UNPRESSED,
7070                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y1,
7071                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y2,
7072                       GDI_EVENT_MASK, event_mask,
7073                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7074                       GDI_CALLBACK_ACTION, HandleControlButtons,
7075                       GDI_END);
7076
7077     if (gi == NULL)
7078       Fail("cannot create gadget");
7079
7080     level_editor_gadget[id] = gi;
7081   }
7082
7083   // create buttons for element list
7084   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
7085   {
7086     int type_id = i;
7087     int id = GADGET_ID_ELEMENTLIST_FIRST + i;
7088     int graphic = IMG_EDITOR_PALETTE_BUTTON;
7089     struct GraphicInfo *gd = &graphic_info[graphic];
7090     Bitmap *gd_bitmap = gd->bitmap;
7091     Bitmap *deco_bitmap;
7092     int deco_x, deco_y, deco_xpos, deco_ypos;
7093     int gd_x1 = gd->src_x;
7094     int gd_y1 = gd->src_y;
7095     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7096     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7097     int xx = i % ED_ELEMENTLIST_BUTTONS_HORIZ;
7098     int yy = i / ED_ELEMENTLIST_BUTTONS_HORIZ;
7099     int x = PX + ED_ELEMENTLIST_XPOS + xx * gd->width;
7100     int y = PY + ED_ELEMENTLIST_YPOS + yy * gd->height;
7101     int element = editor_elements[i];
7102     int tile_size = BUTTON_TILE_SIZE(editor.palette.tile_size);
7103     unsigned int event_mask = GD_EVENT_RELEASED;
7104
7105     getEditorGraphicSource(element, tile_size, &deco_bitmap, &deco_x, &deco_y);
7106
7107     deco_xpos = (gd->width  - tile_size) / 2;
7108     deco_ypos = (gd->height - tile_size) / 2;
7109
7110     gi = CreateGadget(GDI_CUSTOM_ID, id,
7111                       GDI_CUSTOM_TYPE_ID, type_id,
7112                       GDI_IMAGE_ID, graphic,
7113                       GDI_INFO_TEXT, getElementInfoText(element),
7114                       GDI_X, x,
7115                       GDI_Y, y,
7116                       GDI_WIDTH, gd->width,
7117                       GDI_HEIGHT, gd->height,
7118                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7119                       GDI_STATE, GD_BUTTON_UNPRESSED,
7120                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y1,
7121                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y2,
7122                       GDI_DECORATION_DESIGN, deco_bitmap, deco_x, deco_y,
7123                       GDI_DECORATION_POSITION, deco_xpos, deco_ypos,
7124                       GDI_DECORATION_SIZE, tile_size, tile_size,
7125                       GDI_DECORATION_SHIFTING, 1, 1,
7126                       GDI_EVENT_MASK, event_mask,
7127                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7128                       GDI_CALLBACK_ACTION, HandleControlButtons,
7129                       GDI_END);
7130
7131     if (gi == NULL)
7132       Fail("cannot create gadget");
7133
7134     level_editor_gadget[id] = gi;
7135   }
7136 }
7137
7138 static void CreateCounterButtons(void)
7139 {
7140   int max_infotext_len = getMaxInfoTextLength();
7141   int i;
7142
7143   for (i = 0; i < ED_NUM_COUNTERBUTTONS; i++)
7144   {
7145     int type_id = counterbutton_info[i].gadget_type_id;
7146     int x = SX + ED_SETTINGS_X(counterbutton_info[i].x); // down count button
7147     int y = SY + ED_SETTINGS_Y(counterbutton_info[i].y);
7148     int j;
7149
7150     if (type_id != i)
7151       Fail("'counterbutton_info' structure corrupted at index %d -- please fix", i);
7152
7153     // determine horizontal position to the right of specified gadget
7154     if (counterbutton_info[i].gadget_id_align != GADGET_ID_NONE)
7155       x = (right_gadget_border[counterbutton_info[i].gadget_id_align] +
7156            ED_GADGET_TEXT_DISTANCE);
7157
7158     // determine horizontal offset for leading text
7159     if (counterbutton_info[i].text_left != NULL)
7160       x += getTextWidthForGadget(counterbutton_info[i].text_left);
7161
7162     for (j = 0; j < 2; j++)
7163     {
7164       struct GadgetInfo *gi;
7165       int id = (j == 0 ?
7166                 counterbutton_info[i].gadget_id_down :
7167                 counterbutton_info[i].gadget_id_up);
7168       int graphic;
7169       struct GraphicInfo *gd;
7170       int gd_x1, gd_x2, gd_y1, gd_y2;
7171       unsigned int event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
7172       char infotext[max_infotext_len + 1];
7173
7174       if (i == ED_COUNTER_ID_SELECT_LEVEL)
7175       {
7176         graphic = (j == 0 ?
7177                    IMG_GFX_EDITOR_BUTTON_PREV_LEVEL :
7178                    IMG_GFX_EDITOR_BUTTON_NEXT_LEVEL);
7179
7180         event_mask |= GD_EVENT_RELEASED;
7181
7182         if (j == 0)
7183         {
7184           x = DX + editor.button.prev_level.x;
7185           y = DY + editor.button.prev_level.y;
7186         }
7187         else
7188         {
7189           x = DX + editor.button.next_level.x;
7190           y = DY + editor.button.next_level.y;
7191         }
7192       }
7193       else
7194       {
7195         graphic = (j == 0 ?
7196                    IMG_EDITOR_COUNTER_DOWN :
7197                    IMG_EDITOR_COUNTER_UP);
7198       }
7199
7200       gd = &graphic_info[graphic];
7201
7202       gd_x1 = gd->src_x;
7203       gd_y1 = gd->src_y;
7204       gd_x2 = gd->src_x + gd->pressed_xoffset;
7205       gd_y2 = gd->src_y + gd->pressed_yoffset;
7206
7207       sprintf(infotext, "%s counter value by 1, 5 or 10",
7208               (j == 0 ? "decrease" : "increase"));
7209
7210       gi = CreateGadget(GDI_CUSTOM_ID, id,
7211                         GDI_CUSTOM_TYPE_ID, type_id,
7212                         GDI_IMAGE_ID, graphic,
7213                         GDI_INFO_TEXT, infotext,
7214                         GDI_X, x,
7215                         GDI_Y, y,
7216                         GDI_WIDTH, gd->width,
7217                         GDI_HEIGHT, gd->height,
7218                         GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7219                         GDI_STATE, GD_BUTTON_UNPRESSED,
7220                         GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7221                         GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7222                         GDI_EVENT_MASK, event_mask,
7223                         GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7224                         GDI_CALLBACK_ACTION, HandleCounterButtons,
7225                         GDI_END);
7226
7227       if (gi == NULL)
7228         Fail("cannot create gadget");
7229
7230       level_editor_gadget[id] = gi;
7231       right_gadget_border[id] =
7232         getRightGadgetBorder(gi, counterbutton_info[i].text_right);
7233
7234       x += gi->width + ED_GADGET_SMALL_DISTANCE;        // text count button
7235
7236       if (j == 0)
7237       {
7238         int font_type = FONT_INPUT_1;
7239         int font_type_active = FONT_INPUT_1_ACTIVE;
7240
7241         id = counterbutton_info[i].gadget_id_text;
7242
7243         event_mask = GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
7244
7245         if (i == ED_COUNTER_ID_SELECT_LEVEL)
7246         {
7247           graphic = IMG_GFX_EDITOR_INPUT_LEVEL_NUMBER;
7248
7249           font_type = FONT_LEVEL_NUMBER;
7250           font_type_active = FONT_LEVEL_NUMBER_ACTIVE;
7251
7252           x = DX + editor.input.level_number.x;
7253           y = DY + editor.input.level_number.y;
7254         }
7255         else
7256         {
7257           graphic = IMG_EDITOR_COUNTER_INPUT;
7258         }
7259
7260         gd = &graphic_info[graphic];
7261
7262         gd_x1 = gd->src_x;
7263         gd_y1 = gd->src_y;
7264         gd_x2 = gd->src_x + gd->active_xoffset;
7265         gd_y2 = gd->src_y + gd->active_yoffset;
7266
7267         gi = CreateGadget(GDI_CUSTOM_ID, id,
7268                           GDI_CUSTOM_TYPE_ID, type_id,
7269                           GDI_IMAGE_ID, graphic,
7270                           GDI_INFO_TEXT, "enter counter value",
7271                           GDI_X, x,
7272                           GDI_Y, y,
7273                           GDI_TYPE, GD_TYPE_TEXT_INPUT_NUMERIC,
7274                           GDI_NUMBER_VALUE, 0,
7275                           GDI_NUMBER_MIN, counterbutton_info[i].min_value,
7276                           GDI_NUMBER_MAX, counterbutton_info[i].max_value,
7277                           GDI_TEXT_SIZE, 3,     // minimal counter text size
7278                           GDI_TEXT_FONT, font_type,
7279                           GDI_TEXT_FONT_ACTIVE, font_type_active,
7280                           GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7281                           GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7282                           GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7283                           GDI_DESIGN_WIDTH, gd->width,
7284                           GDI_EVENT_MASK, event_mask,
7285                           GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7286                           GDI_CALLBACK_ACTION, HandleCounterButtons,
7287                           GDI_END);
7288
7289         if (gi == NULL)
7290           Fail("cannot create gadget");
7291
7292         level_editor_gadget[id] = gi;
7293         right_gadget_border[id] =
7294           getRightGadgetBorder(gi, counterbutton_info[i].text_right);
7295
7296         x += gi->width + ED_GADGET_SMALL_DISTANCE;      // up count button
7297       }
7298     }
7299   }
7300 }
7301
7302 static void CreateDrawingAreas(void)
7303 {
7304   int i;
7305
7306   // these values are not constant, but can change at runtime
7307   drawingarea_info[ED_DRAWING_ID_DRAWING_LEVEL].area_xsize = MAX_ED_FIELDX;
7308   drawingarea_info[ED_DRAWING_ID_DRAWING_LEVEL].area_ysize = MAX_ED_FIELDY;
7309
7310   for (i = 0; i < ED_NUM_DRAWING_AREAS; i++)
7311   {
7312     struct GadgetInfo *gi;
7313     int id = drawingarea_info[i].gadget_id;
7314     int type_id = drawingarea_info[i].gadget_type_id;
7315     int x = SX + ED_AREA_SETTINGS_X(drawingarea_info[i]);
7316     int y = SY + ED_AREA_SETTINGS_Y(drawingarea_info[i]);
7317     int area_xsize = drawingarea_info[i].area_xsize;
7318     int area_ysize = drawingarea_info[i].area_ysize;
7319     int item_size = (id == GADGET_ID_DRAWING_LEVEL ?
7320                      ed_tilesize : ED_DRAWINGAREA_TILE_SIZE);
7321     unsigned int event_mask =
7322       GD_EVENT_PRESSED | GD_EVENT_RELEASED | GD_EVENT_MOVING |
7323       GD_EVENT_OFF_BORDERS | GD_EVENT_PIXEL_PRECISE;
7324
7325     if (type_id != i)
7326       Fail("'drawingarea_info' structure corrupted at index %d -- please fix", i);
7327
7328     // determine horizontal position to the right of specified gadget
7329     if (drawingarea_info[i].gadget_id_align != GADGET_ID_NONE)
7330       x = (right_gadget_border[drawingarea_info[i].gadget_id_align] +
7331            ED_DRAWINGAREA_TEXT_DISTANCE);
7332
7333     // determine horizontal offset for leading text
7334     if (drawingarea_info[i].text_left != NULL)
7335       x += getTextWidthForDrawingArea(drawingarea_info[i].text_left);
7336
7337     gi = CreateGadget(GDI_CUSTOM_ID, id,
7338                       GDI_CUSTOM_TYPE_ID, type_id,
7339                       GDI_X, x,
7340                       GDI_Y, y,
7341                       GDI_TYPE, GD_TYPE_DRAWING_AREA,
7342                       GDI_AREA_SIZE, area_xsize, area_ysize,
7343                       GDI_ITEM_SIZE, item_size, item_size,
7344                       GDI_EVENT_MASK, event_mask,
7345                       GDI_CALLBACK_INFO, HandleDrawingAreaInfo,
7346                       GDI_CALLBACK_ACTION, HandleDrawingAreas,
7347                       GDI_END);
7348
7349     if (gi == NULL)
7350       Fail("cannot create gadget");
7351
7352     level_editor_gadget[id] = gi;
7353     right_gadget_border[id] =
7354       getRightGadgetBorder(gi, drawingarea_info[i].text_right);
7355   }
7356 }
7357
7358 static void CreateTextInputGadgets(void)
7359 {
7360   struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_INPUT_TEXT];
7361   int max_infotext_len = getMaxInfoTextLength();
7362   int i;
7363
7364   for (i = 0; i < ED_NUM_TEXTINPUT; i++)
7365   {
7366     int gd_x1 = gd->src_x;
7367     int gd_y1 = gd->src_y;
7368     int gd_x2 = gd->src_x + gd->active_xoffset;
7369     int gd_y2 = gd->src_y + gd->active_yoffset;
7370     struct GadgetInfo *gi;
7371     unsigned int event_mask = GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
7372     char infotext[MAX_OUTPUT_LINESIZE + 1];
7373     int id = textinput_info[i].gadget_id;
7374     int type_id = textinput_info[i].gadget_type_id;
7375     int x, y;
7376
7377     if (type_id != i)
7378       Fail("'textinput_info' structure corrupted at index %d -- please fix", i);
7379
7380     if (i == ED_TEXTINPUT_ID_ELEMENT_NAME)
7381     {
7382       int element_border = graphic_info[IMG_EDITOR_ELEMENT_BORDER].border_size;
7383       int border_size = gd->border_size;
7384       int font_nr = FONT_INPUT_1;
7385       int font_height = getFontHeight(font_nr);
7386       int xoffset = element_border + TILEX + element_border + 3 * border_size;
7387       int yoffset = element_border + (TILEY - font_height) / 2;
7388
7389       x = (editor.settings.element_name.x != -1 ?
7390            editor.settings.element_name.x :
7391            editor.settings.element_graphic.x + xoffset) - border_size;
7392       y = (editor.settings.element_name.y != -1 ?
7393            editor.settings.element_name.y :
7394            editor.settings.element_graphic.y + yoffset) - border_size;
7395     }
7396     else
7397     {
7398       x = ED_SETTINGS_X(textinput_info[i].x);
7399       y = ED_SETTINGS_Y(textinput_info[i].y);
7400     }
7401
7402     sprintf(infotext, "Enter %s", textinput_info[i].infotext);
7403     infotext[max_infotext_len] = '\0';
7404
7405     gi = CreateGadget(GDI_CUSTOM_ID, id,
7406                       GDI_CUSTOM_TYPE_ID, type_id,
7407                       GDI_INFO_TEXT, infotext,
7408                       GDI_X, SX + x,
7409                       GDI_Y, SY + y,
7410                       GDI_TYPE, GD_TYPE_TEXT_INPUT_ALPHANUMERIC,
7411                       GDI_TEXT_VALUE, textinput_info[i].value,
7412                       GDI_TEXT_SIZE, textinput_info[i].size,
7413                       GDI_TEXT_FONT, FONT_INPUT_1,
7414                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_1_ACTIVE,
7415                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7416                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7417                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7418                       GDI_DESIGN_WIDTH, gd->width,
7419                       GDI_EVENT_MASK, event_mask,
7420                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7421                       GDI_CALLBACK_ACTION, HandleTextInputGadgets,
7422                       GDI_END);
7423
7424     if (gi == NULL)
7425       Fail("cannot create gadget");
7426
7427     level_editor_gadget[id] = gi;
7428   }
7429 }
7430
7431 static void CreateTextAreaGadgets(void)
7432 {
7433   int max_infotext_len = getMaxInfoTextLength();
7434   int i;
7435
7436   for (i = 0; i < ED_NUM_TEXTAREAS; i++)
7437   {
7438     struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_INPUT_TEXTAREA];
7439     int gd_x1 = gd->src_x;
7440     int gd_y1 = gd->src_y;
7441     int gd_x2 = gd->src_x + gd->active_xoffset;
7442     int gd_y2 = gd->src_y + gd->active_yoffset;
7443     struct GadgetInfo *gi;
7444     unsigned int event_mask = GD_EVENT_TEXT_LEAVING;
7445     char infotext[MAX_OUTPUT_LINESIZE + 1];
7446     int id = textarea_info[i].gadget_id;
7447     int type_id = textarea_info[i].gadget_type_id;
7448     int area_xsize = textarea_info[i].xsize;
7449     int area_ysize = textarea_info[i].ysize;
7450
7451     if (type_id != i)
7452       Fail("'textarea_info' structure corrupted at index %d -- please fix", i);
7453
7454     sprintf(infotext, "Enter %s", textarea_info[i].infotext);
7455     infotext[max_infotext_len] = '\0';
7456
7457     gi = CreateGadget(GDI_CUSTOM_ID, id,
7458                       GDI_CUSTOM_TYPE_ID, type_id,
7459                       GDI_INFO_TEXT, infotext,
7460                       GDI_X, SX + ED_SETTINGS_X(textarea_info[i].x),
7461                       GDI_Y, SY + ED_SETTINGS_Y(textarea_info[i].y),
7462                       GDI_TYPE, GD_TYPE_TEXT_AREA,
7463                       GDI_AREA_SIZE, area_xsize, area_ysize,
7464                       GDI_TEXT_FONT, FONT_INPUT_1,
7465                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_1_ACTIVE,
7466                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7467                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7468                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7469                       GDI_DESIGN_WIDTH, gd->width,
7470                       GDI_EVENT_MASK, event_mask,
7471                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7472                       GDI_CALLBACK_ACTION, HandleTextAreaGadgets,
7473                       GDI_END);
7474
7475     if (gi == NULL)
7476       Fail("cannot create gadget");
7477
7478     level_editor_gadget[id] = gi;
7479   }
7480 }
7481
7482 static void CreateSelectboxGadgets(void)
7483 {
7484   int max_infotext_len = getMaxInfoTextLength();
7485   int i, j;
7486
7487   for (i = 0; i < ED_NUM_SELECTBOX; i++)
7488   {
7489     struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_SELECTBOX_INPUT];
7490     struct GraphicInfo *gd2 = &graphic_info[IMG_EDITOR_SELECTBOX_BUTTON];
7491     int gd_x1 = gd->src_x;
7492     int gd_y1 = gd->src_y;
7493     int gd_x2 = gd->src_x + gd->active_xoffset;
7494     int gd_y2 = gd->src_y + gd->active_yoffset;
7495     int selectbox_button_xsize = gd2->width;
7496     struct GadgetInfo *gi;
7497     char infotext[MAX_OUTPUT_LINESIZE + 1];
7498     int id = selectbox_info[i].gadget_id;
7499     int type_id = selectbox_info[i].gadget_type_id;
7500     int x = SX + ED_SETTINGS_X(selectbox_info[i].x);
7501     int y = SY + ED_SETTINGS_Y(selectbox_info[i].y);
7502     unsigned int event_mask =
7503       GD_EVENT_RELEASED | GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
7504
7505     if (type_id != i)
7506       Fail("'selectbox_info' structure corrupted at index %d -- please fix", i);
7507
7508     if (selectbox_info[i].size == -1)   // dynamically determine size
7509     {
7510       // (we cannot use -1 for uninitialized values if we directly compare
7511       // with results from strlen(), because the '<' and '>' operation will
7512       // implicitely cast -1 to an unsigned integer value!)
7513       selectbox_info[i].size = 0;
7514
7515       for (j = 0; selectbox_info[i].options[j].text != NULL; j++)
7516         if (strlen(selectbox_info[i].options[j].text) > selectbox_info[i].size)
7517           selectbox_info[i].size = strlen(selectbox_info[i].options[j].text);
7518
7519       selectbox_info[i].size++;         // add one character empty space
7520     }
7521
7522     // determine horizontal position to the right of specified gadget
7523     if (selectbox_info[i].gadget_id_align != GADGET_ID_NONE)
7524       x = (right_gadget_border[selectbox_info[i].gadget_id_align] +
7525            ED_GADGET_TEXT_DISTANCE);
7526
7527     // determine horizontal offset for leading text
7528     if (selectbox_info[i].text_left != NULL)
7529       x += getTextWidthForGadget(selectbox_info[i].text_left);
7530
7531     sprintf(infotext, "Select %s", selectbox_info[i].infotext);
7532     infotext[max_infotext_len] = '\0';
7533
7534     gi = CreateGadget(GDI_CUSTOM_ID, id,
7535                       GDI_CUSTOM_TYPE_ID, type_id,
7536                       GDI_INFO_TEXT, infotext,
7537                       GDI_X, x,
7538                       GDI_Y, y,
7539                       GDI_TYPE, GD_TYPE_SELECTBOX,
7540                       GDI_SELECTBOX_OPTIONS, selectbox_info[i].options,
7541                       GDI_SELECTBOX_CHAR_UNSELECTABLE, '[',
7542                       GDI_TEXT_SIZE, selectbox_info[i].size,
7543                       GDI_TEXT_FONT, FONT_INPUT_1,
7544                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_1_ACTIVE,
7545                       GDI_TEXT_FONT_UNSELECTABLE, FONT_TEXT_1,
7546                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7547                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7548                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7549                       GDI_BORDER_SIZE_SELECTBUTTON, selectbox_button_xsize,
7550                       GDI_DESIGN_WIDTH, gd->width,
7551                       GDI_EVENT_MASK, event_mask,
7552                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7553                       GDI_CALLBACK_ACTION, HandleSelectboxGadgets,
7554                       GDI_END);
7555
7556     if (gi == NULL)
7557       Fail("cannot create gadget");
7558
7559     level_editor_gadget[id] = gi;
7560     right_gadget_border[id] =
7561       getRightGadgetBorder(gi, selectbox_info[i].text_right);
7562   }
7563 }
7564
7565 static void CreateTextbuttonGadgets(void)
7566 {
7567   int max_infotext_len = getMaxInfoTextLength();
7568   int i;
7569
7570   for (i = 0; i < ED_NUM_TEXTBUTTONS; i++)
7571   {
7572     int id = textbutton_info[i].gadget_id;
7573     int type_id = textbutton_info[i].gadget_type_id;
7574     int is_tab_button =
7575       ((id >= GADGET_ID_LEVELCONFIG_LEVEL && id <= GADGET_ID_LEVELCONFIG_ENGINE) ||
7576        (id >= GADGET_ID_PROPERTIES_INFO && id <= GADGET_ID_PROPERTIES_CHANGE));
7577     int graphic =
7578       (is_tab_button ? IMG_EDITOR_TABBUTTON : IMG_EDITOR_TEXTBUTTON);
7579     int gadget_distance =
7580       (is_tab_button ? ED_GADGET_SMALL_DISTANCE : ED_GADGET_TEXT_DISTANCE);
7581     struct GraphicInfo *gd = &graphic_info[graphic];
7582     int gd_x1 = gd->src_x;
7583     int gd_y1 = gd->src_y;
7584     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7585     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7586     int gd_x1a = gd->src_x + gd->active_xoffset;
7587     int gd_y1a = gd->src_y + gd->active_yoffset;
7588     int border_xsize = gd->border_size + gd->draw_xoffset;
7589     int border_ysize = gd->border_size;
7590     struct GadgetInfo *gi;
7591     unsigned int event_mask = GD_EVENT_RELEASED;
7592     char infotext[MAX_OUTPUT_LINESIZE + 1];
7593     int x = SX + ED_SETTINGS_X(textbutton_info[i].x);
7594     int y = SY + ED_SETTINGS_Y(textbutton_info[i].y);
7595
7596     if (type_id != i)
7597       Fail("'textbutton_info' structure corrupted at index %d -- please fix", i);
7598
7599     if (textbutton_info[i].size == -1)  // dynamically determine size
7600       textbutton_info[i].size = strlen(textbutton_info[i].text);
7601
7602     sprintf(infotext, "%s", textbutton_info[i].infotext);
7603     infotext[max_infotext_len] = '\0';
7604
7605     // determine horizontal position to the right of specified gadget
7606     if (textbutton_info[i].gadget_id_align != GADGET_ID_NONE)
7607     {
7608       int gadget_id_align = textbutton_info[i].gadget_id_align;
7609
7610       x = right_gadget_border[gadget_id_align] + gadget_distance;
7611
7612       if (textbutton_info[i].y == -1)
7613         y = level_editor_gadget[gadget_id_align]->y;
7614     }
7615
7616     // determine horizontal offset for leading text
7617     if (textbutton_info[i].text_left != NULL)
7618       x += getTextWidthForGadget(textbutton_info[i].text_left);
7619
7620     gi = CreateGadget(GDI_CUSTOM_ID, id,
7621                       GDI_CUSTOM_TYPE_ID, type_id,
7622                       GDI_IMAGE_ID, graphic,
7623                       GDI_INFO_TEXT, infotext,
7624                       GDI_X, x,
7625                       GDI_Y, y,
7626                       GDI_TYPE, GD_TYPE_TEXT_BUTTON,
7627                       GDI_TEXT_VALUE, textbutton_info[i].text,
7628                       GDI_TEXT_SIZE, textbutton_info[i].size,
7629                       GDI_TEXT_FONT, FONT_INPUT_2,
7630                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_2_ACTIVE,
7631                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7632                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7633                       GDI_ALT_DESIGN_UNPRESSED, gd->bitmap, gd_x1a, gd_y1a,
7634                       GDI_BORDER_SIZE, border_xsize, border_ysize,
7635                       GDI_DESIGN_WIDTH, gd->width,
7636                       GDI_DECORATION_SHIFTING, 1, 1,
7637                       GDI_EVENT_MASK, event_mask,
7638                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7639                       GDI_CALLBACK_ACTION, HandleTextbuttonGadgets,
7640                       GDI_END);
7641
7642     if (gi == NULL)
7643       Fail("cannot create gadget");
7644
7645     level_editor_gadget[id] = gi;
7646     right_gadget_border[id] =
7647       getRightGadgetBorder(gi, textbutton_info[i].text_right);
7648   }
7649 }
7650
7651 static void CreateGraphicbuttonGadgets(void)
7652 {
7653   struct GadgetInfo *gi;
7654   int i;
7655
7656   // create buttons for scrolling of drawing area and element list
7657   for (i = 0; i < ED_NUM_GRAPHICBUTTONS; i++)
7658   {
7659     int id = graphicbutton_info[i].gadget_id;
7660     int type_id = graphicbutton_info[i].gadget_type_id;
7661     int x = SX + ED_SETTINGS_X(graphicbutton_info[i].x);
7662     int y = SY + ED_SETTINGS_Y(graphicbutton_info[i].y);
7663     int graphic = graphicbutton_info[i].graphic;
7664     struct GraphicInfo *gd = &graphic_info[graphic];
7665     int gd_x1 = gd->src_x;
7666     int gd_y1 = gd->src_y;
7667     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7668     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7669     unsigned int event_mask = GD_EVENT_RELEASED;
7670
7671     if (type_id != i)
7672       Fail("'graphicbutton_info' structure corrupted at index %d -- please fix", i);
7673
7674     // determine horizontal position to the right of specified gadget
7675     if (graphicbutton_info[i].gadget_id_align != GADGET_ID_NONE)
7676       x = (right_gadget_border[graphicbutton_info[i].gadget_id_align] +
7677            ED_GADGET_TEXT_DISTANCE);
7678
7679     // determine horizontal offset for leading text
7680     if (graphicbutton_info[i].text_left != NULL)
7681       x += getTextWidthForGadget(graphicbutton_info[i].text_left);
7682
7683     gi = CreateGadget(GDI_CUSTOM_ID, id,
7684                       GDI_CUSTOM_TYPE_ID, type_id,
7685                       GDI_IMAGE_ID, graphic,
7686                       GDI_INFO_TEXT, graphicbutton_info[i].infotext,
7687                       GDI_X, x,
7688                       GDI_Y, y,
7689                       GDI_WIDTH, gd->width,
7690                       GDI_HEIGHT, gd->height,
7691                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
7692                       GDI_STATE, GD_BUTTON_UNPRESSED,
7693                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7694                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7695                       GDI_EVENT_MASK, event_mask,
7696                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7697                       GDI_CALLBACK_ACTION, HandleGraphicbuttonGadgets,
7698                       GDI_END);
7699
7700     if (gi == NULL)
7701       Fail("cannot create gadget");
7702
7703     level_editor_gadget[id] = gi;
7704     right_gadget_border[id] =
7705       getRightGadgetBorder(gi, graphicbutton_info[i].text_right);
7706   }
7707 }
7708
7709 static void CreateScrollbarGadgets(void)
7710 {
7711   int i;
7712
7713   // these values are not constant, but can change at runtime
7714   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].x =
7715     SX + ED_SCROLL_HORIZONTAL_XPOS;
7716   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].y =
7717     SY + ED_SCROLL_HORIZONTAL_YPOS;
7718   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].width =
7719     ED_SCROLL_HORIZONTAL_XSIZE;
7720   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].height =
7721     ED_SCROLL_HORIZONTAL_YSIZE;
7722   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].wheel_x      = SX;
7723   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].wheel_y      = SY;
7724   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].wheel_width  = SXSIZE;
7725   scrollbar_pos[ED_SCROLLBAR_ID_AREA_HORIZONTAL].wheel_height = SYSIZE;
7726
7727   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].x =
7728     SX + ED_SCROLL_VERTICAL_XPOS;
7729   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].y =
7730     SY + ED_SCROLL_VERTICAL_YPOS;
7731   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].width =
7732     ED_SCROLL_VERTICAL_XSIZE;
7733   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].height =
7734     ED_SCROLL_VERTICAL_YSIZE;
7735   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].wheel_x      = SX;
7736   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].wheel_y      = SY;
7737   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].wheel_width  = SXSIZE;
7738   scrollbar_pos[ED_SCROLLBAR_ID_AREA_VERTICAL].wheel_height = SYSIZE;
7739
7740   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].x =
7741     PX + ED_SCROLL2_VERTICAL_XPOS;
7742   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].y =
7743     PY + ED_SCROLL2_VERTICAL_YPOS;
7744   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].width =
7745     ED_SCROLL2_VERTICAL_XSIZE;
7746   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].height =
7747     ED_SCROLL2_VERTICAL_YSIZE;
7748   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].wheel_x = PX;
7749   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].wheel_y = PY;
7750   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].wheel_width  = PXSIZE;
7751   scrollbar_pos[ED_SCROLLBAR_ID_LIST_VERTICAL].wheel_height = PYSIZE;
7752
7753   for (i = 0; i < ED_NUM_SCROLLBARS; i++)
7754   {
7755     int id = scrollbar_info[i].gadget_id;
7756     int type_id = scrollbar_info[i].gadget_type_id;
7757     int graphic = scrollbar_info[i].graphic;
7758     struct GraphicInfo *gd = &graphic_info[graphic];
7759     int gd_x1 = gd->src_x;
7760     int gd_y1 = gd->src_y;
7761     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7762     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7763     struct GadgetInfo *gi;
7764     int items_max, items_visible, item_position;
7765     unsigned int event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
7766
7767     if (type_id != i)
7768       Fail("'scrollbar_info' structure corrupted at index %d -- please fix", i);
7769
7770     if (i == ED_SCROLLBAR_ID_LIST_VERTICAL)
7771     {
7772       items_max = num_editor_elements / ED_ELEMENTLIST_BUTTONS_HORIZ;
7773       items_visible = ED_ELEMENTLIST_BUTTONS_VERT;
7774       item_position = element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ;
7775     }
7776     else        // drawing area scrollbars
7777     {
7778       if (scrollbar_info[i].type == GD_TYPE_SCROLLBAR_HORIZONTAL)
7779       {
7780         items_max = MAX(lev_fieldx + 2, ed_fieldx);
7781         items_visible = ed_fieldx;
7782         item_position = 0;
7783       }
7784       else
7785       {
7786         items_max = MAX(lev_fieldy + 2, ed_fieldy);
7787         items_visible = ed_fieldy;
7788         item_position = 0;
7789       }
7790     }
7791
7792     gi = CreateGadget(GDI_CUSTOM_ID, id,
7793                       GDI_CUSTOM_TYPE_ID, type_id,
7794                       GDI_IMAGE_ID, graphic,
7795                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
7796                       GDI_X, scrollbar_pos[i].x,
7797                       GDI_Y, scrollbar_pos[i].y,
7798                       GDI_WIDTH, scrollbar_pos[i].width,
7799                       GDI_HEIGHT, scrollbar_pos[i].height,
7800                       GDI_TYPE, scrollbar_info[i].type,
7801                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
7802                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
7803                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
7804                       GDI_WHEEL_AREA_X, scrollbar_pos[i].wheel_x,
7805                       GDI_WHEEL_AREA_Y, scrollbar_pos[i].wheel_y,
7806                       GDI_WHEEL_AREA_WIDTH, scrollbar_pos[i].wheel_width,
7807                       GDI_WHEEL_AREA_HEIGHT, scrollbar_pos[i].wheel_height,
7808                       GDI_STATE, GD_BUTTON_UNPRESSED,
7809                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7810                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7811                       GDI_BORDER_SIZE, gd->border_size, gd->border_size,
7812                       GDI_EVENT_MASK, event_mask,
7813                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7814                       GDI_CALLBACK_ACTION, HandleControlButtons,
7815                       GDI_END);
7816
7817     if (gi == NULL)
7818       Fail("cannot create gadget");
7819
7820     level_editor_gadget[id] = gi;
7821   }
7822 }
7823
7824 static void CreateCheckbuttonGadgets(void)
7825 {
7826   struct GadgetInfo *gi;
7827   int i;
7828
7829   for (i = 0; i < ED_NUM_CHECKBUTTONS; i++)
7830   {
7831     int id = checkbutton_info[i].gadget_id;
7832     int type_id = checkbutton_info[i].gadget_type_id;
7833     int graphic = (id == GADGET_ID_STICK_ELEMENT ? IMG_EDITOR_STICKYBUTTON :
7834                    IMG_EDITOR_CHECKBOX);
7835     struct GraphicInfo *gd = &graphic_info[graphic];
7836     int gd_x1 = gd->src_x;
7837     int gd_y1 = gd->src_y;
7838     int gd_x2 = gd->src_x + gd->pressed_xoffset;
7839     int gd_y2 = gd->src_y + gd->pressed_yoffset;
7840     int gd_x1a = gd->src_x + gd->active_xoffset;
7841     int gd_y1a = gd->src_y + gd->active_yoffset;
7842     int gd_x2a = gd->src_x + gd->active_xoffset + gd->pressed_xoffset;
7843     int gd_y2a = gd->src_y + gd->active_yoffset + gd->pressed_yoffset;
7844     int x = SX + ED_SETTINGS_X(checkbutton_info[i].x);
7845     int y = SY + ED_SETTINGS_Y(checkbutton_info[i].y);
7846     unsigned int event_mask = GD_EVENT_PRESSED;
7847
7848     if (type_id != i)
7849       Fail("'checkbutton_info' structure corrupted at index %d -- please fix", i);
7850
7851     // determine horizontal position to the right of specified gadget
7852     if (checkbutton_info[i].gadget_id_align != GADGET_ID_NONE)
7853       x = (right_gadget_border[checkbutton_info[i].gadget_id_align] +
7854            ED_GADGET_TEXT_DISTANCE);
7855
7856     // determine horizontal offset for leading text
7857     if (checkbutton_info[i].text_left != NULL)
7858       x += getTextWidthForGadget(checkbutton_info[i].text_left);
7859
7860     gi = CreateGadget(GDI_CUSTOM_ID, id,
7861                       GDI_CUSTOM_TYPE_ID, type_id,
7862                       GDI_IMAGE_ID, graphic,
7863                       GDI_INFO_TEXT, checkbutton_info[i].infotext,
7864                       GDI_X, x,
7865                       GDI_Y, y,
7866                       GDI_WIDTH, gd->width,
7867                       GDI_HEIGHT, gd->height,
7868                       GDI_TYPE, GD_TYPE_CHECK_BUTTON,
7869                       GDI_CHECKED, *checkbutton_info[i].value,
7870                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7871                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7872                       GDI_ALT_DESIGN_UNPRESSED, gd->bitmap, gd_x1a, gd_y1a,
7873                       GDI_ALT_DESIGN_PRESSED, gd->bitmap, gd_x2a, gd_y2a,
7874                       GDI_EVENT_MASK, event_mask,
7875                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7876                       GDI_CALLBACK_ACTION, HandleCheckbuttons,
7877                       GDI_END);
7878
7879     if (gi == NULL)
7880       Fail("cannot create gadget");
7881
7882     level_editor_gadget[id] = gi;
7883     right_gadget_border[id] =
7884       getRightGadgetBorder(gi, checkbutton_info[i].text_right);
7885   }
7886 }
7887
7888 static void CreateRadiobuttonGadgets(void)
7889 {
7890   int graphic = IMG_EDITOR_RADIOBUTTON;
7891   struct GraphicInfo *gd = &graphic_info[graphic];
7892   int gd_x1 = gd->src_x;
7893   int gd_y1 = gd->src_y;
7894   int gd_x2 = gd->src_x + gd->pressed_xoffset;
7895   int gd_y2 = gd->src_y + gd->pressed_yoffset;
7896   int gd_x1a = gd->src_x + gd->active_xoffset;
7897   int gd_y1a = gd->src_y + gd->active_yoffset;
7898   int gd_x2a = gd->src_x + gd->active_xoffset + gd->pressed_xoffset;
7899   int gd_y2a = gd->src_y + gd->active_yoffset + gd->pressed_yoffset;
7900   struct GadgetInfo *gi;
7901   int i;
7902
7903   for (i = 0; i < ED_NUM_RADIOBUTTONS; i++)
7904   {
7905     int id = radiobutton_info[i].gadget_id;
7906     int type_id = radiobutton_info[i].gadget_type_id;
7907     int x = SX + ED_SETTINGS_X(radiobutton_info[i].x);
7908     int y = SY + ED_SETTINGS_Y(radiobutton_info[i].y);
7909     unsigned int event_mask = GD_EVENT_PRESSED;
7910
7911     if (type_id != i)
7912       Fail("'radiobutton_info' structure corrupted at index %d -- please fix", i);
7913
7914     int checked =
7915       (*radiobutton_info[i].value == radiobutton_info[i].checked_value);
7916
7917     // determine horizontal position to the right of specified gadget
7918     if (radiobutton_info[i].gadget_id_align != GADGET_ID_NONE)
7919       x = (right_gadget_border[radiobutton_info[i].gadget_id_align] +
7920            ED_GADGET_TEXT_DISTANCE);
7921
7922     // determine horizontal offset for leading text
7923     if (radiobutton_info[i].text_left != NULL)
7924       x += getTextWidthForGadget(radiobutton_info[i].text_left);
7925
7926     gi = CreateGadget(GDI_CUSTOM_ID, id,
7927                       GDI_CUSTOM_TYPE_ID, type_id,
7928                       GDI_IMAGE_ID, graphic,
7929                       GDI_INFO_TEXT, radiobutton_info[i].infotext,
7930                       GDI_X, x,
7931                       GDI_Y, y,
7932                       GDI_WIDTH, gd->width,
7933                       GDI_HEIGHT, gd->height,
7934                       GDI_TYPE, GD_TYPE_RADIO_BUTTON,
7935                       GDI_RADIO_NR, radiobutton_info[i].radio_button_nr,
7936                       GDI_CHECKED, checked,
7937                       GDI_DESIGN_UNPRESSED, gd->bitmap, gd_x1, gd_y1,
7938                       GDI_DESIGN_PRESSED, gd->bitmap, gd_x2, gd_y2,
7939                       GDI_ALT_DESIGN_UNPRESSED, gd->bitmap, gd_x1a, gd_y1a,
7940                       GDI_ALT_DESIGN_PRESSED, gd->bitmap, gd_x2a, gd_y2a,
7941                       GDI_EVENT_MASK, event_mask,
7942                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
7943                       GDI_CALLBACK_ACTION, HandleRadiobuttons,
7944                       GDI_END);
7945
7946     if (gi == NULL)
7947       Fail("cannot create gadget");
7948
7949     level_editor_gadget[id] = gi;
7950     right_gadget_border[id] =
7951       getRightGadgetBorder(gi, radiobutton_info[i].text_right);
7952   }
7953 }
7954
7955 void CreateLevelEditorGadgets(void)
7956 {
7957   // force EDITOR font inside level editor
7958   SetFontStatus(GAME_MODE_EDITOR);
7959
7960   // these values are not constant, but can change at runtime
7961   ed_fieldx = MAX_ED_FIELDX - 1;
7962   ed_fieldy = MAX_ED_FIELDY - 1;
7963
7964   num_editor_gadgets = NUM_EDITOR_GADGETS;
7965
7966   // Debug("editor", "allocating %d gadgets ...\n", num_editor_gadgets);
7967
7968   level_editor_gadget =
7969     checked_calloc(num_editor_gadgets * sizeof(struct GadgetInfo *));
7970   right_gadget_border =
7971     checked_calloc(num_editor_gadgets * sizeof(int));
7972
7973   // set number of empty (padding) element buttons to maximum number of buttons
7974   num_editor_el_empty = ED_NUM_ELEMENTLIST_BUTTONS;
7975
7976   editor_el_empty = checked_calloc(num_editor_el_empty * sizeof(int));
7977   editor_el_empty_ptr = editor_el_empty;
7978
7979   use_permanent_palette = !editor.palette.show_as_separate_screen;
7980
7981   InitGadgetScreenBorders(-1, INFOTEXT_YPOS);
7982
7983   ReinitializeElementList();
7984
7985   CreateControlButtons();
7986   CreateScrollbarGadgets();
7987
7988   // order of function calls is important because of cross-references
7989   CreateCheckbuttonGadgets();
7990   CreateCounterButtons();
7991   CreateRadiobuttonGadgets();
7992   CreateTextInputGadgets();
7993   CreateTextAreaGadgets();
7994   CreateSelectboxGadgets();
7995   CreateGraphicbuttonGadgets();
7996   CreateTextbuttonGadgets();
7997   CreateDrawingAreas();
7998
7999   ResetFontStatus();
8000 }
8001
8002 void FreeLevelEditorGadgets(void)
8003 {
8004   int i;
8005
8006   // Debug("editor", "freeing %d gadgets ...\n", num_editor_gadgets);
8007
8008   for (i = 0; i < num_editor_gadgets; i++)
8009   {
8010     FreeGadget(level_editor_gadget[i]);
8011
8012     level_editor_gadget[i] = NULL;
8013   }
8014
8015   checked_free(level_editor_gadget);
8016   checked_free(right_gadget_border);
8017
8018   checked_free(editor_el_empty);
8019 }
8020
8021 static void MapCounterButtons(int id)
8022 {
8023   int font_nr = FONT_TEXT_1;
8024   int font_height = getFontHeight(font_nr);
8025   int gadget_id_down = counterbutton_info[id].gadget_id_down;
8026   int gadget_id_text = counterbutton_info[id].gadget_id_text;
8027   int gadget_id_up   = counterbutton_info[id].gadget_id_up;
8028   struct GadgetInfo *gi_down = level_editor_gadget[gadget_id_down];
8029   struct GadgetInfo *gi_text = level_editor_gadget[gadget_id_text];
8030   struct GadgetInfo *gi_up   = level_editor_gadget[gadget_id_up];
8031   int xoffset_left = getTextWidthForGadget(counterbutton_info[id].text_left);
8032   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8033   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8034   int yoffset = (gi_down->height - font_height) / 2;
8035   int x_left = gi_down->x - xoffset_left;
8036   int x_right;  // set after gadget position was modified
8037   int y_above = gi_down->y - yoffset_above;
8038   int x = gi_down->x;
8039   int y;        // set after gadget position was modified
8040
8041   // counter limits must be changed first to prevent value truncation
8042   ModifyEditorCounterLimits(id, counterbutton_info[id].min_value,
8043                             counterbutton_info[id].max_value);
8044
8045   // right text position might have changed after setting position above
8046   x_right = gi_up->x + gi_up->width + xoffset_right;
8047
8048   ModifyEditorCounterValue(id, *counterbutton_info[id].value);
8049
8050   // set position for "value[1,2,3,4]" counter gadgets (score in most cases)
8051   if (id >= ED_COUNTER_ID_ELEMENT_VALUE1 &&
8052       id <= ED_COUNTER_ID_ELEMENT_VALUE4)
8053   {
8054     ModifyGadget(gi_down, GDI_Y,
8055                  SY + ED_SETTINGS_Y(counterbutton_info[id].y), GDI_END);
8056     ModifyGadget(gi_text, GDI_Y,
8057                  SY + ED_SETTINGS_Y(counterbutton_info[id].y), GDI_END);
8058     ModifyGadget(gi_up,   GDI_Y,
8059                  SY + ED_SETTINGS_Y(counterbutton_info[id].y), GDI_END);
8060   }
8061
8062   // vertical position might have changed after setting position above
8063   y = gi_up->y + yoffset;
8064
8065   if (counterbutton_info[id].text_above)
8066     DrawText(x, y_above, counterbutton_info[id].text_above, font_nr);
8067
8068   if (counterbutton_info[id].text_left)
8069     DrawText(x_left, y, counterbutton_info[id].text_left, font_nr);
8070
8071   if (counterbutton_info[id].text_right)
8072     DrawText(x_right, y, counterbutton_info[id].text_right, font_nr);
8073
8074   MapGadget(gi_down);
8075   MapGadget(gi_text);
8076   MapGadget(gi_up);
8077 }
8078
8079 static void MapControlButtons(void)
8080 {
8081   int counter_id;
8082   int i;
8083
8084   // map toolbox buttons (excluding special CE toolbox buttons)
8085   for (i = 0; i < ED_NUM_CTRL1_2_BUTTONS; i++)
8086     MapGadget(level_editor_gadget[i]);
8087
8088   // map toolbox buttons (element properties buttons)
8089   for (i = ED_NUM_CTRL1_4_BUTTONS; i < ED_NUM_CTRL1_7_BUTTONS; i++)
8090     MapGadget(level_editor_gadget[i]);
8091
8092   if (use_permanent_palette)
8093   {
8094     // map buttons to select elements
8095     for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
8096       MapGadget(level_editor_gadget[GADGET_ID_ELEMENTLIST_FIRST + i]);
8097     MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL]);
8098     MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_UP]);
8099     MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_DOWN]);
8100   }
8101
8102   // map buttons to select level
8103   counter_id = ED_COUNTER_ID_SELECT_LEVEL;
8104   counterbutton_info[counter_id].min_value = leveldir_current->first_level;
8105   counterbutton_info[counter_id].max_value = leveldir_current->last_level;
8106   MapCounterButtons(counter_id);
8107 }
8108
8109 static void MapDrawingArea(int id)
8110 {
8111   int font_nr = FONT_TEXT_1;
8112   int font_height = getFontHeight(font_nr);
8113   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
8114   int area_xsize = gi->drawing.area_xsize;
8115   int area_ysize = gi->drawing.area_ysize;
8116   int xoffset_left = getTextWidthForDrawingArea(drawingarea_info[id].text_left);
8117   int xoffset_below = getTextWidth(drawingarea_info[id].text_below, font_nr);
8118   int x_left  = gi->x - xoffset_left;
8119   int x_right = gi->x + gi->width + ED_DRAWINGAREA_TEXT_DISTANCE;
8120   int x_above = gi->x - ED_DRAWINGAREA_BORDER_SIZE;
8121   int x_below = gi->x + (gi->width - xoffset_below) / 2;
8122   int y_side  = gi->y + (gi->height - font_height) / 2;
8123   int y_above = gi->y - font_height - ED_DRAWINGAREA_TEXT_DISTANCE;
8124   int y_below = gi->y + gi->height + ED_DRAWINGAREA_TEXT_DISTANCE;
8125
8126   if (drawingarea_info[id].text_left)
8127     DrawText(x_left, y_side, drawingarea_info[id].text_left, font_nr);
8128
8129   if (drawingarea_info[id].text_right)
8130     DrawText(x_right, y_side, drawingarea_info[id].text_right, font_nr);
8131
8132   if (drawingarea_info[id].text_above)
8133     DrawText(x_above, y_above, drawingarea_info[id].text_above, font_nr);
8134
8135   if (drawingarea_info[id].text_below)
8136     DrawText(x_below, y_below, drawingarea_info[id].text_below, font_nr);
8137
8138   if (id != ED_DRAWING_ID_DRAWING_LEVEL)
8139   {
8140     DrawElementBorder(gi->x, gi->y,
8141                       area_xsize * ED_DRAWINGAREA_TILE_SIZE,
8142                       area_ysize * ED_DRAWINGAREA_TILE_SIZE, TRUE);
8143
8144     DrawDrawingArea(id);
8145   }
8146
8147   MapGadget(gi);
8148 }
8149
8150 static void MapTextInputGadget(int id)
8151 {
8152   int font_nr = FONT_TEXT_1;
8153   int font_height = getFontHeight(font_nr);
8154   struct GadgetInfo *gi = level_editor_gadget[textinput_info[id].gadget_id];
8155   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8156   int x_above = ED_SETTINGS_X(textinput_info[id].x);
8157   int y_above = ED_SETTINGS_Y(textinput_info[id].y) - yoffset_above;
8158
8159   if (textinput_info[id].text_above)
8160     DrawTextS(x_above, y_above, font_nr, textinput_info[id].text_above);
8161
8162   ModifyGadget(gi, GDI_TEXT_VALUE, textinput_info[id].value, GDI_END);
8163
8164   MapGadget(gi);
8165 }
8166
8167 static void MapTextAreaGadget(int id)
8168 {
8169   int font_nr = FONT_TEXT_1;
8170   int font_height = getFontHeight(font_nr);
8171   struct GadgetInfo *gi = level_editor_gadget[textarea_info[id].gadget_id];
8172   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8173   int x_above = ED_SETTINGS_X(textarea_info[id].x);
8174   int y_above = ED_SETTINGS_Y(textarea_info[id].y) - yoffset_above;
8175   char *text_above = textarea_info[id].text_above;
8176
8177   if (gi->textarea.cropped && textarea_info[id].text_above_cropped)
8178     text_above = textarea_info[id].text_above_cropped;
8179
8180   if (text_above)
8181     DrawTextS(x_above, y_above, font_nr, text_above);
8182
8183   ModifyGadget(gi, GDI_TEXT_VALUE, textarea_info[id].value, GDI_END);
8184
8185   MapGadget(gi);
8186 }
8187
8188 static void MapSelectboxGadget(int id)
8189 {
8190   int font_nr = FONT_TEXT_1;
8191   int font_height = getFontHeight(font_nr);
8192   struct GadgetInfo *gi = level_editor_gadget[selectbox_info[id].gadget_id];
8193   int xoffset_left = getTextWidthForGadget(selectbox_info[id].text_left);
8194   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8195   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8196   int yoffset = (gi->height - font_height) / 2;
8197   int x_left = gi->x - xoffset_left;
8198   int x_right = gi->x + gi->width + xoffset_right;
8199   int y_above = gi->y - yoffset_above;
8200   int x = gi->x;
8201   int y = gi->y + yoffset;
8202
8203   if (selectbox_info[id].text_above)
8204     DrawText(x, y_above, selectbox_info[id].text_above, font_nr);
8205
8206   if (selectbox_info[id].text_left)
8207     DrawText(x_left, y, selectbox_info[id].text_left, font_nr);
8208
8209   if (selectbox_info[id].text_right)
8210     DrawText(x_right, y, selectbox_info[id].text_right, font_nr);
8211
8212   ModifyEditorSelectboxValue(id, *selectbox_info[id].value);
8213
8214   MapGadget(gi);
8215 }
8216
8217 static void MapTextbuttonGadget(int id)
8218 {
8219   int font_nr = FONT_TEXT_1;
8220   int font_height = getFontHeight(font_nr);
8221   struct GadgetInfo *gi = level_editor_gadget[textbutton_info[id].gadget_id];
8222   int xoffset_left = getTextWidthForGadget(textbutton_info[id].text_left);
8223   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8224   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8225   int yoffset = (gi->height - font_height) / 2;
8226   int x_left = gi->x - xoffset_left;
8227   int x_right = gi->x + gi->width + xoffset_right;
8228   int y_above = gi->y - yoffset_above;
8229   int x = gi->x;
8230   int y = gi->y + yoffset;
8231
8232   // only show button to delete change pages when more than minimum pages
8233   if (id == ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE &&
8234       custom_element.num_change_pages == MIN_CHANGE_PAGES)
8235     return;
8236
8237   if (textbutton_info[id].text_above)
8238     DrawText(x, y_above, textbutton_info[id].text_above, font_nr);
8239
8240   if (textbutton_info[id].text_left)
8241     DrawText(x_left, y, textbutton_info[id].text_left, font_nr);
8242
8243   if (textbutton_info[id].text_right)
8244     DrawText(x_right, y, textbutton_info[id].text_right, font_nr);
8245
8246   MapGadget(gi);
8247 }
8248
8249 static void MapGraphicbuttonGadget(int id)
8250 {
8251   int font_nr = FONT_TEXT_1;
8252   int font_height = getFontHeight(font_nr);
8253   struct GadgetInfo *gi = level_editor_gadget[graphicbutton_info[id].gadget_id];
8254   int xoffset_left = getTextWidthForGadget(graphicbutton_info[id].text_left);
8255   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8256   int yoffset = (gi->height - font_height) / 2;
8257   int x_left = gi->x - xoffset_left;
8258   int x_right = gi->x + gi->width + xoffset_right;
8259   int y = gi->y + yoffset;
8260
8261   if (graphicbutton_info[id].text_left)
8262     DrawText(x_left, y, graphicbutton_info[id].text_left, font_nr);
8263
8264   if (graphicbutton_info[id].text_right)
8265     DrawText(x_right, y, graphicbutton_info[id].text_right, font_nr);
8266
8267   MapGadget(gi);
8268 }
8269
8270 static void MapRadiobuttonGadget(int id)
8271 {
8272   int font_nr = FONT_TEXT_1;
8273   int font_height = getFontHeight(font_nr);
8274   struct GadgetInfo *gi = level_editor_gadget[radiobutton_info[id].gadget_id];
8275   int xoffset_left = getTextWidthForGadget(checkbutton_info[id].text_left);
8276   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8277   int yoffset = (gi->height - font_height) / 2;
8278   int x_left = gi->x - xoffset_left;
8279   int x_right = gi->x + gi->width + xoffset_right;
8280   int y = gi->y + yoffset;
8281   boolean checked =
8282     (*radiobutton_info[id].value == radiobutton_info[id].checked_value);
8283
8284   if (radiobutton_info[id].text_left)
8285     DrawText(x_left, y, radiobutton_info[id].text_left, font_nr);
8286
8287   if (radiobutton_info[id].text_right)
8288     DrawText(x_right, y, radiobutton_info[id].text_right, font_nr);
8289
8290   ModifyGadget(gi, GDI_CHECKED, checked, GDI_END);
8291
8292   MapGadget(gi);
8293 }
8294
8295 static void MapCheckbuttonGadget(int id)
8296 {
8297   int font_nr = FONT_TEXT_1;
8298   int font_height = getFontHeight(font_nr);
8299   struct GadgetInfo *gi = level_editor_gadget[checkbutton_info[id].gadget_id];
8300   int xoffset_left = getTextWidthForGadget(checkbutton_info[id].text_left);
8301   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
8302   int yoffset_above = font_height + ED_GADGET_LINE_DISTANCE;
8303   int yoffset = (gi->height - font_height) / 2;
8304   int y_above = gi->y - yoffset_above;
8305   int x = gi->x;
8306   int x_left, x_right, y;       // set after gadget position was modified
8307
8308   // set position for gadgets with dynamically determined position
8309   if (checkbutton_info[id].x != -1)     // do not change dynamic positions
8310     ModifyGadget(gi, GDI_X, SX + ED_SETTINGS_X(checkbutton_info[id].x), GDI_END);
8311   ModifyGadget(gi, GDI_Y, SY + ED_SETTINGS_Y(checkbutton_info[id].y), GDI_END);
8312
8313   x_left = gi->x - xoffset_left;
8314   x_right = gi->x + gi->width + xoffset_right;
8315   y = gi->y + yoffset;
8316
8317   if (checkbutton_info[id].text_above)
8318     DrawText(x, y_above, checkbutton_info[id].text_above, font_nr);
8319
8320   if (checkbutton_info[id].text_left)
8321     DrawText(x_left, y, checkbutton_info[id].text_left, font_nr);
8322
8323   if (checkbutton_info[id].text_right)
8324     DrawText(x_right, y, checkbutton_info[id].text_right, font_nr);
8325
8326   ModifyGadget(gi, GDI_CHECKED, *checkbutton_info[id].value, GDI_END);
8327
8328   MapGadget(gi);
8329 }
8330
8331 static void MapMainDrawingArea(void)
8332 {
8333   boolean no_horizontal_scrollbar = (lev_fieldx + 2 <= ed_fieldx);
8334   boolean no_vertical_scrollbar = (lev_fieldy + 2 <= ed_fieldy);
8335   int i;
8336
8337   if (suppressBorderElement())
8338   {
8339     no_horizontal_scrollbar = (lev_fieldx <= ed_fieldx);
8340     no_vertical_scrollbar   = (lev_fieldy <= ed_fieldy);
8341   }
8342
8343   for (i = ED_SCROLLBUTTON_ID_AREA_FIRST; i <= ED_SCROLLBUTTON_ID_AREA_LAST; i++)
8344   {
8345     if (((i == ED_SCROLLBUTTON_ID_AREA_LEFT ||
8346           i == ED_SCROLLBUTTON_ID_AREA_RIGHT) &&
8347          no_horizontal_scrollbar) ||
8348         ((i == ED_SCROLLBUTTON_ID_AREA_UP ||
8349           i == ED_SCROLLBUTTON_ID_AREA_DOWN) &&
8350          no_vertical_scrollbar))
8351       continue;
8352
8353     MapGadget(level_editor_gadget[scrollbutton_info[i].gadget_id]);
8354   }
8355
8356   for (i = ED_SCROLLBAR_ID_AREA_FIRST; i <= ED_SCROLLBAR_ID_AREA_LAST; i++)
8357   {
8358     if ((i == ED_SCROLLBAR_ID_AREA_HORIZONTAL && no_horizontal_scrollbar) ||
8359         (i == ED_SCROLLBAR_ID_AREA_VERTICAL && no_vertical_scrollbar))
8360       continue;
8361
8362     MapGadget(level_editor_gadget[scrollbar_info[i].gadget_id]);
8363   }
8364
8365   MapDrawingArea(ED_DRAWING_ID_DRAWING_LEVEL);
8366 }
8367
8368 static void MapOrUnmapLevelEditorToolboxCustomGadgets(boolean map)
8369 {
8370   int i;
8371
8372   for (i = 0; i < ED_NUM_CTRL_BUTTONS; i++)
8373   {
8374     if (i == GADGET_ID_CUSTOM_COPY_FROM ||
8375         i == GADGET_ID_CUSTOM_COPY_TO ||
8376         i == GADGET_ID_CUSTOM_EXCHANGE ||
8377         i == GADGET_ID_CUSTOM_COPY ||
8378         i == GADGET_ID_CUSTOM_PASTE)
8379     {
8380       if (map)
8381         MapGadget(level_editor_gadget[i]);
8382       else
8383         UnmapGadget(level_editor_gadget[i]);
8384     }
8385   }
8386 }
8387
8388 static void MapLevelEditorToolboxCustomGadgets(void)
8389 {
8390   MapOrUnmapLevelEditorToolboxCustomGadgets(TRUE);
8391 }
8392
8393 static void MapLevelEditorToolboxCustomGadgetsIfNeeded(void)
8394 {
8395   if (IS_CUSTOM_ELEMENT(properties_element) ||
8396       IS_GROUP_ELEMENT(properties_element) ||
8397       IS_EMPTY_ELEMENT(properties_element))
8398     MapLevelEditorToolboxCustomGadgets();
8399 }
8400
8401 static void UnmapLevelEditorToolboxCustomGadgets(void)
8402 {
8403   MapOrUnmapLevelEditorToolboxCustomGadgets(FALSE);
8404 }
8405
8406 static void MapOrUnmapLevelEditorToolboxDrawingGadgets(boolean map)
8407 {
8408   int i;
8409
8410   for (i = 0; i < ED_NUM_CTRL1_BUTTONS; i++)
8411   {
8412     if (i != GADGET_ID_SINGLE_ITEMS &&
8413         i != GADGET_ID_PICK_ELEMENT)
8414     {
8415       struct GadgetInfo *gi = level_editor_gadget[i];
8416
8417       if (map)
8418       {
8419         MapGadget(gi);
8420       }
8421       else
8422       {
8423         int graphic = IMG_EDITOR_NO_TOOLBOX_BUTTON;
8424         struct GraphicInfo *gd = &graphic_info[graphic];
8425
8426         UnmapGadget(gi);
8427
8428         BlitBitmap(gd->bitmap, drawto, gd->src_x, gd->src_y,
8429                    gi->width, gi->height, gi->x, gi->y);
8430
8431         redraw_mask |= REDRAW_DOOR_3;
8432       }
8433     }
8434   }
8435 }
8436
8437 static void MapLevelEditorToolboxDrawingGadgets(void)
8438 {
8439   MapOrUnmapLevelEditorToolboxDrawingGadgets(TRUE);
8440 }
8441
8442 static void UnmapLevelEditorToolboxDrawingGadgets(void)
8443 {
8444   MapOrUnmapLevelEditorToolboxDrawingGadgets(FALSE);
8445 }
8446
8447 static void UnmapDrawingArea(int id)
8448 {
8449   UnmapGadget(level_editor_gadget[drawingarea_info[id].gadget_id]);
8450 }
8451
8452 static void UnmapLevelEditorFieldGadgets(void)
8453 {
8454   int i;
8455
8456   for (i = 0; i < num_editor_gadgets; i++)
8457     if (IN_GFX_FIELD_FULL(level_editor_gadget[i]->x,
8458                           level_editor_gadget[i]->y))
8459       UnmapGadget(level_editor_gadget[i]);
8460 }
8461
8462 void UnmapLevelEditorGadgets(void)
8463 {
8464   int i;
8465
8466   for (i = 0; i < num_editor_gadgets; i++)
8467     UnmapGadget(level_editor_gadget[i]);
8468 }
8469
8470 static void ResetUndoBuffer(void)
8471 {
8472   undo_buffer_position = -1;
8473   undo_buffer_steps = -1;
8474   redo_buffer_steps = 0;
8475
8476   CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
8477
8478   level.changed = FALSE;
8479 }
8480
8481 static void DrawEditModeWindowExt(boolean remap_toolbox_gadgets)
8482 {
8483   if (remap_toolbox_gadgets)
8484   {
8485     ModifyEditorElementList();
8486     RedrawDrawingElements();
8487   }
8488
8489   if (edit_mode == ED_MODE_LEVELCONFIG)
8490     DrawLevelConfigWindow();
8491   else if (edit_mode == ED_MODE_PROPERTIES)
8492     DrawPropertiesWindow();
8493   else if (edit_mode == ED_MODE_PALETTE)
8494     DrawPaletteWindow();
8495   else  // edit_mode == ED_MODE_DRAWING
8496     DrawDrawingWindowExt(remap_toolbox_gadgets);
8497 }
8498
8499 static void DrawEditModeWindow(void)
8500 {
8501   DrawEditModeWindowExt(TRUE);
8502 }
8503
8504 static void DrawEditModeWindow_PlayfieldOnly(void)
8505 {
8506   DrawEditModeWindowExt(FALSE);
8507 }
8508
8509 static void ChangeEditModeWindow(int new_edit_mode)
8510 {
8511   edit_mode = (new_edit_mode != edit_mode ? new_edit_mode : ED_MODE_DRAWING);
8512
8513   DrawEditModeWindow();
8514 }
8515
8516 static boolean LevelChanged(void)
8517 {
8518   boolean field_changed = FALSE;
8519   int x, y;
8520
8521   for (y = 0; y < lev_fieldy; y++) 
8522     for (x = 0; x < lev_fieldx; x++)
8523       if (Tile[x][y] != level.field[x][y])
8524         field_changed = TRUE;
8525
8526   return (level.changed || field_changed);
8527 }
8528
8529 static boolean PrepareSavingIntoPersonalLevelSet(void)
8530 {
8531   static LevelDirTree *last_copied_leveldir = NULL;
8532   static LevelDirTree *last_written_leveldir = NULL;
8533   static int last_copied_level_nr = -1;
8534   static int last_written_level_nr = -1;
8535   LevelDirTree *leveldir_former = leveldir_current;
8536   int level_nr_former = level_nr;
8537   int new_level_nr;
8538
8539   // remember last mod/save so that for current session, we write
8540   // back to the same personal copy, asking only about overwrite.
8541   if (leveldir_current == last_copied_leveldir &&
8542       level_nr == last_copied_level_nr)
8543   {
8544     // "cd" to personal level set dir (as used when writing last copy)
8545     leveldir_current = last_written_leveldir;
8546     level_nr = last_written_level_nr;
8547
8548     return TRUE;
8549   }
8550
8551   if (!Request("This level is read-only! "
8552                "Save into personal level set?", REQ_ASK))
8553     return FALSE;
8554
8555   // "cd" to personal level set dir (for writing copy the first time)
8556   leveldir_current =
8557     getTreeInfoFromIdentifier(leveldir_first, getLoginName());
8558
8559   // this may happen if "setup.internal.create_user_levelset" is FALSE
8560   // or if file "levelinfo.conf" is missing in personal user level set
8561   if (leveldir_current == NULL)
8562   {
8563     Request("Cannot find personal level set?!", REQ_CONFIRM);
8564
8565     leveldir_current = leveldir_former;
8566
8567     return FALSE;
8568   }
8569
8570   // find unused level number
8571   for (new_level_nr = leveldir_current->first_level; ; new_level_nr++)
8572   {
8573     static char *level_filename = NULL;
8574
8575     setString(&level_filename, getDefaultLevelFilename(new_level_nr));
8576
8577     if (!fileExists(level_filename))
8578       break;
8579   }
8580
8581   last_copied_leveldir = leveldir_former;
8582   last_copied_level_nr = level_nr_former;
8583
8584   last_written_leveldir = leveldir_current;
8585   last_written_level_nr = level_nr = new_level_nr;
8586
8587   return TRUE;
8588 }
8589
8590 static void ModifyLevelConfigForSavingIntoPersonalLevelSet(char *former_name)
8591 {
8592   static char *filename_levelinfo = NULL, *mod_name = NULL;
8593   FILE *file;
8594
8595   // annotate this copy-and-mod in personal levelinfo.conf
8596   setString(&filename_levelinfo,
8597             getPath2(getCurrentLevelDir(), LEVELINFO_FILENAME));
8598
8599   if ((file = fopen(filename_levelinfo, MODE_APPEND)))
8600   {
8601     fprintf(file, "\n");
8602     fprintf(file, "# level %d was modified from:\n", level_nr);
8603     fprintf(file, "# - previous level set name:    %s\n",
8604             former_name);
8605     fprintf(file, "# - level within previous set:  %d \"%s\"\n",
8606             level.file_info.nr, level.name);
8607     fprintf(file, "# - previous author:            %s\n",
8608             level.author);
8609     fprintf(file, "# - previous save date:         ");
8610
8611     if (level.creation_date.src == DATE_SRC_LEVELFILE)
8612     {
8613       fprintf(file, "%04d-%02d-%02d\n",
8614               level.creation_date.year,
8615               level.creation_date.month,
8616               level.creation_date.day);
8617     }
8618     else
8619     {
8620       fprintf(file, "not recorded\n");
8621     }
8622
8623     fclose(file);
8624   }
8625
8626   if (level_nr > leveldir_current->last_level)
8627     UpdateUserLevelSet(getLoginName(), NULL, NULL, level_nr + 9);
8628
8629   // else: allow the save even if annotation failed
8630
8631   // now... spray graffiti on the old level vital statistics
8632   // user can change these; just trying to set a good baseline
8633
8634   // don't truncate names for fear of making offensive or silly:
8635   // long-named original author only recorded in levelinfo.conf.
8636   // try to fit "Joe after Bob", "Joe (ed.)", then just "Joe"
8637   if (!strEqual(level.author, leveldir_current->author))
8638   {
8639     setString(&mod_name, getStringCat3(leveldir_current->author,
8640                                        " after ", level.author));
8641
8642     if (strlen(mod_name) > MAX_LEVEL_AUTHOR_LEN)
8643       setString(&mod_name,
8644                 getStringCat2(leveldir_current->author, " (ed.)"));
8645
8646     if (strlen(mod_name) > MAX_LEVEL_AUTHOR_LEN)
8647       setString(&mod_name, leveldir_current->author);
8648
8649     strncpy(level.author, mod_name, MAX_LEVEL_AUTHOR_LEN);
8650
8651     // less worried about truncation here
8652     setString(&mod_name, getStringCat2("Mod: ", level.name));
8653     strncpy(level.name, mod_name, MAX_LEVEL_NAME_LEN);
8654   }
8655 }
8656
8657 static void CopyPlayfield(short src[MAX_LEV_FIELDX][MAX_LEV_FIELDY],
8658                           short dst[MAX_LEV_FIELDX][MAX_LEV_FIELDY])
8659 {
8660   int x, y;
8661
8662   for (x = 0; x < lev_fieldx; x++)
8663     for (y = 0; y < lev_fieldy; y++) 
8664       dst[x][y] = src[x][y];
8665 }
8666
8667 static int setSelectboxValue(int selectbox_id, int new_value)
8668 {
8669   int new_index_value = 0;
8670   int i;
8671
8672   for (i = 0; selectbox_info[selectbox_id].options[i].text != NULL; i++)
8673     if (selectbox_info[selectbox_id].options[i].value == new_value)
8674       new_index_value = i;
8675
8676   *selectbox_info[selectbox_id].value =
8677     selectbox_info[selectbox_id].options[new_index_value].value;
8678
8679   return new_index_value;
8680 }
8681
8682 static void setSelectboxSpecialActionVariablesIfNeeded(void)
8683 {
8684   int i;
8685
8686   // change action mode and arg variables according to action type variable
8687   for (i = 0; action_arg_options[i].value != -1; i++)
8688   {
8689     if (action_arg_options[i].value == custom_element_change.action_type)
8690     {
8691       int mode = action_arg_options[i].mode;
8692
8693       // only change if corresponding selectbox has changed
8694       if (selectbox_info[ED_SELECTBOX_ID_ACTION_MODE].options !=
8695           action_arg_modes[mode])
8696         custom_element_change.action_mode = -1;
8697
8698       // only change if corresponding selectbox has changed
8699       if (selectbox_info[ED_SELECTBOX_ID_ACTION_ARG].options !=
8700           action_arg_options[i].options)
8701         custom_element_change.action_arg = -1;
8702
8703       break;
8704     }
8705   }
8706 }
8707
8708 static void setSelectboxSpecialActionOptions(void)
8709 {
8710   int i;
8711
8712   // change action mode and arg selectbox according to action type selectbox
8713   for (i = 0; action_arg_options[i].value != -1; i++)
8714   {
8715     if (action_arg_options[i].value == custom_element_change.action_type)
8716     {
8717       int mode = action_arg_options[i].mode;
8718
8719       ModifyEditorSelectboxOptions(ED_SELECTBOX_ID_ACTION_MODE,
8720                                    action_arg_modes[mode]);
8721       ModifyEditorSelectboxValue(ED_SELECTBOX_ID_ACTION_MODE,
8722                                  custom_element_change.action_mode);
8723
8724       ModifyEditorSelectboxOptions(ED_SELECTBOX_ID_ACTION_ARG,
8725                                    action_arg_options[i].options);
8726       ModifyEditorSelectboxValue(ED_SELECTBOX_ID_ACTION_ARG,
8727                                  custom_element_change.action_arg);
8728       break;
8729     }
8730   }
8731 }
8732
8733 static void copy_custom_element_settings(int element_from, int element_to)
8734 {
8735   struct ElementInfo *ei_from = &element_info[element_from];
8736   struct ElementInfo *ei_to = &element_info[element_to];
8737
8738   copyElementInfo(ei_from, ei_to);
8739 }
8740
8741 static void replace_custom_element_in_settings(int element_from,
8742                                                int element_to)
8743 {
8744   int i, j, x, y;
8745
8746   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
8747   {
8748     struct ElementInfo *ei = &element_info[i];
8749
8750     for (y = 0; y < 3; y++)
8751       for (x = 0; x < 3; x++)
8752         if (ei->content.e[x][y] == element_from)
8753           ei->content.e[x][y] = element_to;
8754
8755     for (j = 0; j < ei->num_change_pages; j++)
8756     {
8757       struct ElementChangeInfo *change = &ei->change_page[j];
8758
8759       if (change->target_element == element_from)
8760         change->target_element = element_to;
8761
8762       if (change->initial_trigger_element == element_from)
8763         change->initial_trigger_element = element_to;
8764
8765       if (change->action_element == element_from)
8766         change->action_element = element_to;
8767
8768       for (y = 0; y < 3; y++)
8769         for (x = 0; x < 3; x++)
8770           if (change->target_content.e[x][y] == element_from)
8771             change->target_content.e[x][y] = element_to;
8772     }
8773
8774     if (ei->group != NULL)                              // group or internal
8775       for (j = 0; j < MAX_ELEMENTS_IN_GROUP; j++)
8776         if (ei->group->element[j] == element_from)
8777           ei->group->element[j] = element_to;
8778   }
8779 }
8780
8781 static void replace_custom_element_in_playfield(int element_from,
8782                                                 int element_to)
8783 {
8784   int x, y;
8785
8786   for (x = 0; x < lev_fieldx; x++)
8787     for (y = 0; y < lev_fieldy; y++)
8788       if (Tile[x][y] == element_from)
8789         Tile[x][y] = element_to;
8790 }
8791
8792 static boolean CopyCustomElement(int element_old, int element_new,
8793                                  int copy_mode)
8794 {
8795   int copy_mode_orig = copy_mode;
8796
8797   if (copy_mode == GADGET_ID_CUSTOM_COPY)
8798   {
8799     element_new = (IS_CUSTOM_ELEMENT(element_old) ?
8800                    EL_INTERNAL_CLIPBOARD_CUSTOM : EL_INTERNAL_CLIPBOARD_GROUP);
8801     copy_mode = GADGET_ID_CUSTOM_COPY_TO;
8802   }
8803   else if (copy_mode == GADGET_ID_CUSTOM_PASTE)
8804   {
8805     element_old = (IS_CUSTOM_ELEMENT(element_new) ?
8806                    EL_INTERNAL_CLIPBOARD_CUSTOM : EL_INTERNAL_CLIPBOARD_GROUP);
8807     copy_mode = GADGET_ID_CUSTOM_COPY_TO;
8808
8809     level.changed = TRUE;
8810   }
8811   else if (IS_CUSTOM_ELEMENT(element_old) && !IS_CUSTOM_ELEMENT(element_new))
8812   {
8813     Request("Please choose custom element!", REQ_CONFIRM);
8814
8815     return FALSE;
8816   }
8817   else if (IS_GROUP_ELEMENT(element_old) && !IS_GROUP_ELEMENT(element_new))
8818   {
8819     Request("Please choose group element!", REQ_CONFIRM);
8820
8821     return FALSE;
8822   }
8823   else if (IS_EMPTY_ELEMENT(element_old) && !IS_EMPTY_ELEMENT(element_new))
8824   {
8825     Request("Please choose empty element!", REQ_CONFIRM);
8826
8827     return FALSE;
8828   }
8829   else
8830   {
8831     level.changed = TRUE;
8832   }
8833
8834   // when modifying custom/group element, ask for copying level template
8835   if (copy_mode_orig != GADGET_ID_CUSTOM_COPY && level.use_custom_template)
8836   {
8837     if (!AskToCopyAndModifyLevelTemplate())
8838       return FALSE;
8839   }
8840
8841   if (copy_mode == GADGET_ID_CUSTOM_COPY_FROM)
8842   {
8843     copy_custom_element_settings(element_new, element_old);
8844   }
8845   else if (copy_mode == GADGET_ID_CUSTOM_COPY_TO)
8846   {
8847     copy_custom_element_settings(element_old, element_new);
8848   }
8849   else if (copy_mode == GADGET_ID_CUSTOM_EXCHANGE)
8850   {
8851     copy_custom_element_settings(element_old, EL_INTERNAL_DUMMY);
8852     copy_custom_element_settings(element_new, element_old);
8853     copy_custom_element_settings(EL_INTERNAL_DUMMY, element_new);
8854
8855     replace_custom_element_in_settings(element_old, EL_INTERNAL_DUMMY);
8856     replace_custom_element_in_settings(element_new, element_old);
8857     replace_custom_element_in_settings(EL_INTERNAL_DUMMY, element_new);
8858
8859     replace_custom_element_in_playfield(element_old, EL_INTERNAL_DUMMY);
8860     replace_custom_element_in_playfield(element_new, element_old);
8861     replace_custom_element_in_playfield(EL_INTERNAL_DUMMY, element_new);
8862   }
8863
8864   UpdateCustomElementGraphicGadgets();
8865   DrawPropertiesWindow();
8866
8867   return TRUE;
8868 }
8869
8870 static void CopyCustomElementPropertiesToEditor(int element)
8871 {
8872   int i;
8873   int current_change_page = element_info[element].current_change_page;
8874
8875   // dynamically (re)build selectbox for selecting change page
8876   for (i = 0; i < element_info[element].num_change_pages; i++)
8877   {
8878     sprintf(options_change_page_strings[i], "%d", i + 1);
8879
8880     options_change_page[i].value = i;
8881     options_change_page[i].text = options_change_page_strings[i];
8882   }
8883
8884   options_change_page[i].value = -1;
8885   options_change_page[i].text = NULL;
8886
8887   // needed here to initialize combined element properties
8888   InitElementPropertiesEngine(level.game_version);
8889
8890   element_info[element].change =
8891     &element_info[element].change_page[current_change_page];
8892
8893   custom_element = element_info[element];
8894   custom_element_change = *element_info[element].change;
8895
8896   // needed to initially set selectbox options for special action options
8897   setSelectboxSpecialActionOptions();
8898
8899   // needed to initially set selectbox value variables to reliable defaults
8900   for (i = 0; i < ED_NUM_SELECTBOX; i++)
8901     setSelectboxValue(i, *selectbox_info[i].value);
8902
8903   for (i = 0; i < NUM_ELEMENT_PROPERTIES; i++)
8904     custom_element_properties[i] = HAS_PROPERTY(element, i);
8905
8906   for (i = 0; i < NUM_CHANGE_EVENTS; i++)
8907     custom_element_change_events[i] = HAS_CHANGE_EVENT(element, i);
8908
8909   // ---------- element settings: configure (custom elements) -----------------
8910
8911   // set accessible layer selectbox help value
8912   custom_element.access_type =
8913     (IS_WALKABLE(element) ? EP_WALKABLE :
8914      IS_PASSABLE(element) ? EP_PASSABLE :
8915      custom_element.access_type);
8916   custom_element.access_layer =
8917     (IS_ACCESSIBLE_OVER(element) ? EP_ACCESSIBLE_OVER :
8918      IS_ACCESSIBLE_INSIDE(element) ? EP_ACCESSIBLE_INSIDE :
8919      IS_ACCESSIBLE_UNDER(element) ? EP_ACCESSIBLE_UNDER :
8920      custom_element.access_layer);
8921   custom_element.access_protected =
8922     (IS_PROTECTED(element) ? 1 : 0);
8923   custom_element_properties[EP_ACCESSIBLE] =
8924     (IS_ACCESSIBLE_OVER(element) ||
8925      IS_ACCESSIBLE_INSIDE(element) ||
8926      IS_ACCESSIBLE_UNDER(element));
8927
8928   // set walk-to-object action selectbox help value
8929   custom_element.walk_to_action =
8930     (IS_DIGGABLE(element) ? EP_DIGGABLE :
8931      IS_COLLECTIBLE_ONLY(element) ? EP_COLLECTIBLE_ONLY :
8932      IS_DROPPABLE(element) ? EP_DROPPABLE :
8933      IS_THROWABLE(element) ? EP_THROWABLE :
8934      IS_PUSHABLE(element) ? EP_PUSHABLE :
8935      custom_element.walk_to_action);
8936   custom_element_properties[EP_WALK_TO_OBJECT] =
8937     (IS_DIGGABLE(element) ||
8938      IS_COLLECTIBLE_ONLY(element) ||
8939      IS_DROPPABLE(element) ||
8940      IS_THROWABLE(element) ||
8941      IS_PUSHABLE(element));
8942
8943   // set smash targets selectbox help value
8944   custom_element.smash_targets =
8945     (CAN_SMASH_EVERYTHING(element) ? EP_CAN_SMASH_EVERYTHING :
8946      CAN_SMASH_ENEMIES(element) ? EP_CAN_SMASH_ENEMIES :
8947      CAN_SMASH_PLAYER(element) ? EP_CAN_SMASH_PLAYER :
8948      custom_element.smash_targets);
8949   custom_element_properties[EP_CAN_SMASH] =
8950     (CAN_SMASH_EVERYTHING(element) ||
8951      CAN_SMASH_ENEMIES(element) ||
8952      CAN_SMASH_PLAYER(element));
8953
8954   // set deadliness selectbox help value
8955   custom_element.deadliness =
8956     (DONT_TOUCH(element) ? EP_DONT_TOUCH :
8957      DONT_GET_HIT_BY(element) ? EP_DONT_GET_HIT_BY :
8958      DONT_COLLIDE_WITH(element) ? EP_DONT_COLLIDE_WITH :
8959      DONT_RUN_INTO(element) ? EP_DONT_RUN_INTO :
8960      custom_element.deadliness);
8961   custom_element_properties[EP_DEADLY] =
8962     (DONT_TOUCH(element) ||
8963      DONT_GET_HIT_BY(element) ||
8964      DONT_COLLIDE_WITH(element) ||
8965      DONT_RUN_INTO(element));
8966
8967   // ---------- element settings: advanced (custom elements) ------------------
8968
8969   // set "change by direct action" selectbox help value
8970   custom_element_change.direct_action =
8971     (HAS_CHANGE_EVENT(element, CE_NEXT_TO_PLAYER) ? CE_NEXT_TO_PLAYER :
8972      HAS_CHANGE_EVENT(element, CE_TOUCHED_BY_PLAYER) ? CE_TOUCHED_BY_PLAYER :
8973      HAS_CHANGE_EVENT(element, CE_PRESSED_BY_PLAYER) ? CE_PRESSED_BY_PLAYER :
8974      HAS_CHANGE_EVENT(element, CE_SWITCHED_BY_PLAYER) ? CE_SWITCHED_BY_PLAYER :
8975      HAS_CHANGE_EVENT(element, CE_SNAPPED_BY_PLAYER) ? CE_SNAPPED_BY_PLAYER :
8976      HAS_CHANGE_EVENT(element, CE_PUSHED_BY_PLAYER) ? CE_PUSHED_BY_PLAYER :
8977      HAS_CHANGE_EVENT(element, CE_ENTERED_BY_PLAYER) ? CE_ENTERED_BY_PLAYER :
8978      HAS_CHANGE_EVENT(element, CE_LEFT_BY_PLAYER) ? CE_LEFT_BY_PLAYER :
8979      HAS_CHANGE_EVENT(element, CE_DROPPED_BY_PLAYER) ? CE_DROPPED_BY_PLAYER :
8980      HAS_CHANGE_EVENT(element, CE_SWITCHED) ? CE_SWITCHED :
8981      HAS_CHANGE_EVENT(element, CE_HITTING_SOMETHING) ? CE_HITTING_SOMETHING :
8982      HAS_CHANGE_EVENT(element, CE_HIT_BY_SOMETHING) ? CE_HIT_BY_SOMETHING :
8983      HAS_CHANGE_EVENT(element, CE_BLOCKED) ? CE_BLOCKED :
8984      HAS_CHANGE_EVENT(element, CE_IMPACT) ? CE_IMPACT :
8985      HAS_CHANGE_EVENT(element, CE_SMASHED) ? CE_SMASHED :
8986      HAS_CHANGE_EVENT(element, CE_VALUE_CHANGES) ? CE_VALUE_CHANGES :
8987      HAS_CHANGE_EVENT(element, CE_SCORE_CHANGES) ? CE_SCORE_CHANGES :
8988      HAS_CHANGE_EVENT(element, CE_VALUE_GETS_ZERO) ? CE_VALUE_GETS_ZERO :
8989      HAS_CHANGE_EVENT(element, CE_SCORE_GETS_ZERO) ? CE_SCORE_GETS_ZERO :
8990      HAS_CHANGE_EVENT(element, CE_CLICKED_BY_MOUSE) ? CE_CLICKED_BY_MOUSE :
8991      HAS_CHANGE_EVENT(element, CE_PRESSED_BY_MOUSE) ? CE_PRESSED_BY_MOUSE :
8992      custom_element_change.direct_action);
8993
8994   // set "change by other element action" selectbox help value
8995   custom_element_change.other_action =
8996     (HAS_CHANGE_EVENT(element, CE_PLAYER_NEXT_TO_X) ? CE_PLAYER_NEXT_TO_X :
8997      HAS_CHANGE_EVENT(element, CE_PLAYER_TOUCHES_X) ? CE_PLAYER_TOUCHES_X :
8998      HAS_CHANGE_EVENT(element, CE_PLAYER_PRESSES_X) ? CE_PLAYER_PRESSES_X :
8999      HAS_CHANGE_EVENT(element, CE_PLAYER_SWITCHES_X) ? CE_PLAYER_SWITCHES_X :
9000      HAS_CHANGE_EVENT(element, CE_PLAYER_SNAPS_X) ? CE_PLAYER_SNAPS_X :
9001      HAS_CHANGE_EVENT(element, CE_PLAYER_PUSHES_X) ? CE_PLAYER_PUSHES_X :
9002      HAS_CHANGE_EVENT(element, CE_PLAYER_ENTERS_X) ? CE_PLAYER_ENTERS_X :
9003      HAS_CHANGE_EVENT(element, CE_PLAYER_LEAVES_X) ? CE_PLAYER_LEAVES_X :
9004      HAS_CHANGE_EVENT(element, CE_PLAYER_DIGS_X) ? CE_PLAYER_DIGS_X :
9005      HAS_CHANGE_EVENT(element, CE_PLAYER_COLLECTS_X) ? CE_PLAYER_COLLECTS_X :
9006      HAS_CHANGE_EVENT(element, CE_PLAYER_DROPS_X) ? CE_PLAYER_DROPS_X :
9007      HAS_CHANGE_EVENT(element, CE_NEXT_TO_X) ? CE_NEXT_TO_X :
9008      HAS_CHANGE_EVENT(element, CE_TOUCHING_X) ? CE_TOUCHING_X :
9009      HAS_CHANGE_EVENT(element, CE_HITTING_X) ? CE_HITTING_X :
9010      HAS_CHANGE_EVENT(element, CE_DIGGING_X) ? CE_DIGGING_X :
9011      HAS_CHANGE_EVENT(element, CE_HIT_BY_X) ? CE_HIT_BY_X :
9012      HAS_CHANGE_EVENT(element, CE_SWITCH_OF_X) ? CE_SWITCH_OF_X :
9013      HAS_CHANGE_EVENT(element, CE_CHANGE_OF_X) ? CE_CHANGE_OF_X :
9014      HAS_CHANGE_EVENT(element, CE_EXPLOSION_OF_X) ? CE_EXPLOSION_OF_X :
9015      HAS_CHANGE_EVENT(element, CE_MOVE_OF_X) ? CE_MOVE_OF_X :
9016      HAS_CHANGE_EVENT(element, CE_CREATION_OF_X) ? CE_CREATION_OF_X :
9017      HAS_CHANGE_EVENT(element, CE_VALUE_CHANGES_OF_X) ? CE_VALUE_CHANGES_OF_X :
9018      HAS_CHANGE_EVENT(element, CE_SCORE_CHANGES_OF_X) ? CE_SCORE_CHANGES_OF_X :
9019      HAS_CHANGE_EVENT(element, CE_VALUE_GETS_ZERO_OF_X) ? CE_VALUE_GETS_ZERO_OF_X :
9020      HAS_CHANGE_EVENT(element, CE_SCORE_GETS_ZERO_OF_X) ? CE_SCORE_GETS_ZERO_OF_X :
9021      HAS_CHANGE_EVENT(element, CE_MOUSE_CLICKED_ON_X) ? CE_MOUSE_CLICKED_ON_X :
9022      HAS_CHANGE_EVENT(element, CE_MOUSE_PRESSED_ON_X) ? CE_MOUSE_PRESSED_ON_X :
9023      custom_element_change.other_action);
9024 }
9025
9026 static void CopyGroupElementPropertiesToEditor(int element)
9027 {
9028   group_element_info = *element_info[element].group;
9029   custom_element = element_info[element];       // needed for description
9030 }
9031
9032 static void CopyEmptyElementPropertiesToEditor(int element)
9033 {
9034   custom_element = element_info[element];
9035 }
9036
9037 static void CopyClassicElementPropertiesToEditor(int element)
9038 {
9039   if (IS_PLAYER_ELEMENT(element) || COULD_MOVE_INTO_ACID(element))
9040     custom_element_properties[EP_CAN_MOVE_INTO_ACID] =
9041       getMoveIntoAcidProperty(&level, element);
9042
9043   if (MAYBE_DONT_COLLIDE_WITH(element))
9044     custom_element_properties[EP_DONT_COLLIDE_WITH] =
9045       getDontCollideWithProperty(&level, element);
9046 }
9047
9048 static void CopyElementPropertiesToEditor(int element)
9049 {
9050   if (IS_CUSTOM_ELEMENT(element))
9051     CopyCustomElementPropertiesToEditor(element);
9052   else if (IS_GROUP_ELEMENT(element))
9053     CopyGroupElementPropertiesToEditor(element);
9054   else if (IS_EMPTY_ELEMENT(element))
9055     CopyEmptyElementPropertiesToEditor(element);
9056   else
9057     CopyClassicElementPropertiesToEditor(element);
9058 }
9059
9060 static boolean AskToCopyAndModifyLevelTemplate(void)
9061 {
9062   if (Request("Copy and modify settings from level template?", REQ_ASK))
9063   {
9064     level.use_custom_template = FALSE;
9065
9066     ModifyGadget(level_editor_gadget[GADGET_ID_CUSTOM_USE_TEMPLATE_1],
9067                  GDI_CHECKED, FALSE, GDI_END);
9068     ModifyGadget(level_editor_gadget[GADGET_ID_CUSTOM_USE_TEMPLATE_2],
9069                  GDI_CHECKED, FALSE, GDI_END);
9070
9071     return TRUE;
9072   }
9073   else
9074   {
9075     LoadLevelTemplate(-1);      // this resets all element modifications ...
9076
9077     DrawEditModeWindow();       // ... and copies them to 'custom_element'
9078
9079     return FALSE;
9080   }
9081 }
9082
9083 static void CopyCustomElementPropertiesToGame(int element)
9084 {
9085   int i;
9086   int access_type_and_layer;
9087
9088   // mark that this custom element has been modified
9089   custom_element.modified_settings = TRUE;
9090   level.changed = TRUE;
9091
9092   if (level.use_custom_template)
9093     AskToCopyAndModifyLevelTemplate();
9094
9095   element_info[element] = custom_element;
9096   *element_info[element].change = custom_element_change;
9097
9098   // ---------- element settings: configure (custom elements) -----------------
9099
9100   // set accessible property from checkbox and selectbox
9101   custom_element_properties[EP_WALKABLE_OVER] = FALSE;
9102   custom_element_properties[EP_WALKABLE_INSIDE] = FALSE;
9103   custom_element_properties[EP_WALKABLE_UNDER] = FALSE;
9104   custom_element_properties[EP_PASSABLE_OVER] = FALSE;
9105   custom_element_properties[EP_PASSABLE_INSIDE] = FALSE;
9106   custom_element_properties[EP_PASSABLE_UNDER] = FALSE;
9107   access_type_and_layer = ((custom_element.access_type == EP_WALKABLE ?
9108                             EP_WALKABLE_OVER : EP_PASSABLE_OVER) +
9109                            (custom_element.access_layer - EP_ACCESSIBLE_OVER));
9110   custom_element_properties[access_type_and_layer] =
9111     custom_element_properties[EP_ACCESSIBLE];
9112   custom_element_properties[EP_PROTECTED] =
9113     (custom_element.access_protected != 0 &&
9114      custom_element_properties[EP_ACCESSIBLE]);
9115
9116   // set walk-to-object property from checkbox and selectbox
9117   custom_element_properties[EP_DIGGABLE] = FALSE;
9118   custom_element_properties[EP_COLLECTIBLE_ONLY] = FALSE;
9119   custom_element_properties[EP_DROPPABLE] = FALSE;
9120   custom_element_properties[EP_THROWABLE] = FALSE;
9121   custom_element_properties[EP_PUSHABLE] = FALSE;
9122   custom_element_properties[custom_element.walk_to_action] =
9123     custom_element_properties[EP_WALK_TO_OBJECT];
9124
9125   // set smash property from checkbox and selectbox
9126   custom_element_properties[EP_CAN_SMASH_PLAYER] = FALSE;
9127   custom_element_properties[EP_CAN_SMASH_ENEMIES] = FALSE;
9128   custom_element_properties[EP_CAN_SMASH_EVERYTHING] = FALSE;
9129   custom_element_properties[custom_element.smash_targets] =
9130     custom_element_properties[EP_CAN_SMASH];
9131
9132   // set deadliness property from checkbox and selectbox
9133   custom_element_properties[EP_DONT_RUN_INTO] = FALSE;
9134   custom_element_properties[EP_DONT_COLLIDE_WITH] = FALSE;
9135   custom_element_properties[EP_DONT_GET_HIT_BY] = FALSE;
9136   custom_element_properties[EP_DONT_TOUCH] = FALSE;
9137   custom_element_properties[custom_element.deadliness] =
9138     custom_element_properties[EP_DEADLY];
9139
9140   // ---------- element settings: advanced (custom elements) ------------------
9141
9142   // set player change event from checkbox and selectbox
9143   custom_element_change_events[CE_NEXT_TO_PLAYER] = FALSE;
9144   custom_element_change_events[CE_TOUCHED_BY_PLAYER] = FALSE;
9145   custom_element_change_events[CE_PRESSED_BY_PLAYER] = FALSE;
9146   custom_element_change_events[CE_SWITCHED_BY_PLAYER] = FALSE;
9147   custom_element_change_events[CE_SNAPPED_BY_PLAYER] = FALSE;
9148   custom_element_change_events[CE_PUSHED_BY_PLAYER] = FALSE;
9149   custom_element_change_events[CE_ENTERED_BY_PLAYER] = FALSE;
9150   custom_element_change_events[CE_LEFT_BY_PLAYER] = FALSE;
9151   custom_element_change_events[CE_DROPPED_BY_PLAYER] = FALSE;
9152   custom_element_change_events[CE_SWITCHED] = FALSE;
9153   custom_element_change_events[CE_HITTING_SOMETHING] = FALSE;
9154   custom_element_change_events[CE_HIT_BY_SOMETHING] = FALSE;
9155   custom_element_change_events[CE_BLOCKED] = FALSE;
9156   custom_element_change_events[CE_IMPACT] = FALSE;
9157   custom_element_change_events[CE_SMASHED] = FALSE;
9158   custom_element_change_events[CE_VALUE_CHANGES] = FALSE;
9159   custom_element_change_events[CE_SCORE_CHANGES] = FALSE;
9160   custom_element_change_events[CE_VALUE_GETS_ZERO] = FALSE;
9161   custom_element_change_events[CE_SCORE_GETS_ZERO] = FALSE;
9162   custom_element_change_events[CE_CLICKED_BY_MOUSE] = FALSE;
9163   custom_element_change_events[CE_PRESSED_BY_MOUSE] = FALSE;
9164   custom_element_change_events[custom_element_change.direct_action] =
9165     custom_element_change_events[CE_BY_DIRECT_ACTION];
9166
9167   // set other element action change event from checkbox and selectbox
9168   custom_element_change_events[CE_PLAYER_NEXT_TO_X] = FALSE;
9169   custom_element_change_events[CE_PLAYER_TOUCHES_X] = FALSE;
9170   custom_element_change_events[CE_PLAYER_PRESSES_X] = FALSE;
9171   custom_element_change_events[CE_PLAYER_SWITCHES_X] = FALSE;
9172   custom_element_change_events[CE_PLAYER_SNAPS_X] = FALSE;
9173   custom_element_change_events[CE_PLAYER_PUSHES_X] = FALSE;
9174   custom_element_change_events[CE_PLAYER_ENTERS_X] = FALSE;
9175   custom_element_change_events[CE_PLAYER_LEAVES_X] = FALSE;
9176   custom_element_change_events[CE_PLAYER_DIGS_X] = FALSE;
9177   custom_element_change_events[CE_PLAYER_COLLECTS_X] = FALSE;
9178   custom_element_change_events[CE_PLAYER_DROPS_X] = FALSE;
9179   custom_element_change_events[CE_NEXT_TO_X] = FALSE;
9180   custom_element_change_events[CE_TOUCHING_X] = FALSE;
9181   custom_element_change_events[CE_HITTING_X] = FALSE;
9182   custom_element_change_events[CE_DIGGING_X] = FALSE;
9183   custom_element_change_events[CE_HIT_BY_X] = FALSE;
9184   custom_element_change_events[CE_SWITCH_OF_X] = FALSE;
9185   custom_element_change_events[CE_CHANGE_OF_X] = FALSE;
9186   custom_element_change_events[CE_EXPLOSION_OF_X] = FALSE;
9187   custom_element_change_events[CE_MOVE_OF_X] = FALSE;
9188   custom_element_change_events[CE_CREATION_OF_X] = FALSE;
9189   custom_element_change_events[CE_VALUE_CHANGES_OF_X] = FALSE;
9190   custom_element_change_events[CE_SCORE_CHANGES_OF_X] = FALSE;
9191   custom_element_change_events[CE_VALUE_GETS_ZERO_OF_X] = FALSE;
9192   custom_element_change_events[CE_SCORE_GETS_ZERO_OF_X] = FALSE;
9193   custom_element_change_events[CE_MOUSE_CLICKED_ON_X] = FALSE;
9194   custom_element_change_events[CE_MOUSE_PRESSED_ON_X] = FALSE;
9195   custom_element_change_events[custom_element_change.other_action] =
9196     custom_element_change_events[CE_BY_OTHER_ACTION];
9197
9198   for (i = 0; i < NUM_ELEMENT_PROPERTIES; i++)
9199     SET_PROPERTY(element, i, custom_element_properties[i]);
9200
9201   for (i = 0; i < NUM_CHANGE_EVENTS; i++)
9202     SET_CHANGE_EVENT(element, i, custom_element_change_events[i]);
9203
9204   // copy change events also to special level editor variable
9205   custom_element = element_info[element];
9206   custom_element_change = *element_info[element].change;
9207
9208   // needed here to restore runtime value "element_info[element].gfx_element"
9209   InitElementPropertiesGfxElement();
9210 }
9211
9212 static void CopyGroupElementPropertiesToGame(int element)
9213 {
9214   // mark that this group element has been modified
9215   custom_element.modified_settings = TRUE;
9216   level.changed = TRUE;
9217
9218   if (level.use_custom_template)
9219     AskToCopyAndModifyLevelTemplate();
9220
9221   element_info[element] = custom_element;
9222   *element_info[element].group = group_element_info;
9223
9224   // needed here to restore runtime value "element_info[element].gfx_element"
9225   InitElementPropertiesGfxElement();
9226 }
9227
9228 static void CopyEmptyElementPropertiesToGame(int element)
9229 {
9230   // mark that this empty element has been modified
9231   custom_element.modified_settings = TRUE;
9232   level.changed = TRUE;
9233
9234   if (level.use_custom_template)
9235     AskToCopyAndModifyLevelTemplate();
9236
9237   element_info[element] = custom_element;
9238
9239   // needed here to restore runtime value "element_info[element].gfx_element"
9240   InitElementPropertiesGfxElement();
9241 }
9242
9243 static void CopyClassicElementPropertiesToGame(int element)
9244 {
9245   if (IS_PLAYER_ELEMENT(element) || COULD_MOVE_INTO_ACID(element))
9246     setMoveIntoAcidProperty(&level, element,
9247                             custom_element_properties[EP_CAN_MOVE_INTO_ACID]);
9248
9249   if (MAYBE_DONT_COLLIDE_WITH(element))
9250     setDontCollideWithProperty(&level, element,
9251                               custom_element_properties[EP_DONT_COLLIDE_WITH]);
9252 }
9253
9254 static void CopyElementPropertiesToGame(int element)
9255 {
9256   if (IS_CUSTOM_ELEMENT(element))
9257     CopyCustomElementPropertiesToGame(element);
9258   else if (IS_GROUP_ELEMENT(element))
9259     CopyGroupElementPropertiesToGame(element);
9260   else if (IS_EMPTY_ELEMENT(element))
9261     CopyEmptyElementPropertiesToGame(element);
9262   else
9263     CopyClassicElementPropertiesToGame(element);
9264 }
9265
9266 #if DEBUG
9267 static void CheckElementDescriptions(void)
9268 {
9269   int i;
9270
9271   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
9272     if (getElementDescriptionFilename(i) == NULL && !IS_OBSOLETE(i))
9273       Warn("no element description file for element '%s'", EL_NAME(i));
9274 }
9275 #endif
9276
9277 static int getMaxEdFieldX(boolean has_scrollbar)
9278 {
9279   int scrollbar_width = (has_scrollbar ? ED_SCROLLBUTTON_XSIZE : 0);
9280   int sxsize = SXSIZE - scrollbar_width;
9281   int max_ed_fieldx = sxsize / ed_tilesize;
9282
9283   return max_ed_fieldx;
9284 }
9285
9286 static int getMaxEdFieldY(boolean has_scrollbar)
9287 {
9288   int infotext_height = (IN_PIX_FIELD(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY) ?
9289                          INFOTEXT_YSIZE_FULL : 0);
9290   int scrollbar_height = (has_scrollbar ? ED_SCROLLBUTTON_YSIZE : 0);
9291   int sysize = SYSIZE - scrollbar_height - infotext_height;
9292   int max_ed_fieldy = sysize / ed_tilesize;
9293
9294   return max_ed_fieldy;
9295 }
9296
9297 static void InitZoomLevelSettings(int zoom_tilesize)
9298 {
9299   static int last_game_engine_type = GAME_ENGINE_TYPE_UNKNOWN;
9300
9301   if (zoom_tilesize == -1 && level.game_engine_type != last_game_engine_type)
9302   {
9303     ed_tilesize = setup.auto_setup.editor_zoom_tilesize;
9304     ed_tilesize_default = DEFAULT_EDITOR_TILESIZE;
9305
9306     // make sure that tile size is always a power of 2
9307     ed_tilesize = (1 << log_2(ed_tilesize));
9308
9309     if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
9310     {
9311       ed_tilesize = DEFAULT_EDITOR_TILESIZE_MM;
9312       ed_tilesize_default = DEFAULT_EDITOR_TILESIZE_MM;
9313     }
9314   }
9315
9316   last_game_engine_type = level.game_engine_type;
9317
9318   // limit zoom tilesize by upper and lower bound
9319   ed_tilesize = MIN(MAX(MICRO_TILESIZE, ed_tilesize), TILESIZE);
9320
9321   // store zoom tilesize in auto setup file only if it was manually changed
9322   if (zoom_tilesize != -1)
9323     setup.auto_setup.editor_zoom_tilesize = ed_tilesize;
9324
9325   MAX_ED_FIELDX = getMaxEdFieldX(FALSE);
9326   MAX_ED_FIELDY = getMaxEdFieldY(FALSE);
9327 }
9328
9329 static void InitDrawingElements(void)
9330 {
9331   static int game_engine_type_last = GAME_ENGINE_TYPE_UNKNOWN;
9332
9333   if (level.game_engine_type == game_engine_type_last)
9334     return;
9335
9336   if (level.game_engine_type == GAME_ENGINE_TYPE_BD)
9337   {
9338     new_element1 = EL_BD_WALL;
9339     new_element2 = EL_EMPTY;
9340     new_element3 = EL_BD_SAND;
9341   }
9342   else if (level.game_engine_type == GAME_ENGINE_TYPE_SP)
9343   {
9344     new_element1 = EL_SP_CHIP_SINGLE;
9345     new_element2 = EL_EMPTY;
9346     new_element3 = EL_SP_BASE;
9347   }
9348   else if (level.game_engine_type == GAME_ENGINE_TYPE_MM)
9349   {
9350     new_element1 = EL_MM_MIRROR_START;
9351     new_element2 = EL_EMPTY;
9352     new_element3 = EL_MM_WOODEN_WALL;
9353   }
9354   else
9355   {
9356     new_element1 = EL_WALL;
9357     new_element2 = EL_EMPTY;
9358     new_element3 = EL_SAND;
9359   }
9360
9361   game_engine_type_last = level.game_engine_type;
9362 }
9363
9364 static void InitLevelSetInfo(void)
9365 {
9366   snprintf(levelset_name,   MAX_LEVEL_NAME_LEN + 1,
9367            "%s", leveldir_current->name);
9368   snprintf(levelset_author, MAX_LEVEL_AUTHOR_LEN + 1,
9369            "%s", leveldir_current->author);
9370
9371   levelset_num_levels = leveldir_current->levels;
9372
9373   levelset_use_levelset_artwork = FALSE;
9374   levelset_copy_level_template = FALSE;
9375
9376   levelset_save_mode = LEVELSET_SAVE_MODE_UPDATE;
9377 }
9378
9379 static void ChangeEditorToLevelSet(char *levelset_subdir)
9380 {
9381   leveldir_current = getTreeInfoFromIdentifier(leveldir_first, levelset_subdir);
9382
9383   // the previous level set might have used custom artwork
9384   ReloadCustomArtwork(0);
9385
9386   LoadLevelSetup_SeriesInfo();
9387
9388   SaveLevelSetup_LastSeries();
9389   SaveLevelSetup_SeriesInfo();
9390
9391   TapeErase();
9392
9393   LoadLevel(level_nr);
9394   LoadScore(level_nr);
9395
9396   DrawLevelEd();
9397 }
9398
9399 static boolean useEditorDoorAnimation(void)
9400 {
9401   struct RectWithBorder *vp_door_1 = &viewport.door_1[GAME_MODE_MAIN];
9402   boolean door_1_viewport_unchanged =
9403     (vp_door_1->x      == DX     &&
9404      vp_door_1->y      == DY     &&
9405      vp_door_1->width  == DXSIZE &&
9406      vp_door_1->height == DYSIZE);
9407   boolean door_1_contains_toolbox =
9408     (EX >= DX &&
9409      EY >= DY &&
9410      EX + EXSIZE <= DX + DXSIZE &&
9411      EY + EYSIZE <= DY + DYSIZE);
9412
9413   return (door_1_viewport_unchanged && door_1_contains_toolbox);
9414 }
9415
9416 static void DrawEditorDoorBackground(int graphic, int x, int y,
9417                                      int width, int height)
9418 {
9419   struct GraphicInfo *g = &graphic_info[graphic];
9420
9421   if (g->bitmap != NULL)
9422     BlitBitmap(g->bitmap, drawto, g->src_x, g->src_y,
9423                MIN(width, g->width), MIN(height, g->height), x, y);
9424   else
9425     ClearRectangle(drawto, x, y, width, height);
9426 }
9427
9428 static void DrawEditorDoorContent(void)
9429 {
9430   // needed for gadgets drawn on background (like palette scrollbar)
9431   SetDoorBackgroundImage(IMG_UNDEFINED);
9432
9433   // copy default editor door content to main double buffer
9434   DrawEditorDoorBackground(IMG_BACKGROUND_PALETTE, DX, DY, DXSIZE, DYSIZE);
9435
9436   // draw bigger door
9437   DrawSpecialEditorDoor();
9438
9439   // draw new control window
9440   DrawEditorDoorBackground(IMG_BACKGROUND_TOOLBOX, EX, EY, EXSIZE, EYSIZE);
9441
9442   // draw all toolbox gadgets to editor doors
9443   MapControlButtons();
9444
9445   // when returning from test game to properties page, redraw toolbox gadgets
9446   if (edit_mode == ED_MODE_PROPERTIES)
9447   {
9448     UnmapLevelEditorToolboxDrawingGadgets();
9449     UnmapLevelEditorToolboxCustomGadgets();
9450
9451     MapLevelEditorToolboxCustomGadgetsIfNeeded();
9452   }
9453
9454   // draw all palette gadgets to editor doors
9455   ModifyEditorElementList();
9456   RedrawDrawingElements();
9457
9458   // copy actual editor door content to door double buffer for OpenDoor()
9459   BlitBitmap(drawto, bitmap_db_door_1, DX, DY, DXSIZE, DYSIZE, 0, 0);
9460 }
9461
9462 void DrawLevelEd(void)
9463 {
9464   int fade_mask = REDRAW_FIELD;
9465
9466   FadeSoundsAndMusic();
9467
9468   if (CheckFadeAll())
9469     fade_mask = REDRAW_ALL;
9470
9471   FadeOut(fade_mask);
9472
9473   // needed if different viewport properties defined for editor
9474   ChangeViewportPropertiesIfNeeded();
9475
9476   ClearField();
9477
9478   InitZoomLevelSettings(-1);
9479   InitDrawingElements();
9480   InitLevelSetInfo();
9481
9482 #if DEBUG
9483   CheckElementDescriptions();
9484 #endif
9485
9486   if (level_editor_test_game)
9487   {
9488     CopyPlayfield(level.field, Tile);
9489     CopyPlayfield(TileBackup, level.field);
9490
9491     level_editor_test_game = FALSE;
9492   }
9493   else
9494   {
9495     edit_mode = ED_MODE_DRAWING;
9496     edit_mode_levelconfig = ED_MODE_LEVELCONFIG_LEVEL;
9497     edit_mode_properties = ED_MODE_PROPERTIES_INFO;
9498
9499     ResetUndoBuffer();
9500
9501     level_xpos = -1;
9502     level_ypos = -1;
9503   }
9504
9505   // redraw_mask |= REDRAW_ALL;
9506
9507   FreeLevelEditorGadgets();
9508   CreateLevelEditorGadgets();
9509
9510   ReinitializeElementList();            // update dynamic level element list
9511   ReinitializeElementListButtons();     // custom element may look different
9512
9513   InitElementPropertiesGfxElement();
9514
9515   UnmapAllGadgets();
9516
9517   DrawEditModeWindow_PlayfieldOnly();
9518
9519   DrawMaskedBorder(fade_mask);
9520
9521   // use door animation if door 1 viewport is unchanged and contains toolbox
9522   if (useEditorDoorAnimation())
9523   {
9524     FadeIn(fade_mask);
9525
9526     DrawEditorDoorContent();
9527
9528     OpenDoor(DOOR_OPEN_1 | DOOR_FORCE_ANIM);
9529   }
9530   else
9531   {
9532     DrawEditorDoorContent();
9533
9534     FadeIn(fade_mask);
9535   }
9536
9537   SetDoorState(DOOR_OPEN_1 | DOOR_OPEN_2);
9538 }
9539
9540 static void AdjustDrawingAreaGadgets(void)
9541 {
9542   int ed_xsize = lev_fieldx + 2;
9543   int ed_ysize = lev_fieldy + 2;
9544   int max_ed_fieldx = MAX_ED_FIELDX;
9545   int max_ed_fieldy = MAX_ED_FIELDY;
9546   boolean horizontal_scrollbar_needed;
9547   boolean vertical_scrollbar_needed;
9548   int x, y, width, height;
9549
9550   if (suppressBorderElement())
9551   {
9552     ed_xsize = lev_fieldx;
9553     ed_ysize = lev_fieldy;
9554   }
9555
9556   // check if we need any scrollbars
9557   horizontal_scrollbar_needed = (ed_xsize > max_ed_fieldx);
9558   vertical_scrollbar_needed   = (ed_ysize > max_ed_fieldy);
9559
9560   // check if we have a smaller editor field because of scrollbars
9561   max_ed_fieldx = getMaxEdFieldX(vertical_scrollbar_needed);
9562   max_ed_fieldy = getMaxEdFieldY(horizontal_scrollbar_needed);
9563
9564   // check again if we now need more scrollbars because of less space
9565   horizontal_scrollbar_needed = (ed_xsize > max_ed_fieldx);
9566   vertical_scrollbar_needed   = (ed_ysize > max_ed_fieldy);
9567
9568   // check if editor field gets even smaller after adding new scrollbars
9569   max_ed_fieldx = getMaxEdFieldX(vertical_scrollbar_needed);
9570   max_ed_fieldy = getMaxEdFieldY(horizontal_scrollbar_needed);
9571
9572   ed_fieldx = (ed_xsize > max_ed_fieldx ? max_ed_fieldx : ed_xsize);
9573   ed_fieldy = (ed_ysize > max_ed_fieldy ? max_ed_fieldy : ed_ysize);
9574
9575   x = SX + ed_fieldx * ed_tilesize;
9576   y = SY + ed_fieldy * ed_tilesize;
9577
9578   width  = ed_fieldx * ed_tilesize - 2 * ED_SCROLLBUTTON_XSIZE;
9579   height = ed_fieldy * ed_tilesize - 2 * ED_SCROLLBUTTON_YSIZE;
9580
9581   // adjust drawing area gadget
9582   ModifyGadget(level_editor_gadget[GADGET_ID_DRAWING_LEVEL],
9583                GDI_AREA_SIZE, ed_fieldx, ed_fieldy,
9584                GDI_ITEM_SIZE, ed_tilesize, ed_tilesize,
9585                GDI_END);
9586
9587   // adjust horizontal scrollbar gadgets
9588   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LEFT],
9589                GDI_Y, y,
9590                GDI_END);
9591   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_RIGHT],
9592                GDI_X, x - ED_SCROLLBUTTON_XSIZE,
9593                GDI_Y, y,
9594                GDI_END);
9595   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
9596                GDI_Y, y,
9597                GDI_WIDTH, width,
9598                GDI_SCROLLBAR_ITEMS_VISIBLE, ed_fieldx,
9599                GDI_END);
9600
9601   // adjust vertical scrollbar gadgets
9602   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_UP],
9603                GDI_X, x,
9604                GDI_END);
9605   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_DOWN],
9606                GDI_X, x,
9607                GDI_Y, y - ED_SCROLLBUTTON_YSIZE,
9608                GDI_END);
9609   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
9610                GDI_X, x,
9611                GDI_HEIGHT, height,
9612                GDI_SCROLLBAR_ITEMS_VISIBLE, ed_fieldy,
9613                GDI_END);
9614 }
9615
9616 static void AdjustLevelScrollPosition(void)
9617 {
9618   if (level_xpos < -1)
9619     level_xpos = -1;
9620   if (level_xpos > lev_fieldx - ed_fieldx + 1)
9621     level_xpos = lev_fieldx - ed_fieldx + 1;
9622   if (lev_fieldx < ed_fieldx - 2)
9623     level_xpos = -1;
9624
9625   if (level_ypos < -1)
9626     level_ypos = -1;
9627   if (level_ypos > lev_fieldy - ed_fieldy + 1)
9628     level_ypos = lev_fieldy - ed_fieldy + 1;
9629   if (lev_fieldy < ed_fieldy - 2)
9630     level_ypos = -1;
9631
9632   if (suppressBorderElement())
9633   {
9634     level_xpos = 0;
9635     level_ypos = 0;
9636   }
9637 }
9638
9639 static void AdjustEditorScrollbar(int id)
9640 {
9641   struct GadgetInfo *gi = level_editor_gadget[id];
9642   int items_max, items_visible, item_position;
9643
9644   if (id == GADGET_ID_SCROLL_HORIZONTAL)
9645   {
9646     items_max = MAX(lev_fieldx + 2, ed_fieldx);
9647     items_visible = ed_fieldx;
9648     item_position = level_xpos + 1;
9649   }
9650   else
9651   {
9652     items_max = MAX(lev_fieldy + 2, ed_fieldy);
9653     items_visible = ed_fieldy;
9654     item_position = level_ypos + 1;
9655   }
9656
9657   if (item_position > items_max - items_visible)
9658     item_position = items_max - items_visible;
9659
9660   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
9661                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
9662 }
9663
9664 static void AdjustElementListScrollbar(void)
9665 {
9666   struct GadgetInfo *gi = level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL];
9667   int items_max, items_visible, item_position;
9668
9669   // correct position of element list scrollbar
9670   if (element_shift < 0)
9671     element_shift = 0;
9672   if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
9673     element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
9674
9675   items_max = num_editor_elements / ED_ELEMENTLIST_BUTTONS_HORIZ;
9676   items_visible = ED_ELEMENTLIST_BUTTONS_VERT;
9677   item_position = element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ;
9678
9679   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
9680                GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
9681                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
9682 }
9683
9684 static void ModifyEditorCounterValue(int counter_id, int new_value)
9685 {
9686   int *counter_value = counterbutton_info[counter_id].value;
9687   int gadget_id = counterbutton_info[counter_id].gadget_id_text;
9688   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9689
9690   ModifyGadget(gi, GDI_NUMBER_VALUE, new_value, GDI_END);
9691
9692   if (counter_value != NULL)
9693     *counter_value = gi->textinput.number_value;
9694 }
9695
9696 static void ModifyEditorCounterLimits(int counter_id, int min, int max)
9697 {
9698   int gadget_id = counterbutton_info[counter_id].gadget_id_text;
9699   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9700
9701   ModifyGadget(gi, GDI_NUMBER_MIN, min, GDI_NUMBER_MAX, max, GDI_END);
9702
9703   if (counter_id >= ED_COUNTER_ID_ELEMENT_VALUE1 &&
9704       counter_id <= ED_COUNTER_ID_ELEMENT_VALUE4)
9705   {
9706     int gadget_id_up = counterbutton_info[counter_id].gadget_id_up;
9707     struct GadgetInfo *gi_up = level_editor_gadget[gadget_id_up];
9708
9709     ModifyGadget(gi, GDI_TEXT_SIZE, (max < 10 ? 1 : 3), GDI_END);
9710     ModifyGadget(gi_up, GDI_X, gi->x + gi->width + ED_GADGET_SMALL_DISTANCE,
9711                  GDI_END);
9712   }
9713 }
9714
9715 static void ModifyEditorSelectboxValue(int selectbox_id, int new_value)
9716 {
9717   int gadget_id = selectbox_info[selectbox_id].gadget_id;
9718   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9719   int new_index_value = setSelectboxValue(selectbox_id, new_value);
9720
9721   ModifyGadget(gi, GDI_SELECTBOX_INDEX, new_index_value, GDI_END);
9722 }
9723
9724 static void ModifyEditorSelectboxOptions(int selectbox_id,
9725                                          struct ValueTextInfo *options)
9726 {
9727   int gadget_id = selectbox_info[selectbox_id].gadget_id;
9728   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9729
9730   selectbox_info[selectbox_id].options = options;
9731
9732   // set index to zero -- list may be shorter now (correct later, if needed)
9733   ModifyGadget(gi, GDI_SELECTBOX_INDEX, 0,
9734                GDI_SELECTBOX_OPTIONS, options, GDI_END);
9735 }
9736
9737 static void ModifyEditorDrawingArea(int drawingarea_id, int xsize, int ysize)
9738 {
9739   int gadget_id = drawingarea_info[drawingarea_id].gadget_id;
9740   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9741
9742   drawingarea_info[drawingarea_id].area_xsize = xsize;
9743   drawingarea_info[drawingarea_id].area_ysize = ysize;
9744
9745   ModifyGadget(gi, GDI_AREA_SIZE, xsize, ysize, GDI_END);
9746 }
9747
9748 static void ModifyEditorElementList(void)
9749 {
9750   int i;
9751
9752   if (!use_permanent_palette && edit_mode != ED_MODE_PALETTE)
9753     return;
9754
9755   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
9756   {
9757     int gadget_id = GADGET_ID_ELEMENTLIST_FIRST + i;
9758     struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9759     struct GadgetDesign *gd = &gi->deco.design;
9760     int element = editor_elements[element_shift + i];
9761     int tile_size = BUTTON_TILE_SIZE(editor.palette.tile_size);
9762
9763     UnmapGadget(gi);
9764
9765     getEditorGraphicSource(element, tile_size, &gd->bitmap, &gd->x, &gd->y);
9766
9767     ModifyGadget(gi, GDI_INFO_TEXT, getElementInfoText(element), GDI_END);
9768
9769     MapGadget(gi);
9770   }
9771 }
9772
9773 static void DrawDrawingElementGraphic(int element, struct XYTileSize *pos)
9774 {
9775   int graphic = el2edimg(element);
9776   int tile_size = BUTTON_TILE_SIZE(pos->tile_size);
9777
9778   if (pos->x == -1 &&
9779       pos->y == -1)
9780     return;
9781
9782   DrawSizedGraphicExt(drawto, DX + pos->x, DY + pos->y, graphic, 0, tile_size);
9783 }
9784
9785 static void ModifyDrawingElementButton(int element, int id)
9786 {
9787   struct GadgetInfo *gi = level_editor_gadget[id];
9788   Bitmap *deco_bitmap;
9789   int deco_x, deco_y;
9790   int tile_size = gi->deco.width;
9791
9792   getEditorGraphicSource(element, tile_size, &deco_bitmap, &deco_x, &deco_y);
9793
9794   ModifyGadget(gi, GDI_DECORATION_DESIGN, deco_bitmap, deco_x, deco_y, GDI_END);
9795 }
9796
9797 static void PickDrawingElement(int button, int element)
9798 {
9799   struct
9800   {
9801     int *new_element;
9802     struct XYTileSize *pos;
9803     int id;
9804   } de, drawing_elements[] =
9805   {
9806     { &new_element1, &editor.palette.element_left,   GADGET_ID_ELEMENT_LEFT   },
9807     { &new_element2, &editor.palette.element_middle, GADGET_ID_ELEMENT_MIDDLE },
9808     { &new_element3, &editor.palette.element_right,  GADGET_ID_ELEMENT_RIGHT  },
9809   };
9810
9811   if (button < 1 || button > 3)
9812     return;
9813
9814   if (IS_MM_WALL(element))
9815     element = map_mm_wall_element(element);
9816
9817   de = drawing_elements[button - 1];
9818
9819   *de.new_element = element;    // update global drawing element variable
9820
9821   DrawDrawingElementGraphic(element, de.pos);
9822   ModifyDrawingElementButton(element, de.id);
9823
9824   redraw_mask |= REDRAW_DOOR_1;
9825 }
9826
9827 static void RedrawDrawingElements(void)
9828 {
9829   PickDrawingElement(1, new_element1);
9830   PickDrawingElement(2, new_element2);
9831   PickDrawingElement(3, new_element3);
9832 }
9833
9834 static void DrawDrawingWindowExt(boolean remap_toolbox_gadgets)
9835 {
9836   stick_element_properties_window = FALSE;
9837
9838   SetMainBackgroundImage(IMG_UNDEFINED);
9839   ClearField();
9840
9841   UnmapLevelEditorFieldGadgets();
9842
9843   AdjustDrawingAreaGadgets();
9844   AdjustLevelScrollPosition();
9845   AdjustEditorScrollbar(GADGET_ID_SCROLL_HORIZONTAL);
9846   AdjustEditorScrollbar(GADGET_ID_SCROLL_VERTICAL);
9847
9848   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
9849
9850   MapMainDrawingArea();
9851
9852   if (remap_toolbox_gadgets)
9853   {
9854     UnmapLevelEditorToolboxCustomGadgets();
9855     MapLevelEditorToolboxDrawingGadgets();
9856   }
9857 }
9858
9859 static void DrawDrawingWindow(void)
9860 {
9861   DrawDrawingWindowExt(TRUE);
9862 }
9863
9864 static int getTabulatorBarWidth(void)
9865 {
9866   struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_PROPERTIES_INFO];
9867   struct GadgetInfo *gd_gi4 = level_editor_gadget[GADGET_ID_PROPERTIES_CHANGE];
9868
9869   return gd_gi4->x - gd_gi1->x + gd_gi4->width;
9870 }
9871
9872 static int getTabulatorBarHeight(void)
9873 {
9874   return ED_TAB_BAR_HEIGHT;
9875 }
9876
9877 static Pixel getTabulatorBarColor(void)
9878 {
9879   struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_LEVELCONFIG_LEVEL];
9880   struct GadgetDesign *gd = &gd_gi1->alt_design[GD_BUTTON_UNPRESSED];
9881   int gd_x = gd->x + gd_gi1->border.width / 2;
9882   int gd_y = gd->y + gd_gi1->height - 1;
9883
9884   return GetPixel(gd->bitmap, gd_x, gd_y);
9885 }
9886
9887 static void DrawLevelConfigTabulatorGadgets(void)
9888 {
9889   struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_LEVELCONFIG_LEVEL];
9890   Pixel tab_color = getTabulatorBarColor();
9891   int id_first = ED_TEXTBUTTON_ID_LEVELCONFIG_LEVEL;
9892   int id_last  = ED_TEXTBUTTON_ID_LEVELCONFIG_EDITOR;
9893   int i;
9894
9895   // draw additional "engine" tabulator when using native BD engine
9896   if (level.game_engine_type == GAME_ENGINE_TYPE_BD)
9897     id_last = ED_TEXTBUTTON_ID_LEVELCONFIG_ENGINE;
9898
9899   for (i = id_first; i <= id_last; i++)
9900   {
9901     int gadget_id = textbutton_info[i].gadget_id;
9902     struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9903     boolean active = (i != edit_mode_levelconfig);
9904
9905     // draw background line below tabulator button
9906     ClearRectangleOnBackground(drawto, gi->x, gi->y + gi->height, gi->width, 1);
9907
9908     // draw solid line below inactive tabulator buttons
9909     if (!active && tab_color != BLACK_PIXEL)    // black => transparent
9910       FillRectangle(drawto, gi->x, gi->y + gi->height, gi->width,
9911                     ED_GADGET_TINY_DISTANCE, tab_color);
9912
9913     ModifyGadget(gi, GDI_ACTIVE, active, GDI_END);
9914     MapTextbuttonGadget(i);
9915   }
9916
9917   // draw little border line below tabulator buttons
9918   if (tab_color != BLACK_PIXEL)                 // black => transparent
9919     FillRectangle(drawto, gd_gi1->x, gd_gi1->y + gd_gi1->height +
9920                   ED_GADGET_TINY_DISTANCE,
9921                   getTabulatorBarWidth(), getTabulatorBarHeight(), tab_color);
9922 }
9923
9924 static void DrawPropertiesTabulatorGadgets(void)
9925 {
9926   struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_PROPERTIES_INFO];
9927   struct GadgetDesign *gd = &gd_gi1->alt_design[GD_BUTTON_UNPRESSED];
9928   int gd_x = gd->x + gd_gi1->border.width / 2;
9929   int gd_y = gd->y + gd_gi1->height - 1;
9930   Pixel tab_color = GetPixel(gd->bitmap, gd_x, gd_y);
9931   int id_first = ED_TEXTBUTTON_ID_PROPERTIES_INFO;
9932   int id_last  = ED_TEXTBUTTON_ID_PROPERTIES_CONFIG;
9933   int i;
9934
9935   // draw two config tabulators for player elements
9936   if (IS_PLAYER_ELEMENT(properties_element))
9937     id_last = ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2;
9938
9939   // draw two config and one "change" tabulator for custom elements
9940   if (IS_CUSTOM_ELEMENT(properties_element))
9941     id_last = ED_TEXTBUTTON_ID_PROPERTIES_CHANGE;
9942
9943   for (i = id_first; i <= id_last; i++)
9944   {
9945     int gadget_id = textbutton_info[i].gadget_id;
9946     struct GadgetInfo *gi = level_editor_gadget[gadget_id];
9947     boolean active = (i != edit_mode_properties);
9948
9949     // use "config 1" and "config 2" instead of "config" for players and CEs
9950     if (i == ED_TEXTBUTTON_ID_PROPERTIES_CONFIG &&
9951         (IS_PLAYER_ELEMENT(properties_element) ||
9952          IS_CUSTOM_ELEMENT(properties_element)))
9953       continue;
9954
9955     // draw background line below tabulator button
9956     ClearRectangleOnBackground(drawto, gi->x, gi->y + gi->height, gi->width, 1);
9957
9958     // draw solid line below inactive tabulator buttons
9959     if (!active && tab_color != BLACK_PIXEL)    // black => transparent
9960       FillRectangle(drawto, gi->x, gi->y + gi->height, gi->width,
9961                     ED_GADGET_TINY_DISTANCE, tab_color);
9962
9963     ModifyGadget(gi, GDI_ACTIVE, active, GDI_END);
9964     MapTextbuttonGadget(i);
9965   }
9966
9967   // draw little border line below tabulator buttons
9968   if (tab_color != BLACK_PIXEL)                 // black => transparent
9969     FillRectangle(drawto, gd_gi1->x, gd_gi1->y + gd_gi1->height +
9970                   ED_GADGET_TINY_DISTANCE,
9971                   getTabulatorBarWidth(), getTabulatorBarHeight(), tab_color);
9972 }
9973
9974 static void PrintInfoText(char *text, int font_nr, int xpos, int ypos)
9975 {
9976   DrawText(SX + xpos, SY + ypos, text, font_nr);
9977 }
9978
9979 static int PrintElementDescriptionFromFile(char *filename, int font_nr,
9980                                            int xpos, int ypos)
9981 {
9982   int font_width = getFontWidth(font_nr);
9983   int font_height = getFontHeight(font_nr);
9984   int max_chars_per_line = (SXSIZE - 2 * xpos) / font_width;
9985   int max_lines_drawable = (SYSIZE - ypos) / font_height - 1;
9986
9987   return DrawTextFile(SX + xpos, SY + ypos, filename, font_nr,
9988                       max_chars_per_line, -1, max_lines_drawable, 0, -1,
9989                       TRUE, FALSE, FALSE);
9990 }
9991
9992 static void DrawLevelConfigLevel(void)
9993 {
9994   int i;
9995
9996   // draw counter gadgets
9997   for (i = ED_COUNTER_ID_LEVEL_FIRST; i <= ED_COUNTER_ID_LEVEL_LAST; i++)
9998     MapCounterButtons(i);
9999
10000   // draw checkbutton gadgets
10001   for (i = ED_CHECKBUTTON_ID_LEVEL_FIRST; i<= ED_CHECKBUTTON_ID_LEVEL_LAST; i++)
10002     MapCheckbuttonGadget(i);
10003
10004   // draw selectbox gadgets
10005   for (i = ED_SELECTBOX_ID_LEVEL_FIRST; i <= ED_SELECTBOX_ID_LEVEL_LAST; i++)
10006     MapSelectboxGadget(i);
10007
10008   // draw text input gadgets
10009   for (i = ED_TEXTINPUT_ID_LEVEL_FIRST; i <= ED_TEXTINPUT_ID_LEVEL_LAST; i++)
10010     MapTextInputGadget(i);
10011 }
10012
10013 static char *getLevelSubdirFromSaveMode(int save_mode)
10014 {
10015   if (save_mode == LEVELSET_SAVE_MODE_CREATE)
10016     return getNewUserLevelSubdir();
10017
10018   return leveldir_current->subdir;
10019 }
10020
10021 static void DrawLevelConfigLevelSet_DirectoryInfo(void)
10022 {
10023   char *directory_text = "Level set directory:";
10024   char *directory_name = getLevelSubdirFromSaveMode(levelset_save_mode);
10025   int font1_nr = FONT_TEXT_1;
10026   int font2_nr = FONT_TEXT_2;
10027   int font1_height = getFontHeight(font1_nr);
10028   int yoffset_above = font1_height + ED_GADGET_LINE_DISTANCE;
10029   int x = ED_LEVEL_SETTINGS_X(0);
10030   int y = ED_LEVEL_SETTINGS_Y(6);
10031
10032   PrintInfoText(directory_text, font1_nr, x, y - yoffset_above);
10033   PrintInfoText(directory_name, font2_nr, x, y);
10034 }
10035
10036 static void DrawLevelConfigLevelSet(void)
10037 {
10038   boolean artwork_exists = checkIfCustomArtworkExistsForCurrentLevelSet();
10039   boolean template_exists = fileExists(getLocalLevelTemplateFilename());
10040   int i;
10041
10042   // draw counter gadgets
10043   for (i = ED_COUNTER_ID_LEVELSET_FIRST; i <= ED_COUNTER_ID_LEVELSET_LAST; i++)
10044     MapCounterButtons(i);
10045
10046   // draw checkbutton gadgets
10047   for (i = ED_CHECKBUTTON_ID_LEVELSET_FIRST; i <= ED_CHECKBUTTON_ID_LEVELSET_LAST; i++)
10048   {
10049     if (levelset_save_mode == LEVELSET_SAVE_MODE_UPDATE ||
10050         (i == ED_CHECKBUTTON_ID_USE_LEVELSET_ARTWORK && !artwork_exists) ||
10051         (i == ED_CHECKBUTTON_ID_COPY_LEVEL_TEMPLATE  && !template_exists))
10052       continue;
10053
10054     MapCheckbuttonGadget(i);
10055   }
10056
10057   // draw selectbox gadgets
10058   for (i = ED_SELECTBOX_ID_LEVELSET_FIRST; i <= ED_SELECTBOX_ID_LEVELSET_LAST; i++)
10059     MapSelectboxGadget(i);
10060
10061   // draw text input gadgets
10062   for (i = ED_TEXTINPUT_ID_LEVELSET_FIRST; i <= ED_TEXTINPUT_ID_LEVELSET_LAST; i++)
10063     MapTextInputGadget(i);
10064
10065   // draw textbutton gadgets
10066   MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_LEVELSET);
10067
10068   // draw info text
10069   DrawLevelConfigLevelSet_DirectoryInfo();
10070 }
10071
10072 static void DrawLevelConfigEditor(void)
10073 {
10074   int i;
10075
10076   // draw counter gadgets
10077   for (i = ED_COUNTER_ID_EDITOR_FIRST; i <= ED_COUNTER_ID_EDITOR_LAST; i++)
10078     MapCounterButtons(i);
10079
10080   // draw checkbutton gadgets
10081   for (i = ED_CHECKBUTTON_ID_EDITOR_FIRST; i <= ED_CHECKBUTTON_ID_EDITOR_LAST; i++)
10082     MapCheckbuttonGadget(i);
10083
10084   // draw radiobutton gadgets
10085   for (i = ED_RADIOBUTTON_ID_EDITOR_FIRST; i <= ED_RADIOBUTTON_ID_EDITOR_LAST; i++)
10086     MapRadiobuttonGadget(i);
10087
10088   // draw drawing area
10089   MapDrawingArea(ED_DRAWING_ID_RANDOM_BACKGROUND);
10090
10091   // draw textbutton gadgets
10092   MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2);
10093 }
10094
10095 static void DrawLevelConfigEngine(void)
10096 {
10097   int i;
10098
10099   // draw counter gadgets
10100   if (level.bd_scheduling_type == GD_SCHEDULING_MILLISECONDS)
10101   {
10102     MapCounterButtons(ED_COUNTER_ID_BD_CYCLE_DELAY_MS);
10103     MapCounterButtons(ED_COUNTER_ID_BD_HATCHING_DELAY_CYCLES);
10104   }
10105   else
10106   {
10107     MapCounterButtons(ED_COUNTER_ID_BD_CYCLE_DELAY_C64);
10108     MapCounterButtons(ED_COUNTER_ID_BD_HATCHING_DELAY_SECONDS);
10109   }
10110
10111   // draw checkbutton gadgets
10112   for (i = ED_CHECKBUTTON_ID_ENGINE_FIRST; i <= ED_CHECKBUTTON_ID_ENGINE_LAST; i++)
10113     MapCheckbuttonGadget(i);
10114
10115   // draw selectbox gadgets
10116   for (i = ED_SELECTBOX_ID_ENGINE_FIRST; i <= ED_SELECTBOX_ID_ENGINE_LAST; i++)
10117     MapSelectboxGadget(i);
10118 }
10119
10120 static void DrawLevelConfigWindow(void)
10121 {
10122   char *text = "Global Settings";
10123   int font_nr = FONT_TITLE_1;
10124   struct MenuPosInfo *pos = &editor.settings.headline;
10125   int sx = SX + ALIGNED_XPOS(pos->x, getTextWidth(text, font_nr), pos->align);
10126   int sy = SY + pos->y;
10127
10128   stick_element_properties_window = FALSE;
10129
10130   SetAutomaticNumberOfGemsNeeded();
10131
10132   UnmapLevelEditorFieldGadgets();
10133
10134   SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
10135   ClearField();
10136
10137   DrawText(sx, sy, text, font_nr);
10138
10139   DrawLevelConfigTabulatorGadgets();
10140
10141   if (edit_mode_levelconfig == ED_MODE_LEVELCONFIG_LEVEL)
10142     DrawLevelConfigLevel();
10143   else if (edit_mode_levelconfig == ED_MODE_LEVELCONFIG_LEVELSET)
10144     DrawLevelConfigLevelSet();
10145   else if (edit_mode_levelconfig == ED_MODE_LEVELCONFIG_EDITOR)
10146     DrawLevelConfigEditor();
10147   else if (edit_mode_levelconfig == ED_MODE_LEVELCONFIG_ENGINE)
10148     DrawLevelConfigEngine();
10149 }
10150
10151 static void DrawCustomContentArea(void)
10152 {
10153   int id = ED_DRAWING_ID_CUSTOM_CONTENT;
10154   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
10155   int x1 = right_gadget_border[GADGET_ID_CUSTOM_DEADLINESS];
10156   int x2 = right_gadget_border[GADGET_ID_CUSTOM_EXPLOSION_TYPE];
10157   int x3 = right_gadget_border[GADGET_ID_CUSTOM_EXPLODE_IMPACT];
10158   int xoffset = ED_GADGET_SPACE_DISTANCE;
10159
10160   // add distance for potential left text (without drawing area border)
10161   x2 += getTextWidthForGadget(drawingarea_info[id].text_left);
10162
10163   ModifyGadget(gi, GDI_X, MAX(x1, MAX(x2, x3)) + xoffset, GDI_END);
10164
10165   MapDrawingArea(ED_DRAWING_ID_CUSTOM_CONTENT);
10166 }
10167
10168 static void DrawCustomChangeContentArea(void)
10169 {
10170   int id = ED_DRAWING_ID_CUSTOM_CHANGE_CONTENT;
10171   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
10172   int x1 = right_gadget_border[GADGET_ID_CHANGE_USE_CONTENT];
10173   int x2 = right_gadget_border[GADGET_ID_CHANGE_REPLACE_WHEN];
10174   int x3 = right_gadget_border[GADGET_ID_CHANGE_ONLY_COMPLETE];
10175   int xoffset = ED_GADGET_SPACE_DISTANCE;
10176
10177   ModifyGadget(gi, GDI_X, MAX(x1, MAX(x2, x3)) + xoffset, GDI_END);
10178
10179   MapDrawingArea(id);
10180 }
10181
10182 static void RemoveElementContentArea(int id, int font_height)
10183 {
10184   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10185
10186   DrawBackground(SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size,
10187                  SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size,
10188                  3 * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10189                  3 * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size +
10190                  ED_GADGET_TEXT_DISTANCE + font_height);
10191 }
10192
10193 static void DrawYamYamContentAreas(void)
10194 {
10195   int font_nr = FONT_TEXT_1;
10196   int font_height = getFontHeight(font_nr);
10197   int tilesize = ED_DRAWINGAREA_TILE_SIZE;
10198   int yoffset = (tilesize - font_height) / 2;
10199   int x = SX + ED_AREA_YAMYAM_CONTENT_X(3) + 4 * tilesize;
10200   int y = SY + ED_AREA_YAMYAM_CONTENT_Y(3) + yoffset;
10201   int i;
10202
10203   // display counter to choose number of element content areas
10204   MapCounterButtons(ED_COUNTER_ID_YAMYAM_CONTENT);
10205
10206   for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
10207   {
10208     int id = ED_DRAWING_ID_YAMYAM_CONTENT_0 + i;
10209
10210     if (i < level.num_yamyam_contents)
10211     {
10212       MapDrawingArea(id);
10213     }
10214     else
10215     {
10216       UnmapDrawingArea(id);
10217
10218       // delete content areas in case of reducing number of them
10219       RemoveElementContentArea(id, font_height);
10220     }
10221   }
10222
10223   DrawText(x, y + 0 * tilesize, "content", font_nr);
10224   DrawText(x, y + 1 * tilesize, "when",    font_nr);
10225   DrawText(x, y + 2 * tilesize, "smashed", font_nr);
10226 }
10227
10228 static void DrawMagicBallContentAreas(void)
10229 {
10230   int font_nr = FONT_TEXT_1;
10231   int font_height = getFontHeight(font_nr);
10232   int tilesize = ED_DRAWINGAREA_TILE_SIZE;
10233   int yoffset = (tilesize - font_height) / 2;
10234   int x = SX + ED_AREA_MAGIC_BALL_CONTENT_X(3) + 4 * tilesize;
10235   int y = SY + ED_AREA_MAGIC_BALL_CONTENT_Y(3) + yoffset;
10236   int i;
10237
10238   // display counter to choose number of element content areas
10239   MapCounterButtons(ED_COUNTER_ID_BALL_CONTENT);
10240
10241   for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
10242   {
10243     int id = ED_DRAWING_ID_MAGIC_BALL_CONTENT_0 + i;
10244
10245     if (i < level.num_ball_contents)
10246     {
10247       MapDrawingArea(id);
10248     }
10249     else
10250     {
10251       UnmapDrawingArea(id);
10252
10253       // delete content areas in case of reducing number of them
10254       RemoveElementContentArea(id, font_height);
10255     }
10256   }
10257
10258   DrawText(x, y + 0 * tilesize, "generated", font_nr);
10259   DrawText(x, y + 1 * tilesize, "when",      font_nr);
10260   DrawText(x, y + 2 * tilesize, "active",    font_nr);
10261 }
10262
10263 static void DrawAndroidElementArea(void)
10264 {
10265   int id = ED_DRAWING_ID_ANDROID_CONTENT;
10266   int num_elements = level.num_android_clone_elements;
10267   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10268   int sx = SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size;
10269   int sy = SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size;
10270   int xsize = MAX_ANDROID_ELEMENTS;
10271   int ysize = 1;
10272
10273   // display counter to choose number of element areas
10274   MapCounterButtons(ED_COUNTER_ID_ANDROID_CONTENT);
10275
10276   if (drawingarea_info[id].text_left != NULL)
10277     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
10278
10279   UnmapDrawingArea(id);
10280
10281   ModifyEditorDrawingArea(id, num_elements, 1);
10282
10283   // delete content areas in case of reducing number of them
10284   DrawBackground(sx, sy,
10285                  xsize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10286                  ysize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size);
10287
10288   MapDrawingArea(id);
10289 }
10290
10291 static void DrawGroupElementArea(void)
10292 {
10293   int id = ED_DRAWING_ID_GROUP_CONTENT;
10294   int num_elements = group_element_info.num_elements;
10295   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10296   int sx = SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size;
10297   int sy = SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size;
10298   int xsize = MAX_ELEMENTS_IN_GROUP;
10299   int ysize = 1;
10300
10301   if (drawingarea_info[id].text_left != NULL)
10302     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
10303
10304   UnmapDrawingArea(id);
10305
10306   ModifyEditorDrawingArea(id, num_elements, 1);
10307
10308   // delete content areas in case of reducing number of them
10309   DrawBackground(sx, sy,
10310                  xsize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10311                  ysize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size);
10312
10313   MapDrawingArea(id);
10314 }
10315
10316 static void DrawPlayerInitialInventoryArea(int element)
10317 {
10318   int id = ED_DRAWING_ID_INVENTORY_CONTENT;
10319   int player_nr = GET_PLAYER_NR(element);
10320   int num_elements = level.initial_inventory_size[player_nr];
10321   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10322   int sx = SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size;
10323   int sy = SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size;
10324   int xsize = MAX_INITIAL_INVENTORY_SIZE;
10325   int ysize = 1;
10326
10327   // determine horizontal position to the right of specified gadget
10328   if (drawingarea_info[id].gadget_id_align != GADGET_ID_NONE)
10329     sx = (right_gadget_border[drawingarea_info[id].gadget_id_align] +
10330           ED_DRAWINGAREA_TEXT_DISTANCE);
10331
10332   // determine horizontal offset for leading text
10333   if (drawingarea_info[id].text_left != NULL)
10334     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
10335
10336   UnmapDrawingArea(id);
10337
10338   ModifyEditorDrawingArea(id, num_elements, 1);
10339
10340   // delete content areas in case of reducing number of them
10341   DrawBackground(sx, sy,
10342                  xsize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10343                  ysize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size);
10344
10345   MapDrawingArea(id);
10346 }
10347
10348 static void DrawMMBallContentArea(void)
10349 {
10350   int id = ED_DRAWING_ID_MM_BALL_CONTENT;
10351   int num_elements = level.num_mm_ball_contents;
10352   int border_size = ED_DRAWINGAREA_BORDER_SIZE;
10353   int sx = SX + ED_AREA_SETTINGS_X(drawingarea_info[id]) - border_size;
10354   int sy = SY + ED_AREA_SETTINGS_Y(drawingarea_info[id]) - border_size;
10355   int xsize = MAX_MM_BALL_CONTENTS;
10356   int ysize = 1;
10357
10358   if (drawingarea_info[id].text_left != NULL)
10359     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
10360
10361   UnmapDrawingArea(id);
10362
10363   ModifyEditorDrawingArea(id, num_elements, 1);
10364
10365   // delete content areas in case of reducing number of them
10366   DrawBackground(sx, sy,
10367                  xsize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size,
10368                  ysize * ED_DRAWINGAREA_TILE_SIZE + 2 * border_size);
10369
10370   MapDrawingArea(id);
10371 }
10372
10373 static void DrawEnvelopeTextArea(int envelope_nr)
10374 {
10375   int id = ED_TEXTAREA_ID_ENVELOPE_INFO;
10376   struct GadgetInfo *gi = level_editor_gadget[textarea_info[id].gadget_id];
10377
10378   UnmapGadget(gi);
10379
10380   DrawBackground(gi->x, gi->y,
10381                  gi->textarea.crop_width, gi->textarea.crop_height);
10382
10383   if (envelope_nr != -1)
10384     textarea_info[id].value = level.envelope[envelope_nr].text;
10385
10386   ModifyGadget(gi, GDI_AREA_SIZE,
10387                *counterbutton_info[ED_COUNTER_ID_ENVELOPE_XSIZE].value,
10388                *counterbutton_info[ED_COUNTER_ID_ENVELOPE_YSIZE].value,
10389                GDI_END);
10390
10391   MapTextAreaGadget(ED_TEXTAREA_ID_ENVELOPE_INFO);
10392 }
10393
10394 static void DrawPropertiesInfo(void)
10395 {
10396   static struct
10397   {
10398     int value;
10399     char *text;
10400   }
10401   properties[] =
10402   {
10403     // configurable properties
10404
10405     { EP_WALKABLE_OVER,         "- player can walk over it"             },
10406     { EP_WALKABLE_INSIDE,       "- player can walk inside it"           },
10407     { EP_WALKABLE_UNDER,        "- player can walk under it"            },
10408     { EP_PASSABLE_OVER,         "- player can pass over it"             },
10409     { EP_PASSABLE_INSIDE,       "- player can pass through it"          },
10410     { EP_PASSABLE_UNDER,        "- player can pass under it"            },
10411     { EP_PROTECTED,             "- player is protected by it"           },
10412
10413     { EP_DIGGABLE,              "- can be digged away"                  },
10414     { EP_COLLECTIBLE,           "- can be collected"                    },
10415     { EP_DROPPABLE,             "- can be dropped after collecting"     },
10416     { EP_THROWABLE,             "- can be thrown after collecting"      },
10417     { EP_PUSHABLE,              "- can be pushed"                       },
10418
10419     { EP_CAN_FALL,              "- can fall"                            },
10420     { EP_CAN_MOVE,              "- can move"                            },
10421
10422     { EP_CAN_SMASH_PLAYER,      "- can smash player"                    },
10423 #if 0
10424     { EP_CAN_SMASH_ENEMIES,     "- can smash good and bad guys"         },
10425 #endif
10426     { EP_CAN_SMASH_EVERYTHING,  "- can smash everything smashable"      },
10427
10428     { EP_SLIPPERY,              "- slippery for falling elements"       },
10429     { EP_EM_SLIPPERY_WALL,      "- slippery for some gems (EM style)"   },
10430
10431     { EP_DONT_RUN_INTO,         "- deadly when running into"            },
10432     { EP_DONT_COLLIDE_WITH,     "- deadly when colliding with"          },
10433     { EP_DONT_GET_HIT_BY,       "- deadly when getting hit by"          },
10434     { EP_DONT_TOUCH,            "- deadly when touching"                },
10435
10436     { EP_INDESTRUCTIBLE,        "- indestructible"                      },
10437
10438     { EP_CAN_EXPLODE_BY_FIRE,   "- can explode by fire or explosions"   },
10439     { EP_CAN_EXPLODE_SMASHED,   "- can explode when smashed"            },
10440     { EP_CAN_EXPLODE_IMPACT,    "- can explode on impact"               },
10441
10442     { EP_CAN_CHANGE,            "- can change to other element"         },
10443
10444     // pre-defined properties
10445     { EP_CAN_PASS_MAGIC_WALL,   "- can pass magic walls"                },
10446     { EP_CAN_PASS_DC_MAGIC_WALL,"- can pass magic walls (DC style)"     },
10447     { EP_SWITCHABLE,            "- can be switched"                     },
10448 #if 0
10449     { EP_HAS_EDITOR_CONTENT,    "- can contain other elements"          },
10450 #endif
10451
10452     { -1,                       NULL                                    }
10453   };
10454   char *filename = getElementDescriptionFilename(properties_element);
10455   char *num_elements_text = "In this level: ";
10456   char *num_similar_text = "Similar tiles: ";
10457   char *properties_text = "Standard properties: ";
10458   char *description_text = "Description:";
10459   char *no_description_text = "No description available.";
10460   char *none_text = "None";
10461   float percentage;
10462   int num_elements_in_level = 0;
10463   int num_similar_in_level = 0;
10464   int num_hires_tiles_in_level = 0;
10465   int num_standard_properties = 0;
10466   int font1_nr = FONT_TEXT_1;
10467   int font2_nr = FONT_TEXT_2;
10468   int font1_width = getFontWidth(font1_nr);
10469   int font1_height = getFontHeight(font1_nr);
10470   int font2_height = getFontHeight(font2_nr);
10471   int line1_height = font1_height + ED_GADGET_LINE_DISTANCE;
10472   int font2_yoffset = (font1_height - font2_height) / 2;
10473   int num_elements_text_len = strlen(num_elements_text) * font1_width;
10474   int num_similar_text_len = strlen(num_similar_text) * font1_width;
10475   int properties_text_len = strlen(properties_text) * font1_width;
10476   int xpos = ED_ELEMENT_SETTINGS_X(0);
10477   int ypos = ED_ELEMENT_SETTINGS_Y(0) + ED_GADGET_SMALL_DISTANCE;
10478   int i, x, y;
10479
10480   if (setup.editor.show_element_token)
10481   {
10482     int font3_nr = FONT_TEXT_3;
10483     int font3_height = getFontHeight(font3_nr);
10484
10485     DrawTextF(xpos, ypos, font3_nr,
10486               "[%s]", element_info[properties_element].token_name);
10487
10488     ypos += 2 * font3_height;
10489   }
10490
10491   // ----- print number of elements / percentage of this element in level
10492
10493   for (y = 0; y < lev_fieldy; y++)
10494   {
10495     for (x = 0; x < lev_fieldx; x++)
10496     {
10497       if (Tile[x][y] == properties_element)
10498       {
10499         num_elements_in_level++;
10500       }
10501       else if (IS_MM_WALL(Tile[x][y]) &&
10502                map_mm_wall_element(Tile[x][y]) == properties_element)
10503       {
10504         num_hires_tiles_in_level += numHiresTiles(Tile[x][y]);
10505       }
10506     }
10507   }
10508
10509   percentage = num_elements_in_level * 100.0 / (lev_fieldx * lev_fieldy);
10510
10511   DrawTextS(xpos, ypos, font1_nr, num_elements_text);
10512
10513   if (num_hires_tiles_in_level > 0)
10514     DrawTextF(xpos + num_elements_text_len, ypos + font2_yoffset, font2_nr,
10515               "%d wall tiles", num_hires_tiles_in_level);
10516   else if (num_elements_in_level > 0)
10517     DrawTextF(xpos + num_elements_text_len, ypos + font2_yoffset, font2_nr,
10518               "%d (%.2f %%)", num_elements_in_level, percentage);
10519   else
10520     DrawTextF(xpos + num_elements_text_len, ypos + font2_yoffset, font2_nr,
10521               none_text);
10522
10523   // ----- print number of similar elements / percentage of them in level
10524
10525   for (y = 0; y < lev_fieldy; y++)
10526   {
10527     for (x = 0; x < lev_fieldx; x++)
10528     {
10529       if (strEqual(element_info[Tile[x][y]].class_name,
10530                    element_info[properties_element].class_name))
10531       {
10532         num_similar_in_level++;
10533       }
10534     }
10535   }
10536
10537   if (num_similar_in_level != num_elements_in_level)
10538   {
10539     ypos += 1 * MAX(font1_height, font2_height);
10540
10541     percentage = num_similar_in_level * 100.0 / (lev_fieldx * lev_fieldy);
10542
10543     DrawTextS(xpos, ypos, font1_nr, num_similar_text);
10544
10545     if (num_similar_in_level > 0)
10546       DrawTextF(xpos + num_similar_text_len, ypos + font2_yoffset, font2_nr,
10547                 "%d (%.2f %%)", num_similar_in_level, percentage);
10548     else
10549       DrawTextF(xpos + num_similar_text_len, ypos + font2_yoffset, font2_nr,
10550                 none_text);
10551   }
10552
10553   ypos += 2 * MAX(font1_height, font2_height);
10554
10555   // ----- print standard properties of this element
10556
10557   DrawTextS(xpos, ypos, font1_nr, properties_text);
10558
10559   ypos += line1_height;
10560
10561   for (i = 0; properties[i].value != -1; i++)
10562   {
10563     if (!HAS_PROPERTY(properties_element, properties[i].value))
10564       continue;
10565
10566     DrawTextS(xpos, ypos, font2_nr, properties[i].text);
10567
10568     ypos += font2_height;
10569
10570     num_standard_properties++;
10571   }
10572
10573   if (num_standard_properties == 0)
10574   {
10575     DrawTextS(xpos + properties_text_len, ypos - line1_height + font2_yoffset,
10576               font2_nr, none_text);
10577
10578     ypos -= (line1_height - font1_height);
10579   }
10580
10581   ypos += MAX(font1_height, font2_height);
10582
10583   // ----- print special description of this element
10584
10585   PrintInfoText(description_text, font1_nr, xpos, ypos);
10586
10587   ypos += line1_height;
10588
10589   if (PrintElementDescriptionFromFile(filename, font2_nr, xpos, ypos) == 0)
10590     PrintInfoText(no_description_text, font1_nr, xpos, ypos - line1_height);
10591 }
10592
10593 #define TEXT_COLLECTING         "Score for collecting"
10594 #define TEXT_COLLECTING_EXTRA   "Score for extra diamonds"
10595 #define TEXT_SMASHING           "Score for smashing"
10596 #define TEXT_SLURPING           "Score for slurping robot"
10597 #define TEXT_CRACKING           "Score for cracking"
10598 #define TEXT_AMOEBA_SPEED       "Speed of amoeba growth"
10599 #define TEXT_DURATION           "Duration when activated"
10600 #define TEXT_DELAY_ON           "Delay before activating"
10601 #define TEXT_DELAY_OFF          "Delay before deactivating"
10602 #define TEXT_DELAY_CHANGING     "Delay before changing"
10603 #define TEXT_DELAY_EXPLODING    "Delay before exploding"
10604 #define TEXT_DELAY_MOVING       "Delay before moving"
10605 #define TEXT_BALL_DELAY         "Element generation delay"
10606 #define TEXT_MOVE_SPEED         "Speed of android moving"
10607 #define TEXT_CLONE_SPEED        "Speed of android cloning"
10608 #define TEXT_GAME_OF_LIFE_1     "Min neighbours to survive"
10609 #define TEXT_GAME_OF_LIFE_2     "Max neighbours to survive"
10610 #define TEXT_GAME_OF_LIFE_3     "Min neighbours to create"
10611 #define TEXT_GAME_OF_LIFE_4     "Max neighbours to create"
10612 #define TEXT_TIME_BONUS         "Extra time to solve level"
10613
10614 static struct
10615 {
10616   int element;
10617   int *value;
10618   char *text;
10619 } elements_with_counter[] =
10620 {
10621   { EL_EMERALD,                 &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10622   { EL_BD_DIAMOND,              &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10623   { EL_BD_DIAMOND,              &level.score[SC_DIAMOND_EXTRA], TEXT_COLLECTING_EXTRA   },
10624   { EL_EMERALD_YELLOW,          &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10625   { EL_EMERALD_RED,             &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10626   { EL_EMERALD_PURPLE,          &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10627   { EL_SP_INFOTRON,             &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10628   { EL_DIAMOND,                 &level.score[SC_DIAMOND],       TEXT_COLLECTING         },
10629   { EL_CRYSTAL,                 &level.score[SC_CRYSTAL],       TEXT_COLLECTING         },
10630   { EL_PEARL,                   &level.score[SC_PEARL],         TEXT_COLLECTING         },
10631   { EL_BUG,                     &level.score[SC_BUG],           TEXT_SMASHING           },
10632   { EL_BUG_RIGHT,               &level.score[SC_BUG],           TEXT_SMASHING           },
10633   { EL_BUG_UP,                  &level.score[SC_BUG],           TEXT_SMASHING           },
10634   { EL_BUG_LEFT,                &level.score[SC_BUG],           TEXT_SMASHING           },
10635   { EL_BUG_DOWN,                &level.score[SC_BUG],           TEXT_SMASHING           },
10636   { EL_BD_BUTTERFLY,            &level.score[SC_BUG],           TEXT_SMASHING           },
10637   { EL_BD_BUTTERFLY_RIGHT,      &level.score[SC_BUG],           TEXT_SMASHING           },
10638   { EL_BD_BUTTERFLY_UP,         &level.score[SC_BUG],           TEXT_SMASHING           },
10639   { EL_BD_BUTTERFLY_LEFT,       &level.score[SC_BUG],           TEXT_SMASHING           },
10640   { EL_BD_BUTTERFLY_DOWN,       &level.score[SC_BUG],           TEXT_SMASHING           },
10641   { EL_SP_ELECTRON,             &level.score[SC_BUG],           TEXT_SMASHING           },
10642   { EL_SPACESHIP,               &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10643   { EL_SPACESHIP_RIGHT,         &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10644   { EL_SPACESHIP_UP,            &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10645   { EL_SPACESHIP_LEFT,          &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10646   { EL_SPACESHIP_DOWN,          &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10647   { EL_BD_FIREFLY,              &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10648   { EL_BD_FIREFLY_RIGHT,        &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10649   { EL_BD_FIREFLY_UP,           &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10650   { EL_BD_FIREFLY_LEFT,         &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10651   { EL_BD_FIREFLY_DOWN,         &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10652   { EL_SP_SNIKSNAK,             &level.score[SC_SPACESHIP],     TEXT_SMASHING           },
10653   { EL_YAMYAM,                  &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10654   { EL_YAMYAM_LEFT,             &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10655   { EL_YAMYAM_RIGHT,            &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10656   { EL_YAMYAM_UP,               &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10657   { EL_YAMYAM_DOWN,             &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10658   { EL_DARK_YAMYAM,             &level.score[SC_YAMYAM],        TEXT_SMASHING           },
10659   { EL_ROBOT,                   &level.score[SC_ROBOT],         TEXT_SMASHING           },
10660   { EL_PACMAN,                  &level.score[SC_PACMAN],        TEXT_SMASHING           },
10661   { EL_PACMAN_RIGHT,            &level.score[SC_PACMAN],        TEXT_SMASHING           },
10662   { EL_PACMAN_UP,               &level.score[SC_PACMAN],        TEXT_SMASHING           },
10663   { EL_PACMAN_LEFT,             &level.score[SC_PACMAN],        TEXT_SMASHING           },
10664   { EL_PACMAN_DOWN,             &level.score[SC_PACMAN],        TEXT_SMASHING           },
10665   { EL_NUT,                     &level.score[SC_NUT],           TEXT_CRACKING           },
10666   { EL_DYNAMITE,                &level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10667   { EL_EM_DYNAMITE,             &level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10668   { EL_DYNABOMB_INCREASE_NUMBER,&level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10669   { EL_DYNABOMB_INCREASE_SIZE,  &level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10670   { EL_DYNABOMB_INCREASE_POWER, &level.score[SC_DYNAMITE],      TEXT_COLLECTING         },
10671   { EL_SHIELD_NORMAL,           &level.score[SC_SHIELD],        TEXT_COLLECTING         },
10672   { EL_SHIELD_DEADLY,           &level.score[SC_SHIELD],        TEXT_COLLECTING         },
10673   { EL_EXTRA_TIME,              &level.extra_time_score,        TEXT_COLLECTING         },
10674   { EL_KEY_1,                   &level.score[SC_KEY],           TEXT_COLLECTING         },
10675   { EL_KEY_2,                   &level.score[SC_KEY],           TEXT_COLLECTING         },
10676   { EL_KEY_3,                   &level.score[SC_KEY],           TEXT_COLLECTING         },
10677   { EL_KEY_4,                   &level.score[SC_KEY],           TEXT_COLLECTING         },
10678   { EL_EM_KEY_1,                &level.score[SC_KEY],           TEXT_COLLECTING         },
10679   { EL_EM_KEY_2,                &level.score[SC_KEY],           TEXT_COLLECTING         },
10680   { EL_EM_KEY_3,                &level.score[SC_KEY],           TEXT_COLLECTING         },
10681   { EL_EM_KEY_4,                &level.score[SC_KEY],           TEXT_COLLECTING         },
10682   { EL_EMC_KEY_5,               &level.score[SC_KEY],           TEXT_COLLECTING         },
10683   { EL_EMC_KEY_6,               &level.score[SC_KEY],           TEXT_COLLECTING         },
10684   { EL_EMC_KEY_7,               &level.score[SC_KEY],           TEXT_COLLECTING         },
10685   { EL_EMC_KEY_8,               &level.score[SC_KEY],           TEXT_COLLECTING         },
10686   { EL_DC_KEY_WHITE,            &level.score[SC_KEY],           TEXT_COLLECTING         },
10687   { EL_MM_KETTLE,               &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10688   { EL_DF_CELL,                 &level.score[SC_EMERALD],       TEXT_COLLECTING         },
10689   { EL_MM_KEY,                  &level.score[SC_KEY],           TEXT_COLLECTING         },
10690   { EL_MM_LIGHTBALL,            &level.score[SC_ELEM_BONUS],    TEXT_COLLECTING         },
10691   { EL_MM_PACMAN,               &level.score[SC_PACMAN],        TEXT_SMASHING           },
10692   { EL_MM_PACMAN_RIGHT,         &level.score[SC_PACMAN],        TEXT_SMASHING           },
10693   { EL_MM_PACMAN_UP,            &level.score[SC_PACMAN],        TEXT_SMASHING           },
10694   { EL_MM_PACMAN_LEFT,          &level.score[SC_PACMAN],        TEXT_SMASHING           },
10695   { EL_MM_PACMAN_DOWN,          &level.score[SC_PACMAN],        TEXT_SMASHING           },
10696   { EL_AMOEBA_WET,              &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10697   { EL_AMOEBA_DRY,              &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10698   { EL_AMOEBA_FULL,             &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10699   { EL_BD_AMOEBA,               &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10700   { EL_EMC_DRIPPER,             &level.amoeba_speed,            TEXT_AMOEBA_SPEED       },
10701   { EL_MAGIC_WALL,              &level.time_magic_wall,         TEXT_DURATION           },
10702   { EL_BD_MAGIC_WALL,           &level.time_magic_wall,         TEXT_DURATION           },
10703   { EL_DC_MAGIC_WALL,           &level.time_magic_wall,         TEXT_DURATION           },
10704   { EL_ROBOT_WHEEL,             &level.time_wheel,              TEXT_DURATION           },
10705   { EL_TIMEGATE_SWITCH,         &level.time_timegate,           TEXT_DURATION           },
10706   { EL_DC_TIMEGATE_SWITCH,      &level.time_timegate,           TEXT_DURATION           },
10707   { EL_LIGHT_SWITCH,            &level.time_light,              TEXT_DURATION           },
10708   { EL_LIGHT_SWITCH_ACTIVE,     &level.time_light,              TEXT_DURATION           },
10709   { EL_SHIELD_NORMAL,           &level.shield_normal_time,      TEXT_DURATION           },
10710   { EL_SHIELD_DEADLY,           &level.shield_deadly_time,      TEXT_DURATION           },
10711   { EL_EXTRA_TIME,              &level.extra_time,              TEXT_TIME_BONUS         },
10712   { EL_TIME_ORB_FULL,           &level.time_orb_time,           TEXT_TIME_BONUS         },
10713   { EL_GAME_OF_LIFE,            &level.game_of_life[0],         TEXT_GAME_OF_LIFE_1     },
10714   { EL_GAME_OF_LIFE,            &level.game_of_life[1],         TEXT_GAME_OF_LIFE_2     },
10715   { EL_GAME_OF_LIFE,            &level.game_of_life[2],         TEXT_GAME_OF_LIFE_3     },
10716   { EL_GAME_OF_LIFE,            &level.game_of_life[3],         TEXT_GAME_OF_LIFE_4     },
10717   { EL_BIOMAZE,                 &level.biomaze[0],              TEXT_GAME_OF_LIFE_1     },
10718   { EL_BIOMAZE,                 &level.biomaze[1],              TEXT_GAME_OF_LIFE_2     },
10719   { EL_BIOMAZE,                 &level.biomaze[2],              TEXT_GAME_OF_LIFE_3     },
10720   { EL_BIOMAZE,                 &level.biomaze[3],              TEXT_GAME_OF_LIFE_4     },
10721   { EL_EMC_ANDROID,             &level.android_move_time,       TEXT_MOVE_SPEED         },
10722   { EL_EMC_ANDROID,             &level.android_clone_time,      TEXT_CLONE_SPEED        },
10723   { EL_EMC_MAGIC_BALL,          &level.ball_time,               TEXT_BALL_DELAY         },
10724   { EL_EMC_LENSES,              &level.lenses_score,            TEXT_COLLECTING         },
10725   { EL_EMC_MAGNIFIER,           &level.magnify_score,           TEXT_COLLECTING         },
10726   { EL_SPRING,                  &level.slurp_score,             TEXT_SLURPING           },
10727   { EL_SPRING_LEFT,             &level.slurp_score,             TEXT_SLURPING           },
10728   { EL_SPRING_RIGHT,            &level.slurp_score,             TEXT_SLURPING           },
10729   { EL_EMC_LENSES,              &level.lenses_time,             TEXT_DURATION           },
10730   { EL_EMC_MAGNIFIER,           &level.magnify_time,            TEXT_DURATION           },
10731   { EL_MM_FUSE_ACTIVE,          &level.mm_time_fuse,            TEXT_DELAY_OFF          },
10732   { EL_MM_BOMB,                 &level.mm_time_bomb,            TEXT_DELAY_EXPLODING    },
10733   { EL_MM_GRAY_BALL,            &level.mm_time_ball,            TEXT_DELAY_CHANGING     },
10734   { EL_MM_STEEL_BLOCK,          &level.mm_time_block,           TEXT_DELAY_MOVING       },
10735   { EL_MM_WOODEN_BLOCK,         &level.mm_time_block,           TEXT_DELAY_MOVING       },
10736
10737   { -1,                         NULL,                           NULL                    }
10738 };
10739
10740 static boolean checkPropertiesConfig(int element)
10741 {
10742   int i;
10743
10744   // special case: empty space customization only available in R'n'D game engine
10745   if (element == EL_EMPTY_SPACE && level.game_engine_type != GAME_ENGINE_TYPE_RND)
10746     return FALSE;
10747
10748   if (IS_GEM(element) ||
10749       IS_CUSTOM_ELEMENT(element) ||
10750       IS_GROUP_ELEMENT(element) ||
10751       IS_EMPTY_ELEMENT(element) ||
10752       IS_BALLOON_ELEMENT(element) ||
10753       IS_ENVELOPE(element) ||
10754       IS_MM_ENVELOPE(element) ||
10755       IS_MM_MCDUFFIN(element) ||
10756       IS_DF_LASER(element) ||
10757       IS_PLAYER_ELEMENT(element) ||
10758       IS_BD_PLAYER_ELEMENT(element) ||
10759       IS_SOKOBAN_OBJECT_OR_FIELD(element) ||
10760       HAS_EDITOR_CONTENT(element) ||
10761       CAN_GROW(element) ||
10762       COULD_MOVE_INTO_ACID(element) ||
10763       MAYBE_DONT_COLLIDE_WITH(element))
10764     return TRUE;
10765   else
10766     for (i = 0; elements_with_counter[i].element != -1; i++)
10767       if (elements_with_counter[i].element == element)
10768         return TRUE;
10769
10770   return FALSE;
10771 }
10772
10773 static void SetAutomaticNumberOfGemsNeeded(void)
10774 {
10775   int x, y;
10776
10777   if (!level.auto_count_gems)
10778     return;
10779
10780   level.gems_needed = 0;
10781
10782   for (x = 0; x < lev_fieldx; x++)
10783   {
10784     for (y = 0; y < lev_fieldy; y++)
10785     {
10786       int element = Tile[x][y];
10787
10788       switch (element)
10789       {
10790         case EL_EMERALD:
10791         case EL_EMERALD_YELLOW:
10792         case EL_EMERALD_RED:
10793         case EL_EMERALD_PURPLE:
10794         case EL_BD_DIAMOND:
10795         case EL_WALL_EMERALD:
10796         case EL_WALL_EMERALD_YELLOW:
10797         case EL_WALL_EMERALD_RED:
10798         case EL_WALL_EMERALD_PURPLE:
10799         case EL_WALL_BD_DIAMOND:
10800         case EL_NUT:
10801         case EL_SP_INFOTRON:
10802         case EL_MM_KETTLE:
10803         case EL_DF_CELL:
10804           level.gems_needed++;
10805           break;
10806
10807         case EL_DIAMOND:
10808         case EL_WALL_DIAMOND:
10809           level.gems_needed += 3;
10810           break;
10811
10812         case EL_PEARL:
10813         case EL_WALL_PEARL:
10814           level.gems_needed += 5;
10815           break;
10816
10817         case EL_CRYSTAL:
10818         case EL_WALL_CRYSTAL:
10819           level.gems_needed += 8;
10820           break;
10821
10822         default:
10823           break;
10824       }
10825     }
10826   }
10827
10828   ModifyEditorCounterValue(ED_COUNTER_ID_LEVEL_GEMSLIMIT, level.gems_needed);
10829 }
10830
10831 static void DrawPropertiesConfig(void)
10832 {
10833   boolean draw_footer_line = FALSE;
10834   int max_num_element_counters = 4;
10835   int num_element_counters = 0;
10836   int i;
10837
10838   if (!checkPropertiesConfig(properties_element))
10839   {
10840     int xpos = ED_ELEMENT_SETTINGS_X(0);
10841     int ypos = ED_ELEMENT_SETTINGS_Y(0) + ED_GADGET_SMALL_DISTANCE;
10842
10843     PrintInfoText("No configuration options available.",
10844                   FONT_TEXT_1, xpos, ypos);
10845
10846     return;
10847   }
10848
10849   // check if there are elements where a value can be chosen for
10850   for (i = 0; elements_with_counter[i].element != -1; i++)
10851   {
10852     if (elements_with_counter[i].element == properties_element)
10853     {
10854       int counter_id = ED_COUNTER_ID_ELEMENT_VALUE1 + num_element_counters;
10855
10856       counterbutton_info[counter_id].y =
10857         ED_ELEMENT_SETTINGS_YPOS(
10858                 (HAS_EDITOR_CONTENT(properties_element)      ? 1 : 0) +
10859                 (CAN_GROW(properties_element)                ? 1 : 0) +
10860                 (COULD_MOVE_INTO_ACID(properties_element)    ? 1 : 0) +
10861                 (MAYBE_DONT_COLLIDE_WITH(properties_element) ? 1 : 0) +
10862                 (properties_element == EL_EMC_MAGIC_BALL     ? 2 : 0) +
10863                 num_element_counters);
10864
10865       counterbutton_info[counter_id].value = elements_with_counter[i].value;
10866       counterbutton_info[counter_id].text_right = elements_with_counter[i].text;
10867
10868       if (properties_element == EL_GAME_OF_LIFE ||
10869           properties_element == EL_BIOMAZE)
10870       {
10871         counterbutton_info[counter_id].min_value = 0;   // min neighbours
10872         counterbutton_info[counter_id].max_value = 8;   // max neighbours
10873       }
10874       else
10875       {
10876         // !!! CHANGE THIS FOR CERTAIN ELEMENTS !!!
10877         counterbutton_info[counter_id].min_value = MIN_SCORE;
10878         counterbutton_info[counter_id].max_value = MAX_SCORE;
10879       }
10880
10881       MapCounterButtons(counter_id);
10882
10883       num_element_counters++;
10884       if (num_element_counters >= max_num_element_counters)
10885         break;
10886     }
10887   }
10888
10889   if (HAS_EDITOR_CONTENT(properties_element))
10890   {
10891     // draw stickybutton gadget
10892     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
10893
10894     if (IS_AMOEBOID(properties_element))
10895       MapDrawingArea(ED_DRAWING_ID_AMOEBA_CONTENT);
10896     else if (properties_element == EL_YAMYAM ||
10897              properties_element == EL_YAMYAM_LEFT ||
10898              properties_element == EL_YAMYAM_RIGHT ||
10899              properties_element == EL_YAMYAM_UP ||
10900              properties_element == EL_YAMYAM_DOWN)
10901       DrawYamYamContentAreas();
10902     else if (properties_element == EL_EMC_MAGIC_BALL)
10903     {
10904       DrawMagicBallContentAreas();
10905
10906       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_RANDOM_BALL_CONTENT);
10907       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_INITIAL_BALL_ACTIVE);
10908     }
10909     else if (properties_element == EL_EMC_ANDROID)
10910       DrawAndroidElementArea();
10911     else if (properties_element == EL_MM_GRAY_BALL)
10912     {
10913       MapCounterButtons(ED_COUNTER_ID_MM_BALL_CONTENT);
10914       MapSelectboxGadget(ED_SELECTBOX_ID_MM_BALL_CHOICE_MODE);
10915       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_ROTATE_MM_BALL_CONTENT);
10916       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_EXPLODE_MM_BALL);
10917
10918       DrawMMBallContentArea();
10919     }
10920   }
10921
10922   if (IS_PLAYER_ELEMENT(properties_element))
10923   {
10924     int player_nr = GET_PLAYER_NR(properties_element);
10925
10926     // these properties can be set for every player individually
10927
10928     if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_1)
10929     {
10930       drawingarea_info[ED_DRAWING_ID_START_ELEMENT].value =
10931         &level.start_element[player_nr];
10932       drawingarea_info[ED_DRAWING_ID_ARTWORK_ELEMENT].value =
10933         &level.artwork_element[player_nr];
10934       drawingarea_info[ED_DRAWING_ID_EXPLOSION_ELEMENT].value =
10935         &level.explosion_element[player_nr];
10936
10937       checkbutton_info[ED_CHECKBUTTON_ID_USE_START_ELEMENT].value =
10938         &level.use_start_element[player_nr];
10939       checkbutton_info[ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT].value =
10940         &level.use_artwork_element[player_nr];
10941       checkbutton_info[ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT].value =
10942         &level.use_explosion_element[player_nr];
10943       checkbutton_info[ED_CHECKBUTTON_ID_INITIAL_GRAVITY].value =
10944         &level.initial_player_gravity[player_nr];
10945
10946       selectbox_info[ED_SELECTBOX_ID_PLAYER_SPEED].value =
10947         &level.initial_player_stepsize[player_nr];
10948
10949       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID);
10950       MapCheckbuttonGadget(properties_element == EL_SP_MURPHY ?
10951                            ED_CHECKBUTTON_ID_SP_BLOCK_LAST_FIELD :
10952                            ED_CHECKBUTTON_ID_BLOCK_LAST_FIELD);
10953       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BLOCK_SNAP_FIELD);
10954       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CONTINUOUS_SNAPPING);
10955       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_INSTANT_RELOCATION);
10956       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SHIFTED_RELOCATION);
10957       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_LAZY_RELOCATION);
10958       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_START_ELEMENT);
10959       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_ARTWORK_ELEMENT);
10960       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_EXPLOSION_ELEMENT);
10961       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_INITIAL_GRAVITY);
10962       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CAN_PASS_TO_WALKABLE);
10963       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SOLVED_BY_ONE_PLAYER);
10964
10965       MapDrawingArea(ED_DRAWING_ID_START_ELEMENT);
10966       MapDrawingArea(ED_DRAWING_ID_ARTWORK_ELEMENT);
10967       MapDrawingArea(ED_DRAWING_ID_EXPLOSION_ELEMENT);
10968
10969       MapSelectboxGadget(ED_SELECTBOX_ID_PLAYER_SPEED);
10970     }
10971     else if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_2)
10972     {
10973       drawingarea_info[ED_DRAWING_ID_INVENTORY_CONTENT].value =
10974         &level.initial_inventory_content[player_nr][0];
10975
10976       counterbutton_info[ED_COUNTER_ID_INVENTORY_SIZE].value =
10977         &level.initial_inventory_size[player_nr];
10978
10979       checkbutton_info[ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY].value =
10980         &level.use_initial_inventory[player_nr];
10981
10982       // draw checkbutton gadgets
10983       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_INITIAL_INVENTORY);
10984       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_FINISH_DIG_COLLECT);
10985       MapCheckbuttonGadget(ED_CHECKBUTTON_ID_KEEP_WALKABLE_CE);
10986
10987       // draw counter gadgets
10988       MapCounterButtons(ED_COUNTER_ID_INVENTORY_SIZE);
10989
10990       // draw drawing area gadgets
10991       DrawPlayerInitialInventoryArea(properties_element);
10992     }
10993   }
10994
10995   if (IS_BD_PLAYER_ELEMENT(properties_element))
10996     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_BD_DIAGONAL_MOVEMENTS);
10997
10998   // special case: slippery walls option for gems only available in R'n'D game engine
10999   if (IS_GEM(properties_element) && level.game_engine_type == GAME_ENGINE_TYPE_RND)
11000   {
11001     checkbutton_info[ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS].y =
11002       ED_ELEMENT_SETTINGS_XPOS(properties_element == EL_BD_DIAMOND ? 2 : 1);
11003
11004     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS);
11005   }
11006
11007   if (properties_element == EL_EM_DYNAMITE)
11008     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_EM_EXPLODES_BY_FIRE);
11009
11010   if (COULD_MOVE_INTO_ACID(properties_element) &&
11011       !IS_PLAYER_ELEMENT(properties_element) &&
11012       (!IS_CUSTOM_ELEMENT(properties_element) ||
11013        edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_2))
11014   {
11015     // set position for checkbutton for "can move into acid"
11016     checkbutton_info[ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID].x =
11017       ED_ELEMENT_SETTINGS_XPOS(IS_CUSTOM_ELEMENT(properties_element) ? 1 : 0);
11018     checkbutton_info[ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID].y =
11019       ED_ELEMENT_SETTINGS_YPOS(IS_CUSTOM_ELEMENT(properties_element) ? 7 :
11020                                IS_BALLOON_ELEMENT(properties_element) ||
11021                                HAS_EDITOR_CONTENT(properties_element) ? 1 : 0);
11022
11023     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID);
11024   }
11025
11026   if (MAYBE_DONT_COLLIDE_WITH(properties_element))
11027     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH);
11028
11029   if (properties_element == EL_SPRING ||
11030       properties_element == EL_SPRING_LEFT ||
11031       properties_element == EL_SPRING_RIGHT)
11032     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_SPRING_BUG);
11033
11034   if (properties_element == EL_TIME_ORB_FULL)
11035     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_TIME_ORB_BUG);
11036
11037   if (properties_element == EL_GAME_OF_LIFE ||
11038       properties_element == EL_BIOMAZE)
11039     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_USE_LIFE_BUGS);
11040
11041   if (CAN_GROW(properties_element))
11042   {
11043     checkbutton_info[ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE].y =
11044       ED_ELEMENT_SETTINGS_YPOS(HAS_EDITOR_CONTENT(properties_element) ? 1 : 0);
11045
11046     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_GROW_INTO_DIGGABLE);
11047   }
11048
11049   if (properties_element == EL_SOKOBAN_FIELD_EMPTY)
11050     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SB_FIELDS_NEEDED);
11051
11052   if (properties_element == EL_SOKOBAN_OBJECT)
11053     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_SB_OBJECTS_NEEDED);
11054
11055   if (properties_element == EL_SOKOBAN_OBJECT ||
11056       properties_element == EL_SOKOBAN_FIELD_EMPTY ||
11057       properties_element == EL_SOKOBAN_FIELD_FULL)
11058   {
11059     checkbutton_info[ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN].y =
11060       ED_ELEMENT_SETTINGS_XPOS(properties_element == EL_SOKOBAN_FIELD_FULL ?
11061                                0 : 1);
11062
11063     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_AUTO_EXIT_SOKOBAN);
11064   }
11065
11066   if (IS_BALLOON_ELEMENT(properties_element))
11067     MapSelectboxGadget(ED_SELECTBOX_ID_WIND_DIRECTION);
11068
11069   if (IS_ENVELOPE(properties_element) ||
11070       IS_MM_ENVELOPE(properties_element))
11071   {
11072     int counter1_id = ED_COUNTER_ID_ENVELOPE_XSIZE;
11073     int counter2_id = ED_COUNTER_ID_ENVELOPE_YSIZE;
11074     int button1_id = ED_CHECKBUTTON_ID_ENVELOPE_AUTOWRAP;
11075     int button2_id = ED_CHECKBUTTON_ID_ENVELOPE_CENTERED;
11076     int envelope_nr = ENVELOPE_NR(properties_element);
11077
11078     counterbutton_info[counter1_id].value = &level.envelope[envelope_nr].xsize;
11079     counterbutton_info[counter2_id].value = &level.envelope[envelope_nr].ysize;
11080
11081     checkbutton_info[button1_id].value = &level.envelope[envelope_nr].autowrap;
11082     checkbutton_info[button2_id].value = &level.envelope[envelope_nr].centered;
11083
11084     // display counter to choose size of envelope text area
11085     MapCounterButtons(counter1_id);
11086     MapCounterButtons(counter2_id);
11087
11088     // display checkbuttons to choose auto-wrap and alignment properties
11089     MapCheckbuttonGadget(button1_id);
11090     MapCheckbuttonGadget(button2_id);
11091
11092     DrawEnvelopeTextArea(envelope_nr);
11093   }
11094
11095   if (IS_MM_MCDUFFIN(properties_element))
11096   {
11097     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_MM_LASER_RED);
11098     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_MM_LASER_GREEN);
11099     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_MM_LASER_BLUE);
11100   }
11101
11102   if (IS_DF_LASER(properties_element))
11103   {
11104     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DF_LASER_RED);
11105     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DF_LASER_GREEN);
11106     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_DF_LASER_BLUE);
11107   }
11108
11109   if (IS_CUSTOM_ELEMENT(properties_element))
11110   {
11111     // draw stickybutton gadget
11112     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11113
11114     if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_1)
11115     {
11116       // draw checkbutton gadgets
11117       for (i =  ED_CHECKBUTTON_ID_CUSTOM1_FIRST;
11118            i <= ED_CHECKBUTTON_ID_CUSTOM1_LAST; i++)
11119         MapCheckbuttonGadget(i);
11120
11121       // draw counter gadgets
11122       for (i =  ED_COUNTER_ID_CUSTOM1_FIRST;
11123            i <= ED_COUNTER_ID_CUSTOM1_LAST; i++)
11124         MapCounterButtons(i);
11125
11126       // draw selectbox gadgets
11127       for (i =  ED_SELECTBOX_ID_CUSTOM1_FIRST;
11128            i <= ED_SELECTBOX_ID_CUSTOM1_LAST; i++)
11129         MapSelectboxGadget(i);
11130
11131       // draw textbutton gadgets
11132       MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1);
11133
11134       // draw text input gadgets
11135       MapTextInputGadget(ED_TEXTINPUT_ID_ELEMENT_NAME);
11136
11137       // draw drawing area gadgets
11138       MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
11139
11140       draw_footer_line = TRUE;
11141     }
11142     else if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_2)
11143     {
11144       // draw checkbutton gadgets
11145       for (i =  ED_CHECKBUTTON_ID_CUSTOM2_FIRST;
11146            i <= ED_CHECKBUTTON_ID_CUSTOM2_LAST; i++)
11147         MapCheckbuttonGadget(i);
11148
11149       // draw counter gadgets
11150       for (i =  ED_COUNTER_ID_CUSTOM2_FIRST;
11151            i <= ED_COUNTER_ID_CUSTOM2_LAST; i++)
11152         MapCounterButtons(i);
11153
11154       // draw selectbox gadgets
11155       for (i =  ED_SELECTBOX_ID_CUSTOM2_FIRST;
11156            i <= ED_SELECTBOX_ID_CUSTOM2_LAST; i++)
11157         MapSelectboxGadget(i);
11158
11159       // draw drawing area gadgets
11160       MapDrawingArea(ED_DRAWING_ID_CUSTOM_MOVE_ENTER);
11161       MapDrawingArea(ED_DRAWING_ID_CUSTOM_MOVE_LEAVE);
11162       DrawCustomContentArea();
11163     }
11164   }
11165   else if (IS_GROUP_ELEMENT(properties_element))
11166   {
11167     // draw stickybutton gadget
11168     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11169
11170     // draw checkbutton gadgets
11171     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC);
11172     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1);
11173
11174     // draw counter gadgets
11175     MapCounterButtons(ED_COUNTER_ID_GROUP_CONTENT);
11176
11177     // draw selectbox gadgets
11178     MapSelectboxGadget(ED_SELECTBOX_ID_GROUP_CHOICE_MODE);
11179
11180     // draw textbutton gadgets
11181     MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1);
11182
11183     // draw drawing area gadgets
11184     DrawGroupElementArea();
11185
11186     // draw text input gadgets
11187     MapTextInputGadget(ED_TEXTINPUT_ID_ELEMENT_NAME);
11188
11189     // draw drawing area gadgets
11190     MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
11191
11192     draw_footer_line = TRUE;
11193   }
11194   else if (IS_EMPTY_ELEMENT(properties_element))
11195   {
11196     // draw stickybutton gadget
11197     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11198
11199     // draw checkbutton gadgets
11200     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC);
11201     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1);
11202
11203     // draw textbutton gadgets
11204     MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1);
11205
11206     // draw drawing area gadgets
11207     MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
11208
11209     draw_footer_line = TRUE;
11210   }
11211
11212   // draw little footer border line above CE/GE use/save template gadgets
11213   if (draw_footer_line)
11214   {
11215     struct GadgetInfo *gd_gi1 = level_editor_gadget[GADGET_ID_PROPERTIES_INFO];
11216     struct GadgetDesign *gd = &gd_gi1->alt_design[GD_BUTTON_UNPRESSED];
11217     int gd_x = gd->x + gd_gi1->border.width / 2;
11218     int gd_y = gd->y + gd_gi1->height - 1;
11219     Pixel tab_color = GetPixel(gd->bitmap, gd_x, gd_y);
11220
11221     if (tab_color != BLACK_PIXEL)               // black => transparent
11222       FillRectangle(drawto,
11223                     SX + ED_ELEMENT_SETTINGS_X(0),
11224                     SY + ED_ELEMENT_SETTINGS_Y(14) - ED_SETTINGS_TABS_YOFFSET -
11225                     ED_TAB_BAR_HEIGHT,
11226                     getTabulatorBarWidth(), getTabulatorBarHeight(), tab_color);
11227   }
11228 }
11229
11230 static void DrawPropertiesChangeDrawingAreas(void)
11231 {
11232   if (IS_CUSTOM_ELEMENT(properties_element))
11233   {
11234     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_TARGET);
11235     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_TRIGGER);
11236     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_ACTION);
11237
11238     DrawCustomChangeContentArea();
11239   }
11240
11241   redraw_mask |= REDRAW_FIELD;
11242 }
11243
11244 static void DrawPropertiesChange(void)
11245 {
11246   int i;
11247
11248   // needed to initially set selectbox options for special action options
11249   setSelectboxSpecialActionOptions();
11250
11251   // draw stickybutton gadget
11252   MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
11253
11254   // draw checkbutton gadgets
11255   for (i =  ED_CHECKBUTTON_ID_CHANGE_FIRST;
11256        i <= ED_CHECKBUTTON_ID_CHANGE_LAST; i++)
11257     MapCheckbuttonGadget(i);
11258
11259   // draw counter gadgets
11260   for (i =  ED_COUNTER_ID_CHANGE_FIRST;
11261        i <= ED_COUNTER_ID_CHANGE_LAST; i++)
11262     MapCounterButtons(i);
11263
11264   // draw selectbox gadgets
11265   for (i =  ED_SELECTBOX_ID_CHANGE_FIRST;
11266        i <= ED_SELECTBOX_ID_CHANGE_LAST; i++)
11267     MapSelectboxGadget(i);
11268
11269   // draw textbutton gadgets
11270   for (i =  ED_TEXTBUTTON_ID_CHANGE_FIRST;
11271        i <= ED_TEXTBUTTON_ID_CHANGE_LAST; i++)
11272     MapTextbuttonGadget(i);
11273
11274   // draw graphicbutton gadgets
11275   for (i =  ED_GRAPHICBUTTON_ID_CHANGE_FIRST;
11276        i <= ED_GRAPHICBUTTON_ID_CHANGE_LAST; i++)
11277     MapGraphicbuttonGadget(i);
11278
11279   // draw drawing area gadgets
11280   DrawPropertiesChangeDrawingAreas();
11281 }
11282
11283 static void DrawEditorElementAnimation(int x, int y)
11284 {
11285   int graphic = el2img(properties_element);
11286   int frame = (ANIM_MODE(graphic) == ANIM_CE_VALUE ?
11287                custom_element.ce_value_fixed_initial :
11288                ANIM_MODE(graphic) == ANIM_CE_SCORE ?
11289                custom_element.collect_score_initial : FrameCounter);
11290
11291   DrawFixedGraphicAnimationExt(drawto, x, y, graphic, frame, NO_MASKING);
11292 }
11293
11294 static void DrawEditorElementName(int x, int y, int font_nr)
11295 {
11296   char *element_name = getElementInfoText(properties_element);
11297   int font_width = getFontWidth(font_nr);
11298   int font_height = getFontHeight(font_nr);
11299   int max_text_width = SXSIZE - x - ED_ELEMENT_SETTINGS_X(0);
11300   int max_chars_per_line = max_text_width / font_width;
11301
11302   if (strlen(element_name) <= max_chars_per_line)
11303     DrawTextS(x, y, font_nr, element_name);
11304   else
11305   {
11306     char buffer[max_chars_per_line + 1];
11307     int next_pos = max_chars_per_line;
11308
11309     strncpy(buffer, element_name, max_chars_per_line);
11310     buffer[max_chars_per_line] = '\0';
11311
11312     if (element_name[max_chars_per_line] == ' ')
11313       next_pos++;
11314     else
11315     {
11316       int i;
11317
11318       for (i = max_chars_per_line - 1; i >= 0; i--)
11319         if (buffer[i] == ' ')
11320           break;
11321
11322       if (strlen(&element_name[i + 1]) <= max_chars_per_line)
11323       {
11324         buffer[i] = '\0';
11325         next_pos = i + 1;
11326       }
11327     }
11328
11329     DrawTextS(x, y - font_height / 2, font_nr, buffer);
11330
11331     strncpy(buffer, &element_name[next_pos], max_chars_per_line);
11332     buffer[max_chars_per_line] = '\0';
11333
11334     DrawTextS(x, y + font_height / 2, font_nr, buffer);
11335   }
11336 }
11337
11338 static void DrawPropertiesWindow(void)
11339 {
11340   struct GraphicInfo *gd = &graphic_info[IMG_EDITOR_INPUT_TEXT];
11341   int element_border = graphic_info[IMG_EDITOR_ELEMENT_BORDER].border_size;
11342   int border_size = gd->border_size;
11343   int font_nr = FONT_TEXT_1;
11344   int font_height = getFontHeight(font_nr);
11345   int xoffset = TILEX + element_border + 3 * border_size;
11346   int yoffset = (TILEY - font_height) / 2;
11347   int x1 = editor.settings.element_graphic.x + element_border;
11348   int y1 = editor.settings.element_graphic.y + element_border;
11349   int x2 = (editor.settings.element_name.x == -1 ? x1 + xoffset :
11350             editor.settings.element_name.x);
11351   int y2 = (editor.settings.element_name.y == -1 ? y1 + yoffset :
11352             editor.settings.element_name.y);
11353   char *text = "Element Settings";
11354   int font2_nr = FONT_TITLE_1;
11355   struct MenuPosInfo *pos = &editor.settings.headline;
11356   int sx = SX + ALIGNED_XPOS(pos->x, getTextWidth(text, font2_nr), pos->align);
11357   int sy = SY + pos->y;
11358
11359   stick_element_properties_window = FALSE;
11360
11361   // make sure that previous properties edit mode exists for this element
11362   if (edit_mode_properties > ED_MODE_PROPERTIES_CONFIG_2 &&
11363       !IS_CUSTOM_ELEMENT(properties_element))
11364     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG_2;
11365
11366   if (edit_mode_properties > ED_MODE_PROPERTIES_CONFIG &&
11367       !IS_PLAYER_ELEMENT(properties_element) &&
11368       !IS_CUSTOM_ELEMENT(properties_element))
11369     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG;
11370
11371   if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG &&
11372       (IS_PLAYER_ELEMENT(properties_element) ||
11373        IS_CUSTOM_ELEMENT(properties_element)))
11374     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG_1;
11375
11376   CopyElementPropertiesToEditor(properties_element);
11377
11378   UnmapLevelEditorFieldGadgets();
11379   UnmapLevelEditorToolboxDrawingGadgets();
11380   UnmapLevelEditorToolboxCustomGadgets();
11381
11382   MapLevelEditorToolboxCustomGadgetsIfNeeded();
11383
11384   SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
11385   ClearField();
11386
11387   DrawText(sx, sy, text, font2_nr);
11388
11389   FrameCounter = 0;     // restart animation frame counter
11390
11391   DrawElementBorder(SX + x1, SY + y1, TILEX, TILEY, FALSE);
11392   DrawEditorElementAnimation(SX + x1, SY + y1);
11393   DrawEditorElementName(x2, y2, font_nr);
11394
11395   DrawPropertiesTabulatorGadgets();
11396
11397   if (edit_mode_properties == ED_MODE_PROPERTIES_INFO)
11398     DrawPropertiesInfo();
11399   else if (edit_mode_properties == ED_MODE_PROPERTIES_CHANGE)
11400     DrawPropertiesChange();
11401   else  // (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG[_1|_2])
11402     DrawPropertiesConfig();
11403 }
11404
11405 static void DrawPaletteWindow(void)
11406 {
11407   int i;
11408
11409   UnmapLevelEditorFieldGadgets();
11410
11411   SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
11412   ClearField();
11413
11414   // map buttons to select elements
11415   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
11416     MapGadget(level_editor_gadget[GADGET_ID_ELEMENTLIST_FIRST + i]);
11417   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL]);
11418   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_UP]);
11419   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_DOWN]);
11420 }
11421
11422 static void UpdateCustomElementGraphicGadgets(void)
11423 {
11424   int i;
11425
11426   InitElementPropertiesGfxElement();
11427
11428   ModifyEditorElementList();
11429   RedrawDrawingElements();
11430
11431   // force redraw of all mapped drawing area gadgets
11432   for (i = 0; i < ED_NUM_DRAWING_AREAS; i++)
11433   {
11434     struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[i].gadget_id];
11435
11436     if (gi->mapped)
11437       MapDrawingArea(i);
11438   }
11439 }
11440
11441 static int getOpenDirectionFromTube(int element)
11442 {
11443   switch (element)
11444   {
11445     case EL_TUBE_LEFT_UP:               return (MV_LEFT | MV_UP);
11446     case EL_TUBE_LEFT_DOWN:             return (MV_LEFT | MV_DOWN);
11447     case EL_TUBE_RIGHT_UP:              return (MV_RIGHT | MV_UP);
11448     case EL_TUBE_RIGHT_DOWN:            return (MV_RIGHT | MV_DOWN);
11449     case EL_TUBE_HORIZONTAL:            return (MV_HORIZONTAL);
11450     case EL_TUBE_HORIZONTAL_UP:         return (MV_HORIZONTAL | MV_UP);
11451     case EL_TUBE_HORIZONTAL_DOWN:       return (MV_HORIZONTAL | MV_DOWN);
11452     case EL_TUBE_VERTICAL:              return (MV_VERTICAL);
11453     case EL_TUBE_VERTICAL_LEFT:         return (MV_VERTICAL | MV_LEFT);
11454     case EL_TUBE_VERTICAL_RIGHT:        return (MV_VERTICAL | MV_RIGHT);
11455     case EL_TUBE_ANY:                   return (MV_ANY_DIRECTION);
11456   }
11457
11458   return MV_NONE;
11459 }
11460
11461 static int getTubeFromOpenDirection(int direction)
11462 {
11463   switch (direction)
11464   {
11465     case (MV_LEFT | MV_UP):             return EL_TUBE_LEFT_UP;
11466     case (MV_LEFT | MV_DOWN):           return EL_TUBE_LEFT_DOWN;
11467     case (MV_RIGHT | MV_UP):            return EL_TUBE_RIGHT_UP;
11468     case (MV_RIGHT | MV_DOWN):          return EL_TUBE_RIGHT_DOWN;
11469     case (MV_HORIZONTAL):               return EL_TUBE_HORIZONTAL;
11470     case (MV_HORIZONTAL | MV_UP):       return EL_TUBE_HORIZONTAL_UP;
11471     case (MV_HORIZONTAL | MV_DOWN):     return EL_TUBE_HORIZONTAL_DOWN;
11472     case (MV_VERTICAL):                 return EL_TUBE_VERTICAL;
11473     case (MV_VERTICAL | MV_LEFT):       return EL_TUBE_VERTICAL_LEFT;
11474     case (MV_VERTICAL | MV_RIGHT):      return EL_TUBE_VERTICAL_RIGHT;
11475     case (MV_ANY_DIRECTION):            return EL_TUBE_ANY;
11476
11477     // if only one direction, fall back to simple tube with that direction
11478     case (MV_LEFT):                     return EL_TUBE_HORIZONTAL;
11479     case (MV_RIGHT):                    return EL_TUBE_HORIZONTAL;
11480     case (MV_UP):                       return EL_TUBE_VERTICAL;
11481     case (MV_DOWN):                     return EL_TUBE_VERTICAL;
11482   }
11483
11484   return EL_EMPTY;
11485 }
11486
11487 static int getTubeFromOpenDirectionNotEmpty(int direction, int element_old)
11488 {
11489   int element_new = getTubeFromOpenDirection(direction);
11490
11491   return (element_new != EL_EMPTY ? element_new : element_old);
11492 }
11493
11494 static int getOpenDirectionFromBelt(int element)
11495 {
11496   int belt_dir = getBeltDirFromBeltElement(element);
11497
11498   return (belt_dir == MV_LEFT ? MV_RIGHT :
11499           belt_dir == MV_RIGHT ? MV_LEFT :
11500           belt_dir == MV_NONE ? MV_HORIZONTAL : belt_dir);
11501 }
11502
11503 static int getBeltFromNrAndOpenDirection(int nr, int direction)
11504 {
11505   int belt_dir = (direction == MV_LEFT ? MV_RIGHT :
11506                   direction == MV_RIGHT ? MV_LEFT :
11507                   direction == MV_HORIZONTAL ? MV_NONE : direction);
11508
11509   if (direction == MV_NONE)
11510     return EL_EMPTY;
11511
11512   return getBeltElementFromBeltNrAndBeltDir(nr, belt_dir);
11513 }
11514
11515 static int getBeltFromNrAndOpenDirectionNotEmpty(int nr, int direction,
11516                                                  int element_old)
11517 {
11518   int element_new = getBeltFromNrAndOpenDirection(nr, direction);
11519
11520   return (element_new != EL_EMPTY ? element_new : element_old);
11521 }
11522
11523 static int getOpenDirectionFromPool(int element)
11524 {
11525   switch (element)
11526   {
11527     case EL_ACID_POOL_TOPLEFT:          return (MV_DOWN | MV_RIGHT);
11528     case EL_ACID_POOL_TOPRIGHT:         return (MV_DOWN | MV_LEFT);
11529     case EL_ACID_POOL_BOTTOMLEFT:       return (MV_UP | MV_RIGHT);
11530     case EL_ACID_POOL_BOTTOMRIGHT:      return (MV_UP | MV_LEFT);
11531     case EL_ACID_POOL_BOTTOM:           return (MV_HORIZONTAL | MV_UP);
11532     case EL_ACID:                       return (MV_HORIZONTAL | MV_DOWN);
11533   }
11534
11535   return MV_NONE;
11536 }
11537
11538 static int getPoolFromOpenDirection(int direction)
11539 {
11540   switch (direction)
11541   {
11542     case (MV_DOWN | MV_RIGHT):          return EL_ACID_POOL_TOPLEFT;
11543     case (MV_DOWN | MV_LEFT):           return EL_ACID_POOL_TOPRIGHT;
11544     case (MV_UP | MV_RIGHT):            return EL_ACID_POOL_BOTTOMLEFT;
11545     case (MV_UP | MV_LEFT):             return EL_ACID_POOL_BOTTOMRIGHT;
11546     case (MV_HORIZONTAL | MV_UP):       return EL_ACID_POOL_BOTTOM;
11547     case (MV_HORIZONTAL | MV_DOWN):     return EL_ACID;
11548   }
11549
11550   return EL_EMPTY;
11551 }
11552
11553 static int getPoolFromOpenDirectionExt(int direction, int help_element)
11554 {
11555   int element = getPoolFromOpenDirection(direction);
11556   int help_direction = getOpenDirectionFromPool(help_element);
11557
11558   if (element == EL_EMPTY)
11559   {
11560     int help_direction_vertical = help_direction & MV_VERTICAL;
11561
11562     element = getPoolFromOpenDirection(direction | help_direction_vertical);
11563   }
11564
11565   if (element == EL_EMPTY)
11566   {
11567     int help_direction_horizontal = help_direction & MV_HORIZONTAL;
11568
11569     element = getPoolFromOpenDirection(direction | help_direction_horizontal);
11570   }
11571
11572   return element;
11573 }
11574
11575 static int getPoolFromOpenDirectionNotEmpty(int direction, int element_old)
11576 {
11577   int element_new = getPoolFromOpenDirectionExt(direction, element_old);
11578
11579   return (element_new != EL_EMPTY ? element_new : element_old);
11580 }
11581
11582 static int getOpenDirectionFromPillar(int element)
11583 {
11584   switch (element)
11585   {
11586     case EL_EMC_WALL_1:                 return (MV_DOWN);
11587     case EL_EMC_WALL_2:                 return (MV_VERTICAL);
11588     case EL_EMC_WALL_3:                 return (MV_UP);
11589   }
11590
11591   return MV_NONE;
11592 }
11593
11594 static int getPillarFromOpenDirection(int direction)
11595 {
11596   switch (direction)
11597   {
11598     case (MV_DOWN):                     return EL_EMC_WALL_1;
11599     case (MV_VERTICAL):                 return EL_EMC_WALL_2;
11600     case (MV_UP):                       return EL_EMC_WALL_3;
11601   }
11602
11603   return EL_EMPTY;
11604 }
11605
11606 static int getPillarFromOpenDirectionNotEmpty(int direction, int element_old)
11607 {
11608   int element_new = getPillarFromOpenDirection(direction);
11609
11610   return (element_new != EL_EMPTY ? element_new : element_old);
11611 }
11612
11613 static int getOpenDirectionFromSteel2(int element)
11614 {
11615   switch (element)
11616   {
11617     case EL_DC_STEELWALL_2_LEFT:        return (MV_RIGHT);
11618     case EL_DC_STEELWALL_2_RIGHT:       return (MV_LEFT);
11619     case EL_DC_STEELWALL_2_TOP:         return (MV_DOWN);
11620     case EL_DC_STEELWALL_2_BOTTOM:      return (MV_UP);
11621     case EL_DC_STEELWALL_2_HORIZONTAL:  return (MV_HORIZONTAL);
11622     case EL_DC_STEELWALL_2_VERTICAL:    return (MV_VERTICAL);
11623     case EL_DC_STEELWALL_2_MIDDLE:      return (MV_ANY_DIRECTION);
11624     case EL_DC_STEELWALL_2_SINGLE:      return (MV_NONE);
11625   }
11626
11627   return MV_NONE;
11628 }
11629
11630 static int getSteel2FromOpenDirection(int direction)
11631 {
11632   switch (direction)
11633   {
11634     case (MV_RIGHT):                    return EL_DC_STEELWALL_2_LEFT;
11635     case (MV_LEFT):                     return EL_DC_STEELWALL_2_RIGHT;
11636     case (MV_DOWN):                     return EL_DC_STEELWALL_2_TOP;
11637     case (MV_UP):                       return EL_DC_STEELWALL_2_BOTTOM;
11638     case (MV_HORIZONTAL):               return EL_DC_STEELWALL_2_HORIZONTAL;
11639     case (MV_VERTICAL):                 return EL_DC_STEELWALL_2_VERTICAL;
11640     case (MV_ANY_DIRECTION):            return EL_DC_STEELWALL_2_MIDDLE;
11641     case (MV_NONE):                     return EL_DC_STEELWALL_2_SINGLE;
11642   }
11643
11644   return EL_EMPTY;
11645 }
11646
11647 static int getSteel2FromOpenDirectionNotEmpty(int direction, int element_old)
11648 {
11649   int element_new = getSteel2FromOpenDirection(direction);
11650
11651   return (element_new != EL_EMPTY ? element_new : element_old);
11652 }
11653
11654 static int getOpenDirectionFromChip(int element)
11655 {
11656   switch (element)
11657   {
11658     case EL_SP_CHIP_SINGLE:             return (MV_NONE);
11659     case EL_SP_CHIP_LEFT:               return (MV_RIGHT);
11660     case EL_SP_CHIP_RIGHT:              return (MV_LEFT);
11661     case EL_SP_CHIP_TOP:                return (MV_DOWN);
11662     case EL_SP_CHIP_BOTTOM:             return (MV_UP);
11663   }
11664
11665   return MV_NONE;
11666 }
11667
11668 static int getChipFromOpenDirection(int direction)
11669 {
11670   switch (direction)
11671   {
11672     case (MV_NONE):                     return EL_SP_CHIP_SINGLE;
11673     case (MV_LEFT):                     return EL_SP_CHIP_RIGHT;
11674     case (MV_RIGHT):                    return EL_SP_CHIP_LEFT;
11675     case (MV_UP):                       return EL_SP_CHIP_BOTTOM;
11676     case (MV_DOWN):                     return EL_SP_CHIP_TOP;
11677   }
11678
11679   return EL_EMPTY;
11680 }
11681
11682 static int getChipFromOpenDirectionNotEmpty(int direction, int element_old)
11683 {
11684   int element_new = getChipFromOpenDirection(direction);
11685
11686   return (element_new != EL_EMPTY ? element_new : element_old);
11687 }
11688
11689 static int getClosedTube(int x, int y)
11690 {
11691   struct XY *xy = xy_directions;
11692   int element_old = IntelliDrawBuffer[x][y];
11693   int direction_old = getOpenDirectionFromTube(element_old);
11694   int direction_new = MV_NONE;
11695   int i;
11696
11697   for (i = 0; i < NUM_DIRECTIONS; i++)
11698   {
11699     int xx = x + xy[i].x;
11700     int yy = y + xy[i].y;
11701     int dir = MV_DIR_FROM_BIT(i);
11702     int dir_opposite = MV_DIR_OPPOSITE(dir);
11703
11704     if (IN_LEV_FIELD(xx, yy) && IS_TUBE(IntelliDrawBuffer[xx][yy]) &&
11705         (direction_old & dir) &&
11706         (getOpenDirectionFromTube(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11707       direction_new |= dir;
11708   }
11709
11710   return getTubeFromOpenDirectionNotEmpty(direction_new, element_old);
11711 }
11712
11713 static int getClosedBelt(int x, int y)
11714 {
11715   struct XY *xy = xy_directions;
11716   int element_old = IntelliDrawBuffer[x][y];
11717   int nr = getBeltNrFromBeltElement(element_old);
11718   int direction_old = getOpenDirectionFromBelt(element_old);
11719   int direction_new = MV_NONE;
11720   int i;
11721
11722   for (i = MV_BIT_LEFT; i <= MV_BIT_RIGHT; i++)
11723   {
11724     int xx = x + xy[i].x;
11725     int yy = y + xy[i].y;
11726     int dir = MV_DIR_FROM_BIT(i);
11727     int dir_opposite = MV_DIR_OPPOSITE(dir);
11728
11729     if (IN_LEV_FIELD(xx, yy) && IS_BELT(IntelliDrawBuffer[xx][yy]) &&
11730         (direction_old & dir) &&
11731         (getOpenDirectionFromBelt(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11732       direction_new |= dir;
11733   }
11734
11735   return getBeltFromNrAndOpenDirection(nr, direction_new);
11736 }
11737
11738 static int getClosedPool(int x, int y)
11739 {
11740   struct XY *xy = xy_directions;
11741   int element_old = IntelliDrawBuffer[x][y];
11742   int direction_old = getOpenDirectionFromPool(element_old);
11743   int direction_new = MV_NONE;
11744   int i;
11745
11746   for (i = 0; i < NUM_DIRECTIONS; i++)
11747   {
11748     int xx = x + xy[i].x;
11749     int yy = y + xy[i].y;
11750     int dir = MV_DIR_FROM_BIT(i);
11751     int dir_opposite = MV_DIR_OPPOSITE(dir);
11752
11753     if (IN_LEV_FIELD(xx, yy) &&
11754         IS_ACID_POOL_OR_ACID(IntelliDrawBuffer[xx][yy]) &&
11755         (direction_old & dir) &&
11756         (getOpenDirectionFromPool(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11757       direction_new |= dir;
11758   }
11759
11760   return getPoolFromOpenDirectionNotEmpty(direction_new, element_old);
11761 }
11762
11763 static int getClosedPillar(int x, int y)
11764 {
11765   struct XY *xy = xy_directions;
11766   int element_old = IntelliDrawBuffer[x][y];
11767   int direction_old = getOpenDirectionFromPillar(element_old);
11768   int direction_new = MV_NONE;
11769   int i;
11770
11771   for (i = MV_BIT_UP; i <= MV_BIT_DOWN; i++)
11772   {
11773     int xx = x + xy[i].x;
11774     int yy = y + xy[i].y;
11775     int dir = MV_DIR_FROM_BIT(i);
11776     int dir_opposite = MV_DIR_OPPOSITE(dir);
11777
11778     if (IN_LEV_FIELD(xx, yy) && IS_EMC_PILLAR(IntelliDrawBuffer[xx][yy]) &&
11779         (direction_old & dir) &&
11780         (getOpenDirectionFromPillar(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11781       direction_new |= dir;
11782   }
11783
11784   return getPillarFromOpenDirectionNotEmpty(direction_new, element_old);
11785 }
11786
11787 static int getClosedSteel2(int x, int y)
11788 {
11789   struct XY *xy = xy_directions;
11790   int element_old = IntelliDrawBuffer[x][y];
11791   int direction_old = getOpenDirectionFromSteel2(element_old);
11792   int direction_new = MV_NONE;
11793   int i;
11794
11795   for (i = 0; i < NUM_DIRECTIONS; i++)
11796   {
11797     int xx = x + xy[i].x;
11798     int yy = y + xy[i].y;
11799     int dir = MV_DIR_FROM_BIT(i);
11800     int dir_opposite = MV_DIR_OPPOSITE(dir);
11801
11802     if (IN_LEV_FIELD(xx, yy) && IS_DC_STEELWALL_2(IntelliDrawBuffer[xx][yy]) &&
11803         (direction_old & dir) &&
11804         (getOpenDirectionFromSteel2(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11805       direction_new |= dir;
11806   }
11807
11808   return getSteel2FromOpenDirectionNotEmpty(direction_new, element_old);
11809 }
11810
11811 static int getClosedChip(int x, int y)
11812 {
11813   struct XY *xy = xy_directions;
11814   int element_old = IntelliDrawBuffer[x][y];
11815   int direction_old = getOpenDirectionFromChip(element_old);
11816   int direction_new = MV_NONE;
11817   int i;
11818
11819   for (i = 0; i < NUM_DIRECTIONS; i++)
11820   {
11821     int xx = x + xy[i].x;
11822     int yy = y + xy[i].y;
11823     int dir = MV_DIR_FROM_BIT(i);
11824     int dir_opposite = MV_DIR_OPPOSITE(dir);
11825
11826     if (IN_LEV_FIELD(xx, yy) && IS_SP_CHIP(IntelliDrawBuffer[xx][yy]) &&
11827         (direction_old & dir) &&
11828         (getOpenDirectionFromChip(IntelliDrawBuffer[xx][yy]) & dir_opposite))
11829       direction_new |= dir;
11830   }
11831
11832   return getChipFromOpenDirectionNotEmpty(direction_new, element_old);
11833 }
11834
11835 static void SetElementSimpleExt(int x, int y, int dx, int dy, int element,
11836                                 boolean change_level)
11837 {
11838   int sx = x - level_xpos;
11839   int sy = y - level_ypos;
11840   int old_element = Tile[x][y];
11841   int new_element = element;
11842   unsigned int new_bitmask = (getDrawModeHiRes() ? (dx + 1) << (dy * 2) : 0x0f);
11843   boolean draw_masked = FALSE;
11844
11845   if (IS_MM_WALL_EDITOR(element))
11846   {
11847     element = map_mm_wall_element_editor(element) | new_bitmask;
11848
11849     if (IS_MM_WALL(old_element))
11850       element |= MM_WALL_BITS(old_element);
11851
11852     if (!change_level)
11853       draw_masked = TRUE;
11854   }
11855   else if (IS_MM_WALL(old_element) && element == EL_EMPTY)
11856   {
11857     int element_changed = old_element & ~new_bitmask;
11858
11859     if (MM_WALL_BITS(element_changed) != 0)
11860       element = element_changed;
11861   }
11862
11863   IntelliDrawBuffer[x][y] = element;
11864
11865   if (change_level)
11866     Tile[x][y] = element;
11867
11868   if (IN_ED_FIELD(sx, sy))
11869   {
11870     if (IS_MM_WALL(old_element) && new_element == EL_EMPTY)
11871       DrawSizedWallParts_MM(sx, sy, EL_EMPTY, ed_tilesize, FALSE, new_bitmask);
11872     else if (draw_masked)
11873       DrawEditorElementThruMask(sx, sy, element);
11874     else
11875       DrawEditorElement(sx, sy, element);
11876   }
11877 }
11878
11879 static void SetElementSimple(int x, int y, int element, boolean change_level)
11880 {
11881   SetElementSimpleExt(x, y, 0, 0, element, change_level);
11882 }
11883
11884 static void MergeAndCloseNeighbourElements(int x1, int y1, int *element1,
11885                                            int x2, int y2, int *element2,
11886                                            int (*close_function)(int, int),
11887                                            boolean change_level)
11888 {
11889   // set neighbour elements to newly determined connections
11890   SetElementSimple(x1, y1, *element1, change_level);
11891   SetElementSimple(x2, y2, *element2, change_level);
11892
11893   // remove all open connections of neighbour elements
11894   *element1 = close_function(x1, y1);
11895   *element2 = close_function(x2, y2);
11896
11897   // set neighbour elements to new, minimized connections
11898   SetElementSimple(x1, y1, *element1, change_level);
11899   SetElementSimple(x2, y2, *element2, change_level);
11900 }
11901
11902 static void SetElementIntelliDraw(int x, int y, int dx, int dy, int new_element,
11903                                   boolean change_level, int button)
11904 {
11905   struct XY *xy = xy_directions;
11906   static int last_x = -1;
11907   static int last_y = -1;
11908
11909   if (new_element == EL_UNDEFINED)
11910   {
11911     last_x = -1;
11912     last_y = -1;
11913
11914     return;
11915   }
11916
11917   int old_element = IntelliDrawBuffer[x][y];
11918
11919   if (IS_TUBE(new_element))
11920   {
11921     int last_element_new = EL_UNDEFINED;
11922     int direction = MV_NONE;
11923     int i;
11924
11925     // if old element is of same kind, keep all existing directions
11926     if (IS_TUBE(old_element))
11927       direction |= getOpenDirectionFromTube(old_element);
11928
11929     for (i = 0; i < NUM_DIRECTIONS; i++)
11930     {
11931       int xx = x + xy[i].x;
11932       int yy = y + xy[i].y;
11933
11934       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
11935           IS_TUBE(IntelliDrawBuffer[last_x][last_y]))
11936       {
11937         int dir = MV_DIR_FROM_BIT(i);
11938         int dir_opposite = MV_DIR_OPPOSITE(dir);
11939         int last_element_old = IntelliDrawBuffer[last_x][last_y];
11940         int last_direction_old = getOpenDirectionFromTube(last_element_old);
11941         int last_direction_new = last_direction_old | dir_opposite;
11942
11943         last_element_new = getTubeFromOpenDirection(last_direction_new);
11944
11945         direction |= dir;
11946       }
11947     }
11948
11949     new_element = getTubeFromOpenDirectionNotEmpty(direction, new_element);
11950
11951     if (last_element_new != EL_UNDEFINED)
11952       MergeAndCloseNeighbourElements(x, y, &new_element,
11953                                      last_x, last_y, &last_element_new,
11954                                      getClosedTube, change_level);
11955   }
11956   else if (IS_BELT(new_element))
11957   {
11958     int belt_nr = getBeltNrFromBeltElement(new_element);
11959     int last_element_new = EL_UNDEFINED;
11960     int direction = MV_NONE;
11961     int i;
11962
11963     // if old element is of same kind, keep all existing directions
11964     if (IS_BELT(old_element))
11965       direction |= getOpenDirectionFromBelt(old_element);
11966
11967     for (i = MV_BIT_LEFT; i <= MV_BIT_RIGHT; i++)
11968     {
11969       int xx = x + xy[i].x;
11970       int yy = y + xy[i].y;
11971
11972       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
11973           IS_BELT(IntelliDrawBuffer[last_x][last_y]))
11974       {
11975         int dir = MV_DIR_FROM_BIT(i);
11976         int dir_opposite = MV_DIR_OPPOSITE(dir);
11977         int last_element_old = IntelliDrawBuffer[last_x][last_y];
11978         int last_belt_nr = getBeltNrFromBeltElement(last_element_old);
11979         int last_direction_old = getOpenDirectionFromBelt(last_element_old);
11980         int last_direction_new = last_direction_old | dir_opposite;
11981
11982         last_element_new = getBeltFromNrAndOpenDirection(last_belt_nr,
11983                                                          last_direction_new);
11984         direction |= dir;
11985       }
11986     }
11987
11988     new_element = getBeltFromNrAndOpenDirectionNotEmpty(belt_nr, direction,
11989                                                         new_element);
11990     if (last_element_new != EL_UNDEFINED)
11991       MergeAndCloseNeighbourElements(x, y, &new_element,
11992                                      last_x, last_y, &last_element_new,
11993                                      getClosedBelt, change_level);
11994   }
11995   else if (IS_ACID_POOL_OR_ACID(new_element))
11996   {
11997     int last_element_new = EL_UNDEFINED;
11998     int direction = MV_NONE;
11999     int i;
12000
12001     // if old element is of same kind, keep all existing directions
12002     if (IS_ACID_POOL_OR_ACID(old_element))
12003       direction |= getOpenDirectionFromPool(old_element);
12004
12005     for (i = 0; i < NUM_DIRECTIONS; i++)
12006     {
12007       int xx = x + xy[i].x;
12008       int yy = y + xy[i].y;
12009
12010       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12011           IS_ACID_POOL_OR_ACID(IntelliDrawBuffer[last_x][last_y]))
12012       {
12013         int dir = MV_DIR_FROM_BIT(i);
12014         int dir_opposite = MV_DIR_OPPOSITE(dir);
12015         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12016         int last_direction_old = getOpenDirectionFromPool(last_element_old);
12017         int last_direction_new = last_direction_old | dir_opposite;
12018
12019         last_element_new = getPoolFromOpenDirection(last_direction_new);
12020
12021         direction |= dir;
12022       }
12023     }
12024
12025     // special corrections needed for intuitively correct acid pool drawing
12026     if (last_element_new == EL_EMPTY)
12027       last_element_new = new_element;
12028     else if (last_element_new != EL_UNDEFINED)
12029       new_element = last_element_new;
12030
12031     new_element = getPoolFromOpenDirectionNotEmpty(direction, new_element);
12032
12033     if (last_element_new != EL_UNDEFINED)
12034       MergeAndCloseNeighbourElements(x, y, &new_element,
12035                                      last_x, last_y, &last_element_new,
12036                                      getClosedPool, change_level);
12037   }
12038   else if (IS_EMC_PILLAR(new_element))
12039   {
12040     int last_element_new = EL_UNDEFINED;
12041     int direction = MV_NONE;
12042     int i;
12043
12044     // if old element is of same kind, keep all existing directions
12045     if (IS_EMC_PILLAR(old_element))
12046       direction |= getOpenDirectionFromPillar(old_element);
12047
12048     for (i = MV_BIT_UP; i <= MV_BIT_DOWN; i++)
12049     {
12050       int xx = x + xy[i].x;
12051       int yy = y + xy[i].y;
12052
12053       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12054           IS_EMC_PILLAR(IntelliDrawBuffer[last_x][last_y]))
12055       {
12056         int dir = MV_DIR_FROM_BIT(i);
12057         int dir_opposite = MV_DIR_OPPOSITE(dir);
12058         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12059         int last_direction_old = getOpenDirectionFromPillar(last_element_old);
12060         int last_direction_new = last_direction_old | dir_opposite;
12061
12062         last_element_new = getPillarFromOpenDirection(last_direction_new);
12063
12064         direction |= dir;
12065       }
12066     }
12067
12068     new_element = getPillarFromOpenDirectionNotEmpty(direction, new_element);
12069
12070     if (last_element_new != EL_UNDEFINED)
12071       MergeAndCloseNeighbourElements(x, y, &new_element,
12072                                      last_x, last_y, &last_element_new,
12073                                      getClosedPillar, change_level);
12074   }
12075   else if (IS_DC_STEELWALL_2(new_element))
12076   {
12077     int last_element_new = EL_UNDEFINED;
12078     int direction = MV_NONE;
12079     int i;
12080
12081     // if old element is of same kind, keep all existing directions
12082     if (IS_DC_STEELWALL_2(old_element))
12083       direction |= getOpenDirectionFromSteel2(old_element);
12084
12085     for (i = 0; i < NUM_DIRECTIONS; i++)
12086     {
12087       int xx = x + xy[i].x;
12088       int yy = y + xy[i].y;
12089
12090       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12091           IS_DC_STEELWALL_2(IntelliDrawBuffer[last_x][last_y]))
12092       {
12093         int dir = MV_DIR_FROM_BIT(i);
12094         int dir_opposite = MV_DIR_OPPOSITE(dir);
12095         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12096         int last_direction_old = getOpenDirectionFromSteel2(last_element_old);
12097         int last_direction_new = last_direction_old | dir_opposite;
12098
12099         last_element_new = getSteel2FromOpenDirection(last_direction_new);
12100
12101         direction |= dir;
12102       }
12103     }
12104
12105     new_element = getSteel2FromOpenDirectionNotEmpty(direction, new_element);
12106
12107     if (last_element_new != EL_UNDEFINED)
12108       MergeAndCloseNeighbourElements(x, y, &new_element,
12109                                      last_x, last_y, &last_element_new,
12110                                      getClosedSteel2, change_level);
12111   }
12112   else if (IS_SP_CHIP(new_element))
12113   {
12114     int last_element_new = EL_UNDEFINED;
12115     int direction = MV_NONE;
12116     int i;
12117
12118     // (do not keep existing directions, regardless of kind of old element)
12119
12120     for (i = 0; i < NUM_DIRECTIONS; i++)
12121     {
12122       int xx = x + xy[i].x;
12123       int yy = y + xy[i].y;
12124
12125       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12126           IS_SP_CHIP(IntelliDrawBuffer[last_x][last_y]))
12127       {
12128         int dir = MV_DIR_FROM_BIT(i);
12129         int dir_opposite = MV_DIR_OPPOSITE(dir);
12130         int last_element_old = IntelliDrawBuffer[last_x][last_y];
12131         int last_direction_old = getOpenDirectionFromChip(last_element_old);
12132         int last_direction_new = last_direction_old | dir_opposite;
12133
12134         if (last_direction_old == MV_NONE)
12135         {
12136           last_element_new = getChipFromOpenDirection(last_direction_new);
12137           direction |= dir;
12138         }
12139         else if (last_direction_old & (dir | dir_opposite))
12140         {
12141           direction |= MV_DIR_OPPOSITE(last_direction_old);
12142         }
12143         else
12144         {
12145           direction |= MV_DIR_OPPOSITE(dir);
12146         }
12147       }
12148     }
12149
12150     new_element = getChipFromOpenDirectionNotEmpty(direction, new_element);
12151
12152     if (last_element_new != EL_UNDEFINED)
12153       MergeAndCloseNeighbourElements(x, y, &new_element,
12154                                      last_x, last_y, &last_element_new,
12155                                      getClosedChip, change_level);
12156   }
12157   else if (IS_SP_HARDWARE_BASE(new_element))
12158   {
12159     int nr = GetSimpleRandom(6);
12160
12161     new_element = (nr == 0 ? EL_SP_HARDWARE_BASE_1 :
12162                    nr == 1 ? EL_SP_HARDWARE_BASE_2 :
12163                    nr == 2 ? EL_SP_HARDWARE_BASE_3 :
12164                    nr == 3 ? EL_SP_HARDWARE_BASE_4 :
12165                    nr == 4 ? EL_SP_HARDWARE_BASE_5 : EL_SP_HARDWARE_BASE_6);
12166   }
12167   else if (new_element == EL_SP_HARDWARE_GREEN ||
12168            new_element == EL_SP_HARDWARE_BLUE ||
12169            new_element == EL_SP_HARDWARE_RED)
12170   {
12171     int nr = GetSimpleRandom(3);
12172
12173     new_element = (nr == 0 ? EL_SP_HARDWARE_GREEN :
12174                    nr == 1 ? EL_SP_HARDWARE_BLUE : EL_SP_HARDWARE_RED);
12175   }
12176   else if (IS_GROUP_ELEMENT(new_element))
12177   {
12178     boolean connected_drawing = FALSE;
12179     int i;
12180
12181     for (i = 0; i < NUM_DIRECTIONS; i++)
12182     {
12183       int xx = x + xy[i].x;
12184       int yy = y + xy[i].y;
12185
12186       if (last_x == xx && last_y == yy && IN_LEV_FIELD(last_x, last_y) &&
12187           IS_IN_GROUP_EL(IntelliDrawBuffer[last_x][last_y], new_element))
12188         connected_drawing = TRUE;
12189     }
12190
12191     if (!connected_drawing)
12192       ResolveGroupElement(new_element);
12193
12194     new_element = GetElementFromGroupElement(new_element);
12195   }
12196   else if (IS_BELT_SWITCH(old_element))
12197   {
12198     int belt_nr = getBeltNrFromBeltSwitchElement(old_element);
12199     int belt_dir = getBeltDirFromBeltSwitchElement(old_element);
12200
12201     belt_dir = (belt_dir == MV_LEFT ? MV_NONE :
12202                 belt_dir == MV_NONE ? MV_RIGHT : MV_LEFT);
12203
12204     new_element = getBeltSwitchElementFromBeltNrAndBeltDir(belt_nr, belt_dir);
12205   }
12206   else
12207   {
12208     static int swappable_elements[][2] =
12209     {
12210       { EL_EXIT_CLOSED,                 EL_EXIT_OPEN                    },
12211       { EL_DYNAMITE,                    EL_DYNAMITE_ACTIVE              },
12212       { EL_EM_DYNAMITE,                 EL_EM_DYNAMITE_ACTIVE           },
12213       { EL_QUICKSAND_EMPTY,             EL_QUICKSAND_FULL               },
12214       { EL_EMERALD,                     EL_WALL_EMERALD                 },
12215       { EL_EMERALD_YELLOW,              EL_WALL_EMERALD_YELLOW          },
12216       { EL_EMERALD_RED,                 EL_WALL_EMERALD_RED             },
12217       { EL_EMERALD_PURPLE,              EL_WALL_EMERALD_PURPLE          },
12218       { EL_DIAMOND,                     EL_WALL_DIAMOND                 },
12219       { EL_BD_DIAMOND,                  EL_WALL_BD_DIAMOND              },
12220       { EL_GATE_1,                      EL_GATE_1_GRAY                  },
12221       { EL_GATE_2,                      EL_GATE_2_GRAY                  },
12222       { EL_GATE_3,                      EL_GATE_3_GRAY                  },
12223       { EL_GATE_4,                      EL_GATE_4_GRAY                  },
12224       { EL_EM_GATE_1,                   EL_EM_GATE_1_GRAY               },
12225       { EL_EM_GATE_2,                   EL_EM_GATE_2_GRAY               },
12226       { EL_EM_GATE_3,                   EL_EM_GATE_3_GRAY               },
12227       { EL_EM_GATE_4,                   EL_EM_GATE_4_GRAY               },
12228       { EL_EMC_GATE_5,                  EL_EMC_GATE_5_GRAY              },
12229       { EL_EMC_GATE_6,                  EL_EMC_GATE_6_GRAY              },
12230       { EL_EMC_GATE_7,                  EL_EMC_GATE_7_GRAY              },
12231       { EL_EMC_GATE_8,                  EL_EMC_GATE_8_GRAY              },
12232       { EL_DC_GATE_WHITE,               EL_DC_GATE_WHITE_GRAY           },
12233       { EL_TIME_ORB_EMPTY,              EL_TIME_ORB_FULL                },
12234       { EL_LAMP,                        EL_LAMP_ACTIVE                  },
12235       { EL_SOKOBAN_FIELD_EMPTY,         EL_SOKOBAN_FIELD_FULL           },
12236       { EL_SP_BASE,                     EL_SP_BUGGY_BASE                },
12237       { EL_PEARL,                       EL_WALL_PEARL                   },
12238       { EL_CRYSTAL,                     EL_WALL_CRYSTAL                 },
12239       { EL_TIMEGATE_CLOSED,             EL_TIMEGATE_OPEN                },
12240       { EL_SWITCHGATE_CLOSED,           EL_SWITCHGATE_OPEN              },
12241       { EL_SWITCHGATE_SWITCH_UP,        EL_SWITCHGATE_SWITCH_DOWN       },
12242       { EL_DC_SWITCHGATE_SWITCH_UP,     EL_DC_SWITCHGATE_SWITCH_DOWN    },
12243       { EL_LIGHT_SWITCH,                EL_LIGHT_SWITCH_ACTIVE          },
12244       { EL_LANDMINE,                    EL_DC_LANDMINE                  },
12245       { EL_SHIELD_NORMAL,               EL_SHIELD_DEADLY                },
12246       { EL_STEEL_EXIT_CLOSED,           EL_STEEL_EXIT_OPEN              },
12247       { EL_EM_EXIT_CLOSED,              EL_EM_EXIT_OPEN                 },
12248       { EL_EM_STEEL_EXIT_CLOSED,        EL_EM_STEEL_EXIT_OPEN           },
12249       { EL_QUICKSAND_FAST_EMPTY,        EL_QUICKSAND_FAST_FULL          },
12250       { EL_MM_EXIT_CLOSED,              EL_MM_EXIT_OPEN                 },
12251       { EL_MM_FUSE,                     EL_MM_FUSE_ACTIVE               },
12252       { EL_MM_LIGHTBULB,                EL_MM_LIGHTBULB_ACTIVE          },
12253       { EL_MM_FUEL_EMPTY,               EL_MM_FUEL_FULL                 },
12254
12255       { -1,                             -1                              },
12256     };
12257     static int rotatable_elements_4[][4] =
12258     {
12259       {
12260         EL_BUG_UP,
12261         EL_BUG_RIGHT,
12262         EL_BUG_DOWN,
12263         EL_BUG_LEFT
12264       },
12265       {
12266         EL_SPACESHIP_UP,
12267         EL_SPACESHIP_RIGHT,
12268         EL_SPACESHIP_DOWN,
12269         EL_SPACESHIP_LEFT
12270       },
12271       {
12272         EL_BD_BUTTERFLY_UP,
12273         EL_BD_BUTTERFLY_RIGHT,
12274         EL_BD_BUTTERFLY_DOWN,
12275         EL_BD_BUTTERFLY_LEFT
12276       },
12277       {
12278         EL_BD_FIREFLY_UP,
12279         EL_BD_FIREFLY_RIGHT,
12280         EL_BD_FIREFLY_DOWN,
12281         EL_BD_FIREFLY_LEFT
12282       },
12283       {
12284         EL_PACMAN_UP,
12285         EL_PACMAN_RIGHT,
12286         EL_PACMAN_DOWN,
12287         EL_PACMAN_LEFT
12288       },
12289       {
12290         EL_YAMYAM_UP,
12291         EL_YAMYAM_RIGHT,
12292         EL_YAMYAM_DOWN,
12293         EL_YAMYAM_LEFT
12294       },
12295       {
12296         EL_ARROW_UP,
12297         EL_ARROW_RIGHT,
12298         EL_ARROW_DOWN,
12299         EL_ARROW_LEFT
12300       },
12301       {
12302         EL_SP_PORT_UP,
12303         EL_SP_PORT_RIGHT,
12304         EL_SP_PORT_DOWN,
12305         EL_SP_PORT_LEFT
12306       },
12307       {
12308         EL_SP_GRAVITY_PORT_UP,
12309         EL_SP_GRAVITY_PORT_RIGHT,
12310         EL_SP_GRAVITY_PORT_DOWN,
12311         EL_SP_GRAVITY_PORT_LEFT
12312       },
12313       {
12314         EL_SP_GRAVITY_ON_PORT_UP,
12315         EL_SP_GRAVITY_ON_PORT_RIGHT,
12316         EL_SP_GRAVITY_ON_PORT_DOWN,
12317         EL_SP_GRAVITY_ON_PORT_LEFT
12318       },
12319       {
12320         EL_SP_GRAVITY_OFF_PORT_UP,
12321         EL_SP_GRAVITY_OFF_PORT_RIGHT,
12322         EL_SP_GRAVITY_OFF_PORT_DOWN,
12323         EL_SP_GRAVITY_OFF_PORT_LEFT
12324       },
12325       {
12326         EL_MOLE_UP,
12327         EL_MOLE_RIGHT,
12328         EL_MOLE_DOWN,
12329         EL_MOLE_LEFT
12330       },
12331       {
12332         EL_BALLOON_SWITCH_UP,
12333         EL_BALLOON_SWITCH_RIGHT,
12334         EL_BALLOON_SWITCH_DOWN,
12335         EL_BALLOON_SWITCH_LEFT
12336       },
12337       {
12338         EL_MM_MCDUFFIN_UP,
12339         EL_MM_MCDUFFIN_RIGHT,
12340         EL_MM_MCDUFFIN_DOWN,
12341         EL_MM_MCDUFFIN_LEFT
12342       },
12343       {
12344         EL_MM_MIRROR_FIXED_1,
12345         EL_MM_MIRROR_FIXED_4,
12346         EL_MM_MIRROR_FIXED_3,
12347         EL_MM_MIRROR_FIXED_2
12348       },
12349       {
12350         EL_MM_STEEL_GRID_FIXED_1,
12351         EL_MM_STEEL_GRID_FIXED_4,
12352         EL_MM_STEEL_GRID_FIXED_2,
12353         EL_MM_STEEL_GRID_FIXED_3
12354       },
12355       {
12356         EL_MM_WOODEN_GRID_FIXED_1,
12357         EL_MM_WOODEN_GRID_FIXED_4,
12358         EL_MM_WOODEN_GRID_FIXED_2,
12359         EL_MM_WOODEN_GRID_FIXED_3
12360       },
12361       {
12362         EL_MM_POLARIZER_CROSS_1,
12363         EL_MM_POLARIZER_CROSS_4,
12364         EL_MM_POLARIZER_CROSS_3,
12365         EL_MM_POLARIZER_CROSS_2
12366       },
12367       {
12368         EL_MM_PACMAN_UP,
12369         EL_MM_PACMAN_RIGHT,
12370         EL_MM_PACMAN_DOWN,
12371         EL_MM_PACMAN_LEFT
12372       },
12373       {
12374         EL_DF_LASER_UP,
12375         EL_DF_LASER_RIGHT,
12376         EL_DF_LASER_DOWN,
12377         EL_DF_LASER_LEFT
12378       },
12379       {
12380         EL_DF_RECEIVER_UP,
12381         EL_DF_RECEIVER_RIGHT,
12382         EL_DF_RECEIVER_DOWN,
12383         EL_DF_RECEIVER_LEFT
12384       },
12385       {
12386         EL_DF_SLOPE_1,
12387         EL_DF_SLOPE_4,
12388         EL_DF_SLOPE_3,
12389         EL_DF_SLOPE_2
12390       },
12391
12392       {
12393         -1,
12394       },
12395     };
12396     static int rotatable_elements_8[][8] =
12397     {
12398       {
12399         EL_DF_STEEL_GRID_FIXED_1,
12400         EL_DF_STEEL_GRID_FIXED_8,
12401         EL_DF_STEEL_GRID_FIXED_7,
12402         EL_DF_STEEL_GRID_FIXED_6,
12403         EL_DF_STEEL_GRID_FIXED_5,
12404         EL_DF_STEEL_GRID_FIXED_4,
12405         EL_DF_STEEL_GRID_FIXED_3,
12406         EL_DF_STEEL_GRID_FIXED_2
12407       },
12408       {
12409         EL_DF_WOODEN_GRID_FIXED_1,
12410         EL_DF_WOODEN_GRID_FIXED_8,
12411         EL_DF_WOODEN_GRID_FIXED_7,
12412         EL_DF_WOODEN_GRID_FIXED_6,
12413         EL_DF_WOODEN_GRID_FIXED_5,
12414         EL_DF_WOODEN_GRID_FIXED_4,
12415         EL_DF_WOODEN_GRID_FIXED_3,
12416         EL_DF_WOODEN_GRID_FIXED_2
12417       },
12418       {
12419         EL_DF_STEEL_GRID_ROTATING_1,
12420         EL_DF_STEEL_GRID_ROTATING_8,
12421         EL_DF_STEEL_GRID_ROTATING_7,
12422         EL_DF_STEEL_GRID_ROTATING_6,
12423         EL_DF_STEEL_GRID_ROTATING_5,
12424         EL_DF_STEEL_GRID_ROTATING_4,
12425         EL_DF_STEEL_GRID_ROTATING_3,
12426         EL_DF_STEEL_GRID_ROTATING_2
12427       },
12428       {
12429         EL_DF_WOODEN_GRID_ROTATING_1,
12430         EL_DF_WOODEN_GRID_ROTATING_8,
12431         EL_DF_WOODEN_GRID_ROTATING_7,
12432         EL_DF_WOODEN_GRID_ROTATING_6,
12433         EL_DF_WOODEN_GRID_ROTATING_5,
12434         EL_DF_WOODEN_GRID_ROTATING_4,
12435         EL_DF_WOODEN_GRID_ROTATING_3,
12436         EL_DF_WOODEN_GRID_ROTATING_2
12437       },
12438
12439       {
12440         -1,
12441       },
12442     };
12443     static int rotatable_elements_16[][16] =
12444     {
12445       {
12446         EL_MM_MIRROR_1,
12447         EL_MM_MIRROR_16,
12448         EL_MM_MIRROR_15,
12449         EL_MM_MIRROR_14,
12450         EL_MM_MIRROR_13,
12451         EL_MM_MIRROR_12,
12452         EL_MM_MIRROR_11,
12453         EL_MM_MIRROR_10,
12454         EL_MM_MIRROR_9,
12455         EL_MM_MIRROR_8,
12456         EL_MM_MIRROR_7,
12457         EL_MM_MIRROR_6,
12458         EL_MM_MIRROR_5,
12459         EL_MM_MIRROR_4,
12460         EL_MM_MIRROR_3,
12461         EL_MM_MIRROR_2
12462       },
12463       {
12464         EL_MM_TELEPORTER_5,
12465         EL_MM_TELEPORTER_4,
12466         EL_MM_TELEPORTER_3,
12467         EL_MM_TELEPORTER_2,
12468         EL_MM_TELEPORTER_1,
12469         EL_MM_TELEPORTER_16,
12470         EL_MM_TELEPORTER_15,
12471         EL_MM_TELEPORTER_14,
12472         EL_MM_TELEPORTER_13,
12473         EL_MM_TELEPORTER_12,
12474         EL_MM_TELEPORTER_11,
12475         EL_MM_TELEPORTER_10,
12476         EL_MM_TELEPORTER_9,
12477         EL_MM_TELEPORTER_8,
12478         EL_MM_TELEPORTER_7,
12479         EL_MM_TELEPORTER_6
12480       },
12481       {
12482         EL_MM_TELEPORTER_RED_5,
12483         EL_MM_TELEPORTER_RED_4,
12484         EL_MM_TELEPORTER_RED_3,
12485         EL_MM_TELEPORTER_RED_2,
12486         EL_MM_TELEPORTER_RED_1,
12487         EL_MM_TELEPORTER_RED_16,
12488         EL_MM_TELEPORTER_RED_15,
12489         EL_MM_TELEPORTER_RED_14,
12490         EL_MM_TELEPORTER_RED_13,
12491         EL_MM_TELEPORTER_RED_12,
12492         EL_MM_TELEPORTER_RED_11,
12493         EL_MM_TELEPORTER_RED_10,
12494         EL_MM_TELEPORTER_RED_9,
12495         EL_MM_TELEPORTER_RED_8,
12496         EL_MM_TELEPORTER_RED_7,
12497         EL_MM_TELEPORTER_RED_6
12498       },
12499       {
12500         EL_MM_TELEPORTER_YELLOW_5,
12501         EL_MM_TELEPORTER_YELLOW_4,
12502         EL_MM_TELEPORTER_YELLOW_3,
12503         EL_MM_TELEPORTER_YELLOW_2,
12504         EL_MM_TELEPORTER_YELLOW_1,
12505         EL_MM_TELEPORTER_YELLOW_16,
12506         EL_MM_TELEPORTER_YELLOW_15,
12507         EL_MM_TELEPORTER_YELLOW_14,
12508         EL_MM_TELEPORTER_YELLOW_13,
12509         EL_MM_TELEPORTER_YELLOW_12,
12510         EL_MM_TELEPORTER_YELLOW_11,
12511         EL_MM_TELEPORTER_YELLOW_10,
12512         EL_MM_TELEPORTER_YELLOW_9,
12513         EL_MM_TELEPORTER_YELLOW_8,
12514         EL_MM_TELEPORTER_YELLOW_7,
12515         EL_MM_TELEPORTER_YELLOW_6
12516       },
12517       {
12518         EL_MM_TELEPORTER_GREEN_5,
12519         EL_MM_TELEPORTER_GREEN_4,
12520         EL_MM_TELEPORTER_GREEN_3,
12521         EL_MM_TELEPORTER_GREEN_2,
12522         EL_MM_TELEPORTER_GREEN_1,
12523         EL_MM_TELEPORTER_GREEN_16,
12524         EL_MM_TELEPORTER_GREEN_15,
12525         EL_MM_TELEPORTER_GREEN_14,
12526         EL_MM_TELEPORTER_GREEN_13,
12527         EL_MM_TELEPORTER_GREEN_12,
12528         EL_MM_TELEPORTER_GREEN_11,
12529         EL_MM_TELEPORTER_GREEN_10,
12530         EL_MM_TELEPORTER_GREEN_9,
12531         EL_MM_TELEPORTER_GREEN_8,
12532         EL_MM_TELEPORTER_GREEN_7,
12533         EL_MM_TELEPORTER_GREEN_6
12534       },
12535       {
12536         EL_MM_TELEPORTER_BLUE_5,
12537         EL_MM_TELEPORTER_BLUE_4,
12538         EL_MM_TELEPORTER_BLUE_3,
12539         EL_MM_TELEPORTER_BLUE_2,
12540         EL_MM_TELEPORTER_BLUE_1,
12541         EL_MM_TELEPORTER_BLUE_16,
12542         EL_MM_TELEPORTER_BLUE_15,
12543         EL_MM_TELEPORTER_BLUE_14,
12544         EL_MM_TELEPORTER_BLUE_13,
12545         EL_MM_TELEPORTER_BLUE_12,
12546         EL_MM_TELEPORTER_BLUE_11,
12547         EL_MM_TELEPORTER_BLUE_10,
12548         EL_MM_TELEPORTER_BLUE_9,
12549         EL_MM_TELEPORTER_BLUE_8,
12550         EL_MM_TELEPORTER_BLUE_7,
12551         EL_MM_TELEPORTER_BLUE_6
12552       },
12553       {
12554         EL_MM_POLARIZER_1,
12555         EL_MM_POLARIZER_16,
12556         EL_MM_POLARIZER_15,
12557         EL_MM_POLARIZER_14,
12558         EL_MM_POLARIZER_13,
12559         EL_MM_POLARIZER_12,
12560         EL_MM_POLARIZER_11,
12561         EL_MM_POLARIZER_10,
12562         EL_MM_POLARIZER_9,
12563         EL_MM_POLARIZER_8,
12564         EL_MM_POLARIZER_7,
12565         EL_MM_POLARIZER_6,
12566         EL_MM_POLARIZER_5,
12567         EL_MM_POLARIZER_4,
12568         EL_MM_POLARIZER_3,
12569         EL_MM_POLARIZER_2
12570       },
12571       {
12572         EL_DF_MIRROR_1,
12573         EL_DF_MIRROR_16,
12574         EL_DF_MIRROR_15,
12575         EL_DF_MIRROR_14,
12576         EL_DF_MIRROR_13,
12577         EL_DF_MIRROR_12,
12578         EL_DF_MIRROR_11,
12579         EL_DF_MIRROR_10,
12580         EL_DF_MIRROR_9,
12581         EL_DF_MIRROR_8,
12582         EL_DF_MIRROR_7,
12583         EL_DF_MIRROR_6,
12584         EL_DF_MIRROR_5,
12585         EL_DF_MIRROR_4,
12586         EL_DF_MIRROR_3,
12587         EL_DF_MIRROR_2
12588       },
12589       {
12590         EL_DF_MIRROR_ROTATING_1,
12591         EL_DF_MIRROR_ROTATING_16,
12592         EL_DF_MIRROR_ROTATING_15,
12593         EL_DF_MIRROR_ROTATING_14,
12594         EL_DF_MIRROR_ROTATING_13,
12595         EL_DF_MIRROR_ROTATING_12,
12596         EL_DF_MIRROR_ROTATING_11,
12597         EL_DF_MIRROR_ROTATING_10,
12598         EL_DF_MIRROR_ROTATING_9,
12599         EL_DF_MIRROR_ROTATING_8,
12600         EL_DF_MIRROR_ROTATING_7,
12601         EL_DF_MIRROR_ROTATING_6,
12602         EL_DF_MIRROR_ROTATING_5,
12603         EL_DF_MIRROR_ROTATING_4,
12604         EL_DF_MIRROR_ROTATING_3,
12605         EL_DF_MIRROR_ROTATING_2
12606       },
12607       {
12608         EL_DF_MIRROR_FIXED_1,
12609         EL_DF_MIRROR_FIXED_16,
12610         EL_DF_MIRROR_FIXED_15,
12611         EL_DF_MIRROR_FIXED_14,
12612         EL_DF_MIRROR_FIXED_13,
12613         EL_DF_MIRROR_FIXED_12,
12614         EL_DF_MIRROR_FIXED_11,
12615         EL_DF_MIRROR_FIXED_10,
12616         EL_DF_MIRROR_FIXED_9,
12617         EL_DF_MIRROR_FIXED_8,
12618         EL_DF_MIRROR_FIXED_7,
12619         EL_DF_MIRROR_FIXED_6,
12620         EL_DF_MIRROR_FIXED_5,
12621         EL_DF_MIRROR_FIXED_4,
12622         EL_DF_MIRROR_FIXED_3,
12623         EL_DF_MIRROR_FIXED_2
12624       },
12625
12626       {
12627         -1,
12628       },
12629     };
12630     int i, j;
12631
12632     for (i = 0; swappable_elements[i][0] != -1; i++)
12633     {
12634       int element1 = swappable_elements[i][0];
12635       int element2 = swappable_elements[i][1];
12636
12637       if (old_element == element1 || old_element == element2)
12638         new_element = (old_element == element1 ? element2 : element1);
12639     }
12640
12641     for (i = 0; rotatable_elements_4[i][0] != -1; i++)
12642     {
12643       for (j = 0; j < 4; j++)
12644       {
12645         int element = rotatable_elements_4[i][j];
12646
12647         if (old_element == element)
12648           new_element = (button == 1 ? rotatable_elements_4[i][(j + 3) % 4] :
12649                          button == 2 ? rotatable_elements_4[i][0]           :
12650                          button == 3 ? rotatable_elements_4[i][(j + 1) % 4] :
12651                          old_element);
12652       }
12653     }
12654
12655     for (i = 0; rotatable_elements_8[i][0] != -1; i++)
12656     {
12657       for (j = 0; j < 8; j++)
12658       {
12659         int element = rotatable_elements_8[i][j];
12660
12661         if (old_element == element)
12662           new_element = (button == 1 ? rotatable_elements_8[i][(j + 7) % 8] :
12663                          button == 2 ? rotatable_elements_8[i][0]           :
12664                          button == 3 ? rotatable_elements_8[i][(j + 1) % 8] :
12665                          old_element);
12666       }
12667     }
12668
12669     for (i = 0; rotatable_elements_16[i][0] != -1; i++)
12670     {
12671       for (j = 0; j < 16; j++)
12672       {
12673         int element = rotatable_elements_16[i][j];
12674
12675         if (old_element == element)
12676           new_element = (button == 1 ? rotatable_elements_16[i][(j + 15) % 16] :
12677                          button == 2 ? rotatable_elements_16[i][0]             :
12678                          button == 3 ? rotatable_elements_16[i][(j + 1)  % 16] :
12679                          old_element);
12680       }
12681     }
12682
12683     if (old_element != new_element)
12684     {
12685       int max_infotext_len = getMaxInfoTextLength();
12686       char infotext[MAX_OUTPUT_LINESIZE + 1];
12687
12688       strncpy(infotext, getElementInfoText(new_element), max_infotext_len);
12689       infotext[max_infotext_len] = '\0';
12690
12691       ClearEditorGadgetInfoText();
12692
12693       DrawTextS(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY, INFOTEXT_FONT,
12694                 infotext);
12695     }
12696   }
12697
12698   if (IS_MM_WALL_EDITOR(new_element))
12699     SetElementSimpleExt(x, y, dx, dy, new_element, change_level);
12700   else
12701     SetElementSimple(x, y, new_element, change_level);
12702
12703   last_x = x;
12704   last_y = y;
12705 }
12706
12707 static void ResetIntelliDraw(void)
12708 {
12709   int x, y;
12710
12711   for (x = 0; x < lev_fieldx; x++)
12712     for (y = 0; y < lev_fieldy; y++)
12713       IntelliDrawBuffer[x][y] = Tile[x][y];
12714
12715   SetElementIntelliDraw(-1, -1, -1, -1, EL_UNDEFINED, FALSE, -1);
12716 }
12717
12718 static boolean draw_mode_hires = FALSE;
12719
12720 static boolean isHiresTileElement(int element)
12721 {
12722   return (IS_MM_WALL(element)        || element == EL_EMPTY);
12723 }
12724
12725 static boolean isHiresDrawElement(int element)
12726 {
12727   return (IS_MM_WALL_EDITOR(element) || element == EL_EMPTY);
12728 }
12729
12730 static int numHiresTiles(int element)
12731 {
12732   if (IS_MM_WALL(element))
12733     return get_number_of_bits(MM_WALL_BITS(element));
12734
12735   return 1;
12736 }
12737
12738 static void SetDrawModeHiRes(int element)
12739 {
12740   draw_mode_hires =
12741     (level.game_engine_type == GAME_ENGINE_TYPE_MM &&
12742      isHiresDrawElement(element));
12743 }
12744
12745 static boolean getDrawModeHiRes(void)
12746 {
12747   return draw_mode_hires;
12748 }
12749
12750 static int getLoResScreenPos(int pos)
12751 {
12752   return (getDrawModeHiRes() ? pos / 2 : pos);
12753 }
12754
12755 static int getLoResScreenMod(int pos)
12756 {
12757   return (getDrawModeHiRes() ? pos % 2 : 0);
12758 }
12759
12760 static void SetElementExt(int x, int y, int dx, int dy, int element,
12761                           boolean change_level, int button)
12762 {
12763   if (element < 0)
12764     SetElementSimple(x, y, Tile[x][y], change_level);
12765   else if (GetKeyModState() & KMOD_Shift)
12766     SetElementIntelliDraw(x, y, dx, dy, element, change_level, button);
12767   else
12768     SetElementSimpleExt(x, y, dx, dy, element, change_level);
12769 }
12770
12771 static void SetElement(int x, int y, int element)
12772 {
12773   SetElementExt(x, y, 0, 0, element, TRUE, -1);
12774 }
12775
12776 static void SetElementButton(int x, int y, int dx, int dy, int element,
12777                              int button)
12778 {
12779   SetElementExt(x, y, dx, dy, element, TRUE, button);
12780 }
12781
12782 static void SetElementHiRes(int sx2, int sy2, int element, boolean change_level)
12783 {
12784   int lx = getLoResScreenPos(sx2) + level_xpos;
12785   int ly = getLoResScreenPos(sy2) + level_ypos;
12786   int dx = getLoResScreenMod(sx2);
12787   int dy = getLoResScreenMod(sy2);
12788
12789   SetElementExt(lx, ly, dx, dy, element, change_level, -1);
12790 }
12791
12792 static void SetLevelElementHiRes(int lx2, int ly2, int element)
12793 {
12794   int lx = lx2 / 2;
12795   int ly = ly2 / 2;
12796   int dx = lx2 % 2;
12797   int dy = ly2 % 2;
12798
12799   SetElementExt(lx, ly, dx, dy, element, TRUE, -1);
12800 }
12801
12802 static int getLevelElementHiRes(int lx2, int ly2)
12803 {
12804   int lx = lx2 / 2;
12805   int ly = ly2 / 2;
12806   int dx = lx2 % 2;
12807   int dy = ly2 % 2;
12808   int element = Tile[lx][ly];
12809   unsigned int bitmask = (dx + 1) << (dy * 2);
12810
12811   if (IS_MM_WALL(element))
12812   {
12813     if (element & bitmask)
12814       return map_mm_wall_element(element);
12815     else
12816       return EL_EMPTY;
12817   }
12818
12819   return element;
12820 }
12821
12822 static void DrawLineElement(int x, int y, int element, boolean change_level)
12823 {
12824   SetElementHiRes(x, y, element, change_level);
12825 }
12826
12827 static void DrawLine(int from_x, int from_y, int to_x, int to_y,
12828                      int element, boolean change_level)
12829 {
12830   int xsize = ABS(to_x - from_x);
12831   int ysize = ABS(to_y - from_y);
12832   int dx = (to_x < from_x ? -1 : +1);
12833   int dy = (to_y < from_y ? -1 : +1);
12834   int i;
12835
12836   if (from_y == to_y)                   // horizontal line
12837   {
12838     for (i = 0; i <= xsize; i++)
12839       DrawLineElement(from_x + i * dx, from_y, element, change_level);
12840   }
12841   else if (from_x == to_x)              // vertical line
12842   {
12843     for (i = 0; i <= ysize; i++)
12844       DrawLineElement(from_x, from_y + i * dy, element, change_level);
12845   }
12846   else                                  // diagonal line
12847   {
12848     if (ysize < xsize)                  // a < 1
12849     {
12850       float a = (float)ysize / (float)xsize;
12851
12852       for (i = 0; i <= xsize; i++)
12853       {
12854         int x = dx * i;
12855         int y = dy * (int)(a * i + 0.5);
12856
12857         DrawLineElement(from_x + x, from_y + y, element, change_level);
12858       }
12859     }
12860     else                                // a >= 1
12861     {
12862       float a = (float)xsize / (float)ysize;
12863
12864       for (i = 0; i <= ysize; i++)
12865       {
12866         int x = dx * (int)(a * i + 0.5);
12867         int y = dy * i;
12868
12869         DrawLineElement(from_x + x, from_y + y, element, change_level);
12870       }
12871     }
12872   }
12873 }
12874
12875 static void DrawBox(int from_x, int from_y, int to_x, int to_y,
12876                     int element, boolean change_level)
12877 {
12878   DrawLine(from_x, from_y, from_x, to_y, element, change_level);
12879   DrawLine(from_x, to_y, to_x, to_y, element, change_level);
12880   DrawLine(to_x, to_y, to_x, from_y, element, change_level);
12881   DrawLine(to_x, from_y, from_x, from_y, element, change_level);
12882 }
12883
12884 static void DrawFilledBox(int from_x, int from_y, int to_x, int to_y,
12885                           int element, boolean change_level)
12886 {
12887   int y;
12888
12889   if (from_y > to_y)
12890     swap_number_pairs(&from_x, &from_y, &to_x, &to_y);
12891
12892   for (y = from_y; y <= to_y; y++)
12893     DrawLine(from_x, y, to_x, y, element, change_level);
12894 }
12895
12896 static void DrawArcExt(int from_x, int from_y, int to_x2, int to_y2,
12897                        int element, boolean change_level)
12898 {
12899   int to_x = to_x2 - (to_x2 > from_x ? +1 : -1);
12900   int to_y = to_y2 - (to_y2 > from_y ? +1 : -1);
12901   int len_x = ABS(to_x - from_x);
12902   int len_y = ABS(to_y - from_y);
12903   int radius, x, y;
12904
12905   radius = (int)(sqrt((float)(len_x * len_x + len_y * len_y)) + 0.5);
12906
12907   // not optimal (some points get drawn twice) but simple,
12908   // and fast enough for the few points we are drawing
12909
12910   for (x = 0; x <= radius; x++)
12911   {
12912     int sx, sy, sx2, sy2, lx, ly;
12913
12914     y = (int)(sqrt((float)(radius * radius - x * x)) + 0.5);
12915
12916     sx2 = from_x + x * (from_x < to_x2 ? +1 : -1);
12917     sy2 = from_y + y * (from_y < to_y2 ? +1 : -1);
12918     sx = getLoResScreenPos(sx2);
12919     sy = getLoResScreenPos(sy2);
12920     lx = sx + level_xpos;
12921     ly = sy + level_ypos;
12922
12923     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
12924       DrawLineElement(sx2, sy2, element, change_level);
12925   }
12926
12927   for (y = 0; y <= radius; y++)
12928   {
12929     int sx, sy, sx2, sy2, lx, ly;
12930
12931     x = (int)(sqrt((float)(radius * radius - y * y)) + 0.5);
12932
12933     sx2 = from_x + x * (from_x < to_x2 ? +1 : -1);
12934     sy2 = from_y + y * (from_y < to_y2 ? +1 : -1);
12935     sx = getLoResScreenPos(sx2);
12936     sy = getLoResScreenPos(sy2);
12937     lx = sx + level_xpos;
12938     ly = sy + level_ypos;
12939
12940     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
12941       DrawLineElement(sx2, sy2, element, change_level);
12942   }
12943 }
12944
12945 static void DrawArc(int from_x, int from_y, int to_x, int to_y,
12946                     int element, boolean change_level)
12947 {
12948   int to_x2 = to_x + (to_x < from_x ? -1 : +1);
12949   int to_y2 = to_y + (to_y > from_y ? +1 : -1);
12950
12951   DrawArcExt(from_x, from_y, to_x2, to_y2, element, change_level);
12952 }
12953
12954 #define DRAW_CIRCLES_BUTTON_AVAILABLE   0
12955
12956 #if DRAW_CIRCLES_BUTTON_AVAILABLE
12957 static void DrawCircle(int from_x, int from_y, int to_x, int to_y,
12958                        int element, boolean change_level)
12959 {
12960   int to_x2 = to_x + (to_x < from_x ? -1 : +1);
12961   int to_y2 = to_y + (to_y > from_y ? +1 : -1);
12962   int mirror_to_x2 = from_x - (to_x2 - from_x);
12963   int mirror_to_y2 = from_y - (to_y2 - from_y);
12964
12965   DrawArcExt(from_x, from_y, to_x2, to_y2, element, change_level);
12966   DrawArcExt(from_x, from_y, mirror_to_x2, to_y2, element, change_level);
12967   DrawArcExt(from_x, from_y, to_x2, mirror_to_y2, element, change_level);
12968   DrawArcExt(from_x, from_y, mirror_to_x2, mirror_to_y2, element, change_level);
12969 }
12970 #endif
12971
12972 static void DrawAreaBorder(int from_x, int from_y, int to_x, int to_y)
12973 {
12974   int from_sx, from_sy;
12975   int to_sx, to_sy;
12976
12977   if (from_x > to_x)
12978     swap_numbers(&from_x, &to_x);
12979
12980   if (from_y > to_y)
12981     swap_numbers(&from_y, &to_y);
12982
12983   from_sx = SX + from_x * ed_tilesize;
12984   from_sy = SY + from_y * ed_tilesize;
12985   to_sx = SX + (to_x + 1) * ed_tilesize - 1;
12986   to_sy = SY + (to_y + 1) * ed_tilesize - 1;
12987
12988   DrawSimpleWhiteLine(drawto, from_sx, from_sy, to_sx, from_sy);
12989   DrawSimpleWhiteLine(drawto, to_sx, from_sy, to_sx, to_sy);
12990   DrawSimpleWhiteLine(drawto, to_sx, to_sy, from_sx, to_sy);
12991   DrawSimpleWhiteLine(drawto, from_sx, to_sy, from_sx, from_sy);
12992
12993   if (from_x == to_x && from_y == to_y)
12994     MarkTileDirty(from_x/2, from_y/2);
12995   else
12996     redraw_mask |= REDRAW_FIELD;
12997 }
12998
12999 static void DrawAreaBox(int from_x, int from_y, int to_x, int to_y,
13000                         int element, boolean change_level)
13001 {
13002   DrawBox(from_x, from_y, to_x, to_y, element, change_level);
13003 }
13004
13005 static void SelectArea(int from_x, int from_y, int to_x, int to_y,
13006                        int element, boolean change_level)
13007 {
13008   if (element == -1 || change_level)
13009     DrawAreaBox(from_x, from_y, to_x, to_y, -1, FALSE);
13010   else
13011     DrawAreaBorder(from_x, from_y, to_x, to_y);
13012 }
13013
13014 // values for CopyBrushExt()
13015 #define CB_AREA_TO_BRUSH                0
13016 #define CB_BRUSH_TO_CURSOR              1
13017 #define CB_BRUSH_TO_LEVEL               2
13018 #define CB_DELETE_OLD_CURSOR            3
13019 #define CB_DUMP_BRUSH                   4
13020 #define CB_DUMP_BRUSH_SMALL             5
13021 #define CB_CLIPBOARD_TO_BRUSH           6
13022 #define CB_BRUSH_TO_CLIPBOARD           7
13023 #define CB_BRUSH_TO_CLIPBOARD_SMALL     8
13024 #define CB_UPDATE_BRUSH_POSITION        9
13025 #define CB_FLIP_BRUSH_X                 10
13026 #define CB_FLIP_BRUSH_Y                 11
13027 #define CB_FLIP_BRUSH_XY                12
13028
13029 #define MAX_CB_PART_SIZE        10
13030 #define MAX_CB_LINE_SIZE        (MAX_LEV_FIELDX + 1)    // text plus newline
13031 #define MAX_CB_NUM_LINES        (MAX_LEV_FIELDY)
13032 #define MAX_CB_TEXT_SIZE        (MAX_CB_LINE_SIZE *     \
13033                                  MAX_CB_NUM_LINES *     \
13034                                  MAX_CB_PART_SIZE)
13035
13036 static int getFlippedTileExt(int map[], int element)
13037 {
13038   int i;
13039
13040   for (i = 0; map[i] != -1; i++)
13041     if (map[i] == element)
13042       return map[i ^ 1];        // get flipped element by flipping LSB of index
13043
13044   return element;
13045 }
13046
13047 static int getFlippedTileX(int element)
13048 {
13049   int map[] =
13050   {
13051     EL_BD_BUTTERFLY_LEFT,               EL_BD_BUTTERFLY_RIGHT,
13052     EL_BD_FIREFLY_LEFT,                 EL_BD_FIREFLY_RIGHT,
13053     EL_BUG_LEFT,                        EL_BUG_RIGHT,
13054     EL_SPACESHIP_LEFT,                  EL_SPACESHIP_RIGHT,
13055     EL_PACMAN_LEFT,                     EL_PACMAN_RIGHT,
13056     EL_ARROW_LEFT,                      EL_ARROW_RIGHT,
13057     EL_MOLE_LEFT,                       EL_MOLE_RIGHT,
13058     EL_BALLOON_SWITCH_LEFT,             EL_BALLOON_SWITCH_RIGHT,
13059     EL_YAMYAM_LEFT,                     EL_YAMYAM_RIGHT,
13060     EL_SP_PORT_LEFT,                    EL_SP_PORT_RIGHT,
13061     EL_SP_GRAVITY_PORT_LEFT,            EL_SP_GRAVITY_PORT_RIGHT,
13062     EL_SP_GRAVITY_ON_PORT_LEFT,         EL_SP_GRAVITY_ON_PORT_RIGHT,
13063     EL_SP_GRAVITY_OFF_PORT_LEFT,        EL_SP_GRAVITY_OFF_PORT_RIGHT,
13064     EL_CONVEYOR_BELT_1_LEFT,            EL_CONVEYOR_BELT_1_RIGHT,
13065     EL_CONVEYOR_BELT_2_LEFT,            EL_CONVEYOR_BELT_2_RIGHT,
13066     EL_CONVEYOR_BELT_3_LEFT,            EL_CONVEYOR_BELT_3_RIGHT,
13067     EL_CONVEYOR_BELT_4_LEFT,            EL_CONVEYOR_BELT_4_RIGHT,
13068     EL_SPRING_LEFT,                     EL_SPRING_RIGHT,
13069     EL_SP_CHIP_LEFT,                    EL_SP_CHIP_RIGHT,
13070     EL_TUBE_VERTICAL_LEFT,              EL_TUBE_VERTICAL_RIGHT,
13071     EL_TUBE_LEFT_UP,                    EL_TUBE_RIGHT_UP,
13072     EL_TUBE_LEFT_DOWN,                  EL_TUBE_RIGHT_DOWN,
13073     EL_DC_STEELWALL_1_LEFT,             EL_DC_STEELWALL_1_RIGHT,
13074     EL_DC_STEELWALL_1_TOPLEFT,          EL_DC_STEELWALL_1_TOPRIGHT,
13075     EL_DC_STEELWALL_1_BOTTOMLEFT,       EL_DC_STEELWALL_1_BOTTOMRIGHT,
13076     EL_DC_STEELWALL_1_TOPLEFT_2,        EL_DC_STEELWALL_1_TOPRIGHT_2,
13077     EL_DC_STEELWALL_1_BOTTOMLEFT_2,     EL_DC_STEELWALL_1_BOTTOMRIGHT_2,
13078     EL_DC_STEELWALL_2_LEFT,             EL_DC_STEELWALL_2_RIGHT,
13079     EL_ACID_POOL_TOPLEFT,               EL_ACID_POOL_TOPRIGHT,
13080     EL_ACID_POOL_BOTTOMLEFT,            EL_ACID_POOL_BOTTOMRIGHT,
13081
13082     -1
13083   };
13084
13085   return getFlippedTileExt(map, element);
13086 }
13087
13088 static int getFlippedTileY(int element)
13089 {
13090   int map[] =
13091   {
13092     EL_BD_BUTTERFLY_UP,                 EL_BD_BUTTERFLY_DOWN,
13093     EL_BD_FIREFLY_UP,                   EL_BD_FIREFLY_DOWN,
13094     EL_BUG_UP,                          EL_BUG_DOWN,
13095     EL_SPACESHIP_UP,                    EL_SPACESHIP_DOWN,
13096     EL_PACMAN_UP,                       EL_PACMAN_DOWN,
13097     EL_ARROW_UP,                        EL_ARROW_DOWN,
13098     EL_MOLE_UP,                         EL_MOLE_DOWN,
13099     EL_BALLOON_SWITCH_UP,               EL_BALLOON_SWITCH_DOWN,
13100     EL_YAMYAM_UP,                       EL_YAMYAM_DOWN,
13101     EL_SP_PORT_UP,                      EL_SP_PORT_DOWN,
13102     EL_SP_GRAVITY_PORT_UP,              EL_SP_GRAVITY_PORT_DOWN,
13103     EL_SP_GRAVITY_ON_PORT_UP,           EL_SP_GRAVITY_ON_PORT_DOWN,
13104     EL_SP_GRAVITY_OFF_PORT_UP,          EL_SP_GRAVITY_OFF_PORT_DOWN,
13105     EL_SP_CHIP_TOP,                     EL_SP_CHIP_BOTTOM,
13106     EL_TUBE_HORIZONTAL_UP,              EL_TUBE_HORIZONTAL_DOWN,
13107     EL_TUBE_LEFT_UP,                    EL_TUBE_LEFT_DOWN,
13108     EL_TUBE_RIGHT_UP,                   EL_TUBE_RIGHT_DOWN,
13109     EL_DC_STEELWALL_1_TOP,              EL_DC_STEELWALL_1_BOTTOM,
13110     EL_DC_STEELWALL_1_TOPLEFT,          EL_DC_STEELWALL_1_BOTTOMLEFT,
13111     EL_DC_STEELWALL_1_TOPRIGHT,         EL_DC_STEELWALL_1_BOTTOMRIGHT,
13112     EL_DC_STEELWALL_1_TOPLEFT_2,        EL_DC_STEELWALL_1_BOTTOMLEFT_2,
13113     EL_DC_STEELWALL_1_TOPRIGHT_2,       EL_DC_STEELWALL_1_BOTTOMRIGHT_2,
13114     EL_DC_STEELWALL_2_TOP,              EL_DC_STEELWALL_2_BOTTOM,
13115     EL_EMC_WALL_1,                      EL_EMC_WALL_3,
13116
13117     -1
13118   };
13119
13120   return getFlippedTileExt(map, element);
13121 }
13122
13123 static int getFlippedTileXY(int element)
13124 {
13125   int map[] =
13126   {
13127     EL_BD_BUTTERFLY_LEFT,               EL_BD_BUTTERFLY_UP,
13128     EL_BD_BUTTERFLY_RIGHT,              EL_BD_BUTTERFLY_DOWN,
13129     EL_BD_FIREFLY_LEFT,                 EL_BD_FIREFLY_UP,
13130     EL_BD_FIREFLY_RIGHT,                EL_BD_FIREFLY_DOWN,
13131     EL_BUG_LEFT,                        EL_BUG_UP,
13132     EL_BUG_RIGHT,                       EL_BUG_DOWN,
13133     EL_SPACESHIP_LEFT,                  EL_SPACESHIP_UP,
13134     EL_SPACESHIP_RIGHT,                 EL_SPACESHIP_DOWN,
13135     EL_PACMAN_LEFT,                     EL_PACMAN_UP,
13136     EL_PACMAN_RIGHT,                    EL_PACMAN_DOWN,
13137     EL_ARROW_LEFT,                      EL_ARROW_UP,
13138     EL_ARROW_RIGHT,                     EL_ARROW_DOWN,
13139     EL_MOLE_LEFT,                       EL_MOLE_UP,
13140     EL_MOLE_RIGHT,                      EL_MOLE_DOWN,
13141     EL_BALLOON_SWITCH_LEFT,             EL_BALLOON_SWITCH_UP,
13142     EL_BALLOON_SWITCH_RIGHT,            EL_BALLOON_SWITCH_DOWN,
13143     EL_YAMYAM_LEFT,                     EL_YAMYAM_UP,
13144     EL_YAMYAM_RIGHT,                    EL_YAMYAM_DOWN,
13145     EL_SP_PORT_LEFT,                    EL_SP_PORT_UP,
13146     EL_SP_PORT_RIGHT,                   EL_SP_PORT_DOWN,
13147     EL_SP_GRAVITY_PORT_LEFT,            EL_SP_GRAVITY_PORT_UP,
13148     EL_SP_GRAVITY_PORT_RIGHT,           EL_SP_GRAVITY_PORT_DOWN,
13149     EL_SP_GRAVITY_ON_PORT_LEFT,         EL_SP_GRAVITY_ON_PORT_UP,
13150     EL_SP_GRAVITY_ON_PORT_RIGHT,        EL_SP_GRAVITY_ON_PORT_DOWN,
13151     EL_SP_GRAVITY_OFF_PORT_LEFT,        EL_SP_GRAVITY_OFF_PORT_UP,
13152     EL_SP_GRAVITY_OFF_PORT_RIGHT,       EL_SP_GRAVITY_OFF_PORT_DOWN,
13153     EL_SP_CHIP_LEFT,                    EL_SP_CHIP_TOP,
13154     EL_SP_CHIP_RIGHT,                   EL_SP_CHIP_BOTTOM,
13155     EL_TUBE_VERTICAL,                   EL_TUBE_HORIZONTAL,
13156     EL_TUBE_VERTICAL_LEFT,              EL_TUBE_HORIZONTAL_UP,
13157     EL_TUBE_VERTICAL_RIGHT,             EL_TUBE_HORIZONTAL_DOWN,
13158     EL_TUBE_LEFT_DOWN,                  EL_TUBE_RIGHT_UP,
13159     EL_DC_STEELWALL_1_LEFT,             EL_DC_STEELWALL_1_TOP,
13160     EL_DC_STEELWALL_1_RIGHT,            EL_DC_STEELWALL_1_BOTTOM,
13161     EL_DC_STEELWALL_1_HORIZONTAL,       EL_DC_STEELWALL_1_VERTICAL,
13162     EL_DC_STEELWALL_1_TOPRIGHT,         EL_DC_STEELWALL_1_BOTTOMLEFT,
13163     EL_DC_STEELWALL_1_TOPRIGHT_2,       EL_DC_STEELWALL_1_BOTTOMLEFT_2,
13164     EL_DC_STEELWALL_2_LEFT,             EL_DC_STEELWALL_2_TOP,
13165     EL_DC_STEELWALL_2_RIGHT,            EL_DC_STEELWALL_2_BOTTOM,
13166     EL_DC_STEELWALL_2_HORIZONTAL,       EL_DC_STEELWALL_2_VERTICAL,
13167     EL_EXPANDABLE_WALL_HORIZONTAL,      EL_EXPANDABLE_WALL_VERTICAL,
13168     EL_EXPANDABLE_STEELWALL_HORIZONTAL, EL_EXPANDABLE_STEELWALL_VERTICAL,
13169
13170     -1
13171   };
13172
13173   return getFlippedTileExt(map, element);
13174 }
13175
13176 static int getFlippedTile(int element, int mode)
13177 {
13178   if (IS_MM_ELEMENT(element))
13179   {
13180     // get MM game element
13181     element = map_element_RND_to_MM(element);
13182
13183     // get flipped game element
13184     element = (mode == CB_FLIP_BRUSH_X  ? getFlippedTileX_MM(element) :
13185                mode == CB_FLIP_BRUSH_Y  ? getFlippedTileY_MM(element) :
13186                mode == CB_FLIP_BRUSH_XY ? getFlippedTileXY_MM(element) :
13187                element);
13188
13189     // get RND game element again
13190     element = map_element_MM_to_RND(element);
13191   }
13192   else
13193   {
13194     // get flipped game element
13195     element = (mode == CB_FLIP_BRUSH_X  ? getFlippedTileX(element) :
13196                mode == CB_FLIP_BRUSH_Y  ? getFlippedTileY(element) :
13197                mode == CB_FLIP_BRUSH_XY ? getFlippedTileXY(element) :
13198                element);
13199   }
13200
13201   return element;
13202 }
13203
13204 static void SwapFlippedTiles(short *tile1, short *tile2, int mode)
13205 {
13206   // flip tiles
13207   short tile1_flipped = getFlippedTile(*tile1, mode);
13208   short tile2_flipped = getFlippedTile(*tile2, mode);
13209
13210   // swap tiles
13211   *tile1 = tile2_flipped;
13212   *tile2 = tile1_flipped;
13213 }
13214
13215 static void DrawBrushElement(int sx, int sy, int element, boolean change_level)
13216 {
13217   DrawLineElement(sx, sy, element, change_level);
13218 }
13219
13220 static void CopyBrushExt(int from_x, int from_y, int to_x, int to_y,
13221                          int button, int mode)
13222 {
13223   static short brush_buffer[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
13224   static int brush_width, brush_height;
13225   static int last_cursor_x = -1, last_cursor_y = -1;
13226   static boolean delete_old_brush = FALSE;
13227   int new_element = BUTTON_ELEMENT(button);
13228   int x, y;
13229
13230   if (mode == CB_DUMP_BRUSH ||
13231       mode == CB_DUMP_BRUSH_SMALL ||
13232       mode == CB_BRUSH_TO_CLIPBOARD ||
13233       mode == CB_BRUSH_TO_CLIPBOARD_SMALL)
13234   {
13235     if (edit_mode != ED_MODE_DRAWING)
13236       return;
13237
13238     char part[MAX_CB_PART_SIZE + 1] = "";
13239     char text[MAX_CB_TEXT_SIZE + 1] = "";
13240     int width  = (draw_with_brush ? brush_width  : lev_fieldx);
13241     int height = (draw_with_brush ? brush_height : lev_fieldy);
13242     char *format = "%s%03d";
13243
13244     for (y = 0; y < height; y++)
13245       for (x = 0; x < width; x++)
13246         if ((draw_with_brush ? brush_buffer[x][y] : Tile[x][y]) > 999)
13247           format = "%s%04d";
13248
13249     for (y = 0; y < height; y++)
13250     {
13251       for (x = 0; x < width; x++)
13252       {
13253         int element = (draw_with_brush ? brush_buffer[x][y] : Tile[x][y]);
13254         char *prefix = (mode == CB_DUMP_BRUSH ||
13255                         mode == CB_BRUSH_TO_CLIPBOARD ? "`" : "¸");
13256
13257         if (element >= NUM_FILE_ELEMENTS)
13258           element = EL_UNKNOWN;
13259
13260         // copy brush to level sketch text buffer for the R'n'D forum:
13261         // - large tiles: `xxx or `xxxx (0x60 ASCII)
13262         // - small tiles: Â¸xxx or Â¸xxxx (0xb8 ISO-8859-1, 0xc2b8 UTF-8)
13263         snprintf(part, MAX_CB_PART_SIZE + 1, format, prefix, element);
13264         strcat(text, part);
13265       }
13266
13267       strcat(text, "\n");
13268     }
13269
13270     if (mode == CB_BRUSH_TO_CLIPBOARD ||
13271         mode == CB_BRUSH_TO_CLIPBOARD_SMALL)
13272       SDL_SetClipboardText(text);
13273     else
13274       Print("%s", text);        // print brush data to console and log file
13275
13276     return;
13277   }
13278
13279   if (mode == CB_CLIPBOARD_TO_BRUSH)
13280   {
13281     if (edit_mode != ED_MODE_DRAWING)
13282       return;
13283
13284     if (!SDL_HasClipboardText())
13285     {
13286       Request("Clipboard is empty!", REQ_CONFIRM);
13287
13288       return;
13289     }
13290
13291     boolean copy_to_brush = (draw_with_brush ||
13292                              drawing_function == GADGET_ID_GRAB_BRUSH);
13293
13294     // this will delete the old brush, if already drawing with a brush
13295     if (copy_to_brush)
13296       ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS], MB_LEFTBUTTON);
13297
13298     // initialization is required for "odd" (incomplete) clipboard content
13299     for (x = 0; x < MAX_LEV_FIELDX; x++)
13300       for (y = 0; y < MAX_LEV_FIELDY; y++)
13301         brush_buffer[x][y] = EL_EMPTY;
13302
13303     brush_width  = 0;
13304     brush_height = 0;
13305     x = 0;
13306     y = 0;
13307
13308     char *clipboard_text = SDL_GetClipboardText();
13309     char *ptr = clipboard_text;
13310     boolean allow_new_row = FALSE;
13311     boolean stop = FALSE;
13312
13313     while (*ptr && !stop)
13314     {
13315       boolean prefix_found = FALSE;
13316       boolean start_new_row = FALSE;
13317
13318       // level sketch element number prefixes (may be multi-byte characters)
13319       char *prefix_list[] = { "`", "¸" };
13320       int i;
13321
13322       for (i = 0; i < ARRAY_SIZE(prefix_list); i++)
13323       {
13324         char *prefix = prefix_list[i];
13325
13326         // check if string is large enough for prefix
13327         if (strlen(ptr) < strlen(prefix))
13328         {
13329           stop = TRUE;
13330
13331           break;
13332         }
13333
13334         // check if string starts with prefix
13335         if (strPrefix(ptr, prefix))
13336         {
13337           ptr += strlen(prefix);
13338
13339           prefix_found = TRUE;
13340
13341           break;
13342         }
13343       }
13344
13345       // check if prefix found and followed by (at least) three digits
13346       if (prefix_found &&
13347           strlen(ptr) >= 3 &&
13348           ptr[0] >= '0' && ptr[0] <= '9' &&
13349           ptr[1] >= '0' && ptr[1] <= '9' &&
13350           ptr[2] >= '0' && ptr[2] <= '9')
13351       {
13352         int element = ((ptr[0] - '0') * 100 +
13353                        (ptr[1] - '0') * 10 +
13354                        (ptr[2] - '0'));
13355
13356         ptr += 3;
13357
13358         // level sketch element number might consist of four digits
13359         if (ptr[0] >= '0' && ptr[0] <= '9')
13360         {
13361           element = element * 10 + (ptr[0] - '0');
13362           ptr++;
13363         }
13364
13365         // remap some (historic, now obsolete) elements
13366         element = getMappedElement(element);
13367
13368         if (element >= NUM_FILE_ELEMENTS)
13369           element = EL_UNKNOWN;
13370
13371         brush_buffer[x][y] = element;
13372
13373         brush_width  = MAX(x + 1, brush_width);
13374         brush_height = MAX(y + 1, brush_height);
13375
13376         x++;
13377
13378         if (x >= MAX_LEV_FIELDX)
13379           start_new_row = TRUE;
13380
13381         allow_new_row = TRUE;
13382       }
13383       else
13384       {
13385         if ((*ptr == '\n' || *ptr == '\r') && allow_new_row)
13386           start_new_row = TRUE;
13387
13388         ptr++;          // !!! FIX THIS for real UTF-8 handling !!!
13389       }
13390
13391       if (start_new_row)
13392       {
13393         x = 0;
13394         y++;
13395
13396         if (y >= MAX_LEV_FIELDY)
13397           stop = TRUE;
13398
13399         allow_new_row = FALSE;
13400       }
13401     }
13402
13403     SDL_free(clipboard_text);
13404
13405     if (brush_width == 0 || brush_height == 0)
13406     {
13407       Request("No level sketch found in clipboard!", REQ_CONFIRM);
13408
13409       return;
13410     }
13411
13412     if (copy_to_brush)
13413     {
13414       struct GadgetInfo *gi = level_editor_gadget[GADGET_ID_DRAWING_LEVEL];
13415       int mx, my;
13416
13417       SDL_GetMouseState(&mx, &my);
13418
13419       // if inside drawing area, activate and draw brush at last mouse position
13420       if (mx >= gi->x && mx < gi->x + gi->width &&
13421           my >= gi->y && my < gi->y + gi->height)
13422         CopyBrushToCursor(last_cursor_x, last_cursor_y);
13423
13424       draw_with_brush = TRUE;
13425     }
13426     else
13427     {
13428       char request[100];
13429
13430       sprintf(request, "Replace level with %dx%d level sketch from clipboard?",
13431               brush_width, brush_height);
13432
13433       if (!Request(request, REQ_ASK))
13434         return;
13435
13436       for (x = 0; x < MAX_LEV_FIELDX; x++)
13437         for (y = 0; y < MAX_LEV_FIELDY; y++)
13438           Tile[x][y] = brush_buffer[x][y];
13439
13440       lev_fieldx = level.fieldx = brush_width;
13441       lev_fieldy = level.fieldy = brush_height;
13442
13443       boolean use_bd_engine = TRUE;
13444       boolean use_em_engine = TRUE;
13445       boolean use_sp_engine = TRUE;
13446       boolean use_mm_engine = TRUE;
13447
13448       for (x = 0; x < MAX_LEV_FIELDX; x++)
13449       {
13450         for (y = 0; y < MAX_LEV_FIELDY; y++)
13451         {
13452           int element = Tile[x][y];
13453
13454           if (!IS_BD_ELEMENT(element) && !IS_PLAYER_ELEMENT(element))
13455             use_bd_engine = FALSE;
13456
13457           if (!IS_EM_ELEMENT(element) && !IS_PLAYER_ELEMENT(element))
13458             use_em_engine = FALSE;
13459
13460           if (!IS_SP_ELEMENT(element))
13461             use_sp_engine = FALSE;
13462
13463           if (!IS_MM_ELEMENT(element) && element != EL_EMPTY)
13464             use_mm_engine = FALSE;
13465         }
13466       }
13467
13468       level.game_engine_type = (use_bd_engine ? GAME_ENGINE_TYPE_BD :
13469                                 use_em_engine ? GAME_ENGINE_TYPE_EM :
13470                                 use_sp_engine ? GAME_ENGINE_TYPE_SP :
13471                                 use_mm_engine ? GAME_ENGINE_TYPE_MM :
13472                                 GAME_ENGINE_TYPE_RND);
13473
13474       // update element selection list
13475       ReinitializeElementList();
13476       ModifyEditorElementList();
13477
13478       SetBorderElement();
13479
13480       DrawEditModeWindow();
13481       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13482     }
13483
13484     return;
13485   }
13486
13487   if (mode == CB_DELETE_OLD_CURSOR && !delete_old_brush)
13488     return;
13489
13490   if (mode == CB_AREA_TO_BRUSH)
13491   {
13492     int from_lx, from_ly;
13493
13494     if (from_x > to_x)
13495       swap_numbers(&from_x, &to_x);
13496
13497     if (from_y > to_y)
13498       swap_numbers(&from_y, &to_y);
13499
13500     brush_width = to_x - from_x + 1;
13501     brush_height = to_y - from_y + 1;
13502
13503     from_lx = from_x + level_xpos;
13504     from_ly = from_y + level_ypos;
13505
13506     for (y = 0; y < brush_height; y++)
13507     {
13508       for (x = 0; x < brush_width; x++)
13509       {
13510         brush_buffer[x][y] = Tile[from_lx + x][from_ly + y];
13511
13512         if (button != 1)
13513           DrawBrushElement(from_x + x, from_y + y, new_element, TRUE);
13514       }
13515     }
13516
13517     if (button != 1)
13518       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13519
13520     delete_old_brush = FALSE;
13521   }
13522   else if (mode == CB_BRUSH_TO_CURSOR || mode == CB_DELETE_OLD_CURSOR ||
13523            mode == CB_BRUSH_TO_LEVEL)
13524   {
13525     int cursor_x = (mode == CB_DELETE_OLD_CURSOR ? last_cursor_x : from_x);
13526     int cursor_y = (mode == CB_DELETE_OLD_CURSOR ? last_cursor_y : from_y);
13527     int cursor_from_x = cursor_x - brush_width / 2;
13528     int cursor_from_y = cursor_y - brush_height / 2;
13529     int border_from_x = cursor_x, border_from_y = cursor_y;
13530     int border_to_x = cursor_x, border_to_y = cursor_y;
13531
13532     if (mode != CB_DELETE_OLD_CURSOR && delete_old_brush)
13533       CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
13534
13535     if (!IN_ED_FIELD(cursor_x, cursor_y) ||
13536         !IN_LEV_FIELD(cursor_x + level_xpos, cursor_y + level_ypos))
13537     {
13538       delete_old_brush = FALSE;
13539
13540       return;
13541     }
13542
13543     for (y = 0; y < brush_height; y++)
13544     {
13545       for (x = 0; x < brush_width; x++)
13546       {
13547         int sx = cursor_from_x + x;
13548         int sy = cursor_from_y + y;
13549         int lx = sx + level_xpos;
13550         int ly = sy + level_ypos;
13551         boolean change_level = (mode == CB_BRUSH_TO_LEVEL);
13552         int element = (mode == CB_DELETE_OLD_CURSOR ? -1 :
13553                        mode == CB_BRUSH_TO_CURSOR || button == 1 ?
13554                        brush_buffer[x][y] : new_element);
13555
13556         if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
13557         {
13558           if (sx < border_from_x)
13559             border_from_x = sx;
13560           else if (sx > border_to_x)
13561             border_to_x = sx;
13562           if (sy < border_from_y)
13563             border_from_y = sy;
13564           else if (sy > border_to_y)
13565             border_to_y = sy;
13566
13567           DrawBrushElement(sx, sy, element, change_level);
13568         }
13569       }
13570     }
13571
13572     if (mode != CB_DELETE_OLD_CURSOR)
13573       DrawAreaBorder(border_from_x, border_from_y, border_to_x, border_to_y);
13574
13575     last_cursor_x = cursor_x;
13576     last_cursor_y = cursor_y;
13577
13578     delete_old_brush = TRUE;
13579   }
13580   else if (mode == CB_FLIP_BRUSH_X)
13581   {
13582     for (y = 0; y < brush_height; y++)
13583       for (x = 0; x < (brush_width + 1) / 2; x++)
13584         SwapFlippedTiles(&brush_buffer[x][y],
13585                          &brush_buffer[brush_width - x - 1][y], mode);
13586
13587     CopyBrushExt(last_cursor_x, last_cursor_y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13588   }
13589   else if (mode == CB_FLIP_BRUSH_Y)
13590   {
13591     for (y = 0; y < (brush_height + 1) / 2; y++)
13592       for (x = 0; x < brush_width; x++)
13593         SwapFlippedTiles(&brush_buffer[x][y],
13594                          &brush_buffer[x][brush_height - y - 1], mode);
13595
13596     CopyBrushExt(last_cursor_x, last_cursor_y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13597   }
13598   else if (mode == CB_FLIP_BRUSH_XY)
13599   {
13600     CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
13601
13602     for (y = 0; y < MAX(brush_width, brush_height); y++)
13603       for (x = 0; x <= y; x++)
13604         SwapFlippedTiles(&brush_buffer[x][y],
13605                          &brush_buffer[y][x], mode);
13606
13607     swap_numbers(&brush_width, &brush_height);
13608
13609     CopyBrushExt(last_cursor_x, last_cursor_y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13610   }
13611
13612   if (mode == CB_UPDATE_BRUSH_POSITION)
13613   {
13614     last_cursor_x = from_x;
13615     last_cursor_y = from_y;
13616   }
13617 }
13618
13619 static void CopyAreaToBrush(int from_x, int from_y, int to_x, int to_y,
13620                             int button)
13621 {
13622   CopyBrushExt(from_x, from_y, to_x, to_y, button, CB_AREA_TO_BRUSH);
13623 }
13624
13625 static void CopyBrushToLevel(int x, int y, int button)
13626 {
13627   CopyBrushExt(x, y, 0, 0, button, CB_BRUSH_TO_LEVEL);
13628 }
13629
13630 static void CopyBrushToCursor(int x, int y)
13631 {
13632   CopyBrushExt(x, y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
13633 }
13634
13635 static void UpdateBrushPosition(int x, int y)
13636 {
13637   CopyBrushExt(x, y, 0, 0, 0, CB_UPDATE_BRUSH_POSITION);
13638 }
13639
13640 static void DeleteBrushFromCursor(void)
13641 {
13642   CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
13643 }
13644
13645 static void FlipBrushX(void)
13646 {
13647   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_X);
13648 }
13649
13650 static void FlipBrushY(void)
13651 {
13652   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_Y);
13653 }
13654
13655 static void RotateBrush(void)
13656 {
13657   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_XY);
13658   CopyBrushExt(0, 0, 0, 0, 0, CB_FLIP_BRUSH_X);
13659 }
13660
13661 void DumpBrush(void)
13662 {
13663   CopyBrushExt(0, 0, 0, 0, 0, CB_DUMP_BRUSH);
13664 }
13665
13666 void DumpBrush_Small(void)
13667 {
13668   CopyBrushExt(0, 0, 0, 0, 0, CB_DUMP_BRUSH_SMALL);
13669 }
13670
13671 void CopyClipboardToBrush(void)
13672 {
13673   CopyBrushExt(0, 0, 0, 0, 0, CB_CLIPBOARD_TO_BRUSH);
13674 }
13675
13676 void CopyBrushToClipboard(void)
13677 {
13678   CopyBrushExt(0, 0, 0, 0, 0, CB_BRUSH_TO_CLIPBOARD);
13679 }
13680
13681 void CopyBrushToClipboard_Small(void)
13682 {
13683   CopyBrushExt(0, 0, 0, 0, 0, CB_BRUSH_TO_CLIPBOARD_SMALL);
13684 }
13685
13686 void UndoLevelEditorOperation(void)
13687 {
13688   ClickOnGadget(level_editor_gadget[GADGET_ID_UNDO], -1);
13689 }
13690
13691 void RedoLevelEditorOperation(void)
13692 {
13693   ClickOnGadget(level_editor_gadget[GADGET_ID_UNDO], 3);
13694 }
13695
13696 static void FloodFill(int from_x, int from_y, int fill_element)
13697 {
13698   FloodFillLevel(from_x, from_y, fill_element, Tile, lev_fieldx, lev_fieldy);
13699 }
13700
13701 static void FloodFillWall_MM(int from_sx2, int from_sy2, int fill_element)
13702 {
13703   int from_x = from_sx2 + 2 * level_xpos;
13704   int from_y = from_sy2 + 2 * level_ypos;
13705   int max_fillx = lev_fieldx * 2;
13706   int max_filly = lev_fieldy * 2;
13707   short Fill[max_fillx][max_filly];
13708   int x, y;
13709
13710   for (x = 0; x < max_fillx; x++)
13711     for (y = 0; y < max_filly; y++)
13712       Fill[x][y] = getLevelElementHiRes(x, y);
13713
13714   FloodFillLevelExt(from_x, from_y, fill_element, max_fillx, max_filly,
13715                     Fill, max_fillx, max_filly);
13716
13717   for (x = 0; x < max_fillx; x++)
13718     for (y = 0; y < max_filly; y++)
13719       if (Fill[x][y] == fill_element)
13720         SetLevelElementHiRes(x, y, Fill[x][y]);
13721 }
13722
13723 // values for DrawLevelText() modes
13724 #define TEXT_INIT               0
13725 #define TEXT_SETCURSOR          1
13726 #define TEXT_WRITECHAR          2
13727 #define TEXT_BACKSPACE          3
13728 #define TEXT_NEWLINE            4
13729 #define TEXT_END                5
13730 #define TEXT_QUERY_TYPING       6
13731
13732 static int DrawLevelText(int sx, int sy, char letter, int mode)
13733 {
13734   static short delete_buffer[MAX_LEV_FIELDX];
13735   static int start_sx;
13736   static int last_sx, last_sy;
13737   static boolean typing = FALSE;
13738   int letter_element;
13739   int lx = 0, ly = 0;
13740
13741   // map lower case letters to upper case and convert special characters
13742   if (letter >= 'a' && letter <= 'z')
13743     letter_element = EL_CHAR_ASCII0 + letter + (int)('A' - 'a');
13744   else if (letter == CHAR_BYTE_UMLAUT_a || letter == CHAR_BYTE_UMLAUT_A)
13745     letter_element = EL_CHAR_AUMLAUT;
13746   else if (letter == CHAR_BYTE_UMLAUT_o || letter == CHAR_BYTE_UMLAUT_O)
13747     letter_element = EL_CHAR_OUMLAUT;
13748   else if (letter == CHAR_BYTE_UMLAUT_u || letter == CHAR_BYTE_UMLAUT_U)
13749     letter_element = EL_CHAR_UUMLAUT;
13750   else if (letter == '^')
13751     letter_element = EL_CHAR_COPYRIGHT;
13752   else
13753     letter_element = EL_CHAR_ASCII0 + letter;
13754
13755   if (mode != TEXT_INIT)
13756   {
13757     if (!typing)
13758       return FALSE;
13759
13760     if (mode != TEXT_SETCURSOR)
13761     {
13762       sx = last_sx;
13763       sy = last_sy;
13764     }
13765
13766     lx = last_sx + level_xpos;
13767     ly = last_sy + level_ypos;
13768   }
13769
13770   switch (mode)
13771   {
13772     case TEXT_INIT:
13773       if (typing)
13774         DrawLevelText(0, 0, 0, TEXT_END);
13775
13776       typing = TRUE;
13777       start_sx = sx;
13778       last_sx = sx;
13779       last_sy = sy;
13780       DrawLevelText(sx, sy, 0, TEXT_SETCURSOR);
13781       break;
13782
13783     case TEXT_SETCURSOR:
13784       DrawEditorElement(last_sx, last_sy, Tile[lx][ly]);
13785       DrawAreaBorder(sx, sy, sx, sy);
13786       StartTextInput(SX + sx * ed_tilesize, SY + sy * ed_tilesize,
13787                      ed_tilesize, ed_tilesize);
13788       last_sx = sx;
13789       last_sy = sy;
13790       break;
13791
13792     case TEXT_WRITECHAR:
13793       if (letter_element >= EL_CHAR_START && letter_element <= EL_CHAR_END)
13794       {
13795         if (new_element1 >= EL_STEEL_CHAR_START &&
13796             new_element1 <= EL_STEEL_CHAR_END)
13797           letter_element = letter_element - EL_CHAR_START + EL_STEEL_CHAR_START;
13798
13799         delete_buffer[sx - start_sx] = Tile[lx][ly];
13800         Tile[lx][ly] = letter_element;
13801
13802         if (sx + 1 < ed_fieldx && lx + 1 < lev_fieldx)
13803           DrawLevelText(sx + 1, sy, 0, TEXT_SETCURSOR);
13804         else if (sy + 1 < ed_fieldy && ly + 1 < lev_fieldy)
13805           DrawLevelText(start_sx, sy + 1, 0, TEXT_SETCURSOR);
13806         else
13807           DrawLevelText(0, 0, 0, TEXT_END);
13808
13809         level.changed = TRUE;
13810       }
13811       break;
13812
13813     case TEXT_BACKSPACE:
13814       if (sx > start_sx)
13815       {
13816         Tile[lx - 1][ly] = delete_buffer[sx - start_sx - 1];
13817         DrawEditorElement(sx - 1, sy, Tile[lx - 1][ly]);
13818         DrawLevelText(sx - 1, sy, 0, TEXT_SETCURSOR);
13819       }
13820       break;
13821
13822     case TEXT_NEWLINE:
13823       if (sy + 1 < ed_fieldy && ly + 1 < lev_fieldy)
13824         DrawLevelText(start_sx, sy + 1, 0, TEXT_SETCURSOR);
13825       else
13826         DrawLevelText(0, 0, 0, TEXT_END);
13827       break;
13828
13829     case TEXT_END:
13830       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13831       DrawEditorElement(sx, sy, Tile[lx][ly]);
13832       StopTextInput();
13833       typing = FALSE;
13834       break;
13835
13836     case TEXT_QUERY_TYPING:
13837       break;
13838
13839     default:
13840       break;
13841   }
13842
13843   return typing;
13844 }
13845
13846 static void SetTextCursor(int unused_sx, int unused_sy, int sx, int sy,
13847                           int element, boolean change_level)
13848 {
13849   int lx = sx + level_xpos;
13850   int ly = sy + level_ypos;
13851
13852   if (element == -1)
13853     DrawEditorElement(sx, sy, Tile[lx][ly]);
13854   else
13855     DrawAreaBorder(sx, sy, sx, sy);
13856 }
13857
13858 static void CheckLevelBorderElement(boolean redraw_playfield)
13859 {
13860   int last_border_element = BorderElement;
13861
13862   SetBorderElement();
13863
13864   if (redraw_playfield && BorderElement != last_border_element)
13865     DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
13866 }
13867
13868 static void CopyLevelToUndoBuffer(int mode)
13869 {
13870   static boolean accumulated_undo = FALSE;
13871   boolean new_undo_buffer_position = TRUE;
13872   int x, y;
13873
13874   if (undo_buffer_steps == 0)
13875     accumulated_undo = FALSE;
13876
13877   switch (mode)
13878   {
13879     case UNDO_IMMEDIATE:
13880       accumulated_undo = FALSE;
13881       break;
13882
13883     case UNDO_ACCUMULATE:
13884       if (accumulated_undo)
13885         new_undo_buffer_position = FALSE;
13886       accumulated_undo = TRUE;
13887       break;
13888
13889     default:
13890       break;
13891   }
13892
13893   if (new_undo_buffer_position)
13894   {
13895     // advance position in undo buffer ring
13896     undo_buffer_position = (undo_buffer_position + 1) % NUM_UNDO_STEPS;
13897
13898     if (undo_buffer_steps < NUM_UNDO_STEPS - 1)
13899       undo_buffer_steps++;
13900   }
13901
13902   // always reset redo buffer when storing level change into undo buffer
13903   redo_buffer_steps = 0;
13904
13905   for (x = 0; x < lev_fieldx; x++)
13906     for (y = 0; y < lev_fieldy; y++)
13907       UndoBuffer[undo_buffer_position][x][y] = Tile[x][y];
13908
13909   // check if drawing operation forces change of border style
13910   CheckLevelBorderElement(TRUE);
13911
13912   level.changed = TRUE;
13913 }
13914
13915 static void RandomPlacement(int new_element)
13916 {
13917   static boolean free_position[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
13918   int num_free_positions = 0;
13919   int num_percentage, num_elements;
13920   int x, y;
13921
13922   ResetIntelliDraw();
13923
13924   // determine number of free positions for randomly placing the new element
13925   for (x = 0; x < lev_fieldx; x++) for (y = 0; y < lev_fieldy; y++)
13926   {
13927     free_position[x][y] =
13928       (random_placement_background_restricted ?
13929        Tile[x][y] == random_placement_background_element :
13930        Tile[x][y] != new_element);
13931
13932     if (free_position[x][y])
13933       num_free_positions++;
13934   }
13935
13936   // determine number of new elements to place there
13937   num_percentage = num_free_positions * random_placement_value / 100;
13938   num_elements = (random_placement_method == RANDOM_USE_PERCENTAGE ?
13939                   num_percentage : random_placement_value);
13940
13941   // if less free positions than elements to place, fill all these positions
13942   if (num_free_positions < num_elements)
13943   {
13944     for (x = 0; x < lev_fieldx; x++)
13945       for (y = 0; y < lev_fieldy; y++)
13946         if (free_position[x][y])
13947           SetElement(x, y, new_element);
13948   }
13949   else
13950   {
13951     while (num_elements > 0)
13952     {
13953       x = GetSimpleRandom(lev_fieldx);
13954       y = GetSimpleRandom(lev_fieldy);
13955
13956       // don't place element at the same position twice
13957       if (free_position[x][y])
13958       {
13959         free_position[x][y] = FALSE;
13960         SetElement(x, y, new_element);
13961         num_elements--;
13962       }
13963     }
13964   }
13965
13966   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
13967   CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
13968 }
13969
13970 static void WrapLevel(int dx, int dy)
13971 {
13972   int wrap_dx = lev_fieldx - dx;
13973   int wrap_dy = lev_fieldy - dy;
13974   int x, y;
13975
13976   for (x = 0; x < lev_fieldx; x++)
13977     for (y = 0; y < lev_fieldy; y++)
13978       TileBackup[x][y] = Tile[x][y];
13979
13980   for (x = 0; x < lev_fieldx; x++)
13981     for (y = 0; y < lev_fieldy; y++)
13982       Tile[x][y] =
13983         TileBackup[(x + wrap_dx) % lev_fieldx][(y + wrap_dy) % lev_fieldy];
13984
13985   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
13986   CopyLevelToUndoBuffer(UNDO_ACCUMULATE);
13987 }
13988
13989 static void DrawAreaElementHighlight(boolean highlighted,
13990                                      boolean highlighted_similar)
13991 {
13992   DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
13993
13994   if (!highlighted)
13995     return;
13996
13997   int x, y;
13998
13999   for (x = 0; x < ed_fieldx; x++)
14000   {
14001     for (y = 0; y < ed_fieldy; y++)
14002     {
14003       boolean highlight = FALSE;
14004       int lx = x + level_xpos;
14005       int ly = y + level_ypos;
14006
14007       if (!IN_LEV_FIELD(lx, ly))
14008         continue;
14009
14010       // check if element is the same
14011       if (Tile[lx][ly] == new_element1)
14012         highlight = TRUE;
14013
14014       // check if element is similar
14015       if (highlighted_similar &&
14016           strEqual(element_info[Tile[lx][ly]].class_name,
14017                    element_info[new_element1].class_name))
14018         highlight = TRUE;
14019
14020       // check if element is matching MM style wall
14021       if (IS_MM_WALL(Tile[lx][ly]) &&
14022           map_mm_wall_element(Tile[lx][ly]) == new_element1)
14023         highlight = TRUE;
14024
14025       if (!highlight)
14026         continue;
14027
14028       if (IS_MM_WALL(Tile[lx][ly]) && !highlighted_similar)
14029       {
14030         int i;
14031
14032         for (i = 0; i < 4; i++)
14033         {
14034           if (!(MM_WALL_BITS(Tile[lx][ly]) & (1 << i)))
14035             continue;
14036
14037           int xx = x * 2 + (i % 2);
14038           int yy = y * 2 + (i / 2);
14039           int sx = SX + xx * ed_tilesize / 2;
14040           int sy = SY + yy * ed_tilesize / 2;
14041           int from_sx = sx;
14042           int from_sy = sy;
14043           int to_sx = sx + ed_tilesize / 2 - 1;
14044           int to_sy = sy + ed_tilesize / 2 - 1;
14045
14046           DrawSimpleWhiteLine(drawto, from_sx, from_sy, to_sx,   from_sy);
14047           DrawSimpleWhiteLine(drawto, to_sx,   from_sy, to_sx,   to_sy);
14048           DrawSimpleWhiteLine(drawto, to_sx,   to_sy,   from_sx, to_sy);
14049           DrawSimpleWhiteLine(drawto, from_sx, to_sy,   from_sx, from_sy);
14050         }
14051       }
14052       else
14053       {
14054         int sx = SX + x * ed_tilesize;
14055         int sy = SY + y * ed_tilesize;
14056         int from_sx = sx;
14057         int from_sy = sy;
14058         int to_sx = sx + ed_tilesize - 1;
14059         int to_sy = sy + ed_tilesize - 1;
14060
14061         DrawSimpleWhiteLine(drawto, from_sx, from_sy, to_sx,   from_sy);
14062         DrawSimpleWhiteLine(drawto, to_sx,   from_sy, to_sx,   to_sy);
14063         DrawSimpleWhiteLine(drawto, to_sx,   to_sy,   from_sx, to_sy);
14064         DrawSimpleWhiteLine(drawto, from_sx, to_sy,   from_sx, from_sy);
14065       }
14066     }
14067   }
14068 }
14069
14070 static void CopyLevelTemplateToUserLevelSet(char *levelset_subdir)
14071 {
14072   char *template_filename_old = getLocalLevelTemplateFilename();
14073   char *template_filename_new =
14074     getPath2(getUserLevelDir(levelset_subdir), LEVELTEMPLATE_FILENAME);
14075
14076   if (copyFile(template_filename_old, template_filename_new) != 0)
14077     Request("Cannot copy level template!", REQ_CONFIRM);
14078
14079   free(template_filename_new);
14080 }
14081
14082 static void HandleDrawingAreas(struct GadgetInfo *gi)
14083 {
14084   static boolean started_inside_drawing_area = FALSE;
14085   static int last_sx = -1;
14086   static int last_sy = -1;
14087   static int last_sx2 = -1;
14088   static int last_sy2 = -1;
14089   int id = gi->custom_id;
14090   int type_id = gi->custom_type_id;
14091   boolean button_press_event;
14092   boolean button_release_event;
14093   boolean inside_drawing_area = !gi->event.off_borders;
14094   boolean draw_level = (id == GADGET_ID_DRAWING_LEVEL);
14095   int actual_drawing_function;
14096   int button = gi->event.button;
14097   int new_element = BUTTON_ELEMENT(button);
14098   int sx = gi->event.x, sy = gi->event.y;
14099   int min_sx = 0, min_sy = 0;
14100   int max_sx = gi->drawing.area_xsize - 1, max_sy = gi->drawing.area_ysize - 1;
14101   int item_xsize = gi->drawing.item_xsize, item_ysize = gi->drawing.item_ysize;
14102   int mini_item_xsize = item_xsize / 2, mini_item_ysize = item_ysize / 2;
14103   int sx2 = gi->event.mx / mini_item_xsize;
14104   int sy2 = gi->event.my / mini_item_ysize;
14105   int dx = sx2 % 2;
14106   int dy = sy2 % 2;
14107   int lx = 0, ly = 0;
14108   int x, y;
14109
14110   button_press_event = (gi->event.type == GD_EVENT_PRESSED);
14111   button_release_event = (gi->event.type == GD_EVENT_RELEASED);
14112
14113   // make sure to stay inside drawing area boundaries
14114   sx = (sx < min_sx ? min_sx : sx > max_sx ? max_sx : sx);
14115   sy = (sy < min_sy ? min_sy : sy > max_sy ? max_sy : sy);
14116
14117   if (draw_level)
14118   {
14119     int min_lx = 0, min_ly = 0;
14120     int max_lx = lev_fieldx - 1, max_ly = lev_fieldy - 1;
14121
14122     // get positions inside level field
14123     lx = sx + level_xpos;
14124     ly = sy + level_ypos;
14125
14126     if (!IN_LEV_FIELD(lx, ly))
14127       inside_drawing_area = FALSE;
14128
14129     // make sure to stay inside level field boundaries
14130     lx = (lx < min_lx ? min_lx : lx > max_lx ? max_lx : lx);
14131     ly = (ly < min_ly ? min_ly : ly > max_ly ? max_ly : ly);
14132
14133     // correct drawing area positions accordingly
14134     sx = lx - level_xpos;
14135     sy = ly - level_ypos;
14136   }
14137
14138   // also correct MM wall-sized (double) drawing area positions accordingly
14139   if (sx2 / 2 < sx || sx2 / 2 > sx)
14140   {
14141     dx = (sx2 / 2 < sx ? 0 : 1);
14142     sx2 = sx * 2 + dx;
14143   }
14144   if (sy2 / 2 < sy || sy2 / 2 > sy)
14145   {
14146     dy = (sy2 / 2 < sy ? 0 : 1);
14147     sy2 = sy * 2 + dy;
14148   }
14149
14150   if (!button_press_event && !button_release_event)
14151   {
14152     int old_element = (IN_LEV_FIELD(lx, ly) ? Tile[lx][ly] : EL_UNDEFINED);
14153     boolean hires_drawing = (level.game_engine_type == GAME_ENGINE_TYPE_MM &&
14154                              isHiresTileElement(old_element) &&
14155                              isHiresDrawElement(new_element));
14156
14157     // prevent handling events for every pixel position when moving mouse
14158     if ((sx == last_sx && sy == last_sy && !hires_drawing) ||
14159         (sx2 == last_sx2 && sy2 == last_sy2))
14160       return;
14161   }
14162
14163   last_sx = sx;
14164   last_sy = sy;
14165   last_sx2 = sx2;
14166   last_sy2 = sy2;
14167
14168   if (button_press_event)
14169     started_inside_drawing_area = inside_drawing_area;
14170
14171   if (!started_inside_drawing_area)
14172     return;
14173
14174   if (!IS_VALID_BUTTON(button))
14175     return;
14176
14177   // handle info callback for each invocation of action callback
14178   gi->callback_info(gi);
14179
14180   // automatically switch to 'single item' drawing mode, if needed
14181   actual_drawing_function =
14182     (draw_level || drawing_function == GADGET_ID_PICK_ELEMENT ?
14183      drawing_function : GADGET_ID_SINGLE_ITEMS);
14184
14185   // clicking into drawing area with pressed Control key picks element
14186   if (GetKeyModState() & KMOD_Control)
14187   {
14188     last_drawing_function = drawing_function;
14189     actual_drawing_function = GADGET_ID_PICK_ELEMENT;
14190   }
14191
14192   if (GetKeyModState() & KMOD_Shift)
14193   {
14194     if (button_press_event || button_release_event)
14195       ResetIntelliDraw();
14196   }
14197
14198   SetDrawModeHiRes(-1);         // reset to normal draw mode
14199
14200   switch (actual_drawing_function)
14201   {
14202     case GADGET_ID_SINGLE_ITEMS:
14203       if (draw_level)
14204       {
14205         if (button_release_event)
14206         {
14207           CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14208
14209           if (edit_mode == ED_MODE_DRAWING && draw_with_brush &&
14210               !inside_drawing_area)
14211             DeleteBrushFromCursor();
14212
14213           break;
14214         }
14215
14216         if (draw_with_brush)
14217         {
14218           CopyBrushToLevel(sx, sy, button);
14219         }
14220         else
14221         {
14222           SetDrawModeHiRes(new_element);
14223
14224           if (IS_PLAYER_ELEMENT(new_element) || IS_MM_MCDUFFIN(new_element))
14225           {
14226             // remove player at old position
14227             for (y = 0; y < lev_fieldy; y++)
14228             {
14229               for (x = 0; x < lev_fieldx; x++)
14230               {
14231                 int old_element = Tile[x][y];
14232
14233                 if (IS_PLAYER_ELEMENT(old_element) &&
14234                     IS_PLAYER_ELEMENT(new_element))
14235                 {
14236                   int replaced_with_element =
14237                     (old_element == EL_SOKOBAN_FIELD_PLAYER &&
14238                      new_element == EL_PLAYER_1 ? EL_SOKOBAN_FIELD_EMPTY :
14239
14240                      old_element == EL_SOKOBAN_FIELD_PLAYER &&
14241                      new_element == old_element ? EL_SOKOBAN_FIELD_EMPTY :
14242
14243                      new_element == EL_SOKOBAN_FIELD_PLAYER &&
14244                      old_element == EL_PLAYER_1 ? EL_EMPTY :
14245
14246                      new_element >= EL_PLAYER_1 &&
14247                      new_element <= EL_PLAYER_4 &&
14248                      new_element == old_element ? EL_EMPTY :
14249
14250                      old_element);
14251
14252                   SetElement(x, y, replaced_with_element);
14253                 }
14254                 else if (IS_MM_MCDUFFIN(old_element) &&
14255                          IS_MM_MCDUFFIN(new_element))
14256                 {
14257                   // remove McDuffin at old position
14258                   SetElement(x, y, EL_EMPTY);
14259                 }
14260               }
14261             }
14262           }
14263
14264           SetElementButton(lx, ly, dx, dy, new_element, button);
14265         }
14266       }
14267       else if (!button_release_event)
14268       {
14269         int pos = sx * drawingarea_info[type_id].area_ysize + sy;
14270
14271         if (item_xsize == MINI_TILEX && item_ysize == MINI_TILEY)
14272           DrawMiniGraphicExt(drawto,
14273                              gi->x + sx * MINI_TILEX,
14274                              gi->y + sy * MINI_TILEY,
14275                              el2edimg(new_element));
14276         else
14277           DrawFixedGraphicExt(drawto,
14278                               gi->x + sx * TILEX,
14279                               gi->y + sy * TILEY,
14280                               el2edimg(new_element), 0);
14281
14282         if (id == GADGET_ID_CUSTOM_GRAPHIC)
14283           new_element = GFX_ELEMENT(new_element);
14284
14285         drawingarea_info[type_id].value[pos] = new_element;
14286
14287         CopyElementPropertiesToGame(properties_element);
14288
14289         if (id == GADGET_ID_CUSTOM_GRAPHIC)
14290         {
14291           UpdateCustomElementGraphicGadgets();
14292
14293           FrameCounter = 0;     // restart animation frame counter
14294         }
14295       }
14296       break;
14297
14298     case GADGET_ID_CONNECTED_ITEMS:
14299       {
14300         static int last_sx = -1;
14301         static int last_sy = -1;
14302
14303         if (button_release_event)
14304           CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14305
14306         SetDrawModeHiRes(new_element);
14307
14308         if (getDrawModeHiRes())
14309         {
14310           sx = sx2;
14311           sy = sy2;
14312         }
14313
14314         if (!button_press_event)
14315           DrawLine(last_sx, last_sy, sx, sy, new_element, TRUE);
14316
14317         last_sx = sx;
14318         last_sy = sy;
14319       }
14320       break;
14321
14322     case GADGET_ID_LINE:
14323     case GADGET_ID_ARC:
14324     case GADGET_ID_RECTANGLE:
14325     case GADGET_ID_FILLED_BOX:
14326       SetDrawModeHiRes(new_element);
14327
14328       if (getDrawModeHiRes())
14329       {
14330         sx = sx2;
14331         sy = sy2;
14332       }
14333       // FALLTHROUGH
14334     case GADGET_ID_GRAB_BRUSH:
14335     case GADGET_ID_TEXT:
14336       {
14337         static int last_sx = -1;
14338         static int last_sy = -1;
14339         static int start_sx = -1;
14340         static int start_sy = -1;
14341         void (*draw_func)(int, int, int, int, int, boolean);
14342
14343         if (drawing_function == GADGET_ID_LINE)
14344           draw_func = DrawLine;
14345         else if (drawing_function == GADGET_ID_ARC)
14346           draw_func = DrawArc;
14347         else if (drawing_function == GADGET_ID_RECTANGLE)
14348           draw_func = DrawBox;
14349         else if (drawing_function == GADGET_ID_FILLED_BOX)
14350           draw_func = DrawFilledBox;
14351         else if (drawing_function == GADGET_ID_GRAB_BRUSH)
14352           draw_func = SelectArea;
14353         else // (drawing_function == GADGET_ID_TEXT)
14354           draw_func = SetTextCursor;
14355
14356         if (button_press_event)
14357         {
14358           draw_func(sx, sy, sx, sy, new_element, FALSE);
14359           start_sx = last_sx = sx;
14360           start_sy = last_sy = sy;
14361
14362           if (drawing_function == GADGET_ID_TEXT)
14363             DrawLevelText(0, 0, 0, TEXT_END);
14364         }
14365         else if (button_release_event)
14366         {
14367           draw_func(start_sx, start_sy, sx, sy, new_element, TRUE);
14368           if (drawing_function == GADGET_ID_GRAB_BRUSH)
14369           {
14370             CopyAreaToBrush(start_sx, start_sy, sx, sy, button);
14371             CopyBrushToCursor(sx, sy);
14372             ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS],
14373                           MB_LEFTBUTTON);
14374             draw_with_brush = TRUE;
14375           }
14376           else if (drawing_function == GADGET_ID_TEXT)
14377             DrawLevelText(sx, sy, 0, TEXT_INIT);
14378           else
14379             CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14380         }
14381         else if (last_sx != sx || last_sy != sy)
14382         {
14383           draw_func(start_sx, start_sy, last_sx, last_sy, -1, FALSE);
14384           if (IS_MM_WALL_EDITOR(new_element))   // clear wall background
14385             draw_func(start_sx, start_sy, sx, sy, EL_EMPTY, FALSE);
14386           draw_func(start_sx, start_sy, sx, sy, new_element, FALSE);
14387           last_sx = sx;
14388           last_sy = sy;
14389         }
14390       }
14391       break;
14392
14393     case GADGET_ID_FLOOD_FILL:
14394       if (button_press_event && Tile[lx][ly] != new_element)
14395       {
14396         if (IS_MM_WALL_EDITOR(new_element))
14397           FloodFillWall_MM(sx2, sy2, new_element);
14398         else
14399           FloodFill(lx, ly, new_element);
14400
14401         DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
14402         CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
14403       }
14404       break;
14405
14406     case GADGET_ID_PICK_ELEMENT:
14407       if (button_release_event)
14408         ClickOnGadget(level_editor_gadget[last_drawing_function],
14409                       MB_LEFTBUTTON);
14410       else if (draw_level)
14411         PickDrawingElement(button, Tile[lx][ly]);
14412       else
14413       {
14414         int pos = sx * drawingarea_info[type_id].area_ysize + sy;
14415
14416         PickDrawingElement(button, drawingarea_info[type_id].value[pos]);
14417       }
14418
14419     default:
14420       break;
14421   }
14422
14423   // do not mark level as modified for certain non-level-changing gadgets
14424   if ((type_id >= ED_DRAWING_ID_EDITOR_FIRST &&
14425        type_id <= ED_DRAWING_ID_EDITOR_LAST) ||
14426       actual_drawing_function == GADGET_ID_GRAB_BRUSH ||
14427       actual_drawing_function == GADGET_ID_PICK_ELEMENT)
14428     return;
14429
14430   level.changed = TRUE;
14431 }
14432
14433 static void HandleCounterButtons(struct GadgetInfo *gi)
14434 {
14435   int gadget_id = gi->custom_id;
14436   int counter_id = gi->custom_type_id;
14437   int button = gi->event.button;
14438   int *counter_value = counterbutton_info[counter_id].value;
14439   int step = BUTTON_STEPSIZE(button) *
14440     (gadget_id == counterbutton_info[counter_id].gadget_id_down ? -1 : +1);
14441
14442   if (counter_id == ED_COUNTER_ID_SELECT_LEVEL)
14443   {
14444     boolean pressed = (gi->event.type == GD_EVENT_PRESSED);
14445     boolean released = (gi->event.type == GD_EVENT_RELEASED);
14446     boolean level_changed = LevelChanged();
14447
14448     if ((level_changed && pressed) || (!level_changed && released))
14449       return;
14450
14451     if (level_changed && !Request("Level has changed! Discard changes?",
14452                                   REQ_ASK))
14453     {
14454       if (gadget_id == counterbutton_info[counter_id].gadget_id_text)
14455         ModifyEditorCounterValue(counter_id, *counter_value);
14456
14457       return;
14458     }
14459   }
14460
14461   if (gadget_id == counterbutton_info[counter_id].gadget_id_text)
14462     *counter_value = gi->textinput.number_value;
14463   else
14464     ModifyEditorCounterValue(counter_id, *counter_value + step);
14465
14466   if (counter_id == ED_COUNTER_ID_SELECT_LEVEL)
14467   {
14468     int last_game_engine_type = level.game_engine_type;
14469
14470     LoadLevel(level_nr);
14471     LoadScore(level_nr);
14472
14473     SaveLevelSetup_SeriesInfo();
14474
14475     TapeErase();
14476
14477     ResetUndoBuffer();
14478     DrawEditModeWindow();
14479
14480     if (level.game_engine_type != last_game_engine_type)
14481     {
14482       // update element selection list
14483       ReinitializeElementList();
14484       ModifyEditorElementList();
14485     }
14486
14487     return;
14488   }
14489
14490   switch (counter_id)
14491   {
14492     case ED_COUNTER_ID_YAMYAM_CONTENT:
14493       DrawYamYamContentAreas();
14494       break;
14495
14496     case ED_COUNTER_ID_BALL_CONTENT:
14497       DrawMagicBallContentAreas();
14498       break;
14499
14500     case ED_COUNTER_ID_ANDROID_CONTENT:
14501       DrawAndroidElementArea();
14502       break;
14503
14504     case ED_COUNTER_ID_GROUP_CONTENT:
14505       DrawGroupElementArea();
14506       CopyGroupElementPropertiesToGame(properties_element);
14507       break;
14508
14509     case ED_COUNTER_ID_INVENTORY_SIZE:
14510       DrawPlayerInitialInventoryArea(properties_element);
14511       break;
14512
14513     case ED_COUNTER_ID_MM_BALL_CONTENT:
14514       DrawMMBallContentArea();
14515       break;
14516
14517     case ED_COUNTER_ID_ENVELOPE_XSIZE:
14518     case ED_COUNTER_ID_ENVELOPE_YSIZE:
14519       DrawEnvelopeTextArea(-1);
14520       break;
14521
14522     case ED_COUNTER_ID_LEVEL_XSIZE:
14523     case ED_COUNTER_ID_LEVEL_YSIZE:
14524       lev_fieldx = level.fieldx;
14525       lev_fieldy = level.fieldy;
14526
14527       // check if resizing of level results in change of border border
14528       SetBorderElement();
14529
14530       break;
14531
14532     default:
14533       break;
14534   }
14535
14536   if ((counter_id >= ED_COUNTER_ID_CUSTOM_FIRST &&
14537        counter_id <= ED_COUNTER_ID_CUSTOM_LAST) ||
14538       (counter_id >= ED_COUNTER_ID_CHANGE_FIRST &&
14539        counter_id <= ED_COUNTER_ID_CHANGE_LAST))
14540     CopyElementPropertiesToGame(properties_element);
14541
14542   // do not mark level as modified for certain non-level-changing gadgets
14543   if ((counter_id >= ED_COUNTER_ID_LEVELSET_FIRST &&
14544        counter_id <= ED_COUNTER_ID_LEVELSET_LAST) ||
14545       (counter_id >= ED_COUNTER_ID_EDITOR_FIRST &&
14546        counter_id <= ED_COUNTER_ID_EDITOR_LAST))
14547     return;
14548
14549   level.changed = TRUE;
14550 }
14551
14552 static void HandleTextInputGadgets(struct GadgetInfo *gi)
14553 {
14554   int type_id = gi->custom_type_id;
14555
14556   strcpy(textinput_info[type_id].value, gi->textinput.value);
14557
14558   if (type_id == ED_TEXTINPUT_ID_ELEMENT_NAME)
14559   {
14560     CopyElementPropertiesToGame(properties_element);
14561
14562     ModifyEditorElementList();  // update changed button info text
14563   }
14564
14565   // do not mark level as modified for certain non-level-changing gadgets
14566   if (type_id >= ED_TEXTINPUT_ID_LEVELSET_FIRST &&
14567       type_id <= ED_TEXTINPUT_ID_LEVELSET_LAST)
14568     return;
14569
14570   level.changed = TRUE;
14571 }
14572
14573 static void HandleTextAreaGadgets(struct GadgetInfo *gi)
14574 {
14575   int type_id = gi->custom_type_id;
14576
14577   strncpy(textarea_info[type_id].value, gi->textarea.value,
14578           MAX_ENVELOPE_TEXT_LEN);
14579   textarea_info[type_id].value[MAX_ENVELOPE_TEXT_LEN] = '\0';
14580
14581   level.changed = TRUE;
14582 }
14583
14584 static void HandleSelectboxGadgets(struct GadgetInfo *gi)
14585 {
14586   int type_id = gi->custom_type_id;
14587   int value_old = *selectbox_info[type_id].value;
14588   int value_new = selectbox_info[type_id].options[gi->selectbox.index].value;
14589
14590   *selectbox_info[type_id].value = value_new;
14591
14592   if (type_id == ED_SELECTBOX_ID_LEVELSET_SAVE_MODE)
14593   {
14594     DrawLevelConfigWindow();
14595   }
14596   else if (type_id == ED_SELECTBOX_ID_SELECT_CHANGE_PAGE)
14597   {
14598     element_info[properties_element].current_change_page = gi->selectbox.index;
14599
14600     DrawPropertiesWindow();
14601   }
14602   else if ((type_id >= ED_SELECTBOX_ID_CUSTOM_FIRST &&
14603             type_id <= ED_SELECTBOX_ID_CUSTOM_LAST) ||
14604            (type_id >= ED_SELECTBOX_ID_CHANGE_FIRST &&
14605             type_id <= ED_SELECTBOX_ID_CHANGE_LAST) ||
14606            (type_id == ED_SELECTBOX_ID_GROUP_CHOICE_MODE))
14607   {
14608     if (type_id == ED_SELECTBOX_ID_ACTION_TYPE)
14609     {
14610       // when changing action type, also check action mode and action arg
14611       if (value_old != value_new)
14612         setSelectboxSpecialActionVariablesIfNeeded();
14613
14614       DrawPropertiesChange();
14615     }
14616
14617     CopyElementPropertiesToGame(properties_element);
14618   }
14619   else if (type_id == ED_SELECTBOX_ID_GAME_ENGINE_TYPE)
14620   {
14621     // show or hide "engine" tabulator depending on game engine type
14622     DrawLevelConfigWindow();
14623
14624     // update element selection list depending on game engine type
14625     ReinitializeElementList();
14626     ModifyEditorElementList();
14627   }
14628   else if (type_id == ED_SELECTBOX_ID_BD_SCHEDULING_TYPE)
14629   {
14630     // update BD cycle delay counter gadgets depending on BD scheduling type
14631     DrawLevelConfigWindow();
14632   }
14633
14634   // do not mark level as modified for certain non-level-changing gadgets
14635   if (type_id == ED_SELECTBOX_ID_LEVELSET_SAVE_MODE ||
14636       type_id == ED_SELECTBOX_ID_SELECT_CHANGE_PAGE)
14637     return;
14638
14639   level.changed = TRUE;
14640 }
14641
14642 static void HandleTextbuttonGadgets(struct GadgetInfo *gi)
14643 {
14644   int type_id = gi->custom_type_id;
14645   int i;
14646
14647   if (type_id >= ED_TAB_BUTTON_ID_LEVELCONFIG_FIRST &&
14648       type_id <= ED_TAB_BUTTON_ID_LEVELCONFIG_LAST)
14649   {
14650     edit_mode_levelconfig = gi->custom_type_id;
14651
14652     DrawLevelConfigWindow();
14653   }
14654   else if (type_id >= ED_TAB_BUTTON_ID_PROPERTIES_FIRST &&
14655            type_id <= ED_TAB_BUTTON_ID_PROPERTIES_LAST)
14656   {
14657     edit_mode_properties = gi->custom_type_id;
14658
14659     DrawPropertiesWindow();
14660   }
14661   else if (type_id == ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_1 ||
14662            type_id == ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE_2)
14663   {
14664     boolean new_template = !fileExists(getLocalLevelTemplateFilename());
14665
14666     // backup original "level.field" (needed to track playfield changes)
14667     CopyPlayfield(level.field, TileBackup);
14668
14669     // "SaveLevelTemplate()" uses "level.field", so copy editor playfield
14670     CopyPlayfield(Tile, level.field);
14671
14672     if (new_template ||
14673         Request("Save this template and kill the old?", REQ_ASK))
14674       SaveLevelTemplate();
14675
14676     if (new_template)
14677       Request("Template saved!", REQ_CONFIRM);
14678
14679     // restore original "level.field" (needed to track playfield changes)
14680     CopyPlayfield(TileBackup, level.field);
14681   }
14682   else if (type_id == ED_TEXTBUTTON_ID_SAVE_LEVELSET)
14683   {
14684     char *levelset_subdir = getLevelSubdirFromSaveMode(levelset_save_mode);
14685
14686     if (levelset_save_mode == LEVELSET_SAVE_MODE_UPDATE &&
14687         leveldir_current->readonly)
14688     {
14689       Request("This level set is read-only!", REQ_CONFIRM);
14690
14691       return;
14692     }
14693
14694     if (strEqual(levelset_name, ""))
14695     {
14696       Request("Please enter level set title!", REQ_CONFIRM);
14697
14698       return;
14699     }
14700
14701     if (strEqual(levelset_author, ""))
14702     {
14703       Request("Please enter level set author!", REQ_CONFIRM);
14704
14705       return;
14706     }
14707
14708     if (levelset_save_mode == LEVELSET_SAVE_MODE_UPDATE)
14709     {
14710       if (UpdateUserLevelSet(levelset_subdir,
14711                              levelset_name,
14712                              levelset_author,
14713                              levelset_num_levels))
14714       {
14715         Request("Level set updated!", REQ_CONFIRM);
14716       }
14717       else
14718       {
14719         Request("Updating level set failed!", REQ_CONFIRM);
14720       }
14721     }
14722     else if (levelset_save_mode == LEVELSET_SAVE_MODE_CREATE)
14723     {
14724       if (level.changed && !Request("Level has changed! Discard changes?",
14725                                      REQ_ASK))
14726         return;
14727
14728       if (CreateUserLevelSet(levelset_subdir,
14729                              levelset_name,
14730                              levelset_author,
14731                              levelset_num_levels,
14732                              levelset_use_levelset_artwork))
14733       {
14734         if (levelset_copy_level_template)
14735           CopyLevelTemplateToUserLevelSet(levelset_subdir);
14736
14737         Request("New level set created!", REQ_CONFIRM);
14738
14739         AddUserLevelSetToLevelInfo(levelset_subdir);
14740         ChangeEditorToLevelSet(levelset_subdir);
14741       }
14742       else
14743       {
14744         Request("Creating new level set failed!", REQ_CONFIRM);
14745       }
14746     }
14747   }
14748   else if (type_id == ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE &&
14749            custom_element.num_change_pages < MAX_CHANGE_PAGES)
14750   {
14751     struct ElementInfo *ei = &element_info[properties_element];
14752
14753     // when modifying custom element, ask for copying level template
14754     if (level.use_custom_template && !AskToCopyAndModifyLevelTemplate())
14755       return;
14756
14757     setElementChangePages(ei, ei->num_change_pages + 1);
14758
14759     // set new change page to be new current change page
14760     ei->current_change_page = ei->num_change_pages - 1;
14761     ei->change = &ei->change_page[ei->current_change_page];
14762
14763     setElementChangeInfoToDefaults(ei->change);
14764
14765     DrawPropertiesWindow();
14766
14767     level.changed = TRUE;
14768   }
14769   else if (type_id == ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE &&
14770            custom_element.num_change_pages > MIN_CHANGE_PAGES)
14771   {
14772     struct ElementInfo *ei = &element_info[properties_element];
14773
14774     // when modifying custom element, ask for copying level template
14775     if (level.use_custom_template && !AskToCopyAndModifyLevelTemplate())
14776       return;
14777
14778     // copy all change pages after change page to be deleted
14779     for (i = ei->current_change_page; i < ei->num_change_pages - 1; i++)
14780       ei->change_page[i] = ei->change_page[i + 1];
14781
14782     setElementChangePages(ei, ei->num_change_pages - 1);
14783
14784     DrawPropertiesWindow();
14785
14786     level.changed = TRUE;
14787   }
14788 }
14789
14790 static void HandleGraphicbuttonGadgets(struct GadgetInfo *gi)
14791 {
14792   int type_id = gi->custom_type_id;
14793
14794   if (type_id == ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE ||
14795       type_id == ED_GRAPHICBUTTON_ID_NEXT_CHANGE_PAGE)
14796   {
14797     struct ElementInfo *ei = &element_info[properties_element];
14798     int step = BUTTON_STEPSIZE(gi->event.button);
14799
14800     step *= (type_id == ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE ? -1 : +1);
14801     ei->current_change_page += step;
14802
14803     if (ei->current_change_page < 0)
14804       ei->current_change_page = 0;
14805     else if (ei->current_change_page >= ei->num_change_pages)
14806       ei->current_change_page = ei->num_change_pages - 1;
14807
14808     DrawPropertiesWindow();
14809   }
14810   else if (type_id == ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE ||
14811            type_id == ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE)
14812   {
14813     struct ElementInfo *ei = &element_info[properties_element];
14814     int current_change_page = ei->current_change_page;
14815
14816     if (type_id == ED_GRAPHICBUTTON_ID_COPY_CHANGE_PAGE)
14817     {
14818       element_info[EL_INTERNAL_CLIPBOARD_CHANGE].change_page[0] =
14819         ei->change_page[current_change_page];
14820     }
14821     else if (type_id == ED_GRAPHICBUTTON_ID_PASTE_CHANGE_PAGE)
14822     {
14823       // when modifying custom element, ask for copying level template
14824       if (level.use_custom_template && !AskToCopyAndModifyLevelTemplate())
14825         return;
14826
14827       ei->change_page[current_change_page] =
14828         element_info[EL_INTERNAL_CLIPBOARD_CHANGE].change_page[0];
14829
14830       level.changed = TRUE;
14831     }
14832
14833     DrawPropertiesWindow();
14834   }
14835 }
14836
14837 static void HandleRadiobuttons(struct GadgetInfo *gi)
14838 {
14839   int type_id = gi->custom_type_id;
14840
14841   *radiobutton_info[type_id].value =
14842     radiobutton_info[type_id].checked_value;
14843
14844   // do not mark level as modified for certain non-level-changing gadgets
14845   if (type_id >= ED_RADIOBUTTON_ID_EDITOR_FIRST &&
14846       type_id <= ED_RADIOBUTTON_ID_EDITOR_LAST)
14847     return;
14848
14849   level.changed = TRUE;
14850 }
14851
14852 static void HandleCheckbuttons(struct GadgetInfo *gi)
14853 {
14854   int type_id = gi->custom_type_id;
14855
14856   *checkbutton_info[type_id].value ^= TRUE;
14857
14858   if (type_id == ED_CHECKBUTTON_ID_CAN_FALL_INTO_ACID ||
14859       type_id == ED_CHECKBUTTON_ID_CAN_MOVE_INTO_ACID ||
14860       type_id == ED_CHECKBUTTON_ID_DONT_COLLIDE_WITH ||
14861       (((type_id >= ED_CHECKBUTTON_ID_CUSTOM_FIRST &&
14862          type_id <= ED_CHECKBUTTON_ID_CUSTOM_LAST) ||
14863         (type_id >= ED_CHECKBUTTON_ID_CHANGE_FIRST &&
14864          type_id <= ED_CHECKBUTTON_ID_CHANGE_LAST)) &&
14865        type_id != ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1))
14866   {
14867     CopyElementPropertiesToGame(properties_element);
14868   }
14869
14870   if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC)
14871   {
14872     UpdateCustomElementGraphicGadgets();
14873   }
14874   else if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_1 ||
14875            type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2)
14876   {
14877     boolean template_related_changes_found = FALSE;
14878     int i;
14879
14880     // check if any custom, group or empty elements have been changed
14881     for (i = 0; i < NUM_FILE_ELEMENTS; i++)
14882       if ((IS_CUSTOM_ELEMENT(i) ||
14883            IS_GROUP_ELEMENT(i) ||
14884            IS_EMPTY_ELEMENT(i)) &&
14885           element_info[i].modified_settings)
14886         template_related_changes_found = TRUE;
14887
14888     if (level.use_custom_template &&
14889         !fileExists(getGlobalLevelTemplateFilename()))
14890     {
14891       Request("No level template found!", REQ_CONFIRM);
14892
14893       level.use_custom_template = FALSE;
14894
14895       ModifyGadget(gi, GDI_CHECKED, FALSE, GDI_END);
14896
14897       return;
14898     }
14899
14900     if (level.use_custom_template &&
14901         template_related_changes_found &&
14902         !Request("Discard changes and use level template?", REQ_ASK))
14903     {
14904       level.use_custom_template = FALSE;
14905
14906       ModifyGadget(gi, GDI_CHECKED, FALSE, GDI_END);
14907
14908       return;
14909     }
14910
14911     if (!level.use_custom_template &&
14912         Request("Copy settings from level template?", REQ_ASK))
14913     {
14914       return;
14915     }
14916
14917     LoadLevelTemplate(level.use_custom_template ? -1 : level_nr);
14918
14919     DrawEditModeWindow();
14920   }
14921   else if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_3)
14922   {
14923     if (setup.editor.use_template_for_new_levels &&
14924         !fileExists(getGlobalLevelTemplateFilename()))
14925     {
14926       Request("No level template found!", REQ_CONFIRM);
14927
14928       return;
14929     }
14930
14931     if (setup.editor.use_template_for_new_levels &&
14932         level.changed &&
14933         !Request("Discard level and load template?", REQ_ASK))
14934     {
14935       return;
14936     }
14937
14938     if (!setup.editor.use_template_for_new_levels &&
14939         level.changed &&
14940         !Request("Discard level and use empty level?", REQ_ASK))
14941     {
14942       return;
14943     }
14944
14945     LoadLevel(level_nr);
14946     LoadScore(level_nr);
14947
14948     TapeErase();
14949
14950     ResetUndoBuffer();
14951     DrawEditModeWindow();
14952   }
14953   else if (type_id == ED_CHECKBUTTON_ID_AUTO_COUNT_GEMS)
14954   {
14955     SetAutomaticNumberOfGemsNeeded();
14956   }
14957
14958   // do not mark level as modified for certain non-level-changing gadgets
14959   if ((type_id >= ED_CHECKBUTTON_ID_LEVELSET_FIRST &&
14960        type_id <= ED_CHECKBUTTON_ID_LEVELSET_LAST) ||
14961       (type_id >= ED_CHECKBUTTON_ID_EDITOR_FIRST &&
14962        type_id <= ED_CHECKBUTTON_ID_EDITOR_LAST &&
14963        type_id != ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE_2) ||
14964       type_id == ED_CHECKBUTTON_ID_STICK_ELEMENT)
14965     return;
14966
14967   level.changed = TRUE;
14968 }
14969
14970 static void HandleControlButtons(struct GadgetInfo *gi)
14971 {
14972   static int last_level_drawing_function = GADGET_ID_SINGLE_ITEMS;
14973   static int last_edit_mode = ED_MODE_DRAWING;
14974   static int last_custom_copy_mode = -1;
14975   static int last_button = 0;
14976   int id = gi->custom_id;
14977   int button = gi->event.button;
14978   int step = BUTTON_STEPSIZE(button);
14979   int new_element = BUTTON_ELEMENT(button);
14980   int last_properties_element = properties_element;
14981   int x, y;
14982
14983   if (edit_mode == ED_MODE_DRAWING && drawing_function == GADGET_ID_TEXT)
14984     DrawLevelText(0, 0, 0, TEXT_END);
14985
14986   if (id < ED_NUM_CTRL1_BUTTONS &&
14987       id != GADGET_ID_SINGLE_ITEMS &&
14988       id != GADGET_ID_PICK_ELEMENT &&
14989       edit_mode != ED_MODE_DRAWING &&
14990       drawing_function != GADGET_ID_PICK_ELEMENT &&
14991       !(GetKeyModState() & KMOD_Control))
14992     ChangeEditModeWindow(ED_MODE_DRAWING);
14993
14994   // element copy mode active, but no element button pressed => deactivate
14995   if (last_custom_copy_mode != -1 && id < ED_NUM_CTRL_BUTTONS)
14996     last_custom_copy_mode = -1;
14997
14998   // when showing palette on element buttons, change element of button used
14999   if (editor.palette.show_on_element_buttons &&
15000       id >= GADGET_ID_ELEMENT_LEFT && id <= GADGET_ID_ELEMENT_RIGHT)
15001   {
15002     last_button = id - GADGET_ID_ELEMENT_LEFT + 1;
15003
15004     id = GADGET_ID_PALETTE;
15005   }
15006
15007   switch (id)
15008   {
15009     case GADGET_ID_SCROLL_LEFT:
15010       if (level_xpos >= 0)
15011       {
15012         if (lev_fieldx < ed_fieldx - 2)
15013           break;
15014
15015         level_xpos -= step;
15016         if (level_xpos < -1)
15017           level_xpos = -1;
15018         if (button == 1)
15019           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_RIGHT);
15020         else
15021           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15022
15023         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
15024                      GDI_SCROLLBAR_ITEM_POSITION, level_xpos + 1, GDI_END);
15025       }
15026       break;
15027
15028     case GADGET_ID_SCROLL_RIGHT:
15029       if (level_xpos <= lev_fieldx - ed_fieldx)
15030       {
15031         if (lev_fieldx < ed_fieldx - 2)
15032           break;
15033
15034         level_xpos += step;
15035         if (level_xpos > lev_fieldx - ed_fieldx + 1)
15036           level_xpos = lev_fieldx - ed_fieldx + 1;
15037         if (button == 1)
15038           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_LEFT);
15039         else
15040           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15041
15042         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
15043                      GDI_SCROLLBAR_ITEM_POSITION, level_xpos + 1, GDI_END);
15044       }
15045       break;
15046
15047     case GADGET_ID_SCROLL_UP:
15048       if (level_ypos >= 0)
15049       {
15050         if (lev_fieldy < ed_fieldy - 2)
15051           break;
15052
15053         level_ypos -= step;
15054         if (level_ypos < -1)
15055           level_ypos = -1;
15056         if (button == 1)
15057           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_DOWN);
15058         else
15059           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15060
15061         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
15062                      GDI_SCROLLBAR_ITEM_POSITION, level_ypos + 1, GDI_END);
15063       }
15064       break;
15065
15066     case GADGET_ID_SCROLL_DOWN:
15067       if (level_ypos <= lev_fieldy - ed_fieldy)
15068       {
15069         if (lev_fieldy < ed_fieldy - 2)
15070           break;
15071
15072         level_ypos += step;
15073         if (level_ypos > lev_fieldy - ed_fieldy + 1)
15074           level_ypos = lev_fieldy - ed_fieldy + 1;
15075         if (button == 1)
15076           ScrollEditorLevel(level_xpos, level_ypos, ED_SCROLL_UP);
15077         else
15078           DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15079
15080         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
15081                      GDI_SCROLLBAR_ITEM_POSITION, level_ypos + 1, GDI_END);
15082       }
15083       break;
15084
15085     case GADGET_ID_SCROLL_HORIZONTAL:
15086       level_xpos = gi->event.item_position - 1;
15087
15088       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15089       BackToFront();
15090
15091       break;
15092
15093     case GADGET_ID_SCROLL_VERTICAL:
15094       level_ypos = gi->event.item_position - 1;
15095
15096       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15097       BackToFront();
15098
15099       break;
15100
15101     case GADGET_ID_SCROLL_LIST_UP:
15102     case GADGET_ID_SCROLL_LIST_DOWN:
15103     case GADGET_ID_SCROLL_LIST_VERTICAL:
15104       if (id == GADGET_ID_SCROLL_LIST_VERTICAL)
15105         element_shift = gi->event.item_position * ED_ELEMENTLIST_BUTTONS_HORIZ;
15106       else
15107       {
15108         step *= (id == GADGET_ID_SCROLL_LIST_UP ? -1 : +1);
15109         element_shift += step * ED_ELEMENTLIST_BUTTONS_HORIZ;
15110
15111         if (element_shift < 0)
15112           element_shift = 0;
15113         if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
15114           element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
15115
15116         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15117                      GDI_SCROLLBAR_ITEM_POSITION,
15118                      element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15119       }
15120
15121       ModifyEditorElementList();
15122
15123       break;
15124
15125     case GADGET_ID_PROPERTIES:
15126       // always switch off element properties when they are already displayed
15127       last_properties_element = new_element;
15128     case GADGET_ID_ELEMENT_LEFT:
15129     case GADGET_ID_ELEMENT_MIDDLE:
15130     case GADGET_ID_ELEMENT_RIGHT:
15131       properties_element = (id == GADGET_ID_ELEMENT_LEFT   ? new_element1 :
15132                             id == GADGET_ID_ELEMENT_MIDDLE ? new_element2 :
15133                             id == GADGET_ID_ELEMENT_RIGHT  ? new_element3 :
15134                             new_element);
15135
15136       if (edit_mode != ED_MODE_PROPERTIES)
15137       {
15138         last_edit_mode = edit_mode;
15139
15140         ChangeEditModeWindow(ED_MODE_PROPERTIES);
15141
15142         last_level_drawing_function = drawing_function;
15143         ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS],
15144                       MB_LEFTBUTTON);
15145       }
15146       else if (properties_element != last_properties_element)
15147       {
15148         DrawEditModeWindow();
15149       }
15150       else
15151       {
15152         ChangeEditModeWindow(ED_MODE_DRAWING);
15153
15154         ClickOnGadget(level_editor_gadget[last_level_drawing_function],
15155                       MB_LEFTBUTTON);
15156       }
15157       break;
15158
15159     case GADGET_ID_PALETTE:
15160       if (edit_mode != ED_MODE_PALETTE)
15161       {
15162         last_edit_mode = edit_mode;
15163
15164         ChangeEditModeWindow(ED_MODE_PALETTE);
15165       }
15166       else
15167       {
15168         ChangeEditModeWindow(last_edit_mode);
15169       }
15170       break;
15171
15172     case GADGET_ID_WRAP_LEFT:
15173       WrapLevel(-step, 0);
15174       break;
15175
15176     case GADGET_ID_WRAP_RIGHT:
15177       WrapLevel(step, 0);
15178       break;
15179
15180     case GADGET_ID_WRAP_UP:
15181       WrapLevel(0, -step);
15182       break;
15183
15184     case GADGET_ID_WRAP_DOWN:
15185       WrapLevel(0, step);
15186       break;
15187
15188     case GADGET_ID_SINGLE_ITEMS:
15189     case GADGET_ID_CONNECTED_ITEMS:
15190     case GADGET_ID_LINE:
15191     case GADGET_ID_ARC:
15192     case GADGET_ID_TEXT:
15193     case GADGET_ID_RECTANGLE:
15194     case GADGET_ID_FILLED_BOX:
15195     case GADGET_ID_FLOOD_FILL:
15196     case GADGET_ID_GRAB_BRUSH:
15197     case GADGET_ID_PICK_ELEMENT:
15198       if (drawing_function != GADGET_ID_PICK_ELEMENT)
15199         last_drawing_function = drawing_function;
15200       drawing_function = id;
15201       draw_with_brush = FALSE;
15202       break;
15203
15204     case GADGET_ID_RANDOM_PLACEMENT:
15205       RandomPlacement(new_element);
15206       break;
15207
15208     case GADGET_ID_ZOOM:
15209       // zoom level editor tile size in or out (or reset to default size)
15210       ed_tilesize = (button == 1 ? ed_tilesize * 2 :
15211                      button == 2 ? ed_tilesize_default :
15212                      button == 3 ? ed_tilesize / 2 : ed_tilesize);
15213
15214       // when using touch device, cycle through all zoom tilesizes
15215       if (runtime.uses_touch_device && ed_tilesize > TILESIZE)
15216         ed_tilesize = MICRO_TILESIZE;
15217
15218       // limit zoom level by upper and lower bound
15219       ed_tilesize = MIN(MAX(MICRO_TILESIZE, ed_tilesize), TILESIZE);
15220
15221       InitZoomLevelSettings(ed_tilesize);
15222
15223       if (edit_mode == ED_MODE_DRAWING)
15224       {
15225         DrawDrawingWindow();
15226
15227         // redraw zoom gadget info text
15228         PrintEditorGadgetInfoText(level_editor_gadget[id]);
15229       }
15230
15231       // save current editor zoom tilesize
15232       SaveSetup_AutoSetup();
15233
15234       break;
15235
15236     case GADGET_ID_CUSTOM_COPY_FROM:
15237     case GADGET_ID_CUSTOM_COPY_TO:
15238     case GADGET_ID_CUSTOM_EXCHANGE:
15239       last_custom_copy_mode = id;
15240       last_drawing_function = drawing_function;
15241       break;
15242
15243     case GADGET_ID_CUSTOM_COPY:
15244       CopyCustomElement(properties_element, -1, id);
15245       break;
15246
15247     case GADGET_ID_CUSTOM_PASTE:
15248       CopyCustomElement(-1, properties_element, id);
15249       break;
15250
15251     case GADGET_ID_UNDO:
15252       if (button < 0)   // keep button value (even if modifier keys are pressed)
15253         button = -button;
15254       else if (button == 1 && GetKeyModState() & (KMOD_Shift | KMOD_Control))
15255         button = 3;
15256
15257       if (button == 1 && undo_buffer_steps == 0)
15258       {
15259         Request("Undo buffer empty!", REQ_CONFIRM);
15260
15261         break;
15262       }
15263       else if (button == 2)
15264       {
15265         break;
15266       }
15267       else if (button == 3 && redo_buffer_steps == 0)
15268       {
15269         Request("Redo buffer empty!", REQ_CONFIRM);
15270
15271         break;
15272       }
15273
15274       if (edit_mode != ED_MODE_DRAWING)
15275         ChangeEditModeWindow(ED_MODE_DRAWING);
15276
15277       if (button == 1)
15278       {
15279         // undo
15280
15281         undo_buffer_position =
15282           (undo_buffer_position - 1 + NUM_UNDO_STEPS) % NUM_UNDO_STEPS;
15283
15284         undo_buffer_steps--;
15285         redo_buffer_steps++;
15286       }
15287       else
15288       {
15289         // redo
15290
15291         undo_buffer_position = (undo_buffer_position + 1) % NUM_UNDO_STEPS;
15292
15293         undo_buffer_steps++;
15294         redo_buffer_steps--;
15295       }
15296
15297       for (x = 0; x < lev_fieldx; x++)
15298         for (y = 0; y < lev_fieldy; y++)
15299           Tile[x][y] = UndoBuffer[undo_buffer_position][x][y];
15300
15301       // check if undo operation forces change of border style
15302       CheckLevelBorderElement(FALSE);
15303
15304       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15305
15306       break;
15307
15308     case GADGET_ID_CONF:
15309       if (edit_mode != ED_MODE_LEVELCONFIG)
15310       {
15311         last_edit_mode = edit_mode;
15312
15313         ChangeEditModeWindow(ED_MODE_LEVELCONFIG);
15314       }
15315       else
15316       {
15317         ChangeEditModeWindow(ED_MODE_DRAWING);
15318       }
15319       break;
15320
15321     case GADGET_ID_CLEAR:
15322       if (edit_mode != ED_MODE_DRAWING)
15323         ChangeEditModeWindow(ED_MODE_DRAWING);
15324
15325       for (x = 0; x < MAX_LEV_FIELDX; x++)
15326         for (y = 0; y < MAX_LEV_FIELDY; y++)
15327           Tile[x][y] = (button == 1 ? EL_EMPTY : new_element);
15328
15329       CopyLevelToUndoBuffer(GADGET_ID_CLEAR);
15330
15331       DrawEditorLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
15332       break;
15333
15334     case GADGET_ID_SAVE:
15335     {
15336       // saving read-only levels into personal level set modifies global vars
15337       // "leveldir_current" and "level_nr"; restore them after saving level
15338       LevelDirTree *leveldir_former = leveldir_current;
15339       int level_nr_former = level_nr;
15340       char *level_filename;
15341       boolean new_level;
15342
15343       if (leveldir_current->readonly &&
15344           !PrepareSavingIntoPersonalLevelSet())
15345         break;
15346
15347       level_filename = getDefaultLevelFilename(level_nr);
15348       new_level = !fileExists(level_filename);
15349
15350       if (new_level ||
15351           Request("Save this level and kill the old?", REQ_ASK))
15352       {
15353         if (leveldir_former->readonly)
15354           ModifyLevelConfigForSavingIntoPersonalLevelSet(leveldir_former->name);
15355
15356         SetAutomaticNumberOfGemsNeeded();
15357
15358         CopyPlayfield(Tile, level.field);
15359         SaveLevel(level_nr);
15360
15361         level.changed = FALSE;
15362
15363         if (new_level)
15364         {
15365           char level_saved_msg[64];
15366
15367           if (leveldir_former->readonly)
15368             sprintf(level_saved_msg,
15369                     "Level saved as level %d into personal level set!",
15370                     level_nr);
15371           else
15372             strcpy(level_saved_msg, "Level saved!");
15373
15374           Request(level_saved_msg, REQ_CONFIRM);
15375         }
15376       }
15377
15378       // "cd" back to copied-from levelset (in case of saved read-only level)
15379       leveldir_current = leveldir_former;
15380       level_nr = level_nr_former;
15381
15382       break;
15383     }
15384
15385     case GADGET_ID_TEST:
15386       if (LevelChanged())
15387         level.game_version = GAME_VERSION_ACTUAL;
15388
15389       CopyPlayfield(level.field, TileBackup);
15390       CopyPlayfield(Tile, level.field);
15391
15392       CopyNativeLevel_RND_to_Native(&level);
15393
15394       UnmapLevelEditorGadgets();
15395       UndrawSpecialEditorDoor();
15396
15397       CloseDoor(DOOR_CLOSE_ALL);
15398
15399       // needed before playing if editor playfield area has different size
15400       ClearRectangle(drawto, REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE);
15401
15402       // redraw_mask = REDRAW_ALL;
15403
15404       level_editor_test_game = TRUE;
15405
15406       StartGameActions(FALSE, setup.autorecord, level.random_seed);
15407
15408       break;
15409
15410     case GADGET_ID_EXIT:
15411       RequestExitLevelEditor(TRUE, FALSE);  // if level has changed, ask user
15412       break;
15413
15414     default:
15415       if (id >= GADGET_ID_ELEMENTLIST_FIRST &&
15416           id <= GADGET_ID_ELEMENTLIST_LAST)
15417       {
15418         int element_position = id - GADGET_ID_ELEMENTLIST_FIRST;
15419
15420         new_element = editor_elements[element_position + element_shift];
15421
15422         if (IS_EDITOR_CASCADE(new_element))
15423         {
15424           int i;
15425
15426           for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
15427           {
15428             int *cascade_element = &(*editor_elements_info[i].headline_list)[0];
15429             boolean *cascade_value = editor_elements_info[i].setup_cascade_value;
15430
15431             if (*cascade_element == new_element)
15432             {
15433               *cascade_element = EL_CASCADE_TOGGLE(*cascade_element);
15434               *cascade_value = IS_EDITOR_CASCADE_ACTIVE(*cascade_element);
15435
15436               // update element selection list
15437               ReinitializeElementList();
15438               ModifyEditorElementList();
15439
15440               // update cascading gadget info text
15441               PrintEditorGadgetInfoText(level_editor_gadget[id]);
15442
15443               // save current editor cascading state
15444               SaveSetup_EditorCascade();
15445
15446               break;
15447             }
15448           }
15449
15450           break;
15451         }
15452
15453         if (last_custom_copy_mode != -1)
15454         {
15455           if (CopyCustomElement(properties_element, new_element,
15456                                 last_custom_copy_mode))
15457           {
15458             ClickOnGadget(level_editor_gadget[last_drawing_function],
15459                           MB_LEFTBUTTON);
15460
15461             last_custom_copy_mode = -1;
15462           }
15463
15464           break;
15465         }
15466
15467         // change element of button used to show palette
15468         if (editor.palette.show_on_element_buttons)
15469           button = last_button;
15470
15471         PickDrawingElement(button, new_element);
15472
15473         if (!stick_element_properties_window &&
15474             drawing_function != GADGET_ID_PICK_ELEMENT &&
15475             !(GetKeyModState() & KMOD_Control))
15476         {
15477           properties_element = new_element;
15478           if (edit_mode == ED_MODE_PROPERTIES)
15479             DrawPropertiesWindow();
15480         }
15481
15482         if (drawing_function == GADGET_ID_PICK_ELEMENT)
15483           ClickOnGadget(level_editor_gadget[last_drawing_function],
15484                         MB_LEFTBUTTON);
15485
15486         if (!use_permanent_palette)
15487           ChangeEditModeWindow(last_edit_mode);
15488       }
15489 #ifdef DEBUG
15490       else if (gi->event.type == GD_EVENT_PRESSED)
15491         Debug("editor", "default: HandleControlButtons: GD_EVENT_PRESSED(%d)", id);
15492       else if (gi->event.type == GD_EVENT_RELEASED)
15493         Debug("editor", "default: HandleControlButtons: GD_EVENT_RELEASED(%d)", id);
15494       else if (gi->event.type == GD_EVENT_MOVING)
15495         Debug("editor", "default: HandleControlButtons: GD_EVENT_MOVING(%d)", id);
15496       else
15497         Debug("editor", "default: HandleControlButtons: ? (id == %d)", id);
15498 #endif
15499       break;
15500   }
15501 }
15502
15503 void HandleLevelEditorKeyInput(Key key)
15504 {
15505   char letter = getCharFromKey(key);
15506
15507   if (drawing_function == GADGET_ID_TEXT &&
15508       DrawLevelText(0, 0, 0, TEXT_QUERY_TYPING) == TRUE)
15509   {
15510     if (letter)
15511       DrawLevelText(0, 0, letter, TEXT_WRITECHAR);
15512     else if (key == KSYM_Delete || key == KSYM_BackSpace)
15513       DrawLevelText(0, 0, 0, TEXT_BACKSPACE);
15514     else if (key == KSYM_Return)
15515       DrawLevelText(0, 0, 0, TEXT_NEWLINE);
15516     else if (key == KSYM_Escape)
15517       DrawLevelText(0, 0, 0, TEXT_END);
15518
15519     return;
15520   }
15521
15522   int id = GADGET_ID_NONE;
15523   int new_element_shift = element_shift;
15524   int step = ED_ELEMENTLIST_BUTTONS_VERT - 1;
15525   int button = MB_LEFTBUTTON;
15526   int i;
15527
15528   switch (key)
15529   {
15530     case KSYM_Left:
15531       id = GADGET_ID_SCROLL_LEFT;
15532       break;
15533     case KSYM_Right:
15534       id = GADGET_ID_SCROLL_RIGHT;
15535       break;
15536     case KSYM_Up:
15537       id = GADGET_ID_SCROLL_UP;
15538       break;
15539     case KSYM_Down:
15540       id = GADGET_ID_SCROLL_DOWN;
15541       break;
15542
15543     case KSYM_Page_Up:
15544     case KSYM_Page_Down:
15545       step *= (key == KSYM_Page_Up ? -1 : +1);
15546       element_shift += step * ED_ELEMENTLIST_BUTTONS_HORIZ;
15547
15548       if (element_shift < 0)
15549         element_shift = 0;
15550       if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
15551         element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
15552
15553       ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15554                    GDI_SCROLLBAR_ITEM_POSITION,
15555                    element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15556
15557       ModifyEditorElementList();
15558
15559       break;
15560
15561     case KSYM_Home:
15562     case KSYM_End:
15563       element_shift = (key == KSYM_Home ? 0 :
15564                        num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS);
15565
15566       ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15567                    GDI_SCROLLBAR_ITEM_POSITION,
15568                    element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15569
15570       ModifyEditorElementList();
15571
15572       break;
15573
15574     case KSYM_Insert:
15575     case KSYM_Delete:
15576
15577       // this is needed to prevent interference with running "True X-Mouse"
15578       if (GetKeyModStateFromEvents() & KMOD_Control)
15579         break;
15580
15581       // check for last or next editor cascade block in element list
15582       for (i = 0; i < num_editor_elements; i++)
15583       {
15584         if ((key == KSYM_Insert && i == element_shift) ||
15585             (key == KSYM_Delete && new_element_shift > element_shift))
15586           break;
15587
15588         // jump to next cascade block (or to start of element list)
15589         if (i == 0 || IS_EDITOR_CASCADE(editor_elements[i]))
15590           new_element_shift = i;
15591       }
15592
15593       if (i < num_editor_elements)
15594         element_shift = new_element_shift;
15595
15596       if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
15597         element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
15598
15599       ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
15600                    GDI_SCROLLBAR_ITEM_POSITION,
15601                    element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
15602
15603       ModifyEditorElementList();
15604
15605       break;
15606
15607     case KSYM_Escape:
15608       if (edit_mode == ED_MODE_DRAWING)
15609         RequestExitLevelEditor(setup.ask_on_escape_editor, TRUE);
15610       else if (edit_mode == ED_MODE_LEVELCONFIG)
15611         HandleControlButtons(level_editor_gadget[GADGET_ID_CONF]);
15612       else if (edit_mode == ED_MODE_PROPERTIES)
15613         HandleControlButtons(level_editor_gadget[GADGET_ID_PROPERTIES]);
15614       else if (edit_mode == ED_MODE_PALETTE)
15615         HandleControlButtons(level_editor_gadget[GADGET_ID_PALETTE]);
15616       else              // should never happen
15617         ChangeEditModeWindow(ED_MODE_DRAWING);
15618
15619       break;
15620
15621     default:
15622       break;
15623   }
15624
15625   if (id != GADGET_ID_NONE)
15626     ClickOnGadget(level_editor_gadget[id], button);
15627   else if (letter == '1' || letter == '?')
15628     ClickOnGadget(level_editor_gadget[GADGET_ID_ELEMENT_LEFT], button);
15629   else if (letter == '2')
15630     ClickOnGadget(level_editor_gadget[GADGET_ID_ELEMENT_MIDDLE], button);
15631   else if (letter == '3')
15632     ClickOnGadget(level_editor_gadget[GADGET_ID_ELEMENT_RIGHT], button);
15633   else if (letter == '.')
15634     ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS], button);
15635   else if (letter == 'U')
15636     ClickOnGadget(level_editor_gadget[GADGET_ID_UNDO], 3);
15637   else if (letter == '-' || key == KSYM_KP_Subtract)
15638     ClickOnGadget(level_editor_gadget[GADGET_ID_ZOOM], 3);
15639   else if (letter == '0' || key == KSYM_KP_0)
15640     ClickOnGadget(level_editor_gadget[GADGET_ID_ZOOM], 2);
15641   else if (letter == '+' || key == KSYM_KP_Add ||
15642            letter == '=')       // ("Shift-=" is "+" on US keyboards)
15643     ClickOnGadget(level_editor_gadget[GADGET_ID_ZOOM], 1);
15644   else if (key == KSYM_Return ||
15645            key == KSYM_space ||
15646            key == setup.shortcut.toggle_pause)
15647     ClickOnGadget(level_editor_gadget[GADGET_ID_TEST], button);
15648   else
15649     for (i = 0; i < ED_NUM_CTRL_BUTTONS; i++)
15650       if (letter && letter == controlbutton_info[i].shortcut)
15651         if (!anyTextGadgetActive())
15652           ClickOnGadget(level_editor_gadget[i], button);
15653
15654   if (draw_with_brush)
15655   {
15656     if (letter == 'x')
15657       FlipBrushX();
15658     else if (letter == 'y')
15659       FlipBrushY();
15660     else if (letter == 'z')
15661       RotateBrush();
15662   }
15663 }
15664
15665 static void HandleLevelEditorIdle_Properties(void)
15666 {
15667   int element_border = graphic_info[IMG_EDITOR_ELEMENT_BORDER].border_size;
15668   int x = editor.settings.element_graphic.x + element_border;
15669   int y = editor.settings.element_graphic.y + element_border;
15670   static DelayCounter action_delay = { 0 };
15671   int i;
15672
15673   action_delay.value = GameFrameDelay;
15674
15675   if (!DelayReached(&action_delay))
15676     return;
15677
15678   for (i = 0; i < ED_NUM_SELECTBOX; i++)
15679   {
15680     struct GadgetInfo *gi = level_editor_gadget[selectbox_info[i].gadget_id];
15681
15682     if (gi->mapped && gi->active && gi->selectbox.open)
15683       return;
15684   }
15685
15686   DrawEditorElementAnimation(SX + x, SY + y);
15687
15688   redraw_mask |= REDRAW_FIELD;
15689
15690   FrameCounter++;       // increase animation frame counter
15691 }
15692
15693 static void HandleLevelEditorIdle_Drawing(void)
15694 {
15695   static boolean last_highlighted = FALSE;
15696   static boolean last_highlighted_similar = FALSE;
15697   boolean highlighted = (GetKeyModState() & KMOD_Alt);
15698   boolean highlighted_similar = (GetKeyModState() & KMOD_Shift);
15699
15700   if (highlighted != last_highlighted ||
15701       (highlighted && highlighted_similar != last_highlighted_similar))
15702   {
15703     DrawAreaElementHighlight(highlighted, highlighted_similar);
15704
15705     redraw_mask |= REDRAW_FIELD;
15706   }
15707
15708   last_highlighted = highlighted;
15709   last_highlighted_similar = highlighted_similar;
15710 }
15711
15712 void HandleLevelEditorIdle(void)
15713 {
15714   if (edit_mode == ED_MODE_PROPERTIES)
15715     HandleLevelEditorIdle_Properties();
15716   else if (edit_mode == ED_MODE_DRAWING)
15717     HandleLevelEditorIdle_Drawing();
15718 }
15719
15720 static void ClearEditorGadgetInfoText(void)
15721 {
15722   DrawBackground(INFOTEXT_XPOS, INFOTEXT_YPOS, INFOTEXT_XSIZE, INFOTEXT_YSIZE);
15723 }
15724
15725 void PrintEditorGadgetInfoText(struct GadgetInfo *gi)
15726 {
15727   char infotext[MAX_OUTPUT_LINESIZE + 1];
15728   int max_infotext_len = getMaxInfoTextLength();
15729
15730   if (gi == NULL || strlen(gi->info_text) == 0)
15731     return;
15732
15733   strncpy(infotext, gi->info_text, max_infotext_len);
15734   infotext[max_infotext_len] = '\0';
15735
15736   if (gi->custom_id < ED_NUM_CTRL_BUTTONS)
15737   {
15738     int key = controlbutton_info[gi->custom_id].shortcut;
15739
15740     if (key)
15741     {
15742       char shortcut[MAX_OUTPUT_LINESIZE + 1];
15743
15744       if (gi->custom_id == GADGET_ID_SINGLE_ITEMS)
15745         sprintf(shortcut, " ('.' or '%c')", key);
15746       else if (gi->custom_id == GADGET_ID_PICK_ELEMENT)
15747         sprintf(shortcut, " ('%c' or 'Ctrl')", key);
15748       else if (gi->custom_id == GADGET_ID_TEST)
15749         sprintf(shortcut, " ('Enter' or 'Shift-%c')", key);
15750       else if (gi->custom_id == GADGET_ID_UNDO)
15751         sprintf(shortcut, " ('%c/Shift-U')", key);
15752       else if (gi->custom_id == GADGET_ID_ZOOM)
15753         sprintf(shortcut, " ('%c', '0', '-')", key);
15754       else
15755         sprintf(shortcut, " ('%s%c')",
15756                 (key >= 'A' && key <= 'Z' ? "Shift-" : ""), key);
15757
15758       if (strlen(infotext) + strlen(shortcut) <= max_infotext_len)
15759         strcat(infotext, shortcut);
15760     }
15761   }
15762
15763   DrawText(INFOTEXT_XPOS, INFOTEXT_YPOS, infotext, INFOTEXT_FONT);
15764 }
15765
15766 void HandleEditorGadgetInfoText(void *ptr)
15767 {
15768   struct GadgetInfo *gi = (struct GadgetInfo *)ptr;
15769
15770   if (game_status != GAME_MODE_EDITOR)
15771     return;
15772
15773   ClearEditorGadgetInfoText();
15774
15775   if (gi == NULL || gi->event.type == GD_EVENT_INFO_LEAVING)
15776     return;
15777
15778   // misuse this function to delete brush cursor, if needed
15779   if (edit_mode == ED_MODE_DRAWING && draw_with_brush)
15780     DeleteBrushFromCursor();
15781
15782   PrintEditorGadgetInfoText(gi);
15783 }
15784
15785 static void HandleDrawingAreaInfo(struct GadgetInfo *gi)
15786 {
15787   int id = gi->custom_id;
15788   int type_id = gi->custom_type_id;
15789   int sx = gi->event.x;
15790   int sy = gi->event.y;
15791   int lx = sx + level_xpos;
15792   int ly = sy + level_ypos;
15793   int min_sx = 0, min_sy = 0;
15794   int max_sx = gi->drawing.area_xsize - 1;
15795   int max_sy = gi->drawing.area_ysize - 1;
15796   int actual_drawing_function = drawing_function;
15797   int max_infotext_len = getMaxInfoTextLength();
15798   char infotext[MAX_OUTPUT_LINESIZE + 1];
15799
15800   infotext[0] = '\0';           // start with empty info text
15801
15802   // pressed Control key: simulate picking element
15803   if (GetKeyModState() & KMOD_Control)
15804     actual_drawing_function = GADGET_ID_PICK_ELEMENT;
15805
15806   ClearEditorGadgetInfoText();
15807
15808   if (gi->event.type == GD_EVENT_INFO_LEAVING)
15809     return;
15810
15811   // make sure to stay inside drawing area boundaries
15812   sx = (sx < min_sx ? min_sx : sx > max_sx ? max_sx : sx);
15813   sy = (sy < min_sy ? min_sy : sy > max_sy ? max_sy : sy);
15814
15815   if (id == GADGET_ID_DRAWING_LEVEL)
15816   {
15817     if (button_status)
15818     {
15819       int min_lx = 0, min_ly = 0;
15820       int max_lx = lev_fieldx - 1, max_ly = lev_fieldy - 1;
15821
15822       // get positions inside level field
15823       lx = sx + level_xpos;
15824       ly = sy + level_ypos;
15825
15826       // make sure to stay inside level field boundaries
15827       lx = (lx < min_lx ? min_lx : lx > max_lx ? max_lx : lx);
15828       ly = (ly < min_ly ? min_ly : ly > max_ly ? max_ly : ly);
15829
15830       // correct drawing area positions accordingly
15831       sx = lx - level_xpos;
15832       sy = ly - level_ypos;
15833     }
15834
15835     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
15836     {
15837       if (button_status)        // if (gi->state == GD_BUTTON_PRESSED)
15838       {
15839         static int start_lx = 0;
15840         static int start_ly = 0;
15841         char *text;
15842
15843         if (gi->event.type == GD_EVENT_PRESSED)
15844         {
15845           start_lx = lx;
15846           start_ly = ly;
15847         }
15848
15849         switch (actual_drawing_function)
15850         {
15851           case GADGET_ID_SINGLE_ITEMS:
15852             text = "Drawing single items";
15853             break;
15854           case GADGET_ID_CONNECTED_ITEMS:
15855             text = "Drawing connected items";
15856             break;
15857           case GADGET_ID_LINE:
15858             text = "Drawing line";
15859             break;
15860           case GADGET_ID_ARC:
15861             text = "Drawing arc";
15862             break;
15863           case GADGET_ID_TEXT:
15864             text = "Setting text cursor";
15865             break;
15866           case GADGET_ID_RECTANGLE:
15867             text = "Drawing rectangle";
15868             break;
15869           case GADGET_ID_FILLED_BOX:
15870             text = "Drawing filled box";
15871             break;
15872           case GADGET_ID_FLOOD_FILL:
15873             text = "Flood fill";
15874             break;
15875           case GADGET_ID_GRAB_BRUSH:
15876             text = "Grabbing brush";
15877             break;
15878           case GADGET_ID_PICK_ELEMENT:
15879             text = "Picking element";
15880             break;
15881
15882           default:
15883             text = "Drawing position";
15884             break;
15885         }
15886
15887         if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
15888           sprintf(infotext, "%s: %d, %d", text, lx, ly);
15889         else
15890           sprintf(infotext, "%s: %d, %d", text,
15891                   ABS(lx - start_lx) + 1, ABS(ly - start_ly) + 1);
15892       }
15893       else if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
15894         strncpy(infotext, getElementInfoText(Tile[lx][ly]), max_infotext_len);
15895       else
15896         sprintf(infotext, "Level position: %d, %d", lx, ly);
15897     }
15898
15899     // misuse this function to draw brush cursor, if needed
15900     if (edit_mode == ED_MODE_DRAWING && draw_with_brush && !button_status)
15901     {
15902       if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
15903         CopyBrushToCursor(sx, sy);
15904       else
15905         DeleteBrushFromCursor();
15906     }
15907
15908     if (!draw_with_brush)
15909       UpdateBrushPosition(sx, sy);
15910   }
15911   else if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
15912   {
15913     int pos = sx * drawingarea_info[type_id].area_ysize + sy;
15914     int element = drawingarea_info[type_id].value[pos];
15915
15916     strncpy(infotext, getElementInfoText(element), max_infotext_len);
15917   }
15918   else
15919   {
15920     if (id == GADGET_ID_CUSTOM_CONTENT)
15921       sprintf(infotext, "custom element content position: %d, %d", sx, sy);
15922     else if (id == GADGET_ID_GROUP_CONTENT)
15923       sprintf(infotext, "group element position: %d", sx + 1);
15924     else if (id >= GADGET_ID_YAMYAM_CONTENT_0 &&
15925              id <= GADGET_ID_YAMYAM_CONTENT_7)
15926       sprintf(infotext, "content area %d position: %d, %d",
15927               id - GADGET_ID_YAMYAM_CONTENT_0 + 1, sx, sy);
15928     else if (id >= GADGET_ID_MAGIC_BALL_CONTENT_0 &&
15929              id <= GADGET_ID_MAGIC_BALL_CONTENT_7)
15930       sprintf(infotext, "content area %d position: %d, %d",
15931               id - GADGET_ID_MAGIC_BALL_CONTENT_0 + 1, sx, sy);
15932     else if (id == GADGET_ID_ANDROID_CONTENT)
15933       sprintf(infotext, "android element position: %d", sx + 1);
15934     else if (drawingarea_info[type_id].infotext != NULL)
15935       strcpy(infotext, drawingarea_info[type_id].infotext);
15936   }
15937
15938   infotext[max_infotext_len] = '\0';
15939
15940   if (strlen(infotext) > 0)
15941     DrawTextS(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY, INFOTEXT_FONT, infotext);
15942 }
15943
15944 void RequestExitLevelEditor(boolean ask_if_level_has_changed,
15945                             boolean quick_quit)
15946 {
15947   if (!ask_if_level_has_changed ||
15948       !LevelChanged() ||
15949       Request("Level has changed! Exit without saving?",
15950               REQ_ASK | REQ_STAY_OPEN))
15951   {
15952     struct RectWithBorder *vp_door_1 = &viewport.door_1[GAME_MODE_MAIN];
15953     struct RectWithBorder *vp_door_2 = &viewport.door_2[GAME_MODE_MAIN];
15954
15955     // draw normal door
15956     UndrawSpecialEditorDoor();
15957
15958     // use door animation if door 1 viewport is unchanged and contains toolbox
15959     if (useEditorDoorAnimation())
15960       CloseDoor(DOOR_CLOSE_1 | DOOR_FORCE_ANIM);
15961
15962     // close editor doors if viewport definition is the same as in main menu
15963     if (vp_door_1->x      == DX     &&
15964         vp_door_1->y      == DY     &&
15965         vp_door_1->width  == DXSIZE &&
15966         vp_door_1->height == DYSIZE &&
15967         vp_door_2->x      == VX     &&
15968         vp_door_2->y      == VY     &&
15969         vp_door_2->width  == VXSIZE &&
15970         vp_door_2->height == VYSIZE)
15971       CloseDoor(DOOR_CLOSE_ALL | DOOR_NO_DELAY);
15972     else
15973       SetDoorState(DOOR_CLOSE_ALL);
15974
15975     BackToFront();
15976
15977     if (quick_quit)
15978       FadeSkipNextFadeIn();
15979
15980     SetGameStatus(GAME_MODE_MAIN);
15981
15982     DrawMainMenu();
15983   }
15984   else
15985   {
15986     if (!global.use_envelope_request)
15987     {
15988       CloseDoor(DOOR_CLOSE_1);
15989       OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
15990     }
15991   }
15992 }