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