if (gi->mapped &&
mx >= gi->x && mx < gi->x + gi->width &&
my >= gi->y && my < gi->y + gi->height)
- break;
+ {
+
+#if 0
+ if (gi->type & GD_TYPE_SCROLLBAR)
+ {
+ int mpos, gpos;
+
+ if (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL)
+ {
+ mpos = mx;
+ gpos = gi->x;
+ }
+ else
+ {
+ mpos = my;
+ gpos = gi->y;
+ }
+
+ if (mpos >= gpos + gi->scrollbar.position &&
+ mpos < gpos + gi->scrollbar.position + gi->scrollbar.size)
+ break;
+ }
+ else
+#endif
+
+ break;
+ }
gi = gi->next;
}
break;
case GDI_TEXT_VALUE:
- strcpy(new_gadget->text_value, va_arg(ap, char *));
+ {
+ int max_textsize = MAX_GADGET_TEXTSIZE;
+
+ if (new_gadget->text_size)
+ max_textsize = MIN(new_gadget->text_size, MAX_GADGET_TEXTSIZE - 1);
+
+ strncpy(new_gadget->text_value, va_arg(ap, char *), max_textsize);
+ new_gadget->text_value[max_textsize] = '\0';
+ }
+ break;
+
+ case GDI_TEXT_SIZE:
+ {
+ int tag_value = va_arg(ap, int);
+ int max_textsize = MIN(tag_value, MAX_GADGET_TEXTSIZE - 1);
+
+ new_gadget->text_size = max_textsize;
+ new_gadget->text_value[max_textsize] = '\0';
+
+ if (new_gadget->width == 0 && new_gadget->height == 0)
+ {
+ new_gadget->width = (new_gadget->text_size + 1) * FONT2_XSIZE + 6;
+ new_gadget->height = ED_WIN_COUNT_YSIZE;
+ }
+ }
break;
case GDI_DESIGN_UNPRESSED:
new_gadget->alt_design[GD_BUTTON_PRESSED].y = va_arg(ap, int);
break;
+ case GDI_DESIGN_BORDER:
+ new_gadget->design_border = va_arg(ap, int);
+ break;
+
case GDI_EVENT_MASK:
new_gadget->event_mask = va_arg(ap, unsigned long);
break;
}
break;
+ case GDI_SCROLLBAR_ITEMS_MAX:
+ new_gadget->scrollbar.items_max = va_arg(ap, int);
+ break;
+
+ case GDI_SCROLLBAR_ITEMS_VISIBLE:
+ new_gadget->scrollbar.items_visible = va_arg(ap, int);
+ break;
+
+ case GDI_SCROLLBAR_ITEM_POSITION:
+ new_gadget->scrollbar.item_position = va_arg(ap, int);
+ break;
+
case GDI_CALLBACK:
new_gadget->callback = va_arg(ap, gadget_callback_function);
break;
!new_gadget->design[GD_BUTTON_PRESSED].pixmap))
Error(ERR_EXIT, "gadget incomplete (missing Pixmap)");
- /* insert new gadget into gloabl gadget list */
+ if (new_gadget->type & GD_TYPE_SCROLLBAR)
+ {
+ struct GadgetScrollbar *gs = &new_gadget->scrollbar;
+
+ if (new_gadget->width == 0 || new_gadget->height == 0 ||
+ gs->items_max == 0 || gs->items_visible == 0)
+ Error(ERR_EXIT, "scrollbar gadget incomplete (missing tags)");
+
+ /* calculate internal scrollbar values */
+ gs->size_max = (new_gadget->type == GD_TYPE_SCROLLBAR_VERTICAL ?
+ new_gadget->height : new_gadget->width);
+ gs->size = gs->size_max * gs->items_visible / gs->items_max;
+ gs->position = gs->size_max * gs->item_position / gs->items_max;
+ gs->position_max = gs->size_max - gs->size;
+ }
+ /* insert new gadget into global gadget list */
if (gadget_list_last_entry)
{
gadget_list_last_entry->next = new_gadget;
gadget_list_last_entry = gadget_list_last_entry->next;
}
else
- {
gadget_list_first_entry = gadget_list_last_entry = new_gadget;
- }
return new_gadget;
}
&gi->alt_design[state] :
&gi->design[state]);
- if (gi->type != GD_TYPE_NORMAL_BUTTON &&
- gi->type != GD_TYPE_RADIO_BUTTON)
- return;
+ switch (gi->type)
+ {
+ case GD_TYPE_NORMAL_BUTTON:
+ case GD_TYPE_RADIO_BUTTON:
+ XCopyArea(display, gd->pixmap, drawto, gc,
+ gd->x, gd->y, gi->width, gi->height, gi->x, gi->y);
+ break;
+
+ case GD_TYPE_TEXTINPUT:
+ {
+ int i;
+
+ /* left part of gadget */
+ XCopyArea(display, gd->pixmap, drawto, gc,
+ gd->x, gd->y,
+ gi->design_border, gi->height,
+ gi->x, gi->y);
+
+ /* middle part of gadget */
+ for (i=0; i<=gi->text_size; i++)
+ XCopyArea(display, gd->pixmap, drawto, gc,
+ gd->x + gi->design_border, gd->y,
+ FONT2_XSIZE, gi->height,
+ gi->x + gi->design_border + i * FONT2_XSIZE, gi->y);
+
+ /* right part of gadget */
+ XCopyArea(display, gd->pixmap, drawto, gc,
+ gd->x + ED_WIN_COUNT_XSIZE - gi->design_border, gd->y,
+ gi->design_border, gi->height,
+ gi->x + gi->width - gi->design_border, gi->y);
+
+ /* gadget text value */
+ DrawText(gi->x + gi->design_border, gi->y + gi->design_border,
+ gi->text_value, FS_SMALL, (pressed ? FC_GREEN : FC_YELLOW));
+
+ /* draw cursor, if active */
+ DrawText(gi->x + gi->design_border +
+ strlen(gi->text_value) * FONT2_XSIZE,
+ gi->y + gi->design_border,
+ (pressed ? "<" : " "),
+ FS_SMALL, FC_RED);
+ }
+ break;
- XCopyArea(display, gd->pixmap, drawto, gc,
- gd->x, gd->y, gi->width, gi->height, gi->x, gi->y);
+ case GD_TYPE_SCROLLBAR_VERTICAL:
+ {
+ int i;
+ int xpos = gi->x;
+ int ypos = gi->y + gi->scrollbar.position;
+ int design_full = gi->width;
+ int design_body = design_full - 2 * gi->design_border;
+ int size_full = gi->scrollbar.size;
+ int size_body = size_full - 2 * gi->design_border;
+ int num_steps = size_body / design_body;
+ int step_size_remain = size_body - num_steps * design_body;
+
+ /* clear scrollbar area */
+ XFillRectangle(display, backbuffer, gc,
+ gi->x, gi->y, gi->width, gi->height);
+
+ /* upper part of gadget */
+ XCopyArea(display, gd->pixmap, drawto, gc,
+ gd->x, gd->y,
+ gi->width, gi->design_border,
+ xpos, ypos);
+
+ /* middle part of gadget */
+ for (i=0; i<num_steps; i++)
+ XCopyArea(display, gd->pixmap, drawto, gc,
+ gd->x, gd->y + gi->design_border,
+ gi->width, design_body,
+ xpos, ypos + gi->design_border + i * design_body);
+
+ /* remaining middle part of gadget */
+ if (step_size_remain > 0)
+ XCopyArea(display, gd->pixmap, drawto, gc,
+ gd->x, gd->y + gi->design_border,
+ gi->width, step_size_remain,
+ xpos, ypos + gi->design_border + num_steps * design_body);
+
+ /* lower part of gadget */
+ XCopyArea(display, gd->pixmap, drawto, gc,
+ gd->x, gd->y + design_full - gi->design_border,
+ gi->width, gi->design_border,
+ xpos, ypos + size_full - gi->design_border);
+ }
+ break;
+
+ case GD_TYPE_SCROLLBAR_HORIZONTAL:
+ {
+ int i;
+ int xpos = gi->x + gi->scrollbar.position;
+ int ypos = gi->y;
+ int design_full = gi->height;
+ int design_body = design_full - 2 * gi->design_border;
+ int size_full = gi->scrollbar.size;
+ int size_body = size_full - 2 * gi->design_border;
+ int num_steps = size_body / design_body;
+ int step_size_remain = size_body - num_steps * design_body;
+
+ /* clear scrollbar area */
+ XFillRectangle(display, backbuffer, gc,
+ gi->x, gi->y, gi->width, gi->height);
+
+ /* left part of gadget */
+ XCopyArea(display, gd->pixmap, drawto, gc,
+ gd->x, gd->y,
+ gi->design_border, gi->height,
+ xpos, ypos);
+
+ /* middle part of gadget */
+ for (i=0; i<num_steps; i++)
+ XCopyArea(display, gd->pixmap, drawto, gc,
+ gd->x + gi->design_border, gd->y,
+ design_body, gi->height,
+ xpos + gi->design_border + i * design_body, ypos);
+
+ /* remaining middle part of gadget */
+ if (step_size_remain > 0)
+ XCopyArea(display, gd->pixmap, drawto, gc,
+ gd->x + gi->design_border, gd->y,
+ step_size_remain, gi->height,
+ xpos + gi->design_border + num_steps * design_body, ypos);
+
+ /* right part of gadget */
+ XCopyArea(display, gd->pixmap, drawto, gc,
+ gd->x + design_full - gi->design_border, gd->y,
+ gi->design_border, gi->height,
+ xpos + size_full - gi->design_border, ypos);
+ }
+ break;
+
+ default:
+ return;
+ }
if (direct)
XCopyArea(display, drawto, window, gc,
HandleGadgets(gi->x, gi->y, 0);
}
+void AdjustScrollbar(struct GadgetInfo *gi, int items_max, int item_pos)
+{
+ struct GadgetScrollbar *gs = &gi->scrollbar;
+
+ gs->items_max = items_max;
+ gs->item_position = item_pos;
+
+ gs->size = gs->size_max * gs->items_visible / gs->items_max;
+ gs->position = gs->size_max * gs->item_position / gs->items_max;
+ gs->position_max = gs->size_max - gs->size;
+
+ /* finetuning for maximal right/bottom position */
+ if (gs->item_position == gs->items_max - gs->items_visible)
+ gs->position = gs->position_max;
+
+ printf("gs->item_position == %d\n", gs->item_position);
+
+ if (gi->mapped)
+ DrawGadget(gi, DG_UNPRESSED, DG_DIRECT);
+}
+
void MapGadget(struct GadgetInfo *gi)
{
if (gi == NULL)
gi->mapped = FALSE;
}
+static struct GadgetInfo *last_gi = NULL;
+
void HandleGadgets(int mx, int my, int button)
{
- static struct GadgetInfo *last_gi = NULL;
static unsigned long pressed_delay = 0;
-
+ static last_button = 0;
+ static last_mx = 0, last_my = 0;
+ int scrollbar_mouse_pos;
struct GadgetInfo *new_gi, *gi;
+ boolean press_event;
+ boolean release_event;
+ boolean mouse_moving;
boolean gadget_pressed;
boolean gadget_pressed_repeated;
boolean gadget_moving;
+ boolean gadget_moving_inside;
boolean gadget_moving_off_borders;
boolean gadget_released;
+ boolean gadget_released_inside;
boolean gadget_released_off_borders;
+ /* check if there are any gadgets defined */
if (gadget_list_first_entry == NULL)
return;
+ /* check which gadget is under the mouse pointer */
new_gi = getGadgetInfoFromMousePosition(mx, my);
+ /* check if button state has changed since last invocation */
+ press_event = (button != 0 && last_button == 0);
+ release_event = (button == 0 && last_button != 0);
+ last_button = button;
+
+ /* check if mouse has been moved since last invocation */
+ mouse_moving = ((mx != last_mx || my != last_my) && motion_status);
+ last_mx = mx;
+ last_my = my;
+
+ /* if mouse button pressed outside text input gadget, deactivate it */
+ if (last_gi && last_gi->type == GD_TYPE_TEXTINPUT &&
+ button != 0 && new_gi != last_gi && !motion_status)
+ {
+ struct GadgetInfo *gi = last_gi;
+
+ DrawGadget(gi, DG_UNPRESSED, DG_DIRECT);
+
+ if (gi->event_mask & GD_EVENT_TEXT_LEAVING)
+ gi->callback(gi);
+
+ last_gi = NULL;
+ }
+
gadget_pressed =
- (button != 0 && last_gi == NULL && new_gi != NULL && !motion_status);
+ (button != 0 && last_gi == NULL && new_gi != NULL && press_event);
gadget_pressed_repeated =
(button != 0 && last_gi != NULL && new_gi == last_gi);
- gadget_moving =
- (button != 0 && last_gi != NULL && new_gi == last_gi && motion_status);
- gadget_moving_off_borders =
- (button != 0 && last_gi != NULL && new_gi != last_gi && motion_status);
- gadget_released =
- (button == 0 && last_gi != NULL && new_gi == last_gi);
- gadget_released_off_borders =
- (button == 0 && last_gi != NULL && new_gi != last_gi);
+ gadget_released = (button == 0 && last_gi != NULL);
+ gadget_released_inside = (gadget_released && new_gi == last_gi);
+ gadget_released_off_borders = (gadget_released && new_gi != last_gi);
+
+ gadget_moving = (button != 0 && last_gi != NULL && mouse_moving);
+ gadget_moving_inside = (gadget_moving && new_gi == last_gi);
+ gadget_moving_off_borders = (gadget_moving && new_gi != last_gi);
+
+ /* if new gadget pressed, store this gadget */
if (gadget_pressed)
last_gi = new_gi;
+ /* 'gi' is actually handled gadget */
gi = last_gi;
- if (button == 0)
+ /* if gadget is scrollbar, choose mouse position value */
+ if (gi && gi->type & GD_TYPE_SCROLLBAR)
+ scrollbar_mouse_pos =
+ (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL ? mx - gi->x : my - gi->y);
+
+ /* if mouse button released, no gadget needs to be handled anymore */
+ if (button == 0 && last_gi && last_gi->type != GD_TYPE_TEXTINPUT)
last_gi = NULL;
if (gi)
gi->radio_pressed = TRUE;
}
+ else if (gi->type & GD_TYPE_SCROLLBAR)
+ {
+ int mpos, gpos;
+
+ if (gi->type == GD_TYPE_SCROLLBAR_HORIZONTAL)
+ {
+ mpos = mx;
+ gpos = gi->x;
+ }
+ else
+ {
+ mpos = my;
+ gpos = gi->y;
+ }
+
+ if (mpos >= gpos + gi->scrollbar.position &&
+ mpos < gpos + gi->scrollbar.position + gi->scrollbar.size)
+ {
+ /* drag scrollbar */
+ gi->scrollbar.drag_position =
+ scrollbar_mouse_pos - gi->scrollbar.position;
+ }
+ else
+ {
+ /* click scrollbar one scrollbar length up/left or down/right */
+
+ struct GadgetScrollbar *gs = &gi->scrollbar;
+ int old_item_position = gs->item_position;
+ boolean changed_position = FALSE;
+
+ gs->item_position +=
+ gs->items_visible * (mpos < gpos + gi->scrollbar.position ? -1 : +1);
+
+ if (gs->item_position < 0)
+ gs->item_position = 0;
+ if (gs->item_position > gs->items_max - gs->items_visible)
+ gs->item_position = gs->items_max - gs->items_visible;
+
+ if (old_item_position != gs->item_position)
+ {
+ gi->event.item_position = gs->item_position;
+ changed_position = TRUE;
+
+ printf("gs->item_position == %d\n", gs->item_position);
+ }
+
+ AdjustScrollbar(gi, gs->items_max, gs->item_position);
+
+ gi->state = GD_BUTTON_UNPRESSED;
+ gi->event.type = GD_EVENT_MOVING;
+ gi->event.off_borders = FALSE;
+
+ if (gi->event_mask & GD_EVENT_MOVING && changed_position)
+ gi->callback(gi);
+
+ /* don't handle this scrollbar anymore while mouse button pressed */
+ last_gi = NULL;
+
+ return;
+ }
+ }
DrawGadget(gi, DG_PRESSED, DG_DIRECT);
if (gadget_moving)
{
- if (gi->state == GD_BUTTON_UNPRESSED)
- DrawGadget(gi, DG_PRESSED, DG_DIRECT);
+ boolean changed_position = FALSE;
- gi->state = GD_BUTTON_PRESSED;
- gi->event.type = GD_EVENT_MOVING;
- gi->event.off_borders = FALSE;
- if (gi->event_mask & GD_EVENT_MOVING)
- gi->callback(gi);
- }
+ if (gi->type & GD_TYPE_BUTTON)
+ {
+ if (gadget_moving_inside && gi->state == GD_BUTTON_UNPRESSED)
+ DrawGadget(gi, DG_PRESSED, DG_DIRECT);
+ else if (gadget_moving_off_borders && gi->state == GD_BUTTON_PRESSED)
+ DrawGadget(gi, DG_UNPRESSED, DG_DIRECT);
+ }
- if (gadget_moving_off_borders)
- {
- if (gi->state == GD_BUTTON_PRESSED)
- DrawGadget(gi, DG_UNPRESSED, DG_DIRECT);
+ if (gi->type & GD_TYPE_SCROLLBAR)
+ {
+ struct GadgetScrollbar *gs = &gi->scrollbar;
+ int old_item_position = gs->item_position;
- gi->state = GD_BUTTON_UNPRESSED;
+ gs->position = scrollbar_mouse_pos - gs->drag_position;
+
+ if (gs->position < 0)
+ gs->position = 0;
+ if (gs->position > gs->position_max)
+ gs->position = gs->position_max;
+
+ gs->item_position = gs->items_max * gs->position / gs->size_max;
+
+ if (old_item_position != gs->item_position)
+ {
+ gi->event.item_position = gs->item_position;
+ changed_position = TRUE;
+
+ printf("gs->item_position == %d\n", gs->item_position);
+ }
+
+ DrawGadget(gi, DG_PRESSED, DG_DIRECT);
+ }
+
+ gi->state = (gadget_moving_inside || gi->type & GD_TYPE_SCROLLBAR ?
+ GD_BUTTON_PRESSED : GD_BUTTON_UNPRESSED);
gi->event.type = GD_EVENT_MOVING;
- gi->event.off_borders = TRUE;
+ gi->event.off_borders = gadget_moving_off_borders;
- if (gi->event_mask & GD_EVENT_MOVING &&
- gi->event_mask & GD_EVENT_OFF_BORDERS)
+ if (gi->event_mask & GD_EVENT_MOVING && changed_position &&
+ (gadget_moving_inside || gi->event_mask & GD_EVENT_OFF_BORDERS))
gi->callback(gi);
}
- if (gadget_released)
+ if (gadget_released_inside)
{
- DrawGadget(gi, DG_UNPRESSED, DG_DIRECT);
+ if (gi->type != GD_TYPE_TEXTINPUT)
+ DrawGadget(gi, DG_UNPRESSED, DG_DIRECT);
gi->state = GD_BUTTON_UNPRESSED;
gi->event.type = GD_EVENT_RELEASED;
if (gadget_released_off_borders)
{
+ if (gi->type & GD_TYPE_SCROLLBAR)
+ DrawGadget(gi, DG_UNPRESSED, DG_DIRECT);
+
gi->event.type = GD_EVENT_RELEASED;
if (gi->event_mask & GD_EVENT_RELEASED &&
gi->callback(gi);
}
}
+
+void HandleGadgetsKeyInput(KeySym key)
+{
+ struct GadgetInfo *gi = last_gi;
+ int text_length;
+ char letter;
+
+ if (gi == NULL || gi->type != GD_TYPE_TEXTINPUT)
+ return;
+
+ text_length = strlen(gi->text_value);
+ letter = getCharFromKeySym(key);
+
+ if (letter && text_length < gi->text_size)
+ {
+ gi->text_value[text_length] = letter;
+ gi->text_value[text_length + 1] = '\0';
+ DrawGadget(gi, DG_PRESSED, DG_DIRECT);
+ }
+ else if ((key == XK_Delete || key == XK_BackSpace) && text_length > 0)
+ {
+ gi->text_value[text_length - 1] = '\0';
+ DrawGadget(gi, DG_PRESSED, DG_DIRECT);
+ }
+ else if (key == XK_Return)
+ {
+ DrawGadget(gi, DG_UNPRESSED, DG_DIRECT);
+
+ if (gi->event_mask & GD_EVENT_TEXT_RETURN)
+ gi->callback(gi);
+
+ last_gi = NULL;
+ }
+}