summaryrefslogtreecommitdiff
path: root/src/ui
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-08-23 18:39:55 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-08-23 18:39:55 +0300
commit31c297274b9f1212bd8043fb062bf157afe8859f (patch)
tree1ba14fc913b777cca7c1b3c9c8df751dbeb27b8d /src/ui
parentcabaecd2a7cb20b6bf6b31bdde287d6c24c632f7 (diff)
Widget: Optional padding for child arrangement
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/util.c21
-rw-r--r--src/ui/widget.c39
-rw-r--r--src/ui/widget.h6
3 files changed, 38 insertions, 28 deletions
diff --git a/src/ui/util.c b/src/ui/util.c
index 52d5031e..c44fa37c 100644
--- a/src/ui/util.c
+++ b/src/ui/util.c
@@ -500,6 +500,7 @@ iBool filePathHandler_(iWidget *dlg, const char *cmd) {
500 500
501iWidget *makeSheet_Widget(const char *id) { 501iWidget *makeSheet_Widget(const char *id) {
502 iWidget *sheet = new_Widget(); 502 iWidget *sheet = new_Widget();
503 setPadding1_Widget(sheet, 3 * gap_UI);
503 setId_Widget(sheet, id); 504 setId_Widget(sheet, id);
504 setFrameColor_Widget(sheet, uiSeparator_ColorId); 505 setFrameColor_Widget(sheet, uiSeparator_ColorId);
505 setBackgroundColor_Widget(sheet, uiBackground_ColorId); 506 setBackgroundColor_Widget(sheet, uiBackground_ColorId);
@@ -507,9 +508,6 @@ iWidget *makeSheet_Widget(const char *id) {
507 mouseModal_WidgetFlag | keepOnTop_WidgetFlag | arrangeVertical_WidgetFlag | 508 mouseModal_WidgetFlag | keepOnTop_WidgetFlag | arrangeVertical_WidgetFlag |
508 arrangeSize_WidgetFlag, 509 arrangeSize_WidgetFlag,
509 iTrue); 510 iTrue);
510 // const iInt2 rootSize = rootSize_Window(get_Window());
511 // setSize_Widget(sheet, init_I2(rootSize.x / 2, 0));
512 // setFlags_Widget(sheet, fixedHeight_WidgetFlag, iFalse);
513 return sheet; 511 return sheet;
514} 512}
515 513
@@ -787,7 +785,7 @@ iWidget *makeBookmarkEditor_Widget(void) {
787 addChild_Widget( 785 addChild_Widget(
788 div, 786 div,
789 iClob(new_LabelWidget( 787 iClob(new_LabelWidget(
790 uiTextCaution_ColorEscape "Save", SDLK_RETURN, KMOD_PRIMARY, "bmed.accept"))); 788 uiTextCaution_ColorEscape "Save Bookmark", SDLK_RETURN, KMOD_PRIMARY, "bmed.accept")));
791 } 789 }
792 addChild_Widget(dlg, iClob(div)); 790 addChild_Widget(dlg, iClob(div));
793 addChild_Widget(get_Window()->root, iClob(dlg)); 791 addChild_Widget(get_Window()->root, iClob(dlg));
@@ -865,20 +863,9 @@ iWidget *makeIdentityCreation_Widget(void) {
865 addChild_Widget(headings, iClob(makeHeading_Widget("Valid until:"))); 863 addChild_Widget(headings, iClob(makeHeading_Widget("Valid until:")));
866 iInputWidget *until; 864 iInputWidget *until;
867 addChild_Widget(values, iClob(until = new_InputWidget(19))); 865 addChild_Widget(values, iClob(until = new_InputWidget(19)));
868 setTextCStr_InputWidget(until, "YYYY-MM-DD HH:MM:SS"); 866 setHint_InputWidget(until, collectNewCStr_String("YYYY-MM-DD HH:MM:SS"));
869// addChild_Widget(headings, iClob(newEmpty_LabelWidget()));
870// addChild_Widget(values,
871// iClob(newColor_LabelWidget("Time defaults to end of day.",
872// uiAnnotation_ColorId)));
873 addChild_Widget(headings, iClob(makeHeading_Widget("Temporary:"))); 867 addChild_Widget(headings, iClob(makeHeading_Widget("Temporary:")));
874// iWidget *temp = new_Widget();
875// addChildFlags_Widget(
876// values, iClob(temp), arrangeHorizontal_WidgetFlag | arrangeSize_WidgetFlag);
877 addChild_Widget(values, iClob(makeToggle_Widget("ident.temp"))); 868 addChild_Widget(values, iClob(makeToggle_Widget("ident.temp")));
878 // addChild_Widget(
879 // temp,
880 // iClob(newColor_LabelWidget("Temporary identities are not saved.",
881 // uiAnnotation_ColorId)));
882 arrange_Widget(dlg); 869 arrange_Widget(dlg);
883 for (size_t i = 0; i < iElemCount(inputs); ++i) { 870 for (size_t i = 0; i < iElemCount(inputs); ++i) {
884 as_Widget(inputs[i])->rect.size.x = 100 * gap_UI - headings->rect.size.x; 871 as_Widget(inputs[i])->rect.size.x = 100 * gap_UI - headings->rect.size.x;
@@ -889,7 +876,7 @@ iWidget *makeIdentityCreation_Widget(void) {
889 addChild_Widget( 876 addChild_Widget(
890 div, 877 div,
891 iClob(new_LabelWidget( 878 iClob(new_LabelWidget(
892 uiTextAction_ColorEscape "Create", SDLK_RETURN, KMOD_PRIMARY, "ident.accept"))); 879 uiTextAction_ColorEscape "Create Identity", SDLK_RETURN, KMOD_PRIMARY, "ident.accept")));
893 } 880 }
894 addChild_Widget(dlg, iClob(div)); 881 addChild_Widget(dlg, iClob(div));
895 addChild_Widget(get_Window()->root, iClob(dlg)); 882 addChild_Widget(get_Window()->root, iClob(dlg));
diff --git a/src/ui/widget.c b/src/ui/widget.c
index 1175b217..8277b921 100644
--- a/src/ui/widget.c
+++ b/src/ui/widget.c
@@ -76,6 +76,7 @@ void init_Widget(iWidget *d) {
76 d->children = NULL; 76 d->children = NULL;
77 d->parent = NULL; 77 d->parent = NULL;
78 d->commandHandler = NULL; 78 d->commandHandler = NULL;
79 iZap(d->padding);
79} 80}
80 81
81void deinit_Widget(iWidget *d) { 82void deinit_Widget(iWidget *d) {
@@ -142,6 +143,13 @@ void setSize_Widget(iWidget *d, iInt2 size) {
142 setFlags_Widget(d, fixedSize_WidgetFlag, iTrue); 143 setFlags_Widget(d, fixedSize_WidgetFlag, iTrue);
143} 144}
144 145
146void setPadding_Widget(iWidget *d, int left, int top, int right, int bottom) {
147 d->padding[0] = left;
148 d->padding[1] = top;
149 d->padding[2] = right;
150 d->padding[3] = bottom;
151}
152
145void setBackgroundColor_Widget(iWidget *d, int bgColor) { 153void setBackgroundColor_Widget(iWidget *d, int bgColor) {
146 d->bgColor = bgColor; 154 d->bgColor = bgColor;
147} 155}
@@ -191,19 +199,26 @@ iLocalDef iBool isCollapsed_Widget_(const iWidget *d) {
191 (hidden_WidgetFlag | collapse_WidgetFlag); 199 (hidden_WidgetFlag | collapse_WidgetFlag);
192} 200}
193 201
202iLocalDef iRect innerRect_Widget_(const iWidget *d) {
203 return init_Rect(d->padding[0],
204 d->padding[1],
205 width_Rect(d->rect) - d->padding[0] - d->padding[2],
206 height_Rect(d->rect) - d->padding[1] - d->padding[3]);
207}
208
194void arrange_Widget(iWidget *d) { 209void arrange_Widget(iWidget *d) {
195 if (isCollapsed_Widget_(d)) { 210 if (isCollapsed_Widget_(d)) {
196 setFlags_Widget(d, wasCollapsed_WidgetFlag, iTrue); 211 setFlags_Widget(d, wasCollapsed_WidgetFlag, iTrue);
197 return; 212 return;
198 } 213 }
199 if (d->flags & moveToParentRightEdge_WidgetFlag) { 214 if (d->flags & moveToParentRightEdge_WidgetFlag) {
200 d->rect.pos.x = width_Rect(d->parent->rect) - width_Rect(d->rect); 215 d->rect.pos.x = width_Rect(innerRect_Widget_(d->parent)) - width_Rect(d->rect);
201 } 216 }
202 if (d->flags & resizeToParentWidth_WidgetFlag) { 217 if (d->flags & resizeToParentWidth_WidgetFlag) {
203 setWidth_Widget_(d, d->parent->rect.size.x); 218 setWidth_Widget_(d, width_Rect(innerRect_Widget_(d->parent)));
204 } 219 }
205 if (d->flags & resizeToParentHeight_WidgetFlag) { 220 if (d->flags & resizeToParentHeight_WidgetFlag) {
206 setHeight_Widget_(d, d->parent->rect.size.y); 221 setHeight_Widget_(d, height_Rect(innerRect_Widget_(d->parent)));
207 } 222 }
208 /* The rest of the arrangement depends on child widgets. */ 223 /* The rest of the arrangement depends on child widgets. */
209 if (!d->children) { 224 if (!d->children) {
@@ -236,7 +251,7 @@ void arrange_Widget(iWidget *d) {
236 const int expCount = numExpandingChildren_Widget_(d); 251 const int expCount = numExpandingChildren_Widget_(d);
237 /* Only resize the expanding children, not touching the others. */ 252 /* Only resize the expanding children, not touching the others. */
238 if (expCount > 0) { 253 if (expCount > 0) {
239 iInt2 avail = d->rect.size; 254 iInt2 avail = innerRect_Widget_(d).size;
240 iConstForEach(ObjectList, i, d->children) { 255 iConstForEach(ObjectList, i, d->children) {
241 const iWidget *child = constAs_Widget(i.object); 256 const iWidget *child = constAs_Widget(i.object);
242 if (~child->flags & expand_WidgetFlag) { 257 if (~child->flags & expand_WidgetFlag) {
@@ -250,27 +265,27 @@ void arrange_Widget(iWidget *d) {
250 if (child->flags & expand_WidgetFlag) { 265 if (child->flags & expand_WidgetFlag) {
251 if (d->flags & arrangeHorizontal_WidgetFlag) { 266 if (d->flags & arrangeHorizontal_WidgetFlag) {
252 if (dirs.x) setWidth_Widget_(child, avail.x); 267 if (dirs.x) setWidth_Widget_(child, avail.x);
253 if (dirs.y) setHeight_Widget_(child, d->rect.size.y); 268 if (dirs.y) setHeight_Widget_(child, height_Rect(innerRect_Widget_(d)));
254 } 269 }
255 else if (d->flags & arrangeVertical_WidgetFlag) { 270 else if (d->flags & arrangeVertical_WidgetFlag) {
256 if (dirs.x) setWidth_Widget_(child, d->rect.size.x); 271 if (dirs.x) setWidth_Widget_(child, width_Rect(innerRect_Widget_(d)));
257 if (dirs.y) setHeight_Widget_(child, avail.y); 272 if (dirs.y) setHeight_Widget_(child, avail.y);
258 } 273 }
259 } 274 }
260 else { 275 else {
261 /* Fill the off axis, though. */ 276 /* Fill the off axis, though. */
262 if (d->flags & arrangeHorizontal_WidgetFlag) { 277 if (d->flags & arrangeHorizontal_WidgetFlag) {
263 if (dirs.y) setHeight_Widget_(child, d->rect.size.y); 278 if (dirs.y) setHeight_Widget_(child, height_Rect(innerRect_Widget_(d)));
264 } 279 }
265 else if (d->flags & arrangeVertical_WidgetFlag) { 280 else if (d->flags & arrangeVertical_WidgetFlag) {
266 if (dirs.x) setWidth_Widget_(child, d->rect.size.x); 281 if (dirs.x) setWidth_Widget_(child, width_Rect(innerRect_Widget_(d)));
267 } 282 }
268 } 283 }
269 } 284 }
270 } 285 }
271 else { 286 else {
272 /* Evenly size all children. */ 287 /* Evenly size all children. */
273 iInt2 childSize = d->rect.size; 288 iInt2 childSize = innerRect_Widget_(d).size;
274 if (d->flags & arrangeHorizontal_WidgetFlag) { 289 if (d->flags & arrangeHorizontal_WidgetFlag) {
275 childSize.x /= childCount; 290 childSize.x /= childCount;
276 } 291 }
@@ -292,7 +307,7 @@ void arrange_Widget(iWidget *d) {
292 setWidth_Widget_(as_Widget(i.object), widest); 307 setWidth_Widget_(as_Widget(i.object), widest);
293 } 308 }
294 } 309 }
295 iInt2 pos = zero_I2(); 310 iInt2 pos = initv_I2(d->padding);
296 iForEach(ObjectList, i, d->children) { 311 iForEach(ObjectList, i, d->children) {
297 iWidget *child = as_Widget(i.object); 312 iWidget *child = as_Widget(i.object);
298 arrange_Widget(child); 313 arrange_Widget(child);
@@ -308,6 +323,9 @@ void arrange_Widget(iWidget *d) {
308 pos.y += child->rect.size.y; 323 pos.y += child->rect.size.y;
309 } 324 }
310 } 325 }
326 else if (d->flags & resizeChildren_WidgetFlag) {
327 child->rect.pos = pos;
328 }
311 } 329 }
312 /* Update the size of the widget according to the arrangement. */ 330 /* Update the size of the widget according to the arrangement. */
313 if (d->flags & arrangeSize_WidgetFlag) { 331 if (d->flags & arrangeSize_WidgetFlag) {
@@ -321,6 +339,7 @@ void arrange_Widget(iWidget *d) {
321 bounds = union_Rect(bounds, child->rect); 339 bounds = union_Rect(bounds, child->rect);
322 } 340 }
323 } 341 }
342 adjustEdges_Rect(&bounds, -d->padding[1], d->padding[2], d->padding[3], -d->padding[0]);
324 if (d->flags & arrangeWidth_WidgetFlag) { 343 if (d->flags & arrangeWidth_WidgetFlag) {
325 setWidth_Widget_(d, bounds.size.x); 344 setWidth_Widget_(d, bounds.size.x);
326 /* Parent size changed, must update the children.*/ 345 /* Parent size changed, must update the children.*/
diff --git a/src/ui/widget.h b/src/ui/widget.h
index b0022a38..3f03fc07 100644
--- a/src/ui/widget.h
+++ b/src/ui/widget.h
@@ -95,6 +95,7 @@ struct Impl_Widget {
95 iString id; 95 iString id;
96 int flags; 96 int flags;
97 iRect rect; 97 iRect rect;
98 int padding[4]; /* left, top, right, bottom */
98 int bgColor; 99 int bgColor;
99 int frameColor; 100 int frameColor;
100 iObjectList *children; 101 iObjectList *children;
@@ -129,7 +130,8 @@ void destroyPending_Widget(void);
129 130
130const iString *id_Widget (const iWidget *); 131const iString *id_Widget (const iWidget *);
131int flags_Widget (const iWidget *); 132int flags_Widget (const iWidget *);
132iRect bounds_Widget (const iWidget *); 133iRect bounds_Widget (const iWidget *); /* outer bounds */
134iRect innerBounds_Widget (const iWidget *);
133iInt2 localCoord_Widget (const iWidget *, iInt2 coord); 135iInt2 localCoord_Widget (const iWidget *, iInt2 coord);
134iBool contains_Widget (const iWidget *, iInt2 coord); 136iBool contains_Widget (const iWidget *, iInt2 coord);
135iAny * findChild_Widget (const iWidget *, const char *id); 137iAny * findChild_Widget (const iWidget *, const char *id);
@@ -153,6 +155,8 @@ void setId_Widget (iWidget *, const char *id);
153void setFlags_Widget (iWidget *, int flags, iBool set); 155void setFlags_Widget (iWidget *, int flags, iBool set);
154void setPos_Widget (iWidget *, iInt2 pos); 156void setPos_Widget (iWidget *, iInt2 pos);
155void setSize_Widget (iWidget *, iInt2 size); 157void setSize_Widget (iWidget *, iInt2 size);
158void setPadding_Widget (iWidget *, int left, int top, int right, int bottom);
159iLocalDef void setPadding1_Widget (iWidget *d, int padding) { setPadding_Widget(d, padding, padding, padding, padding); }
156void setBackgroundColor_Widget (iWidget *, int bgColor); 160void setBackgroundColor_Widget (iWidget *, int bgColor);
157void setFrameColor_Widget (iWidget *, int frameColor); 161void setFrameColor_Widget (iWidget *, int frameColor);
158void setCommandHandler_Widget (iWidget *, iBool (*handler)(iWidget *, const char *)); 162void setCommandHandler_Widget (iWidget *, iBool (*handler)(iWidget *, const char *));