Changed 's' to 'b' in stack.c. This is the first part of the change from uiStack to uiBox.

This commit is contained in:
Pietro Gagliardi 2015-04-20 10:11:58 -04:00
parent c5e5edf8f0
commit 409480e93a
1 changed files with 148 additions and 148 deletions

296
stack.c
View File

@ -30,15 +30,15 @@ struct stackControl {
static void stackDestroy(uiControl *c) static void stackDestroy(uiControl *c)
{ {
stack *s = (stack *) c; stack *b = (stack *) c;
uintmax_t i; uintmax_t i;
if (s->parent != NULL) if (b->parent != NULL)
complain("attempt to destroy a uiControl at %p while it still has a parent %p", c, s->parent); complain("attempt to destroy a uiControl at %p while it still has a parent %p", c, b->parent);
for (i = 0; i < s->len; i++) for (i = 0; i < b->len; i++)
uiControlDestroy(s->controls[i].c); uiControlDestroy(b->controls[i].c);
uiFree(s->controls); uiFree(b->controls);
uiFree(s); uiFree(b);
} }
static uintptr_t stackHandle(uiControl *c) static uintptr_t stackHandle(uiControl *c)
@ -48,23 +48,23 @@ static uintptr_t stackHandle(uiControl *c)
static void stackSetParent(uiControl *c, uiParent *parent) static void stackSetParent(uiControl *c, uiParent *parent)
{ {
stack *s = (stack *) c; stack *b = (stack *) c;
uintmax_t i; uintmax_t i;
uiParent *oldparent; uiParent *oldparent;
oldparent = s->parent; oldparent = b->parent;
s->parent = parent; b->parent = parent;
for (i = 0; i < s->len; i++) for (i = 0; i < b->len; i++)
uiControlSetParent(s->controls[i].c, s->parent); uiControlSetParent(b->controls[i].c, b->parent);
if (oldparent != NULL) if (oldparent != NULL)
uiParentUpdate(oldparent); uiParentUpdate(oldparent);
if (s->parent != NULL) if (b->parent != NULL)
uiParentUpdate(s->parent); uiParentUpdate(b->parent);
} }
static void stackPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height) static void stackPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
{ {
stack *s = (stack *) c; stack *b = (stack *) c;
int xpadding, ypadding; int xpadding, ypadding;
uintmax_t nStretchy; uintmax_t nStretchy;
// these two contain the largest preferred width and height of all stretchy controls in the stack // these two contain the largest preferred width and height of all stretchy controls in the stack
@ -75,46 +75,46 @@ static void stackPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intma
*width = 0; *width = 0;
*height = 0; *height = 0;
if (s->len == 0) if (b->len == 0)
return; return;
// 0) get this Stack's padding // 0) get this Stack's padding
xpadding = 0; xpadding = 0;
ypadding = 0; ypadding = 0;
if (s->padded) { if (b->padded) {
xpadding = d->xPadding; xpadding = d->xPadding;
ypadding = d->yPadding; ypadding = d->yPadding;
} }
// 1) initialize the desired rect with the needed padding // 1) initialize the desired rect with the needed padding
if (s->vertical) if (b->vertical)
*height = (s->len - 1) * ypadding; *height = (b->len - 1) * ypadding;
else else
*width = (s->len - 1) * xpadding; *width = (b->len - 1) * xpadding;
// 2) add in the size of non-stretchy controls and get (but not add in) the largest widths and heights of stretchy controls // 2) add in the size of non-stretchy controls and get (but not add in) the largest widths and heights of stretchy controls
// we still add in like direction of stretchy controls // we still add in like direction of stretchy controls
nStretchy = 0; nStretchy = 0;
maxStretchyWidth = 0; maxStretchyWidth = 0;
maxStretchyHeight = 0; maxStretchyHeight = 0;
for (i = 0; i < s->len; i++) { for (i = 0; i < b->len; i++) {
if (!uiControlVisible(s->controls[i].c)) if (!uiControlVisible(b->controls[i].c))
continue; continue;
uiControlPreferredSize(s->controls[i].c, d, &preferredWidth, &preferredHeight); uiControlPreferredSize(b->controls[i].c, d, &preferredWidth, &preferredHeight);
if (s->controls[i].stretchy) { if (b->controls[i].stretchy) {
nStretchy++; nStretchy++;
if (maxStretchyWidth < preferredWidth) if (maxStretchyWidth < preferredWidth)
maxStretchyWidth = preferredWidth; maxStretchyWidth = preferredWidth;
if (maxStretchyHeight < preferredHeight) if (maxStretchyHeight < preferredHeight)
maxStretchyHeight = preferredHeight; maxStretchyHeight = preferredHeight;
} }
if (s->vertical) { if (b->vertical) {
if (*width < preferredWidth) if (*width < preferredWidth)
*width = preferredWidth; *width = preferredWidth;
if (!s->controls[i].stretchy) if (!b->controls[i].stretchy)
*height += preferredHeight; *height += preferredHeight;
} else { } else {
if (!s->controls[i].stretchy) if (!b->controls[i].stretchy)
*width += preferredWidth; *width += preferredWidth;
if (*height < preferredHeight) if (*height < preferredHeight)
*height = preferredHeight; *height = preferredHeight;
@ -122,7 +122,7 @@ static void stackPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intma
} }
// 3) and now we can add in stretchy controls // 3) and now we can add in stretchy controls
if (s->vertical) if (b->vertical)
*height += nStretchy * maxStretchyHeight; *height += nStretchy * maxStretchyHeight;
else else
*width += nStretchy * maxStretchyWidth; *width += nStretchy * maxStretchyWidth;
@ -130,271 +130,271 @@ static void stackPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intma
static void stackResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d) static void stackResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
{ {
stack *s = (stack *) c; stack *b = (stack *) c;
int xpadding, ypadding; int xpadding, ypadding;
uintmax_t nStretchy; uintmax_t nStretchy;
intmax_t stretchywid, stretchyht; intmax_t stretchywid, stretchyht;
uintmax_t i; uintmax_t i;
intmax_t preferredWidth, preferredHeight; intmax_t preferredWidth, preferredHeight;
if (s->len == 0) if (b->len == 0)
return; return;
// -1) get this Stack's padding // -1) get this Stack's padding
xpadding = 0; xpadding = 0;
ypadding = 0; ypadding = 0;
if (s->padded) { if (b->padded) {
xpadding = d->xPadding; xpadding = d->xPadding;
ypadding = d->yPadding; ypadding = d->yPadding;
} }
// 0) inset the available rect by the needed padding // 0) inset the available rect by the needed padding
if (s->vertical) if (b->vertical)
height -= (s->len - 1) * ypadding; height -= (b->len - 1) * ypadding;
else else
width -= (s->len - 1) * xpadding; width -= (b->len - 1) * xpadding;
// 1) get width and height of non-stretchy controls // 1) get width and height of non-stretchy controls
// this will tell us how much space will be left for stretchy controls // this will tell us how much space will be left for stretchy controls
stretchywid = width; stretchywid = width;
stretchyht = height; stretchyht = height;
nStretchy = 0; nStretchy = 0;
for (i = 0; i < s->len; i++) { for (i = 0; i < b->len; i++) {
if (!uiControlVisible(s->controls[i].c)) if (!uiControlVisible(b->controls[i].c))
continue; continue;
if (s->controls[i].stretchy) { if (b->controls[i].stretchy) {
nStretchy++; nStretchy++;
continue; continue;
} }
uiControlPreferredSize(s->controls[i].c, d, &preferredWidth, &preferredHeight); uiControlPreferredSize(b->controls[i].c, d, &preferredWidth, &preferredHeight);
if (s->vertical) { // all controls have same width if (b->vertical) { // all controls have same width
s->controls[i].width = width; b->controls[i].width = width;
s->controls[i].height = preferredHeight; b->controls[i].height = preferredHeight;
stretchyht -= preferredHeight; stretchyht -= preferredHeight;
} else { // all controls have same height } else { // all controls have same height
s->controls[i].width = preferredWidth; b->controls[i].width = preferredWidth;
s->controls[i].height = height; b->controls[i].height = height;
stretchywid -= preferredWidth; stretchywid -= preferredWidth;
} }
} }
// 2) now get the size of stretchy controls // 2) now get the size of stretchy controls
if (nStretchy != 0) if (nStretchy != 0)
if (s->vertical) if (b->vertical)
stretchyht /= nStretchy; stretchyht /= nStretchy;
else else
stretchywid /= nStretchy; stretchywid /= nStretchy;
for (i = 0; i < s->len; i++) { for (i = 0; i < b->len; i++) {
if (!uiControlVisible(s->controls[i].c)) if (!uiControlVisible(b->controls[i].c))
continue; continue;
if (s->controls[i].stretchy) { if (b->controls[i].stretchy) {
s->controls[i].width = stretchywid; b->controls[i].width = stretchywid;
s->controls[i].height = stretchyht; b->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 < b->len; i++) {
if (!uiControlVisible(s->controls[i].c)) if (!uiControlVisible(b->controls[i].c))
continue; continue;
uiControlResize(s->controls[i].c, x, y, s->controls[i].width, s->controls[i].height, d); uiControlResize(b->controls[i].c, x, y, b->controls[i].width, b->controls[i].height, d);
if (s->vertical) if (b->vertical)
y += s->controls[i].height + ypadding; y += b->controls[i].height + ypadding;
else else
x += s->controls[i].width + xpadding; x += b->controls[i].width + xpadding;
} }
} }
static int stackVisible(uiControl *c) static int stackVisible(uiControl *c)
{ {
stack *s = (stack *) c; stack *b = (stack *) c;
return !(s->userHid); return !(b->userHid);
} }
static void stackShow(uiControl *c) static void stackShow(uiControl *c)
{ {
stack *s = (stack *) c; stack *b = (stack *) c;
uintmax_t i; uintmax_t i;
s->userHid = 0; b->userHid = 0;
if (!s->containerHid) { if (!b->containerHid) {
for (i = 0; i < s->len; i++) for (i = 0; i < b->len; i++)
uiControlContainerShow(s->controls[i].c); uiControlContainerShow(b->controls[i].c);
if (s->parent != NULL) if (b->parent != NULL)
uiParentUpdate(s->parent); uiParentUpdate(b->parent);
} }
} }
static void stackHide(uiControl *c) static void stackHide(uiControl *c)
{ {
stack *s = (stack *) c; stack *b = (stack *) c;
uintmax_t i; uintmax_t i;
s->userHid = 1; b->userHid = 1;
for (i = 0; i < s->len; i++) for (i = 0; i < b->len; i++)
uiControlContainerHide(s->controls[i].c); uiControlContainerHide(b->controls[i].c);
if (s->parent != NULL) if (b->parent != NULL)
uiParentUpdate(s->parent); uiParentUpdate(b->parent);
} }
static void stackContainerShow(uiControl *c) static void stackContainerShow(uiControl *c)
{ {
stack *s = (stack *) c; stack *b = (stack *) c;
uintmax_t i; uintmax_t i;
s->containerHid = 0; b->containerHid = 0;
if (!s->userHid) { if (!b->userHid) {
for (i = 0; i < s->len; i++) for (i = 0; i < b->len; i++)
uiControlContainerShow(s->controls[i].c); uiControlContainerShow(b->controls[i].c);
if (s->parent != NULL) if (b->parent != NULL)
uiParentUpdate(s->parent); uiParentUpdate(b->parent);
} }
} }
static void stackContainerHide(uiControl *c) static void stackContainerHide(uiControl *c)
{ {
stack *s = (stack *) c; stack *b = (stack *) c;
uintmax_t i; uintmax_t i;
s->containerHid = 1; b->containerHid = 1;
for (i = 0; i < s->len; i++) for (i = 0; i < b->len; i++)
uiControlContainerHide(s->controls[i].c); uiControlContainerHide(b->controls[i].c);
if (s->parent != NULL) if (b->parent != NULL)
uiParentUpdate(s->parent); uiParentUpdate(b->parent);
} }
static void stackEnable(uiControl *c) static void stackEnable(uiControl *c)
{ {
stack *s = (stack *) c; stack *b = (stack *) c;
uintmax_t i; uintmax_t i;
s->userDisabled = 0; b->userDisabled = 0;
if (!s->containerDisabled) if (!b->containerDisabled)
for (i = 0; i < s->len; i++) for (i = 0; i < b->len; i++)
uiControlContainerEnable(s->controls[i].c); uiControlContainerEnable(b->controls[i].c);
} }
static void stackDisable(uiControl *c) static void stackDisable(uiControl *c)
{ {
stack *s = (stack *) c; stack *b = (stack *) c;
uintmax_t i; uintmax_t i;
s->userDisabled = 1; b->userDisabled = 1;
for (i = 0; i < s->len; i++) for (i = 0; i < b->len; i++)
uiControlContainerDisable(s->controls[i].c); uiControlContainerDisable(b->controls[i].c);
} }
static void stackContainerEnable(uiControl *c) static void stackContainerEnable(uiControl *c)
{ {
stack *s = (stack *) c; stack *b = (stack *) c;
uintmax_t i; uintmax_t i;
s->containerDisabled = 0; b->containerDisabled = 0;
if (!s->userDisabled) if (!b->userDisabled)
for (i = 0; i < s->len; i++) for (i = 0; i < b->len; i++)
uiControlContainerEnable(s->controls[i].c); uiControlContainerEnable(b->controls[i].c);
} }
static void stackContainerDisable(uiControl *c) static void stackContainerDisable(uiControl *c)
{ {
stack *s = (stack *) c; stack *b = (stack *) c;
uintmax_t i; uintmax_t i;
s->containerDisabled = 1; b->containerDisabled = 1;
for (i = 0; i < s->len; i++) for (i = 0; i < b->len; i++)
uiControlContainerDisable(s->controls[i].c); uiControlContainerDisable(b->controls[i].c);
} }
#define stackCapGrow 32 #define stackCapGrow 32
static void stackAppend(uiStack *ss, uiControl *c, int stretchy) static void stackAppend(uiStack *ss, uiControl *c, int stretchy)
{ {
stack *s = (stack *) ss; stack *b = (stack *) ss;
if (s->len >= s->cap) { if (b->len >= b->cap) {
s->cap += stackCapGrow; b->cap += stackCapGrow;
s->controls = (stackControl *) uiRealloc(s->controls, s->cap * sizeof (stackControl), "stackControl[]"); b->controls = (stackControl *) uiRealloc(b->controls, b->cap * sizeof (stackControl), "stackControl[]");
} }
s->controls[s->len].c = c; b->controls[b->len].c = c;
s->controls[s->len].stretchy = stretchy; b->controls[b->len].stretchy = stretchy;
s->len++; // must be here for parent updates to work b->len++; // must be here for parent updates to work
if (s->parent != NULL) { if (b->parent != NULL) {
uiControlSetParent(s->controls[s->len - 1].c, s->parent); uiControlSetParent(b->controls[b->len - 1].c, b->parent);
uiParentUpdate(s->parent); uiParentUpdate(b->parent);
} }
} }
static void stackDelete(uiStack *ss, uintmax_t index) static void stackDelete(uiStack *ss, uintmax_t index)
{ {
stack *s = (stack *) ss; stack *b = (stack *) ss;
uiControl *removed; uiControl *removed;
uintmax_t i; uintmax_t i;
removed = s->controls[index].c; removed = b->controls[index].c;
// TODO switch to memmove? // TODO switch to memmove?
for (i = index; i < s->len - 1; i++) for (i = index; i < b->len - 1; i++)
s->controls[i] = s->controls[i + 1]; b->controls[i] = b->controls[i + 1];
// TODO memset the last one to NULL // TODO memset the last one to NULL
s->len--; b->len--;
if (s->parent != NULL) { if (b->parent != NULL) {
uiControlSetParent(removed, NULL); uiControlSetParent(removed, NULL);
uiParentUpdate(s->parent); uiParentUpdate(b->parent);
} }
} }
static int stackPadded(uiStack *ss) static int stackPadded(uiStack *ss)
{ {
stack *s = (stack *) ss; stack *b = (stack *) ss;
return s->padded; return b->padded;
} }
static void stackSetPadded(uiStack *ss, int padded) static void stackSetPadded(uiStack *ss, int padded)
{ {
stack *s = (stack *) ss; stack *b = (stack *) ss;
s->padded = padded; b->padded = padded;
if (s->parent != NULL) if (b->parent != NULL)
uiParentUpdate(s->parent); uiParentUpdate(b->parent);
} }
uiStack *uiNewHorizontalStack(void) uiStack *uiNewHorizontalStack(void)
{ {
stack *s; stack *b;
s = uiNew(stack); b = uiNew(stack);
uiControl(s)->Destroy = stackDestroy; uiControl(b)->Destroy = stackDestroy;
uiControl(s)->Handle = stackHandle; uiControl(b)->Handle = stackHandle;
uiControl(s)->SetParent = stackSetParent; uiControl(b)->SetParent = stackSetParent;
uiControl(s)->PreferredSize = stackPreferredSize; uiControl(b)->PreferredSize = stackPreferredSize;
uiControl(s)->Resize = stackResize; uiControl(b)->Resize = stackResize;
uiControl(s)->Visible = stackVisible; uiControl(b)->Visible = stackVisible;
uiControl(s)->Show = stackShow; uiControl(b)->Show = stackShow;
uiControl(s)->Hide = stackHide; uiControl(b)->Hide = stackHide;
uiControl(s)->ContainerShow = stackContainerShow; uiControl(b)->ContainerShow = stackContainerShow;
uiControl(s)->ContainerHide = stackContainerHide; uiControl(b)->ContainerHide = stackContainerHide;
uiControl(s)->Enable = stackEnable; uiControl(b)->Enable = stackEnable;
uiControl(s)->Disable = stackDisable; uiControl(b)->Disable = stackDisable;
uiControl(s)->ContainerEnable = stackContainerEnable; uiControl(b)->ContainerEnable = stackContainerEnable;
uiControl(s)->ContainerDisable = stackContainerDisable; uiControl(b)->ContainerDisable = stackContainerDisable;
uiStack(s)->Append = stackAppend; uiStack(b)->Append = stackAppend;
uiStack(s)->Delete = stackDelete; uiStack(b)->Delete = stackDelete;
uiStack(s)->Padded = stackPadded; uiStack(b)->Padded = stackPadded;
uiStack(s)->SetPadded = stackSetPadded; uiStack(b)->SetPadded = stackSetPadded;
return uiStack(s); return uiStack(b);
} }
uiStack *uiNewVerticalStack(void) uiStack *uiNewVerticalStack(void)
{ {
uiStack *ss; uiStack *ss;
stack *s; stack *b;
ss = uiNewHorizontalStack(); ss = uiNewHorizontalStack();
s = (stack *) ss; b = (stack *) ss;
s->vertical = 1; b->vertical = 1;
return ss; return ss;
} }