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