summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-09-06 22:44:46 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-09-06 22:44:46 +0300
commit52a1652536e4e27751ac121009f85113e72afe7d (patch)
tree14738b1ee284e811b518c7726768b048c179607a
parent6bd9fbffec920c103d27bf780d60de314316bbd5 (diff)
Widget: API convenience; fixed position flag
-rw-r--r--src/ui/widget.c35
-rw-r--r--src/ui/widget.h19
2 files changed, 39 insertions, 15 deletions
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) {
137 137
138void setPos_Widget(iWidget *d, iInt2 pos) { 138void setPos_Widget(iWidget *d, iInt2 pos) {
139 d->rect.pos = pos; 139 d->rect.pos = pos;
140 setFlags_Widget(d, fixedPosition_WidgetFlag, iTrue);
140} 141}
141 142
142void setSize_Widget(iWidget *d, iInt2 size) { 143void setSize_Widget(iWidget *d, iInt2 size) {
@@ -322,6 +323,9 @@ void arrange_Widget(iWidget *d) {
322 iForEach(ObjectList, i, d->children) { 323 iForEach(ObjectList, i, d->children) {
323 iWidget *child = as_Widget(i.object); 324 iWidget *child = as_Widget(i.object);
324 arrange_Widget(child); 325 arrange_Widget(child);
326 if (child->flags & fixedPosition_WidgetFlag) {
327 continue;
328 }
325 if (d->flags & (arrangeHorizontal_WidgetFlag | arrangeVertical_WidgetFlag)) { 329 if (d->flags & (arrangeHorizontal_WidgetFlag | arrangeVertical_WidgetFlag)) {
326 if (child->flags & moveToParentRightEdge_WidgetFlag) { 330 if (child->flags & moveToParentRightEdge_WidgetFlag) {
327 continue; /* Not part of the sequential arrangement .*/ 331 continue; /* Not part of the sequential arrangement .*/
@@ -460,6 +464,12 @@ iBool dispatchEvent_Widget(iWidget *d, const SDL_Event *ev) {
460 continue; 464 continue;
461 } 465 }
462 if (dispatchEvent_Widget(child, ev)) { 466 if (dispatchEvent_Widget(child, ev)) {
467 if (ev->type == SDL_MOUSEBUTTONDOWN) {
468 printf("widget %p ('%s' class:%s) ate the mouse down\n",
469 child, cstr_String(id_Widget(child)),
470 class_Widget(child)->name);
471 fflush(stdout);
472 }
463 return iTrue; 473 return iTrue;
464 } 474 }
465 } 475 }
@@ -637,8 +647,9 @@ size_t childCount_Widget(const iWidget *d) {
637 return size_ObjectList(d->children); 647 return size_ObjectList(d->children);
638} 648}
639 649
640iBool isVisible_Widget(const iWidget *d) { 650iBool isVisible_Widget(const iAnyObject *d) {
641 if (!d) return iFalse; 651 if (!d) return iFalse;
652 iAssert(isInstance_Object(d, &Class_Widget));
642 for (const iWidget *w = d; w; w = w->parent) { 653 for (const iWidget *w = d; w; w = w->parent) {
643 if (w->flags & hidden_WidgetFlag) { 654 if (w->flags & hidden_WidgetFlag) {
644 return iFalse; 655 return iFalse;
@@ -647,7 +658,8 @@ iBool isVisible_Widget(const iWidget *d) {
647 return iTrue; 658 return iTrue;
648} 659}
649 660
650iBool isDisabled_Widget(const iWidget *d) { 661iBool isDisabled_Widget(const iAnyObject *d) {
662 iAssert(isInstance_Object(d, &Class_Widget));
651 for (const iWidget *w = d; w; w = w->parent) { 663 for (const iWidget *w = d; w; w = w->parent) {
652 if (w->flags & disabled_WidgetFlag) { 664 if (w->flags & disabled_WidgetFlag) {
653 return iTrue; 665 return iTrue;
@@ -656,16 +668,19 @@ iBool isDisabled_Widget(const iWidget *d) {
656 return iFalse; 668 return iFalse;
657} 669}
658 670
659iBool isFocused_Widget(const iWidget *d) { 671iBool isFocused_Widget(const iAnyObject *d) {
672 iAssert(isInstance_Object(d, &Class_Widget));
660 return rootData_.focus == d; 673 return rootData_.focus == d;
661} 674}
662 675
663iBool isHover_Widget(const iWidget *d) { 676iBool isHover_Widget(const iAnyObject *d) {
677 iAssert(isInstance_Object(d, &Class_Widget));
664 return rootData_.hover == d; 678 return rootData_.hover == d;
665} 679}
666 680
667iBool isSelected_Widget(const iWidget *d) { 681iBool isSelected_Widget(const iAnyObject *d) {
668 return d && (d->flags & selected_WidgetFlag) != 0; 682 iAssert(isInstance_Object(d, &Class_Widget));
683 return d && (flags_Widget(d) & selected_WidgetFlag) != 0;
669} 684}
670 685
671iBool equalWidget_Command(const char *cmd, const iWidget *widget, const char *checkCommand) { 686iBool equalWidget_Command(const char *cmd, const iWidget *widget, const char *checkCommand) {
@@ -764,7 +779,7 @@ iWidget *mouseGrab_Widget(void) {
764 return rootData_.mouseGrab; 779 return rootData_.mouseGrab;
765} 780}
766 781
767void postCommand_Widget(const iWidget *d, const char *cmd, ...) { 782void postCommand_Widget(const iAnyObject *d, const char *cmd, ...) {
768 iString str; 783 iString str;
769 init_String(&str); { 784 init_String(&str); {
770 va_list args; 785 va_list args;
@@ -778,14 +793,18 @@ void postCommand_Widget(const iWidget *d, const char *cmd, ...) {
778 remove_Block(&str.chars, 0, 1); 793 remove_Block(&str.chars, 0, 1);
779 } 794 }
780 if (!isGlobal) { 795 if (!isGlobal) {
796 iAssert(isInstance_Object(d, &Class_Widget));
781 appendFormat_String(&str, " ptr:%p", d); 797 appendFormat_String(&str, " ptr:%p", d);
782 } 798 }
783 postCommandString_App(&str); 799 postCommandString_App(&str);
784 deinit_String(&str); 800 deinit_String(&str);
785} 801}
786 802
787void refresh_Widget(const iWidget *d) { 803void refresh_Widget(const iAnyObject *d) {
788 /* TODO: Could be widget specific, if parts of the tree are cached. */ 804 /* TODO: Could be widget specific, if parts of the tree are cached. */
805 /* TODO: The visbuffer in DocumentWidget and ListWidget could be moved to be a general
806 purpose feature of Widget. */
807 iAssert(isInstance_Object(d, &Class_Widget));
789 iUnused(d); 808 iUnused(d);
790 postRefresh_App(); 809 postRefresh_App();
791} 810}
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 {
59 keepOnTop_WidgetFlag = iBit(13), /* gets events first; drawn last */ 59 keepOnTop_WidgetFlag = iBit(13), /* gets events first; drawn last */
60 mouseModal_WidgetFlag = iBit(14), /* eats all unprocessed mouse events */ 60 mouseModal_WidgetFlag = iBit(14), /* eats all unprocessed mouse events */
61 /* arrange behavior */ 61 /* arrange behavior */
62 fixedPosition_WidgetFlag = iBit(16),
62 arrangeHorizontal_WidgetFlag = iBit(17), /* arrange children horizontally */ 63 arrangeHorizontal_WidgetFlag = iBit(17), /* arrange children horizontally */
63 arrangeVertical_WidgetFlag = iBit(18), /* arrange children vertically */ 64 arrangeVertical_WidgetFlag = iBit(18), /* arrange children vertically */
64 arrangeWidth_WidgetFlag = iBit(19), /* area of children becomes parent size */ 65 arrangeWidth_WidgetFlag = iBit(19), /* area of children becomes parent size */
@@ -142,16 +143,20 @@ void draw_Widget (const iWidget *);
142void drawBackground_Widget(const iWidget *); 143void drawBackground_Widget(const iWidget *);
143void drawChildren_Widget (const iWidget *); 144void drawChildren_Widget (const iWidget *);
144 145
146iLocalDef int width_Widget(const iAnyObject *d) {
147 iAssert(isInstance_Object(d, &Class_Widget));
148 return ((const iWidget *) d)->rect.size.x;
149}
145iLocalDef iObjectList *children_Widget(iAnyObject *d) { 150iLocalDef iObjectList *children_Widget(iAnyObject *d) {
146 iAssert(isInstance_Object(d, &Class_Widget)); 151 iAssert(isInstance_Object(d, &Class_Widget));
147 return ((iWidget *) d)->children; 152 return ((iWidget *) d)->children;
148} 153}
149 154
150iBool isVisible_Widget (const iWidget *); 155iBool isVisible_Widget (const iAnyObject *);
151iBool isDisabled_Widget (const iWidget *); 156iBool isDisabled_Widget (const iAnyObject *);
152iBool isFocused_Widget (const iWidget *); 157iBool isFocused_Widget (const iAnyObject *);
153iBool isHover_Widget (const iWidget *); 158iBool isHover_Widget (const iAnyObject *);
154iBool isSelected_Widget (const iWidget *); 159iBool isSelected_Widget (const iAnyObject *);
155iBool isCommand_Widget (const iWidget *d, const SDL_Event *ev, const char *cmd); 160iBool isCommand_Widget (const iWidget *d, const SDL_Event *ev, const char *cmd);
156iBool hasParent_Widget (const iWidget *d, const iWidget *someParent); 161iBool hasParent_Widget (const iWidget *d, const iWidget *someParent);
157void setId_Widget (iWidget *, const char *id); 162void setId_Widget (iWidget *, const char *id);
@@ -172,8 +177,8 @@ size_t childIndex_Widget (const iWidget *, const iAnyObject *child); /* O(n)
172void arrange_Widget (iWidget *); 177void arrange_Widget (iWidget *);
173iBool dispatchEvent_Widget(iWidget *, const SDL_Event *); 178iBool dispatchEvent_Widget(iWidget *, const SDL_Event *);
174iBool processEvent_Widget (iWidget *, const SDL_Event *); 179iBool processEvent_Widget (iWidget *, const SDL_Event *);
175void postCommand_Widget (const iWidget *, const char *cmd, ...); 180void postCommand_Widget (const iAnyObject *, const char *cmd, ...);
176void refresh_Widget (const iWidget *); 181void refresh_Widget (const iAnyObject *);
177 182
178void setFocus_Widget (iWidget *); 183void setFocus_Widget (iWidget *);
179iWidget *focus_Widget (void); 184iWidget *focus_Widget (void);