diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-09-20 13:51:21 +0300 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-09-20 13:51:21 +0300 |
commit | 1323d511ad8f0c2697a8a933c721087197224072 (patch) | |
tree | c6dc47a04ce2961438bf616f30c83b5293aad8fd /src/ui | |
parent | b504d777ac8e134d7223372cb04468cf1895d3ae (diff) |
Centering widgets and sheet contents
Added a new arrange flag for centering a child horizontally.
The two-column layout in Preferences has expanding vertical paddings so the page contents are vertically centered.
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/util.c | 34 | ||||
-rw-r--r-- | src/ui/widget.c | 21 | ||||
-rw-r--r-- | src/ui/widget.h | 6 |
3 files changed, 49 insertions, 12 deletions
diff --git a/src/ui/util.c b/src/ui/util.c index a47b33c4..51c83a8b 100644 --- a/src/ui/util.c +++ b/src/ui/util.c | |||
@@ -474,9 +474,13 @@ iWidget *removeTabPage_Widget(iWidget *tabs, size_t index) { | |||
474 | void resizeToLargestPage_Widget(iWidget *tabs) { | 474 | void resizeToLargestPage_Widget(iWidget *tabs) { |
475 | arrange_Widget(tabs); | 475 | arrange_Widget(tabs); |
476 | iInt2 largest = zero_I2(); | 476 | iInt2 largest = zero_I2(); |
477 | iConstForEach(ObjectList, i, children_Widget(findChild_Widget(tabs, "tabs.pages"))) { | 477 | iWidget *pages = findChild_Widget(tabs, "tabs.pages"); |
478 | iConstForEach(ObjectList, i, children_Widget(pages)) { | ||
478 | largest = max_I2(largest, ((const iWidget *) i.object)->rect.size); | 479 | largest = max_I2(largest, ((const iWidget *) i.object)->rect.size); |
479 | } | 480 | } |
481 | iForEach(ObjectList, j, children_Widget(pages)) { | ||
482 | setSize_Widget(j.object, largest); | ||
483 | } | ||
480 | setSize_Widget(tabs, addY_I2(largest, height_Widget(findChild_Widget(tabs, "tabs.buttons")))); | 484 | setSize_Widget(tabs, addY_I2(largest, height_Widget(findChild_Widget(tabs, "tabs.buttons")))); |
481 | } | 485 | } |
482 | 486 | ||
@@ -602,16 +606,17 @@ iWidget *makeSheet_Widget(const char *id) { | |||
602 | mouseModal_WidgetFlag | keepOnTop_WidgetFlag | arrangeVertical_WidgetFlag | | 606 | mouseModal_WidgetFlag | keepOnTop_WidgetFlag | arrangeVertical_WidgetFlag | |
603 | arrangeSize_WidgetFlag, | 607 | arrangeSize_WidgetFlag, |
604 | iTrue); | 608 | iTrue); |
609 | setFlags2_Widget(sheet, centerHorizontal_WidgetFlag2, iTrue); | ||
605 | return sheet; | 610 | return sheet; |
606 | } | 611 | } |
607 | 612 | ||
608 | void centerSheet_Widget(iWidget *sheet) { | 613 | void centerSheet_Widget(iWidget *sheet) { |
609 | arrange_Widget(sheet); | 614 | arrange_Widget(sheet->parent); |
610 | const iInt2 rootSize = rootSize_Window(get_Window()); | 615 | // const iInt2 rootSize = rootSize_Window(get_Window()); |
611 | const iInt2 orig = localCoord_Widget( | 616 | // const iInt2 orig = localCoord_Widget( |
612 | sheet->parent, | 617 | // sheet->parent, |
613 | init_I2(rootSize.x / 2 - sheet->rect.size.x / 2, bounds_Widget(sheet).pos.y)); | 618 | // init_I2(rootSize.x / 2 - sheet->rect.size.x / 2, bounds_Widget(sheet).pos.y)); |
614 | sheet->rect.pos = orig; | 619 | // sheet->rect.pos = orig; |
615 | postRefresh_App(); | 620 | postRefresh_App(); |
616 | } | 621 | } |
617 | 622 | ||
@@ -815,12 +820,17 @@ iWidget *makeToggle_Widget(const char *id) { | |||
815 | static iWidget *appendTwoColumnPage_(iWidget *tabs, const char *title, iWidget **headings, | 820 | static iWidget *appendTwoColumnPage_(iWidget *tabs, const char *title, iWidget **headings, |
816 | iWidget **values) { | 821 | iWidget **values) { |
817 | iWidget *page = new_Widget(); | 822 | iWidget *page = new_Widget(); |
818 | appendTabPage_Widget(tabs, page, title, 0, 0); | 823 | setFlags_Widget(page, arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag | |
819 | setFlags_Widget(page, arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag, iTrue); | 824 | resizeHeightOfChildren_WidgetFlag, iTrue); |
825 | addChildFlags_Widget(page, iClob(new_Widget()), expand_WidgetFlag); | ||
826 | iWidget *columns = new_Widget(); | ||
827 | addChildFlags_Widget(page, iClob(columns), arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag); | ||
820 | *headings = addChildFlags_Widget( | 828 | *headings = addChildFlags_Widget( |
821 | page, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag); | 829 | columns, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag); |
822 | *values = addChildFlags_Widget( | 830 | *values = addChildFlags_Widget( |
823 | page, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag); | 831 | columns, iClob(new_Widget()), arrangeVertical_WidgetFlag | arrangeSize_WidgetFlag); |
832 | addChildFlags_Widget(page, iClob(new_Widget()), expand_WidgetFlag); | ||
833 | appendTabPage_Widget(tabs, page, title, 0, 0); | ||
824 | return page; | 834 | return page; |
825 | } | 835 | } |
826 | 836 | ||
@@ -831,7 +841,7 @@ static void makeTwoColumnHeading_(const char *title, iWidget *headings, iWidget | |||
831 | } | 841 | } |
832 | 842 | ||
833 | static void expandInputFieldWidth_(iInputWidget *input) { | 843 | static void expandInputFieldWidth_(iInputWidget *input) { |
834 | iWidget *page = as_Widget(input)->parent->parent->parent; /* tabs > page > values > input */ | 844 | iWidget *page = as_Widget(input)->parent->parent->parent->parent; /* tabs > page > values > input */ |
835 | as_Widget(input)->rect.size.x = | 845 | as_Widget(input)->rect.size.x = |
836 | right_Rect(bounds_Widget(page)) - left_Rect(bounds_Widget(constAs_Widget(input))); | 846 | right_Rect(bounds_Widget(page)) - left_Rect(bounds_Widget(constAs_Widget(input))); |
837 | printf("expand to %s, %d - %d\n", cstr_String(id_Widget(page)), | 847 | printf("expand to %s, %d - %d\n", cstr_String(id_Widget(page)), |
diff --git a/src/ui/widget.c b/src/ui/widget.c index 05bb62cc..cace9568 100644 --- a/src/ui/widget.c +++ b/src/ui/widget.c | |||
@@ -71,6 +71,7 @@ iDefineObjectConstruction(Widget) | |||
71 | void init_Widget(iWidget *d) { | 71 | void init_Widget(iWidget *d) { |
72 | init_String(&d->id); | 72 | init_String(&d->id); |
73 | d->flags = 0; | 73 | d->flags = 0; |
74 | d->flags2 = 0; | ||
74 | d->rect = zero_Rect(); | 75 | d->rect = zero_Rect(); |
75 | d->bgColor = none_ColorId; | 76 | d->bgColor = none_ColorId; |
76 | d->frameColor = none_ColorId; | 77 | d->frameColor = none_ColorId; |
@@ -135,6 +136,10 @@ void setFlags_Widget(iWidget *d, int flags, iBool set) { | |||
135 | } | 136 | } |
136 | } | 137 | } |
137 | 138 | ||
139 | void setFlags2_Widget(iWidget *d, int flags2, iBool set) { | ||
140 | iChangeFlags(d->flags2, flags2, set); | ||
141 | } | ||
142 | |||
138 | void setPos_Widget(iWidget *d, iInt2 pos) { | 143 | void setPos_Widget(iWidget *d, iInt2 pos) { |
139 | d->rect.pos = pos; | 144 | d->rect.pos = pos; |
140 | setFlags_Widget(d, fixedPosition_WidgetFlag, iTrue); | 145 | setFlags_Widget(d, fixedPosition_WidgetFlag, iTrue); |
@@ -228,6 +233,13 @@ static size_t numArrangedChildren_Widget_(const iWidget *d) { | |||
228 | return count; | 233 | return count; |
229 | } | 234 | } |
230 | 235 | ||
236 | static void centerHorizontal_Widget_(iWidget *d) { | ||
237 | d->rect.pos.x = ((d->parent ? width_Rect(innerRect_Widget_(d->parent)) | ||
238 | : rootSize_Window(get_Window()).x) - | ||
239 | width_Rect(d->rect)) / | ||
240 | 2; | ||
241 | } | ||
242 | |||
231 | void arrange_Widget(iWidget *d) { | 243 | void arrange_Widget(iWidget *d) { |
232 | if (isCollapsed_Widget_(d)) { | 244 | if (isCollapsed_Widget_(d)) { |
233 | setFlags_Widget(d, wasCollapsed_WidgetFlag, iTrue); | 245 | setFlags_Widget(d, wasCollapsed_WidgetFlag, iTrue); |
@@ -236,6 +248,9 @@ void arrange_Widget(iWidget *d) { | |||
236 | if (d->flags & moveToParentRightEdge_WidgetFlag) { | 248 | if (d->flags & moveToParentRightEdge_WidgetFlag) { |
237 | d->rect.pos.x = width_Rect(innerRect_Widget_(d->parent)) - width_Rect(d->rect); | 249 | d->rect.pos.x = width_Rect(innerRect_Widget_(d->parent)) - width_Rect(d->rect); |
238 | } | 250 | } |
251 | if (d->flags2 & centerHorizontal_WidgetFlag2) { | ||
252 | centerHorizontal_Widget_(d); | ||
253 | } | ||
239 | if (d->flags & resizeToParentWidth_WidgetFlag) { | 254 | if (d->flags & resizeToParentWidth_WidgetFlag) { |
240 | setWidth_Widget_(d, width_Rect(innerRect_Widget_(d->parent))); | 255 | setWidth_Widget_(d, width_Rect(innerRect_Widget_(d->parent))); |
241 | } | 256 | } |
@@ -351,6 +366,9 @@ void arrange_Widget(iWidget *d) { | |||
351 | if (child->flags & fixedPosition_WidgetFlag) { | 366 | if (child->flags & fixedPosition_WidgetFlag) { |
352 | continue; | 367 | continue; |
353 | } | 368 | } |
369 | if (child->flags2 & centerHorizontal_WidgetFlag2) { | ||
370 | continue; | ||
371 | } | ||
354 | if (d->flags & (arrangeHorizontal_WidgetFlag | arrangeVertical_WidgetFlag)) { | 372 | if (d->flags & (arrangeHorizontal_WidgetFlag | arrangeVertical_WidgetFlag)) { |
355 | if (child->flags & moveToParentRightEdge_WidgetFlag) { | 373 | if (child->flags & moveToParentRightEdge_WidgetFlag) { |
356 | continue; /* Not part of the sequential arrangement .*/ | 374 | continue; /* Not part of the sequential arrangement .*/ |
@@ -401,6 +419,9 @@ void arrange_Widget(iWidget *d) { | |||
401 | } | 419 | } |
402 | } | 420 | } |
403 | } | 421 | } |
422 | if (d->flags2 & centerHorizontal_WidgetFlag2) { | ||
423 | centerHorizontal_Widget_(d); | ||
424 | } | ||
404 | } | 425 | } |
405 | } | 426 | } |
406 | 427 | ||
diff --git a/src/ui/widget.h b/src/ui/widget.h index cc6d774e..34e46c0b 100644 --- a/src/ui/widget.h +++ b/src/ui/widget.h | |||
@@ -82,6 +82,10 @@ enum iWidgetFlag { | |||
82 | fixedSize_WidgetFlag = fixedWidth_WidgetFlag | fixedHeight_WidgetFlag, | 82 | fixedSize_WidgetFlag = fixedWidth_WidgetFlag | fixedHeight_WidgetFlag, |
83 | }; | 83 | }; |
84 | 84 | ||
85 | enum iWidgetFlag2 { | ||
86 | centerHorizontal_WidgetFlag2 = iBit(1), | ||
87 | }; | ||
88 | |||
85 | enum iWidgetAddPos { | 89 | enum iWidgetAddPos { |
86 | back_WidgetAddPos, | 90 | back_WidgetAddPos, |
87 | front_WidgetAddPos, | 91 | front_WidgetAddPos, |
@@ -96,6 +100,7 @@ struct Impl_Widget { | |||
96 | iObject object; | 100 | iObject object; |
97 | iString id; | 101 | iString id; |
98 | int flags; | 102 | int flags; |
103 | int flags2; | ||
99 | iRect rect; | 104 | iRect rect; |
100 | int padding[4]; /* left, top, right, bottom */ | 105 | int padding[4]; /* left, top, right, bottom */ |
101 | int bgColor; | 106 | int bgColor; |
@@ -166,6 +171,7 @@ iBool isCommand_Widget (const iWidget *d, const SDL_Event *ev, const char * | |||
166 | iBool hasParent_Widget (const iWidget *d, const iWidget *someParent); | 171 | iBool hasParent_Widget (const iWidget *d, const iWidget *someParent); |
167 | void setId_Widget (iWidget *, const char *id); | 172 | void setId_Widget (iWidget *, const char *id); |
168 | void setFlags_Widget (iWidget *, int flags, iBool set); | 173 | void setFlags_Widget (iWidget *, int flags, iBool set); |
174 | void setFlags2_Widget (iWidget *, int flags2, iBool set); | ||
169 | void setPos_Widget (iWidget *, iInt2 pos); | 175 | void setPos_Widget (iWidget *, iInt2 pos); |
170 | void setSize_Widget (iWidget *, iInt2 size); | 176 | void setSize_Widget (iWidget *, iInt2 size); |
171 | void setPadding_Widget (iWidget *, int left, int top, int right, int bottom); | 177 | void setPadding_Widget (iWidget *, int left, int top, int right, int bottom); |