Skip to content

Commit

Permalink
Merge pull request #1 from andlabs/master
Browse files Browse the repository at this point in the history
Merge upstream changes
  • Loading branch information
emersion authored Jun 15, 2016
2 parents cb81518 + c8dd546 commit 578ff39
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 75 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ This README is being written.<br>

* **14 June 2016**
* uiDarwinControl now has a `ChildVisibilityChanged()` method and a corresponding `NotifyVisibilityChanged()` function that is called by the default show/hide handlers. This is used to make visibility changes work on OS X; uiBox, uiForm, and uiGrid all respect these now.
* The same has been done on the Windows side as well.
* Hiding and showing controls and padding calculations are now correct on Windows at long last.
* Hiding a control in a uiForm now hides its label on all platforms.

* **13 June 2016**
* `intmax_t` and `uintmax_t` are no longer used for libui API functions; now we use `int`. This should make things much easier for bindings. `int` should be at least 32 bits wide; this should be sufficient for all but the most extreme cases.
Expand Down
5 changes: 4 additions & 1 deletion darwin/grid.m
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ - (void)establishOurConstraints
int firstx, firsty;
BOOL *hexpand, *vexpand;
BOOL doit;
BOOL onlyEmptyAndSpanning;

[self removeOurConstraints];
if ([self->children count] == 0)
Expand All @@ -158,6 +159,7 @@ - (void)establishOurConstraints
// ignore hidden controls
first = YES;
for (gc in self->children) {
// this bit is important: it ensures row ymin and column xmin have at least one cell to draw, so the onlyEmptyAndSpanning logic below will never run on those rows
if (!uiControlVisible(gc.c))
continue;
if (first) {
Expand All @@ -177,6 +179,8 @@ - (void)establishOurConstraints
if (ymax < (gc.top + gc.yspan))
ymax = gc.top + gc.yspan;
}
if (first != NO) // the entire grid is hidden; do nothing
return;
xcount = xmax - xmin;
ycount = ymax - ymin;

Expand Down Expand Up @@ -204,7 +208,6 @@ - (void)establishOurConstraints
}

