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