Cleaned up uiStack's data structures to use the stackControl structure instead of four memory blocks for the control-specific parameters.

This commit is contained in:
Pietro Gagliardi 2015-04-11 13:45:59 -04:00
parent 2602330223
commit 22e11ca8f3
1 changed files with 26 additions and 36 deletions

View File

@ -2,17 +2,13 @@
#include "uipriv.h" #include "uipriv.h"
// TODO // TODO
// - use stackControl
// - change prefwid/prefht to preferredWidth/preferredHeight // - change prefwid/prefht to preferredWidth/preferredHeight
typedef struct stack stack; typedef struct stack stack;
typedef struct stackControl stackControl; typedef struct stackControl stackControl;
struct stack { struct stack {
uiControl **controls; stackControl *controls;
int *stretchy;
intmax_t *width;
intmax_t *height;
uintmax_t len; uintmax_t len;
uintmax_t cap; uintmax_t cap;
int vertical; int vertical;
@ -21,7 +17,7 @@ struct stack {
}; };
struct stackControl { struct stackControl {
uiControl *control; uiControl *c;
int stretchy; int stretchy;
intmax_t width; // both used by resize(); preallocated to save time and reduce risk of failure intmax_t width; // both used by resize(); preallocated to save time and reduce risk of failure
intmax_t height; intmax_t height;
@ -33,11 +29,8 @@ static void stackDestroy(uiControl *c)
uintmax_t i; uintmax_t i;
for (i = 0; i < s->len; i++) for (i = 0; i < s->len; i++)
uiControlDestroy(s->controls[i]); uiControlDestroy(s->controls[i].c);
uiFree(s->controls); uiFree(s->controls);
uiFree(s->stretchy);
uiFree(s->width);
uiFree(s->height);
uiFree(s); uiFree(s);
uiFree(c); uiFree(c);
} }
@ -54,7 +47,7 @@ static void stackSetParent(uiControl *c, uintptr_t parent)
s->parent = parent; s->parent = parent;
for (i = 0; i < s->len; i++) for (i = 0; i < s->len; i++)
uiControlSetParent(s->controls[i], s->parent); uiControlSetParent(s->controls[i].c, s->parent);
updateParent(s->parent); updateParent(s->parent);
} }
@ -67,7 +60,7 @@ static void stackRemoveParent(uiControl *c)
oldparent = s->parent; oldparent = s->parent;
s->parent = 0; s->parent = 0;
for (i = 0; i < s->len; i++) for (i = 0; i < s->len; i++)
uiControlRemoveParent(s->controls[i]); uiControlRemoveParent(s->controls[i].c);
updateParent(oldparent); updateParent(oldparent);
} }
@ -105,8 +98,8 @@ static void stackPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intma
maxswid = 0; maxswid = 0;
maxsht = 0; maxsht = 0;
for (i = 0; i < s->len; i++) { for (i = 0; i < s->len; i++) {
uiControlPreferredSize(s->controls[i], d, &prefwid, &prefht); uiControlPreferredSize(s->controls[i].c, d, &prefwid, &prefht);
if (s->stretchy[i]) { if (s->controls[i].stretchy) {
nStretchy++; nStretchy++;
if (maxswid < prefwid) if (maxswid < prefwid)
maxswid = prefwid; maxswid = prefwid;
@ -116,10 +109,10 @@ static void stackPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intma
if (s->vertical) { if (s->vertical) {
if (*width < prefwid) if (*width < prefwid)
*width = prefwid; *width = prefwid;
if (!s->stretchy[i]) if (!s->controls[i].stretchy)
*height += prefht; *height += prefht;
} else { } else {
if (!s->stretchy[i]) if (!s->controls[i].stretchy)
*width += prefwid; *width += prefwid;
if (*height < prefht) if (*height < prefht)
*height = prefht; *height = prefht;
@ -165,19 +158,19 @@ static void stackResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, in
stretchyht = height; stretchyht = height;
nStretchy = 0; nStretchy = 0;
for (i = 0; i < s->len; i++) { for (i = 0; i < s->len; i++) {
if (s->stretchy[i]) { if (s->controls[i].stretchy) {
nStretchy++; nStretchy++;
continue; continue;
} }
c = s->controls[i]; c = s->controls[i].c;
uiControlPreferredSize(s->controls[i], d, &prefwid, &prefht); uiControlPreferredSize(s->controls[i].c, d, &prefwid, &prefht);
if (s->vertical) { // all controls have same width if (s->vertical) { // all controls have same width
s->width[i] = width; s->controls[i].width = width;
s->height[i] = prefht; s->controls[i].height = prefht;
stretchyht -= prefht; stretchyht -= prefht;
} else { // all controls have same height } else { // all controls have same height
s->width[i] = prefwid; s->controls[i].width = prefwid;
s->height[i] = height; s->controls[i].height = height;
stretchywid -= prefwid; stretchywid -= prefwid;
} }
} }
@ -189,18 +182,18 @@ static void stackResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, in
else else
stretchywid /= nStretchy; stretchywid /= nStretchy;
for (i = 0; i < s->len; i++) for (i = 0; i < s->len; i++)
if (s->stretchy[i]) { if (s->controls[i].stretchy) {
s->width[i] = stretchywid; s->controls[i].width = stretchywid;
s->height[i] = stretchyht; s->controls[i].height = stretchyht;
} }
// 3) now we can position controls // 3) now we can position controls
for (i = 0; i < s->len; i++) { for (i = 0; i < s->len; i++) {
uiControlResize(s->controls[i], x, y, s->width[i], s->height[i], d); uiControlResize(s->controls[i].c, x, y, s->controls[i].width, s->controls[i].height, d);
if (s->vertical) if (s->vertical)
y += s->height[i] + ypadding; y += s->controls[i].height + ypadding;
else else
x += s->width[i] + xpadding; x += s->controls[i].width + xpadding;
} }
} }
@ -242,15 +235,12 @@ void uiStackAdd(uiControl *st, uiControl *c, int stretchy)
if (s->len >= s->cap) { if (s->len >= s->cap) {
s->cap += stackCapGrow; s->cap += stackCapGrow;
s->controls = (uiControl **) uiRealloc(s->controls, s->cap * sizeof (uiControl *), "uiControl *[]"); s->controls = (stackControl *) uiRealloc(s->controls, s->cap * sizeof (stackControl *), "stackControl[]");
s->stretchy = (int *) uiRealloc(s->stretchy, s->cap * sizeof (int), "int[]");
s->width = (intmax_t *) uiRealloc(s->width, s->cap * sizeof (intmax_t), "intmax_t[]");
s->height = (intmax_t *) uiRealloc(s->height, s->cap * sizeof (intmax_t), "intmax_t[]");
} }
s->controls[s->len] = c; s->controls[s->len].c = c;
s->stretchy[s->len] = stretchy; s->controls[s->len].stretchy = stretchy;
if (s->parent != 0) if (s->parent != 0)
uiControlSetParent(s->controls[s->len], s->parent); uiControlSetParent(s->controls[s->len].c, s->parent);
s->len++; s->len++;
updateParent(s->parent); updateParent(s->parent);
} }