X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fgadgets.c;h=55174eef8b749c09812f870cd7d2059d33f11205;hb=274935f49f49068998ad7b4ed9d93892aedfc6aa;hp=7b92820009b1138c5d1f8a2dd40a414bdf6e46a1;hpb=caf3da0a0e3af75eb8d10f83e5105581402b387e;p=rocksndiamonds.git diff --git a/src/libgame/gadgets.c b/src/libgame/gadgets.c index 7b928200..55174eef 100644 --- a/src/libgame/gadgets.c +++ b/src/libgame/gadgets.c @@ -22,6 +22,7 @@ /* values for DrawGadget() */ #define DG_UNPRESSED 0 #define DG_PRESSED 1 + #define DG_BUFFERED 0 #define DG_DIRECT 1 @@ -104,6 +105,170 @@ static struct GadgetInfo *getGadgetInfoFromMousePosition(int mx, int my) return NULL; } +#if 1 + +static void setTextAreaCursorExt(struct GadgetInfo *gi, boolean set_cursor_pos) +{ + char *text = gi->textarea.value; + int area_xsize = gi->textarea.xsize; + int area_ysize = gi->textarea.ysize; + int cursor_position = gi->textarea.cursor_position; + int cursor_x = gi->textarea.cursor_x; + int cursor_y = gi->textarea.cursor_y; + int pos = 0; + int x = 0; + int y = 0; + + while (*text) + { + if (set_cursor_pos) /* x/y => position */ + { + if (y == cursor_y && (x == cursor_x || (x < cursor_x && *text == '\n'))) + break; + } + else /* position => x/y */ + { + if (pos == cursor_position) + break; + } + + if (x + 1 >= area_xsize || *text == '\n') + { + if (y + 1 >= area_ysize) + break; + + x = 0; + y++; + } + else + x++; + + text++; + pos++; + } + + if (y >= area_ysize) + { + x = area_xsize - 1; + } + +#if 0 + printf("::: %d, %d [%d]\n", cursor_x, cursor_y, cursor_position); +#endif + + gi->textarea.cursor_x = x; + gi->textarea.cursor_y = y; + gi->textarea.cursor_x_preferred = x; + gi->textarea.cursor_position = pos; +} + +static void setTextAreaCursorXY(struct GadgetInfo *gi, int x, int y) +{ + gi->textarea.cursor_x = x; + gi->textarea.cursor_y = y; + + setTextAreaCursorExt(gi, TRUE); +} + +static void setTextAreaCursorPosition(struct GadgetInfo *gi, int pos) +{ + gi->textarea.cursor_position = pos; + + setTextAreaCursorExt(gi, FALSE); +} + +#else + +static void setTextAreaCursorPosition(struct GadgetInfo *gi, int x, int y) +{ + char *text = gi->textarea.value; + int area_xsize = gi->textarea.xsize; + int area_ysize = gi->textarea.ysize; + int cursor_x = 0; + int cursor_y = 0; + int cursor_position = 0; + + while (*text && cursor_y < area_ysize) + { + char buffer[MAX_OUTPUT_LINESIZE + 1]; + int i; + + for (i=0; i < area_xsize && *text && *text != '\n'; i++) + buffer[i] = *text++; + buffer[i] = '\0'; + +#if 1 + if (i == 0 && *text == '\n') + { + text++; + cursor_position++; + } +#endif + + if (x == -1 && y == -1) /* get x/y from cursor position */ + { + if (cursor_position + i >= gi->textarea.cursor_position) + { +#if 0 + printf("::: cursor: %d + %d >= %d\n", cursor_position, i, + gi->textarea.cursor_position); +#endif + + cursor_x = gi->textarea.cursor_position - cursor_position; + cursor_position = gi->textarea.cursor_position; + + break; + } + } + + if (cursor_y == y || !*text) /* correct y position found */ + { + cursor_x = MIN(i, x); + cursor_position += cursor_x; + + break; + } + else + cursor_position += i; + +#if 0 + if (*text == '\n') + { + text++; + cursor_position++; + + if (i == area_xsize) + cursor_y++; + } +#endif + + cursor_y++; + } + + if (cursor_x >= area_xsize) + { + cursor_x = 0; + cursor_y++; + } + + if (cursor_y >= area_ysize) + { + cursor_x = area_xsize - 1; + cursor_y = area_ysize - 1; + } + +#if 0 + printf("::: %d, %d [%d]\n", cursor_x, cursor_y, cursor_position); +#endif + + gi->textarea.cursor_x = cursor_x; + gi->textarea.cursor_y = cursor_y; + gi->textarea.cursor_x_preferred = cursor_x; + gi->textarea.cursor_position = cursor_position; +} + +#endif + static void default_callback_info(void *ptr) { return; @@ -172,8 +337,8 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) } break; - case GD_TYPE_TEXTINPUT_ALPHANUMERIC: - case GD_TYPE_TEXTINPUT_NUMERIC: + case GD_TYPE_TEXT_INPUT_ALPHANUMERIC: + case GD_TYPE_TEXT_INPUT_NUMERIC: { int i; char cursor_letter; @@ -189,7 +354,7 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) border_x, gi->height, gi->x, gi->y); /* middle part of gadget */ - for (i=0; i < gi->text.size + 1; i++) + for (i=0; i < gi->textinput.size + 1; i++) BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y, font_width, gi->height, gi->x + border_x + i * font_width, gi->y); @@ -201,7 +366,7 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) gi->x + gi->width - border_x, gi->y); /* set text value */ - strcpy(text, gi->text.value); + strcpy(text, gi->textinput.value); strcat(text, " "); /* gadget text value */ @@ -209,19 +374,123 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) gi->x + border_x, gi->y + border_y, text, font_nr, BLIT_MASKED); - cursor_letter = gi->text.value[gi->text.cursor_position]; + cursor_letter = gi->textinput.value[gi->textinput.cursor_position]; cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' '); cursor_string[1] = '\0'; +#if 0 + if (pressed) + printf("::: PRESSED!\n"); +#endif + /* draw cursor, if active */ if (pressed) DrawTextExt(drawto, - gi->x + border_x + gi->text.cursor_position * font_width, + gi->x + border_x + + gi->textinput.cursor_position * font_width, gi->y + border_y, cursor_string, font_nr, BLIT_INVERSE); } break; + case GD_TYPE_TEXT_AREA: + { + int i; + char cursor_letter; + char cursor_string[2]; + int font_nr = (pressed ? gi->font_active : gi->font); + int font_width = getFontWidth(font_nr); + int font_height = getFontHeight(font_nr); + int border_x = gi->border.xsize; + int border_y = gi->border.ysize; + int gd_height = 2 * border_y + font_height; + + /* top left part of gadget border */ + BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y, + border_x, border_y, gi->x, gi->y); + + /* top middle part of gadget border */ + for (i=0; i < gi->textarea.xsize; i++) + BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y, + font_width, border_y, + gi->x + border_x + i * font_width, gi->y); + + /* top right part of gadget border */ + BlitBitmapOnBackground(gd->bitmap, drawto, + gd->x + gi->border.width - border_x, gd->y, + border_x, border_y, + gi->x + gi->width - border_x, gi->y); + + /* left and right part of gadget border for each row */ + for (i=0; i < gi->textarea.ysize; i++) + { + BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y, + border_x, font_height, + gi->x, gi->y + border_y + i * font_height); + BlitBitmapOnBackground(gd->bitmap, drawto, + gd->x + gi->border.width - border_x, + gd->y + border_y, + border_x, font_height, + gi->x + gi->width - border_x, + gi->y + border_y + i * font_height); + } + + /* bottom left part of gadget border */ + BlitBitmapOnBackground(gd->bitmap, drawto, + gd->x, gd->y + gd_height - border_y, + border_x, border_y, + gi->x, gi->y + gi->height - border_y); + + /* bottom middle part of gadget border */ + for (i=0; i < gi->textarea.xsize; i++) + BlitBitmapOnBackground(gd->bitmap, drawto, + gd->x + border_x, + gd->y + gd_height - border_y, + font_width, border_y, + gi->x + border_x + i * font_width, + gi->y + gi->height - border_y); + + /* bottom right part of gadget border */ + BlitBitmapOnBackground(gd->bitmap, drawto, + gd->x + gi->border.width - border_x, + gd->y + gd_height - border_y, + border_x, border_y, + gi->x + gi->width - border_x, + gi->y + gi->height - border_y); + + ClearRectangleOnBackground(drawto, + gi->x + border_x, + gi->y + border_y, + gi->width - 2 * border_x, + gi->height - 2 * border_y); + + /* gadget text value */ + DrawTextToTextArea(gi->x + border_x, gi->y + border_y, + gi->textarea.value, font_nr, + gi->textarea.xsize, gi->textarea.ysize); + + cursor_letter = gi->textarea.value[gi->textarea.cursor_position]; + cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' '); + cursor_string[1] = '\0'; + +#if 0 + printf("::: '%s' [%d, %d] [%d] [%d -> '%s']\n", + gi->textarea.value, + gi->textarea.cursor_x, gi->textarea.cursor_y, + gi->textarea.cursor_position, + pressed, cursor_string); +#endif + + /* draw cursor, if active */ + if (pressed) + DrawTextExt(drawto, + gi->x + border_x + gi->textarea.cursor_x * font_width, + gi->y + border_y + gi->textarea.cursor_y * font_height, + cursor_string, + font_nr, BLIT_INVERSE); + } + break; + case GD_TYPE_SELECTBOX: { int i; @@ -590,6 +859,12 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) 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" */ @@ -601,26 +876,26 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) break; case GDI_NUMBER_VALUE: - gi->text.number_value = va_arg(ap, long); - sprintf(gi->text.value, "%d", gi->text.number_value); - gi->text.cursor_position = strlen(gi->text.value); + gi->textinput.number_value = va_arg(ap, long); + sprintf(gi->textinput.value, "%d", gi->textinput.number_value); + gi->textinput.cursor_position = strlen(gi->textinput.value); break; case GDI_NUMBER_MIN: - gi->text.number_min = va_arg(ap, long); - if (gi->text.number_value < gi->text.number_min) + gi->textinput.number_min = va_arg(ap, long); + if (gi->textinput.number_value < gi->textinput.number_min) { - gi->text.number_value = gi->text.number_min; - sprintf(gi->text.value, "%d", gi->text.number_value); + gi->textinput.number_value = gi->textinput.number_min; + sprintf(gi->textinput.value, "%d", gi->textinput.number_value); } break; case GDI_NUMBER_MAX: - gi->text.number_max = va_arg(ap, long); - if (gi->text.number_value > gi->text.number_max) + gi->textinput.number_max = va_arg(ap, long); + if (gi->textinput.number_value > gi->textinput.number_max) { - gi->text.number_value = gi->text.number_max; - sprintf(gi->text.value, "%d", gi->text.number_value); + gi->textinput.number_value = gi->textinput.number_max; + sprintf(gi->textinput.value, "%d", gi->textinput.number_value); } break; @@ -628,15 +903,16 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) { int max_textsize = MAX_GADGET_TEXTSIZE; - if (gi->text.size) - max_textsize = MIN(gi->text.size, MAX_GADGET_TEXTSIZE - 1); + if (gi->textinput.size) + max_textsize = MIN(gi->textinput.size, MAX_GADGET_TEXTSIZE - 1); - strncpy(gi->text.value, va_arg(ap, char *), max_textsize); - gi->text.value[max_textsize] = '\0'; - gi->text.cursor_position = strlen(gi->text.value); + strncpy(gi->textinput.value, va_arg(ap, char *), max_textsize); + gi->textinput.value[max_textsize] = '\0'; + gi->textinput.cursor_position = strlen(gi->textinput.value); - /* same tag also used for textbutton definition */ - strcpy(gi->textbutton.value, gi->text.value); + /* same tag also used for other gadget definitions */ + strcpy(gi->textbutton.value, gi->textinput.value); + strcpy(gi->textarea.value, gi->textinput.value); } break; @@ -645,13 +921,13 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) int tag_value = va_arg(ap, int); int max_textsize = MIN(tag_value, MAX_GADGET_TEXTSIZE - 1); - gi->text.size = max_textsize; - gi->text.value[max_textsize] = '\0'; + gi->textinput.size = max_textsize; + gi->textinput.value[max_textsize] = '\0'; - /* same tag also used for textbutton and selectbox definition */ - strcpy(gi->textbutton.value, gi->text.value); - gi->textbutton.size = gi->text.size; - gi->selectbox.size = gi->text.size; + /* same tag also used for other gadget definitions */ + strcpy(gi->textbutton.value, gi->textinput.value); + gi->textbutton.size = gi->textinput.size; + gi->selectbox.size = gi->textinput.size; } break; @@ -752,6 +1028,17 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) gi->drawing.item_xsize = gi->width / gi->drawing.area_xsize; gi->drawing.item_ysize = gi->height / gi->drawing.area_ysize; } + + /* same tag also used for other gadget definitions */ + gi->textarea.xsize = gi->drawing.area_xsize; + gi->textarea.ysize = gi->drawing.area_ysize; + + if (gi->type & GD_TYPE_TEXT_AREA) /* force recalculation */ + { + gi->width = 0; + gi->height = 0; + } + break; case GDI_ITEM_SIZE: @@ -808,7 +1095,7 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) /* adjust gadget values in relation to other gadget values */ - if (gi->type & GD_TYPE_TEXTINPUT) + if (gi->type & GD_TYPE_TEXT_INPUT) { int font_nr = gi->font_active; int font_width = getFontWidth(font_nr); @@ -816,7 +1103,7 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) int border_xsize = gi->border.xsize; int border_ysize = gi->border.ysize; - gi->width = 2 * border_xsize + (gi->text.size + 1) * font_width; + gi->width = 2 * border_xsize + (gi->textinput.size + 1) * font_width; gi->height = 2 * border_ysize + font_height; } @@ -828,10 +1115,11 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) int border_xsize = gi->border.xsize; int border_ysize = gi->border.ysize; int button_size = gi->border.xsize_selectbutton; + int bottom_screen_border = gfx.sy + gfx.sysize - font_height; Bitmap *src_bitmap; int src_x, src_y; - gi->width = 2 * border_xsize + gi->text.size * font_width + button_size; + gi->width = 2 * border_xsize + gi->textinput.size*font_width +button_size; gi->height = 2 * border_ysize + font_height; if (gi->selectbox.options == NULL) @@ -848,10 +1136,10 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) gi->selectbox.x = gi->x; gi->selectbox.y = gi->y + gi->height; - if (gi->selectbox.y + gi->selectbox.height > gfx.real_sy + gfx.full_sysize) + if (gi->selectbox.y + gi->selectbox.height > bottom_screen_border) gi->selectbox.y = gi->y - gi->selectbox.height; if (gi->selectbox.y < 0) - gi->selectbox.y = gfx.real_sy + gfx.full_sysize - gi->selectbox.height; + gi->selectbox.y = bottom_screen_border - gi->selectbox.height; getFontCharSource(font_nr, FONT_ASCII_CURSOR, &src_bitmap, &src_x, &src_y); src_x += font_width / 2; @@ -862,9 +1150,9 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) gi->selectbox.open = FALSE; } - if (gi->type & GD_TYPE_TEXTINPUT_NUMERIC) + if (gi->type & GD_TYPE_TEXT_INPUT_NUMERIC) { - struct GadgetTextInput *text = &gi->text; + struct GadgetTextInput *text = &gi->textinput; int value = text->number_value; text->number_value = (value < text->number_min ? text->number_min : @@ -906,6 +1194,26 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) if (gs->item_position == gs->items_max - gs->items_visible) gs->position = gs->position_max; } + + if (gi->type & GD_TYPE_TEXT_AREA) + { + int font_nr = gi->font_active; + int font_width = getFontWidth(font_nr); + int font_height = getFontHeight(font_nr); + int border_xsize = gi->border.xsize; + int border_ysize = gi->border.ysize; + + if (gi->width == 0 || gi->height == 0) + { + gi->width = 2 * border_xsize + gi->textarea.xsize * font_width; + gi->height = 2 * border_ysize + gi->textarea.ysize * font_height; + } + else + { + gi->textarea.xsize = (gi->width - 2 * border_xsize) / font_width; + gi->textarea.ysize = (gi->height - 2 * border_ysize) / font_height; + } + } } void ModifyGadget(struct GadgetInfo *gi, int first_tag, ...) @@ -922,7 +1230,7 @@ void ModifyGadget(struct GadgetInfo *gi, int first_tag, ...) void RedrawGadget(struct GadgetInfo *gi) { if (gi->mapped) - DrawGadget(gi, gi->state, DG_DIRECT); + DrawGadget(gi, gi->state, gi->direct_draw); } struct GadgetInfo *CreateGadget(int first_tag, ...) @@ -935,6 +1243,8 @@ struct GadgetInfo *CreateGadget(int first_tag, ...) new_gadget->callback_info = default_callback_info; new_gadget->callback_action = default_callback_action; new_gadget->active = TRUE; + new_gadget->direct_draw = TRUE; + new_gadget->next = NULL; va_start(ap, first_tag); @@ -974,22 +1284,22 @@ void FreeGadget(struct GadgetInfo *gi) static void CheckRangeOfNumericInputGadget(struct GadgetInfo *gi) { - if (gi->type != GD_TYPE_TEXTINPUT_NUMERIC) + if (gi->type != GD_TYPE_TEXT_INPUT_NUMERIC) return; - gi->text.number_value = atoi(gi->text.value); + gi->textinput.number_value = atoi(gi->textinput.value); - if (gi->text.number_value < gi->text.number_min) - gi->text.number_value = gi->text.number_min; - if (gi->text.number_value > gi->text.number_max) - gi->text.number_value = gi->text.number_max; + if (gi->textinput.number_value < gi->textinput.number_min) + gi->textinput.number_value = gi->textinput.number_min; + if (gi->textinput.number_value > gi->textinput.number_max) + gi->textinput.number_value = gi->textinput.number_max; - sprintf(gi->text.value, "%d", gi->text.number_value); + sprintf(gi->textinput.value, "%d", gi->textinput.number_value); - if (gi->text.cursor_position < 0) - gi->text.cursor_position = 0; - else if (gi->text.cursor_position > strlen(gi->text.value)) - gi->text.cursor_position = strlen(gi->text.value); + if (gi->textinput.cursor_position < 0) + gi->textinput.cursor_position = 0; + else if (gi->textinput.cursor_position > strlen(gi->textinput.value)) + gi->textinput.cursor_position = strlen(gi->textinput.value); } /* global pointer to gadget actually in use (when mouse button pressed) */ @@ -1075,19 +1385,26 @@ void RemapAllGadgets() MultiMapGadgets(MULTIMAP_ALL | MULTIMAP_REMAP); } -boolean anyTextInputGadgetActive() +static boolean anyTextInputGadgetActive() +{ + return (last_gi && (last_gi->type & GD_TYPE_TEXT_INPUT) && last_gi->mapped); +} + +static boolean anyTextAreaGadgetActive() { - return (last_gi && (last_gi->type & GD_TYPE_TEXTINPUT) && last_gi->mapped); + return (last_gi && (last_gi->type & GD_TYPE_TEXT_AREA) && last_gi->mapped); } -boolean anySelectboxGadgetActive() +static boolean anySelectboxGadgetActive() { return (last_gi && (last_gi->type & GD_TYPE_SELECTBOX) && last_gi->mapped); } boolean anyTextGadgetActive() { - return (anyTextInputGadgetActive() || anySelectboxGadgetActive()); + return (anyTextInputGadgetActive() || + anyTextAreaGadgetActive() || + anySelectboxGadgetActive()); } void ClickOnGadget(struct GadgetInfo *gi, int button) @@ -1130,6 +1447,13 @@ void HandleGadgets(int mx, int my, int button) if (gadget_list_first_entry == NULL) return; + /* simulated release of mouse button over last gadget */ + if (mx == -1 && my == -1 && button == 0) + { + mx = last_mx; + my = last_my; + } + /* check which gadget is under the mouse pointer */ new_gi = getGadgetInfoFromMousePosition(mx, my); @@ -1143,6 +1467,78 @@ void HandleGadgets(int mx, int my, int button) last_mx = mx; last_my = my; +#if 1 + +#if 1 + + /* if mouse button pressed outside text or selectbox gadget, deactivate it */ + if (anyTextGadgetActive() && + button != 0 && !motion_status && new_gi != last_gi) + { + CheckRangeOfNumericInputGadget(last_gi); /* in case of numeric gadget */ + + DrawGadget(last_gi, DG_UNPRESSED, last_gi->direct_draw); + + last_gi->event.type = GD_EVENT_TEXT_LEAVING; + + if (last_gi->event_mask & GD_EVENT_TEXT_LEAVING) + last_gi->callback_action(last_gi); + + last_gi = NULL; + } + +#else + + /* special treatment for leaving text and number input gadgets */ + if (anyTextInputGadgetActive() && + button != 0 && !motion_status && new_gi != last_gi) + { + /* if mouse button pressed outside text input gadget, deactivate it */ + CheckRangeOfNumericInputGadget(last_gi); + DrawGadget(last_gi, DG_UNPRESSED, last_gi->direct_draw); + + last_gi->event.type = GD_EVENT_TEXT_LEAVING; + + if (last_gi->event_mask & GD_EVENT_TEXT_LEAVING) + last_gi->callback_action(last_gi); + + last_gi = NULL; + } + + /* special treatment for leaving text area gadgets */ + if (anyTextAreaGadgetActive() && + button != 0 && !motion_status && new_gi != last_gi) + { + /* if mouse button pressed outside text input gadget, deactivate it */ + DrawGadget(last_gi, DG_UNPRESSED, last_gi->direct_draw); + + last_gi->event.type = GD_EVENT_TEXT_LEAVING; + + if (last_gi->event_mask & GD_EVENT_TEXT_LEAVING) + last_gi->callback_action(last_gi); + + last_gi = NULL; + } + + /* special treatment for leaving selectbox gadgets */ + if (anySelectboxGadgetActive() && + button != 0 && !motion_status && new_gi != last_gi) + { + /* if mouse button pressed outside selectbox gadget, deactivate it */ + DrawGadget(last_gi, DG_UNPRESSED, last_gi->direct_draw); + + last_gi->event.type = GD_EVENT_TEXT_LEAVING; + + if (last_gi->event_mask & GD_EVENT_TEXT_LEAVING) + last_gi->callback_action(last_gi); + + last_gi = NULL; + } + +#endif + +#else + /* special treatment for text and number input gadgets */ if (anyTextInputGadgetActive() && button != 0 && !motion_status) { @@ -1150,25 +1546,64 @@ void HandleGadgets(int mx, int my, int button) if (new_gi == last_gi) { - int old_cursor_position = gi->text.cursor_position; + int old_cursor_position = gi->textinput.cursor_position; /* if mouse button pressed inside activated text gadget, set cursor */ - gi->text.cursor_position = + gi->textinput.cursor_position = (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font); - if (gi->text.cursor_position < 0) - gi->text.cursor_position = 0; - else if (gi->text.cursor_position > strlen(gi->text.value)) - gi->text.cursor_position = strlen(gi->text.value); + if (gi->textinput.cursor_position < 0) + gi->textinput.cursor_position = 0; + else if (gi->textinput.cursor_position > strlen(gi->textinput.value)) + gi->textinput.cursor_position = strlen(gi->textinput.value); - if (gi->text.cursor_position != old_cursor_position) - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + if (gi->textinput.cursor_position != old_cursor_position) + DrawGadget(gi, DG_PRESSED, gi->direct_draw); } else { /* if mouse button pressed outside text input gadget, deactivate it */ CheckRangeOfNumericInputGadget(gi); - DrawGadget(gi, DG_UNPRESSED, DG_DIRECT); + DrawGadget(gi, DG_UNPRESSED, gi->direct_draw); + + gi->event.type = GD_EVENT_TEXT_LEAVING; + + if (gi->event_mask & GD_EVENT_TEXT_LEAVING) + gi->callback_action(gi); + + last_gi = NULL; + } + } + + /* special treatment for text area gadgets */ + if (anyTextAreaGadgetActive() && button != 0 && !motion_status) + { + struct GadgetInfo *gi = last_gi; + + if (new_gi == last_gi) + { + int old_cursor_position = gi->textarea.cursor_position; + int x = (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font); + int y = (my - gi->y - gi->border.ysize) / getFontHeight(gi->font); + + x = (x < 0 ? 0 : x >= gi->textarea.xsize ? gi->textarea.xsize - 1 : x); + y = (y < 0 ? 0 : y >= gi->textarea.ysize ? gi->textarea.ysize - 1 : y); + + setTextAreaCursorXY(gi, x, y); + +#if 0 + printf("::: %d -----> %d\n", + old_cursor_position, + gi->textarea.cursor_position); +#endif + + if (gi->textarea.cursor_position != old_cursor_position) + DrawGadget(gi, DG_PRESSED, gi->direct_draw); + } + else + { + /* if mouse button pressed outside text input gadget, deactivate it */ + DrawGadget(gi, DG_UNPRESSED, gi->direct_draw); gi->event.type = GD_EVENT_TEXT_LEAVING; @@ -1191,7 +1626,7 @@ void HandleGadgets(int mx, int my, int button) /* if mouse button pressed inside activated selectbox, select value */ if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height) gi->selectbox.current_index = - (my - gi->selectbox.y - gi->border.xsize) / getFontWidth(gi->font); + (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font); if (gi->selectbox.current_index < 0) gi->selectbox.current_index = 0; @@ -1199,12 +1634,12 @@ void HandleGadgets(int mx, int my, int button) gi->selectbox.current_index = gi->selectbox.num_values - 1; if (gi->selectbox.current_index != old_index) - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + DrawGadget(gi, DG_PRESSED, gi->direct_draw); } else { /* if mouse button pressed outside selectbox gadget, deactivate it */ - DrawGadget(gi, DG_UNPRESSED, DG_DIRECT); + DrawGadget(gi, DG_UNPRESSED, gi->direct_draw); gi->event.type = GD_EVENT_TEXT_LEAVING; @@ -1215,6 +1650,8 @@ void HandleGadgets(int mx, int my, int button) } } +#endif + gadget_pressed = (button != 0 && last_gi == NULL && new_gi != NULL && press_event); gadget_pressed_repeated = @@ -1237,8 +1674,8 @@ void HandleGadgets(int mx, int my, int button) (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); + (mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width && + my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height); } else { @@ -1261,11 +1698,12 @@ 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) && + if (last_gi->type & GD_TYPE_SELECTBOX && (gadget_released_inside_select_line || - gadget_released_off_borders)) /* selectbox stays open */ + gadget_released_off_borders)) /* selectbox stays open */ gi->selectbox.stay_open = TRUE; - else if (!(last_gi->type & GD_TYPE_TEXTINPUT)) /* text input stays open */ + else if (!(last_gi->type & GD_TYPE_TEXT_INPUT || + last_gi->type & GD_TYPE_TEXT_AREA)) /* text input stays open */ last_gi = NULL; } @@ -1289,6 +1727,36 @@ void HandleGadgets(int mx, int my, int button) if (last_x != gi->event.x || last_y != gi->event.y) changed_position = TRUE; } + else if (gi->type & GD_TYPE_TEXT_INPUT && button != 0 && !motion_status) + { + int old_cursor_position = gi->textinput.cursor_position; + + /* if mouse button pressed inside activated text gadget, set cursor */ + gi->textinput.cursor_position = + (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font); + + if (gi->textinput.cursor_position < 0) + gi->textinput.cursor_position = 0; + else if (gi->textinput.cursor_position > strlen(gi->textinput.value)) + gi->textinput.cursor_position = strlen(gi->textinput.value); + + if (gi->textinput.cursor_position != old_cursor_position) + DrawGadget(gi, DG_PRESSED, gi->direct_draw); + } + else if (gi->type & GD_TYPE_TEXT_AREA && button != 0 && !motion_status) + { + int old_cursor_position = gi->textarea.cursor_position; + int x = (mx - gi->x - gi->border.xsize) / getFontWidth(gi->font); + int y = (my - gi->y - gi->border.ysize) / getFontHeight(gi->font); + + x = (x < 0 ? 0 : x >= gi->textarea.xsize ? gi->textarea.xsize - 1 : x); + y = (y < 0 ? 0 : y >= gi->textarea.ysize ? gi->textarea.ysize - 1 : y); + + setTextAreaCursorXY(gi, x, y); + + if (gi->textarea.cursor_position != old_cursor_position) + DrawGadget(gi, DG_PRESSED, gi->direct_draw); + } else if (gi->type & GD_TYPE_SELECTBOX) { int old_index = gi->selectbox.current_index; @@ -1296,7 +1764,7 @@ void HandleGadgets(int mx, int my, int button) /* if mouse moving inside activated selectbox, select value */ if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height) gi->selectbox.current_index = - (my - gi->selectbox.y - gi->border.xsize) / getFontWidth(gi->font); + (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font); if (gi->selectbox.current_index < 0) gi->selectbox.current_index = 0; @@ -1304,7 +1772,7 @@ void HandleGadgets(int mx, int my, int button) gi->selectbox.current_index = gi->selectbox.num_values - 1; if (gi->selectbox.current_index != old_index) - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + DrawGadget(gi, DG_PRESSED, gi->direct_draw); } } @@ -1353,7 +1821,7 @@ void HandleGadgets(int mx, int my, int button) rgi != gi) { rgi->checked = FALSE; - DrawGadget(rgi, DG_UNPRESSED, DG_DIRECT); + DrawGadget(rgi, DG_UNPRESSED, rgi->direct_draw); } rgi = rgi->next; @@ -1423,7 +1891,8 @@ void HandleGadgets(int mx, int my, int button) } } - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + /* !!! bad for TEXT_INPUT ... !!! */ + DrawGadget(gi, DG_PRESSED, gi->direct_draw); gi->state = GD_BUTTON_PRESSED; gi->event.type = GD_EVENT_PRESSED; @@ -1437,6 +1906,11 @@ void HandleGadgets(int mx, int my, int button) gi->callback_action(gi); } +#if 0 + if (!mouse_moving) + printf("::: PRESSED...?\n"); +#endif + if (gadget_pressed_repeated) { gi->event.type = GD_EVENT_PRESSED; @@ -1451,9 +1925,9 @@ void HandleGadgets(int mx, int my, int button) if (gi->type & GD_TYPE_BUTTON) { if (gadget_moving_inside && gi->state == GD_BUTTON_UNPRESSED) - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + DrawGadget(gi, DG_PRESSED, gi->direct_draw); else if (gadget_moving_off_borders && gi->state == GD_BUTTON_PRESSED) - DrawGadget(gi, DG_UNPRESSED, DG_DIRECT); + DrawGadget(gi, DG_UNPRESSED, gi->direct_draw); } else if (gi->type & GD_TYPE_SELECTBOX) { @@ -1462,7 +1936,7 @@ void HandleGadgets(int mx, int my, int button) /* if mouse moving inside activated selectbox, select value */ if (my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height) gi->selectbox.current_index = - (my - gi->selectbox.y - gi->border.xsize) / getFontWidth(gi->font); + (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font); if (gi->selectbox.current_index < 0) gi->selectbox.current_index = 0; @@ -1470,7 +1944,7 @@ void HandleGadgets(int mx, int my, int button) gi->selectbox.current_index = gi->selectbox.num_values - 1; if (gi->selectbox.current_index != old_index) - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + DrawGadget(gi, DG_PRESSED, gi->direct_draw); } else if (gi->type & GD_TYPE_SCROLLBAR) { @@ -1498,7 +1972,7 @@ void HandleGadgets(int mx, int my, int button) changed_position = TRUE; } - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + DrawGadget(gi, DG_PRESSED, gi->direct_draw); } gi->state = (gadget_moving_inside || gi->type & GD_TYPE_SCROLLBAR ? @@ -1525,8 +1999,9 @@ void HandleGadgets(int mx, int my, int button) } if (deactivate_gadget && - !(gi->type & GD_TYPE_TEXTINPUT)) /* text input stays open */ - DrawGadget(gi, DG_UNPRESSED, DG_DIRECT); + !(gi->type & GD_TYPE_TEXT_INPUT || + gi->type & GD_TYPE_TEXT_AREA)) /* text input stays open */ + DrawGadget(gi, DG_UNPRESSED, gi->direct_draw); gi->state = GD_BUTTON_UNPRESSED; gi->event.type = GD_EVENT_RELEASED; @@ -1538,7 +2013,7 @@ void HandleGadgets(int mx, int my, int button) if (gadget_released_off_borders) { if (gi->type & GD_TYPE_SCROLLBAR) - DrawGadget(gi, DG_UNPRESSED, DG_DIRECT); + DrawGadget(gi, DG_UNPRESSED, gi->direct_draw); gi->event.type = GD_EVENT_RELEASED; @@ -1546,6 +2021,33 @@ void HandleGadgets(int mx, int my, int button) gi->event_mask & GD_EVENT_OFF_BORDERS) gi->callback_action(gi); } + + /* handle gadgets unmapped/mapped between pressing and releasing */ + if (release_event && !gadget_released && new_gi) + new_gi->state = GD_BUTTON_UNPRESSED; +} + +static void insertCharIntoTextArea(struct GadgetInfo *gi, char c) +{ + char text[MAX_GADGET_TEXTSIZE]; + int cursor_position = gi->textarea.cursor_position; + + if (strlen(gi->textarea.value) == MAX_GADGET_TEXTSIZE) /* no space left */ + return; + +#if 0 + printf("::: '%s' + '%c'", gi->textarea.value, c); +#endif + + strcpy(text, gi->textarea.value); + strcpy(&gi->textarea.value[cursor_position + 1], &text[cursor_position]); + gi->textarea.value[cursor_position] = c; + +#if 0 + printf(" => '%s'\n", gi->textarea.value); +#endif + + setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1); } void HandleGadgetsKeyInput(Key key) @@ -1553,66 +2055,145 @@ void HandleGadgetsKeyInput(Key key) struct GadgetInfo *gi = last_gi; if (gi == NULL || !gi->mapped || - !((gi->type & GD_TYPE_TEXTINPUT) || (gi->type & GD_TYPE_SELECTBOX))) + !(gi->type & GD_TYPE_TEXT_INPUT || + gi->type & GD_TYPE_TEXT_AREA || + gi->type & GD_TYPE_SELECTBOX)) return; if (key == KSYM_Return) /* valid for both text input and selectbox */ { - if (gi->type & GD_TYPE_TEXTINPUT) + if (gi->type & GD_TYPE_TEXT_INPUT) CheckRangeOfNumericInputGadget(gi); else if (gi->type & GD_TYPE_SELECTBOX) gi->selectbox.index = gi->selectbox.current_index; - DrawGadget(gi, DG_UNPRESSED, DG_DIRECT); + if (gi->type & GD_TYPE_TEXT_AREA) + { + insertCharIntoTextArea(gi, '\n'); - gi->event.type = GD_EVENT_TEXT_RETURN; + DrawGadget(gi, DG_PRESSED, gi->direct_draw); + } + else + { + DrawGadget(gi, DG_UNPRESSED, gi->direct_draw); + + gi->event.type = GD_EVENT_TEXT_RETURN; + + last_gi = NULL; + } if (gi->event_mask & GD_EVENT_TEXT_RETURN) gi->callback_action(gi); - - last_gi = NULL; } - else if (gi->type & GD_TYPE_TEXTINPUT) /* only valid for text input */ + else if (gi->type & GD_TYPE_TEXT_INPUT) /* only valid for text input */ { char text[MAX_GADGET_TEXTSIZE]; - int text_length = strlen(gi->text.value); - int cursor_pos = gi->text.cursor_position; + int text_length = strlen(gi->textinput.value); + int cursor_pos = gi->textinput.cursor_position; char letter = getCharFromKey(key); - boolean legal_letter = (gi->type == GD_TYPE_TEXTINPUT_NUMERIC ? + boolean legal_letter = (gi->type == GD_TYPE_TEXT_INPUT_NUMERIC ? letter >= '0' && letter <= '9' : letter != 0); - if (legal_letter && text_length < gi->text.size) + if (legal_letter && text_length < gi->textinput.size) { - strcpy(text, gi->text.value); - strcpy(&gi->text.value[cursor_pos + 1], &text[cursor_pos]); - gi->text.value[cursor_pos] = letter; - gi->text.cursor_position++; + strcpy(text, gi->textinput.value); + strcpy(&gi->textinput.value[cursor_pos + 1], &text[cursor_pos]); + gi->textinput.value[cursor_pos] = letter; + gi->textinput.cursor_position++; - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + DrawGadget(gi, DG_PRESSED, gi->direct_draw); } else if (key == KSYM_Left && cursor_pos > 0) { - gi->text.cursor_position--; - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + gi->textinput.cursor_position--; + + DrawGadget(gi, DG_PRESSED, gi->direct_draw); } else if (key == KSYM_Right && cursor_pos < text_length) { - gi->text.cursor_position++; - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + gi->textinput.cursor_position++; + + DrawGadget(gi, DG_PRESSED, gi->direct_draw); } else if (key == KSYM_BackSpace && cursor_pos > 0) { - strcpy(text, gi->text.value); - strcpy(&gi->text.value[cursor_pos - 1], &text[cursor_pos]); - gi->text.cursor_position--; - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + strcpy(text, gi->textinput.value); + strcpy(&gi->textinput.value[cursor_pos - 1], &text[cursor_pos]); + gi->textinput.cursor_position--; + + DrawGadget(gi, DG_PRESSED, gi->direct_draw); } else if (key == KSYM_Delete && cursor_pos < text_length) { - strcpy(text, gi->text.value); - strcpy(&gi->text.value[cursor_pos], &text[cursor_pos + 1]); - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + strcpy(text, gi->textinput.value); + strcpy(&gi->textinput.value[cursor_pos], &text[cursor_pos + 1]); + + DrawGadget(gi, DG_PRESSED, gi->direct_draw); + } + } + else if (gi->type & GD_TYPE_TEXT_AREA) /* only valid for text area */ + { + char text[MAX_GADGET_TEXTSIZE]; + int text_length = strlen(gi->textarea.value); + int area_ysize = gi->textarea.ysize; + int cursor_x_pref = gi->textarea.cursor_x_preferred; + int cursor_y = gi->textarea.cursor_y; + int cursor_pos = gi->textarea.cursor_position; + char letter = getCharFromKey(key); + boolean legal_letter = (letter != 0); + +#if 0 + printf("::: KEY: %x!\n", key); +#endif + + if (legal_letter) + { + insertCharIntoTextArea(gi, letter); + + DrawGadget(gi, DG_PRESSED, gi->direct_draw); + } + else if (key == KSYM_Left && cursor_pos > 0) + { + setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1); + + DrawGadget(gi, DG_PRESSED, gi->direct_draw); + } + else if (key == KSYM_Right && cursor_pos < text_length) + { + setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1); + + DrawGadget(gi, DG_PRESSED, gi->direct_draw); + } + else if (key == KSYM_Up && cursor_y > 0) + { + setTextAreaCursorXY(gi, cursor_x_pref, cursor_y - 1); + gi->textarea.cursor_x_preferred = cursor_x_pref; + + DrawGadget(gi, DG_PRESSED, gi->direct_draw); + } + else if (key == KSYM_Down && cursor_y < area_ysize - 1) + { + setTextAreaCursorXY(gi, cursor_x_pref, cursor_y + 1); + gi->textarea.cursor_x_preferred = cursor_x_pref; + + DrawGadget(gi, DG_PRESSED, gi->direct_draw); + } + else if (key == KSYM_BackSpace && cursor_pos > 0) + { + strcpy(text, gi->textarea.value); + strcpy(&gi->textarea.value[cursor_pos - 1], &text[cursor_pos]); + + setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1); + + DrawGadget(gi, DG_PRESSED, gi->direct_draw); + } + else if (key == KSYM_Delete && cursor_pos < text_length) + { + strcpy(text, gi->textarea.value); + strcpy(&gi->textarea.value[cursor_pos], &text[cursor_pos + 1]); + + DrawGadget(gi, DG_PRESSED, gi->direct_draw); } } else if (gi->type & GD_TYPE_SELECTBOX) /* only valid for selectbox */ @@ -1623,12 +2204,14 @@ void HandleGadgetsKeyInput(Key key) if (key == KSYM_Up && index > 0) { gi->selectbox.current_index--; - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + + DrawGadget(gi, DG_PRESSED, gi->direct_draw); } else if (key == KSYM_Down && index < num_values - 1) { gi->selectbox.current_index++; - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + + DrawGadget(gi, DG_PRESSED, gi->direct_draw); } } }