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;
41 static int gadget_screen_border_right = -1;
42 static int gadget_screen_border_bottom = -1;
44 static void (*PlayGadgetSoundActivating)(void) = NULL;
45 static void (*PlayGadgetSoundSelecting)(void) = NULL;
48 void InitGadgetsSoundCallback(void (*activating_function)(void),
49 void (*selecting_function)(void))
51 PlayGadgetSoundActivating = activating_function;
52 PlayGadgetSoundSelecting = selecting_function;
55 void InitGadgetScreenBorders(int border_right, int border_bottom)
57 gadget_screen_border_right = border_right;
58 gadget_screen_border_bottom = border_bottom;
61 static int getGadgetScreenBorderRight(void)
63 if (gadget_screen_border_right < gfx.sx ||
64 gadget_screen_border_right > gfx.sx + gfx.sxsize)
65 return gfx.sx + gfx.sxsize;
67 return gadget_screen_border_right;
70 static int getGadgetScreenBorderBottom(void)
72 if (gadget_screen_border_bottom < gfx.sy ||
73 gadget_screen_border_bottom > gfx.sy + gfx.sysize)
74 return gfx.sy + gfx.sysize;
76 return gadget_screen_border_bottom;
79 static struct GadgetInfo *getGadgetInfoFromGadgetID(int id)
81 struct GadgetInfo *gi = gadget_list_first_entry;
83 while (gi && gi->id != id)
89 static int getNewGadgetID(void)
91 int id = next_free_gadget_id++;
93 if (next_free_gadget_id <= 0) // counter overrun
95 gadget_id_wrapped = TRUE; // now we must check each ID
96 next_free_gadget_id = 0;
99 if (gadget_id_wrapped)
101 next_free_gadget_id++;
102 while (getGadgetInfoFromGadgetID(next_free_gadget_id) != NULL)
103 next_free_gadget_id++;
106 if (next_free_gadget_id <= 0) // cannot get new gadget id
107 Fail("too much gadgets -- this should not happen");
112 static struct GadgetInfo *getGadgetInfoFromMousePosition(int mx, int my,
115 struct GadgetInfo *gi;
117 // first check for scrollbars in case of mouse scroll wheel button events
118 if (IS_WHEEL_BUTTON(button))
120 // real horizontal wheel or vertical wheel with modifier key pressed
121 boolean check_horizontal = (IS_WHEEL_BUTTON_HORIZONTAL(button) ||
122 GetKeyModState() & KMOD_Shift);
124 // check for the first active scrollbar directly under the mouse pointer
125 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
127 if (gi->mapped && gi->active &&
128 (gi->type & GD_TYPE_SCROLLBAR) &&
129 mx >= gi->x && mx < gi->x + gi->width &&
130 my >= gi->y && my < gi->y + gi->height)
134 // check for the first active scrollbar with matching mouse wheel area
135 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
137 if (gi->mapped && gi->active &&
138 ((gi->type & GD_TYPE_SCROLLBAR_HORIZONTAL && check_horizontal) ||
139 (gi->type & GD_TYPE_SCROLLBAR_VERTICAL && !check_horizontal)) &&
140 mx >= gi->wheelarea.x && mx < gi->wheelarea.x + gi->wheelarea.width &&
141 my >= gi->wheelarea.y && my < gi->wheelarea.y + gi->wheelarea.height)
145 // no active scrollbar found -- ignore this scroll wheel button event
149 // open selectboxes may overlap other active gadgets, so check them first
150 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
152 if (gi->mapped && gi->active &&
153 gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open &&
154 mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width &&
155 my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
159 // full text areas may overlap other active gadgets, so check them first
160 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
162 if (gi->mapped && gi->active &&
163 gi->type & GD_TYPE_TEXT_AREA && gi->textarea.full_open &&
164 mx >= gi->textarea.full_x && mx < gi->textarea.full_x + gi->width &&
165 my >= gi->textarea.full_y && my < gi->textarea.full_y + gi->height)
169 // check all other gadgets
170 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
172 if (gi->mapped && gi->active &&
173 mx >= gi->x && mx < gi->x + gi->width &&
174 my >= gi->y && my < gi->y + gi->height)
181 static void setTextAreaCursorExt(struct GadgetInfo *gi, boolean set_cursor_pos)
183 char *text = gi->textarea.value;
184 int area_xsize = gi->textarea.xsize;
185 int area_ysize = gi->textarea.ysize;
186 int cursor_position = gi->textarea.cursor_position;
187 int cursor_x = gi->textarea.cursor_x;
188 int cursor_y = gi->textarea.cursor_y;
195 if (set_cursor_pos) // x/y => position
197 if (y == cursor_y && (x == cursor_x || (x < cursor_x && *text == '\n')))
200 else // position => x/y
202 if (pos == cursor_position)
206 if (x + 1 >= area_xsize || *text == '\n')
208 if (y + 1 >= area_ysize)
221 gi->textarea.cursor_x = x;
222 gi->textarea.cursor_y = y;
223 gi->textarea.cursor_x_preferred = x;
224 gi->textarea.cursor_position = pos;
227 static void setTextAreaCursorXY(struct GadgetInfo *gi, int x, int y)
229 gi->textarea.cursor_x = x;
230 gi->textarea.cursor_y = y;
232 setTextAreaCursorExt(gi, TRUE);
235 static void setTextAreaCursorPosition(struct GadgetInfo *gi, int pos)
237 gi->textarea.cursor_position = pos;
239 setTextAreaCursorExt(gi, FALSE);
242 static void default_callback_info(void *ptr)
247 static void default_callback_action(void *ptr)
252 static void DoGadgetCallbackAction(struct GadgetInfo *gi, boolean changed)
254 if (changed || gi->callback_action_always)
255 gi->callback_action(gi);
258 static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct)
260 struct GadgetDesign *gd;
261 int state = (pressed ? GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
262 boolean redraw_selectbox = FALSE;
264 if (gi == NULL || gi->deactivated)
267 if (gi->overlay_touch_button) // will be drawn later (as texture)
270 gd = (!gi->active ? &gi->alt_design[state] :
271 gi->checked ? &gi->alt_design[state] : &gi->design[state]);
275 case GD_TYPE_NORMAL_BUTTON:
276 case GD_TYPE_CHECK_BUTTON:
277 case GD_TYPE_CHECK_BUTTON_2:
278 case GD_TYPE_RADIO_BUTTON:
280 BlitBitmapOnBackground(gd->bitmap, drawto,
281 gd->x, gd->y, gi->width, gi->height,
284 if (gi->deco.design.bitmap)
286 // make sure that decoration does not overlap gadget border
287 int deco_x = gi->deco.x + (pressed ? gi->deco.xshift : 0);
288 int deco_y = gi->deco.y + (pressed ? gi->deco.yshift : 0);
289 int deco_width = MIN(gi->deco.width, gi->width - deco_x);
290 int deco_height = MIN(gi->deco.height, gi->height - deco_y);
293 BlitBitmapMasked(gi->deco.design.bitmap, drawto,
294 gi->deco.design.x, gi->deco.design.y,
295 deco_width, deco_height,
296 gi->x + deco_x, gi->y + deco_y);
298 BlitBitmap(gi->deco.design.bitmap, drawto,
299 gi->deco.design.x, gi->deco.design.y,
300 deco_width, deco_height,
301 gi->x + deco_x, gi->y + deco_y);
306 case GD_TYPE_TEXT_BUTTON:
309 int font_nr = (gi->active ? gi->font_active : gi->font);
310 int font_width = getFontWidth(font_nr);
311 int border_x = gi->border.xsize;
312 int border_y = gi->border.ysize;
313 int text_size = strlen(gi->textbutton.value);
314 int text_start = (gi->width - text_size * font_width) / 2;
316 // left part of gadget
317 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
318 border_x, gi->height, gi->x, gi->y);
320 // middle part of gadget
321 for (i = 0; i < gi->textbutton.size; i++)
322 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
323 font_width, gi->height,
324 gi->x + border_x + i * font_width, gi->y);
326 // right part of gadget
327 BlitBitmapOnBackground(gd->bitmap, drawto,
328 gd->x + gi->border.width - border_x, gd->y,
329 border_x, gi->height,
330 gi->x + gi->width - border_x, gi->y);
334 gi->x + text_start + (pressed ? gi->deco.xshift : 0),
335 gi->y + border_y + (pressed ? gi->deco.yshift : 0),
336 gi->textbutton.value, font_nr, BLIT_MASKED);
340 case GD_TYPE_TEXT_INPUT_ALPHANUMERIC:
341 case GD_TYPE_TEXT_INPUT_NUMERIC:
345 char cursor_string[2];
346 char text[MAX_GADGET_TEXTSIZE + 1];
347 int font_nr = (pressed ? gi->font_active : gi->font);
348 int font_width = getFontWidth(font_nr);
349 int font_height = getFontHeight(font_nr);
350 struct FontBitmapInfo *font = getFontBitmapInfo(font_nr);
351 int border_x = gi->border.xsize;
352 int border_y = gi->border.ysize;
353 int text_x = gi->x + font->draw_xoffset;
354 int text_y = gi->y + font->draw_yoffset;
356 // left part of gadget
357 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
358 border_x, gi->height, gi->x, gi->y);
360 // middle part of gadget
361 for (i = 0; i < gi->textinput.size + 1; i++)
362 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
363 font_width, gi->height,
364 gi->x + border_x + i * font_width, gi->y);
366 // right part of gadget
367 BlitBitmapOnBackground(gd->bitmap, drawto,
368 gd->x + gi->border.width - border_x, gd->y,
369 border_x, gi->height,
370 gi->x + gi->width - border_x, gi->y);
373 strcpy(text, gi->textinput.value);
376 // dirty workaround to erase text if input gadget font has draw offset
377 if (font->draw_xoffset != 0 || font->draw_yoffset != 0)
378 for (i = 0; i < gi->textinput.size + 1; i++)
379 BlitBitmapOnBackground(gd->bitmap, drawto,
380 gd->x + border_x, gd->y + border_y,
381 font_width, font_height,
382 text_x + border_x + i * font_width,
387 gi->x + border_x, gi->y + border_y, text,
388 font_nr, BLIT_MASKED);
390 cursor_letter = gi->textinput.value[gi->textinput.cursor_position];
391 cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' ');
392 cursor_string[1] = '\0';
394 // draw cursor, if active
398 gi->textinput.cursor_position * font_width,
399 gi->y + border_y, cursor_string,
400 font_nr, BLIT_INVERSE);
404 case GD_TYPE_TEXT_AREA:
408 char cursor_string[2];
409 int font_nr = (pressed ? gi->font_active : gi->font);
410 int font_width = getFontWidth(font_nr);
411 int font_height = getFontHeight(font_nr);
412 int border_x = gi->border.xsize;
413 int border_y = gi->border.ysize;
414 int gd_height = 2 * border_y + font_height;
417 int width = gi->width;
418 int height = gi->height;
419 int xsize = gi->textarea.xsize;
420 int ysize = gi->textarea.ysize;
422 if (gi->textarea.cropped)
426 x = gi->textarea.full_x;
427 y = gi->textarea.full_y;
429 if (!gi->textarea.full_open)
431 gi->textarea.full_open = TRUE;
433 // save background under fully opened text area
434 BlitBitmap(drawto, gfx.field_save_buffer,
435 gi->textarea.full_x, gi->textarea.full_y,
436 gi->width, gi->height,
437 gi->textarea.full_x, gi->textarea.full_y);
442 width = gi->textarea.crop_width;
443 height = gi->textarea.crop_height;
444 xsize = gi->textarea.crop_xsize;
445 ysize = gi->textarea.crop_ysize;
447 if (gi->textarea.full_open)
449 gi->textarea.full_open = FALSE;
451 // restore background under fully opened text area
452 BlitBitmap(gfx.field_save_buffer, drawto,
453 gi->textarea.full_x, gi->textarea.full_y,
454 gi->width, gi->height,
455 gi->textarea.full_x, gi->textarea.full_y);
460 // top left part of gadget border
461 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
462 border_x, border_y, x, y);
464 // top middle part of gadget border
465 for (i = 0; i < xsize; i++)
466 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
467 font_width, border_y,
468 x + border_x + i * font_width, y);
470 // top right part of gadget border
471 BlitBitmapOnBackground(gd->bitmap, drawto,
472 gd->x + gi->border.width - border_x, gd->y,
474 x + width - border_x, y);
476 // left and right part of gadget border for each row
477 for (i = 0; i < ysize; i++)
479 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y,
480 border_x, font_height,
481 x, y + border_y + i * font_height);
482 BlitBitmapOnBackground(gd->bitmap, drawto,
483 gd->x + gi->border.width - border_x,
485 border_x, font_height,
486 x + width - border_x,
487 y + border_y + i * font_height);
490 // bottom left part of gadget border
491 BlitBitmapOnBackground(gd->bitmap, drawto,
492 gd->x, gd->y + gd_height - border_y,
494 x, y + height - border_y);
496 // bottom middle part of gadget border
497 for (i = 0; i < xsize; i++)
498 BlitBitmapOnBackground(gd->bitmap, drawto,
500 gd->y + gd_height - border_y,
501 font_width, border_y,
502 x + border_x + i * font_width,
503 y + height - border_y);
505 // bottom right part of gadget border
506 BlitBitmapOnBackground(gd->bitmap, drawto,
507 gd->x + gi->border.width - border_x,
508 gd->y + gd_height - border_y,
510 x + width - border_x,
511 y + height - border_y);
513 ClearRectangleOnBackground(drawto,
516 width - 2 * border_x,
517 height - 2 * border_y);
520 DrawTextArea(x + border_x, y + border_y, gi->textarea.value,
521 font_nr, xsize, -1, ysize, 0,
522 BLIT_ON_BACKGROUND, FALSE, FALSE, FALSE);
524 cursor_letter = gi->textarea.value[gi->textarea.cursor_position];
525 cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' ');
526 cursor_string[1] = '\0';
528 // draw cursor, if active
531 x + border_x + gi->textarea.cursor_x * font_width,
532 y + border_y + gi->textarea.cursor_y * font_height,
534 font_nr, BLIT_INVERSE);
538 case GD_TYPE_SELECTBOX:
541 char text[MAX_GADGET_TEXTSIZE + 1];
542 int font_nr_default = (pressed ? gi->font_active : gi->font);
543 int font_nr = font_nr_default;
544 int font_width = getFontWidth(font_nr);
545 int font_height = getFontHeight(font_nr);
546 int border_x = gi->border.xsize;
547 int border_y = gi->border.ysize;
548 int button = gi->border.xsize_selectbutton;
549 int width_inner = gi->border.width - button - 2 * border_x;
550 int box_width = gi->selectbox.width;
551 int box_height = gi->selectbox.height;
553 // left part of gadget
554 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
555 border_x, gi->height, gi->x, gi->y);
557 // middle part of gadget
558 for (i = 0; i < gi->selectbox.size; i++)
559 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
560 font_width, gi->height,
561 gi->x + border_x + i * font_width, gi->y);
563 // button part of gadget
564 BlitBitmapOnBackground(gd->bitmap, drawto,
565 gd->x + border_x + width_inner, gd->y,
567 gi->x + gi->width - border_x - button, gi->y);
569 // right part of gadget
570 BlitBitmapOnBackground(gd->bitmap, drawto,
571 gd->x + gi->border.width - border_x, gd->y,
572 border_x, gi->height,
573 gi->x + gi->width - border_x, gi->y);
576 strncpy(text, gi->selectbox.options[gi->selectbox.index].text,
578 text[gi->selectbox.size] = '\0';
581 font_nr = (OPTION_TEXT_SELECTABLE(gi, text) ? font_nr_default :
582 gi->font_unselectable);
585 DrawTextExt(drawto, gi->x + border_x, gi->y + border_y, text,
586 font_nr, BLIT_MASKED);
590 if (!gi->selectbox.open)
592 gi->selectbox.open = TRUE;
593 gi->selectbox.stay_open = FALSE;
594 gi->selectbox.current_index = gi->selectbox.index;
596 // save background under selectbox
597 BlitBitmap(drawto, gfx.field_save_buffer,
598 gi->selectbox.x, gi->selectbox.y,
599 gi->selectbox.width, gi->selectbox.height,
600 gi->selectbox.x, gi->selectbox.y);
603 // draw open selectbox
605 // top left part of gadget border
606 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
608 gi->selectbox.x, gi->selectbox.y);
610 // top middle part of gadget border
611 for (i = 0; i < gi->selectbox.size; i++)
612 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
613 font_width, border_y,
614 gi->selectbox.x + border_x + i * font_width,
617 // top button part of gadget border
618 BlitBitmapOnBackground(gd->bitmap, drawto,
619 gd->x + border_x + width_inner, gd->y,
621 gi->selectbox.x + box_width -border_x -button,
624 // top right part of gadget border
625 BlitBitmapOnBackground(gd->bitmap, drawto,
626 gd->x + gi->border.width - border_x, gd->y,
628 gi->selectbox.x + box_width - border_x,
631 // left and right part of gadget border for each row
632 for (i = 0; i < gi->selectbox.num_values; i++)
634 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y,
635 border_x, font_height,
637 gi->selectbox.y + border_y + i * font_height);
638 BlitBitmapOnBackground(gd->bitmap, drawto,
639 gd->x + gi->border.width - border_x,
641 border_x, font_height,
642 gi->selectbox.x + box_width - border_x,
643 gi->selectbox.y + border_y + i * font_height);
646 // bottom left part of gadget border
647 BlitBitmapOnBackground(gd->bitmap, drawto,
648 gd->x, gd->y + gi->height - border_y,
651 gi->selectbox.y + box_height - border_y);
653 // bottom middle part of gadget border
654 for (i = 0; i < gi->selectbox.size; i++)
655 BlitBitmapOnBackground(gd->bitmap, drawto,
657 gd->y + gi->height - border_y,
658 font_width, border_y,
659 gi->selectbox.x + border_x + i * font_width,
660 gi->selectbox.y + box_height - border_y);
662 // bottom button part of gadget border
663 BlitBitmapOnBackground(gd->bitmap, drawto,
664 gd->x + border_x + width_inner,
665 gd->y + gi->height - border_y,
667 gi->selectbox.x + box_width -border_x -button,
668 gi->selectbox.y + box_height - border_y);
670 // bottom right part of gadget border
671 BlitBitmapOnBackground(gd->bitmap, drawto,
672 gd->x + gi->border.width - border_x,
673 gd->y + gi->height - border_y,
675 gi->selectbox.x + box_width - border_x,
676 gi->selectbox.y + box_height - border_y);
678 ClearRectangleOnBackground(drawto,
679 gi->selectbox.x + border_x,
680 gi->selectbox.y + border_y,
681 gi->selectbox.width - 2 * border_x,
682 gi->selectbox.height - 2 * border_y);
684 // selectbox text values
685 for (i = 0; i < gi->selectbox.num_values; i++)
687 int mask_mode = BLIT_MASKED;
689 strncpy(text, gi->selectbox.options[i].text, gi->selectbox.size);
690 text[gi->selectbox.size] = '\0';
692 font_nr = (OPTION_TEXT_SELECTABLE(gi, text) ? font_nr_default :
693 gi->font_unselectable);
695 if (i == gi->selectbox.current_index &&
696 OPTION_TEXT_SELECTABLE(gi, text))
698 FillRectangle(drawto,
699 gi->selectbox.x + border_x,
700 gi->selectbox.y + border_y + i * font_height,
701 gi->selectbox.width - 2 * border_x, font_height,
702 gi->selectbox.inverse_color);
704 // prevent use of cursor graphic by drawing at least two chars
706 text[gi->selectbox.size] = '\0';
708 mask_mode = BLIT_INVERSE;
712 gi->selectbox.x + border_x,
713 gi->selectbox.y + border_y + i * font_height, text,
717 redraw_selectbox = TRUE;
719 else if (gi->selectbox.open)
721 gi->selectbox.open = FALSE;
723 // restore background under selectbox
724 BlitBitmap(gfx.field_save_buffer, drawto,
725 gi->selectbox.x, gi->selectbox.y,
726 gi->selectbox.width, gi->selectbox.height,
727 gi->selectbox.x, gi->selectbox.y);
729 // redraw closed selectbox
730 DrawGadget(gi, FALSE, FALSE);
732 redraw_selectbox = TRUE;
737 case GD_TYPE_SCROLLBAR_VERTICAL:
741 int ypos = gi->y + gi->scrollbar.position;
742 int design_full = gi->width;
743 int design_body = design_full - 2 * gi->border.ysize;
744 int size_full = gi->scrollbar.size;
745 int size_body = size_full - 2 * gi->border.ysize;
746 int num_steps = size_body / design_body;
747 int step_size_remain = size_body - num_steps * design_body;
749 // clear scrollbar area
750 ClearRectangleOnBackground(backbuffer, gi->x, gi->y,
751 gi->width, gi->height);
753 // upper part of gadget
754 BlitBitmapOnBackground(gd->bitmap, drawto,
756 gi->width, gi->border.ysize,
759 // middle part of gadget
760 for (i = 0; i < num_steps; i++)
761 BlitBitmapOnBackground(gd->bitmap, drawto,
762 gd->x, gd->y + gi->border.ysize,
763 gi->width, design_body,
765 ypos + gi->border.ysize + i * design_body);
767 // remaining middle part of gadget
768 if (step_size_remain > 0)
769 BlitBitmapOnBackground(gd->bitmap, drawto,
770 gd->x, gd->y + gi->border.ysize,
771 gi->width, step_size_remain,
773 ypos + gi->border.ysize
774 + num_steps * design_body);
776 // lower part of gadget
777 BlitBitmapOnBackground(gd->bitmap, drawto,
778 gd->x, gd->y + design_full - gi->border.ysize,
779 gi->width, gi->border.ysize,
780 xpos, ypos + size_full - gi->border.ysize);
784 case GD_TYPE_SCROLLBAR_HORIZONTAL:
787 int xpos = gi->x + gi->scrollbar.position;
789 int design_full = gi->height;
790 int design_body = design_full - 2 * gi->border.xsize;
791 int size_full = gi->scrollbar.size;
792 int size_body = size_full - 2 * gi->border.xsize;
793 int num_steps = size_body / design_body;
794 int step_size_remain = size_body - num_steps * design_body;
796 // clear scrollbar area
797 ClearRectangleOnBackground(backbuffer, gi->x, gi->y,
798 gi->width, gi->height);
800 // left part of gadget
801 BlitBitmapOnBackground(gd->bitmap, drawto,
803 gi->border.xsize, gi->height,
806 // middle part of gadget
807 for (i = 0; i < num_steps; i++)
808 BlitBitmapOnBackground(gd->bitmap, drawto,
809 gd->x + gi->border.xsize, gd->y,
810 design_body, gi->height,
811 xpos + gi->border.xsize + i * design_body,
814 // remaining middle part of gadget
815 if (step_size_remain > 0)
816 BlitBitmapOnBackground(gd->bitmap, drawto,
817 gd->x + gi->border.xsize, gd->y,
818 step_size_remain, gi->height,
819 xpos + gi->border.xsize
820 + num_steps * design_body,
823 // right part of gadget
824 BlitBitmapOnBackground(gd->bitmap, drawto,
825 gd->x + design_full - gi->border.xsize, gd->y,
826 gi->border.xsize, gi->height,
827 xpos + size_full - gi->border.xsize, ypos);
835 // do not use direct gadget drawing anymore; this worked as a speed-up once,
836 // but would slow things down a lot now the screen is always fully redrawn
841 BlitBitmap(drawto, window,
842 gi->x, gi->y, gi->width, gi->height, gi->x, gi->y);
844 if (gi->type == GD_TYPE_SELECTBOX && redraw_selectbox)
845 BlitBitmap(drawto, window,
846 gi->selectbox.x, gi->selectbox.y,
847 gi->selectbox.width, gi->selectbox.height,
848 gi->selectbox.x, gi->selectbox.y);
855 redraw_mask |= (IN_GFX_FIELD_FULL(x, y) ? REDRAW_FIELD :
856 IN_GFX_DOOR_1(x, y) ? REDRAW_DOOR_1 :
857 IN_GFX_DOOR_2(x, y) ? REDRAW_DOOR_2 :
858 IN_GFX_DOOR_3(x, y) ? REDRAW_DOOR_3 : REDRAW_ALL);
862 static void SetGadgetPosition_OverlayTouchButton(struct GadgetInfo *gi)
868 gi->x += video.screen_width;
870 gi->y += video.screen_height;
872 gi->x -= video.screen_xoffset;
873 gi->y -= video.screen_yoffset;
876 void SetGadgetsPosition_OverlayTouchButtons(void)
878 struct GadgetInfo *gi;
880 if (gadget_list_first_entry == NULL)
883 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
884 if (gi->overlay_touch_button)
885 SetGadgetPosition_OverlayTouchButton(gi);
888 static void DrawGadget_OverlayTouchButton(struct GadgetInfo *gi)
890 struct GadgetDesign *gd;
891 int state = (gi->state ? GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
893 if (gi == NULL || gi->deactivated)
896 gd = (!gi->active ? &gi->alt_design[state] :
897 gi->checked ? &gi->alt_design[state] : &gi->design[state]);
899 int x = gi->x + video.screen_xoffset;
900 int y = gi->y + video.screen_yoffset;
901 int alpha = gi->overlay_touch_button_alpha;
902 int alpha_max = SDL_ALPHA_OPAQUE;
903 int alpha_step = ALPHA_FADING_STEPSIZE(alpha_max);
905 // only show mapped overlay touch buttons if touch screen is really used
906 if (gi->mapped && runtime.uses_touch_device)
908 if (alpha < alpha_max)
909 alpha = MIN(alpha + alpha_step, alpha_max);
913 alpha = MAX(0, alpha - alpha_step);
916 gi->overlay_touch_button_alpha = alpha;
923 case GD_TYPE_NORMAL_BUTTON:
924 case GD_TYPE_CHECK_BUTTON:
925 case GD_TYPE_CHECK_BUTTON_2:
926 case GD_TYPE_RADIO_BUTTON:
927 SDL_SetTextureAlphaMod(gd->bitmap->texture_masked, alpha);
928 SDL_SetTextureBlendMode(gd->bitmap->texture_masked, SDL_BLENDMODE_BLEND);
930 BlitToScreenMasked(gd->bitmap, gd->x, gd->y, gi->width, gi->height, x, y);
938 void DrawGadgets_OverlayTouchButtons(void)
940 struct GadgetInfo *gi;
942 if (gadget_list_first_entry == NULL)
945 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
946 if (gi->overlay_touch_button)
947 DrawGadget_OverlayTouchButton(gi);
950 boolean CheckPosition_OverlayTouchButtons(int mx, int my, int button)
952 struct GadgetInfo *gi = getGadgetInfoFromMousePosition(mx, my, button);
954 return (gi != NULL && gi->overlay_touch_button);
957 static int get_minimal_size_for_numeric_input(int minmax_value)
959 int min_size = 1; // value needs at least one digit
962 // add number of digits needed for absolute value
963 for (i = 10; i <= ABS(minmax_value); i *= 10)
966 // if min/max value is negative, add one digit for minus sign
967 if (minmax_value < 0)
973 static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap)
980 while (tag != GDI_END)
985 gi->image_id = va_arg(ap, int);
989 gi->custom_id = va_arg(ap, int);
992 case GDI_CUSTOM_TYPE_ID:
993 gi->custom_type_id = va_arg(ap, int);
998 int max_textsize = MAX_INFO_TEXTSIZE;
999 char *text = va_arg(ap, char *);
1002 strncpy(gi->info_text, text, max_textsize);
1006 gi->info_text[max_textsize] = '\0';
1011 gi->x = gi->orig_x = va_arg(ap, int);
1015 gi->y = gi->orig_y = va_arg(ap, int);
1019 gi->width = va_arg(ap, int);
1023 gi->height = va_arg(ap, int);
1027 gi->type = va_arg(ap, unsigned int);
1031 gi->state = va_arg(ap, unsigned int);
1035 gi->active = (boolean)va_arg(ap, int);
1038 case GDI_DIRECT_DRAW:
1039 gi->direct_draw = (boolean)va_arg(ap, int);
1042 case GDI_OVERLAY_TOUCH_BUTTON:
1043 gi->overlay_touch_button = (boolean)va_arg(ap, int);
1044 if (gi->overlay_touch_button)
1045 SetGadgetPosition_OverlayTouchButton(gi);
1048 case GDI_CALLBACK_ACTION_ALWAYS:
1049 gi->callback_action_always = (boolean)va_arg(ap, int);
1053 gi->checked = (boolean)va_arg(ap, int);
1057 gi->radio_nr = va_arg(ap, unsigned int);
1060 case GDI_NUMBER_VALUE:
1061 gi->textinput.number_value = va_arg(ap, int);
1062 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
1063 strcpy(gi->textinput.last_value, gi->textinput.value);
1064 gi->textinput.cursor_position = strlen(gi->textinput.value);
1067 case GDI_NUMBER_MIN:
1068 gi->textinput.number_min = va_arg(ap, int);
1069 if (gi->textinput.number_value < gi->textinput.number_min)
1071 gi->textinput.number_value = gi->textinput.number_min;
1072 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
1073 strcpy(gi->textinput.last_value, gi->textinput.value);
1077 case GDI_NUMBER_MAX:
1078 gi->textinput.number_max = va_arg(ap, int);
1079 if (gi->textinput.number_value > gi->textinput.number_max)
1081 gi->textinput.number_value = gi->textinput.number_max;
1082 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
1083 strcpy(gi->textinput.last_value, gi->textinput.value);
1087 case GDI_TEXT_VALUE:
1089 int max_textsize = MAX_GADGET_TEXTSIZE;
1091 if (gi->textinput.size)
1092 max_textsize = MIN(gi->textinput.size, MAX_GADGET_TEXTSIZE);
1094 strncpy(gi->textinput.value, va_arg(ap, char *), max_textsize);
1095 strcpy(gi->textinput.last_value, gi->textinput.value);
1097 gi->textinput.value[max_textsize] = '\0';
1098 gi->textinput.cursor_position = strlen(gi->textinput.value);
1100 // same tag also used for other gadget definitions
1101 strcpy(gi->textbutton.value, gi->textinput.value);
1102 strcpy(gi->textarea.value, gi->textinput.value);
1103 strcpy(gi->textarea.last_value, gi->textinput.value);
1109 int tag_value = va_arg(ap, int);
1110 int max_textsize = MIN(tag_value, MAX_GADGET_TEXTSIZE);
1112 gi->textinput.size = max_textsize;
1113 gi->textinput.value[max_textsize] = '\0';
1114 strcpy(gi->textinput.last_value, gi->textinput.value);
1116 // same tag also used for other gadget definitions
1118 gi->textarea.size = max_textsize;
1119 gi->textarea.value[max_textsize] = '\0';
1120 strcpy(gi->textarea.last_value, gi->textinput.value);
1122 gi->textbutton.size = max_textsize;
1123 gi->textbutton.value[max_textsize] = '\0';
1125 gi->selectbox.size = gi->textinput.size;
1130 gi->font = va_arg(ap, int);
1131 if (gi->font_active == 0)
1132 gi->font_active = gi->font;
1133 if (gi->font_unselectable == 0)
1134 gi->font_unselectable = gi->font;
1137 case GDI_TEXT_FONT_ACTIVE:
1138 gi->font_active = va_arg(ap, int);
1141 case GDI_TEXT_FONT_UNSELECTABLE:
1142 gi->font_unselectable = va_arg(ap, int);
1145 case GDI_SELECTBOX_OPTIONS:
1146 gi->selectbox.options = va_arg(ap, struct ValueTextInfo *);
1149 case GDI_SELECTBOX_INDEX:
1150 gi->selectbox.index = va_arg(ap, int);
1153 case GDI_SELECTBOX_CHAR_UNSELECTABLE:
1154 gi->selectbox.char_unselectable = (char)va_arg(ap, int);
1157 case GDI_DESIGN_UNPRESSED:
1158 gi->design[GD_BUTTON_UNPRESSED].bitmap = va_arg(ap, Bitmap *);
1159 gi->design[GD_BUTTON_UNPRESSED].x = va_arg(ap, int);
1160 gi->design[GD_BUTTON_UNPRESSED].y = va_arg(ap, int);
1163 case GDI_DESIGN_PRESSED:
1164 gi->design[GD_BUTTON_PRESSED].bitmap = va_arg(ap, Bitmap *);
1165 gi->design[GD_BUTTON_PRESSED].x = va_arg(ap, int);
1166 gi->design[GD_BUTTON_PRESSED].y = va_arg(ap, int);
1169 case GDI_ALT_DESIGN_UNPRESSED:
1170 gi->alt_design[GD_BUTTON_UNPRESSED].bitmap = va_arg(ap, Bitmap *);
1171 gi->alt_design[GD_BUTTON_UNPRESSED].x = va_arg(ap, int);
1172 gi->alt_design[GD_BUTTON_UNPRESSED].y = va_arg(ap, int);
1175 case GDI_ALT_DESIGN_PRESSED:
1176 gi->alt_design[GD_BUTTON_PRESSED].bitmap = va_arg(ap, Bitmap *);
1177 gi->alt_design[GD_BUTTON_PRESSED].x = va_arg(ap, int);
1178 gi->alt_design[GD_BUTTON_PRESSED].y = va_arg(ap, int);
1181 case GDI_BORDER_SIZE:
1182 gi->border.xsize = va_arg(ap, int);
1183 gi->border.ysize = va_arg(ap, int);
1186 case GDI_BORDER_SIZE_SELECTBUTTON:
1187 gi->border.xsize_selectbutton = va_arg(ap, int);
1190 case GDI_DESIGN_WIDTH:
1191 gi->border.width = va_arg(ap, int);
1194 case GDI_DECORATION_DESIGN:
1195 gi->deco.design.bitmap = va_arg(ap, Bitmap *);
1196 gi->deco.design.x = va_arg(ap, int);
1197 gi->deco.design.y = va_arg(ap, int);
1200 case GDI_DECORATION_POSITION:
1201 gi->deco.x = va_arg(ap, int);
1202 gi->deco.y = va_arg(ap, int);
1205 case GDI_DECORATION_SIZE:
1206 gi->deco.width = va_arg(ap, int);
1207 gi->deco.height = va_arg(ap, int);
1210 case GDI_DECORATION_SHIFTING:
1211 gi->deco.xshift = va_arg(ap, int);
1212 gi->deco.yshift = va_arg(ap, int);
1215 case GDI_DECORATION_MASKED:
1216 gi->deco.masked = (boolean)va_arg(ap, int);
1219 case GDI_EVENT_MASK:
1220 gi->event_mask = va_arg(ap, unsigned int);
1224 gi->drawing.area_xsize = va_arg(ap, int);
1225 gi->drawing.area_ysize = va_arg(ap, int);
1227 // determine dependent values for drawing area gadget, if needed
1228 if (gi->drawing.item_xsize != 0 && gi->drawing.item_ysize != 0)
1230 gi->width = gi->drawing.area_xsize * gi->drawing.item_xsize;
1231 gi->height = gi->drawing.area_ysize * gi->drawing.item_ysize;
1233 else if (gi->width != 0 && gi->height != 0)
1235 gi->drawing.item_xsize = gi->width / gi->drawing.area_xsize;
1236 gi->drawing.item_ysize = gi->height / gi->drawing.area_ysize;
1239 // same tag also used for other gadget definitions
1240 gi->textarea.xsize = gi->drawing.area_xsize;
1241 gi->textarea.ysize = gi->drawing.area_ysize;
1243 if (gi->type & GD_TYPE_TEXT_AREA) // force recalculation
1252 gi->drawing.item_xsize = va_arg(ap, int);
1253 gi->drawing.item_ysize = va_arg(ap, int);
1255 // determine dependent values for drawing area gadget, if needed
1256 if (gi->drawing.area_xsize != 0 && gi->drawing.area_ysize != 0)
1258 gi->width = gi->drawing.area_xsize * gi->drawing.item_xsize;
1259 gi->height = gi->drawing.area_ysize * gi->drawing.item_ysize;
1261 else if (gi->width != 0 && gi->height != 0)
1263 gi->drawing.area_xsize = gi->width / gi->drawing.item_xsize;
1264 gi->drawing.area_ysize = gi->height / gi->drawing.item_ysize;
1268 case GDI_SCROLLBAR_ITEMS_MAX:
1269 gi->scrollbar.items_max = va_arg(ap, int);
1272 case GDI_SCROLLBAR_ITEMS_VISIBLE:
1273 gi->scrollbar.items_visible = va_arg(ap, int);
1276 case GDI_SCROLLBAR_ITEM_POSITION:
1277 gi->scrollbar.item_position = va_arg(ap, int);
1280 case GDI_WHEEL_AREA_X:
1281 gi->wheelarea.x = va_arg(ap, int);
1284 case GDI_WHEEL_AREA_Y:
1285 gi->wheelarea.y = va_arg(ap, int);
1288 case GDI_WHEEL_AREA_WIDTH:
1289 gi->wheelarea.width = va_arg(ap, int);
1292 case GDI_WHEEL_AREA_HEIGHT:
1293 gi->wheelarea.height = va_arg(ap, int);
1296 case GDI_CALLBACK_INFO:
1297 gi->callback_info = va_arg(ap, gadget_function);
1300 case GDI_CALLBACK_ACTION:
1301 gi->callback_action = va_arg(ap, gadget_function);
1305 Fail("HandleGadgetTags(): unknown tag %d", tag);
1308 tag = va_arg(ap, int); // read next tag
1311 gi->deactivated = FALSE;
1313 // check if gadget has undefined bitmaps
1314 if (gi->type != GD_TYPE_DRAWING_AREA &&
1315 (gi->design[GD_BUTTON_UNPRESSED].bitmap == NULL ||
1316 gi->design[GD_BUTTON_PRESSED].bitmap == NULL))
1317 gi->deactivated = TRUE;
1319 // check if gadget is placed off-screen (and is no overlay touch button)
1320 if ((gi->x < 0 || gi->y < 0) && !gi->overlay_touch_button)
1321 gi->deactivated = TRUE;
1323 // adjust gadget values in relation to other gadget values
1325 if (gi->type & GD_TYPE_TEXT_INPUT)
1327 int font_nr = gi->font_active;
1328 int font_width = getFontWidth(font_nr);
1329 int font_height = getFontHeight(font_nr);
1330 int border_xsize = gi->border.xsize;
1331 int border_ysize = gi->border.ysize;
1333 if (gi->type == GD_TYPE_TEXT_INPUT_NUMERIC)
1335 int number_min = gi->textinput.number_min;
1336 int number_max = gi->textinput.number_max;
1337 int min_size_min = get_minimal_size_for_numeric_input(number_min);
1338 int min_size_max = get_minimal_size_for_numeric_input(number_max);
1339 int min_size = MAX(min_size_min, min_size_max);
1341 // expand gadget text input size, if maximal value is too large
1342 if (gi->textinput.size < min_size)
1343 gi->textinput.size = min_size;
1346 gi->width = 2 * border_xsize + (gi->textinput.size + 1) * font_width;
1347 gi->height = 2 * border_ysize + font_height;
1350 if (gi->type & GD_TYPE_SELECTBOX)
1352 int font_nr = gi->font_active;
1353 int font_width = getFontWidth(font_nr);
1354 int font_height = getFontHeight(font_nr);
1355 int border_xsize = gi->border.xsize;
1356 int border_ysize = gi->border.ysize;
1357 int button_size = gi->border.xsize_selectbutton;
1358 int bottom_screen_border = getGadgetScreenBorderBottom();
1362 gi->width = 2 * border_xsize + gi->textinput.size * font_width + button_size;
1363 gi->height = 2 * border_ysize + font_height;
1365 if (gi->selectbox.options == NULL)
1366 Fail("selectbox gadget incomplete (missing options array)");
1368 gi->selectbox.num_values = 0;
1369 while (gi->selectbox.options[gi->selectbox.num_values].text != NULL)
1370 gi->selectbox.num_values++;
1372 // calculate values for open selectbox
1373 gi->selectbox.width = gi->width;
1374 gi->selectbox.height =
1375 2 * border_ysize + gi->selectbox.num_values * font_height;
1377 gi->selectbox.x = gi->x;
1378 gi->selectbox.y = gi->y + gi->height;
1379 if (gi->selectbox.y + gi->selectbox.height > bottom_screen_border)
1380 gi->selectbox.y = gi->y - gi->selectbox.height;
1381 if (gi->selectbox.y < 0)
1382 gi->selectbox.y = bottom_screen_border - gi->selectbox.height;
1384 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &src_bitmap, &src_x, &src_y);
1385 src_x += font_width / 2;
1386 src_y += font_height / 2;
1388 // there may be esoteric cases with missing or too small font bitmap
1389 if (src_bitmap != NULL &&
1390 src_x < src_bitmap->width && src_y < src_bitmap->height)
1391 gi->selectbox.inverse_color = GetPixel(src_bitmap, src_x, src_y);
1393 // always start with closed selectbox
1394 gi->selectbox.open = FALSE;
1397 if (gi->type & GD_TYPE_TEXT_INPUT_NUMERIC)
1399 struct GadgetTextInput *text = &gi->textinput;
1400 int value = text->number_value;
1402 text->number_value = (value < text->number_min ? text->number_min :
1403 value > text->number_max ? text->number_max :
1406 sprintf(text->value, "%d", text->number_value);
1409 if (gi->type & GD_TYPE_TEXT_BUTTON)
1411 int font_nr = gi->font_active;
1412 int font_width = getFontWidth(font_nr);
1413 int font_height = getFontHeight(font_nr);
1414 int border_xsize = gi->border.xsize;
1415 int border_ysize = gi->border.ysize;
1417 gi->width = 2 * border_xsize + gi->textbutton.size * font_width;
1418 gi->height = 2 * border_ysize + font_height;
1421 if (gi->type & GD_TYPE_SCROLLBAR)
1423 struct GadgetScrollbar *gs = &gi->scrollbar;
1424 int scrollbar_size_cmp;
1426 if (gi->width == 0 || gi->height == 0 ||
1427 gs->items_max == 0 || gs->items_visible == 0)
1428 Fail("scrollbar gadget incomplete (missing tags)");
1430 // calculate internal scrollbar values
1431 gs->size_min = (gi->type == GD_TYPE_SCROLLBAR_VERTICAL ?
1432 gi->width : gi->height);
1433 gs->size_max = (gi->type == GD_TYPE_SCROLLBAR_VERTICAL ?
1434 gi->height : gi->width);
1436 scrollbar_size_cmp = gs->size_max * gs->items_visible / gs->items_max;
1437 gs->size = MAX(scrollbar_size_cmp, gs->size_min);
1438 gs->size_max_cmp = (gs->size_max - (gs->size - scrollbar_size_cmp));
1440 gs->position = gs->size_max_cmp * gs->item_position / gs->items_max;
1441 gs->position_max = gs->size_max - gs->size;
1442 gs->correction = gs->size_max / gs->items_max / 2;
1444 // finetuning for maximal right/bottom position
1445 if (gs->item_position == gs->items_max - gs->items_visible)
1446 gs->position = gs->position_max;
1449 if (gi->type & GD_TYPE_TEXT_AREA)
1451 int font_nr = gi->font_active;
1452 int font_width = getFontWidth(font_nr);
1453 int font_height = getFontHeight(font_nr);
1454 int border_xsize = gi->border.xsize;
1455 int border_ysize = gi->border.ysize;
1456 int right_screen_border = getGadgetScreenBorderRight();
1457 int bottom_screen_border = getGadgetScreenBorderBottom();
1459 if (gi->width == 0 || gi->height == 0)
1461 gi->width = 2 * border_xsize + gi->textarea.xsize * font_width;
1462 gi->height = 2 * border_ysize + gi->textarea.ysize * font_height;
1466 gi->textarea.xsize = (gi->width - 2 * border_xsize) / font_width;
1467 gi->textarea.ysize = (gi->height - 2 * border_ysize) / font_height;
1470 gi->textarea.full_x = gi->x;
1471 gi->textarea.full_y = gi->y;
1472 gi->textarea.crop_width = gi->width;
1473 gi->textarea.crop_height = gi->height;
1474 gi->textarea.crop_xsize = gi->textarea.xsize;
1475 gi->textarea.crop_ysize = gi->textarea.ysize;
1477 gi->textarea.cropped = FALSE;
1479 if (gi->x + gi->width > right_screen_border)
1481 gi->textarea.full_x = MAX(0, right_screen_border - gi->width);
1482 gi->textarea.crop_width = right_screen_border - gi->x;
1483 gi->textarea.crop_xsize =
1484 (gi->textarea.crop_width - 2 * border_xsize) / font_width;
1485 gi->textarea.crop_width =
1486 2 * border_xsize + gi->textarea.crop_xsize * font_width;
1488 gi->textarea.cropped = TRUE;
1491 if (gi->y + gi->height > bottom_screen_border)
1493 gi->textarea.full_y = MAX(0, bottom_screen_border - gi->height);
1494 gi->textarea.crop_height = bottom_screen_border - gi->y;
1495 gi->textarea.crop_ysize =
1496 (gi->textarea.crop_height - 2 * border_ysize) / font_height;
1497 gi->textarea.crop_height =
1498 2 * border_ysize + gi->textarea.crop_ysize * font_height;
1500 gi->textarea.cropped = TRUE;
1503 // always start with unselected text area (which is potentially cropped)
1504 gi->textarea.full_open = FALSE;
1508 void ModifyGadget(struct GadgetInfo *gi, int first_tag, ...)
1512 va_start(ap, first_tag);
1513 HandleGadgetTags(gi, first_tag, ap);
1519 void RedrawGadget(struct GadgetInfo *gi)
1521 if (gi == NULL || gi->deactivated)
1525 DrawGadget(gi, gi->state, gi->direct_draw);
1528 struct GadgetInfo *CreateGadget(int first_tag, ...)
1530 struct GadgetInfo *new_gadget = checked_calloc(sizeof(struct GadgetInfo));
1533 // always start with reliable default values
1534 new_gadget->id = getNewGadgetID();
1535 new_gadget->image_id = -1;
1536 new_gadget->callback_info = default_callback_info;
1537 new_gadget->callback_action = default_callback_action;
1538 new_gadget->active = TRUE;
1539 new_gadget->direct_draw = TRUE;
1540 new_gadget->overlay_touch_button = FALSE;
1541 new_gadget->overlay_touch_button_alpha = 0;
1543 new_gadget->next = NULL;
1545 va_start(ap, first_tag);
1546 HandleGadgetTags(new_gadget, first_tag, ap);
1549 // insert new gadget into global gadget list
1550 if (gadget_list_last_entry)
1552 gadget_list_last_entry->next = new_gadget;
1553 gadget_list_last_entry = gadget_list_last_entry->next;
1556 gadget_list_first_entry = gadget_list_last_entry = new_gadget;
1561 void FreeGadget(struct GadgetInfo *gi)
1563 struct GadgetInfo *gi_previous = gadget_list_first_entry;
1568 // prevent "last_info_gi" from pointing to memory that will be freed
1569 if (last_info_gi == gi)
1570 last_info_gi = NULL;
1572 while (gi_previous != NULL && gi_previous->next != gi)
1573 gi_previous = gi_previous->next;
1575 if (gi == gadget_list_first_entry)
1576 gadget_list_first_entry = gi->next;
1578 if (gi == gadget_list_last_entry)
1579 gadget_list_last_entry = gi_previous;
1581 if (gi_previous != NULL)
1582 gi_previous->next = gi->next;
1587 static void CheckRangeOfNumericInputGadget(struct GadgetInfo *gi)
1589 if (gi->type != GD_TYPE_TEXT_INPUT_NUMERIC)
1592 gi->textinput.number_value = atoi(gi->textinput.value);
1594 if (gi->textinput.number_value < gi->textinput.number_min)
1595 gi->textinput.number_value = gi->textinput.number_min;
1596 if (gi->textinput.number_value > gi->textinput.number_max)
1597 gi->textinput.number_value = gi->textinput.number_max;
1599 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
1601 if (gi->textinput.cursor_position < 0)
1602 gi->textinput.cursor_position = 0;
1603 else if (gi->textinput.cursor_position > strlen(gi->textinput.value))
1604 gi->textinput.cursor_position = strlen(gi->textinput.value);
1607 // global pointer to gadget actually in use (when mouse button pressed)
1608 static struct GadgetInfo *last_gi = NULL;
1610 static void MapGadgetExt(struct GadgetInfo *gi, boolean redraw)
1612 if (gi == NULL || gi->deactivated || gi->mapped)
1615 // do not map overlay touch buttons if touch screen is not used
1616 if (gi->overlay_touch_button && !runtime.uses_touch_device)
1622 DrawGadget(gi, DG_UNPRESSED, DG_BUFFERED);
1625 void MapGadget(struct GadgetInfo *gi)
1627 MapGadgetExt(gi, TRUE);
1630 void UnmapGadget(struct GadgetInfo *gi)
1632 if (gi == NULL || gi->deactivated || !gi->mapped)
1641 #define MAX_NUM_GADGETS 1024
1642 #define MULTIMAP_UNMAP (1 << 0)
1643 #define MULTIMAP_REMAP (1 << 1)
1644 #define MULTIMAP_REDRAW (1 << 2)
1645 #define MULTIMAP_PLAYFIELD (1 << 3)
1646 #define MULTIMAP_DOOR_1 (1 << 4)
1647 #define MULTIMAP_DOOR_2 (1 << 5)
1648 #define MULTIMAP_DOOR_3 (1 << 6)
1649 #define MULTIMAP_ALL (MULTIMAP_PLAYFIELD | \
1654 static void MultiMapGadgets(int mode)
1656 struct GadgetInfo *gi = gadget_list_first_entry;
1657 static boolean map_state[MAX_NUM_GADGETS];
1665 if ((mode & MULTIMAP_PLAYFIELD && IN_GFX_FIELD_FULL(x, y)) ||
1666 (mode & MULTIMAP_DOOR_1 && IN_GFX_DOOR_1(x, y)) ||
1667 (mode & MULTIMAP_DOOR_2 && IN_GFX_DOOR_2(x, y)) ||
1668 (mode & MULTIMAP_DOOR_3 && IN_GFX_DOOR_3(x, y)) ||
1669 (mode & MULTIMAP_ALL) == MULTIMAP_ALL)
1671 if (mode & MULTIMAP_UNMAP)
1673 map_state[map_count++ % MAX_NUM_GADGETS] = gi->mapped;
1678 if (map_state[map_count++ % MAX_NUM_GADGETS])
1679 MapGadgetExt(gi, (mode & MULTIMAP_REDRAW));
1687 void UnmapAllGadgets(void)
1689 MultiMapGadgets(MULTIMAP_ALL | MULTIMAP_UNMAP);
1692 void RemapAllGadgets(void)
1694 MultiMapGadgets(MULTIMAP_ALL | MULTIMAP_REMAP);
1697 boolean anyTextInputGadgetActive(void)
1699 return (last_gi && (last_gi->type & GD_TYPE_TEXT_INPUT) && last_gi->mapped);
1702 boolean anyTextAreaGadgetActive(void)
1704 return (last_gi && (last_gi->type & GD_TYPE_TEXT_AREA) && last_gi->mapped);
1707 boolean anySelectboxGadgetActive(void)
1709 return (last_gi && (last_gi->type & GD_TYPE_SELECTBOX) && last_gi->mapped);
1712 boolean anyScrollbarGadgetActive(void)
1714 return (last_gi && (last_gi->type & GD_TYPE_SCROLLBAR) && last_gi->mapped);
1717 boolean anyTextGadgetActive(void)
1719 return (anyTextInputGadgetActive() ||
1720 anyTextAreaGadgetActive() ||
1721 anySelectboxGadgetActive());
1724 static boolean insideSelectboxLine(struct GadgetInfo *gi, int mx, int my)
1726 return (gi != NULL &&
1727 gi->type & GD_TYPE_SELECTBOX &&
1728 mx >= gi->x && mx < gi->x + gi->width &&
1729 my >= gi->y && my < gi->y + gi->height);
1732 static boolean insideSelectboxArea(struct GadgetInfo *gi, int mx, int my)
1734 return (gi != NULL &&
1735 gi->type & GD_TYPE_SELECTBOX &&
1736 mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width &&
1737 my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height);
1740 void ClickOnGadget(struct GadgetInfo *gi, int button)
1742 if (gi == NULL || gi->deactivated || !gi->mapped)
1745 // simulate releasing mouse button over last gadget, if still pressed
1747 HandleGadgets(-1, -1, 0);
1752 // set cursor position to the end of the text for text input gadgets
1753 if (gi->type & GD_TYPE_TEXT_INPUT)
1754 x = gi->x + gi->width - 1;
1756 // simulate pressing mouse button over specified gadget
1757 HandleGadgets(x, y, button);
1759 // simulate releasing mouse button over specified gadget
1760 HandleGadgets(x, y, 0);
1763 boolean HandleGadgets(int mx, int my, int button)
1765 static DelayCounter pressed_delay = { GADGET_FRAME_DELAY };
1766 static int last_button = 0;
1767 static int last_mx = 0, last_my = 0;
1768 static int pressed_mx = 0, pressed_my = 0;
1769 static boolean keep_selectbox_open = FALSE;
1770 static boolean gadget_stopped = FALSE;
1771 int scrollbar_mouse_pos = 0;
1772 struct GadgetInfo *new_gi, *gi;
1773 boolean press_event;
1774 boolean release_event;
1775 boolean mouse_moving;
1776 boolean mouse_inside_select_line;
1777 boolean mouse_inside_select_area;
1778 boolean mouse_released_where_pressed;
1779 boolean gadget_pressed;
1780 boolean gadget_pressed_repeated;
1781 boolean gadget_pressed_off_borders;
1782 boolean gadget_pressed_inside_select_line;
1783 boolean gadget_pressed_delay_reached;
1784 boolean gadget_moving;
1785 boolean gadget_moving_inside;
1786 boolean gadget_moving_off_borders;
1787 boolean gadget_draggable;
1788 boolean gadget_dragging;
1789 boolean gadget_released;
1790 boolean gadget_released_inside;
1791 boolean gadget_released_inside_select_area;
1792 boolean gadget_released_off_borders;
1793 boolean changed_position = FALSE;
1795 // check if there are any gadgets defined
1796 if (gadget_list_first_entry == NULL)
1799 // simulated release of mouse button over last gadget
1800 if (mx == -1 && my == -1 && button == 0)
1806 // check which gadget is under the mouse pointer
1807 new_gi = getGadgetInfoFromMousePosition(mx, my, button);
1809 // check if button state has changed since last invocation
1810 press_event = (button != 0 && last_button == 0);
1811 release_event = (button == 0 && last_button != 0);
1812 last_button = button;
1814 // check if mouse has been moved since last invocation
1815 mouse_moving = ((mx != last_mx || my != last_my) && motion_status);
1819 if (press_event && new_gi != last_gi)
1825 mouse_released_where_pressed =
1826 (release_event && mx == pressed_mx && my == pressed_my);
1828 mouse_inside_select_line = insideSelectboxLine(new_gi, mx, my);
1829 mouse_inside_select_area = insideSelectboxArea(new_gi, mx, my);
1831 gadget_pressed_off_borders = (press_event && new_gi != last_gi);
1833 gadget_pressed_inside_select_line =
1834 (press_event && new_gi != NULL &&
1835 new_gi->type & GD_TYPE_SELECTBOX && new_gi->selectbox.open &&
1836 insideSelectboxLine(new_gi, mx, my));
1838 // if mouse button pressed outside text or selectbox gadget, deactivate it
1839 if (anyTextGadgetActive() &&
1840 (gadget_pressed_off_borders ||
1841 (gadget_pressed_inside_select_line && !mouse_inside_select_area)))
1843 struct GadgetInfo *gi = last_gi;
1844 boolean gadget_changed = ((gi->event_mask & GD_EVENT_TEXT_LEAVING) != 0);
1846 // check if text input gadget has changed its value
1847 if (gi->type & GD_TYPE_TEXT_INPUT)
1849 CheckRangeOfNumericInputGadget(gi);
1851 if (!strEqual(gi->textinput.last_value, gi->textinput.value))
1852 strcpy(gi->textinput.last_value, gi->textinput.value);
1854 gadget_changed = FALSE;
1857 // check if text area gadget has changed its value
1858 if (gi->type & GD_TYPE_TEXT_AREA)
1860 if (!strEqual(gi->textarea.last_value, gi->textarea.value))
1861 strcpy(gi->textarea.last_value, gi->textarea.value);
1863 gadget_changed = FALSE;
1866 // selectbox does not change its value when closed by clicking outside
1867 if (gi->type & GD_TYPE_SELECTBOX)
1868 gadget_changed = FALSE;
1870 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1872 gi->event.type = GD_EVENT_TEXT_LEAVING;
1874 if (!(gi->type & GD_TYPE_SELECTBOX))
1875 DoGadgetCallbackAction(gi, gadget_changed);
1879 if (gadget_pressed_inside_select_line)
1886 (button != 0 && last_gi == NULL && new_gi != NULL && press_event);
1887 gadget_pressed_repeated =
1888 (button != 0 && last_gi != NULL && new_gi == last_gi);
1890 gadget_pressed_delay_reached =
1891 DelayReached(&pressed_delay);
1893 gadget_released = (release_event && last_gi != NULL);
1894 gadget_released_inside = (gadget_released && new_gi == last_gi);
1895 gadget_released_off_borders = (gadget_released && new_gi != last_gi);
1897 gadget_moving = (button != 0 && last_gi != NULL && mouse_moving);
1898 gadget_moving_inside = (gadget_moving && new_gi == last_gi);
1899 gadget_moving_off_borders = (gadget_moving && new_gi != last_gi);
1901 // when handling selectbox, set additional state values
1902 if (gadget_released_inside && (last_gi->type & GD_TYPE_SELECTBOX))
1903 gadget_released_inside_select_area = insideSelectboxArea(last_gi, mx, my);
1905 gadget_released_inside_select_area = FALSE;
1907 // setting state for handling over-large selectbox
1908 if (keep_selectbox_open && (press_event || !mouse_inside_select_line))
1909 keep_selectbox_open = FALSE;
1911 // if new gadget pressed, store this gadget
1915 // 'gi' is actually handled gadget
1918 // if gadget is scrollbar, choose mouse position value
1919 if (gi && gi->type & GD_TYPE_SCROLLBAR)
1920 scrollbar_mouse_pos =
1921 (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx - gi->x : my - gi->y);
1923 // if mouse button released, no gadget needs to be handled anymore
1924 if (gadget_released)
1926 if (gi->type & GD_TYPE_SELECTBOX &&
1927 (keep_selectbox_open ||
1928 mouse_released_where_pressed ||
1929 !gadget_released_inside_select_area ||
1930 !CURRENT_OPTION_SELECTABLE(gi))) // selectbox stays open
1932 gi->selectbox.stay_open = TRUE;
1936 else if (!(gi->type & GD_TYPE_TEXT_INPUT ||
1937 gi->type & GD_TYPE_TEXT_AREA)) // text input stays open
1941 // modify event position values even if no gadget is pressed
1942 if (button == 0 && !release_event)
1945 // if new gadget or if no gadget was pressed, release stopped processing
1946 if (gadget_pressed || new_gi == NULL)
1947 gadget_stopped = FALSE;
1949 // if gadget was stopped while being handled, stop gadget processing here
1955 int last_x = gi->event.x;
1956 int last_y = gi->event.y;
1957 int last_mx = gi->event.mx;
1958 int last_my = gi->event.my;
1960 gi->event.x = gi->event.mx = mx - gi->x;
1961 gi->event.y = gi->event.my = my - gi->y;
1963 if (gi->type == GD_TYPE_DRAWING_AREA)
1965 gi->event.x /= gi->drawing.item_xsize;
1966 gi->event.y /= gi->drawing.item_ysize;
1968 if (last_x != gi->event.x || last_y != gi->event.y ||
1969 ((last_mx != gi->event.mx || last_my != gi->event.my) &&
1970 gi->event_mask & GD_EVENT_PIXEL_PRECISE))
1971 changed_position = TRUE;
1973 else if (gi->type & GD_TYPE_TEXT_INPUT && button != 0 && !motion_status)
1975 int old_cursor_position = gi->textinput.cursor_position;
1977 // if mouse button pressed inside activated text gadget, set cursor
1978 gi->textinput.cursor_position =
1979 (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font);
1981 if (gi->textinput.cursor_position < 0)
1982 gi->textinput.cursor_position = 0;
1983 else if (gi->textinput.cursor_position > strlen(gi->textinput.value))
1984 gi->textinput.cursor_position = strlen(gi->textinput.value);
1986 if (gi->textinput.cursor_position != old_cursor_position)
1987 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1990 StartTextInput(gi->x, gi->y, gi->width, gi->height);
1992 else if (gi->type & GD_TYPE_TEXT_AREA && button != 0 && !motion_status)
1994 int old_cursor_position = gi->textarea.cursor_position;
1995 int gadget_x = mx - gi->textarea.full_x - gi->border.xsize;
1996 int gadget_y = my - gi->textarea.full_y - gi->border.ysize;
1997 int x = gadget_x / getFontWidth(gi->font);
1998 int y = gadget_y / getFontHeight(gi->font);
2000 x = (x < 0 ? 0 : x >= gi->textarea.xsize ? gi->textarea.xsize - 1 : x);
2001 y = (y < 0 ? 0 : y >= gi->textarea.ysize ? gi->textarea.ysize - 1 : y);
2003 setTextAreaCursorXY(gi, x, y);
2005 if (gi->textarea.cursor_position != old_cursor_position)
2006 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2009 StartTextInput(gi->x, gi->y, gi->width, gi->height);
2011 else if (gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open &&
2012 !keep_selectbox_open)
2014 int old_index = gi->selectbox.current_index;
2016 // if mouse moving inside activated selectbox, select value
2017 if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
2018 gi->selectbox.current_index =
2019 (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font);
2021 if (gi->selectbox.current_index < 0)
2022 gi->selectbox.current_index = 0;
2023 else if (gi->selectbox.current_index > gi->selectbox.num_values - 1)
2024 gi->selectbox.current_index = gi->selectbox.num_values - 1;
2026 if (gi->selectbox.current_index != old_index)
2027 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2031 // handle gadget popup info text
2032 if (last_info_gi != new_gi ||
2033 (new_gi && new_gi->type == GD_TYPE_DRAWING_AREA && changed_position))
2035 if (new_gi != NULL && (button == 0 || new_gi == last_gi))
2037 new_gi->event.type = GD_EVENT_INFO_ENTERING;
2038 new_gi->callback_info(new_gi);
2040 else if (last_info_gi != NULL && last_info_gi->mapped)
2042 last_info_gi->event.type = GD_EVENT_INFO_LEAVING;
2043 last_info_gi->callback_info(last_info_gi);
2046 last_info_gi = new_gi;
2049 gadget_draggable = (gi && gi->type & GD_TYPE_SCROLLBAR);
2051 // reset drag position for newly pressed scrollbar to "not dragging"
2052 if (gadget_pressed && gadget_draggable)
2053 gi->scrollbar.drag_position = -1;
2055 gadget_dragging = (gadget_draggable && gi->scrollbar.drag_position != -1);
2057 // clicking next to a scrollbar to move it is not considered "moving"
2058 if (gadget_draggable && !gadget_dragging)
2059 gadget_moving = FALSE;
2061 // when leaving scrollbar area when jump-scrolling, stop gadget processing
2062 if (gadget_draggable && !gadget_dragging && gadget_moving_off_borders)
2063 gadget_stopped = TRUE;
2065 if ((gadget_pressed) ||
2066 (gadget_pressed_repeated && gadget_pressed_delay_reached))
2068 if (gadget_pressed) // gadget pressed the first time
2070 // initialize delay counter
2071 ResetDelayCounter(&pressed_delay);
2073 // start gadget delay with longer delay after first click on gadget
2074 pressed_delay.value = GADGET_FRAME_DELAY_FIRST;
2076 else // gadget hold pressed for some time
2078 // after first repeated gadget click, continue with shorter delay value
2079 pressed_delay.value = GADGET_FRAME_DELAY;
2082 if (gi->type & GD_TYPE_SCROLLBAR && !gadget_dragging)
2084 int mpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx : my);
2085 int gpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? gi->x : gi->y);
2086 int slider_start = gpos + gi->scrollbar.position;
2087 int slider_end = gpos + gi->scrollbar.position + gi->scrollbar.size - 1;
2088 boolean inside_slider = (mpos >= slider_start && mpos <= slider_end);
2090 if (IS_WHEEL_BUTTON(button) || !inside_slider)
2092 // click scrollbar one scrollbar length up/left or down/right
2094 struct GadgetScrollbar *gs = &gi->scrollbar;
2095 int old_item_position = gs->item_position;
2096 int item_steps = gs->items_visible - 1;
2097 int item_direction = (mpos < gpos + gi->scrollbar.position ? -1 : +1);
2099 if (IS_WHEEL_BUTTON(button))
2101 boolean scroll_single_step = ((GetKeyModState() & KMOD_Alt) != 0);
2103 item_steps = (scroll_single_step ? 1 : wheel_steps);
2104 item_direction = (button == MB_WHEEL_UP ||
2105 button == MB_WHEEL_LEFT ? -1 : +1);
2108 changed_position = FALSE;
2110 gs->item_position += item_steps * item_direction;
2112 if (gs->item_position < 0)
2113 gs->item_position = 0;
2114 else if (gs->item_position > gs->items_max - gs->items_visible)
2115 gs->item_position = gs->items_max - gs->items_visible;
2117 if (old_item_position != gs->item_position)
2119 gi->event.item_position = gs->item_position;
2120 changed_position = TRUE;
2123 ModifyGadget(gi, GDI_SCROLLBAR_ITEM_POSITION, gs->item_position,
2126 gi->state = GD_BUTTON_UNPRESSED;
2127 gi->event.type = GD_EVENT_MOVING;
2128 gi->event.off_borders = FALSE;
2130 if (gi->event_mask & GD_EVENT_MOVING)
2131 DoGadgetCallbackAction(gi, changed_position);
2137 // don't handle this scrollbar anymore when mouse position reached
2138 if (gadget_pressed_repeated)
2140 gadget_stopped = TRUE;
2150 PlayGadgetSoundActivating();
2152 if (gi->type == GD_TYPE_CHECK_BUTTON)
2154 gi->checked = !gi->checked;
2156 else if (gi->type == GD_TYPE_RADIO_BUTTON)
2158 struct GadgetInfo *rgi = gadget_list_first_entry;
2163 rgi->type == GD_TYPE_RADIO_BUTTON &&
2164 rgi->radio_nr == gi->radio_nr &&
2167 rgi->checked = FALSE;
2168 DrawGadget(rgi, DG_UNPRESSED, rgi->direct_draw);
2176 else if (gi->type & GD_TYPE_SCROLLBAR)
2178 int mpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx : my);
2179 int gpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? gi->x : gi->y);
2180 int slider_start = gpos + gi->scrollbar.position;
2181 int slider_end = gpos + gi->scrollbar.position + gi->scrollbar.size - 1;
2182 boolean inside_slider = (mpos >= slider_start && mpos <= slider_end);
2184 if (!IS_WHEEL_BUTTON(button) && inside_slider)
2186 // start dragging scrollbar
2187 gi->scrollbar.drag_position =
2188 scrollbar_mouse_pos - gi->scrollbar.position;
2191 else if (gi->type & GD_TYPE_SELECTBOX)
2193 // keep selectbox open in case of over-large selectbox
2194 keep_selectbox_open = (mouse_inside_select_line &&
2195 mouse_inside_select_area);
2198 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2200 gi->state = GD_BUTTON_PRESSED;
2201 gi->event.type = GD_EVENT_PRESSED;
2202 gi->event.button = button;
2203 gi->event.off_borders = FALSE;
2205 if (gi->event_mask & GD_EVENT_PRESSED)
2206 gi->callback_action(gi);
2209 if (gadget_pressed_repeated)
2211 gi->event.type = GD_EVENT_PRESSED;
2213 if (gi->event_mask & GD_EVENT_REPEATED && gadget_pressed_delay_reached)
2214 gi->callback_action(gi);
2219 if (gi->type & GD_TYPE_BUTTON)
2221 if (gadget_moving_inside && gi->state == GD_BUTTON_UNPRESSED)
2222 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2223 else if (gadget_moving_off_borders && gi->state == GD_BUTTON_PRESSED)
2224 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2226 else if (gi->type & GD_TYPE_SELECTBOX && !keep_selectbox_open)
2228 int old_index = gi->selectbox.current_index;
2230 // if mouse moving inside activated selectbox, select value
2231 if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
2232 gi->selectbox.current_index =
2233 (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font);
2235 if (gi->selectbox.current_index < 0)
2236 gi->selectbox.current_index = 0;
2237 else if (gi->selectbox.current_index > gi->selectbox.num_values - 1)
2238 gi->selectbox.current_index = gi->selectbox.num_values - 1;
2240 if (gi->selectbox.current_index != old_index)
2241 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2243 else if (gi->type & GD_TYPE_SCROLLBAR)
2245 struct GadgetScrollbar *gs = &gi->scrollbar;
2246 int old_item_position = gs->item_position;
2248 gs->position = scrollbar_mouse_pos - gs->drag_position;
2250 // make sure to always precisely reach end positions when dragging
2251 if (gs->position <= 0)
2254 gs->item_position = 0;
2256 else if (gs->position >= gs->position_max)
2258 gs->position = gs->position_max;
2259 gs->item_position = gs->items_max - gs->items_visible;
2264 gs->items_max * (gs->position + gs->correction) / gs->size_max_cmp;
2267 if (gs->item_position < 0)
2268 gs->item_position = 0;
2269 if (gs->item_position > gs->items_max - 1)
2270 gs->item_position = gs->items_max - 1;
2272 if (old_item_position != gs->item_position)
2274 gi->event.item_position = gs->item_position;
2275 changed_position = TRUE;
2278 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2281 gi->state = (gadget_moving_inside || gadget_draggable ?
2282 GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
2283 gi->event.type = GD_EVENT_MOVING;
2284 gi->event.off_borders = gadget_moving_off_borders;
2286 if (gi->event_mask & GD_EVENT_MOVING &&
2287 (gadget_moving_inside || gi->event_mask & GD_EVENT_OFF_BORDERS))
2288 DoGadgetCallbackAction(gi, changed_position);
2291 if (gadget_released_inside)
2293 boolean deactivate_gadget = TRUE;
2294 boolean gadget_changed = TRUE;
2296 if (gi->type == GD_TYPE_CHECK_BUTTON_2)
2298 gi->checked = !gi->checked;
2300 else if (gi->type & GD_TYPE_SELECTBOX)
2302 if (keep_selectbox_open ||
2303 mouse_released_where_pressed ||
2304 !gadget_released_inside_select_area ||
2305 !CURRENT_OPTION_SELECTABLE(gi)) // selectbox stays open
2307 deactivate_gadget = FALSE;
2308 gadget_changed = FALSE;
2310 else if (gi->selectbox.index != gi->selectbox.current_index)
2311 gi->selectbox.index = gi->selectbox.current_index;
2313 gadget_changed = FALSE;
2316 if (deactivate_gadget &&
2317 !(gi->type & GD_TYPE_TEXT_INPUT ||
2318 gi->type & GD_TYPE_TEXT_AREA)) // text input stays open
2319 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2321 gi->state = GD_BUTTON_UNPRESSED;
2322 gi->event.type = GD_EVENT_RELEASED;
2324 if ((gi->event_mask & GD_EVENT_RELEASED))
2325 DoGadgetCallbackAction(gi, gadget_changed);
2328 if (gadget_released_off_borders)
2330 if (gi->type & GD_TYPE_SCROLLBAR)
2331 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2333 gi->state = GD_BUTTON_UNPRESSED;
2334 gi->event.type = GD_EVENT_RELEASED;
2336 if (gi->event_mask & GD_EVENT_RELEASED &&
2337 gi->event_mask & GD_EVENT_OFF_BORDERS)
2338 gi->callback_action(gi);
2341 // handle gadgets unmapped/mapped between pressing and releasing
2342 if (release_event && !gadget_released && new_gi)
2343 new_gi->state = GD_BUTTON_UNPRESSED;
2345 return (gadget_pressed || gadget_pressed_repeated ||
2346 gadget_released || gadget_moving);
2349 static void insertCharIntoTextArea(struct GadgetInfo *gi, char c)
2351 char text[MAX_GADGET_TEXTSIZE + 1];
2352 int cursor_position = gi->textarea.cursor_position;
2354 if (strlen(gi->textarea.value) >= MAX_GADGET_TEXTSIZE) // no space left
2357 strcpy(text, gi->textarea.value);
2358 strcpy(&gi->textarea.value[cursor_position + 1], &text[cursor_position]);
2359 gi->textarea.value[cursor_position] = c;
2361 setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1);
2364 boolean HandleGadgetsKeyInput(Key key)
2366 struct GadgetInfo *gi = last_gi;
2368 if (gi == NULL || gi->deactivated || !gi->mapped ||
2369 !(gi->type & GD_TYPE_TEXT_INPUT ||
2370 gi->type & GD_TYPE_TEXT_AREA ||
2371 gi->type & GD_TYPE_SELECTBOX))
2374 if (key == KSYM_Escape)
2376 if (anyTextGadgetActive())
2378 boolean gadget_changed = ((gi->event_mask & GD_EVENT_TEXT_LEAVING) != 0);
2380 // restore previous text (before activating text gadget)
2381 if (gi->type & GD_TYPE_TEXT_INPUT)
2383 strcpy(gi->textinput.value, gi->textinput.last_value);
2385 CheckRangeOfNumericInputGadget(gi);
2388 // store current text for text area gadgets when pressing "Escape" key
2389 if (gi->type & GD_TYPE_TEXT_AREA)
2391 if (!strEqual(gi->textarea.last_value, gi->textarea.value))
2392 strcpy(gi->textarea.last_value, gi->textarea.value);
2394 gadget_changed = FALSE;
2397 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2399 if (gi->type & GD_TYPE_TEXT_AREA)
2401 gi->event.type = GD_EVENT_TEXT_LEAVING;
2403 DoGadgetCallbackAction(gi, gadget_changed);
2411 else if (key == KSYM_Return) // valid for both text input and selectbox
2413 boolean gadget_changed = ((gi->event_mask & GD_EVENT_TEXT_RETURN) != 0);
2415 if (gi->type & GD_TYPE_TEXT_INPUT)
2417 CheckRangeOfNumericInputGadget(gi);
2419 if (!strEqual(gi->textinput.last_value, gi->textinput.value))
2420 strcpy(gi->textinput.last_value, gi->textinput.value);
2422 gadget_changed = FALSE;
2426 else if (gi->type & GD_TYPE_SELECTBOX)
2428 if (gi->selectbox.index != gi->selectbox.current_index)
2429 gi->selectbox.index = gi->selectbox.current_index;
2431 gadget_changed = FALSE;
2434 if (gi->type & GD_TYPE_TEXT_AREA)
2436 insertCharIntoTextArea(gi, '\n');
2438 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2442 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2444 gi->event.type = GD_EVENT_TEXT_RETURN;
2449 DoGadgetCallbackAction(gi, gadget_changed);
2451 else if (gi->type & GD_TYPE_TEXT_INPUT) // only valid for text input
2453 char text[MAX_GADGET_TEXTSIZE + 1];
2454 int text_length = strlen(gi->textinput.value);
2455 int cursor_pos = gi->textinput.cursor_position;
2456 char letter = getCharFromKey(key);
2457 boolean legal_letter = (gi->type == GD_TYPE_TEXT_INPUT_NUMERIC ?
2458 letter >= '0' && letter <= '9' :
2461 if (legal_letter && text_length < gi->textinput.size)
2463 strcpy(text, gi->textinput.value);
2464 strcpy(&gi->textinput.value[cursor_pos + 1], &text[cursor_pos]);
2465 gi->textinput.value[cursor_pos] = letter;
2466 gi->textinput.cursor_position++;
2468 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2470 else if (key == KSYM_Left && cursor_pos > 0)
2472 gi->textinput.cursor_position--;
2474 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2476 else if (key == KSYM_Right && cursor_pos < text_length)
2478 gi->textinput.cursor_position++;
2480 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2482 else if (key == KSYM_BackSpace && cursor_pos > 0)
2484 strcpy(text, gi->textinput.value);
2485 strcpy(&gi->textinput.value[cursor_pos - 1], &text[cursor_pos]);
2486 gi->textinput.cursor_position--;
2488 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2490 else if (key == KSYM_Delete && cursor_pos < text_length)
2492 strcpy(text, gi->textinput.value);
2493 strcpy(&gi->textinput.value[cursor_pos], &text[cursor_pos + 1]);
2495 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2497 else if (key == KSYM_Home && cursor_pos > 0)
2499 gi->textinput.cursor_position = 0;
2501 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2503 else if (key == KSYM_End && cursor_pos < text_length)
2505 gi->textinput.cursor_position = text_length;
2507 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2510 else if (gi->type & GD_TYPE_TEXT_AREA) // only valid for text area
2512 char text[MAX_GADGET_TEXTSIZE + 1];
2513 int text_length = strlen(gi->textarea.value);
2514 int area_ysize = gi->textarea.ysize;
2515 int cursor_x_pref = gi->textarea.cursor_x_preferred;
2516 int cursor_x = gi->textarea.cursor_x;
2517 int cursor_y = gi->textarea.cursor_y;
2518 int cursor_pos = gi->textarea.cursor_position;
2519 char letter = getCharFromKey(key);
2520 boolean legal_letter = (letter != 0);
2524 insertCharIntoTextArea(gi, letter);
2526 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2528 else if (key == KSYM_Left && cursor_pos > 0)
2530 setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1);
2532 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2534 else if (key == KSYM_Right && cursor_pos < text_length)
2536 setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1);
2538 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2540 else if (key == KSYM_Up && cursor_y > 0)
2542 setTextAreaCursorXY(gi, cursor_x_pref, cursor_y - 1);
2543 gi->textarea.cursor_x_preferred = cursor_x_pref;
2545 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2547 else if (key == KSYM_Down && cursor_y < area_ysize - 1)
2549 setTextAreaCursorXY(gi, cursor_x_pref, cursor_y + 1);
2550 gi->textarea.cursor_x_preferred = cursor_x_pref;
2552 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2554 else if (key == KSYM_BackSpace && cursor_pos > 0)
2556 strcpy(text, gi->textarea.value);
2557 strcpy(&gi->textarea.value[cursor_pos - 1], &text[cursor_pos]);
2559 setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1);
2561 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2563 else if (key == KSYM_Delete && cursor_pos < text_length)
2565 strcpy(text, gi->textarea.value);
2566 strcpy(&gi->textarea.value[cursor_pos], &text[cursor_pos + 1]);
2568 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2570 else if (key == KSYM_Home && cursor_x > 0)
2572 setTextAreaCursorPosition(gi, gi->textarea.cursor_position - cursor_x);
2574 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2576 else if (key == KSYM_End && cursor_pos < text_length)
2578 int last_cursor_pos = cursor_pos;
2580 while (gi->textarea.value[cursor_pos] != '\0' &&
2581 gi->textarea.value[cursor_pos] != '\n')
2584 setTextAreaCursorPosition(gi, gi->textarea.cursor_position + cursor_pos -
2587 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2590 else if (gi->type & GD_TYPE_SELECTBOX) // only valid for selectbox
2592 int index = gi->selectbox.current_index;
2593 int num_values = gi->selectbox.num_values;
2595 if (key == KSYM_Up && index > 0)
2597 gi->selectbox.current_index--;
2599 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2601 else if (key == KSYM_Down && index < num_values - 1)
2603 gi->selectbox.current_index++;
2605 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2612 void DumpGadgetIdentifiers(void)
2614 struct GadgetInfo *gi;
2616 Print("Gadgets on current screen:\n");
2618 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
2620 if (gi->mapped && gi->image_id != -1)
2622 char *token = getTokenFromImageID(gi->image_id);
2623 char *prefix = "gfx.";
2625 if (strPrefix(token, prefix))
2626 token = &token[strlen(prefix)];
2628 Print("- '%s'\n", token);
2635 boolean DoGadgetAction(int image_id)
2637 struct GadgetInfo *gi;
2639 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
2641 if (gi->mapped && gi->image_id == image_id)
2643 gi->callback_action(gi);