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