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