rnd-20040218-1-src
[rocksndiamonds.git] / src / editor.c
1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back!               *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 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_CTRL1_BUTTONS_ALT_GFX_YPOS   142
51 #define ED_CTRL3_BUTTONS_ALT_GFX_YPOS   302
52
53 #define ED_CTRL1_BUTTON_XSIZE           22
54 #define ED_CTRL1_BUTTON_YSIZE           22
55 #define ED_CTRL1_BUTTONS_XPOS           6
56 #define ED_CTRL1_BUTTONS_YPOS           6
57 #define ED_CTRL2_BUTTON_XSIZE           30
58 #define ED_CTRL2_BUTTON_YSIZE           20
59 #define ED_CTRL2_BUTTONS_XPOS           5
60 #define ED_CTRL2_BUTTONS_YPOS           99
61 #define ED_CTRL3_BUTTON_XSIZE           22
62 #define ED_CTRL3_BUTTON_YSIZE           22
63 #define ED_CTRL3_BUTTONS_XPOS           6
64 #define ED_CTRL3_BUTTONS_YPOS           6
65
66 #define ED_CTRL1_BUTTONS_HORIZ          4
67 #define ED_CTRL1_BUTTONS_VERT           4
68 #define ED_CTRL2_BUTTONS_HORIZ          3
69 #define ED_CTRL2_BUTTONS_VERT           2
70 #define ED_CTRL3_BUTTONS_HORIZ          3
71 #define ED_CTRL3_BUTTONS_VERT           1
72
73 #define ED_NUM_CTRL1_BUTTONS   (ED_CTRL1_BUTTONS_HORIZ * ED_CTRL1_BUTTONS_VERT)
74 #define ED_NUM_CTRL2_BUTTONS   (ED_CTRL2_BUTTONS_HORIZ * ED_CTRL2_BUTTONS_VERT)
75 #define ED_NUM_CTRL3_BUTTONS   (ED_CTRL3_BUTTONS_HORIZ * ED_CTRL3_BUTTONS_VERT)
76 #define ED_NUM_CTRL1_2_BUTTONS (ED_NUM_CTRL1_BUTTONS + ED_NUM_CTRL2_BUTTONS)
77 #define ED_NUM_CTRL_BUTTONS    (ED_NUM_CTRL1_BUTTONS + \
78                                 ED_NUM_CTRL2_BUTTONS + \
79                                 ED_NUM_CTRL3_BUTTONS)
80
81 /* values for the element list */
82 #define ED_ELEMENTLIST_XPOS             5
83 #define ED_ELEMENTLIST_YPOS             30
84 #define ED_ELEMENTLIST_XSIZE            20
85 #define ED_ELEMENTLIST_YSIZE            20
86 #define ED_ELEMENTLIST_BUTTONS_HORIZ    4
87 #define ED_ELEMENTLIST_BUTTONS_VERT     11
88 #define ED_NUM_ELEMENTLIST_BUTTONS      (ED_ELEMENTLIST_BUTTONS_HORIZ * \
89                                          ED_ELEMENTLIST_BUTTONS_VERT)
90
91 /* standard distances */
92 #define ED_BORDER_SIZE                  3
93 #define ED_BORDER_TEXT_XSIZE            5
94 #define ED_BORDER_AREA_YSIZE            1
95
96 #define ED_GADGET_DISTANCE              2
97 #define ED_GADGET_TEXT_DISTANCE         (2 * ED_GADGET_DISTANCE)
98 #define ED_DRAWINGAREA_TEXT_DISTANCE    (ED_GADGET_TEXT_DISTANCE + \
99                                          MINI_TILEX / 2)
100
101 /* values for the setting windows */
102 #define ED_SETTINGS_XSTART              (3 * MINI_TILEX / 2)
103 #define ED_SETTINGS_YSTART              (MINI_TILEY * 10)
104
105 #define ED_XOFFSET_CHECKBOX             (ED_CHECKBUTTON_XSIZE + \
106                                          2 * ED_GADGET_DISTANCE)
107
108 #define ED_SETTINGS_XOFFSET             ED_XOFFSET_CHECKBOX
109 #define ED_SETTINGS_YOFFSET             (3 * MINI_TILEY / 2)
110
111 #define ED_SETTINGS_XPOS(n)             (ED_SETTINGS_XSTART + \
112                                          n * ED_SETTINGS_XOFFSET)
113 #define ED_SETTINGS_YPOS(n)             (ED_SETTINGS_YSTART + \
114                                          n * ED_SETTINGS_YOFFSET)
115
116 #define ED_SETTINGS1_YPOS               MINI_TILEY
117 #define ED_SETTINGS2_XPOS               MINI_TILEX
118 #define ED_SETTINGS2_YPOS               (ED_SETTINGS1_YPOS + 12 * TILEY - 2)
119
120 /* values for counter gadgets */
121 #define ED_COUNTER_YSTART               (ED_SETTINGS1_YPOS + 2 * TILEY)
122 #define ED_COUNTER_YDISTANCE            (3 * MINI_TILEY)
123 #define ED_COUNTER_YPOS(n)              (ED_COUNTER_YSTART + \
124                                          n * ED_COUNTER_YDISTANCE)
125 #define ED_COUNTER2_YPOS(n)             (ED_COUNTER_YSTART + \
126                                          n * ED_COUNTER_YDISTANCE - 2)
127
128 /* values for element content drawing areas */
129 /* amoeba content */
130 #define ED_AREA_AMOEBA_CONTENT_XPOS     ED_SETTINGS_XPOS(0)
131 #define ED_AREA_AMOEBA_CONTENT_YPOS     (ED_SETTINGS_YPOS(2) + \
132                                          ED_GADGET_DISTANCE)
133
134 /* yamyam content */
135 #define ED_AREA_YAMYAM_CONTENT_XPOS(n)  (2 * MINI_TILEX + \
136                                          5 * (n % 4) * MINI_TILEX)
137 #define ED_AREA_YAMYAM_CONTENT_YPOS(n)  (22 * MINI_TILEY + \
138                                          6 * (n / 4) * MINI_TILEY)
139
140 /* custom change target */
141 #define ED_AREA_ELEM_CONTENT2_YPOS      (ED_SETTINGS_YPOS(1) + \
142                                          ED_GADGET_DISTANCE)
143 /* optional custom graphic */
144 #define ED_AREA_ELEM_CONTENT3_YPOS      (ED_SETTINGS_YPOS(1) + \
145                                          ED_GADGET_DISTANCE)
146 /* custom element content */
147 #define ED_AREA_ELEM_CONTENT4_XPOS      (29 * MINI_TILEX)
148 #define ED_AREA_ELEM_CONTENT4_YPOS      (ED_SETTINGS_YPOS(2) + \
149                                          ED_GADGET_DISTANCE - MINI_TILEY)
150 /* movement enter/leave element */
151 #define ED_AREA_ELEM_CONTENT4a_XPOS     (29 * MINI_TILEX)
152 #define ED_AREA_ELEM_CONTENT4a_YPOS     (ED_SETTINGS_YPOS(10) + \
153                                          ED_GADGET_DISTANCE)
154 #define ED_AREA_ELEM_CONTENT4b_XPOS     (29 * MINI_TILEX)
155 #define ED_AREA_ELEM_CONTENT4b_YPOS     (ED_SETTINGS_YPOS(10) + \
156                                          ED_GADGET_DISTANCE)
157 /* custom change trigger element */
158 #define ED_AREA_ELEM_CONTENT5_YPOS      (ED_SETTINGS_YPOS(5) + \
159                                          ED_GADGET_DISTANCE)
160 /* extended custom change target */
161 #define ED_AREA_ELEM_CONTENT6_XPOS      (29 * MINI_TILEX)
162 #define ED_AREA_ELEM_CONTENT6_YPOS      (ED_SETTINGS_YPOS(9) + \
163                                          ED_GADGET_DISTANCE - MINI_TILEY)
164 /* group element content */
165 #define ED_AREA_GROUP_CONTENT_XPOS      ED_SETTINGS_XPOS(0)
166 #if 1
167 #define ED_AREA_GROUP_CONTENT_YPOS      (ED_SETTINGS_YPOS(4) + \
168                                          ED_GADGET_DISTANCE)
169 #else
170 #define ED_AREA_GROUP_CONTENT_YPOS      (22 * MINI_TILEY)
171 #endif
172
173 /* values for random placement background drawing area */
174 #define ED_AREA_RANDOM_BACKGROUND_YPOS  (31 * MINI_TILEY)
175
176 /* values for scrolling gadgets for drawing area */
177 #define ED_SCROLLBUTTON_XPOS            24
178 #define ED_SCROLLBUTTON_YPOS            0
179 #define ED_SCROLLBAR_XPOS               24
180 #define ED_SCROLLBAR_YPOS               64
181
182 #define ED_SCROLLBUTTON_XSIZE           16
183 #define ED_SCROLLBUTTON_YSIZE           16
184
185 #define ED_SCROLL_UP_XPOS               (SXSIZE - ED_SCROLLBUTTON_XSIZE)
186 #define ED_SCROLL_UP_YPOS               (0)
187 #define ED_SCROLL_DOWN_XPOS             ED_SCROLL_UP_XPOS
188 #define ED_SCROLL_DOWN_YPOS             (SYSIZE - 3 * ED_SCROLLBUTTON_YSIZE)
189 #define ED_SCROLL_LEFT_XPOS             (0)
190 #define ED_SCROLL_LEFT_YPOS             (SYSIZE - 2 * ED_SCROLLBUTTON_YSIZE)
191 #define ED_SCROLL_RIGHT_XPOS            (SXSIZE - 2 * ED_SCROLLBUTTON_XSIZE)
192 #define ED_SCROLL_RIGHT_YPOS            ED_SCROLL_LEFT_YPOS
193 #define ED_SCROLL_HORIZONTAL_XPOS (ED_SCROLL_LEFT_XPOS + ED_SCROLLBUTTON_XSIZE)
194 #define ED_SCROLL_HORIZONTAL_YPOS       ED_SCROLL_LEFT_YPOS
195 #define ED_SCROLL_HORIZONTAL_XSIZE      (SXSIZE - 3 * ED_SCROLLBUTTON_XSIZE)
196 #define ED_SCROLL_HORIZONTAL_YSIZE      ED_SCROLLBUTTON_YSIZE
197 #define ED_SCROLL_VERTICAL_XPOS         ED_SCROLL_UP_XPOS
198 #define ED_SCROLL_VERTICAL_YPOS   (ED_SCROLL_UP_YPOS + ED_SCROLLBUTTON_YSIZE)
199 #define ED_SCROLL_VERTICAL_XSIZE        ED_SCROLLBUTTON_XSIZE
200 #define ED_SCROLL_VERTICAL_YSIZE        (SYSIZE - 4 * ED_SCROLLBUTTON_YSIZE)
201
202 /* values for scrolling gadgets for element list */
203 #define ED_SCROLLBUTTON2_XPOS           50
204 #define ED_SCROLLBUTTON2_YPOS           0
205 #define ED_SCROLLBAR2_XPOS              50
206 #define ED_SCROLLBAR2_YPOS              20
207
208 #define ED_SCROLLBUTTON2_XSIZE          10
209 #define ED_SCROLLBUTTON2_YSIZE          10
210
211 #define ED_SCROLL2_UP_XPOS              85
212 #define ED_SCROLL2_UP_YPOS              30
213 #define ED_SCROLL2_DOWN_XPOS            ED_SCROLL2_UP_XPOS
214 #define ED_SCROLL2_DOWN_YPOS            (ED_SCROLL2_UP_YPOS + \
215                                          ED_ELEMENTLIST_BUTTONS_VERT * \
216                                          ED_ELEMENTLIST_YSIZE - \
217                                          ED_SCROLLBUTTON2_YSIZE)
218 #define ED_SCROLL2_VERTICAL_XPOS        ED_SCROLL2_UP_XPOS
219 #define ED_SCROLL2_VERTICAL_YPOS        (ED_SCROLL2_UP_YPOS + \
220                                          ED_SCROLLBUTTON2_YSIZE)
221 #define ED_SCROLL2_VERTICAL_XSIZE       ED_SCROLLBUTTON2_XSIZE
222 #define ED_SCROLL2_VERTICAL_YSIZE       (ED_ELEMENTLIST_BUTTONS_VERT * \
223                                          ED_ELEMENTLIST_YSIZE - \
224                                          2 * ED_SCROLLBUTTON2_YSIZE)
225
226 /* values for checkbutton gadgets */
227 #define ED_CHECKBUTTON_XSIZE            ED_BUTTON_COUNT_XSIZE
228 #define ED_CHECKBUTTON_YSIZE            ED_BUTTON_COUNT_YSIZE
229 #define ED_CHECKBUTTON_UNCHECKED_XPOS   ED_BUTTON_MINUS_XPOS
230 #define ED_CHECKBUTTON_CHECKED_XPOS     ED_BUTTON_PLUS_XPOS
231 #define ED_CHECKBUTTON_YPOS             (ED_BUTTON_MINUS_YPOS + 22)
232 #define ED_RADIOBUTTON_YPOS             (ED_BUTTON_MINUS_YPOS + 44)
233 #define ED_STICKYBUTTON_YPOS            (ED_BUTTON_MINUS_YPOS + 66)
234
235 /* some positions in the editor control window */
236 #define ED_BUTTON_ELEM_XPOS             6
237 #define ED_BUTTON_ELEM_YPOS             30
238 #define ED_BUTTON_ELEM_XSIZE            22
239 #define ED_BUTTON_ELEM_YSIZE            22
240
241 /* some values for text input, selectbox and counter gadgets */
242 #define ED_BUTTON_COUNT_YPOS            60
243 #define ED_BUTTON_COUNT_XSIZE           20
244 #define ED_BUTTON_COUNT_YSIZE           20
245 #define ED_WIN_COUNT_XPOS               (2 + ED_BUTTON_COUNT_XSIZE + 2)
246 #define ED_WIN_COUNT_YPOS               ED_BUTTON_COUNT_YPOS
247 #define ED_WIN_COUNT_XSIZE              52
248 #define ED_WIN_COUNT_YSIZE              ED_BUTTON_COUNT_YSIZE
249 #define ED_WIN_COUNT2_XPOS              27
250 #define ED_WIN_COUNT2_YPOS              3
251 #define ED_WIN_COUNT2_XSIZE             46
252 #define ED_WIN_COUNT2_YSIZE             ED_BUTTON_COUNT_YSIZE
253
254 #define ED_BUTTON_MINUS_XPOS            2
255 #define ED_BUTTON_MINUS_YPOS            ED_BUTTON_COUNT_YPOS
256 #define ED_BUTTON_MINUS_XSIZE           ED_BUTTON_COUNT_XSIZE
257 #define ED_BUTTON_MINUS_YSIZE           ED_BUTTON_COUNT_YSIZE
258 #define ED_BUTTON_PLUS_XPOS             (ED_WIN_COUNT_XPOS + \
259                                          ED_WIN_COUNT_XSIZE + 2)
260 #define ED_BUTTON_PLUS_YPOS             ED_BUTTON_COUNT_YPOS
261 #define ED_BUTTON_PLUS_XSIZE            ED_BUTTON_COUNT_XSIZE
262 #define ED_BUTTON_PLUS_YSIZE            ED_BUTTON_COUNT_YSIZE
263
264 #define ED_SELECTBOX_XPOS               ED_WIN_COUNT_XPOS
265 #define ED_SELECTBOX_YPOS               (ED_WIN_COUNT_YPOS + \
266                                          2 + ED_WIN_COUNT_YSIZE)
267 #define ED_SELECTBOX_XSIZE              ED_WIN_COUNT_XSIZE
268 #define ED_SELECTBOX_YSIZE              ED_WIN_COUNT_YSIZE
269
270 #define ED_SELECTBOX_BUTTON_XSIZE       14
271
272 #define ED_TEXTBUTTON_XPOS              ED_WIN_COUNT_XPOS
273 #define ED_TEXTBUTTON_YPOS              (ED_WIN_COUNT_YPOS + \
274                                          4 * (2 + ED_WIN_COUNT_YSIZE))
275 #define ED_TEXTBUTTON_INACTIVE_YPOS     ED_TEXTBUTTON_YPOS
276
277 #define ED_TEXTBUTTON_TAB_XPOS          ED_WIN_COUNT_XPOS
278 #define ED_TEXTBUTTON_TAB_YPOS          (ED_WIN_COUNT_YPOS + \
279                                          2 * (2 + ED_WIN_COUNT_YSIZE))
280 #define ED_TEXTBUTTON_TAB_INACTIVE_YPOS (ED_WIN_COUNT_YPOS + \
281                                          3 * (2 + ED_WIN_COUNT_YSIZE))
282
283 #define ED_TEXTBUTTON_XSIZE             ED_WIN_COUNT_XSIZE
284 #define ED_TEXTBUTTON_YSIZE             ED_WIN_COUNT_YSIZE
285
286 /* values for ClearEditorGadgetInfoText() and HandleGadgetInfoText() */
287 #define INFOTEXT_XPOS                   SX
288 #define INFOTEXT_YPOS                   (SY + SYSIZE - MINI_TILEX + 2)
289 #define INFOTEXT_XSIZE                  SXSIZE
290 #define INFOTEXT_YSIZE                  MINI_TILEX
291
292
293 /*
294   -----------------------------------------------------------------------------
295   editor gadget definitions
296   -----------------------------------------------------------------------------
297 */
298
299 /* drawing toolbox buttons */
300 #define GADGET_ID_NONE                  -1
301 #define GADGET_ID_TOOLBOX_FIRST         0
302
303 #define GADGET_ID_SINGLE_ITEMS          (GADGET_ID_TOOLBOX_FIRST + 0)
304 #define GADGET_ID_CONNECTED_ITEMS       (GADGET_ID_TOOLBOX_FIRST + 1)
305 #define GADGET_ID_LINE                  (GADGET_ID_TOOLBOX_FIRST + 2)
306 #define GADGET_ID_ARC                   (GADGET_ID_TOOLBOX_FIRST + 3)
307 #define GADGET_ID_RECTANGLE             (GADGET_ID_TOOLBOX_FIRST + 4)
308 #define GADGET_ID_FILLED_BOX            (GADGET_ID_TOOLBOX_FIRST + 5)
309 #define GADGET_ID_WRAP_UP               (GADGET_ID_TOOLBOX_FIRST + 6)
310 #define GADGET_ID_TEXT                  (GADGET_ID_TOOLBOX_FIRST + 7)
311 #define GADGET_ID_FLOOD_FILL            (GADGET_ID_TOOLBOX_FIRST + 8)
312 #define GADGET_ID_WRAP_LEFT             (GADGET_ID_TOOLBOX_FIRST + 9)
313 #define GADGET_ID_PROPERTIES            (GADGET_ID_TOOLBOX_FIRST + 10)
314 #define GADGET_ID_WRAP_RIGHT            (GADGET_ID_TOOLBOX_FIRST + 11)
315 #define GADGET_ID_RANDOM_PLACEMENT      (GADGET_ID_TOOLBOX_FIRST + 12)
316 #define GADGET_ID_GRAB_BRUSH            (GADGET_ID_TOOLBOX_FIRST + 13)
317 #define GADGET_ID_WRAP_DOWN             (GADGET_ID_TOOLBOX_FIRST + 14)
318 #define GADGET_ID_PICK_ELEMENT          (GADGET_ID_TOOLBOX_FIRST + 15)
319
320 #define GADGET_ID_UNDO                  (GADGET_ID_TOOLBOX_FIRST + 16)
321 #define GADGET_ID_INFO                  (GADGET_ID_TOOLBOX_FIRST + 17)
322 #define GADGET_ID_SAVE                  (GADGET_ID_TOOLBOX_FIRST + 18)
323 #define GADGET_ID_CLEAR                 (GADGET_ID_TOOLBOX_FIRST + 19)
324 #define GADGET_ID_TEST                  (GADGET_ID_TOOLBOX_FIRST + 20)
325 #define GADGET_ID_EXIT                  (GADGET_ID_TOOLBOX_FIRST + 21)
326
327 #define GADGET_ID_CUSTOM_COPY_FROM      (GADGET_ID_TOOLBOX_FIRST + 22)
328 #define GADGET_ID_CUSTOM_COPY_TO        (GADGET_ID_TOOLBOX_FIRST + 23)
329 #define GADGET_ID_CUSTOM_EXCHANGE       (GADGET_ID_TOOLBOX_FIRST + 24)
330
331 /* counter button identifiers */
332 #define GADGET_ID_COUNTER_FIRST         (GADGET_ID_TOOLBOX_FIRST + 25)
333
334 #define GADGET_ID_SELECT_LEVEL_DOWN     (GADGET_ID_COUNTER_FIRST + 0)
335 #define GADGET_ID_SELECT_LEVEL_TEXT     (GADGET_ID_COUNTER_FIRST + 1)
336 #define GADGET_ID_SELECT_LEVEL_UP       (GADGET_ID_COUNTER_FIRST + 2)
337 #define GADGET_ID_LEVEL_XSIZE_DOWN      (GADGET_ID_COUNTER_FIRST + 3)
338 #define GADGET_ID_LEVEL_XSIZE_TEXT      (GADGET_ID_COUNTER_FIRST + 4)
339 #define GADGET_ID_LEVEL_XSIZE_UP        (GADGET_ID_COUNTER_FIRST + 5)
340 #define GADGET_ID_LEVEL_YSIZE_DOWN      (GADGET_ID_COUNTER_FIRST + 6)
341 #define GADGET_ID_LEVEL_YSIZE_TEXT      (GADGET_ID_COUNTER_FIRST + 7)
342 #define GADGET_ID_LEVEL_YSIZE_UP        (GADGET_ID_COUNTER_FIRST + 8)
343 #define GADGET_ID_LEVEL_RANDOM_DOWN     (GADGET_ID_COUNTER_FIRST + 9)
344 #define GADGET_ID_LEVEL_RANDOM_TEXT     (GADGET_ID_COUNTER_FIRST + 10)
345 #define GADGET_ID_LEVEL_RANDOM_UP       (GADGET_ID_COUNTER_FIRST + 11)
346 #define GADGET_ID_LEVEL_GEMSLIMIT_DOWN  (GADGET_ID_COUNTER_FIRST + 12)
347 #define GADGET_ID_LEVEL_GEMSLIMIT_TEXT  (GADGET_ID_COUNTER_FIRST + 13)
348 #define GADGET_ID_LEVEL_GEMSLIMIT_UP    (GADGET_ID_COUNTER_FIRST + 14)
349 #define GADGET_ID_LEVEL_TIMELIMIT_DOWN  (GADGET_ID_COUNTER_FIRST + 15)
350 #define GADGET_ID_LEVEL_TIMELIMIT_TEXT  (GADGET_ID_COUNTER_FIRST + 16)
351 #define GADGET_ID_LEVEL_TIMELIMIT_UP    (GADGET_ID_COUNTER_FIRST + 17)
352 #define GADGET_ID_LEVEL_TIMESCORE_DOWN  (GADGET_ID_COUNTER_FIRST + 18)
353 #define GADGET_ID_LEVEL_TIMESCORE_TEXT  (GADGET_ID_COUNTER_FIRST + 19)
354 #define GADGET_ID_LEVEL_TIMESCORE_UP    (GADGET_ID_COUNTER_FIRST + 20)
355 #define GADGET_ID_ELEMENT_SCORE_DOWN    (GADGET_ID_COUNTER_FIRST + 21)
356 #define GADGET_ID_ELEMENT_SCORE_TEXT    (GADGET_ID_COUNTER_FIRST + 22)
357 #define GADGET_ID_ELEMENT_SCORE_UP      (GADGET_ID_COUNTER_FIRST + 23)
358 #define GADGET_ID_ELEMENT_CONTENT_DOWN  (GADGET_ID_COUNTER_FIRST + 24)
359 #define GADGET_ID_ELEMENT_CONTENT_TEXT  (GADGET_ID_COUNTER_FIRST + 25)
360 #define GADGET_ID_ELEMENT_CONTENT_UP    (GADGET_ID_COUNTER_FIRST + 26)
361 #define GADGET_ID_ENVELOPE_XSIZE_DOWN   (GADGET_ID_COUNTER_FIRST + 27)
362 #define GADGET_ID_ENVELOPE_XSIZE_TEXT   (GADGET_ID_COUNTER_FIRST + 28)
363 #define GADGET_ID_ENVELOPE_XSIZE_UP     (GADGET_ID_COUNTER_FIRST + 29)
364 #define GADGET_ID_ENVELOPE_YSIZE_DOWN   (GADGET_ID_COUNTER_FIRST + 30)
365 #define GADGET_ID_ENVELOPE_YSIZE_TEXT   (GADGET_ID_COUNTER_FIRST + 31)
366 #define GADGET_ID_ENVELOPE_YSIZE_UP     (GADGET_ID_COUNTER_FIRST + 32)
367 #define GADGET_ID_CUSTOM_SCORE_DOWN     (GADGET_ID_COUNTER_FIRST + 33)
368 #define GADGET_ID_CUSTOM_SCORE_TEXT     (GADGET_ID_COUNTER_FIRST + 34)
369 #define GADGET_ID_CUSTOM_SCORE_UP       (GADGET_ID_COUNTER_FIRST + 35)
370 #define GADGET_ID_CUSTOM_GEMCOUNT_DOWN  (GADGET_ID_COUNTER_FIRST + 36)
371 #define GADGET_ID_CUSTOM_GEMCOUNT_TEXT  (GADGET_ID_COUNTER_FIRST + 37)
372 #define GADGET_ID_CUSTOM_GEMCOUNT_UP    (GADGET_ID_COUNTER_FIRST + 38)
373 #define GADGET_ID_PUSH_DELAY_FIX_DOWN   (GADGET_ID_COUNTER_FIRST + 39)
374 #define GADGET_ID_PUSH_DELAY_FIX_TEXT   (GADGET_ID_COUNTER_FIRST + 40)
375 #define GADGET_ID_PUSH_DELAY_FIX_UP     (GADGET_ID_COUNTER_FIRST + 41)
376 #define GADGET_ID_PUSH_DELAY_RND_DOWN   (GADGET_ID_COUNTER_FIRST + 42)
377 #define GADGET_ID_PUSH_DELAY_RND_TEXT   (GADGET_ID_COUNTER_FIRST + 43)
378 #define GADGET_ID_PUSH_DELAY_RND_UP     (GADGET_ID_COUNTER_FIRST + 44)
379 #define GADGET_ID_MOVE_DELAY_FIX_DOWN   (GADGET_ID_COUNTER_FIRST + 45)
380 #define GADGET_ID_MOVE_DELAY_FIX_TEXT   (GADGET_ID_COUNTER_FIRST + 46)
381 #define GADGET_ID_MOVE_DELAY_FIX_UP     (GADGET_ID_COUNTER_FIRST + 47)
382 #define GADGET_ID_MOVE_DELAY_RND_DOWN   (GADGET_ID_COUNTER_FIRST + 48)
383 #define GADGET_ID_MOVE_DELAY_RND_TEXT   (GADGET_ID_COUNTER_FIRST + 49)
384 #define GADGET_ID_MOVE_DELAY_RND_UP     (GADGET_ID_COUNTER_FIRST + 50)
385 #define GADGET_ID_EXPLOSION_DELAY_DOWN  (GADGET_ID_COUNTER_FIRST + 51)
386 #define GADGET_ID_EXPLOSION_DELAY_TEXT  (GADGET_ID_COUNTER_FIRST + 52)
387 #define GADGET_ID_EXPLOSION_DELAY_UP    (GADGET_ID_COUNTER_FIRST + 53)
388 #define GADGET_ID_IGNITION_DELAY_DOWN   (GADGET_ID_COUNTER_FIRST + 54)
389 #define GADGET_ID_IGNITION_DELAY_TEXT   (GADGET_ID_COUNTER_FIRST + 55)
390 #define GADGET_ID_IGNITION_DELAY_UP     (GADGET_ID_COUNTER_FIRST + 56)
391 #define GADGET_ID_CHANGE_DELAY_FIX_DOWN (GADGET_ID_COUNTER_FIRST + 57)
392 #define GADGET_ID_CHANGE_DELAY_FIX_TEXT (GADGET_ID_COUNTER_FIRST + 58)
393 #define GADGET_ID_CHANGE_DELAY_FIX_UP   (GADGET_ID_COUNTER_FIRST + 59)
394 #define GADGET_ID_CHANGE_DELAY_RND_DOWN (GADGET_ID_COUNTER_FIRST + 60)
395 #define GADGET_ID_CHANGE_DELAY_RND_TEXT (GADGET_ID_COUNTER_FIRST + 61)
396 #define GADGET_ID_CHANGE_DELAY_RND_UP   (GADGET_ID_COUNTER_FIRST + 62)
397 #define GADGET_ID_CHANGE_CONT_RND_DOWN  (GADGET_ID_COUNTER_FIRST + 63)
398 #define GADGET_ID_CHANGE_CONT_RND_TEXT  (GADGET_ID_COUNTER_FIRST + 64)
399 #define GADGET_ID_CHANGE_CONT_RND_UP    (GADGET_ID_COUNTER_FIRST + 65)
400 #define GADGET_ID_GROUP_CONTENT_DOWN    (GADGET_ID_COUNTER_FIRST + 66)
401 #define GADGET_ID_GROUP_CONTENT_TEXT    (GADGET_ID_COUNTER_FIRST + 67)
402 #define GADGET_ID_GROUP_CONTENT_UP      (GADGET_ID_COUNTER_FIRST + 68)
403
404 /* drawing area identifiers */
405 #define GADGET_ID_DRAWING_AREA_FIRST    (GADGET_ID_COUNTER_FIRST + 69)
406
407 #define GADGET_ID_DRAWING_LEVEL         (GADGET_ID_DRAWING_AREA_FIRST + 0)
408 #define GADGET_ID_ELEMENT_CONTENT_0     (GADGET_ID_DRAWING_AREA_FIRST + 1)
409 #define GADGET_ID_ELEMENT_CONTENT_1     (GADGET_ID_DRAWING_AREA_FIRST + 2)
410 #define GADGET_ID_ELEMENT_CONTENT_2     (GADGET_ID_DRAWING_AREA_FIRST + 3)
411 #define GADGET_ID_ELEMENT_CONTENT_3     (GADGET_ID_DRAWING_AREA_FIRST + 4)
412 #define GADGET_ID_ELEMENT_CONTENT_4     (GADGET_ID_DRAWING_AREA_FIRST + 5)
413 #define GADGET_ID_ELEMENT_CONTENT_5     (GADGET_ID_DRAWING_AREA_FIRST + 6)
414 #define GADGET_ID_ELEMENT_CONTENT_6     (GADGET_ID_DRAWING_AREA_FIRST + 7)
415 #define GADGET_ID_ELEMENT_CONTENT_7     (GADGET_ID_DRAWING_AREA_FIRST + 8)
416 #define GADGET_ID_AMOEBA_CONTENT        (GADGET_ID_DRAWING_AREA_FIRST + 9)
417 #define GADGET_ID_CUSTOM_GRAPHIC        (GADGET_ID_DRAWING_AREA_FIRST + 10)
418 #define GADGET_ID_CUSTOM_CONTENT        (GADGET_ID_DRAWING_AREA_FIRST + 11)
419 #define GADGET_ID_CUSTOM_MOVE_ENTER     (GADGET_ID_DRAWING_AREA_FIRST + 12)
420 #define GADGET_ID_CUSTOM_MOVE_LEAVE     (GADGET_ID_DRAWING_AREA_FIRST + 13)
421 #define GADGET_ID_CUSTOM_CHANGE_TARGET  (GADGET_ID_DRAWING_AREA_FIRST + 14)
422 #define GADGET_ID_CUSTOM_CHANGE_CONTENT (GADGET_ID_DRAWING_AREA_FIRST + 15)
423 #define GADGET_ID_CUSTOM_CHANGE_TRIGGER (GADGET_ID_DRAWING_AREA_FIRST + 16)
424 #define GADGET_ID_GROUP_CONTENT         (GADGET_ID_DRAWING_AREA_FIRST + 17)
425 #define GADGET_ID_RANDOM_BACKGROUND     (GADGET_ID_DRAWING_AREA_FIRST + 18)
426
427 /* text input identifiers */
428 #define GADGET_ID_TEXT_INPUT_FIRST      (GADGET_ID_DRAWING_AREA_FIRST + 19)
429
430 #define GADGET_ID_LEVEL_NAME            (GADGET_ID_TEXT_INPUT_FIRST + 0)
431 #define GADGET_ID_LEVEL_AUTHOR          (GADGET_ID_TEXT_INPUT_FIRST + 1)
432 #define GADGET_ID_ELEMENT_NAME          (GADGET_ID_TEXT_INPUT_FIRST + 2)
433
434 /* text area identifiers */
435 #define GADGET_ID_TEXT_AREA_FIRST       (GADGET_ID_TEXT_INPUT_FIRST + 3)
436
437 #define GADGET_ID_ENVELOPE_INFO         (GADGET_ID_TEXT_AREA_FIRST + 0)
438
439 /* selectbox identifiers */
440 #define GADGET_ID_SELECTBOX_FIRST       (GADGET_ID_TEXT_AREA_FIRST + 1)
441
442 #define GADGET_ID_CUSTOM_WALK_TO_ACTION (GADGET_ID_SELECTBOX_FIRST + 0)
443 #define GADGET_ID_CUSTOM_CONSISTENCY    (GADGET_ID_SELECTBOX_FIRST + 1)
444 #define GADGET_ID_CUSTOM_DEADLINESS     (GADGET_ID_SELECTBOX_FIRST + 2)
445 #define GADGET_ID_CUSTOM_MOVE_PATTERN   (GADGET_ID_SELECTBOX_FIRST + 3)
446 #define GADGET_ID_CUSTOM_MOVE_DIRECTION (GADGET_ID_SELECTBOX_FIRST + 4)
447 #define GADGET_ID_CUSTOM_MOVE_STEPSIZE  (GADGET_ID_SELECTBOX_FIRST + 5)
448 #define GADGET_ID_CUSTOM_MOVE_LEAVE_TYPE (GADGET_ID_SELECTBOX_FIRST + 6)
449 #define GADGET_ID_CUSTOM_SMASH_TARGETS  (GADGET_ID_SELECTBOX_FIRST + 7)
450 #define GADGET_ID_CUSTOM_SLIPPERY_TYPE  (GADGET_ID_SELECTBOX_FIRST + 8)
451 #define GADGET_ID_CUSTOM_ACCESS_TYPE    (GADGET_ID_SELECTBOX_FIRST + 9)
452 #define GADGET_ID_CUSTOM_ACCESS_LAYER   (GADGET_ID_SELECTBOX_FIRST + 10)
453 #define GADGET_ID_CUSTOM_ACCESS_PROTECTED (GADGET_ID_SELECTBOX_FIRST + 11)
454 #define GADGET_ID_CUSTOM_ACCESS_DIRECTION (GADGET_ID_SELECTBOX_FIRST + 12)
455 #define GADGET_ID_CHANGE_TIME_UNITS     (GADGET_ID_SELECTBOX_FIRST + 13)
456 #define GADGET_ID_CHANGE_DIRECT_ACTION  (GADGET_ID_SELECTBOX_FIRST + 14)
457 #define GADGET_ID_CHANGE_OTHER_ACTION   (GADGET_ID_SELECTBOX_FIRST + 15)
458 #define GADGET_ID_CHANGE_SIDES          (GADGET_ID_SELECTBOX_FIRST + 16)
459 #define GADGET_ID_CHANGE_POWER          (GADGET_ID_SELECTBOX_FIRST + 17)
460 #define GADGET_ID_SELECT_CHANGE_PAGE    (GADGET_ID_SELECTBOX_FIRST + 18)
461 #define GADGET_ID_GROUP_CHOICE_MODE     (GADGET_ID_SELECTBOX_FIRST + 19)
462
463 /* textbutton identifiers */
464 #define GADGET_ID_TEXTBUTTON_FIRST      (GADGET_ID_SELECTBOX_FIRST + 20)
465
466 #define GADGET_ID_PROPERTIES_INFO       (GADGET_ID_TEXTBUTTON_FIRST + 0)
467 #define GADGET_ID_PROPERTIES_CONFIG     (GADGET_ID_TEXTBUTTON_FIRST + 1)
468 #define GADGET_ID_PROPERTIES_CONFIG_1   (GADGET_ID_TEXTBUTTON_FIRST + 2)
469 #define GADGET_ID_PROPERTIES_CONFIG_2   (GADGET_ID_TEXTBUTTON_FIRST + 3)
470 #define GADGET_ID_PROPERTIES_CHANGE     (GADGET_ID_TEXTBUTTON_FIRST + 4)
471 #define GADGET_ID_SAVE_AS_TEMPLATE      (GADGET_ID_TEXTBUTTON_FIRST + 5)
472 #define GADGET_ID_ADD_CHANGE_PAGE       (GADGET_ID_TEXTBUTTON_FIRST + 6)
473 #define GADGET_ID_DEL_CHANGE_PAGE       (GADGET_ID_TEXTBUTTON_FIRST + 7)
474
475 /* graphicbutton identifiers */
476 #define GADGET_ID_GRAPHICBUTTON_FIRST   (GADGET_ID_TEXTBUTTON_FIRST + 8)
477
478 #define GADGET_ID_PREV_CHANGE_PAGE      (GADGET_ID_GRAPHICBUTTON_FIRST + 0)
479 #define GADGET_ID_NEXT_CHANGE_PAGE      (GADGET_ID_GRAPHICBUTTON_FIRST + 1)
480
481 /* gadgets for scrolling of drawing area */
482 #define GADGET_ID_SCROLLING_FIRST       (GADGET_ID_GRAPHICBUTTON_FIRST + 2)
483
484 #define GADGET_ID_SCROLL_UP             (GADGET_ID_SCROLLING_FIRST + 0)
485 #define GADGET_ID_SCROLL_DOWN           (GADGET_ID_SCROLLING_FIRST + 1)
486 #define GADGET_ID_SCROLL_LEFT           (GADGET_ID_SCROLLING_FIRST + 2)
487 #define GADGET_ID_SCROLL_RIGHT          (GADGET_ID_SCROLLING_FIRST + 3)
488 #define GADGET_ID_SCROLL_HORIZONTAL     (GADGET_ID_SCROLLING_FIRST + 4)
489 #define GADGET_ID_SCROLL_VERTICAL       (GADGET_ID_SCROLLING_FIRST + 5)
490
491 /* gadgets for scrolling element list */
492 #define GADGET_ID_SCROLLING_LIST_FIRST  (GADGET_ID_SCROLLING_FIRST + 6)
493
494 #define GADGET_ID_SCROLL_LIST_UP        (GADGET_ID_SCROLLING_LIST_FIRST + 0)
495 #define GADGET_ID_SCROLL_LIST_DOWN      (GADGET_ID_SCROLLING_LIST_FIRST + 1)
496 #define GADGET_ID_SCROLL_LIST_VERTICAL  (GADGET_ID_SCROLLING_LIST_FIRST + 2)
497
498 /* checkbuttons/radiobuttons for level/element properties */
499 #define GADGET_ID_CHECKBUTTON_FIRST     (GADGET_ID_SCROLLING_LIST_FIRST + 3)
500
501 #define GADGET_ID_RANDOM_PERCENTAGE     (GADGET_ID_CHECKBUTTON_FIRST + 0)
502 #define GADGET_ID_RANDOM_QUANTITY       (GADGET_ID_CHECKBUTTON_FIRST + 1)
503 #define GADGET_ID_RANDOM_RESTRICTED     (GADGET_ID_CHECKBUTTON_FIRST + 2)
504 #define GADGET_ID_DOUBLE_SPEED          (GADGET_ID_CHECKBUTTON_FIRST + 3)
505 #define GADGET_ID_GRAVITY               (GADGET_ID_CHECKBUTTON_FIRST + 4)
506 #define GADGET_ID_STICK_ELEMENT         (GADGET_ID_CHECKBUTTON_FIRST + 5)
507 #define GADGET_ID_EM_SLIPPERY_GEMS      (GADGET_ID_CHECKBUTTON_FIRST + 6)
508 #define GADGET_ID_BLOCK_LAST_FIELD      (GADGET_ID_CHECKBUTTON_FIRST + 7)
509 #define GADGET_ID_SP_BLOCK_LAST_FIELD   (GADGET_ID_CHECKBUTTON_FIRST + 8)
510 #define GADGET_ID_CUSTOM_EXPLODE_RESULT (GADGET_ID_CHECKBUTTON_FIRST + 9)
511 #define GADGET_ID_CUSTOM_EXPLODE_FIRE   (GADGET_ID_CHECKBUTTON_FIRST + 10)
512 #define GADGET_ID_CUSTOM_EXPLODE_SMASH  (GADGET_ID_CHECKBUTTON_FIRST + 11)
513 #define GADGET_ID_CUSTOM_EXPLODE_IMPACT (GADGET_ID_CHECKBUTTON_FIRST + 12)
514 #define GADGET_ID_CUSTOM_WALK_TO_OBJECT (GADGET_ID_CHECKBUTTON_FIRST + 13)
515 #define GADGET_ID_CUSTOM_DEADLY         (GADGET_ID_CHECKBUTTON_FIRST + 14)
516 #define GADGET_ID_CUSTOM_CAN_MOVE       (GADGET_ID_CHECKBUTTON_FIRST + 15)
517 #define GADGET_ID_CUSTOM_CAN_FALL       (GADGET_ID_CHECKBUTTON_FIRST + 16)
518 #define GADGET_ID_CUSTOM_CAN_SMASH      (GADGET_ID_CHECKBUTTON_FIRST + 17)
519 #define GADGET_ID_CUSTOM_SLIPPERY       (GADGET_ID_CHECKBUTTON_FIRST + 18)
520 #define GADGET_ID_CUSTOM_ACCESSIBLE     (GADGET_ID_CHECKBUTTON_FIRST + 19)
521 #define GADGET_ID_CUSTOM_USE_GRAPHIC    (GADGET_ID_CHECKBUTTON_FIRST + 20)
522 #define GADGET_ID_CUSTOM_USE_TEMPLATE   (GADGET_ID_CHECKBUTTON_FIRST + 21)
523 #define GADGET_ID_CUSTOM_CAN_CHANGE     (GADGET_ID_CHECKBUTTON_FIRST + 22)
524 #define GADGET_ID_CHANGE_USE_CONTENT    (GADGET_ID_CHECKBUTTON_FIRST + 23)
525 #define GADGET_ID_CHANGE_USE_EXPLOSION  (GADGET_ID_CHECKBUTTON_FIRST + 24)
526 #define GADGET_ID_CHANGE_ONLY_COMPLETE  (GADGET_ID_CHECKBUTTON_FIRST + 25)
527 #define GADGET_ID_CHANGE_USE_RANDOM     (GADGET_ID_CHECKBUTTON_FIRST + 26)
528 #define GADGET_ID_CHANGE_DELAY          (GADGET_ID_CHECKBUTTON_FIRST + 27)
529 #define GADGET_ID_CHANGE_BY_DIRECT_ACT  (GADGET_ID_CHECKBUTTON_FIRST + 28)
530 #define GADGET_ID_CHANGE_BY_OTHER_ACT   (GADGET_ID_CHECKBUTTON_FIRST + 29)
531
532 /* gadgets for buttons in element list */
533 #define GADGET_ID_ELEMENTLIST_FIRST     (GADGET_ID_CHECKBUTTON_FIRST + 30)
534 #define GADGET_ID_ELEMENTLIST_LAST      (GADGET_ID_ELEMENTLIST_FIRST +  \
535                                         ED_NUM_ELEMENTLIST_BUTTONS - 1)
536
537 #define NUM_EDITOR_GADGETS              (GADGET_ID_ELEMENTLIST_LAST + 1)
538
539 /* radio button numbers */
540 #define RADIO_NR_NONE                   0
541 #define RADIO_NR_DRAWING_TOOLBOX        1
542 #define RADIO_NR_RANDOM_ELEMENTS        2
543
544 /* values for counter gadgets */
545 #define ED_COUNTER_ID_SELECT_LEVEL      0
546 #define ED_COUNTER_ID_LEVEL_XSIZE       1
547 #define ED_COUNTER_ID_LEVEL_YSIZE       2
548 #define ED_COUNTER_ID_LEVEL_GEMSLIMIT   3
549 #define ED_COUNTER_ID_LEVEL_TIMELIMIT   4
550 #define ED_COUNTER_ID_LEVEL_TIMESCORE   5
551 #define ED_COUNTER_ID_LEVEL_RANDOM      6
552 #define ED_COUNTER_ID_ELEMENT_SCORE     7
553 #define ED_COUNTER_ID_ELEMENT_CONTENT   8
554 #define ED_COUNTER_ID_ENVELOPE_XSIZE    9
555 #define ED_COUNTER_ID_ENVELOPE_YSIZE    10
556 #define ED_COUNTER_ID_CUSTOM_SCORE      11
557 #define ED_COUNTER_ID_CUSTOM_GEMCOUNT   12
558 #define ED_COUNTER_ID_PUSH_DELAY_FIX    13
559 #define ED_COUNTER_ID_PUSH_DELAY_RND    14
560 #define ED_COUNTER_ID_MOVE_DELAY_FIX    15
561 #define ED_COUNTER_ID_MOVE_DELAY_RND    16
562 #define ED_COUNTER_ID_EXPLOSION_DELAY   17
563 #define ED_COUNTER_ID_IGNITION_DELAY    18
564 #define ED_COUNTER_ID_GROUP_CONTENT     19
565 #define ED_COUNTER_ID_CHANGE_DELAY_FIX  20
566 #define ED_COUNTER_ID_CHANGE_DELAY_RND  21
567 #define ED_COUNTER_ID_CHANGE_CONT_RND   22
568
569 #define ED_NUM_COUNTERBUTTONS           23
570
571 #define ED_COUNTER_ID_LEVEL_FIRST       ED_COUNTER_ID_LEVEL_XSIZE
572 #define ED_COUNTER_ID_LEVEL_LAST        ED_COUNTER_ID_LEVEL_RANDOM
573
574 #define ED_COUNTER_ID_CUSTOM1_FIRST     ED_COUNTER_ID_CUSTOM_SCORE
575 #define ED_COUNTER_ID_CUSTOM1_LAST      ED_COUNTER_ID_MOVE_DELAY_RND
576 #define ED_COUNTER_ID_CUSTOM2_FIRST     ED_COUNTER_ID_EXPLOSION_DELAY
577 #define ED_COUNTER_ID_CUSTOM2_LAST      ED_COUNTER_ID_IGNITION_DELAY
578 #define ED_COUNTER_ID_CUSTOM_FIRST      ED_COUNTER_ID_CUSTOM1_FIRST
579 #define ED_COUNTER_ID_CUSTOM_LAST       ED_COUNTER_ID_CUSTOM2_LAST
580
581 #define ED_COUNTER_ID_CHANGE_FIRST      ED_COUNTER_ID_CHANGE_DELAY_FIX
582 #define ED_COUNTER_ID_CHANGE_LAST       ED_COUNTER_ID_CHANGE_CONT_RND
583
584 /* values for scrollbutton gadgets */
585 #define ED_SCROLLBUTTON_ID_AREA_UP      0
586 #define ED_SCROLLBUTTON_ID_AREA_DOWN    1
587 #define ED_SCROLLBUTTON_ID_AREA_LEFT    2
588 #define ED_SCROLLBUTTON_ID_AREA_RIGHT   3
589 #define ED_SCROLLBUTTON_ID_LIST_UP      4
590 #define ED_SCROLLBUTTON_ID_LIST_DOWN    5
591
592 #define ED_NUM_SCROLLBUTTONS            6
593
594 #define ED_SCROLLBUTTON_ID_AREA_FIRST   ED_SCROLLBUTTON_ID_AREA_UP
595 #define ED_SCROLLBUTTON_ID_AREA_LAST    ED_SCROLLBUTTON_ID_AREA_RIGHT
596
597 /* values for scrollbar gadgets */
598 #define ED_SCROLLBAR_ID_AREA_HORIZONTAL 0
599 #define ED_SCROLLBAR_ID_AREA_VERTICAL   1
600 #define ED_SCROLLBAR_ID_LIST_VERTICAL   2
601
602 #define ED_NUM_SCROLLBARS               3
603
604 #define ED_SCROLLBAR_ID_AREA_FIRST      ED_SCROLLBAR_ID_AREA_HORIZONTAL
605 #define ED_SCROLLBAR_ID_AREA_LAST       ED_SCROLLBAR_ID_AREA_VERTICAL
606
607 /* values for text input gadgets */
608 #define ED_TEXTINPUT_ID_LEVEL_NAME      0
609 #define ED_TEXTINPUT_ID_LEVEL_AUTHOR    1
610 #define ED_TEXTINPUT_ID_ELEMENT_NAME    2
611
612 #define ED_NUM_TEXTINPUT                3
613
614 #define ED_TEXTINPUT_ID_LEVEL_FIRST     ED_TEXTINPUT_ID_LEVEL_NAME
615 #define ED_TEXTINPUT_ID_LEVEL_LAST      ED_TEXTINPUT_ID_LEVEL_AUTHOR
616
617 /* values for text area gadgets */
618 #define ED_TEXTAREA_ID_ENVELOPE_INFO    0
619
620 #define ED_NUM_TEXTAREAS                        1
621
622 #define ED_TEXTAREA_ID_LEVEL_FIRST      ED_TEXTAREA_ID_ENVELOPE
623 #define ED_TEXTAREA_ID_LEVEL_LAST       ED_TEXTAREA_ID_ENVELOPE
624
625 /* values for selectbox gadgets */
626 #define ED_SELECTBOX_ID_CUSTOM_ACCESS_TYPE      0
627 #define ED_SELECTBOX_ID_CUSTOM_ACCESS_LAYER     1
628 #define ED_SELECTBOX_ID_CUSTOM_ACCESS_PROTECTED 2
629 #define ED_SELECTBOX_ID_CUSTOM_ACCESS_DIRECTION 3
630 #define ED_SELECTBOX_ID_CUSTOM_WALK_TO_ACTION   4
631 #define ED_SELECTBOX_ID_CUSTOM_MOVE_PATTERN     5
632 #define ED_SELECTBOX_ID_CUSTOM_MOVE_DIRECTION   6
633 #define ED_SELECTBOX_ID_CUSTOM_MOVE_STEPSIZE    7
634 #define ED_SELECTBOX_ID_CUSTOM_MOVE_LEAVE_TYPE  8
635 #define ED_SELECTBOX_ID_CUSTOM_SMASH_TARGETS    9
636 #define ED_SELECTBOX_ID_CUSTOM_SLIPPERY_TYPE    10
637 #define ED_SELECTBOX_ID_CUSTOM_DEADLINESS       11
638 #define ED_SELECTBOX_ID_CUSTOM_CONSISTENCY      12
639 #define ED_SELECTBOX_ID_CHANGE_TIME_UNITS       13
640 #define ED_SELECTBOX_ID_CHANGE_DIRECT_ACTION    14
641 #define ED_SELECTBOX_ID_CHANGE_OTHER_ACTION     15
642 #define ED_SELECTBOX_ID_CHANGE_SIDES            16
643 #define ED_SELECTBOX_ID_CHANGE_POWER            17
644 #define ED_SELECTBOX_ID_SELECT_CHANGE_PAGE      18
645 #define ED_SELECTBOX_ID_GROUP_CHOICE_MODE       19
646
647 #define ED_NUM_SELECTBOX                        20
648
649 #define ED_SELECTBOX_ID_CUSTOM1_FIRST   ED_SELECTBOX_ID_CUSTOM_ACCESS_TYPE
650 #define ED_SELECTBOX_ID_CUSTOM1_LAST    ED_SELECTBOX_ID_CUSTOM_SLIPPERY_TYPE
651 #define ED_SELECTBOX_ID_CUSTOM2_FIRST   ED_SELECTBOX_ID_CUSTOM_DEADLINESS
652 #define ED_SELECTBOX_ID_CUSTOM2_LAST    ED_SELECTBOX_ID_CUSTOM_CONSISTENCY
653 #define ED_SELECTBOX_ID_CUSTOM_FIRST    ED_SELECTBOX_ID_CUSTOM1_FIRST
654 #define ED_SELECTBOX_ID_CUSTOM_LAST     ED_SELECTBOX_ID_CUSTOM2_LAST
655
656 #define ED_SELECTBOX_ID_CHANGE_FIRST    ED_SELECTBOX_ID_CHANGE_TIME_UNITS
657 #define ED_SELECTBOX_ID_CHANGE_LAST     ED_SELECTBOX_ID_SELECT_CHANGE_PAGE
658
659 /* values for textbutton gadgets */
660 #define ED_TEXTBUTTON_ID_PROPERTIES_INFO        0
661 #define ED_TEXTBUTTON_ID_PROPERTIES_CONFIG      1
662 #define ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_1    2
663 #define ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2    3
664 #define ED_TEXTBUTTON_ID_PROPERTIES_CHANGE      4
665 #define ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE       5
666 #define ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE        6
667 #define ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE        7
668
669 #define ED_NUM_TEXTBUTTONS                      8
670
671 #define ED_TEXTBUTTON_ID_PROPERTIES_FIRST ED_TEXTBUTTON_ID_PROPERTIES_INFO
672 #define ED_TEXTBUTTON_ID_PROPERTIES_LAST  ED_TEXTBUTTON_ID_PROPERTIES_CHANGE
673
674 #define ED_TEXTBUTTON_ID_CHANGE_FIRST   ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE
675 #define ED_TEXTBUTTON_ID_CHANGE_LAST    ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE
676
677 /* values for graphicbutton gadgets */
678 #define ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE    0
679 #define ED_GRAPHICBUTTON_ID_NEXT_CHANGE_PAGE    1
680
681 #define ED_NUM_GRAPHICBUTTONS                   2
682
683 #define ED_GRAPHICBUTTON_ID_CHANGE_FIRST   ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE
684 #define ED_GRAPHICBUTTON_ID_CHANGE_LAST    ED_GRAPHICBUTTON_ID_NEXT_CHANGE_PAGE
685
686 /* values for checkbutton gadgets */
687 #define ED_CHECKBUTTON_ID_DOUBLE_SPEED          0
688 #define ED_CHECKBUTTON_ID_GRAVITY               1
689 #define ED_CHECKBUTTON_ID_RANDOM_RESTRICTED     2
690 #define ED_CHECKBUTTON_ID_STICK_ELEMENT         3
691 #define ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS      4
692 #define ED_CHECKBUTTON_ID_BLOCK_LAST_FIELD      5
693 #define ED_CHECKBUTTON_ID_SP_BLOCK_LAST_FIELD   6
694 #define ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC    7
695 #define ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE   8
696 #define ED_CHECKBUTTON_ID_CUSTOM_ACCESSIBLE     9
697 #define ED_CHECKBUTTON_ID_CUSTOM_WALK_TO_OBJECT 10
698 #define ED_CHECKBUTTON_ID_CUSTOM_CAN_MOVE       11
699 #define ED_CHECKBUTTON_ID_CUSTOM_CAN_FALL       12
700 #define ED_CHECKBUTTON_ID_CUSTOM_CAN_SMASH      13
701 #define ED_CHECKBUTTON_ID_CUSTOM_SLIPPERY       14
702 #define ED_CHECKBUTTON_ID_CUSTOM_DEADLY         15
703 #define ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_RESULT 16
704 #define ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_FIRE   17
705 #define ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_SMASH  18
706 #define ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_IMPACT 19
707 #define ED_CHECKBUTTON_ID_CUSTOM_CAN_CHANGE     20
708 #define ED_CHECKBUTTON_ID_CHANGE_DELAY          21
709 #define ED_CHECKBUTTON_ID_CHANGE_BY_DIRECT_ACT  22
710 #define ED_CHECKBUTTON_ID_CHANGE_BY_OTHER_ACT   23
711 #define ED_CHECKBUTTON_ID_CHANGE_USE_EXPLOSION  24
712 #define ED_CHECKBUTTON_ID_CHANGE_USE_CONTENT    25
713 #define ED_CHECKBUTTON_ID_CHANGE_ONLY_COMPLETE  26
714 #define ED_CHECKBUTTON_ID_CHANGE_USE_RANDOM     27
715
716 #define ED_NUM_CHECKBUTTONS                     28
717
718 #define ED_CHECKBUTTON_ID_LEVEL_FIRST   ED_CHECKBUTTON_ID_DOUBLE_SPEED
719 #define ED_CHECKBUTTON_ID_LEVEL_LAST    ED_CHECKBUTTON_ID_RANDOM_RESTRICTED
720
721 #define ED_CHECKBUTTON_ID_CUSTOM1_FIRST ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC
722 #define ED_CHECKBUTTON_ID_CUSTOM1_LAST  ED_CHECKBUTTON_ID_CUSTOM_SLIPPERY
723 #define ED_CHECKBUTTON_ID_CUSTOM2_FIRST ED_CHECKBUTTON_ID_CUSTOM_DEADLY
724 #define ED_CHECKBUTTON_ID_CUSTOM2_LAST  ED_CHECKBUTTON_ID_CUSTOM_EXPLODE_IMPACT
725 #define ED_CHECKBUTTON_ID_CUSTOM_FIRST  ED_CHECKBUTTON_ID_CUSTOM1_FIRST
726 #define ED_CHECKBUTTON_ID_CUSTOM_LAST   ED_CHECKBUTTON_ID_CUSTOM2_LAST
727
728 #define ED_CHECKBUTTON_ID_CHANGE_FIRST  ED_CHECKBUTTON_ID_CUSTOM_CAN_CHANGE
729 #define ED_CHECKBUTTON_ID_CHANGE_LAST   ED_CHECKBUTTON_ID_CHANGE_USE_RANDOM
730
731 /* values for radiobutton gadgets */
732 #define ED_RADIOBUTTON_ID_PERCENTAGE    0
733 #define ED_RADIOBUTTON_ID_QUANTITY      1
734
735 #define ED_NUM_RADIOBUTTONS             2
736
737 #define ED_RADIOBUTTON_ID_LEVEL_FIRST   ED_RADIOBUTTON_ID_PERCENTAGE
738 #define ED_RADIOBUTTON_ID_LEVEL_LAST    ED_RADIOBUTTON_ID_QUANTITY
739
740 /* values for drawing area gadgets */
741 #define ED_DRAWING_ID_DRAWING_LEVEL             0
742 #define ED_DRAWING_ID_ELEMENT_CONTENT_0         1
743 #define ED_DRAWING_ID_ELEMENT_CONTENT_1         2
744 #define ED_DRAWING_ID_ELEMENT_CONTENT_2         3
745 #define ED_DRAWING_ID_ELEMENT_CONTENT_3         4
746 #define ED_DRAWING_ID_ELEMENT_CONTENT_4         5
747 #define ED_DRAWING_ID_ELEMENT_CONTENT_5         6
748 #define ED_DRAWING_ID_ELEMENT_CONTENT_6         7
749 #define ED_DRAWING_ID_ELEMENT_CONTENT_7         8
750 #define ED_DRAWING_ID_AMOEBA_CONTENT            9
751 #define ED_DRAWING_ID_CUSTOM_GRAPHIC            10
752 #define ED_DRAWING_ID_CUSTOM_CONTENT            11
753 #define ED_DRAWING_ID_CUSTOM_MOVE_ENTER         12
754 #define ED_DRAWING_ID_CUSTOM_MOVE_LEAVE         13
755 #define ED_DRAWING_ID_CUSTOM_CHANGE_TARGET      14
756 #define ED_DRAWING_ID_CUSTOM_CHANGE_CONTENT     15
757 #define ED_DRAWING_ID_CUSTOM_CHANGE_TRIGGER     16
758 #define ED_DRAWING_ID_GROUP_CONTENT             17
759 #define ED_DRAWING_ID_RANDOM_BACKGROUND         18
760
761 #define ED_NUM_DRAWING_AREAS                    19
762
763
764 /*
765   -----------------------------------------------------------------------------
766   some internally used definitions
767   -----------------------------------------------------------------------------
768 */
769
770 /* values for CopyLevelToUndoBuffer() */
771 #define UNDO_IMMEDIATE                  0
772 #define UNDO_ACCUMULATE                 1
773
774 /* values for scrollbars */
775 #define ED_SCROLL_NO                    0
776 #define ED_SCROLL_LEFT                  1
777 #define ED_SCROLL_RIGHT                 2
778 #define ED_SCROLL_UP                    4
779 #define ED_SCROLL_DOWN                  8
780
781 /* screens in the level editor */
782 #define ED_MODE_DRAWING                 0
783 #define ED_MODE_INFO                    1
784 #define ED_MODE_PROPERTIES              2
785
786 /* sub-screens in the element properties section */
787 #define ED_MODE_PROPERTIES_INFO         ED_TEXTBUTTON_ID_PROPERTIES_INFO
788 #define ED_MODE_PROPERTIES_CONFIG       ED_TEXTBUTTON_ID_PROPERTIES_CONFIG
789 #define ED_MODE_PROPERTIES_CONFIG_1     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_1
790 #define ED_MODE_PROPERTIES_CONFIG_2     ED_TEXTBUTTON_ID_PROPERTIES_CONFIG_2
791 #define ED_MODE_PROPERTIES_CHANGE       ED_TEXTBUTTON_ID_PROPERTIES_CHANGE
792
793 /* how many steps can be cancelled */
794 #define NUM_UNDO_STEPS                  (10 + 1)
795
796 /* values for elements with score for certain actions */
797 #define MIN_SCORE                       0
798 #define MAX_SCORE                       255
799
800 /* values for elements with count for collecting */
801 #define MIN_COLLECT_COUNT               0
802 #define MAX_COLLECT_COUNT               100
803
804 /* values for random placement */
805 #define RANDOM_USE_PERCENTAGE           0
806 #define RANDOM_USE_QUANTITY             1
807
808 /* maximal size of level editor drawing area */
809 #define MAX_ED_FIELDX           (2 * SCR_FIELDX)
810 #define MAX_ED_FIELDY           (2 * SCR_FIELDY - 1)
811
812
813 /*
814   -----------------------------------------------------------------------------
815   some internally used data structure definitions
816   -----------------------------------------------------------------------------
817 */
818
819 static struct
820 {
821   char shortcut;
822   char *text;
823 } control_info[ED_NUM_CTRL_BUTTONS] =
824 {
825   { 's',        "draw single items"                     },
826   { 'd',        "draw connected items"                  },
827   { 'l',        "draw lines"                            },
828   { 'a',        "draw arcs"                             },
829   { 'r',        "draw outline rectangles"               },
830   { 'R',        "draw filled rectangles"                },
831   { '\0',       "wrap (rotate) level up"                },
832   { 't',        "enter text elements"                   },
833   { 'f',        "flood fill"                            },
834   { '\0',       "wrap (rotate) level left"              },
835   { '?',        "properties of drawing element"         },
836   { '\0',       "wrap (rotate) level right"             },
837   { '\0',       "random element placement"              },
838   { 'b',        "grab brush"                            },
839   { '\0',       "wrap (rotate) level down"              },
840   { ',',        "pick drawing element"                  },
841
842   { 'U',        "undo last operation"                   },
843   { 'I',        "level properties"                      },
844   { 'S',        "save level"                            },
845   { 'C',        "clear level"                           },
846   { 'T',        "test level"                            },
847   { 'E',        "exit level editor"                     },
848
849   { '\0',       "copy settings from other element"      },
850   { '\0',       "copy settings to other element"        },
851   { '\0',       "exchange settings with other element"  },
852 };
853
854 static int random_placement_value = 10;
855 static int random_placement_method = RANDOM_USE_QUANTITY;
856 static int random_placement_background_element = EL_SAND;
857 static boolean random_placement_background_restricted = FALSE;
858 static boolean stick_element_properties_window = FALSE;
859 static boolean custom_element_properties[NUM_ELEMENT_PROPERTIES];
860 static boolean custom_element_change_events[NUM_CHANGE_EVENTS];
861 static struct ElementChangeInfo custom_element_change;
862 static struct ElementGroupInfo group_element_info;
863 static struct ElementInfo custom_element;
864
865 static struct
866 {
867   int x, y;
868   int min_value, max_value;
869   int gadget_id_down, gadget_id_up;
870   int gadget_id_text;
871   int gadget_id_align;
872   int *value;
873   char *text_above, *text_left, *text_right;
874 } counterbutton_info[ED_NUM_COUNTERBUTTONS] =
875 {
876   /* ---------- level and editor settings ---------------------------------- */
877
878   {
879     DX + 5 - SX,                        DY + 3 - SY,
880     1,                                  100,
881     GADGET_ID_SELECT_LEVEL_DOWN,        GADGET_ID_SELECT_LEVEL_UP,
882     GADGET_ID_SELECT_LEVEL_TEXT,        GADGET_ID_NONE,
883     &level_nr,
884     NULL,                               NULL, NULL
885   },
886   {
887     ED_SETTINGS_XPOS(0),                ED_COUNTER_YPOS(2),
888     MIN_LEV_FIELDX,                     MAX_LEV_FIELDX,
889     GADGET_ID_LEVEL_XSIZE_DOWN,         GADGET_ID_LEVEL_XSIZE_UP,
890     GADGET_ID_LEVEL_XSIZE_TEXT,         GADGET_ID_NONE,
891     &level.fieldx,
892     "playfield size:",                  NULL, "width",
893   },
894   {
895     -1,                                 ED_COUNTER_YPOS(2),
896     MIN_LEV_FIELDY,                     MAX_LEV_FIELDY,
897     GADGET_ID_LEVEL_YSIZE_DOWN,         GADGET_ID_LEVEL_YSIZE_UP,
898     GADGET_ID_LEVEL_YSIZE_TEXT,         GADGET_ID_LEVEL_XSIZE_UP,
899     &level.fieldy,
900     NULL,                               " ", "height",
901   },
902   {
903     ED_SETTINGS_XPOS(0),                ED_COUNTER_YPOS(3),
904     0,                                  999,
905     GADGET_ID_LEVEL_GEMSLIMIT_DOWN,     GADGET_ID_LEVEL_GEMSLIMIT_UP,
906     GADGET_ID_LEVEL_GEMSLIMIT_TEXT,     GADGET_ID_NONE,
907     &level.gems_needed,
908     "number of emeralds to collect:",   NULL, NULL
909   },
910   {
911     ED_SETTINGS_XPOS(0),                ED_COUNTER_YPOS(4),
912     0,                                  999,
913     GADGET_ID_LEVEL_TIMELIMIT_DOWN,     GADGET_ID_LEVEL_TIMELIMIT_UP,
914     GADGET_ID_LEVEL_TIMELIMIT_TEXT,     GADGET_ID_NONE,
915     &level.time,
916     "time available to solve level:",   NULL, "(0 => no time limit)"
917   },
918   {
919     ED_SETTINGS_XPOS(0),                ED_COUNTER_YPOS(5),
920     0,                                  255,
921     GADGET_ID_LEVEL_TIMESCORE_DOWN,     GADGET_ID_LEVEL_TIMESCORE_UP,
922     GADGET_ID_LEVEL_TIMESCORE_TEXT,     GADGET_ID_NONE,
923     &level.score[SC_TIME_BONUS],
924     "score for each 10 seconds left:",  NULL, NULL
925   },
926   {
927     ED_SETTINGS_XPOS(0),                ED_COUNTER2_YPOS(8),
928     1,                                  100,
929     GADGET_ID_LEVEL_RANDOM_DOWN,        GADGET_ID_LEVEL_RANDOM_UP,
930     GADGET_ID_LEVEL_RANDOM_TEXT,        GADGET_ID_NONE,
931     &random_placement_value,
932     "random element placement:",        NULL, "in"
933   },
934
935   /* ---------- element settings: configure (various elements) ------------- */
936
937   {
938     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(0),
939     MIN_SCORE,                          MAX_SCORE,
940     GADGET_ID_ELEMENT_SCORE_DOWN,       GADGET_ID_ELEMENT_SCORE_UP,
941     GADGET_ID_ELEMENT_SCORE_TEXT,       GADGET_ID_NONE,
942     NULL,                               /* will be set when used */
943     NULL,                               NULL, NULL
944   },
945   {
946     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(6),
947     MIN_ELEMENT_CONTENTS,               MAX_ELEMENT_CONTENTS,
948     GADGET_ID_ELEMENT_CONTENT_DOWN,     GADGET_ID_ELEMENT_CONTENT_UP,
949     GADGET_ID_ELEMENT_CONTENT_TEXT,     GADGET_ID_NONE,
950     &level.num_yamyam_contents,
951     NULL,                               NULL, "number of content areas"
952   },
953   {
954     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(0),
955     MIN_ENVELOPE_XSIZE,                 MAX_ENVELOPE_XSIZE,
956     GADGET_ID_ENVELOPE_XSIZE_DOWN,      GADGET_ID_ENVELOPE_XSIZE_UP,
957     GADGET_ID_ENVELOPE_XSIZE_TEXT,      GADGET_ID_NONE,
958     NULL,
959     NULL,                               NULL, "width",
960   },
961   {
962     -1,                                 ED_SETTINGS_YPOS(0),
963     MIN_ENVELOPE_YSIZE,                 MAX_ENVELOPE_YSIZE,
964     GADGET_ID_ENVELOPE_YSIZE_DOWN,      GADGET_ID_ENVELOPE_YSIZE_UP,
965     GADGET_ID_ENVELOPE_YSIZE_TEXT,      GADGET_ID_ENVELOPE_XSIZE_UP,
966     NULL,
967     NULL,                               " ", "height",
968   },
969
970   /* ---------- element settings: configure 1 (custom elements) ------------ */
971
972   {
973     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(6),
974     MIN_SCORE,                          MAX_SCORE,
975     GADGET_ID_CUSTOM_SCORE_DOWN,        GADGET_ID_CUSTOM_SCORE_UP,
976     GADGET_ID_CUSTOM_SCORE_TEXT,        GADGET_ID_NONE,
977     &custom_element.collect_score,
978     NULL,                               "score", " "
979   },
980   {
981     -1,                                 ED_SETTINGS_YPOS(6),
982     MIN_COLLECT_COUNT,                  MAX_COLLECT_COUNT,
983     GADGET_ID_CUSTOM_GEMCOUNT_DOWN,     GADGET_ID_CUSTOM_GEMCOUNT_UP,
984     GADGET_ID_CUSTOM_GEMCOUNT_TEXT,     GADGET_ID_CUSTOM_SCORE_UP,
985     &custom_element.collect_count,
986     NULL,                               "count", NULL
987   },
988   {
989     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(7),
990     0,                                  999,
991     GADGET_ID_PUSH_DELAY_FIX_DOWN,      GADGET_ID_PUSH_DELAY_FIX_UP,
992     GADGET_ID_PUSH_DELAY_FIX_TEXT,      GADGET_ID_NONE,
993     &custom_element.push_delay_fixed,
994     NULL,                               "push delay", NULL
995   },
996   {
997     -1,                                 ED_SETTINGS_YPOS(7),
998     0,                                  999,
999     GADGET_ID_PUSH_DELAY_RND_DOWN,      GADGET_ID_PUSH_DELAY_RND_UP,
1000     GADGET_ID_PUSH_DELAY_RND_TEXT,      GADGET_ID_PUSH_DELAY_FIX_UP,
1001     &custom_element.push_delay_random,
1002     NULL,                               "+random", NULL
1003   },
1004   {
1005     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(12),
1006     0,                                  999,
1007     GADGET_ID_MOVE_DELAY_FIX_DOWN,      GADGET_ID_MOVE_DELAY_FIX_UP,
1008     GADGET_ID_MOVE_DELAY_FIX_TEXT,      GADGET_ID_NONE,
1009     &custom_element.move_delay_fixed,
1010     NULL,                               "move delay", NULL
1011   },
1012   {
1013     -1,                                 ED_SETTINGS_YPOS(12),
1014     0,                                  999,
1015     GADGET_ID_MOVE_DELAY_RND_DOWN,      GADGET_ID_MOVE_DELAY_RND_UP,
1016     GADGET_ID_MOVE_DELAY_RND_TEXT,      GADGET_ID_MOVE_DELAY_FIX_UP,
1017     &custom_element.move_delay_random,
1018     NULL,                               "+random", NULL
1019   },
1020
1021   /* ---------- element settings: configure 2 (custom elements) ------------ */
1022
1023   {
1024     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(4),
1025     0,                                  255,
1026     GADGET_ID_EXPLOSION_DELAY_DOWN,     GADGET_ID_EXPLOSION_DELAY_UP,
1027     GADGET_ID_EXPLOSION_DELAY_TEXT,     GADGET_ID_NONE,
1028     &custom_element.explosion_delay,
1029     NULL,                               "explosion delay", NULL
1030   },
1031   {
1032     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(5),
1033     0,                                  255,
1034     GADGET_ID_IGNITION_DELAY_DOWN,      GADGET_ID_IGNITION_DELAY_UP,
1035     GADGET_ID_IGNITION_DELAY_TEXT,      GADGET_ID_NONE,
1036     &custom_element.ignition_delay,
1037     NULL,                               "ignition delay", "(by fire)"
1038   },
1039
1040   /* ---------- element settings: configure (group elements) --------------- */
1041
1042   {
1043     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(5),
1044     MIN_ELEMENTS_IN_GROUP,              MAX_ELEMENTS_IN_GROUP,
1045     GADGET_ID_GROUP_CONTENT_DOWN,       GADGET_ID_GROUP_CONTENT_UP,
1046     GADGET_ID_GROUP_CONTENT_TEXT,       GADGET_ID_NONE,
1047     &group_element_info.num_elements,
1048     NULL,                               NULL, "number of elements in group"
1049   },
1050
1051   /* ---------- element settings: advanced (custom elements) --------------- */
1052
1053   {
1054     ED_SETTINGS_XPOS(2),                ED_SETTINGS_YPOS(2),
1055     0,                                  999,
1056     GADGET_ID_CHANGE_DELAY_FIX_DOWN,    GADGET_ID_CHANGE_DELAY_FIX_UP,
1057     GADGET_ID_CHANGE_DELAY_FIX_TEXT,    GADGET_ID_NONE,
1058     &custom_element_change.delay_fixed,
1059     NULL,                               "delay", NULL,
1060   },
1061   {
1062     -1,                                 ED_SETTINGS_YPOS(2),
1063     0,                                  999,
1064     GADGET_ID_CHANGE_DELAY_RND_DOWN,    GADGET_ID_CHANGE_DELAY_RND_UP,
1065     GADGET_ID_CHANGE_DELAY_RND_TEXT,    GADGET_ID_CHANGE_DELAY_FIX_UP,
1066     &custom_element_change.delay_random,
1067     NULL,                               "+random", NULL
1068   },
1069   {
1070     ED_SETTINGS_XPOS(3),                ED_SETTINGS_YPOS(11),
1071     0,                                  100,
1072     GADGET_ID_CHANGE_CONT_RND_DOWN,     GADGET_ID_CHANGE_CONT_RND_UP,
1073     GADGET_ID_CHANGE_CONT_RND_TEXT,     GADGET_ID_NONE,
1074     &custom_element_change.random,
1075     NULL,                               "use random replace:", "%"
1076   },
1077 };
1078
1079 static struct
1080 {
1081   int x, y;
1082   int gadget_id;
1083   int size;
1084   char *value;
1085   char *text_above, *infotext;
1086 } textinput_info[ED_NUM_TEXTINPUT] =
1087 {
1088   {
1089     ED_SETTINGS_XPOS(0),                ED_COUNTER_YPOS(0),
1090     GADGET_ID_LEVEL_NAME,
1091     MAX_LEVEL_NAME_LEN,
1092     level.name,
1093     "Title:", "Title"
1094   },
1095   {
1096     ED_SETTINGS_XPOS(0),                ED_COUNTER_YPOS(1),
1097     GADGET_ID_LEVEL_AUTHOR,
1098     MAX_LEVEL_AUTHOR_LEN,
1099     level.author,
1100     "Author:", "Author"
1101   },
1102   {
1103     5 * MINI_TILEX - 2,                 5 * MINI_TILEY - ED_BORDER_SIZE + 1,
1104     GADGET_ID_ELEMENT_NAME,
1105     MAX_ELEMENT_NAME_LEN - 2,           /* currently 2 chars less editable */
1106     custom_element.description,
1107     NULL, "Element name"
1108   }
1109 };
1110
1111 static struct
1112 {
1113   int x, y;
1114   int gadget_id;
1115   int xsize, ysize;
1116   char *value;
1117   char *text_above, *infotext;
1118 } textarea_info[ED_NUM_TEXTAREAS] =
1119 {
1120   {
1121     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(2),
1122     GADGET_ID_ENVELOPE_INFO,
1123     MAX_ENVELOPE_XSIZE, MAX_ENVELOPE_YSIZE,
1124     NULL,
1125     "Envelope Info:", "Envelope Info"
1126   }
1127 };
1128
1129 static struct ValueTextInfo options_access_type[] =
1130 {
1131   { EP_WALKABLE,                "walkable"                      },
1132   { EP_PASSABLE,                "passable"                      },
1133   { -1,                         NULL                            }
1134 };
1135
1136 static struct ValueTextInfo options_access_layer[] =
1137 {
1138   { EP_ACCESSIBLE_OVER,         "over"                          },
1139   { EP_ACCESSIBLE_INSIDE,       "inside"                        },
1140   { EP_ACCESSIBLE_UNDER,        "under"                         },
1141   { -1,                         NULL                            }
1142 };
1143
1144 static struct ValueTextInfo options_access_protected[] =
1145 {
1146   { 0,                          "unprotected"                   },
1147   { 1,                          "protected"                     },
1148   { -1,                         NULL                            }
1149 };
1150
1151 static struct ValueTextInfo options_access_direction[] =
1152 {
1153   { MV_LEFT,                    "left"                          },
1154   { MV_RIGHT,                   "right"                         },
1155   { MV_UP,                      "up"                            },
1156   { MV_DOWN,                    "down"                          },
1157   { MV_LEFT  | MV_UP,           "left + up"                     },
1158   { MV_LEFT  | MV_DOWN,         "left + down"                   },
1159   { MV_RIGHT | MV_UP,           "right + up"                    },
1160   { MV_RIGHT | MV_DOWN,         "right + down"                  },
1161   { MV_HORIZONTAL,              "horizontal"                    },
1162   { MV_VERTICAL,                "vertical"                      },
1163   { MV_HORIZONTAL | MV_UP,      "horizontal + up"               },
1164   { MV_HORIZONTAL | MV_DOWN,    "horizontal + down"             },
1165   { MV_VERTICAL   | MV_LEFT,    "vertical + left"               },
1166   { MV_VERTICAL   | MV_RIGHT,   "vertical + right"              },
1167   { MV_ALL_DIRECTIONS,          "all directions"                },
1168   { -1,                         NULL                            }
1169 };
1170
1171 static struct ValueTextInfo options_walk_to_action[] =
1172 {
1173   { EP_DIGGABLE,                "diggable"                      },
1174   { EP_COLLECTIBLE_ONLY,        "collectible"                   },
1175   { EP_DROPPABLE,               "collectible & droppable"       },
1176   { EP_PUSHABLE,                "pushable"                      },
1177   { -1,                         NULL                            }
1178 };
1179
1180 static struct ValueTextInfo options_move_pattern[] =
1181 {
1182   { MV_LEFT,                    "left"                          },
1183   { MV_RIGHT,                   "right"                         },
1184   { MV_UP,                      "up"                            },
1185   { MV_DOWN,                    "down"                          },
1186   { MV_HORIZONTAL,              "horizontal"                    },
1187   { MV_VERTICAL,                "vertical"                      },
1188   { MV_ALL_DIRECTIONS,          "all directions"                },
1189   { MV_TOWARDS_PLAYER,          "towards player"                },
1190   { MV_AWAY_FROM_PLAYER,        "away from player"              },
1191   { MV_ALONG_LEFT_SIDE,         "along left side"               },
1192   { MV_ALONG_RIGHT_SIDE,        "along right side"              },
1193   { MV_TURNING_LEFT,            "turning left"                  },
1194   { MV_TURNING_RIGHT,           "turning right"                 },
1195   { MV_TURNING_LEFT_RIGHT,      "turning left, right"           },
1196   { MV_TURNING_RIGHT_LEFT,      "turning right, left"           },
1197   { MV_TURNING_RANDOM,          "turning random"                },
1198   { MV_WHEN_PUSHED,             "when pushed"                   },
1199   { MV_WHEN_DROPPED,            "when dropped"                  },
1200   { MV_MAZE_RUNNER,             "maze runner style"             },
1201   { MV_MAZE_HUNTER,             "maze hunter style"             },
1202   { -1,                         NULL                            }
1203 };
1204
1205 static struct ValueTextInfo options_move_direction[] =
1206 {
1207   { MV_START_AUTOMATIC,         "automatic"                     },
1208   { MV_START_LEFT,              "left"                          },
1209   { MV_START_RIGHT,             "right"                         },
1210   { MV_START_UP,                "up"                            },
1211   { MV_START_DOWN,              "down"                          },
1212   { MV_START_RANDOM,            "random"                        },
1213   { MV_START_PREVIOUS,          "previous"                      },
1214   { -1,                         NULL                            }
1215 };
1216
1217 static struct ValueTextInfo options_move_stepsize[] =
1218 {
1219   { 1,                          "very slow"                     },
1220   { 2,                          "slow"                          },
1221   { 4,                          "normal"                        },
1222   { 8,                          "fast"                          },
1223   { 16,                         "very fast"                     },
1224   { -1,                         NULL                            }
1225 };
1226
1227 static struct ValueTextInfo options_move_leave_type[] =
1228 {
1229   { LEAVE_TYPE_UNLIMITED,       "leave behind"                  },
1230   { LEAVE_TYPE_LIMITED,         "change it to"                  },
1231   { -1,                         NULL                            }
1232 };
1233
1234 static struct ValueTextInfo options_smash_targets[] =
1235 {
1236   { EP_CAN_SMASH_PLAYER,        "player"                        },
1237 #if 0
1238   { EP_CAN_SMASH_ENEMIES,       "enemies"                       },
1239 #endif
1240   { EP_CAN_SMASH_EVERYTHING,    "everything"                    },
1241   { -1,                         NULL                            }
1242 };
1243
1244 static struct ValueTextInfo options_slippery_type[] =
1245 {
1246   { SLIPPERY_ANY_RANDOM,        "random"                        },
1247   { SLIPPERY_ANY_LEFT_RIGHT,    "left, right"                   },
1248   { SLIPPERY_ANY_RIGHT_LEFT,    "right, left"                   },
1249   { SLIPPERY_ONLY_LEFT,         "only left"                     },
1250   { SLIPPERY_ONLY_RIGHT,        "only right"                    },
1251   { -1,                         NULL                            }
1252 };
1253
1254 static struct ValueTextInfo options_deadliness[] =
1255 {
1256   { EP_DONT_RUN_INTO,           "running into"                  },
1257   { EP_DONT_COLLIDE_WITH,       "colliding with"                },
1258   { EP_DONT_TOUCH,              "touching"                      },
1259   { -1,                         NULL                            }
1260 };
1261
1262 static struct ValueTextInfo options_consistency[] =
1263 {
1264   { EP_CAN_EXPLODE_3X3,         "can explode 3x3"               },
1265   { EP_CAN_EXPLODE_DYNA,        "can explode 3+3"               },
1266   { EP_CAN_EXPLODE_1X1,         "can explode 1x1"               },
1267   { EP_INDESTRUCTIBLE,          "indestructible"                },
1268   { -1,                         NULL                            }
1269 };
1270
1271 static struct ValueTextInfo options_time_units[] =
1272 {
1273   { 1,                          "frames"                        },
1274   { FRAMES_PER_SECOND,          "seconds"                       },
1275   { -1,                         NULL                            }
1276 };
1277
1278 static struct ValueTextInfo options_change_direct_action[] =
1279 {
1280   { CE_TOUCHED_BY_PLAYER,       "touched by player ..."         },
1281   { CE_PRESSED_BY_PLAYER,       "pressed by player ..."         },
1282   { CE_PUSHED_BY_PLAYER,        "pushed by player ..."          },
1283   { CE_ENTERED_BY_PLAYER,       "entered by player ..."         },
1284   { CE_LEFT_BY_PLAYER,          "left by player ..."            },
1285   { CE_DROPPED_BY_PLAYER,       "dropped by player"             },
1286   { CE_SWITCHED,                "switched ..."                  },
1287 #if 1
1288   { CE_HITTING_SOMETHING,       "hitting something ..."         },
1289   { CE_HIT_BY_SOMETHING,        "hit by something ..."          },
1290 #else
1291   { CE_HITTING_SOMETHING,       "collision ..."                 },
1292 #endif
1293   { CE_IMPACT,                  "impact (on something)"         },
1294   { CE_SMASHED,                 "smashed (from above)"          },
1295   { -1,                         NULL                            }
1296 };
1297
1298 static struct ValueTextInfo options_change_other_action[] =
1299 {
1300   { CE_OTHER_GETS_TOUCHED,      "player touches ..."            },
1301   { CE_OTHER_GETS_PRESSED,      "player presses ..."            },
1302   { CE_OTHER_GETS_PUSHED,       "player pushes ..."             },
1303   { CE_OTHER_GETS_ENTERED,      "player enters ..."             },
1304   { CE_OTHER_GETS_LEFT,         "player leaves ..."             },
1305   { CE_OTHER_GETS_DIGGED,       "player digs"                   },
1306   { CE_OTHER_GETS_COLLECTED,    "player collects"               },
1307   { CE_OTHER_GETS_DROPPED,      "player drops"                  },
1308   { CE_OTHER_IS_TOUCHING,       "touching ..."                  },
1309 #if 1
1310   { CE_OTHER_IS_HITTING,        "hitting ..."                   },
1311   { CE_OTHER_GETS_HIT,          "hit by ..."                    },
1312 #endif
1313   { CE_OTHER_IS_SWITCHING,      "switch of ..."                 },
1314   { CE_OTHER_IS_CHANGING,       "change of"                     },
1315   { CE_OTHER_IS_EXPLODING,      "explosion of"                  },
1316   { -1,                         NULL                            }
1317 };
1318
1319 static struct ValueTextInfo options_change_sides[] =
1320 {
1321   { CH_SIDE_LEFT,               "left side"                     },
1322   { CH_SIDE_RIGHT,              "right side"                    },
1323   { CH_SIDE_TOP,                "top side"                      },
1324   { CH_SIDE_BOTTOM,             "bottom side"                   },
1325   { CH_SIDE_LEFT_RIGHT,         "left/right side"               },
1326   { CH_SIDE_TOP_BOTTOM,         "top/bottom side"               },
1327   { CH_SIDE_ANY,                "any side"                      },
1328   { -1,                         NULL                            }
1329 };
1330
1331 static struct ValueTextInfo options_change_power[] =
1332 {
1333   { CP_NON_DESTRUCTIVE,         "empty"                         },
1334   { CP_HALF_DESTRUCTIVE,        "diggable"                      },
1335   { CP_FULL_DESTRUCTIVE,        "destructible"                  },
1336   { -1,                         NULL                            }
1337 };
1338
1339 static char options_change_page_strings[MAX_CHANGE_PAGES][10];
1340 static struct ValueTextInfo options_change_page[MAX_CHANGE_PAGES + 1] =
1341 {
1342   { -1,                         NULL                            }
1343 };
1344
1345 static struct ValueTextInfo options_group_choice_mode[] =
1346 {
1347   { ANIM_RANDOM,                "random"                        },
1348   { ANIM_LOOP,                  "loop"                          },
1349   { ANIM_LINEAR,                "linear"                        },
1350   { ANIM_PINGPONG,              "pingpong"                      },
1351   { ANIM_PINGPONG2,             "pingpong 2"                    },
1352   { -1,                         NULL                            }
1353 };
1354
1355 static struct
1356 {
1357   int x, y;
1358   int gadget_id;
1359   int gadget_id_align;
1360   int size;     /* char size of selectbox or '-1' (dynamically determined) */
1361   struct ValueTextInfo *options;
1362   int *value;
1363   char *text_left, *text_right, *infotext;
1364 } selectbox_info[ED_NUM_SELECTBOX] =
1365 {
1366   /* ---------- element settings: configure 1 (custom elements) ----------- */
1367
1368   {
1369     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(3),
1370     GADGET_ID_CUSTOM_ACCESS_TYPE,       GADGET_ID_NONE,
1371     -1,
1372     options_access_type,
1373     &custom_element.access_type,
1374     NULL, NULL,                         "type of access to this field"
1375   },
1376   {
1377     -1,                                 ED_SETTINGS_YPOS(3),
1378     GADGET_ID_CUSTOM_ACCESS_LAYER,      GADGET_ID_CUSTOM_ACCESS_TYPE,
1379     -1,
1380     options_access_layer,
1381     &custom_element.access_layer,
1382     NULL, NULL,                         "layer of access for this field"
1383   },
1384   {
1385     -1,                                 ED_SETTINGS_YPOS(3),
1386     GADGET_ID_CUSTOM_ACCESS_PROTECTED,  GADGET_ID_CUSTOM_ACCESS_LAYER,
1387     -1,
1388     options_access_protected,
1389     &custom_element.access_protected,
1390     NULL, NULL,                         "protected access for this field"
1391   },
1392   {
1393     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(4),
1394     GADGET_ID_CUSTOM_ACCESS_DIRECTION,  GADGET_ID_NONE,
1395     -1,
1396     options_access_direction,
1397     &custom_element.access_direction,
1398     "from", NULL,                       "access direction for this field"
1399   },
1400   {
1401     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(5),
1402     GADGET_ID_CUSTOM_WALK_TO_ACTION,    GADGET_ID_NONE,
1403     -1,
1404     options_walk_to_action,
1405     &custom_element.walk_to_action,
1406     NULL, NULL,                         "diggable/collectible/pushable"
1407   },
1408   {
1409     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(8),
1410     GADGET_ID_CUSTOM_MOVE_PATTERN,      GADGET_ID_NONE,
1411     -1,
1412     options_move_pattern,
1413     &custom_element.move_pattern,
1414     "can move", NULL,                   "element move direction"
1415   },
1416   {
1417     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(9),
1418     GADGET_ID_CUSTOM_MOVE_DIRECTION,    GADGET_ID_NONE,
1419     -1,
1420     options_move_direction,
1421     &custom_element.move_direction_initial,
1422     "starts moving", NULL,              "initial element move direction"
1423   },
1424   {
1425     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(11),
1426     GADGET_ID_CUSTOM_MOVE_STEPSIZE,     GADGET_ID_NONE,
1427     -1,
1428     options_move_stepsize,
1429     &custom_element.move_stepsize,
1430     "move/fall speed", NULL,            "speed of element movement"
1431   },
1432   {
1433     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(10),
1434     GADGET_ID_CUSTOM_MOVE_LEAVE_TYPE,   GADGET_ID_NONE,
1435     -1,
1436     options_move_leave_type,
1437     &custom_element.move_leave_type,
1438     "can dig:    can", ":",             "leave behind or change element"
1439   },
1440   {
1441     -1,                                 ED_SETTINGS_YPOS(13),
1442     GADGET_ID_CUSTOM_SMASH_TARGETS,     GADGET_ID_CUSTOM_CAN_SMASH,
1443     -1,
1444     options_smash_targets,
1445     &custom_element.smash_targets,
1446     "can smash", NULL,                  "elements that can be smashed"
1447   },
1448   {
1449     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(14),
1450     GADGET_ID_CUSTOM_SLIPPERY_TYPE,     GADGET_ID_NONE,
1451     -1,
1452     options_slippery_type,
1453     &custom_element.slippery_type,
1454     "slippery", NULL,                   "where other elements fall down"
1455   },
1456
1457   /* ---------- element settings: configure 2 (custom elements) ----------- */
1458
1459   {
1460     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(1),
1461     GADGET_ID_CUSTOM_DEADLINESS,        GADGET_ID_NONE,
1462     -1,
1463     options_deadliness,
1464     &custom_element.deadliness,
1465     "deadly when", NULL,                "deadliness of element"
1466   },
1467   {
1468     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(2),
1469     GADGET_ID_CUSTOM_CONSISTENCY,       GADGET_ID_NONE,
1470     -1,
1471     options_consistency,
1472     &custom_element.consistency,
1473     NULL, NULL,                         "consistency/destructibility"
1474   },
1475
1476   /* ---------- element settings: advanced (custom elements) --------------- */
1477
1478   {
1479     ED_SETTINGS_XPOS(2),                ED_SETTINGS_YPOS(3),
1480     GADGET_ID_CHANGE_TIME_UNITS,        GADGET_ID_NONE,
1481     -1,
1482     options_time_units,
1483     &custom_element_change.delay_frames,
1484     "delay time given in", NULL,        "delay time units for change"
1485   },
1486   {
1487     ED_SETTINGS_XPOS(2),                ED_SETTINGS_YPOS(4),
1488     GADGET_ID_CHANGE_DIRECT_ACTION,     GADGET_ID_NONE,
1489     -1,
1490     options_change_direct_action,
1491     &custom_element_change.direct_action,
1492     NULL, NULL,                         "type of direct action"
1493   },
1494   {
1495     ED_SETTINGS_XPOS(2),                ED_SETTINGS_YPOS(5),
1496     GADGET_ID_CHANGE_OTHER_ACTION,      GADGET_ID_NONE,
1497     -1,
1498     options_change_other_action,
1499     &custom_element_change.other_action,
1500     NULL, "element:",                   "type of other element action"
1501   },
1502   {
1503     ED_SETTINGS_XPOS(2),                ED_SETTINGS_YPOS(6),
1504     GADGET_ID_CHANGE_SIDES,             GADGET_ID_NONE,
1505     -1,
1506     options_change_sides,
1507     &custom_element_change.sides,
1508     "... at", NULL,                     "element side that causes change"
1509   },
1510   {
1511     ED_SETTINGS_XPOS(2),                ED_SETTINGS_YPOS(9),
1512     GADGET_ID_CHANGE_POWER,             GADGET_ID_NONE,
1513     -1,
1514     options_change_power,
1515     &custom_element_change.power,
1516     "replace when", NULL,               "which elements can be replaced"
1517   },
1518   {
1519     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(13),
1520     GADGET_ID_SELECT_CHANGE_PAGE,       GADGET_ID_NONE,
1521     3,
1522     options_change_page,
1523     &custom_element.current_change_page,
1524     NULL, NULL,                         "element change page"
1525   },
1526
1527   /* ---------- element settings: configure (group elements) --------------- */
1528
1529   {
1530     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(6),
1531     GADGET_ID_GROUP_CHOICE_MODE,        GADGET_ID_NONE,
1532     -1,
1533     options_group_choice_mode,
1534     &group_element_info.choice_mode,
1535     "choice type:", NULL,               "type of group element choice"
1536   },
1537 };
1538
1539 static struct
1540 {
1541   int x, y;
1542   int gadget_id;
1543   int gadget_id_align;
1544   int size;
1545   char *text;
1546   char *text_left, *text_right, *infotext;
1547 } textbutton_info[ED_NUM_TEXTBUTTONS] =
1548 {
1549 #if 1
1550   {
1551     ED_SETTINGS_XPOS(0),                ED_COUNTER_YPOS(1),
1552     GADGET_ID_PROPERTIES_INFO,          GADGET_ID_NONE,
1553     8,                                  "Info",                 
1554     NULL, NULL,                         "Show information about element"
1555   },
1556   {
1557     ED_SETTINGS_XPOS(0) + 124,          ED_COUNTER_YPOS(1),
1558     GADGET_ID_PROPERTIES_CONFIG,        GADGET_ID_NONE,
1559     8,                                  "Config",
1560     NULL, NULL,                         "Configure element properties"
1561   },
1562   {
1563     ED_SETTINGS_XPOS(0) + 124,          ED_COUNTER_YPOS(1),
1564     GADGET_ID_PROPERTIES_CONFIG_1,      GADGET_ID_NONE,
1565     8,                                  "Config 1",
1566     NULL, NULL,                         "Configure custom element properties"
1567   },
1568   {
1569     ED_SETTINGS_XPOS(0) + 248,          ED_COUNTER_YPOS(1),
1570     GADGET_ID_PROPERTIES_CONFIG_2,      GADGET_ID_NONE,
1571     8,                                  "Config 2",
1572     NULL, NULL,                         "Configure custom element properties"
1573   },
1574   {
1575     ED_SETTINGS_XPOS(0) + 372,          ED_COUNTER_YPOS(1),
1576     GADGET_ID_PROPERTIES_CHANGE,        GADGET_ID_NONE,
1577     8,                                  "Change",
1578     NULL, NULL,                         "Custom element change configuration"
1579   },
1580 #else
1581   {
1582     ED_SETTINGS_XPOS(0),                ED_COUNTER_YPOS(1),
1583     GADGET_ID_PROPERTIES_INFO,          GADGET_ID_NONE,
1584     11,                                 "Information",                  
1585     NULL, NULL,                         "Show information about element"
1586   },
1587   {
1588     ED_SETTINGS_XPOS(0) + 166,          ED_COUNTER_YPOS(1),
1589     GADGET_ID_PROPERTIES_CONFIG,        GADGET_ID_NONE,
1590     11,                                 "Configure",
1591     NULL, NULL,                         "Configure element properties"
1592   },
1593   {
1594     ED_SETTINGS_XPOS(0) + 332,          ED_COUNTER_YPOS(1),
1595     GADGET_ID_PROPERTIES_CHANGE,        GADGET_ID_NONE,
1596     11,                                 "Advanced",
1597     NULL, NULL,                         "Advanced element configuration"
1598   },
1599 #endif
1600   {
1601     -1,                                 ED_SETTINGS_YPOS(2),
1602     GADGET_ID_SAVE_AS_TEMPLATE,         GADGET_ID_CUSTOM_USE_TEMPLATE,
1603     -1,                                 "Save",
1604     " ", "As Template",                 "Save current settings as new template"
1605   },
1606   {
1607     -1,                                 ED_SETTINGS_YPOS(13),
1608     GADGET_ID_ADD_CHANGE_PAGE,          GADGET_ID_NEXT_CHANGE_PAGE,
1609     -1,                                 "New",
1610     " ", NULL,                          "Add new change page"
1611   },
1612   {
1613     -1,                                 ED_SETTINGS_YPOS(13),
1614     GADGET_ID_DEL_CHANGE_PAGE,          GADGET_ID_ADD_CHANGE_PAGE,
1615     -1,                                 "Delete",
1616     NULL, NULL,                         "Delete current change page"
1617   },
1618 };
1619
1620 static struct
1621 {
1622   int gd_x, gd_y;
1623   int x, y;
1624   int width, height;
1625   int gadget_id;
1626   int gadget_id_align;
1627   char *text_left, *text_right, *infotext;
1628 } graphicbutton_info[ED_NUM_GRAPHICBUTTONS] =
1629 {
1630   {
1631     ED_BUTTON_MINUS_XPOS,               ED_BUTTON_COUNT_YPOS,
1632     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(13),
1633     ED_BUTTON_COUNT_XSIZE,              ED_BUTTON_COUNT_YSIZE,
1634     GADGET_ID_PREV_CHANGE_PAGE,         GADGET_ID_NONE,
1635     NULL, NULL,                         "select previous change page"
1636   },
1637   {
1638     ED_BUTTON_PLUS_XPOS,                ED_BUTTON_COUNT_YPOS,
1639     -1,                                 ED_SETTINGS_YPOS(13),
1640     ED_BUTTON_COUNT_XSIZE,              ED_BUTTON_COUNT_YSIZE,
1641     GADGET_ID_NEXT_CHANGE_PAGE,         GADGET_ID_SELECT_CHANGE_PAGE,
1642     NULL, "change page",                "select next change page"
1643   },
1644 };
1645
1646 static struct
1647 {
1648   int gd_x, gd_y;
1649   int x, y;
1650   int gadget_id;
1651   char *infotext;
1652 } scrollbutton_info[ED_NUM_SCROLLBUTTONS] =
1653 {
1654   {
1655     ED_SCROLLBUTTON_XPOS,   ED_SCROLLBUTTON_YPOS + 0 * ED_SCROLLBUTTON_YSIZE,
1656     ED_SCROLL_UP_XPOS,      ED_SCROLL_UP_YPOS,
1657     GADGET_ID_SCROLL_UP,
1658     "scroll level editing area up"
1659   },
1660   {
1661     ED_SCROLLBUTTON_XPOS,   ED_SCROLLBUTTON_YPOS + 1 * ED_SCROLLBUTTON_YSIZE,
1662     ED_SCROLL_DOWN_XPOS,    ED_SCROLL_DOWN_YPOS,
1663     GADGET_ID_SCROLL_DOWN,
1664     "scroll level editing area down"
1665   },
1666   {
1667     ED_SCROLLBUTTON_XPOS,   ED_SCROLLBUTTON_YPOS + 2 * ED_SCROLLBUTTON_YSIZE,
1668     ED_SCROLL_LEFT_XPOS,    ED_SCROLL_LEFT_YPOS,
1669     GADGET_ID_SCROLL_LEFT,
1670     "scroll level editing area left"
1671   },
1672   {
1673     ED_SCROLLBUTTON_XPOS,   ED_SCROLLBUTTON_YPOS + 3 * ED_SCROLLBUTTON_YSIZE,
1674     ED_SCROLL_RIGHT_XPOS,   ED_SCROLL_RIGHT_YPOS,
1675     GADGET_ID_SCROLL_RIGHT,
1676     "scroll level editing area right"
1677   },
1678   {
1679     ED_SCROLLBUTTON2_XPOS,  ED_SCROLLBUTTON2_YPOS + 0 * ED_SCROLLBUTTON2_YSIZE,
1680     ED_SCROLL2_UP_XPOS,     ED_SCROLL2_UP_YPOS,
1681     GADGET_ID_SCROLL_LIST_UP,
1682     "scroll element list up ('Page Up')"
1683   },
1684   {
1685     ED_SCROLLBUTTON2_XPOS,  ED_SCROLLBUTTON2_YPOS + 1 * ED_SCROLLBUTTON2_YSIZE,
1686     ED_SCROLL2_DOWN_XPOS,   ED_SCROLL2_DOWN_YPOS,
1687     GADGET_ID_SCROLL_LIST_DOWN,
1688     "scroll element list down ('Page Down')"
1689   },
1690 };
1691
1692 static struct
1693 {
1694   int gd_x, gd_y;
1695   int x, y;
1696   int width, height;
1697   int type;
1698   int gadget_id;
1699   char *infotext;
1700 } scrollbar_info[ED_NUM_SCROLLBARS] =
1701 {
1702   {
1703     ED_SCROLLBAR_XPOS,                  ED_SCROLLBAR_YPOS,
1704     SX + ED_SCROLL_HORIZONTAL_XPOS,     SY + ED_SCROLL_HORIZONTAL_YPOS,
1705     ED_SCROLL_HORIZONTAL_XSIZE,         ED_SCROLL_HORIZONTAL_YSIZE,
1706     GD_TYPE_SCROLLBAR_HORIZONTAL,
1707     GADGET_ID_SCROLL_HORIZONTAL,
1708     "scroll level editing area horizontally"
1709   },
1710   {
1711     ED_SCROLLBAR_XPOS,                  ED_SCROLLBAR_YPOS,
1712     SX + ED_SCROLL_VERTICAL_XPOS,       SY + ED_SCROLL_VERTICAL_YPOS,
1713     ED_SCROLL_VERTICAL_XSIZE,           ED_SCROLL_VERTICAL_YSIZE,
1714     GD_TYPE_SCROLLBAR_VERTICAL,
1715     GADGET_ID_SCROLL_VERTICAL,
1716     "scroll level editing area vertically"
1717   },
1718   {
1719     ED_SCROLLBAR2_XPOS,                 ED_SCROLLBAR2_YPOS,
1720     DX + ED_SCROLL2_VERTICAL_XPOS,      DY + ED_SCROLL2_VERTICAL_YPOS,
1721     ED_SCROLL2_VERTICAL_XSIZE,          ED_SCROLL2_VERTICAL_YSIZE,
1722     GD_TYPE_SCROLLBAR_VERTICAL,
1723     GADGET_ID_SCROLL_LIST_VERTICAL,
1724     "scroll element list vertically"
1725   }
1726 };
1727
1728 static struct
1729 {
1730   int x, y;
1731   int gadget_id;
1732   int gadget_id_align;
1733   int radio_button_nr;
1734   int *value;
1735   int checked_value;
1736   char *text_left, *text_right, *infotext;
1737 } radiobutton_info[ED_NUM_RADIOBUTTONS] =
1738 {
1739   {
1740     -1,                                 ED_COUNTER2_YPOS(8),
1741     GADGET_ID_RANDOM_PERCENTAGE,        GADGET_ID_LEVEL_RANDOM_UP,
1742     RADIO_NR_RANDOM_ELEMENTS,
1743     &random_placement_method,           RANDOM_USE_PERCENTAGE,
1744     " ", "percentage",                  "use percentage for random elements"
1745   },
1746   {
1747     -1,                                 ED_COUNTER2_YPOS(8),
1748     GADGET_ID_RANDOM_QUANTITY,          GADGET_ID_RANDOM_PERCENTAGE,
1749     RADIO_NR_RANDOM_ELEMENTS,
1750     &random_placement_method,           RANDOM_USE_QUANTITY,
1751     " ", "quantity",                    "use quantity for random elements"
1752   }
1753 };
1754
1755 static struct
1756 {
1757   int x, y;
1758   int gadget_id;
1759   int gadget_id_align;
1760   boolean *value;
1761   char *text_left, *text_right, *infotext;
1762 } checkbutton_info[ED_NUM_CHECKBUTTONS] =
1763 {
1764   /* ---------- level and editor settings ---------------------------------- */
1765
1766   {
1767     ED_SETTINGS_XPOS(0),                ED_COUNTER_YPOS(6) - MINI_TILEY,
1768     GADGET_ID_DOUBLE_SPEED,             GADGET_ID_NONE,
1769     &level.double_speed,
1770     NULL, "double speed movement",      "set movement speed of player"
1771   },
1772   {
1773     -1,                                 ED_COUNTER_YPOS(6) - MINI_TILEY,
1774     GADGET_ID_GRAVITY,                  GADGET_ID_DOUBLE_SPEED,
1775     &level.initial_gravity,
1776     " ", "gravity",                     "set level gravity"
1777   },
1778   {
1779     ED_SETTINGS_XPOS(0),                ED_COUNTER2_YPOS(9) - MINI_TILEY,
1780     GADGET_ID_RANDOM_RESTRICTED,        GADGET_ID_NONE,
1781     &random_placement_background_restricted,
1782     NULL,
1783     "restrict random placement to:",    "set random placement restriction"
1784   },
1785
1786   /* ---------- element settings: configure (various elements) ------------- */
1787
1788   {
1789     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(0),
1790     GADGET_ID_STICK_ELEMENT,            GADGET_ID_NONE,
1791     &stick_element_properties_window,
1792     NULL,
1793     "stick this screen to edit content","stick this screen to edit content"
1794   },
1795   {
1796     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(1),
1797     GADGET_ID_EM_SLIPPERY_GEMS,         GADGET_ID_NONE,
1798     &level.em_slippery_gems,
1799     NULL,
1800     "slip down from certain flat walls","use EM style slipping behaviour"
1801   },
1802   {
1803     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(0),
1804     GADGET_ID_BLOCK_LAST_FIELD,         GADGET_ID_NONE,
1805     &level.block_last_field,
1806     NULL,
1807     "block last field when moving",     "player blocks last field when moving"
1808   },
1809   {
1810     ED_SETTINGS_XPOS(0),                ED_COUNTER_YPOS(4),
1811     GADGET_ID_SP_BLOCK_LAST_FIELD,      GADGET_ID_NONE,
1812     &level.sp_block_last_field,
1813     NULL,
1814     "block last field when moving",     "player blocks last field when moving"
1815   },
1816
1817   /* ---------- element settings: configure 1 (custom elements) ----------- */
1818
1819   {
1820     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(1),
1821     GADGET_ID_CUSTOM_USE_GRAPHIC,       GADGET_ID_NONE,
1822     &custom_element.use_gfx_element,
1823     NULL, "use graphic of element:",    "use graphic for custom element"
1824   },
1825   {
1826     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(2),
1827     GADGET_ID_CUSTOM_USE_TEMPLATE,      GADGET_ID_NONE,
1828     &level.use_custom_template,
1829     NULL, "use template",               "use template for custom properties"
1830   },
1831   {
1832     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(3),
1833     GADGET_ID_CUSTOM_ACCESSIBLE,        GADGET_ID_NONE,
1834     &custom_element_properties[EP_ACCESSIBLE],
1835     NULL, NULL,                         "player can walk to or pass this field"
1836   },
1837   {
1838     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(5),
1839     GADGET_ID_CUSTOM_WALK_TO_OBJECT,    GADGET_ID_NONE,
1840     &custom_element_properties[EP_WALK_TO_OBJECT],
1841     NULL, NULL,                         "player can dig/collect/push element"
1842   },
1843   {
1844     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(8),
1845     GADGET_ID_CUSTOM_CAN_MOVE,          GADGET_ID_NONE,
1846     &custom_element_properties[EP_CAN_MOVE],
1847     NULL, NULL,                         "element can move in some direction"
1848   },
1849   {
1850     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(13),
1851     GADGET_ID_CUSTOM_CAN_FALL,          GADGET_ID_NONE,
1852     &custom_element_properties[EP_CAN_FALL],
1853     NULL, "can fall",                   "element can fall down"
1854   },
1855   {
1856     -1,                                 ED_SETTINGS_YPOS(13),
1857     GADGET_ID_CUSTOM_CAN_SMASH,         GADGET_ID_CUSTOM_CAN_FALL,
1858     &custom_element_properties[EP_CAN_SMASH],
1859     " ", NULL,                          "element can smash other elements"
1860   },
1861   {
1862     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(14),
1863     GADGET_ID_CUSTOM_SLIPPERY,          GADGET_ID_NONE,
1864     &custom_element_properties[EP_SLIPPERY],
1865     NULL, NULL,                         "other elements can fall down from it"
1866   },
1867
1868   /* ---------- element settings: configure 2 (custom elements) ----------- */
1869
1870   {
1871     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(1),
1872     GADGET_ID_CUSTOM_DEADLY,            GADGET_ID_NONE,
1873     &custom_element_properties[EP_DEADLY],
1874     NULL, NULL,                         "element can kill the player"
1875   },
1876   {
1877     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(2),
1878     GADGET_ID_CUSTOM_EXPLODE_RESULT,    GADGET_ID_NONE,
1879     &custom_element_properties[EP_EXPLODE_RESULT],
1880     NULL, NULL,                         "set consistency/destructibility"
1881   },
1882   {
1883     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(3),
1884     GADGET_ID_CUSTOM_EXPLODE_FIRE,      GADGET_ID_NONE,
1885     &custom_element.can_explode_by_fire,
1886     NULL, "by fire",                    "element can explode by fire/explosion"
1887   },
1888   {
1889     -1,                                 ED_SETTINGS_YPOS(3),
1890     GADGET_ID_CUSTOM_EXPLODE_SMASH,     GADGET_ID_CUSTOM_EXPLODE_FIRE,
1891     &custom_element.can_explode_smashed,
1892     " ", "smashed",                     "element can explode when smashed"
1893   },
1894   {
1895     -1,                                 ED_SETTINGS_YPOS(3),
1896     GADGET_ID_CUSTOM_EXPLODE_IMPACT,    GADGET_ID_CUSTOM_EXPLODE_SMASH,
1897     &custom_element.can_explode_impact,
1898     " ", "impact",                      "element can explode on impact"
1899   },
1900
1901   /* ---------- element settings: advanced (custom elements) --------------- */
1902
1903   {
1904     ED_SETTINGS_XPOS(0),                ED_SETTINGS_YPOS(1),
1905     GADGET_ID_CUSTOM_CAN_CHANGE,        GADGET_ID_NONE,
1906     &custom_element_change.can_change,
1907     NULL, "element changes to:",        "element can change to other element"
1908   },
1909   {
1910     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(2),
1911     GADGET_ID_CHANGE_DELAY,             GADGET_ID_NONE,
1912     &custom_element_change_events[CE_DELAY],
1913     NULL, NULL,                         "element changes after delay"
1914   },
1915   {
1916     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(4),
1917     GADGET_ID_CHANGE_BY_DIRECT_ACT,     GADGET_ID_NONE,
1918     &custom_element_change_events[CE_BY_DIRECT_ACTION],
1919     NULL, NULL,                         "element changes by direct action"
1920   },
1921   {
1922     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(5),
1923     GADGET_ID_CHANGE_BY_OTHER_ACT,      GADGET_ID_NONE,
1924     &custom_element_change_events[CE_BY_OTHER_ACTION],
1925     NULL, NULL,                         "element changes by other element"
1926   },
1927   {
1928     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(7),
1929     GADGET_ID_CHANGE_USE_EXPLOSION,     GADGET_ID_NONE,
1930     &custom_element_change.explode,
1931     NULL, "explode instead of change",  "element explodes instead of change"
1932   },
1933   {
1934     ED_SETTINGS_XPOS(1),                ED_SETTINGS_YPOS(8),
1935     GADGET_ID_CHANGE_USE_CONTENT,       GADGET_ID_NONE,
1936     &custom_element_change.use_content,
1937     NULL, "use extended change target:","element changes to more elements"
1938   },
1939   {
1940     ED_SETTINGS_XPOS(2),                ED_SETTINGS_YPOS(10),
1941     GADGET_ID_CHANGE_ONLY_COMPLETE,     GADGET_ID_NONE,
1942     &custom_element_change.only_complete,
1943     NULL, "replace all or nothing",     "only replace when all can be changed"
1944   },
1945   {
1946     ED_SETTINGS_XPOS(2),                ED_SETTINGS_YPOS(11),
1947     GADGET_ID_CHANGE_USE_RANDOM,        GADGET_ID_NONE,
1948     &custom_element_change.use_random_change,
1949     NULL, NULL,                         "use percentage for random replace"
1950   },
1951 };
1952
1953 static struct
1954 {
1955   int x, y;
1956   int area_xsize, area_ysize;
1957   int gadget_id;
1958   int gadget_id_align;
1959   char *text_left, *text_right, *text_below;
1960 } drawingarea_info[ED_NUM_DRAWING_AREAS] =
1961 {
1962   /* ---------- level playfield content ------------------------------------ */
1963
1964   {
1965     0, 0,
1966     MAX_ED_FIELDX, MAX_ED_FIELDY,
1967     GADGET_ID_DRAWING_LEVEL,            GADGET_ID_NONE,
1968     NULL, NULL,                         NULL
1969   },
1970
1971   /* ---------- yam yam content -------------------------------------------- */
1972
1973   {
1974     ED_AREA_YAMYAM_CONTENT_XPOS(0),     ED_AREA_YAMYAM_CONTENT_YPOS(0),
1975     3, 3,
1976     GADGET_ID_ELEMENT_CONTENT_0,        GADGET_ID_NONE,
1977     NULL, NULL,                         "1"
1978   },
1979   {
1980     ED_AREA_YAMYAM_CONTENT_XPOS(1),     ED_AREA_YAMYAM_CONTENT_YPOS(1),
1981     3, 3,
1982     GADGET_ID_ELEMENT_CONTENT_1,        GADGET_ID_NONE,
1983     NULL, NULL,                         "2"
1984   },
1985   {
1986     ED_AREA_YAMYAM_CONTENT_XPOS(2),     ED_AREA_YAMYAM_CONTENT_YPOS(2),
1987     3, 3,
1988     GADGET_ID_ELEMENT_CONTENT_2,        GADGET_ID_NONE,
1989     NULL, NULL,                         "3"
1990   },
1991   {
1992     ED_AREA_YAMYAM_CONTENT_XPOS(3),     ED_AREA_YAMYAM_CONTENT_YPOS(3),
1993     3, 3,
1994     GADGET_ID_ELEMENT_CONTENT_3,        GADGET_ID_NONE,
1995     NULL, NULL,                         "4"
1996   },
1997   {
1998     ED_AREA_YAMYAM_CONTENT_XPOS(4),     ED_AREA_YAMYAM_CONTENT_YPOS(4),
1999     3, 3,
2000     GADGET_ID_ELEMENT_CONTENT_4,        GADGET_ID_NONE,
2001     NULL, NULL,                         "5"
2002   },
2003   {
2004     ED_AREA_YAMYAM_CONTENT_XPOS(5),     ED_AREA_YAMYAM_CONTENT_YPOS(5),
2005     3, 3,
2006     GADGET_ID_ELEMENT_CONTENT_5,        GADGET_ID_NONE,
2007     NULL, NULL,                         "6"
2008   },
2009   {
2010     ED_AREA_YAMYAM_CONTENT_XPOS(6),     ED_AREA_YAMYAM_CONTENT_YPOS(6),
2011     3, 3,
2012     GADGET_ID_ELEMENT_CONTENT_6,        GADGET_ID_NONE,
2013     NULL, NULL,                         "7"
2014   },
2015   {
2016     ED_AREA_YAMYAM_CONTENT_XPOS(7),     ED_AREA_YAMYAM_CONTENT_YPOS(7),
2017     3, 3,
2018     GADGET_ID_ELEMENT_CONTENT_7,        GADGET_ID_NONE,
2019     NULL, NULL,                         "8"
2020   },
2021
2022   /* ---------- amoeba content --------------------------------------------- */
2023
2024   {
2025     ED_AREA_AMOEBA_CONTENT_XPOS,        ED_AREA_AMOEBA_CONTENT_YPOS,
2026     1, 1,
2027     GADGET_ID_AMOEBA_CONTENT,           GADGET_ID_NONE,
2028     "content:", NULL,                   NULL
2029   },
2030
2031   /* ---------- custom graphic --------------------------------------------- */
2032
2033   {
2034     -1,                                 ED_AREA_ELEM_CONTENT3_YPOS,
2035     1, 1,
2036     GADGET_ID_CUSTOM_GRAPHIC,           GADGET_ID_CUSTOM_USE_GRAPHIC,
2037     NULL, NULL,                         NULL
2038   },
2039
2040   /* ---------- custom content (when exploding) ---------------------------- */
2041
2042   {
2043     ED_AREA_ELEM_CONTENT4_XPOS,         ED_AREA_ELEM_CONTENT4_YPOS,
2044     3, 3,
2045     GADGET_ID_CUSTOM_CONTENT,           GADGET_ID_NONE, /* align three rows */
2046     "content:", NULL,                   NULL
2047   },
2048
2049   /* ---------- custom enter and leave element (when moving) --------------- */
2050
2051   {
2052     ED_SETTINGS_XPOS(1),                ED_AREA_ELEM_CONTENT4a_YPOS,
2053     1, 1,
2054     GADGET_ID_CUSTOM_MOVE_ENTER,        GADGET_ID_NONE,
2055     "can dig:", " ",                    NULL
2056   },
2057   {
2058     -1,                                 ED_AREA_ELEM_CONTENT4b_YPOS,
2059     1, 1,
2060     GADGET_ID_CUSTOM_MOVE_LEAVE,        GADGET_ID_CUSTOM_MOVE_LEAVE_TYPE,
2061     NULL, NULL,                         NULL
2062   },
2063
2064   /* ---------- custom change target --------------------------------------- */
2065
2066   {
2067     -1,                                 ED_AREA_ELEM_CONTENT2_YPOS,
2068     1, 1,
2069     GADGET_ID_CUSTOM_CHANGE_TARGET,     GADGET_ID_CUSTOM_CAN_CHANGE,
2070     NULL, "after/when:",                NULL
2071   },
2072
2073   /* ---------- custom change content (extended change target) ------------- */
2074
2075   {
2076     ED_AREA_ELEM_CONTENT6_XPOS,         ED_AREA_ELEM_CONTENT6_YPOS,
2077     3, 3,
2078     GADGET_ID_CUSTOM_CHANGE_CONTENT,    GADGET_ID_NONE, /* align three rows */
2079     NULL, NULL,                         NULL
2080   },
2081
2082   /* ---------- custom change trigger (element causing change) ------------- */
2083
2084   {
2085     -1,                                 ED_AREA_ELEM_CONTENT5_YPOS,
2086     1, 1,
2087     GADGET_ID_CUSTOM_CHANGE_TRIGGER,    GADGET_ID_CHANGE_OTHER_ACTION,
2088     NULL, NULL,                         NULL
2089   },
2090
2091   /* ---------- group element content -------------------------------------- */
2092
2093   {
2094     ED_AREA_GROUP_CONTENT_XPOS,         ED_AREA_GROUP_CONTENT_YPOS,
2095     MAX_ELEMENTS_IN_GROUP, 1,
2096     GADGET_ID_GROUP_CONTENT,            GADGET_ID_NONE,
2097     "content:", NULL,                   NULL
2098   },
2099
2100   /* ---------- random background (for random painting) -------------------- */
2101
2102   {
2103     -1,                                 ED_AREA_RANDOM_BACKGROUND_YPOS,
2104     1, 1,
2105     GADGET_ID_RANDOM_BACKGROUND,        GADGET_ID_RANDOM_RESTRICTED,
2106     NULL, NULL,                         NULL
2107   },
2108 };
2109
2110
2111 /*
2112   -----------------------------------------------------------------------------
2113   some internally used variables
2114   -----------------------------------------------------------------------------
2115 */
2116
2117 /* actual size of level editor drawing area */
2118 static int ed_fieldx = MAX_ED_FIELDX - 1, ed_fieldy = MAX_ED_FIELDY - 1;
2119
2120 /* actual position of level editor drawing area in level playfield */
2121 static int level_xpos = -1, level_ypos = -1;
2122
2123 #define IN_ED_FIELD(x,y)  ((x)>=0 && (x)<ed_fieldx && (y)>=0 &&(y)<ed_fieldy)
2124
2125 /* drawing elements on the three mouse buttons */
2126 static int new_element1 = EL_WALL;
2127 static int new_element2 = EL_EMPTY;
2128 static int new_element3 = EL_SAND;
2129
2130 #define BUTTON_ELEMENT(button) ((button) == 1 ? new_element1 : \
2131                                 (button) == 2 ? new_element2 : \
2132                                 (button) == 3 ? new_element3 : EL_EMPTY)
2133 #define BUTTON_STEPSIZE(button) ((button) == 1 ? 1 : (button) == 2 ? 5 : 10)
2134
2135 /* forward declaration for internal use */
2136 static void ModifyEditorCounter(int, int);
2137 static void ModifyEditorCounterLimits(int, int, int);
2138 static void ModifyEditorSelectbox(int, int);
2139 static void ModifyEditorDrawingArea(int, int, int);
2140 static void ModifyEditorElementList();
2141 static void RedrawDrawingElements();
2142 static void DrawDrawingWindow();
2143 static void DrawLevelInfoWindow();
2144 static void DrawPropertiesWindow();
2145 static void UpdateCustomElementGraphicGadgets();
2146 static boolean checkPropertiesConfig();
2147 static void CopyLevelToUndoBuffer(int);
2148 static void HandleDrawingAreas(struct GadgetInfo *);
2149 static void HandleCounterButtons(struct GadgetInfo *);
2150 static void HandleTextInputGadgets(struct GadgetInfo *);
2151 static void HandleTextAreaGadgets(struct GadgetInfo *);
2152 static void HandleSelectboxGadgets(struct GadgetInfo *);
2153 static void HandleTextbuttonGadgets(struct GadgetInfo *);
2154 static void HandleGraphicbuttonGadgets(struct GadgetInfo *);
2155 static void HandleRadiobuttons(struct GadgetInfo *);
2156 static void HandleCheckbuttons(struct GadgetInfo *);
2157 static void HandleControlButtons(struct GadgetInfo *);
2158 static void HandleDrawingAreaInfo(struct GadgetInfo *);
2159
2160 static struct GadgetInfo *level_editor_gadget[NUM_EDITOR_GADGETS];
2161 static int right_gadget_border[NUM_EDITOR_GADGETS];
2162
2163 static int drawing_function = GADGET_ID_SINGLE_ITEMS;
2164 static int last_drawing_function = GADGET_ID_SINGLE_ITEMS;
2165 static boolean draw_with_brush = FALSE;
2166 static int properties_element = 0;
2167
2168 static short FieldBackup[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
2169 static short UndoBuffer[NUM_UNDO_STEPS][MAX_LEV_FIELDX][MAX_LEV_FIELDY];
2170 static int undo_buffer_position = 0;
2171 static int undo_buffer_steps = 0;
2172
2173 static int edit_mode;
2174 static int edit_mode_properties;
2175
2176 static int element_shift = 0;
2177
2178 static int editor_hl_boulderdash[] =
2179 {
2180   EL_CHAR('B'),
2181   EL_CHAR('O'),
2182   EL_CHAR('U'),
2183   EL_CHAR('L'),
2184
2185   EL_CHAR('-'),
2186   EL_CHAR('D'),
2187   EL_CHAR('E'),
2188   EL_CHAR('R'),
2189
2190   EL_CHAR('D'),
2191   EL_CHAR('A'),
2192   EL_CHAR('S'),
2193   EL_CHAR('H'),
2194 };
2195
2196 static int editor_el_boulderdash[] =
2197 {
2198   EL_PLAYER_1,
2199   EL_EMPTY,
2200   EL_SAND,
2201   EL_STEELWALL,
2202
2203   EL_BD_WALL,
2204   EL_BD_MAGIC_WALL,
2205   EL_EXIT_CLOSED,
2206   EL_EXIT_OPEN,
2207
2208   EL_BD_DIAMOND,
2209   EL_BD_BUTTERFLY_UP,
2210   EL_BD_FIREFLY_UP,
2211   EL_BD_ROCK,
2212
2213   EL_BD_BUTTERFLY_LEFT,
2214   EL_BD_FIREFLY_LEFT,
2215   EL_BD_BUTTERFLY_RIGHT,
2216   EL_BD_FIREFLY_RIGHT,
2217
2218   EL_BD_AMOEBA,
2219   EL_BD_BUTTERFLY_DOWN,
2220   EL_BD_FIREFLY_DOWN,
2221   EL_EMPTY,
2222 };
2223 static int *editor_hl_boulderdash_ptr = editor_hl_boulderdash;
2224 static int *editor_el_boulderdash_ptr = editor_el_boulderdash;
2225 static int num_editor_hl_boulderdash = SIZEOF_ARRAY_INT(editor_hl_boulderdash);
2226 static int num_editor_el_boulderdash = SIZEOF_ARRAY_INT(editor_el_boulderdash);
2227
2228 static int editor_hl_emerald_mine[] =
2229 {
2230   EL_CHAR('E'),
2231   EL_CHAR('M'),
2232   EL_CHAR('E'),
2233   EL_CHAR('-'),
2234
2235   EL_CHAR('R'),
2236   EL_CHAR('A'),
2237   EL_CHAR('L'),
2238   EL_CHAR('D'),
2239
2240   EL_CHAR('M'),
2241   EL_CHAR('I'),
2242   EL_CHAR('N'),
2243   EL_CHAR('E'),
2244 };
2245
2246 static int editor_el_emerald_mine[] =
2247 {
2248   EL_PLAYER_1,
2249   EL_PLAYER_2,
2250   EL_PLAYER_3,
2251   EL_PLAYER_4,
2252
2253   EL_PLAYER_1,
2254   EL_EMPTY,
2255   EL_SAND,
2256   EL_ROCK,
2257
2258   EL_STEELWALL,
2259   EL_WALL,
2260   EL_WALL_SLIPPERY,
2261   EL_MAGIC_WALL,
2262
2263   EL_EMERALD,
2264   EL_DIAMOND,
2265   EL_NUT,
2266   EL_BOMB,
2267
2268   EL_WALL_EMERALD,
2269   EL_WALL_DIAMOND,
2270   EL_QUICKSAND_EMPTY,
2271   EL_QUICKSAND_FULL,
2272
2273   EL_DYNAMITE,
2274   EL_DYNAMITE_ACTIVE,
2275   EL_EXIT_CLOSED,
2276   EL_EXIT_OPEN,
2277
2278   EL_YAMYAM,
2279   EL_BUG_UP,
2280   EL_SPACESHIP_UP,
2281   EL_ROBOT,
2282
2283   EL_BUG_LEFT,
2284   EL_SPACESHIP_LEFT,
2285   EL_BUG_RIGHT,
2286   EL_SPACESHIP_RIGHT,
2287
2288   EL_ROBOT_WHEEL,
2289   EL_BUG_DOWN,
2290   EL_SPACESHIP_DOWN,
2291   EL_INVISIBLE_WALL,
2292
2293   EL_ACID_POOL_TOPLEFT,
2294   EL_ACID,
2295   EL_ACID_POOL_TOPRIGHT,
2296   EL_EMPTY,
2297
2298   EL_ACID_POOL_BOTTOMLEFT,
2299   EL_ACID_POOL_BOTTOM,
2300   EL_ACID_POOL_BOTTOMRIGHT,
2301   EL_EMPTY,
2302
2303   EL_AMOEBA_DROP,
2304   EL_AMOEBA_DEAD,
2305   EL_AMOEBA_WET,
2306   EL_AMOEBA_DRY,
2307
2308 #if 1
2309   EL_EM_KEY_1,
2310   EL_EM_KEY_2,
2311   EL_EM_KEY_3,
2312   EL_EM_KEY_4,
2313 #else
2314   EL_EM_KEY_1_FILE,
2315   EL_EM_KEY_2_FILE,
2316   EL_EM_KEY_3_FILE,
2317   EL_EM_KEY_4_FILE,
2318 #endif
2319
2320   EL_EM_GATE_1,
2321   EL_EM_GATE_2,
2322   EL_EM_GATE_3,
2323   EL_EM_GATE_4,
2324
2325   EL_EM_GATE_1_GRAY,
2326   EL_EM_GATE_2_GRAY,
2327   EL_EM_GATE_3_GRAY,
2328   EL_EM_GATE_4_GRAY,
2329 };
2330 static int *editor_hl_emerald_mine_ptr = editor_hl_emerald_mine;
2331 static int *editor_el_emerald_mine_ptr = editor_el_emerald_mine;
2332 static int num_editor_hl_emerald_mine=SIZEOF_ARRAY_INT(editor_hl_emerald_mine);
2333 static int num_editor_el_emerald_mine=SIZEOF_ARRAY_INT(editor_el_emerald_mine);
2334
2335 static int editor_hl_more[] =
2336 {
2337   EL_CHAR('M'),
2338   EL_CHAR('O'),
2339   EL_CHAR('R'),
2340   EL_CHAR('E'),
2341 };
2342
2343 static int editor_el_more[] =
2344 {
2345   EL_KEY_1,
2346   EL_KEY_2,
2347   EL_KEY_3,
2348   EL_KEY_4,
2349
2350   EL_GATE_1,
2351   EL_GATE_2,
2352   EL_GATE_3,
2353   EL_GATE_4,
2354
2355   EL_GATE_1_GRAY,
2356   EL_GATE_2_GRAY,
2357   EL_GATE_3_GRAY,
2358   EL_GATE_4_GRAY,
2359
2360   EL_ARROW_LEFT,
2361   EL_ARROW_RIGHT,
2362   EL_ARROW_UP,
2363   EL_ARROW_DOWN,
2364
2365   EL_AMOEBA_FULL,
2366   EL_EMERALD_YELLOW,
2367   EL_EMERALD_RED,
2368   EL_EMERALD_PURPLE,
2369
2370   EL_WALL_BD_DIAMOND,
2371   EL_WALL_EMERALD_YELLOW,
2372   EL_WALL_EMERALD_RED,
2373   EL_WALL_EMERALD_PURPLE,
2374
2375   EL_GAME_OF_LIFE,
2376   EL_PACMAN_UP,
2377   EL_TIME_ORB_FULL,
2378   EL_TIME_ORB_EMPTY,
2379
2380   EL_PACMAN_LEFT,
2381   EL_DARK_YAMYAM,
2382   EL_PACMAN_RIGHT,
2383   EL_EXPANDABLE_WALL,
2384
2385   EL_BIOMAZE,
2386   EL_PACMAN_DOWN,
2387   EL_LAMP,
2388   EL_LAMP_ACTIVE,
2389
2390   EL_DYNABOMB_INCREASE_NUMBER,
2391   EL_DYNABOMB_INCREASE_SIZE,
2392   EL_DYNABOMB_INCREASE_POWER,
2393   EL_STONEBLOCK,
2394
2395   EL_MOLE,
2396   EL_PENGUIN,
2397   EL_PIG,
2398   EL_DRAGON,
2399
2400   EL_BUG,
2401   EL_MOLE_UP,
2402   EL_BD_BUTTERFLY,
2403   EL_BD_FIREFLY,
2404
2405   EL_MOLE_LEFT,
2406   EL_EMPTY,
2407   EL_MOLE_RIGHT,
2408   EL_PACMAN,
2409
2410   EL_SPACESHIP,
2411   EL_MOLE_DOWN,
2412   EL_BALLOON,
2413   EL_BALLOON_SWITCH_ANY,
2414
2415   EL_BALLOON_SWITCH_LEFT,
2416   EL_BALLOON_SWITCH_RIGHT,
2417   EL_BALLOON_SWITCH_UP,
2418   EL_BALLOON_SWITCH_DOWN,
2419
2420   EL_SATELLITE,
2421   EL_EXPANDABLE_WALL_HORIZONTAL,
2422   EL_EXPANDABLE_WALL_VERTICAL,
2423   EL_EXPANDABLE_WALL_ANY,
2424
2425   EL_INVISIBLE_STEELWALL,
2426   EL_INVISIBLE_WALL,
2427   EL_SPEED_PILL,
2428   EL_BLACK_ORB,
2429
2430   EL_EMC_STEELWALL_1,
2431   EL_EMC_WALL_1,
2432   EL_EMC_WALL_2,
2433   EL_EMC_WALL_3,
2434
2435   EL_EMC_WALL_4,
2436   EL_EMC_WALL_5,
2437   EL_EMC_WALL_6,
2438   EL_EMC_WALL_7,
2439 };
2440 static int *editor_hl_more_ptr = editor_hl_more;
2441 static int *editor_el_more_ptr = editor_el_more;
2442 static int num_editor_hl_more = SIZEOF_ARRAY_INT(editor_hl_more);
2443 static int num_editor_el_more = SIZEOF_ARRAY_INT(editor_el_more);
2444
2445 static int editor_hl_sokoban[] =
2446 {
2447   EL_CHAR('S'),
2448   EL_CHAR('O'),
2449   EL_CHAR('K'),
2450   EL_CHAR('O'),
2451
2452   EL_CHAR('-'),
2453   EL_CHAR('B'),
2454   EL_CHAR('A'),
2455   EL_CHAR('N'),
2456 };
2457
2458 static int editor_el_sokoban[] =
2459 {
2460   EL_SOKOBAN_OBJECT,
2461   EL_SOKOBAN_FIELD_EMPTY,
2462   EL_SOKOBAN_FIELD_FULL,
2463   EL_SOKOBAN_FIELD_PLAYER,
2464 };
2465 static int *editor_hl_sokoban_ptr = editor_hl_sokoban;
2466 static int *editor_el_sokoban_ptr = editor_el_sokoban;
2467 static int num_editor_hl_sokoban = SIZEOF_ARRAY_INT(editor_hl_sokoban);
2468 static int num_editor_el_sokoban = SIZEOF_ARRAY_INT(editor_el_sokoban);
2469
2470 static int editor_hl_supaplex[] =
2471 {
2472   EL_CHAR('S'),
2473   EL_CHAR('U'),
2474   EL_CHAR('P'),
2475   EL_CHAR('A'),
2476
2477   EL_CHAR('P'),
2478   EL_CHAR('L'),
2479   EL_CHAR('E'),
2480   EL_CHAR('X'),
2481 };
2482
2483 static int editor_el_supaplex[] =
2484 {
2485 #if 1
2486   EL_EMPTY,
2487 #else
2488   EL_SP_EMPTY,
2489 #endif
2490   EL_SP_ZONK,
2491   EL_SP_BASE,
2492   EL_SP_MURPHY,
2493
2494   EL_SP_INFOTRON,
2495   EL_SP_CHIP_SINGLE,
2496   EL_SP_HARDWARE_GRAY,
2497   EL_SP_EXIT_CLOSED,
2498
2499   EL_SP_DISK_ORANGE,
2500   EL_SP_PORT_RIGHT,
2501   EL_SP_PORT_DOWN,
2502   EL_SP_PORT_LEFT,
2503
2504   EL_SP_PORT_UP,
2505   EL_SP_GRAVITY_PORT_RIGHT,
2506   EL_SP_GRAVITY_PORT_DOWN,
2507   EL_SP_GRAVITY_PORT_LEFT,
2508
2509   EL_SP_GRAVITY_PORT_UP,
2510   EL_SP_SNIKSNAK,
2511   EL_SP_DISK_YELLOW,
2512   EL_SP_TERMINAL,
2513
2514   EL_SP_DISK_RED,
2515   EL_SP_PORT_VERTICAL,
2516   EL_SP_PORT_HORIZONTAL,
2517   EL_SP_PORT_ANY,
2518
2519   EL_SP_ELECTRON,
2520   EL_SP_BUGGY_BASE,
2521   EL_SP_CHIP_LEFT,
2522   EL_SP_CHIP_RIGHT,
2523
2524   EL_SP_HARDWARE_BASE_1,
2525   EL_SP_HARDWARE_GREEN,
2526   EL_SP_HARDWARE_BLUE,
2527   EL_SP_HARDWARE_RED,
2528
2529   EL_SP_HARDWARE_YELLOW,
2530   EL_SP_HARDWARE_BASE_2,
2531   EL_SP_HARDWARE_BASE_3,
2532   EL_SP_HARDWARE_BASE_4,
2533
2534   EL_SP_HARDWARE_BASE_5,
2535   EL_SP_HARDWARE_BASE_6,
2536   EL_SP_CHIP_TOP,
2537   EL_SP_CHIP_BOTTOM,
2538 };
2539 static int *editor_hl_supaplex_ptr = editor_hl_supaplex;
2540 static int *editor_el_supaplex_ptr = editor_el_supaplex;
2541 static int num_editor_hl_supaplex = SIZEOF_ARRAY_INT(editor_hl_supaplex);
2542 static int num_editor_el_supaplex = SIZEOF_ARRAY_INT(editor_el_supaplex);
2543
2544 static int editor_hl_diamond_caves[] =
2545 {
2546   EL_CHAR('D'),
2547   EL_CHAR('I'),
2548   EL_CHAR('A'),
2549   EL_CHAR('-'),
2550
2551   EL_CHAR('M'),
2552   EL_CHAR('O'),
2553   EL_CHAR('N'),
2554   EL_CHAR('D'),
2555
2556   EL_CHAR('C'),
2557   EL_CHAR('A'),
2558   EL_CHAR('V'),
2559   EL_CHAR('E'),
2560
2561   EL_CHAR('S'),
2562   EL_CHAR(' '),
2563   EL_CHAR('I'),
2564   EL_CHAR('I'),
2565 };
2566
2567 static int editor_el_diamond_caves[] =
2568 {
2569   EL_PEARL,
2570   EL_CRYSTAL,
2571   EL_WALL_PEARL,
2572   EL_WALL_CRYSTAL,
2573
2574   EL_CONVEYOR_BELT_1_LEFT,
2575   EL_CONVEYOR_BELT_1_MIDDLE,
2576   EL_CONVEYOR_BELT_1_RIGHT,
2577   EL_CONVEYOR_BELT_1_SWITCH_MIDDLE,
2578
2579   EL_CONVEYOR_BELT_2_LEFT,
2580   EL_CONVEYOR_BELT_2_MIDDLE,
2581   EL_CONVEYOR_BELT_2_RIGHT,
2582   EL_CONVEYOR_BELT_2_SWITCH_MIDDLE,
2583
2584   EL_CONVEYOR_BELT_3_LEFT,
2585   EL_CONVEYOR_BELT_3_MIDDLE,
2586   EL_CONVEYOR_BELT_3_RIGHT,
2587   EL_CONVEYOR_BELT_3_SWITCH_MIDDLE,
2588
2589   EL_CONVEYOR_BELT_4_LEFT,
2590   EL_CONVEYOR_BELT_4_MIDDLE,
2591   EL_CONVEYOR_BELT_4_RIGHT,
2592   EL_CONVEYOR_BELT_4_SWITCH_MIDDLE,
2593
2594   EL_CONVEYOR_BELT_1_SWITCH_LEFT,
2595   EL_CONVEYOR_BELT_2_SWITCH_LEFT,
2596   EL_CONVEYOR_BELT_3_SWITCH_LEFT,
2597   EL_CONVEYOR_BELT_4_SWITCH_LEFT,
2598
2599   EL_CONVEYOR_BELT_1_SWITCH_RIGHT,
2600   EL_CONVEYOR_BELT_2_SWITCH_RIGHT,
2601   EL_CONVEYOR_BELT_3_SWITCH_RIGHT,
2602   EL_CONVEYOR_BELT_4_SWITCH_RIGHT,
2603
2604   EL_SWITCHGATE_OPEN,
2605   EL_SWITCHGATE_CLOSED,
2606   EL_SWITCHGATE_SWITCH_UP,
2607   EL_EMPTY,
2608
2609   EL_ENVELOPE_1,
2610   EL_ENVELOPE_2,
2611   EL_ENVELOPE_3,
2612   EL_ENVELOPE_4,
2613
2614   EL_TIMEGATE_CLOSED,
2615   EL_TIMEGATE_OPEN,
2616   EL_TIMEGATE_SWITCH,
2617   EL_EMPTY,
2618
2619   EL_LANDMINE,
2620   EL_INVISIBLE_SAND,
2621   EL_STEELWALL_SLIPPERY,
2622   EL_EMPTY,
2623
2624   EL_SIGN_EXCLAMATION,
2625   EL_SIGN_STOP,
2626   EL_LIGHT_SWITCH,
2627   EL_LIGHT_SWITCH_ACTIVE,
2628
2629   EL_SHIELD_NORMAL,
2630   EL_SHIELD_DEADLY,
2631   EL_EXTRA_TIME,
2632   EL_EMPTY,
2633 };
2634 static int *editor_hl_diamond_caves_ptr = editor_hl_diamond_caves;
2635 static int *editor_el_diamond_caves_ptr = editor_el_diamond_caves;
2636 static int num_editor_hl_diamond_caves = SIZEOF_ARRAY_INT(editor_hl_diamond_caves);
2637 static int num_editor_el_diamond_caves = SIZEOF_ARRAY_INT(editor_el_diamond_caves);
2638
2639 static int editor_hl_dx_boulderdash[] =
2640 {
2641   EL_CHAR('D'),
2642   EL_CHAR('X'),
2643   EL_CHAR('-'),
2644   EL_CHAR(' '),
2645
2646   EL_CHAR('B'),
2647   EL_CHAR('O'),
2648   EL_CHAR('U'),
2649   EL_CHAR('L'),
2650
2651   EL_CHAR('-'),
2652   EL_CHAR('D'),
2653   EL_CHAR('E'),
2654   EL_CHAR('R'),
2655
2656   EL_CHAR('D'),
2657   EL_CHAR('A'),
2658   EL_CHAR('S'),
2659   EL_CHAR('H'),
2660 };
2661
2662 static int editor_el_dx_boulderdash[] =
2663 {
2664   EL_SPRING,
2665   EL_TUBE_RIGHT_DOWN,
2666   EL_TUBE_HORIZONTAL_DOWN,
2667   EL_TUBE_LEFT_DOWN,
2668
2669   EL_TUBE_HORIZONTAL,
2670   EL_TUBE_VERTICAL_RIGHT,
2671   EL_TUBE_ANY,
2672   EL_TUBE_VERTICAL_LEFT,
2673
2674   EL_TUBE_VERTICAL,
2675   EL_TUBE_RIGHT_UP,
2676   EL_TUBE_HORIZONTAL_UP,
2677   EL_TUBE_LEFT_UP,
2678
2679   EL_TRAP,
2680   EL_DX_SUPABOMB,
2681   EL_EMPTY,
2682   EL_EMPTY
2683 };
2684 static int *editor_hl_dx_boulderdash_ptr = editor_hl_dx_boulderdash;
2685 static int *editor_el_dx_boulderdash_ptr = editor_el_dx_boulderdash;
2686 static int num_editor_hl_dx_boulderdash = SIZEOF_ARRAY_INT(editor_hl_dx_boulderdash);
2687 static int num_editor_el_dx_boulderdash = SIZEOF_ARRAY_INT(editor_el_dx_boulderdash);
2688
2689 static int editor_hl_chars[] =
2690 {
2691   EL_CHAR('T'),
2692   EL_CHAR('E'),
2693   EL_CHAR('X'),
2694   EL_CHAR('T'),
2695 };
2696
2697 static int editor_el_chars[] =
2698 {
2699   EL_CHAR(' '),
2700   EL_CHAR('!'),
2701   EL_CHAR('"'),
2702   EL_CHAR('#'),
2703
2704   EL_CHAR('$'),
2705   EL_CHAR('%'),
2706   EL_CHAR('&'),
2707   EL_CHAR('\''),
2708
2709   EL_CHAR('('),
2710   EL_CHAR(')'),
2711   EL_CHAR('*'),
2712   EL_CHAR('+'),
2713
2714   EL_CHAR(','),
2715   EL_CHAR('-'),
2716   EL_CHAR('.'),
2717   EL_CHAR('/'),
2718
2719   EL_CHAR('0'),
2720   EL_CHAR('1'),
2721   EL_CHAR('2'),
2722   EL_CHAR('3'),
2723
2724   EL_CHAR('4'),
2725   EL_CHAR('5'),
2726   EL_CHAR('6'),
2727   EL_CHAR('7'),
2728
2729   EL_CHAR('8'),
2730   EL_CHAR('9'),
2731   EL_CHAR(':'),
2732   EL_CHAR(';'),
2733
2734   EL_CHAR('<'),
2735   EL_CHAR('='),
2736   EL_CHAR('>'),
2737   EL_CHAR('?'),
2738
2739   EL_CHAR('@'),
2740   EL_CHAR('A'),
2741   EL_CHAR('B'),
2742   EL_CHAR('C'),
2743
2744   EL_CHAR('D'),
2745   EL_CHAR('E'),
2746   EL_CHAR('F'),
2747   EL_CHAR('G'),
2748
2749   EL_CHAR('H'),
2750   EL_CHAR('I'),
2751   EL_CHAR('J'),
2752   EL_CHAR('K'),
2753
2754   EL_CHAR('L'),
2755   EL_CHAR('M'),
2756   EL_CHAR('N'),
2757   EL_CHAR('O'),
2758
2759   EL_CHAR('P'),
2760   EL_CHAR('Q'),
2761   EL_CHAR('R'),
2762   EL_CHAR('S'),
2763
2764   EL_CHAR('T'),
2765   EL_CHAR('U'),
2766   EL_CHAR('V'),
2767   EL_CHAR('W'),
2768
2769   EL_CHAR('X'),
2770   EL_CHAR('Y'),
2771   EL_CHAR('Z'),
2772   EL_CHAR('['),
2773
2774   EL_CHAR('\\'),
2775   EL_CHAR(']'),
2776   EL_CHAR('^'),
2777   EL_CHAR('_'),
2778
2779   EL_CHAR('©'),
2780   EL_CHAR('Ä'),
2781   EL_CHAR('Ö'),
2782   EL_CHAR('Ãœ'),
2783
2784   EL_CHAR('°'),
2785   EL_CHAR('®'),
2786   EL_CHAR(FONT_ASCII_CURSOR),
2787   EL_CHAR(' ')
2788 };
2789 static int *editor_hl_chars_ptr = editor_hl_chars;
2790 static int *editor_el_chars_ptr = editor_el_chars;
2791 static int num_editor_hl_chars = SIZEOF_ARRAY_INT(editor_hl_chars);
2792 static int num_editor_el_chars = SIZEOF_ARRAY_INT(editor_el_chars);
2793
2794 static int editor_hl_custom[] =
2795 {
2796   EL_CHAR('C'),
2797   EL_CHAR('U'),
2798   EL_CHAR('S'),
2799   EL_CHAR('-'),
2800
2801   EL_CHAR('T'),
2802   EL_CHAR('O'),
2803   EL_CHAR('M'),
2804   EL_CHAR(' '),
2805
2806   EL_CHAR('E'),
2807   EL_CHAR('L'),
2808   EL_CHAR('E'),
2809   EL_CHAR('M'),
2810
2811   EL_CHAR('E'),
2812   EL_CHAR('N'),
2813   EL_CHAR('T'),
2814   EL_CHAR('S'),
2815 };
2816
2817 static int editor_el_custom[] =
2818 {
2819   EL_CUSTOM_START + 0,
2820   EL_CUSTOM_START + 1,
2821   EL_CUSTOM_START + 2,
2822   EL_CUSTOM_START + 3,
2823
2824   EL_CUSTOM_START + 4,
2825   EL_CUSTOM_START + 5,
2826   EL_CUSTOM_START + 6,
2827   EL_CUSTOM_START + 7,
2828
2829   EL_CUSTOM_START + 8,
2830   EL_CUSTOM_START + 9,
2831   EL_CUSTOM_START + 10,
2832   EL_CUSTOM_START + 11,
2833
2834   EL_CUSTOM_START + 12,
2835   EL_CUSTOM_START + 13,
2836   EL_CUSTOM_START + 14,
2837   EL_CUSTOM_START + 15,
2838
2839   EL_CUSTOM_START + 16,
2840   EL_CUSTOM_START + 17,
2841   EL_CUSTOM_START + 18,
2842   EL_CUSTOM_START + 19,
2843
2844   EL_CUSTOM_START + 20,
2845   EL_CUSTOM_START + 21,
2846   EL_CUSTOM_START + 22,
2847   EL_CUSTOM_START + 23,
2848
2849   EL_CUSTOM_START + 24,
2850   EL_CUSTOM_START + 25,
2851   EL_CUSTOM_START + 26,
2852   EL_CUSTOM_START + 27,
2853
2854   EL_CUSTOM_START + 28,
2855   EL_CUSTOM_START + 29,
2856   EL_CUSTOM_START + 30,
2857   EL_CUSTOM_START + 31,
2858
2859   EL_CUSTOM_START + 32,
2860   EL_CUSTOM_START + 33,
2861   EL_CUSTOM_START + 34,
2862   EL_CUSTOM_START + 35,
2863
2864   EL_CUSTOM_START + 36,
2865   EL_CUSTOM_START + 37,
2866   EL_CUSTOM_START + 38,
2867   EL_CUSTOM_START + 39,
2868
2869   EL_CUSTOM_START + 40,
2870   EL_CUSTOM_START + 41,
2871   EL_CUSTOM_START + 42,
2872   EL_CUSTOM_START + 43,
2873
2874   EL_CUSTOM_START + 44,
2875   EL_CUSTOM_START + 45,
2876   EL_CUSTOM_START + 46,
2877   EL_CUSTOM_START + 47,
2878
2879   EL_CUSTOM_START + 48,
2880   EL_CUSTOM_START + 49,
2881   EL_CUSTOM_START + 50,
2882   EL_CUSTOM_START + 51,
2883
2884   EL_CUSTOM_START + 52,
2885   EL_CUSTOM_START + 53,
2886   EL_CUSTOM_START + 54,
2887   EL_CUSTOM_START + 55,
2888
2889   EL_CUSTOM_START + 56,
2890   EL_CUSTOM_START + 57,
2891   EL_CUSTOM_START + 58,
2892   EL_CUSTOM_START + 59,
2893
2894   EL_CUSTOM_START + 60,
2895   EL_CUSTOM_START + 61,
2896   EL_CUSTOM_START + 62,
2897   EL_CUSTOM_START + 63,
2898
2899   EL_CUSTOM_START + 64,
2900   EL_CUSTOM_START + 65,
2901   EL_CUSTOM_START + 66,
2902   EL_CUSTOM_START + 67,
2903
2904   EL_CUSTOM_START + 68,
2905   EL_CUSTOM_START + 69,
2906   EL_CUSTOM_START + 70,
2907   EL_CUSTOM_START + 71,
2908
2909   EL_CUSTOM_START + 72,
2910   EL_CUSTOM_START + 73,
2911   EL_CUSTOM_START + 74,
2912   EL_CUSTOM_START + 75,
2913
2914   EL_CUSTOM_START + 76,
2915   EL_CUSTOM_START + 77,
2916   EL_CUSTOM_START + 78,
2917   EL_CUSTOM_START + 79,
2918
2919   EL_CUSTOM_START + 80,
2920   EL_CUSTOM_START + 81,
2921   EL_CUSTOM_START + 82,
2922   EL_CUSTOM_START + 83,
2923
2924   EL_CUSTOM_START + 84,
2925   EL_CUSTOM_START + 85,
2926   EL_CUSTOM_START + 86,
2927   EL_CUSTOM_START + 87,
2928
2929   EL_CUSTOM_START + 88,
2930   EL_CUSTOM_START + 89,
2931   EL_CUSTOM_START + 90,
2932   EL_CUSTOM_START + 91,
2933
2934   EL_CUSTOM_START + 92,
2935   EL_CUSTOM_START + 93,
2936   EL_CUSTOM_START + 94,
2937   EL_CUSTOM_START + 95,
2938
2939   EL_CUSTOM_START + 96,
2940   EL_CUSTOM_START + 97,
2941   EL_CUSTOM_START + 98,
2942   EL_CUSTOM_START + 99,
2943
2944   EL_CUSTOM_START + 100,
2945   EL_CUSTOM_START + 101,
2946   EL_CUSTOM_START + 102,
2947   EL_CUSTOM_START + 103,
2948
2949   EL_CUSTOM_START + 104,
2950   EL_CUSTOM_START + 105,
2951   EL_CUSTOM_START + 106,
2952   EL_CUSTOM_START + 107,
2953
2954   EL_CUSTOM_START + 108,
2955   EL_CUSTOM_START + 109,
2956   EL_CUSTOM_START + 110,
2957   EL_CUSTOM_START + 111,
2958
2959   EL_CUSTOM_START + 112,
2960   EL_CUSTOM_START + 113,
2961   EL_CUSTOM_START + 114,
2962   EL_CUSTOM_START + 115,
2963
2964   EL_CUSTOM_START + 116,
2965   EL_CUSTOM_START + 117,
2966   EL_CUSTOM_START + 118,
2967   EL_CUSTOM_START + 119,
2968
2969   EL_CUSTOM_START + 120,
2970   EL_CUSTOM_START + 121,
2971   EL_CUSTOM_START + 122,
2972   EL_CUSTOM_START + 123,
2973
2974   EL_CUSTOM_START + 124,
2975   EL_CUSTOM_START + 125,
2976   EL_CUSTOM_START + 126,
2977   EL_CUSTOM_START + 127
2978 };
2979 static int *editor_hl_custom_ptr = editor_hl_custom;
2980 static int *editor_el_custom_ptr = editor_el_custom;
2981 static int num_editor_hl_custom = SIZEOF_ARRAY_INT(editor_hl_custom);
2982 static int num_editor_el_custom = SIZEOF_ARRAY_INT(editor_el_custom);
2983
2984 static int editor_hl_custom_more[] =
2985 {
2986 };
2987
2988 static int editor_el_custom_more[] =
2989 {
2990   EL_CUSTOM_START + 128,
2991   EL_CUSTOM_START + 129,
2992   EL_CUSTOM_START + 130,
2993   EL_CUSTOM_START + 131,
2994
2995   EL_CUSTOM_START + 132,
2996   EL_CUSTOM_START + 133,
2997   EL_CUSTOM_START + 134,
2998   EL_CUSTOM_START + 135,
2999
3000   EL_CUSTOM_START + 136,
3001   EL_CUSTOM_START + 137,
3002   EL_CUSTOM_START + 138,
3003   EL_CUSTOM_START + 139,
3004
3005   EL_CUSTOM_START + 140,
3006   EL_CUSTOM_START + 141,
3007   EL_CUSTOM_START + 142,
3008   EL_CUSTOM_START + 143,
3009
3010   EL_CUSTOM_START + 144,
3011   EL_CUSTOM_START + 145,
3012   EL_CUSTOM_START + 146,
3013   EL_CUSTOM_START + 147,
3014
3015   EL_CUSTOM_START + 148,
3016   EL_CUSTOM_START + 149,
3017   EL_CUSTOM_START + 150,
3018   EL_CUSTOM_START + 151,
3019
3020   EL_CUSTOM_START + 152,
3021   EL_CUSTOM_START + 153,
3022   EL_CUSTOM_START + 154,
3023   EL_CUSTOM_START + 155,
3024
3025   EL_CUSTOM_START + 156,
3026   EL_CUSTOM_START + 157,
3027   EL_CUSTOM_START + 158,
3028   EL_CUSTOM_START + 159,
3029
3030   EL_CUSTOM_START + 160,
3031   EL_CUSTOM_START + 161,
3032   EL_CUSTOM_START + 162,
3033   EL_CUSTOM_START + 163,
3034
3035   EL_CUSTOM_START + 164,
3036   EL_CUSTOM_START + 165,
3037   EL_CUSTOM_START + 166,
3038   EL_CUSTOM_START + 167,
3039
3040   EL_CUSTOM_START + 168,
3041   EL_CUSTOM_START + 169,
3042   EL_CUSTOM_START + 170,
3043   EL_CUSTOM_START + 171,
3044
3045   EL_CUSTOM_START + 172,
3046   EL_CUSTOM_START + 173,
3047   EL_CUSTOM_START + 174,
3048   EL_CUSTOM_START + 175,
3049
3050   EL_CUSTOM_START + 176,
3051   EL_CUSTOM_START + 177,
3052   EL_CUSTOM_START + 178,
3053   EL_CUSTOM_START + 179,
3054
3055   EL_CUSTOM_START + 180,
3056   EL_CUSTOM_START + 181,
3057   EL_CUSTOM_START + 182,
3058   EL_CUSTOM_START + 183,
3059
3060   EL_CUSTOM_START + 184,
3061   EL_CUSTOM_START + 185,
3062   EL_CUSTOM_START + 186,
3063   EL_CUSTOM_START + 187,
3064
3065   EL_CUSTOM_START + 188,
3066   EL_CUSTOM_START + 189,
3067   EL_CUSTOM_START + 190,
3068   EL_CUSTOM_START + 191,
3069
3070   EL_CUSTOM_START + 192,
3071   EL_CUSTOM_START + 193,
3072   EL_CUSTOM_START + 194,
3073   EL_CUSTOM_START + 195,
3074
3075   EL_CUSTOM_START + 196,
3076   EL_CUSTOM_START + 197,
3077   EL_CUSTOM_START + 198,
3078   EL_CUSTOM_START + 199,
3079
3080   EL_CUSTOM_START + 200,
3081   EL_CUSTOM_START + 201,
3082   EL_CUSTOM_START + 202,
3083   EL_CUSTOM_START + 203,
3084
3085   EL_CUSTOM_START + 204,
3086   EL_CUSTOM_START + 205,
3087   EL_CUSTOM_START + 206,
3088   EL_CUSTOM_START + 207,
3089
3090   EL_CUSTOM_START + 208,
3091   EL_CUSTOM_START + 209,
3092   EL_CUSTOM_START + 210,
3093   EL_CUSTOM_START + 211,
3094
3095   EL_CUSTOM_START + 212,
3096   EL_CUSTOM_START + 213,
3097   EL_CUSTOM_START + 214,
3098   EL_CUSTOM_START + 215,
3099
3100   EL_CUSTOM_START + 216,
3101   EL_CUSTOM_START + 217,
3102   EL_CUSTOM_START + 218,
3103   EL_CUSTOM_START + 219,
3104
3105   EL_CUSTOM_START + 220,
3106   EL_CUSTOM_START + 221,
3107   EL_CUSTOM_START + 222,
3108   EL_CUSTOM_START + 223,
3109
3110   EL_CUSTOM_START + 224,
3111   EL_CUSTOM_START + 225,
3112   EL_CUSTOM_START + 226,
3113   EL_CUSTOM_START + 227,
3114
3115   EL_CUSTOM_START + 228,
3116   EL_CUSTOM_START + 229,
3117   EL_CUSTOM_START + 230,
3118   EL_CUSTOM_START + 231,
3119
3120   EL_CUSTOM_START + 232,
3121   EL_CUSTOM_START + 233,
3122   EL_CUSTOM_START + 234,
3123   EL_CUSTOM_START + 235,
3124
3125   EL_CUSTOM_START + 236,
3126   EL_CUSTOM_START + 237,
3127   EL_CUSTOM_START + 238,
3128   EL_CUSTOM_START + 239,
3129
3130   EL_CUSTOM_START + 240,
3131   EL_CUSTOM_START + 241,
3132   EL_CUSTOM_START + 242,
3133   EL_CUSTOM_START + 243,
3134
3135   EL_CUSTOM_START + 244,
3136   EL_CUSTOM_START + 245,
3137   EL_CUSTOM_START + 246,
3138   EL_CUSTOM_START + 247,
3139
3140   EL_CUSTOM_START + 248,
3141   EL_CUSTOM_START + 249,
3142   EL_CUSTOM_START + 250,
3143   EL_CUSTOM_START + 251,
3144
3145   EL_CUSTOM_START + 252,
3146   EL_CUSTOM_START + 253,
3147   EL_CUSTOM_START + 254,
3148   EL_CUSTOM_START + 255
3149 };
3150 static int *editor_hl_custom_more_ptr = editor_hl_custom_more;
3151 static int *editor_el_custom_more_ptr = editor_el_custom_more;
3152 static int num_editor_hl_custom_more = SIZEOF_ARRAY_INT(editor_hl_custom_more);
3153 static int num_editor_el_custom_more = SIZEOF_ARRAY_INT(editor_el_custom_more);
3154
3155 static int editor_hl_group[] =
3156 {
3157   EL_CHAR('G'),
3158   EL_CHAR('R'),
3159   EL_CHAR('O'),
3160   EL_CHAR('U'),
3161
3162   EL_CHAR('P'),
3163   EL_CHAR(' '),
3164   EL_CHAR(' '),
3165   EL_CHAR(' '),
3166
3167   EL_CHAR('E'),
3168   EL_CHAR('L'),
3169   EL_CHAR('E'),
3170   EL_CHAR('M'),
3171
3172   EL_CHAR('E'),
3173   EL_CHAR('N'),
3174   EL_CHAR('T'),
3175   EL_CHAR('S'),
3176 };
3177
3178 static int editor_el_group[] =
3179 {
3180   EL_GROUP_START + 0,
3181   EL_GROUP_START + 1,
3182   EL_GROUP_START + 2,
3183   EL_GROUP_START + 3,
3184
3185   EL_GROUP_START + 4,
3186   EL_GROUP_START + 5,
3187   EL_GROUP_START + 6,
3188   EL_GROUP_START + 7,
3189
3190   EL_GROUP_START + 8,
3191   EL_GROUP_START + 9,
3192   EL_GROUP_START + 10,
3193   EL_GROUP_START + 11,
3194
3195   EL_GROUP_START + 12,
3196   EL_GROUP_START + 13,
3197   EL_GROUP_START + 14,
3198   EL_GROUP_START + 15
3199 };
3200 static int *editor_hl_group_ptr = editor_hl_group;
3201 static int *editor_el_group_ptr = editor_el_group;
3202 static int num_editor_hl_group = SIZEOF_ARRAY_INT(editor_hl_group);
3203 static int num_editor_el_group = SIZEOF_ARRAY_INT(editor_el_group);
3204
3205 static int editor_hl_group_more[] =
3206 {
3207 };
3208
3209 static int editor_el_group_more[] =
3210 {
3211   EL_GROUP_START + 16,
3212   EL_GROUP_START + 17,
3213   EL_GROUP_START + 18,
3214   EL_GROUP_START + 19,
3215
3216   EL_GROUP_START + 20,
3217   EL_GROUP_START + 21,
3218   EL_GROUP_START + 22,
3219   EL_GROUP_START + 23,
3220
3221   EL_GROUP_START + 24,
3222   EL_GROUP_START + 25,
3223   EL_GROUP_START + 26,
3224   EL_GROUP_START + 27,
3225
3226   EL_GROUP_START + 28,
3227   EL_GROUP_START + 29,
3228   EL_GROUP_START + 30,
3229   EL_GROUP_START + 31
3230 };
3231 static int *editor_hl_group_more_ptr = editor_hl_group_more;
3232 static int *editor_el_group_more_ptr = editor_el_group_more;
3233 static int num_editor_hl_group_more = SIZEOF_ARRAY_INT(editor_hl_group_more);
3234 static int num_editor_el_group_more = SIZEOF_ARRAY_INT(editor_el_group_more);
3235
3236 static int editor_hl_user_defined[] =
3237 {
3238   EL_CHAR('U'),
3239   EL_CHAR('S'),
3240   EL_CHAR('E'),
3241   EL_CHAR('R'),
3242
3243   EL_CHAR('D'),
3244   EL_CHAR('E'),
3245   EL_CHAR('F'),
3246   EL_CHAR('I'),
3247
3248   EL_CHAR('-'),
3249   EL_CHAR('N'),
3250   EL_CHAR('E'),
3251   EL_CHAR('D'),
3252 };
3253
3254 static int *editor_hl_user_defined_ptr = editor_hl_user_defined;
3255 static int *editor_el_user_defined_ptr = NULL;
3256 static int num_editor_hl_user_defined=SIZEOF_ARRAY_INT(editor_hl_user_defined);
3257 static int num_editor_el_user_defined = 0;
3258
3259 static int editor_hl_empty[] = { };
3260 static int editor_el_empty[ED_NUM_ELEMENTLIST_BUTTONS];
3261
3262 static int *editor_hl_empty_ptr = editor_hl_empty;
3263 static int *editor_el_empty_ptr = editor_el_empty;
3264 static int num_editor_hl_empty = 0;
3265 static int num_editor_el_empty = 0;     /* dynamically determined, if needed */
3266
3267 static boolean use_el_empty = FALSE;
3268
3269 static int *editor_elements = NULL;     /* dynamically allocated */
3270 static int num_editor_elements = 0;     /* dynamically determined */
3271
3272 static struct
3273 {
3274   boolean *setup_value;
3275
3276   int **headline_list;
3277   int *headline_list_size;
3278
3279   int **element_list;
3280   int *element_list_size;
3281
3282   boolean last_setup_value;
3283 }
3284 editor_elements_info[] =
3285 {
3286   {
3287     &setup.editor.el_boulderdash,
3288     &editor_hl_boulderdash_ptr,         &num_editor_hl_boulderdash,
3289     &editor_el_boulderdash_ptr,         &num_editor_el_boulderdash
3290   },
3291   {
3292     &setup.editor.el_emerald_mine,
3293     &editor_hl_emerald_mine_ptr,        &num_editor_hl_emerald_mine,
3294     &editor_el_emerald_mine_ptr,        &num_editor_el_emerald_mine
3295   },
3296   {
3297     &setup.editor.el_more,
3298     &editor_hl_more_ptr,                &num_editor_hl_more,
3299     &editor_el_more_ptr,                &num_editor_el_more
3300   },
3301   {
3302     &setup.editor.el_sokoban,
3303     &editor_hl_sokoban_ptr,             &num_editor_hl_sokoban,
3304     &editor_el_sokoban_ptr,             &num_editor_el_sokoban
3305   },
3306   {
3307     &setup.editor.el_supaplex,
3308     &editor_hl_supaplex_ptr,            &num_editor_hl_supaplex,
3309     &editor_el_supaplex_ptr,            &num_editor_el_supaplex
3310   },
3311   {
3312     &setup.editor.el_diamond_caves,
3313     &editor_hl_diamond_caves_ptr,       &num_editor_hl_diamond_caves,
3314     &editor_el_diamond_caves_ptr,       &num_editor_el_diamond_caves
3315   },
3316   {
3317     &setup.editor.el_dx_boulderdash,
3318     &editor_hl_dx_boulderdash_ptr,      &num_editor_hl_dx_boulderdash,
3319     &editor_el_dx_boulderdash_ptr,      &num_editor_el_dx_boulderdash
3320   },
3321   {
3322     &setup.editor.el_chars,
3323     &editor_hl_chars_ptr,               &num_editor_hl_chars,
3324     &editor_el_chars_ptr,               &num_editor_el_chars
3325   },
3326   {
3327     &setup.editor.el_custom,
3328     &editor_hl_custom_ptr,              &num_editor_hl_custom,
3329     &editor_el_custom_ptr,              &num_editor_el_custom
3330   },
3331   {
3332     &setup.editor.el_custom_more,
3333     &editor_hl_custom_more_ptr,         &num_editor_hl_custom_more,
3334     &editor_el_custom_more_ptr,         &num_editor_el_custom_more
3335   },
3336   {
3337     &setup.editor.el_custom,
3338     &editor_hl_group_ptr,               &num_editor_hl_group,
3339     &editor_el_group_ptr,               &num_editor_el_group
3340   },
3341   {
3342     &setup.editor.el_custom_more,
3343     &editor_hl_group_more_ptr,          &num_editor_hl_group_more,
3344     &editor_el_group_more_ptr,          &num_editor_el_group_more
3345   },
3346   {
3347     &setup.editor.el_user_defined,
3348     &editor_hl_user_defined_ptr,        &num_editor_hl_user_defined,
3349     &editor_el_user_defined_ptr,        &num_editor_el_user_defined
3350   },
3351   {
3352     &use_el_empty,
3353     &editor_hl_empty_ptr,               &num_editor_hl_empty,
3354     &editor_el_empty_ptr,               &num_editor_el_empty,
3355   },
3356   {
3357     NULL,
3358     NULL,                               NULL,
3359     NULL,                               NULL
3360   }
3361 };
3362
3363 static struct
3364 {
3365   short element;
3366   short element_mapped;
3367 }
3368 forum_sketch_element_mapping[] =
3369 {
3370   { EL_WALL_SLIPPERY,                   EL_WALL                 },
3371   { EL_EXPANDABLE_WALL,                 EL_WALL                 },
3372   { EL_EXPANDABLE_WALL_HORIZONTAL,      EL_WALL                 },
3373   { EL_EXPANDABLE_WALL_VERTICAL,        EL_WALL                 },
3374   { EL_EXPANDABLE_WALL_ANY,             EL_WALL                 },
3375   { EL_WALL_EMERALD,                    EL_WALL                 },
3376   { EL_WALL_DIAMOND,                    EL_WALL                 },
3377   { EL_WALL_BD_DIAMOND,                 EL_WALL                 },
3378   { EL_WALL_EMERALD_YELLOW,             EL_WALL                 },
3379   { EL_WALL_EMERALD_RED,                EL_WALL                 },
3380   { EL_WALL_EMERALD_PURPLE,             EL_WALL                 },
3381   { EL_ACID_POOL_TOPLEFT,               EL_WALL                 },
3382   { EL_ACID_POOL_TOPRIGHT,              EL_WALL                 },
3383   { EL_ACID_POOL_BOTTOMLEFT,            EL_WALL                 },
3384   { EL_ACID_POOL_BOTTOM,                EL_WALL                 },
3385   { EL_ACID_POOL_BOTTOMRIGHT,           EL_WALL                 },
3386   { EL_BD_WALL,                         EL_WALL                 },
3387
3388   { EL_BD_ROCK,                         EL_ROCK                 },
3389
3390   { EL_SP_ZONK,                         EL_ROCK                 },
3391   { EL_SP_BASE,                         EL_SAND                 },
3392   { EL_SP_MURPHY,                       EL_PLAYER_1             },
3393   { EL_SP_INFOTRON,                     EL_EMERALD              },
3394   { EL_SP_HARDWARE_GRAY,                EL_STEELWALL            },
3395   { EL_SP_EXIT_CLOSED,                  EL_EXIT_OPEN            },
3396   { EL_SP_DISK_ORANGE,                  EL_BOMB                 },
3397   { EL_SP_DISK_RED,                     EL_BOMB                 },
3398   { EL_SP_DISK_YELLOW,                  EL_BOMB                 },
3399   { EL_SP_PORT_RIGHT,                   EL_GATE_1_GRAY          },
3400   { EL_SP_PORT_DOWN,                    EL_GATE_1_GRAY          },
3401   { EL_SP_PORT_LEFT,                    EL_GATE_1_GRAY          },
3402   { EL_SP_PORT_UP,                      EL_GATE_1_GRAY          },
3403   { EL_SP_GRAVITY_PORT_RIGHT,           EL_GATE_1_GRAY          },
3404   { EL_SP_GRAVITY_PORT_DOWN,            EL_GATE_1_GRAY          },
3405   { EL_SP_GRAVITY_PORT_LEFT,            EL_GATE_1_GRAY          },
3406   { EL_SP_GRAVITY_PORT_UP,              EL_GATE_1_GRAY          },
3407   { EL_SP_PORT_VERTICAL,                EL_GATE_1_GRAY          },
3408   { EL_SP_PORT_HORIZONTAL,              EL_GATE_1_GRAY          },
3409   { EL_SP_PORT_ANY,                     EL_GATE_1_GRAY          },
3410   { EL_SP_SNIKSNAK,                     EL_BUG                  },
3411   { EL_SP_ELECTRON,                     EL_SPACESHIP            },
3412   { EL_SP_CHIP_SINGLE,                  EL_WALL                 },
3413   { EL_SP_CHIP_LEFT,                    EL_WALL                 },
3414   { EL_SP_CHIP_RIGHT,                   EL_WALL                 },
3415   { EL_SP_CHIP_TOP,                     EL_WALL                 },
3416   { EL_SP_CHIP_BOTTOM,                  EL_WALL                 },
3417   { EL_SP_HARDWARE_BASE_1,              EL_STEELWALL            },
3418   { EL_SP_HARDWARE_GREEN,               EL_STEELWALL            },
3419   { EL_SP_HARDWARE_BLUE,                EL_STEELWALL            },
3420   { EL_SP_HARDWARE_RED,                 EL_STEELWALL            },
3421   { EL_SP_HARDWARE_YELLOW,              EL_STEELWALL            },
3422   { EL_SP_HARDWARE_BASE_2,              EL_STEELWALL            },
3423   { EL_SP_HARDWARE_BASE_3,              EL_STEELWALL            },
3424   { EL_SP_HARDWARE_BASE_3,              EL_STEELWALL            },
3425   { EL_SP_HARDWARE_BASE_4,              EL_STEELWALL            },
3426   { EL_SP_HARDWARE_BASE_5,              EL_STEELWALL            },
3427   { EL_SP_HARDWARE_BASE_6,              EL_STEELWALL            },
3428
3429   { EL_STEELWALL_SLIPPERY,              EL_STEELWALL            },
3430
3431   { EL_EXIT_CLOSED,                     EL_EXIT_OPEN            },
3432
3433   { EL_DIAMOND,                         EL_EMERALD              },
3434   { EL_BD_DIAMOND,                      EL_EMERALD              },
3435   { EL_EMERALD_YELLOW,                  EL_EMERALD              },
3436   { EL_EMERALD_RED,                     EL_EMERALD              },
3437   { EL_EMERALD_PURPLE,                  EL_EMERALD              },
3438
3439   { EL_AMOEBA_DEAD,                     EL_AMOEBA               },
3440   { EL_AMOEBA_DROP,                     EL_AMOEBA               },
3441   { EL_AMOEBA_WET,                      EL_AMOEBA               },
3442   { EL_AMOEBA_DRY,                      EL_AMOEBA               },
3443   { EL_AMOEBA_FULL,                     EL_AMOEBA               },
3444   { EL_BD_AMOEBA,                       EL_AMOEBA               },
3445   { EL_AMOEBA_TO_DIAMOND,               EL_AMOEBA               },
3446
3447   { EL_DYNAMITE_ACTIVE,                 EL_DYNAMITE             },
3448
3449   { EL_GATE_2_GRAY,                     EL_GATE_1_GRAY          },
3450   { EL_GATE_3_GRAY,                     EL_GATE_1_GRAY          },
3451   { EL_GATE_4_GRAY,                     EL_GATE_1_GRAY          },
3452
3453   { EL_EM_KEY_1,                        EL_KEY_1                },
3454   { EL_EM_KEY_2,                        EL_KEY_2                },
3455   { EL_EM_KEY_3,                        EL_KEY_3                },
3456   { EL_EM_KEY_4,                        EL_KEY_4                },
3457
3458   { EL_EM_GATE_1,                       EL_GATE_1               },
3459   { EL_EM_GATE_2,                       EL_GATE_2               },
3460   { EL_EM_GATE_3,                       EL_GATE_3               },
3461   { EL_EM_GATE_4,                       EL_GATE_4               },
3462
3463   { EL_EM_GATE_1_GRAY,                  EL_GATE_1_GRAY          },
3464   { EL_EM_GATE_2_GRAY,                  EL_GATE_1_GRAY          },
3465   { EL_EM_GATE_3_GRAY,                  EL_GATE_1_GRAY          },
3466   { EL_EM_GATE_4_GRAY,                  EL_GATE_1_GRAY          },
3467
3468   { EL_INVISIBLE_SAND,                  EL_EMPTY                },
3469   { EL_INVISIBLE_WALL,                  EL_EMPTY                },
3470   { EL_INVISIBLE_STEELWALL,             EL_EMPTY                },
3471
3472   { EL_PACMAN,                          EL_YAMYAM               },
3473   { EL_DARK_YAMYAM,                     EL_YAMYAM               },
3474
3475   { EL_BD_MAGIC_WALL,                   EL_MAGIC_WALL           },
3476
3477   { EL_DYNABOMB_INCREASE_NUMBER,        EL_BOMB                 },
3478   { EL_DYNABOMB_INCREASE_SIZE,          EL_BOMB                 },
3479   { EL_DYNABOMB_INCREASE_POWER,         EL_BOMB                 },
3480   { EL_BLACK_ORB,                       EL_BOMB                 },
3481   { EL_DX_SUPABOMB,                     EL_BOMB                 },
3482
3483   { EL_SOKOBAN_OBJECT,                  EL_KEY_1                },
3484   { EL_SOKOBAN_FIELD_EMPTY,             EL_GATE_1_GRAY          },
3485   { EL_SOKOBAN_FIELD_FULL,              EL_GATE_1               },
3486   { EL_SOKOBAN_FIELD_PLAYER,            EL_PLAYER_1             },
3487
3488   { EL_BD_BUTTERFLY_RIGHT,              EL_BD_BUTTERFLY         },
3489   { EL_BD_BUTTERFLY_UP,                 EL_BD_BUTTERFLY         },
3490   { EL_BD_BUTTERFLY_LEFT,               EL_BD_BUTTERFLY         },
3491   { EL_BD_BUTTERFLY_DOWN,               EL_BD_BUTTERFLY         },
3492
3493   { EL_BD_FIREFLY_RIGHT,                EL_BD_FIREFLY           },
3494   { EL_BD_FIREFLY_UP,                   EL_BD_FIREFLY           },
3495   { EL_BD_FIREFLY_LEFT,                 EL_BD_FIREFLY           },
3496   { EL_BD_FIREFLY_DOWN,                 EL_BD_FIREFLY           },
3497
3498   { EL_PLAYER_2,                        EL_PLAYER_1             },
3499   { EL_PLAYER_3,                        EL_PLAYER_1             },
3500   { EL_PLAYER_4,                        EL_PLAYER_1             },
3501
3502   { EL_BUG_RIGHT,                       EL_BUG                  },
3503   { EL_BUG_UP,                          EL_BUG                  },
3504   { EL_BUG_LEFT,                        EL_BUG                  },
3505   { EL_BUG_DOWN,                        EL_BUG                  },
3506
3507   { EL_SPACESHIP_RIGHT,                 EL_SPACESHIP            },
3508   { EL_SPACESHIP_UP,                    EL_SPACESHIP            },
3509   { EL_SPACESHIP_LEFT,                  EL_SPACESHIP            },
3510   { EL_SPACESHIP_DOWN,                  EL_SPACESHIP            },
3511
3512   { EL_PACMAN_RIGHT,                    EL_YAMYAM               },
3513   { EL_PACMAN_UP,                       EL_YAMYAM               },
3514   { EL_PACMAN_LEFT,                     EL_YAMYAM               },
3515   { EL_PACMAN_DOWN,                     EL_YAMYAM               },
3516
3517   { -1,                                 -1                      }
3518 };
3519
3520 static struct
3521 {
3522   short element;
3523   char *element_string;
3524 }
3525 forum_sketch_element_strings[] =
3526 {
3527   { EL_PLAYER_1,                "p" },
3528   { EL_EMPTY,                   "_" },
3529   { EL_SAND,                    "s" },
3530   { EL_ROCK,                    "b" },
3531   { EL_EMERALD,                 "e" },
3532   { EL_BUG,                     "u" },
3533   { EL_SPACESHIP,               "h" },
3534   { EL_BD_FIREFLY,              "f" },
3535   { EL_BD_BUTTERFLY,            "b" },
3536   { EL_AMOEBA,                  "a" },
3537   { EL_YAMYAM,                  "y" },
3538   { EL_WALL,                    "w" },
3539   { EL_STEELWALL,               "t" },
3540   { EL_MAGIC_WALL,              "m" },
3541   { EL_EXIT_OPEN,               "x" },
3542   { EL_DYNAMITE,                "d" },
3543   { EL_KEY_1,                   "1" },
3544   { EL_KEY_2,                   "2" },
3545   { EL_KEY_3,                   "3" },
3546   { EL_KEY_4,                   "4" },
3547   { EL_GATE_1,                  "5" },
3548   { EL_GATE_2,                  "6" },
3549   { EL_GATE_3,                  "7" },
3550   { EL_GATE_4,                  "8" },
3551   { EL_GATE_1_GRAY,             "9" },
3552   { EL_BOMB,                    "@" },
3553   { EL_ROBOT,                   "r" },
3554   { EL_ROBOT_WHEEL,             "0" },
3555   { EL_NUT,                     "n" },
3556   { EL_CUSTOM_1,                "c" },
3557   { EL_CHAR_QUESTION,           "?" },
3558
3559   { -1,                         NULL }
3560 };
3561
3562
3563 /*
3564   -----------------------------------------------------------------------------
3565   functions
3566   -----------------------------------------------------------------------------
3567 */
3568
3569 static int getMaxInfoTextLength()
3570 {
3571   return (SXSIZE / getFontWidth(FONT_TEXT_2));
3572 }
3573
3574 static int getTextWidthForGadget(char *text)
3575 {
3576   if (text == NULL)
3577     return 0;
3578
3579   return (getTextWidth(text, FONT_TEXT_1) + ED_GADGET_TEXT_DISTANCE);
3580 }
3581
3582 static int getTextWidthForDrawingArea(char *text)
3583 {
3584   if (text == NULL)
3585     return 0;
3586
3587   return (getTextWidth(text, FONT_TEXT_1) + ED_DRAWINGAREA_TEXT_DISTANCE);
3588 }
3589
3590 static int getRightGadgetBorder(struct GadgetInfo *gi, char *text)
3591 {
3592   return (gi->x + gi->width + getTextWidthForGadget(text));
3593 }
3594
3595 static char *getElementInfoText(int element)
3596 {
3597   char *info_text = NULL;
3598
3599   if (element < NUM_FILE_ELEMENTS)
3600   {
3601     if (strlen(element_info[element].description) > 0)
3602       info_text = element_info[element].description;
3603     else if (element_info[element].custom_description != NULL)
3604       info_text = element_info[element].custom_description;
3605     else if (element_info[element].editor_description != NULL)
3606       info_text = element_info[element].editor_description;
3607   }
3608
3609   if (info_text == NULL)
3610     info_text = INFOTEXT_UNKNOWN_ELEMENT;
3611
3612   return info_text;
3613 }
3614
3615 static void ReinitializeElementList()
3616 {
3617   static boolean initialized = FALSE;
3618   int pos = 0;
3619   int i, j;
3620
3621   checked_free(editor_elements);
3622
3623   if (!initialized)
3624   {
3625     /* initialize optional user defined element list */
3626     LoadUserDefinedEditorElementList(&editor_el_user_defined_ptr,
3627                                      &num_editor_el_user_defined);
3628
3629     /* initialize list of empty elements (used for padding, if needed) */
3630     for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
3631       editor_el_empty[i] = EL_EMPTY;
3632
3633     /* do some sanity checks for each element from element list */
3634     for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
3635     {
3636       for (j = 0; j < *editor_elements_info[i].element_list_size; j++)
3637       {
3638         int element = (*editor_elements_info[i].element_list)[j];
3639
3640         if (element >= NUM_FILE_ELEMENTS)
3641           Error(ERR_WARN, "editor element %d is runtime element", element);
3642
3643         if (strcmp(getElementInfoText(element), INFOTEXT_UNKNOWN_ELEMENT) == 0)
3644           Error(ERR_WARN, "no element description for element %d", element);
3645       }
3646     }
3647
3648     initialized = TRUE;
3649   }
3650
3651   num_editor_elements = 0;
3652   use_el_empty = FALSE;
3653
3654   /* determine size of element list */
3655   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
3656   {
3657     if (*editor_elements_info[i].setup_value)
3658     {
3659       if (setup.editor.el_headlines)
3660         num_editor_elements += *editor_elements_info[i].headline_list_size;
3661
3662       num_editor_elements += *editor_elements_info[i].element_list_size;
3663     }
3664   }
3665
3666   if (num_editor_elements < ED_NUM_ELEMENTLIST_BUTTONS)
3667   {
3668     /* offer at least as many elements as element buttons exist */
3669     use_el_empty = TRUE;
3670     num_editor_el_empty = ED_NUM_ELEMENTLIST_BUTTONS - num_editor_elements;
3671
3672     num_editor_elements += num_editor_el_empty;
3673   }
3674
3675   editor_elements = checked_malloc(num_editor_elements * sizeof(int));
3676
3677   /* fill element list */
3678   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
3679   {
3680     if (*editor_elements_info[i].setup_value)
3681     {
3682       if (setup.editor.el_headlines)
3683         for (j = 0; j < *editor_elements_info[i].headline_list_size; j++)
3684           editor_elements[pos++] = (*editor_elements_info[i].headline_list)[j];
3685
3686       for (j = 0; j < *editor_elements_info[i].element_list_size; j++)
3687         editor_elements[pos++] = (*editor_elements_info[i].element_list)[j];
3688     }
3689   }
3690
3691   /* correct position of element list scrollbar */
3692   if (element_shift < 0)
3693     element_shift = 0;
3694   if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
3695     element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
3696 }
3697
3698 void PrintEditorElementList()
3699 {
3700   boolean *stop = &setup.editor.el_user_defined;
3701   int i, j;
3702
3703   for (i = 0; editor_elements_info[i].setup_value != stop; i++)
3704   {
3705     for (j = 0; j < *editor_elements_info[i].headline_list_size; j++)
3706     {
3707       int element = (*editor_elements_info[i].headline_list)[j];
3708
3709       printf("# %s\n", element_info[element].token_name);
3710     }
3711
3712     if (j > 0)
3713       printf("#\n");
3714
3715     for (j = 0; j < *editor_elements_info[i].element_list_size; j++)
3716     {
3717       int element = (*editor_elements_info[i].element_list)[j];
3718
3719       printf("# %s\n", element_info[element].token_name);
3720     }
3721
3722     if (j > 0)
3723       printf("#\n");
3724   }
3725 }
3726
3727 static void ReinitializeElementListButtons()
3728 {
3729   static boolean last_setup_value_headlines = FALSE;
3730   static boolean initialization_needed = TRUE;
3731   int i;
3732
3733   if (!initialization_needed)   /* check if editor element setup has changed */
3734   {
3735     if (last_setup_value_headlines != setup.editor.el_headlines)
3736       initialization_needed = TRUE;
3737
3738     for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
3739       if (editor_elements_info[i].last_setup_value !=
3740           *editor_elements_info[i].setup_value)
3741         initialization_needed = TRUE;
3742   }
3743
3744   if (!initialization_needed)
3745     return;
3746
3747   FreeLevelEditorGadgets();
3748   CreateLevelEditorGadgets();
3749
3750   /* store current setup values for next invocation of this function */
3751   last_setup_value_headlines = setup.editor.el_headlines;
3752   for (i = 0; editor_elements_info[i].setup_value != NULL; i++)
3753     editor_elements_info[i].last_setup_value =
3754       *editor_elements_info[i].setup_value;
3755
3756   initialization_needed = FALSE;
3757 }
3758
3759 static void DrawElementBorder(int dest_x, int dest_y, int width, int height,
3760                               boolean input)
3761 {
3762   int border_graphic =
3763     (input ? IMG_EDITOR_ELEMENT_BORDER_INPUT : IMG_EDITOR_ELEMENT_BORDER);
3764   Bitmap *src_bitmap;
3765   int src_x, src_y;
3766   int num_mini_tilex = width  / MINI_TILEX + 1;
3767   int num_mini_tiley = height / MINI_TILEY + 1;
3768   int x, y;
3769
3770   getMiniGraphicSource(border_graphic, &src_bitmap, &src_x, &src_y);
3771
3772   for (y = 0; y < num_mini_tiley; y++)
3773     for (x = 0; x < num_mini_tilex; x++)
3774       BlitBitmap(src_bitmap, drawto, src_x, src_y, MINI_TILEX, MINI_TILEY,
3775                  dest_x - MINI_TILEX / 2 + x * MINI_TILEX,
3776                  dest_y - MINI_TILEY / 2 + y * MINI_TILEY);
3777
3778   ClearRectangle(drawto, dest_x - 1, dest_y - 1, width + 2, height + 2);
3779 }
3780
3781 static void DrawDrawingArea(int id)
3782 {
3783   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
3784   int x, y;
3785
3786   if (id == ED_DRAWING_ID_RANDOM_BACKGROUND)
3787     DrawMiniGraphicExt(drawto, gi->x, gi->y,
3788                        el2edimg(random_placement_background_element));
3789   else if (id == ED_DRAWING_ID_AMOEBA_CONTENT)
3790     DrawMiniGraphicExt(drawto, gi->x, gi->y, el2edimg(level.amoeba_content));
3791   else if (id == ED_DRAWING_ID_CUSTOM_GRAPHIC)
3792     DrawMiniGraphicExt(drawto, gi->x, gi->y,
3793                        el2edimg(custom_element.gfx_element));
3794   else if (id == ED_DRAWING_ID_CUSTOM_CONTENT)
3795     for (y = 0; y < 3; y++)
3796       for (x = 0; x < 3; x++)
3797         DrawMiniGraphicExt(drawto,
3798                            gi->x + x * MINI_TILEX, gi->y + y * MINI_TILEY,
3799                            el2edimg(custom_element.content[x][y]));
3800   else if (id == ED_DRAWING_ID_CUSTOM_MOVE_ENTER)
3801     DrawMiniGraphicExt(drawto, gi->x, gi->y,
3802                        el2edimg(custom_element.move_enter_element));
3803   else if (id == ED_DRAWING_ID_CUSTOM_MOVE_LEAVE)
3804     DrawMiniGraphicExt(drawto, gi->x, gi->y,
3805                        el2edimg(custom_element.move_leave_element));
3806   else if (id == ED_DRAWING_ID_CUSTOM_CHANGE_TARGET)
3807     DrawMiniGraphicExt(drawto, gi->x, gi->y,
3808                        el2edimg(custom_element_change.target_element));
3809   else if (id == ED_DRAWING_ID_CUSTOM_CHANGE_CONTENT)
3810     for (y = 0; y < 3; y++)
3811       for (x = 0; x < 3; x++)
3812         DrawMiniGraphicExt(drawto,
3813                            gi->x + x * MINI_TILEX, gi->y + y * MINI_TILEY,
3814                            el2edimg(custom_element_change.content[x][y]));
3815   else if (id == ED_DRAWING_ID_CUSTOM_CHANGE_TRIGGER)
3816     DrawMiniGraphicExt(drawto, gi->x, gi->y,
3817                        el2edimg(custom_element_change.trigger_element));
3818   else if (id == ED_DRAWING_ID_GROUP_CONTENT)
3819     for (x = 0; x < group_element_info.num_elements; x++)
3820       DrawMiniGraphicExt(drawto, gi->x + x * MINI_TILEX, gi->y,
3821                          el2edimg(group_element_info.element[x]));
3822   else if (id >= ED_DRAWING_ID_ELEMENT_CONTENT_0 &&
3823            id <= ED_DRAWING_ID_ELEMENT_CONTENT_7)
3824   {
3825     int nr = id - ED_DRAWING_ID_ELEMENT_CONTENT_0;
3826
3827     for (y = 0; y < 3; y++)
3828       for (x = 0; x < 3; x++)
3829         DrawMiniGraphicExt(drawto,
3830                            gi->x + x * MINI_TILEX, gi->y + y * MINI_TILEY,
3831                            el2edimg(level.yamyam_content[nr][x][y]));
3832   }
3833 }
3834
3835 static void ScrollMiniLevel(int from_x, int from_y, int scroll)
3836 {
3837   int x,y;
3838   int dx = (scroll == ED_SCROLL_LEFT ? -1 : scroll == ED_SCROLL_RIGHT ? 1 : 0);
3839   int dy = (scroll == ED_SCROLL_UP   ? -1 : scroll == ED_SCROLL_DOWN  ? 1 : 0);
3840
3841   BlitBitmap(drawto, drawto,
3842              SX + (dx == -1 ? MINI_TILEX : 0),
3843              SY + (dy == -1 ? MINI_TILEY : 0),
3844              (ed_fieldx * MINI_TILEX) - (dx != 0 ? MINI_TILEX : 0),
3845              (ed_fieldy * MINI_TILEY) - (dy != 0 ? MINI_TILEY : 0),
3846              SX + (dx == +1 ? MINI_TILEX : 0),
3847              SY + (dy == +1 ? MINI_TILEY : 0));
3848   if (dx)
3849   {
3850     x = (dx == 1 ? 0 : ed_fieldx - 1);
3851     for (y = 0; y < ed_fieldy; y++)
3852       DrawMiniElementOrWall(x, y, from_x, from_y);
3853   }
3854   else if (dy)
3855   {
3856     y = (dy == 1 ? 0 : ed_fieldy - 1);
3857     for (x = 0; x < ed_fieldx; x++)
3858       DrawMiniElementOrWall(x, y, from_x, from_y);
3859   }
3860
3861   redraw_mask |= REDRAW_FIELD;
3862   BackToFront();
3863 }
3864
3865 static void CreateControlButtons()
3866 {
3867   Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
3868   struct GadgetInfo *gi;
3869   unsigned long event_mask;
3870   int i;
3871
3872   /* create toolbox buttons */
3873   for (i = 0; i < ED_NUM_CTRL_BUTTONS; i++)
3874   {
3875     int id = i;
3876     int width, height;
3877     int gd_xoffset, gd_yoffset;
3878     int gd_x1, gd_x2, gd_y1, gd_y2;
3879     int button_type;
3880     int radio_button_nr;
3881     boolean checked;
3882
3883     if (id == GADGET_ID_SINGLE_ITEMS ||
3884         id == GADGET_ID_CONNECTED_ITEMS ||
3885         id == GADGET_ID_LINE ||
3886         id == GADGET_ID_ARC ||
3887         id == GADGET_ID_TEXT ||
3888         id == GADGET_ID_RECTANGLE ||
3889         id == GADGET_ID_FILLED_BOX ||
3890         id == GADGET_ID_FLOOD_FILL ||
3891         id == GADGET_ID_GRAB_BRUSH ||
3892         id == GADGET_ID_PICK_ELEMENT ||
3893         id == GADGET_ID_CUSTOM_COPY_FROM ||
3894         id == GADGET_ID_CUSTOM_COPY_TO ||
3895         id == GADGET_ID_CUSTOM_EXCHANGE)
3896     {
3897       button_type = GD_TYPE_RADIO_BUTTON;
3898       radio_button_nr = RADIO_NR_DRAWING_TOOLBOX;
3899       checked = (id == drawing_function ? TRUE : FALSE);
3900       event_mask = GD_EVENT_PRESSED;
3901     }
3902     else
3903     {
3904       button_type = GD_TYPE_NORMAL_BUTTON;
3905       radio_button_nr = RADIO_NR_NONE;
3906       checked = FALSE;
3907
3908       if (id == GADGET_ID_WRAP_LEFT ||
3909           id == GADGET_ID_WRAP_RIGHT ||
3910           id == GADGET_ID_WRAP_UP ||
3911           id == GADGET_ID_WRAP_DOWN)
3912         event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
3913       else
3914         event_mask = GD_EVENT_RELEASED;
3915     }
3916
3917     if (id < ED_NUM_CTRL1_BUTTONS)
3918     {
3919       int x = i % ED_CTRL1_BUTTONS_HORIZ;
3920       int y = i / ED_CTRL1_BUTTONS_HORIZ;
3921
3922       gd_xoffset = ED_CTRL1_BUTTONS_XPOS + x * ED_CTRL1_BUTTON_XSIZE;
3923       gd_yoffset = ED_CTRL1_BUTTONS_YPOS + y * ED_CTRL1_BUTTON_YSIZE;
3924       width = ED_CTRL1_BUTTON_XSIZE;
3925       height = ED_CTRL1_BUTTON_YSIZE;
3926
3927       gd_x1 = DOOR_GFX_PAGEX8 + gd_xoffset;
3928       gd_x2 = DOOR_GFX_PAGEX7 + gd_xoffset;
3929       gd_y1 = DOOR_GFX_PAGEY1 + ED_CTRL1_BUTTONS_GFX_YPOS     + gd_yoffset;
3930       gd_y2 = DOOR_GFX_PAGEY1 + ED_CTRL1_BUTTONS_ALT_GFX_YPOS + gd_yoffset;
3931     }
3932     else if (id < ED_NUM_CTRL1_2_BUTTONS)
3933     {
3934       int x = (i - ED_NUM_CTRL1_BUTTONS) % ED_CTRL2_BUTTONS_HORIZ;
3935       int y = (i - ED_NUM_CTRL1_BUTTONS) / ED_CTRL2_BUTTONS_HORIZ;
3936
3937       gd_xoffset = ED_CTRL2_BUTTONS_XPOS + x * ED_CTRL2_BUTTON_XSIZE;
3938       gd_yoffset = ED_CTRL2_BUTTONS_YPOS + y * ED_CTRL2_BUTTON_YSIZE;
3939       width = ED_CTRL2_BUTTON_XSIZE;
3940       height = ED_CTRL2_BUTTON_YSIZE;
3941
3942       gd_x1 = DOOR_GFX_PAGEX8 + gd_xoffset;
3943       gd_x2 = DOOR_GFX_PAGEX7 + gd_xoffset;
3944       gd_y1 = DOOR_GFX_PAGEY1 + ED_CTRL2_BUTTONS_GFX_YPOS + gd_yoffset;
3945       gd_y2 = 0;        /* no alternative graphic for these buttons */
3946     }
3947     else
3948     {
3949       int x = (i - ED_NUM_CTRL1_2_BUTTONS) % ED_CTRL3_BUTTONS_HORIZ + 1;
3950       int y = (i - ED_NUM_CTRL1_2_BUTTONS) / ED_CTRL3_BUTTONS_HORIZ;
3951
3952       gd_xoffset = ED_CTRL3_BUTTONS_XPOS + x * ED_CTRL3_BUTTON_XSIZE;
3953       gd_yoffset = ED_CTRL3_BUTTONS_YPOS + y * ED_CTRL3_BUTTON_YSIZE;
3954       width = ED_CTRL3_BUTTON_XSIZE;
3955       height = ED_CTRL3_BUTTON_YSIZE;
3956
3957       gd_x1 = DOOR_GFX_PAGEX6 + gd_xoffset;
3958       gd_x2 = DOOR_GFX_PAGEX5 + gd_xoffset;
3959       gd_y1 = DOOR_GFX_PAGEY1 + ED_CTRL3_BUTTONS_GFX_YPOS     + gd_yoffset;
3960       gd_y2 = DOOR_GFX_PAGEY1 + ED_CTRL3_BUTTONS_ALT_GFX_YPOS + gd_yoffset;
3961     }
3962
3963     gi = CreateGadget(GDI_CUSTOM_ID, id,
3964                       GDI_CUSTOM_TYPE_ID, i,
3965                       GDI_INFO_TEXT, control_info[i].text,
3966                       GDI_X, EX + gd_xoffset,
3967                       GDI_Y, EY + gd_yoffset,
3968                       GDI_WIDTH, width,
3969                       GDI_HEIGHT, height,
3970                       GDI_TYPE, button_type,
3971                       GDI_STATE, GD_BUTTON_UNPRESSED,
3972                       GDI_RADIO_NR, radio_button_nr,
3973                       GDI_CHECKED, checked,
3974                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y1,
3975                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y1,
3976                       GDI_ALT_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y2,
3977                       GDI_ALT_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y2,
3978                       GDI_EVENT_MASK, event_mask,
3979                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
3980                       GDI_CALLBACK_ACTION, HandleControlButtons,
3981                       GDI_END);
3982
3983     if (gi == NULL)
3984       Error(ERR_EXIT, "cannot create gadget");
3985
3986     level_editor_gadget[id] = gi;
3987   }
3988
3989   /* create buttons for scrolling of drawing area and element list */
3990   for (i = 0; i < ED_NUM_SCROLLBUTTONS; i++)
3991   {
3992     int id = scrollbutton_info[i].gadget_id;
3993     int x, y, width, height;
3994     int gd_x1, gd_x2, gd_y1, gd_y2;
3995
3996     x = scrollbutton_info[i].x;
3997     y = scrollbutton_info[i].y;
3998
3999     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
4000
4001     if (id == GADGET_ID_SCROLL_LIST_UP ||
4002         id == GADGET_ID_SCROLL_LIST_DOWN)
4003     {
4004       x += DX;
4005       y += DY;
4006       width = ED_SCROLLBUTTON2_XSIZE;
4007       height = ED_SCROLLBUTTON2_YSIZE;
4008       gd_x1 = DOOR_GFX_PAGEX8 + scrollbutton_info[i].gd_x;
4009       gd_y1 = DOOR_GFX_PAGEY1 + scrollbutton_info[i].gd_y;
4010       gd_x2 = gd_x1 - ED_SCROLLBUTTON2_XSIZE;
4011       gd_y2 = gd_y1;
4012     }
4013     else
4014     {
4015       x += SX;
4016       y += SY;
4017       width = ED_SCROLLBUTTON_XSIZE;
4018       height = ED_SCROLLBUTTON_YSIZE;
4019       gd_x1 = DOOR_GFX_PAGEX8 + scrollbutton_info[i].gd_x;
4020       gd_y1 = DOOR_GFX_PAGEY1 + scrollbutton_info[i].gd_y;
4021       gd_x2 = gd_x1 - ED_SCROLLBUTTON_XSIZE;
4022       gd_y2 = gd_y1;
4023     }
4024
4025     gi = CreateGadget(GDI_CUSTOM_ID, id,
4026                       GDI_CUSTOM_TYPE_ID, i,
4027                       GDI_INFO_TEXT, scrollbutton_info[i].infotext,
4028                       GDI_X, x,
4029                       GDI_Y, y,
4030                       GDI_WIDTH, width,
4031                       GDI_HEIGHT, height,
4032                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
4033                       GDI_STATE, GD_BUTTON_UNPRESSED,
4034                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y1,
4035                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y2,
4036                       GDI_EVENT_MASK, event_mask,
4037                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
4038                       GDI_CALLBACK_ACTION, HandleControlButtons,
4039                       GDI_END);
4040
4041     if (gi == NULL)
4042       Error(ERR_EXIT, "cannot create gadget");
4043
4044     level_editor_gadget[id] = gi;
4045   }
4046
4047   /* create buttons for element list */
4048   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
4049   {
4050     Bitmap *deco_bitmap;
4051     int deco_x, deco_y, deco_xpos, deco_ypos;
4052     int gd_xoffset, gd_yoffset;
4053     int gd_x1, gd_x2, gd_y;
4054     int x = i % ED_ELEMENTLIST_BUTTONS_HORIZ;
4055     int y = i / ED_ELEMENTLIST_BUTTONS_HORIZ;
4056     int id = GADGET_ID_ELEMENTLIST_FIRST + i;
4057     int element = editor_elements[i];
4058
4059     event_mask = GD_EVENT_RELEASED;
4060
4061     gd_xoffset = ED_ELEMENTLIST_XPOS + x * ED_ELEMENTLIST_XSIZE;
4062     gd_yoffset = ED_ELEMENTLIST_YPOS + y * ED_ELEMENTLIST_YSIZE;
4063
4064     gd_x1 = DOOR_GFX_PAGEX6 + ED_ELEMENTLIST_XPOS + ED_ELEMENTLIST_XSIZE;
4065     gd_x2 = DOOR_GFX_PAGEX6 + ED_ELEMENTLIST_XPOS;
4066     gd_y  = DOOR_GFX_PAGEY1 + ED_ELEMENTLIST_YPOS;
4067
4068     getMiniGraphicSource(el2edimg(element), &deco_bitmap, &deco_x, &deco_y);
4069     deco_xpos = (ED_ELEMENTLIST_XSIZE - MINI_TILEX) / 2;
4070     deco_ypos = (ED_ELEMENTLIST_YSIZE - MINI_TILEY) / 2;
4071
4072     gi = CreateGadget(GDI_CUSTOM_ID, id,
4073                       GDI_CUSTOM_TYPE_ID, i,
4074                       GDI_INFO_TEXT, getElementInfoText(element),
4075                       GDI_X, DX + gd_xoffset,
4076                       GDI_Y, DY + gd_yoffset,
4077                       GDI_WIDTH, ED_ELEMENTLIST_XSIZE,
4078                       GDI_HEIGHT, ED_ELEMENTLIST_YSIZE,
4079                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
4080                       GDI_STATE, GD_BUTTON_UNPRESSED,
4081                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y,
4082                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y,
4083                       GDI_DECORATION_DESIGN, deco_bitmap, deco_x, deco_y,
4084                       GDI_DECORATION_POSITION, deco_xpos, deco_ypos,
4085                       GDI_DECORATION_SIZE, MINI_TILEX, MINI_TILEY,
4086                       GDI_DECORATION_SHIFTING, 1, 1,
4087                       GDI_EVENT_MASK, event_mask,
4088                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
4089                       GDI_CALLBACK_ACTION, HandleControlButtons,
4090                       GDI_END);
4091
4092     if (gi == NULL)
4093       Error(ERR_EXIT, "cannot create gadget");
4094
4095     level_editor_gadget[id] = gi;
4096   }
4097 }
4098
4099 static void CreateCounterButtons()
4100 {
4101   int max_infotext_len = getMaxInfoTextLength();
4102   int i;
4103
4104   for (i = 0; i < ED_NUM_COUNTERBUTTONS; i++)
4105   {
4106     int j;
4107     int x = SX + counterbutton_info[i].x;       /* down count button */
4108     int y = SY + counterbutton_info[i].y;
4109
4110     /* determine horizontal position to the right of specified gadget */
4111     if (counterbutton_info[i].gadget_id_align != GADGET_ID_NONE)
4112       x = (right_gadget_border[counterbutton_info[i].gadget_id_align] +
4113            ED_GADGET_TEXT_DISTANCE);
4114
4115     /* determine horizontal offset for leading text */
4116     if (counterbutton_info[i].text_left != NULL)
4117       x += getTextWidthForGadget(counterbutton_info[i].text_left);
4118
4119     for (j = 0; j < 2; j++)
4120     {
4121       Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
4122       struct GadgetInfo *gi;
4123       int id = (j == 0 ?
4124                 counterbutton_info[i].gadget_id_down :
4125                 counterbutton_info[i].gadget_id_up);
4126       int gd_xoffset;
4127       int gd_x, gd_x1, gd_x2, gd_y;
4128       int x_size, y_size;
4129       unsigned long event_mask;
4130       char infotext[max_infotext_len + 1];
4131
4132       event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
4133
4134       if (i == ED_COUNTER_ID_SELECT_LEVEL)
4135       {
4136         int sid = (j == 0 ?
4137                    ED_SCROLLBUTTON_ID_AREA_LEFT :
4138                    ED_SCROLLBUTTON_ID_AREA_RIGHT);
4139
4140         event_mask |= GD_EVENT_RELEASED;
4141
4142         if (j == 1)
4143           x += 2 * ED_GADGET_DISTANCE;
4144         y += ED_GADGET_DISTANCE;
4145
4146         gd_x1 = DOOR_GFX_PAGEX8 + scrollbutton_info[sid].gd_x;
4147         gd_x2 = gd_x1 - ED_SCROLLBUTTON_XSIZE;
4148         gd_y  = DOOR_GFX_PAGEY1 + scrollbutton_info[sid].gd_y;
4149         x_size = ED_SCROLLBUTTON_XSIZE;
4150         y_size = ED_SCROLLBUTTON_YSIZE;
4151       }
4152       else
4153       {
4154         gd_xoffset = (j == 0 ? ED_BUTTON_MINUS_XPOS : ED_BUTTON_PLUS_XPOS);
4155         gd_x1 = DOOR_GFX_PAGEX4 + gd_xoffset;
4156         gd_x2 = DOOR_GFX_PAGEX3 + gd_xoffset;
4157         gd_y  = DOOR_GFX_PAGEY1 + ED_BUTTON_COUNT_YPOS;
4158         x_size = ED_BUTTON_COUNT_XSIZE;
4159         y_size = ED_BUTTON_COUNT_YSIZE;
4160       }
4161
4162       sprintf(infotext, "%s counter value by 1, 5 or 10",
4163               (j == 0 ? "decrease" : "increase"));
4164
4165       gi = CreateGadget(GDI_CUSTOM_ID, id,
4166                         GDI_CUSTOM_TYPE_ID, i,
4167                         GDI_INFO_TEXT, infotext,
4168                         GDI_X, x,
4169                         GDI_Y, y,
4170                         GDI_WIDTH, x_size,
4171                         GDI_HEIGHT, y_size,
4172                         GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
4173                         GDI_STATE, GD_BUTTON_UNPRESSED,
4174                         GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y,
4175                         GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y,
4176                         GDI_EVENT_MASK, event_mask,
4177                         GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
4178                         GDI_CALLBACK_ACTION, HandleCounterButtons,
4179                         GDI_END);
4180
4181       if (gi == NULL)
4182         Error(ERR_EXIT, "cannot create gadget");
4183
4184       level_editor_gadget[id] = gi;
4185       right_gadget_border[id] =
4186         getRightGadgetBorder(gi, counterbutton_info[i].text_right);
4187
4188       x += gi->width + ED_GADGET_DISTANCE;      /* text count button */
4189
4190       if (j == 0)
4191       {
4192         int font_type = FONT_INPUT_1;
4193         int font_type_active = FONT_INPUT_1_ACTIVE;
4194         int gd_width = ED_WIN_COUNT_XSIZE;
4195
4196         id = counterbutton_info[i].gadget_id_text;
4197         event_mask = GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
4198
4199         if (i == ED_COUNTER_ID_SELECT_LEVEL)
4200         {
4201           font_type = FONT_LEVEL_NUMBER;
4202           font_type_active = FONT_LEVEL_NUMBER;
4203
4204           x += 2 * ED_GADGET_DISTANCE;
4205           y -= ED_GADGET_DISTANCE;
4206
4207           gd_x = DOOR_GFX_PAGEX6 + ED_WIN_COUNT2_XPOS;
4208           gd_y = DOOR_GFX_PAGEY1 + ED_WIN_COUNT2_YPOS;
4209           gd_width = ED_WIN_COUNT2_XSIZE;
4210         }
4211         else
4212         {
4213           gd_x = DOOR_GFX_PAGEX4 + ED_WIN_COUNT_XPOS;
4214           gd_y = DOOR_GFX_PAGEY1 + ED_WIN_COUNT_YPOS;
4215         }
4216
4217         gi = CreateGadget(GDI_CUSTOM_ID, id,
4218                           GDI_CUSTOM_TYPE_ID, i,
4219                           GDI_INFO_TEXT, "enter counter value",
4220                           GDI_X, x,
4221                           GDI_Y, y,
4222                           GDI_TYPE, GD_TYPE_TEXT_INPUT_NUMERIC,
4223                           GDI_NUMBER_VALUE, 0,
4224                           GDI_NUMBER_MIN, counterbutton_info[i].min_value,
4225                           GDI_NUMBER_MAX, counterbutton_info[i].max_value,
4226                           GDI_TEXT_SIZE, 3,
4227                           GDI_TEXT_FONT, font_type,
4228                           GDI_TEXT_FONT_ACTIVE, font_type_active,
4229                           GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x, gd_y,
4230                           GDI_DESIGN_PRESSED, gd_bitmap, gd_x, gd_y,
4231                           GDI_BORDER_SIZE, ED_BORDER_SIZE, ED_BORDER_SIZE,
4232                           GDI_DESIGN_WIDTH, gd_width,
4233                           GDI_EVENT_MASK, event_mask,
4234                           GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
4235                           GDI_CALLBACK_ACTION, HandleCounterButtons,
4236                           GDI_END);
4237
4238         if (gi == NULL)
4239           Error(ERR_EXIT, "cannot create gadget");
4240
4241         level_editor_gadget[id] = gi;
4242         right_gadget_border[id] =
4243           getRightGadgetBorder(gi, counterbutton_info[i].text_right);
4244
4245         x += gi->width + ED_GADGET_DISTANCE;    /* up count button */
4246       }
4247     }
4248   }
4249 }
4250
4251 static void CreateDrawingAreas()
4252 {
4253   int i;
4254
4255   for (i = 0; i < ED_NUM_DRAWING_AREAS; i++)
4256   {
4257     struct GadgetInfo *gi;
4258     unsigned long event_mask;
4259     int id = drawingarea_info[i].gadget_id;
4260     int x = SX + drawingarea_info[i].x;
4261     int y = SY + drawingarea_info[i].y;
4262     int area_xsize = drawingarea_info[i].area_xsize;
4263     int area_ysize = drawingarea_info[i].area_ysize;
4264
4265     event_mask =
4266       GD_EVENT_PRESSED | GD_EVENT_RELEASED | GD_EVENT_MOVING |
4267       GD_EVENT_OFF_BORDERS;
4268
4269     /* determine horizontal position to the right of specified gadget */
4270     if (drawingarea_info[i].gadget_id_align != GADGET_ID_NONE)
4271       x = (right_gadget_border[drawingarea_info[i].gadget_id_align] +
4272            ED_DRAWINGAREA_TEXT_DISTANCE);
4273
4274     /* determine horizontal offset for leading text */
4275     if (drawingarea_info[i].text_left != NULL)
4276       x += getTextWidthForDrawingArea(drawingarea_info[i].text_left);
4277
4278     gi = CreateGadget(GDI_CUSTOM_ID, id,
4279                       GDI_CUSTOM_TYPE_ID, i,
4280                       GDI_X, x,
4281                       GDI_Y, y,
4282                       GDI_TYPE, GD_TYPE_DRAWING_AREA,
4283                       GDI_AREA_SIZE, area_xsize, area_ysize,
4284                       GDI_ITEM_SIZE, MINI_TILEX, MINI_TILEY,
4285                       GDI_EVENT_MASK, event_mask,
4286                       GDI_CALLBACK_INFO, HandleDrawingAreaInfo,
4287                       GDI_CALLBACK_ACTION, HandleDrawingAreas,
4288                       GDI_END);
4289
4290     if (gi == NULL)
4291       Error(ERR_EXIT, "cannot create gadget");
4292
4293     level_editor_gadget[id] = gi;
4294     right_gadget_border[id] =
4295       getRightGadgetBorder(gi, drawingarea_info[i].text_right);
4296   }
4297 }
4298
4299 static void CreateTextInputGadgets()
4300 {
4301   int max_infotext_len = getMaxInfoTextLength();
4302   int i;
4303
4304   for (i = 0; i < ED_NUM_TEXTINPUT; i++)
4305   {
4306     Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
4307     int gd_x, gd_y;
4308     struct GadgetInfo *gi;
4309     unsigned long event_mask;
4310     char infotext[MAX_OUTPUT_LINESIZE + 1];
4311     int id = textinput_info[i].gadget_id;
4312
4313     event_mask = GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
4314
4315     gd_x = DOOR_GFX_PAGEX4 + ED_WIN_COUNT_XPOS;
4316     gd_y = DOOR_GFX_PAGEY1 + ED_WIN_COUNT_YPOS;
4317
4318     sprintf(infotext, "Enter %s", textinput_info[i].infotext);
4319     infotext[max_infotext_len] = '\0';
4320
4321     gi = CreateGadget(GDI_CUSTOM_ID, id,
4322                       GDI_CUSTOM_TYPE_ID, i,
4323                       GDI_INFO_TEXT, infotext,
4324                       GDI_X, SX + textinput_info[i].x,
4325                       GDI_Y, SY + textinput_info[i].y,
4326                       GDI_TYPE, GD_TYPE_TEXT_INPUT_ALPHANUMERIC,
4327                       GDI_TEXT_VALUE, textinput_info[i].value,
4328                       GDI_TEXT_SIZE, textinput_info[i].size,
4329                       GDI_TEXT_FONT, FONT_INPUT_1,
4330                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_1_ACTIVE,
4331                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x, gd_y,
4332                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x, gd_y,
4333                       GDI_BORDER_SIZE, ED_BORDER_SIZE, ED_BORDER_SIZE,
4334                       GDI_DESIGN_WIDTH, ED_WIN_COUNT_XSIZE,
4335                       GDI_EVENT_MASK, event_mask,
4336                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
4337                       GDI_CALLBACK_ACTION, HandleTextInputGadgets,
4338                       GDI_END);
4339
4340     if (gi == NULL)
4341       Error(ERR_EXIT, "cannot create gadget");
4342
4343     level_editor_gadget[id] = gi;
4344   }
4345 }
4346
4347 static void CreateTextAreaGadgets()
4348 {
4349   int max_infotext_len = getMaxInfoTextLength();
4350   int i;
4351
4352   for (i = 0; i < ED_NUM_TEXTAREAS; i++)
4353   {
4354     Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
4355     int gd_x, gd_y;
4356     struct GadgetInfo *gi;
4357     unsigned long event_mask;
4358     char infotext[MAX_OUTPUT_LINESIZE + 1];
4359     int id = textarea_info[i].gadget_id;
4360     int area_xsize = textarea_info[i].xsize;
4361     int area_ysize = textarea_info[i].ysize;
4362
4363     event_mask = GD_EVENT_TEXT_LEAVING;
4364
4365     gd_x = DOOR_GFX_PAGEX4 + ED_WIN_COUNT_XPOS;
4366     gd_y = DOOR_GFX_PAGEY1 + ED_WIN_COUNT_YPOS;
4367
4368     sprintf(infotext, "Enter %s", textarea_info[i].infotext);
4369     infotext[max_infotext_len] = '\0';
4370
4371     gi = CreateGadget(GDI_CUSTOM_ID, id,
4372                       GDI_CUSTOM_TYPE_ID, i,
4373                       GDI_INFO_TEXT, infotext,
4374                       GDI_X, SX + textarea_info[i].x,
4375                       GDI_Y, SY + textarea_info[i].y,
4376                       GDI_TYPE, GD_TYPE_TEXT_AREA,
4377                       GDI_AREA_SIZE, area_xsize, area_ysize,
4378                       GDI_TEXT_FONT, FONT_INPUT_1,
4379                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_1_ACTIVE,
4380                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x, gd_y,
4381                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x, gd_y,
4382                       GDI_BORDER_SIZE, ED_BORDER_SIZE, ED_BORDER_SIZE,
4383                       GDI_DESIGN_WIDTH, ED_WIN_COUNT_XSIZE,
4384                       GDI_EVENT_MASK, event_mask,
4385                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
4386                       GDI_CALLBACK_ACTION, HandleTextAreaGadgets,
4387                       GDI_END);
4388
4389     if (gi == NULL)
4390       Error(ERR_EXIT, "cannot create gadget");
4391
4392     level_editor_gadget[id] = gi;
4393   }
4394 }
4395
4396 static void CreateSelectboxGadgets()
4397 {
4398   int max_infotext_len = getMaxInfoTextLength();
4399   int i, j;
4400
4401   for (i = 0; i < ED_NUM_SELECTBOX; i++)
4402   {
4403     Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
4404     int gd_x, gd_y;
4405     struct GadgetInfo *gi;
4406     unsigned long event_mask;
4407     char infotext[MAX_OUTPUT_LINESIZE + 1];
4408     int id = selectbox_info[i].gadget_id;
4409     int x = SX + selectbox_info[i].x;
4410     int y = SY + selectbox_info[i].y;
4411
4412     if (selectbox_info[i].size == -1)   /* dynamically determine size */
4413     {
4414       /* (we cannot use -1 for uninitialized values if we directly compare
4415          with results from strlen(), because the '<' and '>' operation will
4416          implicitely cast -1 to an unsigned integer value!) */
4417       selectbox_info[i].size = 0;
4418
4419       for (j = 0; selectbox_info[i].options[j].text != NULL; j++)
4420         if (strlen(selectbox_info[i].options[j].text) > selectbox_info[i].size)
4421           selectbox_info[i].size = strlen(selectbox_info[i].options[j].text);
4422
4423       selectbox_info[i].size++;         /* add one character empty space */
4424     }
4425
4426     event_mask = GD_EVENT_RELEASED |
4427       GD_EVENT_TEXT_RETURN | GD_EVENT_TEXT_LEAVING;
4428
4429     gd_x = DOOR_GFX_PAGEX4 + ED_SELECTBOX_XPOS;
4430     gd_y = DOOR_GFX_PAGEY1 + ED_SELECTBOX_YPOS;
4431
4432     /* determine horizontal position to the right of specified gadget */
4433     if (selectbox_info[i].gadget_id_align != GADGET_ID_NONE)
4434       x = (right_gadget_border[selectbox_info[i].gadget_id_align] +
4435            ED_GADGET_TEXT_DISTANCE);
4436
4437     /* determine horizontal offset for leading text */
4438     if (selectbox_info[i].text_left != NULL)
4439       x += getTextWidthForGadget(selectbox_info[i].text_left);
4440
4441     sprintf(infotext, "Select %s", selectbox_info[i].infotext);
4442     infotext[max_infotext_len] = '\0';
4443
4444     gi = CreateGadget(GDI_CUSTOM_ID, id,
4445                       GDI_CUSTOM_TYPE_ID, i,
4446                       GDI_INFO_TEXT, infotext,
4447                       GDI_X, x,
4448                       GDI_Y, y,
4449                       GDI_TYPE, GD_TYPE_SELECTBOX,
4450                       GDI_SELECTBOX_OPTIONS, selectbox_info[i].options,
4451                       GDI_TEXT_SIZE, selectbox_info[i].size,
4452                       GDI_TEXT_FONT, FONT_INPUT_1,
4453                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_1_ACTIVE,
4454                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x, gd_y,
4455                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x, gd_y,
4456                       GDI_BORDER_SIZE, ED_BORDER_SIZE, ED_BORDER_SIZE,
4457                       GDI_BORDER_SIZE_SELECTBUTTON, ED_SELECTBOX_BUTTON_XSIZE,
4458                       GDI_DESIGN_WIDTH, ED_WIN_COUNT_XSIZE,
4459                       GDI_EVENT_MASK, event_mask,
4460                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
4461                       GDI_CALLBACK_ACTION, HandleSelectboxGadgets,
4462                       GDI_END);
4463
4464     if (gi == NULL)
4465       Error(ERR_EXIT, "cannot create gadget");
4466
4467     level_editor_gadget[id] = gi;
4468     right_gadget_border[id] =
4469       getRightGadgetBorder(gi, selectbox_info[i].text_right);
4470   }
4471 }
4472
4473 static void CreateTextbuttonGadgets()
4474 {
4475   int max_infotext_len = getMaxInfoTextLength();
4476   int i;
4477
4478   for (i = 0; i < ED_NUM_TEXTBUTTONS; i++)
4479   {
4480     Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
4481     int gd_x1, gd_x2, gd_y1, gd_y2;
4482     struct GadgetInfo *gi;
4483     unsigned long event_mask;
4484     char infotext[MAX_OUTPUT_LINESIZE + 1];
4485     int id = textbutton_info[i].gadget_id;
4486     int x = SX + textbutton_info[i].x;
4487     int y = SY + textbutton_info[i].y;
4488
4489     if (textbutton_info[i].size == -1)  /* dynamically determine size */
4490       textbutton_info[i].size = strlen(textbutton_info[i].text);
4491
4492     event_mask = GD_EVENT_RELEASED;
4493
4494     if (id >= GADGET_ID_PROPERTIES_INFO && id <= GADGET_ID_PROPERTIES_CHANGE)
4495     {
4496       gd_x1 = DOOR_GFX_PAGEX4 + ED_TEXTBUTTON_TAB_XPOS;
4497       gd_x2 = DOOR_GFX_PAGEX3 + ED_TEXTBUTTON_TAB_XPOS;
4498       gd_y1 = DOOR_GFX_PAGEY1 + ED_TEXTBUTTON_TAB_YPOS;
4499       gd_y2 = DOOR_GFX_PAGEY1 + ED_TEXTBUTTON_TAB_INACTIVE_YPOS;
4500     }
4501     else
4502     {
4503       gd_x1 = DOOR_GFX_PAGEX4 + ED_TEXTBUTTON_XPOS;
4504       gd_x2 = DOOR_GFX_PAGEX3 + ED_TEXTBUTTON_XPOS;
4505       gd_y1 = DOOR_GFX_PAGEY1 + ED_TEXTBUTTON_YPOS;
4506       gd_y2 = DOOR_GFX_PAGEY1 + ED_TEXTBUTTON_INACTIVE_YPOS;
4507     }
4508
4509     sprintf(infotext, "%s", textbutton_info[i].infotext);
4510     infotext[max_infotext_len] = '\0';
4511
4512     /* determine horizontal position to the right of specified gadget */
4513     if (textbutton_info[i].gadget_id_align != GADGET_ID_NONE)
4514       x = (right_gadget_border[textbutton_info[i].gadget_id_align] +
4515            ED_GADGET_TEXT_DISTANCE);
4516
4517     /* determine horizontal offset for leading text */
4518     if (textbutton_info[i].text_left != NULL)
4519       x += getTextWidthForGadget(textbutton_info[i].text_left);
4520
4521     gi = CreateGadget(GDI_CUSTOM_ID, id,
4522                       GDI_CUSTOM_TYPE_ID, i,
4523                       GDI_INFO_TEXT, infotext,
4524                       GDI_X, x,
4525                       GDI_Y, y,
4526                       GDI_TYPE, GD_TYPE_TEXT_BUTTON,
4527                       GDI_TEXT_VALUE, textbutton_info[i].text,
4528                       GDI_TEXT_SIZE, textbutton_info[i].size,
4529                       GDI_TEXT_FONT, FONT_INPUT_2_ACTIVE,
4530                       GDI_TEXT_FONT_ACTIVE, FONT_INPUT_2,
4531                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y1,
4532                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y1,
4533                       GDI_ALT_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y2,
4534                       GDI_BORDER_SIZE, ED_BORDER_TEXT_XSIZE, ED_BORDER_SIZE,
4535                       GDI_DESIGN_WIDTH, ED_WIN_COUNT_XSIZE,
4536                       GDI_DECORATION_SHIFTING, 1, 1,
4537                       GDI_EVENT_MASK, event_mask,
4538                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
4539                       GDI_CALLBACK_ACTION, HandleTextbuttonGadgets,
4540                       GDI_END);
4541
4542     if (gi == NULL)
4543       Error(ERR_EXIT, "cannot create gadget");
4544
4545     level_editor_gadget[id] = gi;
4546     right_gadget_border[id] =
4547       getRightGadgetBorder(gi, textbutton_info[i].text_right);
4548   }
4549 }
4550
4551 static void CreateGraphicbuttonGadgets()
4552 {
4553   Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
4554   struct GadgetInfo *gi;
4555   unsigned long event_mask;
4556   int i;
4557
4558   /* create buttons for scrolling of drawing area and element list */
4559   for (i = 0; i < ED_NUM_GRAPHICBUTTONS; i++)
4560   {
4561     int id = graphicbutton_info[i].gadget_id;
4562     int gd_x1, gd_x2, gd_y1, gd_y2;
4563     int x = SX + graphicbutton_info[i].x;
4564     int y = SY + graphicbutton_info[i].y;
4565
4566     event_mask = GD_EVENT_PRESSED | GD_EVENT_REPEATED;
4567
4568     gd_x1 = DOOR_GFX_PAGEX4 + graphicbutton_info[i].gd_x;
4569     gd_x2 = DOOR_GFX_PAGEX3 + graphicbutton_info[i].gd_x;
4570     gd_y1 = DOOR_GFX_PAGEY1 + graphicbutton_info[i].gd_y;
4571     gd_y2 = gd_y1;
4572
4573     /* determine horizontal position to the right of specified gadget */
4574     if (graphicbutton_info[i].gadget_id_align != GADGET_ID_NONE)
4575       x = (right_gadget_border[graphicbutton_info[i].gadget_id_align] +
4576            ED_GADGET_TEXT_DISTANCE);
4577
4578     /* determine horizontal offset for leading text */
4579     if (graphicbutton_info[i].text_left != NULL)
4580       x += getTextWidthForGadget(graphicbutton_info[i].text_left);
4581
4582     gi = CreateGadget(GDI_CUSTOM_ID, id,
4583                       GDI_CUSTOM_TYPE_ID, i,
4584                       GDI_INFO_TEXT, graphicbutton_info[i].infotext,
4585                       GDI_X, x,
4586                       GDI_Y, y,
4587                       GDI_WIDTH, graphicbutton_info[i].width,
4588                       GDI_HEIGHT, graphicbutton_info[i].height,
4589                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
4590                       GDI_STATE, GD_BUTTON_UNPRESSED,
4591                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y1,
4592                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y2,
4593                       GDI_EVENT_MASK, event_mask,
4594                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
4595                       GDI_CALLBACK_ACTION, HandleGraphicbuttonGadgets,
4596                       GDI_END);
4597
4598     if (gi == NULL)
4599       Error(ERR_EXIT, "cannot create gadget");
4600
4601     level_editor_gadget[id] = gi;
4602     right_gadget_border[id] =
4603       getRightGadgetBorder(gi, graphicbutton_info[i].text_right);
4604   }
4605 }
4606
4607 static void CreateScrollbarGadgets()
4608 {
4609   int i;
4610
4611   for (i = 0; i < ED_NUM_SCROLLBARS; i++)
4612   {
4613     int id = scrollbar_info[i].gadget_id;
4614     Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
4615     int gd_x1, gd_x2, gd_y1, gd_y2;
4616     struct GadgetInfo *gi;
4617     int items_max, items_visible, item_position;
4618     unsigned long event_mask;
4619
4620     if (i == ED_SCROLLBAR_ID_LIST_VERTICAL)
4621     {
4622       items_max = num_editor_elements / ED_ELEMENTLIST_BUTTONS_HORIZ;
4623       items_visible = ED_ELEMENTLIST_BUTTONS_VERT;
4624       item_position = element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ;
4625     }
4626     else        /* drawing area scrollbars */
4627     {
4628       if (scrollbar_info[i].type == GD_TYPE_SCROLLBAR_HORIZONTAL)
4629       {
4630         items_max = MAX(lev_fieldx + 2, ed_fieldx);
4631         items_visible = ed_fieldx;
4632         item_position = 0;
4633       }
4634       else
4635       {
4636         items_max = MAX(lev_fieldy + 2, ed_fieldy);
4637         items_visible = ed_fieldy;
4638         item_position = 0;
4639       }
4640     }
4641
4642     event_mask = GD_EVENT_MOVING | GD_EVENT_OFF_BORDERS;
4643
4644     gd_x1 = DOOR_GFX_PAGEX8 + scrollbar_info[i].gd_x;
4645     gd_x2 = (gd_x1 - (scrollbar_info[i].type == GD_TYPE_SCROLLBAR_HORIZONTAL ?
4646                       scrollbar_info[i].height : scrollbar_info[i].width));
4647     gd_y1 = DOOR_GFX_PAGEY1 + scrollbar_info[i].gd_y;
4648     gd_y2 = DOOR_GFX_PAGEY1 + scrollbar_info[i].gd_y;
4649
4650     gi = CreateGadget(GDI_CUSTOM_ID, id,
4651                       GDI_CUSTOM_TYPE_ID, i,
4652                       GDI_INFO_TEXT, scrollbar_info[i].infotext,
4653                       GDI_X, scrollbar_info[i].x,
4654                       GDI_Y, scrollbar_info[i].y,
4655                       GDI_WIDTH, scrollbar_info[i].width,
4656                       GDI_HEIGHT, scrollbar_info[i].height,
4657                       GDI_TYPE, scrollbar_info[i].type,
4658                       GDI_SCROLLBAR_ITEMS_MAX, items_max,
4659                       GDI_SCROLLBAR_ITEMS_VISIBLE, items_visible,
4660                       GDI_SCROLLBAR_ITEM_POSITION, item_position,
4661                       GDI_STATE, GD_BUTTON_UNPRESSED,
4662                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y1,
4663                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y2,
4664                       GDI_BORDER_SIZE, ED_BORDER_SIZE, ED_BORDER_SIZE,
4665                       GDI_EVENT_MASK, event_mask,
4666                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
4667                       GDI_CALLBACK_ACTION, HandleControlButtons,
4668                       GDI_END);
4669
4670     if (gi == NULL)
4671       Error(ERR_EXIT, "cannot create gadget");
4672
4673     level_editor_gadget[id] = gi;
4674   }
4675 }
4676
4677 static void CreateCheckbuttonGadgets()
4678 {
4679   Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
4680   struct GadgetInfo *gi;
4681   unsigned long event_mask;
4682   int gd_x1, gd_x2, gd_x3, gd_x4, gd_y;
4683   int i;
4684
4685   event_mask = GD_EVENT_PRESSED;
4686
4687   gd_x1 = DOOR_GFX_PAGEX4 + ED_CHECKBUTTON_UNCHECKED_XPOS;
4688   gd_x2 = DOOR_GFX_PAGEX3 + ED_CHECKBUTTON_UNCHECKED_XPOS;
4689   gd_x3 = DOOR_GFX_PAGEX4 + ED_CHECKBUTTON_CHECKED_XPOS;
4690   gd_x4 = DOOR_GFX_PAGEX3 + ED_CHECKBUTTON_CHECKED_XPOS;
4691   gd_y  = DOOR_GFX_PAGEY1 + ED_RADIOBUTTON_YPOS;
4692
4693   for (i = 0; i < ED_NUM_CHECKBUTTONS; i++)
4694   {
4695     int id = checkbutton_info[i].gadget_id;
4696     int x = SX + checkbutton_info[i].x;
4697     int y = SY + checkbutton_info[i].y;
4698
4699     if (id == GADGET_ID_STICK_ELEMENT)
4700       gd_y  = DOOR_GFX_PAGEY1 + ED_STICKYBUTTON_YPOS;
4701     else
4702       gd_y  = DOOR_GFX_PAGEY1 + ED_CHECKBUTTON_YPOS;
4703
4704     /* determine horizontal position to the right of specified gadget */
4705     if (checkbutton_info[i].gadget_id_align != GADGET_ID_NONE)
4706       x = (right_gadget_border[checkbutton_info[i].gadget_id_align] +
4707            ED_GADGET_TEXT_DISTANCE);
4708
4709     /* determine horizontal offset for leading text */
4710     if (checkbutton_info[i].text_left != NULL)
4711       x += getTextWidthForGadget(checkbutton_info[i].text_left);
4712
4713     gi = CreateGadget(GDI_CUSTOM_ID, id,
4714                       GDI_CUSTOM_TYPE_ID, i,
4715                       GDI_INFO_TEXT, checkbutton_info[i].infotext,
4716                       GDI_X, x,
4717                       GDI_Y, y,
4718                       GDI_WIDTH, ED_CHECKBUTTON_XSIZE,
4719                       GDI_HEIGHT, ED_CHECKBUTTON_YSIZE,
4720                       GDI_TYPE, GD_TYPE_CHECK_BUTTON,
4721                       GDI_CHECKED, *checkbutton_info[i].value,
4722                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y,
4723                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y,
4724                       GDI_ALT_DESIGN_UNPRESSED, gd_bitmap, gd_x3, gd_y,
4725                       GDI_ALT_DESIGN_PRESSED, gd_bitmap, gd_x4, gd_y,
4726                       GDI_EVENT_MASK, event_mask,
4727                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
4728                       GDI_CALLBACK_ACTION, HandleCheckbuttons,
4729                       GDI_END);
4730
4731     if (gi == NULL)
4732       Error(ERR_EXIT, "cannot create gadget");
4733
4734     level_editor_gadget[id] = gi;
4735     right_gadget_border[id] =
4736       getRightGadgetBorder(gi, checkbutton_info[i].text_right);
4737   }
4738 }
4739
4740 static void CreateRadiobuttonGadgets()
4741 {
4742   Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
4743   struct GadgetInfo *gi;
4744   unsigned long event_mask;
4745   int gd_x1, gd_x2, gd_x3, gd_x4, gd_y;
4746   int i;
4747
4748   event_mask = GD_EVENT_PRESSED;
4749
4750   gd_x1 = DOOR_GFX_PAGEX4 + ED_CHECKBUTTON_UNCHECKED_XPOS;
4751   gd_x2 = DOOR_GFX_PAGEX3 + ED_CHECKBUTTON_UNCHECKED_XPOS;
4752   gd_x3 = DOOR_GFX_PAGEX4 + ED_CHECKBUTTON_CHECKED_XPOS;
4753   gd_x4 = DOOR_GFX_PAGEX3 + ED_CHECKBUTTON_CHECKED_XPOS;
4754   gd_y  = DOOR_GFX_PAGEY1 + ED_RADIOBUTTON_YPOS;
4755
4756   for (i = 0; i < ED_NUM_RADIOBUTTONS; i++)
4757   {
4758     int id = radiobutton_info[i].gadget_id;
4759     int x = SX + radiobutton_info[i].x;
4760     int y = SY + radiobutton_info[i].y;
4761
4762     int checked =
4763       (*radiobutton_info[i].value == radiobutton_info[i].checked_value);
4764
4765     /* determine horizontal position to the right of specified gadget */
4766     if (radiobutton_info[i].gadget_id_align != GADGET_ID_NONE)
4767       x = (right_gadget_border[radiobutton_info[i].gadget_id_align] +
4768            ED_GADGET_TEXT_DISTANCE);
4769
4770     /* determine horizontal offset for leading text */
4771     if (radiobutton_info[i].text_left != NULL)
4772       x += getTextWidthForGadget(radiobutton_info[i].text_left);
4773
4774     gi = CreateGadget(GDI_CUSTOM_ID, id,
4775                       GDI_CUSTOM_TYPE_ID, i,
4776                       GDI_INFO_TEXT, radiobutton_info[i].infotext,
4777                       GDI_X, x,
4778                       GDI_Y, y,
4779                       GDI_WIDTH, ED_CHECKBUTTON_XSIZE,
4780                       GDI_HEIGHT, ED_CHECKBUTTON_YSIZE,
4781                       GDI_TYPE, GD_TYPE_RADIO_BUTTON,
4782                       GDI_RADIO_NR, radiobutton_info[i].radio_button_nr,
4783                       GDI_CHECKED, checked,
4784                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y,
4785                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y,
4786                       GDI_ALT_DESIGN_UNPRESSED, gd_bitmap, gd_x3, gd_y,
4787                       GDI_ALT_DESIGN_PRESSED, gd_bitmap, gd_x4, gd_y,
4788                       GDI_EVENT_MASK, event_mask,
4789                       GDI_CALLBACK_INFO, HandleEditorGadgetInfoText,
4790                       GDI_CALLBACK_ACTION, HandleRadiobuttons,
4791                       GDI_END);
4792
4793     if (gi == NULL)
4794       Error(ERR_EXIT, "cannot create gadget");
4795
4796     level_editor_gadget[id] = gi;
4797     right_gadget_border[id] =
4798       getRightGadgetBorder(gi, radiobutton_info[i].text_right);
4799   }
4800 }
4801
4802 void CreateLevelEditorGadgets()
4803 {
4804   int old_game_status = game_status;
4805
4806   /* setting 'game_status' is needed to get the right fonts for the editor */
4807   game_status = GAME_MODE_EDITOR;
4808
4809   ReinitializeElementList();
4810
4811   CreateControlButtons();
4812   CreateScrollbarGadgets();
4813
4814   /* order of function calls is important because of cross-references */
4815   CreateCheckbuttonGadgets();
4816   CreateCounterButtons();
4817   CreateRadiobuttonGadgets();
4818   CreateTextInputGadgets();
4819   CreateTextAreaGadgets();
4820   CreateSelectboxGadgets();
4821   CreateGraphicbuttonGadgets();
4822   CreateTextbuttonGadgets();
4823   CreateDrawingAreas();
4824
4825   game_status = old_game_status;
4826 }
4827
4828 void FreeLevelEditorGadgets()
4829 {
4830   int i;
4831
4832   for (i = 0; i < NUM_EDITOR_GADGETS; i++)
4833     FreeGadget(level_editor_gadget[i]);
4834 }
4835
4836 static void MapCounterButtons(int id)
4837 {
4838   int gadget_id_down = counterbutton_info[id].gadget_id_down;
4839   int gadget_id_text = counterbutton_info[id].gadget_id_text;
4840   int gadget_id_up   = counterbutton_info[id].gadget_id_up;
4841   struct GadgetInfo *gi_down = level_editor_gadget[gadget_id_down];
4842   struct GadgetInfo *gi_text = level_editor_gadget[gadget_id_text];
4843   struct GadgetInfo *gi_up   = level_editor_gadget[gadget_id_up];
4844 #if 0
4845   char infotext[MAX_OUTPUT_LINESIZE + 1];
4846   int max_infotext_len = getMaxInfoTextLength();
4847   int xoffset_left = 0;
4848   int yoffset_left = ED_BORDER_SIZE;
4849   int xoffset_right = getCounterGadgetWidth();
4850   int yoffset_right = ED_BORDER_SIZE;
4851 #else
4852   int xoffset_left = getTextWidthForGadget(counterbutton_info[id].text_left);
4853   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
4854   int yoffset_above = MINI_TILEX + ED_GADGET_DISTANCE;
4855   int yoffset = ED_BORDER_SIZE;
4856   int x_left = gi_down->x - xoffset_left;
4857   int x_right = gi_up->x + gi_up->width + xoffset_right;
4858   int y_above = gi_down->y - yoffset_above;
4859   int x = gi_down->x;
4860   int y = gi_up->y + yoffset;
4861 #endif
4862
4863 #if 1
4864   /* special case needed for "score" counter gadget */
4865   if (id == ED_COUNTER_ID_ELEMENT_SCORE)
4866   {
4867     ModifyGadget(gi_down, GDI_Y, SY + counterbutton_info[id].y, GDI_END);
4868     ModifyGadget(gi_text, GDI_Y, SY + counterbutton_info[id].y, GDI_END);
4869     ModifyGadget(gi_up,   GDI_Y, SY + counterbutton_info[id].y, GDI_END);
4870     y = gi_up->y + yoffset;
4871   }
4872 #endif
4873
4874   if (counterbutton_info[id].text_above)
4875     DrawText(x, y_above, counterbutton_info[id].text_above, FONT_TEXT_1);
4876
4877   if (counterbutton_info[id].text_left)
4878     DrawText(x_left, y, counterbutton_info[id].text_left, FONT_TEXT_1);
4879
4880   if (counterbutton_info[id].text_right)
4881     DrawText(x_right, y, counterbutton_info[id].text_right, FONT_TEXT_1);
4882
4883   ModifyEditorCounter(id, *counterbutton_info[id].value);
4884
4885   MapGadget(gi_down);
4886   MapGadget(gi_text);
4887   MapGadget(gi_up);
4888 }
4889
4890 static void MapControlButtons()
4891 {
4892   int counter_id;
4893   int i;
4894
4895   /* map toolbox buttons (excluding special CE toolbox buttons) */
4896   for (i = 0; i < ED_NUM_CTRL1_2_BUTTONS; i++)
4897     MapGadget(level_editor_gadget[i]);
4898
4899   /* map buttons to select elements */
4900   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
4901     MapGadget(level_editor_gadget[GADGET_ID_ELEMENTLIST_FIRST + i]);
4902   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL]);
4903   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_UP]);
4904   MapGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_DOWN]);
4905
4906   /* map buttons to select level */
4907   counter_id = ED_COUNTER_ID_SELECT_LEVEL;
4908   ModifyEditorCounterLimits(counter_id,
4909                             leveldir_current->first_level,
4910                             leveldir_current->last_level);
4911   MapCounterButtons(counter_id);
4912 }
4913
4914 static void MapDrawingArea(int id)
4915 {
4916   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
4917   int area_xsize = gi->drawing.area_xsize;
4918   int area_ysize = gi->drawing.area_ysize;
4919   int xoffset_left= getTextWidthForDrawingArea(drawingarea_info[id].text_left);
4920   int xoffset_below= getTextWidth(drawingarea_info[id].text_below,FONT_TEXT_1);
4921   int x_left  = gi->x - xoffset_left;
4922   int x_right = gi->x + gi->width + ED_DRAWINGAREA_TEXT_DISTANCE;
4923   int x_below = gi->x + (gi->width - xoffset_below) / 2;
4924   int y_side  = gi->y + (gi->height - getFontHeight(FONT_TEXT_1)) / 2;
4925   int y_below = gi->y + gi->height + ED_DRAWINGAREA_TEXT_DISTANCE;
4926
4927   if (drawingarea_info[id].text_left)
4928     DrawText(x_left, y_side, drawingarea_info[id].text_left, FONT_TEXT_1);
4929
4930   if (drawingarea_info[id].text_right)
4931     DrawText(x_right, y_side, drawingarea_info[id].text_right, FONT_TEXT_1);
4932
4933   if (drawingarea_info[id].text_below)
4934     DrawText(x_below, y_below, drawingarea_info[id].text_below, FONT_TEXT_1);
4935
4936   if (id != ED_DRAWING_ID_DRAWING_LEVEL)
4937   {
4938     DrawElementBorder(gi->x, gi->y,
4939                       area_xsize * MINI_TILEX, area_ysize * MINI_TILEY, TRUE);
4940
4941     DrawDrawingArea(id);
4942   }
4943
4944   MapGadget(gi);
4945 }
4946
4947 static void MapTextInputGadget(int id)
4948 {
4949   struct GadgetInfo *gi = level_editor_gadget[textinput_info[id].gadget_id];
4950   int xoffset_above = 0;
4951   int yoffset_above = -(MINI_TILEX + ED_GADGET_DISTANCE);
4952   int x_above = textinput_info[id].x + xoffset_above;
4953   int y_above = textinput_info[id].y + yoffset_above;
4954
4955   if (textinput_info[id].text_above)
4956     DrawTextS(x_above, y_above, FONT_TEXT_1, textinput_info[id].text_above);
4957
4958   ModifyGadget(gi, GDI_TEXT_VALUE, textinput_info[id].value, GDI_END);
4959
4960   MapGadget(gi);
4961 }
4962
4963 static void MapTextAreaGadget(int id)
4964 {
4965   struct GadgetInfo *gi = level_editor_gadget[textarea_info[id].gadget_id];
4966   int xoffset_above = 0;
4967   int yoffset_above = -(MINI_TILEX + ED_GADGET_DISTANCE);
4968   int x_above = textarea_info[id].x + xoffset_above;
4969   int y_above = textarea_info[id].y + yoffset_above;
4970
4971   if (textarea_info[id].text_above)
4972     DrawTextS(x_above, y_above, FONT_TEXT_1, textarea_info[id].text_above);
4973
4974   ModifyGadget(gi, GDI_TEXT_VALUE, textarea_info[id].value, GDI_END);
4975
4976   MapGadget(gi);
4977 }
4978
4979 static void MapSelectboxGadget(int id)
4980 {
4981   struct GadgetInfo *gi = level_editor_gadget[selectbox_info[id].gadget_id];
4982   int xoffset_left = getTextWidthForGadget(selectbox_info[id].text_left);
4983   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
4984   int yoffset = ED_BORDER_SIZE;
4985   int x_left = gi->x - xoffset_left;
4986   int x_right = gi->x + gi->width + xoffset_right;
4987   int y = gi->y + yoffset;
4988
4989   if (selectbox_info[id].text_left)
4990     DrawText(x_left, y, selectbox_info[id].text_left, FONT_TEXT_1);
4991
4992   if (selectbox_info[id].text_right)
4993     DrawText(x_right, y, selectbox_info[id].text_right, FONT_TEXT_1);
4994
4995   ModifyEditorSelectbox(id, *selectbox_info[id].value);
4996
4997   MapGadget(gi);
4998 }
4999
5000 static void MapTextbuttonGadget(int id)
5001 {
5002   struct GadgetInfo *gi = level_editor_gadget[textbutton_info[id].gadget_id];
5003   int xoffset_left = getTextWidthForGadget(textbutton_info[id].text_left);
5004   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
5005   int yoffset = ED_BORDER_SIZE;
5006   int x_left = gi->x - xoffset_left;
5007   int x_right = gi->x + gi->width + xoffset_right;
5008   int y = gi->y + yoffset;
5009
5010   /* only show button to delete change pages when more than minimum pages */
5011   if (id == ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE &&
5012       custom_element.num_change_pages == MIN_CHANGE_PAGES)
5013     return;
5014
5015   if (textbutton_info[id].text_left)
5016     DrawText(x_left, y, textbutton_info[id].text_left, FONT_TEXT_1);
5017
5018   if (textbutton_info[id].text_right)
5019     DrawText(x_right, y, textbutton_info[id].text_right, FONT_TEXT_1);
5020
5021   MapGadget(gi);
5022 }
5023
5024 static void MapGraphicbuttonGadget(int id)
5025 {
5026   struct GadgetInfo *gi= level_editor_gadget[graphicbutton_info[id].gadget_id];
5027   int xoffset_left = getTextWidthForGadget(graphicbutton_info[id].text_left);
5028   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
5029   int yoffset = ED_BORDER_SIZE;
5030   int x_left = gi->x - xoffset_left;
5031   int x_right = gi->x + gi->width + xoffset_right;
5032   int y = gi->y + yoffset;
5033
5034   if (graphicbutton_info[id].text_left)
5035     DrawText(x_left, y, graphicbutton_info[id].text_left, FONT_TEXT_1);
5036
5037   if (graphicbutton_info[id].text_right)
5038     DrawText(x_right, y, graphicbutton_info[id].text_right, FONT_TEXT_1);
5039
5040   MapGadget(gi);
5041 }
5042
5043 static void MapRadiobuttonGadget(int id)
5044 {
5045   struct GadgetInfo *gi = level_editor_gadget[radiobutton_info[id].gadget_id];
5046   int xoffset_left = getTextWidthForGadget(checkbutton_info[id].text_left);
5047   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
5048   int yoffset = ED_BORDER_SIZE;
5049   int x_left = gi->x - xoffset_left;
5050   int x_right = gi->x + gi->width + xoffset_right;
5051   int y = gi->y + yoffset;
5052   boolean checked =
5053     (*radiobutton_info[id].value == radiobutton_info[id].checked_value);
5054
5055   if (radiobutton_info[id].text_left)
5056     DrawText(x_left, y, radiobutton_info[id].text_left, FONT_TEXT_1);
5057
5058   if (radiobutton_info[id].text_right)
5059     DrawText(x_right, y, radiobutton_info[id].text_right, FONT_TEXT_1);
5060
5061   ModifyGadget(gi, GDI_CHECKED, checked, GDI_END);
5062
5063   MapGadget(gi);
5064 }
5065
5066 static void MapCheckbuttonGadget(int id)
5067 {
5068   struct GadgetInfo *gi = level_editor_gadget[checkbutton_info[id].gadget_id];
5069   int xoffset_left = getTextWidthForGadget(checkbutton_info[id].text_left);
5070   int xoffset_right = ED_GADGET_TEXT_DISTANCE;
5071   int yoffset = ED_BORDER_SIZE;
5072   int x_left = gi->x - xoffset_left;
5073   int x_right = gi->x + gi->width + xoffset_right;
5074   int y = gi->y + yoffset;
5075
5076   /* special case needed for "sticky" gadget */
5077   ModifyGadget(gi, GDI_CHECKED, *checkbutton_info[id].value,
5078                GDI_Y, SY + checkbutton_info[id].y, GDI_END);
5079   y = gi->y + yoffset;
5080
5081   if (checkbutton_info[id].text_left)
5082     DrawText(x_left, y, checkbutton_info[id].text_left, FONT_TEXT_1);
5083
5084   if (checkbutton_info[id].text_right)
5085     DrawText(x_right, y, checkbutton_info[id].text_right, FONT_TEXT_1);
5086
5087   MapGadget(gi);
5088 }
5089
5090 static void MapMainDrawingArea()
5091 {
5092   boolean no_horizontal_scrollbar = (lev_fieldx + 2 <= ed_fieldx);
5093   boolean no_vertical_scrollbar = (lev_fieldy + 2 <= ed_fieldy);
5094   int i;
5095
5096   for (i=ED_SCROLLBUTTON_ID_AREA_FIRST; i <= ED_SCROLLBUTTON_ID_AREA_LAST; i++)
5097   {
5098     if (((i == ED_SCROLLBUTTON_ID_AREA_LEFT ||
5099           i == ED_SCROLLBUTTON_ID_AREA_RIGHT) &&
5100          no_horizontal_scrollbar) ||
5101         ((i == ED_SCROLLBUTTON_ID_AREA_UP ||
5102           i == ED_SCROLLBUTTON_ID_AREA_DOWN) &&
5103          no_vertical_scrollbar))
5104       continue;
5105
5106     MapGadget(level_editor_gadget[scrollbutton_info[i].gadget_id]);
5107   }
5108
5109   for (i = ED_SCROLLBAR_ID_AREA_FIRST; i <= ED_SCROLLBAR_ID_AREA_LAST; i++)
5110   {
5111     if ((i == ED_SCROLLBAR_ID_AREA_HORIZONTAL && no_horizontal_scrollbar) ||
5112         (i == ED_SCROLLBAR_ID_AREA_VERTICAL && no_vertical_scrollbar))
5113       continue;
5114
5115     MapGadget(level_editor_gadget[scrollbar_info[i].gadget_id]);
5116   }
5117
5118   MapDrawingArea(ED_DRAWING_ID_DRAWING_LEVEL);
5119 }
5120
5121 static void MapOrUnmapLevelEditorToolboxCustomGadgets(boolean map)
5122 {
5123   int i;
5124
5125   for (i = 0; i < ED_NUM_CTRL_BUTTONS; i++)
5126   {
5127     if (i == GADGET_ID_CUSTOM_COPY_FROM ||
5128         i == GADGET_ID_CUSTOM_COPY_TO ||
5129         i == GADGET_ID_CUSTOM_EXCHANGE)
5130     {
5131       if (map)
5132         MapGadget(level_editor_gadget[i]);
5133       else
5134         UnmapGadget(level_editor_gadget[i]);
5135     }
5136   }
5137 }
5138
5139 static void MapLevelEditorToolboxCustomGadgets()
5140 {
5141   MapOrUnmapLevelEditorToolboxCustomGadgets(TRUE);
5142 }
5143
5144 static void UnmapLevelEditorToolboxCustomGadgets()
5145 {
5146   MapOrUnmapLevelEditorToolboxCustomGadgets(FALSE);
5147 }
5148
5149 static void MapOrUnmapLevelEditorToolboxDrawingGadgets(boolean map)
5150 {
5151   Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
5152   int i;
5153
5154   for (i = 0; i < ED_NUM_CTRL1_BUTTONS; i++)
5155   {
5156     if (i != GADGET_ID_SINGLE_ITEMS &&
5157         i != GADGET_ID_PROPERTIES &&
5158         i != GADGET_ID_PICK_ELEMENT)
5159     {
5160       struct GadgetInfo *gi = level_editor_gadget[i];
5161
5162       if (map)
5163         MapGadget(gi);
5164       else
5165       {
5166         UnmapGadget(gi);
5167
5168         BlitBitmap(gd_bitmap, drawto,
5169                    DOOR_GFX_PAGEX6 + ED_CTRL_NO_BUTTONS_GFX_XPOS,
5170                    DOOR_GFX_PAGEY1 + ED_CTRL_NO_BUTTONS_GFX_YPOS,
5171                    gi->width, gi->height, gi->x, gi->y);
5172
5173         redraw_mask |= REDRAW_DOOR_3;
5174       }
5175     }
5176   }
5177 }
5178
5179 static void MapLevelEditorToolboxDrawingGadgets()
5180 {
5181   MapOrUnmapLevelEditorToolboxDrawingGadgets(TRUE);
5182 }
5183
5184 static void UnmapLevelEditorToolboxDrawingGadgets()
5185 {
5186   MapOrUnmapLevelEditorToolboxDrawingGadgets(FALSE);
5187 }
5188
5189 static void UnmapDrawingArea(int id)
5190 {
5191   UnmapGadget(level_editor_gadget[drawingarea_info[id].gadget_id]);
5192 }
5193
5194 static void UnmapLevelEditorWindowGadgets()
5195 {
5196   int i;
5197
5198   for (i = 0; i < NUM_EDITOR_GADGETS; i++)
5199     if (level_editor_gadget[i]->x < SX + SXSIZE)
5200       UnmapGadget(level_editor_gadget[i]);
5201 }
5202
5203 void UnmapLevelEditorGadgets()
5204 {
5205   int i;
5206
5207   for (i = 0; i < NUM_EDITOR_GADGETS; i++)
5208     UnmapGadget(level_editor_gadget[i]);
5209 }
5210
5211 static void ResetUndoBuffer()
5212 {
5213   undo_buffer_position = -1;
5214   undo_buffer_steps = -1;
5215   CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
5216 }
5217
5218 static void DrawEditModeWindow()
5219 {
5220   ModifyEditorElementList();
5221   RedrawDrawingElements();
5222
5223   if (edit_mode == ED_MODE_INFO)
5224     DrawLevelInfoWindow();
5225   else if (edit_mode == ED_MODE_PROPERTIES)
5226     DrawPropertiesWindow();
5227   else  /* edit_mode == ED_MODE_DRAWING */
5228     DrawDrawingWindow();
5229 }
5230
5231 static boolean LevelChanged()
5232 {
5233   boolean level_changed = FALSE;
5234   int x, y;
5235
5236   for (y = 0; y < lev_fieldy; y++) 
5237     for (x = 0; x < lev_fieldx; x++)
5238       if (Feld[x][y] != level.field[x][y])
5239         level_changed = TRUE;
5240
5241   return level_changed;
5242 }
5243
5244 static boolean LevelContainsPlayer()
5245 {
5246   boolean player_found = FALSE;
5247   int x, y;
5248
5249   for (y = 0; y < lev_fieldy; y++) 
5250     for (x = 0; x < lev_fieldx; x++)
5251       if (Feld[x][y] == EL_PLAYER_1 ||
5252           Feld[x][y] == EL_SP_MURPHY) 
5253         player_found = TRUE;
5254
5255   return player_found;
5256 }
5257
5258 static void CopyPlayfield(short src[MAX_LEV_FIELDX][MAX_LEV_FIELDY],
5259                           short dst[MAX_LEV_FIELDX][MAX_LEV_FIELDY])
5260 {
5261   int x, y;
5262
5263   for (x = 0; x < lev_fieldx; x++)
5264     for (y = 0; y < lev_fieldy; y++) 
5265       dst[x][y] = src[x][y];
5266 }
5267
5268 static int setSelectboxValue(int selectbox_id, int new_value)
5269 {
5270   int new_index_value = 0;
5271   int i;
5272
5273   for (i = 0; selectbox_info[selectbox_id].options[i].text != NULL; i++)
5274     if (selectbox_info[selectbox_id].options[i].value == new_value)
5275       new_index_value = i;
5276
5277   *selectbox_info[selectbox_id].value =
5278     selectbox_info[selectbox_id].options[new_index_value].value;
5279
5280   return new_index_value;
5281 }
5282
5283 static void copy_custom_element_settings(int element_from, int element_to)
5284 {
5285 #if 1
5286   struct ElementInfo ei_to_old = element_info[element_to];
5287   struct ElementInfo *ei_from = &element_info[element_from];
5288   struct ElementInfo *ei_to = &element_info[element_to];
5289   int i;
5290
5291   /* ---------- copy whole element structure ---------- */
5292   *ei_to = *ei_from;
5293
5294   /* ---------- restore structure pointers which cannot be copied ---------- */
5295   ei_to->token_name         = ei_to_old.token_name;
5296   ei_to->class_name         = ei_to_old.class_name;
5297   ei_to->editor_description = ei_to_old.editor_description;
5298   ei_to->custom_description = ei_to_old.custom_description;
5299   ei_to->change_page        = ei_to_old.change_page;
5300   ei_to->change             = ei_to_old.change;
5301   ei_to->group              = ei_to_old.group;
5302
5303   /* ---------- copy element base properties ---------- */
5304   Properties[element_to][EP_BITFIELD_BASE] =
5305     Properties[element_from][EP_BITFIELD_BASE];
5306
5307   /* ---------- reinitialize and copy change pages ---------- */
5308   setElementChangePages(ei_to, ei_to->num_change_pages);
5309
5310   for (i=0; i < ei_to->num_change_pages; i++)
5311     ei_to->change_page[i] = ei_from->change_page[i];
5312
5313   /* ---------- copy group element info ---------- */
5314   if (ei_from->group != NULL && ei_to->group != NULL)   /* group or internal */
5315     *ei_to->group = *ei_from->group;
5316
5317 #else
5318
5319   struct ElementInfo *ei_from = &element_info[element_from];
5320   struct ElementInfo *ei_to = &element_info[element_to];
5321   int i, x, y;
5322
5323   /* ---------- copy element description ---------- */
5324   for (i = 0; i < MAX_ELEMENT_NAME_LEN + 1; i++)
5325     ei_to->description[i] = ei_from->description[i];
5326
5327   /* ---------- copy element properties ---------- */
5328   Properties[element_to][EP_BITFIELD_BASE] =
5329     Properties[element_from][EP_BITFIELD_BASE];
5330
5331   /* ---------- copy custom property values ---------- */
5332
5333   ei_to->use_gfx_element = ei_from->use_gfx_element;
5334   ei_to->gfx_element = ei_from->gfx_element;
5335
5336   ei_to->collect_score = ei_from->collect_score;
5337   ei_to->collect_count = ei_from->collect_count;
5338
5339   ei_to->push_delay_fixed = ei_from->push_delay_fixed;
5340   ei_to->push_delay_random = ei_from->push_delay_random;
5341   ei_to->move_delay_fixed = ei_from->move_delay_fixed;
5342   ei_to->move_delay_random = ei_from->move_delay_random;
5343
5344   ei_to->move_pattern = ei_from->move_pattern;
5345   ei_to->move_direction_initial = ei_from->move_direction_initial;
5346   ei_to->move_stepsize = ei_from->move_stepsize;
5347
5348   ei_to->slippery_type = ei_from->slippery_type;
5349
5350   for (y = 0; y < 3; y++)
5351     for (x = 0; x < 3; x++)
5352       ei_to->content[x][y] = ei_from->content[x][y];
5353
5354   ei_to->num_change_pages = ei_from->num_change_pages;
5355   setElementChangePages(ei_to, ei_to->num_change_pages);
5356
5357   for (i=0; i < ei_to->num_change_pages; i++)
5358   {
5359     struct ElementChangeInfo *change_to = &ei_to->change_page[i];
5360     struct ElementChangeInfo *change_from = &ei_from->change_page[i];
5361
5362     /* always start with reliable default values */
5363     setElementChangeInfoToDefaults(change_to);
5364
5365     change_to->events = change_from->events;
5366
5367     change_to->target_element = change_from->target_element;
5368
5369     change_to->delay_fixed = change_from->delay_fixed;
5370     change_to->delay_random = change_from->delay_random;
5371     change_to->delay_frames = change_from->delay_frames;
5372
5373     change_to->trigger_element = change_from->trigger_element;
5374
5375     change_to->explode = change_from->explode;
5376     change_to->use_content = change_from->use_content;
5377     change_to->only_complete = change_from->only_complete;
5378     change_to->use_random_change = change_from->use_random_change;
5379
5380     change_to->random = change_from->random;
5381     change_to->power = change_from->power;
5382
5383     for (y = 0; y < 3; y++)
5384       for (x = 0; x < 3; x++)
5385         change_to->content[x][y] = change_from->content[x][y];
5386
5387     change_to->can_change = change_from->can_change;
5388
5389     change_to->sides = change_from->sides;
5390   }
5391 #endif
5392
5393   /* mark this custom element as modified */
5394   ei_to->modified_settings = TRUE;
5395 }
5396
5397 static void replace_custom_element_in_settings(int element_from,
5398                                                int element_to)
5399 {
5400   int i, j, x, y;
5401
5402   for (i = 0; i < NUM_FILE_ELEMENTS; i++)
5403   {
5404     struct ElementInfo *ei = &element_info[i];
5405
5406     for (y = 0; y < 3; y++)
5407       for (x = 0; x < 3; x++)
5408         if (ei->content[x][y] == element_from)
5409           ei->content[x][y] = element_to;
5410
5411     for (j = 0; j < ei->num_change_pages; j++)
5412     {
5413       struct ElementChangeInfo *change = &ei->change_page[j];
5414
5415       if (change->target_element == element_from)
5416         change->target_element = element_to;
5417
5418       if (change->trigger_element == element_from)
5419         change->trigger_element = element_to;
5420
5421       for (y = 0; y < 3; y++)
5422         for (x = 0; x < 3; x++)
5423           if (change->content[x][y] == element_from)
5424             change->content[x][y] = element_to;
5425     }
5426
5427     if (ei->group != NULL)                              /* group or internal */
5428       for (j = 0; j < MAX_ELEMENTS_IN_GROUP; j++)
5429         if (ei->group->element[j] == element_from)
5430           ei->group->element[j] = element_to;
5431   }
5432 }
5433
5434 static void replace_custom_element_in_playfield(int element_from,
5435                                                 int element_to)
5436 {
5437   int x, y;
5438
5439   for (x = 0; x < lev_fieldx; x++)
5440     for (y = 0; y < lev_fieldy; y++)
5441       if (Feld[x][y] == element_from)
5442         Feld[x][y] = element_to;
5443 }
5444
5445 static boolean CopyCustomElement(int element_old, int element_new,
5446                                  int copy_mode)
5447 {
5448   if (IS_CUSTOM_ELEMENT(element_old) && !IS_CUSTOM_ELEMENT(element_new))
5449   {
5450     Request("Please choose custom element !", REQ_CONFIRM);
5451
5452     return FALSE;
5453   }
5454   else if (IS_GROUP_ELEMENT(element_old) && !IS_GROUP_ELEMENT(element_new))
5455   {
5456     Request("Please choose group element !", REQ_CONFIRM);
5457
5458     return FALSE;
5459   }
5460
5461   if (copy_mode == GADGET_ID_CUSTOM_COPY_FROM)
5462   {
5463     copy_custom_element_settings(element_new, element_old);
5464   }
5465   else if (copy_mode == GADGET_ID_CUSTOM_COPY_TO)
5466   {
5467     copy_custom_element_settings(element_old, element_new);
5468   }
5469   else if (copy_mode == GADGET_ID_CUSTOM_EXCHANGE)
5470   {
5471     copy_custom_element_settings(element_old, EL_INTERNAL_EDITOR);
5472     copy_custom_element_settings(element_new, element_old);
5473     copy_custom_element_settings(EL_INTERNAL_EDITOR, element_new);
5474
5475     replace_custom_element_in_settings(element_old, EL_INTERNAL_EDITOR);
5476     replace_custom_element_in_settings(element_new, element_old);
5477     replace_custom_element_in_settings(EL_INTERNAL_EDITOR, element_new);
5478
5479     replace_custom_element_in_playfield(element_old, EL_INTERNAL_EDITOR);
5480     replace_custom_element_in_playfield(element_new, element_old);
5481     replace_custom_element_in_playfield(EL_INTERNAL_EDITOR, element_new);
5482   }
5483
5484   UpdateCustomElementGraphicGadgets();
5485   DrawPropertiesWindow();
5486
5487   return TRUE;
5488 }
5489
5490 static void CopyCustomElementPropertiesToEditor(int element)
5491 {
5492   int i;
5493   int current_change_page = element_info[element].current_change_page;
5494
5495   /* dynamically (re)build selectbox for selecting change page */
5496   for (i = 0; i < element_info[element].num_change_pages; i++)
5497   {
5498     sprintf(options_change_page_strings[i], "%d", i + 1);
5499
5500     options_change_page[i].value = i;
5501     options_change_page[i].text = options_change_page_strings[i];
5502   }
5503
5504   options_change_page[i].value = -1;
5505   options_change_page[i].text = NULL;
5506
5507   /* needed here to initialize combined element properties */
5508   InitElementPropertiesEngine(level.game_version);
5509
5510   element_info[element].change =
5511     &element_info[element].change_page[current_change_page];
5512
5513   custom_element = element_info[element];
5514   custom_element_change = *element_info[element].change;
5515
5516   /* needed to initially set selectbox value variables to reliable defaults */
5517   for (i = 0; i < ED_NUM_SELECTBOX; i++)
5518     setSelectboxValue(i, *selectbox_info[i].value);
5519
5520   for (i = 0; i < NUM_ELEMENT_PROPERTIES; i++)
5521     custom_element_properties[i] = HAS_PROPERTY(element, i);
5522
5523   for (i = 0; i < NUM_CHANGE_EVENTS; i++)
5524     custom_element_change_events[i] = HAS_CHANGE_EVENT(element, i);
5525
5526   /* ---------- element settings: configure (custom elements) ------------- */
5527
5528   /* set accessible layer selectbox help value */
5529   custom_element.access_type =
5530     (IS_WALKABLE(element) ? EP_WALKABLE :
5531      IS_PASSABLE(element) ? EP_PASSABLE :
5532      custom_element.access_type);
5533   custom_element.access_layer =
5534     (IS_ACCESSIBLE_OVER(element) ? EP_ACCESSIBLE_OVER :
5535      IS_ACCESSIBLE_INSIDE(element) ? EP_ACCESSIBLE_INSIDE :
5536      IS_ACCESSIBLE_UNDER(element) ? EP_ACCESSIBLE_UNDER :
5537      custom_element.access_layer);
5538   custom_element.access_protected =
5539     (IS_PROTECTED(element) ? 1 : 0);
5540   custom_element_properties[EP_ACCESSIBLE] =
5541     (IS_ACCESSIBLE_OVER(element) ||
5542      IS_ACCESSIBLE_INSIDE(element) ||
5543      IS_ACCESSIBLE_UNDER(element));
5544
5545   /* set walk-to-object action selectbox help value */
5546   custom_element.walk_to_action =
5547     (IS_DIGGABLE(element) ? EP_DIGGABLE :
5548      IS_COLLECTIBLE_ONLY(element) ? EP_COLLECTIBLE_ONLY :
5549      IS_DROPPABLE(element) ? EP_DROPPABLE :
5550      IS_PUSHABLE(element) ? EP_PUSHABLE :
5551      custom_element.walk_to_action);
5552   custom_element_properties[EP_WALK_TO_OBJECT] =
5553     (IS_DIGGABLE(element) ||
5554      IS_COLLECTIBLE_ONLY(element) ||
5555      IS_DROPPABLE(element) ||
5556      IS_PUSHABLE(element));
5557
5558   /* set smash targets selectbox help value */
5559   custom_element.smash_targets =
5560     (CAN_SMASH_EVERYTHING(element) ? EP_CAN_SMASH_EVERYTHING :
5561      CAN_SMASH_ENEMIES(element) ? EP_CAN_SMASH_ENEMIES :
5562      CAN_SMASH_PLAYER(element) ? EP_CAN_SMASH_PLAYER :
5563      custom_element.smash_targets);
5564   custom_element_properties[EP_CAN_SMASH] =
5565     (CAN_SMASH_EVERYTHING(element) ||
5566      CAN_SMASH_ENEMIES(element) ||
5567      CAN_SMASH_PLAYER(element));
5568
5569   /* set deadliness selectbox help value */
5570   custom_element.deadliness =
5571     (DONT_TOUCH(element) ? EP_DONT_TOUCH :
5572      DONT_COLLIDE_WITH(element) ? EP_DONT_COLLIDE_WITH :
5573      DONT_RUN_INTO(element) ? EP_DONT_RUN_INTO :
5574      custom_element.deadliness);
5575   custom_element_properties[EP_DEADLY] =
5576     (DONT_TOUCH(element) ||
5577      DONT_COLLIDE_WITH(element) ||
5578      DONT_RUN_INTO(element));
5579
5580   /* set consistency selectbox help value */
5581   custom_element.consistency =
5582     (IS_INDESTRUCTIBLE(element) ? EP_INDESTRUCTIBLE :
5583      CAN_EXPLODE_1X1(element) ? EP_CAN_EXPLODE_1X1 :
5584      CAN_EXPLODE_3X3(element) ? EP_CAN_EXPLODE_3X3 :
5585      CAN_EXPLODE_DYNA(element) ? EP_CAN_EXPLODE_DYNA :
5586      custom_element.consistency);
5587   custom_element_properties[EP_EXPLODE_RESULT] =
5588     (IS_INDESTRUCTIBLE(element) ||
5589      CAN_EXPLODE_1X1(element) ||
5590      CAN_EXPLODE_3X3(element) ||
5591      CAN_EXPLODE_DYNA(element));
5592
5593   /* special case: sub-settings dependent from main setting */
5594   if (CAN_EXPLODE_BY_FIRE(element))
5595     custom_element.can_explode_by_fire = TRUE;
5596   if (CAN_EXPLODE_SMASHED(element))
5597     custom_element.can_explode_smashed = TRUE;
5598   if (CAN_EXPLODE_IMPACT(element))
5599     custom_element.can_explode_impact  = TRUE;
5600
5601   /* ---------- element settings: advanced (custom elements) --------------- */
5602
5603   /* set "change by direct action" selectbox help value */
5604   custom_element_change.direct_action =
5605     (HAS_CHANGE_EVENT(element, CE_TOUCHED_BY_PLAYER) ? CE_TOUCHED_BY_PLAYER :
5606      HAS_CHANGE_EVENT(element, CE_PRESSED_BY_PLAYER) ? CE_PRESSED_BY_PLAYER :
5607      HAS_CHANGE_EVENT(element, CE_PUSHED_BY_PLAYER) ? CE_PUSHED_BY_PLAYER :
5608      HAS_CHANGE_EVENT(element, CE_ENTERED_BY_PLAYER) ? CE_ENTERED_BY_PLAYER :
5609      HAS_CHANGE_EVENT(element, CE_LEFT_BY_PLAYER) ? CE_LEFT_BY_PLAYER :
5610      HAS_CHANGE_EVENT(element, CE_DROPPED_BY_PLAYER) ? CE_DROPPED_BY_PLAYER :
5611      HAS_CHANGE_EVENT(element, CE_SWITCHED) ? CE_SWITCHED :
5612      HAS_CHANGE_EVENT(element, CE_HITTING_SOMETHING) ? CE_HITTING_SOMETHING :
5613      HAS_CHANGE_EVENT(element, CE_HIT_BY_SOMETHING) ? CE_HIT_BY_SOMETHING :
5614      HAS_CHANGE_EVENT(element, CE_IMPACT) ? CE_IMPACT :
5615      HAS_CHANGE_EVENT(element, CE_SMASHED) ? CE_SMASHED :
5616      custom_element_change.direct_action);
5617
5618   /* set "change by other element action" selectbox help value */
5619   custom_element_change.other_action =
5620     (HAS_CHANGE_EVENT(element, CE_OTHER_GETS_TOUCHED) ? CE_OTHER_GETS_TOUCHED :
5621      HAS_CHANGE_EVENT(element, CE_OTHER_GETS_PRESSED) ? CE_OTHER_GETS_PRESSED :
5622      HAS_CHANGE_EVENT(element, CE_OTHER_GETS_PUSHED) ? CE_OTHER_GETS_PUSHED :
5623      HAS_CHANGE_EVENT(element, CE_OTHER_GETS_ENTERED) ? CE_OTHER_GETS_ENTERED :
5624      HAS_CHANGE_EVENT(element, CE_OTHER_GETS_LEFT) ? CE_OTHER_GETS_LEFT :
5625      HAS_CHANGE_EVENT(element, CE_OTHER_GETS_DIGGED) ? CE_OTHER_GETS_DIGGED :
5626      HAS_CHANGE_EVENT(element, CE_OTHER_GETS_COLLECTED) ? CE_OTHER_GETS_COLLECTED :
5627      HAS_CHANGE_EVENT(element, CE_OTHER_GETS_DROPPED) ? CE_OTHER_GETS_DROPPED :
5628      HAS_CHANGE_EVENT(element, CE_OTHER_IS_TOUCHING) ? CE_OTHER_IS_TOUCHING :
5629      HAS_CHANGE_EVENT(element, CE_OTHER_IS_HITTING) ? CE_OTHER_IS_HITTING :
5630      HAS_CHANGE_EVENT(element, CE_OTHER_GETS_HIT) ? CE_OTHER_GETS_HIT :
5631      HAS_CHANGE_EVENT(element, CE_OTHER_IS_SWITCHING) ? CE_OTHER_IS_SWITCHING :
5632      HAS_CHANGE_EVENT(element, CE_OTHER_IS_CHANGING) ? CE_OTHER_IS_CHANGING :
5633      HAS_CHANGE_EVENT(element, CE_OTHER_IS_EXPLODING) ? CE_OTHER_IS_EXPLODING :
5634      custom_element_change.other_action);
5635 }
5636
5637 static void CopyGroupElementPropertiesToEditor(int element)
5638 {
5639   group_element_info = *element_info[element].group;
5640   custom_element = element_info[element];       /* needed for description */
5641 }
5642
5643 static void CopyElementPropertiesToEditor(int element)
5644 {
5645   if (IS_CUSTOM_ELEMENT(element))
5646     CopyCustomElementPropertiesToEditor(element);
5647   else if (IS_GROUP_ELEMENT(element))
5648     CopyGroupElementPropertiesToEditor(element);
5649 }
5650
5651 static void CopyCustomElementPropertiesToGame(int element)
5652 {
5653   int i;
5654   int access_type_and_layer;
5655
5656   /* mark that this custom element has been modified */
5657   custom_element.modified_settings = TRUE;
5658
5659   if (level.use_custom_template)
5660   {
5661     if (Request("Copy and modify level template ?", REQ_ASK))
5662     {
5663       level.use_custom_template = FALSE;
5664       ModifyGadget(level_editor_gadget[GADGET_ID_CUSTOM_USE_TEMPLATE],
5665                    GDI_CHECKED, FALSE, GDI_END);
5666     }
5667     else
5668     {
5669       LoadLevelTemplate(-1);    /* this resets all element modifications ... */
5670
5671       DrawEditModeWindow();     /* ... and copies them to 'custom_element' */
5672     }
5673   }
5674
5675   element_info[element] = custom_element;
5676   *element_info[element].change = custom_element_change;
5677
5678   /* ---------- element settings: configure (custom elements) ------------- */
5679
5680   /* set accessible property from checkbox and selectbox */
5681   custom_element_properties[EP_WALKABLE_OVER] = FALSE;
5682   custom_element_properties[EP_WALKABLE_INSIDE] = FALSE;
5683   custom_element_properties[EP_WALKABLE_UNDER] = FALSE;
5684   custom_element_properties[EP_PASSABLE_OVER] = FALSE;
5685   custom_element_properties[EP_PASSABLE_INSIDE] = FALSE;
5686   custom_element_properties[EP_PASSABLE_UNDER] = FALSE;
5687   access_type_and_layer = ((custom_element.access_type == EP_WALKABLE ?
5688                             EP_WALKABLE_OVER : EP_PASSABLE_OVER) +
5689                            (custom_element.access_layer - EP_ACCESSIBLE_OVER));
5690   custom_element_properties[access_type_and_layer] =
5691     custom_element_properties[EP_ACCESSIBLE];
5692   custom_element_properties[EP_PROTECTED] =
5693     (custom_element.access_protected != 0 &&
5694      custom_element_properties[EP_ACCESSIBLE]);
5695
5696   /* set walk-to-object property from checkbox and selectbox */
5697   custom_element_properties[EP_DIGGABLE] = FALSE;
5698   custom_element_properties[EP_COLLECTIBLE_ONLY] = FALSE;
5699   custom_element_properties[EP_DROPPABLE] = FALSE;
5700   custom_element_properties[EP_PUSHABLE] = FALSE;
5701   custom_element_properties[custom_element.walk_to_action] =
5702     custom_element_properties[EP_WALK_TO_OBJECT];
5703
5704   /* set smash property from checkbox and selectbox */
5705   custom_element_properties[EP_CAN_SMASH_PLAYER] = FALSE;
5706   custom_element_properties[EP_CAN_SMASH_ENEMIES] = FALSE;
5707   custom_element_properties[EP_CAN_SMASH_EVERYTHING] = FALSE;
5708   custom_element_properties[custom_element.smash_targets] =
5709     custom_element_properties[EP_CAN_SMASH];
5710
5711   /* set deadliness property from checkbox and selectbox */
5712   custom_element_properties[EP_DONT_RUN_INTO] = FALSE;
5713   custom_element_properties[EP_DONT_COLLIDE_WITH] = FALSE;
5714   custom_element_properties[EP_DONT_TOUCH] = FALSE;
5715   custom_element_properties[custom_element.deadliness] =
5716     custom_element_properties[EP_DEADLY];
5717
5718   /* set consistency property from checkbox and selectbox */
5719   custom_element_properties[EP_INDESTRUCTIBLE] = FALSE;
5720   custom_element_properties[EP_CAN_EXPLODE_1X1] = FALSE;
5721   custom_element_properties[EP_CAN_EXPLODE_3X3] = FALSE;
5722   custom_element_properties[EP_CAN_EXPLODE_DYNA] = FALSE;
5723   custom_element_properties[EP_CAN_EXPLODE_BY_FIRE] = FALSE;
5724   custom_element_properties[EP_CAN_EXPLODE_SMASHED] = FALSE;
5725   custom_element_properties[EP_CAN_EXPLODE_IMPACT] = FALSE;
5726   custom_element_properties[custom_element.consistency] =
5727     custom_element_properties[EP_EXPLODE_RESULT];
5728
5729   /* special case: sub-settings dependent from main setting */
5730   if (custom_element_properties[EP_CAN_EXPLODE_1X1] ||
5731       custom_element_properties[EP_CAN_EXPLODE_3X3] ||
5732       custom_element_properties[EP_CAN_EXPLODE_DYNA])
5733   {
5734     custom_element_properties[EP_CAN_EXPLODE_BY_FIRE] =
5735       custom_element.can_explode_by_fire;
5736     custom_element_properties[EP_CAN_EXPLODE_SMASHED] =
5737       custom_element.can_explode_smashed;
5738     custom_element_properties[EP_CAN_EXPLODE_IMPACT] =
5739       custom_element.can_explode_impact;
5740   }
5741
5742   /* ---------- element settings: advanced (custom elements) --------------- */
5743
5744   /* set player change event from checkbox and selectbox */
5745   custom_element_change_events[CE_TOUCHED_BY_PLAYER] = FALSE;
5746   custom_element_change_events[CE_PRESSED_BY_PLAYER] = FALSE;
5747   custom_element_change_events[CE_PUSHED_BY_PLAYER] = FALSE;
5748   custom_element_change_events[CE_ENTERED_BY_PLAYER] = FALSE;
5749   custom_element_change_events[CE_LEFT_BY_PLAYER] = FALSE;
5750   custom_element_change_events[CE_DROPPED_BY_PLAYER] = FALSE;
5751   custom_element_change_events[CE_SWITCHED] = FALSE;
5752   custom_element_change_events[CE_HITTING_SOMETHING] = FALSE;
5753   custom_element_change_events[CE_HIT_BY_SOMETHING] = FALSE;
5754   custom_element_change_events[CE_IMPACT] = FALSE;
5755   custom_element_change_events[CE_SMASHED] = FALSE;
5756   custom_element_change_events[custom_element_change.direct_action] =
5757     custom_element_change_events[CE_BY_DIRECT_ACTION];
5758
5759   /* set other element action change event from checkbox and selectbox */
5760   custom_element_change_events[CE_OTHER_GETS_TOUCHED] = FALSE;
5761   custom_element_change_events[CE_OTHER_GETS_PRESSED] = FALSE;
5762   custom_element_change_events[CE_OTHER_GETS_PUSHED] = FALSE;
5763   custom_element_change_events[CE_OTHER_GETS_ENTERED] = FALSE;
5764   custom_element_change_events[CE_OTHER_GETS_LEFT] = FALSE;
5765   custom_element_change_events[CE_OTHER_GETS_DIGGED] = FALSE;
5766   custom_element_change_events[CE_OTHER_GETS_COLLECTED] = FALSE;
5767   custom_element_change_events[CE_OTHER_GETS_DROPPED] = FALSE;
5768   custom_element_change_events[CE_OTHER_IS_TOUCHING] = FALSE;
5769   custom_element_change_events[CE_OTHER_IS_HITTING] = FALSE;
5770   custom_element_change_events[CE_OTHER_GETS_HIT] = FALSE;
5771   custom_element_change_events[CE_OTHER_IS_SWITCHING] = FALSE;
5772   custom_element_change_events[CE_OTHER_IS_CHANGING] = FALSE;
5773   custom_element_change_events[CE_OTHER_IS_EXPLODING] = FALSE;
5774   custom_element_change_events[custom_element_change.other_action] =
5775     custom_element_change_events[CE_BY_OTHER_ACTION];
5776
5777   for (i = 0; i < NUM_ELEMENT_PROPERTIES; i++)
5778     SET_PROPERTY(element, i, custom_element_properties[i]);
5779
5780   for (i = 0; i < NUM_CHANGE_EVENTS; i++)
5781     SET_CHANGE_EVENT(element, i, custom_element_change_events[i]);
5782
5783   /* copy change events also to special level editor variable */
5784   custom_element = element_info[element];
5785   custom_element_change = *element_info[element].change;
5786 }
5787
5788 static void CopyGroupElementPropertiesToGame(int element)
5789 {
5790   element_info[element] = custom_element;
5791   *element_info[element].group = group_element_info;
5792
5793   /* mark that this group element has been modified */
5794   element_info[element].modified_settings = TRUE;
5795 }
5796
5797 static void CopyElementPropertiesToGame(int element)
5798 {
5799   if (IS_CUSTOM_ELEMENT(element))
5800     CopyCustomElementPropertiesToGame(element);
5801   else if (IS_GROUP_ELEMENT(element))
5802     CopyGroupElementPropertiesToGame(element);
5803 }
5804
5805 void DrawLevelEd()
5806 {
5807   CloseDoor(DOOR_CLOSE_ALL);
5808   OpenDoor(DOOR_OPEN_2 | DOOR_NO_DELAY);
5809
5810   if (level_editor_test_game)
5811   {
5812     CopyPlayfield(level.field, Feld);
5813     CopyPlayfield(FieldBackup, level.field);
5814
5815     level_editor_test_game = FALSE;
5816   }
5817   else
5818   {
5819     edit_mode = ED_MODE_DRAWING;
5820     edit_mode_properties = ED_MODE_PROPERTIES_INFO;
5821
5822     ResetUndoBuffer();
5823
5824     level_xpos = -1;
5825     level_ypos = -1;
5826   }
5827
5828   /* copy default editor door content to main double buffer */
5829   BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
5830              DOOR_GFX_PAGEX6, DOOR_GFX_PAGEY1, DXSIZE, DYSIZE, DX, DY);
5831
5832 #if 0
5833   /* draw mouse button brush elements */
5834   RedrawDrawingElements();
5835 #endif
5836
5837   /* draw bigger door */
5838   DrawSpecialEditorDoor();
5839
5840   /* draw new control window */
5841   BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
5842              DOOR_GFX_PAGEX8, 236, EXSIZE, EYSIZE, EX, EY);
5843
5844   redraw_mask |= REDRAW_ALL;
5845
5846   ReinitializeElementListButtons();     /* only needed after setup changes */
5847 #if 0
5848   ModifyEditorElementList();            /* may be needed for custom elements */
5849 #endif
5850
5851   UnmapTapeButtons();
5852   MapControlButtons();
5853
5854   DrawEditModeWindow();
5855
5856   /* copy actual editor door content to door double buffer for OpenDoor() */
5857   BlitBitmap(drawto, bitmap_db_door,
5858              DX, DY, DXSIZE, DYSIZE, DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
5859
5860   OpenDoor(DOOR_OPEN_1);
5861 }
5862
5863 static void AdjustDrawingAreaGadgets()
5864 {
5865   int ed_xsize = lev_fieldx + 2;
5866   int ed_ysize = lev_fieldy + 2;
5867   int max_ed_fieldx = MAX_ED_FIELDX;
5868   int max_ed_fieldy = MAX_ED_FIELDY;
5869   boolean horizontal_scrollbar_needed;
5870   boolean vertical_scrollbar_needed;
5871   int x, y, width, height;
5872   int xoffset, yoffset;
5873
5874   /* check if we need any scrollbars */
5875   horizontal_scrollbar_needed = (ed_xsize > max_ed_fieldx);
5876   vertical_scrollbar_needed = (ed_ysize > max_ed_fieldy);
5877
5878   /* check if we have a smaller editor field because of scrollbars */
5879   if (horizontal_scrollbar_needed)
5880     max_ed_fieldy = MAX_ED_FIELDY - 1;
5881   if (vertical_scrollbar_needed)
5882     max_ed_fieldx = MAX_ED_FIELDX - 1;
5883
5884   /* check again if we now need more scrollbars because of less space */
5885   horizontal_scrollbar_needed = (ed_xsize > max_ed_fieldx);
5886   vertical_scrollbar_needed = (ed_ysize > max_ed_fieldy);
5887
5888   /* check if editor field gets even smaller after adding new scrollbars */
5889   if (horizontal_scrollbar_needed)
5890     max_ed_fieldy = MAX_ED_FIELDY - 1;
5891   if (vertical_scrollbar_needed)
5892     max_ed_fieldx = MAX_ED_FIELDX - 1;
5893
5894   ed_fieldx = (ed_xsize < MAX_ED_FIELDX ? ed_xsize : max_ed_fieldx);
5895   ed_fieldy = (ed_ysize < MAX_ED_FIELDY ? ed_ysize : max_ed_fieldy);
5896
5897   ModifyGadget(level_editor_gadget[GADGET_ID_DRAWING_LEVEL],
5898                GDI_WIDTH, ed_fieldx * MINI_TILEX,
5899                GDI_HEIGHT, ed_fieldy * MINI_TILEY,
5900                GDI_AREA_SIZE, ed_fieldx, ed_fieldy,
5901                GDI_END);
5902
5903   xoffset = (ed_fieldx == MAX_ED_FIELDX ? ED_SCROLLBUTTON_XSIZE : 0);
5904   yoffset = (ed_fieldy == MAX_ED_FIELDY ? ED_SCROLLBUTTON_YSIZE : 0);
5905
5906   x = SX + scrollbutton_info[ED_SCROLLBUTTON_ID_AREA_RIGHT].x + xoffset;
5907   y = SX + scrollbutton_info[ED_SCROLLBUTTON_ID_AREA_DOWN].y + yoffset;
5908
5909   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_RIGHT], GDI_X, x, GDI_END);
5910   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_DOWN], GDI_Y, y, GDI_END);
5911
5912   width = scrollbar_info[ED_SCROLLBAR_ID_AREA_HORIZONTAL].width + xoffset;
5913   height = scrollbar_info[ED_SCROLLBAR_ID_AREA_VERTICAL].height + yoffset;
5914
5915   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
5916                GDI_WIDTH, width,
5917                GDI_SCROLLBAR_ITEMS_VISIBLE, ed_fieldx,
5918                GDI_END);
5919   ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
5920                GDI_HEIGHT, height,
5921                GDI_SCROLLBAR_ITEMS_VISIBLE, ed_fieldy,
5922                GDI_END);
5923 }
5924
5925 static void AdjustLevelScrollPosition()
5926 {
5927   if (level_xpos < -1)
5928     level_xpos = -1;
5929   if (level_xpos > lev_fieldx - ed_fieldx + 1)
5930     level_xpos = lev_fieldx - ed_fieldx + 1;
5931   if (lev_fieldx < ed_fieldx - 2)
5932     level_xpos = -1;
5933
5934   if (level_ypos < -1)
5935     level_ypos = -1;
5936   if (level_ypos > lev_fieldy - ed_fieldy + 1)
5937     level_ypos = lev_fieldy - ed_fieldy + 1;
5938   if (lev_fieldy < ed_fieldy - 2)
5939     level_ypos = -1;
5940 }
5941
5942 static void AdjustEditorScrollbar(int id)
5943 {
5944   struct GadgetInfo *gi = level_editor_gadget[id];
5945   int items_max, items_visible, item_position;
5946
5947   if (id == GADGET_ID_SCROLL_HORIZONTAL)
5948   {
5949     items_max = MAX(lev_fieldx + 2, ed_fieldx);
5950     items_visible = ed_fieldx;
5951     item_position = level_xpos + 1;
5952   }
5953   else
5954   {
5955     items_max = MAX(lev_fieldy + 2, ed_fieldy);
5956     items_visible = ed_fieldy;
5957     item_position = level_ypos + 1;
5958   }
5959
5960   if (item_position > items_max - items_visible)
5961     item_position = items_max - items_visible;
5962
5963   ModifyGadget(gi, GDI_SCROLLBAR_ITEMS_MAX, items_max,
5964                GDI_SCROLLBAR_ITEM_POSITION, item_position, GDI_END);
5965 }
5966
5967 static void ModifyEditorCounter(int counter_id, int new_value)
5968 {
5969   int *counter_value = counterbutton_info[counter_id].value;
5970   int gadget_id = counterbutton_info[counter_id].gadget_id_text;
5971   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
5972
5973   ModifyGadget(gi, GDI_NUMBER_VALUE, new_value, GDI_END);
5974
5975   if (counter_value != NULL)
5976     *counter_value = gi->textinput.number_value;
5977 }
5978
5979 static void ModifyEditorCounterLimits(int counter_id, int min, int max)
5980 {
5981   int gadget_id = counterbutton_info[counter_id].gadget_id_text;
5982   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
5983
5984   ModifyGadget(gi, GDI_NUMBER_MIN, min, GDI_NUMBER_MAX, max, GDI_END);
5985 }
5986
5987 static void ModifyEditorSelectbox(int selectbox_id, int new_value)
5988 {
5989   int gadget_id = selectbox_info[selectbox_id].gadget_id;
5990   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
5991   int new_index_value = setSelectboxValue(selectbox_id, new_value);
5992
5993   ModifyGadget(gi, GDI_SELECTBOX_INDEX, new_index_value, GDI_END);
5994 }
5995
5996 static void ModifyEditorDrawingArea(int drawingarea_id, int xsize, int ysize)
5997 {
5998   int gadget_id = drawingarea_info[drawingarea_id].gadget_id;
5999   struct GadgetInfo *gi = level_editor_gadget[gadget_id];
6000
6001   ModifyGadget(gi, GDI_AREA_SIZE, xsize, ysize, GDI_END);
6002 }
6003
6004 static void ModifyEditorElementList()
6005 {
6006   int i;
6007
6008   for (i = 0; i < ED_NUM_ELEMENTLIST_BUTTONS; i++)
6009   {
6010     int gadget_id = GADGET_ID_ELEMENTLIST_FIRST + i;
6011     struct GadgetInfo *gi = level_editor_gadget[gadget_id];
6012     struct GadgetDesign *gd = &gi->deco.design;
6013     int element = editor_elements[element_shift + i];
6014
6015     UnmapGadget(gi);
6016     getMiniGraphicSource(el2edimg(element), &gd->bitmap, &gd->x, &gd->y);
6017     ModifyGadget(gi, GDI_INFO_TEXT, getElementInfoText(element), GDI_END);
6018     MapGadget(gi);
6019   }
6020 }
6021
6022 static void PickDrawingElement(int button, int element)
6023 {
6024   if (button < 1 || button > 3)
6025     return;
6026
6027   if (button == 1)
6028   {
6029     new_element1 = element;
6030     DrawMiniGraphicExt(drawto,
6031                        DX + ED_WIN_MB_LEFT_XPOS, DY + ED_WIN_MB_LEFT_YPOS,
6032                        el2edimg(new_element1));
6033   }
6034   else if (button == 2)
6035   {
6036     new_element2 = element;
6037     DrawMiniGraphicExt(drawto,
6038                        DX + ED_WIN_MB_MIDDLE_XPOS, DY + ED_WIN_MB_MIDDLE_YPOS,
6039                        el2edimg(new_element2));
6040   }
6041   else
6042   {
6043     new_element3 = element;
6044     DrawMiniGraphicExt(drawto,
6045                        DX + ED_WIN_MB_RIGHT_XPOS, DY + ED_WIN_MB_RIGHT_YPOS,
6046                        el2edimg(new_element3));
6047   }
6048
6049   redraw_mask |= REDRAW_DOOR_1;
6050 }
6051
6052 static void RedrawDrawingElements()
6053 {
6054   PickDrawingElement(1, new_element1);
6055   PickDrawingElement(2, new_element2);
6056   PickDrawingElement(3, new_element3);
6057 }
6058
6059 static void DrawDrawingWindow()
6060 {
6061   stick_element_properties_window = FALSE;
6062
6063   SetMainBackgroundImage(IMG_UNDEFINED);
6064   ClearWindow();
6065
6066   UnmapLevelEditorWindowGadgets();
6067   UnmapLevelEditorToolboxCustomGadgets();
6068
6069   AdjustDrawingAreaGadgets();
6070   AdjustLevelScrollPosition();
6071   AdjustEditorScrollbar(GADGET_ID_SCROLL_HORIZONTAL);
6072   AdjustEditorScrollbar(GADGET_ID_SCROLL_VERTICAL);
6073
6074   DrawMiniLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
6075
6076   MapMainDrawingArea();
6077   MapLevelEditorToolboxDrawingGadgets();
6078 }
6079
6080 static void DrawLevelInfoWindow()
6081 {
6082   int i;
6083
6084   stick_element_properties_window = FALSE;
6085
6086   SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
6087   ClearWindow();
6088   UnmapLevelEditorWindowGadgets();
6089
6090   DrawText(SX + ED_SETTINGS2_XPOS, SY + ED_SETTINGS1_YPOS,
6091            "Level Settings", FONT_TITLE_1);
6092   DrawText(SX + ED_SETTINGS2_XPOS, SY + ED_SETTINGS2_YPOS,
6093            "Editor Settings", FONT_TITLE_1);
6094
6095   /* draw counter gadgets */
6096   for (i = ED_COUNTER_ID_LEVEL_FIRST; i <= ED_COUNTER_ID_LEVEL_LAST; i++)
6097     MapCounterButtons(i);
6098
6099   /* draw checkbutton gadgets */
6100   for (i=ED_CHECKBUTTON_ID_LEVEL_FIRST; i <= ED_CHECKBUTTON_ID_LEVEL_LAST; i++)
6101     MapCheckbuttonGadget(i);
6102
6103   /* draw radiobutton gadgets */
6104   for (i=ED_RADIOBUTTON_ID_LEVEL_FIRST; i <= ED_RADIOBUTTON_ID_LEVEL_LAST; i++)
6105     MapRadiobuttonGadget(i);
6106
6107   /* draw text input gadgets */
6108   for (i = ED_TEXTINPUT_ID_LEVEL_FIRST; i <= ED_TEXTINPUT_ID_LEVEL_LAST; i++)
6109     MapTextInputGadget(i);
6110
6111   /* draw drawing area */
6112   MapDrawingArea(ED_DRAWING_ID_RANDOM_BACKGROUND);
6113 }
6114
6115 static void DrawCustomContentArea()
6116 {
6117   int id = ED_DRAWING_ID_CUSTOM_CONTENT;
6118   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
6119   int x1 = right_gadget_border[GADGET_ID_CUSTOM_DEADLINESS];
6120   int x2 = right_gadget_border[GADGET_ID_CUSTOM_CONSISTENCY];
6121   int x3 = right_gadget_border[GADGET_ID_CUSTOM_EXPLODE_IMPACT];
6122   int xoffset = ED_DRAWINGAREA_TEXT_DISTANCE;
6123
6124   /* add distance for potential left text (without drawing area border) */
6125   x2 += getTextWidthForGadget(drawingarea_info[id].text_left);
6126
6127   ModifyGadget(gi, GDI_X, MAX(x1, MAX(x2, x3)) + xoffset, GDI_END);
6128
6129   MapDrawingArea(ED_DRAWING_ID_CUSTOM_CONTENT);
6130 }
6131
6132 static void DrawCustomChangeContentArea()
6133 {
6134   int id = ED_DRAWING_ID_CUSTOM_CHANGE_CONTENT;
6135   struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[id].gadget_id];
6136   int x1 = right_gadget_border[GADGET_ID_CHANGE_USE_CONTENT];
6137   int x2 = right_gadget_border[GADGET_ID_CHANGE_POWER];
6138   int x3 = right_gadget_border[GADGET_ID_CHANGE_ONLY_COMPLETE];
6139   int xoffset = ED_DRAWINGAREA_TEXT_DISTANCE;
6140
6141   ModifyGadget(gi, GDI_X, MAX(x1, MAX(x2, x3)) + xoffset, GDI_END);
6142
6143   MapDrawingArea(id);
6144 }
6145
6146 static void DrawElementContentAreas()
6147 {
6148   int x = SX + ED_AREA_YAMYAM_CONTENT_XPOS(3) + 4 * MINI_TILEX;
6149   int y = SY + ED_AREA_YAMYAM_CONTENT_YPOS(0) + ED_BORDER_AREA_YSIZE;
6150   int i;
6151
6152   /* display counter to choose number of element content areas */
6153   MapCounterButtons(ED_COUNTER_ID_ELEMENT_CONTENT);
6154
6155   for (i = 0; i < MAX_ELEMENT_CONTENTS; i++)
6156   {
6157     int id = ED_DRAWING_ID_ELEMENT_CONTENT_0 + i;
6158     int font_height = getFontHeight(FONT_TEXT_1);
6159
6160     if (i < level.num_yamyam_contents)
6161       MapDrawingArea(id);
6162     else
6163     {
6164       UnmapDrawingArea(id);
6165
6166       /* delete content areas in case of reducing number of them */
6167       DrawBackground(SX + drawingarea_info[id].x - MINI_TILEX / 2,
6168                      SY + drawingarea_info[id].y - MINI_TILEY / 2,
6169                      4 * MINI_TILEX,
6170                      4 * MINI_TILEY + ED_GADGET_TEXT_DISTANCE + font_height);
6171     }
6172   }
6173
6174   DrawText(x, y + 0 * MINI_TILEY, "content", FONT_TEXT_1);
6175   DrawText(x, y + 1 * MINI_TILEY, "when",    FONT_TEXT_1);
6176   DrawText(x, y + 2 * MINI_TILEY, "smashed", FONT_TEXT_1);
6177 }
6178
6179 static void DrawGroupElementArea(int element)
6180 {
6181   int num_elements = group_element_info.num_elements;
6182   int id = ED_DRAWING_ID_GROUP_CONTENT;
6183   int sx = SX + drawingarea_info[id].x - MINI_TILEX / 2;
6184   int sy = SY + drawingarea_info[id].y - MINI_TILEY / 2;
6185   int xsize = MAX_ELEMENTS_IN_GROUP;
6186   int ysize = 1;
6187
6188   if (drawingarea_info[id].text_left != NULL)
6189     sx += getTextWidthForDrawingArea(drawingarea_info[id].text_left);
6190
6191   UnmapDrawingArea(id);
6192
6193   ModifyEditorDrawingArea(id, num_elements, 1);
6194
6195   /* delete content areas in case of reducing number of them */
6196   DrawBackground(sx, sy, (xsize + 1) * MINI_TILEX, (ysize + 1) * MINI_TILEY);
6197
6198   MapDrawingArea(id);
6199 }
6200
6201 static void DrawEnvelopeTextArea(int envelope_nr)
6202 {
6203   int id = ED_TEXTAREA_ID_ENVELOPE_INFO;
6204   struct GadgetInfo *gi = level_editor_gadget[textarea_info[id].gadget_id];
6205
6206   UnmapGadget(gi);
6207   DrawBackground(gi->x, gi->y, gi->width, gi->height);
6208
6209   if (envelope_nr != -1)
6210     textarea_info[id].value = level.envelope_text[envelope_nr];
6211
6212   ModifyGadget(gi, GDI_AREA_SIZE,
6213                *counterbutton_info[ED_COUNTER_ID_ENVELOPE_XSIZE].value,
6214                *counterbutton_info[ED_COUNTER_ID_ENVELOPE_YSIZE].value,
6215                GDI_END);
6216
6217   MapTextAreaGadget(ED_TEXTAREA_ID_ENVELOPE_INFO);
6218 }
6219
6220 char *getElementDescriptionFilename(int element)
6221 {
6222   char *docs_dir = options.docs_directory;
6223   char *elements_subdir = "elements";
6224   static char *filename = NULL;
6225   char basename[MAX_FILENAME_LEN];
6226
6227   checked_free(filename);
6228
6229   /* 1st try: look for element description file for exactly this element */
6230   sprintf(basename, "%s.txt", element_info[element].token_name);
6231   filename = getPath3(docs_dir, elements_subdir, basename);
6232   if (fileExists(filename))
6233     return filename;
6234
6235   free(filename);
6236
6237   /* 2nd try: look for element description file for this element's class */
6238   sprintf(basename, "%s.txt", element_info[element].class_name);
6239   filename = getPath3(docs_dir, elements_subdir, basename);
6240   if (fileExists(filename))
6241     return filename;
6242
6243   return NULL;
6244 }
6245
6246 static boolean PrintInfoText(char *text, int font_nr, int start_line)
6247 {
6248   int font_height = getFontHeight(font_nr);
6249   int pad_x = ED_SETTINGS_XPOS(0);
6250   int pad_y = ED_SETTINGS_YPOS(0) + ED_BORDER_SIZE;
6251   int sx = SX + pad_x;
6252   int sy = SY + pad_y;
6253   int max_lines_per_screen = (SYSIZE - pad_y) / font_height - 1;
6254
6255   if (start_line >= max_lines_per_screen)
6256     return FALSE;
6257
6258   DrawText(sx, sy + start_line * font_height, text, font_nr);
6259
6260   return TRUE;
6261 }
6262
6263 #if 1
6264
6265 static int PrintElementDescriptionFromFile(char *filename, int start_line)
6266 {
6267   int font_nr = FONT_TEXT_2;
6268   int font_width = getFontWidth(font_nr);
6269   int font_height = getFontHeight(font_nr);
6270   int pad_x = ED_SETTINGS_XPOS(0);
6271   int pad_y = ED_SETTINGS_YPOS(0) + ED_BORDER_SIZE;
6272   int sx = SX + pad_x;
6273   int sy = SY + pad_y + start_line * font_height;
6274   int max_chars_per_line = (SXSIZE - 2 * pad_x) / font_width;
6275   int max_lines_per_screen = (SYSIZE - pad_y) / font_height - 1;
6276
6277   return DrawTextFromFile(sx, sy, filename, font_nr, max_chars_per_line,
6278                           max_lines_per_screen);
6279 }
6280
6281 #else
6282
6283 static int PrintElementDescriptionFromFile(char *filename, int start_line)
6284 {
6285   int font_nr = FONT_TEXT_2;
6286   int font_width = getFontWidth(font_nr);
6287   int font_height = getFontHeight(font_nr);
6288   int pad_x = ED_SETTINGS_XPOS(0);
6289   int pad_y = ED_SETTINGS_YPOS(0) + ED_BORDER_SIZE;
6290   int sx = SX + pad_x;
6291   int sy = SY + pad_y;
6292   int max_chars_per_line = (SXSIZE - 2 * pad_x) / font_width;
6293   int max_lines_per_screen = (SYSIZE - pad_y) / font_height - 1;
6294   int current_line = start_line;
6295   char line[MAX_LINE_LEN];
6296   char buffer[max_chars_per_line + 1];
6297   int buffer_len;
6298   FILE *file;
6299
6300   if (current_line >= max_lines_per_screen)
6301     return 0;
6302
6303   if (filename == NULL)
6304     return 0;
6305
6306   if (!(file = fopen(filename, MODE_READ)))
6307     return 0;
6308
6309   buffer[0] = '\0';
6310   buffer_len = 0;
6311
6312   while (!feof(file) && current_line < max_lines_per_screen)
6313   {
6314     char *line_ptr;
6315     boolean last_line_was_empty = TRUE;
6316
6317     /* read next line of input file */
6318     if (!fgets(line, MAX_LINE_LEN, file))
6319       break;
6320
6321     /* skip comments (lines directly beginning with '#') */
6322     if (line[0] == '#')
6323       continue;
6324
6325     /* cut trailing newline from input line */
6326     for (line_ptr = line; *line_ptr; line_ptr++)
6327     {
6328       if (*line_ptr == '\n' || *line_ptr == '\r')
6329       {
6330         *line_ptr = '\0';
6331         break;
6332       }
6333     }
6334
6335     if (strlen(line) == 0)              /* special case: force empty line */
6336       strcpy(line, "\n");
6337
6338     line_ptr = line;
6339
6340     while (*line_ptr && current_line < max_lines_per_screen)
6341     {
6342       boolean buffer_filled = RenderLineToBuffer(&line_ptr,
6343                                                  buffer, &buffer_len,
6344                                                  last_line_was_empty,
6345                                                  max_chars_per_line);
6346       if (buffer_filled)
6347       {
6348         DrawText(sx, sy + current_line * font_height, buffer, font_nr);
6349         current_line++;
6350
6351         last_line_was_empty = (buffer_len == 0);
6352
6353         buffer[0] = '\0';
6354         buffer_len = 0;
6355       }
6356     }
6357   }
6358
6359   fclose(file);
6360
6361   if (buffer_len > 0 && current_line < max_lines_per_screen)
6362   {
6363     DrawText(sx, sy + current_line * font_height, buffer, font_nr);
6364     current_line++;
6365   }
6366
6367   return (current_line - start_line);
6368 }
6369
6370 #endif
6371
6372 static void DrawPropertiesTabulatorGadgets()
6373 {
6374   struct GadgetInfo *gd_gi = level_editor_gadget[GADGET_ID_PROPERTIES_INFO];
6375   struct GadgetDesign *gd = &gd_gi->alt_design[GD_BUTTON_UNPRESSED];
6376   int gd_x = gd->x + gd_gi->border.width / 2;
6377   int gd_y = gd->y + gd_gi->height - 1;
6378   Pixel tab_color = GetPixel(gd->bitmap, gd_x, gd_y);
6379   int id_first = ED_TEXTBUTTON_ID_PROPERTIES_INFO;
6380   int id_last  = ED_TEXTBUTTON_ID_PROPERTIES_CONFIG;
6381   int max_tabs = 4;
6382   int i;
6383
6384   /* draw additional "advanced" tabulator for custom elements */
6385   if (IS_CUSTOM_ELEMENT(properties_element))
6386     id_last = ED_TEXTBUTTON_ID_PROPERTIES_CHANGE;
6387
6388   for (i = id_first; i <= id_last; i++)
6389   {
6390     int gadget_id = textbutton_info[i].gadget_id;
6391     struct GadgetInfo *gi = level_editor_gadget[gadget_id];
6392     boolean active = (i != edit_mode_properties);
6393
6394     /* use "config 1" and "config 2" instead of "config" */
6395     if (i == ED_TEXTBUTTON_ID_PROPERTIES_CONFIG &&
6396         IS_CUSTOM_ELEMENT(properties_element))
6397       continue;
6398
6399     /* draw background line below tabulator button */
6400     ClearRectangleOnBackground(drawto, gi->x, gi->y + gi->height, gi->width,1);
6401
6402     /* draw solid line below inactive tabulator buttons */
6403     if (!active && tab_color != BLACK_PIXEL)    /* black => transparent */
6404       FillRectangle(drawto, gi->x, gi->y + gi->height, gi->width,1, tab_color);
6405
6406     ModifyGadget(gi, GDI_ACTIVE, active, GDI_END);
6407     MapTextbuttonGadget(i);
6408   }
6409
6410   /* draw little border line below tabulator buttons */
6411   if (tab_color != BLACK_PIXEL)                 /* black => transparent */
6412     FillRectangle(drawto, gd_gi->x, gd_gi->y + gd_gi->height + 1,
6413                   max_tabs * gd_gi->width + (max_tabs -1) * ED_GADGET_DISTANCE,
6414                   ED_GADGET_DISTANCE, tab_color);
6415 }
6416
6417 static void DrawPropertiesInfo()
6418 {
6419   static struct
6420   {
6421     int value;
6422     char *text;
6423   }
6424   properties[] =
6425   {
6426     /* configurable properties */
6427
6428     { EP_WALKABLE_OVER,         "- player can walk over it"             },
6429     { EP_WALKABLE_INSIDE,       "- player can walk inside it"           },
6430     { EP_WALKABLE_UNDER,        "- player can walk under it"            },
6431     { EP_PASSABLE_OVER,         "- player can pass over it"             },
6432     { EP_PASSABLE_INSIDE,       "- player can pass through it"          },
6433     { EP_PASSABLE_UNDER,        "- player can pass under it"            },
6434
6435     { EP_DIGGABLE,              "- can be digged away"                  },
6436     { EP_COLLECTIBLE,           "- can be collected"                    },
6437     { EP_DROPPABLE,             "- can be dropped after collecting"     },
6438     { EP_PUSHABLE,              "- can be pushed"                       },
6439
6440     { EP_CAN_MOVE,              "- can move"                            },
6441     { EP_CAN_FALL,              "- can fall"                            },
6442
6443     { EP_CAN_SMASH_PLAYER,      "- can smash player"                    },
6444 #if 0
6445     { EP_CAN_SMASH_ENEMIES,     "- can smash good and bad guys"         },
6446 #endif
6447     { EP_CAN_SMASH_EVERYTHING,  "- can smash everything smashable"      },
6448
6449     { EP_SLIPPERY,              "- slippery for falling objects"        },
6450     { EP_EM_SLIPPERY_WALL,      "- slippery for some gems (EM style)"   },
6451
6452     { EP_DONT_RUN_INTO,         "- deadly when running into"            },
6453     { EP_DONT_COLLIDE_WITH,     "- deadly when colliding with"          },
6454     { EP_DONT_TOUCH,            "- deadly when touching"                },
6455
6456     { EP_INDESTRUCTIBLE,        "- indestructible"                      },
6457
6458     { EP_CAN_EXPLODE_BY_FIRE,   "- can explode by fire or explosions"   },
6459     { EP_CAN_EXPLODE_SMASHED,   "- can explode when smashed"            },
6460     { EP_CAN_EXPLODE_IMPACT,    "- can explode on impact"               },
6461
6462     { EP_CAN_CHANGE,            "- can change to other element"         },
6463
6464     /* pre-defined properties */
6465     { EP_CAN_PASS_MAGIC_WALL,   "- can pass magic walls"                },
6466     { EP_HAS_CONTENT,           "- can contain other elements"          },
6467
6468     { -1,                       NULL                                    }
6469   };
6470   char *filename = getElementDescriptionFilename(properties_element);
6471   char *percentage_text = "In this level:";
6472   char *properties_text = "Standard properties:";
6473   float percentage;
6474   int num_elements_in_level;
6475   int num_standard_properties = 0;
6476   int font1_nr = FONT_TEXT_1;
6477   int font2_nr = FONT_TEXT_2;
6478   int font1_width = getFontWidth(font1_nr);
6479   int font2_height = getFontHeight(font2_nr);
6480   int pad_x = ED_SETTINGS_XPOS(0);
6481   int pad_y = ED_SETTINGS_YPOS(0) + ED_BORDER_SIZE;
6482   int screen_line = 0;
6483   int i, x, y;
6484
6485 #if DEBUG
6486   if (IS_CUSTOM_ELEMENT(properties_element))
6487   {
6488     DrawTextF(pad_x, pad_y + screen_line++ * font2_height, FONT_TEXT_3,
6489               "[Custom Element %d]", properties_element - EL_CUSTOM_START + 1);
6490     screen_line++;
6491   }
6492   else if (IS_GROUP_ELEMENT(properties_element))
6493   {
6494     DrawTextF(pad_x, pad_y + screen_line++ * font2_height, FONT_TEXT_3,
6495               "[Group Element %d]", properties_element - EL_GROUP_START + 1);
6496     screen_line++;
6497   }
6498 #endif
6499
6500   /* ----- print number of elements / percentage of this element in level */
6501
6502   num_elements_in_level = 0;
6503   for (y = 0; y < lev_fieldy; y++) 
6504     for (x = 0; x < lev_fieldx; x++)
6505       if (Feld[x][y] == properties_element)
6506         num_elements_in_level++;
6507   percentage = num_elements_in_level * 100.0 / (lev_fieldx * lev_fieldy);
6508
6509   DrawTextS(pad_x, pad_y + screen_line * font2_height, font1_nr,
6510             percentage_text);
6511   DrawTextF(pad_x + strlen(percentage_text) * font1_width,
6512             pad_y + screen_line++ * font2_height, font2_nr,
6513             "%d (%.2f%%)", num_elements_in_level, percentage);
6514
6515   screen_line++;
6516
6517   /* ----- print standard properties of this element */
6518
6519   DrawTextS(pad_x, pad_y + screen_line++ * font2_height, font1_nr,
6520             properties_text);
6521
6522   for (i = 0; properties[i].value != -1; i++)
6523   {
6524     if (!HAS_PROPERTY(properties_element, properties[i].value))
6525       continue;
6526
6527     DrawTextS(pad_x, pad_y + screen_line++ * font2_height, font2_nr,
6528               properties[i].text);
6529     num_standard_properties++;
6530   }
6531
6532   if (num_standard_properties == 0)
6533     DrawTextS(pad_x + strlen(properties_text) * font1_width,
6534               pad_y + (screen_line - 1) * font2_height, font2_nr, "none");
6535
6536   screen_line++;
6537
6538   /* ----- print special description of this element */
6539
6540   PrintInfoText("Description:", FONT_TEXT_1, screen_line);
6541   if (PrintElementDescriptionFromFile(filename, screen_line + 1) == 0)
6542     PrintInfoText("No description available.", FONT_TEXT_1, screen_line);
6543 }
6544
6545 #define TEXT_COLLECTING         "Score for collecting"
6546 #define TEXT_SMASHING           "Score for smashing"
6547 #define TEXT_CRACKING           "Score for cracking"
6548 #define TEXT_SPEED              "Speed of amoeba growth"
6549 #define TEXT_DURATION           "Duration when activated"
6550
6551 static struct
6552 {
6553   int element;
6554   int *value;
6555   char *text;
6556 } elements_with_counter[] =
6557 {
6558   { EL_EMERALD,         &level.score[SC_EMERALD],       TEXT_COLLECTING },
6559   { EL_BD_DIAMOND,      &level.score[SC_EMERALD],       TEXT_COLLECTING },
6560   { EL_EMERALD_YELLOW,  &level.score[SC_EMERALD],       TEXT_COLLECTING },
6561   { EL_EMERALD_RED,     &level.score[SC_EMERALD],       TEXT_COLLECTING },
6562   { EL_EMERALD_PURPLE,  &level.score[SC_EMERALD],       TEXT_COLLECTING },
6563   { EL_SP_INFOTRON,     &level.score[SC_EMERALD],       TEXT_COLLECTING },
6564   { EL_DIAMOND,         &level.score[SC_DIAMOND],       TEXT_COLLECTING },
6565   { EL_CRYSTAL,         &level.score[SC_CRYSTAL],       TEXT_COLLECTING },
6566   { EL_PEARL,           &level.score[SC_PEARL],         TEXT_COLLECTING },
6567   { EL_BUG_RIGHT,       &level.score[SC_BUG],           TEXT_SMASHING   },
6568   { EL_BUG_UP,          &level.score[SC_BUG],           TEXT_SMASHING   },
6569   { EL_BUG_LEFT,        &level.score[SC_BUG],           TEXT_SMASHING   },
6570   { EL_BUG_DOWN,        &level.score[SC_BUG],           TEXT_SMASHING   },
6571   { EL_BD_BUTTERFLY_RIGHT,&level.score[SC_BUG],         TEXT_SMASHING   },
6572   { EL_BD_BUTTERFLY_UP,   &level.score[SC_BUG],         TEXT_SMASHING   },
6573   { EL_BD_BUTTERFLY_LEFT, &level.score[SC_BUG],         TEXT_SMASHING   },
6574   { EL_BD_BUTTERFLY_DOWN, &level.score[SC_BUG],         TEXT_SMASHING   },
6575   { EL_SP_ELECTRON,     &level.score[SC_BUG],           TEXT_SMASHING   },
6576   { EL_SPACESHIP_RIGHT, &level.score[SC_SPACESHIP],     TEXT_SMASHING   },
6577   { EL_SPACESHIP_UP,    &level.score[SC_SPACESHIP],     TEXT_SMASHING   },
6578   { EL_SPACESHIP_LEFT,  &level.score[SC_SPACESHIP],     TEXT_SMASHING   },
6579   { EL_SPACESHIP_DOWN,  &level.score[SC_SPACESHIP],     TEXT_SMASHING   },
6580   { EL_BD_FIREFLY_RIGHT,&level.score[SC_SPACESHIP],     TEXT_SMASHING   },
6581   { EL_BD_FIREFLY_UP,   &level.score[SC_SPACESHIP],     TEXT_SMASHING   },
6582   { EL_BD_FIREFLY_LEFT, &level.score[SC_SPACESHIP],     TEXT_SMASHING   },
6583   { EL_BD_FIREFLY_DOWN, &level.score[SC_SPACESHIP],     TEXT_SMASHING   },
6584   { EL_SP_SNIKSNAK,     &level.score[SC_SPACESHIP],     TEXT_SMASHING   },
6585   { EL_YAMYAM,          &level.score[SC_YAMYAM],        TEXT_SMASHING   },
6586   { EL_DARK_YAMYAM,     &level.score[SC_YAMYAM],        TEXT_SMASHING   },
6587   { EL_ROBOT,           &level.score[SC_ROBOT],         TEXT_SMASHING   },
6588   { EL_PACMAN_RIGHT,    &level.score[SC_PACMAN],        TEXT_SMASHING   },
6589   { EL_PACMAN_UP,       &level.score[SC_PACMAN],        TEXT_SMASHING   },
6590   { EL_PACMAN_LEFT,     &level.score[SC_PACMAN],        TEXT_SMASHING   },
6591   { EL_PACMAN_DOWN,     &level.score[SC_PACMAN],        TEXT_SMASHING   },
6592   { EL_NUT,             &level.score[SC_NUT],           TEXT_CRACKING   },
6593   { EL_DYNAMITE,        &level.score[SC_DYNAMITE],      TEXT_COLLECTING },
6594   { EL_DYNABOMB_INCREASE_NUMBER,&level.score[SC_DYNAMITE],TEXT_COLLECTING },
6595   { EL_DYNABOMB_INCREASE_SIZE,  &level.score[SC_DYNAMITE],TEXT_COLLECTING },
6596   { EL_DYNABOMB_INCREASE_POWER, &level.score[SC_DYNAMITE],TEXT_COLLECTING },
6597   { EL_SHIELD_NORMAL,   &level.score[SC_SHIELD],        TEXT_COLLECTING },
6598   { EL_SHIELD_DEADLY,   &level.score[SC_SHIELD],        TEXT_COLLECTING },
6599   { EL_EXTRA_TIME,      &level.score[SC_TIME_BONUS],    TEXT_COLLECTING },
6600   { EL_KEY_1,           &level.score[SC_KEY],           TEXT_COLLECTING },
6601   { EL_KEY_2,           &level.score[SC_KEY],           TEXT_COLLECTING },
6602   { EL_KEY_3,           &level.score[SC_KEY],           TEXT_COLLECTING },
6603   { EL_KEY_4,           &level.score[SC_KEY],           TEXT_COLLECTING },
6604 #if 1
6605   { EL_EM_KEY_1,        &level.score[SC_KEY],           TEXT_COLLECTING },
6606   { EL_EM_KEY_2,        &level.score[SC_KEY],           TEXT_COLLECTING },
6607   { EL_EM_KEY_3,        &level.score[SC_KEY],           TEXT_COLLECTING },
6608   { EL_EM_KEY_4,        &level.score[SC_KEY],           TEXT_COLLECTING },
6609 #else
6610   { EL_EM_KEY_1_FILE,   &level.score[SC_KEY],           TEXT_COLLECTING },
6611   { EL_EM_KEY_2_FILE,   &level.score[SC_KEY],           TEXT_COLLECTING },
6612   { EL_EM_KEY_3_FILE,   &level.score[SC_KEY],           TEXT_COLLECTING },
6613   { EL_EM_KEY_4_FILE,   &level.score[SC_KEY],           TEXT_COLLECTING },
6614 #endif
6615   { EL_AMOEBA_WET,      &level.amoeba_speed,            TEXT_SPEED      },
6616   { EL_AMOEBA_DRY,      &level.amoeba_speed,            TEXT_SPEED      },
6617   { EL_AMOEBA_FULL,     &level.amoeba_speed,            TEXT_SPEED      },
6618   { EL_BD_AMOEBA,       &level.amoeba_speed,            TEXT_SPEED      },
6619   { EL_MAGIC_WALL,      &level.time_magic_wall,         TEXT_DURATION   },
6620   { EL_ROBOT_WHEEL,     &level.time_wheel,              TEXT_DURATION   },
6621   { -1,                 NULL,                           NULL            }
6622 };
6623
6624 static boolean checkPropertiesConfig()
6625 {
6626   int i;
6627
6628   if (IS_GEM(properties_element) ||
6629       IS_CUSTOM_ELEMENT(properties_element) ||
6630       IS_GROUP_ELEMENT(properties_element) ||
6631       IS_ENVELOPE(properties_element) ||
6632       ELEM_IS_PLAYER(properties_element) ||
6633       HAS_CONTENT(properties_element))
6634     return TRUE;
6635   else
6636     for (i = 0; elements_with_counter[i].element != -1; i++)
6637       if (elements_with_counter[i].element == properties_element)
6638         return TRUE;
6639
6640   return FALSE;
6641 }
6642
6643 static void DrawPropertiesConfig()
6644 {
6645   int i;
6646
6647   if (!checkPropertiesConfig())
6648   {
6649     PrintInfoText("No configuration options available.", FONT_TEXT_1, 0);
6650
6651     return;
6652   }
6653
6654   /* check if there are elements where a score can be chosen for */
6655   for (i = 0; elements_with_counter[i].element != -1; i++)
6656   {
6657     if (elements_with_counter[i].element == properties_element)
6658     {
6659       int counter_id = ED_COUNTER_ID_ELEMENT_SCORE;
6660
6661       if (HAS_CONTENT(properties_element))      /* needs stickybutton */
6662         counterbutton_info[counter_id].y = ED_SETTINGS_YPOS(1);
6663       else
6664         counterbutton_info[counter_id].y = ED_SETTINGS_YPOS(0);
6665
6666       counterbutton_info[counter_id].value = elements_with_counter[i].value;
6667       counterbutton_info[counter_id].text_right= elements_with_counter[i].text;
6668
6669       MapCounterButtons(counter_id);
6670
6671       break;
6672     }
6673   }
6674
6675   if (HAS_CONTENT(properties_element))
6676   {
6677     /* draw stickybutton gadget */
6678     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
6679
6680     if (IS_AMOEBOID(properties_element))
6681       MapDrawingArea(ED_DRAWING_ID_AMOEBA_CONTENT);
6682     else
6683       DrawElementContentAreas();
6684   }
6685
6686   if (IS_GEM(properties_element))
6687     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_EM_SLIPPERY_GEMS);
6688
6689   if (ELEM_IS_PLAYER(properties_element))
6690     MapCheckbuttonGadget(properties_element == EL_SP_MURPHY ?
6691                          ED_CHECKBUTTON_ID_SP_BLOCK_LAST_FIELD :
6692                          ED_CHECKBUTTON_ID_BLOCK_LAST_FIELD);
6693
6694   if (IS_ENVELOPE(properties_element))
6695   {
6696     int counter1_id = ED_COUNTER_ID_ENVELOPE_XSIZE;
6697     int counter2_id = ED_COUNTER_ID_ENVELOPE_YSIZE;
6698     int envelope_nr = properties_element - EL_ENVELOPE_1;
6699
6700     counterbutton_info[counter1_id].value = &level.envelope_xsize[envelope_nr];
6701     counterbutton_info[counter2_id].value = &level.envelope_ysize[envelope_nr];
6702
6703     /* display counter to choose size of envelope text area */
6704     MapCounterButtons(ED_COUNTER_ID_ENVELOPE_XSIZE);
6705     MapCounterButtons(ED_COUNTER_ID_ENVELOPE_YSIZE);
6706
6707     DrawEnvelopeTextArea(envelope_nr);
6708   }
6709
6710   if (IS_CUSTOM_ELEMENT(properties_element))
6711   {
6712     /* draw stickybutton gadget */
6713     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
6714
6715     if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_1)
6716     {
6717       /* draw checkbutton gadgets */
6718       for (i =  ED_CHECKBUTTON_ID_CUSTOM1_FIRST;
6719            i <= ED_CHECKBUTTON_ID_CUSTOM1_LAST; i++)
6720         MapCheckbuttonGadget(i);
6721
6722       /* draw counter gadgets */
6723       for (i =  ED_COUNTER_ID_CUSTOM1_FIRST;
6724            i <= ED_COUNTER_ID_CUSTOM1_LAST; i++)
6725         MapCounterButtons(i);
6726
6727       /* draw selectbox gadgets */
6728       for (i =  ED_SELECTBOX_ID_CUSTOM1_FIRST;
6729            i <= ED_SELECTBOX_ID_CUSTOM1_LAST; i++)
6730         MapSelectboxGadget(i);
6731
6732       /* draw textbutton gadgets */
6733       MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE);
6734
6735       /* draw text input gadgets */
6736       MapTextInputGadget(ED_TEXTINPUT_ID_ELEMENT_NAME);
6737
6738       /* draw drawing area gadgets */
6739       MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
6740       MapDrawingArea(ED_DRAWING_ID_CUSTOM_MOVE_ENTER);
6741       MapDrawingArea(ED_DRAWING_ID_CUSTOM_MOVE_LEAVE);
6742     }
6743     else if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG_2)
6744     {
6745       /* draw checkbutton gadgets */
6746       for (i =  ED_CHECKBUTTON_ID_CUSTOM2_FIRST;
6747            i <= ED_CHECKBUTTON_ID_CUSTOM2_LAST; i++)
6748         MapCheckbuttonGadget(i);
6749
6750       /* draw counter gadgets */
6751       for (i =  ED_COUNTER_ID_CUSTOM2_FIRST;
6752            i <= ED_COUNTER_ID_CUSTOM2_LAST; i++)
6753         MapCounterButtons(i);
6754
6755       /* draw selectbox gadgets */
6756       for (i =  ED_SELECTBOX_ID_CUSTOM2_FIRST;
6757            i <= ED_SELECTBOX_ID_CUSTOM2_LAST; i++)
6758         MapSelectboxGadget(i);
6759
6760       /* draw drawing area gadgets */
6761       DrawCustomContentArea();
6762     }
6763   }
6764   else if (IS_GROUP_ELEMENT(properties_element))
6765   {
6766     /* draw stickybutton gadget */
6767     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
6768
6769     /* draw checkbutton gadgets */
6770     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC);
6771     MapCheckbuttonGadget(ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE);
6772
6773     /* draw counter gadgets */
6774     MapCounterButtons(ED_COUNTER_ID_GROUP_CONTENT);
6775
6776     /* draw selectbox gadgets */
6777     MapSelectboxGadget(ED_SELECTBOX_ID_GROUP_CHOICE_MODE);
6778
6779     /* draw textbutton gadgets */
6780     MapTextbuttonGadget(ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE);
6781
6782     /* draw drawing area gadgets */
6783     DrawGroupElementArea(properties_element);
6784
6785     /* draw text input gadgets */
6786     MapTextInputGadget(ED_TEXTINPUT_ID_ELEMENT_NAME);
6787
6788     /* draw drawing area gadgets */
6789     MapDrawingArea(ED_DRAWING_ID_CUSTOM_GRAPHIC);
6790   }
6791 }
6792
6793 static void DrawPropertiesChangeDrawingAreas()
6794 {
6795   if (IS_CUSTOM_ELEMENT(properties_element))
6796   {
6797     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_TARGET);
6798     MapDrawingArea(ED_DRAWING_ID_CUSTOM_CHANGE_TRIGGER);
6799
6800     DrawCustomChangeContentArea();
6801   }
6802
6803   redraw_mask |= REDRAW_FIELD;
6804 }
6805
6806 static void DrawPropertiesChange()
6807 {
6808   int i;
6809
6810   /* draw stickybutton gadget */
6811   MapCheckbuttonGadget(ED_CHECKBUTTON_ID_STICK_ELEMENT);
6812
6813   /* draw checkbutton gadgets */
6814   for (i =  ED_CHECKBUTTON_ID_CHANGE_FIRST;
6815        i <= ED_CHECKBUTTON_ID_CHANGE_LAST; i++)
6816     MapCheckbuttonGadget(i);
6817
6818   /* draw counter gadgets */
6819   for (i =  ED_COUNTER_ID_CHANGE_FIRST;
6820        i <= ED_COUNTER_ID_CHANGE_LAST; i++)
6821     MapCounterButtons(i);
6822
6823   /* draw selectbox gadgets */
6824   for (i =  ED_SELECTBOX_ID_CHANGE_FIRST;
6825        i <= ED_SELECTBOX_ID_CHANGE_LAST; i++)
6826     MapSelectboxGadget(i);
6827
6828   /* draw textbutton gadgets */
6829   for (i =  ED_TEXTBUTTON_ID_CHANGE_FIRST;
6830        i <= ED_TEXTBUTTON_ID_CHANGE_LAST; i++)
6831     MapTextbuttonGadget(i);
6832
6833   /* draw graphicbutton gadgets */
6834   for (i =  ED_GRAPHICBUTTON_ID_CHANGE_FIRST;
6835        i <= ED_GRAPHICBUTTON_ID_CHANGE_LAST; i++)
6836     MapGraphicbuttonGadget(i);
6837
6838   /* draw drawing area gadgets */
6839   DrawPropertiesChangeDrawingAreas();
6840 }
6841
6842 static void DrawElementName(int x, int y, int element)
6843 {
6844   char *element_name = getElementInfoText(element);
6845   int font_nr = FONT_TEXT_1;
6846   int font_width = getFontWidth(font_nr);
6847   int font_height = getFontHeight(font_nr);
6848   int max_text_width = SXSIZE - x - ED_SETTINGS_XPOS(0);
6849   int max_chars_per_line = max_text_width / font_width;
6850   char buffer[max_chars_per_line + 1];
6851
6852   if (strlen(element_name) <= max_chars_per_line)
6853     DrawTextS(x, y, font_nr, element_name);
6854   else
6855   {
6856     int next_pos = max_chars_per_line;
6857
6858     strncpy(buffer, element_name, max_chars_per_line);
6859     buffer[max_chars_per_line] = '\0';
6860
6861     if (element_name[max_chars_per_line] == ' ')
6862       next_pos++;
6863     else
6864     {
6865       int i;
6866
6867       for (i = max_chars_per_line - 1; i >= 0; i--)
6868         if (buffer[i] == ' ')
6869           break;
6870
6871       if (strlen(&element_name[i + 1]) <= max_chars_per_line)
6872       {
6873         buffer[i] = '\0';
6874         next_pos = i + 1;
6875       }
6876     }
6877
6878     DrawTextS(x, y - font_height / 2, font_nr, buffer);
6879
6880     strncpy(buffer, &element_name[next_pos], max_chars_per_line);
6881     buffer[max_chars_per_line] = '\0';
6882
6883     DrawTextS(x, y + font_height / 2, font_nr, buffer);
6884   }
6885 }
6886
6887 static void DrawPropertiesWindow()
6888 {
6889   int xstart = 2;
6890   int ystart = 4;
6891
6892   stick_element_properties_window = FALSE;
6893
6894   /* make sure that previous properties edit mode exists for this element */
6895   if (edit_mode_properties > ED_MODE_PROPERTIES_CONFIG &&
6896       !IS_CUSTOM_ELEMENT(properties_element))
6897     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG;
6898
6899   if (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG &&
6900       IS_CUSTOM_ELEMENT(properties_element))
6901     edit_mode_properties = ED_MODE_PROPERTIES_CONFIG_1;
6902
6903   CopyElementPropertiesToEditor(properties_element);
6904
6905   UnmapLevelEditorWindowGadgets();
6906   UnmapLevelEditorToolboxDrawingGadgets();
6907   UnmapLevelEditorToolboxCustomGadgets();
6908
6909   if (IS_CUSTOM_ELEMENT(properties_element) ||
6910       IS_GROUP_ELEMENT(properties_element))
6911     MapLevelEditorToolboxCustomGadgets();
6912
6913   SetMainBackgroundImage(IMG_BACKGROUND_EDITOR);
6914   ClearWindow();
6915
6916   DrawText(SX + ED_SETTINGS2_XPOS, SY + ED_SETTINGS1_YPOS,
6917            "Element Settings", FONT_TITLE_1);
6918
6919   DrawElementBorder(SX + xstart * MINI_TILEX,
6920                     SY + ystart * MINI_TILEY + MINI_TILEY / 2,
6921                     TILEX, TILEY, FALSE);
6922   DrawGraphicAnimationExt(drawto,
6923                           SX + xstart * MINI_TILEX,
6924                           SY + ystart * MINI_TILEY + MINI_TILEY / 2,
6925                           el2img(properties_element), -1, NO_MASKING);
6926
6927   FrameCounter = 0;     /* restart animation frame counter */
6928
6929   DrawElementName((xstart + 3) * MINI_TILEX + 1, (ystart + 1) * MINI_TILEY + 1,
6930                   properties_element);
6931
6932   DrawPropertiesTabulatorGadgets();
6933
6934   if (edit_mode_properties == ED_MODE_PROPERTIES_INFO)
6935     DrawPropertiesInfo();
6936   else if (edit_mode_properties == ED_MODE_PROPERTIES_CHANGE)
6937     DrawPropertiesChange();
6938   else  /* (edit_mode_properties == ED_MODE_PROPERTIES_CONFIG[_1|_2]) */
6939     DrawPropertiesConfig();
6940 }
6941
6942 static void UpdateCustomElementGraphicGadgets()
6943 {
6944   int i;
6945
6946   ModifyEditorElementList();
6947   RedrawDrawingElements();
6948
6949   /* force redraw of all mapped drawing area gadgets */
6950   for (i = 0; i < ED_NUM_DRAWING_AREAS; i++)
6951   {
6952     struct GadgetInfo *gi = level_editor_gadget[drawingarea_info[i].gadget_id];
6953
6954     if (gi->mapped)
6955       MapDrawingArea(i);
6956   }
6957 }
6958
6959 static void DrawLineElement(int sx, int sy, int element, boolean change_level)
6960 {
6961   int lx = sx + level_xpos;
6962   int ly = sy + level_ypos;
6963
6964   DrawMiniElement(sx, sy, (element < 0 ? Feld[lx][ly] : element));
6965
6966   if (change_level)
6967     Feld[lx][ly] = element;
6968 }
6969
6970 static void DrawLine(int from_x, int from_y, int to_x, int to_y,
6971                      int element, boolean change_level)
6972 {
6973   if (from_y == to_y)                   /* horizontal line */
6974   {
6975     int x;
6976     int y = from_y;
6977
6978     if (from_x > to_x)
6979       swap_numbers(&from_x, &to_x);
6980
6981     for (x = from_x; x <= to_x; x++)
6982       DrawLineElement(x, y, element, change_level);
6983   }
6984   else if (from_x == to_x)              /* vertical line */
6985   {
6986     int x = from_x;
6987     int y;
6988
6989     if (from_y > to_y)
6990       swap_numbers(&from_y, &to_y);
6991
6992     for (y = from_y; y <= to_y; y++)
6993       DrawLineElement(x, y, element, change_level);
6994   }
6995   else                                  /* diagonal line */
6996   {
6997     int len_x = ABS(to_x - from_x);
6998     int len_y = ABS(to_y - from_y);
6999     int x, y;
7000
7001     if (len_y < len_x)                  /* a < 1 */
7002     {
7003       float a = (float)len_y / (float)len_x;
7004
7005       if (from_x > to_x)
7006         swap_number_pairs(&from_x, &from_y, &to_x, &to_y);
7007
7008       for (x = 0; x <= len_x; x++)
7009       {
7010         y = (int)(a * x + 0.5) * (to_y < from_y ? -1 : +1);
7011         DrawLineElement(from_x + x, from_y + y, element, change_level);
7012       }
7013     }
7014     else                                /* a >= 1 */
7015     {
7016       float a = (float)len_x / (float)len_y;
7017
7018       if (from_y > to_y)
7019         swap_number_pairs(&from_x, &from_y, &to_x, &to_y);
7020
7021       for (y = 0; y <= len_y; y++)
7022       {
7023         x = (int)(a * y + 0.5) * (to_x < from_x ? -1 : +1);
7024         DrawLineElement(from_x + x, from_y + y, element, change_level);
7025       }
7026     }
7027   }
7028 }
7029
7030 static void DrawBox(int from_x, int from_y, int to_x, int to_y,
7031                     int element, boolean change_level)
7032 {
7033   DrawLine(from_x, from_y, from_x, to_y, element, change_level);
7034   DrawLine(from_x, to_y, to_x, to_y, element, change_level);
7035   DrawLine(to_x, to_y, to_x, from_y, element, change_level);
7036   DrawLine(to_x, from_y, from_x, from_y, element, change_level);
7037 }
7038
7039 static void DrawFilledBox(int from_x, int from_y, int to_x, int to_y,
7040                           int element, boolean change_level)
7041 {
7042   int y;
7043
7044   if (from_y > to_y)
7045     swap_number_pairs(&from_x, &from_y, &to_x, &to_y);
7046
7047   for (y = from_y; y <= to_y; y++)
7048     DrawLine(from_x, y, to_x, y, element, change_level);
7049 }
7050
7051 static void DrawArcExt(int from_x, int from_y, int to_x2, int to_y2,
7052                        int element, boolean change_level)
7053 {
7054   int to_x = to_x2 - (to_x2 > from_x ? +1 : -1);
7055   int to_y = to_y2 - (to_y2 > from_y ? +1 : -1);
7056   int len_x = ABS(to_x - from_x);
7057   int len_y = ABS(to_y - from_y);
7058   int radius, x, y;
7059
7060   radius = (int)(sqrt((float)(len_x * len_x + len_y * len_y)) + 0.5);
7061
7062   /* not optimal (some points get drawn twice) but simple,
7063      and fast enough for the few points we are drawing */
7064
7065   for (x = 0; x <= radius; x++)
7066   {
7067     int sx, sy, lx, ly;
7068
7069     y = (int)(sqrt((float)(radius * radius - x * x)) + 0.5);
7070
7071     sx = from_x + x * (from_x < to_x2 ? +1 : -1);
7072     sy = from_y + y * (from_y < to_y2 ? +1 : -1);
7073     lx = sx + level_xpos;
7074     ly = sy + level_ypos;
7075
7076     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
7077       DrawLineElement(sx, sy, element, change_level);
7078   }
7079
7080   for (y = 0; y <= radius; y++)
7081   {
7082     int sx, sy, lx, ly;
7083
7084     x = (int)(sqrt((float)(radius * radius - y * y)) + 0.5);
7085
7086     sx = from_x + x * (from_x < to_x2 ? +1 : -1);
7087     sy = from_y + y * (from_y < to_y2 ? +1 : -1);
7088     lx = sx + level_xpos;
7089     ly = sy + level_ypos;
7090
7091     if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
7092       DrawLineElement(sx, sy, element, change_level);
7093   }
7094 }
7095
7096 static void DrawArc(int from_x, int from_y, int to_x, int to_y,
7097                     int element, boolean change_level)
7098 {
7099   int to_x2 = to_x + (to_x < from_x ? -1 : +1);
7100   int to_y2 = to_y + (to_y > from_y ? +1 : -1);
7101
7102   DrawArcExt(from_x, from_y, to_x2, to_y2, element, change_level);
7103 }
7104
7105 #define DRAW_CIRCLES_BUTTON_AVAILABLE   0
7106 #if DRAW_CIRCLES_BUTTON_AVAILABLE
7107 static void DrawCircle(int from_x, int from_y, int to_x, int to_y,
7108                        int element, boolean change_level)
7109 {
7110   int to_x2 = to_x + (to_x < from_x ? -1 : +1);
7111   int to_y2 = to_y + (to_y > from_y ? +1 : -1);
7112   int mirror_to_x2 = from_x - (to_x2 - from_x);
7113   int mirror_to_y2 = from_y - (to_y2 - from_y);
7114
7115   DrawArcExt(from_x, from_y, to_x2, to_y2, element, change_level);
7116   DrawArcExt(from_x, from_y, mirror_to_x2, to_y2, element, change_level);
7117   DrawArcExt(from_x, from_y, to_x2, mirror_to_y2, element, change_level);
7118   DrawArcExt(from_x, from_y, mirror_to_x2, mirror_to_y2, element,change_level);
7119 }
7120 #endif
7121
7122 static void DrawAreaBorder(int from_x, int from_y, int to_x, int to_y)
7123 {
7124   int from_sx, from_sy;
7125   int to_sx, to_sy;
7126
7127   if (from_x > to_x)
7128     swap_numbers(&from_x, &to_x);
7129
7130   if (from_y > to_y)
7131     swap_numbers(&from_y, &to_y);
7132
7133   from_sx = SX + from_x * MINI_TILEX;
7134   from_sy = SY + from_y * MINI_TILEY;
7135   to_sx = SX + to_x * MINI_TILEX + MINI_TILEX - 1;
7136   to_sy = SY + to_y * MINI_TILEY + MINI_TILEY - 1;
7137
7138   DrawSimpleWhiteLine(drawto, from_sx, from_sy, to_sx, from_sy);
7139   DrawSimpleWhiteLine(drawto, to_sx, from_sy, to_sx, to_sy);
7140   DrawSimpleWhiteLine(drawto, to_sx, to_sy, from_sx, to_sy);
7141   DrawSimpleWhiteLine(drawto, from_sx, to_sy, from_sx, from_sy);
7142
7143   if (from_x == to_x && from_y == to_y)
7144     MarkTileDirty(from_x/2, from_y/2);
7145   else
7146     redraw_mask |= REDRAW_FIELD;
7147 }
7148
7149 static void SelectArea(int from_x, int from_y, int to_x, int to_y,
7150                        int element, boolean change_level)
7151 {
7152   if (element == -1 || change_level)
7153     DrawBox(from_x, from_y, to_x, to_y, -1, FALSE);
7154   else
7155     DrawAreaBorder(from_x, from_y, to_x, to_y);
7156 }
7157
7158 /* values for CopyBrushExt() */
7159 #define CB_AREA_TO_BRUSH        0
7160 #define CB_BRUSH_TO_CURSOR      1
7161 #define CB_BRUSH_TO_LEVEL       2
7162 #define CB_DELETE_OLD_CURSOR    3
7163 #define CB_DUMP_BRUSH           4
7164
7165 static void CopyBrushExt(int from_x, int from_y, int to_x, int to_y,
7166                          int button, int mode)
7167 {
7168   static short brush_buffer[MAX_ED_FIELDX][MAX_ED_FIELDY];
7169   static int brush_width, brush_height;
7170   static int last_cursor_x = -1, last_cursor_y = -1;
7171   static boolean delete_old_brush;
7172   int new_element = BUTTON_ELEMENT(button);
7173   int x, y;
7174
7175   if (mode == CB_DUMP_BRUSH)
7176   {
7177     if (!draw_with_brush)
7178     {
7179       Error(ERR_WARN, "no brush selected");
7180
7181       return;
7182     }
7183
7184     for (y = 0; y < brush_height; y++)
7185     {
7186       for (x = 0; x < brush_width; x++)
7187       {
7188         int element = brush_buffer[x][y];
7189         int element_mapped = element;
7190         char *element_string = "?";
7191         int k;
7192
7193         for (k = 0; forum_sketch_element_mapping[k].element != -1; k++)
7194         {
7195           if (forum_sketch_element_mapping[k].element == element)
7196           {
7197             element_mapped = forum_sketch_element_mapping[k].element_mapped;
7198             break;
7199           }
7200         }
7201
7202         if (IS_CUSTOM_ELEMENT(element))
7203           element_mapped = EL_CUSTOM_1;
7204
7205         for (k = 0; forum_sketch_element_strings[k].element != -1; k++)
7206         {
7207           if (forum_sketch_element_strings[k].element == element_mapped)
7208           {
7209             element_string = forum_sketch_element_strings[k].element_string;
7210             break;
7211           }
7212         }
7213
7214         printf("`%s", element_string);
7215       }
7216
7217       printf("\n");
7218     }
7219
7220     return;
7221   }
7222
7223   if (mode == CB_DELETE_OLD_CURSOR && !delete_old_brush)
7224     return;
7225
7226   if (mode == CB_AREA_TO_BRUSH)
7227   {
7228     int from_lx, from_ly;
7229
7230     if (from_x > to_x)
7231       swap_numbers(&from_x, &to_x);
7232
7233     if (from_y > to_y)
7234       swap_numbers(&from_y, &to_y);
7235
7236     brush_width = to_x - from_x + 1;
7237     brush_height = to_y - from_y + 1;
7238
7239     from_lx = from_x + level_xpos;
7240     from_ly = from_y + level_ypos;
7241
7242     for (y = 0; y < brush_height; y++)
7243     {
7244       for (x=0; x < brush_width; x++)
7245       {
7246         brush_buffer[x][y] = Feld[from_lx + x][from_ly + y];
7247
7248         if (button != 1)
7249           DrawLineElement(from_x + x, from_y + y, new_element, TRUE);
7250       }
7251     }
7252
7253     if (button != 1)
7254       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
7255
7256     delete_old_brush = FALSE;
7257   }
7258   else if (mode == CB_BRUSH_TO_CURSOR || mode == CB_DELETE_OLD_CURSOR ||
7259            mode == CB_BRUSH_TO_LEVEL)
7260   {
7261     int cursor_x = (mode == CB_DELETE_OLD_CURSOR ? last_cursor_x : from_x);
7262     int cursor_y = (mode == CB_DELETE_OLD_CURSOR ? last_cursor_y : from_y);
7263     int cursor_from_x = cursor_x - brush_width / 2;
7264     int cursor_from_y = cursor_y - brush_height / 2;
7265     int border_from_x = cursor_x, border_from_y = cursor_y;
7266     int border_to_x = cursor_x, border_to_y = cursor_y;
7267
7268     if (mode != CB_DELETE_OLD_CURSOR && delete_old_brush)
7269       CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
7270
7271     if (!IN_ED_FIELD(cursor_x, cursor_y) ||
7272         !IN_LEV_FIELD(cursor_x + level_xpos, cursor_y + level_ypos))
7273     {
7274       delete_old_brush = FALSE;
7275       return;
7276     }
7277
7278     for (y = 0; y < brush_height; y++)
7279     {
7280       for (x = 0; x < brush_width; x++)
7281       {
7282         int sx = cursor_from_x + x;
7283         int sy = cursor_from_y + y;
7284         int lx = sx + level_xpos;
7285         int ly = sy + level_ypos;
7286         boolean change_level = (mode == CB_BRUSH_TO_LEVEL);
7287         int element = (mode == CB_DELETE_OLD_CURSOR ? -1 :
7288                        mode == CB_BRUSH_TO_CURSOR || button == 1 ?
7289                        brush_buffer[x][y] : new_element);
7290
7291         if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
7292         {
7293           if (sx < border_from_x)
7294             border_from_x = sx;
7295           else if (sx > border_to_x)
7296             border_to_x = sx;
7297           if (sy < border_from_y)
7298             border_from_y = sy;
7299           else if (sy > border_to_y)
7300             border_to_y = sy;
7301
7302           DrawLineElement(sx, sy, element, change_level);
7303         }
7304       }
7305     }
7306
7307     if (mode != CB_DELETE_OLD_CURSOR)
7308       DrawAreaBorder(border_from_x, border_from_y, border_to_x, border_to_y);
7309
7310     last_cursor_x = cursor_x;
7311     last_cursor_y = cursor_y;
7312     delete_old_brush = TRUE;
7313   }
7314 }
7315
7316 static void CopyAreaToBrush(int from_x, int from_y, int to_x, int to_y,
7317                             int button)
7318 {
7319   CopyBrushExt(from_x, from_y, to_x, to_y, button, CB_AREA_TO_BRUSH);
7320 }
7321
7322 static void CopyBrushToLevel(int x, int y, int button)
7323 {
7324   CopyBrushExt(x, y, 0, 0, button, CB_BRUSH_TO_LEVEL);
7325 }
7326
7327 static void CopyBrushToCursor(int x, int y)
7328 {
7329   CopyBrushExt(x, y, 0, 0, 0, CB_BRUSH_TO_CURSOR);
7330 }
7331
7332 static void DeleteBrushFromCursor()
7333 {
7334   CopyBrushExt(0, 0, 0, 0, 0, CB_DELETE_OLD_CURSOR);
7335 }
7336
7337 void DumpBrush()
7338 {
7339   CopyBrushExt(0, 0, 0, 0, 0, CB_DUMP_BRUSH);
7340 }
7341
7342 static void FloodFill(int from_x, int from_y, int fill_element)
7343 {
7344   int i,x,y;
7345   int old_element;
7346   static int check[4][2] = { {-1,0}, {0,-1}, {1,0}, {0,1} };
7347   static int safety = 0;
7348
7349   /* check if starting field still has the desired content */
7350   if (Feld[from_x][from_y] == fill_element)
7351     return;
7352
7353   safety++;
7354
7355   if (safety > lev_fieldx*lev_fieldy)
7356     Error(ERR_EXIT, "Something went wrong in 'FloodFill()'. Please debug.");
7357
7358   old_element = Feld[from_x][from_y];
7359   Feld[from_x][from_y] = fill_element;
7360
7361   for (i = 0; i < 4; i++)
7362   {
7363     x = from_x + check[i][0];
7364     y = from_y + check[i][1];
7365
7366     if (IN_LEV_FIELD(x,y) && Feld[x][y] == old_element)
7367       FloodFill(x, y, fill_element);
7368   }
7369
7370   safety--;
7371 }
7372
7373 /* values for DrawLevelText() modes */
7374 #define TEXT_INIT               0
7375 #define TEXT_SETCURSOR          1
7376 #define TEXT_WRITECHAR          2
7377 #define TEXT_BACKSPACE          3
7378 #define TEXT_NEWLINE            4
7379 #define TEXT_END                5
7380 #define TEXT_QUERY_TYPING       6
7381
7382 static int DrawLevelText(int sx, int sy, char letter, int mode)
7383 {
7384   static short delete_buffer[MAX_LEV_FIELDX];
7385   static int start_sx, start_sy;
7386   static int last_sx, last_sy;
7387   static boolean typing = FALSE;
7388   int letter_element = EL_CHAR_ASCII0 + letter;
7389   int lx = 0, ly = 0;
7390
7391   /* map lower case letters to upper case and convert special characters */
7392   if (letter >= 'a' && letter <= 'z')
7393     letter_element = EL_CHAR_ASCII0 + letter + (int)('A' - 'a');
7394   else if (letter == 'ä' || letter == 'Ä')
7395     letter_element = EL_CHAR_AUMLAUT;
7396   else if (letter == 'ö' || letter == 'Ö')
7397     letter_element = EL_CHAR_OUMLAUT;
7398   else if (letter == 'ü' || letter == 'Ãœ')
7399     letter_element = EL_CHAR_UUMLAUT;
7400   else if (letter == '^')
7401     letter_element = EL_CHAR_COPYRIGHT;
7402   else
7403     letter_element = EL_CHAR_ASCII0 + letter;
7404
7405   if (mode != TEXT_INIT)
7406   {
7407     if (!typing)
7408       return FALSE;
7409
7410     if (mode != TEXT_SETCURSOR)
7411     {
7412       sx = last_sx;
7413       sy = last_sy;
7414     }
7415
7416     lx = last_sx + level_xpos;
7417     ly = last_sy + level_ypos;
7418   }
7419
7420   switch (mode)
7421   {
7422     case TEXT_INIT:
7423       if (typing)
7424         DrawLevelText(0, 0, 0, TEXT_END);
7425
7426       typing = TRUE;
7427       start_sx = last_sx = sx;
7428       start_sy = last_sy = sy;
7429       DrawLevelText(sx, sy, 0, TEXT_SETCURSOR);
7430       break;
7431
7432     case TEXT_SETCURSOR:
7433       DrawMiniElement(last_sx, last_sy, Feld[lx][ly]);
7434       DrawAreaBorder(sx, sy, sx, sy);
7435       last_sx = sx;
7436       last_sy = sy;
7437       break;
7438
7439     case TEXT_WRITECHAR:
7440       if (letter_element >= EL_CHAR_START && letter_element <= EL_CHAR_END)
7441       {
7442         delete_buffer[sx - start_sx] = Feld[lx][ly];
7443         Feld[lx][ly] = letter_element;
7444
7445         if (sx + 1 < ed_fieldx && lx + 1 < lev_fieldx)
7446           DrawLevelText(sx + 1, sy, 0, TEXT_SETCURSOR);
7447         else if (sy + 1 < ed_fieldy && ly + 1 < lev_fieldy)
7448           DrawLevelText(start_sx, sy + 1, 0, TEXT_SETCURSOR);
7449         else
7450           DrawLevelText(0, 0, 0, TEXT_END);
7451       }
7452       break;
7453
7454     case TEXT_BACKSPACE:
7455       if (sx > start_sx)
7456       {
7457         Feld[lx - 1][ly] = delete_buffer[sx - start_sx - 1];
7458         DrawMiniElement(sx - 1, sy, Feld[lx - 1][ly]);
7459         DrawLevelText(sx - 1, sy, 0, TEXT_SETCURSOR);
7460       }
7461       break;
7462
7463     case TEXT_NEWLINE:
7464       if (sy + 1 < ed_fieldy - 1 && ly + 1 < lev_fieldy - 1)
7465         DrawLevelText(start_sx, sy + 1, 0, TEXT_SETCURSOR);
7466       else
7467         DrawLevelText(0, 0, 0, TEXT_END);
7468       break;
7469
7470     case TEXT_END:
7471       CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
7472       DrawMiniElement(sx, sy, Feld[lx][ly]);
7473       typing = FALSE;
7474       break;
7475
7476     case TEXT_QUERY_TYPING:
7477       break;
7478
7479     default:
7480       break;
7481   }
7482
7483   return typing;
7484 }
7485
7486 static void SetTextCursor(int unused_sx, int unused_sy, int sx, int sy,
7487                           int element, boolean change_level)
7488 {
7489   int lx = sx + level_xpos;
7490   int ly = sy + level_ypos;
7491
7492   if (element == -1)
7493     DrawMiniElement(sx, sy, Feld[lx][ly]);
7494   else
7495     DrawAreaBorder(sx, sy, sx, sy);
7496 }
7497
7498 static void CopyLevelToUndoBuffer(int mode)
7499 {
7500   static boolean accumulated_undo = FALSE;
7501   boolean new_undo_buffer_position = TRUE;
7502   int last_border_element;
7503   int x, y;
7504
7505   switch (mode)
7506   {
7507     case UNDO_IMMEDIATE:
7508       accumulated_undo = FALSE;
7509       break;
7510
7511     case UNDO_ACCUMULATE:
7512       if (accumulated_undo)
7513         new_undo_buffer_position = FALSE;
7514       accumulated_undo = TRUE;
7515       break;
7516
7517     default:
7518       break;
7519   }
7520
7521   if (new_undo_buffer_position)
7522   {
7523     /* new position in undo buffer ring */
7524     undo_buffer_position = (undo_buffer_position + 1) % NUM_UNDO_STEPS;
7525
7526     if (undo_buffer_steps < NUM_UNDO_STEPS - 1)
7527       undo_buffer_steps++;
7528   }
7529
7530   for (x = 0; x < lev_fieldx; x++)
7531     for (y = 0; y < lev_fieldy; y++)
7532       UndoBuffer[undo_buffer_position][x][y] = Feld[x][y];
7533
7534   /* check if drawing operation forces change of border style */
7535   last_border_element = BorderElement;
7536   SetBorderElement();
7537   if (BorderElement != last_border_element)
7538     DrawMiniLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
7539 }
7540
7541 static void RandomPlacement(int new_element)
7542 {
7543   static boolean free_position[MAX_LEV_FIELDX][MAX_LEV_FIELDY];
7544   int num_free_positions;
7545   int num_percentage;
7546   int num_elements;
7547   int x, y;
7548
7549   /* determine number of free positions for the new elements */
7550   /* (maybe this statement should be formatted a bit more readable...) */
7551   num_free_positions = 0;
7552   for (x = 0; x < lev_fieldx; x++)
7553     for (y = 0; y < lev_fieldy; y++)
7554       if ((free_position[x][y] =
7555            ((random_placement_background_restricted &&
7556              Feld[x][y] == random_placement_background_element) ||
7557             (!random_placement_background_restricted &&
7558              Feld[x][y] != new_element))) == TRUE)
7559         num_free_positions++;
7560
7561   /* determine number of new elements to place there */
7562   num_percentage = num_free_positions * random_placement_value / 100;
7563   num_elements = (random_placement_method == RANDOM_USE_PERCENTAGE ?
7564                   num_percentage : random_placement_value);
7565
7566   /* if not more free positions than elements to place, fill whole level */
7567   if (num_elements >= num_free_positions)
7568   {
7569     for (x = 0; x < lev_fieldx; x++)
7570       for (y = 0; y < lev_fieldy; y++)
7571         Feld[x][y] = new_element;
7572
7573     DrawMiniLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
7574     CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
7575     return;
7576   }
7577
7578   while (num_elements > 0)
7579   {
7580     x = RND(lev_fieldx);
7581     y = RND(lev_fieldy);
7582
7583     /* don't place element at the same position twice */
7584     if (free_position[x][y])
7585     {
7586       free_position[x][y] = FALSE;
7587       Feld[x][y] = new_element;
7588       num_elements--;
7589     }
7590   }
7591
7592   DrawMiniLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
7593   CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
7594 }
7595
7596 void WrapLevel(int dx, int dy)
7597 {
7598   int wrap_dx = lev_fieldx - dx;
7599   int wrap_dy = lev_fieldy - dy;
7600   int x, y;
7601
7602   for (x = 0; x < lev_fieldx; x++)
7603     for (y = 0; y < lev_fieldy; y++)
7604       FieldBackup[x][y] = Feld[x][y];
7605
7606   for (x = 0; x < lev_fieldx; x++)
7607     for (y = 0; y < lev_fieldy; y++)
7608       Feld[x][y] =
7609         FieldBackup[(x + wrap_dx) % lev_fieldx][(y + wrap_dy) % lev_fieldy];
7610
7611   DrawMiniLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
7612   CopyLevelToUndoBuffer(UNDO_ACCUMULATE);
7613 }
7614
7615 static void HandleDrawingAreas(struct GadgetInfo *gi)
7616 {
7617   static boolean started_inside_drawing_area = FALSE;
7618   int id = gi->custom_id;
7619   boolean button_press_event;
7620   boolean button_release_event;
7621   boolean inside_drawing_area = !gi->event.off_borders;
7622   boolean draw_level = (id == GADGET_ID_DRAWING_LEVEL);
7623   int actual_drawing_function;
7624   int button = gi->event.button;
7625   int new_element = BUTTON_ELEMENT(button);
7626   int sx = gi->event.x, sy = gi->event.y;
7627   int min_sx = 0, min_sy = 0;
7628   int max_sx = gi->drawing.area_xsize - 1, max_sy = gi->drawing.area_ysize - 1;
7629   int item_xsize = gi->drawing.item_xsize, item_ysize = gi->drawing.item_ysize;
7630   int lx = 0, ly = 0;
7631   int min_lx = 0, min_ly = 0;
7632   int max_lx = lev_fieldx - 1, max_ly = lev_fieldy - 1;
7633   int x, y;
7634
7635   /* handle info callback for each invocation of action callback */
7636   gi->callback_info(gi);
7637
7638   button_press_event = (gi->event.type == GD_EVENT_PRESSED);
7639   button_release_event = (gi->event.type == GD_EVENT_RELEASED);
7640
7641   /* make sure to stay inside drawing area boundaries */
7642   sx = (sx < min_sx ? min_sx : sx > max_sx ? max_sx : sx);
7643   sy = (sy < min_sy ? min_sy : sy > max_sy ? max_sy : sy);
7644
7645   if (draw_level)
7646   {
7647     /* get positions inside level field */
7648     lx = sx + level_xpos;
7649     ly = sy + level_ypos;
7650
7651     if (!IN_LEV_FIELD(lx, ly))
7652       inside_drawing_area = FALSE;
7653
7654     /* make sure to stay inside level field boundaries */
7655     lx = (lx < min_lx ? min_lx : lx > max_lx ? max_lx : lx);
7656     ly = (ly < min_ly ? min_ly : ly > max_ly ? max_ly : ly);
7657
7658     /* correct drawing area positions accordingly */
7659     sx = lx - level_xpos;
7660     sy = ly - level_ypos;
7661   }
7662
7663   if (button_press_event)
7664     started_inside_drawing_area = inside_drawing_area;
7665
7666   if (!started_inside_drawing_area)
7667     return;
7668
7669   if (!button && !button_release_event)
7670     return;
7671
7672   /* automatically switch to 'single item' drawing mode, if needed */
7673   actual_drawing_function =
7674     (draw_level || drawing_function == GADGET_ID_PICK_ELEMENT ?
7675      drawing_function : GADGET_ID_SINGLE_ITEMS);
7676
7677   /* clicking into drawing area with pressed Control key picks element */
7678   if (GetKeyModState() & KMOD_Control)
7679   {
7680     last_drawing_function = drawing_function;
7681     actual_drawing_function = GADGET_ID_PICK_ELEMENT;
7682   }
7683
7684   switch (actual_drawing_function)
7685   {
7686     case GADGET_ID_SINGLE_ITEMS:
7687       if (draw_level)
7688       {
7689         if (button_release_event)
7690         {
7691           CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
7692
7693           if (edit_mode == ED_MODE_DRAWING && draw_with_brush &&
7694               !inside_drawing_area)
7695             DeleteBrushFromCursor();
7696         }
7697
7698         if (!button)
7699           break;
7700
7701         if (draw_with_brush)
7702         {
7703           if (!button_release_event)
7704             CopyBrushToLevel(sx, sy, button);
7705         }
7706         else if (new_element != Feld[lx][ly])
7707         {
7708           if (new_element == EL_PLAYER_1)
7709           {
7710             /* remove player at old position */
7711             for (y = 0; y < lev_fieldy; y++)
7712             {
7713               for (x = 0; x < lev_fieldx; x++)
7714               {
7715                 if (Feld[x][y] == EL_PLAYER_1)
7716                 {
7717                   Feld[x][y] = EL_EMPTY;
7718                   if (x - level_xpos >= 0 && x - level_xpos < ed_fieldx &&
7719                       y - level_ypos >= 0 && y - level_ypos < ed_fieldy)
7720                     DrawMiniElement(x - level_xpos, y - level_ypos,
7721                                     EL_EMPTY);
7722                 }
7723               }
7724             }
7725           }
7726
7727           Feld[lx][ly] = new_element;
7728           DrawMiniElement(sx, sy, new_element);
7729         }
7730       }
7731       else
7732       {
7733         if (item_xsize == MINI_TILEX && item_ysize == MINI_TILEY)
7734           DrawMiniGraphicExt(drawto,
7735                              gi->x + sx * MINI_TILEX,
7736                              gi->y + sy * MINI_TILEY,
7737                              el2edimg(new_element));
7738         else
7739           DrawGraphicExt(drawto,
7740                          gi->x + sx * TILEX,
7741                          gi->y + sy * TILEY,
7742                          el2img(new_element), 0);
7743
7744         if (id == GADGET_ID_AMOEBA_CONTENT)
7745           level.amoeba_content = new_element;
7746         else if (id == GADGET_ID_CUSTOM_GRAPHIC)
7747         {
7748           new_element = GFX_ELEMENT(new_element);
7749           custom_element.gfx_element = new_element;
7750
7751           CopyElementPropertiesToGame(properties_element);
7752
7753           UpdateCustomElementGraphicGadgets();
7754
7755           FrameCounter = 0;     /* restart animation frame counter */
7756         }
7757         else if (id == GADGET_ID_CUSTOM_CONTENT)
7758         {
7759           custom_element.content[sx][sy] = new_element;
7760
7761           CopyCustomElementPropertiesToGame(properties_element);
7762         }
7763         else if (id == GADGET_ID_CUSTOM_MOVE_ENTER)
7764         {
7765           custom_element.move_enter_element = new_element;
7766
7767           CopyCustomElementPropertiesToGame(properties_element);
7768         }
7769         else if (id == GADGET_ID_CUSTOM_MOVE_LEAVE)
7770         {
7771           custom_element.move_leave_element = new_element;
7772
7773           CopyCustomElementPropertiesToGame(properties_element);
7774         }
7775         else if (id == GADGET_ID_CUSTOM_CHANGE_TARGET)
7776         {
7777           custom_element_change.target_element = new_element;
7778
7779           CopyCustomElementPropertiesToGame(properties_element);
7780         }
7781         else if (id == GADGET_ID_CUSTOM_CHANGE_CONTENT)
7782         {
7783           custom_element_change.content[sx][sy] = new_element;
7784
7785           CopyCustomElementPropertiesToGame(properties_element);
7786         }
7787         else if (id == GADGET_ID_CUSTOM_CHANGE_TRIGGER)
7788         {
7789           custom_element_change.trigger_element = new_element;
7790
7791           CopyCustomElementPropertiesToGame(properties_element);
7792         }
7793         else if (id == GADGET_ID_GROUP_CONTENT)
7794         {
7795           group_element_info.element[sx] = new_element;
7796
7797           CopyGroupElementPropertiesToGame(properties_element);
7798         }
7799         else if (id == GADGET_ID_RANDOM_BACKGROUND)
7800           random_placement_background_element = new_element;
7801         else if (id >= GADGET_ID_ELEMENT_CONTENT_0 &&
7802                  id <= GADGET_ID_ELEMENT_CONTENT_7)
7803           level.yamyam_content[id - GADGET_ID_ELEMENT_CONTENT_0][sx][sy] =
7804             new_element;
7805       }
7806       break;
7807
7808     case GADGET_ID_CONNECTED_ITEMS:
7809       {
7810         static int last_sx = -1;
7811         static int last_sy = -1;
7812
7813         if (button_release_event)
7814           CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
7815
7816         if (button)
7817         {
7818           if (!button_press_event)
7819             DrawLine(last_sx, last_sy, sx, sy, new_element, TRUE);
7820
7821           last_sx = sx;
7822           last_sy = sy;
7823         }
7824       }
7825       break;
7826
7827     case GADGET_ID_LINE:
7828     case GADGET_ID_ARC:
7829     case GADGET_ID_RECTANGLE:
7830     case GADGET_ID_FILLED_BOX:
7831     case GADGET_ID_GRAB_BRUSH:
7832     case GADGET_ID_TEXT:
7833       {
7834         static int last_sx = -1;
7835         static int last_sy = -1;
7836         static int start_sx = -1;
7837         static int start_sy = -1;
7838         void (*draw_func)(int, int, int, int, int, boolean);
7839
7840         if (drawing_function == GADGET_ID_LINE)
7841           draw_func = DrawLine;
7842         else if (drawing_function == GADGET_ID_ARC)
7843           draw_func = DrawArc;
7844         else if (drawing_function == GADGET_ID_RECTANGLE)
7845           draw_func = DrawBox;
7846         else if (drawing_function == GADGET_ID_FILLED_BOX)
7847           draw_func = DrawFilledBox;
7848         else if (drawing_function == GADGET_ID_GRAB_BRUSH)
7849           draw_func = SelectArea;
7850         else /* (drawing_function == GADGET_ID_TEXT) */
7851           draw_func = SetTextCursor;
7852
7853         if (button_press_event)
7854         {
7855           draw_func(sx, sy, sx, sy, new_element, FALSE);
7856           start_sx = last_sx = sx;
7857           start_sy = last_sy = sy;
7858
7859           if (drawing_function == GADGET_ID_TEXT)
7860             DrawLevelText(0, 0, 0, TEXT_END);
7861         }
7862         else if (button_release_event)
7863         {
7864           draw_func(start_sx, start_sy, sx, sy, new_element, TRUE);
7865           if (drawing_function == GADGET_ID_GRAB_BRUSH)
7866           {
7867             CopyAreaToBrush(start_sx, start_sy, sx, sy, button);
7868             CopyBrushToCursor(sx, sy);
7869             ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS],
7870                           MB_LEFTBUTTON);
7871             draw_with_brush = TRUE;
7872           }
7873           else if (drawing_function == GADGET_ID_TEXT)
7874             DrawLevelText(sx, sy, 0, TEXT_INIT);
7875           else
7876             CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
7877         }
7878         else if (last_sx != sx || last_sy != sy)
7879         {
7880           draw_func(start_sx, start_sy, last_sx, last_sy, -1, FALSE);
7881           draw_func(start_sx, start_sy, sx, sy, new_element, FALSE);
7882           last_sx = sx;
7883           last_sy = sy;
7884         }
7885       }
7886       break;
7887
7888     case GADGET_ID_FLOOD_FILL:
7889       if (button_press_event && Feld[lx][ly] != new_element)
7890       {
7891         FloodFill(lx, ly, new_element);
7892         DrawMiniLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
7893         CopyLevelToUndoBuffer(UNDO_IMMEDIATE);
7894       }
7895       break;
7896
7897     case GADGET_ID_PICK_ELEMENT:
7898       if (button_release_event)
7899         ClickOnGadget(level_editor_gadget[last_drawing_function],
7900                       MB_LEFTBUTTON);
7901       else if (draw_level)
7902         PickDrawingElement(button, Feld[lx][ly]);
7903       else if (id == GADGET_ID_AMOEBA_CONTENT)
7904         PickDrawingElement(button, level.amoeba_content);
7905       else if (id == GADGET_ID_CUSTOM_GRAPHIC)
7906         PickDrawingElement(button, custom_element.gfx_element);
7907       else if (id == GADGET_ID_CUSTOM_CONTENT)
7908         PickDrawingElement(button, custom_element.content[sx][sy]);
7909       else if (id == GADGET_ID_CUSTOM_MOVE_ENTER)
7910         PickDrawingElement(button, custom_element.move_enter_element);
7911       else if (id == GADGET_ID_CUSTOM_MOVE_LEAVE)
7912         PickDrawingElement(button, custom_element.move_leave_element);
7913       else if (id == GADGET_ID_CUSTOM_CHANGE_TARGET)
7914         PickDrawingElement(button, custom_element_change.target_element);
7915       else if (id == GADGET_ID_CUSTOM_CHANGE_CONTENT)
7916         PickDrawingElement(button, custom_element_change.content[sx][sy]);
7917       else if (id == GADGET_ID_CUSTOM_CHANGE_TRIGGER)
7918         PickDrawingElement(button, custom_element_change.trigger_element);
7919       else if (id == GADGET_ID_GROUP_CONTENT)
7920         PickDrawingElement(button, group_element_info.element[sx]);
7921       else if (id == GADGET_ID_RANDOM_BACKGROUND)
7922         PickDrawingElement(button, random_placement_background_element);
7923       else if (id >= GADGET_ID_ELEMENT_CONTENT_0 &&
7924                id <= GADGET_ID_ELEMENT_CONTENT_7)
7925       {
7926         int i = id - GADGET_ID_ELEMENT_CONTENT_0;
7927
7928         PickDrawingElement(button, level.yamyam_content[i][sx][sy]);
7929       }
7930
7931       break;
7932
7933     default:
7934       break;
7935   }
7936 }
7937
7938 static void HandleCounterButtons(struct GadgetInfo *gi)
7939 {
7940   int gadget_id = gi->custom_id;
7941   int counter_id = gi->custom_type_id;
7942   int button = gi->event.button;
7943   int *counter_value = counterbutton_info[counter_id].value;
7944   int step = BUTTON_STEPSIZE(button) *
7945     (gadget_id == counterbutton_info[counter_id].gadget_id_down ? -1 : +1);
7946
7947   if (counter_id == ED_COUNTER_ID_SELECT_LEVEL)
7948   {
7949     boolean pressed = (gi->event.type == GD_EVENT_PRESSED);
7950     boolean released = (gi->event.type == GD_EVENT_RELEASED);
7951     boolean level_changed = LevelChanged();
7952
7953     if ((level_changed && pressed) || (!level_changed && released))
7954       return;
7955
7956     if (level_changed && !Request("Level has changed! Discard changes ?",
7957                                   REQ_ASK))
7958     {
7959       if (gadget_id == counterbutton_info[counter_id].gadget_id_text)
7960         ModifyEditorCounter(counter_id, *counter_value);
7961       return;
7962     }
7963   }
7964
7965   if (gadget_id == counterbutton_info[counter_id].gadget_id_text)
7966     *counter_value = gi->textinput.number_value;
7967   else
7968     ModifyEditorCounter(counter_id, *counter_value + step);
7969
7970   switch (counter_id)
7971   {
7972     case ED_COUNTER_ID_ELEMENT_CONTENT:
7973       DrawElementContentAreas();
7974       break;
7975
7976     case ED_COUNTER_ID_GROUP_CONTENT:
7977       DrawGroupElementArea(properties_element);
7978       CopyGroupElementPropertiesToGame(properties_element);
7979       break;
7980
7981     case ED_COUNTER_ID_ENVELOPE_XSIZE:
7982     case ED_COUNTER_ID_ENVELOPE_YSIZE:
7983       DrawEnvelopeTextArea(-1);
7984       break;
7985
7986     case ED_COUNTER_ID_LEVEL_XSIZE:
7987     case ED_COUNTER_ID_LEVEL_YSIZE:
7988       lev_fieldx = level.fieldx;
7989       lev_fieldy = level.fieldy;
7990       break;
7991
7992     case ED_COUNTER_ID_SELECT_LEVEL:
7993       LoadLevel(level_nr);
7994       TapeErase();
7995       ResetUndoBuffer();
7996       DrawEditModeWindow();
7997       break;
7998
7999     default:
8000       break;
8001   }
8002
8003   if ((counter_id >= ED_COUNTER_ID_CUSTOM_FIRST &&
8004        counter_id <= ED_COUNTER_ID_CUSTOM_LAST) ||
8005       (counter_id >= ED_COUNTER_ID_CHANGE_FIRST &&
8006        counter_id <= ED_COUNTER_ID_CHANGE_LAST))
8007     CopyElementPropertiesToGame(properties_element);
8008 }
8009
8010 static void HandleTextInputGadgets(struct GadgetInfo *gi)
8011 {
8012   int type_id = gi->custom_type_id;
8013
8014   strcpy(textinput_info[type_id].value, gi->textinput.value);
8015
8016   if (type_id == ED_TEXTINPUT_ID_ELEMENT_NAME)
8017   {
8018     CopyElementPropertiesToGame(properties_element);
8019
8020     ModifyEditorElementList();  /* update changed button info text */
8021   }
8022 }
8023
8024 static void HandleTextAreaGadgets(struct GadgetInfo *gi)
8025 {
8026   int type_id = gi->custom_type_id;
8027
8028   strcpy(textarea_info[type_id].value, gi->textarea.value);
8029 }
8030
8031 static void HandleSelectboxGadgets(struct GadgetInfo *gi)
8032 {
8033   int type_id = gi->custom_type_id;
8034
8035   *selectbox_info[type_id].value =
8036     selectbox_info[type_id].options[gi->selectbox.index].value;
8037
8038   if (type_id == ED_SELECTBOX_ID_SELECT_CHANGE_PAGE)
8039   {
8040     element_info[properties_element].current_change_page = gi->selectbox.index;
8041
8042     DrawPropertiesWindow();
8043   }
8044   else if ((type_id >= ED_SELECTBOX_ID_CUSTOM_FIRST &&
8045             type_id <= ED_SELECTBOX_ID_CUSTOM_LAST) ||
8046            (type_id >= ED_SELECTBOX_ID_CHANGE_FIRST &&
8047             type_id <= ED_SELECTBOX_ID_CHANGE_LAST) ||
8048            (type_id == ED_SELECTBOX_ID_GROUP_CHOICE_MODE))
8049     CopyElementPropertiesToGame(properties_element);
8050 }
8051
8052 static void HandleTextbuttonGadgets(struct GadgetInfo *gi)
8053 {
8054   int type_id = gi->custom_type_id;
8055   int i;
8056
8057   if (type_id >= ED_TEXTBUTTON_ID_PROPERTIES_FIRST &&
8058       type_id <= ED_TEXTBUTTON_ID_PROPERTIES_LAST)
8059   {
8060     edit_mode_properties = gi->custom_type_id;
8061
8062     DrawPropertiesWindow();
8063   }
8064   else if (type_id == ED_TEXTBUTTON_ID_SAVE_AS_TEMPLATE)
8065   {
8066     char *template_filename = getDefaultLevelFilename(-1);
8067     boolean new_template = !fileExists(template_filename);
8068
8069     if (new_template ||
8070         Request("Save this template and kill the old ?", REQ_ASK))
8071       SaveLevelTemplate();
8072
8073     if (new_template)
8074       Request("Template saved !", REQ_CONFIRM);
8075   }
8076   else if (type_id == ED_TEXTBUTTON_ID_ADD_CHANGE_PAGE &&
8077            custom_element.num_change_pages < MAX_CHANGE_PAGES)
8078   {
8079     struct ElementInfo *ei = &element_info[properties_element];
8080
8081     setElementChangePages(ei, ei->num_change_pages + 1);
8082
8083     /* set new change page to be new current change page */
8084     ei->current_change_page = ei->num_change_pages - 1;
8085     ei->change = &ei->change_page[ei->current_change_page];
8086
8087     setElementChangeInfoToDefaults(ei->change);
8088
8089     DrawPropertiesWindow();
8090   }
8091   else if (type_id == ED_TEXTBUTTON_ID_DEL_CHANGE_PAGE &&
8092            custom_element.num_change_pages > MIN_CHANGE_PAGES)
8093   {
8094     struct ElementInfo *ei = &element_info[properties_element];
8095
8096     /* copy all change pages after change page to be deleted */
8097     for (i = ei->current_change_page; i < ei->num_change_pages - 1; i++)
8098       ei->change_page[i] = ei->change_page[i + 1];
8099
8100     setElementChangePages(ei, ei->num_change_pages - 1);
8101
8102     DrawPropertiesWindow();
8103   }
8104 }
8105
8106 static void HandleGraphicbuttonGadgets(struct GadgetInfo *gi)
8107 {
8108   int type_id = gi->custom_type_id;
8109
8110   if (type_id == ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE ||
8111       type_id == ED_GRAPHICBUTTON_ID_NEXT_CHANGE_PAGE)
8112   {
8113     struct ElementInfo *ei = &element_info[properties_element];
8114     int step = BUTTON_STEPSIZE(gi->event.button);
8115
8116     step *= (type_id == ED_GRAPHICBUTTON_ID_PREV_CHANGE_PAGE ? -1 : +1);
8117     ei->current_change_page += step;
8118
8119     if (ei->current_change_page < 0)
8120       ei->current_change_page = 0;
8121     else if (ei->current_change_page >= ei->num_change_pages)
8122       ei->current_change_page = ei->num_change_pages - 1;
8123
8124     DrawPropertiesWindow();
8125   }
8126 }
8127
8128 static void HandleRadiobuttons(struct GadgetInfo *gi)
8129 {
8130   *radiobutton_info[gi->custom_type_id].value =
8131     radiobutton_info[gi->custom_type_id].checked_value;
8132 }
8133
8134 static void HandleCheckbuttons(struct GadgetInfo *gi)
8135 {
8136   int type_id = gi->custom_type_id;
8137
8138   *checkbutton_info[type_id].value ^= TRUE;
8139
8140   if (((type_id >= ED_CHECKBUTTON_ID_CUSTOM_FIRST &&
8141         type_id <= ED_CHECKBUTTON_ID_CUSTOM_LAST) ||
8142        (type_id >= ED_CHECKBUTTON_ID_CHANGE_FIRST &&
8143         type_id <= ED_CHECKBUTTON_ID_CHANGE_LAST)) &&
8144       type_id != ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE)
8145   {
8146     CopyElementPropertiesToGame(properties_element);
8147   }
8148
8149   if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_GRAPHIC)
8150   {
8151     UpdateCustomElementGraphicGadgets();
8152   }
8153   else if (type_id == ED_CHECKBUTTON_ID_CUSTOM_USE_TEMPLATE)
8154   {
8155     char *template_filename = getDefaultLevelFilename(-1);
8156
8157     if (level.use_custom_template && !fileExists(template_filename))
8158     {
8159       Request("No level template found !", REQ_CONFIRM);
8160
8161       level.use_custom_template = FALSE;
8162       ModifyGadget(gi, GDI_CHECKED, FALSE, GDI_END);
8163
8164       return;
8165     }
8166
8167     LoadLevelTemplate(level.use_custom_template ? -1 : level_nr);
8168
8169     DrawEditModeWindow();
8170   }
8171 }
8172
8173 static void HandleControlButtons(struct GadgetInfo *gi)
8174 {
8175   static int last_level_drawing_function = GADGET_ID_SINGLE_ITEMS;
8176   static int last_edit_mode = ED_MODE_DRAWING;
8177   static int last_custom_copy_mode = -1;
8178   int id = gi->custom_id;
8179   int button = gi->event.button;
8180   int step = BUTTON_STEPSIZE(button);
8181   int new_element = BUTTON_ELEMENT(button);
8182   int x, y;
8183
8184   if (edit_mode == ED_MODE_DRAWING && drawing_function == GADGET_ID_TEXT)
8185     DrawLevelText(0, 0, 0, TEXT_END);
8186
8187   if (id < ED_NUM_CTRL1_BUTTONS &&
8188       id != GADGET_ID_SINGLE_ITEMS &&
8189       id != GADGET_ID_PROPERTIES &&
8190       id != GADGET_ID_PICK_ELEMENT &&
8191       edit_mode != ED_MODE_DRAWING &&
8192       drawing_function != GADGET_ID_PICK_ELEMENT &&
8193       !(GetKeyModState() & KMOD_Control))
8194   {
8195     DrawDrawingWindow();
8196     edit_mode = ED_MODE_DRAWING;
8197   }
8198
8199   /* element copy mode active, but no element button pressed => deactivate */
8200   if (last_custom_copy_mode != -1 && id < ED_NUM_CTRL_BUTTONS)
8201     last_custom_copy_mode = -1;
8202
8203   switch (id)
8204   {
8205     case GADGET_ID_SCROLL_LEFT:
8206       if (level_xpos >= 0)
8207       {
8208         if (lev_fieldx < ed_fieldx - 2)
8209           break;
8210
8211         level_xpos -= step;
8212         if (level_xpos < -1)
8213           level_xpos = -1;
8214         if (button == 1)
8215           ScrollMiniLevel(level_xpos, level_ypos, ED_SCROLL_RIGHT);
8216         else
8217           DrawMiniLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
8218
8219         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
8220                      GDI_SCROLLBAR_ITEM_POSITION, level_xpos + 1, GDI_END);
8221       }
8222       break;
8223
8224     case GADGET_ID_SCROLL_RIGHT:
8225       if (level_xpos <= lev_fieldx - ed_fieldx)
8226       {
8227         if (lev_fieldx < ed_fieldx - 2)
8228           break;
8229
8230         level_xpos += step;
8231         if (level_xpos > lev_fieldx - ed_fieldx + 1)
8232           level_xpos = lev_fieldx - ed_fieldx + 1;
8233         if (button == 1)
8234           ScrollMiniLevel(level_xpos, level_ypos, ED_SCROLL_LEFT);
8235         else
8236           DrawMiniLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
8237
8238         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_HORIZONTAL],
8239                      GDI_SCROLLBAR_ITEM_POSITION, level_xpos + 1, GDI_END);
8240       }
8241       break;
8242
8243     case GADGET_ID_SCROLL_UP:
8244       if (level_ypos >= 0)
8245       {
8246         if (lev_fieldy < ed_fieldy - 2)
8247           break;
8248
8249         level_ypos -= step;
8250         if (level_ypos < -1)
8251           level_ypos = -1;
8252         if (button == 1)
8253           ScrollMiniLevel(level_xpos, level_ypos, ED_SCROLL_DOWN);
8254         else
8255           DrawMiniLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
8256
8257         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
8258                      GDI_SCROLLBAR_ITEM_POSITION, level_ypos + 1, GDI_END);
8259       }
8260       break;
8261
8262     case GADGET_ID_SCROLL_DOWN:
8263       if (level_ypos <= lev_fieldy - ed_fieldy)
8264       {
8265         if (lev_fieldy < ed_fieldy - 2)
8266           break;
8267
8268         level_ypos += step;
8269         if (level_ypos > lev_fieldy - ed_fieldy + 1)
8270           level_ypos = lev_fieldy - ed_fieldy + 1;
8271         if (button == 1)
8272           ScrollMiniLevel(level_xpos, level_ypos, ED_SCROLL_UP);
8273         else
8274           DrawMiniLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
8275
8276         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_VERTICAL],
8277                      GDI_SCROLLBAR_ITEM_POSITION, level_ypos + 1, GDI_END);
8278       }
8279       break;
8280
8281     case GADGET_ID_SCROLL_HORIZONTAL:
8282       level_xpos = gi->event.item_position - 1;
8283       DrawMiniLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
8284       break;
8285
8286     case GADGET_ID_SCROLL_VERTICAL:
8287       level_ypos = gi->event.item_position - 1;
8288       DrawMiniLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
8289       break;
8290
8291     case GADGET_ID_SCROLL_LIST_UP:
8292     case GADGET_ID_SCROLL_LIST_DOWN:
8293     case GADGET_ID_SCROLL_LIST_VERTICAL:
8294       if (id == GADGET_ID_SCROLL_LIST_VERTICAL)
8295         element_shift = gi->event.item_position * ED_ELEMENTLIST_BUTTONS_HORIZ;
8296       else
8297       {
8298         step *= (id == GADGET_ID_SCROLL_LIST_UP ? -1 : +1);
8299         element_shift += step * ED_ELEMENTLIST_BUTTONS_HORIZ;
8300
8301         if (element_shift < 0)
8302           element_shift = 0;
8303         if (element_shift > num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS)
8304           element_shift = num_editor_elements - ED_NUM_ELEMENTLIST_BUTTONS;
8305
8306         ModifyGadget(level_editor_gadget[GADGET_ID_SCROLL_LIST_VERTICAL],
8307                      GDI_SCROLLBAR_ITEM_POSITION,
8308                      element_shift / ED_ELEMENTLIST_BUTTONS_HORIZ, GDI_END);
8309       }
8310
8311       ModifyEditorElementList();
8312
8313       break;
8314
8315     case GADGET_ID_WRAP_LEFT:
8316       WrapLevel(-step, 0);
8317       break;
8318
8319     case GADGET_ID_WRAP_RIGHT:
8320       WrapLevel(step, 0);
8321       break;
8322
8323     case GADGET_ID_WRAP_UP:
8324       WrapLevel(0, -step);
8325       break;
8326
8327     case GADGET_ID_WRAP_DOWN:
8328       WrapLevel(0, step);
8329       break;
8330
8331     case GADGET_ID_SINGLE_ITEMS:
8332     case GADGET_ID_CONNECTED_ITEMS:
8333     case GADGET_ID_LINE:
8334     case GADGET_ID_ARC:
8335     case GADGET_ID_TEXT:
8336     case GADGET_ID_RECTANGLE:
8337     case GADGET_ID_FILLED_BOX:
8338     case GADGET_ID_FLOOD_FILL:
8339     case GADGET_ID_GRAB_BRUSH:
8340     case GADGET_ID_PICK_ELEMENT:
8341       if (drawing_function != GADGET_ID_PICK_ELEMENT)
8342         last_drawing_function = drawing_function;
8343       drawing_function = id;
8344       draw_with_brush = FALSE;
8345       break;
8346
8347     case GADGET_ID_RANDOM_PLACEMENT:
8348       RandomPlacement(new_element);
8349       break;
8350
8351     case GADGET_ID_PROPERTIES:
8352       if (edit_mode != ED_MODE_PROPERTIES)
8353       {
8354         properties_element = new_element;
8355         DrawPropertiesWindow();
8356         edit_mode = ED_MODE_PROPERTIES;
8357
8358         last_level_drawing_function = drawing_function;
8359         ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS],
8360                       MB_LEFTBUTTON);
8361       }
8362       else
8363       {
8364         DrawDrawingWindow();
8365         edit_mode = ED_MODE_DRAWING;
8366
8367         ClickOnGadget(level_editor_gadget[last_level_drawing_function],
8368                       MB_LEFTBUTTON);
8369       }
8370       break;
8371
8372     case GADGET_ID_CUSTOM_COPY_FROM:
8373     case GADGET_ID_CUSTOM_COPY_TO:
8374     case GADGET_ID_CUSTOM_EXCHANGE:
8375       last_custom_copy_mode = id;
8376       last_drawing_function = drawing_function;
8377       break;
8378
8379     case GADGET_ID_UNDO:
8380       if (undo_buffer_steps == 0)
8381       {
8382         Request("Undo buffer empty !", REQ_CONFIRM);
8383         break;
8384       }
8385
8386       if (edit_mode != ED_MODE_DRAWING)
8387       {
8388         DrawDrawingWindow();
8389         edit_mode = ED_MODE_DRAWING;
8390       }
8391
8392       undo_buffer_position =
8393         (undo_buffer_position - 1 + NUM_UNDO_STEPS) % NUM_UNDO_STEPS;
8394       undo_buffer_steps--;
8395
8396       for (x = 0; x < lev_fieldx; x++)
8397         for (y = 0; y < lev_fieldy; y++)
8398           Feld[x][y] = UndoBuffer[undo_buffer_position][x][y];
8399       DrawMiniLevel(ed_fieldx, ed_fieldy, level_xpos,level_ypos);
8400       break;
8401
8402     case GADGET_ID_INFO:
8403       if (edit_mode != ED_MODE_INFO)
8404       {
8405         last_edit_mode = edit_mode;
8406         edit_mode = ED_MODE_INFO;
8407
8408         DrawLevelInfoWindow();
8409       }
8410       else
8411       {
8412         edit_mode = last_edit_mode;
8413
8414         DrawEditModeWindow();
8415       }
8416       break;
8417
8418     case GADGET_ID_CLEAR:
8419       if (edit_mode != ED_MODE_DRAWING)
8420       {
8421         DrawDrawingWindow();
8422         edit_mode = ED_MODE_DRAWING;
8423       }
8424
8425       for (x = 0; x < MAX_LEV_FIELDX; x++) 
8426         for (y = 0; y < MAX_LEV_FIELDY; y++) 
8427           Feld[x][y] = (button == 1 ? EL_EMPTY : new_element);
8428       CopyLevelToUndoBuffer(GADGET_ID_CLEAR);
8429
8430       DrawMiniLevel(ed_fieldx, ed_fieldy, level_xpos, level_ypos);
8431       break;
8432
8433     case GADGET_ID_SAVE:
8434       if (leveldir_current->readonly)
8435       {
8436         Request("This level is read only !", REQ_CONFIRM);
8437         break;
8438       }
8439
8440       if (!LevelContainsPlayer)
8441         Request("No Level without Gregor Mc Duffin please !", REQ_CONFIRM);
8442       else
8443       {
8444         char *level_filename = getDefaultLevelFilename(level_nr);
8445         boolean new_level = !fileExists(level_filename);
8446
8447         if (new_level ||
8448             Request("Save this level and kill the old ?", REQ_ASK))
8449         {
8450           CopyPlayfield(Feld, level.field);
8451
8452           SaveLevel(level_nr);
8453         }
8454
8455         if (new_level)
8456           Request("Level saved !", REQ_CONFIRM);
8457       }
8458       break;
8459
8460     case GADGET_ID_TEST:
8461       if (!LevelContainsPlayer)
8462         Request("No Level without Gregor Mc Duffin please !", REQ_CONFIRM);
8463       else
8464       {
8465         if (LevelChanged())
8466           level.game_version = GAME_VERSION_ACTUAL;
8467
8468         CopyPlayfield(level.field, FieldBackup);
8469         CopyPlayfield(Feld, level.field);
8470
8471         UnmapLevelEditorGadgets();
8472         UndrawSpecialEditorDoor();
8473
8474         CloseDoor(DOOR_CLOSE_ALL);
8475
8476         DrawCompleteVideoDisplay();
8477
8478         if (setup.autorecord)
8479           TapeStartRecording();
8480
8481         level_editor_test_game = TRUE;
8482         game_status = GAME_MODE_PLAYING;
8483
8484         InitGame();
8485       }
8486       break;
8487
8488     case GADGET_ID_EXIT:
8489       RequestExitLevelEditor(TRUE);     /* if level has changed, ask user */
8490       break;
8491
8492     default:
8493       if (id >= GADGET_ID_ELEMENTLIST_FIRST &&
8494           id <= GADGET_ID_ELEMENTLIST_LAST)
8495       {
8496         int element_position = id - GADGET_ID_ELEMENTLIST_FIRST;
8497         int new_element = editor_elements[element_position + element_shift];
8498
8499         if (last_custom_copy_mode != -1)
8500         {
8501           if (CopyCustomElement(properties_element, new_element,
8502                                 last_custom_copy_mode))
8503           {
8504             ClickOnGadget(level_editor_gadget[last_drawing_function],
8505                           MB_LEFTBUTTON);
8506
8507             last_custom_copy_mode = -1;
8508           }
8509
8510           break;
8511         }
8512
8513         PickDrawingElement(button, new_element);
8514
8515         if (!stick_element_properties_window &&
8516             drawing_function != GADGET_ID_PICK_ELEMENT &&
8517             !(GetKeyModState() & KMOD_Control))
8518         {
8519           properties_element = new_element;
8520           if (edit_mode == ED_MODE_PROPERTIES)
8521             DrawPropertiesWindow();
8522         }
8523
8524         if (drawing_function == GADGET_ID_PICK_ELEMENT)
8525           ClickOnGadget(level_editor_gadget[last_drawing_function],
8526                         MB_LEFTBUTTON);
8527       }
8528 #ifdef DEBUG
8529       else if (gi->event.type == GD_EVENT_PRESSED)
8530         printf("default: HandleControlButtons: GD_EVENT_PRESSED(%d)\n", id);
8531       else if (gi->event.type == GD_EVENT_RELEASED)
8532         printf("default: HandleControlButtons: GD_EVENT_RELEASED(%d)\n", id);
8533       else if (gi->event.type == GD_EVENT_MOVING)
8534         printf("default: HandleControlButtons: GD_EVENT_MOVING(%d)\n", id);
8535       else
8536         printf("default: HandleControlButtons: ? (id == %d)\n", id);
8537 #endif
8538       break;
8539   }
8540 }
8541
8542 void HandleLevelEditorKeyInput(Key key)
8543 {
8544   char letter = getCharFromKey(key);
8545   int button = MB_LEFTBUTTON;
8546
8547   if (drawing_function == GADGET_ID_TEXT &&
8548       DrawLevelText(0, 0, 0, TEXT_QUERY_TYPING) == TRUE)
8549   {
8550     if (letter)
8551       DrawLevelText(0, 0, letter, TEXT_WRITECHAR);
8552     else if (key == KSYM_Delete || key == KSYM_BackSpace)
8553       DrawLevelText(0, 0, 0, TEXT_BACKSPACE);
8554     else if (key == KSYM_Return)
8555       DrawLevelText(0, 0, 0, TEXT_NEWLINE);
8556     else if (key == KSYM_Escape)
8557       DrawLevelText(0, 0, 0, TEXT_END);
8558   }
8559   else if (button_status == MB_RELEASED)
8560   {
8561     int i, id = GADGET_ID_NONE;
8562
8563     switch (key)
8564     {
8565       case KSYM_Left:
8566         id = GADGET_ID_SCROLL_LEFT;
8567         break;
8568       case KSYM_Right:
8569         id = GADGET_ID_SCROLL_RIGHT;
8570         break;
8571       case KSYM_Up:
8572         id = GADGET_ID_SCROLL_UP;
8573         break;
8574       case KSYM_Down:
8575         id = GADGET_ID_SCROLL_DOWN;
8576         break;
8577       case KSYM_Page_Up:
8578         id = GADGET_ID_SCROLL_LIST_UP;
8579         button = MB_RIGHTBUTTON;
8580         break;
8581       case KSYM_Page_Down:
8582         id = GADGET_ID_SCROLL_LIST_DOWN;
8583         button = MB_RIGHTBUTTON;
8584         break;
8585
8586       case KSYM_Escape:
8587         if (edit_mode == ED_MODE_DRAWING)
8588         {
8589           RequestExitLevelEditor(setup.ask_on_escape);
8590         }
8591         else
8592         {
8593           DrawDrawingWindow();
8594           edit_mode = ED_MODE_DRAWING;
8595         }
8596         break;
8597
8598       default:
8599         break;
8600     }
8601
8602     if (id != GADGET_ID_NONE)
8603       ClickOnGadget(level_editor_gadget[id], button);
8604     else if (letter == '.')
8605       ClickOnGadget(level_editor_gadget[GADGET_ID_SINGLE_ITEMS], button);
8606     else if (key == KSYM_Return || key == setup.shortcut.toggle_pause)
8607       ClickOnGadget(level_editor_gadget[GADGET_ID_TEST], button);
8608     else
8609       for (i = 0; i < ED_NUM_CTRL_BUTTONS; i++)
8610         if (letter && letter == control_info[i].shortcut)
8611           if (!anyTextGadgetActive())
8612             ClickOnGadget(level_editor_gadget[i], button);
8613   }
8614 }
8615
8616 void HandleLevelEditorIdle()
8617 {
8618   static unsigned long action_delay = 0;
8619   unsigned long action_delay_value = GameFrameDelay;
8620   int xpos = 1, ypos = 2;
8621
8622   if (edit_mode != ED_MODE_PROPERTIES)
8623     return;
8624
8625   if (!DelayReached(&action_delay, action_delay_value))
8626     return;
8627
8628   DrawGraphicAnimationExt(drawto,
8629                           SX + xpos * TILEX,
8630                           SY + ypos * TILEY + MINI_TILEY / 2,
8631                           el2img(properties_element), -1, NO_MASKING);
8632
8633   MarkTileDirty(xpos, ypos);
8634   MarkTileDirty(xpos, ypos + 1);
8635
8636   FrameCounter++;       /* increase animation frame counter */
8637 }
8638
8639 void ClearEditorGadgetInfoText()
8640 {
8641   DrawBackground(INFOTEXT_XPOS, INFOTEXT_YPOS, INFOTEXT_XSIZE, INFOTEXT_YSIZE);
8642 }
8643
8644 void HandleEditorGadgetInfoText(void *ptr)
8645 {
8646   struct GadgetInfo *gi = (struct GadgetInfo *)ptr;
8647   char infotext[MAX_OUTPUT_LINESIZE + 1];
8648   char shortcut[MAX_OUTPUT_LINESIZE + 1];
8649   int max_infotext_len = getMaxInfoTextLength();
8650
8651   if (game_status != GAME_MODE_EDITOR)
8652     return;
8653
8654   ClearEditorGadgetInfoText();
8655
8656   if (gi->event.type == GD_EVENT_INFO_LEAVING)
8657     return;
8658
8659   /* misuse this function to delete brush cursor, if needed */
8660   if (edit_mode == ED_MODE_DRAWING && draw_with_brush)
8661     DeleteBrushFromCursor();
8662
8663   if (gi == NULL || gi->info_text == NULL)
8664     return;
8665
8666   strncpy(infotext, gi->info_text, max_infotext_len);
8667   infotext[max_infotext_len] = '\0';
8668
8669   if (gi->custom_id < ED_NUM_CTRL_BUTTONS)
8670   {
8671     int key = control_info[gi->custom_id].shortcut;
8672
8673     if (key)
8674     {
8675       if (gi->custom_id == GADGET_ID_SINGLE_ITEMS)      /* special case 1 */
8676         sprintf(shortcut, " ('.' or '%c')", key);
8677       else if (gi->custom_id == GADGET_ID_PICK_ELEMENT) /* special case 2 */
8678         sprintf(shortcut, " ('%c' or 'Ctrl')", key);
8679       else if (gi->custom_id == GADGET_ID_TEST)         /* special case 3 */
8680         sprintf(shortcut, " ('Enter' or 'Shift-%c')", key);
8681       else                                              /* normal case */
8682         sprintf(shortcut, " ('%s%c')",
8683                 (key >= 'A' && key <= 'Z' ? "Shift-" : ""), key);
8684
8685       if (strlen(infotext) + strlen(shortcut) <= max_infotext_len)
8686         strcat(infotext, shortcut);
8687     }
8688   }
8689
8690   DrawText(INFOTEXT_XPOS, INFOTEXT_YPOS, infotext, FONT_TEXT_2);
8691 }
8692
8693 static void HandleDrawingAreaInfo(struct GadgetInfo *gi)
8694 {
8695   static int start_lx, start_ly;
8696   int id = gi->custom_id;
8697   int sx = gi->event.x;
8698   int sy = gi->event.y;
8699   int lx = sx + level_xpos;
8700   int ly = sy + level_ypos;
8701   int min_sx = 0, min_sy = 0;
8702   int max_sx = gi->drawing.area_xsize - 1;
8703   int max_sy = gi->drawing.area_ysize - 1;
8704   int actual_drawing_function = drawing_function;
8705   int max_infotext_len = getMaxInfoTextLength();
8706   char infotext[MAX_OUTPUT_LINESIZE + 1];
8707   char *text;
8708
8709   infotext[0] = '\0';           /* start with empty info text */
8710
8711   /* pressed Control key: simulate picking element */
8712   if (GetKeyModState() & KMOD_Control)
8713     actual_drawing_function = GADGET_ID_PICK_ELEMENT;
8714
8715   ClearEditorGadgetInfoText();
8716
8717   if (gi->event.type == GD_EVENT_INFO_LEAVING)
8718     return;
8719
8720   /* make sure to stay inside drawing area boundaries */
8721   sx = (sx < min_sx ? min_sx : sx > max_sx ? max_sx : sx);
8722   sy = (sy < min_sy ? min_sy : sy > max_sy ? max_sy : sy);
8723
8724   if (id == GADGET_ID_DRAWING_LEVEL)
8725   {
8726     if (button_status)
8727     {
8728       int min_lx = 0, min_ly = 0;
8729       int max_lx = lev_fieldx - 1, max_ly = lev_fieldy - 1;
8730
8731       /* get positions inside level field */
8732       lx = sx + level_xpos;
8733       ly = sy + level_ypos;
8734
8735       /* make sure to stay inside level field boundaries */
8736       lx = (lx < min_lx ? min_lx : lx > max_lx ? max_lx : lx);
8737       ly = (ly < min_ly ? min_ly : ly > max_ly ? max_ly : ly);
8738
8739       /* correct drawing area positions accordingly */
8740       sx = lx - level_xpos;
8741       sy = ly - level_ypos;
8742     }
8743
8744     if (IN_ED_FIELD(sx,sy) && IN_LEV_FIELD(lx, ly))
8745     {
8746       if (button_status)        /* if (gi->state == GD_BUTTON_PRESSED) */
8747       {
8748         if (gi->event.type == GD_EVENT_PRESSED)
8749         {
8750           start_lx = lx;
8751           start_ly = ly;
8752         }
8753
8754         switch (actual_drawing_function)
8755         {
8756           case GADGET_ID_SINGLE_ITEMS:
8757             text = "Drawing single items";
8758             break;
8759           case GADGET_ID_CONNECTED_ITEMS:
8760             text = "Drawing connected items";
8761             break;
8762           case GADGET_ID_LINE:
8763             text = "Drawing line";
8764             break;
8765           case GADGET_ID_ARC:
8766             text = "Drawing arc";
8767             break;
8768           case GADGET_ID_TEXT:
8769             text = "Setting text cursor";
8770             break;
8771           case GADGET_ID_RECTANGLE:
8772             text = "Drawing rectangle";
8773             break;
8774           case GADGET_ID_FILLED_BOX:
8775             text = "Drawing filled box";
8776             break;
8777           case GADGET_ID_FLOOD_FILL:
8778             text = "Flood fill";
8779             break;
8780           case GADGET_ID_GRAB_BRUSH:
8781             text = "Grabbing brush";
8782             break;
8783           case GADGET_ID_PICK_ELEMENT:
8784             text = "Picking element";
8785             break;
8786
8787           default:
8788             text = "Drawing position";
8789             break;
8790         }
8791
8792 #if 1
8793         if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
8794           sprintf(infotext, "%s: %d, %d", text, lx, ly);
8795         else
8796           sprintf(infotext, "%s: %d, %d", text,
8797                   ABS(lx - start_lx) + 1, ABS(ly - start_ly) + 1);
8798 #else
8799         if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
8800           DrawTextF(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY, FONT_TEXT_2,
8801                     "%s: %d, %d", infotext, lx, ly);
8802         else
8803           DrawTextF(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY, FONT_TEXT_2,
8804                     "%s: %d, %d", infotext,
8805                     ABS(lx - start_lx) + 1, ABS(ly - start_ly) + 1);
8806 #endif
8807       }
8808 #if 1
8809       else if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
8810         strncpy(infotext, getElementInfoText(Feld[lx][ly]), max_infotext_len);
8811       else
8812         sprintf(infotext, "Level position: %d, %d", lx, ly);
8813 #else
8814       else if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
8815         DrawTextF(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY, FONT_TEXT_2,
8816                   "%s", getElementInfoText(Feld[lx][ly]));
8817       else
8818         DrawTextF(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY, FONT_TEXT_2,
8819                   "Level position: %d, %d", lx, ly);
8820 #endif
8821     }
8822
8823     /* misuse this function to draw brush cursor, if needed */
8824     if (edit_mode == ED_MODE_DRAWING && draw_with_brush && !button_status)
8825     {
8826       if (IN_ED_FIELD(sx, sy) && IN_LEV_FIELD(lx, ly))
8827         CopyBrushToCursor(sx, sy);
8828       else
8829         DeleteBrushFromCursor();
8830     }
8831   }
8832   else if (actual_drawing_function == GADGET_ID_PICK_ELEMENT)
8833   {
8834     int element = EL_EMPTY;
8835
8836     if (id == GADGET_ID_AMOEBA_CONTENT)
8837       element = level.amoeba_content;
8838     else if (id == GADGET_ID_CUSTOM_GRAPHIC)
8839       element = custom_element.gfx_element;
8840     else if (id == GADGET_ID_CUSTOM_CONTENT)
8841       element = custom_element.content[sx][sy];
8842     else if (id == GADGET_ID_CUSTOM_MOVE_ENTER)
8843       element = custom_element.move_enter_element;
8844     else if (id == GADGET_ID_CUSTOM_MOVE_LEAVE)
8845       element = custom_element.move_leave_element;
8846     else if (id == GADGET_ID_CUSTOM_CHANGE_TARGET)
8847       element = custom_element_change.target_element;
8848     else if (id == GADGET_ID_CUSTOM_CHANGE_CONTENT)
8849       element = custom_element_change.content[sx][sy];
8850     else if (id == GADGET_ID_CUSTOM_CHANGE_TRIGGER)
8851       element = custom_element_change.trigger_element;
8852     else if (id == GADGET_ID_GROUP_CONTENT)
8853       element = group_element_info.element[sx];
8854     else if (id == GADGET_ID_RANDOM_BACKGROUND)
8855       element = random_placement_background_element;
8856     else if (id >= GADGET_ID_ELEMENT_CONTENT_0 &&
8857              id <= GADGET_ID_ELEMENT_CONTENT_7)
8858       element = level.yamyam_content[id - GADGET_ID_ELEMENT_CONTENT_0][sx][sy];
8859
8860     strncpy(infotext, getElementInfoText(element), max_infotext_len);
8861   }
8862   else
8863   {
8864     if (id == GADGET_ID_AMOEBA_CONTENT)
8865       strcpy(infotext, "Amoeba content");
8866     else if (id == GADGET_ID_CUSTOM_GRAPHIC)
8867       strcpy(infotext, "Custom graphic element");
8868     else if (id == GADGET_ID_CUSTOM_CONTENT)
8869       sprintf(infotext, "Custom element content position: %d, %d", sx, sy);
8870     else if (id == GADGET_ID_CUSTOM_MOVE_ENTER)
8871       strcpy(infotext, "Element that can be digged/collected");
8872     else if (id == GADGET_ID_CUSTOM_MOVE_LEAVE)
8873       strcpy(infotext, "Element that will be left behind");
8874     else if (id == GADGET_ID_CUSTOM_CHANGE_TARGET)
8875       strcpy(infotext, "New element after change");
8876     else if (id == GADGET_ID_CUSTOM_CHANGE_CONTENT)
8877       strcpy(infotext, "New extended elements after change");
8878     else if (id == GADGET_ID_CUSTOM_CHANGE_TRIGGER)
8879       strcpy(infotext, "Other element triggering change");
8880     else if (id == GADGET_ID_GROUP_CONTENT)
8881       sprintf(infotext, "Group element position: %d", sx + 1);
8882     else if (id == GADGET_ID_RANDOM_BACKGROUND)
8883       strcpy(infotext, "Random placement background");
8884     else if (id >= GADGET_ID_ELEMENT_CONTENT_0 &&
8885              id <= GADGET_ID_ELEMENT_CONTENT_7)
8886       sprintf(infotext, "Content area %d position: %d, %d",
8887               id - GADGET_ID_ELEMENT_CONTENT_0 + 1, sx, sy);
8888   }
8889
8890   infotext[max_infotext_len] = '\0';
8891
8892   if (strlen(infotext) > 0)
8893     DrawTextS(INFOTEXT_XPOS - SX, INFOTEXT_YPOS - SY, FONT_TEXT_2, infotext);
8894 }
8895
8896 void RequestExitLevelEditor(boolean ask_if_level_has_changed)
8897 {
8898   if (!ask_if_level_has_changed ||
8899       !LevelChanged() ||
8900       Request("Level has changed! Exit without saving ?",
8901               REQ_ASK | REQ_STAY_OPEN))
8902   {
8903 #if 1
8904     CloseDoor(DOOR_CLOSE_1);
8905     SetDoorState(DOOR_CLOSE_2);
8906 #else
8907     CloseDoor(DOOR_CLOSE_ALL);
8908 #endif
8909     game_status = GAME_MODE_MAIN;
8910     DrawMainMenu();
8911   }
8912   else
8913   {
8914     CloseDoor(DOOR_CLOSE_1);
8915     BlitBitmap(bitmap_db_door, bitmap_db_door,
8916                DOOR_GFX_PAGEX2, DOOR_GFX_PAGEY1, DXSIZE,DYSIZE,
8917                DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY1);
8918     OpenDoor(DOOR_OPEN_1);
8919   }
8920 }