From 79f0650fee09a78e3466e5126757b873c191db7d Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Mon, 13 Apr 2015 13:43:29 -0400 Subject: [PATCH] Added uiStackRemove(), the first removal function in package ui! That took long enough... Now I can piece together all the control and parent logic... --- stack.c | 16 ++++++++++++++++ test.c | 36 +++++++++++++++++++++++++++++++++++- ui.h | 1 + 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/stack.c b/stack.c index 1a08ee05..41a67d61 100644 --- a/stack.c +++ b/stack.c @@ -367,6 +367,22 @@ void uiStackAdd(uiControl *st, uiControl *c, int stretchy) uiParentUpdate(s->parent); } +void uiStackRemove(uiControl *st, uintptr_t index) +{ + stack *s = (stack *) (st->data); + uiControl *removed; + uintmax_t nAfter; + + removed = s->controls[index].c; + nAfter = s->len - index - 1; + memmove(&(s->controls[index + 1]), &(s->controls[index]), nAfter * sizeof (stackControl)); + s->len--; + if (s->parent != NULL) { + uiControlRemoveParent(removed); + uiParentUpdate(s->parent); + } +} + int uiStackPadded(uiControl *c) { stack *s = (stack *) (c->data); diff --git a/test.c b/test.c index 5acfdc40..978cb551 100644 --- a/test.c +++ b/test.c @@ -67,7 +67,7 @@ static void setCheckboxText(uiControl *b, void *data) } uiWindow *w; -#define nStacks 9 +#define nStacks 11 uiControl *stacks[nStacks]; uiControl *spaced; @@ -148,6 +148,22 @@ static void setLabelText(uiControl *b, void *data) uiFreeText(text); } +uiControl *firstStack; +uiControl *secondStack; +uiControl *movingLabel; + +static void moveToFirst(uiControl *c, void *data) +{ + uiStackRemove(secondStack, 1); + uiStackAdd(firstStack, movingLabel, 1); +} + +static void moveToSecond(uiControl *c, void *data) +{ + uiStackRemove(firstStack, 1); + uiStackAdd(secondStack, movingLabel, 1); +} + int main(int argc, char *argv[]) { uiInitOptions o; @@ -289,6 +305,24 @@ int main(int argc, char *argv[]) uiTabAddPage(tab, "Page 2", stacks[i]); i++; + stacks[i] = uiNewHorizontalStack(); + firstStack = stacks[i]; + getButton = uiNewButton("Move Here"); + uiButtonOnClicked(getButton, moveToFirst, NULL); + uiStackAdd(stacks[i], getButton, 0); + movingLabel = uiNewLabel("This label moves!"); + uiStackAdd(stacks[i], movingLabel, 1); + uiStackAdd(stacks[page2stack], stacks[i], 0);; + i++; + + stacks[i] = uiNewHorizontalStack(); + secondStack = stacks[i]; + getButton = uiNewButton("Move Here"); + uiButtonOnClicked(getButton, moveToSecond, NULL); + uiStackAdd(stacks[i], getButton, 0); + uiStackAdd(stacks[page2stack], stacks[i], 0); + i++; + if (i != nStacks) { fprintf(stderr, "forgot to update nStacks (expected %d)\n", i); return 1; diff --git a/ui.h b/ui.h index 3120d6d9..f82e8005 100644 --- a/ui.h +++ b/ui.h @@ -131,6 +131,7 @@ void uiButtonOnClicked(uiControl *, void (*)(uiControl *, void *), void *); uiControl *uiNewHorizontalStack(void); uiControl *uiNewVerticalStack(void); void uiStackAdd(uiControl *, uiControl *, int); +void uiStackRemove(uiControl *, uintmax_t); int uiStackPadded(uiControl *); void uiStackSetPadded(uiControl *, int);