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