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)
77 printf("WHOA! SCROLL WHEEL DETECTED [%d]\n", button);
80 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
82 if (gi->mapped && gi->active &&
83 gi->type & GD_TYPE_SCROLLBAR)
87 /* no active scrollbar found -- ignore this button event */
91 /* open selectboxes may overlap other active gadgets, so check them first */
92 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
94 if (gi->mapped && gi->active &&
95 gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open &&
96 mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width &&
97 my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
101 /* check all other gadgets */
102 for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next)
104 if (gi->mapped && gi->active &&
105 mx >= gi->x && mx < gi->x + gi->width &&
106 my >= gi->y && my < gi->y + gi->height)
113 static void setTextAreaCursorExt(struct GadgetInfo *gi, boolean set_cursor_pos)
115 char *text = gi->textarea.value;
116 int area_xsize = gi->textarea.xsize;
117 int area_ysize = gi->textarea.ysize;
118 int cursor_position = gi->textarea.cursor_position;
119 int cursor_x = gi->textarea.cursor_x;
120 int cursor_y = gi->textarea.cursor_y;
127 if (set_cursor_pos) /* x/y => position */
129 if (y == cursor_y && (x == cursor_x || (x < cursor_x && *text == '\n')))
132 else /* position => x/y */
134 if (pos == cursor_position)
138 if (x + 1 >= area_xsize || *text == '\n')
140 if (y + 1 >= area_ysize)
153 gi->textarea.cursor_x = x;
154 gi->textarea.cursor_y = y;
155 gi->textarea.cursor_x_preferred = x;
156 gi->textarea.cursor_position = pos;
159 static void setTextAreaCursorXY(struct GadgetInfo *gi, int x, int y)
161 gi->textarea.cursor_x = x;
162 gi->textarea.cursor_y = y;
164 setTextAreaCursorExt(gi, TRUE);
167 static void setTextAreaCursorPosition(struct GadgetInfo *gi, int pos)
169 gi->textarea.cursor_position = pos;
171 setTextAreaCursorExt(gi, FALSE);
174 static void default_callback_info(void *ptr)
179 static void default_callback_action(void *ptr)
184 static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct)
186 struct GadgetDesign *gd;
187 int state = (pressed ? GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
188 boolean redraw_selectbox = FALSE;
193 gd = (!gi->active ? &gi->alt_design[state] :
194 gi->checked ? &gi->alt_design[state] : &gi->design[state]);
198 case GD_TYPE_NORMAL_BUTTON:
199 case GD_TYPE_CHECK_BUTTON:
200 case GD_TYPE_RADIO_BUTTON:
201 BlitBitmapOnBackground(gd->bitmap, drawto,
202 gd->x, gd->y, gi->width, gi->height,
204 if (gi->deco.design.bitmap)
205 BlitBitmap(gi->deco.design.bitmap, drawto,
206 gi->deco.design.x, gi->deco.design.y,
207 gi->deco.width, gi->deco.height,
208 gi->x + gi->deco.x + (pressed ? gi->deco.xshift : 0),
209 gi->y + gi->deco.y + (pressed ? gi->deco.yshift : 0));
212 case GD_TYPE_TEXT_BUTTON:
215 int font_nr = (gi->active ? gi->font_active : gi->font);
216 int font_width = getFontWidth(font_nr);
217 int border_x = gi->border.xsize;
218 int border_y = gi->border.ysize;
219 int text_size = strlen(gi->textbutton.value);
220 int text_start = (gi->width - text_size * font_width) / 2;
222 /* left part of gadget */
223 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
224 border_x, gi->height, gi->x, gi->y);
226 /* middle part of gadget */
227 for (i=0; i < gi->textbutton.size; i++)
228 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
229 font_width, gi->height,
230 gi->x + border_x + i * font_width, gi->y);
232 /* right part of gadget */
233 BlitBitmapOnBackground(gd->bitmap, drawto,
234 gd->x + gi->border.width - border_x, gd->y,
235 border_x, gi->height,
236 gi->x + gi->width - border_x, gi->y);
238 /* gadget text value */
240 gi->x + text_start + (pressed ? gi->deco.xshift : 0),
241 gi->y + border_y + (pressed ? gi->deco.yshift : 0),
242 gi->textbutton.value, font_nr, BLIT_MASKED);
246 case GD_TYPE_TEXT_INPUT_ALPHANUMERIC:
247 case GD_TYPE_TEXT_INPUT_NUMERIC:
251 char cursor_string[2];
252 char text[MAX_GADGET_TEXTSIZE + 1];
253 int font_nr = (pressed ? gi->font_active : gi->font);
254 int font_width = getFontWidth(font_nr);
255 int border_x = gi->border.xsize;
256 int border_y = gi->border.ysize;
258 /* left part of gadget */
259 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
260 border_x, gi->height, gi->x, gi->y);
262 /* middle part of gadget */
263 for (i=0; i < gi->textinput.size + 1; i++)
264 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
265 font_width, gi->height,
266 gi->x + border_x + i * font_width, gi->y);
268 /* right part of gadget */
269 BlitBitmapOnBackground(gd->bitmap, drawto,
270 gd->x + gi->border.width - border_x, gd->y,
271 border_x, gi->height,
272 gi->x + gi->width - border_x, gi->y);
275 strcpy(text, gi->textinput.value);
278 /* gadget text value */
280 gi->x + border_x, gi->y + border_y, text,
281 font_nr, BLIT_MASKED);
283 cursor_letter = gi->textinput.value[gi->textinput.cursor_position];
284 cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' ');
285 cursor_string[1] = '\0';
287 /* draw cursor, if active */
291 gi->textinput.cursor_position * font_width,
292 gi->y + border_y, cursor_string,
293 font_nr, BLIT_INVERSE);
297 case GD_TYPE_TEXT_AREA:
301 char cursor_string[2];
302 int font_nr = (pressed ? gi->font_active : gi->font);
303 int font_width = getFontWidth(font_nr);
304 int font_height = getFontHeight(font_nr);
305 int border_x = gi->border.xsize;
306 int border_y = gi->border.ysize;
307 int gd_height = 2 * border_y + font_height;
309 /* top left part of gadget border */
310 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
311 border_x, border_y, gi->x, gi->y);
313 /* top middle part of gadget border */
314 for (i=0; i < gi->textarea.xsize; i++)
315 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
316 font_width, border_y,
317 gi->x + border_x + i * font_width, gi->y);
319 /* top right part of gadget border */
320 BlitBitmapOnBackground(gd->bitmap, drawto,
321 gd->x + gi->border.width - border_x, gd->y,
323 gi->x + gi->width - border_x, gi->y);
325 /* left and right part of gadget border for each row */
326 for (i=0; i < gi->textarea.ysize; i++)
328 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y,
329 border_x, font_height,
330 gi->x, gi->y + border_y + i * font_height);
331 BlitBitmapOnBackground(gd->bitmap, drawto,
332 gd->x + gi->border.width - border_x,
334 border_x, font_height,
335 gi->x + gi->width - border_x,
336 gi->y + border_y + i * font_height);
339 /* bottom left part of gadget border */
340 BlitBitmapOnBackground(gd->bitmap, drawto,
341 gd->x, gd->y + gd_height - border_y,
343 gi->x, gi->y + gi->height - border_y);
345 /* bottom middle part of gadget border */
346 for (i=0; i < gi->textarea.xsize; i++)
347 BlitBitmapOnBackground(gd->bitmap, drawto,
349 gd->y + gd_height - border_y,
350 font_width, border_y,
351 gi->x + border_x + i * font_width,
352 gi->y + gi->height - border_y);
354 /* bottom right part of gadget border */
355 BlitBitmapOnBackground(gd->bitmap, drawto,
356 gd->x + gi->border.width - border_x,
357 gd->y + gd_height - border_y,
359 gi->x + gi->width - border_x,
360 gi->y + gi->height - border_y);
362 ClearRectangleOnBackground(drawto,
365 gi->width - 2 * border_x,
366 gi->height - 2 * border_y);
368 /* gadget text value */
369 DrawTextToTextArea(gi->x + border_x, gi->y + border_y,
370 gi->textarea.value, font_nr, gi->textarea.xsize,
371 gi->textarea.xsize, gi->textarea.ysize,
374 cursor_letter = gi->textarea.value[gi->textarea.cursor_position];
375 cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' ');
376 cursor_string[1] = '\0';
378 /* draw cursor, if active */
381 gi->x + border_x + gi->textarea.cursor_x * font_width,
382 gi->y + border_y + gi->textarea.cursor_y * font_height,
384 font_nr, BLIT_INVERSE);
388 case GD_TYPE_SELECTBOX:
391 char text[MAX_GADGET_TEXTSIZE + 1];
392 int font_nr = (pressed ? gi->font_active : gi->font);
393 int font_width = getFontWidth(font_nr);
394 int font_height = getFontHeight(font_nr);
395 int border_x = gi->border.xsize;
396 int border_y = gi->border.ysize;
397 int button = gi->border.xsize_selectbutton;
398 int width_inner = gi->border.width - button - 2 * border_x;
399 int box_width = gi->selectbox.width;
400 int box_height = gi->selectbox.height;
402 /* left part of gadget */
403 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
404 border_x, gi->height, gi->x, gi->y);
406 /* middle part of gadget */
407 for (i=0; i < gi->selectbox.size; i++)
408 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
409 font_width, gi->height,
410 gi->x + border_x + i * font_width, gi->y);
412 /* button part of gadget */
413 BlitBitmapOnBackground(gd->bitmap, drawto,
414 gd->x + border_x + width_inner, gd->y,
416 gi->x + gi->width - border_x - button, gi->y);
418 /* right part of gadget */
419 BlitBitmapOnBackground(gd->bitmap, drawto,
420 gd->x + gi->border.width - border_x, gd->y,
421 border_x, gi->height,
422 gi->x + gi->width - border_x, gi->y);
425 strncpy(text, gi->selectbox.options[gi->selectbox.index].text,
427 text[gi->selectbox.size] = '\0';
429 /* gadget text value */
430 DrawTextExt(drawto, gi->x + border_x, gi->y + border_y, text,
431 font_nr, BLIT_MASKED);
435 if (!gi->selectbox.open)
437 gi->selectbox.open = TRUE;
438 gi->selectbox.stay_open = FALSE;
439 gi->selectbox.current_index = gi->selectbox.index;
441 /* save background under selectbox */
442 BlitBitmap(drawto, gfx.field_save_buffer,
443 gi->selectbox.x, gi->selectbox.y,
444 gi->selectbox.width, gi->selectbox.height,
445 gi->selectbox.x, gi->selectbox.y);
448 /* draw open selectbox */
450 /* top left part of gadget border */
451 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
453 gi->selectbox.x, gi->selectbox.y);
455 /* top middle part of gadget border */
456 for (i=0; i < gi->selectbox.size; i++)
457 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
458 font_width, border_y,
459 gi->selectbox.x + border_x + i * font_width,
462 /* top button part of gadget border */
463 BlitBitmapOnBackground(gd->bitmap, drawto,
464 gd->x + border_x + width_inner, gd->y,
466 gi->selectbox.x + box_width -border_x -button,
469 /* top right part of gadget border */
470 BlitBitmapOnBackground(gd->bitmap, drawto,
471 gd->x + gi->border.width - border_x, gd->y,
473 gi->selectbox.x + box_width - border_x,
476 /* left and right part of gadget border for each row */
477 for (i=0; i < gi->selectbox.num_values; i++)
479 BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y,
480 border_x, font_height,
482 gi->selectbox.y + border_y + i*font_height);
483 BlitBitmapOnBackground(gd->bitmap, drawto,
484 gd->x + gi->border.width - border_x,
486 border_x, font_height,
487 gi->selectbox.x + box_width - border_x,
488 gi->selectbox.y + border_y + i*font_height);
491 /* bottom left part of gadget border */
492 BlitBitmapOnBackground(gd->bitmap, drawto,
493 gd->x, gd->y + gi->height - border_y,
496 gi->selectbox.y + box_height - border_y);
498 /* bottom middle part of gadget border */
499 for (i=0; i < gi->selectbox.size; i++)
500 BlitBitmapOnBackground(gd->bitmap, drawto,
502 gd->y + gi->height - border_y,
503 font_width, border_y,
504 gi->selectbox.x + border_x + i * font_width,
505 gi->selectbox.y + box_height - border_y);
507 /* bottom button part of gadget border */
508 BlitBitmapOnBackground(gd->bitmap, drawto,
509 gd->x + border_x + width_inner,
510 gd->y + gi->height - border_y,
512 gi->selectbox.x + box_width -border_x -button,
513 gi->selectbox.y + box_height - border_y);
515 /* bottom right part of gadget border */
516 BlitBitmapOnBackground(gd->bitmap, drawto,
517 gd->x + gi->border.width - border_x,
518 gd->y + gi->height - border_y,
520 gi->selectbox.x + box_width - border_x,
521 gi->selectbox.y + box_height - border_y);
523 ClearRectangleOnBackground(drawto,
524 gi->selectbox.x + border_x,
525 gi->selectbox.y + border_y,
526 gi->selectbox.width - 2 * border_x,
527 gi->selectbox.height - 2 * border_y);
529 /* selectbox text values */
530 for (i=0; i < gi->selectbox.num_values; i++)
532 int mask_mode = BLIT_MASKED;
534 strncpy(text, gi->selectbox.options[i].text, gi->selectbox.size);
535 text[gi->selectbox.size] = '\0';
537 if (i == gi->selectbox.current_index)
539 FillRectangle(drawto,
540 gi->selectbox.x + border_x,
541 gi->selectbox.y + border_y + i * font_height,
542 gi->selectbox.width - 2 * border_x, font_height,
543 gi->selectbox.inverse_color);
545 /* prevent use of cursor graphic by drawing at least two chars */
547 text[gi->selectbox.size] = '\0';
549 mask_mode = BLIT_INVERSE;
553 gi->selectbox.x + border_x,
554 gi->selectbox.y + border_y + i * font_height, text,
558 redraw_selectbox = TRUE;
560 else if (gi->selectbox.open)
562 gi->selectbox.open = FALSE;
564 /* restore background under selectbox */
565 BlitBitmap(gfx.field_save_buffer, drawto,
566 gi->selectbox.x, gi->selectbox.y,
567 gi->selectbox.width, gi->selectbox.height,
568 gi->selectbox.x, gi->selectbox.y);
570 /* redraw closed selectbox */
571 DrawGadget(gi, FALSE, FALSE);
573 redraw_selectbox = TRUE;
578 case GD_TYPE_SCROLLBAR_VERTICAL:
582 int ypos = gi->y + gi->scrollbar.position;
583 int design_full = gi->width;
584 int design_body = design_full - 2 * gi->border.ysize;
585 int size_full = gi->scrollbar.size;
586 int size_body = size_full - 2 * gi->border.ysize;
587 int num_steps = size_body / design_body;
588 int step_size_remain = size_body - num_steps * design_body;
590 /* clear scrollbar area */
591 ClearRectangleOnBackground(backbuffer, gi->x, gi->y,
592 gi->width, gi->height);
594 /* upper part of gadget */
595 BlitBitmapOnBackground(gd->bitmap, drawto,
597 gi->width, gi->border.ysize,
600 /* middle part of gadget */
601 for (i=0; i < num_steps; i++)
602 BlitBitmapOnBackground(gd->bitmap, drawto,
603 gd->x, gd->y + gi->border.ysize,
604 gi->width, design_body,
606 ypos + gi->border.ysize + i * design_body);
608 /* remaining middle part of gadget */
609 if (step_size_remain > 0)
610 BlitBitmapOnBackground(gd->bitmap, drawto,
611 gd->x, gd->y + gi->border.ysize,
612 gi->width, step_size_remain,
614 ypos + gi->border.ysize
615 + num_steps * design_body);
617 /* lower part of gadget */
618 BlitBitmapOnBackground(gd->bitmap, drawto,
619 gd->x, gd->y + design_full - gi->border.ysize,
620 gi->width, gi->border.ysize,
621 xpos, ypos + size_full - gi->border.ysize);
625 case GD_TYPE_SCROLLBAR_HORIZONTAL:
628 int xpos = gi->x + gi->scrollbar.position;
630 int design_full = gi->height;
631 int design_body = design_full - 2 * gi->border.xsize;
632 int size_full = gi->scrollbar.size;
633 int size_body = size_full - 2 * gi->border.xsize;
634 int num_steps = size_body / design_body;
635 int step_size_remain = size_body - num_steps * design_body;
637 /* clear scrollbar area */
638 ClearRectangleOnBackground(backbuffer, gi->x, gi->y,
639 gi->width, gi->height);
641 /* left part of gadget */
642 BlitBitmapOnBackground(gd->bitmap, drawto,
644 gi->border.xsize, gi->height,
647 /* middle part of gadget */
648 for (i=0; i < num_steps; i++)
649 BlitBitmapOnBackground(gd->bitmap, drawto,
650 gd->x + gi->border.xsize, gd->y,
651 design_body, gi->height,
652 xpos + gi->border.xsize + i * design_body,
655 /* remaining middle part of gadget */
656 if (step_size_remain > 0)
657 BlitBitmapOnBackground(gd->bitmap, drawto,
658 gd->x + gi->border.xsize, gd->y,
659 step_size_remain, gi->height,
660 xpos + gi->border.xsize
661 + num_steps * design_body,
664 /* right part of gadget */
665 BlitBitmapOnBackground(gd->bitmap, drawto,
666 gd->x + design_full - gi->border.xsize, gd->y,
667 gi->border.xsize, gi->height,
668 xpos + size_full - gi->border.xsize, ypos);
678 BlitBitmap(drawto, window,
679 gi->x, gi->y, gi->width, gi->height, gi->x, gi->y);
681 if (gi->type == GD_TYPE_SELECTBOX && redraw_selectbox)
682 BlitBitmap(drawto, window,
683 gi->selectbox.x, gi->selectbox.y,
684 gi->selectbox.width, gi->selectbox.height,
685 gi->selectbox.x, gi->selectbox.y);
688 redraw_mask |= (gi->x < gfx.sx + gfx.sxsize ? REDRAW_FIELD :
689 gi->y < gfx.dy + gfx.dysize ? REDRAW_DOOR_1 :
690 gi->y > gfx.vy ? REDRAW_DOOR_2 : REDRAW_DOOR_3);
693 static int get_minimal_size_for_numeric_input(int minmax_value)
695 int min_size = 1; /* value needs at least one digit */
698 /* add number of digits needed for absolute value */
699 for (i = 10; i <= ABS(minmax_value); i *= 10)
702 /* if min/max value is negative, add one digit for minus sign */
703 if (minmax_value < 0)
709 static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap)
716 while (tag != GDI_END)
721 gi->custom_id = va_arg(ap, int);
724 case GDI_CUSTOM_TYPE_ID:
725 gi->custom_type_id = va_arg(ap, int);
730 int max_textsize = MAX_INFO_TEXTSIZE - 1;
731 char *text = va_arg(ap, char *);
734 strncpy(gi->info_text, text, max_textsize);
738 gi->info_text[max_textsize] = '\0';
743 gi->x = va_arg(ap, int);
747 gi->y = va_arg(ap, int);
751 gi->width = va_arg(ap, int);
755 gi->height = va_arg(ap, int);
759 gi->type = va_arg(ap, unsigned int);
763 gi->state = va_arg(ap, unsigned int);
767 /* take care here: "boolean" is typedef'ed as "unsigned char",
768 which gets promoted to "int" */
769 gi->active = (boolean)va_arg(ap, int);
772 case GDI_DIRECT_DRAW:
773 /* take care here: "boolean" is typedef'ed as "unsigned char",
774 which gets promoted to "int" */
775 gi->direct_draw = (boolean)va_arg(ap, int);
779 /* take care here: "boolean" is typedef'ed as "unsigned char",
780 which gets promoted to "int" */
781 gi->checked = (boolean)va_arg(ap, int);
785 gi->radio_nr = va_arg(ap, unsigned int);
788 case GDI_NUMBER_VALUE:
789 gi->textinput.number_value = va_arg(ap, int);
790 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
791 strcpy(gi->textinput.last_value, gi->textinput.value);
792 gi->textinput.cursor_position = strlen(gi->textinput.value);
796 gi->textinput.number_min = va_arg(ap, int);
797 if (gi->textinput.number_value < gi->textinput.number_min)
799 gi->textinput.number_value = gi->textinput.number_min;
800 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
801 strcpy(gi->textinput.last_value, gi->textinput.value);
806 gi->textinput.number_max = va_arg(ap, int);
807 if (gi->textinput.number_value > gi->textinput.number_max)
809 gi->textinput.number_value = gi->textinput.number_max;
810 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
811 strcpy(gi->textinput.last_value, gi->textinput.value);
817 int max_textsize = MAX_GADGET_TEXTSIZE;
819 if (gi->textinput.size)
820 max_textsize = MIN(gi->textinput.size, MAX_GADGET_TEXTSIZE - 1);
822 strncpy(gi->textinput.value, va_arg(ap, char *), max_textsize);
823 strcpy(gi->textinput.last_value, gi->textinput.value);
825 gi->textinput.value[max_textsize] = '\0';
826 gi->textinput.cursor_position = strlen(gi->textinput.value);
828 /* same tag also used for other gadget definitions */
829 strcpy(gi->textbutton.value, gi->textinput.value);
830 strcpy(gi->textarea.value, gi->textinput.value);
831 strcpy(gi->textarea.last_value, gi->textinput.value);
837 int tag_value = va_arg(ap, int);
838 int max_textsize = MIN(tag_value, MAX_GADGET_TEXTSIZE - 1);
840 gi->textinput.size = max_textsize;
841 gi->textinput.value[max_textsize] = '\0';
842 strcpy(gi->textinput.last_value, gi->textinput.value);
844 /* same tag also used for other gadget definitions */
846 gi->textarea.size = max_textsize;
847 gi->textarea.value[max_textsize] = '\0';
848 strcpy(gi->textarea.last_value, gi->textinput.value);
850 gi->textbutton.size = max_textsize;
851 gi->textbutton.value[max_textsize] = '\0';
853 gi->selectbox.size = gi->textinput.size;
858 gi->font = va_arg(ap, int);
859 if (gi->font_active == 0)
860 gi->font_active = gi->font;
863 case GDI_TEXT_FONT_ACTIVE:
864 gi->font_active = va_arg(ap, int);
867 case GDI_SELECTBOX_OPTIONS:
868 gi->selectbox.options = va_arg(ap, struct ValueTextInfo *);
871 case GDI_SELECTBOX_INDEX:
872 gi->selectbox.index = va_arg(ap, int);
875 case GDI_DESIGN_UNPRESSED:
876 gi->design[GD_BUTTON_UNPRESSED].bitmap = va_arg(ap, Bitmap *);
877 gi->design[GD_BUTTON_UNPRESSED].x = va_arg(ap, int);
878 gi->design[GD_BUTTON_UNPRESSED].y = va_arg(ap, int);
881 case GDI_DESIGN_PRESSED:
882 gi->design[GD_BUTTON_PRESSED].bitmap = va_arg(ap, Bitmap *);
883 gi->design[GD_BUTTON_PRESSED].x = va_arg(ap, int);
884 gi->design[GD_BUTTON_PRESSED].y = va_arg(ap, int);
887 case GDI_ALT_DESIGN_UNPRESSED:
888 gi->alt_design[GD_BUTTON_UNPRESSED].bitmap= va_arg(ap, Bitmap *);
889 gi->alt_design[GD_BUTTON_UNPRESSED].x = va_arg(ap, int);
890 gi->alt_design[GD_BUTTON_UNPRESSED].y = va_arg(ap, int);
893 case GDI_ALT_DESIGN_PRESSED:
894 gi->alt_design[GD_BUTTON_PRESSED].bitmap = va_arg(ap, Bitmap *);
895 gi->alt_design[GD_BUTTON_PRESSED].x = va_arg(ap, int);
896 gi->alt_design[GD_BUTTON_PRESSED].y = va_arg(ap, int);
899 case GDI_BORDER_SIZE:
900 gi->border.xsize = va_arg(ap, int);
901 gi->border.ysize = va_arg(ap, int);
904 case GDI_BORDER_SIZE_SELECTBUTTON:
905 gi->border.xsize_selectbutton = va_arg(ap, int);
908 case GDI_DESIGN_WIDTH:
909 gi->border.width = va_arg(ap, int);
912 case GDI_DECORATION_DESIGN:
913 gi->deco.design.bitmap = va_arg(ap, Bitmap *);
914 gi->deco.design.x = va_arg(ap, int);
915 gi->deco.design.y = va_arg(ap, int);
918 case GDI_DECORATION_POSITION:
919 gi->deco.x = va_arg(ap, int);
920 gi->deco.y = va_arg(ap, int);
923 case GDI_DECORATION_SIZE:
924 gi->deco.width = va_arg(ap, int);
925 gi->deco.height = va_arg(ap, int);
928 case GDI_DECORATION_SHIFTING:
929 gi->deco.xshift = va_arg(ap, int);
930 gi->deco.yshift = va_arg(ap, int);
934 gi->event_mask = va_arg(ap, unsigned int);
938 gi->drawing.area_xsize = va_arg(ap, int);
939 gi->drawing.area_ysize = va_arg(ap, int);
941 /* determine dependent values for drawing area gadget, if needed */
942 if (gi->width == 0 && gi->height == 0 &&
943 gi->drawing.item_xsize !=0 && gi->drawing.item_ysize !=0)
945 gi->width = gi->drawing.area_xsize * gi->drawing.item_xsize;
946 gi->height = gi->drawing.area_ysize * gi->drawing.item_ysize;
948 else if (gi->drawing.item_xsize == 0 && gi->drawing.item_ysize == 0 &&
949 gi->width != 0 && gi->height != 0)
951 gi->drawing.item_xsize = gi->width / gi->drawing.area_xsize;
952 gi->drawing.item_ysize = gi->height / gi->drawing.area_ysize;
955 /* same tag also used for other gadget definitions */
956 gi->textarea.xsize = gi->drawing.area_xsize;
957 gi->textarea.ysize = gi->drawing.area_ysize;
959 if (gi->type & GD_TYPE_TEXT_AREA) /* force recalculation */
968 gi->drawing.item_xsize = va_arg(ap, int);
969 gi->drawing.item_ysize = va_arg(ap, int);
971 /* determine dependent values for drawing area gadget, if needed */
972 if (gi->width == 0 && gi->height == 0 &&
973 gi->drawing.area_xsize !=0 && gi->drawing.area_ysize !=0)
975 gi->width = gi->drawing.area_xsize * gi->drawing.item_xsize;
976 gi->height = gi->drawing.area_ysize * gi->drawing.item_ysize;
978 else if (gi->drawing.area_xsize == 0 && gi->drawing.area_ysize == 0 &&
979 gi->width != 0 && gi->height != 0)
981 gi->drawing.area_xsize = gi->width / gi->drawing.item_xsize;
982 gi->drawing.area_ysize = gi->height / gi->drawing.item_ysize;
986 case GDI_SCROLLBAR_ITEMS_MAX:
987 gi->scrollbar.items_max = va_arg(ap, int);
990 case GDI_SCROLLBAR_ITEMS_VISIBLE:
991 gi->scrollbar.items_visible = va_arg(ap, int);
994 case GDI_SCROLLBAR_ITEM_POSITION:
995 gi->scrollbar.item_position = va_arg(ap, int);
998 case GDI_CALLBACK_INFO:
999 gi->callback_info = va_arg(ap, gadget_function);
1002 case GDI_CALLBACK_ACTION:
1003 gi->callback_action = va_arg(ap, gadget_function);
1007 Error(ERR_EXIT, "HandleGadgetTags(): unknown tag %d", tag);
1010 tag = va_arg(ap, int); /* read next tag */
1013 /* check if gadget is complete */
1014 if (gi->type != GD_TYPE_DRAWING_AREA &&
1015 (!gi->design[GD_BUTTON_UNPRESSED].bitmap ||
1016 !gi->design[GD_BUTTON_PRESSED].bitmap))
1017 Error(ERR_EXIT, "gadget incomplete (missing Bitmap)");
1019 /* adjust gadget values in relation to other gadget values */
1021 if (gi->type & GD_TYPE_TEXT_INPUT)
1023 int font_nr = gi->font_active;
1024 int font_width = getFontWidth(font_nr);
1025 int font_height = getFontHeight(font_nr);
1026 int border_xsize = gi->border.xsize;
1027 int border_ysize = gi->border.ysize;
1029 if (gi->type == GD_TYPE_TEXT_INPUT_NUMERIC)
1031 int number_min = gi->textinput.number_min;
1032 int number_max = gi->textinput.number_max;
1033 int min_size_min = get_minimal_size_for_numeric_input(number_min);
1034 int min_size_max = get_minimal_size_for_numeric_input(number_max);
1035 int min_size = MAX(min_size_min, min_size_max);
1037 /* expand gadget text input size, if maximal value is too large */
1038 if (gi->textinput.size < min_size)
1039 gi->textinput.size = min_size;
1042 gi->width = 2 * border_xsize + (gi->textinput.size + 1) * font_width;
1043 gi->height = 2 * border_ysize + font_height;
1046 if (gi->type & GD_TYPE_SELECTBOX)
1048 int font_nr = gi->font_active;
1049 int font_width = getFontWidth(font_nr);
1050 int font_height = getFontHeight(font_nr);
1051 int border_xsize = gi->border.xsize;
1052 int border_ysize = gi->border.ysize;
1053 int button_size = gi->border.xsize_selectbutton;
1054 int bottom_screen_border = gfx.sy + gfx.sysize - font_height;
1058 gi->width = 2 * border_xsize + gi->textinput.size*font_width +button_size;
1059 gi->height = 2 * border_ysize + font_height;
1061 if (gi->selectbox.options == NULL)
1062 Error(ERR_EXIT, "selectbox gadget incomplete (missing options array)");
1064 gi->selectbox.num_values = 0;
1065 while (gi->selectbox.options[gi->selectbox.num_values].text != NULL)
1066 gi->selectbox.num_values++;
1068 /* calculate values for open selectbox */
1069 gi->selectbox.width = gi->width;
1070 gi->selectbox.height =
1071 2 * border_ysize + gi->selectbox.num_values * font_height;
1073 gi->selectbox.x = gi->x;
1074 gi->selectbox.y = gi->y + gi->height;
1075 if (gi->selectbox.y + gi->selectbox.height > bottom_screen_border)
1076 gi->selectbox.y = gi->y - gi->selectbox.height;
1077 if (gi->selectbox.y < 0)
1078 gi->selectbox.y = bottom_screen_border - gi->selectbox.height;
1080 getFontCharSource(font_nr, FONT_ASCII_CURSOR, &src_bitmap, &src_x, &src_y);
1081 src_x += font_width / 2;
1082 src_y += font_height / 2;
1084 /* there may be esoteric cases with missing or too small font bitmap */
1085 if (src_bitmap != NULL &&
1086 src_x < src_bitmap->width && src_y < src_bitmap->height)
1087 gi->selectbox.inverse_color = GetPixel(src_bitmap, src_x, src_y);
1089 /* always start with closed selectbox */
1090 gi->selectbox.open = FALSE;
1093 if (gi->type & GD_TYPE_TEXT_INPUT_NUMERIC)
1095 struct GadgetTextInput *text = &gi->textinput;
1096 int value = text->number_value;
1098 text->number_value = (value < text->number_min ? text->number_min :
1099 value > text->number_max ? text->number_max :
1102 sprintf(text->value, "%d", text->number_value);
1105 if (gi->type & GD_TYPE_TEXT_BUTTON)
1107 int font_nr = gi->font_active;
1108 int font_width = getFontWidth(font_nr);
1109 int font_height = getFontHeight(font_nr);
1110 int border_xsize = gi->border.xsize;
1111 int border_ysize = gi->border.ysize;
1113 gi->width = 2 * border_xsize + gi->textbutton.size * font_width;
1114 gi->height = 2 * border_ysize + font_height;
1117 if (gi->type & GD_TYPE_SCROLLBAR)
1119 struct GadgetScrollbar *gs = &gi->scrollbar;
1120 int scrollbar_size_cmp;
1122 if (gi->width == 0 || gi->height == 0 ||
1123 gs->items_max == 0 || gs->items_visible == 0)
1124 Error(ERR_EXIT, "scrollbar gadget incomplete (missing tags)");
1126 /* calculate internal scrollbar values */
1127 gs->size_min = (gi->type == GD_TYPE_SCROLLBAR_VERTICAL ?
1128 gi->width : gi->height);
1129 gs->size_max = (gi->type == GD_TYPE_SCROLLBAR_VERTICAL ?
1130 gi->height : gi->width);
1132 scrollbar_size_cmp = gs->size_max * gs->items_visible / gs->items_max;
1133 gs->size = MAX(scrollbar_size_cmp, gs->size_min);
1134 gs->size_max_cmp = (gs->size_max - (gs->size - scrollbar_size_cmp));
1136 gs->position = gs->size_max_cmp * gs->item_position / gs->items_max;
1137 gs->position_max = gs->size_max - gs->size;
1138 gs->correction = gs->size_max / gs->items_max / 2;
1140 /* finetuning for maximal right/bottom position */
1141 if (gs->item_position == gs->items_max - gs->items_visible)
1142 gs->position = gs->position_max;
1145 if (gi->type & GD_TYPE_TEXT_AREA)
1147 int font_nr = gi->font_active;
1148 int font_width = getFontWidth(font_nr);
1149 int font_height = getFontHeight(font_nr);
1150 int border_xsize = gi->border.xsize;
1151 int border_ysize = gi->border.ysize;
1153 if (gi->width == 0 || gi->height == 0)
1155 gi->width = 2 * border_xsize + gi->textarea.xsize * font_width;
1156 gi->height = 2 * border_ysize + gi->textarea.ysize * font_height;
1160 gi->textarea.xsize = (gi->width - 2 * border_xsize) / font_width;
1161 gi->textarea.ysize = (gi->height - 2 * border_ysize) / font_height;
1166 void ModifyGadget(struct GadgetInfo *gi, int first_tag, ...)
1170 va_start(ap, first_tag);
1171 HandleGadgetTags(gi, first_tag, ap);
1177 void RedrawGadget(struct GadgetInfo *gi)
1183 DrawGadget(gi, gi->state, gi->direct_draw);
1186 struct GadgetInfo *CreateGadget(int first_tag, ...)
1188 struct GadgetInfo *new_gadget = checked_calloc(sizeof(struct GadgetInfo));
1191 /* always start with reliable default values */
1192 new_gadget->id = getNewGadgetID();
1193 new_gadget->callback_info = default_callback_info;
1194 new_gadget->callback_action = default_callback_action;
1195 new_gadget->active = TRUE;
1196 new_gadget->direct_draw = TRUE;
1198 new_gadget->next = NULL;
1200 va_start(ap, first_tag);
1201 HandleGadgetTags(new_gadget, first_tag, ap);
1204 /* insert new gadget into global gadget list */
1205 if (gadget_list_last_entry)
1207 gadget_list_last_entry->next = new_gadget;
1208 gadget_list_last_entry = gadget_list_last_entry->next;
1211 gadget_list_first_entry = gadget_list_last_entry = new_gadget;
1216 void FreeGadget(struct GadgetInfo *gi)
1218 struct GadgetInfo *gi_previous = gadget_list_first_entry;
1220 /* prevent "last_info_gi" from pointing to memory that will be freed */
1221 if (last_info_gi == gi)
1222 last_info_gi = NULL;
1224 while (gi_previous != NULL && gi_previous->next != gi)
1225 gi_previous = gi_previous->next;
1227 if (gi == gadget_list_first_entry)
1228 gadget_list_first_entry = gi->next;
1230 if (gi == gadget_list_last_entry)
1231 gadget_list_last_entry = gi_previous;
1233 if (gi_previous != NULL)
1234 gi_previous->next = gi->next;
1239 static void CheckRangeOfNumericInputGadget(struct GadgetInfo *gi)
1241 if (gi->type != GD_TYPE_TEXT_INPUT_NUMERIC)
1244 gi->textinput.number_value = atoi(gi->textinput.value);
1246 if (gi->textinput.number_value < gi->textinput.number_min)
1247 gi->textinput.number_value = gi->textinput.number_min;
1248 if (gi->textinput.number_value > gi->textinput.number_max)
1249 gi->textinput.number_value = gi->textinput.number_max;
1251 sprintf(gi->textinput.value, "%d", gi->textinput.number_value);
1253 if (gi->textinput.cursor_position < 0)
1254 gi->textinput.cursor_position = 0;
1255 else if (gi->textinput.cursor_position > strlen(gi->textinput.value))
1256 gi->textinput.cursor_position = strlen(gi->textinput.value);
1259 /* global pointer to gadget actually in use (when mouse button pressed) */
1260 static struct GadgetInfo *last_gi = NULL;
1262 static void MapGadgetExt(struct GadgetInfo *gi, boolean redraw)
1264 if (gi == NULL || gi->mapped)
1270 DrawGadget(gi, DG_UNPRESSED, DG_BUFFERED);
1273 void MapGadget(struct GadgetInfo *gi)
1275 MapGadgetExt(gi, TRUE);
1278 void UnmapGadget(struct GadgetInfo *gi)
1280 if (gi == NULL || !gi->mapped)
1289 #define MAX_NUM_GADGETS 1024
1290 #define MULTIMAP_UNMAP (1 << 0)
1291 #define MULTIMAP_REMAP (1 << 1)
1292 #define MULTIMAP_REDRAW (1 << 2)
1293 #define MULTIMAP_PLAYFIELD (1 << 3)
1294 #define MULTIMAP_DOOR_1 (1 << 4)
1295 #define MULTIMAP_DOOR_2 (1 << 5)
1296 #define MULTIMAP_ALL (MULTIMAP_PLAYFIELD | \
1300 static void MultiMapGadgets(int mode)
1302 struct GadgetInfo *gi = gadget_list_first_entry;
1303 static boolean map_state[MAX_NUM_GADGETS];
1308 if ((mode & MULTIMAP_PLAYFIELD &&
1309 gi->x < gfx.sx + gfx.sxsize) ||
1310 (mode & MULTIMAP_DOOR_1 &&
1311 gi->x >= gfx.dx && gi->y < gfx.dy + gfx.dysize) ||
1312 (mode & MULTIMAP_DOOR_2 &&
1313 gi->x >= gfx.dx && gi->y > gfx.dy + gfx.dysize) ||
1314 (mode & MULTIMAP_ALL) == MULTIMAP_ALL)
1316 if (mode & MULTIMAP_UNMAP)
1318 map_state[map_count++ % MAX_NUM_GADGETS] = gi->mapped;
1323 if (map_state[map_count++ % MAX_NUM_GADGETS])
1324 MapGadgetExt(gi, (mode & MULTIMAP_REDRAW));
1332 void UnmapAllGadgets()
1334 MultiMapGadgets(MULTIMAP_ALL | MULTIMAP_UNMAP);
1337 void RemapAllGadgets()
1339 MultiMapGadgets(MULTIMAP_ALL | MULTIMAP_REMAP);
1342 boolean anyTextInputGadgetActive()
1344 return (last_gi && (last_gi->type & GD_TYPE_TEXT_INPUT) && last_gi->mapped);
1347 boolean anyTextAreaGadgetActive()
1349 return (last_gi && (last_gi->type & GD_TYPE_TEXT_AREA) && last_gi->mapped);
1352 boolean anySelectboxGadgetActive()
1354 return (last_gi && (last_gi->type & GD_TYPE_SELECTBOX) && last_gi->mapped);
1357 boolean anyScrollbarGadgetActive()
1359 return (last_gi && (last_gi->type & GD_TYPE_SCROLLBAR) && last_gi->mapped);
1362 boolean anyTextGadgetActive()
1364 return (anyTextInputGadgetActive() ||
1365 anyTextAreaGadgetActive() ||
1366 anySelectboxGadgetActive());
1369 static boolean insideSelectboxLine(struct GadgetInfo *gi, int mx, int my)
1371 return(gi != NULL &&
1372 gi->type & GD_TYPE_SELECTBOX &&
1373 mx >= gi->x && mx < gi->x + gi->width &&
1374 my >= gi->y && my < gi->y + gi->height);
1377 static boolean insideSelectboxArea(struct GadgetInfo *gi, int mx, int my)
1379 return(gi != NULL &&
1380 gi->type & GD_TYPE_SELECTBOX &&
1381 mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width &&
1382 my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height);
1385 void ClickOnGadget(struct GadgetInfo *gi, int button)
1390 /* simulate releasing mouse button over last gadget, if still pressed */
1392 HandleGadgets(-1, -1, 0);
1394 /* simulate pressing mouse button over specified gadget */
1395 HandleGadgets(gi->x, gi->y, button);
1397 /* simulate releasing mouse button over specified gadget */
1398 HandleGadgets(gi->x, gi->y, 0);
1401 boolean HandleGadgets(int mx, int my, int button)
1403 static unsigned long pressed_delay = 0;
1404 static int last_button = 0;
1405 static int last_mx = 0, last_my = 0;
1406 static int pressed_mx = 0, pressed_my = 0;
1407 static boolean keep_selectbox_open = FALSE;
1408 static boolean gadget_stopped = FALSE;
1409 int scrollbar_mouse_pos = 0;
1410 struct GadgetInfo *new_gi, *gi;
1411 boolean press_event;
1412 boolean release_event;
1413 boolean mouse_moving;
1414 boolean mouse_inside_select_line;
1415 boolean mouse_inside_select_area;
1416 boolean mouse_released_where_pressed;
1417 boolean gadget_pressed;
1418 boolean gadget_pressed_repeated;
1419 boolean gadget_pressed_off_borders;
1420 boolean gadget_pressed_inside_select_line;
1421 boolean gadget_pressed_delay_reached;
1422 boolean gadget_moving;
1423 boolean gadget_moving_inside;
1424 boolean gadget_moving_off_borders;
1425 boolean gadget_draggable;
1426 boolean gadget_dragging;
1427 boolean gadget_released;
1428 boolean gadget_released_inside;
1429 boolean gadget_released_inside_select_line;
1430 boolean gadget_released_inside_select_area;
1431 boolean gadget_released_off_borders;
1432 boolean changed_position = FALSE;
1434 /* check if there are any gadgets defined */
1435 if (gadget_list_first_entry == NULL)
1438 /* simulated release of mouse button over last gadget */
1439 if (mx == -1 && my == -1 && button == 0)
1445 /* check which gadget is under the mouse pointer */
1446 new_gi = getGadgetInfoFromMousePosition(mx, my, button);
1448 /* check if button state has changed since last invocation */
1449 press_event = (button != 0 && last_button == 0);
1450 release_event = (button == 0 && last_button != 0);
1451 last_button = button;
1453 /* check if mouse has been moved since last invocation */
1454 mouse_moving = ((mx != last_mx || my != last_my) && motion_status);
1458 if (press_event && new_gi != last_gi)
1464 mouse_released_where_pressed =
1465 (release_event && mx == pressed_mx && my == pressed_my);
1467 mouse_inside_select_line = insideSelectboxLine(new_gi, mx, my);
1468 mouse_inside_select_area = insideSelectboxArea(new_gi, mx, my);
1470 gadget_pressed_off_borders = (press_event && new_gi != last_gi);
1472 gadget_pressed_inside_select_line =
1473 (press_event && new_gi != NULL &&
1474 new_gi->type & GD_TYPE_SELECTBOX && new_gi->selectbox.open &&
1475 insideSelectboxLine(new_gi, mx, my));
1477 /* if mouse button pressed outside text or selectbox gadget, deactivate it */
1478 if (anyTextGadgetActive() &&
1479 (gadget_pressed_off_borders ||
1480 (gadget_pressed_inside_select_line && !mouse_inside_select_area)))
1482 struct GadgetInfo *gi = last_gi;
1483 boolean gadget_changed = (gi->event_mask & GD_EVENT_TEXT_LEAVING);
1485 /* check if text gadget has changed its value */
1486 if (gi->type & GD_TYPE_TEXT_INPUT)
1488 CheckRangeOfNumericInputGadget(gi);
1490 if (!strEqual(gi->textinput.last_value, gi->textinput.value))
1491 strcpy(gi->textinput.last_value, gi->textinput.value);
1493 gadget_changed = FALSE;
1496 /* selectbox does not change its value when closed by clicking outside */
1497 if (gi->type & GD_TYPE_SELECTBOX)
1498 gadget_changed = FALSE;
1500 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1502 gi->event.type = GD_EVENT_TEXT_LEAVING;
1504 if (gadget_changed && !(gi->type & GD_TYPE_SELECTBOX))
1505 gi->callback_action(gi);
1509 if (gadget_pressed_inside_select_line)
1514 (button != 0 && last_gi == NULL && new_gi != NULL && press_event);
1515 gadget_pressed_repeated =
1516 (button != 0 && last_gi != NULL && new_gi == last_gi);
1518 gadget_pressed_delay_reached =
1519 DelayReached(&pressed_delay, GADGET_FRAME_DELAY);
1521 gadget_released = (release_event && last_gi != NULL);
1522 gadget_released_inside = (gadget_released && new_gi == last_gi);
1523 gadget_released_off_borders = (gadget_released && new_gi != last_gi);
1525 gadget_moving = (button != 0 && last_gi != NULL && mouse_moving);
1526 gadget_moving_inside = (gadget_moving && new_gi == last_gi);
1527 gadget_moving_off_borders = (gadget_moving && new_gi != last_gi);
1529 /* when handling selectbox, set additional state values */
1530 if (gadget_released_inside && (last_gi->type & GD_TYPE_SELECTBOX))
1532 gadget_released_inside_select_line = insideSelectboxLine(last_gi, mx, my);
1533 gadget_released_inside_select_area = insideSelectboxArea(last_gi, mx, my);
1537 gadget_released_inside_select_line = FALSE;
1538 gadget_released_inside_select_area = FALSE;
1541 /* setting state for handling over-large selectbox */
1542 if (keep_selectbox_open && (press_event || !mouse_inside_select_line))
1543 keep_selectbox_open = FALSE;
1545 /* if new gadget pressed, store this gadget */
1549 /* 'gi' is actually handled gadget */
1552 /* if gadget is scrollbar, choose mouse position value */
1553 if (gi && gi->type & GD_TYPE_SCROLLBAR)
1554 scrollbar_mouse_pos =
1555 (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx - gi->x : my - gi->y);
1557 /* if mouse button released, no gadget needs to be handled anymore */
1558 if (gadget_released)
1560 if (gi->type & GD_TYPE_SELECTBOX &&
1561 (keep_selectbox_open ||
1562 mouse_released_where_pressed ||
1563 !gadget_released_inside_select_area)) /* selectbox stays open */
1565 gi->selectbox.stay_open = TRUE;
1569 else if (!(gi->type & GD_TYPE_TEXT_INPUT ||
1570 gi->type & GD_TYPE_TEXT_AREA)) /* text input stays open */
1574 /* modify event position values even if no gadget is pressed */
1575 if (button == 0 && !release_event)
1578 /* if new gadget or if no gadget was pressed, release stopped processing */
1579 if (gadget_pressed || new_gi == NULL)
1580 gadget_stopped = FALSE;
1582 /* if gadget was stopped while being handled, stop gadget processing here */
1588 int last_x = gi->event.x;
1589 int last_y = gi->event.y;
1591 gi->event.x = mx - gi->x;
1592 gi->event.y = my - gi->y;
1594 if (gi->type == GD_TYPE_DRAWING_AREA)
1596 gi->event.x /= gi->drawing.item_xsize;
1597 gi->event.y /= gi->drawing.item_ysize;
1599 if (last_x != gi->event.x || last_y != gi->event.y)
1600 changed_position = TRUE;
1602 else if (gi->type & GD_TYPE_TEXT_INPUT && button != 0 && !motion_status)
1604 int old_cursor_position = gi->textinput.cursor_position;
1606 /* if mouse button pressed inside activated text gadget, set cursor */
1607 gi->textinput.cursor_position =
1608 (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font);
1610 if (gi->textinput.cursor_position < 0)
1611 gi->textinput.cursor_position = 0;
1612 else if (gi->textinput.cursor_position > strlen(gi->textinput.value))
1613 gi->textinput.cursor_position = strlen(gi->textinput.value);
1615 if (gi->textinput.cursor_position != old_cursor_position)
1616 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1618 else if (gi->type & GD_TYPE_TEXT_AREA && button != 0 && !motion_status)
1620 int old_cursor_position = gi->textarea.cursor_position;
1621 int x = (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font);
1622 int y = (my - gi->y - gi->border.ysize) / getFontHeight(gi->font);
1624 x = (x < 0 ? 0 : x >= gi->textarea.xsize ? gi->textarea.xsize - 1 : x);
1625 y = (y < 0 ? 0 : y >= gi->textarea.ysize ? gi->textarea.ysize - 1 : y);
1627 setTextAreaCursorXY(gi, x, y);
1629 if (gi->textarea.cursor_position != old_cursor_position)
1630 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1632 else if (gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open &&
1633 !keep_selectbox_open)
1635 int old_index = gi->selectbox.current_index;
1637 /* if mouse moving inside activated selectbox, select value */
1638 if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
1639 gi->selectbox.current_index =
1640 (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font);
1642 if (gi->selectbox.current_index < 0)
1643 gi->selectbox.current_index = 0;
1644 else if (gi->selectbox.current_index > gi->selectbox.num_values - 1)
1645 gi->selectbox.current_index = gi->selectbox.num_values - 1;
1647 if (gi->selectbox.current_index != old_index)
1648 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1652 /* handle gadget popup info text */
1653 if (last_info_gi != new_gi ||
1654 (new_gi && new_gi->type == GD_TYPE_DRAWING_AREA && changed_position))
1656 if (new_gi != NULL && (button == 0 || new_gi == last_gi))
1658 new_gi->event.type = GD_EVENT_INFO_ENTERING;
1659 new_gi->callback_info(new_gi);
1661 else if (last_info_gi != NULL)
1663 last_info_gi->event.type = GD_EVENT_INFO_LEAVING;
1664 last_info_gi->callback_info(last_info_gi);
1667 last_info_gi = new_gi;
1672 gadget_draggable = (gi && gi->type & GD_TYPE_SCROLLBAR);
1674 /* reset drag position for newly pressed scrollbar to "not dragging" */
1675 if (gadget_pressed && gadget_draggable)
1676 gi->scrollbar.drag_position = -1;
1678 gadget_dragging = (gadget_draggable && gi->scrollbar.drag_position != -1);
1680 /* clicking next to a scrollbar to move it is not considered "moving" */
1681 if (gadget_draggable && !gadget_dragging)
1682 gadget_moving = FALSE;
1684 /* when leaving scrollbar area when jump-scrolling, stop gadget processing */
1685 if (gadget_draggable && !gadget_dragging && gadget_moving_off_borders)
1686 gadget_stopped = TRUE;
1688 if ((gadget_pressed) ||
1689 (gadget_pressed_repeated && gadget_pressed_delay_reached))
1691 if (gi->type & GD_TYPE_SCROLLBAR && !gadget_dragging)
1693 int mpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx : my);
1694 int gpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? gi->x : gi->y);
1697 mpos < gpos + gi->scrollbar.position ||
1698 mpos >= gpos + gi->scrollbar.position + gi->scrollbar.size)
1700 /* click scrollbar one scrollbar length up/left or down/right */
1702 struct GadgetScrollbar *gs = &gi->scrollbar;
1703 int old_item_position = gs->item_position;
1704 int item_steps = gs->items_visible - 1;
1705 int item_direction = (mpos < gpos + gi->scrollbar.position ? -1 : +1);
1710 item_direction = (button == 4 ? -1 : +1);
1713 changed_position = FALSE;
1715 gs->item_position += item_steps * item_direction;
1717 if (gs->item_position < 0)
1718 gs->item_position = 0;
1719 else if (gs->item_position > gs->items_max - gs->items_visible)
1720 gs->item_position = gs->items_max - gs->items_visible;
1722 if (old_item_position != gs->item_position)
1724 gi->event.item_position = gs->item_position;
1725 changed_position = TRUE;
1728 ModifyGadget(gi, GDI_SCROLLBAR_ITEM_POSITION, gs->item_position,
1731 gi->state = GD_BUTTON_UNPRESSED;
1732 gi->event.type = GD_EVENT_MOVING;
1733 gi->event.off_borders = FALSE;
1735 if (gi->event_mask & GD_EVENT_MOVING && changed_position)
1736 gi->callback_action(gi);
1742 /* don't handle this scrollbar anymore when mouse position reached */
1743 if (gadget_pressed_repeated)
1745 gadget_stopped = TRUE;
1757 if (gi->type == GD_TYPE_CHECK_BUTTON)
1759 gi->checked = !gi->checked;
1761 else if (gi->type == GD_TYPE_RADIO_BUTTON)
1763 struct GadgetInfo *rgi = gadget_list_first_entry;
1768 rgi->type == GD_TYPE_RADIO_BUTTON &&
1769 rgi->radio_nr == gi->radio_nr &&
1772 rgi->checked = FALSE;
1773 DrawGadget(rgi, DG_UNPRESSED, rgi->direct_draw);
1781 else if (gi->type & GD_TYPE_SCROLLBAR)
1783 int mpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx : my);
1784 int gpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? gi->x : gi->y);
1786 if (button >= 1 && button <= 3 &&
1787 mpos >= gpos + gi->scrollbar.position &&
1788 mpos < gpos + gi->scrollbar.position + gi->scrollbar.size)
1790 /* start dragging scrollbar */
1791 gi->scrollbar.drag_position =
1792 scrollbar_mouse_pos - gi->scrollbar.position;
1795 else if (gi->type & GD_TYPE_SELECTBOX)
1797 /* keep selectbox open in case of over-large selectbox */
1798 keep_selectbox_open = (mouse_inside_select_line &&
1799 mouse_inside_select_area);
1802 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1804 gi->state = GD_BUTTON_PRESSED;
1805 gi->event.type = GD_EVENT_PRESSED;
1806 gi->event.button = button;
1807 gi->event.off_borders = FALSE;
1809 /* initialize delay counter */
1810 DelayReached(&pressed_delay, 0);
1812 if (gi->event_mask & GD_EVENT_PRESSED)
1813 gi->callback_action(gi);
1816 if (gadget_pressed_repeated)
1818 gi->event.type = GD_EVENT_PRESSED;
1821 if (gi->event_mask & GD_EVENT_REPEATED && gadget_pressed_delay_reached)
1822 gi->callback_action(gi);
1824 if (gi->event_mask & GD_EVENT_REPEATED &&
1825 DelayReached(&pressed_delay, GADGET_FRAME_DELAY))
1826 gi->callback_action(gi);
1832 if (gi->type & GD_TYPE_BUTTON)
1834 if (gadget_moving_inside && gi->state == GD_BUTTON_UNPRESSED)
1835 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1836 else if (gadget_moving_off_borders && gi->state == GD_BUTTON_PRESSED)
1837 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1839 else if (gi->type & GD_TYPE_SELECTBOX && !keep_selectbox_open)
1841 int old_index = gi->selectbox.current_index;
1843 /* if mouse moving inside activated selectbox, select value */
1844 if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height)
1845 gi->selectbox.current_index =
1846 (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font);
1848 if (gi->selectbox.current_index < 0)
1849 gi->selectbox.current_index = 0;
1850 else if (gi->selectbox.current_index > gi->selectbox.num_values - 1)
1851 gi->selectbox.current_index = gi->selectbox.num_values - 1;
1853 if (gi->selectbox.current_index != old_index)
1854 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1856 else if (gi->type & GD_TYPE_SCROLLBAR)
1858 struct GadgetScrollbar *gs = &gi->scrollbar;
1859 int old_item_position = gs->item_position;
1861 gs->position = scrollbar_mouse_pos - gs->drag_position;
1863 /* make sure to always precisely reach end positions when dragging */
1864 if (gs->position <= 0)
1867 gs->item_position = 0;
1869 else if (gs->position >= gs->position_max)
1871 gs->position = gs->position_max;
1872 gs->item_position = gs->items_max - gs->items_visible;
1877 gs->items_max * (gs->position + gs->correction) / gs->size_max_cmp;
1880 if (gs->item_position < 0)
1881 gs->item_position = 0;
1882 if (gs->item_position > gs->items_max - 1)
1883 gs->item_position = gs->items_max - 1;
1885 if (old_item_position != gs->item_position)
1887 gi->event.item_position = gs->item_position;
1888 changed_position = TRUE;
1891 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
1894 gi->state = (gadget_moving_inside || gadget_draggable ?
1895 GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
1896 gi->event.type = GD_EVENT_MOVING;
1897 gi->event.off_borders = gadget_moving_off_borders;
1899 if (gi->event_mask & GD_EVENT_MOVING && changed_position &&
1900 (gadget_moving_inside || gi->event_mask & GD_EVENT_OFF_BORDERS))
1901 gi->callback_action(gi);
1904 if (gadget_released_inside)
1906 boolean deactivate_gadget = TRUE;
1907 boolean gadget_changed = TRUE;
1909 if (gi->type & GD_TYPE_SELECTBOX)
1911 if (keep_selectbox_open ||
1912 mouse_released_where_pressed ||
1913 !gadget_released_inside_select_area) /* selectbox stays open */
1915 deactivate_gadget = FALSE;
1916 gadget_changed = FALSE;
1918 else if (gi->selectbox.index != gi->selectbox.current_index)
1919 gi->selectbox.index = gi->selectbox.current_index;
1921 gadget_changed = FALSE;
1924 if (deactivate_gadget &&
1925 !(gi->type & GD_TYPE_TEXT_INPUT ||
1926 gi->type & GD_TYPE_TEXT_AREA)) /* text input stays open */
1927 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1929 gi->state = GD_BUTTON_UNPRESSED;
1930 gi->event.type = GD_EVENT_RELEASED;
1932 if ((gi->event_mask & GD_EVENT_RELEASED) && gadget_changed)
1934 gi->callback_action(gi);
1938 if (gadget_released_off_borders)
1940 if (gi->type & GD_TYPE_SCROLLBAR)
1941 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
1944 gi->state = GD_BUTTON_UNPRESSED;
1946 gi->event.type = GD_EVENT_RELEASED;
1948 if (gi->event_mask & GD_EVENT_RELEASED &&
1949 gi->event_mask & GD_EVENT_OFF_BORDERS)
1950 gi->callback_action(gi);
1953 /* handle gadgets unmapped/mapped between pressing and releasing */
1954 if (release_event && !gadget_released && new_gi)
1955 new_gi->state = GD_BUTTON_UNPRESSED;
1957 return (gadget_pressed || gadget_pressed_repeated ||
1958 gadget_released || gadget_moving);
1961 static void insertCharIntoTextArea(struct GadgetInfo *gi, char c)
1963 char text[MAX_GADGET_TEXTSIZE];
1964 int cursor_position = gi->textarea.cursor_position;
1966 if (strlen(gi->textarea.value) == MAX_GADGET_TEXTSIZE) /* no space left */
1969 strcpy(text, gi->textarea.value);
1970 strcpy(&gi->textarea.value[cursor_position + 1], &text[cursor_position]);
1971 gi->textarea.value[cursor_position] = c;
1973 setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1);
1976 boolean HandleGadgetsKeyInput(Key key)
1978 struct GadgetInfo *gi = last_gi;
1980 if (gi == NULL || !gi->mapped ||
1981 !(gi->type & GD_TYPE_TEXT_INPUT ||
1982 gi->type & GD_TYPE_TEXT_AREA ||
1983 gi->type & GD_TYPE_SELECTBOX))
1986 if (key == KSYM_Return) /* valid for both text input and selectbox */
1988 boolean gadget_changed = (gi->event_mask & GD_EVENT_TEXT_RETURN);
1990 if (gi->type & GD_TYPE_TEXT_INPUT)
1992 CheckRangeOfNumericInputGadget(gi);
1994 if (!strEqual(gi->textinput.last_value, gi->textinput.value))
1995 strcpy(gi->textinput.last_value, gi->textinput.value);
1997 gadget_changed = FALSE;
1999 else if (gi->type & GD_TYPE_SELECTBOX)
2001 if (gi->selectbox.index != gi->selectbox.current_index)
2002 gi->selectbox.index = gi->selectbox.current_index;
2004 gadget_changed = FALSE;
2007 if (gi->type & GD_TYPE_TEXT_AREA)
2009 insertCharIntoTextArea(gi, '\n');
2011 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2015 DrawGadget(gi, DG_UNPRESSED, gi->direct_draw);
2017 gi->event.type = GD_EVENT_TEXT_RETURN;
2023 gi->callback_action(gi);
2025 else if (gi->type & GD_TYPE_TEXT_INPUT) /* only valid for text input */
2027 char text[MAX_GADGET_TEXTSIZE];
2028 int text_length = strlen(gi->textinput.value);
2029 int cursor_pos = gi->textinput.cursor_position;
2030 char letter = getCharFromKey(key);
2031 boolean legal_letter = (gi->type == GD_TYPE_TEXT_INPUT_NUMERIC ?
2032 letter >= '0' && letter <= '9' :
2035 if (legal_letter && text_length < gi->textinput.size)
2037 strcpy(text, gi->textinput.value);
2038 strcpy(&gi->textinput.value[cursor_pos + 1], &text[cursor_pos]);
2039 gi->textinput.value[cursor_pos] = letter;
2040 gi->textinput.cursor_position++;
2042 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2044 else if (key == KSYM_Left && cursor_pos > 0)
2046 gi->textinput.cursor_position--;
2048 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2050 else if (key == KSYM_Right && cursor_pos < text_length)
2052 gi->textinput.cursor_position++;
2054 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2056 else if (key == KSYM_BackSpace && cursor_pos > 0)
2058 strcpy(text, gi->textinput.value);
2059 strcpy(&gi->textinput.value[cursor_pos - 1], &text[cursor_pos]);
2060 gi->textinput.cursor_position--;
2062 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2064 else if (key == KSYM_Delete && cursor_pos < text_length)
2066 strcpy(text, gi->textinput.value);
2067 strcpy(&gi->textinput.value[cursor_pos], &text[cursor_pos + 1]);
2069 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2072 else if (gi->type & GD_TYPE_TEXT_AREA) /* only valid for text area */
2074 char text[MAX_GADGET_TEXTSIZE];
2075 int text_length = strlen(gi->textarea.value);
2076 int area_ysize = gi->textarea.ysize;
2077 int cursor_x_pref = gi->textarea.cursor_x_preferred;
2078 int cursor_y = gi->textarea.cursor_y;
2079 int cursor_pos = gi->textarea.cursor_position;
2080 char letter = getCharFromKey(key);
2081 boolean legal_letter = (letter != 0);
2085 insertCharIntoTextArea(gi, letter);
2087 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2089 else if (key == KSYM_Left && cursor_pos > 0)
2091 setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1);
2093 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2095 else if (key == KSYM_Right && cursor_pos < text_length)
2097 setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1);
2099 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2101 else if (key == KSYM_Up && cursor_y > 0)
2103 setTextAreaCursorXY(gi, cursor_x_pref, cursor_y - 1);
2104 gi->textarea.cursor_x_preferred = cursor_x_pref;
2106 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2108 else if (key == KSYM_Down && cursor_y < area_ysize - 1)
2110 setTextAreaCursorXY(gi, cursor_x_pref, cursor_y + 1);
2111 gi->textarea.cursor_x_preferred = cursor_x_pref;
2113 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2115 else if (key == KSYM_BackSpace && cursor_pos > 0)
2117 strcpy(text, gi->textarea.value);
2118 strcpy(&gi->textarea.value[cursor_pos - 1], &text[cursor_pos]);
2120 setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1);
2122 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2124 else if (key == KSYM_Delete && cursor_pos < text_length)
2126 strcpy(text, gi->textarea.value);
2127 strcpy(&gi->textarea.value[cursor_pos], &text[cursor_pos + 1]);
2129 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2132 else if (gi->type & GD_TYPE_SELECTBOX) /* only valid for selectbox */
2134 int index = gi->selectbox.current_index;
2135 int num_values = gi->selectbox.num_values;
2137 if (key == KSYM_Up && index > 0)
2139 gi->selectbox.current_index--;
2141 DrawGadget(gi, DG_PRESSED, gi->direct_draw);
2143 else if (key == KSYM_Down && index < num_values - 1)
2145 gi->selectbox.current_index++;
2147 DrawGadget(gi, DG_PRESSED, gi->direct_draw);