libui/box.c

398 lines
8.8 KiB
C
Raw Normal View History

// 7 april 2015
#include "uipriv.h"
2015-04-20 09:19:25 -05:00
typedef struct box box;
typedef struct boxControl boxControl;
2015-04-14 08:46:24 -05:00
2015-04-20 09:19:25 -05:00
struct box {
uiBox s;
boxControl *controls;
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;
int userHid;
int containerHid;
int userDisabled;
int containerDisabled;
};
2015-04-20 09:19:25 -05:00
struct boxControl {
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-20 09:19:25 -05:00
static void boxDestroy(uiControl *c)
{
2015-04-20 09:19:25 -05:00
box *b = (box *) c;
uintmax_t i;
if (b->parent != NULL)
complain("attempt to destroy a uiControl at %p while it still has a parent %p", c, b->parent);
for (i = 0; i < b->len; i++)
uiControlDestroy(b->controls[i].c);
uiFree(b->controls);
uiFree(b);
}
2015-04-20 09:19:25 -05:00
static uintptr_t boxHandle(uiControl *c)
{
return 0;
}
2015-04-20 09:19:25 -05:00
static void boxSetParent(uiControl *c, uiParent *parent)
{
2015-04-20 09:19:25 -05:00
box *b = (box *) c;
uintmax_t i;
2015-04-12 19:08:32 -05:00
uiParent *oldparent;
oldparent = b->parent;
b->parent = parent;
for (i = 0; i < b->len; i++)
uiControlSetParent(b->controls[i].c, b->parent);
if (oldparent != NULL)
uiParentUpdate(oldparent);
if (b->parent != NULL)
uiParentUpdate(b->parent);
}
2015-04-20 09:19:25 -05:00
static void boxPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
{
2015-04-20 09:19:25 -05:00
box *b = (box *) c;
int xpadding, ypadding;
uintmax_t nStretchy;
2015-04-20 09:19:25 -05:00
// these two contain the largest preferred width and height of all stretchy controls in the box
// all stretchy controls will use this value to determine the final preferred size
intmax_t maxStretchyWidth, maxStretchyHeight;
uintmax_t i;
intmax_t preferredWidth, preferredHeight;
2015-04-09 15:57:55 -05:00
*width = 0;
*height = 0;
if (b->len == 0)
2015-04-09 15:57:55 -05:00
return;
2015-04-20 09:19:25 -05:00
// 0) get this Box's padding
xpadding = 0;
ypadding = 0;
if (b->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
if (b->vertical)
*height = (b->len - 1) * ypadding;
else
*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
// we still add in like direction of stretchy controls
nStretchy = 0;
maxStretchyWidth = 0;
maxStretchyHeight = 0;
for (i = 0; i < b->len; i++) {
if (!uiControlVisible(b->controls[i].c))
continue;
uiControlPreferredSize(b->controls[i].c, d, &preferredWidth, &preferredHeight);
if (b->controls[i].stretchy) {
nStretchy++;
if (maxStretchyWidth < preferredWidth)
maxStretchyWidth = preferredWidth;
if (maxStretchyHeight < preferredHeight)
maxStretchyHeight = preferredHeight;
}
if (b->vertical) {
if (*width < preferredWidth)
*width = preferredWidth;
if (!b->controls[i].stretchy)
*height += preferredHeight;
} else {
if (!b->controls[i].stretchy)
*width += preferredWidth;
if (*height < preferredHeight)
*height = preferredHeight;
}
}
// 3) and now we can add in stretchy controls
if (b->vertical)
*height += nStretchy * maxStretchyHeight;
else
*width += nStretchy * maxStretchyWidth;
}
2015-04-20 09:19:25 -05:00
static void boxResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
{
2015-04-20 09:19:25 -05:00
box *b = (box *) c;
int xpadding, ypadding;
uintmax_t nStretchy;
intmax_t stretchywid, stretchyht;
uintmax_t i;
intmax_t preferredWidth, preferredHeight;
if (b->len == 0)
return;
2015-04-20 09:19:25 -05:00
// -1) get this Box's padding
xpadding = 0;
ypadding = 0;
if (b->padded) {
2015-04-09 15:57:55 -05:00
xpadding = d->xPadding;
ypadding = d->yPadding;
2015-04-09 14:59:40 -05:00
}
// 0) inset the available rect by the needed padding
if (b->vertical)
height -= (b->len - 1) * ypadding;
else
width -= (b->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 < b->len; i++) {
if (!uiControlVisible(b->controls[i].c))
continue;
if (b->controls[i].stretchy) {
nStretchy++;
continue;
}
uiControlPreferredSize(b->controls[i].c, d, &preferredWidth, &preferredHeight);
if (b->vertical) { // all controls have same width
b->controls[i].width = width;
b->controls[i].height = preferredHeight;
stretchyht -= preferredHeight;
} else { // all controls have same height
b->controls[i].width = preferredWidth;
b->controls[i].height = height;
stretchywid -= preferredWidth;
}
}
// 2) now get the size of stretchy controls
if (nStretchy != 0)
if (b->vertical)
stretchyht /= nStretchy;
else
stretchywid /= nStretchy;
for (i = 0; i < b->len; i++) {
if (!uiControlVisible(b->controls[i].c))
continue;
if (b->controls[i].stretchy) {
b->controls[i].width = stretchywid;
b->controls[i].height = stretchyht;
}
}
// 3) now we can position controls
for (i = 0; i < b->len; i++) {
if (!uiControlVisible(b->controls[i].c))
continue;
uiControlResize(b->controls[i].c, x, y, b->controls[i].width, b->controls[i].height, d);
if (b->vertical)
y += b->controls[i].height + ypadding;
else
x += b->controls[i].width + xpadding;
}
}
2015-04-20 09:19:25 -05:00
static int boxVisible(uiControl *c)
{
2015-04-20 09:19:25 -05:00
box *b = (box *) c;
return !(b->userHid);
}
2015-04-20 09:19:25 -05:00
static void boxShow(uiControl *c)
{
2015-04-20 09:19:25 -05:00
box *b = (box *) c;
uintmax_t i;
b->userHid = 0;
if (!b->containerHid) {
for (i = 0; i < b->len; i++)
uiControlContainerShow(b->controls[i].c);
if (b->parent != NULL)
uiParentUpdate(b->parent);
}
}
2015-04-20 09:19:25 -05:00
static void boxHide(uiControl *c)
{
2015-04-20 09:19:25 -05:00
box *b = (box *) c;
uintmax_t i;
b->userHid = 1;
for (i = 0; i < b->len; i++)
uiControlContainerHide(b->controls[i].c);
if (b->parent != NULL)
uiParentUpdate(b->parent);
}
2015-04-20 09:19:25 -05:00
static void boxContainerShow(uiControl *c)
{
2015-04-20 09:19:25 -05:00
box *b = (box *) c;
uintmax_t i;
b->containerHid = 0;
if (!b->userHid) {
for (i = 0; i < b->len; i++)
uiControlContainerShow(b->controls[i].c);
if (b->parent != NULL)
uiParentUpdate(b->parent);
}
}
2015-04-20 09:19:25 -05:00
static void boxContainerHide(uiControl *c)
{
2015-04-20 09:19:25 -05:00
box *b = (box *) c;
uintmax_t i;
b->containerHid = 1;
for (i = 0; i < b->len; i++)
uiControlContainerHide(b->controls[i].c);
if (b->parent != NULL)
uiParentUpdate(b->parent);
}
2015-04-20 09:19:25 -05:00
static void boxEnable(uiControl *c)
{
2015-04-20 09:19:25 -05:00
box *b = (box *) c;
uintmax_t i;
b->userDisabled = 0;
if (!b->containerDisabled)
for (i = 0; i < b->len; i++)
uiControlContainerEnable(b->controls[i].c);
}
2015-04-20 09:19:25 -05:00
static void boxDisable(uiControl *c)
{
2015-04-20 09:19:25 -05:00
box *b = (box *) c;
uintmax_t i;
b->userDisabled = 1;
for (i = 0; i < b->len; i++)
uiControlContainerDisable(b->controls[i].c);
}
2015-04-20 09:19:25 -05:00
static void boxContainerEnable(uiControl *c)
{
2015-04-20 09:19:25 -05:00
box *b = (box *) c;
uintmax_t i;
b->containerDisabled = 0;
if (!b->userDisabled)
for (i = 0; i < b->len; i++)
uiControlContainerEnable(b->controls[i].c);
}
2015-04-20 09:19:25 -05:00
static void boxContainerDisable(uiControl *c)
{
2015-04-20 09:19:25 -05:00
box *b = (box *) c;
uintmax_t i;
b->containerDisabled = 1;
for (i = 0; i < b->len; i++)
uiControlContainerDisable(b->controls[i].c);
}
2015-04-20 09:19:25 -05:00
#define boxCapGrow 32
2015-04-20 09:19:25 -05:00
static void boxAppend(uiBox *ss, uiControl *c, int stretchy)
{
2015-04-20 09:19:25 -05:00
box *b = (box *) ss;
if (b->len >= b->cap) {
2015-04-20 09:19:25 -05:00
b->cap += boxCapGrow;
b->controls = (boxControl *) uiRealloc(b->controls, b->cap * sizeof (boxControl), "boxControl[]");
}
b->controls[b->len].c = c;
b->controls[b->len].stretchy = stretchy;
b->len++; // must be here for parent updates to work
if (b->parent != NULL) {
uiControlSetParent(b->controls[b->len - 1].c, b->parent);
uiParentUpdate(b->parent);
2015-04-13 13:05:07 -05:00
}
}
2015-04-09 14:59:40 -05:00
2015-04-20 09:19:25 -05:00
static void boxDelete(uiBox *ss, uintmax_t index)
{
2015-04-20 09:19:25 -05:00
box *b = (box *) ss;
uiControl *removed;
uintmax_t i;
removed = b->controls[index].c;
// TODO switch to memmove?
for (i = index; i < b->len - 1; i++)
b->controls[i] = b->controls[i + 1];
// TODO memset the last one to NULL
b->len--;
if (b->parent != NULL) {
uiControlSetParent(removed, NULL);
uiParentUpdate(b->parent);
}
}
2015-04-20 09:19:25 -05:00
static int boxPadded(uiBox *ss)
2015-04-09 19:04:18 -05:00
{
2015-04-20 09:19:25 -05:00
box *b = (box *) ss;
2015-04-09 19:04:18 -05:00
return b->padded;
2015-04-09 19:04:18 -05:00
}
2015-04-09 14:59:40 -05:00
2015-04-20 09:19:25 -05:00
static void boxSetPadded(uiBox *ss, int padded)
2015-04-09 14:59:40 -05:00
{
2015-04-20 09:19:25 -05:00
box *b = (box *) ss;
2015-04-09 14:59:40 -05:00
b->padded = padded;
if (b->parent != NULL)
uiParentUpdate(b->parent);
2015-04-09 14:59:40 -05:00
}
2015-04-15 21:34:22 -05:00
2015-04-20 09:19:25 -05:00
uiBox *uiNewHorizontalBox(void)
2015-04-15 21:34:22 -05:00
{
2015-04-20 09:19:25 -05:00
box *b;
b = uiNew(box);
uiControl(b)->Destroy = boxDestroy;
uiControl(b)->Handle = boxHandle;
uiControl(b)->SetParent = boxSetParent;
uiControl(b)->PreferredSize = boxPreferredSize;
uiControl(b)->Resize = boxResize;
uiControl(b)->Visible = boxVisible;
uiControl(b)->Show = boxShow;
uiControl(b)->Hide = boxHide;
uiControl(b)->ContainerShow = boxContainerShow;
uiControl(b)->ContainerHide = boxContainerHide;
uiControl(b)->Enable = boxEnable;
uiControl(b)->Disable = boxDisable;
uiControl(b)->ContainerEnable = boxContainerEnable;
uiControl(b)->ContainerDisable = boxContainerDisable;
uiBox(b)->Append = boxAppend;
uiBox(b)->Delete = boxDelete;
uiBox(b)->Padded = boxPadded;
uiBox(b)->SetPadded = boxSetPadded;
return uiBox(b);
2015-04-15 21:34:22 -05:00
}
2015-04-20 09:19:25 -05:00
uiBox *uiNewVerticalBox(void)
2015-04-15 21:34:22 -05:00
{
2015-04-20 09:19:25 -05:00
uiBox *ss;
box *b;
2015-04-15 21:34:22 -05:00
2015-04-20 09:19:25 -05:00
ss = uiNewHorizontalBox();
b = (box *) ss;
b->vertical = 1;
2015-04-15 21:34:22 -05:00
return ss;
}