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