X-Git-Url: https://git.artsoft.org/?p=rocksndiamonds.git;a=blobdiff_plain;f=src%2Flibgame%2Fgadgets.c;h=9c07371bc31a7321e6422ea839f45133af95143f;hp=dd9d28838273d6a98b8960b71d58705d950b9349;hb=abe44529b439ad39b4d8dbf19cbd67c9b9844279;hpb=8e5671db64b55432158b1d43d7fd2e684544ebeb diff --git a/src/libgame/gadgets.c b/src/libgame/gadgets.c index dd9d2883..9c07371b 100644 --- a/src/libgame/gadgets.c +++ b/src/libgame/gadgets.c @@ -1,15 +1,13 @@ -/*********************************************************** -* Artsoft Retro-Game Library * -*----------------------------------------------------------* -* (c) 1994-2002 Artsoft Entertainment * -* Holger Schemel * -* Detmolder Strasse 189 * -* 33604 Bielefeld * -* Germany * -* e-mail: info@artsoft.org * -*----------------------------------------------------------* -* gadgets.c * -***********************************************************/ +// ============================================================================ +// Artsoft Retro-Game Library +// ---------------------------------------------------------------------------- +// (c) 1995-2014 by Artsoft Entertainment +// Holger Schemel +// info@artsoft.org +// http://www.artsoft.org/ +// ---------------------------------------------------------------------------- +// gadgets.c +// ============================================================================ #include #include @@ -26,11 +24,26 @@ #define DG_BUFFERED 0 #define DG_DIRECT 1 +#define GADGET_DEACTIVATED(g) ((g)->x < 0 || (g)->y < 0) + + static struct GadgetInfo *gadget_list_first_entry = NULL; static struct GadgetInfo *gadget_list_last_entry = NULL; +static struct GadgetInfo *last_info_gi = NULL; static int next_free_gadget_id = 1; static boolean gadget_id_wrapped = FALSE; +static void (*PlayGadgetSoundActivating)(void) = NULL; +static void (*PlayGadgetSoundSelecting)(void) = NULL; + + +void InitGadgetsSoundCallback(void (*activating_function)(void), + void (*selecting_function)(void)) +{ + PlayGadgetSoundActivating = activating_function; + PlayGadgetSoundSelecting = selecting_function; +} + static struct GadgetInfo *getGadgetInfoFromGadgetID(int id) { struct GadgetInfo *gi = gadget_list_first_entry; @@ -64,24 +77,32 @@ static int getNewGadgetID() return id; } -#if 0 -void DUMP_GADGET_MAP_STATE() +static struct GadgetInfo *getGadgetInfoFromMousePosition(int mx, int my, + int button) { - struct GadgetInfo *gi = gadget_list_first_entry; + struct GadgetInfo *gi; - while (gi != NULL) + /* first check for scrollbars in case of mouse scroll wheel button events */ + if (IS_WHEEL_BUTTON(button)) { - printf("-XXX-1-> '%s': %s\n", - gi->info_text, (gi->mapped ? "mapped" : "not mapped")); + /* real horizontal wheel or vertical wheel with modifier key pressed */ + boolean check_horizontal = (IS_WHEEL_BUTTON_HORIZONTAL(button) || + GetKeyModState() & KMOD_Shift); - gi = gi->next; - } -} -#endif + /* check for the first active scrollbar with matching mouse wheel area */ + for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next) + { + if (gi->mapped && gi->active && + ((gi->type & GD_TYPE_SCROLLBAR_HORIZONTAL && check_horizontal) || + (gi->type & GD_TYPE_SCROLLBAR_VERTICAL && !check_horizontal)) && + mx >= gi->wheelarea.x && mx < gi->wheelarea.x + gi->wheelarea.width && + my >= gi->wheelarea.y && my < gi->wheelarea.y + gi->wheelarea.height) + return gi; + } -static struct GadgetInfo *getGadgetInfoFromMousePosition(int mx, int my) -{ - struct GadgetInfo *gi; + /* no active scrollbar found -- ignore this scroll wheel button event */ + return NULL; + } /* open selectboxes may overlap other active gadgets, so check them first */ for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next) @@ -178,12 +199,16 @@ static void default_callback_action(void *ptr) static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) { + struct GadgetDesign *gd; int state = (pressed ? GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED); - struct GadgetDesign *gd = (!gi->active ? &gi->alt_design[state] : - gi->checked ? &gi->alt_design[state] : - &gi->design[state]); boolean redraw_selectbox = FALSE; + if (gi == NULL) + return; + + gd = (!gi->active ? &gi->alt_design[state] : + gi->checked ? &gi->alt_design[state] : &gi->design[state]); + switch (gi->type) { case GD_TYPE_NORMAL_BUTTON: @@ -357,10 +382,16 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) gi->height - 2 * border_y); /* gadget text value */ +#if 1 + DrawTextBuffer(gi->x + border_x, gi->y + border_y, gi->textarea.value, + font_nr, gi->textarea.xsize, -1, gi->textarea.ysize, 0, + BLIT_ON_BACKGROUND, FALSE, FALSE, FALSE); +#else DrawTextToTextArea(gi->x + border_x, gi->y + border_y, - gi->textarea.value, font_nr, + gi->textarea.value, font_nr, gi->textarea.xsize, gi->textarea.xsize, gi->textarea.ysize, BLIT_ON_BACKGROUND); +#endif cursor_letter = gi->textarea.value[gi->textarea.cursor_position]; cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' '); @@ -552,15 +583,15 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) { gi->selectbox.open = FALSE; - /* redraw closed selectbox */ - DrawGadget(gi, FALSE, FALSE); - /* restore background under selectbox */ BlitBitmap(gfx.field_save_buffer, drawto, gi->selectbox.x, gi->selectbox.y, gi->selectbox.width, gi->selectbox.height, gi->selectbox.x, gi->selectbox.y); + /* redraw closed selectbox */ + DrawGadget(gi, FALSE, FALSE); + redraw_selectbox = TRUE; } } @@ -589,7 +620,7 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) xpos, ypos); /* middle part of gadget */ - for (i=0; ibitmap, drawto, gd->x, gd->y + gi->border.ysize, gi->width, design_body, @@ -636,7 +667,7 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) xpos, ypos); /* middle part of gadget */ - for (i=0; ibitmap, drawto, gd->x + gi->border.xsize, gd->y, design_body, gi->height, @@ -676,15 +707,46 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) gi->selectbox.x, gi->selectbox.y); } else + { +#if 1 + int x = gi->x; + int y = gi->y; + + redraw_mask |= (IN_GFX_FIELD_FULL(x, y) ? REDRAW_FIELD : + IN_GFX_DOOR_1(x, y) ? REDRAW_DOOR_1 : + IN_GFX_DOOR_2(x, y) ? REDRAW_DOOR_2 : + IN_GFX_DOOR_3(x, y) ? REDRAW_DOOR_3 : REDRAW_ALL); +#else redraw_mask |= (gi->x < gfx.sx + gfx.sxsize ? REDRAW_FIELD : gi->y < gfx.dy + gfx.dysize ? REDRAW_DOOR_1 : gi->y > gfx.vy ? REDRAW_DOOR_2 : REDRAW_DOOR_3); +#endif + } +} + +static int get_minimal_size_for_numeric_input(int minmax_value) +{ + int min_size = 1; /* value needs at least one digit */ + int i; + + /* add number of digits needed for absolute value */ + for (i = 10; i <= ABS(minmax_value); i *= 10) + min_size++; + + /* if min/max value is negative, add one digit for minus sign */ + if (minmax_value < 0) + min_size++; + + return min_size; } static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) { int tag = first_tag; + if (gi == NULL) + return; + while (tag != GDI_END) { switch(tag) @@ -699,7 +761,7 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) case GDI_INFO_TEXT: { - int max_textsize = MAX_INFO_TEXTSIZE - 1; + int max_textsize = MAX_INFO_TEXTSIZE; char *text = va_arg(ap, char *); if (text != NULL) @@ -728,56 +790,53 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) break; case GDI_TYPE: - gi->type = va_arg(ap, unsigned long); + gi->type = va_arg(ap, unsigned int); break; case GDI_STATE: - gi->state = va_arg(ap, unsigned long); + gi->state = va_arg(ap, unsigned int); break; case GDI_ACTIVE: - /* take care here: "boolean" is typedef'ed as "unsigned char", - which gets promoted to "int" */ gi->active = (boolean)va_arg(ap, int); break; case GDI_DIRECT_DRAW: - /* take care here: "boolean" is typedef'ed as "unsigned char", - which gets promoted to "int" */ gi->direct_draw = (boolean)va_arg(ap, int); break; case GDI_CHECKED: - /* take care here: "boolean" is typedef'ed as "unsigned char", - which gets promoted to "int" */ gi->checked = (boolean)va_arg(ap, int); break; case GDI_RADIO_NR: - gi->radio_nr = va_arg(ap, unsigned long); + gi->radio_nr = va_arg(ap, unsigned int); break; case GDI_NUMBER_VALUE: - gi->textinput.number_value = va_arg(ap, long); + gi->textinput.number_value = va_arg(ap, int); sprintf(gi->textinput.value, "%d", gi->textinput.number_value); + strcpy(gi->textinput.last_value, gi->textinput.value); gi->textinput.cursor_position = strlen(gi->textinput.value); break; case GDI_NUMBER_MIN: - gi->textinput.number_min = va_arg(ap, long); + gi->textinput.number_min = va_arg(ap, int); if (gi->textinput.number_value < gi->textinput.number_min) { gi->textinput.number_value = gi->textinput.number_min; sprintf(gi->textinput.value, "%d", gi->textinput.number_value); + strcpy(gi->textinput.last_value, gi->textinput.value); } break; case GDI_NUMBER_MAX: - gi->textinput.number_max = va_arg(ap, long); + gi->textinput.number_max = va_arg(ap, int); if (gi->textinput.number_value > gi->textinput.number_max) { gi->textinput.number_value = gi->textinput.number_max; sprintf(gi->textinput.value, "%d", gi->textinput.number_value); + strcpy(gi->textinput.last_value, gi->textinput.value); } break; @@ -786,29 +845,39 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) int max_textsize = MAX_GADGET_TEXTSIZE; if (gi->textinput.size) - max_textsize = MIN(gi->textinput.size, MAX_GADGET_TEXTSIZE - 1); + max_textsize = MIN(gi->textinput.size, MAX_GADGET_TEXTSIZE); strncpy(gi->textinput.value, va_arg(ap, char *), max_textsize); + strcpy(gi->textinput.last_value, gi->textinput.value); + gi->textinput.value[max_textsize] = '\0'; gi->textinput.cursor_position = strlen(gi->textinput.value); /* same tag also used for other gadget definitions */ strcpy(gi->textbutton.value, gi->textinput.value); strcpy(gi->textarea.value, gi->textinput.value); + strcpy(gi->textarea.last_value, gi->textinput.value); } break; case GDI_TEXT_SIZE: { int tag_value = va_arg(ap, int); - int max_textsize = MIN(tag_value, MAX_GADGET_TEXTSIZE - 1); + int max_textsize = MIN(tag_value, MAX_GADGET_TEXTSIZE); gi->textinput.size = max_textsize; gi->textinput.value[max_textsize] = '\0'; + strcpy(gi->textinput.last_value, gi->textinput.value); /* same tag also used for other gadget definitions */ - strcpy(gi->textbutton.value, gi->textinput.value); - gi->textbutton.size = gi->textinput.size; + + gi->textarea.size = max_textsize; + gi->textarea.value[max_textsize] = '\0'; + strcpy(gi->textarea.last_value, gi->textinput.value); + + gi->textbutton.size = max_textsize; + gi->textbutton.value[max_textsize] = '\0'; + gi->selectbox.size = gi->textinput.size; } break; @@ -890,7 +959,7 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) break; case GDI_EVENT_MASK: - gi->event_mask = va_arg(ap, unsigned long); + gi->event_mask = va_arg(ap, unsigned int); break; case GDI_AREA_SIZE: @@ -954,6 +1023,22 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) gi->scrollbar.item_position = va_arg(ap, int); break; + case GDI_WHEEL_AREA_X: + gi->wheelarea.x = va_arg(ap, int); + break; + + case GDI_WHEEL_AREA_Y: + gi->wheelarea.y = va_arg(ap, int); + break; + + case GDI_WHEEL_AREA_WIDTH: + gi->wheelarea.width = va_arg(ap, int); + break; + + case GDI_WHEEL_AREA_HEIGHT: + gi->wheelarea.height = va_arg(ap, int); + break; + case GDI_CALLBACK_INFO: gi->callback_info = va_arg(ap, gadget_function); break; @@ -985,6 +1070,19 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) int border_xsize = gi->border.xsize; int border_ysize = gi->border.ysize; + if (gi->type == GD_TYPE_TEXT_INPUT_NUMERIC) + { + int number_min = gi->textinput.number_min; + int number_max = gi->textinput.number_max; + int min_size_min = get_minimal_size_for_numeric_input(number_min); + int min_size_max = get_minimal_size_for_numeric_input(number_max); + int min_size = MAX(min_size_min, min_size_max); + + /* expand gadget text input size, if maximal value is too large */ + if (gi->textinput.size < min_size) + gi->textinput.size = min_size; + } + gi->width = 2 * border_xsize + (gi->textinput.size + 1) * font_width; gi->height = 2 * border_ysize + font_height; } @@ -1026,7 +1124,11 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) getFontCharSource(font_nr, FONT_ASCII_CURSOR, &src_bitmap, &src_x, &src_y); src_x += font_width / 2; src_y += font_height / 2; - gi->selectbox.inverse_color = GetPixel(src_bitmap, src_x, src_y); + + /* there may be esoteric cases with missing or too small font bitmap */ + if (src_bitmap != NULL && + src_x < src_bitmap->width && src_y < src_bitmap->height) + gi->selectbox.inverse_color = GetPixel(src_bitmap, src_x, src_y); /* always start with closed selectbox */ gi->selectbox.open = FALSE; @@ -1059,16 +1161,23 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) if (gi->type & GD_TYPE_SCROLLBAR) { struct GadgetScrollbar *gs = &gi->scrollbar; + int scrollbar_size_cmp; if (gi->width == 0 || gi->height == 0 || gs->items_max == 0 || gs->items_visible == 0) Error(ERR_EXIT, "scrollbar gadget incomplete (missing tags)"); /* calculate internal scrollbar values */ + gs->size_min = (gi->type == GD_TYPE_SCROLLBAR_VERTICAL ? + gi->width : gi->height); gs->size_max = (gi->type == GD_TYPE_SCROLLBAR_VERTICAL ? gi->height : gi->width); - gs->size = gs->size_max * gs->items_visible / gs->items_max; - gs->position = gs->size_max * gs->item_position / gs->items_max; + + scrollbar_size_cmp = gs->size_max * gs->items_visible / gs->items_max; + gs->size = MAX(scrollbar_size_cmp, gs->size_min); + gs->size_max_cmp = (gs->size_max - (gs->size - scrollbar_size_cmp)); + + gs->position = gs->size_max_cmp * gs->item_position / gs->items_max; gs->position_max = gs->size_max - gs->size; gs->correction = gs->size_max / gs->items_max / 2; @@ -1111,6 +1220,9 @@ void ModifyGadget(struct GadgetInfo *gi, int first_tag, ...) void RedrawGadget(struct GadgetInfo *gi) { + if (gi == NULL) + return; + if (gi->mapped) DrawGadget(gi, gi->state, gi->direct_draw); } @@ -1149,6 +1261,13 @@ void FreeGadget(struct GadgetInfo *gi) { struct GadgetInfo *gi_previous = gadget_list_first_entry; + if (gi == NULL) + return; + + /* prevent "last_info_gi" from pointing to memory that will be freed */ + if (last_info_gi == gi) + last_info_gi = NULL; + while (gi_previous != NULL && gi_previous->next != gi) gi_previous = gi_previous->next; @@ -1189,7 +1308,7 @@ static struct GadgetInfo *last_gi = NULL; static void MapGadgetExt(struct GadgetInfo *gi, boolean redraw) { - if (gi == NULL || gi->mapped) + if (gi == NULL || gi->mapped || GADGET_DEACTIVATED(gi)) return; gi->mapped = TRUE; @@ -1221,9 +1340,11 @@ void UnmapGadget(struct GadgetInfo *gi) #define MULTIMAP_PLAYFIELD (1 << 3) #define MULTIMAP_DOOR_1 (1 << 4) #define MULTIMAP_DOOR_2 (1 << 5) +#define MULTIMAP_DOOR_3 (1 << 6) #define MULTIMAP_ALL (MULTIMAP_PLAYFIELD | \ - MULTIMAP_DOOR_1 | \ - MULTIMAP_DOOR_2) + MULTIMAP_DOOR_1 | \ + MULTIMAP_DOOR_2 | \ + MULTIMAP_DOOR_3) static void MultiMapGadgets(int mode) { @@ -1233,6 +1354,16 @@ static void MultiMapGadgets(int mode) while (gi != NULL) { +#if 1 + int x = gi->x; + int y = gi->y; + + if ((mode & MULTIMAP_PLAYFIELD && IN_GFX_FIELD_FULL(x, y)) || + (mode & MULTIMAP_DOOR_1 && IN_GFX_DOOR_1(x, y)) || + (mode & MULTIMAP_DOOR_2 && IN_GFX_DOOR_2(x, y)) || + (mode & MULTIMAP_DOOR_3 && IN_GFX_DOOR_3(x, y)) || + (mode & MULTIMAP_ALL) == MULTIMAP_ALL) +#else if ((mode & MULTIMAP_PLAYFIELD && gi->x < gfx.sx + gfx.sxsize) || (mode & MULTIMAP_DOOR_1 && @@ -1240,6 +1371,7 @@ static void MultiMapGadgets(int mode) (mode & MULTIMAP_DOOR_2 && gi->x >= gfx.dx && gi->y > gfx.dy + gfx.dysize) || (mode & MULTIMAP_ALL) == MULTIMAP_ALL) +#endif { if (mode & MULTIMAP_UNMAP) { @@ -1267,21 +1399,26 @@ void RemapAllGadgets() MultiMapGadgets(MULTIMAP_ALL | MULTIMAP_REMAP); } -static boolean anyTextInputGadgetActive() +boolean anyTextInputGadgetActive() { return (last_gi && (last_gi->type & GD_TYPE_TEXT_INPUT) && last_gi->mapped); } -static boolean anyTextAreaGadgetActive() +boolean anyTextAreaGadgetActive() { return (last_gi && (last_gi->type & GD_TYPE_TEXT_AREA) && last_gi->mapped); } -static boolean anySelectboxGadgetActive() +boolean anySelectboxGadgetActive() { return (last_gi && (last_gi->type & GD_TYPE_SELECTBOX) && last_gi->mapped); } +boolean anyScrollbarGadgetActive() +{ + return (last_gi && (last_gi->type & GD_TYPE_SCROLLBAR) && last_gi->mapped); +} + boolean anyTextGadgetActive() { return (anyTextInputGadgetActive() || @@ -1289,8 +1426,27 @@ boolean anyTextGadgetActive() anySelectboxGadgetActive()); } +static boolean insideSelectboxLine(struct GadgetInfo *gi, int mx, int my) +{ + return (gi != NULL && + gi->type & GD_TYPE_SELECTBOX && + mx >= gi->x && mx < gi->x + gi->width && + my >= gi->y && my < gi->y + gi->height); +} + +static boolean insideSelectboxArea(struct GadgetInfo *gi, int mx, int my) +{ + return (gi != NULL && + gi->type & GD_TYPE_SELECTBOX && + mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width && + my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height); +} + void ClickOnGadget(struct GadgetInfo *gi, int button) { + if (!gi->mapped) + return; + /* simulate releasing mouse button over last gadget, if still pressed */ if (button_status) HandleGadgets(-1, -1, 0); @@ -1302,32 +1458,45 @@ void ClickOnGadget(struct GadgetInfo *gi, int button) HandleGadgets(gi->x, gi->y, 0); } -void HandleGadgets(int mx, int my, int button) +boolean HandleGadgets(int mx, int my, int button) { - static struct GadgetInfo *last_info_gi = NULL; - static unsigned long pressed_delay = 0; + static unsigned int pressed_delay = 0; + static unsigned int pressed_delay_value = GADGET_FRAME_DELAY; static int last_button = 0; static int last_mx = 0, last_my = 0; + static int pressed_mx = 0, pressed_my = 0; + static boolean keep_selectbox_open = FALSE; + static boolean gadget_stopped = FALSE; int scrollbar_mouse_pos = 0; struct GadgetInfo *new_gi, *gi; boolean press_event; boolean release_event; boolean mouse_moving; + boolean mouse_inside_select_line; + boolean mouse_inside_select_area; + boolean mouse_released_where_pressed; boolean gadget_pressed; boolean gadget_pressed_repeated; + boolean gadget_pressed_off_borders; + boolean gadget_pressed_inside_select_line; + boolean gadget_pressed_delay_reached; boolean gadget_moving; boolean gadget_moving_inside; boolean gadget_moving_off_borders; + boolean gadget_draggable; + boolean gadget_dragging; boolean gadget_released; boolean gadget_released_inside; +#if 0 boolean gadget_released_inside_select_line; +#endif boolean gadget_released_inside_select_area; boolean gadget_released_off_borders; boolean changed_position = FALSE; /* check if there are any gadgets defined */ if (gadget_list_first_entry == NULL) - return; + return FALSE; /* simulated release of mouse button over last gadget */ if (mx == -1 && my == -1 && button == 0) @@ -1337,10 +1506,10 @@ void HandleGadgets(int mx, int my, int button) } /* check which gadget is under the mouse pointer */ - new_gi = getGadgetInfoFromMousePosition(mx, my); + new_gi = getGadgetInfoFromMousePosition(mx, my, button); /* check if button state has changed since last invocation */ - press_event = (button != 0 && last_button == 0); + press_event = (button != 0 && last_button == 0); release_event = (button == 0 && last_button != 0); last_button = button; @@ -1349,20 +1518,59 @@ void HandleGadgets(int mx, int my, int button) last_mx = mx; last_my = my; + if (press_event && new_gi != last_gi) + { + pressed_mx = mx; + pressed_my = my; + } + + mouse_released_where_pressed = + (release_event && mx == pressed_mx && my == pressed_my); + + mouse_inside_select_line = insideSelectboxLine(new_gi, mx, my); + mouse_inside_select_area = insideSelectboxArea(new_gi, mx, my); + + gadget_pressed_off_borders = (press_event && new_gi != last_gi); + + gadget_pressed_inside_select_line = + (press_event && new_gi != NULL && + new_gi->type & GD_TYPE_SELECTBOX && new_gi->selectbox.open && + insideSelectboxLine(new_gi, mx, my)); + /* if mouse button pressed outside text or selectbox gadget, deactivate it */ if (anyTextGadgetActive() && - button != 0 && !motion_status && new_gi != last_gi) + (gadget_pressed_off_borders || + (gadget_pressed_inside_select_line && !mouse_inside_select_area))) { - CheckRangeOfNumericInputGadget(last_gi); /* in case of numeric gadget */ + struct GadgetInfo *gi = last_gi; + boolean gadget_changed = ((gi->event_mask & GD_EVENT_TEXT_LEAVING) != 0); - DrawGadget(last_gi, DG_UNPRESSED, last_gi->direct_draw); + /* check if text gadget has changed its value */ + if (gi->type & GD_TYPE_TEXT_INPUT) + { + CheckRangeOfNumericInputGadget(gi); - last_gi->event.type = GD_EVENT_TEXT_LEAVING; + if (!strEqual(gi->textinput.last_value, gi->textinput.value)) + strcpy(gi->textinput.last_value, gi->textinput.value); + else + gadget_changed = FALSE; + } - if (last_gi->event_mask & GD_EVENT_TEXT_LEAVING) - last_gi->callback_action(last_gi); + /* selectbox does not change its value when closed by clicking outside */ + if (gi->type & GD_TYPE_SELECTBOX) + gadget_changed = FALSE; + + DrawGadget(gi, DG_UNPRESSED, gi->direct_draw); + + gi->event.type = GD_EVENT_TEXT_LEAVING; + + if (gadget_changed && !(gi->type & GD_TYPE_SELECTBOX)) + gi->callback_action(gi); last_gi = NULL; + + if (gadget_pressed_inside_select_line) + new_gi = NULL; } gadget_pressed = @@ -1370,6 +1578,9 @@ void HandleGadgets(int mx, int my, int button) gadget_pressed_repeated = (button != 0 && last_gi != NULL && new_gi == last_gi); + gadget_pressed_delay_reached = + DelayReached(&pressed_delay, pressed_delay_value); + gadget_released = (release_event && last_gi != NULL); gadget_released_inside = (gadget_released && new_gi == last_gi); gadget_released_off_borders = (gadget_released && new_gi != last_gi); @@ -1381,21 +1592,23 @@ void HandleGadgets(int mx, int my, int button) /* when handling selectbox, set additional state values */ if (gadget_released_inside && (last_gi->type & GD_TYPE_SELECTBOX)) { - struct GadgetInfo *gi = last_gi; - - gadget_released_inside_select_line = - (mx >= gi->x && mx < gi->x + gi->width && - my >= gi->y && my < gi->y + gi->height); - gadget_released_inside_select_area = - (mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width && - my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height); +#if 0 + gadget_released_inside_select_line = insideSelectboxLine(last_gi, mx, my); +#endif + gadget_released_inside_select_area = insideSelectboxArea(last_gi, mx, my); } else { +#if 0 gadget_released_inside_select_line = FALSE; +#endif gadget_released_inside_select_area = FALSE; } + /* setting state for handling over-large selectbox */ + if (keep_selectbox_open && (press_event || !mouse_inside_select_line)) + keep_selectbox_open = FALSE; + /* if new gadget pressed, store this gadget */ if (gadget_pressed) last_gi = new_gi; @@ -1411,12 +1624,17 @@ void HandleGadgets(int mx, int my, int button) /* if mouse button released, no gadget needs to be handled anymore */ if (gadget_released) { - if (last_gi->type & GD_TYPE_SELECTBOX && - (gadget_released_inside_select_line || - gadget_released_off_borders)) /* selectbox stays open */ + if (gi->type & GD_TYPE_SELECTBOX && + (keep_selectbox_open || + mouse_released_where_pressed || + !gadget_released_inside_select_area)) /* selectbox stays open */ + { gi->selectbox.stay_open = TRUE; - else if (!(last_gi->type & GD_TYPE_TEXT_INPUT || - last_gi->type & GD_TYPE_TEXT_AREA)) /* text input stays open */ + pressed_mx = 0; + pressed_my = 0; + } + else if (!(gi->type & GD_TYPE_TEXT_INPUT || + gi->type & GD_TYPE_TEXT_AREA)) /* text input stays open */ last_gi = NULL; } @@ -1424,6 +1642,14 @@ void HandleGadgets(int mx, int my, int button) if (button == 0 && !release_event) gi = new_gi; + /* if new gadget or if no gadget was pressed, release stopped processing */ + if (gadget_pressed || new_gi == NULL) + gadget_stopped = FALSE; + + /* if gadget was stopped while being handled, stop gadget processing here */ + if (gadget_stopped) + return TRUE; + if (gi != NULL) { int last_x = gi->event.x; @@ -1470,7 +1696,8 @@ void HandleGadgets(int mx, int my, int button) if (gi->textarea.cursor_position != old_cursor_position) DrawGadget(gi, DG_PRESSED, gi->direct_draw); } - else if (gi->type & GD_TYPE_SELECTBOX) + else if (gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open && + !keep_selectbox_open) { int old_index = gi->selectbox.current_index; @@ -1507,69 +1734,72 @@ void HandleGadgets(int mx, int my, int button) last_info_gi = new_gi; } - if (gadget_pressed) - { - if (gi->type == GD_TYPE_CHECK_BUTTON) - { - gi->checked = !gi->checked; - } - else if (gi->type == GD_TYPE_RADIO_BUTTON) - { - struct GadgetInfo *rgi = gadget_list_first_entry; + gadget_draggable = (gi && gi->type & GD_TYPE_SCROLLBAR); - while (rgi) - { - if (rgi->mapped && - rgi->type == GD_TYPE_RADIO_BUTTON && - rgi->radio_nr == gi->radio_nr && - rgi != gi) - { - rgi->checked = FALSE; - DrawGadget(rgi, DG_UNPRESSED, rgi->direct_draw); - } + /* reset drag position for newly pressed scrollbar to "not dragging" */ + if (gadget_pressed && gadget_draggable) + gi->scrollbar.drag_position = -1; - rgi = rgi->next; - } + gadget_dragging = (gadget_draggable && gi->scrollbar.drag_position != -1); - gi->checked = TRUE; + /* clicking next to a scrollbar to move it is not considered "moving" */ + if (gadget_draggable && !gadget_dragging) + gadget_moving = FALSE; + + /* when leaving scrollbar area when jump-scrolling, stop gadget processing */ + if (gadget_draggable && !gadget_dragging && gadget_moving_off_borders) + gadget_stopped = TRUE; + + if ((gadget_pressed) || + (gadget_pressed_repeated && gadget_pressed_delay_reached)) + { + if (gadget_pressed) /* gadget pressed the first time */ + { + /* initialize delay counter */ + DelayReached(&pressed_delay, 0); + + /* start gadget delay with longer delay after first click on gadget */ + pressed_delay_value = GADGET_FRAME_DELAY_FIRST; } - else if (gi->type & GD_TYPE_SCROLLBAR) + else /* gadget hold pressed for some time */ { - int mpos, gpos; + /* after first repeated gadget click, continue with shorter delay value */ + pressed_delay_value = GADGET_FRAME_DELAY; + } - if (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL) - { - mpos = mx; - gpos = gi->x; - } - else - { - mpos = my; - gpos = gi->y; - } + if (gi->type & GD_TYPE_SCROLLBAR && !gadget_dragging) + { + int mpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx : my); + int gpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? gi->x : gi->y); + int slider_start = gpos + gi->scrollbar.position; + int slider_end = gpos + gi->scrollbar.position + gi->scrollbar.size - 1; + boolean inside_slider = (mpos >= slider_start && mpos <= slider_end); - if (mpos >= gpos + gi->scrollbar.position && - mpos < gpos + gi->scrollbar.position + gi->scrollbar.size) - { - /* drag scrollbar */ - gi->scrollbar.drag_position = - scrollbar_mouse_pos - gi->scrollbar.position; - } - else + if (IS_WHEEL_BUTTON(button) || !inside_slider) { /* click scrollbar one scrollbar length up/left or down/right */ struct GadgetScrollbar *gs = &gi->scrollbar; int old_item_position = gs->item_position; + int item_steps = gs->items_visible - 1; + int item_direction = (mpos < gpos + gi->scrollbar.position ? -1 : +1); + + if (IS_WHEEL_BUTTON(button)) + { + boolean scroll_single_step = ((GetKeyModState() & KMOD_Alt) != 0); + + item_steps = (scroll_single_step ? 1 : DEFAULT_WHEEL_STEPS); + item_direction = (button == MB_WHEEL_UP || + button == MB_WHEEL_LEFT ? -1 : +1); + } changed_position = FALSE; - gs->item_position += - gs->items_visible * (mpos < gpos + gi->scrollbar.position ? -1 : +1); + gs->item_position += item_steps * item_direction; if (gs->item_position < 0) gs->item_position = 0; - if (gs->item_position > gs->items_max - gs->items_visible) + else if (gs->item_position > gs->items_max - gs->items_visible) gs->item_position = gs->items_max - gs->items_visible; if (old_item_position != gs->item_position) @@ -1588,12 +1818,70 @@ void HandleGadgets(int mx, int my, int button) if (gi->event_mask & GD_EVENT_MOVING && changed_position) gi->callback_action(gi); - /* don't handle this scrollbar anymore while mouse button pressed */ - last_gi = NULL; + return TRUE; + } + else + { + /* don't handle this scrollbar anymore when mouse position reached */ + if (gadget_pressed_repeated) + { + gadget_stopped = TRUE; + + return TRUE; + } + } + } + } + + if (gadget_pressed) + { + PlayGadgetSoundActivating(); + + if (gi->type == GD_TYPE_CHECK_BUTTON) + { + gi->checked = !gi->checked; + } + else if (gi->type == GD_TYPE_RADIO_BUTTON) + { + struct GadgetInfo *rgi = gadget_list_first_entry; + + while (rgi) + { + if (rgi->mapped && + rgi->type == GD_TYPE_RADIO_BUTTON && + rgi->radio_nr == gi->radio_nr && + rgi != gi) + { + rgi->checked = FALSE; + DrawGadget(rgi, DG_UNPRESSED, rgi->direct_draw); + } - return; + rgi = rgi->next; + } + + gi->checked = TRUE; + } + else if (gi->type & GD_TYPE_SCROLLBAR) + { + int mpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx : my); + int gpos = (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? gi->x : gi->y); + int slider_start = gpos + gi->scrollbar.position; + int slider_end = gpos + gi->scrollbar.position + gi->scrollbar.size - 1; + boolean inside_slider = (mpos >= slider_start && mpos <= slider_end); + + if (!IS_WHEEL_BUTTON(button) && inside_slider) + { + /* start dragging scrollbar */ + gi->scrollbar.drag_position = + scrollbar_mouse_pos - gi->scrollbar.position; } } + else if (gi->type & GD_TYPE_SELECTBOX) + { + /* keep selectbox open in case of over-large selectbox */ + keep_selectbox_open = (mouse_inside_select_line && + mouse_inside_select_area); + } DrawGadget(gi, DG_PRESSED, gi->direct_draw); @@ -1602,9 +1890,6 @@ void HandleGadgets(int mx, int my, int button) gi->event.button = button; gi->event.off_borders = FALSE; - /* initialize delay counter */ - DelayReached(&pressed_delay, 0); - if (gi->event_mask & GD_EVENT_PRESSED) gi->callback_action(gi); } @@ -1613,8 +1898,7 @@ void HandleGadgets(int mx, int my, int button) { gi->event.type = GD_EVENT_PRESSED; - if (gi->event_mask & GD_EVENT_REPEATED && - DelayReached(&pressed_delay, GADGET_FRAME_DELAY)) + if (gi->event_mask & GD_EVENT_REPEATED && gadget_pressed_delay_reached) gi->callback_action(gi); } @@ -1627,7 +1911,7 @@ void HandleGadgets(int mx, int my, int button) else if (gadget_moving_off_borders && gi->state == GD_BUTTON_PRESSED) DrawGadget(gi, DG_UNPRESSED, gi->direct_draw); } - else if (gi->type & GD_TYPE_SELECTBOX) + else if (gi->type & GD_TYPE_SELECTBOX && !keep_selectbox_open) { int old_index = gi->selectbox.current_index; @@ -1651,13 +1935,22 @@ void HandleGadgets(int mx, int my, int button) gs->position = scrollbar_mouse_pos - gs->drag_position; - if (gs->position < 0) + /* make sure to always precisely reach end positions when dragging */ + if (gs->position <= 0) + { gs->position = 0; - if (gs->position > gs->position_max) + gs->item_position = 0; + } + else if (gs->position >= gs->position_max) + { gs->position = gs->position_max; - - gs->item_position = - gs->items_max * (gs->position + gs->correction) / gs->size_max; + gs->item_position = gs->items_max - gs->items_visible; + } + else + { + gs->item_position = + gs->items_max * (gs->position + gs->correction) / gs->size_max_cmp; + } if (gs->item_position < 0) gs->item_position = 0; @@ -1673,7 +1966,7 @@ void HandleGadgets(int mx, int my, int button) DrawGadget(gi, DG_PRESSED, gi->direct_draw); } - gi->state = (gadget_moving_inside || gi->type & GD_TYPE_SCROLLBAR ? + gi->state = (gadget_moving_inside || gadget_draggable ? GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED); gi->event.type = GD_EVENT_MOVING; gi->event.off_borders = gadget_moving_off_borders; @@ -1686,14 +1979,21 @@ void HandleGadgets(int mx, int my, int button) if (gadget_released_inside) { boolean deactivate_gadget = TRUE; + boolean gadget_changed = TRUE; if (gi->type & GD_TYPE_SELECTBOX) { - if (gadget_released_inside_select_line || - gadget_released_off_borders) /* selectbox stays open */ + if (keep_selectbox_open || + mouse_released_where_pressed || + !gadget_released_inside_select_area) /* selectbox stays open */ + { deactivate_gadget = FALSE; - else + gadget_changed = FALSE; + } + else if (gi->selectbox.index != gi->selectbox.current_index) gi->selectbox.index = gi->selectbox.current_index; + else + gadget_changed = FALSE; } if (deactivate_gadget && @@ -1704,8 +2004,10 @@ void HandleGadgets(int mx, int my, int button) gi->state = GD_BUTTON_UNPRESSED; gi->event.type = GD_EVENT_RELEASED; - if ((gi->event_mask & GD_EVENT_RELEASED) && deactivate_gadget) + if ((gi->event_mask & GD_EVENT_RELEASED) && gadget_changed) + { gi->callback_action(gi); + } } if (gadget_released_off_borders) @@ -1713,6 +2015,7 @@ void HandleGadgets(int mx, int my, int button) if (gi->type & GD_TYPE_SCROLLBAR) DrawGadget(gi, DG_UNPRESSED, gi->direct_draw); + gi->state = GD_BUTTON_UNPRESSED; gi->event.type = GD_EVENT_RELEASED; if (gi->event_mask & GD_EVENT_RELEASED && @@ -1723,14 +2026,17 @@ void HandleGadgets(int mx, int my, int button) /* handle gadgets unmapped/mapped between pressing and releasing */ if (release_event && !gadget_released && new_gi) new_gi->state = GD_BUTTON_UNPRESSED; + + return (gadget_pressed || gadget_pressed_repeated || + gadget_released || gadget_moving); } static void insertCharIntoTextArea(struct GadgetInfo *gi, char c) { - char text[MAX_GADGET_TEXTSIZE]; + char text[MAX_GADGET_TEXTSIZE + 1]; int cursor_position = gi->textarea.cursor_position; - if (strlen(gi->textarea.value) == MAX_GADGET_TEXTSIZE) /* no space left */ + if (strlen(gi->textarea.value) >= MAX_GADGET_TEXTSIZE) /* no space left */ return; strcpy(text, gi->textarea.value); @@ -1740,7 +2046,7 @@ static void insertCharIntoTextArea(struct GadgetInfo *gi, char c) setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1); } -void HandleGadgetsKeyInput(Key key) +boolean HandleGadgetsKeyInput(Key key) { struct GadgetInfo *gi = last_gi; @@ -1748,14 +2054,28 @@ void HandleGadgetsKeyInput(Key key) !(gi->type & GD_TYPE_TEXT_INPUT || gi->type & GD_TYPE_TEXT_AREA || gi->type & GD_TYPE_SELECTBOX)) - return; + return FALSE; if (key == KSYM_Return) /* valid for both text input and selectbox */ { + boolean gadget_changed = ((gi->event_mask & GD_EVENT_TEXT_RETURN) != 0); + if (gi->type & GD_TYPE_TEXT_INPUT) + { CheckRangeOfNumericInputGadget(gi); + + if (!strEqual(gi->textinput.last_value, gi->textinput.value)) + strcpy(gi->textinput.last_value, gi->textinput.value); + else + gadget_changed = FALSE; + } else if (gi->type & GD_TYPE_SELECTBOX) - gi->selectbox.index = gi->selectbox.current_index; + { + if (gi->selectbox.index != gi->selectbox.current_index) + gi->selectbox.index = gi->selectbox.current_index; + else + gadget_changed = FALSE; + } if (gi->type & GD_TYPE_TEXT_AREA) { @@ -1772,12 +2092,12 @@ void HandleGadgetsKeyInput(Key key) last_gi = NULL; } - if (gi->event_mask & GD_EVENT_TEXT_RETURN) + if (gadget_changed) gi->callback_action(gi); } else if (gi->type & GD_TYPE_TEXT_INPUT) /* only valid for text input */ { - char text[MAX_GADGET_TEXTSIZE]; + char text[MAX_GADGET_TEXTSIZE + 1]; int text_length = strlen(gi->textinput.value); int cursor_pos = gi->textinput.cursor_position; char letter = getCharFromKey(key); @@ -1824,7 +2144,7 @@ void HandleGadgetsKeyInput(Key key) } else if (gi->type & GD_TYPE_TEXT_AREA) /* only valid for text area */ { - char text[MAX_GADGET_TEXTSIZE]; + char text[MAX_GADGET_TEXTSIZE + 1]; int text_length = strlen(gi->textarea.value); int area_ysize = gi->textarea.ysize; int cursor_x_pref = gi->textarea.cursor_x_preferred; @@ -1900,4 +2220,6 @@ void HandleGadgetsKeyInput(Key key) DrawGadget(gi, DG_PRESSED, gi->direct_draw); } } + + return TRUE; }