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