1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1994-2002 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
22 /* values for DrawGadget() */
23 #define DG_UNPRESSED 0
29 static struct GadgetInfo *gadget_list_first_entry = NULL;
30 static struct GadgetInfo *gadget_list_last_entry = NULL;
31 static struct GadgetInfo *last_info_gi = NULL;
32 static int next_free_gadget_id = 1;
33 static boolean gadget_id_wrapped = FALSE;
35 static struct GadgetInfo *getGadgetInfoFromGadgetID(int id)
37 struct GadgetInfo *gi = gadget_list_first_entry;
39 while (gi && gi->id != id)
45 static int getNewGadgetID()
47 int id = next_free_gadget_id++;
49 if (next_free_gadget_id <= 0) /* counter overrun */
51 gadget_id_wrapped = TRUE; /* now we must check each ID */
52 next_free_gadget_id = 0;
55 if (gadget_id_wrapped)
57 next_free_gadget_id++;
58 while (getGadgetInfoFromGadgetID(next_free_gadget_id) != NULL)
59 next_free_gadget_id++;
62 if (next_free_gadget_id <= 0) /* cannot get new gadget id */
63 Error(ERR_EXIT, "too much gadgets -- this should not happen");
68 static struct GadgetInfo *getGadgetInfoFromMousePosition(int mx, int my,
71 struct GadgetInfo *gi;
73 /* first check for scrollbars in case of mouse scroll wheel button events */
74 if (button == 4 || button == 5)
76 boolean check_horizontal = (GetKeyModState() & KMOD_Control);
78 /* check for the first active scrollbar with matching mouse wheel area */
79 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
81 if (gi->mapped && gi->active &&
82 ((gi->type & GD_TYPE_SCROLLBAR_HORIZONTAL && check_horizontal) ||
83 (gi->type & GD_TYPE_SCROLLBAR_VERTICAL && !check_horizontal)) &&
84 mx >= gi->wheelarea.x && mx < gi->wheelarea.x + gi->wheelarea.width &&
85 my >= gi->wheelarea.y && my < gi->wheelarea.y + gi->wheelarea.height)
89 /* no active scrollbar found -- ignore this scroll wheel button event */
93 /* open selectboxes may overlap other active gadgets, so check them first */
94 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
96 if (gi->mapped && gi->active &&
97 gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open &&
98 mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width &&
99 my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
103 /* check all other gadgets */
104 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
106 if (gi->mapped && gi->active &&
107 mx >= gi->x && mx < gi->x + gi->width &&
108 my >= gi->y && my < gi->y + gi->height)
115 static void setTextAreaCursorExt(struct GadgetInfo *gi, boolean set_cursor_pos)
117 char *text = gi->textarea.value;
118 int area_xsize = gi->textarea.xsize;
119 int area_ysize = gi->textarea.ysize;
120 int cursor_position = gi->textarea.cursor_position;
121 int cursor_x = gi->textarea.cursor_x;
122 int cursor_y = gi->textarea.cursor_y;
129 if (set_cursor_pos) /* x/y => position */
131 if (y == cursor_y && (x == cursor_x || (x < cursor_x && *text == '\n')))
134 else /* position => x/y */
136 if (pos == cursor_position)
140 if (x + 1 >= area_xsize || *text == '\n')
142 if (y + 1 >= area_ysize)
155 gi->textarea.cursor_x = x;
156 gi->textarea.cursor_y = y;
157 gi->textarea.cursor_x_preferred = x;
158 gi->textarea.cursor_position = pos;
161 static void setTextAreaCursorXY(struct GadgetInfo *gi, int x, int y)
163 gi->textarea.cursor_x = x;
164 gi->textarea.cursor_y = y;
166 setTextAreaCursorExt(gi, TRUE);
169 static void setTextAreaCursorPosition(struct GadgetInfo *gi, int pos)
171 gi->textarea.cursor_position = pos;
173 setTextAreaCursorExt(gi, FALSE);
176 static void default_callback_info(void *ptr)
181 static void default_callback_action(void *ptr)
186 static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct)
188 struct GadgetDesign *gd;
189 int state = (pressed ? GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
190 boolean redraw_selectbox = FALSE;
195 gd = (!gi->active ? &gi->alt_design[state] :
196 gi->checked ? &gi->alt_design[state] : &gi->design[state]);
200 case GD_TYPE_NORMAL_BUTTON:
201 case GD_TYPE_CHECK_BUTTON:
202 case GD_TYPE_RADIO_BUTTON:
203 BlitBitmapOnBackground(gd->bitmap, drawto,
204 gd->x, gd->y, gi->width, gi->height,
206 if (gi->deco.design.bitmap)
207 BlitBitmap(gi->deco.design.bitmap, drawto,
208 gi->deco.design.x, gi->deco.design.y,
209 gi->deco.width, gi->deco.height,
210 gi->x + gi->deco.x + (pressed ? gi->deco.xshift : 0),
211 gi->y + gi->deco.y + (pressed ? gi->deco.yshift : 0));
214 case GD_TYPE_TEXT_BUTTON:
217 int font_nr = (gi->active ? gi->font_active : gi->font);
218 int font_width = getFontWidth(font_nr);
219 int border_x = gi->border.xsize;
220 int border_y = gi->border.ysize;
221 int text_size = strlen(gi->textbutton.value);
222 int text_start = (gi->width - text_size * font_width) / 2;
224 /* left part of gadget */
225 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
226 border_x, gi->height, gi->x, gi->y);
228 /* middle part of gadget */
229 for (i=0; i < gi->textbutton.size; i++)
230 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
231 font_width, gi->height,
232 gi->x + border_x + i * font_width, gi->y);
234 /* right part of gadget */
235 BlitBitmapOnBackground(gd->bitmap, drawto,
236 gd->x + gi->border.width - border_x, gd->y,
237 border_x, gi->height,
238 gi->x + gi->width - border_x, gi->y);
240 /* gadget text value */
242 gi->x + text_start + (pressed ? gi->deco.xshift : 0),
243 gi->y + border_y + (pressed ? gi->deco.yshift : 0),
244 gi->textbutton.value, font_nr, BLIT_MASKED);
248 case GD_TYPE_TEXT_INPUT_ALPHANUMERIC:
249 case GD_TYPE_TEXT_INPUT_NUMERIC:
253 char cursor_string[2];
254 char text[MAX_GADGET_TEXTSIZE + 1];
255 int font_nr = (pressed ? gi->font_active : gi->font);
256 int font_width = getFontWidth(font_nr);
257 int border_x = gi->border.xsize;
258 int border_y = gi->border.ysize;
260 /* left part of gadget */
261 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
262 border_x, gi->height, gi->x, gi->y);
264 /* middle part of gadget */
265 for (i=0; i < gi->textinput.size + 1; i++)
266 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
267 font_width, gi->height,
268 gi->x + border_x + i * font_width, gi->y);
270 /* right part of gadget */
271 BlitBitmapOnBackground(gd->bitmap, drawto,
272 gd->x + gi->border.width - border_x, gd->y,
273 border_x, gi->height,
274 gi->x + gi->width - border_x, gi->y);
277 strcpy(text, gi->textinput.value);
280 /* gadget text value */
282 gi->x + border_x, gi->y + border_y, text,
283 font_nr, BLIT_MASKED);
285 cursor_letter = gi->textinput.value[gi->textinput.cursor_position];
286 cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' ');
287 cursor_string[1] = '\0';
289 /* draw cursor, if active */
293 gi->textinput.cursor_position * font_width,
294 gi->y + border_y, cursor_string,
295 font_nr, BLIT_INVERSE);
299 case GD_TYPE_TEXT_AREA:
303 char cursor_string[2];
304 int font_nr = (pressed ? gi->font_active : gi->font);
305 int font_width = getFontWidth(font_nr);
306 int font_height = getFontHeight(font_nr);
307 int border_x = gi->border.xsize;
308 int border_y = gi->border.ysize;
309 int gd_height = 2 * border_y + font_height;
311 /* top left part of gadget border */
312 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
313 border_x, border_y, gi->x, gi->y);
315 /* top middle part of gadget border */
316 for (i=0; i < gi->textarea.xsize; i++)
317 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
318 font_width, border_y,
319 gi->x + border_x + i * font_width, gi->y);
321 /* top right part of gadget border */
322 BlitBitmapOnBackground(gd->bitmap, drawto,
323 gd->x + gi->border.width - border_x, gd->y,
325 gi->x + gi->width - border_x, gi->y);
327 /* left and right part of gadget border for each row */
328 for (i=0; i < gi->textarea.ysize; i++)
330 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y,
331 border_x, font_height,
332 gi->x, gi->y + border_y + i * font_height);
333 BlitBitmapOnBackground(gd->bitmap, drawto,
334 gd->x + gi->border.width - border_x,
336 border_x, font_height,
337 gi->x + gi->width - border_x,
338 gi->y + border_y + i * font_height);
341 /* bottom left part of gadget border */
342 BlitBitmapOnBackground(gd->bitmap, drawto,
343 gd->x, gd->y + gd_height - border_y,
345 gi->x, gi->y + gi->height - border_y);
347 /* bottom middle part of gadget border */
348 for (i=0; i < gi->textarea.xsize; i++)
349 BlitBitmapOnBackground(gd->bitmap, drawto,
351 gd->y + gd_height - border_y,
352 font_width, border_y,
353 gi->x + border_x + i * font_width,
354 gi->y + gi->height - border_y);
356 /* bottom right part of gadget border */
357 BlitBitmapOnBackground(gd->bitmap, drawto,
358 gd->x + gi->border.width - border_x,
359 gd->y + gd_height - border_y,
361 gi->x + gi->width - border_x,
362 gi->y + gi->height - border_y);
364 ClearRectangleOnBackground(drawto,
367 gi->width - 2 * border_x,
368 gi->height - 2 * border_y);
370 /* gadget text value */
371 DrawTextToTextArea(gi->x + border_x, gi->y + border_y,
372 gi->textarea.value, font_nr, gi->textarea.xsize,
373 gi->textarea.xsize, gi->textarea.ysize,
376 cursor_letter = gi->textarea.value[gi->textarea.cursor_position];
377 cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' ');
378 cursor_string[1] = '\0';
380 /* draw cursor, if active */
383 gi->x + border_x + gi->textarea.cursor_x * font_width,
384 gi->y + border_y + gi->textarea.cursor_y * font_height,
386 font_nr, BLIT_INVERSE);
390 case GD_TYPE_SELECTBOX:
393 char text[MAX_GADGET_TEXTSIZE + 1];
394 int font_nr = (pressed ? gi->font_active : gi->font);
395 int font_width = getFontWidth(font_nr);
396 int font_height = getFontHeight(font_nr);
397 int border_x = gi->border.xsize;
398 int border_y = gi->border.ysize;
399 int button = gi->border.xsize_selectbutton;
400 int width_inner = gi->border.width - button - 2 * border_x;
401 int box_width = gi->selectbox.width;
402 int box_height = gi->selectbox.height;
404 /* left part of gadget */
405 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
406 border_x, gi->height, gi->x, gi->y);
408 /* middle part of gadget */
409 for (i=0; i < gi->selectbox.size; i++)
410 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
411 font_width, gi->height,
412 gi->x + border_x + i * font_width, gi->y);
414 /* button part of gadget */
415 BlitBitmapOnBackground(gd->bitmap, drawto,
416 gd->x + border_x + width_inner, gd->y,
418 gi->x + gi->width - border_x - button, gi->y);
420 /* right part of gadget */
421 BlitBitmapOnBackground(gd->bitmap, drawto,
422 gd->x + gi->border.width - border_x, gd->y,
423 border_x, gi->height,
424 gi->x + gi->width - border_x, gi->y);
427 strncpy(text, gi->selectbox.options[gi->selectbox.index].text,
429 text[gi->selectbox.size] = '\0';
431 /* gadget text value */
432 DrawTextExt(drawto, gi->x + border_x, gi->y + border_y, text,
433 font_nr, BLIT_MASKED);
437 if (!gi->selectbox.open)
439 gi->selectbox.open = TRUE;
440 gi->selectbox.stay_open = FALSE;
441 gi->selectbox.current_index = gi->selectbox.index;
443 /* save background under selectbox */
444 BlitBitmap(drawto, gfx.field_save_buffer,
445 gi->selectbox.x, gi->selectbox.y,
446 gi->selectbox.width, gi->selectbox.height,
447 gi->selectbox.x, gi->selectbox.y);
450 /* draw open selectbox */
452 /* top left part of gadget border */
453 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
455 gi->selectbox.x, gi->selectbox.y);
457 /* top middle part of gadget border */
458 for (i=0; i < gi->selectbox.size; i++)
459 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
460 font_width, border_y,
461 gi->selectbox.x + border_x + i * font_width,
464 /* top button part of gadget border */
465 BlitBitmapOnBackground(gd->bitmap, drawto,
466 gd->x + border_x + width_inner, gd->y,
468 gi->selectbox.x + box_width -border_x -button,
471 /* top right part of gadget border */
472 BlitBitmapOnBackground(gd->bitmap, drawto,
473 gd->x + gi->border.width - border_x, gd->y,
475 gi->selectbox.x + box_width - border_x,
478 /* left and right part of gadget border for each row */
479 for (i=0; i < gi->selectbox.num_values; i++)
481 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y,
482 border_x, font_height,
484 gi->selectbox.y + border_y + i*font_height);
485 BlitBitmapOnBackground(gd->bitmap, drawto,
486 gd->x + gi->border.width - border_x,
488 border_x, font_height,
489 gi->selectbox.x + box_width - border_x,
490 gi->selectbox.y + border_y + i*font_height);
493 /* bottom left part of gadget border */
494 BlitBitmapOnBackground(gd->bitmap, drawto,
495 gd->x, gd->y + gi->height - border_y,
498 gi->selectbox.y + box_height - border_y);
500 /* bottom middle part of gadget border */
501 for (i=0; i < gi->selectbox.size; i++)
502 BlitBitmapOnBackground(gd->bitmap, drawto,
504 gd->y + gi->height - border_y,
505 font_width, border_y,
506 gi->selectbox.x + border_x + i * font_width,
507 gi->selectbox.y + box_height - border_y);
509 /* bottom button part of gadget border */
510 BlitBitmapOnBackground(gd->bitmap, drawto,
511 gd->x + border_x + width_inner,
512 gd->y + gi->height - border_y,
514 gi->selectbox.x + box_width -border_x -button,
515 gi->selectbox.y + box_height - border_y);
517 /* bottom right part of gadget border */
518 BlitBitmapOnBackground(gd->bitmap, drawto,
519 gd->x + gi->border.width - border_x,
520 gd->y + gi->height - border_y,
522 gi->selectbox.x + box_width - border_x,
523 gi->selectbox.y + box_height - border_y);
525 ClearRectangleOnBackground(drawto,
526 gi->selectbox.x + border_x,
527 gi->selectbox.y + border_y,
528 gi->selectbox.width - 2 * border_x,
529 gi->selectbox.height - 2 * border_y);
531 /* selectbox text values */
532 for (i=0; i < gi->selectbox.num_values; i++)
534 int mask_mode = BLIT_MASKED;
536 strncpy(text, gi->selectbox.options[i].text, gi->selectbox.size);
537 text[gi->selectbox.size] = '\0';
539 if (i == gi->selectbox.current_index)
541 FillRectangle(drawto,
542 gi->selectbox.x + border_x,
543 gi->selectbox.y + border_y + i * font_height,
544 gi->selectbox.width - 2 * border_x, font_height,
545 gi->selectbox.inverse_color);
547 /* prevent use of cursor graphic by drawing at least two chars */
549 text[gi->selectbox.size] = '\0';
551 mask_mode = BLIT_INVERSE;
555 gi->selectbox.x + border_x,
556 gi->selectbox.y + border_y + i * font_height, text,
560 redraw_selectbox = TRUE;
562 else if (gi->selectbox.open)
564 gi->selectbox.open = FALSE;
566 /* restore background under selectbox */
567 BlitBitmap(gfx.field_save_buffer, drawto,
568 gi->selectbox.x, gi->selectbox.y,
569 gi->selectbox.width, gi->selectbox.height,
570 gi->selectbox.x, gi->selectbox.y);
572 /* redraw closed selectbox */
573 DrawGadget(gi, FALSE, FALSE);
575 redraw_selectbox = TRUE;
580 case GD_TYPE_SCROLLBAR_VERTICAL:
584 int ypos = gi->y + gi->scrollbar.position;
585 int design_full = gi->width;
586 int design_body = design_full - 2 * gi->border.ysize;
587 int size_full = gi->scrollbar.size;
588 int size_body = size_full - 2 * gi->border.ysize;
589 int num_steps = size_body / design_body;
590 int step_size_remain = size_body - num_steps * design_body;
592 /* clear scrollbar area */
593 ClearRectangleOnBackground(backbuffer, gi->x, gi->y,
594 gi->width, gi->height);
596 /* upper part of gadget */
597 BlitBitmapOnBackground(gd->bitmap, drawto,
599 gi->width, gi->border.ysize,
602 /* middle part of gadget */
603 for (i=0; i < num_steps; i++)
604 BlitBitmapOnBackground(gd->bitmap, drawto,
605 gd->x, gd->y + gi->border.ysize,
606 gi->width, design_body,
608 ypos + gi->border.ysize + i * design_body);
610 /* remaining middle part of gadget */
611 if (step_size_remain > 0)
612 BlitBitmapOnBackground(gd->bitmap, drawto,
613 gd->x, gd->y + gi->border.ysize,
614 gi->width, step_size_remain,
616 ypos + gi->border.ysize
617 + num_steps * design_body);
619 /* lower part of gadget */
620 BlitBitmapOnBackground(gd->bitmap, drawto,
621 gd->x, gd->y + design_full - gi->border.ysize,
622 gi->width, gi->border.ysize,
623 xpos, ypos + size_full - gi->border.ysize);
627 case GD_TYPE_SCROLLBAR_HORIZONTAL:
630 int xpos = gi->x + gi->scrollbar.position;
632 int design_full = gi->height;
633 int design_body = design_full - 2 * gi->border.xsize;
634 int size_full = gi->scrollbar.size;
635 int size_body = size_full - 2 * gi->border.xsize;
636 int num_steps = size_body / design_body;
637 int step_size_remain = size_body - num_steps * design_body;
639 /* clear scrollbar area */
640 ClearRectangleOnBackground(backbuffer, gi->x, gi->y,
641 gi->width, gi->height);
643 /* left part of gadget */
644 BlitBitmapOnBackground(gd->bitmap, drawto,
646 gi->border.xsize, gi->height,
649 /* middle part of gadget */
650 for (i=0; i < num_steps; i++)
651 BlitBitmapOnBackground(gd->bitmap, drawto,
652 gd->x + gi->border.xsize, gd->y,
653 design_body, gi->height,
654 xpos + gi->border.xsize + i * design_body,
657 /* remaining middle part of gadget */
658 if (step_size_remain > 0)
659 BlitBitmapOnBackground(gd->bitmap, drawto,
660 gd->x + gi->border.xsize, gd->y,
661 step_size_remain, gi->height,
662 xpos + gi->border.xsize
663 + num_steps * design_body,
666 /* right part of gadget */
667 BlitBitmapOnBackground(gd->bitmap, drawto,
668 gd->x + design_full - gi->border.xsize, gd->y,
669 gi->border.xsize, gi->height,
670 xpos + size_full - gi->border.xsize, ypos);
680 BlitBitmap(drawto, window,
681 gi->x, gi->y, gi->width, gi->height, gi->x, gi->y);
683 if (gi->type == GD_TYPE_SELECTBOX && redraw_selectbox)
684 BlitBitmap(drawto, window,
685 gi->selectbox.x, gi->selectbox.y,
686 gi->selectbox.width, gi->selectbox.height,
687 gi->selectbox.x, gi->selectbox.y);
690 redraw_mask |= (gi->x < gfx.sx + gfx.sxsize ? REDRAW_FIELD :
691 gi->y < gfx.dy + gfx.dysize ? REDRAW_DOOR_1 :
692 gi->y > gfx.vy ? REDRAW_DOOR_2 : REDRAW_DOOR_3);
695 static int get_minimal_size_for_numeric_input(int minmax_value)
697 int min_size = 1; /* value needs at least one digit */
700 /* add number of digits needed for absolute value */
701 for (i = 10; i <= ABS(minmax_value); i *= 10)
704 /* if min/max value is negative, add one digit for minus sign */
705 if (minmax_value < 0)
711 static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap)
718 while (tag != GDI_END)
723 gi->custom_id = va_arg(ap, int);
726 case GDI_CUSTOM_TYPE_ID:
727 gi->custom_type_id = va_arg(ap, int);
732 int max_textsize = MAX_INFO_TEXTSIZE - 1;
733 char *text = va_arg(ap, char *);
736 strncpy(gi->info_text, text, max_textsize);
740 gi->info_text[max_textsize] = '\0';
745 gi->x = va_arg(ap, int);
749 gi->y = va_arg(ap, int);
753 gi->width = va_arg(ap, int);
757 gi->height = va_arg(ap, int);
761 gi->type = va_arg(ap, unsigned int);
765 gi->state = va_arg(ap, unsigned int);
769 /* take care here: "boolean" is typedef'ed as "unsigned char",
770 which gets promoted to "int" */
771 gi->active = (boolean)va_arg(ap, int);
774 case GDI_DIRECT_DRAW:
775 /* take care here: "boolean" is typedef'ed as "unsigned char",
776 which gets promoted to "int" */
777 gi->direct_draw = (boolean)va_arg(ap, int);
781 /* take care here: "boolean" is typedef'ed as "unsigned char",
782 which gets promoted to "int" */
783 gi->checked = (boolean)va_arg(ap, int);
787 gi->radio_nr = va_arg(ap, unsigned int);
790 case GDI_NUMBER_VALUE:
791 gi->textinput.number_value = va_arg(ap, int);
792 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
793 strcpy(gi->textinput.last_value, gi->textinput.value);
794 gi->textinput.cursor_position = strlen(gi->textinput.value);
798 gi->textinput.number_min = va_arg(ap, int);
799 if (gi->textinput.number_value < gi->textinput.number_min)
801 gi->textinput.number_value = gi->textinput.number_min;
802 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
803 strcpy(gi->textinput.last_value, gi->textinput.value);
808 gi->textinput.number_max = va_arg(ap, int);
809 if (gi->textinput.number_value > gi->textinput.number_max)
811 gi->textinput.number_value = gi->textinput.number_max;
812 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
813 strcpy(gi->textinput.last_value, gi->textinput.value);
819 int max_textsize = MAX_GADGET_TEXTSIZE;
821 if (gi->textinput.size)
822 max_textsize = MIN(gi->textinput.size, MAX_GADGET_TEXTSIZE - 1);
824 strncpy(gi->textinput.value, va_arg(ap, char *), max_textsize);
825 strcpy(gi->textinput.last_value, gi->textinput.value);
827 gi->textinput.value[max_textsize] = '\0';
828 gi->textinput.cursor_position = strlen(gi->textinput.value);
830 /* same tag also used for other gadget definitions */
831 strcpy(gi->textbutton.value, gi->textinput.value);
832 strcpy(gi->textarea.value, gi->textinput.value);
833 strcpy(gi->textarea.last_value, gi->textinput.value);
839 int tag_value = va_arg(ap, int);
840 int max_textsize = MIN(tag_value, MAX_GADGET_TEXTSIZE - 1);
842 gi->textinput.size = max_textsize;
843 gi->textinput.value[max_textsize] = '\0';
844 strcpy(gi->textinput.last_value, gi->textinput.value);
846 /* same tag also used for other gadget definitions */
848 gi->textarea.size = max_textsize;
849 gi->textarea.value[max_textsize] = '\0';
850 strcpy(gi->textarea.last_value, gi->textinput.value);
852 gi->textbutton.size = max_textsize;
853 gi->textbutton.value[max_textsize] = '\0';
855 gi->selectbox.size = gi->textinput.size;
860 gi->font = va_arg(ap, int);
861 if (gi->font_active == 0)
862 gi->font_active = gi->font;
865 case GDI_TEXT_FONT_ACTIVE:
866 gi->font_active = va_arg(ap, int);
869 case GDI_SELECTBOX_OPTIONS:
870 gi->selectbox.options = va_arg(ap, struct ValueTextInfo *);
873 case GDI_SELECTBOX_INDEX:
874 gi->selectbox.index = va_arg(ap, int);
877 case GDI_DESIGN_UNPRESSED:
878 gi->design[GD_BUTTON_UNPRESSED].bitmap = va_arg(ap, Bitmap *);
879 gi->design[GD_BUTTON_UNPRESSED].x = va_arg(ap, int);
880 gi->design[GD_BUTTON_UNPRESSED].y = va_arg(ap, int);
883 case GDI_DESIGN_PRESSED:
884 gi->design[GD_BUTTON_PRESSED].bitmap = va_arg(ap, Bitmap *);
885 gi->design[GD_BUTTON_PRESSED].x = va_arg(ap, int);
886 gi->design[GD_BUTTON_PRESSED].y = va_arg(ap, int);
889 case GDI_ALT_DESIGN_UNPRESSED:
890 gi->alt_design[GD_BUTTON_UNPRESSED].bitmap= va_arg(ap, Bitmap *);
891 gi->alt_design[GD_BUTTON_UNPRESSED].x = va_arg(ap, int);
892 gi->alt_design[GD_BUTTON_UNPRESSED].y = va_arg(ap, int);
895 case GDI_ALT_DESIGN_PRESSED:
896 gi->alt_design[GD_BUTTON_PRESSED].bitmap = va_arg(ap, Bitmap *);
897 gi->alt_design[GD_BUTTON_PRESSED].x = va_arg(ap, int);
898 gi->alt_design[GD_BUTTON_PRESSED].y = va_arg(ap, int);
901 case GDI_BORDER_SIZE:
902 gi->border.xsize = va_arg(ap, int);
903 gi->border.ysize = va_arg(ap, int);
906 case GDI_BORDER_SIZE_SELECTBUTTON:
907 gi->border.xsize_selectbutton = va_arg(ap, int);
910 case GDI_DESIGN_WIDTH:
911 gi->border.width = va_arg(ap, int);
914 case GDI_DECORATION_DESIGN:
915 gi->deco.design.bitmap = va_arg(ap, Bitmap *);
916 gi->deco.design.x = va_arg(ap, int);
917 gi->deco.design.y = va_arg(ap, int);
920 case GDI_DECORATION_POSITION:
921 gi->deco.x = va_arg(ap, int);
922 gi->deco.y = va_arg(ap, int);
925 case GDI_DECORATION_SIZE:
926 gi->deco.width = va_arg(ap, int);
927 gi->deco.height = va_arg(ap, int);
930 case GDI_DECORATION_SHIFTING:
931 gi->deco.xshift = va_arg(ap, int);
932 gi->deco.yshift = va_arg(ap, int);
936 gi->event_mask = va_arg(ap, unsigned int);
940 gi->drawing.area_xsize = va_arg(ap, int);
941 gi->drawing.area_ysize = va_arg(ap, int);
943 /* determine dependent values for drawing area gadget, if needed */
944 if (gi->width == 0 && gi->height == 0 &&
945 gi->drawing.item_xsize !=0 && gi->drawing.item_ysize !=0)
947 gi->width = gi->drawing.area_xsize * gi->drawing.item_xsize;
948 gi->height = gi->drawing.area_ysize * gi->drawing.item_ysize;
950 else if (gi->drawing.item_xsize == 0 && gi->drawing.item_ysize == 0 &&
951 gi->width != 0 && gi->height != 0)
953 gi->drawing.item_xsize = gi->width / gi->drawing.area_xsize;
954 gi->drawing.item_ysize = gi->height / gi->drawing.area_ysize;
957 /* same tag also used for other gadget definitions */
958 gi->textarea.xsize = gi->drawing.area_xsize;
959 gi->textarea.ysize = gi->drawing.area_ysize;
961 if (gi->type & GD_TYPE_TEXT_AREA) /* force recalculation */
970 gi->drawing.item_xsize = va_arg(ap, int);
971 gi->drawing.item_ysize = va_arg(ap, int);
973 /* determine dependent values for drawing area gadget, if needed */
974 if (gi->width == 0 && gi->height == 0 &&
975 gi->drawing.area_xsize !=0 && gi->drawing.area_ysize !=0)
977 gi->width = gi->drawing.area_xsize * gi->drawing.item_xsize;
978 gi->height = gi->drawing.area_ysize * gi->drawing.item_ysize;
980 else if (gi->drawing.area_xsize == 0 && gi->drawing.area_ysize == 0 &&
981 gi->width != 0 && gi->height != 0)
983 gi->drawing.area_xsize = gi->width / gi->drawing.item_xsize;
984 gi->drawing.area_ysize = gi->height / gi->drawing.item_ysize;
988 case GDI_SCROLLBAR_ITEMS_MAX:
989 gi->scrollbar.items_max = va_arg(ap, int);
992 case GDI_SCROLLBAR_ITEMS_VISIBLE:
993 gi->scrollbar.items_visible = va_arg(ap, int);
996 case GDI_SCROLLBAR_ITEM_POSITION:
997 gi->scrollbar.item_position = va_arg(ap, int);
1000 case GDI_WHEEL_AREA_X:
1001 gi->wheelarea.x = va_arg(ap, int);
1004 case GDI_WHEEL_AREA_Y:
1005 gi->wheelarea.y = va_arg(ap, int);
1008 case GDI_WHEEL_AREA_WIDTH:
1009 gi->wheelarea.width = va_arg(ap, int);
1012 case GDI_WHEEL_AREA_HEIGHT:
1013 gi->wheelarea.height = va_arg(ap, int);
1016 case GDI_CALLBACK_INFO:
1017 gi->callback_info = va_arg(ap, gadget_function);
1020 case GDI_CALLBACK_ACTION:
1021 gi->callback_action = va_arg(ap, gadget_function);
1025 Error(ERR_EXIT, "HandleGadgetTags(): unknown tag %d", tag);
1028 tag = va_arg(ap, int); /* read next tag */
1031 /* check if gadget is complete */
1032 if (gi->type != GD_TYPE_DRAWING_AREA &&
1033 (!gi->design[GD_BUTTON_UNPRESSED].bitmap ||
1034 !gi->design[GD_BUTTON_PRESSED].bitmap))
1035 Error(ERR_EXIT, "gadget incomplete (missing Bitmap)");
1037 /* adjust gadget values in relation to other gadget values */
1039 if (gi->type & GD_TYPE_TEXT_INPUT)
1041 int font_nr = gi->font_active;
1042 int font_width = getFontWidth(font_nr);
1043 int font_height = getFontHeight(font_nr);
1044 int border_xsize = gi->border.xsize;
1045 int border_ysize = gi->border.ysize;
1047 if (gi->type == GD_TYPE_TEXT_INPUT_NUMERIC)
1049 int number_min = gi->textinput.number_min;
1050 int number_max = gi->textinput.number_max;
1051 int min_size_min = get_minimal_size_for_numeric_input(number_min);
1052 int min_size_max = get_minimal_size_for_numeric_input(number_max);
1053 int min_size = MAX(min_size_min, min_size_max);
1055 /* expand gadget text input size, if maximal value is too large */
1056 if (gi->textinput.size < min_size)
1057 gi->textinput.size = min_size;
1060 gi->width = 2 * border_xsize + (gi->textinput.size + 1) * font_width;
1061 gi->height = 2 * border_ysize + font_height;
1064 if (gi->type & GD_TYPE_SELECTBOX)
1066 int font_nr = gi->font_active;
1067 int font_width = getFontWidth(font_nr);
1068 int font_height = getFontHeight(font_nr);
1069 int border_xsize = gi->border.xsize;
1070 int border_ysize = gi->border.ysize;
1071 int button_size = gi->border.xsize_selectbutton;
1072 int bottom_screen_border = gfx.sy + gfx.sysize - font_height;
1076 gi->width = 2 * border_xsize + gi->textinput.size*font_width +button_size;
1077 gi->height = 2 * border_ysize + font_height;
1079 if (gi->selectbox.options == NULL)
1080 Error(ERR_EXIT, "selectbox gadget incomplete (missing options array)");
1082 gi->selectbox.num_values = 0;
1083 while (gi->selectbox.options[gi->selectbox.num_values].text != NULL)
1084 gi->selectbox.num_values++;
1086 /* calculate values for open selectbox */
1087 gi->selectbox.width = gi->width;
1088 gi->selectbox.height =
1089 2 * border_ysize + gi->selectbox.num_values * font_height;
1091 gi->selectbox.x = gi->x;
1092 gi->selectbox.y = gi->y + gi->height;
1093 if (gi->selectbox.y + gi->selectbox.height > bottom_screen_border)
1094 gi->selectbox.y = gi->y - gi->selectbox.height;
1095 if (gi->selectbox.y < 0)
1096 gi->selectbox.y = bottom_screen_border - gi->selectbox.height;
1098 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &src_bitmap, &src_x, &src_y);
1099 src_x += font_width / 2;
1100 src_y += font_height / 2;
1102 /* there may be esoteric cases with missing or too small font bitmap */
1103 if (src_bitmap != NULL &&
1104 src_x < src_bitmap->width && src_y < src_bitmap->height)
1105 gi->selectbox.inverse_color = GetPixel(src_bitmap, src_x, src_y);
1107 /* always start with closed selectbox */
1108 gi->selectbox.open = FALSE;
1111 if (gi->type & GD_TYPE_TEXT_INPUT_NUMERIC)
1113 struct GadgetTextInput *text = &gi->textinput;
1114 int value = text->number_value;
1116 text->number_value = (value < text->number_min ? text->number_min :
1117 value > text->number_max ? text->number_max :
1120 sprintf(text->value, "%d", text->number_value);
1123 if (gi->type & GD_TYPE_TEXT_BUTTON)
1125 int font_nr = gi->font_active;
1126 int font_width = getFontWidth(font_nr);
1127 int font_height = getFontHeight(font_nr);
1128 int border_xsize = gi->border.xsize;
1129 int border_ysize = gi->border.ysize;
1131 gi->width = 2 * border_xsize + gi->textbutton.size * font_width;
1132 gi->height = 2 * border_ysize + font_height;
1135 if (gi->type & GD_TYPE_SCROLLBAR)
1137 struct GadgetScrollbar *gs = &gi->scrollbar;
1138 int scrollbar_size_cmp;
1140 if (gi->width == 0 || gi->height == 0 ||
1141 gs->items_max == 0 || gs->items_visible == 0)
1142 Error(ERR_EXIT, "scrollbar gadget incomplete (missing tags)");
1144 /* calculate internal scrollbar values */
1145 gs->size_min = (gi->type == GD_TYPE_SCROLLBAR_VERTICAL ?
1146 gi->width : gi->height);
1147 gs->size_max = (gi->type == GD_TYPE_SCROLLBAR_VERTICAL ?
1148 gi->height : gi->width);
1150 scrollbar_size_cmp = gs->size_max * gs->items_visible / gs->items_max;
1151 gs->size = MAX(scrollbar_size_cmp, gs->size_min);
1152 gs->size_max_cmp = (gs->size_max - (gs->size - scrollbar_size_cmp));
1154 gs->position = gs->size_max_cmp * gs->item_position / gs->items_max;
1155 gs->position_max = gs->size_max - gs->size;
1156 gs->correction = gs->size_max / gs->items_max / 2;
1158 /* finetuning for maximal right/bottom position */
1159 if (gs->item_position == gs->items_max - gs->items_visible)
1160 gs->position = gs->position_max;
1163 if (gi->type & GD_TYPE_TEXT_AREA)
1165 int font_nr = gi->font_active;
1166 int font_width = getFontWidth(font_nr);
1167 int font_height = getFontHeight(font_nr);
1168 int border_xsize = gi->border.xsize;
1169 int border_ysize = gi->border.ysize;
1171 if (gi->width == 0 || gi->height == 0)
1173 gi->width = 2 * border_xsize + gi->textarea.xsize * font_width;
1174 gi->height = 2 * border_ysize + gi->textarea.ysize * font_height;
1178 gi->textarea.xsize = (gi->width - 2 * border_xsize) / font_width;
1179 gi->textarea.ysize = (gi->height - 2 * border_ysize) / font_height;
1184 void ModifyGadget(struct GadgetInfo *gi, int first_tag, ...)
1188 va_start(ap, first_tag);
1189 HandleGadgetTags(gi, first_tag, ap);
1195 void RedrawGadget(struct GadgetInfo *gi)
1201 DrawGadget(gi, gi->state, gi->direct_draw);
1204 struct GadgetInfo *CreateGadget(int first_tag, ...)
1206 struct GadgetInfo *new_gadget = checked_calloc(sizeof(struct GadgetInfo));
1209 /* always start with reliable default values */
1210 new_gadget->id = getNewGadgetID();
1211 new_gadget->callback_info = default_callback_info;
1212 new_gadget->callback_action = default_callback_action;
1213 new_gadget->active = TRUE;
1214 new_gadget->direct_draw = TRUE;
1216 new_gadget->next = NULL;
1218 va_start(ap, first_tag);
1219 HandleGadgetTags(new_gadget, first_tag, ap);
1222 /* insert new gadget into global gadget list */
1223 if (gadget_list_last_entry)
1225 gadget_list_last_entry->next = new_gadget;
1226 gadget_list_last_entry = gadget_list_last_entry->next;
1229 gadget_list_first_entry = gadget_list_last_entry = new_gadget;
1234 void FreeGadget(struct GadgetInfo *gi)
1236 struct GadgetInfo *gi_previous = gadget_list_first_entry;
1238 /* prevent "last_info_gi" from pointing to memory that will be freed */
1239 if (last_info_gi == gi)
1240 last_info_gi = NULL;
1242 while (gi_previous != NULL && gi_previous->next != gi)
1243 gi_previous = gi_previous->next;
1245 if (gi == gadget_list_first_entry)
1246 gadget_list_first_entry = gi->next;
1248 if (gi == gadget_list_last_entry)
1249 gadget_list_last_entry = gi_previous;
1251 if (gi_previous != NULL)
1252 gi_previous->next = gi->next;
1257 static void CheckRangeOfNumericInputGadget(struct GadgetInfo *gi)
1259 if (gi->type != GD_TYPE_TEXT_INPUT_NUMERIC)
1262 gi->textinput.number_value = atoi(gi->textinput.value);
1264 if (gi->textinput.number_value < gi->textinput.number_min)
1265 gi->textinput.number_value = gi->textinput.number_min;
1266 if (gi->textinput.number_value > gi->textinput.number_max)
1267 gi->textinput.number_value = gi->textinput.number_max;
1269 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
1271 if (gi->textinput.cursor_position < 0)
1272 gi->textinput.cursor_position = 0;
1273 else if (gi->textinput.cursor_position > strlen(gi->textinput.value))
1274 gi->textinput.cursor_position = strlen(gi->textinput.value);
1277 /* global pointer to gadget actually in use (when mouse button pressed) */
1278 static struct GadgetInfo *last_gi = NULL;
1280 static void MapGadgetExt(struct GadgetInfo *gi, boolean redraw)
1282 if (gi == NULL || gi->mapped)
1288 DrawGadget(gi, DG_UNPRESSED, DG_BUFFERED);
1291 void MapGadget(struct GadgetInfo *gi)
1293 MapGadgetExt(gi, TRUE);
1296 void UnmapGadget(struct GadgetInfo *gi)
1298 if (gi == NULL || !gi->mapped)
1307 #define MAX_NUM_GADGETS 1024
1308 #define MULTIMAP_UNMAP (1 << 0)
1309 #define MULTIMAP_REMAP (1 << 1)
1310 #define MULTIMAP_REDRAW (1 << 2)
1311 #define MULTIMAP_PLAYFIELD (1 << 3)
1312 #define MULTIMAP_DOOR_1 (1 << 4)
1313 #define MULTIMAP_DOOR_2 (1 << 5)
1314 #define MULTIMAP_ALL (MULTIMAP_PLAYFIELD | \
1318 static void MultiMapGadgets(int mode)
1320 struct GadgetInfo *gi = gadget_list_first_entry;
1321 static boolean map_state[MAX_NUM_GADGETS];
1326 if ((mode & MULTIMAP_PLAYFIELD &&
1327 gi->x < gfx.sx + gfx.sxsize) ||
1328 (mode & MULTIMAP_DOOR_1 &&
1329 gi->x >= gfx.dx && gi->y < gfx.dy + gfx.dysize) ||
1330 (mode & MULTIMAP_DOOR_2 &&
1331 gi->x >= gfx.dx && gi->y > gfx.dy + gfx.dysize) ||
1332 (mode & MULTIMAP_ALL) == MULTIMAP_ALL)
1334 if (mode & MULTIMAP_UNMAP)
1336 map_state[map_count++ % MAX_NUM_GADGETS] = gi->mapped;
1341 if (map_state[map_count++ % MAX_NUM_GADGETS])
1342 MapGadgetExt(gi, (mode & MULTIMAP_REDRAW));
1350 void UnmapAllGadgets()
1352 MultiMapGadgets(MULTIMAP_ALL | MULTIMAP_UNMAP);
1355 void RemapAllGadgets()
1357 MultiMapGadgets(MULTIMAP_ALL | MULTIMAP_REMAP);
1360 boolean anyTextInputGadgetActive()
1362 return (last_gi && (last_gi->type & GD_TYPE_TEXT_INPUT) && last_gi->mapped);
1365 boolean anyTextAreaGadgetActive()
1367 return (last_gi && (last_gi->type & GD_TYPE_TEXT_AREA) && last_gi->mapped);
1370 boolean anySelectboxGadgetActive()
1372 return (last_gi && (last_gi->type & GD_TYPE_SELECTBOX) && last_gi->mapped);
1375 boolean anyScrollbarGadgetActive()
1377 return (last_gi && (last_gi->type & GD_TYPE_SCROLLBAR) && last_gi->mapped);
1380 boolean anyTextGadgetActive()
1382 return (anyTextInputGadgetActive() ||
1383 anyTextAreaGadgetActive() ||
1384 anySelectboxGadgetActive());
1387 static boolean insideSelectboxLine(struct GadgetInfo *gi, int mx, int my)
1389 return(gi != NULL &&
1390 gi->type & GD_TYPE_SELECTBOX &&
1391 mx >= gi->x && mx < gi->x + gi->width &&
1392 my >= gi->y && my < gi->y + gi->height);
1395 static boolean insideSelectboxArea(struct GadgetInfo *gi, int mx, int my)
1397 return(gi != NULL &&
1398 gi->type & GD_TYPE_SELECTBOX &&
1399 mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width &&
1400 my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height);
1403 void ClickOnGadget(struct GadgetInfo *gi, int button)
1408 /* simulate releasing mouse button over last gadget, if still pressed */
1410 HandleGadgets(-1, -1, 0);
1412 /* simulate pressing mouse button over specified gadget */
1413 HandleGadgets(gi->x, gi->y, button);
1415 /* simulate releasing mouse button over specified gadget */
1416 HandleGadgets(gi->x, gi->y, 0);
1419 boolean HandleGadgets(int mx, int my, int button)
1421 static unsigned long pressed_delay = 0;
1422 static unsigned long pressed_delay_value = GADGET_FRAME_DELAY;
1423 static int last_button = 0;
1424 static int last_mx = 0, last_my = 0;
1425 static int pressed_mx = 0, pressed_my = 0;
1426 static boolean keep_selectbox_open = FALSE;
1427 static boolean gadget_stopped = FALSE;
1428 int scrollbar_mouse_pos = 0;
1429 struct GadgetInfo *new_gi, *gi;
1430 boolean press_event;
1431 boolean release_event;
1432 boolean mouse_moving;
1433 boolean mouse_inside_select_line;
1434 boolean mouse_inside_select_area;
1435 boolean mouse_released_where_pressed;
1436 boolean gadget_pressed;
1437 boolean gadget_pressed_repeated;
1438 boolean gadget_pressed_off_borders;
1439 boolean gadget_pressed_inside_select_line;
1440 boolean gadget_pressed_delay_reached;
1441 boolean gadget_moving;
1442 boolean gadget_moving_inside;
1443 boolean gadget_moving_off_borders;
1444 boolean gadget_draggable;
1445 boolean gadget_dragging;
1446 boolean gadget_released;
1447 boolean gadget_released_inside;
1448 boolean gadget_released_inside_select_line;
1449 boolean gadget_released_inside_select_area;
1450 boolean gadget_released_off_borders;
1451 boolean changed_position = FALSE;
1453 /* check if there are any gadgets defined */
1454 if (gadget_list_first_entry == NULL)
1457 /* simulated release of mouse button over last gadget */
1458 if (mx == -1 && my == -1 && button == 0)
1464 /* check which gadget is under the mouse pointer */
1465 new_gi = getGadgetInfoFromMousePosition(mx, my, button);
1467 /* check if button state has changed since last invocation */
1468 press_event = (button != 0 && last_button == 0);
1469 release_event = (button == 0 && last_button != 0);
1470 last_button = button;
1472 /* check if mouse has been moved since last invocation */
1473 mouse_moving = ((mx != last_mx || my != last_my) && motion_status);
1477 if (press_event && new_gi != last_gi)
1483 mouse_released_where_pressed =
1484 (release_event && mx == pressed_mx && my == pressed_my);
1486 mouse_inside_select_line = insideSelectboxLine(new_gi, mx, my);
1487 mouse_inside_select_area = insideSelectboxArea(new_gi, mx, my);
1489 gadget_pressed_off_borders = (press_event && new_gi != last_gi);
1491 gadget_pressed_inside_select_line =
1492 (press_event && new_gi != NULL &&
1493 new_gi->type & GD_TYPE_SELECTBOX && new_gi->selectbox.open &&
1494 insideSelectboxLine(new_gi, mx, my));
1496 /* if mouse button pressed outside text or selectbox gadget, deactivate it */
1497 if (anyTextGadgetActive() &&
1498 (gadget_pressed_off_borders ||
1499 (gadget_pressed_inside_select_line && !mouse_inside_select_area)))
1501 struct GadgetInfo *gi = last_gi;
1502 boolean gadget_changed = (gi->event_mask & GD_EVENT_TEXT_LEAVING);
1504 /* check if text gadget has changed its value */
1505 if (gi->type & GD_TYPE_TEXT_INPUT)
1507 CheckRangeOfNumericInputGadget(gi);
1509 if (!strEqual(gi->textinput.last_value, gi->textinput.value))
1510 strcpy(gi->textinput.last_value, gi->textinput.value);
1512 gadget_changed = FALSE;
1515 /* selectbox does not change its value when closed by clicking outside */
1516 if (gi->type & GD_TYPE_SELECTBOX)
1517 gadget_changed = FALSE;
1519 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1521 gi->event.type = GD_EVENT_TEXT_LEAVING;
1523 if (gadget_changed && !(gi->type & GD_TYPE_SELECTBOX))
1524 gi->callback_action(gi);
1528 if (gadget_pressed_inside_select_line)
1533 (button != 0 && last_gi == NULL && new_gi != NULL && press_event);
1534 gadget_pressed_repeated =
1535 (button != 0 && last_gi != NULL && new_gi == last_gi);
1537 gadget_pressed_delay_reached =
1538 DelayReached(&pressed_delay, pressed_delay_value);
1540 gadget_released = (release_event && last_gi != NULL);
1541 gadget_released_inside = (gadget_released && new_gi == last_gi);
1542 gadget_released_off_borders = (gadget_released && new_gi != last_gi);
1544 gadget_moving = (button != 0 && last_gi != NULL && mouse_moving);
1545 gadget_moving_inside = (gadget_moving && new_gi == last_gi);
1546 gadget_moving_off_borders = (gadget_moving && new_gi != last_gi);
1548 /* when handling selectbox, set additional state values */
1549 if (gadget_released_inside && (last_gi->type & GD_TYPE_SELECTBOX))
1551 gadget_released_inside_select_line = insideSelectboxLine(last_gi, mx, my);
1552 gadget_released_inside_select_area = insideSelectboxArea(last_gi, mx, my);
1556 gadget_released_inside_select_line = FALSE;
1557 gadget_released_inside_select_area = FALSE;
1560 /* setting state for handling over-large selectbox */
1561 if (keep_selectbox_open && (press_event || !mouse_inside_select_line))
1562 keep_selectbox_open = FALSE;
1564 /* if new gadget pressed, store this gadget */
1568 /* 'gi' is actually handled gadget */
1571 /* if gadget is scrollbar, choose mouse position value */
1572 if (gi && gi->type & GD_TYPE_SCROLLBAR)
1573 scrollbar_mouse_pos =
1574 (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx - gi->x : my - gi->y);
1576 /* if mouse button released, no gadget needs to be handled anymore */
1577 if (gadget_released)
1579 if (gi->type & GD_TYPE_SELECTBOX &&
1580 (keep_selectbox_open ||
1581 mouse_released_where_pressed ||
1582 !gadget_released_inside_select_area)) /* selectbox stays open */
1584 gi->selectbox.stay_open = TRUE;
1588 else if (!(gi->type & GD_TYPE_TEXT_INPUT ||
1589 gi->type & GD_TYPE_TEXT_AREA)) /* text input stays open */
1593 /* modify event position values even if no gadget is pressed */
1594 if (button == 0 && !release_event)
1597 /* if new gadget or if no gadget was pressed, release stopped processing */
1598 if (gadget_pressed || new_gi == NULL)
1599 gadget_stopped = FALSE;
1601 /* if gadget was stopped while being handled, stop gadget processing here */
1607 int last_x = gi->event.x;
1608 int last_y = gi->event.y;
1610 gi->event.x = mx - gi->x;
1611 gi->event.y = my - gi->y;
1613 if (gi->type == GD_TYPE_DRAWING_AREA)
1615 gi->event.x /= gi->drawing.item_xsize;
1616 gi->event.y /= gi->drawing.item_ysize;
1618 if (last_x != gi->event.x || last_y != gi->event.y)
1619 changed_position = TRUE;
1621 else if (gi->type & GD_TYPE_TEXT_INPUT && button != 0 && !motion_status)
1623 int old_cursor_position = gi->textinput.cursor_position;
1625 /* if mouse button pressed inside activated text gadget, set cursor */
1626 gi->textinput.cursor_position =
1627 (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font);
1629 if (gi->textinput.cursor_position < 0)
1630 gi->textinput.cursor_position = 0;
1631 else if (gi->textinput.cursor_position > strlen(gi->textinput.value))
1632 gi->textinput.cursor_position = strlen(gi->textinput.value);
1634 if (gi->textinput.cursor_position != old_cursor_position)
1635 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1637 else if (gi->type & GD_TYPE_TEXT_AREA && button != 0 && !motion_status)
1639 int old_cursor_position = gi->textarea.cursor_position;
1640 int x = (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font);
1641 int y = (my - gi->y - gi->border.ysize) / getFontHeight(gi->font);
1643 x = (x < 0 ? 0 : x >= gi->textarea.xsize ? gi->textarea.xsize - 1 : x);
1644 y = (y < 0 ? 0 : y >= gi->textarea.ysize ? gi->textarea.ysize - 1 : y);
1646 setTextAreaCursorXY(gi, x, y);
1648 if (gi->textarea.cursor_position != old_cursor_position)
1649 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1651 else if (gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open &&
1652 !keep_selectbox_open)
1654 int old_index = gi->selectbox.current_index;
1656 /* if mouse moving inside activated selectbox, select value */
1657 if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
1658 gi->selectbox.current_index =
1659 (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font);
1661 if (gi->selectbox.current_index < 0)
1662 gi->selectbox.current_index = 0;
1663 else if (gi->selectbox.current_index > gi->selectbox.num_values - 1)
1664 gi->selectbox.current_index = gi->selectbox.num_values - 1;
1666 if (gi->selectbox.current_index != old_index)
1667 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1671 /* handle gadget popup info text */
1672 if (last_info_gi != new_gi ||
1673 (new_gi && new_gi->type == GD_TYPE_DRAWING_AREA && changed_position))
1675 if (new_gi != NULL && (button == 0 || new_gi == last_gi))
1677 new_gi->event.type = GD_EVENT_INFO_ENTERING;
1678 new_gi->callback_info(new_gi);
1680 else if (last_info_gi != NULL)
1682 last_info_gi->event.type = GD_EVENT_INFO_LEAVING;
1683 last_info_gi->callback_info(last_info_gi);
1686 last_info_gi = new_gi;
1691 gadget_draggable = (gi && gi->type & GD_TYPE_SCROLLBAR);
1693 /* reset drag position for newly pressed scrollbar to "not dragging" */
1694 if (gadget_pressed && gadget_draggable)
1695 gi->scrollbar.drag_position = -1;
1697 gadget_dragging = (gadget_draggable && gi->scrollbar.drag_position != -1);
1699 /* clicking next to a scrollbar to move it is not considered "moving" */
1700 if (gadget_draggable && !gadget_dragging)
1701 gadget_moving = FALSE;
1703 /* when leaving scrollbar area when jump-scrolling, stop gadget processing */
1704 if (gadget_draggable && !gadget_dragging && gadget_moving_off_borders)
1705 gadget_stopped = TRUE;
1707 if ((gadget_pressed) ||
1708 (gadget_pressed_repeated && gadget_pressed_delay_reached))
1710 if (gadget_pressed) /* gadget pressed the first time */
1712 /* initialize delay counter */
1713 DelayReached(&pressed_delay, 0);
1715 /* start gadget delay with longer delay after first click on gadget */
1716 pressed_delay_value = GADGET_FRAME_DELAY_FIRST;
1718 else /* gadget hold pressed for some time */
1720 /* after first repeated gadget click, continue with shorter delay value */
1721 pressed_delay_value = GADGET_FRAME_DELAY;
1724 if (gi->type & GD_TYPE_SCROLLBAR && !gadget_dragging)
1726 int mpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx : my);
1727 int gpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? gi->x : gi->y);
1730 mpos < gpos + gi->scrollbar.position ||
1731 mpos >= gpos + gi->scrollbar.position + gi->scrollbar.size)
1733 /* click scrollbar one scrollbar length up/left or down/right */
1735 struct GadgetScrollbar *gs = &gi->scrollbar;
1736 int old_item_position = gs->item_position;
1737 int item_steps = gs->items_visible - 1;
1738 int item_direction = (mpos < gpos + gi->scrollbar.position ? -1 : +1);
1743 item_direction = (button == 4 ? -1 : +1);
1746 changed_position = FALSE;
1748 gs->item_position += item_steps * item_direction;
1750 if (gs->item_position < 0)
1751 gs->item_position = 0;
1752 else if (gs->item_position > gs->items_max - gs->items_visible)
1753 gs->item_position = gs->items_max - gs->items_visible;
1755 if (old_item_position != gs->item_position)
1757 gi->event.item_position = gs->item_position;
1758 changed_position = TRUE;
1761 ModifyGadget(gi, GDI_SCROLLBAR_ITEM_POSITION, gs->item_position,
1764 gi->state = GD_BUTTON_UNPRESSED;
1765 gi->event.type = GD_EVENT_MOVING;
1766 gi->event.off_borders = FALSE;
1768 if (gi->event_mask & GD_EVENT_MOVING && changed_position)
1769 gi->callback_action(gi);
1775 /* don't handle this scrollbar anymore when mouse position reached */
1776 if (gadget_pressed_repeated)
1778 gadget_stopped = TRUE;
1790 if (gi->type == GD_TYPE_CHECK_BUTTON)
1792 gi->checked = !gi->checked;
1794 else if (gi->type == GD_TYPE_RADIO_BUTTON)
1796 struct GadgetInfo *rgi = gadget_list_first_entry;
1801 rgi->type == GD_TYPE_RADIO_BUTTON &&
1802 rgi->radio_nr == gi->radio_nr &&
1805 rgi->checked = FALSE;
1806 DrawGadget(rgi, DG_UNPRESSED, rgi->direct_draw);
1814 else if (gi->type & GD_TYPE_SCROLLBAR)
1816 int mpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx : my);
1817 int gpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? gi->x : gi->y);
1819 if (button >= 1 && button <= 3 &&
1820 mpos >= gpos + gi->scrollbar.position &&
1821 mpos < gpos + gi->scrollbar.position + gi->scrollbar.size)
1823 /* start dragging scrollbar */
1824 gi->scrollbar.drag_position =
1825 scrollbar_mouse_pos - gi->scrollbar.position;
1828 else if (gi->type & GD_TYPE_SELECTBOX)
1830 /* keep selectbox open in case of over-large selectbox */
1831 keep_selectbox_open = (mouse_inside_select_line &&
1832 mouse_inside_select_area);
1835 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1837 gi->state = GD_BUTTON_PRESSED;
1838 gi->event.type = GD_EVENT_PRESSED;
1839 gi->event.button = button;
1840 gi->event.off_borders = FALSE;
1842 if (gi->event_mask & GD_EVENT_PRESSED)
1843 gi->callback_action(gi);
1846 if (gadget_pressed_repeated)
1848 gi->event.type = GD_EVENT_PRESSED;
1850 if (gi->event_mask & GD_EVENT_REPEATED && gadget_pressed_delay_reached)
1851 gi->callback_action(gi);
1856 if (gi->type & GD_TYPE_BUTTON)
1858 if (gadget_moving_inside && gi->state == GD_BUTTON_UNPRESSED)
1859 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1860 else if (gadget_moving_off_borders && gi->state == GD_BUTTON_PRESSED)
1861 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1863 else if (gi->type & GD_TYPE_SELECTBOX && !keep_selectbox_open)
1865 int old_index = gi->selectbox.current_index;
1867 /* if mouse moving inside activated selectbox, select value */
1868 if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
1869 gi->selectbox.current_index =
1870 (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font);
1872 if (gi->selectbox.current_index < 0)
1873 gi->selectbox.current_index = 0;
1874 else if (gi->selectbox.current_index > gi->selectbox.num_values - 1)
1875 gi->selectbox.current_index = gi->selectbox.num_values - 1;
1877 if (gi->selectbox.current_index != old_index)
1878 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1880 else if (gi->type & GD_TYPE_SCROLLBAR)
1882 struct GadgetScrollbar *gs = &gi->scrollbar;
1883 int old_item_position = gs->item_position;
1885 gs->position = scrollbar_mouse_pos - gs->drag_position;
1887 /* make sure to always precisely reach end positions when dragging */
1888 if (gs->position <= 0)
1891 gs->item_position = 0;
1893 else if (gs->position >= gs->position_max)
1895 gs->position = gs->position_max;
1896 gs->item_position = gs->items_max - gs->items_visible;
1901 gs->items_max * (gs->position + gs->correction) / gs->size_max_cmp;
1904 if (gs->item_position < 0)
1905 gs->item_position = 0;
1906 if (gs->item_position > gs->items_max - 1)
1907 gs->item_position = gs->items_max - 1;
1909 if (old_item_position != gs->item_position)
1911 gi->event.item_position = gs->item_position;
1912 changed_position = TRUE;
1915 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1918 gi->state = (gadget_moving_inside || gadget_draggable ?
1919 GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
1920 gi->event.type = GD_EVENT_MOVING;
1921 gi->event.off_borders = gadget_moving_off_borders;
1923 if (gi->event_mask & GD_EVENT_MOVING && changed_position &&
1924 (gadget_moving_inside || gi->event_mask & GD_EVENT_OFF_BORDERS))
1925 gi->callback_action(gi);
1928 if (gadget_released_inside)
1930 boolean deactivate_gadget = TRUE;
1931 boolean gadget_changed = TRUE;
1933 if (gi->type & GD_TYPE_SELECTBOX)
1935 if (keep_selectbox_open ||
1936 mouse_released_where_pressed ||
1937 !gadget_released_inside_select_area) /* selectbox stays open */
1939 deactivate_gadget = FALSE;
1940 gadget_changed = FALSE;
1942 else if (gi->selectbox.index != gi->selectbox.current_index)
1943 gi->selectbox.index = gi->selectbox.current_index;
1945 gadget_changed = FALSE;
1948 if (deactivate_gadget &&
1949 !(gi->type & GD_TYPE_TEXT_INPUT ||
1950 gi->type & GD_TYPE_TEXT_AREA)) /* text input stays open */
1951 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1953 gi->state = GD_BUTTON_UNPRESSED;
1954 gi->event.type = GD_EVENT_RELEASED;
1956 if ((gi->event_mask & GD_EVENT_RELEASED) && gadget_changed)
1958 gi->callback_action(gi);
1962 if (gadget_released_off_borders)
1964 if (gi->type & GD_TYPE_SCROLLBAR)
1965 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1968 gi->state = GD_BUTTON_UNPRESSED;
1970 gi->event.type = GD_EVENT_RELEASED;
1972 if (gi->event_mask & GD_EVENT_RELEASED &&
1973 gi->event_mask & GD_EVENT_OFF_BORDERS)
1974 gi->callback_action(gi);
1977 /* handle gadgets unmapped/mapped between pressing and releasing */
1978 if (release_event && !gadget_released && new_gi)
1979 new_gi->state = GD_BUTTON_UNPRESSED;
1981 return (gadget_pressed || gadget_pressed_repeated ||
1982 gadget_released || gadget_moving);
1985 static void insertCharIntoTextArea(struct GadgetInfo *gi, char c)
1987 char text[MAX_GADGET_TEXTSIZE];
1988 int cursor_position = gi->textarea.cursor_position;
1990 if (strlen(gi->textarea.value) == MAX_GADGET_TEXTSIZE) /* no space left */
1993 strcpy(text, gi->textarea.value);
1994 strcpy(&gi->textarea.value[cursor_position + 1], &text[cursor_position]);
1995 gi->textarea.value[cursor_position] = c;
1997 setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1);
2000 boolean HandleGadgetsKeyInput(Key key)
2002 struct GadgetInfo *gi = last_gi;
2004 if (gi == NULL || !gi->mapped ||
2005 !(gi->type & GD_TYPE_TEXT_INPUT ||
2006 gi->type & GD_TYPE_TEXT_AREA ||
2007 gi->type & GD_TYPE_SELECTBOX))
2010 if (key == KSYM_Return) /* valid for both text input and selectbox */
2012 boolean gadget_changed = (gi->event_mask & GD_EVENT_TEXT_RETURN);
2014 if (gi->type & GD_TYPE_TEXT_INPUT)
2016 CheckRangeOfNumericInputGadget(gi);
2018 if (!strEqual(gi->textinput.last_value, gi->textinput.value))
2019 strcpy(gi->textinput.last_value, gi->textinput.value);
2021 gadget_changed = FALSE;
2023 else if (gi->type & GD_TYPE_SELECTBOX)
2025 if (gi->selectbox.index != gi->selectbox.current_index)
2026 gi->selectbox.index = gi->selectbox.current_index;
2028 gadget_changed = FALSE;
2031 if (gi->type & GD_TYPE_TEXT_AREA)
2033 insertCharIntoTextArea(gi, '\n');
2035 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2039 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2041 gi->event.type = GD_EVENT_TEXT_RETURN;
2047 gi->callback_action(gi);
2049 else if (gi->type & GD_TYPE_TEXT_INPUT) /* only valid for text input */
2051 char text[MAX_GADGET_TEXTSIZE];
2052 int text_length = strlen(gi->textinput.value);
2053 int cursor_pos = gi->textinput.cursor_position;
2054 char letter = getCharFromKey(key);
2055 boolean legal_letter = (gi->type == GD_TYPE_TEXT_INPUT_NUMERIC ?
2056 letter >= '0' && letter <= '9' :
2059 if (legal_letter && text_length < gi->textinput.size)
2061 strcpy(text, gi->textinput.value);
2062 strcpy(&gi->textinput.value[cursor_pos + 1], &text[cursor_pos]);
2063 gi->textinput.value[cursor_pos] = letter;
2064 gi->textinput.cursor_position++;
2066 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2068 else if (key == KSYM_Left && cursor_pos > 0)
2070 gi->textinput.cursor_position--;
2072 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2074 else if (key == KSYM_Right && cursor_pos < text_length)
2076 gi->textinput.cursor_position++;
2078 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2080 else if (key == KSYM_BackSpace && cursor_pos > 0)
2082 strcpy(text, gi->textinput.value);
2083 strcpy(&gi->textinput.value[cursor_pos - 1], &text[cursor_pos]);
2084 gi->textinput.cursor_position--;
2086 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2088 else if (key == KSYM_Delete && cursor_pos < text_length)
2090 strcpy(text, gi->textinput.value);
2091 strcpy(&gi->textinput.value[cursor_pos], &text[cursor_pos + 1]);
2093 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2096 else if (gi->type & GD_TYPE_TEXT_AREA) /* only valid for text area */
2098 char text[MAX_GADGET_TEXTSIZE];
2099 int text_length = strlen(gi->textarea.value);
2100 int area_ysize = gi->textarea.ysize;
2101 int cursor_x_pref = gi->textarea.cursor_x_preferred;
2102 int cursor_y = gi->textarea.cursor_y;
2103 int cursor_pos = gi->textarea.cursor_position;
2104 char letter = getCharFromKey(key);
2105 boolean legal_letter = (letter != 0);
2109 insertCharIntoTextArea(gi, letter);
2111 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2113 else if (key == KSYM_Left && cursor_pos > 0)
2115 setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1);
2117 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2119 else if (key == KSYM_Right && cursor_pos < text_length)
2121 setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1);
2123 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2125 else if (key == KSYM_Up && cursor_y > 0)
2127 setTextAreaCursorXY(gi, cursor_x_pref, cursor_y - 1);
2128 gi->textarea.cursor_x_preferred = cursor_x_pref;
2130 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2132 else if (key == KSYM_Down && cursor_y < area_ysize - 1)
2134 setTextAreaCursorXY(gi, cursor_x_pref, cursor_y + 1);
2135 gi->textarea.cursor_x_preferred = cursor_x_pref;
2137 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2139 else if (key == KSYM_BackSpace && cursor_pos > 0)
2141 strcpy(text, gi->textarea.value);
2142 strcpy(&gi->textarea.value[cursor_pos - 1], &text[cursor_pos]);
2144 setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1);
2146 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2148 else if (key == KSYM_Delete && cursor_pos < text_length)
2150 strcpy(text, gi->textarea.value);
2151 strcpy(&gi->textarea.value[cursor_pos], &text[cursor_pos + 1]);
2153 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2156 else if (gi->type & GD_TYPE_SELECTBOX) /* only valid for selectbox */
2158 int index = gi->selectbox.current_index;
2159 int num_values = gi->selectbox.num_values;
2161 if (key == KSYM_Up && index > 0)
2163 gi->selectbox.current_index--;
2165 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2167 else if (key == KSYM_Down && index < num_values - 1)
2169 gi->selectbox.current_index++;
2171 DrawGadget(gi, DG_PRESSED, gi->direct_draw);