From fd9f6cea6a603954619ef880ce6d2f5c63daf4b9 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 14 May 2016 22:09:02 -0400 Subject: [PATCH] Allowed uiGroups and uiTabs to have no and NULL controls with defined behavior on OS X. Actually added the test code this time. --- darwin/tab.m | 44 +++++++++++++++++++++++++----------------- test/page11.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 17 deletions(-) create mode 100644 test/page11.c diff --git a/darwin/tab.m b/darwin/tab.m index 0e7a0d19..a98bf0fd 100644 --- a/darwin/tab.m +++ b/darwin/tab.m @@ -58,6 +58,7 @@ struct uiTab { { [self removeChildConstraints]; if (self.c == NULL) + // TODO make sure we need the margins return; singleChildConstraintsEstablish(&(self->constraints), self->view, [self childView], @@ -97,9 +98,11 @@ static void uiTabDestroy(uiControl *c) // then destroy all the children for (page in t->pages) { [page removeChildConstraints]; - uiControlSetParent(page.c, NULL); - uiDarwinControlSetSuperview(uiDarwinControl(page.c), nil); - uiControlDestroy(page.c); + if (page.c != NULL) { + uiControlSetParent(page.c, NULL); + uiDarwinControlSetSuperview(uiDarwinControl(page.c), nil); + uiControlDestroy(page.c); + } } // and finally destroy ourselves [t->pages release]; @@ -187,23 +190,26 @@ void uiTabInsertAt(uiTab *t, const char *name, uintmax_t n, uiControl *child) NSTabViewItem *i; NSObject *pageID; - uiControlSetParent(child, uiControl(t)); - view = [[NSView alloc] initWithFrame:NSZeroRect]; - // TODO if we turn off the autoresizing mask, nothing shows up; didn't this get documented somewhere? - uiDarwinControlSetSuperview(uiDarwinControl(child), view); - uiDarwinControlSyncEnableState(uiDarwinControl(child), uiControlEnabledToUser(uiControl(t))); + // don't turn off the autoresizing mask on view; if we do, nothing shows up + if (child != NULL) { + uiControlSetParent(child, uiControl(t)); + uiDarwinControlSetSuperview(uiDarwinControl(child), view); + uiDarwinControlSyncEnableState(uiDarwinControl(child), uiControlEnabledToUser(uiControl(t))); + } // the documentation says these can be nil but the headers say these must not be; let's be safe and make them non-nil anyway pageID = [NSObject new]; page = [[tabPage alloc] initWithView:view pageID:pageID]; page.c = child; - // don't hug, just in case we're a stretchy tab - page.oldHorzHuggingPri = uiDarwinControlHuggingPriority(uiDarwinControl(page.c), NSLayoutConstraintOrientationHorizontal); - page.oldVertHuggingPri = uiDarwinControlHuggingPriority(uiDarwinControl(page.c), NSLayoutConstraintOrientationVertical); - uiDarwinControlSetHuggingPriority(uiDarwinControl(page.c), NSLayoutPriorityDefaultLow, NSLayoutConstraintOrientationHorizontal); - uiDarwinControlSetHuggingPriority(uiDarwinControl(page.c), NSLayoutPriorityDefaultLow, NSLayoutConstraintOrientationVertical); + if (page.c != NULL) { + // don't hug, just in case we're a stretchy tab + page.oldHorzHuggingPri = uiDarwinControlHuggingPriority(uiDarwinControl(page.c), NSLayoutConstraintOrientationHorizontal); + page.oldVertHuggingPri = uiDarwinControlHuggingPriority(uiDarwinControl(page.c), NSLayoutConstraintOrientationVertical); + uiDarwinControlSetHuggingPriority(uiDarwinControl(page.c), NSLayoutPriorityDefaultLow, NSLayoutConstraintOrientationHorizontal); + uiDarwinControlSetHuggingPriority(uiDarwinControl(page.c), NSLayoutPriorityDefaultLow, NSLayoutConstraintOrientationVertical); + } [t->pages insertObject:page atIndex:n]; [page release]; // no need for initial reference @@ -228,15 +234,19 @@ void uiTabDelete(uiTab *t, uintmax_t n) page = (tabPage *) [t->pages objectAtIndex:n]; - uiDarwinControlSetHuggingPriority(uiDarwinControl(page.c), page.oldHorzHuggingPri, NSLayoutConstraintOrientationHorizontal); - uiDarwinControlSetHuggingPriority(uiDarwinControl(page.c), page.oldVertHuggingPri, NSLayoutConstraintOrientationVertical); + if (page.c != NULL) { + uiDarwinControlSetHuggingPriority(uiDarwinControl(page.c), page.oldHorzHuggingPri, NSLayoutConstraintOrientationHorizontal); + uiDarwinControlSetHuggingPriority(uiDarwinControl(page.c), page.oldVertHuggingPri, NSLayoutConstraintOrientationVertical); + } child = page.c; [page removeChildConstraints]; [t->pages removeObjectAtIndex:n]; - uiControlSetParent(child, NULL); - uiDarwinControlSetSuperview(uiDarwinControl(child), nil); + if (child != NULL) { + uiControlSetParent(child, NULL); + uiDarwinControlSetSuperview(uiDarwinControl(child), nil); + } i = [t->tabview tabViewItemAtIndex:n]; [t->tabview removeTabViewItem:i]; diff --git a/test/page11.c b/test/page11.c new file mode 100644 index 00000000..cbe3d6cd --- /dev/null +++ b/test/page11.c @@ -0,0 +1,53 @@ +// 14 may 2016 +#include "test.h" + +// TODO add a test for childless windows + +static uiGroup *newg(const char *n, int s) +{ + uiGroup *g; + + g = uiNewGroup(n); + if (s) + uiGroupSetChild(g, NULL); + return g; +} + +static uiTab *newt(int tt) +{ + uiTab *t; + + t = uiNewTab(); + if (tt) + uiTabAppend(t, "Test", NULL); + return t; +} + +uiBox *makePage11(void) +{ + uiBox *page11; + uiBox *ns; + uiBox *s; + + page11 = newHorizontalBox(); + + ns = newVerticalBox(); + uiBoxAppend(ns, uiControl(newg("", 0)), 0); + uiBoxAppend(ns, uiControl(newg("", 1)), 0); + uiBoxAppend(ns, uiControl(newg("Group", 0)), 0); + uiBoxAppend(ns, uiControl(newg("Group", 1)), 0); + uiBoxAppend(ns, uiControl(newt(0)), 0); + uiBoxAppend(ns, uiControl(newt(1)), 0); + uiBoxAppend(page11, uiControl(ns), 1); + + s = newVerticalBox(); + uiBoxAppend(s, uiControl(newg("", 0)), 1); + uiBoxAppend(s, uiControl(newg("", 1)), 1); + uiBoxAppend(s, uiControl(newg("Group", 0)), 1); + uiBoxAppend(s, uiControl(newg("Group", 1)), 1); + uiBoxAppend(s, uiControl(newt(0)), 1); + uiBoxAppend(s, uiControl(newt(1)), 1); + uiBoxAppend(page11, uiControl(s), 1); + + return page11; +}