// if a row or column only contains emptys and spanning cells of a opposite-direction spannings, remove it by duplicating the previous row or column
BOOL onlyEmptyAndSpanning;
for (y = 0; y < ycount; y++) {
onlyEmptyAndSpanning = YES;
for (x = 0; x < xcount; x++)
Expand Down
1 change: 1 addition & 0 deletions doc/form
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hiding a control also hides its label
16 changes: 15 additions & 1 deletion ui_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct uiWindowsControl {
void (*MinimumSizeChanged)(uiWindowsControl *);
void (*LayoutRect)(uiWindowsControl *c, RECT *r);
void (*AssignControlIDZOrder)(uiWindowsControl *, LONG_PTR *, HWND *);
void (*ChildVisibilityChanged)(uiWindowsControl *);
};
#define uiWindowsControl(this) ((uiWindowsControl *) (this))
// TODO document
Expand All @@ -35,6 +36,7 @@ _UI_EXTERN void uiWindowsControlMinimumSize(uiWindowsControl *, int *, int *);
_UI_EXTERN void uiWindowsControlMinimumSizeChanged(uiWindowsControl *);
_UI_EXTERN void uiWindowsControlLayoutRect(uiWindowsControl *, RECT *);
_UI_EXTERN void uiWindowsControlAssignControlIDZOrder(uiWindowsControl *, LONG_PTR *, HWND *);
_UI_EXTERN void uiWindowsControlChildVisibilityChanged(uiWindowsControl *);

// TODO document
#define uiWindowsControlDefaultDestroy(type) \
Expand Down Expand Up @@ -74,12 +76,14 @@ _UI_EXTERN void uiWindowsControlAssignControlIDZOrder(uiWindowsControl *, LONG_P
{ \
uiWindowsControl(c)->visible = 1; \
ShowWindow(type(c)->hwnd, SW_SHOW); \
uiWindowsControlNotifyVisibilityChanged(uiWindowsControl(c)); \
}
#define uiWindowsControlDefaultHide(type) \
static void type ## Hide(uiControl *c) \
{ \
uiWindowsControl(c)->visible = 0; \
ShowWindow(type(c)->hwnd, SW_HIDE); \
uiWindowsControlNotifyVisibilityChanged(uiWindowsControl(c)); \
}
#define uiWindowsControlDefaultEnabled(type) \
static int type ## Enabled(uiControl *c) \
Expand Down Expand Up @@ -131,6 +135,11 @@ _UI_EXTERN void uiWindowsControlAssignControlIDZOrder(uiWindowsControl *, LONG_P
{ \
uiWindowsEnsureAssignControlIDZOrder(type(c)->hwnd, controlID, insertAfter); \
}
#define uiWindowsControlDefaultChildVisibilityChanged(type) \
static void type ## ChildVisibilityChanged(uiWindowsControl *c) \
{ \
/* do nothing */ \
}

#define uiWindowsControlAllDefaultsExceptDestroy(type) \
uiWindowsControlDefaultHandle(type) \
Expand All @@ -147,7 +156,8 @@ _UI_EXTERN void uiWindowsControlAssignControlIDZOrder(uiWindowsControl *, LONG_P
uiWindowsControlDefaultSetParentHWND(type) \
uiWindowsControlDefaultMinimumSizeChanged(type) \
uiWindowsControlDefaultLayoutRect(type) \
uiWindowsControlDefaultAssignControlIDZOrder(type)
uiWindowsControlDefaultAssignControlIDZOrder(type) \
uiWindowsControlDefaultChildVisibilityChanged(type)

#define uiWindowsControlAllDefaults(type) \
uiWindowsControlDefaultDestroy(type) \
Expand All @@ -173,6 +183,7 @@ _UI_EXTERN void uiWindowsControlAssignControlIDZOrder(uiWindowsControl *, LONG_P
uiWindowsControl(var)->MinimumSizeChanged = type ## MinimumSizeChanged; \
uiWindowsControl(var)->LayoutRect = type ## LayoutRect; \
uiWindowsControl(var)->AssignControlIDZOrder = type ## AssignControlIDZOrder; \
uiWindowsControl(var)->ChildVisibilityChanged = type ## ChildVisibilityChanged; \
uiWindowsControl(var)->visible = 1; \
uiWindowsControl(var)->enabled = 1;
// TODO document
Expand Down Expand Up @@ -246,6 +257,9 @@ _UI_EXTERN void uiWindowsControlAssignSoleControlIDZOrder(uiWindowsControl *);
// TODO document
_UI_EXTERN BOOL uiWindowsShouldStopSyncEnableState(uiWindowsControl *c, int enabled);

// TODO document
_UI_EXTERN void uiWindowsControlNotifyVisibilityChanged(uiWindowsControl *c);

#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 5 additions & 1 deletion unix/form.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct formChild {
GtkAlign oldhalign;
gboolean oldvexpand;
GtkAlign oldvalign;
GBinding *labelBinding;
};

struct uiForm {
Expand Down Expand Up @@ -82,7 +83,10 @@ void uiFormAppend(uiForm *f, const char *label, uiControl *c, int stretchy)
gtk_grid_attach(f->grid, fc.label,
0, row,
1, 1);
gtk_widget_show_all(fc.label);
// and make them share visibility so if the control is hidden, so is its label
fc.labelBinding = g_object_bind_property(GTK_WIDGET(uiControlHandle(fc.c)), "visible",
fc.label, "visible",
G_BINDING_SYNC_CREATE);

uiControlSetParent(fc.c, uiControl(f));
uiUnixControlSetContainer(uiUnixControl(fc.c), f->container, FALSE);
Expand Down
67 changes: 42 additions & 25 deletions windows/box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ static void boxRelayout(uiBox *b)
int stretchywid, stretchyht;
int i;
int minimumWidth, minimumHeight;
int nVisible;
uiWindowsSizing *d;

if (b->controls->size() == 0)
Expand All @@ -51,21 +52,16 @@ static void boxRelayout(uiBox *b)
// -1) get this Box's padding
boxPadding(b, &xpadding, &ypadding);

// 0) inset the available rect by the needed padding
// TODO this is incorrect if any controls are hidden
if (b->vertical)
height -= (b->controls->size() - 1) * ypadding;
else
width -= (b->controls->size() - 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;
nVisible = 0;
for (struct boxChild &bc : *(b->controls)) {
if (!uiControlVisible(bc.c))
continue;
nVisible++;
if (bc.stretchy) {
nStretchy++;
continue;
Expand All @@ -81,24 +77,35 @@ static void boxRelayout(uiBox *b)
stretchywid -= minimumWidth;
}
}
if (nVisible == 0) // nothing to do
return;

// 2) now inset the available rect by the needed padding
if (b->vertical) {
height -= (nVisible - 1) * ypadding;
stretchyht -= (nVisible - 1) * ypadding;
} else {
width -= (nVisible - 1) * xpadding;
stretchywid -= (nVisible - 1) * xpadding;
}

// 2) now get the size of stretchy controls
if (nStretchy != 0)
// 3) now get the size of stretchy controls
if (nStretchy != 0) {
if (b->vertical)
stretchyht /= nStretchy;
else
stretchywid /= nStretchy;
// TODO put this in the above if
for (struct boxChild &bc : *(b->controls)) {
if (!uiControlVisible(bc.c))
continue;
if (bc.stretchy) {
bc.width = stretchywid;
bc.height = stretchyht;
for (struct boxChild &bc : *(b->controls)) {
if (!uiControlVisible(bc.c))
continue;
if (bc.stretchy) {
bc.width = stretchywid;
bc.height = stretchyht;
}
}
}

// 3) now we can position controls
// 4) now we can position controls
// first, make relative to the top-left corner of the container
x = 0;
y = 0;
Expand Down Expand Up @@ -159,6 +166,7 @@ static void uiBoxMinimumSize(uiWindowsControl *c, int *width, int *height)
int maxStretchyWidth, maxStretchyHeight;
int i;
int minimumWidth, minimumHeight;
int nVisible;
uiWindowsSizing sizing;

*width = 0;
Expand All @@ -169,21 +177,16 @@ static void uiBoxMinimumSize(uiWindowsControl *c, int *width, int *height)
// 0) get this Box's padding
boxPadding(b, &xpadding, &ypadding);

// 1) initialize the desired rect with the needed padding
// TODO this is wrong if any controls are hidden
if (b->vertical)
*height = (b->controls->size() - 1) * ypadding;
else
*width = (b->controls->size() - 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
// 1) 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;
nVisible = 0;
for (const struct boxChild &bc : *(b->controls)) {
if (!uiControlVisible(bc.c))
continue;
nVisible++;
uiWindowsControlMinimumSize(uiWindowsControl(bc.c), &minimumWidth, &minimumHeight);
if (bc.stretchy) {
nStretchy++;
Expand All @@ -204,6 +207,14 @@ static void uiBoxMinimumSize(uiWindowsControl *c, int *width, int *height)
*height = minimumHeight;
}
}
if (nVisible == 0) // just return 0x0
return;

// 2) now outset the desired rect with the needed padding
if (b->vertical)
*height += (nVisible - 1) * ypadding;
else
*width += (nVisible - 1) * xpadding;

// 3) and now we can add in stretchy controls
if (b->vertical)
Expand All @@ -226,6 +237,12 @@ static void uiBoxMinimumSizeChanged(uiWindowsControl *c)
uiWindowsControlDefaultLayoutRect(uiBox)
uiWindowsControlDefaultAssignControlIDZOrder(uiBox)

static void uiBoxChildVisibilityChanged(uiWindowsControl *c)
{
// TODO eliminate the redundancy
uiWindowsControlMinimumSizeChanged(c);
}

static void boxArrangeChildren(uiBox *b)
{
LONG_PTR controlID;
Expand Down
13 changes: 13 additions & 0 deletions windows/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void uiWindowsControlMinimumSizeChanged(uiWindowsControl *c)
(*(c->MinimumSizeChanged))(c);
}

// TODO get rid of this
void uiWindowsControlLayoutRect(uiWindowsControl *c, RECT *r)
{
(*(c->LayoutRect))(c, r);
Expand All @@ -31,6 +32,11 @@ void uiWindowsControlAssignControlIDZOrder(uiWindowsControl *c, LONG_PTR *contro
(*(c->AssignControlIDZOrder))(c, controlID, insertAfter);
}

void uiWindowsControlChildVisibilityChanged(uiWindowsControl *c)
{
(*(c->ChildVisibilityChanged))(c);
}

HWND uiWindowsEnsureCreateControlHWND(DWORD dwExStyle, LPCWSTR lpClassName, LPCWSTR lpWindowName, DWORD dwStyle, HINSTANCE hInstance, LPVOID lpParam, BOOL useStandardControlFont)
{
HWND hwnd;
Expand Down Expand Up @@ -106,3 +112,10 @@ void uiWindowsControlContinueMinimumSizeChanged(uiWindowsControl *c)
if (parent != NULL)
uiWindowsControlMinimumSizeChanged(uiWindowsControl(parent));
}

// TODO rename this nad the OS X this and hugging ones to NotifyChild
void uiWindowsControlNotifyVisibilityChanged(uiWindowsControl *c)
{
// TODO we really need to figure this out; the duplication is a mess
uiWindowsControlContinueMinimumSizeChanged(c);
}
Loading

0 comments on commit 578ff39

Please # to comment.