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 GADGET_DEACTIVATED(g) ((g)->x < 0 || (g)->y < 0)
29 #define OPTION_TEXT_SELECTABLE(g, t) \
30 (t[0] != g->selectbox.char_unselectable && \
33 #define CURRENT_OPTION_SELECTABLE(g) \
34 OPTION_TEXT_SELECTABLE(g, g->selectbox.options[g->selectbox.current_index].text)
37 static struct GadgetInfo *gadget_list_first_entry = NULL;
38 static struct GadgetInfo *gadget_list_last_entry = NULL;
39 static struct GadgetInfo *last_info_gi = NULL;
40 static int next_free_gadget_id = 1;
41 static boolean gadget_id_wrapped = FALSE;
43 static void (*PlayGadgetSoundActivating)(void) = NULL;
44 static void (*PlayGadgetSoundSelecting)(void) = NULL;
47 void InitGadgetsSoundCallback(void (*activating_function)(void),
48 void (*selecting_function)(void))
50 PlayGadgetSoundActivating = activating_function;
51 PlayGadgetSoundSelecting = selecting_function;
54 static struct GadgetInfo *getGadgetInfoFromGadgetID(int id)
56 struct GadgetInfo *gi = gadget_list_first_entry;
58 while (gi && gi->id != id)
64 static int getNewGadgetID()
66 int id = next_free_gadget_id++;
68 if (next_free_gadget_id <= 0) /* counter overrun */
70 gadget_id_wrapped = TRUE; /* now we must check each ID */
71 next_free_gadget_id = 0;
74 if (gadget_id_wrapped)
76 next_free_gadget_id++;
77 while (getGadgetInfoFromGadgetID(next_free_gadget_id) != NULL)
78 next_free_gadget_id++;
81 if (next_free_gadget_id <= 0) /* cannot get new gadget id */
82 Error(ERR_EXIT, "too much gadgets -- this should not happen");
87 static struct GadgetInfo *getGadgetInfoFromMousePosition(int mx, int my,
90 struct GadgetInfo *gi;
92 /* first check for scrollbars in case of mouse scroll wheel button events */
93 if (IS_WHEEL_BUTTON(button))
95 /* real horizontal wheel or vertical wheel with modifier key pressed */
96 boolean check_horizontal = (IS_WHEEL_BUTTON_HORIZONTAL(button) ||
97 GetKeyModState() & KMOD_Shift);
99 /* check for the first active scrollbar with matching mouse wheel area */
100 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
102 if (gi->mapped && gi->active &&
103 ((gi->type & GD_TYPE_SCROLLBAR_HORIZONTAL && check_horizontal) ||
104 (gi->type & GD_TYPE_SCROLLBAR_VERTICAL && !check_horizontal)) &&
105 mx >= gi->wheelarea.x && mx < gi->wheelarea.x + gi->wheelarea.width &&
106 my >= gi->wheelarea.y && my < gi->wheelarea.y + gi->wheelarea.height)
110 /* no active scrollbar found -- ignore this scroll wheel button event */
114 /* open selectboxes may overlap other active gadgets, so check them first */
115 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
117 if (gi->mapped && gi->active &&
118 gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open &&
119 mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width &&
120 my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
124 /* check all other gadgets */
125 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
127 if (gi->mapped && gi->active &&
128 mx >= gi->x && mx < gi->x + gi->width &&
129 my >= gi->y && my < gi->y + gi->height)
136 static void setTextAreaCursorExt(struct GadgetInfo *gi, boolean set_cursor_pos)
138 char *text = gi->textarea.value;
139 int area_xsize = gi->textarea.xsize;
140 int area_ysize = gi->textarea.ysize;
141 int cursor_position = gi->textarea.cursor_position;
142 int cursor_x = gi->textarea.cursor_x;
143 int cursor_y = gi->textarea.cursor_y;
150 if (set_cursor_pos) /* x/y => position */
152 if (y == cursor_y && (x == cursor_x || (x < cursor_x && *text == '\n')))
155 else /* position => x/y */
157 if (pos == cursor_position)
161 if (x + 1 >= area_xsize || *text == '\n')
163 if (y + 1 >= area_ysize)
176 gi->textarea.cursor_x = x;
177 gi->textarea.cursor_y = y;
178 gi->textarea.cursor_x_preferred = x;
179 gi->textarea.cursor_position = pos;
182 static void setTextAreaCursorXY(struct GadgetInfo *gi, int x, int y)
184 gi->textarea.cursor_x = x;
185 gi->textarea.cursor_y = y;
187 setTextAreaCursorExt(gi, TRUE);
190 static void setTextAreaCursorPosition(struct GadgetInfo *gi, int pos)
192 gi->textarea.cursor_position = pos;
194 setTextAreaCursorExt(gi, FALSE);
197 static void default_callback_info(void *ptr)
202 static void default_callback_action(void *ptr)
207 static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct)
209 struct GadgetDesign *gd;
210 int state = (pressed ? GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
211 boolean redraw_selectbox = FALSE;
216 gd = (!gi->active ? &gi->alt_design[state] :
217 gi->checked ? &gi->alt_design[state] : &gi->design[state]);
221 case GD_TYPE_NORMAL_BUTTON:
222 case GD_TYPE_CHECK_BUTTON:
223 case GD_TYPE_RADIO_BUTTON:
224 BlitBitmapOnBackground(gd->bitmap, drawto,
225 gd->x, gd->y, gi->width, gi->height,
227 if (gi->deco.design.bitmap)
228 BlitBitmap(gi->deco.design.bitmap, drawto,
229 gi->deco.design.x, gi->deco.design.y,
230 gi->deco.width, gi->deco.height,
231 gi->x + gi->deco.x + (pressed ? gi->deco.xshift : 0),
232 gi->y + gi->deco.y + (pressed ? gi->deco.yshift : 0));
235 case GD_TYPE_TEXT_BUTTON:
238 int font_nr = (gi->active ? gi->font_active : gi->font);
239 int font_width = getFontWidth(font_nr);
240 int border_x = gi->border.xsize;
241 int border_y = gi->border.ysize;
242 int text_size = strlen(gi->textbutton.value);
243 int text_start = (gi->width - text_size * font_width) / 2;
245 /* left part of gadget */
246 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
247 border_x, gi->height, gi->x, gi->y);
249 /* middle part of gadget */
250 for (i=0; i < gi->textbutton.size; i++)
251 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
252 font_width, gi->height,
253 gi->x + border_x + i * font_width, gi->y);
255 /* right part of gadget */
256 BlitBitmapOnBackground(gd->bitmap, drawto,
257 gd->x + gi->border.width - border_x, gd->y,
258 border_x, gi->height,
259 gi->x + gi->width - border_x, gi->y);
261 /* gadget text value */
263 gi->x + text_start + (pressed ? gi->deco.xshift : 0),
264 gi->y + border_y + (pressed ? gi->deco.yshift : 0),
265 gi->textbutton.value, font_nr, BLIT_MASKED);
269 case GD_TYPE_TEXT_INPUT_ALPHANUMERIC:
270 case GD_TYPE_TEXT_INPUT_NUMERIC:
274 char cursor_string[2];
275 char text[MAX_GADGET_TEXTSIZE + 1];
276 int font_nr = (pressed ? gi->font_active : gi->font);
277 int font_width = getFontWidth(font_nr);
278 int border_x = gi->border.xsize;
279 int border_y = gi->border.ysize;
281 /* left part of gadget */
282 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
283 border_x, gi->height, gi->x, gi->y);
285 /* middle part of gadget */
286 for (i=0; i < gi->textinput.size + 1; i++)
287 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
288 font_width, gi->height,
289 gi->x + border_x + i * font_width, gi->y);
291 /* right part of gadget */
292 BlitBitmapOnBackground(gd->bitmap, drawto,
293 gd->x + gi->border.width - border_x, gd->y,
294 border_x, gi->height,
295 gi->x + gi->width - border_x, gi->y);
298 strcpy(text, gi->textinput.value);
301 /* gadget text value */
303 gi->x + border_x, gi->y + border_y, text,
304 font_nr, BLIT_MASKED);
306 cursor_letter = gi->textinput.value[gi->textinput.cursor_position];
307 cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' ');
308 cursor_string[1] = '\0';
310 /* draw cursor, if active */
314 gi->textinput.cursor_position * font_width,
315 gi->y + border_y, cursor_string,
316 font_nr, BLIT_INVERSE);
320 case GD_TYPE_TEXT_AREA:
324 char cursor_string[2];
325 int font_nr = (pressed ? gi->font_active : gi->font);
326 int font_width = getFontWidth(font_nr);
327 int font_height = getFontHeight(font_nr);
328 int border_x = gi->border.xsize;
329 int border_y = gi->border.ysize;
330 int gd_height = 2 * border_y + font_height;
332 /* top left part of gadget border */
333 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
334 border_x, border_y, gi->x, gi->y);
336 /* top middle part of gadget border */
337 for (i=0; i < gi->textarea.xsize; i++)
338 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
339 font_width, border_y,
340 gi->x + border_x + i * font_width, gi->y);
342 /* top right part of gadget border */
343 BlitBitmapOnBackground(gd->bitmap, drawto,
344 gd->x + gi->border.width - border_x, gd->y,
346 gi->x + gi->width - border_x, gi->y);
348 /* left and right part of gadget border for each row */
349 for (i=0; i < gi->textarea.ysize; i++)
351 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y,
352 border_x, font_height,
353 gi->x, gi->y + border_y + i * font_height);
354 BlitBitmapOnBackground(gd->bitmap, drawto,
355 gd->x + gi->border.width - border_x,
357 border_x, font_height,
358 gi->x + gi->width - border_x,
359 gi->y + border_y + i * font_height);
362 /* bottom left part of gadget border */
363 BlitBitmapOnBackground(gd->bitmap, drawto,
364 gd->x, gd->y + gd_height - border_y,
366 gi->x, gi->y + gi->height - border_y);
368 /* bottom middle part of gadget border */
369 for (i=0; i < gi->textarea.xsize; i++)
370 BlitBitmapOnBackground(gd->bitmap, drawto,
372 gd->y + gd_height - border_y,
373 font_width, border_y,
374 gi->x + border_x + i * font_width,
375 gi->y + gi->height - border_y);
377 /* bottom right part of gadget border */
378 BlitBitmapOnBackground(gd->bitmap, drawto,
379 gd->x + gi->border.width - border_x,
380 gd->y + gd_height - border_y,
382 gi->x + gi->width - border_x,
383 gi->y + gi->height - border_y);
385 ClearRectangleOnBackground(drawto,
388 gi->width - 2 * border_x,
389 gi->height - 2 * border_y);
391 /* gadget text value */
392 DrawTextBuffer(gi->x + border_x, gi->y + border_y, gi->textarea.value,
393 font_nr, gi->textarea.xsize, -1, gi->textarea.ysize, 0,
394 BLIT_ON_BACKGROUND, FALSE, FALSE, FALSE);
396 cursor_letter = gi->textarea.value[gi->textarea.cursor_position];
397 cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' ');
398 cursor_string[1] = '\0';
400 /* draw cursor, if active */
403 gi->x + border_x + gi->textarea.cursor_x * font_width,
404 gi->y + border_y + gi->textarea.cursor_y * font_height,
406 font_nr, BLIT_INVERSE);
410 case GD_TYPE_SELECTBOX:
413 char text[MAX_GADGET_TEXTSIZE + 1];
414 int font_nr_default = (pressed ? gi->font_active : gi->font);
415 int font_nr = font_nr_default;
416 int font_width = getFontWidth(font_nr);
417 int font_height = getFontHeight(font_nr);
418 int border_x = gi->border.xsize;
419 int border_y = gi->border.ysize;
420 int button = gi->border.xsize_selectbutton;
421 int width_inner = gi->border.width - button - 2 * border_x;
422 int box_width = gi->selectbox.width;
423 int box_height = gi->selectbox.height;
425 /* left part of gadget */
426 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
427 border_x, gi->height, gi->x, gi->y);
429 /* middle part of gadget */
430 for (i=0; i < gi->selectbox.size; i++)
431 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
432 font_width, gi->height,
433 gi->x + border_x + i * font_width, gi->y);
435 /* button part of gadget */
436 BlitBitmapOnBackground(gd->bitmap, drawto,
437 gd->x + border_x + width_inner, gd->y,
439 gi->x + gi->width - border_x - button, gi->y);
441 /* right part of gadget */
442 BlitBitmapOnBackground(gd->bitmap, drawto,
443 gd->x + gi->border.width - border_x, gd->y,
444 border_x, gi->height,
445 gi->x + gi->width - border_x, gi->y);
448 strncpy(text, gi->selectbox.options[gi->selectbox.index].text,
450 text[gi->selectbox.size] = '\0';
453 font_nr = (OPTION_TEXT_SELECTABLE(gi, text) ? font_nr_default :
454 gi->font_unselectable);
456 /* gadget text value */
457 DrawTextExt(drawto, gi->x + border_x, gi->y + border_y, text,
458 font_nr, BLIT_MASKED);
462 if (!gi->selectbox.open)
464 gi->selectbox.open = TRUE;
465 gi->selectbox.stay_open = FALSE;
466 gi->selectbox.current_index = gi->selectbox.index;
468 /* save background under selectbox */
469 BlitBitmap(drawto, gfx.field_save_buffer,
470 gi->selectbox.x, gi->selectbox.y,
471 gi->selectbox.width, gi->selectbox.height,
472 gi->selectbox.x, gi->selectbox.y);
475 /* draw open selectbox */
477 /* top left part of gadget border */
478 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
480 gi->selectbox.x, gi->selectbox.y);
482 /* top middle part of gadget border */
483 for (i=0; i < gi->selectbox.size; i++)
484 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
485 font_width, border_y,
486 gi->selectbox.x + border_x + i * font_width,
489 /* top button part of gadget border */
490 BlitBitmapOnBackground(gd->bitmap, drawto,
491 gd->x + border_x + width_inner, gd->y,
493 gi->selectbox.x + box_width -border_x -button,
496 /* top right part of gadget border */
497 BlitBitmapOnBackground(gd->bitmap, drawto,
498 gd->x + gi->border.width - border_x, gd->y,
500 gi->selectbox.x + box_width - border_x,
503 /* left and right part of gadget border for each row */
504 for (i=0; i < gi->selectbox.num_values; i++)
506 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y,
507 border_x, font_height,
509 gi->selectbox.y + border_y + i*font_height);
510 BlitBitmapOnBackground(gd->bitmap, drawto,
511 gd->x + gi->border.width - border_x,
513 border_x, font_height,
514 gi->selectbox.x + box_width - border_x,
515 gi->selectbox.y + border_y + i*font_height);
518 /* bottom left part of gadget border */
519 BlitBitmapOnBackground(gd->bitmap, drawto,
520 gd->x, gd->y + gi->height - border_y,
523 gi->selectbox.y + box_height - border_y);
525 /* bottom middle part of gadget border */
526 for (i=0; i < gi->selectbox.size; i++)
527 BlitBitmapOnBackground(gd->bitmap, drawto,
529 gd->y + gi->height - border_y,
530 font_width, border_y,
531 gi->selectbox.x + border_x + i * font_width,
532 gi->selectbox.y + box_height - border_y);
534 /* bottom button part of gadget border */
535 BlitBitmapOnBackground(gd->bitmap, drawto,
536 gd->x + border_x + width_inner,
537 gd->y + gi->height - border_y,
539 gi->selectbox.x + box_width -border_x -button,
540 gi->selectbox.y + box_height - border_y);
542 /* bottom right part of gadget border */
543 BlitBitmapOnBackground(gd->bitmap, drawto,
544 gd->x + gi->border.width - border_x,
545 gd->y + gi->height - border_y,
547 gi->selectbox.x + box_width - border_x,
548 gi->selectbox.y + box_height - border_y);
550 ClearRectangleOnBackground(drawto,
551 gi->selectbox.x + border_x,
552 gi->selectbox.y + border_y,
553 gi->selectbox.width - 2 * border_x,
554 gi->selectbox.height - 2 * border_y);
556 /* selectbox text values */
557 for (i=0; i < gi->selectbox.num_values; i++)
559 int mask_mode = BLIT_MASKED;
561 strncpy(text, gi->selectbox.options[i].text, gi->selectbox.size);
562 text[gi->selectbox.size] = '\0';
564 font_nr = (OPTION_TEXT_SELECTABLE(gi, text) ? font_nr_default :
565 gi->font_unselectable);
567 if (i == gi->selectbox.current_index &&
568 OPTION_TEXT_SELECTABLE(gi, text))
570 FillRectangle(drawto,
571 gi->selectbox.x + border_x,
572 gi->selectbox.y + border_y + i * font_height,
573 gi->selectbox.width - 2 * border_x, font_height,
574 gi->selectbox.inverse_color);
576 /* prevent use of cursor graphic by drawing at least two chars */
578 text[gi->selectbox.size] = '\0';
580 mask_mode = BLIT_INVERSE;
584 gi->selectbox.x + border_x,
585 gi->selectbox.y + border_y + i * font_height, text,
589 redraw_selectbox = TRUE;
591 else if (gi->selectbox.open)
593 gi->selectbox.open = FALSE;
595 /* restore background under selectbox */
596 BlitBitmap(gfx.field_save_buffer, drawto,
597 gi->selectbox.x, gi->selectbox.y,
598 gi->selectbox.width, gi->selectbox.height,
599 gi->selectbox.x, gi->selectbox.y);
601 /* redraw closed selectbox */
602 DrawGadget(gi, FALSE, FALSE);
604 redraw_selectbox = TRUE;
609 case GD_TYPE_SCROLLBAR_VERTICAL:
613 int ypos = gi->y + gi->scrollbar.position;
614 int design_full = gi->width;
615 int design_body = design_full - 2 * gi->border.ysize;
616 int size_full = gi->scrollbar.size;
617 int size_body = size_full - 2 * gi->border.ysize;
618 int num_steps = size_body / design_body;
619 int step_size_remain = size_body - num_steps * design_body;
621 /* clear scrollbar area */
622 ClearRectangleOnBackground(backbuffer, gi->x, gi->y,
623 gi->width, gi->height);
625 /* upper part of gadget */
626 BlitBitmapOnBackground(gd->bitmap, drawto,
628 gi->width, gi->border.ysize,
631 /* middle part of gadget */
632 for (i=0; i < num_steps; i++)
633 BlitBitmapOnBackground(gd->bitmap, drawto,
634 gd->x, gd->y + gi->border.ysize,
635 gi->width, design_body,
637 ypos + gi->border.ysize + i * design_body);
639 /* remaining middle part of gadget */
640 if (step_size_remain > 0)
641 BlitBitmapOnBackground(gd->bitmap, drawto,
642 gd->x, gd->y + gi->border.ysize,
643 gi->width, step_size_remain,
645 ypos + gi->border.ysize
646 + num_steps * design_body);
648 /* lower part of gadget */
649 BlitBitmapOnBackground(gd->bitmap, drawto,
650 gd->x, gd->y + design_full - gi->border.ysize,
651 gi->width, gi->border.ysize,
652 xpos, ypos + size_full - gi->border.ysize);
656 case GD_TYPE_SCROLLBAR_HORIZONTAL:
659 int xpos = gi->x + gi->scrollbar.position;
661 int design_full = gi->height;
662 int design_body = design_full - 2 * gi->border.xsize;
663 int size_full = gi->scrollbar.size;
664 int size_body = size_full - 2 * gi->border.xsize;
665 int num_steps = size_body / design_body;
666 int step_size_remain = size_body - num_steps * design_body;
668 /* clear scrollbar area */
669 ClearRectangleOnBackground(backbuffer, gi->x, gi->y,
670 gi->width, gi->height);
672 /* left part of gadget */
673 BlitBitmapOnBackground(gd->bitmap, drawto,
675 gi->border.xsize, gi->height,
678 /* middle part of gadget */
679 for (i=0; i < num_steps; i++)
680 BlitBitmapOnBackground(gd->bitmap, drawto,
681 gd->x + gi->border.xsize, gd->y,
682 design_body, gi->height,
683 xpos + gi->border.xsize + i * design_body,
686 /* remaining middle part of gadget */
687 if (step_size_remain > 0)
688 BlitBitmapOnBackground(gd->bitmap, drawto,
689 gd->x + gi->border.xsize, gd->y,
690 step_size_remain, gi->height,
691 xpos + gi->border.xsize
692 + num_steps * design_body,
695 /* right part of gadget */
696 BlitBitmapOnBackground(gd->bitmap, drawto,
697 gd->x + design_full - gi->border.xsize, gd->y,
698 gi->border.xsize, gi->height,
699 xpos + size_full - gi->border.xsize, ypos);
709 BlitBitmap(drawto, window,
710 gi->x, gi->y, gi->width, gi->height, gi->x, gi->y);
712 if (gi->type == GD_TYPE_SELECTBOX && redraw_selectbox)
713 BlitBitmap(drawto, window,
714 gi->selectbox.x, gi->selectbox.y,
715 gi->selectbox.width, gi->selectbox.height,
716 gi->selectbox.x, gi->selectbox.y);
723 redraw_mask |= (IN_GFX_FIELD_FULL(x, y) ? REDRAW_FIELD :
724 IN_GFX_DOOR_1(x, y) ? REDRAW_DOOR_1 :
725 IN_GFX_DOOR_2(x, y) ? REDRAW_DOOR_2 :
726 IN_GFX_DOOR_3(x, y) ? REDRAW_DOOR_3 : REDRAW_ALL);
730 static int get_minimal_size_for_numeric_input(int minmax_value)
732 int min_size = 1; /* value needs at least one digit */
735 /* add number of digits needed for absolute value */
736 for (i = 10; i <= ABS(minmax_value); i *= 10)
739 /* if min/max value is negative, add one digit for minus sign */
740 if (minmax_value < 0)
746 static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap)
753 while (tag != GDI_END)
758 gi->custom_id = va_arg(ap, int);
761 case GDI_CUSTOM_TYPE_ID:
762 gi->custom_type_id = va_arg(ap, int);
767 int max_textsize = MAX_INFO_TEXTSIZE;
768 char *text = va_arg(ap, char *);
771 strncpy(gi->info_text, text, max_textsize);
775 gi->info_text[max_textsize] = '\0';
780 gi->x = va_arg(ap, int);
784 gi->y = va_arg(ap, int);
788 gi->width = va_arg(ap, int);
792 gi->height = va_arg(ap, int);
796 gi->type = va_arg(ap, unsigned int);
800 gi->state = va_arg(ap, unsigned int);
804 gi->active = (boolean)va_arg(ap, int);
807 case GDI_DIRECT_DRAW:
808 gi->direct_draw = (boolean)va_arg(ap, int);
812 gi->checked = (boolean)va_arg(ap, int);
816 gi->radio_nr = va_arg(ap, unsigned int);
819 case GDI_NUMBER_VALUE:
820 gi->textinput.number_value = va_arg(ap, int);
821 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
822 strcpy(gi->textinput.last_value, gi->textinput.value);
823 gi->textinput.cursor_position = strlen(gi->textinput.value);
827 gi->textinput.number_min = va_arg(ap, int);
828 if (gi->textinput.number_value < gi->textinput.number_min)
830 gi->textinput.number_value = gi->textinput.number_min;
831 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
832 strcpy(gi->textinput.last_value, gi->textinput.value);
837 gi->textinput.number_max = va_arg(ap, int);
838 if (gi->textinput.number_value > gi->textinput.number_max)
840 gi->textinput.number_value = gi->textinput.number_max;
841 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
842 strcpy(gi->textinput.last_value, gi->textinput.value);
848 int max_textsize = MAX_GADGET_TEXTSIZE;
850 if (gi->textinput.size)
851 max_textsize = MIN(gi->textinput.size, MAX_GADGET_TEXTSIZE);
853 strncpy(gi->textinput.value, va_arg(ap, char *), max_textsize);
854 strcpy(gi->textinput.last_value, gi->textinput.value);
856 gi->textinput.value[max_textsize] = '\0';
857 gi->textinput.cursor_position = strlen(gi->textinput.value);
859 /* same tag also used for other gadget definitions */
860 strcpy(gi->textbutton.value, gi->textinput.value);
861 strcpy(gi->textarea.value, gi->textinput.value);
862 strcpy(gi->textarea.last_value, gi->textinput.value);
868 int tag_value = va_arg(ap, int);
869 int max_textsize = MIN(tag_value, MAX_GADGET_TEXTSIZE);
871 gi->textinput.size = max_textsize;
872 gi->textinput.value[max_textsize] = '\0';
873 strcpy(gi->textinput.last_value, gi->textinput.value);
875 /* same tag also used for other gadget definitions */
877 gi->textarea.size = max_textsize;
878 gi->textarea.value[max_textsize] = '\0';
879 strcpy(gi->textarea.last_value, gi->textinput.value);
881 gi->textbutton.size = max_textsize;
882 gi->textbutton.value[max_textsize] = '\0';
884 gi->selectbox.size = gi->textinput.size;
889 gi->font = va_arg(ap, int);
890 if (gi->font_active == 0)
891 gi->font_active = gi->font;
892 if (gi->font_unselectable == 0)
893 gi->font_unselectable = gi->font;
896 case GDI_TEXT_FONT_ACTIVE:
897 gi->font_active = va_arg(ap, int);
900 case GDI_TEXT_FONT_UNSELECTABLE:
901 gi->font_unselectable = va_arg(ap, int);
904 case GDI_SELECTBOX_OPTIONS:
905 gi->selectbox.options = va_arg(ap, struct ValueTextInfo *);
908 case GDI_SELECTBOX_INDEX:
909 gi->selectbox.index = va_arg(ap, int);
912 case GDI_SELECTBOX_CHAR_UNSELECTABLE:
913 gi->selectbox.char_unselectable = (char)va_arg(ap, int);
916 case GDI_DESIGN_UNPRESSED:
917 gi->design[GD_BUTTON_UNPRESSED].bitmap = va_arg(ap, Bitmap *);
918 gi->design[GD_BUTTON_UNPRESSED].x = va_arg(ap, int);
919 gi->design[GD_BUTTON_UNPRESSED].y = va_arg(ap, int);
922 case GDI_DESIGN_PRESSED:
923 gi->design[GD_BUTTON_PRESSED].bitmap = va_arg(ap, Bitmap *);
924 gi->design[GD_BUTTON_PRESSED].x = va_arg(ap, int);
925 gi->design[GD_BUTTON_PRESSED].y = va_arg(ap, int);
928 case GDI_ALT_DESIGN_UNPRESSED:
929 gi->alt_design[GD_BUTTON_UNPRESSED].bitmap= va_arg(ap, Bitmap *);
930 gi->alt_design[GD_BUTTON_UNPRESSED].x = va_arg(ap, int);
931 gi->alt_design[GD_BUTTON_UNPRESSED].y = va_arg(ap, int);
934 case GDI_ALT_DESIGN_PRESSED:
935 gi->alt_design[GD_BUTTON_PRESSED].bitmap = va_arg(ap, Bitmap *);
936 gi->alt_design[GD_BUTTON_PRESSED].x = va_arg(ap, int);
937 gi->alt_design[GD_BUTTON_PRESSED].y = va_arg(ap, int);
940 case GDI_BORDER_SIZE:
941 gi->border.xsize = va_arg(ap, int);
942 gi->border.ysize = va_arg(ap, int);
945 case GDI_BORDER_SIZE_SELECTBUTTON:
946 gi->border.xsize_selectbutton = va_arg(ap, int);
949 case GDI_DESIGN_WIDTH:
950 gi->border.width = va_arg(ap, int);
953 case GDI_DECORATION_DESIGN:
954 gi->deco.design.bitmap = va_arg(ap, Bitmap *);
955 gi->deco.design.x = va_arg(ap, int);
956 gi->deco.design.y = va_arg(ap, int);
959 case GDI_DECORATION_POSITION:
960 gi->deco.x = va_arg(ap, int);
961 gi->deco.y = va_arg(ap, int);
964 case GDI_DECORATION_SIZE:
965 gi->deco.width = va_arg(ap, int);
966 gi->deco.height = va_arg(ap, int);
969 case GDI_DECORATION_SHIFTING:
970 gi->deco.xshift = va_arg(ap, int);
971 gi->deco.yshift = va_arg(ap, int);
975 gi->event_mask = va_arg(ap, unsigned int);
979 gi->drawing.area_xsize = va_arg(ap, int);
980 gi->drawing.area_ysize = va_arg(ap, int);
982 /* determine dependent values for drawing area gadget, if needed */
983 if (gi->drawing.item_xsize != 0 && gi->drawing.item_ysize != 0)
985 gi->width = gi->drawing.area_xsize * gi->drawing.item_xsize;
986 gi->height = gi->drawing.area_ysize * gi->drawing.item_ysize;
988 else if (gi->width != 0 && gi->height != 0)
990 gi->drawing.item_xsize = gi->width / gi->drawing.area_xsize;
991 gi->drawing.item_ysize = gi->height / gi->drawing.area_ysize;
994 /* same tag also used for other gadget definitions */
995 gi->textarea.xsize = gi->drawing.area_xsize;
996 gi->textarea.ysize = gi->drawing.area_ysize;
998 if (gi->type & GD_TYPE_TEXT_AREA) /* force recalculation */
1007 gi->drawing.item_xsize = va_arg(ap, int);
1008 gi->drawing.item_ysize = va_arg(ap, int);
1010 /* determine dependent values for drawing area gadget, if needed */
1011 if (gi->drawing.area_xsize != 0 && gi->drawing.area_ysize != 0)
1013 gi->width = gi->drawing.area_xsize * gi->drawing.item_xsize;
1014 gi->height = gi->drawing.area_ysize * gi->drawing.item_ysize;
1016 else if (gi->width != 0 && gi->height != 0)
1018 gi->drawing.area_xsize = gi->width / gi->drawing.item_xsize;
1019 gi->drawing.area_ysize = gi->height / gi->drawing.item_ysize;
1023 case GDI_SCROLLBAR_ITEMS_MAX:
1024 gi->scrollbar.items_max = va_arg(ap, int);
1027 case GDI_SCROLLBAR_ITEMS_VISIBLE:
1028 gi->scrollbar.items_visible = va_arg(ap, int);
1031 case GDI_SCROLLBAR_ITEM_POSITION:
1032 gi->scrollbar.item_position = va_arg(ap, int);
1035 case GDI_WHEEL_AREA_X:
1036 gi->wheelarea.x = va_arg(ap, int);
1039 case GDI_WHEEL_AREA_Y:
1040 gi->wheelarea.y = va_arg(ap, int);
1043 case GDI_WHEEL_AREA_WIDTH:
1044 gi->wheelarea.width = va_arg(ap, int);
1047 case GDI_WHEEL_AREA_HEIGHT:
1048 gi->wheelarea.height = va_arg(ap, int);
1051 case GDI_CALLBACK_INFO:
1052 gi->callback_info = va_arg(ap, gadget_function);
1055 case GDI_CALLBACK_ACTION:
1056 gi->callback_action = va_arg(ap, gadget_function);
1060 Error(ERR_EXIT, "HandleGadgetTags(): unknown tag %d", tag);
1063 tag = va_arg(ap, int); /* read next tag */
1066 /* check if gadget is complete */
1067 if (gi->type != GD_TYPE_DRAWING_AREA &&
1068 (!gi->design[GD_BUTTON_UNPRESSED].bitmap ||
1069 !gi->design[GD_BUTTON_PRESSED].bitmap))
1070 Error(ERR_EXIT, "gadget incomplete (missing Bitmap)");
1072 /* adjust gadget values in relation to other gadget values */
1074 if (gi->type & GD_TYPE_TEXT_INPUT)
1076 int font_nr = gi->font_active;
1077 int font_width = getFontWidth(font_nr);
1078 int font_height = getFontHeight(font_nr);
1079 int border_xsize = gi->border.xsize;
1080 int border_ysize = gi->border.ysize;
1082 if (gi->type == GD_TYPE_TEXT_INPUT_NUMERIC)
1084 int number_min = gi->textinput.number_min;
1085 int number_max = gi->textinput.number_max;
1086 int min_size_min = get_minimal_size_for_numeric_input(number_min);
1087 int min_size_max = get_minimal_size_for_numeric_input(number_max);
1088 int min_size = MAX(min_size_min, min_size_max);
1090 /* expand gadget text input size, if maximal value is too large */
1091 if (gi->textinput.size < min_size)
1092 gi->textinput.size = min_size;
1095 gi->width = 2 * border_xsize + (gi->textinput.size + 1) * font_width;
1096 gi->height = 2 * border_ysize + font_height;
1099 if (gi->type & GD_TYPE_SELECTBOX)
1101 int font_nr = gi->font_active;
1102 int font_width = getFontWidth(font_nr);
1103 int font_height = getFontHeight(font_nr);
1104 int border_xsize = gi->border.xsize;
1105 int border_ysize = gi->border.ysize;
1106 int button_size = gi->border.xsize_selectbutton;
1107 int bottom_screen_border = gfx.sy + gfx.sysize - font_height;
1111 gi->width = 2 * border_xsize + gi->textinput.size*font_width +button_size;
1112 gi->height = 2 * border_ysize + font_height;
1114 if (gi->selectbox.options == NULL)
1115 Error(ERR_EXIT, "selectbox gadget incomplete (missing options array)");
1117 gi->selectbox.num_values = 0;
1118 while (gi->selectbox.options[gi->selectbox.num_values].text != NULL)
1119 gi->selectbox.num_values++;
1121 /* calculate values for open selectbox */
1122 gi->selectbox.width = gi->width;
1123 gi->selectbox.height =
1124 2 * border_ysize + gi->selectbox.num_values * font_height;
1126 gi->selectbox.x = gi->x;
1127 gi->selectbox.y = gi->y + gi->height;
1128 if (gi->selectbox.y + gi->selectbox.height > bottom_screen_border)
1129 gi->selectbox.y = gi->y - gi->selectbox.height;
1130 if (gi->selectbox.y < 0)
1131 gi->selectbox.y = bottom_screen_border - gi->selectbox.height;
1133 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &src_bitmap, &src_x, &src_y);
1134 src_x += font_width / 2;
1135 src_y += font_height / 2;
1137 /* there may be esoteric cases with missing or too small font bitmap */
1138 if (src_bitmap != NULL &&
1139 src_x < src_bitmap->width && src_y < src_bitmap->height)
1140 gi->selectbox.inverse_color = GetPixel(src_bitmap, src_x, src_y);
1142 /* always start with closed selectbox */
1143 gi->selectbox.open = FALSE;
1146 if (gi->type & GD_TYPE_TEXT_INPUT_NUMERIC)
1148 struct GadgetTextInput *text = &gi->textinput;
1149 int value = text->number_value;
1151 text->number_value = (value < text->number_min ? text->number_min :
1152 value > text->number_max ? text->number_max :
1155 sprintf(text->value, "%d", text->number_value);
1158 if (gi->type & GD_TYPE_TEXT_BUTTON)
1160 int font_nr = gi->font_active;
1161 int font_width = getFontWidth(font_nr);
1162 int font_height = getFontHeight(font_nr);
1163 int border_xsize = gi->border.xsize;
1164 int border_ysize = gi->border.ysize;
1166 gi->width = 2 * border_xsize + gi->textbutton.size * font_width;
1167 gi->height = 2 * border_ysize + font_height;
1170 if (gi->type & GD_TYPE_SCROLLBAR)
1172 struct GadgetScrollbar *gs = &gi->scrollbar;
1173 int scrollbar_size_cmp;
1175 if (gi->width == 0 || gi->height == 0 ||
1176 gs->items_max == 0 || gs->items_visible == 0)
1177 Error(ERR_EXIT, "scrollbar gadget incomplete (missing tags)");
1179 /* calculate internal scrollbar values */
1180 gs->size_min = (gi->type == GD_TYPE_SCROLLBAR_VERTICAL ?
1181 gi->width : gi->height);
1182 gs->size_max = (gi->type == GD_TYPE_SCROLLBAR_VERTICAL ?
1183 gi->height : gi->width);
1185 scrollbar_size_cmp = gs->size_max * gs->items_visible / gs->items_max;
1186 gs->size = MAX(scrollbar_size_cmp, gs->size_min);
1187 gs->size_max_cmp = (gs->size_max - (gs->size - scrollbar_size_cmp));
1189 gs->position = gs->size_max_cmp * gs->item_position / gs->items_max;
1190 gs->position_max = gs->size_max - gs->size;
1191 gs->correction = gs->size_max / gs->items_max / 2;
1193 /* finetuning for maximal right/bottom position */
1194 if (gs->item_position == gs->items_max - gs->items_visible)
1195 gs->position = gs->position_max;
1198 if (gi->type & GD_TYPE_TEXT_AREA)
1200 int font_nr = gi->font_active;
1201 int font_width = getFontWidth(font_nr);
1202 int font_height = getFontHeight(font_nr);
1203 int border_xsize = gi->border.xsize;
1204 int border_ysize = gi->border.ysize;
1206 if (gi->width == 0 || gi->height == 0)
1208 gi->width = 2 * border_xsize + gi->textarea.xsize * font_width;
1209 gi->height = 2 * border_ysize + gi->textarea.ysize * font_height;
1213 gi->textarea.xsize = (gi->width - 2 * border_xsize) / font_width;
1214 gi->textarea.ysize = (gi->height - 2 * border_ysize) / font_height;
1219 void ModifyGadget(struct GadgetInfo *gi, int first_tag, ...)
1223 va_start(ap, first_tag);
1224 HandleGadgetTags(gi, first_tag, ap);
1230 void RedrawGadget(struct GadgetInfo *gi)
1236 DrawGadget(gi, gi->state, gi->direct_draw);
1239 struct GadgetInfo *CreateGadget(int first_tag, ...)
1241 struct GadgetInfo *new_gadget = checked_calloc(sizeof(struct GadgetInfo));
1244 /* always start with reliable default values */
1245 new_gadget->id = getNewGadgetID();
1246 new_gadget->callback_info = default_callback_info;
1247 new_gadget->callback_action = default_callback_action;
1248 new_gadget->active = TRUE;
1249 new_gadget->direct_draw = TRUE;
1251 new_gadget->next = NULL;
1253 va_start(ap, first_tag);
1254 HandleGadgetTags(new_gadget, first_tag, ap);
1257 /* insert new gadget into global gadget list */
1258 if (gadget_list_last_entry)
1260 gadget_list_last_entry->next = new_gadget;
1261 gadget_list_last_entry = gadget_list_last_entry->next;
1264 gadget_list_first_entry = gadget_list_last_entry = new_gadget;
1269 void FreeGadget(struct GadgetInfo *gi)
1271 struct GadgetInfo *gi_previous = gadget_list_first_entry;
1276 /* prevent "last_info_gi" from pointing to memory that will be freed */
1277 if (last_info_gi == gi)
1278 last_info_gi = NULL;
1280 while (gi_previous != NULL && gi_previous->next != gi)
1281 gi_previous = gi_previous->next;
1283 if (gi == gadget_list_first_entry)
1284 gadget_list_first_entry = gi->next;
1286 if (gi == gadget_list_last_entry)
1287 gadget_list_last_entry = gi_previous;
1289 if (gi_previous != NULL)
1290 gi_previous->next = gi->next;
1295 static void CheckRangeOfNumericInputGadget(struct GadgetInfo *gi)
1297 if (gi->type != GD_TYPE_TEXT_INPUT_NUMERIC)
1300 gi->textinput.number_value = atoi(gi->textinput.value);
1302 if (gi->textinput.number_value < gi->textinput.number_min)
1303 gi->textinput.number_value = gi->textinput.number_min;
1304 if (gi->textinput.number_value > gi->textinput.number_max)
1305 gi->textinput.number_value = gi->textinput.number_max;
1307 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
1309 if (gi->textinput.cursor_position < 0)
1310 gi->textinput.cursor_position = 0;
1311 else if (gi->textinput.cursor_position > strlen(gi->textinput.value))
1312 gi->textinput.cursor_position = strlen(gi->textinput.value);
1315 /* global pointer to gadget actually in use (when mouse button pressed) */
1316 static struct GadgetInfo *last_gi = NULL;
1318 static void MapGadgetExt(struct GadgetInfo *gi, boolean redraw)
1320 if (gi == NULL || gi->mapped || GADGET_DEACTIVATED(gi))
1326 DrawGadget(gi, DG_UNPRESSED, DG_BUFFERED);
1329 void MapGadget(struct GadgetInfo *gi)
1331 MapGadgetExt(gi, TRUE);
1334 void UnmapGadget(struct GadgetInfo *gi)
1336 if (gi == NULL || !gi->mapped)
1345 #define MAX_NUM_GADGETS 1024
1346 #define MULTIMAP_UNMAP (1 << 0)
1347 #define MULTIMAP_REMAP (1 << 1)
1348 #define MULTIMAP_REDRAW (1 << 2)
1349 #define MULTIMAP_PLAYFIELD (1 << 3)
1350 #define MULTIMAP_DOOR_1 (1 << 4)
1351 #define MULTIMAP_DOOR_2 (1 << 5)
1352 #define MULTIMAP_DOOR_3 (1 << 6)
1353 #define MULTIMAP_ALL (MULTIMAP_PLAYFIELD | \
1358 static void MultiMapGadgets(int mode)
1360 struct GadgetInfo *gi = gadget_list_first_entry;
1361 static boolean map_state[MAX_NUM_GADGETS];
1369 if ((mode & MULTIMAP_PLAYFIELD && IN_GFX_FIELD_FULL(x, y)) ||
1370 (mode & MULTIMAP_DOOR_1 && IN_GFX_DOOR_1(x, y)) ||
1371 (mode & MULTIMAP_DOOR_2 && IN_GFX_DOOR_2(x, y)) ||
1372 (mode & MULTIMAP_DOOR_3 && IN_GFX_DOOR_3(x, y)) ||
1373 (mode & MULTIMAP_ALL) == MULTIMAP_ALL)
1375 if (mode & MULTIMAP_UNMAP)
1377 map_state[map_count++ % MAX_NUM_GADGETS] = gi->mapped;
1382 if (map_state[map_count++ % MAX_NUM_GADGETS])
1383 MapGadgetExt(gi, (mode & MULTIMAP_REDRAW));
1391 void UnmapAllGadgets()
1393 MultiMapGadgets(MULTIMAP_ALL | MULTIMAP_UNMAP);
1396 void RemapAllGadgets()
1398 MultiMapGadgets(MULTIMAP_ALL | MULTIMAP_REMAP);
1401 boolean anyTextInputGadgetActive()
1403 return (last_gi && (last_gi->type & GD_TYPE_TEXT_INPUT) && last_gi->mapped);
1406 boolean anyTextAreaGadgetActive()
1408 return (last_gi && (last_gi->type & GD_TYPE_TEXT_AREA) && last_gi->mapped);
1411 boolean anySelectboxGadgetActive()
1413 return (last_gi && (last_gi->type & GD_TYPE_SELECTBOX) && last_gi->mapped);
1416 boolean anyScrollbarGadgetActive()
1418 return (last_gi && (last_gi->type & GD_TYPE_SCROLLBAR) && last_gi->mapped);
1421 boolean anyTextGadgetActive()
1423 return (anyTextInputGadgetActive() ||
1424 anyTextAreaGadgetActive() ||
1425 anySelectboxGadgetActive());
1428 static boolean insideSelectboxLine(struct GadgetInfo *gi, int mx, int my)
1430 return (gi != NULL &&
1431 gi->type & GD_TYPE_SELECTBOX &&
1432 mx >= gi->x && mx < gi->x + gi->width &&
1433 my >= gi->y && my < gi->y + gi->height);
1436 static boolean insideSelectboxArea(struct GadgetInfo *gi, int mx, int my)
1438 return (gi != NULL &&
1439 gi->type & GD_TYPE_SELECTBOX &&
1440 mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width &&
1441 my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height);
1444 void ClickOnGadget(struct GadgetInfo *gi, int button)
1449 /* simulate releasing mouse button over last gadget, if still pressed */
1451 HandleGadgets(-1, -1, 0);
1453 /* simulate pressing mouse button over specified gadget */
1454 HandleGadgets(gi->x, gi->y, button);
1456 /* simulate releasing mouse button over specified gadget */
1457 HandleGadgets(gi->x, gi->y, 0);
1460 boolean HandleGadgets(int mx, int my, int button)
1462 static unsigned int pressed_delay = 0;
1463 static unsigned int pressed_delay_value = GADGET_FRAME_DELAY;
1464 static int last_button = 0;
1465 static int last_mx = 0, last_my = 0;
1466 static int pressed_mx = 0, pressed_my = 0;
1467 static boolean keep_selectbox_open = FALSE;
1468 static boolean gadget_stopped = FALSE;
1469 int scrollbar_mouse_pos = 0;
1470 struct GadgetInfo *new_gi, *gi;
1471 boolean press_event;
1472 boolean release_event;
1473 boolean mouse_moving;
1474 boolean mouse_inside_select_line;
1475 boolean mouse_inside_select_area;
1476 boolean mouse_released_where_pressed;
1477 boolean gadget_pressed;
1478 boolean gadget_pressed_repeated;
1479 boolean gadget_pressed_off_borders;
1480 boolean gadget_pressed_inside_select_line;
1481 boolean gadget_pressed_delay_reached;
1482 boolean gadget_moving;
1483 boolean gadget_moving_inside;
1484 boolean gadget_moving_off_borders;
1485 boolean gadget_draggable;
1486 boolean gadget_dragging;
1487 boolean gadget_released;
1488 boolean gadget_released_inside;
1489 boolean gadget_released_inside_select_area;
1490 boolean gadget_released_off_borders;
1491 boolean changed_position = FALSE;
1493 /* check if there are any gadgets defined */
1494 if (gadget_list_first_entry == NULL)
1497 /* simulated release of mouse button over last gadget */
1498 if (mx == -1 && my == -1 && button == 0)
1504 /* check which gadget is under the mouse pointer */
1505 new_gi = getGadgetInfoFromMousePosition(mx, my, button);
1507 /* check if button state has changed since last invocation */
1508 press_event = (button != 0 && last_button == 0);
1509 release_event = (button == 0 && last_button != 0);
1510 last_button = button;
1512 /* check if mouse has been moved since last invocation */
1513 mouse_moving = ((mx != last_mx || my != last_my) && motion_status);
1517 if (press_event && new_gi != last_gi)
1523 mouse_released_where_pressed =
1524 (release_event && mx == pressed_mx && my == pressed_my);
1526 mouse_inside_select_line = insideSelectboxLine(new_gi, mx, my);
1527 mouse_inside_select_area = insideSelectboxArea(new_gi, mx, my);
1529 gadget_pressed_off_borders = (press_event && new_gi != last_gi);
1531 gadget_pressed_inside_select_line =
1532 (press_event && new_gi != NULL &&
1533 new_gi->type & GD_TYPE_SELECTBOX && new_gi->selectbox.open &&
1534 insideSelectboxLine(new_gi, mx, my));
1536 /* if mouse button pressed outside text or selectbox gadget, deactivate it */
1537 if (anyTextGadgetActive() &&
1538 (gadget_pressed_off_borders ||
1539 (gadget_pressed_inside_select_line && !mouse_inside_select_area)))
1541 struct GadgetInfo *gi = last_gi;
1542 boolean gadget_changed = ((gi->event_mask & GD_EVENT_TEXT_LEAVING) != 0);
1544 /* check if text gadget has changed its value */
1545 if (gi->type & GD_TYPE_TEXT_INPUT)
1547 CheckRangeOfNumericInputGadget(gi);
1549 if (!strEqual(gi->textinput.last_value, gi->textinput.value))
1550 strcpy(gi->textinput.last_value, gi->textinput.value);
1552 gadget_changed = FALSE;
1555 /* selectbox does not change its value when closed by clicking outside */
1556 if (gi->type & GD_TYPE_SELECTBOX)
1557 gadget_changed = FALSE;
1559 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1561 gi->event.type = GD_EVENT_TEXT_LEAVING;
1563 if (gadget_changed && !(gi->type & GD_TYPE_SELECTBOX))
1564 gi->callback_action(gi);
1568 if (gadget_pressed_inside_select_line)
1573 (button != 0 && last_gi == NULL && new_gi != NULL && press_event);
1574 gadget_pressed_repeated =
1575 (button != 0 && last_gi != NULL && new_gi == last_gi);
1577 gadget_pressed_delay_reached =
1578 DelayReached(&pressed_delay, pressed_delay_value);
1580 gadget_released = (release_event && last_gi != NULL);
1581 gadget_released_inside = (gadget_released && new_gi == last_gi);
1582 gadget_released_off_borders = (gadget_released && new_gi != last_gi);
1584 gadget_moving = (button != 0 && last_gi != NULL && mouse_moving);
1585 gadget_moving_inside = (gadget_moving && new_gi == last_gi);
1586 gadget_moving_off_borders = (gadget_moving && new_gi != last_gi);
1588 /* when handling selectbox, set additional state values */
1589 if (gadget_released_inside && (last_gi->type & GD_TYPE_SELECTBOX))
1590 gadget_released_inside_select_area = insideSelectboxArea(last_gi, mx, my);
1592 gadget_released_inside_select_area = FALSE;
1594 /* setting state for handling over-large selectbox */
1595 if (keep_selectbox_open && (press_event || !mouse_inside_select_line))
1596 keep_selectbox_open = FALSE;
1598 /* if new gadget pressed, store this gadget */
1602 /* 'gi' is actually handled gadget */
1605 /* if gadget is scrollbar, choose mouse position value */
1606 if (gi && gi->type & GD_TYPE_SCROLLBAR)
1607 scrollbar_mouse_pos =
1608 (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx - gi->x : my - gi->y);
1610 /* if mouse button released, no gadget needs to be handled anymore */
1611 if (gadget_released)
1613 if (gi->type & GD_TYPE_SELECTBOX &&
1614 (keep_selectbox_open ||
1615 mouse_released_where_pressed ||
1616 !gadget_released_inside_select_area ||
1617 !CURRENT_OPTION_SELECTABLE(gi))) /* selectbox stays open */
1619 gi->selectbox.stay_open = TRUE;
1623 else if (!(gi->type & GD_TYPE_TEXT_INPUT ||
1624 gi->type & GD_TYPE_TEXT_AREA)) /* text input stays open */
1628 /* modify event position values even if no gadget is pressed */
1629 if (button == 0 && !release_event)
1632 /* if new gadget or if no gadget was pressed, release stopped processing */
1633 if (gadget_pressed || new_gi == NULL)
1634 gadget_stopped = FALSE;
1636 /* if gadget was stopped while being handled, stop gadget processing here */
1642 int last_x = gi->event.x;
1643 int last_y = gi->event.y;
1645 gi->event.x = mx - gi->x;
1646 gi->event.y = my - gi->y;
1648 if (gi->type == GD_TYPE_DRAWING_AREA)
1650 gi->event.x /= gi->drawing.item_xsize;
1651 gi->event.y /= gi->drawing.item_ysize;
1653 if (last_x != gi->event.x || last_y != gi->event.y)
1654 changed_position = TRUE;
1656 else if (gi->type & GD_TYPE_TEXT_INPUT && button != 0 && !motion_status)
1658 int old_cursor_position = gi->textinput.cursor_position;
1660 /* if mouse button pressed inside activated text gadget, set cursor */
1661 gi->textinput.cursor_position =
1662 (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font);
1664 if (gi->textinput.cursor_position < 0)
1665 gi->textinput.cursor_position = 0;
1666 else if (gi->textinput.cursor_position > strlen(gi->textinput.value))
1667 gi->textinput.cursor_position = strlen(gi->textinput.value);
1669 if (gi->textinput.cursor_position != old_cursor_position)
1670 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1672 else if (gi->type & GD_TYPE_TEXT_AREA && button != 0 && !motion_status)
1674 int old_cursor_position = gi->textarea.cursor_position;
1675 int x = (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font);
1676 int y = (my - gi->y - gi->border.ysize) / getFontHeight(gi->font);
1678 x = (x < 0 ? 0 : x >= gi->textarea.xsize ? gi->textarea.xsize - 1 : x);
1679 y = (y < 0 ? 0 : y >= gi->textarea.ysize ? gi->textarea.ysize - 1 : y);
1681 setTextAreaCursorXY(gi, x, y);
1683 if (gi->textarea.cursor_position != old_cursor_position)
1684 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1686 else if (gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open &&
1687 !keep_selectbox_open)
1689 int old_index = gi->selectbox.current_index;
1691 /* if mouse moving inside activated selectbox, select value */
1692 if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
1693 gi->selectbox.current_index =
1694 (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font);
1696 if (gi->selectbox.current_index < 0)
1697 gi->selectbox.current_index = 0;
1698 else if (gi->selectbox.current_index > gi->selectbox.num_values - 1)
1699 gi->selectbox.current_index = gi->selectbox.num_values - 1;
1701 if (gi->selectbox.current_index != old_index)
1702 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1706 /* handle gadget popup info text */
1707 if (last_info_gi != new_gi ||
1708 (new_gi && new_gi->type == GD_TYPE_DRAWING_AREA && changed_position))
1710 if (new_gi != NULL && (button == 0 || new_gi == last_gi))
1712 new_gi->event.type = GD_EVENT_INFO_ENTERING;
1713 new_gi->callback_info(new_gi);
1715 else if (last_info_gi != NULL)
1717 last_info_gi->event.type = GD_EVENT_INFO_LEAVING;
1718 last_info_gi->callback_info(last_info_gi);
1721 last_info_gi = new_gi;
1724 gadget_draggable = (gi && gi->type & GD_TYPE_SCROLLBAR);
1726 /* reset drag position for newly pressed scrollbar to "not dragging" */
1727 if (gadget_pressed && gadget_draggable)
1728 gi->scrollbar.drag_position = -1;
1730 gadget_dragging = (gadget_draggable && gi->scrollbar.drag_position != -1);
1732 /* clicking next to a scrollbar to move it is not considered "moving" */
1733 if (gadget_draggable && !gadget_dragging)
1734 gadget_moving = FALSE;
1736 /* when leaving scrollbar area when jump-scrolling, stop gadget processing */
1737 if (gadget_draggable && !gadget_dragging && gadget_moving_off_borders)
1738 gadget_stopped = TRUE;
1740 if ((gadget_pressed) ||
1741 (gadget_pressed_repeated && gadget_pressed_delay_reached))
1743 if (gadget_pressed) /* gadget pressed the first time */
1745 /* initialize delay counter */
1746 DelayReached(&pressed_delay, 0);
1748 /* start gadget delay with longer delay after first click on gadget */
1749 pressed_delay_value = GADGET_FRAME_DELAY_FIRST;
1751 else /* gadget hold pressed for some time */
1753 /* after first repeated gadget click, continue with shorter delay value */
1754 pressed_delay_value = GADGET_FRAME_DELAY;
1757 if (gi->type & GD_TYPE_SCROLLBAR && !gadget_dragging)
1759 int mpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx : my);
1760 int gpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? gi->x : gi->y);
1761 int slider_start = gpos + gi->scrollbar.position;
1762 int slider_end = gpos + gi->scrollbar.position + gi->scrollbar.size - 1;
1763 boolean inside_slider = (mpos >= slider_start && mpos <= slider_end);
1765 if (IS_WHEEL_BUTTON(button) || !inside_slider)
1767 /* click scrollbar one scrollbar length up/left or down/right */
1769 struct GadgetScrollbar *gs = &gi->scrollbar;
1770 int old_item_position = gs->item_position;
1771 int item_steps = gs->items_visible - 1;
1772 int item_direction = (mpos < gpos + gi->scrollbar.position ? -1 : +1);
1774 if (IS_WHEEL_BUTTON(button))
1776 boolean scroll_single_step = ((GetKeyModState() & KMOD_Alt) != 0);
1778 item_steps = (scroll_single_step ? 1 : DEFAULT_WHEEL_STEPS);
1779 item_direction = (button == MB_WHEEL_UP ||
1780 button == MB_WHEEL_LEFT ? -1 : +1);
1783 changed_position = FALSE;
1785 gs->item_position += item_steps * item_direction;
1787 if (gs->item_position < 0)
1788 gs->item_position = 0;
1789 else if (gs->item_position > gs->items_max - gs->items_visible)
1790 gs->item_position = gs->items_max - gs->items_visible;
1792 if (old_item_position != gs->item_position)
1794 gi->event.item_position = gs->item_position;
1795 changed_position = TRUE;
1798 ModifyGadget(gi, GDI_SCROLLBAR_ITEM_POSITION, gs->item_position,
1801 gi->state = GD_BUTTON_UNPRESSED;
1802 gi->event.type = GD_EVENT_MOVING;
1803 gi->event.off_borders = FALSE;
1805 if (gi->event_mask & GD_EVENT_MOVING && changed_position)
1806 gi->callback_action(gi);
1812 /* don't handle this scrollbar anymore when mouse position reached */
1813 if (gadget_pressed_repeated)
1815 gadget_stopped = TRUE;
1825 PlayGadgetSoundActivating();
1827 if (gi->type == GD_TYPE_CHECK_BUTTON)
1829 gi->checked = !gi->checked;
1831 else if (gi->type == GD_TYPE_RADIO_BUTTON)
1833 struct GadgetInfo *rgi = gadget_list_first_entry;
1838 rgi->type == GD_TYPE_RADIO_BUTTON &&
1839 rgi->radio_nr == gi->radio_nr &&
1842 rgi->checked = FALSE;
1843 DrawGadget(rgi, DG_UNPRESSED, rgi->direct_draw);
1851 else if (gi->type & GD_TYPE_SCROLLBAR)
1853 int mpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx : my);
1854 int gpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? gi->x : gi->y);
1855 int slider_start = gpos + gi->scrollbar.position;
1856 int slider_end = gpos + gi->scrollbar.position + gi->scrollbar.size - 1;
1857 boolean inside_slider = (mpos >= slider_start && mpos <= slider_end);
1859 if (!IS_WHEEL_BUTTON(button) && inside_slider)
1861 /* start dragging scrollbar */
1862 gi->scrollbar.drag_position =
1863 scrollbar_mouse_pos - gi->scrollbar.position;
1866 else if (gi->type & GD_TYPE_SELECTBOX)
1868 /* keep selectbox open in case of over-large selectbox */
1869 keep_selectbox_open = (mouse_inside_select_line &&
1870 mouse_inside_select_area);
1873 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1875 gi->state = GD_BUTTON_PRESSED;
1876 gi->event.type = GD_EVENT_PRESSED;
1877 gi->event.button = button;
1878 gi->event.off_borders = FALSE;
1880 if (gi->event_mask & GD_EVENT_PRESSED)
1881 gi->callback_action(gi);
1884 if (gadget_pressed_repeated)
1886 gi->event.type = GD_EVENT_PRESSED;
1888 if (gi->event_mask & GD_EVENT_REPEATED && gadget_pressed_delay_reached)
1889 gi->callback_action(gi);
1894 if (gi->type & GD_TYPE_BUTTON)
1896 if (gadget_moving_inside && gi->state == GD_BUTTON_UNPRESSED)
1897 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1898 else if (gadget_moving_off_borders && gi->state == GD_BUTTON_PRESSED)
1899 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1901 else if (gi->type & GD_TYPE_SELECTBOX && !keep_selectbox_open)
1903 int old_index = gi->selectbox.current_index;
1905 /* if mouse moving inside activated selectbox, select value */
1906 if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
1907 gi->selectbox.current_index =
1908 (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font);
1910 if (gi->selectbox.current_index < 0)
1911 gi->selectbox.current_index = 0;
1912 else if (gi->selectbox.current_index > gi->selectbox.num_values - 1)
1913 gi->selectbox.current_index = gi->selectbox.num_values - 1;
1915 if (gi->selectbox.current_index != old_index)
1916 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1918 else if (gi->type & GD_TYPE_SCROLLBAR)
1920 struct GadgetScrollbar *gs = &gi->scrollbar;
1921 int old_item_position = gs->item_position;
1923 gs->position = scrollbar_mouse_pos - gs->drag_position;
1925 /* make sure to always precisely reach end positions when dragging */
1926 if (gs->position <= 0)
1929 gs->item_position = 0;
1931 else if (gs->position >= gs->position_max)
1933 gs->position = gs->position_max;
1934 gs->item_position = gs->items_max - gs->items_visible;
1939 gs->items_max * (gs->position + gs->correction) / gs->size_max_cmp;
1942 if (gs->item_position < 0)
1943 gs->item_position = 0;
1944 if (gs->item_position > gs->items_max - 1)
1945 gs->item_position = gs->items_max - 1;
1947 if (old_item_position != gs->item_position)
1949 gi->event.item_position = gs->item_position;
1950 changed_position = TRUE;
1953 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1956 gi->state = (gadget_moving_inside || gadget_draggable ?
1957 GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
1958 gi->event.type = GD_EVENT_MOVING;
1959 gi->event.off_borders = gadget_moving_off_borders;
1961 if (gi->event_mask & GD_EVENT_MOVING && changed_position &&
1962 (gadget_moving_inside || gi->event_mask & GD_EVENT_OFF_BORDERS))
1963 gi->callback_action(gi);
1966 if (gadget_released_inside)
1968 boolean deactivate_gadget = TRUE;
1969 boolean gadget_changed = TRUE;
1971 if (gi->type & GD_TYPE_SELECTBOX)
1973 if (keep_selectbox_open ||
1974 mouse_released_where_pressed ||
1975 !gadget_released_inside_select_area ||
1976 !CURRENT_OPTION_SELECTABLE(gi)) /* selectbox stays open */
1978 deactivate_gadget = FALSE;
1979 gadget_changed = FALSE;
1981 else if (gi->selectbox.index != gi->selectbox.current_index)
1982 gi->selectbox.index = gi->selectbox.current_index;
1984 gadget_changed = FALSE;
1987 if (deactivate_gadget &&
1988 !(gi->type & GD_TYPE_TEXT_INPUT ||
1989 gi->type & GD_TYPE_TEXT_AREA)) /* text input stays open */
1990 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1992 gi->state = GD_BUTTON_UNPRESSED;
1993 gi->event.type = GD_EVENT_RELEASED;
1995 if ((gi->event_mask & GD_EVENT_RELEASED) && gadget_changed)
1997 gi->callback_action(gi);
2001 if (gadget_released_off_borders)
2003 if (gi->type & GD_TYPE_SCROLLBAR)
2004 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2006 gi->state = GD_BUTTON_UNPRESSED;
2007 gi->event.type = GD_EVENT_RELEASED;
2009 if (gi->event_mask & GD_EVENT_RELEASED &&
2010 gi->event_mask & GD_EVENT_OFF_BORDERS)
2011 gi->callback_action(gi);
2014 /* handle gadgets unmapped/mapped between pressing and releasing */
2015 if (release_event && !gadget_released && new_gi)
2016 new_gi->state = GD_BUTTON_UNPRESSED;
2018 return (gadget_pressed || gadget_pressed_repeated ||
2019 gadget_released || gadget_moving);
2022 static void insertCharIntoTextArea(struct GadgetInfo *gi, char c)
2024 char text[MAX_GADGET_TEXTSIZE + 1];
2025 int cursor_position = gi->textarea.cursor_position;
2027 if (strlen(gi->textarea.value) >= MAX_GADGET_TEXTSIZE) /* no space left */
2030 strcpy(text, gi->textarea.value);
2031 strcpy(&gi->textarea.value[cursor_position + 1], &text[cursor_position]);
2032 gi->textarea.value[cursor_position] = c;
2034 setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1);
2037 boolean HandleGadgetsKeyInput(Key key)
2039 struct GadgetInfo *gi = last_gi;
2041 if (gi == NULL || !gi->mapped ||
2042 !(gi->type & GD_TYPE_TEXT_INPUT ||
2043 gi->type & GD_TYPE_TEXT_AREA ||
2044 gi->type & GD_TYPE_SELECTBOX))
2047 if (key == KSYM_Return) /* valid for both text input and selectbox */
2049 boolean gadget_changed = ((gi->event_mask & GD_EVENT_TEXT_RETURN) != 0);
2051 if (gi->type & GD_TYPE_TEXT_INPUT)
2053 CheckRangeOfNumericInputGadget(gi);
2055 if (!strEqual(gi->textinput.last_value, gi->textinput.value))
2056 strcpy(gi->textinput.last_value, gi->textinput.value);
2058 gadget_changed = FALSE;
2060 else if (gi->type & GD_TYPE_SELECTBOX)
2062 if (gi->selectbox.index != gi->selectbox.current_index)
2063 gi->selectbox.index = gi->selectbox.current_index;
2065 gadget_changed = FALSE;
2068 if (gi->type & GD_TYPE_TEXT_AREA)
2070 insertCharIntoTextArea(gi, '\n');
2072 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2076 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2078 gi->event.type = GD_EVENT_TEXT_RETURN;
2084 gi->callback_action(gi);
2086 else if (gi->type & GD_TYPE_TEXT_INPUT) /* only valid for text input */
2088 char text[MAX_GADGET_TEXTSIZE + 1];
2089 int text_length = strlen(gi->textinput.value);
2090 int cursor_pos = gi->textinput.cursor_position;
2091 char letter = getCharFromKey(key);
2092 boolean legal_letter = (gi->type == GD_TYPE_TEXT_INPUT_NUMERIC ?
2093 letter >= '0' && letter <= '9' :
2096 if (legal_letter && text_length < gi->textinput.size)
2098 strcpy(text, gi->textinput.value);
2099 strcpy(&gi->textinput.value[cursor_pos + 1], &text[cursor_pos]);
2100 gi->textinput.value[cursor_pos] = letter;
2101 gi->textinput.cursor_position++;
2103 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2105 else if (key == KSYM_Left && cursor_pos > 0)
2107 gi->textinput.cursor_position--;
2109 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2111 else if (key == KSYM_Right && cursor_pos < text_length)
2113 gi->textinput.cursor_position++;
2115 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2117 else if (key == KSYM_BackSpace && cursor_pos > 0)
2119 strcpy(text, gi->textinput.value);
2120 strcpy(&gi->textinput.value[cursor_pos - 1], &text[cursor_pos]);
2121 gi->textinput.cursor_position--;
2123 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2125 else if (key == KSYM_Delete && cursor_pos < text_length)
2127 strcpy(text, gi->textinput.value);
2128 strcpy(&gi->textinput.value[cursor_pos], &text[cursor_pos + 1]);
2130 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2133 else if (gi->type & GD_TYPE_TEXT_AREA) /* only valid for text area */
2135 char text[MAX_GADGET_TEXTSIZE + 1];
2136 int text_length = strlen(gi->textarea.value);
2137 int area_ysize = gi->textarea.ysize;
2138 int cursor_x_pref = gi->textarea.cursor_x_preferred;
2139 int cursor_y = gi->textarea.cursor_y;
2140 int cursor_pos = gi->textarea.cursor_position;
2141 char letter = getCharFromKey(key);
2142 boolean legal_letter = (letter != 0);
2146 insertCharIntoTextArea(gi, letter);
2148 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2150 else if (key == KSYM_Left && cursor_pos > 0)
2152 setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1);
2154 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2156 else if (key == KSYM_Right && cursor_pos < text_length)
2158 setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1);
2160 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2162 else if (key == KSYM_Up && cursor_y > 0)
2164 setTextAreaCursorXY(gi, cursor_x_pref, cursor_y - 1);
2165 gi->textarea.cursor_x_preferred = cursor_x_pref;
2167 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2169 else if (key == KSYM_Down && cursor_y < area_ysize - 1)
2171 setTextAreaCursorXY(gi, cursor_x_pref, cursor_y + 1);
2172 gi->textarea.cursor_x_preferred = cursor_x_pref;
2174 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2176 else if (key == KSYM_BackSpace && cursor_pos > 0)
2178 strcpy(text, gi->textarea.value);
2179 strcpy(&gi->textarea.value[cursor_pos - 1], &text[cursor_pos]);
2181 setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1);
2183 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2185 else if (key == KSYM_Delete && cursor_pos < text_length)
2187 strcpy(text, gi->textarea.value);
2188 strcpy(&gi->textarea.value[cursor_pos], &text[cursor_pos + 1]);
2190 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2193 else if (gi->type & GD_TYPE_SELECTBOX) /* only valid for selectbox */
2195 int index = gi->selectbox.current_index;
2196 int num_values = gi->selectbox.num_values;
2198 if (key == KSYM_Up && index > 0)
2200 gi->selectbox.current_index--;
2202 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2204 else if (key == KSYM_Down && index < num_values - 1)
2206 gi->selectbox.current_index++;
2208 DrawGadget(gi, DG_PRESSED, gi->direct_draw);