2015-04-22 16:04:30 -05:00
|
|
|
// 7 april 2015
|
2015-05-16 00:45:20 -05:00
|
|
|
#include "out/ui.h"
|
2015-04-22 16:04:30 -05:00
|
|
|
#include "uipriv.h"
|
|
|
|
|
|
|
|
struct box {
|
2015-04-26 22:01:00 -05:00
|
|
|
uiBox b;
|
2015-05-29 19:40:54 -05:00
|
|
|
void (*baseCommitDestroy)(uiControl *);
|
|
|
|
uintptr_t handle;
|
2015-05-07 21:52:27 -05:00
|
|
|
struct ptrArray *controls;
|
2015-04-22 16:04:30 -05:00
|
|
|
int vertical;
|
|
|
|
int padded;
|
2015-05-15 23:44:24 -05:00
|
|
|
void (*baseResize)(uiControl *, intmax_t, intmax_t, intmax_t, intmax_t, uiSizing *);
|
2015-04-22 16:04:30 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
struct boxControl {
|
|
|
|
uiControl *c;
|
|
|
|
int stretchy;
|
|
|
|
intmax_t width; // both used by resize(); preallocated to save time and reduce risk of failure
|
|
|
|
intmax_t height;
|
|
|
|
};
|
|
|
|
|
2015-05-29 21:44:48 -05:00
|
|
|
uiDefineControlType(uiBox, uiTypeBox, struct box)
|
|
|
|
|
2015-05-29 19:40:54 -05:00
|
|
|
static void boxCommitDestroy(uiControl *c)
|
2015-04-22 16:04:30 -05:00
|
|
|
{
|
2015-05-04 14:37:03 -05:00
|
|
|
struct box *b = (struct box *) c;
|
2015-05-07 21:52:27 -05:00
|
|
|
struct boxControl *bc;
|
2015-04-22 16:04:30 -05:00
|
|
|
|
2015-04-26 22:41:18 -05:00
|
|
|
// don't chain up to base here; we need to destroy children ourselves first
|
2015-05-07 21:52:27 -05:00
|
|
|
while (b->controls->len != 0) {
|
|
|
|
bc = ptrArrayIndex(b->controls, struct boxControl *, 0);
|
|
|
|
uiControlSetParent(bc->c, NULL);
|
|
|
|
uiControlDestroy(bc->c);
|
|
|
|
ptrArrayDelete(b->controls, 0);
|
|
|
|
uiFree(bc);
|
2015-04-22 16:54:05 -05:00
|
|
|
}
|
2015-05-07 21:52:27 -05:00
|
|
|
ptrArrayDestroy(b->controls);
|
2015-04-26 22:41:18 -05:00
|
|
|
// NOW we can chain up to base
|
2015-05-29 19:40:54 -05:00
|
|
|
(*(b->baseCommitDestroy))(uiControl(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
static uintptr_t boxHandle(uiControl *c)
|
|
|
|
{
|
|
|
|
struct box *b = (struct box *) c;
|
|
|
|
|
|
|
|
return b->handle;
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void boxPreferredSize(uiControl *c, uiSizing *d, intmax_t *width, intmax_t *height)
|
|
|
|
{
|
2015-05-04 14:37:03 -05:00
|
|
|
struct box *b = (struct box *) c;
|
2015-05-07 21:52:27 -05:00
|
|
|
struct boxControl *bc;
|
2015-04-22 16:04:30 -05:00
|
|
|
int xpadding, ypadding;
|
|
|
|
uintmax_t nStretchy;
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
*width = 0;
|
|
|
|
*height = 0;
|
2015-05-07 21:52:27 -05:00
|
|
|
if (b->controls->len == 0)
|
2015-04-22 16:04:30 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
// 0) get this Box's padding
|
|
|
|
xpadding = 0;
|
|
|
|
ypadding = 0;
|
|
|
|
if (b->padded) {
|
2015-05-07 13:33:46 -05:00
|
|
|
xpadding = d->XPadding;
|
|
|
|
ypadding = d->YPadding;
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 1) initialize the desired rect with the needed padding
|
2015-05-07 15:33:33 -05:00
|
|
|
// TODO this is wrong if any controls are hidden
|
2015-04-22 16:04:30 -05:00
|
|
|
if (b->vertical)
|
2015-05-07 21:52:27 -05:00
|
|
|
*height = (b->controls->len - 1) * ypadding;
|
2015-04-22 16:04:30 -05:00
|
|
|
else
|
2015-05-07 21:52:27 -05:00
|
|
|
*width = (b->controls->len - 1) * xpadding;
|
2015-04-22 16:04:30 -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;
|
|
|
|
maxStretchyWidth = 0;
|
|
|
|
maxStretchyHeight = 0;
|
2015-05-07 21:52:27 -05:00
|
|
|
for (i = 0; i < b->controls->len; i++) {
|
|
|
|
bc = ptrArrayIndex(b->controls, struct boxControl *, i);
|
2015-05-16 00:45:20 -05:00
|
|
|
if (!uiControlContainerVisible(bc->c))
|
2015-04-22 16:04:30 -05:00
|
|
|
continue;
|
2015-05-18 11:04:52 -05:00
|
|
|
// TODO dchild
|
2015-05-07 21:52:27 -05:00
|
|
|
uiControlPreferredSize(bc->c, d, &preferredWidth, &preferredHeight);
|
|
|
|
if (bc->stretchy) {
|
2015-04-22 16:04:30 -05:00
|
|
|
nStretchy++;
|
|
|
|
if (maxStretchyWidth < preferredWidth)
|
|
|
|
maxStretchyWidth = preferredWidth;
|
|
|
|
if (maxStretchyHeight < preferredHeight)
|
|
|
|
maxStretchyHeight = preferredHeight;
|
|
|
|
}
|
|
|
|
if (b->vertical) {
|
|
|
|
if (*width < preferredWidth)
|
|
|
|
*width = preferredWidth;
|
2015-05-07 21:52:27 -05:00
|
|
|
if (!bc->stretchy)
|
2015-04-22 16:04:30 -05:00
|
|
|
*height += preferredHeight;
|
|
|
|
} else {
|
2015-05-07 21:52:27 -05:00
|
|
|
if (!bc->stretchy)
|
2015-04-22 16:04:30 -05:00
|
|
|
*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-05-16 00:45:20 -05:00
|
|
|
static void boxResize(uiControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height, uiSizing *d)
|
2015-04-22 16:04:30 -05:00
|
|
|
{
|
2015-05-04 14:37:03 -05:00
|
|
|
struct box *b = (struct box *) c;
|
2015-05-07 21:52:27 -05:00
|
|
|
struct boxControl *bc;
|
2015-04-22 16:04:30 -05:00
|
|
|
int xpadding, ypadding;
|
|
|
|
uintmax_t nStretchy;
|
|
|
|
intmax_t stretchywid, stretchyht;
|
|
|
|
uintmax_t i;
|
|
|
|
intmax_t preferredWidth, preferredHeight;
|
2015-05-18 11:04:52 -05:00
|
|
|
uiSizing *dchild;
|
2015-04-22 16:04:30 -05:00
|
|
|
|
2015-05-15 23:44:24 -05:00
|
|
|
(*(b->baseResize))(uiControl(b), x, y, width, height, d);
|
|
|
|
|
2015-05-07 21:52:27 -05:00
|
|
|
if (b->controls->len == 0)
|
2015-04-22 16:04:30 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
// -1) get this Box's padding
|
|
|
|
xpadding = 0;
|
|
|
|
ypadding = 0;
|
|
|
|
if (b->padded) {
|
2015-05-07 13:33:46 -05:00
|
|
|
xpadding = d->XPadding;
|
|
|
|
ypadding = d->YPadding;
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 0) inset the available rect by the needed padding
|
2015-05-07 15:33:33 -05:00
|
|
|
// TODO this is incorrect if any controls are hidden
|
2015-04-22 16:04:30 -05:00
|
|
|
if (b->vertical)
|
2015-05-07 21:52:27 -05:00
|
|
|
height -= (b->controls->len - 1) * ypadding;
|
2015-04-22 16:04:30 -05:00
|
|
|
else
|
2015-05-07 21:52:27 -05:00
|
|
|
width -= (b->controls->len - 1) * xpadding;
|
2015-04-22 16:04:30 -05:00
|
|
|
|
|
|
|
// 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;
|
2015-05-07 21:52:27 -05:00
|
|
|
for (i = 0; i < b->controls->len; i++) {
|
|
|
|
bc = ptrArrayIndex(b->controls, struct boxControl *, i);
|
2015-05-16 00:45:20 -05:00
|
|
|
if (!uiControlContainerVisible(bc->c))
|
2015-04-22 16:04:30 -05:00
|
|
|
continue;
|
2015-05-07 21:52:27 -05:00
|
|
|
if (bc->stretchy) {
|
2015-04-22 16:04:30 -05:00
|
|
|
nStretchy++;
|
|
|
|
continue;
|
|
|
|
}
|
2015-05-07 21:52:27 -05:00
|
|
|
uiControlPreferredSize(bc->c, d, &preferredWidth, &preferredHeight);
|
2015-04-22 16:04:30 -05:00
|
|
|
if (b->vertical) { // all controls have same width
|
2015-05-07 21:52:27 -05:00
|
|
|
bc->width = width;
|
|
|
|
bc->height = preferredHeight;
|
2015-04-22 16:04:30 -05:00
|
|
|
stretchyht -= preferredHeight;
|
|
|
|
} else { // all controls have same height
|
2015-05-07 21:52:27 -05:00
|
|
|
bc->width = preferredWidth;
|
|
|
|
bc->height = height;
|
2015-04-22 16:04:30 -05:00
|
|
|
stretchywid -= preferredWidth;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2) now get the size of stretchy controls
|
|
|
|
if (nStretchy != 0)
|
|
|
|
if (b->vertical)
|
|
|
|
stretchyht /= nStretchy;
|
|
|
|
else
|
|
|
|
stretchywid /= nStretchy;
|
2015-05-07 21:52:27 -05:00
|
|
|
for (i = 0; i < b->controls->len; i++) {
|
|
|
|
bc = ptrArrayIndex(b->controls, struct boxControl *, i);
|
2015-05-16 00:45:20 -05:00
|
|
|
if (!uiControlContainerVisible(bc->c))
|
2015-04-22 16:04:30 -05:00
|
|
|
continue;
|
2015-05-07 21:52:27 -05:00
|
|
|
if (bc->stretchy) {
|
|
|
|
bc->width = stretchywid;
|
|
|
|
bc->height = stretchyht;
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3) now we can position controls
|
2015-05-18 11:04:52 -05:00
|
|
|
dchild = uiControlSizing(uiControl(b));
|
2015-05-07 21:52:27 -05:00
|
|
|
for (i = 0; i < b->controls->len; i++) {
|
|
|
|
bc = ptrArrayIndex(b->controls, struct boxControl *, i);
|
2015-05-16 00:45:20 -05:00
|
|
|
if (!uiControlContainerVisible(bc->c))
|
2015-04-22 16:04:30 -05:00
|
|
|
continue;
|
2015-05-18 11:04:52 -05:00
|
|
|
uiControlResize(bc->c, x, y, bc->width, bc->height, dchild);
|
2015-04-22 16:04:30 -05:00
|
|
|
if (b->vertical)
|
2015-05-07 21:52:27 -05:00
|
|
|
y += bc->height + ypadding;
|
2015-04-22 16:04:30 -05:00
|
|
|
else
|
2015-05-07 21:52:27 -05:00
|
|
|
x += bc->width + xpadding;
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
2015-05-18 11:04:52 -05:00
|
|
|
uiFreeSizing(dchild);
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
|
2015-05-30 16:33:37 -05:00
|
|
|
static int boxHasTabStops(uiControl *c)
|
|
|
|
{
|
|
|
|
struct box *b = (struct box *) c;
|
|
|
|
struct boxControl *bc;
|
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < b->controls->len; i++) {
|
|
|
|
bc = ptrArrayIndex(b->controls, struct boxControl *, i);
|
|
|
|
if (uiControlHasTabStops(bc->c))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void boxContainerUpdateState(uiControl *c)
|
2015-05-15 23:44:24 -05:00
|
|
|
{
|
|
|
|
struct box *b = (struct box *) c;
|
|
|
|
struct boxControl *bc;
|
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < b->controls->len; i++) {
|
|
|
|
bc = ptrArrayIndex(b->controls, struct boxControl *, i);
|
2015-05-30 16:33:37 -05:00
|
|
|
uiControlUpdateState(bc->c);
|
2015-05-15 23:44:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 16:04:30 -05:00
|
|
|
static void boxAppend(uiBox *ss, uiControl *c, int stretchy)
|
|
|
|
{
|
2015-05-04 14:37:03 -05:00
|
|
|
struct box *b = (struct box *) ss;
|
2015-05-07 21:36:32 -05:00
|
|
|
struct boxControl *bc;
|
2015-04-22 16:04:30 -05:00
|
|
|
|
2015-05-07 21:36:32 -05:00
|
|
|
bc = uiNew(struct boxControl);
|
2015-05-07 21:52:27 -05:00
|
|
|
bc->c = c;
|
|
|
|
bc->stretchy = stretchy;
|
2015-05-16 00:45:20 -05:00
|
|
|
uiControlSetParent(bc->c, uiControl(b));
|
2015-05-07 21:52:27 -05:00
|
|
|
ptrArrayAppend(b->controls, bc);
|
2015-05-15 23:44:24 -05:00
|
|
|
uiControlQueueResize(uiControl(b));
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void boxDelete(uiBox *ss, uintmax_t index)
|
|
|
|
{
|
2015-05-04 14:37:03 -05:00
|
|
|
struct box *b = (struct box *) ss;
|
2015-05-07 21:52:27 -05:00
|
|
|
struct boxControl *bc;
|
2015-04-22 16:04:30 -05:00
|
|
|
uiControl *removed;
|
|
|
|
|
2015-05-07 21:52:27 -05:00
|
|
|
// TODO rearrange this
|
|
|
|
// TODO sync call order with that of Destroy()
|
|
|
|
bc = ptrArrayIndex(b->controls, struct boxControl *, index);
|
|
|
|
removed = bc->c;
|
|
|
|
ptrArrayDelete(b->controls, index);
|
2015-04-26 22:12:38 -05:00
|
|
|
uiControlSetParent(removed, NULL);
|
2015-05-07 21:52:27 -05:00
|
|
|
uiFree(bc);
|
2015-05-15 23:44:24 -05:00
|
|
|
uiControlQueueResize(uiControl(b));
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int boxPadded(uiBox *ss)
|
|
|
|
{
|
2015-05-04 14:37:03 -05:00
|
|
|
struct box *b = (struct box *) ss;
|
2015-04-22 16:04:30 -05:00
|
|
|
|
|
|
|
return b->padded;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void boxSetPadded(uiBox *ss, int padded)
|
|
|
|
{
|
2015-05-04 14:37:03 -05:00
|
|
|
struct box *b = (struct box *) ss;
|
2015-04-22 16:04:30 -05:00
|
|
|
|
|
|
|
b->padded = padded;
|
2015-05-15 23:44:24 -05:00
|
|
|
uiControlQueueResize(uiControl(b));
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uiBox *uiNewHorizontalBox(void)
|
|
|
|
{
|
2015-05-04 14:37:03 -05:00
|
|
|
struct box *b;
|
2015-04-22 16:04:30 -05:00
|
|
|
|
2015-05-30 10:44:37 -05:00
|
|
|
b = (struct box *) uiNewControl(uiTypeBox());
|
2015-04-22 16:04:30 -05:00
|
|
|
|
2015-05-29 19:40:54 -05:00
|
|
|
b->handle = uiMakeContainer(uiControl(b));
|
2015-04-26 22:01:00 -05:00
|
|
|
|
2015-05-07 21:52:27 -05:00
|
|
|
b->controls = newPtrArray();
|
|
|
|
|
2015-05-29 19:40:54 -05:00
|
|
|
uiControl(b)->Handle = boxHandle;
|
2015-04-22 16:04:30 -05:00
|
|
|
uiControl(b)->PreferredSize = boxPreferredSize;
|
2015-05-16 00:45:20 -05:00
|
|
|
b->baseResize = uiControl(b)->Resize;
|
2015-05-15 23:44:24 -05:00
|
|
|
uiControl(b)->Resize = boxResize;
|
2015-05-30 16:33:37 -05:00
|
|
|
uiControl(b)->HasTabStops = boxHasTabStops;
|
2015-05-29 19:40:54 -05:00
|
|
|
b->baseCommitDestroy = uiControl(b)->CommitDestroy;
|
|
|
|
uiControl(b)->CommitDestroy = boxCommitDestroy;
|
2015-05-30 16:33:37 -05:00
|
|
|
uiControl(b)->ContainerUpdateState = boxContainerUpdateState;
|
2015-04-26 23:57:44 -05:00
|
|
|
|
2015-04-22 16:04:30 -05:00
|
|
|
uiBox(b)->Append = boxAppend;
|
|
|
|
uiBox(b)->Delete = boxDelete;
|
|
|
|
uiBox(b)->Padded = boxPadded;
|
|
|
|
uiBox(b)->SetPadded = boxSetPadded;
|
|
|
|
|
|
|
|
return uiBox(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
uiBox *uiNewVerticalBox(void)
|
|
|
|
{
|
2015-04-26 22:01:00 -05:00
|
|
|
uiBox *bb;
|
2015-05-04 14:37:03 -05:00
|
|
|
struct box *b;
|
2015-04-22 16:04:30 -05:00
|
|
|
|
2015-04-26 22:01:00 -05:00
|
|
|
bb = uiNewHorizontalBox();
|
2015-05-04 14:37:03 -05:00
|
|
|
b = (struct box *) bb;
|
2015-04-22 16:04:30 -05:00
|
|
|
b->vertical = 1;
|
2015-04-27 21:17:15 -05:00
|
|
|
return bb;
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|