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