2015-04-22 16:04:30 -05:00
|
|
|
// 7 april 2015
|
2016-04-23 10:22:46 -05:00
|
|
|
#include "uipriv_windows.hpp"
|
|
|
|
|
|
|
|
// TODO C++-ize
|
2015-04-22 16:04:30 -05:00
|
|
|
|
2015-08-31 16:50:23 -05:00
|
|
|
struct uiBox {
|
2015-08-31 16:05:50 -05:00
|
|
|
uiWindowsControl c;
|
|
|
|
HWND hwnd;
|
2015-05-07 21:52:27 -05:00
|
|
|
struct ptrArray *controls;
|
2015-04-22 16:04:30 -05:00
|
|
|
int vertical;
|
|
|
|
int padded;
|
|
|
|
};
|
|
|
|
|
2015-08-31 16:05:50 -05:00
|
|
|
#define ctrlStretchy(child) childFlag(child)
|
|
|
|
#define ctrlSetStretchy(child, stretchy) childSetFlag(child, stretchy)
|
|
|
|
// both used by resize(); preallocated to save time and reduce risk of failure
|
|
|
|
#define ctrlWidth(child) childIntmax(child, 0)
|
|
|
|
#define ctrlSetWidth(child, w) childSetIntmax(child, 0, w)
|
|
|
|
#define ctrlHeight(child) childIntmax(child, 1)
|
|
|
|
#define ctrlSetHeight(child, h) childSetIntmax(child, 1, h)
|
|
|
|
|
|
|
|
static void onDestroy(uiBox *);
|
2015-04-22 16:04:30 -05:00
|
|
|
|
2015-08-31 16:05:50 -05:00
|
|
|
uiWindowsDefineControlWithOnDestroy(
|
|
|
|
uiBox, // type name
|
2016-04-23 15:52:53 -05:00
|
|
|
onDestroy(me); // on destroy
|
2015-08-31 16:05:50 -05:00
|
|
|
)
|
2015-05-29 21:44:48 -05:00
|
|
|
|
2015-08-31 16:05:50 -05:00
|
|
|
static void onDestroy(uiBox *b)
|
2015-04-22 16:04:30 -05:00
|
|
|
{
|
2015-08-31 16:05:50 -05:00
|
|
|
struct child *bc;
|
2015-04-22 16:04:30 -05:00
|
|
|
|
2015-05-07 21:52:27 -05:00
|
|
|
while (b->controls->len != 0) {
|
2015-08-31 16:50:23 -05:00
|
|
|
bc = ptrArrayIndex(b->controls, struct child *, 0);
|
2015-05-07 21:52:27 -05:00
|
|
|
ptrArrayDelete(b->controls, 0);
|
2015-08-31 16:05:50 -05:00
|
|
|
childDestroy(bc);
|
2015-04-22 16:54:05 -05:00
|
|
|
}
|
2015-05-07 21:52:27 -05:00
|
|
|
ptrArrayDestroy(b->controls);
|
2015-05-29 19:40:54 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:50:23 -05:00
|
|
|
static void minimumSize(uiWindowsControl *c, uiWindowsSizing *d, intmax_t *width, intmax_t *height)
|
2015-05-29 19:40:54 -05:00
|
|
|
{
|
2015-08-31 16:05:50 -05:00
|
|
|
uiBox *b = uiBox(c);
|
|
|
|
struct child *bc;
|
2015-04-22 16:04:30 -05:00
|
|
|
int xpadding, ypadding;
|
|
|
|
uintmax_t nStretchy;
|
2015-09-01 08:18:37 -05:00
|
|
|
// these two contain the largest minimum width and height of all stretchy controls in the box
|
|
|
|
// all stretchy controls will use this value to determine the final minimum size
|
2015-04-22 16:04:30 -05:00
|
|
|
intmax_t maxStretchyWidth, maxStretchyHeight;
|
|
|
|
uintmax_t i;
|
2015-09-01 08:18:37 -05:00
|
|
|
intmax_t minimumWidth, minimumHeight;
|
|
|
|
uiWindowsSizing *dself;
|
2015-04-22 16:04:30 -05:00
|
|
|
|
|
|
|
*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;
|
|
|
|
|
2015-09-01 08:18:37 -05:00
|
|
|
dself = uiWindowsNewSizing(b->hwnd);
|
|
|
|
|
2015-04-22 16:04:30 -05:00
|
|
|
// 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++) {
|
2015-08-31 16:50:23 -05:00
|
|
|
bc = ptrArrayIndex(b->controls, struct child *, i);
|
2015-09-01 15:44:09 -05:00
|
|
|
if (!childVisible(bc))
|
|
|
|
continue;
|
2015-09-01 08:18:37 -05:00
|
|
|
childMinimumSize(bc, dself, &minimumWidth, &minimumHeight);
|
|
|
|
if (ctrlStretchy(bc)) {
|
2015-04-22 16:04:30 -05:00
|
|
|
nStretchy++;
|
2015-09-01 08:18:37 -05:00
|
|
|
if (maxStretchyWidth < minimumWidth)
|
|
|
|
maxStretchyWidth = minimumWidth;
|
|
|
|
if (maxStretchyHeight < minimumHeight)
|
|
|
|
maxStretchyHeight = minimumHeight;
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
if (b->vertical) {
|
2015-09-01 08:18:37 -05:00
|
|
|
if (*width < minimumWidth)
|
|
|
|
*width = minimumWidth;
|
|
|
|
if (!ctrlStretchy(bc))
|
|
|
|
*height += minimumHeight;
|
2015-04-22 16:04:30 -05:00
|
|
|
} else {
|
2015-09-01 08:18:37 -05:00
|
|
|
if (!ctrlStretchy(bc))
|
|
|
|
*width += minimumWidth;
|
|
|
|
if (*height < minimumHeight)
|
|
|
|
*height = minimumHeight;
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3) and now we can add in stretchy controls
|
|
|
|
if (b->vertical)
|
|
|
|
*height += nStretchy * maxStretchyHeight;
|
|
|
|
else
|
|
|
|
*width += nStretchy * maxStretchyWidth;
|
2015-09-01 08:18:37 -05:00
|
|
|
|
|
|
|
uiWindowsFreeSizing(dself);
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
|
2015-09-01 06:21:18 -05:00
|
|
|
static void boxRelayout(uiWindowsControl *c, intmax_t x, intmax_t y, intmax_t width, intmax_t height)
|
2015-04-22 16:04:30 -05:00
|
|
|
{
|
2015-09-01 08:18:37 -05:00
|
|
|
uiBox *b = uiBox(c);
|
2015-08-31 16:50:23 -05:00
|
|
|
struct child *bc;
|
2015-04-22 16:04:30 -05:00
|
|
|
int xpadding, ypadding;
|
|
|
|
uintmax_t nStretchy;
|
|
|
|
intmax_t stretchywid, stretchyht;
|
|
|
|
uintmax_t i;
|
2015-09-01 08:18:37 -05:00
|
|
|
intmax_t minimumWidth, minimumHeight;
|
|
|
|
uiWindowsSizing *d;
|
2015-04-22 16:04:30 -05:00
|
|
|
|
2016-04-23 20:38:51 -05:00
|
|
|
uiWindowsEnsureMoveWindowDuringResize(b->hwnd, x, y, width, height);
|
2015-05-15 23:44:24 -05:00
|
|
|
|
2015-05-07 21:52:27 -05:00
|
|
|
if (b->controls->len == 0)
|
2015-04-22 16:04:30 -05:00
|
|
|
return;
|
|
|
|
|
2015-09-01 08:18:37 -05:00
|
|
|
d = uiWindowsNewSizing(b->hwnd);
|
|
|
|
|
2015-04-22 16:04:30 -05:00
|
|
|
// -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++) {
|
2015-08-31 16:50:23 -05:00
|
|
|
bc = ptrArrayIndex(b->controls, struct child *, i);
|
2015-09-01 15:44:09 -05:00
|
|
|
if (!childVisible(bc))
|
|
|
|
continue;
|
2015-09-01 08:18:37 -05:00
|
|
|
if (ctrlStretchy(bc)) {
|
2015-04-22 16:04:30 -05:00
|
|
|
nStretchy++;
|
|
|
|
continue;
|
|
|
|
}
|
2015-09-01 08:18:37 -05:00
|
|
|
childMinimumSize(bc, d, &minimumWidth, &minimumHeight);
|
2015-04-22 16:04:30 -05:00
|
|
|
if (b->vertical) { // all controls have same width
|
2015-09-01 08:18:37 -05:00
|
|
|
ctrlSetWidth(bc, width);
|
|
|
|
ctrlSetHeight(bc, minimumHeight);
|
|
|
|
stretchyht -= minimumHeight;
|
2015-04-22 16:04:30 -05:00
|
|
|
} else { // all controls have same height
|
2015-09-01 08:18:37 -05:00
|
|
|
ctrlSetWidth(bc, minimumWidth);
|
|
|
|
ctrlSetHeight(bc, height);
|
|
|
|
stretchywid -= minimumWidth;
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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++) {
|
2015-08-31 16:50:23 -05:00
|
|
|
bc = ptrArrayIndex(b->controls, struct child *, i);
|
2015-09-01 15:44:09 -05:00
|
|
|
if (!childVisible(bc))
|
|
|
|
continue;
|
2015-09-01 08:18:37 -05:00
|
|
|
if (ctrlStretchy(bc)) {
|
|
|
|
ctrlSetWidth(bc, stretchywid);
|
|
|
|
ctrlSetHeight(bc, stretchyht);
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3) now we can position controls
|
2015-09-01 10:46:53 -05:00
|
|
|
// first, make relative to the top-left corner of the container
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
2015-05-07 21:52:27 -05:00
|
|
|
for (i = 0; i < b->controls->len; i++) {
|
2015-08-31 16:50:23 -05:00
|
|
|
bc = ptrArrayIndex(b->controls, struct child *, i);
|
2015-09-01 15:44:09 -05:00
|
|
|
if (!childVisible(bc))
|
|
|
|
continue;
|
2015-09-01 08:39:25 -05:00
|
|
|
childRelayout(bc, x, y, ctrlWidth(bc), ctrlHeight(bc));
|
2015-04-22 16:04:30 -05:00
|
|
|
if (b->vertical)
|
2015-09-01 08:18:37 -05:00
|
|
|
y += ctrlHeight(bc) + ypadding;
|
2015-04-22 16:04:30 -05:00
|
|
|
else
|
2015-09-01 08:18:37 -05:00
|
|
|
x += ctrlWidth(bc) + xpadding;
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
2015-09-01 08:18:37 -05:00
|
|
|
|
|
|
|
uiWindowsFreeSizing(d);
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
|
2015-05-30 16:33:37 -05:00
|
|
|
static void boxContainerUpdateState(uiControl *c)
|
2015-05-15 23:44:24 -05:00
|
|
|
{
|
2015-08-31 16:50:23 -05:00
|
|
|
uiBox *b = uiBox(c);
|
2015-08-31 16:05:50 -05:00
|
|
|
struct child *bc;
|
2015-05-15 23:44:24 -05:00
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < b->controls->len; i++) {
|
2015-08-31 16:05:50 -05:00
|
|
|
bc = ptrArrayIndex(b->controls, struct child *, i);
|
|
|
|
childUpdateState(bc);
|
2015-05-15 23:44:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 08:36:53 -05:00
|
|
|
static void redoControlIDsZOrder(uiBox *b)
|
2015-04-22 16:04:30 -05:00
|
|
|
{
|
2015-08-31 16:05:50 -05:00
|
|
|
struct child *bc;
|
2015-09-02 08:36:53 -05:00
|
|
|
LONG_PTR controlID;
|
|
|
|
HWND insertAfter;
|
2015-06-02 16:42:51 -05:00
|
|
|
uintmax_t i;
|
|
|
|
|
2015-09-02 08:36:53 -05:00
|
|
|
controlID = 100;
|
|
|
|
insertAfter = NULL;
|
|
|
|
for (i = 0; i < b->controls->len; i++) {
|
|
|
|
bc = ptrArrayIndex(b->controls, struct child *, i);
|
|
|
|
childAssignControlIDZOrder(bc, &controlID, &insertAfter);
|
2015-06-02 16:42:51 -05:00
|
|
|
}
|
2015-09-02 08:36:53 -05:00
|
|
|
}
|
|
|
|
|
2015-09-02 11:59:57 -05:00
|
|
|
static void boxArrangeChildrenControlIDsZOrder(uiWindowsControl *c)
|
|
|
|
{
|
|
|
|
uiBox *b = uiBox(c);
|
|
|
|
|
|
|
|
redoControlIDsZOrder(b);
|
|
|
|
}
|
|
|
|
|
2015-09-02 08:36:53 -05:00
|
|
|
void uiBoxAppend(uiBox *b, uiControl *c, int stretchy)
|
|
|
|
{
|
|
|
|
struct child *bc;
|
2015-04-22 16:04:30 -05:00
|
|
|
|
2015-08-31 16:50:23 -05:00
|
|
|
bc = newChild(c, uiControl(b), b->hwnd);
|
2015-08-31 16:05:50 -05:00
|
|
|
ctrlSetStretchy(bc, stretchy);
|
2015-05-07 21:52:27 -05:00
|
|
|
ptrArrayAppend(b->controls, bc);
|
2015-09-02 08:36:53 -05:00
|
|
|
redoControlIDsZOrder(b);
|
2015-09-01 06:21:18 -05:00
|
|
|
uiWindowsControlQueueRelayout(uiWindowsControl(b));
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:05:50 -05:00
|
|
|
void uiBoxDelete(uiBox *b, uintmax_t index)
|
2015-04-22 16:04:30 -05:00
|
|
|
{
|
2015-08-31 16:05:50 -05:00
|
|
|
struct child *bc;
|
2015-04-22 16:04:30 -05:00
|
|
|
|
2015-08-31 16:05:50 -05:00
|
|
|
bc = ptrArrayIndex(b->controls, struct child *, index);
|
2015-05-07 21:52:27 -05:00
|
|
|
ptrArrayDelete(b->controls, index);
|
2015-08-31 16:05:50 -05:00
|
|
|
childRemove(bc);
|
2015-09-02 08:36:53 -05:00
|
|
|
redoControlIDsZOrder(b);
|
2015-09-01 06:21:18 -05:00
|
|
|
uiWindowsControlQueueRelayout(uiWindowsControl(b));
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:05:50 -05:00
|
|
|
int uiBoxPadded(uiBox *b)
|
2015-04-22 16:04:30 -05:00
|
|
|
{
|
|
|
|
return b->padded;
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:05:50 -05:00
|
|
|
void uiBoxSetPadded(uiBox *b, int padded)
|
2015-04-22 16:04:30 -05:00
|
|
|
{
|
|
|
|
b->padded = padded;
|
2015-09-01 06:21:18 -05:00
|
|
|
uiWindowsControlQueueRelayout(uiWindowsControl(b));
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:05:50 -05:00
|
|
|
static uiBox *finishNewBox(int vertical)
|
2015-04-22 16:04:30 -05:00
|
|
|
{
|
2015-08-31 16:05:50 -05:00
|
|
|
uiBox *b;
|
2015-04-22 16:04:30 -05:00
|
|
|
|
2016-04-24 16:38:48 -05:00
|
|
|
b = (uiBox *) uiNewControl(uiBox);
|
2015-04-22 16:04:30 -05:00
|
|
|
|
2015-08-31 16:50:23 -05:00
|
|
|
b->hwnd = newContainer();
|
2015-04-26 22:01:00 -05:00
|
|
|
|
2015-08-31 16:05:50 -05:00
|
|
|
b->vertical = vertical;
|
2015-05-07 21:52:27 -05:00
|
|
|
b->controls = newPtrArray();
|
|
|
|
|
2015-08-31 16:05:50 -05:00
|
|
|
uiWindowsFinishNewControl(b, uiBox);
|
2015-09-01 06:21:18 -05:00
|
|
|
uiControl(b)->ContainerUpdateState = boxContainerUpdateState;
|
|
|
|
uiWindowsControl(b)->Relayout = boxRelayout;
|
2015-09-02 11:59:57 -05:00
|
|
|
uiWindowsControl(b)->ArrangeChildrenControlIDsZOrder = boxArrangeChildrenControlIDsZOrder;
|
2015-08-31 16:05:50 -05:00
|
|
|
|
|
|
|
return b;
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:05:50 -05:00
|
|
|
uiBox *uiNewHorizontalBox(void)
|
2015-04-22 16:04:30 -05:00
|
|
|
{
|
2015-08-31 16:05:50 -05:00
|
|
|
return finishNewBox(0);
|
|
|
|
}
|
2015-04-22 16:04:30 -05:00
|
|
|
|
2015-08-31 16:05:50 -05:00
|
|
|
uiBox *uiNewVerticalBox(void)
|
|
|
|
{
|
|
|
|
return finishNewBox(1);
|
2015-04-22 16:04:30 -05:00
|
|
|
}
|