2015-04-07 17:53:09 -05:00
|
|
|
// 7 april 2015
|
|
|
|
#include "uipriv.h"
|
|
|
|
|
|
|
|
typedef struct stack stack;
|
2015-04-09 15:57:55 -05:00
|
|
|
typedef struct stackControl stackControl;
|
2015-04-07 17:53:09 -05:00
|
|
|
|
|
|
|
struct stack {
|
2015-04-11 12:45:59 -05:00
|
|
|
stackControl *controls;
|
2015-04-07 17:53:09 -05:00
|
|
|
uintmax_t len;
|
|
|
|
uintmax_t cap;
|
|
|
|
int vertical;
|
2015-04-12 19:08:32 -05:00
|
|
|
uiParent *parent;
|
2015-04-09 14:59:40 -05:00
|
|
|
int padded;
|
2015-04-11 13:44:40 -05:00
|
|
|
int userHid;
|
|
|
|
int containerHid;
|
|
|
|
int userDisabled;
|
|
|
|
int containerDisabled;
|
2015-04-07 17:53:09 -05:00
|
|
|
};
|
|
|
|
|
2015-04-09 15:57:55 -05:00
|
|
|
struct stackControl {
|
2015-04-11 12:45:59 -05:00
|
|
|
uiControl *c;
|
2015-04-09 15:57:55 -05:00
|
|
|
int stretchy;
|
|
|
|
intmax_t width; // both used by resize(); preallocated to save time and reduce risk of failure
|
|
|
|
intmax_t height;
|
|
|
|
};
|
2015-04-07 17:53:09 -05:00
|
|
|
|
2015-04-07 23:43:35 -05:00
|
|
|
static void stackDestroy(uiControl *c)
|
|
|
|
{
|
2015-04-09 15:57:55 -05:00
|
|
|
stack *s = (stack *) (c->data);
|
2015-04-07 23:43:35 -05:00
|
|
|
uintmax_t i;
|
|
|
|
|
2015-04-09 15:57:55 -05:00
|
|
|
for (i = 0; i < s->len; i++)
|
2015-04-11 12:45:59 -05:00
|
|
|
uiControlDestroy(s->controls[i].c);
|
2015-04-07 23:43:35 -05:00
|
|
|
uiFree(s->controls);
|
|
|
|
uiFree(s);
|
2015-04-09 15:57:55 -05:00
|
|
|
uiFree(c);
|
2015-04-07 23:43:35 -05:00
|
|
|
}
|
|
|
|
|
2015-04-07 17:53:09 -05:00
|
|
|
static uintptr_t stackHandle(uiControl *c)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-12 19:08:32 -05:00
|
|
|
static void stackSetParent(uiControl *c, uiParent *parent)
|
2015-04-07 17:53:09 -05:00
|
|
|
{
|
2015-04-09 15:57:55 -05:00
|
|
|
stack *s = (stack *) (c->data);
|
2015-04-07 17:53:09 -05:00
|
|
|
uintmax_t i;
|
|
|
|
|
2015-04-08 17:04:46 -05:00
|
|
|
s->parent = parent;
|
2015-04-09 15:57:55 -05:00
|
|
|
for (i = 0; i < s->len; i++)
|
2015-04-11 12:45:59 -05:00
|
|
|
uiControlSetParent(s->controls[i].c, s->parent);
|
2015-04-12 19:08:32 -05:00
|
|
|
uiParentUpdate(s->parent);
|
2015-04-07 17:53:09 -05:00
|
|
|
}
|
|
|
|
|
2015-04-09 15:21:09 -05:00
|
|
|
static void stackRemoveParent(uiControl *c)
|
|
|
|
{
|
2015-04-09 15:57:55 -05:00
|
|
|
stack *s = (stack *) (c->data);
|
2015-04-09 15:21:09 -05:00
|
|
|
uintmax_t i;
|
2015-04-12 19:08:32 -05:00
|
|
|
uiParent *oldparent;
|
2015-04-09 15:21:09 -05:00
|
|
|
|
|
|
|
oldparent = s->parent;
|
2015-04-12 19:08:32 -05:00
|
|
|
s->parent = NULL;
|
2015-04-09 15:57:55 -05:00
|
|
|
for (i = 0; i < s->len; i++)
|
2015-04-11 12:45:59 -05:00
|
|
|
uiControlRemoveParent(s->controls[i].c);
|
2015-04-12 19:08:32 -05:00
|
|
|
uiParentUpdate(oldparent);
|
2015-04-09 15:21:09 -05:00
|
|
|
}
|
|
|
|
|
2015-04-09 15:57:55 -05:00
|
|
|
static void stackPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
|
2015-04-07 17:53:09 -05:00
|
|
|
{
|
2015-04-09 15:57:55 -05:00
|
|
|
stack *s = (stack *) (c->data);
|
2015-04-07 17:53:09 -05:00
|
|
|
int xpadding, ypadding;
|
|
|
|
uintmax_t nStretchy;
|
2015-04-11 12:51:37 -05:00
|
|
|
// these two contain the largest preferred width and height of all stretchy controls in the stack
|
|
|
|
// all stretchy controls will use this value to determine the final preferred size
|
|
|
|
intmax_t maxStretchyWidth, maxStretchyHeight;
|
2015-04-07 17:53:09 -05:00
|
|
|
uintmax_t i;
|
2015-04-11 12:51:37 -05:00
|
|
|
intmax_t preferredWidth, preferredHeight;
|
2015-04-07 17:53:09 -05:00
|
|
|
|
2015-04-09 15:57:55 -05:00
|
|
|
*width = 0;
|
|
|
|
*height = 0;
|
2015-04-07 17:53:09 -05:00
|
|
|
if (s->len == 0)
|
2015-04-09 15:57:55 -05:00
|
|
|
return;
|
2015-04-07 17:53:09 -05:00
|
|
|
|
2015-04-09 14:59:40 -05:00
|
|
|
// 0) get this Stack's padding
|
2015-04-07 17:53:09 -05:00
|
|
|
xpadding = 0;
|
|
|
|
ypadding = 0;
|
2015-04-09 14:59:40 -05:00
|
|
|
if (s->padded) {
|
2015-04-09 15:57:55 -05:00
|
|
|
xpadding = d->xPadding;
|
|
|
|
ypadding = d->yPadding;
|
2015-04-09 14:59:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 1) initialize the desired rect with the needed padding
|
2015-04-07 17:53:09 -05:00
|
|
|
if (s->vertical)
|
2015-04-09 15:57:55 -05:00
|
|
|
*height = (s->len - 1) * ypadding;
|
2015-04-07 17:53:09 -05:00
|
|
|
else
|
2015-04-09 15:57:55 -05:00
|
|
|
*width = (s->len - 1) * xpadding;
|
2015-04-07 17:53:09 -05:00
|
|
|
|
|
|
|
// 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
|
|
|
|
nStretchy = 0;
|
2015-04-11 12:51:37 -05:00
|
|
|
maxStretchyWidth = 0;
|
|
|
|
maxStretchyHeight = 0;
|
2015-04-07 17:53:09 -05:00
|
|
|
for (i = 0; i < s->len; i++) {
|
2015-04-11 13:30:07 -05:00
|
|
|
if (!uiControlVisible(s->controls[i].c))
|
|
|
|
continue;
|
2015-04-11 12:51:37 -05:00
|
|
|
uiControlPreferredSize(s->controls[i].c, d, &preferredWidth, &preferredHeight);
|
2015-04-11 12:45:59 -05:00
|
|
|
if (s->controls[i].stretchy) {
|
2015-04-07 17:53:09 -05:00
|
|
|
nStretchy++;
|
2015-04-11 12:51:37 -05:00
|
|
|
if (maxStretchyWidth < preferredWidth)
|
|
|
|
maxStretchyWidth = preferredWidth;
|
|
|
|
if (maxStretchyHeight < preferredHeight)
|
|
|
|
maxStretchyHeight = preferredHeight;
|
2015-04-07 17:53:09 -05:00
|
|
|
}
|
|
|
|
if (s->vertical) {
|
2015-04-11 12:51:37 -05:00
|
|
|
if (*width < preferredWidth)
|
|
|
|
*width = preferredWidth;
|
2015-04-11 12:45:59 -05:00
|
|
|
if (!s->controls[i].stretchy)
|
2015-04-11 12:51:37 -05:00
|
|
|
*height += preferredHeight;
|
2015-04-07 17:53:09 -05:00
|
|
|
} else {
|
2015-04-11 12:45:59 -05:00
|
|
|
if (!s->controls[i].stretchy)
|
2015-04-11 12:51:37 -05:00
|
|
|
*width += preferredWidth;
|
|
|
|
if (*height < preferredHeight)
|
|
|
|
*height = preferredHeight;
|
2015-04-07 17:53:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3) and now we can add in stretchy controls
|
|
|
|
if (s->vertical)
|
2015-04-11 12:51:37 -05:00
|
|
|
*height += nStretchy * maxStretchyHeight;
|
2015-04-07 17:53:09 -05:00
|
|
|
else
|
2015-04-11 12:51:37 -05:00
|
|
|
*width += nStretchy * maxStretchyWidth;
|
2015-04-07 17:53:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void stackResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
|
|
|
|
{
|
2015-04-09 15:57:55 -05:00
|
|
|
stack *s = (stack *) (c->data);
|
2015-04-07 17:53:09 -05:00
|
|
|
int xpadding, ypadding;
|
|
|
|
uintmax_t nStretchy;
|
|
|
|
intmax_t stretchywid, stretchyht;
|
|
|
|
uintmax_t i;
|
2015-04-11 12:51:37 -05:00
|
|
|
intmax_t preferredWidth, preferredHeight;
|
2015-04-07 17:53:09 -05:00
|
|
|
|
|
|
|
if (s->len == 0)
|
|
|
|
return;
|
|
|
|
|
2015-04-09 14:59:40 -05:00
|
|
|
// -1) get this Stack's padding
|
2015-04-07 17:53:09 -05:00
|
|
|
xpadding = 0;
|
|
|
|
ypadding = 0;
|
2015-04-09 14:59:40 -05:00
|
|
|
if (s->padded) {
|
2015-04-09 15:57:55 -05:00
|
|
|
xpadding = d->xPadding;
|
|
|
|
ypadding = d->yPadding;
|
2015-04-09 14:59:40 -05:00
|
|
|
}
|
2015-04-07 17:53:09 -05:00
|
|
|
|
|
|
|
// 0) inset the available rect by the needed padding
|
|
|
|
if (s->vertical)
|
|
|
|
height -= (s->len - 1) * ypadding;
|
|
|
|
else
|
|
|
|
width -= (s->len - 1) * xpadding;
|
|
|
|
|
|
|
|
// 1) get width and height of non-stretchy controls
|
|
|
|
// this will tell us how much space will be left for stretchy controls
|
|
|
|
stretchywid = width;
|
|
|
|
stretchyht = height;
|
|
|
|
nStretchy = 0;
|
|
|
|
for (i = 0; i < s->len; i++) {
|
2015-04-11 13:30:07 -05:00
|
|
|
if (!uiControlVisible(s->controls[i].c))
|
|
|
|
continue;
|
2015-04-11 12:45:59 -05:00
|
|
|
if (s->controls[i].stretchy) {
|
2015-04-07 17:53:09 -05:00
|
|
|
nStretchy++;
|
|
|
|
continue;
|
|
|
|
}
|
2015-04-11 12:51:37 -05:00
|
|
|
uiControlPreferredSize(s->controls[i].c, d, &preferredWidth, &preferredHeight);
|
2015-04-07 17:53:09 -05:00
|
|
|
if (s->vertical) { // all controls have same width
|
2015-04-11 12:45:59 -05:00
|
|
|
s->controls[i].width = width;
|
2015-04-11 12:51:37 -05:00
|
|
|
s->controls[i].height = preferredHeight;
|
|
|
|
stretchyht -= preferredHeight;
|
2015-04-07 17:53:09 -05:00
|
|
|
} else { // all controls have same height
|
2015-04-11 12:51:37 -05:00
|
|
|
s->controls[i].width = preferredWidth;
|
2015-04-11 12:45:59 -05:00
|
|
|
s->controls[i].height = height;
|
2015-04-11 12:51:37 -05:00
|
|
|
stretchywid -= preferredWidth;
|
2015-04-07 17:53:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2) now get the size of stretchy controls
|
|
|
|
if (nStretchy != 0)
|
|
|
|
if (s->vertical)
|
|
|
|
stretchyht /= nStretchy;
|
|
|
|
else
|
|
|
|
stretchywid /= nStretchy;
|
2015-04-11 13:30:07 -05:00
|
|
|
for (i = 0; i < s->len; i++) {
|
|
|
|
if (!uiControlVisible(s->controls[i].c))
|
|
|
|
continue;
|
2015-04-11 12:45:59 -05:00
|
|
|
if (s->controls[i].stretchy) {
|
|
|
|
s->controls[i].width = stretchywid;
|
|
|
|
s->controls[i].height = stretchyht;
|
2015-04-07 17:53:09 -05:00
|
|
|
}
|
2015-04-11 13:30:07 -05:00
|
|
|
}
|
2015-04-07 17:53:09 -05:00
|
|
|
|
|
|
|
// 3) now we can position controls
|
|
|
|
for (i = 0; i < s->len; i++) {
|
2015-04-11 13:30:07 -05:00
|
|
|
if (!uiControlVisible(s->controls[i].c))
|
|
|
|
continue;
|
2015-04-11 12:45:59 -05:00
|
|
|
uiControlResize(s->controls[i].c, x, y, s->controls[i].width, s->controls[i].height, d);
|
2015-04-07 17:53:09 -05:00
|
|
|
if (s->vertical)
|
2015-04-11 12:45:59 -05:00
|
|
|
y += s->controls[i].height + ypadding;
|
2015-04-07 17:53:09 -05:00
|
|
|
else
|
2015-04-11 12:45:59 -05:00
|
|
|
x += s->controls[i].width + xpadding;
|
2015-04-07 17:53:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-11 13:44:40 -05:00
|
|
|
static int stackVisible(uiControl *c)
|
|
|
|
{
|
|
|
|
stack *s = (stack *) (c->data);
|
|
|
|
|
|
|
|
return !(s->userHid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stackShow(uiControl *c)
|
|
|
|
{
|
|
|
|
stack *s = (stack *) (c->data);
|
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
s->userHid = 0;
|
|
|
|
if (!s->containerHid) {
|
|
|
|
for (i = 0; i < s->len; i++)
|
|
|
|
uiControlContainerShow(s->controls[i].c);
|
2015-04-12 19:08:32 -05:00
|
|
|
if (s->parent != NULL)
|
|
|
|
uiParentUpdate(s->parent);
|
2015-04-11 13:44:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stackHide(uiControl *c)
|
|
|
|
{
|
|
|
|
stack *s = (stack *) (c->data);
|
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
s->userHid = 1;
|
|
|
|
for (i = 0; i < s->len; i++)
|
|
|
|
uiControlContainerHide(s->controls[i].c);
|
2015-04-12 19:08:32 -05:00
|
|
|
if (s->parent != NULL)
|
|
|
|
uiParentUpdate(s->parent);
|
2015-04-11 13:44:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void stackContainerShow(uiControl *c)
|
|
|
|
{
|
|
|
|
stack *s = (stack *) (c->data);
|
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
s->containerHid = 0;
|
|
|
|
if (!s->userHid) {
|
|
|
|
for (i = 0; i < s->len; i++)
|
|
|
|
uiControlContainerShow(s->controls[i].c);
|
2015-04-12 19:08:32 -05:00
|
|
|
if (s->parent != NULL)
|
|
|
|
uiParentUpdate(s->parent);
|
2015-04-11 13:44:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stackContainerHide(uiControl *c)
|
|
|
|
{
|
|
|
|
stack *s = (stack *) (c->data);
|
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
s->containerHid = 1;
|
|
|
|
for (i = 0; i < s->len; i++)
|
|
|
|
uiControlContainerHide(s->controls[i].c);
|
2015-04-12 19:08:32 -05:00
|
|
|
if (s->parent != NULL)
|
|
|
|
uiParentUpdate(s->parent);
|
2015-04-11 13:44:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void stackEnable(uiControl *c)
|
|
|
|
{
|
|
|
|
stack *s = (stack *) (c->data);
|
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
s->userDisabled = 0;
|
|
|
|
if (!s->containerDisabled)
|
|
|
|
for (i = 0; i < s->len; i++)
|
|
|
|
uiControlContainerEnable(s->controls[i].c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stackDisable(uiControl *c)
|
|
|
|
{
|
|
|
|
stack *s = (stack *) (c->data);
|
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
s->userDisabled = 1;
|
|
|
|
for (i = 0; i < s->len; i++)
|
|
|
|
uiControlContainerDisable(s->controls[i].c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stackContainerEnable(uiControl *c)
|
|
|
|
{
|
|
|
|
stack *s = (stack *) (c->data);
|
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
s->containerDisabled = 0;
|
|
|
|
if (!s->userDisabled)
|
|
|
|
for (i = 0; i < s->len; i++)
|
|
|
|
uiControlContainerEnable(s->controls[i].c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stackContainerDisable(uiControl *c)
|
|
|
|
{
|
|
|
|
stack *s = (stack *) (c->data);
|
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
s->containerDisabled = 1;
|
|
|
|
for (i = 0; i < s->len; i++)
|
|
|
|
uiControlContainerDisable(s->controls[i].c);
|
|
|
|
}
|
|
|
|
|
2015-04-07 17:53:09 -05:00
|
|
|
uiControl *uiNewHorizontalStack(void)
|
|
|
|
{
|
2015-04-09 15:57:55 -05:00
|
|
|
uiControl *c;
|
2015-04-07 17:53:09 -05:00
|
|
|
stack *s;
|
|
|
|
|
2015-04-09 15:57:55 -05:00
|
|
|
c = uiNew(uiControl);
|
2015-04-07 17:53:09 -05:00
|
|
|
s = uiNew(stack);
|
|
|
|
|
2015-04-09 15:57:55 -05:00
|
|
|
c->data = s;
|
|
|
|
c->destroy = stackDestroy;
|
|
|
|
c->handle = stackHandle;
|
|
|
|
c->setParent = stackSetParent;
|
|
|
|
c->removeParent = stackRemoveParent;
|
|
|
|
c->preferredSize = stackPreferredSize;
|
|
|
|
c->resize = stackResize;
|
2015-04-11 13:44:40 -05:00
|
|
|
c->visible = stackVisible;
|
|
|
|
c->show = stackShow;
|
|
|
|
c->hide = stackHide;
|
|
|
|
c->containerShow = stackContainerShow;
|
|
|
|
c->containerHide = stackContainerHide;
|
|
|
|
c->enable = stackEnable;
|
|
|
|
c->disable = stackDisable;
|
|
|
|
c->containerEnable = stackContainerEnable;
|
|
|
|
c->containerDisable = stackContainerDisable;
|
2015-04-07 17:53:09 -05:00
|
|
|
|
2015-04-09 15:57:55 -05:00
|
|
|
return c;
|
2015-04-07 17:53:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uiControl *uiNewVerticalStack(void)
|
|
|
|
{
|
|
|
|
uiControl *c;
|
2015-04-09 15:57:55 -05:00
|
|
|
stack *s;
|
2015-04-07 17:53:09 -05:00
|
|
|
|
2015-04-07 18:08:39 -05:00
|
|
|
c = uiNewHorizontalStack();
|
2015-04-09 15:57:55 -05:00
|
|
|
s = (stack *) (c->data);
|
|
|
|
s->vertical = 1;
|
2015-04-07 17:53:09 -05:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define stackCapGrow 32
|
|
|
|
|
|
|
|
void uiStackAdd(uiControl *st, uiControl *c, int stretchy)
|
|
|
|
{
|
2015-04-09 15:57:55 -05:00
|
|
|
stack *s = (stack *) (st->data);
|
2015-04-07 17:53:09 -05:00
|
|
|
|
|
|
|
if (s->len >= s->cap) {
|
|
|
|
s->cap += stackCapGrow;
|
2015-04-11 18:34:38 -05:00
|
|
|
s->controls = (stackControl *) uiRealloc(s->controls, s->cap * sizeof (stackControl), "stackControl[]");
|
2015-04-07 17:53:09 -05:00
|
|
|
}
|
2015-04-11 12:45:59 -05:00
|
|
|
s->controls[s->len].c = c;
|
|
|
|
s->controls[s->len].stretchy = stretchy;
|
2015-04-07 17:53:09 -05:00
|
|
|
if (s->parent != 0)
|
2015-04-11 12:45:59 -05:00
|
|
|
uiControlSetParent(s->controls[s->len].c, s->parent);
|
2015-04-07 17:53:09 -05:00
|
|
|
s->len++;
|
2015-04-12 19:08:32 -05:00
|
|
|
if (s->parent != NULL)
|
|
|
|
uiParentUpdate(s->parent);
|
2015-04-07 17:53:09 -05:00
|
|
|
}
|
2015-04-09 14:59:40 -05:00
|
|
|
|
2015-04-09 19:04:18 -05:00
|
|
|
int uiStackPadded(uiControl *c)
|
|
|
|
{
|
|
|
|
stack *s = (stack *) (c->data);
|
|
|
|
|
|
|
|
return s->padded;
|
|
|
|
}
|
2015-04-09 14:59:40 -05:00
|
|
|
|
2015-04-09 15:57:55 -05:00
|
|
|
void uiStackSetPadded(uiControl *c, int padded)
|
2015-04-09 14:59:40 -05:00
|
|
|
{
|
2015-04-09 15:57:55 -05:00
|
|
|
stack *s = (stack *) (c->data);
|
2015-04-09 14:59:40 -05:00
|
|
|
|
|
|
|
s->padded = padded;
|
2015-04-12 19:08:32 -05:00
|
|
|
if (s->parent != NULL)
|
|
|
|
uiParentUpdate(s->parent);
|
2015-04-09 14:59:40 -05:00
|
|
|
}
|