1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
20 /* values for DrawGadget() */
21 #define DG_UNPRESSED 0
27 #define OPTION_TEXT_SELECTABLE(g, t) \
28 (t[0] != g->selectbox.char_unselectable && \
31 #define CURRENT_OPTION_SELECTABLE(g) \
32 OPTION_TEXT_SELECTABLE(g, g->selectbox.options[g->selectbox.current_index].text)
35 static struct GadgetInfo *gadget_list_first_entry = NULL;
36 static struct GadgetInfo *gadget_list_last_entry = NULL;
37 static struct GadgetInfo *last_info_gi = NULL;
38 static int next_free_gadget_id = 1;
39 static boolean gadget_id_wrapped = FALSE;
41 static void (*PlayGadgetSoundActivating)(void) = NULL;
42 static void (*PlayGadgetSoundSelecting)(void) = NULL;
45 void InitGadgetsSoundCallback(void (*activating_function)(void),
46 void (*selecting_function)(void))
48 PlayGadgetSoundActivating = activating_function;
49 PlayGadgetSoundSelecting = selecting_function;
52 static struct GadgetInfo *getGadgetInfoFromGadgetID(int id)
54 struct GadgetInfo *gi = gadget_list_first_entry;
56 while (gi && gi->id != id)
62 static int getNewGadgetID()
64 int id = next_free_gadget_id++;
66 if (next_free_gadget_id <= 0) /* counter overrun */
68 gadget_id_wrapped = TRUE; /* now we must check each ID */
69 next_free_gadget_id = 0;
72 if (gadget_id_wrapped)
74 next_free_gadget_id++;
75 while (getGadgetInfoFromGadgetID(next_free_gadget_id) != NULL)
76 next_free_gadget_id++;
79 if (next_free_gadget_id <= 0) /* cannot get new gadget id */
80 Error(ERR_EXIT, "too much gadgets -- this should not happen");
85 static struct GadgetInfo *getGadgetInfoFromMousePosition(int mx, int my,
88 struct GadgetInfo *gi;
90 /* first check for scrollbars in case of mouse scroll wheel button events */
91 if (IS_WHEEL_BUTTON(button))
93 /* real horizontal wheel or vertical wheel with modifier key pressed */
94 boolean check_horizontal = (IS_WHEEL_BUTTON_HORIZONTAL(button) ||
95 GetKeyModState() & KMOD_Shift);
97 /* check for the first active scrollbar directly under the mouse pointer */
98 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
100 if (gi->mapped && gi->active &&
101 (gi->type & GD_TYPE_SCROLLBAR) &&
102 mx >= gi->x && mx < gi->x + gi->width &&
103 my >= gi->y && my < gi->y + gi->height)
107 /* check for the first active scrollbar with matching mouse wheel area */
108 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
110 if (gi->mapped && gi->active &&
111 ((gi->type & GD_TYPE_SCROLLBAR_HORIZONTAL && check_horizontal) ||
112 (gi->type & GD_TYPE_SCROLLBAR_VERTICAL && !check_horizontal)) &&
113 mx >= gi->wheelarea.x && mx < gi->wheelarea.x + gi->wheelarea.width &&
114 my >= gi->wheelarea.y && my < gi->wheelarea.y + gi->wheelarea.height)
118 /* no active scrollbar found -- ignore this scroll wheel button event */
122 /* open selectboxes may overlap other active gadgets, so check them first */
123 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
125 if (gi->mapped && gi->active &&
126 gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open &&
127 mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width &&
128 my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
132 /* check all other gadgets */
133 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
135 if (gi->mapped && gi->active &&
136 mx >= gi->x && mx < gi->x + gi->width &&
137 my >= gi->y && my < gi->y + gi->height)
144 static void setTextAreaCursorExt(struct GadgetInfo *gi, boolean set_cursor_pos)
146 char *text = gi->textarea.value;
147 int area_xsize = gi->textarea.xsize;
148 int area_ysize = gi->textarea.ysize;
149 int cursor_position = gi->textarea.cursor_position;
150 int cursor_x = gi->textarea.cursor_x;
151 int cursor_y = gi->textarea.cursor_y;
158 if (set_cursor_pos) /* x/y => position */
160 if (y == cursor_y && (x == cursor_x || (x < cursor_x && *text == '\n')))
163 else /* position => x/y */
165 if (pos == cursor_position)
169 if (x + 1 >= area_xsize || *text == '\n')
171 if (y + 1 >= area_ysize)
184 gi->textarea.cursor_x = x;
185 gi->textarea.cursor_y = y;
186 gi->textarea.cursor_x_preferred = x;
187 gi->textarea.cursor_position = pos;
190 static void setTextAreaCursorXY(struct GadgetInfo *gi, int x, int y)
192 gi->textarea.cursor_x = x;
193 gi->textarea.cursor_y = y;
195 setTextAreaCursorExt(gi, TRUE);
198 static void setTextAreaCursorPosition(struct GadgetInfo *gi, int pos)
200 gi->textarea.cursor_position = pos;
202 setTextAreaCursorExt(gi, FALSE);
205 static void default_callback_info(void *ptr)
210 static void default_callback_action(void *ptr)
215 static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct)
217 struct GadgetDesign *gd;
218 int state = (pressed ? GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
219 boolean redraw_selectbox = FALSE;
221 if (gi == NULL || gi->deactivated)
224 gd = (!gi->active ? &gi->alt_design[state] :
225 gi->checked ? &gi->alt_design[state] : &gi->design[state]);
229 case GD_TYPE_NORMAL_BUTTON:
230 case GD_TYPE_CHECK_BUTTON:
231 case GD_TYPE_RADIO_BUTTON:
233 BlitBitmapOnBackground(gd->bitmap, drawto,
234 gd->x, gd->y, gi->width, gi->height,
237 if (gi->deco.design.bitmap)
239 // make sure that decoration does not overlap gadget border
240 int deco_x = gi->deco.x + (pressed ? gi->deco.xshift : 0);
241 int deco_y = gi->deco.y + (pressed ? gi->deco.yshift : 0);
242 int deco_width = MIN(gi->deco.width, gi->width - deco_x);
243 int deco_height = MIN(gi->deco.height, gi->height - deco_y);
246 BlitBitmapMasked(gi->deco.design.bitmap, drawto,
247 gi->deco.design.x, gi->deco.design.y,
248 deco_width, deco_height,
249 gi->x + deco_x, gi->y + deco_y);
251 BlitBitmap(gi->deco.design.bitmap, drawto,
252 gi->deco.design.x, gi->deco.design.y,
253 deco_width, deco_height,
254 gi->x + deco_x, gi->y + deco_y);
259 case GD_TYPE_TEXT_BUTTON:
262 int font_nr = (gi->active ? gi->font_active : gi->font);
263 int font_width = getFontWidth(font_nr);
264 int border_x = gi->border.xsize;
265 int border_y = gi->border.ysize;
266 int text_size = strlen(gi->textbutton.value);
267 int text_start = (gi->width - text_size * font_width) / 2;
269 /* left part of gadget */
270 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
271 border_x, gi->height, gi->x, gi->y);
273 /* middle part of gadget */
274 for (i=0; i < gi->textbutton.size; i++)
275 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
276 font_width, gi->height,
277 gi->x + border_x + i * font_width, gi->y);
279 /* right part of gadget */
280 BlitBitmapOnBackground(gd->bitmap, drawto,
281 gd->x + gi->border.width - border_x, gd->y,
282 border_x, gi->height,
283 gi->x + gi->width - border_x, gi->y);
285 /* gadget text value */
287 gi->x + text_start + (pressed ? gi->deco.xshift : 0),
288 gi->y + border_y + (pressed ? gi->deco.yshift : 0),
289 gi->textbutton.value, font_nr, BLIT_MASKED);
293 case GD_TYPE_TEXT_INPUT_ALPHANUMERIC:
294 case GD_TYPE_TEXT_INPUT_NUMERIC:
298 char cursor_string[2];
299 char text[MAX_GADGET_TEXTSIZE + 1];
300 int font_nr = (pressed ? gi->font_active : gi->font);
301 int font_width = getFontWidth(font_nr);
302 int border_x = gi->border.xsize;
303 int border_y = gi->border.ysize;
305 /* left part of gadget */
306 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
307 border_x, gi->height, gi->x, gi->y);
309 /* middle part of gadget */
310 for (i=0; i < gi->textinput.size + 1; i++)
311 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
312 font_width, gi->height,
313 gi->x + border_x + i * font_width, gi->y);
315 /* right part of gadget */
316 BlitBitmapOnBackground(gd->bitmap, drawto,
317 gd->x + gi->border.width - border_x, gd->y,
318 border_x, gi->height,
319 gi->x + gi->width - border_x, gi->y);
322 strcpy(text, gi->textinput.value);
325 /* gadget text value */
327 gi->x + border_x, gi->y + border_y, text,
328 font_nr, BLIT_MASKED);
330 cursor_letter = gi->textinput.value[gi->textinput.cursor_position];
331 cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' ');
332 cursor_string[1] = '\0';
334 /* draw cursor, if active */
338 gi->textinput.cursor_position * font_width,
339 gi->y + border_y, cursor_string,
340 font_nr, BLIT_INVERSE);
344 case GD_TYPE_TEXT_AREA:
348 char cursor_string[2];
349 int font_nr = (pressed ? gi->font_active : gi->font);
350 int font_width = getFontWidth(font_nr);
351 int font_height = getFontHeight(font_nr);
352 int border_x = gi->border.xsize;
353 int border_y = gi->border.ysize;
354 int gd_height = 2 * border_y + font_height;
356 /* top left part of gadget border */
357 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
358 border_x, border_y, gi->x, gi->y);
360 /* top middle part of gadget border */
361 for (i=0; i < gi->textarea.xsize; i++)
362 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
363 font_width, border_y,
364 gi->x + border_x + i * font_width, gi->y);
366 /* top right part of gadget border */
367 BlitBitmapOnBackground(gd->bitmap, drawto,
368 gd->x + gi->border.width - border_x, gd->y,
370 gi->x + gi->width - border_x, gi->y);
372 /* left and right part of gadget border for each row */
373 for (i=0; i < gi->textarea.ysize; i++)
375 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y,
376 border_x, font_height,
377 gi->x, gi->y + border_y + i * font_height);
378 BlitBitmapOnBackground(gd->bitmap, drawto,
379 gd->x + gi->border.width - border_x,
381 border_x, font_height,
382 gi->x + gi->width - border_x,
383 gi->y + border_y + i * font_height);
386 /* bottom left part of gadget border */
387 BlitBitmapOnBackground(gd->bitmap, drawto,
388 gd->x, gd->y + gd_height - border_y,
390 gi->x, gi->y + gi->height - border_y);
392 /* bottom middle part of gadget border */
393 for (i=0; i < gi->textarea.xsize; i++)
394 BlitBitmapOnBackground(gd->bitmap, drawto,
396 gd->y + gd_height - border_y,
397 font_width, border_y,
398 gi->x + border_x + i * font_width,
399 gi->y + gi->height - border_y);
401 /* bottom right part of gadget border */
402 BlitBitmapOnBackground(gd->bitmap, drawto,
403 gd->x + gi->border.width - border_x,
404 gd->y + gd_height - border_y,
406 gi->x + gi->width - border_x,
407 gi->y + gi->height - border_y);
409 ClearRectangleOnBackground(drawto,
412 gi->width - 2 * border_x,
413 gi->height - 2 * border_y);
415 /* gadget text value */
416 DrawTextBuffer(gi->x + border_x, gi->y + border_y, gi->textarea.value,
417 font_nr, gi->textarea.xsize, -1, gi->textarea.ysize, 0,
418 BLIT_ON_BACKGROUND, FALSE, FALSE, FALSE);
420 cursor_letter = gi->textarea.value[gi->textarea.cursor_position];
421 cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' ');
422 cursor_string[1] = '\0';
424 /* draw cursor, if active */
427 gi->x + border_x + gi->textarea.cursor_x * font_width,
428 gi->y + border_y + gi->textarea.cursor_y * font_height,
430 font_nr, BLIT_INVERSE);
434 case GD_TYPE_SELECTBOX:
437 char text[MAX_GADGET_TEXTSIZE + 1];
438 int font_nr_default = (pressed ? gi->font_active : gi->font);
439 int font_nr = font_nr_default;
440 int font_width = getFontWidth(font_nr);
441 int font_height = getFontHeight(font_nr);
442 int border_x = gi->border.xsize;
443 int border_y = gi->border.ysize;
444 int button = gi->border.xsize_selectbutton;
445 int width_inner = gi->border.width - button - 2 * border_x;
446 int box_width = gi->selectbox.width;
447 int box_height = gi->selectbox.height;
449 /* left part of gadget */
450 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
451 border_x, gi->height, gi->x, gi->y);
453 /* middle part of gadget */
454 for (i=0; i < gi->selectbox.size; i++)
455 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
456 font_width, gi->height,
457 gi->x + border_x + i * font_width, gi->y);
459 /* button part of gadget */
460 BlitBitmapOnBackground(gd->bitmap, drawto,
461 gd->x + border_x + width_inner, gd->y,
463 gi->x + gi->width - border_x - button, gi->y);
465 /* right part of gadget */
466 BlitBitmapOnBackground(gd->bitmap, drawto,
467 gd->x + gi->border.width - border_x, gd->y,
468 border_x, gi->height,
469 gi->x + gi->width - border_x, gi->y);
472 strncpy(text, gi->selectbox.options[gi->selectbox.index].text,
474 text[gi->selectbox.size] = '\0';
477 font_nr = (OPTION_TEXT_SELECTABLE(gi, text) ? font_nr_default :
478 gi->font_unselectable);
480 /* gadget text value */
481 DrawTextExt(drawto, gi->x + border_x, gi->y + border_y, text,
482 font_nr, BLIT_MASKED);
486 if (!gi->selectbox.open)
488 gi->selectbox.open = TRUE;
489 gi->selectbox.stay_open = FALSE;
490 gi->selectbox.current_index = gi->selectbox.index;
492 /* save background under selectbox */
493 BlitBitmap(drawto, gfx.field_save_buffer,
494 gi->selectbox.x, gi->selectbox.y,
495 gi->selectbox.width, gi->selectbox.height,
496 gi->selectbox.x, gi->selectbox.y);
499 /* draw open selectbox */
501 /* top left part of gadget border */
502 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
504 gi->selectbox.x, gi->selectbox.y);
506 /* top middle part of gadget border */
507 for (i=0; i < gi->selectbox.size; i++)
508 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
509 font_width, border_y,
510 gi->selectbox.x + border_x + i * font_width,
513 /* top button part of gadget border */
514 BlitBitmapOnBackground(gd->bitmap, drawto,
515 gd->x + border_x + width_inner, gd->y,
517 gi->selectbox.x + box_width -border_x -button,
520 /* top right part of gadget border */
521 BlitBitmapOnBackground(gd->bitmap, drawto,
522 gd->x + gi->border.width - border_x, gd->y,
524 gi->selectbox.x + box_width - border_x,
527 /* left and right part of gadget border for each row */
528 for (i=0; i < gi->selectbox.num_values; i++)
530 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y,
531 border_x, font_height,
533 gi->selectbox.y + border_y + i*font_height);
534 BlitBitmapOnBackground(gd->bitmap, drawto,
535 gd->x + gi->border.width - border_x,
537 border_x, font_height,
538 gi->selectbox.x + box_width - border_x,
539 gi->selectbox.y + border_y + i*font_height);
542 /* bottom left part of gadget border */
543 BlitBitmapOnBackground(gd->bitmap, drawto,
544 gd->x, gd->y + gi->height - border_y,
547 gi->selectbox.y + box_height - border_y);
549 /* bottom middle part of gadget border */
550 for (i=0; i < gi->selectbox.size; i++)
551 BlitBitmapOnBackground(gd->bitmap, drawto,
553 gd->y + gi->height - border_y,
554 font_width, border_y,
555 gi->selectbox.x + border_x + i * font_width,
556 gi->selectbox.y + box_height - border_y);
558 /* bottom button part of gadget border */
559 BlitBitmapOnBackground(gd->bitmap, drawto,
560 gd->x + border_x + width_inner,
561 gd->y + gi->height - border_y,
563 gi->selectbox.x + box_width -border_x -button,
564 gi->selectbox.y + box_height - border_y);
566 /* bottom right part of gadget border */
567 BlitBitmapOnBackground(gd->bitmap, drawto,
568 gd->x + gi->border.width - border_x,
569 gd->y + gi->height - border_y,
571 gi->selectbox.x + box_width - border_x,
572 gi->selectbox.y + box_height - border_y);
574 ClearRectangleOnBackground(drawto,
575 gi->selectbox.x + border_x,
576 gi->selectbox.y + border_y,
577 gi->selectbox.width - 2 * border_x,
578 gi->selectbox.height - 2 * border_y);
580 /* selectbox text values */
581 for (i=0; i < gi->selectbox.num_values; i++)
583 int mask_mode = BLIT_MASKED;
585 strncpy(text, gi->selectbox.options[i].text, gi->selectbox.size);
586 text[gi->selectbox.size] = '\0';
588 font_nr = (OPTION_TEXT_SELECTABLE(gi, text) ? font_nr_default :
589 gi->font_unselectable);
591 if (i == gi->selectbox.current_index &&
592 OPTION_TEXT_SELECTABLE(gi, text))
594 FillRectangle(drawto,
595 gi->selectbox.x + border_x,
596 gi->selectbox.y + border_y + i * font_height,
597 gi->selectbox.width - 2 * border_x, font_height,
598 gi->selectbox.inverse_color);
600 /* prevent use of cursor graphic by drawing at least two chars */
602 text[gi->selectbox.size] = '\0';
604 mask_mode = BLIT_INVERSE;
608 gi->selectbox.x + border_x,
609 gi->selectbox.y + border_y + i * font_height, text,
613 redraw_selectbox = TRUE;
615 else if (gi->selectbox.open)
617 gi->selectbox.open = FALSE;
619 /* restore background under selectbox */
620 BlitBitmap(gfx.field_save_buffer, drawto,
621 gi->selectbox.x, gi->selectbox.y,
622 gi->selectbox.width, gi->selectbox.height,
623 gi->selectbox.x, gi->selectbox.y);
625 /* redraw closed selectbox */
626 DrawGadget(gi, FALSE, FALSE);
628 redraw_selectbox = TRUE;
633 case GD_TYPE_SCROLLBAR_VERTICAL:
637 int ypos = gi->y + gi->scrollbar.position;
638 int design_full = gi->width;
639 int design_body = design_full - 2 * gi->border.ysize;
640 int size_full = gi->scrollbar.size;
641 int size_body = size_full - 2 * gi->border.ysize;
642 int num_steps = size_body / design_body;
643 int step_size_remain = size_body - num_steps * design_body;
645 /* clear scrollbar area */
646 ClearRectangleOnBackground(backbuffer, gi->x, gi->y,
647 gi->width, gi->height);
649 /* upper part of gadget */
650 BlitBitmapOnBackground(gd->bitmap, drawto,
652 gi->width, gi->border.ysize,
655 /* middle part of gadget */
656 for (i=0; i < num_steps; i++)
657 BlitBitmapOnBackground(gd->bitmap, drawto,
658 gd->x, gd->y + gi->border.ysize,
659 gi->width, design_body,
661 ypos + gi->border.ysize + i * design_body);
663 /* remaining middle part of gadget */
664 if (step_size_remain > 0)
665 BlitBitmapOnBackground(gd->bitmap, drawto,
666 gd->x, gd->y + gi->border.ysize,
667 gi->width, step_size_remain,
669 ypos + gi->border.ysize
670 + num_steps * design_body);
672 /* lower part of gadget */
673 BlitBitmapOnBackground(gd->bitmap, drawto,
674 gd->x, gd->y + design_full - gi->border.ysize,
675 gi->width, gi->border.ysize,
676 xpos, ypos + size_full - gi->border.ysize);
680 case GD_TYPE_SCROLLBAR_HORIZONTAL:
683 int xpos = gi->x + gi->scrollbar.position;
685 int design_full = gi->height;
686 int design_body = design_full - 2 * gi->border.xsize;
687 int size_full = gi->scrollbar.size;
688 int size_body = size_full - 2 * gi->border.xsize;
689 int num_steps = size_body / design_body;
690 int step_size_remain = size_body - num_steps * design_body;
692 /* clear scrollbar area */
693 ClearRectangleOnBackground(backbuffer, gi->x, gi->y,
694 gi->width, gi->height);
696 /* left part of gadget */
697 BlitBitmapOnBackground(gd->bitmap, drawto,
699 gi->border.xsize, gi->height,
702 /* middle part of gadget */
703 for (i=0; i < num_steps; i++)
704 BlitBitmapOnBackground(gd->bitmap, drawto,
705 gd->x + gi->border.xsize, gd->y,
706 design_body, gi->height,
707 xpos + gi->border.xsize + i * design_body,
710 /* remaining middle part of gadget */
711 if (step_size_remain > 0)
712 BlitBitmapOnBackground(gd->bitmap, drawto,
713 gd->x + gi->border.xsize, gd->y,
714 step_size_remain, gi->height,
715 xpos + gi->border.xsize
716 + num_steps * design_body,
719 /* right part of gadget */
720 BlitBitmapOnBackground(gd->bitmap, drawto,
721 gd->x + design_full - gi->border.xsize, gd->y,
722 gi->border.xsize, gi->height,
723 xpos + size_full - gi->border.xsize, ypos);
731 // do not use direct gadget drawing anymore; this worked as a speed-up once,
732 // but would slow things down a lot now the screen is always fully redrawn
737 BlitBitmap(drawto, window,
738 gi->x, gi->y, gi->width, gi->height, gi->x, gi->y);
740 if (gi->type == GD_TYPE_SELECTBOX && redraw_selectbox)
741 BlitBitmap(drawto, window,
742 gi->selectbox.x, gi->selectbox.y,
743 gi->selectbox.width, gi->selectbox.height,
744 gi->selectbox.x, gi->selectbox.y);
751 redraw_mask |= (IN_GFX_FIELD_FULL(x, y) ? REDRAW_FIELD :
752 IN_GFX_DOOR_1(x, y) ? REDRAW_DOOR_1 :
753 IN_GFX_DOOR_2(x, y) ? REDRAW_DOOR_2 :
754 IN_GFX_DOOR_3(x, y) ? REDRAW_DOOR_3 : REDRAW_ALL);
758 static int get_minimal_size_for_numeric_input(int minmax_value)
760 int min_size = 1; /* value needs at least one digit */
763 /* add number of digits needed for absolute value */
764 for (i = 10; i <= ABS(minmax_value); i *= 10)
767 /* if min/max value is negative, add one digit for minus sign */
768 if (minmax_value < 0)
774 static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap)
778 if (gi == NULL || gi->deactivated)
781 while (tag != GDI_END)
786 gi->custom_id = va_arg(ap, int);
789 case GDI_CUSTOM_TYPE_ID:
790 gi->custom_type_id = va_arg(ap, int);
795 int max_textsize = MAX_INFO_TEXTSIZE;
796 char *text = va_arg(ap, char *);
799 strncpy(gi->info_text, text, max_textsize);
803 gi->info_text[max_textsize] = '\0';
808 gi->x = va_arg(ap, int);
812 gi->y = va_arg(ap, int);
816 gi->width = va_arg(ap, int);
820 gi->height = va_arg(ap, int);
824 gi->type = va_arg(ap, unsigned int);
828 gi->state = va_arg(ap, unsigned int);
832 gi->active = (boolean)va_arg(ap, int);
835 case GDI_DIRECT_DRAW:
836 gi->direct_draw = (boolean)va_arg(ap, int);
840 gi->checked = (boolean)va_arg(ap, int);
844 gi->radio_nr = va_arg(ap, unsigned int);
847 case GDI_NUMBER_VALUE:
848 gi->textinput.number_value = va_arg(ap, int);
849 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
850 strcpy(gi->textinput.last_value, gi->textinput.value);
851 gi->textinput.cursor_position = strlen(gi->textinput.value);
855 gi->textinput.number_min = va_arg(ap, int);
856 if (gi->textinput.number_value < gi->textinput.number_min)
858 gi->textinput.number_value = gi->textinput.number_min;
859 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
860 strcpy(gi->textinput.last_value, gi->textinput.value);
865 gi->textinput.number_max = va_arg(ap, int);
866 if (gi->textinput.number_value > gi->textinput.number_max)
868 gi->textinput.number_value = gi->textinput.number_max;
869 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
870 strcpy(gi->textinput.last_value, gi->textinput.value);
876 int max_textsize = MAX_GADGET_TEXTSIZE;
878 if (gi->textinput.size)
879 max_textsize = MIN(gi->textinput.size, MAX_GADGET_TEXTSIZE);
881 strncpy(gi->textinput.value, va_arg(ap, char *), max_textsize);
882 strcpy(gi->textinput.last_value, gi->textinput.value);
884 gi->textinput.value[max_textsize] = '\0';
885 gi->textinput.cursor_position = strlen(gi->textinput.value);
887 /* same tag also used for other gadget definitions */
888 strcpy(gi->textbutton.value, gi->textinput.value);
889 strcpy(gi->textarea.value, gi->textinput.value);
890 strcpy(gi->textarea.last_value, gi->textinput.value);
896 int tag_value = va_arg(ap, int);
897 int max_textsize = MIN(tag_value, MAX_GADGET_TEXTSIZE);
899 gi->textinput.size = max_textsize;
900 gi->textinput.value[max_textsize] = '\0';
901 strcpy(gi->textinput.last_value, gi->textinput.value);
903 /* same tag also used for other gadget definitions */
905 gi->textarea.size = max_textsize;
906 gi->textarea.value[max_textsize] = '\0';
907 strcpy(gi->textarea.last_value, gi->textinput.value);
909 gi->textbutton.size = max_textsize;
910 gi->textbutton.value[max_textsize] = '\0';
912 gi->selectbox.size = gi->textinput.size;
917 gi->font = va_arg(ap, int);
918 if (gi->font_active == 0)
919 gi->font_active = gi->font;
920 if (gi->font_unselectable == 0)
921 gi->font_unselectable = gi->font;
924 case GDI_TEXT_FONT_ACTIVE:
925 gi->font_active = va_arg(ap, int);
928 case GDI_TEXT_FONT_UNSELECTABLE:
929 gi->font_unselectable = va_arg(ap, int);
932 case GDI_SELECTBOX_OPTIONS:
933 gi->selectbox.options = va_arg(ap, struct ValueTextInfo *);
936 case GDI_SELECTBOX_INDEX:
937 gi->selectbox.index = va_arg(ap, int);
940 case GDI_SELECTBOX_CHAR_UNSELECTABLE:
941 gi->selectbox.char_unselectable = (char)va_arg(ap, int);
944 case GDI_DESIGN_UNPRESSED:
945 gi->design[GD_BUTTON_UNPRESSED].bitmap = va_arg(ap, Bitmap *);
946 gi->design[GD_BUTTON_UNPRESSED].x = va_arg(ap, int);
947 gi->design[GD_BUTTON_UNPRESSED].y = va_arg(ap, int);
950 case GDI_DESIGN_PRESSED:
951 gi->design[GD_BUTTON_PRESSED].bitmap = va_arg(ap, Bitmap *);
952 gi->design[GD_BUTTON_PRESSED].x = va_arg(ap, int);
953 gi->design[GD_BUTTON_PRESSED].y = va_arg(ap, int);
956 case GDI_ALT_DESIGN_UNPRESSED:
957 gi->alt_design[GD_BUTTON_UNPRESSED].bitmap= va_arg(ap, Bitmap *);
958 gi->alt_design[GD_BUTTON_UNPRESSED].x = va_arg(ap, int);
959 gi->alt_design[GD_BUTTON_UNPRESSED].y = va_arg(ap, int);
962 case GDI_ALT_DESIGN_PRESSED:
963 gi->alt_design[GD_BUTTON_PRESSED].bitmap = va_arg(ap, Bitmap *);
964 gi->alt_design[GD_BUTTON_PRESSED].x = va_arg(ap, int);
965 gi->alt_design[GD_BUTTON_PRESSED].y = va_arg(ap, int);
968 case GDI_BORDER_SIZE:
969 gi->border.xsize = va_arg(ap, int);
970 gi->border.ysize = va_arg(ap, int);
973 case GDI_BORDER_SIZE_SELECTBUTTON:
974 gi->border.xsize_selectbutton = va_arg(ap, int);
977 case GDI_DESIGN_WIDTH:
978 gi->border.width = va_arg(ap, int);
981 case GDI_DECORATION_DESIGN:
982 gi->deco.design.bitmap = va_arg(ap, Bitmap *);
983 gi->deco.design.x = va_arg(ap, int);
984 gi->deco.design.y = va_arg(ap, int);
987 case GDI_DECORATION_POSITION:
988 gi->deco.x = va_arg(ap, int);
989 gi->deco.y = va_arg(ap, int);
992 case GDI_DECORATION_SIZE:
993 gi->deco.width = va_arg(ap, int);
994 gi->deco.height = va_arg(ap, int);
997 case GDI_DECORATION_SHIFTING:
998 gi->deco.xshift = va_arg(ap, int);
999 gi->deco.yshift = va_arg(ap, int);
1002 case GDI_DECORATION_MASKED:
1003 gi->deco.masked = (boolean)va_arg(ap, int);
1006 case GDI_EVENT_MASK:
1007 gi->event_mask = va_arg(ap, unsigned int);
1011 gi->drawing.area_xsize = va_arg(ap, int);
1012 gi->drawing.area_ysize = va_arg(ap, int);
1014 /* determine dependent values for drawing area gadget, if needed */
1015 if (gi->drawing.item_xsize != 0 && gi->drawing.item_ysize != 0)
1017 gi->width = gi->drawing.area_xsize * gi->drawing.item_xsize;
1018 gi->height = gi->drawing.area_ysize * gi->drawing.item_ysize;
1020 else if (gi->width != 0 && gi->height != 0)
1022 gi->drawing.item_xsize = gi->width / gi->drawing.area_xsize;
1023 gi->drawing.item_ysize = gi->height / gi->drawing.area_ysize;
1026 /* same tag also used for other gadget definitions */
1027 gi->textarea.xsize = gi->drawing.area_xsize;
1028 gi->textarea.ysize = gi->drawing.area_ysize;
1030 if (gi->type & GD_TYPE_TEXT_AREA) /* force recalculation */
1039 gi->drawing.item_xsize = va_arg(ap, int);
1040 gi->drawing.item_ysize = va_arg(ap, int);
1042 /* determine dependent values for drawing area gadget, if needed */
1043 if (gi->drawing.area_xsize != 0 && gi->drawing.area_ysize != 0)
1045 gi->width = gi->drawing.area_xsize * gi->drawing.item_xsize;
1046 gi->height = gi->drawing.area_ysize * gi->drawing.item_ysize;
1048 else if (gi->width != 0 && gi->height != 0)
1050 gi->drawing.area_xsize = gi->width / gi->drawing.item_xsize;
1051 gi->drawing.area_ysize = gi->height / gi->drawing.item_ysize;
1055 case GDI_SCROLLBAR_ITEMS_MAX:
1056 gi->scrollbar.items_max = va_arg(ap, int);
1059 case GDI_SCROLLBAR_ITEMS_VISIBLE:
1060 gi->scrollbar.items_visible = va_arg(ap, int);
1063 case GDI_SCROLLBAR_ITEM_POSITION:
1064 gi->scrollbar.item_position = va_arg(ap, int);
1067 case GDI_WHEEL_AREA_X:
1068 gi->wheelarea.x = va_arg(ap, int);
1071 case GDI_WHEEL_AREA_Y:
1072 gi->wheelarea.y = va_arg(ap, int);
1075 case GDI_WHEEL_AREA_WIDTH:
1076 gi->wheelarea.width = va_arg(ap, int);
1079 case GDI_WHEEL_AREA_HEIGHT:
1080 gi->wheelarea.height = va_arg(ap, int);
1083 case GDI_CALLBACK_INFO:
1084 gi->callback_info = va_arg(ap, gadget_function);
1087 case GDI_CALLBACK_ACTION:
1088 gi->callback_action = va_arg(ap, gadget_function);
1092 Error(ERR_EXIT, "HandleGadgetTags(): unknown tag %d", tag);
1095 tag = va_arg(ap, int); /* read next tag */
1098 gi->deactivated = FALSE;
1100 /* check if gadget has undefined bitmaps */
1101 if (gi->type != GD_TYPE_DRAWING_AREA &&
1102 (gi->design[GD_BUTTON_UNPRESSED].bitmap == NULL ||
1103 gi->design[GD_BUTTON_PRESSED].bitmap == NULL))
1104 gi->deactivated = TRUE;
1106 /* check if gadget is placed off-screen */
1107 if (gi->x < 0 || gi->y < 0)
1108 gi->deactivated = TRUE;
1110 /* adjust gadget values in relation to other gadget values */
1112 if (gi->type & GD_TYPE_TEXT_INPUT)
1114 int font_nr = gi->font_active;
1115 int font_width = getFontWidth(font_nr);
1116 int font_height = getFontHeight(font_nr);
1117 int border_xsize = gi->border.xsize;
1118 int border_ysize = gi->border.ysize;
1120 if (gi->type == GD_TYPE_TEXT_INPUT_NUMERIC)
1122 int number_min = gi->textinput.number_min;
1123 int number_max = gi->textinput.number_max;
1124 int min_size_min = get_minimal_size_for_numeric_input(number_min);
1125 int min_size_max = get_minimal_size_for_numeric_input(number_max);
1126 int min_size = MAX(min_size_min, min_size_max);
1128 /* expand gadget text input size, if maximal value is too large */
1129 if (gi->textinput.size < min_size)
1130 gi->textinput.size = min_size;
1133 gi->width = 2 * border_xsize + (gi->textinput.size + 1) * font_width;
1134 gi->height = 2 * border_ysize + font_height;
1137 if (gi->type & GD_TYPE_SELECTBOX)
1139 int font_nr = gi->font_active;
1140 int font_width = getFontWidth(font_nr);
1141 int font_height = getFontHeight(font_nr);
1142 int border_xsize = gi->border.xsize;
1143 int border_ysize = gi->border.ysize;
1144 int button_size = gi->border.xsize_selectbutton;
1145 int bottom_screen_border = gfx.sy + gfx.sysize - font_height;
1149 gi->width = 2 * border_xsize + gi->textinput.size*font_width +button_size;
1150 gi->height = 2 * border_ysize + font_height;
1152 if (gi->selectbox.options == NULL)
1153 Error(ERR_EXIT, "selectbox gadget incomplete (missing options array)");
1155 gi->selectbox.num_values = 0;
1156 while (gi->selectbox.options[gi->selectbox.num_values].text != NULL)
1157 gi->selectbox.num_values++;
1159 /* calculate values for open selectbox */
1160 gi->selectbox.width = gi->width;
1161 gi->selectbox.height =
1162 2 * border_ysize + gi->selectbox.num_values * font_height;
1164 gi->selectbox.x = gi->x;
1165 gi->selectbox.y = gi->y + gi->height;
1166 if (gi->selectbox.y + gi->selectbox.height > bottom_screen_border)
1167 gi->selectbox.y = gi->y - gi->selectbox.height;
1168 if (gi->selectbox.y < 0)
1169 gi->selectbox.y = bottom_screen_border - gi->selectbox.height;
1171 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &src_bitmap, &src_x, &src_y);
1172 src_x += font_width / 2;
1173 src_y += font_height / 2;
1175 /* there may be esoteric cases with missing or too small font bitmap */
1176 if (src_bitmap != NULL &&
1177 src_x < src_bitmap->width && src_y < src_bitmap->height)
1178 gi->selectbox.inverse_color = GetPixel(src_bitmap, src_x, src_y);
1180 /* always start with closed selectbox */
1181 gi->selectbox.open = FALSE;
1184 if (gi->type & GD_TYPE_TEXT_INPUT_NUMERIC)
1186 struct GadgetTextInput *text = &gi->textinput;
1187 int value = text->number_value;
1189 text->number_value = (value < text->number_min ? text->number_min :
1190 value > text->number_max ? text->number_max :
1193 sprintf(text->value, "%d", text->number_value);
1196 if (gi->type & GD_TYPE_TEXT_BUTTON)
1198 int font_nr = gi->font_active;
1199 int font_width = getFontWidth(font_nr);
1200 int font_height = getFontHeight(font_nr);
1201 int border_xsize = gi->border.xsize;
1202 int border_ysize = gi->border.ysize;
1204 gi->width = 2 * border_xsize + gi->textbutton.size * font_width;
1205 gi->height = 2 * border_ysize + font_height;
1208 if (gi->type & GD_TYPE_SCROLLBAR)
1210 struct GadgetScrollbar *gs = &gi->scrollbar;
1211 int scrollbar_size_cmp;
1213 if (gi->width == 0 || gi->height == 0 ||
1214 gs->items_max == 0 || gs->items_visible == 0)
1215 Error(ERR_EXIT, "scrollbar gadget incomplete (missing tags)");
1217 /* calculate internal scrollbar values */
1218 gs->size_min = (gi->type == GD_TYPE_SCROLLBAR_VERTICAL ?
1219 gi->width : gi->height);
1220 gs->size_max = (gi->type == GD_TYPE_SCROLLBAR_VERTICAL ?
1221 gi->height : gi->width);
1223 scrollbar_size_cmp = gs->size_max * gs->items_visible / gs->items_max;
1224 gs->size = MAX(scrollbar_size_cmp, gs->size_min);
1225 gs->size_max_cmp = (gs->size_max - (gs->size - scrollbar_size_cmp));
1227 gs->position = gs->size_max_cmp * gs->item_position / gs->items_max;
1228 gs->position_max = gs->size_max - gs->size;
1229 gs->correction = gs->size_max / gs->items_max / 2;
1231 /* finetuning for maximal right/bottom position */
1232 if (gs->item_position == gs->items_max - gs->items_visible)
1233 gs->position = gs->position_max;
1236 if (gi->type & GD_TYPE_TEXT_AREA)
1238 int font_nr = gi->font_active;
1239 int font_width = getFontWidth(font_nr);
1240 int font_height = getFontHeight(font_nr);
1241 int border_xsize = gi->border.xsize;
1242 int border_ysize = gi->border.ysize;
1244 if (gi->width == 0 || gi->height == 0)
1246 gi->width = 2 * border_xsize + gi->textarea.xsize * font_width;
1247 gi->height = 2 * border_ysize + gi->textarea.ysize * font_height;
1251 gi->textarea.xsize = (gi->width - 2 * border_xsize) / font_width;
1252 gi->textarea.ysize = (gi->height - 2 * border_ysize) / font_height;
1257 void ModifyGadget(struct GadgetInfo *gi, int first_tag, ...)
1261 va_start(ap, first_tag);
1262 HandleGadgetTags(gi, first_tag, ap);
1268 void RedrawGadget(struct GadgetInfo *gi)
1270 if (gi == NULL || gi->deactivated)
1274 DrawGadget(gi, gi->state, gi->direct_draw);
1277 struct GadgetInfo *CreateGadget(int first_tag, ...)
1279 struct GadgetInfo *new_gadget = checked_calloc(sizeof(struct GadgetInfo));
1282 /* always start with reliable default values */
1283 new_gadget->id = getNewGadgetID();
1284 new_gadget->callback_info = default_callback_info;
1285 new_gadget->callback_action = default_callback_action;
1286 new_gadget->active = TRUE;
1287 new_gadget->direct_draw = TRUE;
1289 new_gadget->next = NULL;
1291 va_start(ap, first_tag);
1292 HandleGadgetTags(new_gadget, first_tag, ap);
1295 /* insert new gadget into global gadget list */
1296 if (gadget_list_last_entry)
1298 gadget_list_last_entry->next = new_gadget;
1299 gadget_list_last_entry = gadget_list_last_entry->next;
1302 gadget_list_first_entry = gadget_list_last_entry = new_gadget;
1307 void FreeGadget(struct GadgetInfo *gi)
1309 struct GadgetInfo *gi_previous = gadget_list_first_entry;
1314 /* prevent "last_info_gi" from pointing to memory that will be freed */
1315 if (last_info_gi == gi)
1316 last_info_gi = NULL;
1318 while (gi_previous != NULL && gi_previous->next != gi)
1319 gi_previous = gi_previous->next;
1321 if (gi == gadget_list_first_entry)
1322 gadget_list_first_entry = gi->next;
1324 if (gi == gadget_list_last_entry)
1325 gadget_list_last_entry = gi_previous;
1327 if (gi_previous != NULL)
1328 gi_previous->next = gi->next;
1333 static void CheckRangeOfNumericInputGadget(struct GadgetInfo *gi)
1335 if (gi->type != GD_TYPE_TEXT_INPUT_NUMERIC)
1338 gi->textinput.number_value = atoi(gi->textinput.value);
1340 if (gi->textinput.number_value < gi->textinput.number_min)
1341 gi->textinput.number_value = gi->textinput.number_min;
1342 if (gi->textinput.number_value > gi->textinput.number_max)
1343 gi->textinput.number_value = gi->textinput.number_max;
1345 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
1347 if (gi->textinput.cursor_position < 0)
1348 gi->textinput.cursor_position = 0;
1349 else if (gi->textinput.cursor_position > strlen(gi->textinput.value))
1350 gi->textinput.cursor_position = strlen(gi->textinput.value);
1353 /* global pointer to gadget actually in use (when mouse button pressed) */
1354 static struct GadgetInfo *last_gi = NULL;
1356 static void MapGadgetExt(struct GadgetInfo *gi, boolean redraw)
1358 if (gi == NULL || gi->deactivated || gi->mapped)
1364 DrawGadget(gi, DG_UNPRESSED, DG_BUFFERED);
1367 void MapGadget(struct GadgetInfo *gi)
1369 MapGadgetExt(gi, TRUE);
1372 void UnmapGadget(struct GadgetInfo *gi)
1374 if (gi == NULL || gi->deactivated || !gi->mapped)
1383 #define MAX_NUM_GADGETS 1024
1384 #define MULTIMAP_UNMAP (1 << 0)
1385 #define MULTIMAP_REMAP (1 << 1)
1386 #define MULTIMAP_REDRAW (1 << 2)
1387 #define MULTIMAP_PLAYFIELD (1 << 3)
1388 #define MULTIMAP_DOOR_1 (1 << 4)
1389 #define MULTIMAP_DOOR_2 (1 << 5)
1390 #define MULTIMAP_DOOR_3 (1 << 6)
1391 #define MULTIMAP_ALL (MULTIMAP_PLAYFIELD | \
1396 static void MultiMapGadgets(int mode)
1398 struct GadgetInfo *gi = gadget_list_first_entry;
1399 static boolean map_state[MAX_NUM_GADGETS];
1407 if ((mode & MULTIMAP_PLAYFIELD && IN_GFX_FIELD_FULL(x, y)) ||
1408 (mode & MULTIMAP_DOOR_1 && IN_GFX_DOOR_1(x, y)) ||
1409 (mode & MULTIMAP_DOOR_2 && IN_GFX_DOOR_2(x, y)) ||
1410 (mode & MULTIMAP_DOOR_3 && IN_GFX_DOOR_3(x, y)) ||
1411 (mode & MULTIMAP_ALL) == MULTIMAP_ALL)
1413 if (mode & MULTIMAP_UNMAP)
1415 map_state[map_count++ % MAX_NUM_GADGETS] = gi->mapped;
1420 if (map_state[map_count++ % MAX_NUM_GADGETS])
1421 MapGadgetExt(gi, (mode & MULTIMAP_REDRAW));
1429 void UnmapAllGadgets()
1431 MultiMapGadgets(MULTIMAP_ALL | MULTIMAP_UNMAP);
1434 void RemapAllGadgets()
1436 MultiMapGadgets(MULTIMAP_ALL | MULTIMAP_REMAP);
1439 boolean anyTextInputGadgetActive()
1441 return (last_gi && (last_gi->type & GD_TYPE_TEXT_INPUT) && last_gi->mapped);
1444 boolean anyTextAreaGadgetActive()
1446 return (last_gi && (last_gi->type & GD_TYPE_TEXT_AREA) && last_gi->mapped);
1449 boolean anySelectboxGadgetActive()
1451 return (last_gi && (last_gi->type & GD_TYPE_SELECTBOX) && last_gi->mapped);
1454 boolean anyScrollbarGadgetActive()
1456 return (last_gi && (last_gi->type & GD_TYPE_SCROLLBAR) && last_gi->mapped);
1459 boolean anyTextGadgetActive()
1461 return (anyTextInputGadgetActive() ||
1462 anyTextAreaGadgetActive() ||
1463 anySelectboxGadgetActive());
1466 static boolean insideSelectboxLine(struct GadgetInfo *gi, int mx, int my)
1468 return (gi != NULL &&
1469 gi->type & GD_TYPE_SELECTBOX &&
1470 mx >= gi->x && mx < gi->x + gi->width &&
1471 my >= gi->y && my < gi->y + gi->height);
1474 static boolean insideSelectboxArea(struct GadgetInfo *gi, int mx, int my)
1476 return (gi != NULL &&
1477 gi->type & GD_TYPE_SELECTBOX &&
1478 mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width &&
1479 my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height);
1482 void ClickOnGadget(struct GadgetInfo *gi, int button)
1487 /* simulate releasing mouse button over last gadget, if still pressed */
1489 HandleGadgets(-1, -1, 0);
1491 /* simulate pressing mouse button over specified gadget */
1492 HandleGadgets(gi->x, gi->y, button);
1494 /* simulate releasing mouse button over specified gadget */
1495 HandleGadgets(gi->x, gi->y, 0);
1498 boolean HandleGadgets(int mx, int my, int button)
1500 static unsigned int pressed_delay = 0;
1501 static unsigned int pressed_delay_value = GADGET_FRAME_DELAY;
1502 static int last_button = 0;
1503 static int last_mx = 0, last_my = 0;
1504 static int pressed_mx = 0, pressed_my = 0;
1505 static boolean keep_selectbox_open = FALSE;
1506 static boolean gadget_stopped = FALSE;
1507 int scrollbar_mouse_pos = 0;
1508 struct GadgetInfo *new_gi, *gi;
1509 boolean press_event;
1510 boolean release_event;
1511 boolean mouse_moving;
1512 boolean mouse_inside_select_line;
1513 boolean mouse_inside_select_area;
1514 boolean mouse_released_where_pressed;
1515 boolean gadget_pressed;
1516 boolean gadget_pressed_repeated;
1517 boolean gadget_pressed_off_borders;
1518 boolean gadget_pressed_inside_select_line;
1519 boolean gadget_pressed_delay_reached;
1520 boolean gadget_moving;
1521 boolean gadget_moving_inside;
1522 boolean gadget_moving_off_borders;
1523 boolean gadget_draggable;
1524 boolean gadget_dragging;
1525 boolean gadget_released;
1526 boolean gadget_released_inside;
1527 boolean gadget_released_inside_select_area;
1528 boolean gadget_released_off_borders;
1529 boolean changed_position = FALSE;
1531 /* check if there are any gadgets defined */
1532 if (gadget_list_first_entry == NULL)
1535 /* simulated release of mouse button over last gadget */
1536 if (mx == -1 && my == -1 && button == 0)
1542 /* check which gadget is under the mouse pointer */
1543 new_gi = getGadgetInfoFromMousePosition(mx, my, button);
1545 /* check if button state has changed since last invocation */
1546 press_event = (button != 0 && last_button == 0);
1547 release_event = (button == 0 && last_button != 0);
1548 last_button = button;
1550 /* check if mouse has been moved since last invocation */
1551 mouse_moving = ((mx != last_mx || my != last_my) && motion_status);
1555 if (press_event && new_gi != last_gi)
1561 mouse_released_where_pressed =
1562 (release_event && mx == pressed_mx && my == pressed_my);
1564 mouse_inside_select_line = insideSelectboxLine(new_gi, mx, my);
1565 mouse_inside_select_area = insideSelectboxArea(new_gi, mx, my);
1567 gadget_pressed_off_borders = (press_event && new_gi != last_gi);
1569 gadget_pressed_inside_select_line =
1570 (press_event && new_gi != NULL &&
1571 new_gi->type & GD_TYPE_SELECTBOX && new_gi->selectbox.open &&
1572 insideSelectboxLine(new_gi, mx, my));
1574 /* if mouse button pressed outside text or selectbox gadget, deactivate it */
1575 if (anyTextGadgetActive() &&
1576 (gadget_pressed_off_borders ||
1577 (gadget_pressed_inside_select_line && !mouse_inside_select_area)))
1579 struct GadgetInfo *gi = last_gi;
1580 boolean gadget_changed = ((gi->event_mask & GD_EVENT_TEXT_LEAVING) != 0);
1582 /* check if text gadget has changed its value */
1583 if (gi->type & GD_TYPE_TEXT_INPUT)
1585 CheckRangeOfNumericInputGadget(gi);
1587 if (!strEqual(gi->textinput.last_value, gi->textinput.value))
1588 strcpy(gi->textinput.last_value, gi->textinput.value);
1590 gadget_changed = FALSE;
1593 /* selectbox does not change its value when closed by clicking outside */
1594 if (gi->type & GD_TYPE_SELECTBOX)
1595 gadget_changed = FALSE;
1597 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1599 gi->event.type = GD_EVENT_TEXT_LEAVING;
1601 if (gadget_changed && !(gi->type & GD_TYPE_SELECTBOX))
1602 gi->callback_action(gi);
1606 if (gadget_pressed_inside_select_line)
1613 (button != 0 && last_gi == NULL && new_gi != NULL && press_event);
1614 gadget_pressed_repeated =
1615 (button != 0 && last_gi != NULL && new_gi == last_gi);
1617 gadget_pressed_delay_reached =
1618 DelayReached(&pressed_delay, pressed_delay_value);
1620 gadget_released = (release_event && last_gi != NULL);
1621 gadget_released_inside = (gadget_released && new_gi == last_gi);
1622 gadget_released_off_borders = (gadget_released && new_gi != last_gi);
1624 gadget_moving = (button != 0 && last_gi != NULL && mouse_moving);
1625 gadget_moving_inside = (gadget_moving && new_gi == last_gi);
1626 gadget_moving_off_borders = (gadget_moving && new_gi != last_gi);
1628 /* when handling selectbox, set additional state values */
1629 if (gadget_released_inside && (last_gi->type & GD_TYPE_SELECTBOX))
1630 gadget_released_inside_select_area = insideSelectboxArea(last_gi, mx, my);
1632 gadget_released_inside_select_area = FALSE;
1634 /* setting state for handling over-large selectbox */
1635 if (keep_selectbox_open && (press_event || !mouse_inside_select_line))
1636 keep_selectbox_open = FALSE;
1638 /* if new gadget pressed, store this gadget */
1642 /* 'gi' is actually handled gadget */
1645 /* if gadget is scrollbar, choose mouse position value */
1646 if (gi && gi->type & GD_TYPE_SCROLLBAR)
1647 scrollbar_mouse_pos =
1648 (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx - gi->x : my - gi->y);
1650 /* if mouse button released, no gadget needs to be handled anymore */
1651 if (gadget_released)
1653 if (gi->type & GD_TYPE_SELECTBOX &&
1654 (keep_selectbox_open ||
1655 mouse_released_where_pressed ||
1656 !gadget_released_inside_select_area ||
1657 !CURRENT_OPTION_SELECTABLE(gi))) /* selectbox stays open */
1659 gi->selectbox.stay_open = TRUE;
1663 else if (!(gi->type & GD_TYPE_TEXT_INPUT ||
1664 gi->type & GD_TYPE_TEXT_AREA)) /* text input stays open */
1668 /* modify event position values even if no gadget is pressed */
1669 if (button == 0 && !release_event)
1672 /* if new gadget or if no gadget was pressed, release stopped processing */
1673 if (gadget_pressed || new_gi == NULL)
1674 gadget_stopped = FALSE;
1676 /* if gadget was stopped while being handled, stop gadget processing here */
1682 int last_x = gi->event.x;
1683 int last_y = gi->event.y;
1684 int last_mx = gi->event.mx;
1685 int last_my = gi->event.my;
1687 gi->event.x = gi->event.mx = mx - gi->x;
1688 gi->event.y = gi->event.my = my - gi->y;
1690 if (gi->type == GD_TYPE_DRAWING_AREA)
1692 gi->event.x /= gi->drawing.item_xsize;
1693 gi->event.y /= gi->drawing.item_ysize;
1695 if (last_x != gi->event.x || last_y != gi->event.y ||
1696 ((last_mx != gi->event.mx || last_my != gi->event.my) &&
1697 gi->event_mask & GD_EVENT_PIXEL_PRECISE))
1698 changed_position = TRUE;
1700 else if (gi->type & GD_TYPE_TEXT_INPUT && button != 0 && !motion_status)
1702 int old_cursor_position = gi->textinput.cursor_position;
1704 /* if mouse button pressed inside activated text gadget, set cursor */
1705 gi->textinput.cursor_position =
1706 (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font);
1708 if (gi->textinput.cursor_position < 0)
1709 gi->textinput.cursor_position = 0;
1710 else if (gi->textinput.cursor_position > strlen(gi->textinput.value))
1711 gi->textinput.cursor_position = strlen(gi->textinput.value);
1713 if (gi->textinput.cursor_position != old_cursor_position)
1714 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1717 StartTextInput(gi->x, gi->y, gi->width, gi->height);
1719 else if (gi->type & GD_TYPE_TEXT_AREA && button != 0 && !motion_status)
1721 int old_cursor_position = gi->textarea.cursor_position;
1722 int x = (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font);
1723 int y = (my - gi->y - gi->border.ysize) / getFontHeight(gi->font);
1725 x = (x < 0 ? 0 : x >= gi->textarea.xsize ? gi->textarea.xsize - 1 : x);
1726 y = (y < 0 ? 0 : y >= gi->textarea.ysize ? gi->textarea.ysize - 1 : y);
1728 setTextAreaCursorXY(gi, x, y);
1730 if (gi->textarea.cursor_position != old_cursor_position)
1731 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1734 StartTextInput(gi->x, gi->y, gi->width, gi->height);
1736 else if (gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open &&
1737 !keep_selectbox_open)
1739 int old_index = gi->selectbox.current_index;
1741 /* if mouse moving inside activated selectbox, select value */
1742 if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
1743 gi->selectbox.current_index =
1744 (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font);
1746 if (gi->selectbox.current_index < 0)
1747 gi->selectbox.current_index = 0;
1748 else if (gi->selectbox.current_index > gi->selectbox.num_values - 1)
1749 gi->selectbox.current_index = gi->selectbox.num_values - 1;
1751 if (gi->selectbox.current_index != old_index)
1752 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1756 /* handle gadget popup info text */
1757 if (last_info_gi != new_gi ||
1758 (new_gi && new_gi->type == GD_TYPE_DRAWING_AREA && changed_position))
1760 if (new_gi != NULL && (button == 0 || new_gi == last_gi))
1762 new_gi->event.type = GD_EVENT_INFO_ENTERING;
1763 new_gi->callback_info(new_gi);
1765 else if (last_info_gi != NULL && last_info_gi->mapped)
1767 last_info_gi->event.type = GD_EVENT_INFO_LEAVING;
1768 last_info_gi->callback_info(last_info_gi);
1771 last_info_gi = new_gi;
1774 gadget_draggable = (gi && gi->type & GD_TYPE_SCROLLBAR);
1776 /* reset drag position for newly pressed scrollbar to "not dragging" */
1777 if (gadget_pressed && gadget_draggable)
1778 gi->scrollbar.drag_position = -1;
1780 gadget_dragging = (gadget_draggable && gi->scrollbar.drag_position != -1);
1782 /* clicking next to a scrollbar to move it is not considered "moving" */
1783 if (gadget_draggable && !gadget_dragging)
1784 gadget_moving = FALSE;
1786 /* when leaving scrollbar area when jump-scrolling, stop gadget processing */
1787 if (gadget_draggable && !gadget_dragging && gadget_moving_off_borders)
1788 gadget_stopped = TRUE;
1790 if ((gadget_pressed) ||
1791 (gadget_pressed_repeated && gadget_pressed_delay_reached))
1793 if (gadget_pressed) /* gadget pressed the first time */
1795 /* initialize delay counter */
1796 DelayReached(&pressed_delay, 0);
1798 /* start gadget delay with longer delay after first click on gadget */
1799 pressed_delay_value = GADGET_FRAME_DELAY_FIRST;
1801 else /* gadget hold pressed for some time */
1803 /* after first repeated gadget click, continue with shorter delay value */
1804 pressed_delay_value = GADGET_FRAME_DELAY;
1807 if (gi->type & GD_TYPE_SCROLLBAR && !gadget_dragging)
1809 int mpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx : my);
1810 int gpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? gi->x : gi->y);
1811 int slider_start = gpos + gi->scrollbar.position;
1812 int slider_end = gpos + gi->scrollbar.position + gi->scrollbar.size - 1;
1813 boolean inside_slider = (mpos >= slider_start && mpos <= slider_end);
1815 if (IS_WHEEL_BUTTON(button) || !inside_slider)
1817 /* click scrollbar one scrollbar length up/left or down/right */
1819 struct GadgetScrollbar *gs = &gi->scrollbar;
1820 int old_item_position = gs->item_position;
1821 int item_steps = gs->items_visible - 1;
1822 int item_direction = (mpos < gpos + gi->scrollbar.position ? -1 : +1);
1824 if (IS_WHEEL_BUTTON(button))
1826 boolean scroll_single_step = ((GetKeyModState() & KMOD_Alt) != 0);
1828 item_steps = (scroll_single_step ? 1 : wheel_steps);
1829 item_direction = (button == MB_WHEEL_UP ||
1830 button == MB_WHEEL_LEFT ? -1 : +1);
1833 changed_position = FALSE;
1835 gs->item_position += item_steps * item_direction;
1837 if (gs->item_position < 0)
1838 gs->item_position = 0;
1839 else if (gs->item_position > gs->items_max - gs->items_visible)
1840 gs->item_position = gs->items_max - gs->items_visible;
1842 if (old_item_position != gs->item_position)
1844 gi->event.item_position = gs->item_position;
1845 changed_position = TRUE;
1848 ModifyGadget(gi, GDI_SCROLLBAR_ITEM_POSITION, gs->item_position,
1851 gi->state = GD_BUTTON_UNPRESSED;
1852 gi->event.type = GD_EVENT_MOVING;
1853 gi->event.off_borders = FALSE;
1855 if (gi->event_mask & GD_EVENT_MOVING && changed_position)
1856 gi->callback_action(gi);
1862 /* don't handle this scrollbar anymore when mouse position reached */
1863 if (gadget_pressed_repeated)
1865 gadget_stopped = TRUE;
1875 PlayGadgetSoundActivating();
1877 if (gi->type == GD_TYPE_CHECK_BUTTON)
1879 gi->checked = !gi->checked;
1881 else if (gi->type == GD_TYPE_RADIO_BUTTON)
1883 struct GadgetInfo *rgi = gadget_list_first_entry;
1888 rgi->type == GD_TYPE_RADIO_BUTTON &&
1889 rgi->radio_nr == gi->radio_nr &&
1892 rgi->checked = FALSE;
1893 DrawGadget(rgi, DG_UNPRESSED, rgi->direct_draw);
1901 else if (gi->type & GD_TYPE_SCROLLBAR)
1903 int mpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx : my);
1904 int gpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? gi->x : gi->y);
1905 int slider_start = gpos + gi->scrollbar.position;
1906 int slider_end = gpos + gi->scrollbar.position + gi->scrollbar.size - 1;
1907 boolean inside_slider = (mpos >= slider_start && mpos <= slider_end);
1909 if (!IS_WHEEL_BUTTON(button) && inside_slider)
1911 /* start dragging scrollbar */
1912 gi->scrollbar.drag_position =
1913 scrollbar_mouse_pos - gi->scrollbar.position;
1916 else if (gi->type & GD_TYPE_SELECTBOX)
1918 /* keep selectbox open in case of over-large selectbox */
1919 keep_selectbox_open = (mouse_inside_select_line &&
1920 mouse_inside_select_area);
1923 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1925 gi->state = GD_BUTTON_PRESSED;
1926 gi->event.type = GD_EVENT_PRESSED;
1927 gi->event.button = button;
1928 gi->event.off_borders = FALSE;
1930 if (gi->event_mask & GD_EVENT_PRESSED)
1931 gi->callback_action(gi);
1934 if (gadget_pressed_repeated)
1936 gi->event.type = GD_EVENT_PRESSED;
1938 if (gi->event_mask & GD_EVENT_REPEATED && gadget_pressed_delay_reached)
1939 gi->callback_action(gi);
1944 if (gi->type & GD_TYPE_BUTTON)
1946 if (gadget_moving_inside && gi->state == GD_BUTTON_UNPRESSED)
1947 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1948 else if (gadget_moving_off_borders && gi->state == GD_BUTTON_PRESSED)
1949 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1951 else if (gi->type & GD_TYPE_SELECTBOX && !keep_selectbox_open)
1953 int old_index = gi->selectbox.current_index;
1955 /* if mouse moving inside activated selectbox, select value */
1956 if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
1957 gi->selectbox.current_index =
1958 (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font);
1960 if (gi->selectbox.current_index < 0)
1961 gi->selectbox.current_index = 0;
1962 else if (gi->selectbox.current_index > gi->selectbox.num_values - 1)
1963 gi->selectbox.current_index = gi->selectbox.num_values - 1;
1965 if (gi->selectbox.current_index != old_index)
1966 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1968 else if (gi->type & GD_TYPE_SCROLLBAR)
1970 struct GadgetScrollbar *gs = &gi->scrollbar;
1971 int old_item_position = gs->item_position;
1973 gs->position = scrollbar_mouse_pos - gs->drag_position;
1975 /* make sure to always precisely reach end positions when dragging */
1976 if (gs->position <= 0)
1979 gs->item_position = 0;
1981 else if (gs->position >= gs->position_max)
1983 gs->position = gs->position_max;
1984 gs->item_position = gs->items_max - gs->items_visible;
1989 gs->items_max * (gs->position + gs->correction) / gs->size_max_cmp;
1992 if (gs->item_position < 0)
1993 gs->item_position = 0;
1994 if (gs->item_position > gs->items_max - 1)
1995 gs->item_position = gs->items_max - 1;
1997 if (old_item_position != gs->item_position)
1999 gi->event.item_position = gs->item_position;
2000 changed_position = TRUE;
2003 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2006 gi->state = (gadget_moving_inside || gadget_draggable ?
2007 GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
2008 gi->event.type = GD_EVENT_MOVING;
2009 gi->event.off_borders = gadget_moving_off_borders;
2011 if (gi->event_mask & GD_EVENT_MOVING && changed_position &&
2012 (gadget_moving_inside || gi->event_mask & GD_EVENT_OFF_BORDERS))
2013 gi->callback_action(gi);
2016 if (gadget_released_inside)
2018 boolean deactivate_gadget = TRUE;
2019 boolean gadget_changed = TRUE;
2021 if (gi->type & GD_TYPE_SELECTBOX)
2023 if (keep_selectbox_open ||
2024 mouse_released_where_pressed ||
2025 !gadget_released_inside_select_area ||
2026 !CURRENT_OPTION_SELECTABLE(gi)) /* selectbox stays open */
2028 deactivate_gadget = FALSE;
2029 gadget_changed = FALSE;
2031 else if (gi->selectbox.index != gi->selectbox.current_index)
2032 gi->selectbox.index = gi->selectbox.current_index;
2034 gadget_changed = FALSE;
2037 if (deactivate_gadget &&
2038 !(gi->type & GD_TYPE_TEXT_INPUT ||
2039 gi->type & GD_TYPE_TEXT_AREA)) /* text input stays open */
2040 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2042 gi->state = GD_BUTTON_UNPRESSED;
2043 gi->event.type = GD_EVENT_RELEASED;
2045 if ((gi->event_mask & GD_EVENT_RELEASED) && gadget_changed)
2047 gi->callback_action(gi);
2051 if (gadget_released_off_borders)
2053 if (gi->type & GD_TYPE_SCROLLBAR)
2054 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2056 gi->state = GD_BUTTON_UNPRESSED;
2057 gi->event.type = GD_EVENT_RELEASED;
2059 if (gi->event_mask & GD_EVENT_RELEASED &&
2060 gi->event_mask & GD_EVENT_OFF_BORDERS)
2061 gi->callback_action(gi);
2064 /* handle gadgets unmapped/mapped between pressing and releasing */
2065 if (release_event && !gadget_released && new_gi)
2066 new_gi->state = GD_BUTTON_UNPRESSED;
2068 return (gadget_pressed || gadget_pressed_repeated ||
2069 gadget_released || gadget_moving);
2072 static void insertCharIntoTextArea(struct GadgetInfo *gi, char c)
2074 char text[MAX_GADGET_TEXTSIZE + 1];
2075 int cursor_position = gi->textarea.cursor_position;
2077 if (strlen(gi->textarea.value) >= MAX_GADGET_TEXTSIZE) /* no space left */
2080 strcpy(text, gi->textarea.value);
2081 strcpy(&gi->textarea.value[cursor_position + 1], &text[cursor_position]);
2082 gi->textarea.value[cursor_position] = c;
2084 setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1);
2087 boolean HandleGadgetsKeyInput(Key key)
2089 struct GadgetInfo *gi = last_gi;
2091 if (gi == NULL || gi->deactivated || !gi->mapped ||
2092 !(gi->type & GD_TYPE_TEXT_INPUT ||
2093 gi->type & GD_TYPE_TEXT_AREA ||
2094 gi->type & GD_TYPE_SELECTBOX))
2097 if (key == KSYM_Escape)
2101 else if (key == KSYM_Return) /* valid for both text input and selectbox */
2103 boolean gadget_changed = ((gi->event_mask & GD_EVENT_TEXT_RETURN) != 0);
2105 if (gi->type & GD_TYPE_TEXT_INPUT)
2107 CheckRangeOfNumericInputGadget(gi);
2109 if (!strEqual(gi->textinput.last_value, gi->textinput.value))
2110 strcpy(gi->textinput.last_value, gi->textinput.value);
2112 gadget_changed = FALSE;
2116 else if (gi->type & GD_TYPE_SELECTBOX)
2118 if (gi->selectbox.index != gi->selectbox.current_index)
2119 gi->selectbox.index = gi->selectbox.current_index;
2121 gadget_changed = FALSE;
2124 if (gi->type & GD_TYPE_TEXT_AREA)
2126 insertCharIntoTextArea(gi, '\n');
2128 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2132 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2134 gi->event.type = GD_EVENT_TEXT_RETURN;
2140 gi->callback_action(gi);
2142 else if (gi->type & GD_TYPE_TEXT_INPUT) /* only valid for text input */
2144 char text[MAX_GADGET_TEXTSIZE + 1];
2145 int text_length = strlen(gi->textinput.value);
2146 int cursor_pos = gi->textinput.cursor_position;
2147 char letter = getCharFromKey(key);
2148 boolean legal_letter = (gi->type == GD_TYPE_TEXT_INPUT_NUMERIC ?
2149 letter >= '0' && letter <= '9' :
2152 if (legal_letter && text_length < gi->textinput.size)
2154 strcpy(text, gi->textinput.value);
2155 strcpy(&gi->textinput.value[cursor_pos + 1], &text[cursor_pos]);
2156 gi->textinput.value[cursor_pos] = letter;
2157 gi->textinput.cursor_position++;
2159 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2161 else if (key == KSYM_Left && cursor_pos > 0)
2163 gi->textinput.cursor_position--;
2165 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2167 else if (key == KSYM_Right && cursor_pos < text_length)
2169 gi->textinput.cursor_position++;
2171 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2173 else if (key == KSYM_BackSpace && cursor_pos > 0)
2175 strcpy(text, gi->textinput.value);
2176 strcpy(&gi->textinput.value[cursor_pos - 1], &text[cursor_pos]);
2177 gi->textinput.cursor_position--;
2179 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2181 else if (key == KSYM_Delete && cursor_pos < text_length)
2183 strcpy(text, gi->textinput.value);
2184 strcpy(&gi->textinput.value[cursor_pos], &text[cursor_pos + 1]);
2186 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2189 else if (gi->type & GD_TYPE_TEXT_AREA) /* only valid for text area */
2191 char text[MAX_GADGET_TEXTSIZE + 1];
2192 int text_length = strlen(gi->textarea.value);
2193 int area_ysize = gi->textarea.ysize;
2194 int cursor_x_pref = gi->textarea.cursor_x_preferred;
2195 int cursor_y = gi->textarea.cursor_y;
2196 int cursor_pos = gi->textarea.cursor_position;
2197 char letter = getCharFromKey(key);
2198 boolean legal_letter = (letter != 0);
2202 insertCharIntoTextArea(gi, letter);
2204 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2206 else if (key == KSYM_Left && cursor_pos > 0)
2208 setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1);
2210 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2212 else if (key == KSYM_Right && cursor_pos < text_length)
2214 setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1);
2216 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2218 else if (key == KSYM_Up && cursor_y > 0)
2220 setTextAreaCursorXY(gi, cursor_x_pref, cursor_y - 1);
2221 gi->textarea.cursor_x_preferred = cursor_x_pref;
2223 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2225 else if (key == KSYM_Down && cursor_y < area_ysize - 1)
2227 setTextAreaCursorXY(gi, cursor_x_pref, cursor_y + 1);
2228 gi->textarea.cursor_x_preferred = cursor_x_pref;
2230 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2232 else if (key == KSYM_BackSpace && cursor_pos > 0)
2234 strcpy(text, gi->textarea.value);
2235 strcpy(&gi->textarea.value[cursor_pos - 1], &text[cursor_pos]);
2237 setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1);
2239 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2241 else if (key == KSYM_Delete && cursor_pos < text_length)
2243 strcpy(text, gi->textarea.value);
2244 strcpy(&gi->textarea.value[cursor_pos], &text[cursor_pos + 1]);
2246 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2249 else if (gi->type & GD_TYPE_SELECTBOX) /* only valid for selectbox */
2251 int index = gi->selectbox.current_index;
2252 int num_values = gi->selectbox.num_values;
2254 if (key == KSYM_Up && index > 0)
2256 gi->selectbox.current_index--;
2258 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2260 else if (key == KSYM_Down && index < num_values - 1)
2262 gi->selectbox.current_index++;
2264 DrawGadget(gi, DG_PRESSED, gi->direct_draw);