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