X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fgadgets.c;h=50dc853129dd107128b5a6f103520758cb46c763;hb=fda8c9f42aa13663ad6b2f698da787a7280b9965;hp=594131676338192ac4fd05444fd8b1ce6d6b9b15;hpb=5f23eef52ec62dc30c388b1016aea87b80873d20;p=rocksndiamonds.git diff --git a/src/libgame/gadgets.c b/src/libgame/gadgets.c index 59413167..50dc8531 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 @@ -68,7 +69,7 @@ void DUMP_GADGET_MAP_STATE() { struct GadgetInfo *gi = gadget_list_first_entry; - while (gi) + while (gi != NULL) { printf("-XXX-1-> '%s': %s\n", gi->info_text, (gi->mapped ? "mapped" : "not mapped")); @@ -80,22 +81,89 @@ void DUMP_GADGET_MAP_STATE() static struct GadgetInfo *getGadgetInfoFromMousePosition(int mx, int my) { - struct GadgetInfo *gi = gadget_list_first_entry; + struct GadgetInfo *gi; - while (gi) + /* open selectboxes may overlap other active gadgets, so check them first */ + for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next) { - if (gi->mapped && - ((mx >= gi->x && mx < gi->x + gi->width && - my >= gi->y && my < gi->y + gi->height) || - (gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open && - mx >= gi->selectbox.x && mx < gi->selectbox.x+gi->selectbox.width && - my >= gi->selectbox.y && my < gi->selectbox.y+gi->selectbox.height))) - break; + if (gi->mapped && gi->active && + gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open && + mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width && + my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height) + return gi; + } - gi = gi->next; + /* check all other gadgets */ + for (gi = gadget_list_first_entry; gi != NULL; gi = gi->next) + { + if (gi->mapped && gi->active && + mx >= gi->x && mx < gi->x + gi->width && + my >= gi->y && my < gi->y + gi->height) + return gi; } - return gi; + return NULL; +} + +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++; + } + + 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); } static void default_callback_info(void *ptr) @@ -110,9 +178,9 @@ static void default_callback_action(void *ptr) static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) { - int state = (pressed ? 1 : 0); - struct GadgetDesign *gd = (gi->checked ? - &gi->alt_design[state] : + 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; @@ -132,54 +200,179 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) gi->y + gi->deco.y + (pressed ? gi->deco.yshift : 0)); break; - case GD_TYPE_TEXTINPUT_ALPHANUMERIC: - case GD_TYPE_TEXTINPUT_NUMERIC: + case GD_TYPE_TEXT_BUTTON: + { + int i; + int font_nr = (gi->active ? gi->font_active : gi->font); + int font_width = getFontWidth(font_nr); + int border_x = gi->border.xsize; + int border_y = gi->border.ysize; + int text_size = strlen(gi->textbutton.value); + int text_start = (gi->width - text_size * font_width) / 2; + + /* left part of gadget */ + BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y, + border_x, gi->height, gi->x, gi->y); + + /* middle part of gadget */ + for (i=0; i < gi->textbutton.size; i++) + BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y, + font_width, gi->height, + gi->x + border_x + i * font_width, gi->y); + + /* right part of gadget */ + BlitBitmapOnBackground(gd->bitmap, drawto, + gd->x + gi->border.width - border_x, gd->y, + border_x, gi->height, + gi->x + gi->width - border_x, gi->y); + + /* gadget text value */ + DrawTextExt(drawto, + gi->x + text_start + (pressed ? gi->deco.xshift : 0), + gi->y + border_y + (pressed ? gi->deco.yshift : 0), + gi->textbutton.value, font_nr, BLIT_MASKED); + } + break; + + case GD_TYPE_TEXT_INPUT_ALPHANUMERIC: + case GD_TYPE_TEXT_INPUT_NUMERIC: { int i; char cursor_letter; - char cursor_string[3]; + char cursor_string[2]; char text[MAX_GADGET_TEXTSIZE + 1]; - int font_type = gi->text.font_type; - int font_width = getFontWidth(font_type); - int border = gi->border.size; + int font_nr = (pressed ? gi->font_active : gi->font); + int font_width = getFontWidth(font_nr); + int border_x = gi->border.xsize; + int border_y = gi->border.ysize; /* left part of gadget */ - BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x, gd->y, border, gi->height, gi->x, gi->y); + BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y, + border_x, gi->height, gi->x, gi->y); /* middle part of gadget */ - for (i=0; i < gi->text.size + 1; i++) - BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x + border, gd->y, font_width, gi->height, - gi->x + border + i * font_width, gi->y); + 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); /* right part of gadget */ BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x + gi->border.width - border, gd->y,border, - gi->height, gi->x + gi->width - border, gi->y); + gd->x + gi->border.width - border_x, gd->y, + border_x, gi->height, + 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 */ DrawTextExt(drawto, - gi->x + border, gi->y + border, text, - font_type, BLIT_MASKED); + gi->x + border_x, gi->y + border_y, text, + font_nr, BLIT_MASKED); - cursor_letter = gi->text.value[gi->text.cursor_position]; - cursor_string[0] = '~'; - cursor_string[1] = (cursor_letter != '\0' ? cursor_letter : ' '); - cursor_string[2] = '\0'; + cursor_letter = gi->textinput.value[gi->textinput.cursor_position]; + cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' '); + cursor_string[1] = '\0'; - SetInverseTextColor(gi->text.inverse_color); + /* draw cursor, if active */ + if (pressed) + DrawTextExt(drawto, + 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.xsize, gi->textarea.ysize, + BLIT_ON_BACKGROUND); + + cursor_letter = gi->textarea.value[gi->textarea.cursor_position]; + cursor_string[0] = (cursor_letter != '\0' ? cursor_letter : ' '); + cursor_string[1] = '\0'; /* draw cursor, if active */ if (pressed) DrawTextExt(drawto, - gi->x + border + gi->text.cursor_position * font_width, - gi->y + border, cursor_string, - font_type, BLIT_MASKED); + 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; @@ -187,44 +380,46 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) { int i; char text[MAX_GADGET_TEXTSIZE + 1]; - int font_type = gi->selectbox.font_type; - int font_width = getFontWidth(font_type); - int font_height = getFontHeight(font_type); - int border = gi->border.size; - int button = gi->border.size_selectbutton; - int width_inner = gi->border.width - button - 2 * border; + 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 button = gi->border.xsize_selectbutton; + int width_inner = gi->border.width - button - 2 * border_x; int box_width = gi->selectbox.width; int box_height = gi->selectbox.height; /* left part of gadget */ - BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x, gd->y, border, gi->height, gi->x, gi->y); + BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y, + border_x, gi->height, gi->x, gi->y); /* middle part of gadget */ for (i=0; i < gi->selectbox.size; i++) - BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x + border, gd->y, font_width, gi->height, - gi->x + border + i * font_width, gi->y); + BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y, + font_width, gi->height, + gi->x + border_x + i * font_width, gi->y); /* button part of gadget */ BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x + border + width_inner, gd->y, + gd->x + border_x + width_inner, gd->y, button, gi->height, - gi->x + gi->width - border - button, gi->y); + gi->x + gi->width - border_x - button, gi->y); /* right part of gadget */ BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x + gi->border.width - border, gd->y,border, - gi->height, gi->x + gi->width - border, gi->y); + gd->x + gi->border.width - border_x, gd->y, + border_x, gi->height, + gi->x + gi->width - border_x, gi->y); /* set text value */ - strncpy(text, gi->selectbox.values[gi->selectbox.index], + strncpy(text, gi->selectbox.options[gi->selectbox.index].text, gi->selectbox.size); text[gi->selectbox.size] = '\0'; /* gadget text value */ - DrawTextExt(drawto, gi->x + border, gi->y + border, text, - font_type, BLIT_MASKED); + DrawTextExt(drawto, gi->x + border_x, gi->y + border_y, text, + font_nr, BLIT_MASKED); if (pressed) { @@ -245,109 +440,110 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) /* top left part of gadget border */ BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y, - border, border, + border_x, border_y, gi->selectbox.x, gi->selectbox.y); /* top middle part of gadget border */ for (i=0; i < gi->selectbox.size; i++) - BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border, gd->y, - font_width, border, - gi->selectbox.x + border + i * font_width, + BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y, + font_width, border_y, + gi->selectbox.x + border_x + i * font_width, gi->selectbox.y); /* top button part of gadget border */ BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x + border + width_inner, gd->y, - button, border, - gi->selectbox.x + box_width - border - button, + gd->x + border_x + width_inner, gd->y, + button, border_y, + gi->selectbox.x + box_width -border_x -button, gi->selectbox.y); /* top right part of gadget border */ BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x + gi->border.width - border, gd->y, - border, border, - gi->selectbox.x + box_width - border, + gd->x + gi->border.width - border_x, gd->y, + border_x, border_y, + gi->selectbox.x + box_width - border_x, gi->selectbox.y); /* left and right part of gadget border for each row */ for (i=0; i < gi->selectbox.num_values; i++) { - BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border, - border, font_height, + BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y, + border_x, font_height, gi->selectbox.x, - gi->selectbox.y + border + i * font_height); + gi->selectbox.y + border_y + i*font_height); BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x + gi->border.width - border, - gd->y + border, - border, font_height, - gi->selectbox.x + box_width - border, - gi->selectbox.y + border + i * font_height); + gd->x + gi->border.width - border_x, + gd->y + border_y, + border_x, font_height, + gi->selectbox.x + box_width - border_x, + gi->selectbox.y + border_y + i*font_height); } /* bottom left part of gadget border */ BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x, gd->y + gi->height - border, - border, border, + gd->x, gd->y + gi->height - border_y, + border_x, border_y, gi->selectbox.x, - gi->selectbox.y + box_height - border); + gi->selectbox.y + box_height - border_y); /* bottom middle part of gadget border */ for (i=0; i < gi->selectbox.size; i++) BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x + border, gd->y + gi->height - border, - font_width, border, - gi->selectbox.x + border + i * font_width, - gi->selectbox.y + box_height - border); + gd->x + border_x, + gd->y + gi->height - border_y, + font_width, border_y, + gi->selectbox.x + border_x + i * font_width, + gi->selectbox.y + box_height - border_y); /* bottom button part of gadget border */ BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x + border + width_inner, - gd->y + gi->height - border, - button, border, - gi->selectbox.x + box_width - border - button, - gi->selectbox.y + box_height - border); + gd->x + border_x + width_inner, + gd->y + gi->height - border_y, + button, border_y, + gi->selectbox.x + box_width -border_x -button, + gi->selectbox.y + box_height - border_y); /* bottom right part of gadget border */ BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x + gi->border.width - border, - gd->y + gi->height - border, - border, border, - gi->selectbox.x + box_width - border, - gi->selectbox.y + box_height - border); + gd->x + gi->border.width - border_x, + gd->y + gi->height - border_y, + border_x, border_y, + gi->selectbox.x + box_width - border_x, + gi->selectbox.y + box_height - border_y); ClearRectangleOnBackground(drawto, - gi->selectbox.x + border, - gi->selectbox.y + border, - gi->selectbox.width - 2 * border, - gi->selectbox.height - 2 * border); + gi->selectbox.x + border_x, + gi->selectbox.y + border_y, + gi->selectbox.width - 2 * border_x, + gi->selectbox.height - 2 * border_y); /* selectbox text values */ for (i=0; i < gi->selectbox.num_values; i++) { + int mask_mode = BLIT_MASKED; + + strncpy(text, gi->selectbox.options[i].text, gi->selectbox.size); + text[gi->selectbox.size] = '\0'; + if (i == gi->selectbox.current_index) { FillRectangle(drawto, - gi->selectbox.x + border, - gi->selectbox.y + border + i * font_height, - gi->selectbox.width - 2 * border, font_height, + gi->selectbox.x + border_x, + gi->selectbox.y + border_y + i * font_height, + gi->selectbox.width - 2 * border_x, font_height, gi->selectbox.inverse_color); - text[0] = '~'; - strncpy(&text[1], gi->selectbox.values[i], gi->selectbox.size); - text[1 + gi->selectbox.size] = '\0'; - - SetInverseTextColor(gi->selectbox.inverse_color); - } - else - { - strncpy(text, gi->selectbox.values[i], gi->selectbox.size); + /* prevent use of cursor graphic by drawing at least two chars */ + strcat(text, " "); text[gi->selectbox.size] = '\0'; + + mask_mode = BLIT_INVERSE; } DrawTextExt(drawto, - gi->selectbox.x + border, - gi->selectbox.y + border + i * font_height, text, - font_type, BLIT_MASKED); + gi->selectbox.x + border_x, + gi->selectbox.y + border_y + i * font_height, text, + font_nr, mask_mode); } redraw_selectbox = TRUE; @@ -356,15 +552,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; } } @@ -376,9 +572,9 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) int xpos = gi->x; int ypos = gi->y + gi->scrollbar.position; int design_full = gi->width; - int design_body = design_full - 2 * gi->border.size; + int design_body = design_full - 2 * gi->border.ysize; int size_full = gi->scrollbar.size; - int size_body = size_full - 2 * gi->border.size; + int size_body = size_full - 2 * gi->border.ysize; int num_steps = size_body / design_body; int step_size_remain = size_body - num_steps * design_body; @@ -389,31 +585,31 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) /* upper part of gadget */ BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y, - gi->width, gi->border.size, + gi->width, gi->border.ysize, xpos, ypos); /* middle part of gadget */ - for (i=0; ibitmap, drawto, - gd->x, gd->y + gi->border.size, + gd->x, gd->y + gi->border.ysize, gi->width, design_body, xpos, - ypos + gi->border.size + i * design_body); + ypos + gi->border.ysize + i * design_body); /* remaining middle part of gadget */ if (step_size_remain > 0) BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x, gd->y + gi->border.size, + gd->x, gd->y + gi->border.ysize, gi->width, step_size_remain, xpos, - ypos + gi->border.size + ypos + gi->border.ysize + num_steps * design_body); /* lower part of gadget */ BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x, gd->y + design_full - gi->border.size, - gi->width, gi->border.size, - xpos, ypos + size_full - gi->border.size); + gd->x, gd->y + design_full - gi->border.ysize, + gi->width, gi->border.ysize, + xpos, ypos + size_full - gi->border.ysize); } break; @@ -423,9 +619,9 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) int xpos = gi->x + gi->scrollbar.position; int ypos = gi->y; int design_full = gi->height; - int design_body = design_full - 2 * gi->border.size; + int design_body = design_full - 2 * gi->border.xsize; int size_full = gi->scrollbar.size; - int size_body = size_full - 2 * gi->border.size; + int size_body = size_full - 2 * gi->border.xsize; int num_steps = size_body / design_body; int step_size_remain = size_body - num_steps * design_body; @@ -436,31 +632,31 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) /* left part of gadget */ BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y, - gi->border.size, gi->height, + gi->border.xsize, gi->height, xpos, ypos); /* middle part of gadget */ - for (i=0; ibitmap, drawto, - gd->x + gi->border.size, gd->y, + gd->x + gi->border.xsize, gd->y, design_body, gi->height, - xpos + gi->border.size + i * design_body, + xpos + gi->border.xsize + i * design_body, ypos); /* remaining middle part of gadget */ if (step_size_remain > 0) BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x + gi->border.size, gd->y, + gd->x + gi->border.xsize, gd->y, step_size_remain, gi->height, - xpos + gi->border.size + xpos + gi->border.xsize + num_steps * design_body, ypos); /* right part of gadget */ BlitBitmapOnBackground(gd->bitmap, drawto, - gd->x + design_full - gi->border.size, gd->y, - gi->border.size, gi->height, - xpos + size_full - gi->border.size, ypos); + gd->x + design_full - gi->border.xsize, gd->y, + gi->border.xsize, gi->height, + xpos + size_full - gi->border.xsize, ypos); } break; @@ -485,6 +681,22 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct) gi->y > gfx.vy ? REDRAW_DOOR_2 : REDRAW_DOOR_3); } +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; @@ -539,6 +751,18 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) gi->state = va_arg(ap, unsigned long); 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" */ @@ -550,26 +774,29 @@ 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); + strcpy(gi->textinput.last_value, gi->textinput.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); + strcpy(gi->textinput.last_value, gi->textinput.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); + strcpy(gi->textinput.last_value, gi->textinput.value); } break; @@ -577,12 +804,19 @@ 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->textinput.value, va_arg(ap, char *), max_textsize); + strcpy(gi->textinput.last_value, gi->textinput.value); - strncpy(gi->text.value, va_arg(ap, char *), max_textsize); - gi->text.value[max_textsize] = '\0'; - gi->text.cursor_position = strlen(gi->text.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; @@ -591,23 +825,35 @@ 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'; + strcpy(gi->textinput.last_value, gi->textinput.value); + + /* same tag also used for other gadget definitions */ + + gi->textarea.size = max_textsize; + gi->textarea.value[max_textsize] = '\0'; + strcpy(gi->textarea.last_value, gi->textinput.value); - /* same tag also used for selectbox definition */ - gi->selectbox.size = gi->text.size; + gi->textbutton.size = max_textsize; + gi->textbutton.value[max_textsize] = '\0'; + + gi->selectbox.size = gi->textinput.size; } break; case GDI_TEXT_FONT: - gi->text.font_type = va_arg(ap, int); + gi->font = va_arg(ap, int); + if (gi->font_active == 0) + gi->font_active = gi->font; + break; - /* same tag also used for selectbox definition */ - gi->selectbox.font_type = gi->text.font_type; + case GDI_TEXT_FONT_ACTIVE: + gi->font_active = va_arg(ap, int); break; - case GDI_SELECTBOX_VALUES: - gi->selectbox.values = va_arg(ap, const char **); + case GDI_SELECTBOX_OPTIONS: + gi->selectbox.options = va_arg(ap, struct ValueTextInfo *); break; case GDI_SELECTBOX_INDEX: @@ -639,14 +885,15 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) break; case GDI_BORDER_SIZE: - gi->border.size = va_arg(ap, int); + gi->border.xsize = va_arg(ap, int); + gi->border.ysize = va_arg(ap, int); break; case GDI_BORDER_SIZE_SELECTBUTTON: - gi->border.size_selectbutton = va_arg(ap, int); + gi->border.xsize_selectbutton = va_arg(ap, int); break; - case GDI_TEXTINPUT_DESIGN_WIDTH: + case GDI_DESIGN_WIDTH: gi->border.width = va_arg(ap, int); break; @@ -692,6 +939,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: @@ -748,55 +1006,64 @@ 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->text.font_type; + int font_nr = gi->font_active; int font_width = getFontWidth(font_nr); int font_height = getFontHeight(font_nr); - int border_size = gi->border.size; - Bitmap *src_bitmap; - int src_x, src_y; + int border_xsize = gi->border.xsize; + int border_ysize = gi->border.ysize; - gi->width = 2 * border_size + (gi->text.size + 1) * font_width; - gi->height = 2 * border_size + font_height; + 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; + } - getFontCharSource(font_nr, FONT_ASCII_CURSOR, &src_bitmap, &src_x, &src_y); - src_x += font_width / 2; - src_y += font_height / 2; - gi->text.inverse_color = GetPixel(src_bitmap, src_x, src_y); + gi->width = 2 * border_xsize + (gi->textinput.size + 1) * font_width; + gi->height = 2 * border_ysize + font_height; } if (gi->type & GD_TYPE_SELECTBOX) { - int font_nr = gi->selectbox.font_type; + int font_nr = gi->font_active; int font_width = getFontWidth(font_nr); int font_height = getFontHeight(font_nr); - int border_size = gi->border.size; - int button_size = gi->border.size_selectbutton; + 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_size + gi->text.size * font_width + button_size; - gi->height = 2 * border_size + font_height; + gi->width = 2 * border_xsize + gi->textinput.size*font_width +button_size; + gi->height = 2 * border_ysize + font_height; - if (gi->selectbox.values == NULL) - Error(ERR_EXIT, "selectbox gadget incomplete (missing values array)"); + if (gi->selectbox.options == NULL) + Error(ERR_EXIT, "selectbox gadget incomplete (missing options array)"); gi->selectbox.num_values = 0; - while (gi->selectbox.values[gi->selectbox.num_values] != NULL) + while (gi->selectbox.options[gi->selectbox.num_values].text != NULL) gi->selectbox.num_values++; /* calculate values for open selectbox */ gi->selectbox.width = gi->width; gi->selectbox.height = - 2 * border_size + gi->selectbox.num_values * font_height; + 2 * border_ysize + gi->selectbox.num_values * font_height; 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; @@ -807,9 +1074,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 : @@ -819,19 +1086,38 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap) sprintf(text->value, "%d", text->number_value); } + if (gi->type & GD_TYPE_TEXT_BUTTON) + { + 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; + + gi->width = 2 * border_xsize + gi->textbutton.size * font_width; + gi->height = 2 * border_ysize + font_height; + } + 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; @@ -839,6 +1125,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, ...) @@ -855,7 +1161,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, ...) @@ -867,6 +1173,9 @@ struct GadgetInfo *CreateGadget(int first_tag, ...) new_gadget->id = getNewGadgetID(); 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); @@ -906,22 +1215,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) */ @@ -971,7 +1280,7 @@ static void MultiMapGadgets(int mode) static boolean map_state[MAX_NUM_GADGETS]; int map_count = 0; - while (gi) + while (gi != NULL) { if ((mode & MULTIMAP_PLAYFIELD && gi->x < gfx.sx + gfx.sxsize) || @@ -1009,7 +1318,12 @@ void RemapAllGadgets() boolean anyTextInputGadgetActive() { - return (last_gi && (last_gi->type & GD_TYPE_TEXTINPUT) && last_gi->mapped); + return (last_gi && (last_gi->type & GD_TYPE_TEXT_INPUT) && last_gi->mapped); +} + +boolean anyTextAreaGadgetActive() +{ + return (last_gi && (last_gi->type & GD_TYPE_TEXT_AREA) && last_gi->mapped); } boolean anySelectboxGadgetActive() @@ -1017,13 +1331,39 @@ 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() || anySelectboxGadgetActive()); + return (anyTextInputGadgetActive() || + anyTextAreaGadgetActive() || + 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); @@ -1035,19 +1375,25 @@ 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 int last_button = 0; static int last_mx = 0, last_my = 0; + static int pressed_mx = 0, pressed_my = 0; 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_moving; boolean gadget_moving_inside; boolean gadget_moving_off_borders; @@ -1060,7 +1406,14 @@ void HandleGadgets(int mx, int my, int button) /* 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) + { + mx = last_mx; + my = last_my; + } /* check which gadget is under the mouse pointer */ new_gi = getGadgetInfoFromMousePosition(mx, my); @@ -1075,77 +1428,69 @@ void HandleGadgets(int mx, int my, int button) last_mx = mx; last_my = my; - /* special treatment for text and number input gadgets */ - if (anyTextInputGadgetActive() && button != 0 && !motion_status) + if (press_event && new_gi != last_gi) { - struct GadgetInfo *gi = last_gi; - - if (new_gi == last_gi) - { - int old_cursor_position = gi->text.cursor_position; - - /* if mouse button pressed inside activated text gadget, set cursor */ - gi->text.cursor_position = - (mx - gi->x - gi->border.size) / getFontWidth(gi->text.font_type); + pressed_mx = mx; + pressed_my = my; + } - 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); + mouse_released_where_pressed = + (release_event && mx == pressed_mx && my == pressed_my); - if (gi->text.cursor_position != old_cursor_position) - DrawGadget(gi, DG_PRESSED, DG_DIRECT); - } - else - { - /* if mouse button pressed outside text input gadget, deactivate it */ - CheckRangeOfNumericInputGadget(gi); - DrawGadget(gi, DG_UNPRESSED, DG_DIRECT); + mouse_inside_select_line = insideSelectboxLine(new_gi, mx, my); + mouse_inside_select_area = insideSelectboxArea(new_gi, mx, my); - gi->event.type = GD_EVENT_TEXT_LEAVING; + gadget_pressed_off_borders = (press_event && new_gi != last_gi); - if (gi->event_mask & GD_EVENT_TEXT_LEAVING) - gi->callback_action(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)); - last_gi = NULL; - } - } - - /* special treatment for selectbox gadgets */ - if (anySelectboxGadgetActive() && button != 0 && !motion_status) + /* if mouse button pressed outside text or selectbox gadget, deactivate it */ +#if 1 + if (anyTextGadgetActive() && + (gadget_pressed_off_borders || + (gadget_pressed_inside_select_line && !mouse_inside_select_area))) +#else + if (anyTextGadgetActive() && + button != 0 && !motion_status && new_gi != last_gi) +#endif { struct GadgetInfo *gi = last_gi; + boolean gadget_changed = (gi->event_mask & GD_EVENT_TEXT_LEAVING); - if (new_gi == last_gi) + /* check if text gadget has changed its value */ + if (gi->type & GD_TYPE_TEXT_INPUT) { - int old_index = gi->selectbox.current_index; + CheckRangeOfNumericInputGadget(gi); - /* 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.size) / - getFontWidth(gi->selectbox.font_type); + if (strcmp(gi->textinput.value, gi->textinput.last_value) != 0) + strcpy(gi->textinput.last_value, gi->textinput.value); + else + gadget_changed = FALSE; + } - if (gi->selectbox.current_index < 0) - gi->selectbox.current_index = 0; - else if (gi->selectbox.current_index > gi->selectbox.num_values - 1) - gi->selectbox.current_index = gi->selectbox.num_values - 1; + /* selectbox does not change its value when closed by clicking outside */ + if (gi->type & GD_TYPE_SELECTBOX) + gadget_changed = FALSE; - if (gi->selectbox.current_index != old_index) - DrawGadget(gi, DG_PRESSED, DG_DIRECT); - } - 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; - gi->event.type = GD_EVENT_TEXT_LEAVING; +#if 1 + if (gadget_changed && !(gi->type & GD_TYPE_SELECTBOX)) + gi->callback_action(gi); +#else + if (gi->event_mask & GD_EVENT_TEXT_LEAVING) + gi->callback_action(gi); +#endif - if (gi->event_mask & GD_EVENT_TEXT_LEAVING) - gi->callback_action(gi); + last_gi = NULL; - last_gi = NULL; - } + if (gadget_pressed_inside_select_line) + new_gi = NULL; } gadget_pressed = @@ -1166,12 +1511,17 @@ void HandleGadgets(int mx, int my, int button) { struct GadgetInfo *gi = last_gi; +#if 1 + gadget_released_inside_select_line = insideSelectboxLine(gi, mx, my); + gadget_released_inside_select_area = insideSelectboxArea(gi, mx, my); +#else 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); + (mx >= gi->selectbox.x && mx < gi->selectbox.x + gi->selectbox.width && + my >= gi->selectbox.y && my < gi->selectbox.y + gi->selectbox.height); +#endif } else { @@ -1194,11 +1544,23 @@ 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 1 + if (gi->type & GD_TYPE_SELECTBOX && + (mouse_released_where_pressed || + !gadget_released_inside_select_area)) /* selectbox stays open */ + { + gi->selectbox.stay_open = TRUE; + pressed_mx = 0; + pressed_my = 0; + } +#else + if (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 */ +#endif + else if (!(gi->type & GD_TYPE_TEXT_INPUT || + gi->type & GD_TYPE_TEXT_AREA)) /* text input stays open */ last_gi = NULL; } @@ -1206,7 +1568,7 @@ void HandleGadgets(int mx, int my, int button) if (button == 0 && !release_event) gi = new_gi; - if (gi) + if (gi != NULL) { int last_x = gi->event.x; int last_y = gi->event.y; @@ -1222,15 +1584,44 @@ 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_SELECTBOX) + 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 && gi->selectbox.open) { int old_index = gi->selectbox.current_index; /* 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.size) / - getFontWidth(gi->selectbox.font_type); + (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font); if (gi->selectbox.current_index < 0) gi->selectbox.current_index = 0; @@ -1238,7 +1629,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); } } @@ -1255,15 +1646,6 @@ void HandleGadgets(int mx, int my, int button) { last_info_gi->event.type = GD_EVENT_INFO_LEAVING; last_info_gi->callback_info(last_info_gi); - -#if 0 - default_callback_info(NULL); - - printf("It seems that we are leaving gadget [%s]!\n", - (last_info_gi != NULL && - last_info_gi->info_text != NULL ? - last_info_gi->info_text : "")); -#endif } last_info_gi = new_gi; @@ -1287,7 +1669,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; @@ -1353,11 +1735,11 @@ void HandleGadgets(int mx, int my, int button) /* don't handle this scrollbar anymore while mouse button pressed */ last_gi = NULL; - return; + return TRUE; } } - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + DrawGadget(gi, DG_PRESSED, gi->direct_draw); gi->state = GD_BUTTON_PRESSED; gi->event.type = GD_EVENT_PRESSED; @@ -1385,9 +1767,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) { @@ -1396,8 +1778,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.size) / - getFontWidth(gi->selectbox.font_type); + (my - gi->selectbox.y - gi->border.ysize) / getFontHeight(gi->font); if (gi->selectbox.current_index < 0) gi->selectbox.current_index = 0; @@ -1405,7 +1786,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) { @@ -1420,7 +1801,7 @@ void HandleGadgets(int mx, int my, int button) gs->position = gs->position_max; gs->item_position = - gs->items_max * (gs->position + gs->correction) / gs->size_max; + gs->items_max * (gs->position + gs->correction) / gs->size_max_cmp; if (gs->item_position < 0) gs->item_position = 0; @@ -1433,7 +1814,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 ? @@ -1449,31 +1830,54 @@ 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 1 + if (mouse_released_where_pressed || + !gadget_released_inside_select_area) /* selectbox stays open */ + { + deactivate_gadget = FALSE; + gadget_changed = FALSE; + } +#else if (gadget_released_inside_select_line || - gadget_released_off_borders) /* selectbox stays open */ + gadget_released_off_borders) /* selectbox stays open */ + { deactivate_gadget = FALSE; - else + gadget_changed = FALSE; + } +#endif + else if (gi->selectbox.index != gi->selectbox.current_index) gi->selectbox.index = gi->selectbox.current_index; + else + gadget_changed = FALSE; } 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; +#if 1 + if ((gi->event_mask & GD_EVENT_RELEASED) && gadget_changed) + { + gi->callback_action(gi); + } +#else if ((gi->event_mask & GD_EVENT_RELEASED) && deactivate_gadget) gi->callback_action(gi); +#endif } 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; @@ -1481,73 +1885,189 @@ 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; + + return (gadget_pressed || gadget_pressed_repeated || + gadget_released || gadget_moving); } -void HandleGadgetsKeyInput(Key key) +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; + + strcpy(text, gi->textarea.value); + strcpy(&gi->textarea.value[cursor_position + 1], &text[cursor_position]); + gi->textarea.value[cursor_position] = c; + + setTextAreaCursorPosition(gi, gi->textarea.cursor_position + 1); +} + +boolean HandleGadgetsKeyInput(Key key) { struct GadgetInfo *gi = last_gi; if (gi == NULL || !gi->mapped || - !((gi->type & GD_TYPE_TEXTINPUT) || (gi->type & GD_TYPE_SELECTBOX))) - return; + !(gi->type & GD_TYPE_TEXT_INPUT || + gi->type & GD_TYPE_TEXT_AREA || + gi->type & GD_TYPE_SELECTBOX)) + return FALSE; if (key == KSYM_Return) /* valid for both text input and selectbox */ { - if (gi->type & GD_TYPE_TEXTINPUT) + boolean gadget_changed = (gi->event_mask & GD_EVENT_TEXT_RETURN); + + if (gi->type & GD_TYPE_TEXT_INPUT) + { CheckRangeOfNumericInputGadget(gi); + + if (strcmp(gi->textinput.value, gi->textinput.last_value) != 0) + 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) + { + insertCharIntoTextArea(gi, '\n'); + + DrawGadget(gi, DG_PRESSED, gi->direct_draw); + } + else + { + DrawGadget(gi, DG_UNPRESSED, gi->direct_draw); - DrawGadget(gi, DG_UNPRESSED, DG_DIRECT); + gi->event.type = GD_EVENT_TEXT_RETURN; - gi->event.type = GD_EVENT_TEXT_RETURN; + last_gi = NULL; + } +#if 1 + if (gadget_changed) + gi->callback_action(gi); +#else if (gi->event_mask & GD_EVENT_TEXT_RETURN) gi->callback_action(gi); - - last_gi = NULL; +#endif } - 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->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, gi->direct_draw); + } + else if (key == KSYM_Left && cursor_pos > 0) + { + gi->textinput.cursor_position--; + + DrawGadget(gi, DG_PRESSED, gi->direct_draw); + } + else if (key == KSYM_Right && cursor_pos < text_length) { - 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++; + gi->textinput.cursor_position++; - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + DrawGadget(gi, DG_PRESSED, gi->direct_draw); + } + else if (key == KSYM_BackSpace && cursor_pos > 0) + { + 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->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 (legal_letter) + { + insertCharIntoTextArea(gi, letter); + + 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); + setTextAreaCursorPosition(gi, gi->textarea.cursor_position - 1); + + 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); + 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->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->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->text.value); - strcpy(&gi->text.value[cursor_pos], &text[cursor_pos + 1]); - DrawGadget(gi, DG_PRESSED, DG_DIRECT); + 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 */ @@ -1558,12 +2078,16 @@ 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); } } + + return TRUE; }