1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
21 /* values for DrawGadget() */
22 #define DG_UNPRESSED 0
28 #define OPTION_TEXT_SELECTABLE(g, t) \
29 (t[0] != g->selectbox.char_unselectable && \
32 #define CURRENT_OPTION_SELECTABLE(g) \
33 OPTION_TEXT_SELECTABLE(g, g->selectbox.options[g->selectbox.current_index].text)
36 static struct GadgetInfo *gadget_list_first_entry = NULL;
37 static struct GadgetInfo *gadget_list_last_entry = NULL;
38 static struct GadgetInfo *last_info_gi = NULL;
39 static int next_free_gadget_id = 1;
40 static boolean gadget_id_wrapped = FALSE;
42 static void (*PlayGadgetSoundActivating)(void) = NULL;
43 static void (*PlayGadgetSoundSelecting)(void) = NULL;
46 void InitGadgetsSoundCallback(void (*activating_function)(void),
47 void (*selecting_function)(void))
49 PlayGadgetSoundActivating = activating_function;
50 PlayGadgetSoundSelecting = selecting_function;
53 static struct GadgetInfo *getGadgetInfoFromGadgetID(int id)
55 struct GadgetInfo *gi = gadget_list_first_entry;
57 while (gi && gi->id != id)
63 static int getNewGadgetID()
65 int id = next_free_gadget_id++;
67 if (next_free_gadget_id <= 0) /* counter overrun */
69 gadget_id_wrapped = TRUE; /* now we must check each ID */
70 next_free_gadget_id = 0;
73 if (gadget_id_wrapped)
75 next_free_gadget_id++;
76 while (getGadgetInfoFromGadgetID(next_free_gadget_id) != NULL)
77 next_free_gadget_id++;
80 if (next_free_gadget_id <= 0) /* cannot get new gadget id */
81 Error(ERR_EXIT, "too much gadgets -- this should not happen");
86 static struct GadgetInfo *getGadgetInfoFromMousePosition(int mx, int my,
89 struct GadgetInfo *gi;
91 /* first check for scrollbars in case of mouse scroll wheel button events */
92 if (IS_WHEEL_BUTTON(button))
94 /* real horizontal wheel or vertical wheel with modifier key pressed */
95 boolean check_horizontal = (IS_WHEEL_BUTTON_HORIZONTAL(button) ||
96 GetKeyModState() & KMOD_Shift);
98 /* check for the first active scrollbar directly under the mouse pointer */
99 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
101 if (gi->mapped && gi->active &&
102 (gi->type & GD_TYPE_SCROLLBAR) &&
103 mx >= gi->x && mx < gi->x + gi->width &&
104 my >= gi->y && my < gi->y + gi->height)
108 /* check for the first active scrollbar with matching mouse wheel area */
109 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
111 if (gi->mapped && gi->active &&
112 ((gi->type & GD_TYPE_SCROLLBAR_HORIZONTAL && check_horizontal) ||
113 (gi->type & GD_TYPE_SCROLLBAR_VERTICAL && !check_horizontal)) &&
114 mx >= gi->wheelarea.x && mx < gi->wheelarea.x + gi->wheelarea.width &&
115 my >= gi->wheelarea.y && my < gi->wheelarea.y + gi->wheelarea.height)
119 /* no active scrollbar found -- ignore this scroll wheel button event */
123 /* open selectboxes may overlap other active gadgets, so check them first */
124 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
126 if (gi->mapped && gi->active &&
127 gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open &&
128 mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width &&
129 my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
133 /* check all other gadgets */
134 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
136 if (gi->mapped && gi->active &&
137 mx >= gi->x && mx < gi->x + gi->width &&
138 my >= gi->y && my < gi->y + gi->height)
145 static void setTextAreaCursorExt(struct GadgetInfo *gi, boolean set_cursor_pos)
147 char *text = gi->textarea.value;
148 int area_xsize = gi->textarea.xsize;
149 int area_ysize = gi->textarea.ysize;
150 int cursor_position = gi->textarea.cursor_position;
151 int cursor_x = gi->textarea.cursor_x;
152 int cursor_y = gi->textarea.cursor_y;
159 if (set_cursor_pos) /* x/y => position */
161 if (y == cursor_y && (x == cursor_x || (x < cursor_x && *text == '\n')))
164 else /* position => x/y */
166 if (pos == cursor_position)
170 if (x + 1 >= area_xsize || *text == '\n')
172 if (y + 1 >= area_ysize)
185 gi->textarea.cursor_x = x;
186 gi->textarea.cursor_y = y;
187 gi->textarea.cursor_x_preferred = x;
188 gi->textarea.cursor_position = pos;
191 static void setTextAreaCursorXY(struct GadgetInfo *gi, int x, int y)
193 gi->textarea.cursor_x = x;
194 gi->textarea.cursor_y = y;
196 setTextAreaCursorExt(gi, TRUE);
199 static void setTextAreaCursorPosition(struct GadgetInfo *gi, int pos)
201 gi->textarea.cursor_position = pos;
203 setTextAreaCursorExt(gi, FALSE);
206 static void default_callback_info(void *ptr)
211 static void default_callback_action(void *ptr)
216 static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct)
218 struct GadgetDesign *gd;
219 int state = (pressed ? GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
220 boolean redraw_selectbox = FALSE;
222 if (gi == NULL || gi->deactivated)
225 gd = (!gi->active ? &gi->alt_design[state] :
226 gi->checked ? &gi->alt_design[state] : &gi->design[state]);
230 case GD_TYPE_NORMAL_BUTTON:
231 case GD_TYPE_CHECK_BUTTON:
232 case GD_TYPE_RADIO_BUTTON:
234 BlitBitmapOnBackground(gd->bitmap, drawto,
235 gd->x, gd->y, gi->width, gi->height,
238 if (gi->deco.design.bitmap)
240 // make sure that decoration does not overlap gadget border
241 int deco_x = gi->deco.x + (pressed ? gi->deco.xshift : 0);
242 int deco_y = gi->deco.y + (pressed ? gi->deco.yshift : 0);
243 int deco_width = MIN(gi->deco.width, gi->width - deco_x);
244 int deco_height = MIN(gi->deco.height, gi->height - deco_y);
247 BlitBitmapMasked(gi->deco.design.bitmap, drawto,
248 gi->deco.design.x, gi->deco.design.y,
249 deco_width, deco_height,
250 gi->x + deco_x, gi->y + deco_y);
252 BlitBitmap(gi->deco.design.bitmap, drawto,
253 gi->deco.design.x, gi->deco.design.y,
254 deco_width, deco_height,
255 gi->x + deco_x, gi->y + deco_y);
260 case GD_TYPE_TEXT_BUTTON:
263 int font_nr = (gi->active ? gi->font_active : gi->font);
264 int font_width = getFontWidth(font_nr);
265 int border_x = gi->border.xsize;
266 int border_y = gi->border.ysize;
267 int text_size = strlen(gi->textbutton.value);
268 int text_start = (gi->width - text_size * font_width) / 2;
270 /* left part of gadget */
271 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
272 border_x, gi->height, gi->x, gi->y);
274 /* middle part of gadget */
275 for (i=0; i < gi->textbutton.size; i++)
276 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
277 font_width, gi->height,
278 gi->x + border_x + i * font_width, gi->y);
280 /* right part of gadget */
281 BlitBitmapOnBackground(gd->bitmap, drawto,
282 gd->x + gi->border.width - border_x, gd->y,
283 border_x, gi->height,
284 gi->x + gi->width - border_x, gi->y);
286 /* gadget text value */
288 gi->x + text_start + (pressed ? gi->deco.xshift : 0),
289 gi->y + border_y + (pressed ? gi->deco.yshift : 0),
290 gi->textbutton.value, font_nr, BLIT_MASKED);
294 case GD_TYPE_TEXT_INPUT_ALPHANUMERIC:
295 case GD_TYPE_TEXT_INPUT_NUMERIC:
299 char cursor_string[2];
300 char text[MAX_GADGET_TEXTSIZE + 1];
301 int font_nr = (pressed ? gi->font_active : gi->font);
302 int font_width = getFontWidth(font_nr);
303 int border_x = gi->border.xsize;
304 int border_y = gi->border.ysize;
306 /* left part of gadget */
307 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
308 border_x, gi->height, gi->x, gi->y);
310 /* middle part of gadget */
311 for (i=0; i < gi->textinput.size + 1; i++)
312 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
313 font_width, gi->height,
314 gi->x + border_x + i * font_width, gi->y);
316 /* right part of gadget */
317 BlitBitmapOnBackground(gd->bitmap, drawto,
318 gd->x + gi->border.width - border_x, gd->y,
319 border_x, gi->height,
320 gi->x + gi->width - border_x, gi->y);
323 strcpy(text, gi->textinput.value);
326 /* gadget text value */
328 gi->x + border_x, gi->y + border_y, text,
329 font_nr, BLIT_MASKED);
331 cursor_letter = gi->textinput.value[gi->textinput.cursor_position];
332 cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' ');
333 cursor_string[1] = '\0';
335 /* draw cursor, if active */
339 gi->textinput.cursor_position * font_width,
340 gi->y + border_y, cursor_string,
341 font_nr, BLIT_INVERSE);
345 case GD_TYPE_TEXT_AREA:
349 char cursor_string[2];
350 int font_nr = (pressed ? gi->font_active : gi->font);
351 int font_width = getFontWidth(font_nr);
352 int font_height = getFontHeight(font_nr);
353 int border_x = gi->border.xsize;
354 int border_y = gi->border.ysize;
355 int gd_height = 2 * border_y + font_height;
357 /* top left part of gadget border */
358 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
359 border_x, border_y, gi->x, gi->y);
361 /* top middle part of gadget border */
362 for (i=0; i < gi->textarea.xsize; i++)
363 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
364 font_width, border_y,
365 gi->x + border_x + i * font_width, gi->y);
367 /* top right part of gadget border */
368 BlitBitmapOnBackground(gd->bitmap, drawto,
369 gd->x + gi->border.width - border_x, gd->y,
371 gi->x + gi->width - border_x, gi->y);
373 /* left and right part of gadget border for each row */
374 for (i=0; i < gi->textarea.ysize; i++)
376 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y,
377 border_x, font_height,
378 gi->x, gi->y + border_y + i * font_height);
379 BlitBitmapOnBackground(gd->bitmap, drawto,
380 gd->x + gi->border.width - border_x,
382 border_x, font_height,
383 gi->x + gi->width - border_x,
384 gi->y + border_y + i * font_height);
387 /* bottom left part of gadget border */
388 BlitBitmapOnBackground(gd->bitmap, drawto,
389 gd->x, gd->y + gd_height - border_y,
391 gi->x, gi->y + gi->height - border_y);
393 /* bottom middle part of gadget border */
394 for (i=0; i < gi->textarea.xsize; i++)
395 BlitBitmapOnBackground(gd->bitmap, drawto,
397 gd->y + gd_height - border_y,
398 font_width, border_y,
399 gi->x + border_x + i * font_width,
400 gi->y + gi->height - border_y);
402 /* bottom right part of gadget border */
403 BlitBitmapOnBackground(gd->bitmap, drawto,
404 gd->x + gi->border.width - border_x,
405 gd->y + gd_height - border_y,
407 gi->x + gi->width - border_x,
408 gi->y + gi->height - border_y);
410 ClearRectangleOnBackground(drawto,
413 gi->width - 2 * border_x,
414 gi->height - 2 * border_y);
416 /* gadget text value */
417 DrawTextBuffer(gi->x + border_x, gi->y + border_y, gi->textarea.value,
418 font_nr, gi->textarea.xsize, -1, gi->textarea.ysize, 0,
419 BLIT_ON_BACKGROUND, FALSE, FALSE, FALSE);
421 cursor_letter = gi->textarea.value[gi->textarea.cursor_position];
422 cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' ');
423 cursor_string[1] = '\0';
425 /* draw cursor, if active */
428 gi->x + border_x + gi->textarea.cursor_x * font_width,
429 gi->y + border_y + gi->textarea.cursor_y * font_height,
431 font_nr, BLIT_INVERSE);
435 case GD_TYPE_SELECTBOX:
438 char text[MAX_GADGET_TEXTSIZE + 1];
439 int font_nr_default = (pressed ? gi->font_active : gi->font);
440 int font_nr = font_nr_default;
441 int font_width = getFontWidth(font_nr);
442 int font_height = getFontHeight(font_nr);
443 int border_x = gi->border.xsize;
444 int border_y = gi->border.ysize;
445 int button = gi->border.xsize_selectbutton;
446 int width_inner = gi->border.width - button - 2 * border_x;
447 int box_width = gi->selectbox.width;
448 int box_height = gi->selectbox.height;
450 /* left part of gadget */
451 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
452 border_x, gi->height, gi->x, gi->y);
454 /* middle part of gadget */
455 for (i=0; i < gi->selectbox.size; i++)
456 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
457 font_width, gi->height,
458 gi->x + border_x + i * font_width, gi->y);
460 /* button part of gadget */
461 BlitBitmapOnBackground(gd->bitmap, drawto,
462 gd->x + border_x + width_inner, gd->y,
464 gi->x + gi->width - border_x - button, gi->y);
466 /* right part of gadget */
467 BlitBitmapOnBackground(gd->bitmap, drawto,
468 gd->x + gi->border.width - border_x, gd->y,
469 border_x, gi->height,
470 gi->x + gi->width - border_x, gi->y);
473 strncpy(text, gi->selectbox.options[gi->selectbox.index].text,
475 text[gi->selectbox.size] = '\0';
478 font_nr = (OPTION_TEXT_SELECTABLE(gi, text) ? font_nr_default :
479 gi->font_unselectable);
481 /* gadget text value */
482 DrawTextExt(drawto, gi->x + border_x, gi->y + border_y, text,
483 font_nr, BLIT_MASKED);
487 if (!gi->selectbox.open)
489 gi->selectbox.open = TRUE;
490 gi->selectbox.stay_open = FALSE;
491 gi->selectbox.current_index = gi->selectbox.index;
493 /* save background under selectbox */
494 BlitBitmap(drawto, gfx.field_save_buffer,
495 gi->selectbox.x, gi->selectbox.y,
496 gi->selectbox.width, gi->selectbox.height,
497 gi->selectbox.x, gi->selectbox.y);
500 /* draw open selectbox */
502 /* top left part of gadget border */
503 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
505 gi->selectbox.x, gi->selectbox.y);
507 /* top middle part of gadget border */
508 for (i=0; i < gi->selectbox.size; i++)
509 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
510 font_width, border_y,
511 gi->selectbox.x + border_x + i * font_width,
514 /* top button part of gadget border */
515 BlitBitmapOnBackground(gd->bitmap, drawto,
516 gd->x + border_x + width_inner, gd->y,
518 gi->selectbox.x + box_width -border_x -button,
521 /* top right part of gadget border */
522 BlitBitmapOnBackground(gd->bitmap, drawto,
523 gd->x + gi->border.width - border_x, gd->y,
525 gi->selectbox.x + box_width - border_x,
528 /* left and right part of gadget border for each row */
529 for (i=0; i < gi->selectbox.num_values; i++)
531 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y,
532 border_x, font_height,
534 gi->selectbox.y + border_y + i*font_height);
535 BlitBitmapOnBackground(gd->bitmap, drawto,
536 gd->x + gi->border.width - border_x,
538 border_x, font_height,
539 gi->selectbox.x + box_width - border_x,
540 gi->selectbox.y + border_y + i*font_height);
543 /* bottom left part of gadget border */
544 BlitBitmapOnBackground(gd->bitmap, drawto,
545 gd->x, gd->y + gi->height - border_y,
548 gi->selectbox.y + box_height - border_y);
550 /* bottom middle part of gadget border */
551 for (i=0; i < gi->selectbox.size; i++)
552 BlitBitmapOnBackground(gd->bitmap, drawto,
554 gd->y + gi->height - border_y,
555 font_width, border_y,
556 gi->selectbox.x + border_x + i * font_width,
557 gi->selectbox.y + box_height - border_y);
559 /* bottom button part of gadget border */
560 BlitBitmapOnBackground(gd->bitmap, drawto,
561 gd->x + border_x + width_inner,
562 gd->y + gi->height - border_y,
564 gi->selectbox.x + box_width -border_x -button,
565 gi->selectbox.y + box_height - border_y);
567 /* bottom right part of gadget border */
568 BlitBitmapOnBackground(gd->bitmap, drawto,
569 gd->x + gi->border.width - border_x,
570 gd->y + gi->height - border_y,
572 gi->selectbox.x + box_width - border_x,
573 gi->selectbox.y + box_height - border_y);
575 ClearRectangleOnBackground(drawto,
576 gi->selectbox.x + border_x,
577 gi->selectbox.y + border_y,
578 gi->selectbox.width - 2 * border_x,
579 gi->selectbox.height - 2 * border_y);
581 /* selectbox text values */
582 for (i=0; i < gi->selectbox.num_values; i++)
584 int mask_mode = BLIT_MASKED;
586 strncpy(text, gi->selectbox.options[i].text, gi->selectbox.size);
587 text[gi->selectbox.size] = '\0';
589 font_nr = (OPTION_TEXT_SELECTABLE(gi, text) ? font_nr_default :
590 gi->font_unselectable);
592 if (i == gi->selectbox.current_index &&
593 OPTION_TEXT_SELECTABLE(gi, text))
595 FillRectangle(drawto,
596 gi->selectbox.x + border_x,
597 gi->selectbox.y + border_y + i * font_height,
598 gi->selectbox.width - 2 * border_x, font_height,
599 gi->selectbox.inverse_color);
601 /* prevent use of cursor graphic by drawing at least two chars */
603 text[gi->selectbox.size] = '\0';
605 mask_mode = BLIT_INVERSE;
609 gi->selectbox.x + border_x,
610 gi->selectbox.y + border_y + i * font_height, text,
614 redraw_selectbox = TRUE;
616 else if (gi->selectbox.open)
618 gi->selectbox.open = FALSE;
620 /* restore background under selectbox */
621 BlitBitmap(gfx.field_save_buffer, drawto,
622 gi->selectbox.x, gi->selectbox.y,
623 gi->selectbox.width, gi->selectbox.height,
624 gi->selectbox.x, gi->selectbox.y);
626 /* redraw closed selectbox */
627 DrawGadget(gi, FALSE, FALSE);
629 redraw_selectbox = TRUE;
634 case GD_TYPE_SCROLLBAR_VERTICAL:
638 int ypos = gi->y + gi->scrollbar.position;
639 int design_full = gi->width;
640 int design_body = design_full - 2 * gi->border.ysize;
641 int size_full = gi->scrollbar.size;
642 int size_body = size_full - 2 * gi->border.ysize;
643 int num_steps = size_body / design_body;
644 int step_size_remain = size_body - num_steps * design_body;
646 /* clear scrollbar area */
647 ClearRectangleOnBackground(backbuffer, gi->x, gi->y,
648 gi->width, gi->height);
650 /* upper part of gadget */
651 BlitBitmapOnBackground(gd->bitmap, drawto,
653 gi->width, gi->border.ysize,
656 /* middle part of gadget */
657 for (i=0; i < num_steps; i++)
658 BlitBitmapOnBackground(gd->bitmap, drawto,
659 gd->x, gd->y + gi->border.ysize,
660 gi->width, design_body,
662 ypos + gi->border.ysize + i * design_body);
664 /* remaining middle part of gadget */
665 if (step_size_remain > 0)
666 BlitBitmapOnBackground(gd->bitmap, drawto,
667 gd->x, gd->y + gi->border.ysize,
668 gi->width, step_size_remain,
670 ypos + gi->border.ysize
671 + num_steps * design_body);
673 /* lower part of gadget */
674 BlitBitmapOnBackground(gd->bitmap, drawto,
675 gd->x, gd->y + design_full - gi->border.ysize,
676 gi->width, gi->border.ysize,
677 xpos, ypos + size_full - gi->border.ysize);
681 case GD_TYPE_SCROLLBAR_HORIZONTAL:
684 int xpos = gi->x + gi->scrollbar.position;
686 int design_full = gi->height;
687 int design_body = design_full - 2 * gi->border.xsize;
688 int size_full = gi->scrollbar.size;
689 int size_body = size_full - 2 * gi->border.xsize;
690 int num_steps = size_body / design_body;
691 int step_size_remain = size_body - num_steps * design_body;
693 /* clear scrollbar area */
694 ClearRectangleOnBackground(backbuffer, gi->x, gi->y,
695 gi->width, gi->height);
697 /* left part of gadget */
698 BlitBitmapOnBackground(gd->bitmap, drawto,
700 gi->border.xsize, gi->height,
703 /* middle part of gadget */
704 for (i=0; i < num_steps; i++)
705 BlitBitmapOnBackground(gd->bitmap, drawto,
706 gd->x + gi->border.xsize, gd->y,
707 design_body, gi->height,
708 xpos + gi->border.xsize + i * design_body,
711 /* remaining middle part of gadget */
712 if (step_size_remain > 0)
713 BlitBitmapOnBackground(gd->bitmap, drawto,
714 gd->x + gi->border.xsize, gd->y,
715 step_size_remain, gi->height,
716 xpos + gi->border.xsize
717 + num_steps * design_body,
720 /* right part of gadget */
721 BlitBitmapOnBackground(gd->bitmap, drawto,
722 gd->x + design_full - gi->border.xsize, gd->y,
723 gi->border.xsize, gi->height,
724 xpos + size_full - gi->border.xsize, ypos);
732 // do not use direct gadget drawing anymore; this worked as a speed-up once,
733 // but would slow things down a lot now the screen is always fully redrawn
738 BlitBitmap(drawto, window,
739 gi->x, gi->y, gi->width, gi->height, gi->x, gi->y);
741 if (gi->type == GD_TYPE_SELECTBOX && redraw_selectbox)
742 BlitBitmap(drawto, window,
743 gi->selectbox.x, gi->selectbox.y,
744 gi->selectbox.width, gi->selectbox.height,
745 gi->selectbox.x, gi->selectbox.y);
752 redraw_mask |= (IN_GFX_FIELD_FULL(x, y) ? REDRAW_FIELD :
753 IN_GFX_DOOR_1(x, y) ? REDRAW_DOOR_1 :
754 IN_GFX_DOOR_2(x, y) ? REDRAW_DOOR_2 :
755 IN_GFX_DOOR_3(x, y) ? REDRAW_DOOR_3 : REDRAW_ALL);
759 static int get_minimal_size_for_numeric_input(int minmax_value)
761 int min_size = 1; /* value needs at least one digit */
764 /* add number of digits needed for absolute value */
765 for (i = 10; i <= ABS(minmax_value); i *= 10)
768 /* if min/max value is negative, add one digit for minus sign */
769 if (minmax_value < 0)
775 static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap)
779 if (gi == NULL || gi->deactivated)
782 while (tag != GDI_END)
787 gi->image_id = va_arg(ap, int);
791 gi->custom_id = va_arg(ap, int);
794 case GDI_CUSTOM_TYPE_ID:
795 gi->custom_type_id = va_arg(ap, int);
800 int max_textsize = MAX_INFO_TEXTSIZE;
801 char *text = va_arg(ap, char *);
804 strncpy(gi->info_text, text, max_textsize);
808 gi->info_text[max_textsize] = '\0';
813 gi->x = va_arg(ap, int);
817 gi->y = va_arg(ap, int);
821 gi->width = va_arg(ap, int);
825 gi->height = va_arg(ap, int);
829 gi->type = va_arg(ap, unsigned int);
833 gi->state = va_arg(ap, unsigned int);
837 gi->active = (boolean)va_arg(ap, int);
840 case GDI_DIRECT_DRAW:
841 gi->direct_draw = (boolean)va_arg(ap, int);
845 gi->checked = (boolean)va_arg(ap, int);
849 gi->radio_nr = va_arg(ap, unsigned int);
852 case GDI_NUMBER_VALUE:
853 gi->textinput.number_value = va_arg(ap, int);
854 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
855 strcpy(gi->textinput.last_value, gi->textinput.value);
856 gi->textinput.cursor_position = strlen(gi->textinput.value);
860 gi->textinput.number_min = va_arg(ap, int);
861 if (gi->textinput.number_value < gi->textinput.number_min)
863 gi->textinput.number_value = gi->textinput.number_min;
864 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
865 strcpy(gi->textinput.last_value, gi->textinput.value);
870 gi->textinput.number_max = va_arg(ap, int);
871 if (gi->textinput.number_value > gi->textinput.number_max)
873 gi->textinput.number_value = gi->textinput.number_max;
874 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
875 strcpy(gi->textinput.last_value, gi->textinput.value);
881 int max_textsize = MAX_GADGET_TEXTSIZE;
883 if (gi->textinput.size)
884 max_textsize = MIN(gi->textinput.size, MAX_GADGET_TEXTSIZE);
886 strncpy(gi->textinput.value, va_arg(ap, char *), max_textsize);
887 strcpy(gi->textinput.last_value, gi->textinput.value);
889 gi->textinput.value[max_textsize] = '\0';
890 gi->textinput.cursor_position = strlen(gi->textinput.value);
892 /* same tag also used for other gadget definitions */
893 strcpy(gi->textbutton.value, gi->textinput.value);
894 strcpy(gi->textarea.value, gi->textinput.value);
895 strcpy(gi->textarea.last_value, gi->textinput.value);
901 int tag_value = va_arg(ap, int);
902 int max_textsize = MIN(tag_value, MAX_GADGET_TEXTSIZE);
904 gi->textinput.size = max_textsize;
905 gi->textinput.value[max_textsize] = '\0';
906 strcpy(gi->textinput.last_value, gi->textinput.value);
908 /* same tag also used for other gadget definitions */
910 gi->textarea.size = max_textsize;
911 gi->textarea.value[max_textsize] = '\0';
912 strcpy(gi->textarea.last_value, gi->textinput.value);
914 gi->textbutton.size = max_textsize;
915 gi->textbutton.value[max_textsize] = '\0';
917 gi->selectbox.size = gi->textinput.size;
922 gi->font = va_arg(ap, int);
923 if (gi->font_active == 0)
924 gi->font_active = gi->font;
925 if (gi->font_unselectable == 0)
926 gi->font_unselectable = gi->font;
929 case GDI_TEXT_FONT_ACTIVE:
930 gi->font_active = va_arg(ap, int);
933 case GDI_TEXT_FONT_UNSELECTABLE:
934 gi->font_unselectable = va_arg(ap, int);
937 case GDI_SELECTBOX_OPTIONS:
938 gi->selectbox.options = va_arg(ap, struct ValueTextInfo *);
941 case GDI_SELECTBOX_INDEX:
942 gi->selectbox.index = va_arg(ap, int);
945 case GDI_SELECTBOX_CHAR_UNSELECTABLE:
946 gi->selectbox.char_unselectable = (char)va_arg(ap, int);
949 case GDI_DESIGN_UNPRESSED:
950 gi->design[GD_BUTTON_UNPRESSED].bitmap = va_arg(ap, Bitmap *);
951 gi->design[GD_BUTTON_UNPRESSED].x = va_arg(ap, int);
952 gi->design[GD_BUTTON_UNPRESSED].y = va_arg(ap, int);
955 case GDI_DESIGN_PRESSED:
956 gi->design[GD_BUTTON_PRESSED].bitmap = va_arg(ap, Bitmap *);
957 gi->design[GD_BUTTON_PRESSED].x = va_arg(ap, int);
958 gi->design[GD_BUTTON_PRESSED].y = va_arg(ap, int);
961 case GDI_ALT_DESIGN_UNPRESSED:
962 gi->alt_design[GD_BUTTON_UNPRESSED].bitmap= va_arg(ap, Bitmap *);
963 gi->alt_design[GD_BUTTON_UNPRESSED].x = va_arg(ap, int);
964 gi->alt_design[GD_BUTTON_UNPRESSED].y = va_arg(ap, int);
967 case GDI_ALT_DESIGN_PRESSED:
968 gi->alt_design[GD_BUTTON_PRESSED].bitmap = va_arg(ap, Bitmap *);
969 gi->alt_design[GD_BUTTON_PRESSED].x = va_arg(ap, int);
970 gi->alt_design[GD_BUTTON_PRESSED].y = va_arg(ap, int);
973 case GDI_BORDER_SIZE:
974 gi->border.xsize = va_arg(ap, int);
975 gi->border.ysize = va_arg(ap, int);
978 case GDI_BORDER_SIZE_SELECTBUTTON:
979 gi->border.xsize_selectbutton = va_arg(ap, int);
982 case GDI_DESIGN_WIDTH:
983 gi->border.width = va_arg(ap, int);
986 case GDI_DECORATION_DESIGN:
987 gi->deco.design.bitmap = va_arg(ap, Bitmap *);
988 gi->deco.design.x = va_arg(ap, int);
989 gi->deco.design.y = va_arg(ap, int);
992 case GDI_DECORATION_POSITION:
993 gi->deco.x = va_arg(ap, int);
994 gi->deco.y = va_arg(ap, int);
997 case GDI_DECORATION_SIZE:
998 gi->deco.width = va_arg(ap, int);
999 gi->deco.height = va_arg(ap, int);
1002 case GDI_DECORATION_SHIFTING:
1003 gi->deco.xshift = va_arg(ap, int);
1004 gi->deco.yshift = va_arg(ap, int);
1007 case GDI_DECORATION_MASKED:
1008 gi->deco.masked = (boolean)va_arg(ap, int);
1011 case GDI_EVENT_MASK:
1012 gi->event_mask = va_arg(ap, unsigned int);
1016 gi->drawing.area_xsize = va_arg(ap, int);
1017 gi->drawing.area_ysize = va_arg(ap, int);
1019 /* determine dependent values for drawing area gadget, if needed */
1020 if (gi->drawing.item_xsize != 0 && gi->drawing.item_ysize != 0)
1022 gi->width = gi->drawing.area_xsize * gi->drawing.item_xsize;
1023 gi->height = gi->drawing.area_ysize * gi->drawing.item_ysize;
1025 else if (gi->width != 0 && gi->height != 0)
1027 gi->drawing.item_xsize = gi->width / gi->drawing.area_xsize;
1028 gi->drawing.item_ysize = gi->height / gi->drawing.area_ysize;
1031 /* same tag also used for other gadget definitions */
1032 gi->textarea.xsize = gi->drawing.area_xsize;
1033 gi->textarea.ysize = gi->drawing.area_ysize;
1035 if (gi->type & GD_TYPE_TEXT_AREA) /* force recalculation */
1044 gi->drawing.item_xsize = va_arg(ap, int);
1045 gi->drawing.item_ysize = va_arg(ap, int);
1047 /* determine dependent values for drawing area gadget, if needed */
1048 if (gi->drawing.area_xsize != 0 && gi->drawing.area_ysize != 0)
1050 gi->width = gi->drawing.area_xsize * gi->drawing.item_xsize;
1051 gi->height = gi->drawing.area_ysize * gi->drawing.item_ysize;
1053 else if (gi->width != 0 && gi->height != 0)
1055 gi->drawing.area_xsize = gi->width / gi->drawing.item_xsize;
1056 gi->drawing.area_ysize = gi->height / gi->drawing.item_ysize;
1060 case GDI_SCROLLBAR_ITEMS_MAX:
1061 gi->scrollbar.items_max = va_arg(ap, int);
1064 case GDI_SCROLLBAR_ITEMS_VISIBLE:
1065 gi->scrollbar.items_visible = va_arg(ap, int);
1068 case GDI_SCROLLBAR_ITEM_POSITION:
1069 gi->scrollbar.item_position = va_arg(ap, int);
1072 case GDI_WHEEL_AREA_X:
1073 gi->wheelarea.x = va_arg(ap, int);
1076 case GDI_WHEEL_AREA_Y:
1077 gi->wheelarea.y = va_arg(ap, int);
1080 case GDI_WHEEL_AREA_WIDTH:
1081 gi->wheelarea.width = va_arg(ap, int);
1084 case GDI_WHEEL_AREA_HEIGHT:
1085 gi->wheelarea.height = va_arg(ap, int);
1088 case GDI_CALLBACK_INFO:
1089 gi->callback_info = va_arg(ap, gadget_function);
1092 case GDI_CALLBACK_ACTION:
1093 gi->callback_action = va_arg(ap, gadget_function);
1097 Error(ERR_EXIT, "HandleGadgetTags(): unknown tag %d", tag);
1100 tag = va_arg(ap, int); /* read next tag */
1103 gi->deactivated = FALSE;
1105 /* check if gadget has undefined bitmaps */
1106 if (gi->type != GD_TYPE_DRAWING_AREA &&
1107 (gi->design[GD_BUTTON_UNPRESSED].bitmap == NULL ||
1108 gi->design[GD_BUTTON_PRESSED].bitmap == NULL))
1109 gi->deactivated = TRUE;
1111 /* check if gadget is placed off-screen */
1112 if (gi->x < 0 || gi->y < 0)
1113 gi->deactivated = TRUE;
1115 /* adjust gadget values in relation to other gadget values */
1117 if (gi->type & GD_TYPE_TEXT_INPUT)
1119 int font_nr = gi->font_active;
1120 int font_width = getFontWidth(font_nr);
1121 int font_height = getFontHeight(font_nr);
1122 int border_xsize = gi->border.xsize;
1123 int border_ysize = gi->border.ysize;
1125 if (gi->type == GD_TYPE_TEXT_INPUT_NUMERIC)
1127 int number_min = gi->textinput.number_min;
1128 int number_max = gi->textinput.number_max;
1129 int min_size_min = get_minimal_size_for_numeric_input(number_min);
1130 int min_size_max = get_minimal_size_for_numeric_input(number_max);
1131 int min_size = MAX(min_size_min, min_size_max);
1133 /* expand gadget text input size, if maximal value is too large */
1134 if (gi->textinput.size < min_size)
1135 gi->textinput.size = min_size;
1138 gi->width = 2 * border_xsize + (gi->textinput.size + 1) * font_width;
1139 gi->height = 2 * border_ysize + font_height;
1142 if (gi->type & GD_TYPE_SELECTBOX)
1144 int font_nr = gi->font_active;
1145 int font_width = getFontWidth(font_nr);
1146 int font_height = getFontHeight(font_nr);
1147 int border_xsize = gi->border.xsize;
1148 int border_ysize = gi->border.ysize;
1149 int button_size = gi->border.xsize_selectbutton;
1150 int bottom_screen_border = gfx.sy + gfx.sysize - font_height;
1154 gi->width = 2 * border_xsize + gi->textinput.size*font_width +button_size;
1155 gi->height = 2 * border_ysize + font_height;
1157 if (gi->selectbox.options == NULL)
1158 Error(ERR_EXIT, "selectbox gadget incomplete (missing options array)");
1160 gi->selectbox.num_values = 0;
1161 while (gi->selectbox.options[gi->selectbox.num_values].text != NULL)
1162 gi->selectbox.num_values++;
1164 /* calculate values for open selectbox */
1165 gi->selectbox.width = gi->width;
1166 gi->selectbox.height =
1167 2 * border_ysize + gi->selectbox.num_values * font_height;
1169 gi->selectbox.x = gi->x;
1170 gi->selectbox.y = gi->y + gi->height;
1171 if (gi->selectbox.y + gi->selectbox.height > bottom_screen_border)
1172 gi->selectbox.y = gi->y - gi->selectbox.height;
1173 if (gi->selectbox.y < 0)
1174 gi->selectbox.y = bottom_screen_border - gi->selectbox.height;
1176 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &src_bitmap, &src_x, &src_y);
1177 src_x += font_width / 2;
1178 src_y += font_height / 2;
1180 /* there may be esoteric cases with missing or too small font bitmap */
1181 if (src_bitmap != NULL &&
1182 src_x < src_bitmap->width && src_y < src_bitmap->height)
1183 gi->selectbox.inverse_color = GetPixel(src_bitmap, src_x, src_y);
1185 /* always start with closed selectbox */
1186 gi->selectbox.open = FALSE;
1189 if (gi->type & GD_TYPE_TEXT_INPUT_NUMERIC)
1191 struct GadgetTextInput *text = &gi->textinput;
1192 int value = text->number_value;
1194 text->number_value = (value < text->number_min ? text->number_min :
1195 value > text->number_max ? text->number_max :
1198 sprintf(text->value, "%d", text->number_value);
1201 if (gi->type & GD_TYPE_TEXT_BUTTON)
1203 int font_nr = gi->font_active;
1204 int font_width = getFontWidth(font_nr);
1205 int font_height = getFontHeight(font_nr);
1206 int border_xsize = gi->border.xsize;
1207 int border_ysize = gi->border.ysize;
1209 gi->width = 2 * border_xsize + gi->textbutton.size * font_width;
1210 gi->height = 2 * border_ysize + font_height;
1213 if (gi->type & GD_TYPE_SCROLLBAR)
1215 struct GadgetScrollbar *gs = &gi->scrollbar;
1216 int scrollbar_size_cmp;
1218 if (gi->width == 0 || gi->height == 0 ||
1219 gs->items_max == 0 || gs->items_visible == 0)
1220 Error(ERR_EXIT, "scrollbar gadget incomplete (missing tags)");
1222 /* calculate internal scrollbar values */
1223 gs->size_min = (gi->type == GD_TYPE_SCROLLBAR_VERTICAL ?
1224 gi->width : gi->height);
1225 gs->size_max = (gi->type == GD_TYPE_SCROLLBAR_VERTICAL ?
1226 gi->height : gi->width);
1228 scrollbar_size_cmp = gs->size_max * gs->items_visible / gs->items_max;
1229 gs->size = MAX(scrollbar_size_cmp, gs->size_min);
1230 gs->size_max_cmp = (gs->size_max - (gs->size - scrollbar_size_cmp));
1232 gs->position = gs->size_max_cmp * gs->item_position / gs->items_max;
1233 gs->position_max = gs->size_max - gs->size;
1234 gs->correction = gs->size_max / gs->items_max / 2;
1236 /* finetuning for maximal right/bottom position */
1237 if (gs->item_position == gs->items_max - gs->items_visible)
1238 gs->position = gs->position_max;
1241 if (gi->type & GD_TYPE_TEXT_AREA)
1243 int font_nr = gi->font_active;
1244 int font_width = getFontWidth(font_nr);
1245 int font_height = getFontHeight(font_nr);
1246 int border_xsize = gi->border.xsize;
1247 int border_ysize = gi->border.ysize;
1249 if (gi->width == 0 || gi->height == 0)
1251 gi->width = 2 * border_xsize + gi->textarea.xsize * font_width;
1252 gi->height = 2 * border_ysize + gi->textarea.ysize * font_height;
1256 gi->textarea.xsize = (gi->width - 2 * border_xsize) / font_width;
1257 gi->textarea.ysize = (gi->height - 2 * border_ysize) / font_height;
1262 void ModifyGadget(struct GadgetInfo *gi, int first_tag, ...)
1266 va_start(ap, first_tag);
1267 HandleGadgetTags(gi, first_tag, ap);
1273 void RedrawGadget(struct GadgetInfo *gi)
1275 if (gi == NULL || gi->deactivated)
1279 DrawGadget(gi, gi->state, gi->direct_draw);
1282 struct GadgetInfo *CreateGadget(int first_tag, ...)
1284 struct GadgetInfo *new_gadget = checked_calloc(sizeof(struct GadgetInfo));
1287 /* always start with reliable default values */
1288 new_gadget->id = getNewGadgetID();
1289 new_gadget->image_id = -1;
1290 new_gadget->callback_info = default_callback_info;
1291 new_gadget->callback_action = default_callback_action;
1292 new_gadget->active = TRUE;
1293 new_gadget->direct_draw = TRUE;
1295 new_gadget->next = NULL;
1297 va_start(ap, first_tag);
1298 HandleGadgetTags(new_gadget, first_tag, ap);
1301 /* insert new gadget into global gadget list */
1302 if (gadget_list_last_entry)
1304 gadget_list_last_entry->next = new_gadget;
1305 gadget_list_last_entry = gadget_list_last_entry->next;
1308 gadget_list_first_entry = gadget_list_last_entry = new_gadget;
1313 void FreeGadget(struct GadgetInfo *gi)
1315 struct GadgetInfo *gi_previous = gadget_list_first_entry;
1320 /* prevent "last_info_gi" from pointing to memory that will be freed */
1321 if (last_info_gi == gi)
1322 last_info_gi = NULL;
1324 while (gi_previous != NULL && gi_previous->next != gi)
1325 gi_previous = gi_previous->next;
1327 if (gi == gadget_list_first_entry)
1328 gadget_list_first_entry = gi->next;
1330 if (gi == gadget_list_last_entry)
1331 gadget_list_last_entry = gi_previous;
1333 if (gi_previous != NULL)
1334 gi_previous->next = gi->next;
1339 static void CheckRangeOfNumericInputGadget(struct GadgetInfo *gi)
1341 if (gi->type != GD_TYPE_TEXT_INPUT_NUMERIC)
1344 gi->textinput.number_value = atoi(gi->textinput.value);
1346 if (gi->textinput.number_value < gi->textinput.number_min)
1347 gi->textinput.number_value = gi->textinput.number_min;
1348 if (gi->textinput.number_value > gi->textinput.number_max)
1349 gi->textinput.number_value = gi->textinput.number_max;
1351 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
1353 if (gi->textinput.cursor_position < 0)
1354 gi->textinput.cursor_position = 0;
1355 else if (gi->textinput.cursor_position > strlen(gi->textinput.value))
1356 gi->textinput.cursor_position = strlen(gi->textinput.value);
1359 /* global pointer to gadget actually in use (when mouse button pressed) */
1360 static struct GadgetInfo *last_gi = NULL;
1362 static void MapGadgetExt(struct GadgetInfo *gi, boolean redraw)
1364 if (gi == NULL || gi->deactivated || gi->mapped)
1370 DrawGadget(gi, DG_UNPRESSED, DG_BUFFERED);
1373 void MapGadget(struct GadgetInfo *gi)
1375 MapGadgetExt(gi, TRUE);
1378 void UnmapGadget(struct GadgetInfo *gi)
1380 if (gi == NULL || gi->deactivated || !gi->mapped)
1389 #define MAX_NUM_GADGETS 1024
1390 #define MULTIMAP_UNMAP (1 << 0)
1391 #define MULTIMAP_REMAP (1 << 1)
1392 #define MULTIMAP_REDRAW (1 << 2)
1393 #define MULTIMAP_PLAYFIELD (1 << 3)
1394 #define MULTIMAP_DOOR_1 (1 << 4)
1395 #define MULTIMAP_DOOR_2 (1 << 5)
1396 #define MULTIMAP_DOOR_3 (1 << 6)
1397 #define MULTIMAP_ALL (MULTIMAP_PLAYFIELD | \
1402 static void MultiMapGadgets(int mode)
1404 struct GadgetInfo *gi = gadget_list_first_entry;
1405 static boolean map_state[MAX_NUM_GADGETS];
1413 if ((mode & MULTIMAP_PLAYFIELD && IN_GFX_FIELD_FULL(x, y)) ||
1414 (mode & MULTIMAP_DOOR_1 && IN_GFX_DOOR_1(x, y)) ||
1415 (mode & MULTIMAP_DOOR_2 && IN_GFX_DOOR_2(x, y)) ||
1416 (mode & MULTIMAP_DOOR_3 && IN_GFX_DOOR_3(x, y)) ||
1417 (mode & MULTIMAP_ALL) == MULTIMAP_ALL)
1419 if (mode & MULTIMAP_UNMAP)
1421 map_state[map_count++ % MAX_NUM_GADGETS] = gi->mapped;
1426 if (map_state[map_count++ % MAX_NUM_GADGETS])
1427 MapGadgetExt(gi, (mode & MULTIMAP_REDRAW));
1435 void UnmapAllGadgets()
1437 MultiMapGadgets(MULTIMAP_ALL | MULTIMAP_UNMAP);
1440 void RemapAllGadgets()
1442 MultiMapGadgets(MULTIMAP_ALL | MULTIMAP_REMAP);
1445 boolean anyTextInputGadgetActive()
1447 return (last_gi && (last_gi->type & GD_TYPE_TEXT_INPUT) && last_gi->mapped);
1450 boolean anyTextAreaGadgetActive()
1452 return (last_gi && (last_gi->type & GD_TYPE_TEXT_AREA) && last_gi->mapped);
1455 boolean anySelectboxGadgetActive()
1457 return (last_gi && (last_gi->type & GD_TYPE_SELECTBOX) && last_gi->mapped);
1460 boolean anyScrollbarGadgetActive()
1462 return (last_gi && (last_gi->type & GD_TYPE_SCROLLBAR) && last_gi->mapped);
1465 boolean anyTextGadgetActive()
1467 return (anyTextInputGadgetActive() ||
1468 anyTextAreaGadgetActive() ||
1469 anySelectboxGadgetActive());
1472 static boolean insideSelectboxLine(struct GadgetInfo *gi, int mx, int my)
1474 return (gi != NULL &&
1475 gi->type & GD_TYPE_SELECTBOX &&
1476 mx >= gi->x && mx < gi->x + gi->width &&
1477 my >= gi->y && my < gi->y + gi->height);
1480 static boolean insideSelectboxArea(struct GadgetInfo *gi, int mx, int my)
1482 return (gi != NULL &&
1483 gi->type & GD_TYPE_SELECTBOX &&
1484 mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width &&
1485 my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height);
1488 void ClickOnGadget(struct GadgetInfo *gi, int button)
1490 if (gi == NULL || gi->deactivated || !gi->mapped)
1493 /* simulate releasing mouse button over last gadget, if still pressed */
1495 HandleGadgets(-1, -1, 0);
1497 /* simulate pressing mouse button over specified gadget */
1498 HandleGadgets(gi->x, gi->y, button);
1500 /* simulate releasing mouse button over specified gadget */
1501 HandleGadgets(gi->x, gi->y, 0);
1504 boolean HandleGadgets(int mx, int my, int button)
1506 static unsigned int pressed_delay = 0;
1507 static unsigned int pressed_delay_value = GADGET_FRAME_DELAY;
1508 static int last_button = 0;
1509 static int last_mx = 0, last_my = 0;
1510 static int pressed_mx = 0, pressed_my = 0;
1511 static boolean keep_selectbox_open = FALSE;
1512 static boolean gadget_stopped = FALSE;
1513 int scrollbar_mouse_pos = 0;
1514 struct GadgetInfo *new_gi, *gi;
1515 boolean press_event;
1516 boolean release_event;
1517 boolean mouse_moving;
1518 boolean mouse_inside_select_line;
1519 boolean mouse_inside_select_area;
1520 boolean mouse_released_where_pressed;
1521 boolean gadget_pressed;
1522 boolean gadget_pressed_repeated;
1523 boolean gadget_pressed_off_borders;
1524 boolean gadget_pressed_inside_select_line;
1525 boolean gadget_pressed_delay_reached;
1526 boolean gadget_moving;
1527 boolean gadget_moving_inside;
1528 boolean gadget_moving_off_borders;
1529 boolean gadget_draggable;
1530 boolean gadget_dragging;
1531 boolean gadget_released;
1532 boolean gadget_released_inside;
1533 boolean gadget_released_inside_select_area;
1534 boolean gadget_released_off_borders;
1535 boolean changed_position = FALSE;
1537 /* check if there are any gadgets defined */
1538 if (gadget_list_first_entry == NULL)
1541 /* simulated release of mouse button over last gadget */
1542 if (mx == -1 && my == -1 && button == 0)
1548 /* check which gadget is under the mouse pointer */
1549 new_gi = getGadgetInfoFromMousePosition(mx, my, button);
1551 /* check if button state has changed since last invocation */
1552 press_event = (button != 0 && last_button == 0);
1553 release_event = (button == 0 && last_button != 0);
1554 last_button = button;
1556 /* check if mouse has been moved since last invocation */
1557 mouse_moving = ((mx != last_mx || my != last_my) && motion_status);
1561 if (press_event && new_gi != last_gi)
1567 mouse_released_where_pressed =
1568 (release_event && mx == pressed_mx && my == pressed_my);
1570 mouse_inside_select_line = insideSelectboxLine(new_gi, mx, my);
1571 mouse_inside_select_area = insideSelectboxArea(new_gi, mx, my);
1573 gadget_pressed_off_borders = (press_event && new_gi != last_gi);
1575 gadget_pressed_inside_select_line =
1576 (press_event && new_gi != NULL &&
1577 new_gi->type & GD_TYPE_SELECTBOX && new_gi->selectbox.open &&
1578 insideSelectboxLine(new_gi, mx, my));
1580 /* if mouse button pressed outside text or selectbox gadget, deactivate it */
1581 if (anyTextGadgetActive() &&
1582 (gadget_pressed_off_borders ||
1583 (gadget_pressed_inside_select_line && !mouse_inside_select_area)))
1585 struct GadgetInfo *gi = last_gi;
1586 boolean gadget_changed = ((gi->event_mask & GD_EVENT_TEXT_LEAVING) != 0);
1588 /* check if text gadget has changed its value */
1589 if (gi->type & GD_TYPE_TEXT_INPUT)
1591 CheckRangeOfNumericInputGadget(gi);
1593 if (!strEqual(gi->textinput.last_value, gi->textinput.value))
1594 strcpy(gi->textinput.last_value, gi->textinput.value);
1596 gadget_changed = FALSE;
1599 /* selectbox does not change its value when closed by clicking outside */
1600 if (gi->type & GD_TYPE_SELECTBOX)
1601 gadget_changed = FALSE;
1603 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1605 gi->event.type = GD_EVENT_TEXT_LEAVING;
1607 if (gadget_changed && !(gi->type & GD_TYPE_SELECTBOX))
1608 gi->callback_action(gi);
1612 if (gadget_pressed_inside_select_line)
1619 (button != 0 && last_gi == NULL && new_gi != NULL && press_event);
1620 gadget_pressed_repeated =
1621 (button != 0 && last_gi != NULL && new_gi == last_gi);
1623 gadget_pressed_delay_reached =
1624 DelayReached(&pressed_delay, pressed_delay_value);
1626 gadget_released = (release_event && last_gi != NULL);
1627 gadget_released_inside = (gadget_released && new_gi == last_gi);
1628 gadget_released_off_borders = (gadget_released && new_gi != last_gi);
1630 gadget_moving = (button != 0 && last_gi != NULL && mouse_moving);
1631 gadget_moving_inside = (gadget_moving && new_gi == last_gi);
1632 gadget_moving_off_borders = (gadget_moving && new_gi != last_gi);
1634 /* when handling selectbox, set additional state values */
1635 if (gadget_released_inside && (last_gi->type & GD_TYPE_SELECTBOX))
1636 gadget_released_inside_select_area = insideSelectboxArea(last_gi, mx, my);
1638 gadget_released_inside_select_area = FALSE;
1640 /* setting state for handling over-large selectbox */
1641 if (keep_selectbox_open && (press_event || !mouse_inside_select_line))
1642 keep_selectbox_open = FALSE;
1644 /* if new gadget pressed, store this gadget */
1648 /* 'gi' is actually handled gadget */
1651 /* if gadget is scrollbar, choose mouse position value */
1652 if (gi && gi->type & GD_TYPE_SCROLLBAR)
1653 scrollbar_mouse_pos =
1654 (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx - gi->x : my - gi->y);
1656 /* if mouse button released, no gadget needs to be handled anymore */
1657 if (gadget_released)
1659 if (gi->type & GD_TYPE_SELECTBOX &&
1660 (keep_selectbox_open ||
1661 mouse_released_where_pressed ||
1662 !gadget_released_inside_select_area ||
1663 !CURRENT_OPTION_SELECTABLE(gi))) /* selectbox stays open */
1665 gi->selectbox.stay_open = TRUE;
1669 else if (!(gi->type & GD_TYPE_TEXT_INPUT ||
1670 gi->type & GD_TYPE_TEXT_AREA)) /* text input stays open */
1674 /* modify event position values even if no gadget is pressed */
1675 if (button == 0 && !release_event)
1678 /* if new gadget or if no gadget was pressed, release stopped processing */
1679 if (gadget_pressed || new_gi == NULL)
1680 gadget_stopped = FALSE;
1682 /* if gadget was stopped while being handled, stop gadget processing here */
1688 int last_x = gi->event.x;
1689 int last_y = gi->event.y;
1690 int last_mx = gi->event.mx;
1691 int last_my = gi->event.my;
1693 gi->event.x = gi->event.mx = mx - gi->x;
1694 gi->event.y = gi->event.my = my - gi->y;
1696 if (gi->type == GD_TYPE_DRAWING_AREA)
1698 gi->event.x /= gi->drawing.item_xsize;
1699 gi->event.y /= gi->drawing.item_ysize;
1701 if (last_x != gi->event.x || last_y != gi->event.y ||
1702 ((last_mx != gi->event.mx || last_my != gi->event.my) &&
1703 gi->event_mask & GD_EVENT_PIXEL_PRECISE))
1704 changed_position = TRUE;
1706 else if (gi->type & GD_TYPE_TEXT_INPUT && button != 0 && !motion_status)
1708 int old_cursor_position = gi->textinput.cursor_position;
1710 /* if mouse button pressed inside activated text gadget, set cursor */
1711 gi->textinput.cursor_position =
1712 (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font);
1714 if (gi->textinput.cursor_position < 0)
1715 gi->textinput.cursor_position = 0;
1716 else if (gi->textinput.cursor_position > strlen(gi->textinput.value))
1717 gi->textinput.cursor_position = strlen(gi->textinput.value);
1719 if (gi->textinput.cursor_position != old_cursor_position)
1720 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1723 StartTextInput(gi->x, gi->y, gi->width, gi->height);
1725 else if (gi->type & GD_TYPE_TEXT_AREA && button != 0 && !motion_status)
1727 int old_cursor_position = gi->textarea.cursor_position;
1728 int x = (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font);
1729 int y = (my - gi->y - gi->border.ysize) / getFontHeight(gi->font);
1731 x = (x < 0 ? 0 : x >= gi->textarea.xsize ? gi->textarea.xsize - 1 : x);
1732 y = (y < 0 ? 0 : y >= gi->textarea.ysize ? gi->textarea.ysize - 1 : y);
1734 setTextAreaCursorXY(gi, x, y);
1736 if (gi->textarea.cursor_position != old_cursor_position)
1737 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1740 StartTextInput(gi->x, gi->y, gi->width, gi->height);
1742 else if (gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open &&
1743 !keep_selectbox_open)
1745 int old_index = gi->selectbox.current_index;
1747 /* if mouse moving inside activated selectbox, select value */
1748 if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
1749 gi->selectbox.current_index =
1750 (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font);
1752 if (gi->selectbox.current_index < 0)
1753 gi->selectbox.current_index = 0;
1754 else if (gi->selectbox.current_index > gi->selectbox.num_values - 1)
1755 gi->selectbox.current_index = gi->selectbox.num_values - 1;
1757 if (gi->selectbox.current_index != old_index)
1758 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1762 /* handle gadget popup info text */
1763 if (last_info_gi != new_gi ||
1764 (new_gi && new_gi->type == GD_TYPE_DRAWING_AREA && changed_position))
1766 if (new_gi != NULL && (button == 0 || new_gi == last_gi))
1768 new_gi->event.type = GD_EVENT_INFO_ENTERING;
1769 new_gi->callback_info(new_gi);
1771 else if (last_info_gi != NULL && last_info_gi->mapped)
1773 last_info_gi->event.type = GD_EVENT_INFO_LEAVING;
1774 last_info_gi->callback_info(last_info_gi);
1777 last_info_gi = new_gi;
1780 gadget_draggable = (gi && gi->type & GD_TYPE_SCROLLBAR);
1782 /* reset drag position for newly pressed scrollbar to "not dragging" */
1783 if (gadget_pressed && gadget_draggable)
1784 gi->scrollbar.drag_position = -1;
1786 gadget_dragging = (gadget_draggable && gi->scrollbar.drag_position != -1);
1788 /* clicking next to a scrollbar to move it is not considered "moving" */
1789 if (gadget_draggable && !gadget_dragging)
1790 gadget_moving = FALSE;
1792 /* when leaving scrollbar area when jump-scrolling, stop gadget processing */
1793 if (gadget_draggable && !gadget_dragging && gadget_moving_off_borders)
1794 gadget_stopped = TRUE;
1796 if ((gadget_pressed) ||
1797 (gadget_pressed_repeated && gadget_pressed_delay_reached))
1799 if (gadget_pressed) /* gadget pressed the first time */
1801 /* initialize delay counter */
1802 DelayReached(&pressed_delay, 0);
1804 /* start gadget delay with longer delay after first click on gadget */
1805 pressed_delay_value = GADGET_FRAME_DELAY_FIRST;
1807 else /* gadget hold pressed for some time */
1809 /* after first repeated gadget click, continue with shorter delay value */
1810 pressed_delay_value = GADGET_FRAME_DELAY;
1813 if (gi->type & GD_TYPE_SCROLLBAR && !gadget_dragging)
1815 int mpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx : my);
1816 int gpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? gi->x : gi->y);
1817 int slider_start = gpos + gi->scrollbar.position;
1818 int slider_end = gpos + gi->scrollbar.position + gi->scrollbar.size - 1;
1819 boolean inside_slider = (mpos >= slider_start && mpos <= slider_end);
1821 if (IS_WHEEL_BUTTON(button) || !inside_slider)
1823 /* click scrollbar one scrollbar length up/left or down/right */
1825 struct GadgetScrollbar *gs = &gi->scrollbar;
1826 int old_item_position = gs->item_position;
1827 int item_steps = gs->items_visible - 1;
1828 int item_direction = (mpos < gpos + gi->scrollbar.position ? -1 : +1);
1830 if (IS_WHEEL_BUTTON(button))
1832 boolean scroll_single_step = ((GetKeyModState() & KMOD_Alt) != 0);
1834 item_steps = (scroll_single_step ? 1 : wheel_steps);
1835 item_direction = (button == MB_WHEEL_UP ||
1836 button == MB_WHEEL_LEFT ? -1 : +1);
1839 changed_position = FALSE;
1841 gs->item_position += item_steps * item_direction;
1843 if (gs->item_position < 0)
1844 gs->item_position = 0;
1845 else if (gs->item_position > gs->items_max - gs->items_visible)
1846 gs->item_position = gs->items_max - gs->items_visible;
1848 if (old_item_position != gs->item_position)
1850 gi->event.item_position = gs->item_position;
1851 changed_position = TRUE;
1854 ModifyGadget(gi, GDI_SCROLLBAR_ITEM_POSITION, gs->item_position,
1857 gi->state = GD_BUTTON_UNPRESSED;
1858 gi->event.type = GD_EVENT_MOVING;
1859 gi->event.off_borders = FALSE;
1861 if (gi->event_mask & GD_EVENT_MOVING && changed_position)
1862 gi->callback_action(gi);
1868 /* don't handle this scrollbar anymore when mouse position reached */
1869 if (gadget_pressed_repeated)
1871 gadget_stopped = TRUE;
1881 PlayGadgetSoundActivating();
1883 if (gi->type == GD_TYPE_CHECK_BUTTON)
1885 gi->checked = !gi->checked;
1887 else if (gi->type == GD_TYPE_RADIO_BUTTON)
1889 struct GadgetInfo *rgi = gadget_list_first_entry;
1894 rgi->type == GD_TYPE_RADIO_BUTTON &&
1895 rgi->radio_nr == gi->radio_nr &&
1898 rgi->checked = FALSE;
1899 DrawGadget(rgi, DG_UNPRESSED, rgi->direct_draw);
1907 else if (gi->type & GD_TYPE_SCROLLBAR)
1909 int mpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx : my);
1910 int gpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? gi->x : gi->y);
1911 int slider_start = gpos + gi->scrollbar.position;
1912 int slider_end = gpos + gi->scrollbar.position + gi->scrollbar.size - 1;
1913 boolean inside_slider = (mpos >= slider_start && mpos <= slider_end);
1915 if (!IS_WHEEL_BUTTON(button) && inside_slider)
1917 /* start dragging scrollbar */
1918 gi->scrollbar.drag_position =
1919 scrollbar_mouse_pos - gi->scrollbar.position;
1922 else if (gi->type & GD_TYPE_SELECTBOX)
1924 /* keep selectbox open in case of over-large selectbox */
1925 keep_selectbox_open = (mouse_inside_select_line &&
1926 mouse_inside_select_area);
1929 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1931 gi->state = GD_BUTTON_PRESSED;
1932 gi->event.type = GD_EVENT_PRESSED;
1933 gi->event.button = button;
1934 gi->event.off_borders = FALSE;
1936 if (gi->event_mask & GD_EVENT_PRESSED)
1937 gi->callback_action(gi);
1940 if (gadget_pressed_repeated)
1942 gi->event.type = GD_EVENT_PRESSED;
1944 if (gi->event_mask & GD_EVENT_REPEATED && gadget_pressed_delay_reached)
1945 gi->callback_action(gi);
1950 if (gi->type & GD_TYPE_BUTTON)
1952 if (gadget_moving_inside && gi->state == GD_BUTTON_UNPRESSED)
1953 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1954 else if (gadget_moving_off_borders && gi->state == GD_BUTTON_PRESSED)
1955 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1957 else if (gi->type & GD_TYPE_SELECTBOX && !keep_selectbox_open)
1959 int old_index = gi->selectbox.current_index;
1961 /* if mouse moving inside activated selectbox, select value */
1962 if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
1963 gi->selectbox.current_index =
1964 (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font);
1966 if (gi->selectbox.current_index < 0)
1967 gi->selectbox.current_index = 0;
1968 else if (gi->selectbox.current_index > gi->selectbox.num_values - 1)
1969 gi->selectbox.current_index = gi->selectbox.num_values - 1;
1971 if (gi->selectbox.current_index != old_index)
1972 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1974 else if (gi->type & GD_TYPE_SCROLLBAR)
1976 struct GadgetScrollbar *gs = &gi->scrollbar;
1977 int old_item_position = gs->item_position;
1979 gs->position = scrollbar_mouse_pos - gs->drag_position;
1981 /* make sure to always precisely reach end positions when dragging */
1982 if (gs->position <= 0)
1985 gs->item_position = 0;
1987 else if (gs->position >= gs->position_max)
1989 gs->position = gs->position_max;
1990 gs->item_position = gs->items_max - gs->items_visible;
1995 gs->items_max * (gs->position + gs->correction) / gs->size_max_cmp;
1998 if (gs->item_position < 0)
1999 gs->item_position = 0;
2000 if (gs->item_position > gs->items_max - 1)
2001 gs->item_position = gs->items_max - 1;
2003 if (old_item_position != gs->item_position)
2005 gi->event.item_position = gs->item_position;
2006 changed_position = TRUE;
2009 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2012 gi->state = (gadget_moving_inside || gadget_draggable ?
2013 GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
2014 gi->event.type = GD_EVENT_MOVING;
2015 gi->event.off_borders = gadget_moving_off_borders;
2017 if (gi->event_mask & GD_EVENT_MOVING && changed_position &&
2018 (gadget_moving_inside || gi->event_mask & GD_EVENT_OFF_BORDERS))
2019 gi->callback_action(gi);
2022 if (gadget_released_inside)
2024 boolean deactivate_gadget = TRUE;
2025 boolean gadget_changed = TRUE;
2027 if (gi->type & GD_TYPE_SELECTBOX)
2029 if (keep_selectbox_open ||
2030 mouse_released_where_pressed ||
2031 !gadget_released_inside_select_area ||
2032 !CURRENT_OPTION_SELECTABLE(gi)) /* selectbox stays open */
2034 deactivate_gadget = FALSE;
2035 gadget_changed = FALSE;
2037 else if (gi->selectbox.index != gi->selectbox.current_index)
2038 gi->selectbox.index = gi->selectbox.current_index;
2040 gadget_changed = FALSE;
2043 if (deactivate_gadget &&
2044 !(gi->type & GD_TYPE_TEXT_INPUT ||
2045 gi->type & GD_TYPE_TEXT_AREA)) /* text input stays open */
2046 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2048 gi->state = GD_BUTTON_UNPRESSED;
2049 gi->event.type = GD_EVENT_RELEASED;
2051 if ((gi->event_mask & GD_EVENT_RELEASED) && gadget_changed)
2053 gi->callback_action(gi);
2057 if (gadget_released_off_borders)
2059 if (gi->type & GD_TYPE_SCROLLBAR)
2060 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2062 gi->state = GD_BUTTON_UNPRESSED;
2063 gi->event.type = GD_EVENT_RELEASED;
2065 if (gi->event_mask & GD_EVENT_RELEASED &&
2066 gi->event_mask & GD_EVENT_OFF_BORDERS)
2067 gi->callback_action(gi);
2070 /* handle gadgets unmapped/mapped between pressing and releasing */
2071 if (release_event && !gadget_released && new_gi)
2072 new_gi->state = GD_BUTTON_UNPRESSED;
2074 return (gadget_pressed || gadget_pressed_repeated ||
2075 gadget_released || gadget_moving);
2078 static void insertCharIntoTextArea(struct GadgetInfo *gi, char c)
2080 char text[MAX_GADGET_TEXTSIZE + 1];
2081 int cursor_position = gi->textarea.cursor_position;
2083 if (strlen(gi->textarea.value) >= MAX_GADGET_TEXTSIZE) /* no space left */
2086 strcpy(text, gi->textarea.value);
2087 strcpy(&gi->textarea.value[cursor_position + 1], &text[cursor_position]);
2088 gi->textarea.value[cursor_position] = c;
2090 setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1);
2093 boolean HandleGadgetsKeyInput(Key key)
2095 struct GadgetInfo *gi = last_gi;
2097 if (gi == NULL || gi->deactivated || !gi->mapped ||
2098 !(gi->type & GD_TYPE_TEXT_INPUT ||
2099 gi->type & GD_TYPE_TEXT_AREA ||
2100 gi->type & GD_TYPE_SELECTBOX))
2103 if (key == KSYM_Escape)
2107 else if (key == KSYM_Return) /* valid for both text input and selectbox */
2109 boolean gadget_changed = ((gi->event_mask & GD_EVENT_TEXT_RETURN) != 0);
2111 if (gi->type & GD_TYPE_TEXT_INPUT)
2113 CheckRangeOfNumericInputGadget(gi);
2115 if (!strEqual(gi->textinput.last_value, gi->textinput.value))
2116 strcpy(gi->textinput.last_value, gi->textinput.value);
2118 gadget_changed = FALSE;
2122 else if (gi->type & GD_TYPE_SELECTBOX)
2124 if (gi->selectbox.index != gi->selectbox.current_index)
2125 gi->selectbox.index = gi->selectbox.current_index;
2127 gadget_changed = FALSE;
2130 if (gi->type & GD_TYPE_TEXT_AREA)
2132 insertCharIntoTextArea(gi, '\n');
2134 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2138 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2140 gi->event.type = GD_EVENT_TEXT_RETURN;
2146 gi->callback_action(gi);
2148 else if (gi->type & GD_TYPE_TEXT_INPUT) /* only valid for text input */
2150 char text[MAX_GADGET_TEXTSIZE + 1];
2151 int text_length = strlen(gi->textinput.value);
2152 int cursor_pos = gi->textinput.cursor_position;
2153 char letter = getCharFromKey(key);
2154 boolean legal_letter = (gi->type == GD_TYPE_TEXT_INPUT_NUMERIC ?
2155 letter >= '0' && letter <= '9' :
2158 if (legal_letter && text_length < gi->textinput.size)
2160 strcpy(text, gi->textinput.value);
2161 strcpy(&gi->textinput.value[cursor_pos + 1], &text[cursor_pos]);
2162 gi->textinput.value[cursor_pos] = letter;
2163 gi->textinput.cursor_position++;
2165 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2167 else if (key == KSYM_Left && cursor_pos > 0)
2169 gi->textinput.cursor_position--;
2171 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2173 else if (key == KSYM_Right && cursor_pos < text_length)
2175 gi->textinput.cursor_position++;
2177 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2179 else if (key == KSYM_BackSpace && cursor_pos > 0)
2181 strcpy(text, gi->textinput.value);
2182 strcpy(&gi->textinput.value[cursor_pos - 1], &text[cursor_pos]);
2183 gi->textinput.cursor_position--;
2185 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2187 else if (key == KSYM_Delete && cursor_pos < text_length)
2189 strcpy(text, gi->textinput.value);
2190 strcpy(&gi->textinput.value[cursor_pos], &text[cursor_pos + 1]);
2192 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2195 else if (gi->type & GD_TYPE_TEXT_AREA) /* only valid for text area */
2197 char text[MAX_GADGET_TEXTSIZE + 1];
2198 int text_length = strlen(gi->textarea.value);
2199 int area_ysize = gi->textarea.ysize;
2200 int cursor_x_pref = gi->textarea.cursor_x_preferred;
2201 int cursor_y = gi->textarea.cursor_y;
2202 int cursor_pos = gi->textarea.cursor_position;
2203 char letter = getCharFromKey(key);
2204 boolean legal_letter = (letter != 0);
2208 insertCharIntoTextArea(gi, letter);
2210 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2212 else if (key == KSYM_Left && cursor_pos > 0)
2214 setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1);
2216 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2218 else if (key == KSYM_Right && cursor_pos < text_length)
2220 setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1);
2222 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2224 else if (key == KSYM_Up && cursor_y > 0)
2226 setTextAreaCursorXY(gi, cursor_x_pref, cursor_y - 1);
2227 gi->textarea.cursor_x_preferred = cursor_x_pref;
2229 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2231 else if (key == KSYM_Down && cursor_y < area_ysize - 1)
2233 setTextAreaCursorXY(gi, cursor_x_pref, cursor_y + 1);
2234 gi->textarea.cursor_x_preferred = cursor_x_pref;
2236 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2238 else if (key == KSYM_BackSpace && cursor_pos > 0)
2240 strcpy(text, gi->textarea.value);
2241 strcpy(&gi->textarea.value[cursor_pos - 1], &text[cursor_pos]);
2243 setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1);
2245 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2247 else if (key == KSYM_Delete && cursor_pos < text_length)
2249 strcpy(text, gi->textarea.value);
2250 strcpy(&gi->textarea.value[cursor_pos], &text[cursor_pos + 1]);
2252 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2255 else if (gi->type & GD_TYPE_SELECTBOX) /* only valid for selectbox */
2257 int index = gi->selectbox.current_index;
2258 int num_values = gi->selectbox.num_values;
2260 if (key == KSYM_Up && index > 0)
2262 gi->selectbox.current_index--;
2264 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2266 else if (key == KSYM_Down && index < num_values - 1)
2268 gi->selectbox.current_index++;
2270 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2277 void DumpGadgetIdentifiers()
2279 struct GadgetInfo *gi;
2281 Print("Gadgets on current screen:\n");
2283 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
2285 if (gi->mapped && gi->image_id != -1)
2287 char *token = getTokenFromImageID(gi->image_id);
2288 char *prefix = "gfx.";
2290 if (strPrefix(token, prefix))
2291 token = &token[strlen(prefix)];
2293 Print("- '%s'\n", token);
2300 boolean DoGadgetAction(int image_id)
2302 struct GadgetInfo *gi;
2304 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
2306 if (gi->mapped && gi->image_id == image_id)
2308 gi->callback_action(gi);