1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // https://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(void)
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 Fail("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 DoGadgetCallbackAction(struct GadgetInfo *gi, boolean changed)
218 if (changed || gi->callback_action_always)
219 gi->callback_action(gi);
222 static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct)
224 struct GadgetDesign *gd;
225 int state = (pressed ? GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
226 boolean redraw_selectbox = FALSE;
228 if (gi == NULL || gi->deactivated)
231 if (gi->overlay_touch_button) // will be drawn later (as texture)
234 gd = (!gi->active ? &gi->alt_design[state] :
235 gi->checked ? &gi->alt_design[state] : &gi->design[state]);
239 case GD_TYPE_NORMAL_BUTTON:
240 case GD_TYPE_CHECK_BUTTON:
241 case GD_TYPE_RADIO_BUTTON:
243 BlitBitmapOnBackground(gd->bitmap, drawto,
244 gd->x, gd->y, gi->width, gi->height,
247 if (gi->deco.design.bitmap)
249 // make sure that decoration does not overlap gadget border
250 int deco_x = gi->deco.x + (pressed ? gi->deco.xshift : 0);
251 int deco_y = gi->deco.y + (pressed ? gi->deco.yshift : 0);
252 int deco_width = MIN(gi->deco.width, gi->width - deco_x);
253 int deco_height = MIN(gi->deco.height, gi->height - deco_y);
256 BlitBitmapMasked(gi->deco.design.bitmap, drawto,
257 gi->deco.design.x, gi->deco.design.y,
258 deco_width, deco_height,
259 gi->x + deco_x, gi->y + deco_y);
261 BlitBitmap(gi->deco.design.bitmap, drawto,
262 gi->deco.design.x, gi->deco.design.y,
263 deco_width, deco_height,
264 gi->x + deco_x, gi->y + deco_y);
269 case GD_TYPE_TEXT_BUTTON:
272 int font_nr = (gi->active ? gi->font_active : gi->font);
273 int font_width = getFontWidth(font_nr);
274 int border_x = gi->border.xsize;
275 int border_y = gi->border.ysize;
276 int text_size = strlen(gi->textbutton.value);
277 int text_start = (gi->width - text_size * font_width) / 2;
279 // left part of gadget
280 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
281 border_x, gi->height, gi->x, gi->y);
283 // middle part of gadget
284 for (i=0; i < gi->textbutton.size; i++)
285 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
286 font_width, gi->height,
287 gi->x + border_x + i * font_width, gi->y);
289 // right part of gadget
290 BlitBitmapOnBackground(gd->bitmap, drawto,
291 gd->x + gi->border.width - border_x, gd->y,
292 border_x, gi->height,
293 gi->x + gi->width - border_x, gi->y);
297 gi->x + text_start + (pressed ? gi->deco.xshift : 0),
298 gi->y + border_y + (pressed ? gi->deco.yshift : 0),
299 gi->textbutton.value, font_nr, BLIT_MASKED);
303 case GD_TYPE_TEXT_INPUT_ALPHANUMERIC:
304 case GD_TYPE_TEXT_INPUT_NUMERIC:
308 char cursor_string[2];
309 char text[MAX_GADGET_TEXTSIZE + 1];
310 int font_nr = (pressed ? gi->font_active : gi->font);
311 int font_width = getFontWidth(font_nr);
312 int border_x = gi->border.xsize;
313 int border_y = gi->border.ysize;
315 // left part of gadget
316 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
317 border_x, gi->height, gi->x, gi->y);
319 // middle part of gadget
320 for (i=0; i < gi->textinput.size + 1; i++)
321 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
322 font_width, gi->height,
323 gi->x + border_x + i * font_width, gi->y);
325 // right part of gadget
326 BlitBitmapOnBackground(gd->bitmap, drawto,
327 gd->x + gi->border.width - border_x, gd->y,
328 border_x, gi->height,
329 gi->x + gi->width - border_x, gi->y);
332 strcpy(text, gi->textinput.value);
337 gi->x + border_x, gi->y + border_y, text,
338 font_nr, BLIT_MASKED);
340 cursor_letter = gi->textinput.value[gi->textinput.cursor_position];
341 cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' ');
342 cursor_string[1] = '\0';
344 // draw cursor, if active
348 gi->textinput.cursor_position * font_width,
349 gi->y + border_y, cursor_string,
350 font_nr, BLIT_INVERSE);
354 case GD_TYPE_TEXT_AREA:
358 char cursor_string[2];
359 int font_nr = (pressed ? gi->font_active : gi->font);
360 int font_width = getFontWidth(font_nr);
361 int font_height = getFontHeight(font_nr);
362 int border_x = gi->border.xsize;
363 int border_y = gi->border.ysize;
364 int gd_height = 2 * border_y + font_height;
366 // top left part of gadget border
367 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
368 border_x, border_y, gi->x, gi->y);
370 // top middle part of gadget border
371 for (i=0; i < gi->textarea.xsize; i++)
372 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
373 font_width, border_y,
374 gi->x + border_x + i * font_width, gi->y);
376 // top right part of gadget border
377 BlitBitmapOnBackground(gd->bitmap, drawto,
378 gd->x + gi->border.width - border_x, gd->y,
380 gi->x + gi->width - border_x, gi->y);
382 // left and right part of gadget border for each row
383 for (i=0; i < gi->textarea.ysize; i++)
385 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y,
386 border_x, font_height,
387 gi->x, gi->y + border_y + i * font_height);
388 BlitBitmapOnBackground(gd->bitmap, drawto,
389 gd->x + gi->border.width - border_x,
391 border_x, font_height,
392 gi->x + gi->width - border_x,
393 gi->y + border_y + i * font_height);
396 // bottom left part of gadget border
397 BlitBitmapOnBackground(gd->bitmap, drawto,
398 gd->x, gd->y + gd_height - border_y,
400 gi->x, gi->y + gi->height - border_y);
402 // bottom middle part of gadget border
403 for (i=0; i < gi->textarea.xsize; i++)
404 BlitBitmapOnBackground(gd->bitmap, drawto,
406 gd->y + gd_height - border_y,
407 font_width, border_y,
408 gi->x + border_x + i * font_width,
409 gi->y + gi->height - border_y);
411 // bottom right part of gadget border
412 BlitBitmapOnBackground(gd->bitmap, drawto,
413 gd->x + gi->border.width - border_x,
414 gd->y + gd_height - border_y,
416 gi->x + gi->width - border_x,
417 gi->y + gi->height - border_y);
419 ClearRectangleOnBackground(drawto,
422 gi->width - 2 * border_x,
423 gi->height - 2 * border_y);
426 DrawTextBuffer(gi->x + border_x, gi->y + border_y, gi->textarea.value,
427 font_nr, gi->textarea.xsize, -1, gi->textarea.ysize, 0,
428 BLIT_ON_BACKGROUND, FALSE, FALSE, FALSE);
430 cursor_letter = gi->textarea.value[gi->textarea.cursor_position];
431 cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' ');
432 cursor_string[1] = '\0';
434 // draw cursor, if active
437 gi->x + border_x + gi->textarea.cursor_x * font_width,
438 gi->y + border_y + gi->textarea.cursor_y * font_height,
440 font_nr, BLIT_INVERSE);
444 case GD_TYPE_SELECTBOX:
447 char text[MAX_GADGET_TEXTSIZE + 1];
448 int font_nr_default = (pressed ? gi->font_active : gi->font);
449 int font_nr = font_nr_default;
450 int font_width = getFontWidth(font_nr);
451 int font_height = getFontHeight(font_nr);
452 int border_x = gi->border.xsize;
453 int border_y = gi->border.ysize;
454 int button = gi->border.xsize_selectbutton;
455 int width_inner = gi->border.width - button - 2 * border_x;
456 int box_width = gi->selectbox.width;
457 int box_height = gi->selectbox.height;
459 // left part of gadget
460 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
461 border_x, gi->height, gi->x, gi->y);
463 // middle part of gadget
464 for (i=0; i < gi->selectbox.size; i++)
465 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
466 font_width, gi->height,
467 gi->x + border_x + i * font_width, gi->y);
469 // button part of gadget
470 BlitBitmapOnBackground(gd->bitmap, drawto,
471 gd->x + border_x + width_inner, gd->y,
473 gi->x + gi->width - border_x - button, gi->y);
475 // right part of gadget
476 BlitBitmapOnBackground(gd->bitmap, drawto,
477 gd->x + gi->border.width - border_x, gd->y,
478 border_x, gi->height,
479 gi->x + gi->width - border_x, gi->y);
482 strncpy(text, gi->selectbox.options[gi->selectbox.index].text,
484 text[gi->selectbox.size] = '\0';
487 font_nr = (OPTION_TEXT_SELECTABLE(gi, text) ? font_nr_default :
488 gi->font_unselectable);
491 DrawTextExt(drawto, gi->x + border_x, gi->y + border_y, text,
492 font_nr, BLIT_MASKED);
496 if (!gi->selectbox.open)
498 gi->selectbox.open = TRUE;
499 gi->selectbox.stay_open = FALSE;
500 gi->selectbox.current_index = gi->selectbox.index;
502 // save background under selectbox
503 BlitBitmap(drawto, gfx.field_save_buffer,
504 gi->selectbox.x, gi->selectbox.y,
505 gi->selectbox.width, gi->selectbox.height,
506 gi->selectbox.x, gi->selectbox.y);
509 // draw open selectbox
511 // top left part of gadget border
512 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
514 gi->selectbox.x, gi->selectbox.y);
516 // top middle part of gadget border
517 for (i=0; i < gi->selectbox.size; i++)
518 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
519 font_width, border_y,
520 gi->selectbox.x + border_x + i * font_width,
523 // top button part of gadget border
524 BlitBitmapOnBackground(gd->bitmap, drawto,
525 gd->x + border_x + width_inner, gd->y,
527 gi->selectbox.x + box_width -border_x -button,
530 // top right part of gadget border
531 BlitBitmapOnBackground(gd->bitmap, drawto,
532 gd->x + gi->border.width - border_x, gd->y,
534 gi->selectbox.x + box_width - border_x,
537 // left and right part of gadget border for each row
538 for (i=0; i < gi->selectbox.num_values; i++)
540 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y,
541 border_x, font_height,
543 gi->selectbox.y + border_y + i*font_height);
544 BlitBitmapOnBackground(gd->bitmap, drawto,
545 gd->x + gi->border.width - border_x,
547 border_x, font_height,
548 gi->selectbox.x + box_width - border_x,
549 gi->selectbox.y + border_y + i*font_height);
552 // bottom left part of gadget border
553 BlitBitmapOnBackground(gd->bitmap, drawto,
554 gd->x, gd->y + gi->height - border_y,
557 gi->selectbox.y + box_height - border_y);
559 // bottom middle part of gadget border
560 for (i=0; i < gi->selectbox.size; i++)
561 BlitBitmapOnBackground(gd->bitmap, drawto,
563 gd->y + gi->height - border_y,
564 font_width, border_y,
565 gi->selectbox.x + border_x + i * font_width,
566 gi->selectbox.y + box_height - border_y);
568 // bottom button part of gadget border
569 BlitBitmapOnBackground(gd->bitmap, drawto,
570 gd->x + border_x + width_inner,
571 gd->y + gi->height - border_y,
573 gi->selectbox.x + box_width -border_x -button,
574 gi->selectbox.y + box_height - border_y);
576 // bottom right part of gadget border
577 BlitBitmapOnBackground(gd->bitmap, drawto,
578 gd->x + gi->border.width - border_x,
579 gd->y + gi->height - border_y,
581 gi->selectbox.x + box_width - border_x,
582 gi->selectbox.y + box_height - border_y);
584 ClearRectangleOnBackground(drawto,
585 gi->selectbox.x + border_x,
586 gi->selectbox.y + border_y,
587 gi->selectbox.width - 2 * border_x,
588 gi->selectbox.height - 2 * border_y);
590 // selectbox text values
591 for (i=0; i < gi->selectbox.num_values; i++)
593 int mask_mode = BLIT_MASKED;
595 strncpy(text, gi->selectbox.options[i].text, gi->selectbox.size);
596 text[gi->selectbox.size] = '\0';
598 font_nr = (OPTION_TEXT_SELECTABLE(gi, text) ? font_nr_default :
599 gi->font_unselectable);
601 if (i == gi->selectbox.current_index &&
602 OPTION_TEXT_SELECTABLE(gi, text))
604 FillRectangle(drawto,
605 gi->selectbox.x + border_x,
606 gi->selectbox.y + border_y + i * font_height,
607 gi->selectbox.width - 2 * border_x, font_height,
608 gi->selectbox.inverse_color);
610 // prevent use of cursor graphic by drawing at least two chars
612 text[gi->selectbox.size] = '\0';
614 mask_mode = BLIT_INVERSE;
618 gi->selectbox.x + border_x,
619 gi->selectbox.y + border_y + i * font_height, text,
623 redraw_selectbox = TRUE;
625 else if (gi->selectbox.open)
627 gi->selectbox.open = FALSE;
629 // restore background under selectbox
630 BlitBitmap(gfx.field_save_buffer, drawto,
631 gi->selectbox.x, gi->selectbox.y,
632 gi->selectbox.width, gi->selectbox.height,
633 gi->selectbox.x, gi->selectbox.y);
635 // redraw closed selectbox
636 DrawGadget(gi, FALSE, FALSE);
638 redraw_selectbox = TRUE;
643 case GD_TYPE_SCROLLBAR_VERTICAL:
647 int ypos = gi->y + gi->scrollbar.position;
648 int design_full = gi->width;
649 int design_body = design_full - 2 * gi->border.ysize;
650 int size_full = gi->scrollbar.size;
651 int size_body = size_full - 2 * gi->border.ysize;
652 int num_steps = size_body / design_body;
653 int step_size_remain = size_body - num_steps * design_body;
655 // clear scrollbar area
656 ClearRectangleOnBackground(backbuffer, gi->x, gi->y,
657 gi->width, gi->height);
659 // upper part of gadget
660 BlitBitmapOnBackground(gd->bitmap, drawto,
662 gi->width, gi->border.ysize,
665 // middle part of gadget
666 for (i=0; i < num_steps; i++)
667 BlitBitmapOnBackground(gd->bitmap, drawto,
668 gd->x, gd->y + gi->border.ysize,
669 gi->width, design_body,
671 ypos + gi->border.ysize + i * design_body);
673 // remaining middle part of gadget
674 if (step_size_remain > 0)
675 BlitBitmapOnBackground(gd->bitmap, drawto,
676 gd->x, gd->y + gi->border.ysize,
677 gi->width, step_size_remain,
679 ypos + gi->border.ysize
680 + num_steps * design_body);
682 // lower part of gadget
683 BlitBitmapOnBackground(gd->bitmap, drawto,
684 gd->x, gd->y + design_full - gi->border.ysize,
685 gi->width, gi->border.ysize,
686 xpos, ypos + size_full - gi->border.ysize);
690 case GD_TYPE_SCROLLBAR_HORIZONTAL:
693 int xpos = gi->x + gi->scrollbar.position;
695 int design_full = gi->height;
696 int design_body = design_full - 2 * gi->border.xsize;
697 int size_full = gi->scrollbar.size;
698 int size_body = size_full - 2 * gi->border.xsize;
699 int num_steps = size_body / design_body;
700 int step_size_remain = size_body - num_steps * design_body;
702 // clear scrollbar area
703 ClearRectangleOnBackground(backbuffer, gi->x, gi->y,
704 gi->width, gi->height);
706 // left part of gadget
707 BlitBitmapOnBackground(gd->bitmap, drawto,
709 gi->border.xsize, gi->height,
712 // middle part of gadget
713 for (i=0; i < num_steps; i++)
714 BlitBitmapOnBackground(gd->bitmap, drawto,
715 gd->x + gi->border.xsize, gd->y,
716 design_body, gi->height,
717 xpos + gi->border.xsize + i * design_body,
720 // remaining middle part of gadget
721 if (step_size_remain > 0)
722 BlitBitmapOnBackground(gd->bitmap, drawto,
723 gd->x + gi->border.xsize, gd->y,
724 step_size_remain, gi->height,
725 xpos + gi->border.xsize
726 + num_steps * design_body,
729 // right part of gadget
730 BlitBitmapOnBackground(gd->bitmap, drawto,
731 gd->x + design_full - gi->border.xsize, gd->y,
732 gi->border.xsize, gi->height,
733 xpos + size_full - gi->border.xsize, ypos);
741 // do not use direct gadget drawing anymore; this worked as a speed-up once,
742 // but would slow things down a lot now the screen is always fully redrawn
747 BlitBitmap(drawto, window,
748 gi->x, gi->y, gi->width, gi->height, gi->x, gi->y);
750 if (gi->type == GD_TYPE_SELECTBOX && redraw_selectbox)
751 BlitBitmap(drawto, window,
752 gi->selectbox.x, gi->selectbox.y,
753 gi->selectbox.width, gi->selectbox.height,
754 gi->selectbox.x, gi->selectbox.y);
761 redraw_mask |= (IN_GFX_FIELD_FULL(x, y) ? REDRAW_FIELD :
762 IN_GFX_DOOR_1(x, y) ? REDRAW_DOOR_1 :
763 IN_GFX_DOOR_2(x, y) ? REDRAW_DOOR_2 :
764 IN_GFX_DOOR_3(x, y) ? REDRAW_DOOR_3 : REDRAW_ALL);
768 static void SetGadgetPosition_OverlayTouchButton(struct GadgetInfo *gi)
774 gi->x += video.screen_width;
776 gi->y += video.screen_height;
778 gi->x -= video.screen_xoffset;
779 gi->y -= video.screen_yoffset;
782 void SetGadgetsPosition_OverlayTouchButtons(void)
784 struct GadgetInfo *gi;
786 if (gadget_list_first_entry == NULL)
789 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
790 if (gi->overlay_touch_button)
791 SetGadgetPosition_OverlayTouchButton(gi);
794 static void DrawGadget_OverlayTouchButton(struct GadgetInfo *gi)
796 struct GadgetDesign *gd;
797 int state = (gi->state ? GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
799 if (gi == NULL || gi->deactivated)
802 gd = (!gi->active ? &gi->alt_design[state] :
803 gi->checked ? &gi->alt_design[state] : &gi->design[state]);
805 int x = gi->x + video.screen_xoffset;
806 int y = gi->y + video.screen_yoffset;
807 int alpha = gi->overlay_touch_button_alpha;
808 int alpha_max = SDL_ALPHA_OPAQUE;
809 int alpha_step = ALPHA_FADING_STEPSIZE(alpha_max);
811 // only show mapped overlay touch buttons if touch screen is really used
812 if (gi->mapped && runtime.uses_touch_device)
814 if (alpha < alpha_max)
815 alpha = MIN(alpha + alpha_step, alpha_max);
819 alpha = MAX(0, alpha - alpha_step);
822 gi->overlay_touch_button_alpha = alpha;
829 case GD_TYPE_NORMAL_BUTTON:
830 case GD_TYPE_CHECK_BUTTON:
831 case GD_TYPE_RADIO_BUTTON:
832 SDL_SetTextureAlphaMod(gd->bitmap->texture_masked, alpha);
833 SDL_SetTextureBlendMode(gd->bitmap->texture_masked, SDL_BLENDMODE_BLEND);
835 BlitToScreenMasked(gd->bitmap, gd->x, gd->y, gi->width, gi->height, x, y);
843 void DrawGadgets_OverlayTouchButtons(void)
845 struct GadgetInfo *gi;
847 if (gadget_list_first_entry == NULL)
850 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
851 if (gi->overlay_touch_button)
852 DrawGadget_OverlayTouchButton(gi);
855 boolean CheckPosition_OverlayTouchButtons(int mx, int my, int button)
857 struct GadgetInfo *gi = getGadgetInfoFromMousePosition(mx, my, button);
859 return (gi != NULL && gi->overlay_touch_button);
862 static int get_minimal_size_for_numeric_input(int minmax_value)
864 int min_size = 1; // value needs at least one digit
867 // add number of digits needed for absolute value
868 for (i = 10; i <= ABS(minmax_value); i *= 10)
871 // if min/max value is negative, add one digit for minus sign
872 if (minmax_value < 0)
878 static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap)
882 if (gi == NULL || gi->deactivated)
885 while (tag != GDI_END)
890 gi->image_id = va_arg(ap, int);
894 gi->custom_id = va_arg(ap, int);
897 case GDI_CUSTOM_TYPE_ID:
898 gi->custom_type_id = va_arg(ap, int);
903 int max_textsize = MAX_INFO_TEXTSIZE;
904 char *text = va_arg(ap, char *);
907 strncpy(gi->info_text, text, max_textsize);
911 gi->info_text[max_textsize] = '\0';
916 gi->x = gi->orig_x = va_arg(ap, int);
920 gi->y = gi->orig_y = va_arg(ap, int);
924 gi->width = va_arg(ap, int);
928 gi->height = va_arg(ap, int);
932 gi->type = va_arg(ap, unsigned int);
936 gi->state = va_arg(ap, unsigned int);
940 gi->active = (boolean)va_arg(ap, int);
943 case GDI_DIRECT_DRAW:
944 gi->direct_draw = (boolean)va_arg(ap, int);
947 case GDI_OVERLAY_TOUCH_BUTTON:
948 gi->overlay_touch_button = (boolean)va_arg(ap, int);
949 if (gi->overlay_touch_button)
950 SetGadgetPosition_OverlayTouchButton(gi);
953 case GDI_CALLBACK_ACTION_ALWAYS:
954 gi->callback_action_always = (boolean)va_arg(ap, int);
958 gi->checked = (boolean)va_arg(ap, int);
962 gi->radio_nr = va_arg(ap, unsigned int);
965 case GDI_NUMBER_VALUE:
966 gi->textinput.number_value = va_arg(ap, int);
967 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
968 strcpy(gi->textinput.last_value, gi->textinput.value);
969 gi->textinput.cursor_position = strlen(gi->textinput.value);
973 gi->textinput.number_min = va_arg(ap, int);
974 if (gi->textinput.number_value < gi->textinput.number_min)
976 gi->textinput.number_value = gi->textinput.number_min;
977 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
978 strcpy(gi->textinput.last_value, gi->textinput.value);
983 gi->textinput.number_max = va_arg(ap, int);
984 if (gi->textinput.number_value > gi->textinput.number_max)
986 gi->textinput.number_value = gi->textinput.number_max;
987 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
988 strcpy(gi->textinput.last_value, gi->textinput.value);
994 int max_textsize = MAX_GADGET_TEXTSIZE;
996 if (gi->textinput.size)
997 max_textsize = MIN(gi->textinput.size, MAX_GADGET_TEXTSIZE);
999 strncpy(gi->textinput.value, va_arg(ap, char *), max_textsize);
1000 strcpy(gi->textinput.last_value, gi->textinput.value);
1002 gi->textinput.value[max_textsize] = '\0';
1003 gi->textinput.cursor_position = strlen(gi->textinput.value);
1005 // same tag also used for other gadget definitions
1006 strcpy(gi->textbutton.value, gi->textinput.value);
1007 strcpy(gi->textarea.value, gi->textinput.value);
1008 strcpy(gi->textarea.last_value, gi->textinput.value);
1014 int tag_value = va_arg(ap, int);
1015 int max_textsize = MIN(tag_value, MAX_GADGET_TEXTSIZE);
1017 gi->textinput.size = max_textsize;
1018 gi->textinput.value[max_textsize] = '\0';
1019 strcpy(gi->textinput.last_value, gi->textinput.value);
1021 // same tag also used for other gadget definitions
1023 gi->textarea.size = max_textsize;
1024 gi->textarea.value[max_textsize] = '\0';
1025 strcpy(gi->textarea.last_value, gi->textinput.value);
1027 gi->textbutton.size = max_textsize;
1028 gi->textbutton.value[max_textsize] = '\0';
1030 gi->selectbox.size = gi->textinput.size;
1035 gi->font = va_arg(ap, int);
1036 if (gi->font_active == 0)
1037 gi->font_active = gi->font;
1038 if (gi->font_unselectable == 0)
1039 gi->font_unselectable = gi->font;
1042 case GDI_TEXT_FONT_ACTIVE:
1043 gi->font_active = va_arg(ap, int);
1046 case GDI_TEXT_FONT_UNSELECTABLE:
1047 gi->font_unselectable = va_arg(ap, int);
1050 case GDI_SELECTBOX_OPTIONS:
1051 gi->selectbox.options = va_arg(ap, struct ValueTextInfo *);
1054 case GDI_SELECTBOX_INDEX:
1055 gi->selectbox.index = va_arg(ap, int);
1058 case GDI_SELECTBOX_CHAR_UNSELECTABLE:
1059 gi->selectbox.char_unselectable = (char)va_arg(ap, int);
1062 case GDI_DESIGN_UNPRESSED:
1063 gi->design[GD_BUTTON_UNPRESSED].bitmap = va_arg(ap, Bitmap *);
1064 gi->design[GD_BUTTON_UNPRESSED].x = va_arg(ap, int);
1065 gi->design[GD_BUTTON_UNPRESSED].y = va_arg(ap, int);
1068 case GDI_DESIGN_PRESSED:
1069 gi->design[GD_BUTTON_PRESSED].bitmap = va_arg(ap, Bitmap *);
1070 gi->design[GD_BUTTON_PRESSED].x = va_arg(ap, int);
1071 gi->design[GD_BUTTON_PRESSED].y = va_arg(ap, int);
1074 case GDI_ALT_DESIGN_UNPRESSED:
1075 gi->alt_design[GD_BUTTON_UNPRESSED].bitmap= va_arg(ap, Bitmap *);
1076 gi->alt_design[GD_BUTTON_UNPRESSED].x = va_arg(ap, int);
1077 gi->alt_design[GD_BUTTON_UNPRESSED].y = va_arg(ap, int);
1080 case GDI_ALT_DESIGN_PRESSED:
1081 gi->alt_design[GD_BUTTON_PRESSED].bitmap = va_arg(ap, Bitmap *);
1082 gi->alt_design[GD_BUTTON_PRESSED].x = va_arg(ap, int);
1083 gi->alt_design[GD_BUTTON_PRESSED].y = va_arg(ap, int);
1086 case GDI_BORDER_SIZE:
1087 gi->border.xsize = va_arg(ap, int);
1088 gi->border.ysize = va_arg(ap, int);
1091 case GDI_BORDER_SIZE_SELECTBUTTON:
1092 gi->border.xsize_selectbutton = va_arg(ap, int);
1095 case GDI_DESIGN_WIDTH:
1096 gi->border.width = va_arg(ap, int);
1099 case GDI_DECORATION_DESIGN:
1100 gi->deco.design.bitmap = va_arg(ap, Bitmap *);
1101 gi->deco.design.x = va_arg(ap, int);
1102 gi->deco.design.y = va_arg(ap, int);
1105 case GDI_DECORATION_POSITION:
1106 gi->deco.x = va_arg(ap, int);
1107 gi->deco.y = va_arg(ap, int);
1110 case GDI_DECORATION_SIZE:
1111 gi->deco.width = va_arg(ap, int);
1112 gi->deco.height = va_arg(ap, int);
1115 case GDI_DECORATION_SHIFTING:
1116 gi->deco.xshift = va_arg(ap, int);
1117 gi->deco.yshift = va_arg(ap, int);
1120 case GDI_DECORATION_MASKED:
1121 gi->deco.masked = (boolean)va_arg(ap, int);
1124 case GDI_EVENT_MASK:
1125 gi->event_mask = va_arg(ap, unsigned int);
1129 gi->drawing.area_xsize = va_arg(ap, int);
1130 gi->drawing.area_ysize = va_arg(ap, int);
1132 // determine dependent values for drawing area gadget, if needed
1133 if (gi->drawing.item_xsize != 0 && gi->drawing.item_ysize != 0)
1135 gi->width = gi->drawing.area_xsize * gi->drawing.item_xsize;
1136 gi->height = gi->drawing.area_ysize * gi->drawing.item_ysize;
1138 else if (gi->width != 0 && gi->height != 0)
1140 gi->drawing.item_xsize = gi->width / gi->drawing.area_xsize;
1141 gi->drawing.item_ysize = gi->height / gi->drawing.area_ysize;
1144 // same tag also used for other gadget definitions
1145 gi->textarea.xsize = gi->drawing.area_xsize;
1146 gi->textarea.ysize = gi->drawing.area_ysize;
1148 if (gi->type & GD_TYPE_TEXT_AREA) // force recalculation
1157 gi->drawing.item_xsize = va_arg(ap, int);
1158 gi->drawing.item_ysize = va_arg(ap, int);
1160 // determine dependent values for drawing area gadget, if needed
1161 if (gi->drawing.area_xsize != 0 && gi->drawing.area_ysize != 0)
1163 gi->width = gi->drawing.area_xsize * gi->drawing.item_xsize;
1164 gi->height = gi->drawing.area_ysize * gi->drawing.item_ysize;
1166 else if (gi->width != 0 && gi->height != 0)
1168 gi->drawing.area_xsize = gi->width / gi->drawing.item_xsize;
1169 gi->drawing.area_ysize = gi->height / gi->drawing.item_ysize;
1173 case GDI_SCROLLBAR_ITEMS_MAX:
1174 gi->scrollbar.items_max = va_arg(ap, int);
1177 case GDI_SCROLLBAR_ITEMS_VISIBLE:
1178 gi->scrollbar.items_visible = va_arg(ap, int);
1181 case GDI_SCROLLBAR_ITEM_POSITION:
1182 gi->scrollbar.item_position = va_arg(ap, int);
1185 case GDI_WHEEL_AREA_X:
1186 gi->wheelarea.x = va_arg(ap, int);
1189 case GDI_WHEEL_AREA_Y:
1190 gi->wheelarea.y = va_arg(ap, int);
1193 case GDI_WHEEL_AREA_WIDTH:
1194 gi->wheelarea.width = va_arg(ap, int);
1197 case GDI_WHEEL_AREA_HEIGHT:
1198 gi->wheelarea.height = va_arg(ap, int);
1201 case GDI_CALLBACK_INFO:
1202 gi->callback_info = va_arg(ap, gadget_function);
1205 case GDI_CALLBACK_ACTION:
1206 gi->callback_action = va_arg(ap, gadget_function);
1210 Fail("HandleGadgetTags(): unknown tag %d", tag);
1213 tag = va_arg(ap, int); // read next tag
1216 gi->deactivated = FALSE;
1218 // check if gadget has undefined bitmaps
1219 if (gi->type != GD_TYPE_DRAWING_AREA &&
1220 (gi->design[GD_BUTTON_UNPRESSED].bitmap == NULL ||
1221 gi->design[GD_BUTTON_PRESSED].bitmap == NULL))
1222 gi->deactivated = TRUE;
1224 // check if gadget is placed off-screen (and is no overlay touch button)
1225 if ((gi->x < 0 || gi->y < 0) && !gi->overlay_touch_button)
1226 gi->deactivated = TRUE;
1228 // adjust gadget values in relation to other gadget values
1230 if (gi->type & GD_TYPE_TEXT_INPUT)
1232 int font_nr = gi->font_active;
1233 int font_width = getFontWidth(font_nr);
1234 int font_height = getFontHeight(font_nr);
1235 int border_xsize = gi->border.xsize;
1236 int border_ysize = gi->border.ysize;
1238 if (gi->type == GD_TYPE_TEXT_INPUT_NUMERIC)
1240 int number_min = gi->textinput.number_min;
1241 int number_max = gi->textinput.number_max;
1242 int min_size_min = get_minimal_size_for_numeric_input(number_min);
1243 int min_size_max = get_minimal_size_for_numeric_input(number_max);
1244 int min_size = MAX(min_size_min, min_size_max);
1246 // expand gadget text input size, if maximal value is too large
1247 if (gi->textinput.size < min_size)
1248 gi->textinput.size = min_size;
1251 gi->width = 2 * border_xsize + (gi->textinput.size + 1) * font_width;
1252 gi->height = 2 * border_ysize + font_height;
1255 if (gi->type & GD_TYPE_SELECTBOX)
1257 int font_nr = gi->font_active;
1258 int font_width = getFontWidth(font_nr);
1259 int font_height = getFontHeight(font_nr);
1260 int border_xsize = gi->border.xsize;
1261 int border_ysize = gi->border.ysize;
1262 int button_size = gi->border.xsize_selectbutton;
1263 int bottom_screen_border = gfx.sy + gfx.sysize - font_height;
1267 gi->width = 2 * border_xsize + gi->textinput.size*font_width +button_size;
1268 gi->height = 2 * border_ysize + font_height;
1270 if (gi->selectbox.options == NULL)
1271 Fail("selectbox gadget incomplete (missing options array)");
1273 gi->selectbox.num_values = 0;
1274 while (gi->selectbox.options[gi->selectbox.num_values].text != NULL)
1275 gi->selectbox.num_values++;
1277 // calculate values for open selectbox
1278 gi->selectbox.width = gi->width;
1279 gi->selectbox.height =
1280 2 * border_ysize + gi->selectbox.num_values * font_height;
1282 gi->selectbox.x = gi->x;
1283 gi->selectbox.y = gi->y + gi->height;
1284 if (gi->selectbox.y + gi->selectbox.height > bottom_screen_border)
1285 gi->selectbox.y = gi->y - gi->selectbox.height;
1286 if (gi->selectbox.y < 0)
1287 gi->selectbox.y = bottom_screen_border - gi->selectbox.height;
1289 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &src_bitmap, &src_x, &src_y);
1290 src_x += font_width / 2;
1291 src_y += font_height / 2;
1293 // there may be esoteric cases with missing or too small font bitmap
1294 if (src_bitmap != NULL &&
1295 src_x < src_bitmap->width && src_y < src_bitmap->height)
1296 gi->selectbox.inverse_color = GetPixel(src_bitmap, src_x, src_y);
1298 // always start with closed selectbox
1299 gi->selectbox.open = FALSE;
1302 if (gi->type & GD_TYPE_TEXT_INPUT_NUMERIC)
1304 struct GadgetTextInput *text = &gi->textinput;
1305 int value = text->number_value;
1307 text->number_value = (value < text->number_min ? text->number_min :
1308 value > text->number_max ? text->number_max :
1311 sprintf(text->value, "%d", text->number_value);
1314 if (gi->type & GD_TYPE_TEXT_BUTTON)
1316 int font_nr = gi->font_active;
1317 int font_width = getFontWidth(font_nr);
1318 int font_height = getFontHeight(font_nr);
1319 int border_xsize = gi->border.xsize;
1320 int border_ysize = gi->border.ysize;
1322 gi->width = 2 * border_xsize + gi->textbutton.size * font_width;
1323 gi->height = 2 * border_ysize + font_height;
1326 if (gi->type & GD_TYPE_SCROLLBAR)
1328 struct GadgetScrollbar *gs = &gi->scrollbar;
1329 int scrollbar_size_cmp;
1331 if (gi->width == 0 || gi->height == 0 ||
1332 gs->items_max == 0 || gs->items_visible == 0)
1333 Fail("scrollbar gadget incomplete (missing tags)");
1335 // calculate internal scrollbar values
1336 gs->size_min = (gi->type == GD_TYPE_SCROLLBAR_VERTICAL ?
1337 gi->width : gi->height);
1338 gs->size_max = (gi->type == GD_TYPE_SCROLLBAR_VERTICAL ?
1339 gi->height : gi->width);
1341 scrollbar_size_cmp = gs->size_max * gs->items_visible / gs->items_max;
1342 gs->size = MAX(scrollbar_size_cmp, gs->size_min);
1343 gs->size_max_cmp = (gs->size_max - (gs->size - scrollbar_size_cmp));
1345 gs->position = gs->size_max_cmp * gs->item_position / gs->items_max;
1346 gs->position_max = gs->size_max - gs->size;
1347 gs->correction = gs->size_max / gs->items_max / 2;
1349 // finetuning for maximal right/bottom position
1350 if (gs->item_position == gs->items_max - gs->items_visible)
1351 gs->position = gs->position_max;
1354 if (gi->type & GD_TYPE_TEXT_AREA)
1356 int font_nr = gi->font_active;
1357 int font_width = getFontWidth(font_nr);
1358 int font_height = getFontHeight(font_nr);
1359 int border_xsize = gi->border.xsize;
1360 int border_ysize = gi->border.ysize;
1362 if (gi->width == 0 || gi->height == 0)
1364 gi->width = 2 * border_xsize + gi->textarea.xsize * font_width;
1365 gi->height = 2 * border_ysize + gi->textarea.ysize * font_height;
1369 gi->textarea.xsize = (gi->width - 2 * border_xsize) / font_width;
1370 gi->textarea.ysize = (gi->height - 2 * border_ysize) / font_height;
1375 void ModifyGadget(struct GadgetInfo *gi, int first_tag, ...)
1379 va_start(ap, first_tag);
1380 HandleGadgetTags(gi, first_tag, ap);
1386 void RedrawGadget(struct GadgetInfo *gi)
1388 if (gi == NULL || gi->deactivated)
1392 DrawGadget(gi, gi->state, gi->direct_draw);
1395 struct GadgetInfo *CreateGadget(int first_tag, ...)
1397 struct GadgetInfo *new_gadget = checked_calloc(sizeof(struct GadgetInfo));
1400 // always start with reliable default values
1401 new_gadget->id = getNewGadgetID();
1402 new_gadget->image_id = -1;
1403 new_gadget->callback_info = default_callback_info;
1404 new_gadget->callback_action = default_callback_action;
1405 new_gadget->active = TRUE;
1406 new_gadget->direct_draw = TRUE;
1407 new_gadget->overlay_touch_button = FALSE;
1408 new_gadget->overlay_touch_button_alpha = 0;
1410 new_gadget->next = NULL;
1412 va_start(ap, first_tag);
1413 HandleGadgetTags(new_gadget, first_tag, ap);
1416 // insert new gadget into global gadget list
1417 if (gadget_list_last_entry)
1419 gadget_list_last_entry->next = new_gadget;
1420 gadget_list_last_entry = gadget_list_last_entry->next;
1423 gadget_list_first_entry = gadget_list_last_entry = new_gadget;
1428 void FreeGadget(struct GadgetInfo *gi)
1430 struct GadgetInfo *gi_previous = gadget_list_first_entry;
1435 // prevent "last_info_gi" from pointing to memory that will be freed
1436 if (last_info_gi == gi)
1437 last_info_gi = NULL;
1439 while (gi_previous != NULL && gi_previous->next != gi)
1440 gi_previous = gi_previous->next;
1442 if (gi == gadget_list_first_entry)
1443 gadget_list_first_entry = gi->next;
1445 if (gi == gadget_list_last_entry)
1446 gadget_list_last_entry = gi_previous;
1448 if (gi_previous != NULL)
1449 gi_previous->next = gi->next;
1454 static void CheckRangeOfNumericInputGadget(struct GadgetInfo *gi)
1456 if (gi->type != GD_TYPE_TEXT_INPUT_NUMERIC)
1459 gi->textinput.number_value = atoi(gi->textinput.value);
1461 if (gi->textinput.number_value < gi->textinput.number_min)
1462 gi->textinput.number_value = gi->textinput.number_min;
1463 if (gi->textinput.number_value > gi->textinput.number_max)
1464 gi->textinput.number_value = gi->textinput.number_max;
1466 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
1468 if (gi->textinput.cursor_position < 0)
1469 gi->textinput.cursor_position = 0;
1470 else if (gi->textinput.cursor_position > strlen(gi->textinput.value))
1471 gi->textinput.cursor_position = strlen(gi->textinput.value);
1474 // global pointer to gadget actually in use (when mouse button pressed)
1475 static struct GadgetInfo *last_gi = NULL;
1477 static void MapGadgetExt(struct GadgetInfo *gi, boolean redraw)
1479 if (gi == NULL || gi->deactivated || gi->mapped)
1482 // do not map overlay touch buttons if touch screen is not used
1483 if (gi->overlay_touch_button && !runtime.uses_touch_device)
1489 DrawGadget(gi, DG_UNPRESSED, DG_BUFFERED);
1492 void MapGadget(struct GadgetInfo *gi)
1494 MapGadgetExt(gi, TRUE);
1497 void UnmapGadget(struct GadgetInfo *gi)
1499 if (gi == NULL || gi->deactivated || !gi->mapped)
1508 #define MAX_NUM_GADGETS 1024
1509 #define MULTIMAP_UNMAP (1 << 0)
1510 #define MULTIMAP_REMAP (1 << 1)
1511 #define MULTIMAP_REDRAW (1 << 2)
1512 #define MULTIMAP_PLAYFIELD (1 << 3)
1513 #define MULTIMAP_DOOR_1 (1 << 4)
1514 #define MULTIMAP_DOOR_2 (1 << 5)
1515 #define MULTIMAP_DOOR_3 (1 << 6)
1516 #define MULTIMAP_ALL (MULTIMAP_PLAYFIELD | \
1521 static void MultiMapGadgets(int mode)
1523 struct GadgetInfo *gi = gadget_list_first_entry;
1524 static boolean map_state[MAX_NUM_GADGETS];
1532 if ((mode & MULTIMAP_PLAYFIELD && IN_GFX_FIELD_FULL(x, y)) ||
1533 (mode & MULTIMAP_DOOR_1 && IN_GFX_DOOR_1(x, y)) ||
1534 (mode & MULTIMAP_DOOR_2 && IN_GFX_DOOR_2(x, y)) ||
1535 (mode & MULTIMAP_DOOR_3 && IN_GFX_DOOR_3(x, y)) ||
1536 (mode & MULTIMAP_ALL) == MULTIMAP_ALL)
1538 if (mode & MULTIMAP_UNMAP)
1540 map_state[map_count++ % MAX_NUM_GADGETS] = gi->mapped;
1545 if (map_state[map_count++ % MAX_NUM_GADGETS])
1546 MapGadgetExt(gi, (mode & MULTIMAP_REDRAW));
1554 void UnmapAllGadgets(void)
1556 MultiMapGadgets(MULTIMAP_ALL | MULTIMAP_UNMAP);
1559 void RemapAllGadgets(void)
1561 MultiMapGadgets(MULTIMAP_ALL | MULTIMAP_REMAP);
1564 boolean anyTextInputGadgetActive(void)
1566 return (last_gi && (last_gi->type & GD_TYPE_TEXT_INPUT) && last_gi->mapped);
1569 boolean anyTextAreaGadgetActive(void)
1571 return (last_gi && (last_gi->type & GD_TYPE_TEXT_AREA) && last_gi->mapped);
1574 boolean anySelectboxGadgetActive(void)
1576 return (last_gi && (last_gi->type & GD_TYPE_SELECTBOX) && last_gi->mapped);
1579 boolean anyScrollbarGadgetActive(void)
1581 return (last_gi && (last_gi->type & GD_TYPE_SCROLLBAR) && last_gi->mapped);
1584 boolean anyTextGadgetActive(void)
1586 return (anyTextInputGadgetActive() ||
1587 anyTextAreaGadgetActive() ||
1588 anySelectboxGadgetActive());
1591 static boolean insideSelectboxLine(struct GadgetInfo *gi, int mx, int my)
1593 return (gi != NULL &&
1594 gi->type & GD_TYPE_SELECTBOX &&
1595 mx >= gi->x && mx < gi->x + gi->width &&
1596 my >= gi->y && my < gi->y + gi->height);
1599 static boolean insideSelectboxArea(struct GadgetInfo *gi, int mx, int my)
1601 return (gi != NULL &&
1602 gi->type & GD_TYPE_SELECTBOX &&
1603 mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width &&
1604 my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height);
1607 void ClickOnGadget(struct GadgetInfo *gi, int button)
1609 if (gi == NULL || gi->deactivated || !gi->mapped)
1612 // simulate releasing mouse button over last gadget, if still pressed
1614 HandleGadgets(-1, -1, 0);
1619 // set cursor position to the end of the text for text input gadgets
1620 if (gi->type & GD_TYPE_TEXT_INPUT)
1621 x = gi->x + gi->width - 1;
1623 // simulate pressing mouse button over specified gadget
1624 HandleGadgets(x, y, button);
1626 // simulate releasing mouse button over specified gadget
1627 HandleGadgets(x, y, 0);
1630 boolean HandleGadgets(int mx, int my, int button)
1632 static unsigned int pressed_delay = 0;
1633 static unsigned int pressed_delay_value = GADGET_FRAME_DELAY;
1634 static int last_button = 0;
1635 static int last_mx = 0, last_my = 0;
1636 static int pressed_mx = 0, pressed_my = 0;
1637 static boolean keep_selectbox_open = FALSE;
1638 static boolean gadget_stopped = FALSE;
1639 int scrollbar_mouse_pos = 0;
1640 struct GadgetInfo *new_gi, *gi;
1641 boolean press_event;
1642 boolean release_event;
1643 boolean mouse_moving;
1644 boolean mouse_inside_select_line;
1645 boolean mouse_inside_select_area;
1646 boolean mouse_released_where_pressed;
1647 boolean gadget_pressed;
1648 boolean gadget_pressed_repeated;
1649 boolean gadget_pressed_off_borders;
1650 boolean gadget_pressed_inside_select_line;
1651 boolean gadget_pressed_delay_reached;
1652 boolean gadget_moving;
1653 boolean gadget_moving_inside;
1654 boolean gadget_moving_off_borders;
1655 boolean gadget_draggable;
1656 boolean gadget_dragging;
1657 boolean gadget_released;
1658 boolean gadget_released_inside;
1659 boolean gadget_released_inside_select_area;
1660 boolean gadget_released_off_borders;
1661 boolean changed_position = FALSE;
1663 // check if there are any gadgets defined
1664 if (gadget_list_first_entry == NULL)
1667 // simulated release of mouse button over last gadget
1668 if (mx == -1 && my == -1 && button == 0)
1674 // check which gadget is under the mouse pointer
1675 new_gi = getGadgetInfoFromMousePosition(mx, my, button);
1677 // check if button state has changed since last invocation
1678 press_event = (button != 0 && last_button == 0);
1679 release_event = (button == 0 && last_button != 0);
1680 last_button = button;
1682 // check if mouse has been moved since last invocation
1683 mouse_moving = ((mx != last_mx || my != last_my) && motion_status);
1687 if (press_event && new_gi != last_gi)
1693 mouse_released_where_pressed =
1694 (release_event && mx == pressed_mx && my == pressed_my);
1696 mouse_inside_select_line = insideSelectboxLine(new_gi, mx, my);
1697 mouse_inside_select_area = insideSelectboxArea(new_gi, mx, my);
1699 gadget_pressed_off_borders = (press_event && new_gi != last_gi);
1701 gadget_pressed_inside_select_line =
1702 (press_event && new_gi != NULL &&
1703 new_gi->type & GD_TYPE_SELECTBOX && new_gi->selectbox.open &&
1704 insideSelectboxLine(new_gi, mx, my));
1706 // if mouse button pressed outside text or selectbox gadget, deactivate it
1707 if (anyTextGadgetActive() &&
1708 (gadget_pressed_off_borders ||
1709 (gadget_pressed_inside_select_line && !mouse_inside_select_area)))
1711 struct GadgetInfo *gi = last_gi;
1712 boolean gadget_changed = ((gi->event_mask & GD_EVENT_TEXT_LEAVING) != 0);
1714 // check if text input gadget has changed its value
1715 if (gi->type & GD_TYPE_TEXT_INPUT)
1717 CheckRangeOfNumericInputGadget(gi);
1719 if (!strEqual(gi->textinput.last_value, gi->textinput.value))
1720 strcpy(gi->textinput.last_value, gi->textinput.value);
1722 gadget_changed = FALSE;
1725 // check if text area gadget has changed its value
1726 if (gi->type & GD_TYPE_TEXT_AREA)
1728 if (!strEqual(gi->textarea.last_value, gi->textarea.value))
1729 strcpy(gi->textarea.last_value, gi->textarea.value);
1731 gadget_changed = FALSE;
1734 // selectbox does not change its value when closed by clicking outside
1735 if (gi->type & GD_TYPE_SELECTBOX)
1736 gadget_changed = FALSE;
1738 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1740 gi->event.type = GD_EVENT_TEXT_LEAVING;
1742 if (!(gi->type & GD_TYPE_SELECTBOX))
1743 DoGadgetCallbackAction(gi, gadget_changed);
1747 if (gadget_pressed_inside_select_line)
1754 (button != 0 && last_gi == NULL && new_gi != NULL && press_event);
1755 gadget_pressed_repeated =
1756 (button != 0 && last_gi != NULL && new_gi == last_gi);
1758 gadget_pressed_delay_reached =
1759 DelayReached(&pressed_delay, pressed_delay_value);
1761 gadget_released = (release_event && last_gi != NULL);
1762 gadget_released_inside = (gadget_released && new_gi == last_gi);
1763 gadget_released_off_borders = (gadget_released && new_gi != last_gi);
1765 gadget_moving = (button != 0 && last_gi != NULL && mouse_moving);
1766 gadget_moving_inside = (gadget_moving && new_gi == last_gi);
1767 gadget_moving_off_borders = (gadget_moving && new_gi != last_gi);
1769 // when handling selectbox, set additional state values
1770 if (gadget_released_inside && (last_gi->type & GD_TYPE_SELECTBOX))
1771 gadget_released_inside_select_area = insideSelectboxArea(last_gi, mx, my);
1773 gadget_released_inside_select_area = FALSE;
1775 // setting state for handling over-large selectbox
1776 if (keep_selectbox_open && (press_event || !mouse_inside_select_line))
1777 keep_selectbox_open = FALSE;
1779 // if new gadget pressed, store this gadget
1783 // 'gi' is actually handled gadget
1786 // if gadget is scrollbar, choose mouse position value
1787 if (gi && gi->type & GD_TYPE_SCROLLBAR)
1788 scrollbar_mouse_pos =
1789 (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx - gi->x : my - gi->y);
1791 // if mouse button released, no gadget needs to be handled anymore
1792 if (gadget_released)
1794 if (gi->type & GD_TYPE_SELECTBOX &&
1795 (keep_selectbox_open ||
1796 mouse_released_where_pressed ||
1797 !gadget_released_inside_select_area ||
1798 !CURRENT_OPTION_SELECTABLE(gi))) // selectbox stays open
1800 gi->selectbox.stay_open = TRUE;
1804 else if (!(gi->type & GD_TYPE_TEXT_INPUT ||
1805 gi->type & GD_TYPE_TEXT_AREA)) // text input stays open
1809 // modify event position values even if no gadget is pressed
1810 if (button == 0 && !release_event)
1813 // if new gadget or if no gadget was pressed, release stopped processing
1814 if (gadget_pressed || new_gi == NULL)
1815 gadget_stopped = FALSE;
1817 // if gadget was stopped while being handled, stop gadget processing here
1823 int last_x = gi->event.x;
1824 int last_y = gi->event.y;
1825 int last_mx = gi->event.mx;
1826 int last_my = gi->event.my;
1828 gi->event.x = gi->event.mx = mx - gi->x;
1829 gi->event.y = gi->event.my = my - gi->y;
1831 if (gi->type == GD_TYPE_DRAWING_AREA)
1833 gi->event.x /= gi->drawing.item_xsize;
1834 gi->event.y /= gi->drawing.item_ysize;
1836 if (last_x != gi->event.x || last_y != gi->event.y ||
1837 ((last_mx != gi->event.mx || last_my != gi->event.my) &&
1838 gi->event_mask & GD_EVENT_PIXEL_PRECISE))
1839 changed_position = TRUE;
1841 else if (gi->type & GD_TYPE_TEXT_INPUT && button != 0 && !motion_status)
1843 int old_cursor_position = gi->textinput.cursor_position;
1845 // if mouse button pressed inside activated text gadget, set cursor
1846 gi->textinput.cursor_position =
1847 (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font);
1849 if (gi->textinput.cursor_position < 0)
1850 gi->textinput.cursor_position = 0;
1851 else if (gi->textinput.cursor_position > strlen(gi->textinput.value))
1852 gi->textinput.cursor_position = strlen(gi->textinput.value);
1854 if (gi->textinput.cursor_position != old_cursor_position)
1855 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1858 StartTextInput(gi->x, gi->y, gi->width, gi->height);
1860 else if (gi->type & GD_TYPE_TEXT_AREA && button != 0 && !motion_status)
1862 int old_cursor_position = gi->textarea.cursor_position;
1863 int x = (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font);
1864 int y = (my - gi->y - gi->border.ysize) / getFontHeight(gi->font);
1866 x = (x < 0 ? 0 : x >= gi->textarea.xsize ? gi->textarea.xsize - 1 : x);
1867 y = (y < 0 ? 0 : y >= gi->textarea.ysize ? gi->textarea.ysize - 1 : y);
1869 setTextAreaCursorXY(gi, x, y);
1871 if (gi->textarea.cursor_position != old_cursor_position)
1872 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1875 StartTextInput(gi->x, gi->y, gi->width, gi->height);
1877 else if (gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open &&
1878 !keep_selectbox_open)
1880 int old_index = gi->selectbox.current_index;
1882 // if mouse moving inside activated selectbox, select value
1883 if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
1884 gi->selectbox.current_index =
1885 (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font);
1887 if (gi->selectbox.current_index < 0)
1888 gi->selectbox.current_index = 0;
1889 else if (gi->selectbox.current_index > gi->selectbox.num_values - 1)
1890 gi->selectbox.current_index = gi->selectbox.num_values - 1;
1892 if (gi->selectbox.current_index != old_index)
1893 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1897 // handle gadget popup info text
1898 if (last_info_gi != new_gi ||
1899 (new_gi && new_gi->type == GD_TYPE_DRAWING_AREA && changed_position))
1901 if (new_gi != NULL && (button == 0 || new_gi == last_gi))
1903 new_gi->event.type = GD_EVENT_INFO_ENTERING;
1904 new_gi->callback_info(new_gi);
1906 else if (last_info_gi != NULL && last_info_gi->mapped)
1908 last_info_gi->event.type = GD_EVENT_INFO_LEAVING;
1909 last_info_gi->callback_info(last_info_gi);
1912 last_info_gi = new_gi;
1915 gadget_draggable = (gi && gi->type & GD_TYPE_SCROLLBAR);
1917 // reset drag position for newly pressed scrollbar to "not dragging"
1918 if (gadget_pressed && gadget_draggable)
1919 gi->scrollbar.drag_position = -1;
1921 gadget_dragging = (gadget_draggable && gi->scrollbar.drag_position != -1);
1923 // clicking next to a scrollbar to move it is not considered "moving"
1924 if (gadget_draggable && !gadget_dragging)
1925 gadget_moving = FALSE;
1927 // when leaving scrollbar area when jump-scrolling, stop gadget processing
1928 if (gadget_draggable && !gadget_dragging && gadget_moving_off_borders)
1929 gadget_stopped = TRUE;
1931 if ((gadget_pressed) ||
1932 (gadget_pressed_repeated && gadget_pressed_delay_reached))
1934 if (gadget_pressed) // gadget pressed the first time
1936 // initialize delay counter
1937 ResetDelayCounter(&pressed_delay);
1939 // start gadget delay with longer delay after first click on gadget
1940 pressed_delay_value = GADGET_FRAME_DELAY_FIRST;
1942 else // gadget hold pressed for some time
1944 // after first repeated gadget click, continue with shorter delay value
1945 pressed_delay_value = GADGET_FRAME_DELAY;
1948 if (gi->type & GD_TYPE_SCROLLBAR && !gadget_dragging)
1950 int mpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx : my);
1951 int gpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? gi->x : gi->y);
1952 int slider_start = gpos + gi->scrollbar.position;
1953 int slider_end = gpos + gi->scrollbar.position + gi->scrollbar.size - 1;
1954 boolean inside_slider = (mpos >= slider_start && mpos <= slider_end);
1956 if (IS_WHEEL_BUTTON(button) || !inside_slider)
1958 // click scrollbar one scrollbar length up/left or down/right
1960 struct GadgetScrollbar *gs = &gi->scrollbar;
1961 int old_item_position = gs->item_position;
1962 int item_steps = gs->items_visible - 1;
1963 int item_direction = (mpos < gpos + gi->scrollbar.position ? -1 : +1);
1965 if (IS_WHEEL_BUTTON(button))
1967 boolean scroll_single_step = ((GetKeyModState() & KMOD_Alt) != 0);
1969 item_steps = (scroll_single_step ? 1 : wheel_steps);
1970 item_direction = (button == MB_WHEEL_UP ||
1971 button == MB_WHEEL_LEFT ? -1 : +1);
1974 changed_position = FALSE;
1976 gs->item_position += item_steps * item_direction;
1978 if (gs->item_position < 0)
1979 gs->item_position = 0;
1980 else if (gs->item_position > gs->items_max - gs->items_visible)
1981 gs->item_position = gs->items_max - gs->items_visible;
1983 if (old_item_position != gs->item_position)
1985 gi->event.item_position = gs->item_position;
1986 changed_position = TRUE;
1989 ModifyGadget(gi, GDI_SCROLLBAR_ITEM_POSITION, gs->item_position,
1992 gi->state = GD_BUTTON_UNPRESSED;
1993 gi->event.type = GD_EVENT_MOVING;
1994 gi->event.off_borders = FALSE;
1996 if (gi->event_mask & GD_EVENT_MOVING)
1997 DoGadgetCallbackAction(gi, changed_position);
2003 // don't handle this scrollbar anymore when mouse position reached
2004 if (gadget_pressed_repeated)
2006 gadget_stopped = TRUE;
2016 PlayGadgetSoundActivating();
2018 if (gi->type == GD_TYPE_CHECK_BUTTON)
2020 gi->checked = !gi->checked;
2022 else if (gi->type == GD_TYPE_RADIO_BUTTON)
2024 struct GadgetInfo *rgi = gadget_list_first_entry;
2029 rgi->type == GD_TYPE_RADIO_BUTTON &&
2030 rgi->radio_nr == gi->radio_nr &&
2033 rgi->checked = FALSE;
2034 DrawGadget(rgi, DG_UNPRESSED, rgi->direct_draw);
2042 else if (gi->type & GD_TYPE_SCROLLBAR)
2044 int mpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx : my);
2045 int gpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? gi->x : gi->y);
2046 int slider_start = gpos + gi->scrollbar.position;
2047 int slider_end = gpos + gi->scrollbar.position + gi->scrollbar.size - 1;
2048 boolean inside_slider = (mpos >= slider_start && mpos <= slider_end);
2050 if (!IS_WHEEL_BUTTON(button) && inside_slider)
2052 // start dragging scrollbar
2053 gi->scrollbar.drag_position =
2054 scrollbar_mouse_pos - gi->scrollbar.position;
2057 else if (gi->type & GD_TYPE_SELECTBOX)
2059 // keep selectbox open in case of over-large selectbox
2060 keep_selectbox_open = (mouse_inside_select_line &&
2061 mouse_inside_select_area);
2064 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2066 gi->state = GD_BUTTON_PRESSED;
2067 gi->event.type = GD_EVENT_PRESSED;
2068 gi->event.button = button;
2069 gi->event.off_borders = FALSE;
2071 if (gi->event_mask & GD_EVENT_PRESSED)
2072 gi->callback_action(gi);
2075 if (gadget_pressed_repeated)
2077 gi->event.type = GD_EVENT_PRESSED;
2079 if (gi->event_mask & GD_EVENT_REPEATED && gadget_pressed_delay_reached)
2080 gi->callback_action(gi);
2085 if (gi->type & GD_TYPE_BUTTON)
2087 if (gadget_moving_inside && gi->state == GD_BUTTON_UNPRESSED)
2088 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2089 else if (gadget_moving_off_borders && gi->state == GD_BUTTON_PRESSED)
2090 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2092 else if (gi->type & GD_TYPE_SELECTBOX && !keep_selectbox_open)
2094 int old_index = gi->selectbox.current_index;
2096 // if mouse moving inside activated selectbox, select value
2097 if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
2098 gi->selectbox.current_index =
2099 (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font);
2101 if (gi->selectbox.current_index < 0)
2102 gi->selectbox.current_index = 0;
2103 else if (gi->selectbox.current_index > gi->selectbox.num_values - 1)
2104 gi->selectbox.current_index = gi->selectbox.num_values - 1;
2106 if (gi->selectbox.current_index != old_index)
2107 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2109 else if (gi->type & GD_TYPE_SCROLLBAR)
2111 struct GadgetScrollbar *gs = &gi->scrollbar;
2112 int old_item_position = gs->item_position;
2114 gs->position = scrollbar_mouse_pos - gs->drag_position;
2116 // make sure to always precisely reach end positions when dragging
2117 if (gs->position <= 0)
2120 gs->item_position = 0;
2122 else if (gs->position >= gs->position_max)
2124 gs->position = gs->position_max;
2125 gs->item_position = gs->items_max - gs->items_visible;
2130 gs->items_max * (gs->position + gs->correction) / gs->size_max_cmp;
2133 if (gs->item_position < 0)
2134 gs->item_position = 0;
2135 if (gs->item_position > gs->items_max - 1)
2136 gs->item_position = gs->items_max - 1;
2138 if (old_item_position != gs->item_position)
2140 gi->event.item_position = gs->item_position;
2141 changed_position = TRUE;
2144 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2147 gi->state = (gadget_moving_inside || gadget_draggable ?
2148 GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
2149 gi->event.type = GD_EVENT_MOVING;
2150 gi->event.off_borders = gadget_moving_off_borders;
2152 if (gi->event_mask & GD_EVENT_MOVING &&
2153 (gadget_moving_inside || gi->event_mask & GD_EVENT_OFF_BORDERS))
2154 DoGadgetCallbackAction(gi, changed_position);
2157 if (gadget_released_inside)
2159 boolean deactivate_gadget = TRUE;
2160 boolean gadget_changed = TRUE;
2162 if (gi->type & GD_TYPE_SELECTBOX)
2164 if (keep_selectbox_open ||
2165 mouse_released_where_pressed ||
2166 !gadget_released_inside_select_area ||
2167 !CURRENT_OPTION_SELECTABLE(gi)) // selectbox stays open
2169 deactivate_gadget = FALSE;
2170 gadget_changed = FALSE;
2172 else if (gi->selectbox.index != gi->selectbox.current_index)
2173 gi->selectbox.index = gi->selectbox.current_index;
2175 gadget_changed = FALSE;
2178 if (deactivate_gadget &&
2179 !(gi->type & GD_TYPE_TEXT_INPUT ||
2180 gi->type & GD_TYPE_TEXT_AREA)) // text input stays open
2181 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2183 gi->state = GD_BUTTON_UNPRESSED;
2184 gi->event.type = GD_EVENT_RELEASED;
2186 if ((gi->event_mask & GD_EVENT_RELEASED))
2187 DoGadgetCallbackAction(gi, gadget_changed);
2190 if (gadget_released_off_borders)
2192 if (gi->type & GD_TYPE_SCROLLBAR)
2193 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2195 gi->state = GD_BUTTON_UNPRESSED;
2196 gi->event.type = GD_EVENT_RELEASED;
2198 if (gi->event_mask & GD_EVENT_RELEASED &&
2199 gi->event_mask & GD_EVENT_OFF_BORDERS)
2200 gi->callback_action(gi);
2203 // handle gadgets unmapped/mapped between pressing and releasing
2204 if (release_event && !gadget_released && new_gi)
2205 new_gi->state = GD_BUTTON_UNPRESSED;
2207 return (gadget_pressed || gadget_pressed_repeated ||
2208 gadget_released || gadget_moving);
2211 static void insertCharIntoTextArea(struct GadgetInfo *gi, char c)
2213 char text[MAX_GADGET_TEXTSIZE + 1];
2214 int cursor_position = gi->textarea.cursor_position;
2216 if (strlen(gi->textarea.value) >= MAX_GADGET_TEXTSIZE) // no space left
2219 strcpy(text, gi->textarea.value);
2220 strcpy(&gi->textarea.value[cursor_position + 1], &text[cursor_position]);
2221 gi->textarea.value[cursor_position] = c;
2223 setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1);
2226 boolean HandleGadgetsKeyInput(Key key)
2228 struct GadgetInfo *gi = last_gi;
2230 if (gi == NULL || gi->deactivated || !gi->mapped ||
2231 !(gi->type & GD_TYPE_TEXT_INPUT ||
2232 gi->type & GD_TYPE_TEXT_AREA ||
2233 gi->type & GD_TYPE_SELECTBOX))
2236 if (key == KSYM_Escape)
2238 if (anyTextGadgetActive())
2240 boolean gadget_changed = ((gi->event_mask & GD_EVENT_TEXT_LEAVING) != 0);
2242 // restore previous text (before activating text gadget)
2243 if (gi->type & GD_TYPE_TEXT_INPUT)
2245 strcpy(gi->textinput.value, gi->textinput.last_value);
2247 CheckRangeOfNumericInputGadget(gi);
2250 // store current text for text area gadgets when pressing "Escape" key
2251 if (gi->type & GD_TYPE_TEXT_AREA)
2253 if (!strEqual(gi->textarea.last_value, gi->textarea.value))
2254 strcpy(gi->textarea.last_value, gi->textarea.value);
2256 gadget_changed = FALSE;
2259 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2261 if (gi->type & GD_TYPE_TEXT_AREA)
2263 gi->event.type = GD_EVENT_TEXT_LEAVING;
2265 DoGadgetCallbackAction(gi, gadget_changed);
2273 else if (key == KSYM_Return) // valid for both text input and selectbox
2275 boolean gadget_changed = ((gi->event_mask & GD_EVENT_TEXT_RETURN) != 0);
2277 if (gi->type & GD_TYPE_TEXT_INPUT)
2279 CheckRangeOfNumericInputGadget(gi);
2281 if (!strEqual(gi->textinput.last_value, gi->textinput.value))
2282 strcpy(gi->textinput.last_value, gi->textinput.value);
2284 gadget_changed = FALSE;
2288 else if (gi->type & GD_TYPE_SELECTBOX)
2290 if (gi->selectbox.index != gi->selectbox.current_index)
2291 gi->selectbox.index = gi->selectbox.current_index;
2293 gadget_changed = FALSE;
2296 if (gi->type & GD_TYPE_TEXT_AREA)
2298 insertCharIntoTextArea(gi, '\n');
2300 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2304 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2306 gi->event.type = GD_EVENT_TEXT_RETURN;
2311 DoGadgetCallbackAction(gi, gadget_changed);
2313 else if (gi->type & GD_TYPE_TEXT_INPUT) // only valid for text input
2315 char text[MAX_GADGET_TEXTSIZE + 1];
2316 int text_length = strlen(gi->textinput.value);
2317 int cursor_pos = gi->textinput.cursor_position;
2318 char letter = getCharFromKey(key);
2319 boolean legal_letter = (gi->type == GD_TYPE_TEXT_INPUT_NUMERIC ?
2320 letter >= '0' && letter <= '9' :
2323 if (legal_letter && text_length < gi->textinput.size)
2325 strcpy(text, gi->textinput.value);
2326 strcpy(&gi->textinput.value[cursor_pos + 1], &text[cursor_pos]);
2327 gi->textinput.value[cursor_pos] = letter;
2328 gi->textinput.cursor_position++;
2330 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2332 else if (key == KSYM_Left && cursor_pos > 0)
2334 gi->textinput.cursor_position--;
2336 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2338 else if (key == KSYM_Right && cursor_pos < text_length)
2340 gi->textinput.cursor_position++;
2342 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2344 else if (key == KSYM_BackSpace && cursor_pos > 0)
2346 strcpy(text, gi->textinput.value);
2347 strcpy(&gi->textinput.value[cursor_pos - 1], &text[cursor_pos]);
2348 gi->textinput.cursor_position--;
2350 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2352 else if (key == KSYM_Delete && cursor_pos < text_length)
2354 strcpy(text, gi->textinput.value);
2355 strcpy(&gi->textinput.value[cursor_pos], &text[cursor_pos + 1]);
2357 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2360 else if (gi->type & GD_TYPE_TEXT_AREA) // only valid for text area
2362 char text[MAX_GADGET_TEXTSIZE + 1];
2363 int text_length = strlen(gi->textarea.value);
2364 int area_ysize = gi->textarea.ysize;
2365 int cursor_x_pref = gi->textarea.cursor_x_preferred;
2366 int cursor_y = gi->textarea.cursor_y;
2367 int cursor_pos = gi->textarea.cursor_position;
2368 char letter = getCharFromKey(key);
2369 boolean legal_letter = (letter != 0);
2373 insertCharIntoTextArea(gi, letter);
2375 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2377 else if (key == KSYM_Left && cursor_pos > 0)
2379 setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1);
2381 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2383 else if (key == KSYM_Right && cursor_pos < text_length)
2385 setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1);
2387 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2389 else if (key == KSYM_Up && cursor_y > 0)
2391 setTextAreaCursorXY(gi, cursor_x_pref, cursor_y - 1);
2392 gi->textarea.cursor_x_preferred = cursor_x_pref;
2394 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2396 else if (key == KSYM_Down && cursor_y < area_ysize - 1)
2398 setTextAreaCursorXY(gi, cursor_x_pref, cursor_y + 1);
2399 gi->textarea.cursor_x_preferred = cursor_x_pref;
2401 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2403 else if (key == KSYM_BackSpace && cursor_pos > 0)
2405 strcpy(text, gi->textarea.value);
2406 strcpy(&gi->textarea.value[cursor_pos - 1], &text[cursor_pos]);
2408 setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1);
2410 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2412 else if (key == KSYM_Delete && cursor_pos < text_length)
2414 strcpy(text, gi->textarea.value);
2415 strcpy(&gi->textarea.value[cursor_pos], &text[cursor_pos + 1]);
2417 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2420 else if (gi->type & GD_TYPE_SELECTBOX) // only valid for selectbox
2422 int index = gi->selectbox.current_index;
2423 int num_values = gi->selectbox.num_values;
2425 if (key == KSYM_Up && index > 0)
2427 gi->selectbox.current_index--;
2429 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2431 else if (key == KSYM_Down && index < num_values - 1)
2433 gi->selectbox.current_index++;
2435 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2442 void DumpGadgetIdentifiers(void)
2444 struct GadgetInfo *gi;
2446 Print("Gadgets on current screen:\n");
2448 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
2450 if (gi->mapped && gi->image_id != -1)
2452 char *token = getTokenFromImageID(gi->image_id);
2453 char *prefix = "gfx.";
2455 if (strPrefix(token, prefix))
2456 token = &token[strlen(prefix)];
2458 Print("- '%s'\n", token);
2465 boolean DoGadgetAction(int image_id)
2467 struct GadgetInfo *gi;
2469 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
2471 if (gi->mapped && gi->image_id == image_id)
2473 gi->callback_action(gi);