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