From 52a1652536e4e27751ac121009f85113e72afe7d Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Sun, 6 Sep 2020 22:44:46 +0300 Subject: Widget: API convenience; fixed position flag --- src/ui/widget.c | 35 +++++++++++++++++++++++++++-------- src/ui/widget.h | 19 ++++++++++++------- 2 files changed, 39 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/ui/widget.c b/src/ui/widget.c index 11f1f2c0..c4003254 100644 --- a/src/ui/widget.c +++ b/src/ui/widget.c @@ -137,6 +137,7 @@ void setFlags_Widget(iWidget *d, int flags, iBool set) { void setPos_Widget(iWidget *d, iInt2 pos) { d->rect.pos = pos; + setFlags_Widget(d, fixedPosition_WidgetFlag, iTrue); } void setSize_Widget(iWidget *d, iInt2 size) { @@ -322,6 +323,9 @@ void arrange_Widget(iWidget *d) { iForEach(ObjectList, i, d->children) { iWidget *child = as_Widget(i.object); arrange_Widget(child); + if (child->flags & fixedPosition_WidgetFlag) { + continue; + } if (d->flags & (arrangeHorizontal_WidgetFlag | arrangeVertical_WidgetFlag)) { if (child->flags & moveToParentRightEdge_WidgetFlag) { continue; /* Not part of the sequential arrangement .*/ @@ -460,6 +464,12 @@ iBool dispatchEvent_Widget(iWidget *d, const SDL_Event *ev) { continue; } if (dispatchEvent_Widget(child, ev)) { + if (ev->type == SDL_MOUSEBUTTONDOWN) { + printf("widget %p ('%s' class:%s) ate the mouse down\n", + child, cstr_String(id_Widget(child)), + class_Widget(child)->name); + fflush(stdout); + } return iTrue; } } @@ -637,8 +647,9 @@ size_t childCount_Widget(const iWidget *d) { return size_ObjectList(d->children); } -iBool isVisible_Widget(const iWidget *d) { +iBool isVisible_Widget(const iAnyObject *d) { if (!d) return iFalse; + iAssert(isInstance_Object(d, &Class_Widget)); for (const iWidget *w = d; w; w = w->parent) { if (w->flags & hidden_WidgetFlag) { return iFalse; @@ -647,7 +658,8 @@ iBool isVisible_Widget(const iWidget *d) { return iTrue; } -iBool isDisabled_Widget(const iWidget *d) { +iBool isDisabled_Widget(const iAnyObject *d) { + iAssert(isInstance_Object(d, &Class_Widget)); for (const iWidget *w = d; w; w = w->parent) { if (w->flags & disabled_WidgetFlag) { return iTrue; @@ -656,16 +668,19 @@ iBool isDisabled_Widget(const iWidget *d) { return iFalse; } -iBool isFocused_Widget(const iWidget *d) { +iBool isFocused_Widget(const iAnyObject *d) { + iAssert(isInstance_Object(d, &Class_Widget)); return rootData_.focus == d; } -iBool isHover_Widget(const iWidget *d) { +iBool isHover_Widget(const iAnyObject *d) { + iAssert(isInstance_Object(d, &Class_Widget)); return rootData_.hover == d; } -iBool isSelected_Widget(const iWidget *d) { - return d && (d->flags & selected_WidgetFlag) != 0; +iBool isSelected_Widget(const iAnyObject *d) { + iAssert(isInstance_Object(d, &Class_Widget)); + return d && (flags_Widget(d) & selected_WidgetFlag) != 0; } iBool equalWidget_Command(const char *cmd, const iWidget *widget, const char *checkCommand) { @@ -764,7 +779,7 @@ iWidget *mouseGrab_Widget(void) { return rootData_.mouseGrab; } -void postCommand_Widget(const iWidget *d, const char *cmd, ...) { +void postCommand_Widget(const iAnyObject *d, const char *cmd, ...) { iString str; init_String(&str); { va_list args; @@ -778,14 +793,18 @@ void postCommand_Widget(const iWidget *d, const char *cmd, ...) { remove_Block(&str.chars, 0, 1); } if (!isGlobal) { + iAssert(isInstance_Object(d, &Class_Widget)); appendFormat_String(&str, " ptr:%p", d); } postCommandString_App(&str); deinit_String(&str); } -void refresh_Widget(const iWidget *d) { +void refresh_Widget(const iAnyObject *d) { /* TODO: Could be widget specific, if parts of the tree are cached. */ + /* TODO: The visbuffer in DocumentWidget and ListWidget could be moved to be a general + purpose feature of Widget. */ + iAssert(isInstance_Object(d, &Class_Widget)); iUnused(d); postRefresh_App(); } diff --git a/src/ui/widget.h b/src/ui/widget.h index b1b7a1d7..25208c30 100644 --- a/src/ui/widget.h +++ b/src/ui/widget.h @@ -59,6 +59,7 @@ enum iWidgetFlag { keepOnTop_WidgetFlag = iBit(13), /* gets events first; drawn last */ mouseModal_WidgetFlag = iBit(14), /* eats all unprocessed mouse events */ /* arrange behavior */ + fixedPosition_WidgetFlag = iBit(16), arrangeHorizontal_WidgetFlag = iBit(17), /* arrange children horizontally */ arrangeVertical_WidgetFlag = iBit(18), /* arrange children vertically */ arrangeWidth_WidgetFlag = iBit(19), /* area of children becomes parent size */ @@ -142,16 +143,20 @@ void draw_Widget (const iWidget *); void drawBackground_Widget(const iWidget *); void drawChildren_Widget (const iWidget *); +iLocalDef int width_Widget(const iAnyObject *d) { + iAssert(isInstance_Object(d, &Class_Widget)); + return ((const iWidget *) d)->rect.size.x; +} iLocalDef iObjectList *children_Widget(iAnyObject *d) { iAssert(isInstance_Object(d, &Class_Widget)); return ((iWidget *) d)->children; } -iBool isVisible_Widget (const iWidget *); -iBool isDisabled_Widget (const iWidget *); -iBool isFocused_Widget (const iWidget *); -iBool isHover_Widget (const iWidget *); -iBool isSelected_Widget (const iWidget *); +iBool isVisible_Widget (const iAnyObject *); +iBool isDisabled_Widget (const iAnyObject *); +iBool isFocused_Widget (const iAnyObject *); +iBool isHover_Widget (const iAnyObject *); +iBool isSelected_Widget (const iAnyObject *); iBool isCommand_Widget (const iWidget *d, const SDL_Event *ev, const char *cmd); iBool hasParent_Widget (const iWidget *d, const iWidget *someParent); void setId_Widget (iWidget *, const char *id); @@ -172,8 +177,8 @@ size_t childIndex_Widget (const iWidget *, const iAnyObject *child); /* O(n) void arrange_Widget (iWidget *); iBool dispatchEvent_Widget(iWidget *, const SDL_Event *); iBool processEvent_Widget (iWidget *, const SDL_Event *); -void postCommand_Widget (const iWidget *, const char *cmd, ...); -void refresh_Widget (const iWidget *); +void postCommand_Widget (const iAnyObject *, const char *cmd, ...); +void refresh_Widget (const iAnyObject *); void setFocus_Widget (iWidget *); iWidget *focus_Widget (void); -- cgit v1.2.3